diff mbox series

[FFmpeg-devel,087/114] avcodec/vc1: Avoid code duplication when initializing VLCs

Message ID 20201110104851.321029-88-andreas.rheinhardt@gmail.com
State Superseded
Headers show
Series VLC, esp. init_vlc patches
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Nov. 10, 2020, 10:48 a.m. UTC
This has been achieved by switching those VLCs that still used
ff_init_vlc_sparse() to ff_init_vlc_lengths() even though the codes
tables used uint8_t in these cases. But it allows to use one auxiliary
function to initialize the VLCs and by using tables that interleave
symbols and lengths said function only needs one parameter for the
tables, not two. The only table that uses an uint16_t symbols table
still has to be treated specially for this reason.

The offsets table has been removed as a byproduct of these changes.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/vc1.c     | 158 +++++++++++++++---------------------------
 libavcodec/vc1data.c | 159 +++++++++++++++++++++----------------------
 libavcodec/vc1data.h |  21 ++----
 3 files changed, 138 insertions(+), 200 deletions(-)

Comments

Jerome Borsboom Nov. 18, 2020, 2:08 p.m. UTC | #1
I don't think it is a good idea to change the format of the table to 
something different than the tables in the SMPTE VC-1 documentation. 
People fluent in this documentation have a hard time correlating the new 
tables to the 'official' ones.

---
Regards,
Jerome

