diff mbox series

[FFmpeg-devel,2/6] avcodec/mpegvideo: Make inlining is_mpeg12 more flexible

Message ID GV1P250MB0737AEEB637C14640131FC258F299@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit 409c4723ec04fe8c616f4607c7e45bb57478bc82
Headers show
Series [FFmpeg-devel,1/6] avcodec/mpegvideo: Ignore skip_idct for encoders | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt Oct. 17, 2022, 1:34 a.m. UTC
There are two types of checks for whether the current codec
is MPEG-1/2 in mpv_reconstruct_mb_internal(): Those that are
required for correctness and those that are not; an example
of the latter is "is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)".
The reason for the existence of such checks is that
mpv_reconstruct_mb_internal() has the av_always_inline attribute
and is_mpeg12 is usually inlined, so that in case we are dealing
with MPEG-1/2 the above check can be completely optimized away.

But is_mpeg12 is not always inlined: it is not in case
CONFIG_SMALL is true in which case is_mpeg12 is always zero,
so that the checks required for correctness need to check
out_format explicitly. This is currently done via a macro
in mpv_reconstruct_mb_internal(), so that the fact that
it is CONFIG_SMALL that determines this is encoded at two places.

This commit changes this by making is_mpeg12 a three-state:
DEFINITELY_MPEG12, MAY_BE_MPEG12 and NOT_MPEG12. In the second
case, one has to resort to check out_format, in the other cases
is_mpeg12 can be taken at face-value. This will allow to make
inlining is_mpeg12 more flexible in a future commit.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegvideo.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 448b65bb96..c2fedbfdaa 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -1294,6 +1294,10 @@  void ff_clean_intra_table_entries(MpegEncContext *s)
     s->mbintra_table[xy]= 0;
 }
 
+#define NOT_MPEG12        0
+#define MAY_BE_MPEG12     1
+#define DEFINITELY_MPEG12 2
+
 /* generic function called after a macroblock has been parsed by the
    decoder or after it has been encoded by the encoder.
 
@@ -1309,14 +1313,14 @@  void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
                             int lowres_flag, int is_mpeg12)
 {
 #define IS_ENCODER(s) (CONFIG_MPEGVIDEOENC && !lowres_flag && (s)->encoding)
-#define IS_MPEG12(s) (CONFIG_SMALL ? ((s)->out_format == FMT_MPEG1) : is_mpeg12)
+#define IS_MPEG12(s) (is_mpeg12 == MAY_BE_MPEG12 ? ((s)->out_format == FMT_MPEG1) : is_mpeg12)
     const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
 
     s->current_picture.qscale_table[mb_xy] = s->qscale;
 
     /* update DC predictors for P macroblocks */
     if (!s->mb_intra) {
-        if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
+        if (is_mpeg12 != DEFINITELY_MPEG12 && (s->h263_pred || s->h263_aic)) {
             if(s->mbintra_table[mb_xy])
                 ff_clean_intra_table_entries(s);
         } else {
@@ -1324,8 +1328,7 @@  void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
             s->last_dc[1] =
             s->last_dc[2] = 128 << s->intra_dc_precision;
         }
-    }
-    else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
+    } else if (is_mpeg12 != DEFINITELY_MPEG12 && (s->h263_pred || s->h263_aic))
         s->mbintra_table[mb_xy]=1;
 
     if (!IS_ENCODER(s) || (s->avctx->flags & AV_CODEC_FLAG_PSNR) || s->frame_skip_threshold || s->frame_skip_factor ||
@@ -1399,7 +1402,7 @@  void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
                     }
                 }else{
                     op_qpix = s->me.qpel_put;
-                    if ((is_mpeg12 || !s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
+                    if ((is_mpeg12 == DEFINITELY_MPEG12 || !s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
                         op_pix = s->hdsp.put_pixels_tab;
                     }else{
                         op_pix = s->hdsp.put_no_rnd_pixels_tab;
@@ -1444,7 +1447,7 @@  void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
                         add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
                     }
                 }
-            } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
+            } else if (is_mpeg12 == DEFINITELY_MPEG12 || (s->codec_id != AV_CODEC_ID_WMV2)){
                 add_dct(s, block[0], 0, dest_y                          , dct_linesize);
                 add_dct(s, block[1], 1, dest_y              + block_size, dct_linesize);
                 add_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize);
@@ -1477,7 +1480,8 @@  void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
         } else {
             /* Only MPEG-4 Simple Studio Profile is supported in > 8-bit mode.
                TODO: Integrate 10-bit properly into mpegvideo.c so that ER works properly */
-            if (!is_mpeg12 && CONFIG_MPEG4_DECODER && /* s->codec_id == AV_CODEC_ID_MPEG4 && */
+            if (is_mpeg12 != DEFINITELY_MPEG12 && CONFIG_MPEG4_DECODER &&
+                /* s->codec_id == AV_CODEC_ID_MPEG4 && */
                 s->avctx->bits_per_raw_sample > 8) {
                 ff_mpeg4_decode_studio(s, dest_y, dest_cb, dest_cr, block_size,
                                        uvlinesize, dct_linesize, dct_offset);
@@ -1558,12 +1562,16 @@  void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
 
 #if !CONFIG_SMALL
     if(s->out_format == FMT_MPEG1) {
-        if(s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, 1);
-        else                 mpv_reconstruct_mb_internal(s, block, 0, 1);
-    } else
+        if (s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, DEFINITELY_MPEG12);
+        else                  mpv_reconstruct_mb_internal(s, block, 0, DEFINITELY_MPEG12);
+    } else {
+        if (s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, NOT_MPEG12);
+        else                  mpv_reconstruct_mb_internal(s, block, 0, NOT_MPEG12);
+    }
+#else
+    if (s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, MAY_BE_MPEG12);
+    else                  mpv_reconstruct_mb_internal(s, block, 0, MAY_BE_MPEG12);
 #endif
-    if(s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, 0);
-    else                  mpv_reconstruct_mb_internal(s, block, 0, 0);
 }
 
 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename