コード例 #1
0
        public override bool Decode(Packet packet, Frame outFrame)
        {
            if (!(outFrame is AudioFrame audioFrame))
            {
                throw new ArgumentException($"{nameof(outFrame)}必须是{nameof(AudioFrame)}类型且不为null。");
            }

            if (packet != null)
            {
                int gotPicture = 0;
                FF.avcodec_decode_audio4(codecContext, outFrame.frame, &gotPicture, packet.packet).CheckFFmpegCode("音频解码发生错误");
                if (gotPicture == 0)
                {
                    return(false);
                }

                if (stream != null)
                {
                    outFrame.presentTimestamp = new Timestamp(outFrame.frame->Pts, stream->TimeBase);
                }
                audioFrame.format = InFormat;
                if (resampler != null)
                {
                    resampler.InternalResample(audioFrame);                     // resample and update
                }
                else
                {
                    outFrame.UpdateFromNative();
                }
            }
            else if (resampler != null)
            {
                resampler.ResampleFinal(audioFrame);
                if (audioFrame.LineDataBytes == 0)
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
コード例 #2
0
        public override bool Encode(Frame frame, Packet outPacket)
        {
            if (frame != null)
            {
                if (!(frame is AudioFrame))
                {
                    throw new ArgumentException($"{nameof(frame)}必须是{nameof(AudioFrame)}类型", nameof(frame));
                }
                if (!(frame as AudioFrame).format.Equals(InFormat))
                {
                    throw new ArgumentException("输入帧的格式和编码器输入格式不同");
                }
            }

            if (resampler != null)
            {
                if (frame != null)
                {
                    resampler.Resample(frame as AudioFrame, tempFrame);
                    frame = tempFrame;
                }
                else
                {
                    resampler.ResampleFinal(tempFrame);
                    if (tempFrame.SampleCount > 0)
                    {
                        frame = tempFrame;
                    }
                }
            }

            encodeFrames = 0;
            outPacket.ReleaseNativeBuffer();
            int gotPicture = 0;

            if (frame != null)
            {
                try {
                    frame.SetupToNative();
                    frame.presentTimestamp = new Timestamp(inputFrames, new AVRational(1, OutFormat.SampleRate));
                    frame.presentTimestamp.Transform(codecContext->TimeBase);
                    frame.frame->Pts = frame.presentTimestamp.Value;
                    FF.avcodec_encode_audio2(codecContext, outPacket.packet, frame.frame, &gotPicture).CheckFFmpegCode("音频编码发生错误");
                } finally {
                    frame.ReleaseSetup();
                }
                encodeFrames = frame.frame->NbSamples;
                inputFrames += encodeFrames;
            }
            else
            {
                FF.avcodec_encode_audio2(codecContext, outPacket.packet, null, &gotPicture).CheckFFmpegCode("音频编码发生错误");
            }

            if (gotPicture != 0)
            {
                ConfigPakcet(outPacket);
                return(true);
            }
            return(false);
        }