[FFmpeg-devel] [PATCH v2 62/69] avcodec/mpegvideo: Move max_b_frames to MPVMainEncContext

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue Feb 1 15:06:59 EET 2022


With the exception of VC-1-based decoders no decoder sets this,
so move it to MPVMainEncContext and add a replacement field to
VC1Context.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/dxva2_vc1.c     |  2 +-
 libavcodec/mpeg4videoenc.c |  8 ++++----
 libavcodec/mpegvideo.h     |  1 -
 libavcodec/mpegvideo_dec.c |  1 -
 libavcodec/mpegvideo_enc.c | 38 +++++++++++++++++++-------------------
 libavcodec/mpegvideoenc.h  |  1 +
 libavcodec/mss2.c          |  2 +-
 libavcodec/nvdec_vc1.c     |  2 +-
 libavcodec/ratecontrol.c   |  6 +++---
 libavcodec/vc1.c           |  4 ++--
 libavcodec/vc1.h           |  1 +
 libavcodec/vdpau_vc1.c     |  2 +-
 12 files changed, 34 insertions(+), 34 deletions(-)

diff --git a/libavcodec/dxva2_vc1.c b/libavcodec/dxva2_vc1.c
index 836367ffe8..d901f023df 100644
--- a/libavcodec/dxva2_vc1.c
+++ b/libavcodec/dxva2_vc1.c
@@ -113,7 +113,7 @@ static void fill_picture_parameters(AVCodecContext *avctx,
                                   (v->multires       << 5) |
                                   (v->resync_marker  << 4) |
                                   (v->rangered       << 3) |
-                                  (s->max_b_frames       );
+                                  (v->max_b_frames       );
     pp->bPicExtrapolation       = (!v->interlace || v->fcm == PROGRESSIVE) ? 1 : 2;
     pp->bPicDeblocked           = ((!pp->bPicBackwardPrediction && v->overlap)        << 6) |
                                   ((v->profile != PROFILE_ADVANCED && v->rangeredfrm) << 5) |
diff --git a/libavcodec/mpeg4videoenc.c b/libavcodec/mpeg4videoenc.c
index 07c82cc54d..9bc6fb9388 100644
--- a/libavcodec/mpeg4videoenc.c
+++ b/libavcodec/mpeg4videoenc.c
@@ -637,7 +637,7 @@ void ff_mpeg4_encode_mb(MPVEncContext *s, int16_t block[6][64],
                 /* Check if the B-frames can skip it too, as we must skip it
                  * if we skip here why didn't they just compress
                  * the skip-mb bits instead of reusing them ?! */
-                if (s->max_b_frames > 0) {
+                if (m->max_b_frames > 0) {
                     int i;
                     int x, y, offset;
                     uint8_t *p_pic;
@@ -649,7 +649,7 @@ void ff_mpeg4_encode_mb(MPVEncContext *s, int16_t block[6][64],
                     p_pic  = s->new_picture.f->data[0] + offset;
 
                     s->mb_skipped = 1;
-                    for (i = 0; i < s->max_b_frames; i++) {
+                    for (i = 0; i < m->max_b_frames; i++) {
                         const Picture *const pic = m->reordered_input_picture[i + 1];
                         uint8_t *b_pic;
                         int diff;
@@ -921,7 +921,7 @@ static void mpeg4_encode_visual_object_header(MPVMainEncContext *m)
 
     if (s->avctx->profile != FF_PROFILE_UNKNOWN) {
         profile_and_level_indication = s->avctx->profile << 4;
-    } else if (s->max_b_frames || s->quarter_sample) {
+    } else if (m->max_b_frames || s->quarter_sample) {
         profile_and_level_indication = 0xF0;  // adv simple
     } else {
         profile_and_level_indication = 0x00;  // simple
@@ -965,7 +965,7 @@ static void mpeg4_encode_vol_header(MPVMainEncContext *m,
     MPVEncContext *const s = &m->common.s;
     int vo_ver_id, vo_type, aspect_ratio_info;
 
-    if (s->max_b_frames || s->quarter_sample) {
+    if (m->max_b_frames || s->quarter_sample) {
         vo_ver_id  = 5;
         vo_type = ADV_SIMPLE_VO_TYPE;
     } else {
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index fefcb90188..8632fadae5 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -95,7 +95,6 @@ typedef struct MPVContext {
 
     enum AVCodecID codec_id;     /* see AV_CODEC_ID_xxx */
     int encoding;     ///< true if we are encoding (vs decoding)
-    int max_b_frames; ///< max number of B-frames for encoding
     int luma_elim_threshold;
     int chroma_elim_threshold;
     int strict_std_compliance; ///< strictly follow the std (MPEG-4, ...)
diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c
index fd97e6ac1a..57c93de621 100644
--- a/libavcodec/mpegvideo_dec.c
+++ b/libavcodec/mpegvideo_dec.c
@@ -151,7 +151,6 @@ do {\
            (char *) &s1->last_time_base);
 
     // B-frame info
-    s->max_b_frames = s1->max_b_frames;
     s->low_delay    = s1->low_delay;
     s->droppable    = s1->droppable;
 
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 4a90799d17..2fbcfd56e1 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -363,9 +363,9 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
                "max b frames must be 0 or positive for mpegvideo based encoders\n");
         return AVERROR(EINVAL);
     }
-    s->max_b_frames = avctx->max_b_frames;
+    m->max_b_frames = avctx->max_b_frames;
     s->codec_id     = avctx->codec->id;
-    if (s->max_b_frames && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
+    if (m->max_b_frames && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
         av_log(avctx, AV_LOG_ERROR, "B-frames not supported by codec\n");
         return AVERROR(EINVAL);
     }
@@ -591,7 +591,7 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
                    "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
             return AVERROR(EINVAL);
         }
-        if (s->max_b_frames != 0) {
+        if (m->max_b_frames != 0) {
             av_log(avctx, AV_LOG_ERROR,
                    "B-frames cannot be used with low delay\n");
             return AVERROR(EINVAL);
@@ -658,12 +658,12 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
     case AV_CODEC_ID_MPEG1VIDEO:
         s->out_format = FMT_MPEG1;
         s->low_delay  = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
-        avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
+        avctx->delay  = s->low_delay ? 0 : (m->max_b_frames + 1);
         break;
     case AV_CODEC_ID_MPEG2VIDEO:
         s->out_format = FMT_MPEG1;
         s->low_delay  = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
-        avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
+        avctx->delay  = s->low_delay ? 0 : (m->max_b_frames + 1);
         s->rtp_mode   = 1;
         break;
 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
@@ -759,8 +759,8 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
         s->out_format      = FMT_H263;
         s->h263_pred       = 1;
         s->unrestricted_mv = 1;
-        s->low_delay       = s->max_b_frames ? 0 : 1;
-        avctx->delay       = s->low_delay ? 0 : (s->max_b_frames + 1);
+        s->low_delay       = m->max_b_frames ? 0 : 1;
+        avctx->delay       = s->low_delay ? 0 : (m->max_b_frames + 1);
         break;
     case AV_CODEC_ID_MSMPEG4V2:
         s->out_format      = FMT_H263;
@@ -957,7 +957,7 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
         return ret;
 
     if (m->b_frame_strategy == 2) {
-        for (i = 0; i < s->max_b_frames + 2; i++) {
+        for (i = 0; i < m->max_b_frames + 2; i++) {
             m->tmp_frames[i] = av_frame_alloc();
             if (!m->tmp_frames[i])
                 return AVERROR(ENOMEM);
@@ -1073,7 +1073,7 @@ static int load_input_picture(MPVMainEncContext *m, const AVFrame *pic_arg)
     Picture *pic = NULL;
     int64_t pts;
     int i, display_picture_number = 0, ret;
-    int encoding_delay = s->max_b_frames ? s->max_b_frames
+    int encoding_delay = m->max_b_frames ? m->max_b_frames
                                          : (s->low_delay ? 0 : 1);
     int flush_offset = 1;
     int direct = 1;
@@ -1310,7 +1310,7 @@ static int estimate_best_b_count(MPVMainEncContext *m)
     lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
                FF_LAMBDA_SHIFT;
 
-    for (i = 0; i < s->max_b_frames + 2; i++) {
+    for (i = 0; i < m->max_b_frames + 2; i++) {
         Picture pre_input, *pre_input_ptr = i ? m->input_picture[i - 1] :
                                                 s->next_picture_ptr;
         uint8_t *data[4];
@@ -1343,7 +1343,7 @@ static int estimate_best_b_count(MPVMainEncContext *m)
         }
     }
 
-    for (j = 0; j < s->max_b_frames + 1; j++) {
+    for (j = 0; j < m->max_b_frames + 1; j++) {
         AVCodecContext *c;
         int64_t rd = 0;
 
@@ -1366,7 +1366,7 @@ static int estimate_best_b_count(MPVMainEncContext *m)
         c->me_sub_cmp   = s->avctx->me_sub_cmp;
         c->pix_fmt      = AV_PIX_FMT_YUV420P;
         c->time_base    = s->avctx->time_base;
-        c->max_b_frames = s->max_b_frames;
+        c->max_b_frames = m->max_b_frames;
 
         ret = avcodec_open2(c, s->avctx->codec, NULL);
         if (ret < 0)
@@ -1384,8 +1384,8 @@ static int estimate_best_b_count(MPVMainEncContext *m)
 
         //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
 
-        for (i = 0; i < s->max_b_frames + 1; i++) {
-            int is_p = i % (j + 1) == j || i == s->max_b_frames;
+        for (i = 0; i < m->max_b_frames + 1; i++) {
+            int is_p = i % (j + 1) == j || i == m->max_b_frames;
 
             m->tmp_frames[i + 1]->pict_type = is_p ?
                                      AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
@@ -1464,7 +1464,7 @@ static int select_input_picture(MPVMainEncContext *m)
             int b_frames = 0;
 
             if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
-                for (i = 0; i < s->max_b_frames + 1; i++) {
+                for (i = 0; i < m->max_b_frames + 1; i++) {
                     int pict_num = m->input_picture[0]->f->display_picture_number + i;
 
                     if (pict_num >= m->rc_context.num_entries)
@@ -1480,11 +1480,11 @@ static int select_input_picture(MPVMainEncContext *m)
             }
 
             if (m->b_frame_strategy == 0) {
-                b_frames = s->max_b_frames;
+                b_frames = m->max_b_frames;
                 while (b_frames && !m->input_picture[b_frames])
                     b_frames--;
             } else if (m->b_frame_strategy == 1) {
-                for (i = 1; i < s->max_b_frames + 1; i++) {
+                for (i = 1; i < m->max_b_frames + 1; i++) {
                     if (m->input_picture[i] &&
                         m->input_picture[i]->b_frame_score == 0) {
                         m->input_picture[i]->b_frame_score =
@@ -1494,7 +1494,7 @@ static int select_input_picture(MPVMainEncContext *m)
                                             s->linesize) + 1;
                     }
                 }
-                for (i = 0; i < s->max_b_frames + 1; i++) {
+                for (i = 0; i < m->max_b_frames + 1; i++) {
                     if (!m->input_picture[i] ||
                         m->input_picture[i]->b_frame_score - 1 >
                             s->mb_num / m->b_sensitivity)
@@ -1521,7 +1521,7 @@ static int select_input_picture(MPVMainEncContext *m)
                     b_frames = i;
             }
             if (m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
-                b_frames == s->max_b_frames) {
+                b_frames == m->max_b_frames) {
                 av_log(s->avctx, AV_LOG_ERROR,
                        "warning, too many B-frames in a row\n");
             }
diff --git a/libavcodec/mpegvideoenc.h b/libavcodec/mpegvideoenc.h
index 91d41f86e8..ef9b92112c 100644
--- a/libavcodec/mpegvideoenc.h
+++ b/libavcodec/mpegvideoenc.h
@@ -99,6 +99,7 @@ typedef struct MPVMainEncContext {
 
     /* temporary frames used by b_frame_strategy == 2 */
     AVFrame *tmp_frames[MPVENC_MAX_B_FRAMES + 2];
+    int max_b_frames; ///< max number of B-frames
     int b_frame_strategy;
     int b_sensitivity;
     int brd_scale;
diff --git a/libavcodec/mss2.c b/libavcodec/mss2.c
index 66ce266b51..f650a84e8d 100644
--- a/libavcodec/mss2.c
+++ b/libavcodec/mss2.c
@@ -783,7 +783,7 @@ static av_cold int wmv9_init(AVCodecContext *avctx)
     v->resync_marker   = 0;
     v->rangered        = 0;
 
-    v->s.s.max_b_frames = avctx->max_b_frames = 0;
+    v->max_b_frames   = avctx->max_b_frames = 0;
     v->quantizer_mode = 0;
 
     v->finterpflag = 0;
diff --git a/libavcodec/nvdec_vc1.c b/libavcodec/nvdec_vc1.c
index afec5c1f7c..e8cae4d724 100644
--- a/libavcodec/nvdec_vc1.c
+++ b/libavcodec/nvdec_vc1.c
@@ -80,7 +80,7 @@ static int nvdec_vc1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u
             .multires          = v->multires,
             .syncmarker        = v->resync_marker,
             .rangered          = v->rangered,
-            .maxbframes        = s->max_b_frames,
+            .maxbframes        = v->max_b_frames,
 
             .panscan_flag      = v->panscanflag,
             .refdist_flag      = v->refdist_flag,
diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index bdf5c82aa1..980f8c59f2 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -557,7 +557,7 @@ av_cold int ff_rate_control_init(MPVMainEncContext *m)
         p = s->avctx->stats_in;
         for (i = -1; p; i++)
             p = strchr(p + 1, ';');
-        i += s->max_b_frames;
+        i += m->max_b_frames;
         if (i <= 0 || i >= INT_MAX / sizeof(RateControlEntry))
             return -1;
         rcc->entry       = av_mallocz(i * sizeof(RateControlEntry));
@@ -578,7 +578,7 @@ av_cold int ff_rate_control_init(MPVMainEncContext *m)
 
         /* read stats */
         p = s->avctx->stats_in;
-        for (i = 0; i < rcc->num_entries - s->max_b_frames; i++) {
+        for (i = 0; i < rcc->num_entries - m->max_b_frames; i++) {
             RateControlEntry *rce;
             int picture_number;
             int e;
@@ -636,7 +636,7 @@ av_cold int ff_rate_control_init(MPVMainEncContext *m)
 
                 if (i % ((m->gop_size + 3) / 4) == 0)
                     rce.pict_type = AV_PICTURE_TYPE_I;
-                else if (i % (s->max_b_frames + 1))
+                else if (i % (m->max_b_frames + 1))
                     rce.pict_type = AV_PICTURE_TYPE_B;
                 else
                     rce.pict_type = AV_PICTURE_TYPE_P;
diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index d9061b1f74..13a211e916 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -359,7 +359,7 @@ int ff_vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitCo
                "RANGERED should be set to 0 in Simple Profile\n");
     }
 
-    s->max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
+    v->max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
     v->quantizer_mode = get_bits(gb, 2); //common
 
     v->finterpflag = get_bits1(gb); //common
@@ -441,7 +441,7 @@ static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
         av_log(s->avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
         return -1;
     }
-    s->max_b_frames = s->avctx->max_b_frames = 7;
+    v->max_b_frames = s->avctx->max_b_frames = 7;
     if (get_bits1(gb)) { //Display Info - decoding is not affected by it
         int w, h, ar = 0;
         av_log(s->avctx, AV_LOG_DEBUG, "Display extended info:\n");
diff --git a/libavcodec/vc1.h b/libavcodec/vc1.h
index f203b28694..6aa1a32352 100644
--- a/libavcodec/vc1.h
+++ b/libavcodec/vc1.h
@@ -222,6 +222,7 @@ typedef struct VC1Context{
     int dquant;           ///< How qscale varies with MBs, 2 bits (not in Simple)
     int vstransform;      ///< variable-size [48]x[48] transform type + info
     int overlap;          ///< overlapped transforms in use
+    int max_b_frames;
     int quantizer_mode;   ///< 2 bits, quantizer mode used for sequence, see QUANT_*
     int finterpflag;      ///< INTERPFRM present
     //@}
diff --git a/libavcodec/vdpau_vc1.c b/libavcodec/vdpau_vc1.c
index f13b62f723..b91b158511 100644
--- a/libavcodec/vdpau_vc1.c
+++ b/libavcodec/vdpau_vc1.c
@@ -90,7 +90,7 @@ static int vdpau_vc1_start_frame(AVCodecContext *avctx,
     info->multires          = v->multires;
     info->syncmarker        = v->resync_marker;
     info->rangered          = v->rangered | (v->rangeredfrm << 1);
-    info->maxbframes        = v->s.s.max_b_frames;
+    info->maxbframes        = v->max_b_frames;
     info->deblockEnable     = v->postprocflag & 1;
     info->pquant            = v->pq;
 
-- 
2.32.0



More information about the ffmpeg-devel mailing list