diff mbox series

[FFmpeg-devel,v2,29/69] avcodec/mpegvideoenc: Move tmp bframes to MPVMainEncContext

Message ID AM7PR03MB666023B73E3A2DBAF0B973708F269@AM7PR03MB6660.eurprd03.prod.outlook.com
State New
Headers show
Series [FFmpeg-devel,v2,01/69] avcodec/avcodec: Avoid MpegEncContext in AVHWAccel.decode_mb | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Feb. 1, 2022, 1:06 p.m. UTC
Also move b_frame_strategy and b_sensitivity; they are all
only used by an encoder's main thread.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegvideo.h     |  7 -----
 libavcodec/mpegvideo_enc.c | 61 +++++++++++++++++++-------------------
 libavcodec/mpegvideoenc.h  | 15 +++++++---
 3 files changed, 41 insertions(+), 42 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index a32bd277eb..017aadb3e7 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -56,8 +56,6 @@ 
 
 #define MAX_THREADS 32
 
-#define MAX_B_FRAMES 16
-
 /**
  * MPVContext.
  */
@@ -528,11 +526,6 @@  typedef struct MPVContext {
 
     int error_rate;
 
-    /* temporary frames used by b_frame_strategy = 2 */
-    AVFrame *tmp_frames[MAX_B_FRAMES + 2];
-    int b_frame_strategy;
-    int b_sensitivity;
-
     /* frame skip options for encoding */
     int frame_skip_threshold;
     int frame_skip_factor;
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 5f607d9e11..e365f2fd81 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -354,10 +354,10 @@  av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
     }
     s->gop_size     = avctx->gop_size;
     s->avctx        = avctx;
-    if (avctx->max_b_frames > MAX_B_FRAMES) {
+    if (avctx->max_b_frames > MPVENC_MAX_B_FRAMES) {
         av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
-               "is %d.\n", MAX_B_FRAMES);
-        avctx->max_b_frames = MAX_B_FRAMES;
+               "is " AV_STRINGIFY(MPVENC_MAX_B_FRAMES) ".\n");
+        avctx->max_b_frames = MPVENC_MAX_B_FRAMES;
     } else if (avctx->max_b_frames < 0) {
         av_log(avctx, AV_LOG_ERROR,
                "max b frames must be 0 or positive for mpegvideo based encoders\n");
@@ -612,10 +612,10 @@  av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
         return AVERROR(EINVAL);
     }
 
-    if (s->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
+    if (m->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
         av_log(avctx, AV_LOG_INFO,
                "notice: b_frame_strategy only affects the first pass\n");
-        s->b_frame_strategy = 0;
+        m->b_frame_strategy = 0;
     }
 
     i = av_gcd(avctx->time_base.den, avctx->time_base.num);
@@ -904,17 +904,17 @@  av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
     if ((ret = ff_rate_control_init(m)) < 0)
         return ret;
 
-    if (s->b_frame_strategy == 2) {
+    if (m->b_frame_strategy == 2) {
         for (i = 0; i < s->max_b_frames + 2; i++) {
-            s->tmp_frames[i] = av_frame_alloc();
-            if (!s->tmp_frames[i])
+            m->tmp_frames[i] = av_frame_alloc();
+            if (!m->tmp_frames[i])
                 return AVERROR(ENOMEM);
 
-            s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
-            s->tmp_frames[i]->width  = s->width  >> s->brd_scale;
-            s->tmp_frames[i]->height = s->height >> s->brd_scale;
+            m->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
+            m->tmp_frames[i]->width  = s->width  >> s->brd_scale;
+            m->tmp_frames[i]->height = s->height >> s->brd_scale;
 
-            ret = av_frame_get_buffer(s->tmp_frames[i], 0);
+            ret = av_frame_get_buffer(m->tmp_frames[i], 0);
             if (ret < 0)
                 return ret;
         }
@@ -935,14 +935,13 @@  av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
 {
     MPVMainEncContext *const m = avctx->priv_data;
     MPVMainContext  *const s = &m->common;
-    int i;
 
     ff_rate_control_uninit(m);
 
     ff_mpv_common_end(&m->common);
 
-    for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
-        av_frame_free(&s->tmp_frames[i]);
+    for (int i = 0; i < FF_ARRAY_ELEMS(m->tmp_frames); i++)
+        av_frame_free(&m->tmp_frames[i]);
 
     ff_mpv_picture_free(avctx, &s->new_picture);
 
@@ -1267,18 +1266,18 @@  static int estimate_best_b_count(MPVMainEncContext *m)
                 data[2] += INPLACE_OFFSET;
             }
 
-            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
-                                       s->tmp_frames[i]->linesize[0],
+            s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[0],
+                                       m->tmp_frames[i]->linesize[0],
                                        data[0],
                                        pre_input.f->linesize[0],
                                        width, height);
-            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
-                                       s->tmp_frames[i]->linesize[1],
+            s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[1],
+                                       m->tmp_frames[i]->linesize[1],
                                        data[1],
                                        pre_input.f->linesize[1],
                                        width >> 1, height >> 1);
-            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
-                                       s->tmp_frames[i]->linesize[2],
+            s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[2],
+                                       m->tmp_frames[i]->linesize[2],
                                        data[2],
                                        pre_input.f->linesize[2],
                                        width >> 1, height >> 1);
