[FFmpeg-devel] [PATCH 11/61] avcodec/msmpeg4_vc1_data: Avoid superfluous VLC structures

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Wed Sep 27 01:16:42 EEST 2023


Of all these VLCs here, only VLC.table was really used
after init, so use the ff_vlc_init_tables API
to get rid of them.
Also combine the ff_msmp4_dc_(luma|chroma)_vlcs as well
as the tables used to generate them to simplify the code.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/msmpeg4_vc1_data.c | 61 +++++++++++++++++------------------
 libavcodec/msmpeg4_vc1_data.h | 10 ++----
 libavcodec/msmpeg4dec.c       | 13 +++-----
 libavcodec/msmpeg4enc.c       | 15 ++-------
 libavcodec/vc1_block.c        | 31 +++++-------------
 libavcodec/wmv2dec.c          |  2 +-
 6 files changed, 48 insertions(+), 84 deletions(-)

diff --git a/libavcodec/msmpeg4_vc1_data.c b/libavcodec/msmpeg4_vc1_data.c
index e643668730..a25eb956ff 100644
--- a/libavcodec/msmpeg4_vc1_data.c
+++ b/libavcodec/msmpeg4_vc1_data.c
@@ -32,28 +32,25 @@
 #include "libavutil/attributes.h"
 #include "libavutil/thread.h"
 
