[FFmpeg-devel] [PATCH 39/61] avcodec/msmpeg4dec: Avoid superfluous VLC structures

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Wed Sep 27 01:17:10 EEST 2023


For all VLCs here, the number of bits of the VLC is write-only,
because it is hardcoded at the call site. Therefore one can replace
these VLC structures with the only thing that is actually used:
The pointer to the VLCElem table. And in some cases one can even
avoid this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/msmpeg4data.h |  2 +-
 libavcodec/msmpeg4dec.c  | 95 +++++++++++++++++++++-------------------
 libavcodec/msmpeg4dec.h  |  4 +-
 libavcodec/wmv2dec.c     |  4 +-
 4 files changed, 54 insertions(+), 51 deletions(-)

diff --git a/libavcodec/msmpeg4data.h b/libavcodec/msmpeg4data.h
index 26ee960f82..aa4ca86a05 100644
--- a/libavcodec/msmpeg4data.h
+++ b/libavcodec/msmpeg4data.h
@@ -44,7 +44,7 @@ typedef struct MVTable {
     const uint8_t *table_mvx;
     const uint8_t *table_mvy;
     uint16_t *table_mv_index; /* encoding: convert mv to index in table_mv */
-    VLC vlc;                /* decoding: vlc */
+    const VLCElem *vlc;       /* decoding: vlc */
 } MVTable;
 
 FF_VISIBILITY_PUSH_HIDDEN
diff --git a/libavcodec/msmpeg4dec.c b/libavcodec/msmpeg4dec.c
index 716fc13529..5b10d8f225 100644
--- a/libavcodec/msmpeg4dec.c
+++ b/libavcodec/msmpeg4dec.c
@@ -63,12 +63,12 @@ static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
 /****************************************/
 /* decoding stuff */
 
-VLC ff_mb_non_intra_vlc[4];
-static VLC v2_dc_lum_vlc;
-static VLC v2_dc_chroma_vlc;
-static VLC v2_intra_cbpc_vlc;
-static VLC v2_mb_type_vlc;
-VLC ff_inter_intra_vlc;
+const VLCElem *ff_mb_non_intra_vlc[4];
+static VLCElem v2_dc_lum_vlc[1472];
+static VLCElem v2_dc_chroma_vlc[1506];
+static VLCElem v2_intra_cbpc_vlc[8];
+static VLCElem v2_mb_type_vlc[128];
+VLCElem ff_inter_intra_vlc[8];
 
 /* This is identical to H.263 except that its range is multiplied by 2. */
 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
@@ -125,7 +125,7 @@ static int msmpeg4v12_decode_mb(MpegEncContext *s, int16_t block[6][64])
         }
 
         if(s->msmpeg4_version==2)
-            code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
+            code = get_vlc2(&s->gb, v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 1);
         else
             code = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2);
         if(code<0 || code>7){
@@ -139,7 +139,7 @@ static int msmpeg4v12_decode_mb(MpegEncContext *s, int16_t block[6][64])
     } else {
         s->mb_intra = 1;
         if(s->msmpeg4_version==2)
-            cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
+            cbp = get_vlc2(&s->gb, v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 1);
         else
             cbp = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2);
         if(cbp<0 || cbp>3){
@@ -230,7 +230,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
             }
         }
 
-        code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
+        code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX], MB_NON_INTRA_VLC_BITS, 3);
         //s->mb_intra = (code & 0x40) ? 0 : 1;
         s->mb_intra = (~code & 0x40) >> 6;
 
@@ -271,7 +271,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
         s->ac_pred = get_bits1(&s->gb);
         *mb_type_ptr = MB_TYPE_INTRA;
         if(s->inter_intra_pred){
-            s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
+            s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 1);
             ff_dlog(s, "%d%d %d %d/",
                     s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
         }