> This has been achieved by switching those VLCs that still used
> ff_init_vlc_sparse() to ff_init_vlc_lengths() even though the codes
> tables used uint8_t in these cases. But it allows to use one auxiliary
> function to initialize the VLCs and by using tables that interleave
> symbols and lengths said function only needs one parameter for the
> tables, not two. The only table that uses an uint16_t symbols table
> still has to be treated specially for this reason.
> 
> The offsets table has been removed as a byproduct of these changes.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
> ---
>  libavcodec/vc1.c     | 158 +++++++++++++++---------------------------
>  libavcodec/vc1data.c | 159 +++++++++++++++++++++----------------------
>  libavcodec/vc1data.h |  21 ++----
>  3 files changed, 138 insertions(+), 200 deletions(-)
> 
...
>  
>  /* 4MV Block pattern VLC tables */
> -const uint8_t ff_vc1_4mv_block_pattern_codes[4][16] = {
> -    { 14, 58, 59, 25, 12, 26, 15, 15, 13, 24, 27,  0, 28,  1,  2,  2 },
> -    {  8, 18, 19,  4, 20,  5, 30, 11, 21, 31,  6, 12,  7, 13, 14,  0 },
> -    { 15,  6,  7,  2,  8,  3, 28,  9, 10, 29,  4, 11,  5, 12, 13,  0 },
> -    {  0, 11, 12,  4, 13,  5, 30, 16, 14, 31,  6, 17,  7, 18, 19, 10 }
> -};
> -const uint8_t ff_vc1_4mv_block_pattern_bits[4][16] = {
> -    { 5, 6, 6, 5, 5, 5, 5, 4, 5, 5, 5, 3, 5, 3, 3, 2 },
> -    { 4, 5, 5, 4, 5, 4, 5, 4, 5, 5, 4, 4, 4, 4, 4, 2 },
> -    { 4, 4, 4, 4, 4, 4, 5, 4, 4, 5, 4, 4, 4, 4, 4, 3 },
> -    { 2, 4, 4, 4, 4, 4, 5, 5, 4, 5, 4, 5, 4, 5, 5, 4 }
> +const uint8_t ff_vc1_4mv_block_pattern_tabs[4][16][2] = {
> +    {
> +        { 0x0B, 3 }, { 0x0D, 3 }, { 0x0E, 3 }, { 0x04, 5 }, { 0x08, 5 },
> +        { 0x00, 5 }, { 0x06, 5 }, { 0x0F, 2 }, { 0x09, 5 }, { 0x03, 5 },
> +        { 0x05, 5 }, { 0x0A, 5 }, { 0x0C, 5 }, { 0x01, 6 }, { 0x02, 6 },
> +        { 0x07, 4 },
> +    },
> +    {
> +        { 0x0F, 2 }, { 0x03, 4 }, { 0x05, 4 }, { 0x0A, 4 }, { 0x0C, 4 },
> +        { 0x00, 4 }, { 0x01, 5 }, { 0x02, 5 }, { 0x04, 5 }, { 0x08, 5 },
> +        { 0x07, 4 }, { 0x0B, 4 }, { 0x0D, 4 }, { 0x0E, 4 }, { 0x06, 5 },
> +        { 0x09, 5 },
> +    },
> +    {
> +        { 0x0F, 3 }, { 0x03, 4 }, { 0x05, 4 }, { 0x0A, 4 }, { 0x0C, 4 },
> +        { 0x01, 4 }, { 0x02, 4 }, { 0x04, 4 }, { 0x07, 4 }, { 0x08, 4 },
> +        { 0x0B, 4 }, { 0x0D, 4 }, { 0x0E, 4 }, { 0x06, 5 }, { 0x09, 5 },
> +        { 0x00, 4 },
> +    },
> +    {
> +        { 0x00, 2 }, { 0x03, 4 }, { 0x05, 4 }, { 0x0A, 4 }, { 0x0C, 4 },
> +        { 0x07, 5 }, { 0x0B, 5 }, { 0x0D, 5 }, { 0x0E, 5 }, { 0x0F, 4 },
> +        { 0x01, 4 }, { 0x02, 4 }, { 0x04, 4 }, { 0x08, 4 }, { 0x06, 5 },
> +        { 0x09, 5 },
> +    },
>  };
>  
>  /* 2MV Block pattern VLC tables */
> -const uint8_t ff_vc1_2mv_block_pattern_codes[4][4] = {
> -    { 2, 1, 0, 3 }, { 1, 0, 2, 3 }, { 2, 0, 3, 1 }, { 1, 3, 2, 0 }
> -};
> -
> -const uint8_t ff_vc1_2mv_block_pattern_bits[4][4] = {
> -    { 2, 2, 2, 2 }, { 1, 2, 3, 3 }, { 3, 2, 3, 1 }, { 1, 3, 3, 2 }
> +const uint8_t ff_vc1_2mv_block_pattern_tabs[4][4][2] = {
> +    { { 0x02, 2 }, { 0x01, 2 }, { 0x00, 2 }, { 0x03, 2 } },
> +    { { 0x01, 2 }, { 0x02, 3 }, { 0x03, 3 }, { 0x00, 1 } },
> +    { { 0x01, 2 }, { 0x00, 3 }, { 0x02, 3 }, { 0x03, 1 } },
> +    { { 0x03, 2 }, { 0x02, 3 }, { 0x01, 3 }, { 0x00, 1 } },
>  };
>  
>  /* Interlaced frame picture 4MV MBMODE VLC tables (tables 160-163) */
> @@ -350,46 +349,42 @@ const uint8_t ff_vc1_intfr_non4mv_mbmode_tabs[4][9][2] = {
>  
>  /* Interlaced field picture MBMODE VLC tables (p. 356 - 11.4.1, 11.4.2) */
>  /* mixed-MV */
> -const uint8_t ff_vc1_if_mmv_mbmode_codes[8][8] = {
> -    { 16, 17,  3,  3,  0,  5,  9,  2 },
> -    {  8,  9,  3,  6,  7,  0,  5,  2 },
> -    { 16, 17,  5,  3,  0,  3,  9,  2 },
> -    { 56, 57, 15,  4,  5,  6, 29,  0 },
> -    { 52, 53, 27, 14, 15,  2, 12,  0 },
> -    { 56, 57, 29,  5,  6,  0, 15,  4 },
> -    { 16, 17,  6,  7,  0,  1,  9,  5 },
> -    { 56, 57,  0,  5,  6, 29,  4, 15 }
> -};
> -const uint8_t ff_vc1_if_mmv_mbmode_bits[8][8] = {
> -    { 6, 6, 2, 3, 2, 4, 5, 2 },
> -    { 5, 5, 3, 3, 3, 2, 4, 2 },
> -    { 6, 6, 4, 3, 2, 2, 5, 2 },
> -    { 6, 6, 4, 3, 3, 3, 5, 1 },
> -    { 6, 6, 5, 4, 4, 2, 4, 1 },
> -    { 6, 6, 5, 3, 3, 1, 4, 3 },
> -    { 5, 5, 3, 3, 2, 2, 4, 3 },
> -    { 6, 6, 1, 3, 3, 5, 3, 4 }
> +const uint8_t ff_vc1_if_mmv_mbmode_tabs[8][8][2] = {
> +    { { 0x04, 2 }, { 0x00, 6 }, { 0x01, 6 }, { 0x06, 5 },
> +      { 0x05, 4 }, { 0x03, 3 }, { 0x07, 2 }, { 0x02, 2 } },
> +    { { 0x05, 2 }, { 0x00, 5 }, { 0x01, 5 }, { 0x06, 4 },
> +      { 0x02, 3 }, { 0x07, 2 }, { 0x03, 3 }, { 0x04, 3 } },
> +    { { 0x04, 2 }, { 0x00, 6 }, { 0x01, 6 }, { 0x06, 5 },
> +      { 0x02, 4 }, { 0x03, 3 }, { 0x07, 2 }, { 0x05, 2 } },
> +    { { 0x07, 1 }, { 0x03, 3 }, { 0x04, 3 }, { 0x05, 3 },
> +      { 0x00, 6 }, { 0x01, 6 }, { 0x06, 5 }, { 0x02, 4 } },
> +    { { 0x07, 1 }, { 0x05, 2 }, { 0x06, 4 }, { 0x00, 6 },
> +      { 0x01, 6 }, { 0x02, 5 }, { 0x03, 4 }, { 0x04, 4 } },
> +    { { 0x05, 1 }, { 0x07, 3 }, { 0x03, 3 }, { 0x04, 3 },
> +      { 0x00, 6 }, { 0x01, 6 }, { 0x02, 5 }, { 0x06, 4 } },
> +    { { 0x04, 2 }, { 0x05, 2 }, { 0x00, 5 }, { 0x01, 5 },
> +      { 0x06, 4 }, { 0x07, 3 }, { 0x02, 3 }, { 0x03, 3 } },
> +    { { 0x02, 1 }, { 0x06, 3 }, { 0x03, 3 }, { 0x04, 3 },
> +      { 0x00, 6 }, { 0x01, 6 }, { 0x05, 5 }, { 0x07, 4 } },
>  };
>  /* 1MV */
> -const uint8_t ff_vc1_if_1mv_mbmode_codes[8][6] = {
> -    {  0,  1,  1,  1,  1,  1 },
> -    {  0,  1,  1,  1,  1,  1 },
> -    { 16, 17,  3,  0,  9,  5 },
> -    { 20, 21,  3, 11,  0,  4 },
> -    {  4,  5,  2,  3,  3,  0 },
> -    {  4,  5,  3,  2,  0,  3 },
> -    {  0,  1,  1,  1,  1,  1 },
> -    { 16, 17,  9,  5,  3,  0 }
> -};
> -const uint8_t ff_vc1_if_1mv_mbmode_bits[8][6] = {
> -    { 5, 5, 1, 3, 2, 4 },
> -    { 5, 5, 1, 2, 3, 4 },
> -    { 5, 5, 2, 1, 4, 3 },
> -    { 5, 5, 2, 4, 1, 3 },
> -    { 4, 4, 2, 3, 2, 2 },
> -    { 4, 4, 3, 2, 2, 2 },
> -    { 5, 5, 3, 4, 1, 2 },
> -    { 5, 5, 4, 3, 2, 1 }
> +const uint8_t ff_vc1_if_1mv_mbmode_tabs[8][6][2] = {
> +    { { 0x00, 5 }, { 0x01, 5 }, { 0x05, 4 },
> +      { 0x03, 3 }, { 0x04, 2 }, { 0x02, 1 } },
> +    { { 0x00, 5 }, { 0x01, 5 }, { 0x05, 4 },
> +      { 0x04, 3 }, { 0x03, 2 }, { 0x02, 1 } },
> +    { { 0x03, 1 }, { 0x00, 5 }, { 0x01, 5 },
> +      { 0x04, 4 }, { 0x05, 3 }, { 0x02, 2 } },
> +    { { 0x04, 1 }, { 0x05, 3 }, { 0x00, 5 },
> +      { 0x01, 5 }, { 0x03, 4 }, { 0x02, 2 } },
> +    { { 0x05, 2 }, { 0x00, 4 }, { 0x01, 4 },
> +      { 0x03, 3 }, { 0x02, 2 }, { 0x04, 2 } },
> +    { { 0x04, 2 }, { 0x00, 4 }, { 0x01, 4 },
> +      { 0x02, 3 }, { 0x03, 2 }, { 0x05, 2 } },
> +    { { 0x00, 5 }, { 0x01, 5 }, { 0x03, 4 },
> +      { 0x02, 3 }, { 0x05, 2 }, { 0x04, 1 } },
> +    { { 0x05, 1 }, { 0x00, 5 }, { 0x01, 5 },
> +      { 0x02, 4 }, { 0x03, 3 }, { 0x04, 2 } },
>  };
...
> -- 
> 2.25.1
diff mbox series

Patch

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index dedabaea6c..9db8063c12 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -1345,15 +1345,20 @@  int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
     return 0;
 }
 
-static const uint16_t vlc_offs[] = {
-        0,   520,   552,   616,  1128,  1160,  1224,  1740,  1772,  1836,  1900,  2436,
-     2986,  3050,  3610,  4154,  4218,  4746,  5326,  5390,  5902,  6554,  7658,  8342,
-     9304,  9988, 10630, 11234, 12174, 13006, 13560, 14232, 14786, 15432, 16350, 17522,
-    20372, 21818, 22330, 22394, 23166, 23678, 23742, 24820, 25332, 25396, 26460, 26980,
-    27048, 27592, 27600, 27608, 27616, 27624, 28224, 28258, 28290, 28802, 28834, 28866,
-    29378, 29412, 29444, 29960, 29994, 30026, 30538, 30572, 30604, 31120, 31154, 31186,
-    31714, 31746, 31778, 32306, 32340, 32372
-};
+static VLC_TYPE vlc_buf[33080][2];
+
+static av_cold void vc1_init_vlc(VLC *vlc, int nb_bits, int nb_codes,
+                                 unsigned *buf_offset, const uint8_t (*tab)[2],
+                                 int offset)
+{
+    vlc->table           = &vlc_buf[*buf_offset];
+    vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *buf_offset;
+
+    ff_init_vlc_from_lengths(vlc, nb_bits, nb_codes,
+                             &tab[0][1], 2, &tab[0][0], 2, 1,
+                             offset, INIT_VLC_USE_NEW_STATIC);
+    *buf_offset += vlc->table_size;
+}
 
 /**
  * Init VC-1 specific tables and VC1Context members
@@ -1364,129 +1369,74 @@  av_cold int ff_vc1_init_common(VC1Context *v)
 {
     static int done = 0;
     int i = 0;
-    static VLC_TYPE vlc_table[32372][2];
 
     v->hrd_rate = v->hrd_buffer = NULL;
 
     /* VLC tables */
     if (!done) {
-        INIT_VLC_STATIC(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
-                        ff_vc1_bfraction_bits, 1, 1,
-                        ff_vc1_bfraction_codes, 1, 1, 1 << VC1_BFRACTION_VLC_BITS);
-        INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
-                        ff_vc1_norm2_bits, 1, 1,
-                        ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS);
-        INIT_VLC_STATIC_FROM_LENGTHS(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 77,
-                                     &ff_vc1_norm6_tab[0][1], 2,
-                                     &ff_vc1_norm6_tab[0][0], 2, 1, 0, 0, 556);
-        INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
-                        ff_vc1_imode_bits, 1, 1,
-                        ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS);
+        unsigned offset = 0;
+        vc1_init_vlc(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
+                     &offset, ff_vc1_bfraction_tab, 0);
+        vc1_init_vlc(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
+                     &offset, ff_vc1_norm2_tab, 0);
+        vc1_init_vlc(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 77,
+                     &offset, ff_vc1_norm6_tab, 0);
+        vc1_init_vlc(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
+                     &offset, ff_vc1_imode_tab, 0);
         for (i = 0; i < 3; i++) {
-            ff_vc1_ttmb_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 0]];
-            ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i * 3 + 1] - vlc_offs[i * 3 + 0];
-            ff_init_vlc_from_lengths(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
-                                     &ff_vc1_ttmb_tabs[i][0][1], 2,
-                                     &ff_vc1_ttmb_tabs[i][0][0], 2, 1,
-                                     0, INIT_VLC_USE_NEW_STATIC);
-            ff_vc1_ttblk_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 1]];
-            ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i * 3 + 2] - vlc_offs[i * 3 + 1];
-            ff_init_vlc_from_lengths(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
-                                     &ff_vc1_ttblk_tabs[i][0][1], 2,
-                                     &ff_vc1_ttblk_tabs[i][0][0], 2, 1,
-                                     0, INIT_VLC_USE_NEW_STATIC);
-            ff_vc1_subblkpat_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 2]];
-            ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i * 3 + 3] - vlc_offs[i * 3 + 2];
-            ff_init_vlc_from_lengths(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
-                                     &ff_vc1_subblkpat_tabs[i][0][1], 2,
-                                     &ff_vc1_subblkpat_tabs[i][0][0], 2, 1,
-                                     0, INIT_VLC_USE_NEW_STATIC);
+            vc1_init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
+                         &offset, ff_vc1_ttmb_tabs[i], 0);
+            vc1_init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
+                         &offset, ff_vc1_ttblk_tabs[i], 0);
+            vc1_init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
+                         &offset, ff_vc1_subblkpat_tabs[i], 0);
         }
         for (i = 0; i < 4; i++) {
-            ff_vc1_4mv_block_pattern_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 9]];
-            ff_vc1_4mv_block_pattern_vlc[i].table_allocated = vlc_offs[i * 3 + 10] - vlc_offs[i * 3 + 9];
-            init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
-                     ff_vc1_4mv_block_pattern_bits[i], 1, 1,
-                     ff_vc1_4mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
-            ff_vc1_cbpcy_p_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 10]];
-            ff_vc1_cbpcy_p_vlc[i].table_allocated = vlc_offs[i * 3 + 11] - vlc_offs[i * 3 + 10];
-            ff_init_vlc_from_lengths(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
-                                     &ff_vc1_cbpcy_p_tabs[i][0][1], 2,
-                                     &ff_vc1_cbpcy_p_tabs[i][0][0], 2, 1,
-                                     0, INIT_VLC_USE_NEW_STATIC);
-            ff_vc1_mv_diff_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 11]];
-            ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i * 3 + 12] - vlc_offs[i * 3 + 11];
-            ff_init_vlc_from_lengths(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
-                                     &ff_vc1_mv_diff_tabs[i][0][1], 2,
-                                     &ff_vc1_mv_diff_tabs[i][0][0], 2, 1,
-                                     -4, INIT_VLC_USE_NEW_STATIC);
+            vc1_init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
+                         &offset, ff_vc1_4mv_block_pattern_tabs[i], 0);
+            vc1_init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
+                         &offset, ff_vc1_cbpcy_p_tabs[i], 0);
+            vc1_init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
+                         &offset, ff_vc1_mv_diff_tabs[i], -4);
         }
         for (int i = 0, ac_offset = 0; i < 8; i++) {
-            ff_vc1_ac_coeff_table[i].table           = &vlc_table[vlc_offs[i * 2 + 21]];
-            ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i * 2 + 22] - vlc_offs[i * 2 + 21];
+            ff_vc1_ac_coeff_table[i].table           = &vlc_buf[offset];
+            ff_vc1_ac_coeff_table[i].table_allocated = FF_ARRAY_ELEMS(vlc_buf) - offset;
             ff_init_vlc_from_lengths(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, ff_vc1_ac_sizes[i],
                                      ff_vc1_ac_coeff_lens + ac_offset, 1,
                                      ff_vc1_ac_coeff_syms + ac_offset, 2, 2,
                                      0, INIT_VLC_USE_NEW_STATIC);
             ac_offset += ff_vc1_ac_sizes[i];
