[FFmpeg-devel] [PATCH 2/2] lavc/qsvenc: add support for oneVPL string API

Xiang, Haihao haihao.xiang at intel.com
Sat May 4 08:45:05 EEST 2024


On Do, 2024-02-29 at 13:34 +0800, Xiang, Haihao wrote:
> From: "Mandava, Mounika" <mounika.mandava at intel.com>
> 
> A new option -qsv_params <str> is added, where <str> is a :-separated
> list of key=value parameters.
> 
> Example:
> $ ffmpeg -y -f lavfi -i testsrc -vf "format=nv12" -c:v h264_qsv -qsv_params
> "TargetUsage=1:GopPicSize=30:GopRefDist=2:TargetKbps=5000" -f null -
> 
> Signed-off-by: Mounika Mandava <mounika.mandava at intel.com>
> Signed-off-by: Haihao Xiang <haihao.xiang at intel.com>
> ---
>  Changelog           |  1 +
>  configure           |  2 ++
>  doc/encoders.texi   | 14 ++++++++++
>  libavcodec/qsvenc.c | 62 +++++++++++++++++++++++++++++++++++++++++++++
>  libavcodec/qsvenc.h |  8 +++++-
>  5 files changed, 86 insertions(+), 1 deletion(-)
> 
> diff --git a/Changelog b/Changelog
> index 610ee61dd6..b137d089d8 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -27,6 +27,7 @@ version <next>:
>  - a C11-compliant compiler is now required; note that this requirement
>    will be bumped to C17 in the near future, so consider updating your
>    build environment if it lacks C17 support
> +- qsv_params option added for QSV encoders
>  
>  version 6.1:
>  - libaribcaption decoder
> diff --git a/configure b/configure
> index bb5e630bad..046c481f63 100755
> --- a/configure
> +++ b/configure
> @@ -2431,6 +2431,7 @@ TYPES_LIST="
>      struct_sockaddr_storage
>      struct_stat_st_mtim_tv_nsec
>      struct_v4l2_frmivalenum_discrete
> +    struct_mfxConfigInterface
>  "
>  
>  HAVE_LIST="
> @@ -6828,6 +6829,7 @@ elif enabled libvpl; then
>      check_pkg_config libmfx "vpl >= 2.6" "mfxvideo.h mfxdispatcher.h" MFXLoad
> || \
>              die "ERROR: libvpl >= 2.6 not found"
>      add_cflags -DMFX_DEPRECATED_OFF
> +    check_type "vpl/mfxdefs.h vpl/mfxvideo.h" "struct mfxConfigInterface"
>  fi
>  
>  if enabled libmfx; then
> diff --git a/doc/encoders.texi b/doc/encoders.texi
> index 9f477d7c53..cbd3b538cf 100644
> --- a/doc/encoders.texi
> +++ b/doc/encoders.texi
> @@ -3485,6 +3485,20 @@ Change these value to reset qsv codec's bitrate control
> configuration.
>  @item @var{pic_timing_sei}
>  Supported in h264_qsv and hevc_qsv.
>  Change this value to reset qsv codec's pic_timing_sei configuration.
> +
> + at item @var{qsv_params}
> +Set QSV encoder parameters as a colon-separated list of key-value pairs.
> +
> +The @option{qsv_params} should be formatted as
> @code{key1=value1:key2=value2:...}.
> +
> +These parameters are passed directly to the underlying Intel Quick Sync Video
> (QSV) encoder using the MFXSetParameter function.
> +
> +Example:
> + at example
> +ffmpeg -i input.mp4 -c:v h264_qsv -qsv_params
> "CodingOption1=1:CodingOption2=2" output.mp4
> + at end example
> +
> +This option allows fine-grained control over various encoder-specific
> settings provided by the QSV encoder.
>  @end table
>  
>  @subsection H264 options
> diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
> index 27e0e7ee71..0189c219d2 100644
> --- a/libavcodec/qsvenc.c
> +++ b/libavcodec/qsvenc.c
> @@ -31,6 +31,7 @@
>  #include "libavutil/hwcontext_qsv.h"
>  #include "libavutil/mem.h"
>  #include "libavutil/log.h"
> +#include "libavutil/dict.h"
>  #include "libavutil/time.h"
>  #include "libavutil/imgutils.h"
>  
> @@ -1609,6 +1610,11 @@ int ff_qsv_enc_init(AVCodecContext *avctx,
> QSVEncContext *q)
>      int opaque_alloc = 0;
>      int ret;
>      void *tmp;
> +#if HAVE_STRUCT_MFXCONFIGINTERFACE
> +    mfxExtBuffer ext_buf;
> +    mfxConfigInterface *iface = NULL;
> +    const AVDictionaryEntry *param = NULL;
> +#endif
>  
>      q->param.AsyncDepth = q->async_depth;
>  
> @@ -1703,6 +1709,58 @@ int ff_qsv_enc_init(AVCodecContext *avctx,
> QSVEncContext *q)
>      q->param.ExtParam    = q->extparam;
>      q->param.NumExtParam = q->nb_extparam;
>  
> +#if HAVE_STRUCT_MFXCONFIGINTERFACE
> +    ret = MFXVideoCORE_GetHandle(q->session, MFX_HANDLE_CONFIG_INTERFACE,
> (mfxHDL *)(&iface));
> +    if (ret < 0)
> +        return ff_qsv_print_error(avctx, ret,
> +                                  "Error getting mfx config interface
> handle");
> +
> +    while ((param = av_dict_get(q->qsv_params, "", param,
> AV_DICT_IGNORE_SUFFIX))) {
> +        const char *param_key = param->key;
> +        const char *param_value = param->value;
> +        mfxExtBuffer *new_ext_buf;
> +        void *tmp;
> +
> +        av_log(avctx, AV_LOG_VERBOSE, "Parameter key: %s, value: %s\n",
> param_key, param_value);
> +
> +        // Set encoding parameters using MFXSetParameter
> +        for (int i = 0; i < 2; i++) {
> +            ret = iface->SetParameter(iface, (mfxU8*)param_key,
> (mfxU8*)param_value, MFX_STRUCTURE_TYPE_VIDEO_PARAM, &q->param, &ext_buf);
> +            if (ret == MFX_ERR_NONE) {
> +                break;
> +            } else if (i == 0 && ret == MFX_ERR_MORE_EXTBUFFER) {
> +                tmp = av_realloc_array(q->extparam_str, q->nb_extparam_str +
> 1, sizeof(*q->extparam_str));
> +                if (!tmp)
> +                    return AVERROR(ENOMEM);
> +                q->extparam_str = tmp;
> +
> +                tmp = av_realloc_array(q->extparam, q->nb_extparam + 1,
> sizeof(*q->extparam));
> +                if (!tmp)
> +                    return AVERROR(ENOMEM);
> +                q->extparam = tmp;
> +
> +                new_ext_buf = (mfxExtBuffer*)av_mallocz(ext_buf.BufferSz);
> +                if (!new_ext_buf)
> +                    return AVERROR(ENOMEM);
> +
> +                new_ext_buf->BufferId = ext_buf.BufferId;
> +                new_ext_buf->BufferSz = ext_buf.BufferSz;
> +                q->extparam_str[q->nb_extparam_str++] = new_ext_buf;
> +                q->extparam[q->nb_extparam++] = new_ext_buf;
> +                q->param.ExtParam    = q->extparam;
> +                q->param.NumExtParam = q->nb_extparam;
> +            } else {
> +                av_log(avctx, AV_LOG_ERROR, "Failed to set parameter: %s\n",
> param_key);
> +                return AVERROR_UNKNOWN;
> +            }
> +       }
> +    }
> +#else
> +    if (q->qsv_params) {
> +        av_log(avctx, AV_LOG_WARNING, "MFX string API is not supported,
> ignore qsv_params option\n");
> +    }
> +#endif
> +
>      ret = MFXVideoENCODE_Query(q->session, &q->param, &q->param);
>      if (ret == MFX_WRN_PARTIAL_ACCELERATION) {
>          av_log(avctx, AV_LOG_WARNING, "Encoder will work with partial HW
> acceleration\n");
> @@ -2662,6 +2720,10 @@ int ff_qsv_enc_close(AVCodecContext *avctx,
> QSVEncContext *q)
>      av_buffer_unref(&q->opaque_alloc_buf);
>  #endif
>  
> +    for (int i = 0; i < q->nb_extparam_str; i++)
> +        av_free(q->extparam_str[i]);
> +
> +    av_freep(&q->extparam_str);
>      av_freep(&q->extparam);
>  
>      return 0;
> diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h
> index f2f0687bfe..e3eb083746 100644
> --- a/libavcodec/qsvenc.h
> +++ b/libavcodec/qsvenc.h
> @@ -64,7 +64,8 @@
>  { "slower",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_2 
> },            INT_MIN, INT_MAX, VE, .unit = "preset"
> },                                          \
>  { "veryslow",    NULL, 0, AV_OPT_TYPE_CONST, { .i64 =
> MFX_TARGETUSAGE_BEST_QUALITY  }, INT_MIN, INT_MAX, VE, .unit = "preset"
> },                                          \
>  { "forced_idr",     "Forcing I frames as IDR frames",        
> OFFSET(qsv.forced_idr),     AV_OPT_TYPE_BOOL,{ .i64 = 0  },  0,          1, VE
> },                         \
> -{ "low_power", "enable low power mode(experimental: many limitations by mfx
> version, BRC modes, etc.)", OFFSET(qsv.low_power), AV_OPT_TYPE_BOOL, { .i64 =
> -1}, -1, 1, VE},
> +{ "low_power", "enable low power mode(experimental: many limitations by mfx
> version, BRC modes, etc.)", OFFSET(qsv.low_power), AV_OPT_TYPE_BOOL, { .i64 =
> -1}, -1, 1, VE},\
> +{ "qsv_params", "Set QSV encoder parameters as key1=value1:key2=value2:...",
> OFFSET(qsv.qsv_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
>  
>  #if QSV_HAVE_HE
>  #define QSV_HE_OPTIONS \
> @@ -195,6 +196,9 @@ typedef struct QSVEncContext {
>      mfxExtBuffer  *extparam_internal[5 + (QSV_HAVE_MF * 2) +
> (QSV_HAVE_EXT_AV1_PARAM * 2) + QSV_HAVE_HE];
>      int         nb_extparam_internal;
>  
> +    mfxExtBuffer  **extparam_str;
> +    int         nb_extparam_str;
> +
>      mfxExtBuffer **extparam;
>      int         nb_extparam;
>  
> @@ -315,6 +319,8 @@ typedef struct QSVEncContext {
>      int skip_frame;
>      // This is used for Hyper Encode
>      int dual_gfx;
> +
> +    AVDictionary *qsv_params;
>  } QSVEncContext;
>  
>  int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q);

Will apply this patchset,

- Haihao




More information about the ffmpeg-devel mailing list