@@ -296,6 +296,8 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
 /* init all vlc decoding tables */
 static av_cold void msmpeg4_decode_init_static(void)
 {
+    static VLCElem vlc_buf[3714 + 2694 + 1636 + 2648 + 1532 + 2488];
+    VLCInitState state = VLC_INIT_STATE(vlc_buf);
     MVTable *mv;
 
     INIT_FIRST_VLC_RL(ff_rl_table[0], 642);
@@ -308,43 +310,44 @@ static av_cold void msmpeg4_decode_init_static(void)
     av_assert1(ff_h263_rl_inter.rl_vlc[0]);
     memcpy(ff_rl_table[5].rl_vlc, ff_h263_rl_inter.rl_vlc, sizeof(ff_rl_table[5].rl_vlc));
 
+    VLC_INIT_STATIC_TABLE(v2_dc_lum_vlc, MSMP4_DC_VLC_BITS, 512,
+                          &ff_v2_dc_lum_table[0][1], 8, 4,
+                          &ff_v2_dc_lum_table[0][0], 8, 4, 0);
+    VLC_INIT_STATIC_TABLE(v2_dc_chroma_vlc, MSMP4_DC_VLC_BITS, 512,
+                          &ff_v2_dc_chroma_table[0][1], 8, 4,
+                          &ff_v2_dc_chroma_table[0][0], 8, 4, 0);
+
+    VLC_INIT_STATIC_TABLE(v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
+                          &ff_v2_intra_cbpc[0][1], 2, 1,
+                          &ff_v2_intra_cbpc[0][0], 2, 1, 0);
+    VLC_INIT_STATIC_TABLE(v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
+                          &ff_v2_mb_type[0][1], 2, 1,
+                          &ff_v2_mb_type[0][0], 2, 1, 0);
+
     mv = &ff_mv_tables[0];
-    VLC_INIT_STATIC(&mv->vlc, MV_VLC_BITS, MSMPEG4_MV_TABLES_NB_ELEMS + 1,
-                    mv->table_mv_bits, 1, 1,
-                    mv->table_mv_code, 2, 2, 3714);
+    mv->vlc = ff_vlc_init_tables_sparse(&state, MV_VLC_BITS,
+                                        MSMPEG4_MV_TABLES_NB_ELEMS + 1,
+                                        mv->table_mv_bits, 1, 1,
+                                        mv->table_mv_code, 2, 2,
+                                        NULL, 0, 0, 0);
     mv = &ff_mv_tables[1];
-    VLC_INIT_STATIC(&mv->vlc, MV_VLC_BITS, MSMPEG4_MV_TABLES_NB_ELEMS + 1,
-                    mv->table_mv_bits, 1, 1,
-                    mv->table_mv_code, 2, 2, 2694);
-
-    VLC_INIT_STATIC(&v2_dc_lum_vlc, MSMP4_DC_VLC_BITS, 512,
-                    &ff_v2_dc_lum_table[0][1], 8, 4,
-                    &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
-    VLC_INIT_STATIC(&v2_dc_chroma_vlc, MSMP4_DC_VLC_BITS, 512,
-                    &ff_v2_dc_chroma_table[0][1], 8, 4,
-                    &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
-
-    VLC_INIT_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
-                    &ff_v2_intra_cbpc[0][1], 2, 1,
-                    &ff_v2_intra_cbpc[0][0], 2, 1, 8);
-    VLC_INIT_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
-                    &ff_v2_mb_type[0][1], 2, 1,
-                    &ff_v2_mb_type[0][0], 2, 1, 128);
-
-    for (unsigned i = 0, offset = 0; i < 4; i++) {
-        static VLCElem vlc_buf[1636 + 2648 + 1532 + 2488];
-        ff_mb_non_intra_vlc[i].table           = &vlc_buf[offset];
-        ff_mb_non_intra_vlc[i].table_allocated = FF_ARRAY_ELEMS(vlc_buf) - offset;
-        vlc_init(&ff_mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128,
-                 &ff_wmv2_inter_table[i][0][1], 8, 4,
-                 &ff_wmv2_inter_table[i][0][0], 8, 4,
-                 VLC_INIT_STATIC_OVERLONG);
-        offset += ff_mb_non_intra_vlc[i].table_size;
+    mv->vlc = ff_vlc_init_tables_sparse(&state, MV_VLC_BITS,
+                                        MSMPEG4_MV_TABLES_NB_ELEMS + 1,
+                                        mv->table_mv_bits, 1, 1,
+                                        mv->table_mv_code, 2, 2,
+                                        NULL, 0, 0, 0);
+
+    for (unsigned i = 0; i < 4; i++) {
+        ff_mb_non_intra_vlc[i] =
+            ff_vlc_init_tables_sparse(&state, MB_NON_INTRA_VLC_BITS, 128,
+                                      &ff_wmv2_inter_table[i][0][1], 8, 4,
+                                      &ff_wmv2_inter_table[i][0][0], 8, 4,
+                                      NULL, 0, 0, 0);
     }
 
-    VLC_INIT_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
-                    &ff_table_inter_intra[0][1], 2, 1,
-                    &ff_table_inter_intra[0][0], 2, 1, 8);
+    VLC_INIT_STATIC_TABLE(ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
+                          &ff_table_inter_intra[0][1], 2, 1,
+                          &ff_table_inter_intra[0][0], 2, 1, 0);
     ff_msmp4_vc1_vlcs_init_once();
 }
 