@@ -1315,10 +1314,10 @@  static int estimate_best_b_count(MPVMainEncContext *m)
             goto fail;
 
 
-        s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
-        s->tmp_frames[0]->quality   = 1 * FF_QP2LAMBDA;
+        m->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
+        m->tmp_frames[0]->quality   = 1 * FF_QP2LAMBDA;
 
-        out_size = encode_frame(c, s->tmp_frames[0], pkt);
+        out_size = encode_frame(c, m->tmp_frames[0], pkt);
         if (out_size < 0) {
             ret = out_size;
             goto fail;
@@ -1329,11 +1328,11 @@  static int estimate_best_b_count(MPVMainEncContext *m)
         for (i = 0; i < s->max_b_frames + 1; i++) {
             int is_p = i % (j + 1) == j || i == s->max_b_frames;
 
-            s->tmp_frames[i + 1]->pict_type = is_p ?
+            m->tmp_frames[i + 1]->pict_type = is_p ?
                                      AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
-            s->tmp_frames[i + 1]->quality   = is_p ? p_lambda : b_lambda;
+            m->tmp_frames[i + 1]->quality   = is_p ? p_lambda : b_lambda;
 
-            out_size = encode_frame(c, s->tmp_frames[i + 1], pkt);
+            out_size = encode_frame(c, m->tmp_frames[i + 1], pkt);
             if (out_size < 0) {
                 ret = out_size;
                 goto fail;
@@ -1420,11 +1419,11 @@  static int select_input_picture(MPVMainEncContext *m)
                 }
             }
 
-            if (s->b_frame_strategy == 0) {
+            if (m->b_frame_strategy == 0) {
                 b_frames = s->max_b_frames;
                 while (b_frames && !s->input_picture[b_frames])
                     b_frames--;
-            } else if (s->b_frame_strategy == 1) {
+            } else if (m->b_frame_strategy == 1) {
                 for (i = 1; i < s->max_b_frames + 1; i++) {
                     if (s->input_picture[i] &&
                         s->input_picture[i]->b_frame_score == 0) {
@@ -1438,7 +1437,7 @@  static int select_input_picture(MPVMainEncContext *m)
                 for (i = 0; i < s->max_b_frames + 1; i++) {
                     if (!s->input_picture[i] ||
                         s->input_picture[i]->b_frame_score - 1 >
-                            s->mb_num / s->b_sensitivity)
+                            s->mb_num / m->b_sensitivity)
                         break;
                 }
 
@@ -1448,7 +1447,7 @@  static int select_input_picture(MPVMainEncContext *m)
                 for (i = 0; i < b_frames + 1; i++) {
                     s->input_picture[i]->b_frame_score = 0;
                 }
-            } else if (s->b_frame_strategy == 2) {
+            } else if (m->b_frame_strategy == 2) {
                 b_frames = estimate_best_b_count(m);
                 if (b_frames < 0)
                     return b_frames;
diff --git a/libavcodec/mpegvideoenc.h b/libavcodec/mpegvideoenc.h
index 37375132d7..7b58ace64b 100644
--- a/libavcodec/mpegvideoenc.h
+++ b/libavcodec/mpegvideoenc.h
@@ -32,9 +32,16 @@ 
 #include "internal.h"
 #include "mpegvideo.h"
 
+#define MPVENC_MAX_B_FRAMES 16
+
 typedef MPVContext MPVEncContext;
 typedef struct MPVMainEncContext {
     MPVMainContext common;
+
+    /* temporary frames used by b_frame_strategy == 2 */
+    AVFrame *tmp_frames[MPVENC_MAX_B_FRAMES + 2];
+    int b_frame_strategy;
+    int b_sensitivity;
 } MPVMainEncContext;
 
 #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
@@ -115,8 +122,8 @@  FF_MPV_OPT_CMP_FUNC, \
 {"intra_penalty", "Penalty for intra blocks in block decision", FF_MPV_OFFSET(intra_penalty), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX/2, FF_MPV_OPT_FLAGS }, \
 
 #define FF_MPV_COMMON_BFRAME_OPTS \
-{"b_strategy", "Strategy to choose between I/P/B-frames",      FF_MPV_OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, FF_MPV_OPT_FLAGS }, \
-{"b_sensitivity", "Adjust sensitivity of b_frame_strategy 1",  FF_MPV_OFFSET(b_sensitivity), AV_OPT_TYPE_INT, {.i64 = 40 }, 1, INT_MAX, FF_MPV_OPT_FLAGS }, \
+{"b_strategy", "Strategy to choose between I/P/B-frames",      FF_MPV_MAIN_OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, FF_MPV_OPT_FLAGS }, \
+{"b_sensitivity", "Adjust sensitivity of b_frame_strategy 1",  FF_MPV_MAIN_OFFSET(b_sensitivity), AV_OPT_TYPE_INT, {.i64 = 40 }, 1, INT_MAX, FF_MPV_OPT_FLAGS }, \
 {"brd_scale", "Downscale frames for dynamic B-frame decision", FF_MPV_OFFSET(brd_scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 3, FF_MPV_OPT_FLAGS },
 
 #if FF_API_MPEGVIDEO_OPTS
@@ -127,8 +134,8 @@  FF_MPV_OPT_CMP_FUNC, \
 #define FF_MPV_DEPRECATED_MATRIX_OPT \
    { "force_duplicated_matrix", "Deprecated, does nothing", FF_MPV_OFFSET(dummy), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FF_MPV_OPT_FLAGS | AV_OPT_FLAG_DEPRECATED },
 #define FF_MPV_DEPRECATED_BFRAME_OPTS \
-   { "b_strategy",    "Deprecated, does nothing", FF_MPV_OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, { .i64 =  0 }, 0, 2, FF_MPV_OPT_FLAGS | AV_OPT_FLAG_DEPRECATED }, \
-   { "b_sensitivity", "Deprecated, does nothing", FF_MPV_OFFSET(b_sensitivity),    AV_OPT_TYPE_INT, { .i64 = 40 }, 1, INT_MAX, FF_MPV_OPT_FLAGS | AV_OPT_FLAG_DEPRECATED }, \
+   { "b_strategy",    "Deprecated, does nothing", FF_MPV_MAIN_OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, { .i64 =  0 }, 0, 2, FF_MPV_OPT_FLAGS | AV_OPT_FLAG_DEPRECATED }, \
+   { "b_sensitivity", "Deprecated, does nothing", FF_MPV_MAIN_OFFSET(b_sensitivity),    AV_OPT_TYPE_INT, { .i64 = 40 }, 1, INT_MAX, FF_MPV_OPT_FLAGS | AV_OPT_FLAG_DEPRECATED }, \
    { "brd_scale",     "Deprecated, does nothing", FF_MPV_OFFSET(brd_scale),        AV_OPT_TYPE_INT, { .i64 =  0 }, 0, 3, FF_MPV_OPT_FLAGS | AV_OPT_FLAG_DEPRECATED },
 #endif