+            offset    += ff_vc1_ac_coeff_table[i].table_size;
             /* initialize interlaced MVDATA tables (2-Ref) */
-            ff_vc1_2ref_mvdata_vlc[i].table           = &vlc_table[vlc_offs[i * 2 + 22]];
-            ff_vc1_2ref_mvdata_vlc[i].table_allocated = vlc_offs[i * 2 + 23] - vlc_offs[i * 2 + 22];
-            ff_init_vlc_from_lengths(&ff_vc1_2ref_mvdata_vlc[i], VC1_2REF_MVDATA_VLC_BITS, 126,
-                                     &ff_vc1_2ref_mvdata_tabs[i][0][1], 2,
-                                     &ff_vc1_2ref_mvdata_tabs[i][0][0], 2, 1,
-                                     0, INIT_VLC_USE_NEW_STATIC);
+            vc1_init_vlc(&ff_vc1_2ref_mvdata_vlc[i], VC1_2REF_MVDATA_VLC_BITS, 126,
+                         &offset, ff_vc1_2ref_mvdata_tabs[i], 0);
         }
         for (i = 0; i < 4; i++) {
             /* initialize 4MV MBMODE VLC tables for interlaced frame P picture */
-            ff_vc1_intfr_4mv_mbmode_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 37]];
-            ff_vc1_intfr_4mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 38] - vlc_offs[i * 3 + 37];
-            ff_init_vlc_from_lengths(&ff_vc1_intfr_4mv_mbmode_vlc[i], VC1_INTFR_4MV_MBMODE_VLC_BITS, 15,
-                                     &ff_vc1_intfr_4mv_mbmode_tabs[i][0][1], 2,
-                                     &ff_vc1_intfr_4mv_mbmode_tabs[i][0][0], 2, 1,
-                                     0, INIT_VLC_USE_NEW_STATIC);
+            vc1_init_vlc(&ff_vc1_intfr_4mv_mbmode_vlc[i], VC1_INTFR_4MV_MBMODE_VLC_BITS, 15,
+                         &offset, ff_vc1_intfr_4mv_mbmode_tabs[i], 0);
             /* initialize NON-4MV MBMODE VLC tables for the same */
