[FFmpeg-devel] [PATCH v2 086/162] avcodec/mpeg4video: Make tables used to initialize VLCs smaller

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Fri Nov 20 09:19:59 EET 2020


Switching from ff_init_vlc_sparse() to ff_init_vlc_from_lengths()
allows to replace codes which are so long that they need to be stored
in an uint16_t by symbols which fit into an uint8_t; and even these can
be avoided in case of the sprite trajectory VLC.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/mpeg4data.h     | 150 +++++++++++++++----------------------
 libavcodec/mpeg4video.h    |   8 +-
 libavcodec/mpeg4videodec.c |  31 ++++----
 3 files changed, 80 insertions(+), 109 deletions(-)

diff --git a/libavcodec/mpeg4data.h b/libavcodec/mpeg4data.h
index 4756e9ea1d..30179d9801 100644
--- a/libavcodec/mpeg4data.h
+++ b/libavcodec/mpeg4data.h
@@ -323,10 +323,8 @@ RLTable ff_rvlc_rl_intra = {
     intra_rvlc_level,
 };
 
-const uint16_t ff_sprite_trajectory_tab[15][2] = {
- {0x00, 2}, {0x02, 3},  {0x03, 3},  {0x04, 3}, {0x05, 3}, {0x06, 3},
- {0x0E, 4}, {0x1E, 5},  {0x3E, 6},  {0x7E, 7}, {0xFE, 8},
- {0x1FE, 9},{0x3FE, 10},{0x7FE, 11},{0xFFE, 12},
+const uint8_t ff_sprite_trajectory_lens[15] = {
+    2,  3,  3,  3,  3,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,
 };
 
 const uint8_t ff_mb_type_b_tab[4][2] = {
@@ -374,119 +372,91 @@ const uint8_t ff_mpeg4_dc_threshold[8]={
 };
 
 /* Note these are different in studio mode */
-const uint16_t ff_mpeg4_studio_dc_luma[19][2]={
-    {0x0e,  6}, {0x06,  5}, {0x00,  4}, {0x02,  4},
-    {0x07,  3}, {0x05,  3}, {0x03,  3}, {0x02,  3},
-    {0x04,  3}, {0x06,  3}, {0x01,  4}, {0x1e,  7},
-    {0x3e,  8}, {0x7e,  9}, {0xfe, 10}, {0x1fe, 11},
-    {0x3fe, 12}, {0x7fe, 13}, {0x7ff, 13}
+const uint8_t ff_mpeg4_studio_dc_luma[19][2] = {
+    {  2,  4 }, { 10,  4 }, {  3,  4 }, {  1,  5 }, {  0,  6 }, { 11,  7 },
+    { 12,  8 }, { 13,  9 }, { 14, 10 }, { 15, 11 }, { 16, 12 }, { 17, 13 },
+    { 18, 13 }, {  7,  3 }, {  6,  3 }, {  8,  3 }, {  5,  3 }, {  9,  3 },
+    {  4,  3 },
 };
 
-const uint16_t ff_mpeg4_studio_dc_chroma[19][2]={
-    {0x00,  4}, {0x02,  4}, {0x07,  3}, {0x05,  3},
-    {0x03,  3}, {0x02,  3}, {0x04,  3}, {0x06,  3},
-    {0x01,  4}, {0x06,  5}, {0x0e,  6}, {0x1e,  7},
-    {0x3e,  8}, {0x7e,  9}, {0xfe, 10}, {0x1fe, 11},
-    {0x3fe, 12}, {0x7fe, 13}, {0x7ff, 13}
+const uint8_t ff_mpeg4_studio_dc_chroma[19][2] = {
+    {  0,  4 }, {  8,  4 }, {  1,  4 }, {  9,  5 }, { 10,  6 }, { 11,  7 },
+    { 12,  8 }, { 13,  9 }, { 14, 10 }, { 15, 11 }, { 16, 12 }, { 17, 13 },
+    { 18, 13 }, {  5,  3 }, {  4,  3 }, {  6,  3 }, {  3,  3 }, {  7,  3 },
+    {  2,  3 },
 };
 
-const uint16_t ff_mpeg4_studio_intra[12][22][2]={
+const uint8_t ff_mpeg4_studio_intra[12][24][2] = {
     {
-        {0x05,  4}, {0x04,  4}, {0x05,  7}, {0x09,  9},
-        {0x21, 11}, {0x41, 12}, {0x81, 13}, {0x03,  4},
-        {0x03,  5}, {0x05,  6}, {0x04,  7}, {0x03,  7},
-        {0x05,  8}, {0x03,  2}, {0x05,  3}, {0x04,  3},
-        {0x03,  3}, {0x02,  4}, {0x04,  6}, {0x03,  6},
-        {0x11, 10}, {0x80, 13}
+        {  0, -6 }, { 21, 13 }, {  6, 13 }, {  5, 12 }, {  4, 11 }, { 20, 10 },
+        {  3,  9 }, { 12,  8 }, { 11,  7 }, { 10,  7 }, {  2,  7 }, { 19,  6 },
+        { 18,  6 }, {  9,  6 }, {  8,  5 }, { 17,  4 }, {  7,  4 }, {  1,  4 },
+        {  0,  4 }, { 16,  3 }, { 15,  3 }, { 14,  3 }, { 13,  2 },
     },
     {
-        {0x00,  0}, {0x00,  0}, {0x00,  0}, {0x00,  0},
-        {0x00,  0}, {0x00,  0}, {0x00,  0}, {0x00,  0},
-        {0x00,  0}, {0x00,  0}, {0x00,  0}, {0x00,  0},
-        {0x00,  0}, {0x00,  0}, {0x01,  1}, {0x01,  2},
-        {0x01,  3}, {0x01,  4}, {0x01,  5}, {0x03,  7},
-        {0x05,  8}, {0x04,  8}
+        {  0, -6 }, { 21,  8 }, { 20,  8 }, { 19,  7 }, { 18,  5 }, { 17,  4 },
+        { 16,  3 }, { 15,  2 }, { 14,  1 },
     },
     {
-        {0x05,  3},  {0x03,  5},  {0x02,  5},  {0x03,  7},
-        {0x09,  9},  {0x103, 14}, {0x102, 14}, {0x04,  3},
-        {0x03,  3},  {0x03,  4},  {0x02,  4},  {0x03,  6},
-        {0x11, 10},  {0x03,  2},  {0x02,  3},  {0x02,  6},
-        {0x05,  8},  {0x21, 11},  {0x83, 13},  {0x101, 14},
-        {0x201, 15}, {0x82, 13}
+        {  0, -6 }, {  0,-15 }, { 20, 15 }, { 19, 14 }, {  6, 14 }, {  5, 14 },
+        { 21, 13 }, { 18, 13 }, { 17, 11 }, { 12, 10 }, {  4,  9 }, { 16,  8 },
+        {  3,  7 }, { 15,  6 }, { 11,  6 }, {  2,  5 }, {  1,  5 }, { 10,  4 },
+        {  9,  4 }, { 14,  3 }, {  8,  3 }, {  7,  3 }, {  0,  3 }, { 13,  2 },
     },
     {
-        {0x05,  5}, {0x05,  4}, {0x04,  5}, {0x03,  6},
-        {0x09,  9}, {0x83, 13}, {0x82, 13}, {0x03,  3},
-        {0x04,  4}, {0x03,  4}, {0x03,  5}, {0x05,  8},
-        {0x81, 13}, {0x03,  2}, {0x02,  2}, {0x02,  5},
-        {0x02,  6}, {0x03,  7}, {0x11, 10}, {0x43, 12},
-        {0x80, 13}, {0x42, 12}
+        {  0, -6 }, { 20, 13 }, { 12, 13 }, {  6, 13 }, {  5, 13 }, { 21, 12 },
+        { 19, 12 }, { 18, 10 }, {  4,  9 }, { 11,  8 }, { 17,  7 }, { 16,  6 },
+        {  3,  6 }, { 15,  5 }, { 10,  5 }, {  2,  5 }, {  0,  5 }, {  9,  4 },
+        {  8,  4 }, {  1,  4 }, {  7,  3 }, { 14,  2 }, { 13,  2 },
     },
     {
-        {0x05,  7},  {0x03,  4}, {0x03,  5},  {0x04,  7},
-        {0x09,  9},  {0x83, 13}, {0x101, 14}, {0x03,  3},
-        {0x02,  4},  {0x05,  6}, {0x03,  7},  {0x11, 10},
-        {0x201, 15}, {0x03,  2}, {0x02,  2},  {0x02,  3},
-        {0x04,  6},  {0x03,  6}, {0x05,  8},  {0x21, 11},
-        {0x82, 13},  {0x81, 13}
+        {  0, -6 }, {  0,-15 }, { 12, 15 }, {  6, 14 }, { 21, 13 }, { 20, 13 },
+        {  5, 13 }, { 19, 11 }, { 11, 10 }, {  4,  9 }, { 18,  8 }, { 10,  7 },
+        {  3,  7 }, {  0,  7 }, { 17,  6 }, { 16,  6 }, {  9,  6 }, {  2,  5 },
+        {  8,  4 }, {  1,  4 }, { 15,  3 }, {  7,  3 }, { 14,  2 }, { 13,  2 },
     },
     {
-        {0x13, 10},  {0x03,  5}, {0x05,  7}, {0x12, 10},
-        {0x43, 12},  {0x83, 13}, {0x82, 13}, {0x02,  5},
-        {0x04,  7},  {0x05,  8}, {0x23, 11}, {0x81, 13},
-        {0x101, 14}, {0x03,  2}, {0x02,  2}, {0x01,  2},
-        {0x01,  3},  {0x03,  6}, {0x03,  7}, {0x22, 11},
-        {0x201, 15}, {0x42, 12}
+        {  0, -6 }, {  0,-15 }, { 20, 15 }, { 12, 14 }, { 11, 13 }, {  6, 13 },
+        {  5, 13 }, { 21, 12 }, {  4, 12 }, { 19, 11 }, { 10, 11 }, {  3, 10 },
+        {  0, 10 }, {  9,  8 }, { 18,  7 }, {  8,  7 }, {  2,  7 }, { 17,  6 },
+        {  7,  5 }, {  1,  5 }, { 16,  3 }, { 15,  2 }, { 14,  2 }, { 13,  2 },
     },
     {
-        {0x23, 11},  {0x01,  4},  {0x07,  8},  {0x13, 10},
-        {0x22, 11},  {0x103, 14}, {0x102, 14}, {0x03,  6},
-        {0x06,  8},  {0x12, 10},  {0x43, 12},  {0x101, 14},
-        {0x201, 15}, {0x03,  3},  {0x02,  3},  {0x03,  2},
-        {0x02,  2},  {0x01,  3},  {0x02,  6},  {0x05,  8},
-        {0x42, 12},  {0x41, 12}
+        {  0, -6 }, {  0,-15 }, { 12, 15 }, { 11, 14 }, {  6, 14 }, {  5, 14 },
+        { 21, 12 }, { 20, 12 }, { 10, 12 }, {  4, 11 }, {  0, 11 }, {  9, 10 },
+        {  3, 10 }, { 19,  8 }, {  8,  8 }, {  2,  8 }, { 18,  6 }, {  7,  6 },
+        {  1,  4 }, { 17,  3 }, { 14,  3 }, { 13,  3 }, { 16,  2 }, { 15,  2 },
     },
     {
-        {0x0b,  9}, {0x03,  5}, {0x07,  8}, {0x07,  7},
-        {0x06,  7}, {0x23, 11}, {0x41, 12}, {0x05,  7},
-        {0x06,  8}, {0x0a,  9}, {0x13, 10}, {0x22, 11},
-        {0x40, 12}, {0x03,  4}, {0x02,  4}, {0x03,  2},
-        {0x02,  2}, {0x01,  2}, {0x02,  5}, {0x04,  7},
-        {0x12, 10}, {0x21, 11}
+        {  0, -6 }, { 12, 12 }, {  6, 12 }, { 21, 11 }, { 11, 11 }, {  5, 11 },
+        { 20, 10 }, { 10, 10 }, {  9,  9 }, {  0,  9 }, {  8,  8 }, {  2,  8 },
+        { 19,  7 }, {  7,  7 }, {  4,  7 }, {  3,  7 }, { 18,  5 }, {  1,  5 },
+        { 14,  4 }, { 13,  4 }, { 17,  2 }, { 16,  2 }, { 15,  2 },
     },
     {
-        {0x15, 10}, {0x03,  6}, {0x14, 10}, {0x23, 11},
-        {0x07,  8}, {0x43, 12}, {0x81, 13}, {0x06,  8},
-        {0x0b,  9}, {0x13, 10}, {0x12, 10}, {0x42, 12},
-        {0x80, 13}, {0x01,  4}, {0x03,  3}, {0x02,  3},
-        {0x03,  2}, {0x02,  2}, {0x01,  3}, {0x02,  6},
-        {0x22, 11}, {0x41, 12}
+        {  0, -6 }, { 12, 13 }, {  6, 13 }, { 21, 12 }, { 11, 12 }, {  5, 12 },
+        { 20, 11 }, {  3, 11 }, { 10, 10 }, {  9, 10 }, {  2, 10 }, {  0, 10 },
+        {  8,  9 }, {  7,  8 }, {  4,  8 }, { 19,  6 }, {  1,  6 }, { 13,  4 },
+        { 18,  3 }, { 15,  3 }, { 14,  3 }, { 17,  2 }, { 16,  2 },
     },
     {
-        {0x43, 12}, {0x05,  6}, {0x07,  8}, {0x04,  6},
-        {0x03,  6}, {0x13, 10}, {0x42, 12}, {0x05,  7},
-        {0x04,  7}, {0x06,  8}, {0x12, 10}, {0x41, 12},
-        {0x40, 12}, {0x03,  5}, {0x03,  4}, {0x03,  3},
-        {0x02,  3}, {0x03,  2}, {0x02,  2}, {0x02,  4},
-        {0x05,  8}, {0x11, 10}
+        {  0, -6 }, { 12, 12 }, { 11, 12 }, {  6, 12 }, {  0, 12 }, { 21, 10 },
+        { 10, 10 }, {  5, 10 }, { 20,  8 }, {  9,  8 }, {  2,  8 }, {  8,  7 },
+        {  7,  7 }, {  4,  6 }, {  3,  6 }, {  1,  6 }, { 13,  5 }, { 19,  4 },
+        { 14,  4 }, { 16,  3 }, { 15,  3 }, { 18,  2 }, { 17,  2 },
     },
     {
-        {0x83, 13}, {0x05,  7}, {0x07,  8}, {0x03,  4},
-        {0x21, 11}, {0x82, 13}, {0x81, 13}, {0x04,  7},
-        {0x06,  8}, {0x0b,  9}, {0x0a,  9}, {0x11, 10},
-        {0x80, 13}, {0x03,  5}, {0x02,  5}, {0x02,  4},
-        {0x03,  3}, {0x02,  3}, {0x03,  2}, {0x02,  2},
-        {0x03,  6}, {0x09,  9}
+        {  0, -6 }, { 12, 13 }, {  6, 13 }, {  5, 13 }, {  0, 13 }, {  4, 11 },
+        { 11, 10 }, { 21,  9 }, { 10,  9 }, {  9,  9 }, {  8,  8 }, {  2,  8 },
+        {  7,  7 }, {  1,  7 }, { 20,  6 }, { 14,  5 }, { 13,  5 }, { 15,  4 },
+        {  3,  4 }, { 17,  3 }, { 16,  3 }, { 19,  2 }, { 18,  2 },
     },
     {
-        {0x13, 10}, {0x03,  5}, {0x03,  6}, {0x0d,  9},
-        {0x0c,  9}, {0x21, 11}, {0x20, 11}, {0x02,  5},
-        {0x02,  6}, {0x07,  8}, {0x0b,  9}, {0x12, 10},
-        {0x11, 10}, {0x05,  3}, {0x04,  3}, {0x05,  4},
-        {0x04,  4}, {0x03,  4}, {0x02,  4}, {0x03,  3},
-        {0x03,  2}, {0x0a,  9}
-    }
+        {  0, -6 }, {  6, 11 }, {  5, 11 }, { 12, 10 }, { 11, 10 }, {  0, 10 },
+        { 21,  9 }, { 10,  9 }, {  4,  9 }, {  3,  9 }, {  9,  8 }, {  8,  6 },
+        {  2,  6 }, {  7,  5 }, {  1,  5 }, { 18,  4 }, { 17,  4 }, { 16,  4 },
+        { 15,  4 }, { 19,  3 }, { 14,  3 }, { 13,  3 }, { 20,  2 },
+    },
 };
 
 #endif /* AVCODEC_MPEG4DATA_H */
diff --git a/libavcodec/mpeg4video.h b/libavcodec/mpeg4video.h
index 1a5da31928..3de598465f 100644
--- a/libavcodec/mpeg4video.h
+++ b/libavcodec/mpeg4video.h
@@ -138,7 +138,7 @@ extern RLTable ff_mpeg4_rl_intra;
 extern RLTable ff_rvlc_rl_inter;
 extern RLTable ff_rvlc_rl_intra;
 
-extern const uint16_t ff_sprite_trajectory_tab[15][2];
+extern const uint8_t ff_sprite_trajectory_lens[15];
 extern const uint8_t ff_mb_type_b_tab[4][2];
 
 /* these matrixes will be permuted for the idct */
@@ -151,9 +151,9 @@ extern const uint16_t ff_mpeg4_resync_prefix[8];
 
 extern const uint8_t ff_mpeg4_dc_threshold[8];
 
-extern const uint16_t ff_mpeg4_studio_dc_luma[19][2];
-extern const uint16_t ff_mpeg4_studio_dc_chroma[19][2];
-extern const uint16_t ff_mpeg4_studio_intra[12][22][2];
+extern const uint8_t ff_mpeg4_studio_dc_luma[19][2];
+extern const uint8_t ff_mpeg4_studio_dc_chroma[19][2];
+extern const uint8_t ff_mpeg4_studio_intra[12][24][2];
 
 void ff_mpeg4_encode_mb(MpegEncContext *s,
                         int16_t block[6][64],
diff --git a/libavcodec/mpeg4videodec.c b/libavcodec/mpeg4videodec.c
index ff5c6acf67..cc3c36d722 100644
--- a/libavcodec/mpeg4videodec.c
+++ b/libavcodec/mpeg4videodec.c
@@ -3394,9 +3394,9 @@ av_cold void ff_mpeg4videodec_static_init(void) {
         INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
                         &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
                         &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
-        INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
-                        &ff_sprite_trajectory_tab[0][1], 4, 2,
-                        &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
+        INIT_VLC_STATIC_FROM_LENGTHS(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
+                                     ff_sprite_trajectory_lens, 1,
+                                     NULL, 0, 0, 0, 0, 128);
         INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
                         &ff_mb_type_b_tab[0][1], 2, 1,
                         &ff_mb_type_b_tab[0][0], 2, 1, 16);
@@ -3506,26 +3506,27 @@ static av_cold int init_studio_vlcs(Mpeg4DecContext *ctx)
     int i, ret;
 
     for (i = 0; i < 12; i++) {
-        ret = init_vlc(&ctx->studio_intra_tab[i], STUDIO_INTRA_BITS, 22,
-                       &ff_mpeg4_studio_intra[i][0][1], 4, 2,
-                       &ff_mpeg4_studio_intra[i][0][0], 4, 2,
-                       0);
+        ret = ff_init_vlc_from_lengths(&ctx->studio_intra_tab[i],
+                                       STUDIO_INTRA_BITS, 24,
+                                       &ff_mpeg4_studio_intra[i][0][1], 2,
+                                       &ff_mpeg4_studio_intra[i][0][0], 2, 1,
+                                       0, 0, NULL);
 
         if (ret < 0)
             return ret;
     }
 
-    ret = init_vlc(&ctx->studio_luma_dc, STUDIO_INTRA_BITS, 19,
-                   &ff_mpeg4_studio_dc_luma[0][1], 4, 2,
-                   &ff_mpeg4_studio_dc_luma[0][0], 4, 2,
-                   0);
+    ret = ff_init_vlc_from_lengths(&ctx->studio_luma_dc, STUDIO_INTRA_BITS, 19,
+                                   &ff_mpeg4_studio_dc_luma[0][1], 2,
+                                   &ff_mpeg4_studio_dc_luma[0][0], 2, 1,
+                                   0, 0, NULL);
     if (ret < 0)
         return ret;
 
-    ret = init_vlc(&ctx->studio_chroma_dc, STUDIO_INTRA_BITS, 19,
-                   &ff_mpeg4_studio_dc_chroma[0][1], 4, 2,
-                   &ff_mpeg4_studio_dc_chroma[0][0], 4, 2,
-                   0);
+    ret = ff_init_vlc_from_lengths(&ctx->studio_chroma_dc, STUDIO_INTRA_BITS, 19,
+                                   &ff_mpeg4_studio_dc_chroma[0][1], 2,
+                                   &ff_mpeg4_studio_dc_chroma[0][0], 2, 1,
+                                   0, 0, NULL);
     if (ret < 0)
         return ret;
 
-- 
2.25.1



More information about the ffmpeg-devel mailing list