@@ -572,9 +575,9 @@ static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
 
     if(s->msmpeg4_version<=2){
         if (n < 4) {
-            level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, MSMP4_DC_VLC_BITS, 3);
+            level = get_vlc2(&s->gb, v2_dc_lum_vlc, MSMP4_DC_VLC_BITS, 3);
         } else {
-            level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, MSMP4_DC_VLC_BITS, 3);
+            level = get_vlc2(&s->gb, v2_dc_chroma_vlc, MSMP4_DC_VLC_BITS, 3);
         }
         if (level < 0) {
             av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
@@ -813,7 +816,7 @@ void ff_msmpeg4_decode_motion(MpegEncContext *s, int *mx_ptr, int *my_ptr)
 
     mv = &ff_mv_tables[s->mv_table_index];
 
-    code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
+    code = get_vlc2(&s->gb, mv->vlc, MV_VLC_BITS, 2);
     if (code == MSMPEG4_MV_TABLES_NB_ELEMS) {
         mx = get_bits(&s->gb, 6);
         my = get_bits(&s->gb, 6);
diff --git a/libavcodec/msmpeg4dec.h b/libavcodec/msmpeg4dec.h
index ad41eea9d4..5daa7c6bc3 100644
--- a/libavcodec/msmpeg4dec.h
+++ b/libavcodec/msmpeg4dec.h
@@ -28,8 +28,8 @@
 #define INTER_INTRA_VLC_BITS 3
 #define MB_NON_INTRA_VLC_BITS 9
 
-extern VLC ff_mb_non_intra_vlc[4];
-extern VLC ff_inter_intra_vlc;
+extern const VLCElem *ff_mb_non_intra_vlc[4];
+extern VLCElem ff_inter_intra_vlc[8];
 
 int ff_msmpeg4_decode_init(AVCodecContext *avctx);
 int ff_msmpeg4_decode_picture_header(MpegEncContext *s);
diff --git a/libavcodec/wmv2dec.c b/libavcodec/wmv2dec.c
index d7c193455d..ed6140d29c 100644
--- a/libavcodec/wmv2dec.c
+++ b/libavcodec/wmv2dec.c
@@ -473,7 +473,7 @@ static int wmv2_decode_mb(MpegEncContext *s, int16_t block[6][64])
         if (get_bits_left(&s->gb) <= 0)
             return AVERROR_INVALIDDATA;
 
-        code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index].table,
+        code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index],
                         MB_NON_INTRA_VLC_BITS, 3);
         s->mb_intra = (~code & 0x40) >> 6;
 
@@ -539,7 +539,7 @@ static int wmv2_decode_mb(MpegEncContext *s, int16_t block[6][64])
                 show_bits(&s->gb, 24));
         s->ac_pred = get_bits1(&s->gb);
         if (s->inter_intra_pred) {
-            s->h263_aic_dir = get_vlc2(&s->gb, ff_inter_intra_vlc.table,
+            s->h263_aic_dir = get_vlc2(&s->gb, ff_inter_intra_vlc,
                                        INTER_INTRA_VLC_BITS, 1);
             ff_dlog(s->avctx, "%d%d %d %d/",
                     s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
-- 
2.34.1



More information about the ffmpeg-devel mailing list