/// <exception cref="DecoderException"></exception>
        public IDecodedAudioFrame GetDecodedFrame(AudioConversionParameters optionalAudioConversionParameters = null)
        {
            IntPtr outBufferPtr;
            int    dataSize;

            AudioFrameFormat format;

            int resultCode;

            if (optionalAudioConversionParameters == null ||
                (optionalAudioConversionParameters.OutSampleRate == 0 || optionalAudioConversionParameters.OutSampleRate == _currentFrameFormat.SampleRate) &&
                (optionalAudioConversionParameters.OutBitsPerSample == 0 || optionalAudioConversionParameters.OutBitsPerSample == _currentFrameFormat.BitPerSample) &&
                (optionalAudioConversionParameters.OutChannels == 0 || optionalAudioConversionParameters.OutChannels == _currentFrameFormat.Channels))
            {
                resultCode = FFmpegAudioPInvoke.GetDecodedFrame(_decoderHandle, out outBufferPtr, out dataSize);

                if (resultCode != 0)
                {
                    throw new DecoderException($"An error occurred while getting decoded audio frame, {_audioCodecId} codec, code: {resultCode}");
                }

                format = _currentFrameFormat;
            }
            else
            {
                if (_resamplerHandle == IntPtr.Zero)
                {
                    resultCode = FFmpegAudioPInvoke.CreateAudioResampler(_decoderHandle,
                                                                         optionalAudioConversionParameters.OutSampleRate, optionalAudioConversionParameters.OutBitsPerSample,
                                                                         optionalAudioConversionParameters.OutChannels, out _resamplerHandle);

                    if (resultCode != 0)
                    {
                        throw new DecoderException($"An error occurred while creating audio resampler, code: {resultCode}");
                    }
                }

                resultCode = FFmpegAudioPInvoke.ResampleDecodedFrame(_decoderHandle, _resamplerHandle, out outBufferPtr, out dataSize);

                if (resultCode != 0)
                {
                    throw new DecoderException($"An error occurred while converting audio frame, code: {resultCode}");
                }

                format = new AudioFrameFormat(optionalAudioConversionParameters.OutSampleRate != 0 ? optionalAudioConversionParameters.OutSampleRate : _currentFrameFormat.SampleRate,
                                              optionalAudioConversionParameters.OutBitsPerSample != 0 ? optionalAudioConversionParameters.OutBitsPerSample : _currentFrameFormat.BitPerSample,
                                              optionalAudioConversionParameters.OutChannels != 0 ? optionalAudioConversionParameters.OutChannels : _currentFrameFormat.Channels);
            }

            if (_decodedFrameBuffer.Length < dataSize)
            {
                _decodedFrameBuffer = new byte[dataSize];
            }

            Marshal.Copy(outBufferPtr, _decodedFrameBuffer, 0, dataSize);
            return(new DecodedAudioFrame(_currentRawFrameTimestamp, new ArraySegment <byte>(_decodedFrameBuffer, 0, dataSize), format));
        }
Exemple #2
0
        /// <exception cref="DecoderException"></exception>
        public unsafe IDecodedAudioFrame GetDecodedFrame(ArraySegment <byte> bufferSegment)
        {
            Debug.Assert(bufferSegment.Array != null, "bufferSegment.Array != null");

            fixed(byte *outByteSegmentPtr = &bufferSegment.Array[bufferSegment.Offset])
            {
                int resultCode = FFmpegAudioPInvoke.GetDecodedFrame(_decoderHandle, (IntPtr)outByteSegmentPtr);

                if (resultCode != 0)
                {
                    throw new DecoderException($"An error occurred while getting decoded audio frame, {_audioCodecId} codec, code: {resultCode}");
                }
            }

            return(new DecodedAudioFrame(_currentRawFrameTimestamp, bufferSegment, _currentFrameFormat));
        }