[FFmpeg-devel] [PATCH] avcodec/amfenc: Add support for on-demand key frames

Anton Khirnov anton at khirnov.net
Fri Sep 6 09:57:02 EEST 2024


Quoting Lynne via ffmpeg-devel (2024-09-05 23:27:40)
> On 05/09/2024 07:16, Cameron Gutman wrote:
> > Signed-off-by: Cameron Gutman <aicommander at gmail.com>
> > ---
> >   libavcodec/amfenc.c | 34 +++++++++++++++++++++++++++++++++-
> >   1 file changed, 33 insertions(+), 1 deletion(-)
> > 
> > diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
> > index 41eaef9758..6f2b211d14 100644
> > --- a/libavcodec/amfenc.c
> > +++ b/libavcodec/amfenc.c
> > @@ -766,11 +766,43 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
> >           switch (avctx->codec->id) {
> >           case AV_CODEC_ID_H264:
> >               AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!ctx->aud);
> > +            if (frame->flags & AV_FRAME_FLAG_KEY) {
> > +                AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_SPS, 1);
> > +                AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_PPS, 1);
> > +                AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_IDR);
> > +            } else {
> > +                switch (frame->pict_type) {
> > +                case AV_PICTURE_TYPE_I:
> > +                    AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_I); break;
> > +                case AV_PICTURE_TYPE_P:
> > +                    AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_P); break;
> > +                case AV_PICTURE_TYPE_B:
> > +                    AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_B); break;
> > +                }
> > +            }
> >               break;
> >           case AV_CODEC_ID_HEVC:
> >               AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!ctx->aud);
> > +            if (frame->flags & AV_FRAME_FLAG_KEY) {
> > +                AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_HEADER, 1);
> > +                AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_IDR);
> > +            } else {
> > +                switch (frame->pict_type) {
> > +                case AV_PICTURE_TYPE_I:
> > +                    AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_I); break;
> > +                case AV_PICTURE_TYPE_P:
> > +                    AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_P); break;
> > +                }
> > +            }
> > +            break;
> > +        case AV_CODEC_ID_AV1:
> > +            if (frame->flags & AV_FRAME_FLAG_KEY) {
> > +                AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_INSERT_SEQUENCE_HEADER, 1);
> > +                AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_KEY);
> > +            } else if (frame->pict_type == AV_PICTURE_TYPE_I) {
> > +                AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_INTRA_ONLY);
> > +            }
> >               break;
> > -        //case AV_CODEC_ID_AV1 not supported
> >           default:
> >               break;
> >           }
> 
> You should hook up FFCodec.flush instead. That's what nvenc uses to 
> reset the GOP.

No, the patch does almost the right thing, though most other encoders
look at pict_type instead.

-- 
Anton Khirnov


More information about the ffmpeg-devel mailing list