-VLC ff_msmp4_mb_i_vlc;
-VLC ff_msmp4_dc_luma_vlc[2];
-VLC ff_msmp4_dc_chroma_vlc[2];
+VLCElem ff_msmp4_mb_i_vlc[536];
+const VLCElem *ff_msmp4_dc_vlc[2][2];
 
 static av_cold void msmp4_vc1_vlcs_init(void)
 {
-    VLC_INIT_STATIC(&ff_msmp4_dc_luma_vlc[0], MSMP4_DC_VLC_BITS, 120,
-                    &ff_table0_dc_lum[0][1], 8, 4,
-                    &ff_table0_dc_lum[0][0], 8, 4, 1158);
-    VLC_INIT_STATIC(&ff_msmp4_dc_chroma_vlc[0], MSMP4_DC_VLC_BITS, 120,
-                    &ff_table0_dc_chroma[0][1], 8, 4,
-                    &ff_table0_dc_chroma[0][0], 8, 4, 1118);
-    VLC_INIT_STATIC(&ff_msmp4_dc_luma_vlc[1], MSMP4_DC_VLC_BITS, 120,
-                    &ff_table1_dc_lum[0][1], 8, 4,
-                    &ff_table1_dc_lum[0][0], 8, 4, 1476);
-    VLC_INIT_STATIC(&ff_msmp4_dc_chroma_vlc[1], MSMP4_DC_VLC_BITS, 120,
-                    &ff_table1_dc_chroma[0][1], 8, 4,
-                    &ff_table1_dc_chroma[0][0], 8, 4, 1216);
+    static VLCElem vlc_buf[1158 + 1118 + 1476 + 1216];
+    VLCInitState state = VLC_INIT_STATE(vlc_buf);
 
-    VLC_INIT_STATIC(&ff_msmp4_mb_i_vlc, MSMP4_MB_INTRA_VLC_BITS, 64,
-                    &ff_msmp4_mb_i_table[0][1], 4, 2,
-                    &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
+    for (int i = 0; i < 2; i++) {
+        for (int j = 0; j < 2; j++) {
+            ff_msmp4_dc_vlc[i][j] =
+                ff_vlc_init_tables(&state, MSMP4_DC_VLC_BITS, 120,
+                                   &ff_msmp4_dc_tables[i][j][0][1], 8, 4,
+                                   &ff_msmp4_dc_tables[i][j][0][0], 8, 4, 0);
+        }
+    }
+    VLC_INIT_STATIC_TABLE(ff_msmp4_mb_i_vlc, MSMP4_MB_INTRA_VLC_BITS, 64,
+                          &ff_msmp4_mb_i_table[0][1], 4, 2,
+                          &ff_msmp4_mb_i_table[0][0], 4, 2, 0);
 }
 
 av_cold void ff_msmp4_vc1_vlcs_init_once(void)
@@ -82,9 +79,11 @@ const uint16_t ff_msmp4_mb_i_table[64][2] = {
     {  0xd, 8 }, { 0x713, 13 }, { 0x1da, 10 }, { 0x169, 10 },
 };
 
-/* dc table 0 */
 
-const uint32_t ff_table0_dc_lum[120][2] = {
+const uint32_t ff_msmp4_dc_tables[2][2][120][2] = {
+{
+    /* dc table 0 */
+    {
     {     0x1,  1 }, {     0x1,  2 }, {     0x1,  4 }, {     0x1,  5 },
     {     0x5,  5 }, {     0x7,  5 }, {     0x8,  6 }, {     0xc,  6 },
     {     0x0,  7 }, {     0x2,  7 }, {    0x12,  7 }, {    0x1a,  7 },
@@ -115,9 +114,8 @@ const uint32_t ff_table0_dc_lum[120][2] = {
     { 0x60784, 24 }, { 0x60785, 24 }, { 0x60786, 24 }, { 0x60787, 24 },
     { 0x60788, 24 }, { 0x60789, 24 }, { 0x6078a, 24 }, { 0x6078b, 24 },
     { 0x6078c, 24 }, { 0x6078d, 24 }, { 0x6078e, 24 }, { 0x6078f, 24 },
-};
-
-const uint32_t ff_table0_dc_chroma[120][2] = {
+    },
+    {
     {      0x0,  2 }, {      0x1,  2 }, {      0x5,  3 }, {      0x9,  4 },
     {      0xd,  4 }, {     0x11,  5 }, {     0x1d,  5 }, {     0x1f,  5 },
     {     0x21,  6 }, {     0x31,  6 }, {     0x38,  6 }, {     0x33,  6 },
@@ -148,11 +146,11 @@ const uint32_t ff_table0_dc_chroma[120][2] = {
     { 0x608884, 23 }, { 0x608885, 23 }, { 0x608886, 23 }, { 0x608887, 23 },
     { 0x608888, 23 }, { 0x608889, 23 }, { 0x60888a, 23 }, { 0x60888b, 23 },
     { 0x60888c, 23 }, { 0x60888d, 23 }, { 0x60888e, 23 }, { 0x60888f, 23 },
-};
-
-/* dc table 1 */
-
-const uint32_t ff_table1_dc_lum[120][2] = {
+    }
+},
+{
+    /* dc table 1 */
+    {
     {      0x2,  2 }, {      0x3,  2 }, {      0x3,  3 }, {      0x2,  4 },
     {      0x5,  4 }, {      0x1,  5 }, {      0x3,  5 }, {      0x8,  5 },
     {      0x0,  6 }, {      0x5,  6 }, {      0xd,  6 }, {      0xf,  6 },
@@ -183,9 +181,8 @@ const uint32_t ff_table1_dc_lum[120][2] = {
     { 0x1e695c, 26 }, { 0x1e695d, 26 }, { 0x1e695e, 26 }, { 0x1e695f, 26 },
     { 0x1e6960, 26 }, { 0x1e6961, 26 }, { 0x1e6962, 26 }, { 0x1e6963, 26 },
     { 0x1e6964, 26 }, { 0x1e6965, 26 }, { 0x1e6966, 26 }, { 0x1e6967, 26 },
-};
-
-const uint32_t ff_table1_dc_chroma[120][2] = {
+    },
+    {
     {       0x0,  2 }, {       0x1,  2 }, {       0x4,  3 }, {       0x7,  3 },
     {       0xb,  4 }, {       0xd,  4 }, {      0x15,  5 }, {      0x28,  6 },
     {      0x30,  6 }, {      0x32,  6 }, {      0x52,  7 }, {      0x62,  7 },
@@ -216,6 +213,8 @@ const uint32_t ff_table1_dc_chroma[120][2] = {
     { 0x18f6484, 25 }, { 0x18f6485, 25 }, { 0x18f6486, 25 }, { 0x18f6487, 25 },
     { 0x18f6488, 25 }, { 0x18f6489, 25 }, { 0x18f648a, 25 }, { 0x18f648b, 25 },
     { 0x18f648c, 25 }, { 0x18f648d, 25 }, { 0x18f648e, 25 }, { 0x18f648f, 25 },
+    }
+}
 };
 
 const uint8_t ff_wmv1_scantable[WMV1_SCANTABLE_COUNT][64] = {
diff --git a/libavcodec/msmpeg4_vc1_data.h b/libavcodec/msmpeg4_vc1_data.h
index d12fcb032f..a92193a8f5 100644
--- a/libavcodec/msmpeg4_vc1_data.h
+++ b/libavcodec/msmpeg4_vc1_data.h
@@ -34,10 +34,9 @@ FF_VISIBILITY_PUSH_HIDDEN
 void ff_msmp4_vc1_vlcs_init_once(void);
 
 #define MSMP4_MB_INTRA_VLC_BITS 9
-extern VLC ff_msmp4_mb_i_vlc;
+extern VLCElem ff_msmp4_mb_i_vlc[];
 #define MSMP4_DC_VLC_BITS 9
-extern VLC ff_msmp4_dc_luma_vlc[2];
-extern VLC ff_msmp4_dc_chroma_vlc[2];
+extern const VLCElem *ff_msmp4_dc_vlc[2 /* dc_table_index */][2 /* 0: luma, 1: chroma */];
 
 /* intra picture macroblock coded block pattern */
 extern const uint16_t ff_msmp4_mb_i_table[64][2];
@@ -46,10 +45,7 @@ extern const uint16_t ff_msmp4_mb_i_table[64][2];
 
 extern const uint8_t ff_wmv1_scantable[WMV1_SCANTABLE_COUNT][64];
 
-extern const uint32_t ff_table0_dc_lum[120][2];
-extern const uint32_t ff_table1_dc_lum[120][2];
-extern const uint32_t ff_table0_dc_chroma[120][2];
-extern const uint32_t ff_table1_dc_chroma[120][2];
+extern const uint32_t ff_msmp4_dc_tables[2 /* dc_table_index */][2 /* 0: luma, 1: chroma */][120][2];
 FF_VISIBILITY_POP_HIDDEN
 
 #endif /* AVCODEC_MSMPEG4_VC1_DATA_H */
diff --git a/libavcodec/msmpeg4dec.c b/libavcodec/msmpeg4dec.c
index e61045c2f9..716fc13529 100644
--- a/libavcodec/msmpeg4dec.c
+++ b/libavcodec/msmpeg4dec.c
@@ -237,7 +237,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
         cbp = code & 0x3f;
     } else {
         s->mb_intra = 1;
-        code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MSMP4_MB_INTRA_VLC_BITS, 2);
+        code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc, MSMP4_MB_INTRA_VLC_BITS, 2);
         /* predict coded block pattern */
         cbp = 0;
         for(i=0;i<6;i++) {
@@ -582,14 +582,9 @@ static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
             return -1;
         }
         level-=256;
-    }else{  //FIXME optimize use unified tables & index
-        if (n < 4) {
-            level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table,
-                             MSMP4_DC_VLC_BITS, 3);
-        } else {
-            level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table,
-                             MSMP4_DC_VLC_BITS, 3);
-        }
+    } else {
+        level = get_vlc2(&s->gb, ff_msmp4_dc_vlc[s->dc_table_index][n >= 4],
+                         MSMP4_DC_VLC_BITS, 3);
 
         if (level == DC_MAX) {
             level = get_bits(&s->gb, 8);
diff --git a/libavcodec/msmpeg4enc.c b/libavcodec/msmpeg4enc.c
index a8ddb8d8e1..119ea8f15e 100644
--- a/libavcodec/msmpeg4enc.c
+++ b/libavcodec/msmpeg4enc.c
@@ -544,19 +544,8 @@ static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr
         if (code > DC_MAX)
             code = DC_MAX;
 
-        if (s->dc_table_index == 0) {
-            if (n < 4) {
-                put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
-            } else {
-                put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
-            }
-        } else {
-            if (n < 4) {
-                put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
-            } else {
-                put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
-            }
-        }
+        put_bits(&s->pb, ff_msmp4_dc_tables[s->dc_table_index][n >= 4][code][1],
+                         ff_msmp4_dc_tables[s->dc_table_index][n >= 4][code][0]);
 
         if (code == DC_MAX)
             put_bits(&s->pb, 8, level);
diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index 1baa6a9bf6..751ea57617 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -592,13 +592,8 @@ static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
     int dcdiff, scale;
 
     /* Get DC differential */
-    if (n < 4) {
-        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table,
-                          MSMP4_DC_VLC_BITS, 3);
-    } else {
-        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table,
-                          MSMP4_DC_VLC_BITS, 3);
-    }
+    dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_vlc[s->dc_table_index][n >= 4],
+                      MSMP4_DC_VLC_BITS, 3);
     if (dcdiff) {
         const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
         if (dcdiff == 119 /* ESC index value */) {
@@ -738,13 +733,8 @@ static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
     int quant = FFABS(mquant);
 
     /* Get DC differential */
-    if (n < 4) {
-        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table,
-                          MSMP4_DC_VLC_BITS, 3);
-    } else {
-        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table,
-                          MSMP4_DC_VLC_BITS, 3);
-    }
+    dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_vlc[s->dc_table_index][n >= 4],
+                      MSMP4_DC_VLC_BITS, 3);
     if (dcdiff) {
         const int m = (quant == 1 || quant == 2) ? 3 - quant : 0;
         if (dcdiff == 119 /* ESC index value */) {
@@ -940,13 +930,8 @@ static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
     s->c_dc_scale = s->c_dc_scale_table[quant];
 
     /* Get DC differential */
-    if (n < 4) {
-        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table,
-                          MSMP4_DC_VLC_BITS, 3);
-    } else {
-        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table,
-                          MSMP4_DC_VLC_BITS, 3);
-    }
+    dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_vlc[s->dc_table_index][n >= 4],
+                      MSMP4_DC_VLC_BITS, 3);
     if (dcdiff) {
         const int m = (quant == 1 || quant == 2) ? 3 - quant : 0;
         if (dcdiff == 119 /* ESC index value */) {
@@ -2591,7 +2576,7 @@ static void vc1_decode_i_blocks(VC1Context *v)
             }
 
             // do actual MB decoding and displaying
-            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table,
+            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc,
                            MSMP4_MB_INTRA_VLC_BITS, 2);
             v->s.ac_pred = get_bits1(&v->s.gb);
 
@@ -2727,7 +2712,7 @@ static int vc1_decode_i_blocks_adv(VC1Context *v)
                 return 0;
             }
 
-            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table,
+            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc,
                            MSMP4_MB_INTRA_VLC_BITS, 2);
             if (v->acpred_is_raw)
                 v->s.ac_pred = get_bits1(&v->s.gb);
diff --git a/libavcodec/wmv2dec.c b/libavcodec/wmv2dec.c
index 469b2c4b2b..d7c193455d 100644
--- a/libavcodec/wmv2dec.c
+++ b/libavcodec/wmv2dec.c
@@ -482,7 +482,7 @@ static int wmv2_decode_mb(MpegEncContext *s, int16_t block[6][64])
         s->mb_intra = 1;
         if (get_bits_left(&s->gb) <= 0)
             return AVERROR_INVALIDDATA;
-        code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table,
+        code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc,
                         MSMP4_MB_INTRA_VLC_BITS, 2);
         /* predict coded block pattern */
         cbp = 0;
-- 
2.34.1



More information about the ffmpeg-devel mailing list