[FFmpeg-devel] [PATCH 3/4] avcodec/mpegvideo_enc: do not use AVFrame.*_picture_number for encoding

Marton Balint cus at passwd.hu
Sat Jan 28 20:15:22 EET 2023


Move these fields to MPEGPicture instead and use that.

Signed-off-by: Marton Balint <cus at passwd.hu>
---
 libavcodec/mpeg12enc.c     |  4 ++--
 libavcodec/mpegpicture.c   |  4 ++++
 libavcodec/mpegpicture.h   |  3 +++
 libavcodec/mpegvideo_enc.c | 17 ++++++++++-------
 libavcodec/ratecontrol.c   |  4 ++--
 libavcodec/snowenc.c       |  5 ++---
 6 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c
index a6663a158b..00a8085d12 100644
--- a/libavcodec/mpeg12enc.c
+++ b/libavcodec/mpeg12enc.c
@@ -420,10 +420,10 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s)
     /* time code: we must convert from the real frame rate to a
      * fake MPEG frame rate in case of low frame rate */
     fps       = (framerate.num + framerate.den / 2) / framerate.den;
-    time_code = s->current_picture_ptr->f->coded_picture_number +
+    time_code = s->current_picture_ptr->coded_picture_number +
                 mpeg12->timecode_frame_start;
 
-    mpeg12->gop_picture_number = s->current_picture_ptr->f->coded_picture_number;
+    mpeg12->gop_picture_number = s->current_picture_ptr->coded_picture_number;
 
     av_assert0(mpeg12->drop_frame_timecode == !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
     if (mpeg12->drop_frame_timecode)
diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c
index 977bc65191..3204a70578 100644
--- a/libavcodec/mpegpicture.c
+++ b/libavcodec/mpegpicture.c
@@ -332,6 +332,8 @@ void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
     pic->needs_realloc = 0;
     pic->reference     = 0;
     pic->shared        = 0;
+    pic->display_picture_number = 0;
+    pic->coded_picture_number   = 0;
 }
 
 int ff_update_picture_tables(Picture *dst, const Picture *src)
@@ -397,6 +399,8 @@ int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
     dst->needs_realloc           = src->needs_realloc;
     dst->reference               = src->reference;
     dst->shared                  = src->shared;
+    dst->display_picture_number  = src->display_picture_number;
+    dst->coded_picture_number    = src->coded_picture_number;
 
     return 0;
 fail:
diff --git a/libavcodec/mpegpicture.h b/libavcodec/mpegpicture.h
index a1455ee13c..7919aa402c 100644
--- a/libavcodec/mpegpicture.h
+++ b/libavcodec/mpegpicture.h
@@ -76,6 +76,9 @@ typedef struct Picture {
 
     int reference;
     int shared;
+
+    int display_picture_number;
+    int coded_picture_number;
 } Picture;
 
 /**
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index bb101612e5..4e49a103e1 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -1219,7 +1219,7 @@ static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
         if (ret < 0)
             return ret;
 
-        pic->f->display_picture_number = display_picture_number;
+        pic->display_picture_number = display_picture_number;
         pic->f->pts = pts; // we set this here to avoid modifying pic_arg
     } else {
         /* Flushing: When we have not received enough input frames,
@@ -1477,14 +1477,14 @@ static int select_input_picture(MpegEncContext *s)
             !s->next_picture_ptr || s->intra_only) {
             s->reordered_input_picture[0] = s->input_picture[0];
             s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
-            s->reordered_input_picture[0]->f->coded_picture_number =
+            s->reordered_input_picture[0]->coded_picture_number =
                 s->coded_picture_number++;
         } else {
             int b_frames = 0;
 
             if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
                 for (i = 0; i < s->max_b_frames + 1; i++) {
-                    int pict_num = s->input_picture[0]->f->display_picture_number + i;
+                    int pict_num = s->input_picture[0]->display_picture_number + i;
 
                     if (pict_num >= s->rc_context.num_entries)
                         break;
@@ -1563,13 +1563,13 @@ static int select_input_picture(MpegEncContext *s)
             s->reordered_input_picture[0] = s->input_picture[b_frames];
             if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
                 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
-            s->reordered_input_picture[0]->f->coded_picture_number =
+            s->reordered_input_picture[0]->coded_picture_number =
                 s->coded_picture_number++;
             for (i = 0; i < b_frames; i++) {
                 s->reordered_input_picture[i + 1] = s->input_picture[i];
                 s->reordered_input_picture[i + 1]->f->pict_type =
                     AV_PICTURE_TYPE_B;
-                s->reordered_input_picture[i + 1]->f->coded_picture_number =
+                s->reordered_input_picture[i + 1]->coded_picture_number =
                     s->coded_picture_number++;
             }
         }
@@ -1604,6 +1604,8 @@ no_output_pic:
             ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
             if (ret < 0)
                 return ret;
+            pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
+            pic->display_picture_number = s->reordered_input_picture[0]->display_picture_number;
 
             /* mark us unused / free shared pic */
             av_frame_unref(s->reordered_input_picture[0]->f);
@@ -1618,7 +1620,8 @@ no_output_pic:
                     s->new_picture->data[i] += INPLACE_OFFSET;
             }
         }
-        s->picture_number = s->new_picture->display_picture_number;
+        s->picture_number = s->current_picture_ptr->display_picture_number;
+
     }
     return 0;
 }
@@ -1953,7 +1956,7 @@ vbv_retry:
 
         pkt->pts = s->current_picture.f->pts;
         if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
-            if (!s->current_picture.f->coded_picture_number)
+            if (!s->current_picture.coded_picture_number)
                 pkt->dts = pkt->pts - s->dts_delta;
             else
                 pkt->dts = s->reordered_pts;
diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index 4829172c2c..6a40f9cbdc 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -39,8 +39,8 @@ void ff_write_pass1_stats(MpegEncContext *s)
     snprintf(s->avctx->stats_out, 256,
              "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d "
              "fcode:%d bcode:%d mc-var:%"PRId64" var:%"PRId64" icount:%d skipcount:%d hbits:%d;\n",
-             s->current_picture_ptr->f->display_picture_number,
-             s->current_picture_ptr->f->coded_picture_number,
+             s->current_picture_ptr->display_picture_number,
+             s->current_picture_ptr->coded_picture_number,
              s->pict_type,
              s->current_picture.f->quality,
              s->i_tex_bits,
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index 171a36ba00..d37c7fdb5a 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -1856,13 +1856,12 @@ redo_frame:
 
     ff_snow_release_buffer(avctx);
 
-    s->current_picture->coded_picture_number = avctx->frame_num;
     s->current_picture->pict_type = pic->pict_type;
     s->current_picture->quality = pic->quality;
     s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
     s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
-    s->m.current_picture.f->display_picture_number =
-    s->m.current_picture.f->coded_picture_number   = avctx->frame_num;
+    s->m.current_picture.display_picture_number =
+    s->m.current_picture.coded_picture_number   = avctx->frame_num;
     s->m.current_picture.f->quality                = pic->quality;
     s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
     if(s->pass1_rc)
-- 
2.35.3



More information about the ffmpeg-devel mailing list