-            ff_vc1_intfr_non4mv_mbmode_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 38]];
-            ff_vc1_intfr_non4mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 39] - vlc_offs[i * 3 + 38];
-            ff_init_vlc_from_lengths(&ff_vc1_intfr_non4mv_mbmode_vlc[i], VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 9,
-                                     &ff_vc1_intfr_non4mv_mbmode_tabs[i][0][1], 2,
-                                     &ff_vc1_intfr_non4mv_mbmode_tabs[i][0][0], 2, 1,
-                                     0, INIT_VLC_USE_NEW_STATIC);
+            vc1_init_vlc(&ff_vc1_intfr_non4mv_mbmode_vlc[i], VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 9,
+                         &offset, ff_vc1_intfr_non4mv_mbmode_tabs[i], 0);
             /* initialize interlaced MVDATA tables (1-Ref) */
-            ff_vc1_1ref_mvdata_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 39]];
-            ff_vc1_1ref_mvdata_vlc[i].table_allocated = vlc_offs[i * 3 + 40] - vlc_offs[i * 3 + 39];
-            ff_init_vlc_from_lengths(&ff_vc1_1ref_mvdata_vlc[i], VC1_1REF_MVDATA_VLC_BITS, 72,
-                                     &ff_vc1_1ref_mvdata_tabs[i][0][1], 2,
-                                     &ff_vc1_1ref_mvdata_tabs[i][0][0], 2, 1,
-                                     0, INIT_VLC_USE_NEW_STATIC);
+            vc1_init_vlc(&ff_vc1_1ref_mvdata_vlc[i], VC1_1REF_MVDATA_VLC_BITS, 72,
+                         &offset, ff_vc1_1ref_mvdata_tabs[i], 0);
         }
         for (i = 0; i < 4; i++) {
             /* Initialize 2MV Block pattern VLC tables */
-            ff_vc1_2mv_block_pattern_vlc[i].table           = &vlc_table[vlc_offs[i + 49]];
-            ff_vc1_2mv_block_pattern_vlc[i].table_allocated = vlc_offs[i + 50] - vlc_offs[i + 49];
-            init_vlc(&ff_vc1_2mv_block_pattern_vlc[i], VC1_2MV_BLOCK_PATTERN_VLC_BITS, 4,
-                     ff_vc1_2mv_block_pattern_bits[i], 1, 1,
-                     ff_vc1_2mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
+            vc1_init_vlc(&ff_vc1_2mv_block_pattern_vlc[i], VC1_2MV_BLOCK_PATTERN_VLC_BITS, 4,
+                         &offset, ff_vc1_2mv_block_pattern_tabs[i], 0);
         }
         for (i = 0; i < 8; i++) {
             /* Initialize interlaced CBPCY VLC tables (Table 124 - Table 131) */
-            ff_vc1_icbpcy_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 53]];
-            ff_vc1_icbpcy_vlc[i].table_allocated = vlc_offs[i * 3 + 54] - vlc_offs[i * 3 + 53];
-            ff_init_vlc_from_lengths(&ff_vc1_icbpcy_vlc[i], VC1_ICBPCY_VLC_BITS, 63,
-                                     &ff_vc1_icbpcy_p_tabs[i][0][1], 2,
-                                     &ff_vc1_icbpcy_p_tabs[i][0][0], 2, 1,
-                                     0, INIT_VLC_USE_NEW_STATIC);
+            vc1_init_vlc(&ff_vc1_icbpcy_vlc[i], VC1_ICBPCY_VLC_BITS, 63,
+                         &offset, ff_vc1_icbpcy_p_tabs[i], 0);
             /* Initialize interlaced field picture MBMODE VLC tables */
