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

            if (resampler != null)
            {
                if (frame != null)
                {
                    resampler.Resample(frame as VideoFrame, tempFrame);
                    frame = tempFrame;
                }
            }

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

            if (frame != null)
            {
                try {
                    frame.SetupToNative();
                    frame.presentTimestamp = new Timestamp(inputFrames, framePerSecond.Reciprocal);
                    frame.presentTimestamp.Transform(codecContext->TimeBase);
                    frame.frame->Pts = frame.presentTimestamp.Value;
                    FF.avcodec_encode_video2(codecContext, outPacket.packet, frame.frame, &gotPicture).CheckFFmpegCode("视频编码发生错误");
                } finally {
                    frame.ReleaseSetup();
                }

                inputFrames++;
                encodeFrames = 1;
            }
            else
            {
                FF.avcodec_encode_video2(codecContext, outPacket.packet, null, &gotPicture).CheckFFmpegCode("视频编码发生错误");
            }

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