[FFmpeg-devel] [PATCH 2/3] libavcodec/qsvenc: Add bitrate reset support to qsvenc

Wenbin Chen wenbin.chen at intel.com
Fri Sep 23 05:44:11 EEST 2022


Signed-off-by: Wenbin Chen <wenbin.chen at intel.com>
---
 doc/encoders.texi   |  6 ++++++
 libavcodec/qsvenc.c | 38 ++++++++++++++++++++++++++++++++++++++
 libavcodec/qsvenc.h |  5 +++++
 3 files changed, 49 insertions(+)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index 4ed7ce1bb0..2b6412dbec 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -3363,6 +3363,12 @@ Change this value to reset qsv codec's low_delay_brc configuration.
 
 @item @var{framerate}
 Change this value to reset qsv codec's framerate configuration.
+
+ at item @var{bit_rate}
+ at item @var{rc_buffer_size}
+ at item @var{rc_initial_buffer_occupancy}
+ at item @var{rc_max_rate}
+Change these value to reset qsv codec's bitrate control configuration.
 @end table
 
 @subsection H264 options
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index ce00cd302b..3371711872 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -718,6 +718,10 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
     max_bitrate_kbps           = avctx->rc_max_rate / 1000;
     brc_param_multiplier       = (FFMAX(FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
                                   initial_delay_in_kilobytes) + 0x10000) / 0x10000;
+    q->old_rc_buffer_size = avctx->rc_buffer_size;
+    q->old_rc_initial_buffer_occupancy = avctx->rc_initial_buffer_occupancy;
+    q->old_bit_rate = avctx->bit_rate;
+    q->old_rc_max_rate = avctx->rc_max_rate;
 
     switch (q->param.mfx.RateControlMethod) {
     case MFX_RATECONTROL_CBR:
@@ -1863,6 +1867,39 @@ static int update_frame_rate(AVCodecContext *avctx, QSVEncContext *q)
     return updated;
 }
 
+static int update_bitrate(AVCodecContext *avctx, QSVEncContext *q)
+{
+    int updated = 0;
+    int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
+    int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
+
+    UPDATE_PARAM(q->old_rc_buffer_size, avctx->rc_buffer_size);
+    UPDATE_PARAM(q->old_rc_initial_buffer_occupancy, avctx->rc_initial_buffer_occupancy);
+    UPDATE_PARAM(q->old_bit_rate, avctx->bit_rate);
+    UPDATE_PARAM(q->old_rc_max_rate, avctx->rc_max_rate);
+    if (!updated)
+        return 0;
+
+    buffer_size_in_kilobytes   = avctx->rc_buffer_size / 8000;
+    initial_delay_in_kilobytes = avctx->rc_initial_buffer_occupancy / 8000;
+    target_bitrate_kbps        = avctx->bit_rate / 1000;
+    max_bitrate_kbps           = avctx->rc_max_rate / 1000;
+    brc_param_multiplier       = (FFMAX(FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
+                                    initial_delay_in_kilobytes) + 0x10000) / 0x10000;
+
+    q->param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
+    q->param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
+    q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
+    q->param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
+    q->param.mfx.BRCParamMultiplier = brc_param_multiplier;
+    av_log(avctx, AV_LOG_VERBOSE,
+            "Reset BufferSizeInKB: %d; InitialDelayInKB: %d; "
+            "TargetKbps: %d; MaxKbps: %d; BRCParamMultiplier: %d\n",
+            q->param.mfx.BufferSizeInKB, q->param.mfx.InitialDelayInKB,
+            q->param.mfx.TargetKbps, q->param.mfx.MaxKbps, q->param.mfx.BRCParamMultiplier);
+    return updated;
+}
+
 static int update_parameters(AVCodecContext *avctx, QSVEncContext *q,
                              const AVFrame *frame)
 {
@@ -1877,6 +1914,7 @@ static int update_parameters(AVCodecContext *avctx, QSVEncContext *q,
     needReset |= update_rir(avctx, q);
     needReset |= update_low_delay_brc(avctx, q);
     needReset |= update_frame_rate(avctx, q);
+    needReset |= update_bitrate(avctx, q);
     ret = update_min_max_qp(avctx, q);
     if (ret < 0)
         return ret;
diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h
index 960197f159..6e1132d2e3 100644
--- a/libavcodec/qsvenc.h
+++ b/libavcodec/qsvenc.h
@@ -273,6 +273,11 @@ typedef struct QSVEncContext {
     int old_low_delay_brc;
     // This is used for framerate reset
     AVRational old_framerate;
+    // These are used for bitrate control reset
+    int old_bit_rate;
+    int old_rc_buffer_size;
+    int old_rc_initial_buffer_occupancy;
+    int old_rc_max_rate;
 } QSVEncContext;
 
 int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q);
-- 
2.32.0



More information about the ffmpeg-devel mailing list