-            ff_vc1_if_mmv_mbmode_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 54]];
-            ff_vc1_if_mmv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 55] - vlc_offs[i * 3 + 54];
-            init_vlc(&ff_vc1_if_mmv_mbmode_vlc[i], VC1_IF_MMV_MBMODE_VLC_BITS, 8,
-                     ff_vc1_if_mmv_mbmode_bits[i], 1, 1,
-                     ff_vc1_if_mmv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
-            ff_vc1_if_1mv_mbmode_vlc[i].table           = &vlc_table[vlc_offs[i * 3 + 55]];
-            ff_vc1_if_1mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 56] - vlc_offs[i * 3 + 55];
-            init_vlc(&ff_vc1_if_1mv_mbmode_vlc[i], VC1_IF_1MV_MBMODE_VLC_BITS, 6,
-                     ff_vc1_if_1mv_mbmode_bits[i], 1, 1,
-                     ff_vc1_if_1mv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
+            vc1_init_vlc(&ff_vc1_if_mmv_mbmode_vlc[i], VC1_IF_MMV_MBMODE_VLC_BITS, 8,
+                         &offset, ff_vc1_if_mmv_mbmode_tabs[i], 0);
+            vc1_init_vlc(&ff_vc1_if_1mv_mbmode_vlc[i], VC1_IF_1MV_MBMODE_VLC_BITS, 6,
+                         &offset, ff_vc1_if_1mv_mbmode_tabs[i], 0);
         }
         done = 1;
     }
diff --git a/libavcodec/vc1data.c b/libavcodec/vc1data.c
index ce9948281f..c7c409bb46 100644
--- a/libavcodec/vc1data.c
+++ b/libavcodec/vc1data.c
@@ -129,23 +129,14 @@  const int16_t ff_vc1_bfraction_lut[23] = {
 };
 #endif
 
