[FFmpeg-devel] [PATCH] avcodec/mfenc: expose more properties of the media foundation encoder

Mark Thompson sw at jkqxz.net
Tue Mar 26 22:57:44 EET 2024


On 26/03/2024 19:25, Mark Samuelson wrote:
> ---
>   libavcodec/mf_utils.h |  5 +++++
>   libavcodec/mfenc.c    | 19 +++++++++++++++++++
>   2 files changed, 24 insertions(+)

This seems like a good idea.

> diff --git a/libavcodec/mf_utils.h b/libavcodec/mf_utils.h
> index aebfb9ad21..387c005f38 100644
> --- a/libavcodec/mf_utils.h
> +++ b/libavcodec/mf_utils.h
> @@ -97,6 +97,11 @@ DEFINE_GUID(ff_CODECAPI_AVEncH264CABACEnable,    0xee6cad62, 0xd305, 0x4248, 0xa
>   DEFINE_GUID(ff_CODECAPI_AVEncVideoForceKeyFrame, 0x398c1b98, 0x8353, 0x475a, 0x9e, 0xf2, 0x8f, 0x26, 0x5d, 0x26, 0x3, 0x45);
>   DEFINE_GUID(ff_CODECAPI_AVEncMPVDefaultBPictureCount, 0x8d390aac, 0xdc5c, 0x4200, 0xb5, 0x7f, 0x81, 0x4d, 0x04, 0xba, 0xba, 0xb2);
>   DEFINE_GUID(ff_CODECAPI_AVScenarioInfo, 0xb28a6e64,0x3ff9,0x446a,0x8a,0x4b,0x0d,0x7a,0x53,0x41,0x32,0x36);
> +DEFINE_GUID(ff_CODECAPI_AVEncCommonBufferSize, 0x0db96574, 0xb6a4, 0x4c8b, 0x81, 0x06, 0x37, 0x73, 0xde, 0x03, 0x10, 0xcd);
> +DEFINE_GUID(ff_CODECAPI_AVEncCommonMaxBitRate, 0x9651eae4, 0x39b9, 0x4ebf, 0x85, 0xef, 0xd7, 0xf4, 0x44, 0xec, 0x74, 0x65);
> +DEFINE_GUID(ff_CODECAPI_AVEncCommonQualityVsSpeed, 0x98332df8, 0x03cd, 0x476b, 0x89, 0xfa, 0x3f, 0x9e, 0x44, 0x2d, 0xec, 0x9f);
> +DEFINE_GUID(ff_CODECAPI_AVEncMPVGOPSize, 0x95f31b26, 0x95a4, 0x41aa, 0x93, 0x03, 0x24, 0x6a, 0x7f, 0xc6, 0xee, 0xf1);
> +DEFINE_GUID(ff_CODECAPI_AVEncVideoEncodeQP, 0x2cb5696b, 0x23fb, 0x4ce1, 0xa0, 0xf9, 0xef, 0x5b, 0x90, 0xfd, 0x55, 0xca);
>   
>   DEFINE_GUID(ff_MF_SA_D3D11_BINDFLAGS, 0xeacf97ad, 0x065c, 0x4408, 0xbe, 0xe3, 0xfd, 0xcb, 0xfd, 0x12, 0x8b, 0xe2);
>   DEFINE_GUID(ff_MF_SA_D3D11_USAGE, 0xe85fe442, 0x2ca3, 0x486e, 0xa9, 0xc7, 0x10, 0x9d, 0xda, 0x60, 0x98, 0x80);
> diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c
> index 9225692c51..36f64a93b8 100644
> --- a/libavcodec/mfenc.c
> +++ b/libavcodec/mfenc.c
> @@ -54,6 +54,8 @@ typedef struct MFContext {
>       int opt_enc_quality;
>       int opt_enc_scenario;
>       int opt_enc_hw;
> +    int opt_enc_bufferSize;
> +    int opt_enc_encodeQP;
>   } MFContext;
>   
>   static int mf_choose_output_type(AVCodecContext *avctx);
> @@ -695,6 +697,21 @@ FF_ENABLE_DEPRECATION_WARNINGS
>           if (c->opt_enc_quality >= 0)
>               ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncCommonQuality, FF_VAL_VT_UI4(c->opt_enc_quality));
>   
> +        if (avctx->rc_max_rate > 0)
> +            ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncCommonMaxBitRate, FF_VAL_VT_UI4(avctx->rc_max_rate));
> +
> +        if (avctx->gop_size > 0)
> +            ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncMPVGOPSize, FF_VAL_VT_UI4(avctx->gop_size));

AVCodecContext.gop_size has a surprise default value of 12, which trips this up.  Suggest adding an FFCodecDefault section so that your new option works as a user might expect.

> +
> +        if(c->opt_enc_bufferSize > 0)
> +            ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncCommonBufferSize, FF_VAL_VT_UI4(c->opt_enc_bufferSize));

For VBV/HRD buffer size use AVCodecContext.rc_buffer_size (but be careful with the units!).

> +
> +        if(avctx->compression_level >= 0)
> +            ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncCommonQualityVsSpeed, FF_VAL_VT_UI4(avctx->compression_level));
> +
> +        if(c->opt_enc_encodeQP > 15)
> +            ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncVideoEncodeQP, FF_VAL_VT_UI4(c->opt_enc_encodeQP));

Where has 15 come from?  Ideally don't constrain the range unless there is some specific reason to do so.

Can you use AVCodecContext.global_quality to avoid adding an ad-hoc option?

> +
>           // Always set the number of b-frames. Qualcomm's HEVC encoder on SD835
>           // defaults this to 1, and that setting is buggy with many of the
>           // rate control modes. (0 or 2 b-frames works fine with most rate
> @@ -1280,6 +1297,8 @@ static const AVOption venc_opts[] = {
>   
>       {"quality",       "Quality", OFFSET(opt_enc_quality), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE},
>       {"hw_encoding",   "Force hardware encoding", OFFSET(opt_enc_hw), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VE},
> +    {"buffer_size", "BufferSize", OFFSET(opt_enc_bufferSize), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE},
> +    {"encodeQP", "QualityQP", OFFSET(opt_enc_encodeQP), AV_OPT_TYPE_INT, {.i64 = 15}, 15, 51, VE},
>       {NULL}
>   };
>   

Thanks,

- Mark


More information about the ffmpeg-devel mailing list