-const uint8_t ff_vc1_bfraction_bits[23] = {
-    3, 3, 3, 3,
-    3, 3, 3,
-    7, 7, 7, 7,
-    7, 7, 7, 7,
-    7, 7, 7, 7,
-    7, 7,
-    7, 7
-};
-const uint8_t ff_vc1_bfraction_codes[23] = {
-      0,   1,   2,   3,
-      4,   5,   6,
-    112, 113, 114, 115,
-    116, 117, 118, 119,
-    120, 121, 122, 123,
-    124, 125,
-    126, 127
+const uint8_t ff_vc1_bfraction_tab[23][2] = {
+    {  0, 3 }, {  1, 3 }, {  2, 3 }, {  3, 3 },
+    {  4, 3 }, {  5, 3 }, {  6, 3 },
+    {  7, 7 }, {  8, 7 }, {  9, 7 }, { 10, 7 },
+    { 11, 7 }, { 12, 7 }, { 13, 7 }, { 14, 7 },
+    { 15, 7 }, { 16, 7 }, { 17, 7 }, { 18, 7 },
+    { 19, 7 }, { 20, 7 },
+    { 21, 7 }, { 22, 7 },
 };
 
 //Same as H.264
@@ -169,19 +160,14 @@  const AVRational ff_vc1_pixel_aspect[16] = {
 };
 
 /* BitPlane IMODE - such a small table... */
-const uint8_t ff_vc1_imode_codes[7] = {
-    0, 2, 1, 3, 1, 2, 3
-};
-const uint8_t ff_vc1_imode_bits[7] = {
-    4, 2, 3, 2, 4, 3, 3
+const uint8_t ff_vc1_imode_tab[7][2] = {
+    { IMODE_RAW, 4 }, { IMODE_DIFF6, 4 }, { IMODE_DIFF2, 3 }, { IMODE_ROWSKIP, 3 },
+    { IMODE_COLSKIP, 3 }, { IMODE_NORM2, 2 }, { IMODE_NORM6, 2 },
 };
 
 /* Normal-2 imode */
-const uint8_t ff_vc1_norm2_codes[4] = {
-    0, 4, 5, 3
-};
-const uint8_t ff_vc1_norm2_bits[4] = {
-    1, 3, 3, 2
+const uint8_t ff_vc1_norm2_tab[4][2] = {
+    { 0, 1 }, { 1, 3 }, { 2, 3 }, { 3, 2 },
 };
 
 /* The negative lengths in the following table correspond to
@@ -206,26 +192,39 @@  const uint8_t ff_vc1_norm6_tab[77][2] = {
 };
 
 /* 4MV Block pattern VLC tables */
-const uint8_t ff_vc1_4mv_block_pattern_codes[4][16] = {
-    { 14, 58, 59, 25, 12, 26, 15, 15, 13, 24, 27,  0, 28,  1,  2,  2 },
-    {  8, 18, 19,  4, 20,  5, 30, 11, 21, 31,  6, 12,  7, 13, 14,  0 },
-    { 15,  6,  7,  2,  8,  3, 28,  9, 10, 29,  4, 11,  5, 12, 13,  0 },
-    {  0, 11, 12,  4, 13,  5, 30, 16, 14, 31,  6, 17,  7, 18, 19, 10 }
-};
-const uint8_t ff_vc1_4mv_block_pattern_bits[4][16] = {
-    { 5, 6, 6, 5, 5, 5, 5, 4, 5, 5, 5, 3, 5, 3, 3, 2 },
-    { 4, 5, 5, 4, 5, 4, 5, 4, 5, 5, 4, 4, 4, 4, 4, 2 },
-    { 4, 4, 4, 4, 4, 4, 5, 4, 4, 5, 4, 4, 4, 4, 4, 3 },
-    { 2, 4, 4, 4, 4, 4, 5, 5, 4, 5, 4, 5, 4, 5, 5, 4 }
+const uint8_t ff_vc1_4mv_block_pattern_tabs[4][16][2] = {
+    {
+        { 0x0B, 3 }, { 0x0D, 3 }, { 0x0E, 3 }, { 0x04, 5 }, { 0x08, 5 },
+        { 0x00, 5 }, { 0x06, 5 }, { 0x0F, 2 }, { 0x09, 5 }, { 0x03, 5 },
+        { 0x05, 5 }, { 0x0A, 5 }, { 0x0C, 5 }, { 0x01, 6 }, { 0x02, 6 },
+        { 0x07, 4 },
+    },
+    {
+        { 0x0F, 2 }, { 0x03, 4 }, { 0x05, 4 }, { 0x0A, 4 }, { 0x0C, 4 },
+        { 0x00, 4 }, { 0x01, 5 }, { 0x02, 5 }, { 0x04, 5 }, { 0x08, 5 },
+        { 0x07, 4 }, { 0x0B, 4 }, { 0x0D, 4 }, { 0x0E, 4 }, { 0x06, 5 },
+        { 0x09, 5 },
+    },
+    {
+        { 0x0F, 3 }, { 0x03, 4 }, { 0x05, 4 }, { 0x0A, 4 }, { 0x0C, 4 },
+        { 0x01, 4 }, { 0x02, 4 }, { 0x04, 4 }, { 0x07, 4 }, { 0x08, 4 },
+        { 0x0B, 4 }, { 0x0D, 4 }, { 0x0E, 4 }, { 0x06, 5 }, { 0x09, 5 },
+        { 0x00, 4 },
+    },
+    {
+        { 0x00, 2 }, { 0x03, 4 }, { 0x05, 4 }, { 0x0A, 4 }, { 0x0C, 4 },
+        { 0x07, 5 }, { 0x0B, 5 }, { 0x0D, 5 }, { 0x0E, 5 }, { 0x0F, 4 },
+        { 0x01, 4 }, { 0x02, 4 }, { 0x04, 4 }, { 0x08, 4 }, { 0x06, 5 },
+        { 0x09, 5 },
+    },
 };
 
 /* 2MV Block pattern VLC tables */
-const uint8_t ff_vc1_2mv_block_pattern_codes[4][4] = {
-    { 2, 1, 0, 3 }, { 1, 0, 2, 3 }, { 2, 0, 3, 1 }, { 1, 3, 2, 0 }
-};
-
-const uint8_t ff_vc1_2mv_block_pattern_bits[4][4] = {
-    { 2, 2, 2, 2 }, { 1, 2, 3, 3 }, { 3, 2, 3, 1 }, { 1, 3, 3, 2 }
+const uint8_t ff_vc1_2mv_block_pattern_tabs[4][4][2] = {
+    { { 0x02, 2 }, { 0x01, 2 }, { 0x00, 2 }, { 0x03, 2 } },
+    { { 0x01, 2 }, { 0x02, 3 }, { 0x03, 3 }, { 0x00, 1 } },
+    { { 0x01, 2 }, { 0x00, 3 }, { 0x02, 3 }, { 0x03, 1 } },
+    { { 0x03, 2 }, { 0x02, 3 }, { 0x01, 3 }, { 0x00, 1 } },
 };
 
 /* Interlaced frame picture 4MV MBMODE VLC tables (tables 160-163) */
@@ -350,46 +349,42 @@  const uint8_t ff_vc1_intfr_non4mv_mbmode_tabs[4][9][2] = {
 
 /* Interlaced field picture MBMODE VLC tables (p. 356 - 11.4.1, 11.4.2) */
 /* mixed-MV */
-const uint8_t ff_vc1_if_mmv_mbmode_codes[8][8] = {
-    { 16, 17,  3,  3,  0,  5,  9,  2 },
-    {  8,  9,  3,  6,  7,  0,  5,  2 },
-    { 16, 17,  5,  3,  0,  3,  9,  2 },
-    { 56, 57, 15,  4,  5,  6, 29,  0 },
-    { 52, 53, 27, 14, 15,  2, 12,  0 },
-    { 56, 57, 29,  5,  6,  0, 15,  4 },
-    { 16, 17,  6,  7,  0,  1,  9,  5 },
-    { 56, 57,  0,  5,  6, 29,  4, 15 }
-};
-const uint8_t ff_vc1_if_mmv_mbmode_bits[8][8] = {
-    { 6, 6, 2, 3, 2, 4, 5, 2 },
-    { 5, 5, 3, 3, 3, 2, 4, 2 },
-    { 6, 6, 4, 3, 2, 2, 5, 2 },
-    { 6, 6, 4, 3, 3, 3, 5, 1 },
-    { 6, 6, 5, 4, 4, 2, 4, 1 },
-    { 6, 6, 5, 3, 3, 1, 4, 3 },
-    { 5, 5, 3, 3, 2, 2, 4, 3 },
-    { 6, 6, 1, 3, 3, 5, 3, 4 }
+const uint8_t ff_vc1_if_mmv_mbmode_tabs[8][8][2] = {
+    { { 0x04, 2 }, { 0x00, 6 }, { 0x01, 6 }, { 0x06, 5 },
+      { 0x05, 4 }, { 0x03, 3 }, { 0x07, 2 }, { 0x02, 2 } },
+    { { 0x05, 2 }, { 0x00, 5 }, { 0x01, 5 }, { 0x06, 4 },
+      { 0x02, 3 }, { 0x07, 2 }, { 0x03, 3 }, { 0x04, 3 } },
+    { { 0x04, 2 }, { 0x00, 6 }, { 0x01, 6 }, { 0x06, 5 },
+      { 0x02, 4 }, { 0x03, 3 }, { 0x07, 2 }, { 0x05, 2 } },
+    { { 0x07, 1 }, { 0x03, 3 }, { 0x04, 3 }, { 0x05, 3 },
+      { 0x00, 6 }, { 0x01, 6 }, { 0x06, 5 }, { 0x02, 4 } },
+    { { 0x07, 1 }, { 0x05, 2 }, { 0x06, 4 }, { 0x00, 6 },
+      { 0x01, 6 }, { 0x02, 5 }, { 0x03, 4 }, { 0x04, 4 } },
+    { { 0x05, 1 }, { 0x07, 3 }, { 0x03, 3 }, { 0x04, 3 },
+      { 0x00, 6 }, { 0x01, 6 }, { 0x02, 5 }, { 0x06, 4 } },
+    { { 0x04, 2 }, { 0x05, 2 }, { 0x00, 5 }, { 0x01, 5 },
+      { 0x06, 4 }, { 0x07, 3 }, { 0x02, 3 }, { 0x03, 3 } },
+    { { 0x02, 1 }, { 0x06, 3 }, { 0x03, 3 }, { 0x04, 3 },
+      { 0x00, 6 }, { 0x01, 6 }, { 0x05, 5 }, { 0x07, 4 } },
 };
 /* 1MV */
-const uint8_t ff_vc1_if_1mv_mbmode_codes[8][6] = {
-    {  0,  1,  1,  1,  1,  1 },
-    {  0,  1,  1,  1,  1,  1 },
-    { 16, 17,  3,  0,  9,  5 },
-    { 20, 21,  3, 11,  0,  4 },
-    {  4,  5,  2,  3,  3,  0 },
-    {  4,  5,  3,  2,  0,  3 },
-    {  0,  1,  1,  1,  1,  1 },
-    { 16, 17,  9,  5,  3,  0 }
-};
-const uint8_t ff_vc1_if_1mv_mbmode_bits[8][6] = {
-    { 5, 5, 1, 3, 2, 4 },
-    { 5, 5, 1, 2, 3, 4 },
-    { 5, 5, 2, 1, 4, 3 },
-    { 5, 5, 2, 4, 1, 3 },
-    { 4, 4, 2, 3, 2, 2 },
-    { 4, 4, 3, 2, 2, 2 },
-    { 5, 5, 3, 4, 1, 2 },
-    { 5, 5, 4, 3, 2, 1 }
+const uint8_t ff_vc1_if_1mv_mbmode_tabs[8][6][2] = {
+    { { 0x00, 5 }, { 0x01, 5 }, { 0x05, 4 },
+      { 0x03, 3 }, { 0x04, 2 }, { 0x02, 1 } },
+    { { 0x00, 5 }, { 0x01, 5 }, { 0x05, 4 },
+      { 0x04, 3 }, { 0x03, 2 }, { 0x02, 1 } },
+    { { 0x03, 1 }, { 0x00, 5 }, { 0x01, 5 },
+      { 0x04, 4 }, { 0x05, 3 }, { 0x02, 2 } },
+    { { 0x04, 1 }, { 0x05, 3 }, { 0x00, 5 },
+      { 0x01, 5 }, { 0x03, 4 }, { 0x02, 2 } },
+    { { 0x05, 2 }, { 0x00, 4 }, { 0x01, 4 },
+      { 0x03, 3 }, { 0x02, 2 }, { 0x04, 2 } },
+    { { 0x04, 2 }, { 0x00, 4 }, { 0x01, 4 },
+      { 0x02, 3 }, { 0x03, 2 }, { 0x05, 2 } },
+    { { 0x00, 5 }, { 0x01, 5 }, { 0x03, 4 },
+      { 0x02, 3 }, { 0x05, 2 }, { 0x04, 1 } },
+    { { 0x05, 1 }, { 0x00, 5 }, { 0x01, 5 },
+      { 0x02, 4 }, { 0x03, 3 }, { 0x04, 2 } },
 };
 
 /* Interlaced frame/field picture MVDATA VLC tables */
diff --git a/libavcodec/vc1data.h b/libavcodec/vc1data.h
index 257ec2fd30..c64e3725a8 100644
--- a/libavcodec/vc1data.h
+++ b/libavcodec/vc1data.h
@@ -94,30 +94,25 @@  extern VLC ff_vc1_ac_coeff_table[8];
 
 /* pre-computed scales for all bfractions and base=256 */
 extern const int16_t ff_vc1_bfraction_lut[23];
-extern const uint8_t ff_vc1_bfraction_bits[23];
-extern const uint8_t ff_vc1_bfraction_codes[23];
+extern const uint8_t ff_vc1_bfraction_tab[23][2];
 
 //Same as H.264
 extern const AVRational ff_vc1_pixel_aspect[16];
 
 /* BitPlane IMODE - such a small table... */
-extern const uint8_t ff_vc1_imode_codes[7];
-extern const uint8_t ff_vc1_imode_bits[7];
+extern const uint8_t ff_vc1_imode_tab[7][2];
 
 /* Normal-2 imode */
-extern const uint8_t ff_vc1_norm2_codes[4];
-extern const uint8_t ff_vc1_norm2_bits[4];
+extern const uint8_t ff_vc1_norm2_tab[4][2];
 extern const uint8_t ff_vc1_norm6_tab[77][2];
 /* Normal-6 imode */
 extern const uint8_t ff_vc1_norm6_spec[64][5];
 
 /* 4MV Block pattern VLC tables */
-extern const uint8_t ff_vc1_4mv_block_pattern_codes[4][16];
-extern const uint8_t ff_vc1_4mv_block_pattern_bits[4][16];
+extern const uint8_t ff_vc1_4mv_block_pattern_tabs[4][16][2];
 
 /* 2MV Block pattern VLC tables */
-extern const uint8_t ff_vc1_2mv_block_pattern_codes[4][4];
-extern const uint8_t ff_vc1_2mv_block_pattern_bits[4][4];
+extern const uint8_t ff_vc1_2mv_block_pattern_tabs[4][4][2];
 
 extern const uint8_t ff_wmv3_dc_scale_table[32];
 
@@ -158,10 +153,8 @@  extern const uint8_t ff_vc1_intfr_4mv_mbmode_tabs[4][15][2];
 extern const uint8_t ff_vc1_intfr_non4mv_mbmode_tabs[4][9][2];
 
 /* Interlaced field picture MBMODE VLC tables (p. 356 - 11.4.1, 11.4.2) */
-extern const uint8_t ff_vc1_if_mmv_mbmode_codes[8][8];
-extern const uint8_t ff_vc1_if_mmv_mbmode_bits[8][8];
-extern const uint8_t ff_vc1_if_1mv_mbmode_codes[8][6];
-extern const uint8_t ff_vc1_if_1mv_mbmode_bits[8][6];
+extern const uint8_t ff_vc1_if_mmv_mbmode_tabs[8][8][2];
+extern const uint8_t ff_vc1_if_1mv_mbmode_tabs[8][6][2];
 
 /* Interlaced frame/field picture MVDATA VLC tables
  * The escape value of each table has the symbol 0;