diff mbox series

[FFmpeg-devel,v2,051/162] avcodec/mpc8: Reduce the size of the length tables to initialize VLCs

Message ID 20201120072116.818090-52-andreas.rheinhardt@gmail.com
State New
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. 20, 2020, 7:19 a.m. UTC
After permuting both length, code as well as symbol tables so that
the codes are ordered from left to right in the tree, it became apparent
that the length of the codes decreases from left to right. Therefore one
can run-length encode the lengths to save space. This commit implements
this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/mpc8.c     | 114 +++++++++++++++--------------
 libavcodec/mpc8huff.h | 166 ++++++++++++------------------------------
 2 files changed, 104 insertions(+), 176 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/mpc8.c b/libavcodec/mpc8.c
index b5f89c845b..64b1967d8d 100644
--- a/libavcodec/mpc8.c
+++ b/libavcodec/mpc8.c
@@ -90,6 +90,21 @@  static const uint16_t vlc_offsets[13] = {
     0, 640, 1184, 1748, 2298, 2426, 2554, 3066, 3578, 4106, 4618, 5196, 5708
 };
 
+static av_cold void build_vlc(VLC *vlc, int nb_bits,
+                              const uint8_t codes_counts[16],
+                              const uint8_t syms[], int offset)
+{
+    uint8_t len[MPC8_MAX_VLC_SIZE];
+    unsigned num = 0;
+
+    for (int i = 16; i > 0; i--)
+        for (unsigned tmp = num + codes_counts[i - 1]; num < tmp; num++)
+            len[num] = i;
+
+    ff_init_vlc_from_lengths(vlc, nb_bits, num, len, 1,
+                             syms, 1, 1, offset, INIT_VLC_USE_NEW_STATIC, NULL);
+}
+
 static av_cold int mpc8_decode_init(AVCodecContext * avctx)
 {
     int i;
@@ -97,7 +112,6 @@  static av_cold int mpc8_decode_init(AVCodecContext * avctx)
     GetBitContext gb;
     static int vlc_initialized = 0;
     int channels;
-
     static VLC_TYPE codes_table[5708][2];
 
     if(avctx->extradata_size < 2){
@@ -133,77 +147,65 @@  static av_cold int mpc8_decode_init(AVCodecContext * avctx)
     if(vlc_initialized) return 0;
     av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
 
-    INIT_VLC_STATIC_FROM_LENGTHS(&band_vlc, MPC8_BANDS_BITS, MPC8_BANDS_SIZE,
-                                 mpc8_bands_bits, 1,
-                                 mpc8_bands_syms, 1, 1, 0, 0, 542);
-
-    INIT_VLC_STATIC_FROM_LENGTHS(&q1_vlc, MPC8_Q1_BITS, MPC8_Q1_SIZE,
-                                 mpc8_q1_bits, 1,
-                                 mpc8_q1_syms, 1, 1, 0, 0, 520);
-    INIT_VLC_STATIC_FROM_LENGTHS(&q9up_vlc, MPC8_Q9UP_BITS, MPC8_Q9UP_SIZE,
-                                 mpc8_q9up_bits, 1,
-                                 mpc8_q9up_syms, 1, 1, 0, 0, 524);
-
-    INIT_VLC_STATIC_FROM_LENGTHS(&scfi_vlc[0], MPC8_SCFI0_BITS, MPC8_SCFI0_SIZE,
-                                 mpc8_scfi0_bits, 1,
-                                 mpc8_scfi0_syms, 1, 1, 0, 0, 1 << MPC8_SCFI0_BITS);
-    INIT_VLC_STATIC_FROM_LENGTHS(&scfi_vlc[1], MPC8_SCFI1_BITS, MPC8_SCFI1_SIZE,
-                                 mpc8_scfi1_bits, 1,
-                                 mpc8_scfi1_syms, 1, 1, 0, 0, 1 << MPC8_SCFI1_BITS);
-
-    INIT_VLC_STATIC_FROM_LENGTHS(&dscf_vlc[0], MPC8_DSCF0_BITS, MPC8_DSCF0_SIZE,
-                                 mpc8_dscf0_bits, 1,
-                                 mpc8_dscf0_syms, 1, 1, 0, 0, 560);
-    INIT_VLC_STATIC_FROM_LENGTHS(&dscf_vlc[1], MPC8_DSCF1_BITS, MPC8_DSCF1_SIZE,
-                                 mpc8_dscf1_bits, 1,
-                                 mpc8_dscf1_syms, 1, 1, 0, 0, 598);
-
-    INIT_VLC_STATIC_FROM_LENGTHS(&q3_vlc[0], MPC8_Q3_BITS, MPC8_Q3_SIZE,
-                                 mpc8_q3_bits, 1,
-                                 mpc8_q3_syms, 1, 1, MPC8_Q3_OFFSET, 0, 512);
-    INIT_VLC_STATIC_FROM_LENGTHS(&q3_vlc[1], MPC8_Q4_BITS, MPC8_Q4_SIZE,
-                                 mpc8_q4_bits, 1,
-                                 mpc8_q4_syms, 1, 1, MPC8_Q4_OFFSET, 0, 516);
+#define INIT_VLC(vlc, bits, len_counts, symbols, offset, static_size) \
+    do {                                                                   \
+        static VLC_TYPE table[static_size][2];                             \
+        (vlc)->table           = table;                                    \
+        (vlc)->table_allocated = static_size;                              \
+        build_vlc(vlc, bits, len_counts, symbols, offset);                 \
+    } while (0)
+
+
+    INIT_VLC(&band_vlc, MPC8_BANDS_BITS,
+             mpc8_bands_len_counts, mpc8_bands_syms, 0, 542);
+
+    INIT_VLC(&q1_vlc, MPC8_Q1_BITS,
+             mpc8_q1_len_counts, mpc8_q1_syms, 0, 520);
+    INIT_VLC(&q9up_vlc, MPC8_Q9UP_BITS,
+             mpc8_q9up_len_counts, mpc8_q9up_syms, 0, 524);
+
+    INIT_VLC(&scfi_vlc[0], MPC8_SCFI0_BITS,
+             mpc8_scfi_len_counts[0], mpc8_scfi0_syms, 0, 1 << MPC8_SCFI0_BITS);
+    INIT_VLC(&scfi_vlc[1], MPC8_SCFI1_BITS,
+             mpc8_scfi_len_counts[1], mpc8_scfi1_syms, 0, 1 << MPC8_SCFI1_BITS);
+
+    INIT_VLC(&dscf_vlc[0], MPC8_DSCF0_BITS,
+             mpc8_dscf_len_counts[0], mpc8_dscf0_syms, 0, 560);
+    INIT_VLC(&dscf_vlc[1], MPC8_DSCF1_BITS,
+             mpc8_dscf_len_counts[1], mpc8_dscf1_syms, 0, 598);
+
+    INIT_VLC(&q3_vlc[0], MPC8_Q3_BITS,
+             mpc8_q3_len_counts, mpc8_q3_syms, MPC8_Q3_OFFSET, 512);
+    INIT_VLC(&q3_vlc[1], MPC8_Q4_BITS,
+             mpc8_q4_len_counts, mpc8_q4_syms, MPC8_Q4_OFFSET, 516);
 
     for(i = 0; i < 2; i++){
         res_vlc[i].table = &codes_table[vlc_offsets[0+i]];
         res_vlc[i].table_allocated = vlc_offsets[1+i] - vlc_offsets[0+i];
-        ff_init_vlc_from_lengths(&res_vlc[i], MPC8_RES_BITS, MPC8_RES_SIZE,
-                                 mpc8_res_bits[i], 1,
-                                 mpc8_res_syms[i], 1, 1,
-                                 0, INIT_VLC_USE_NEW_STATIC, NULL);
+        build_vlc(&res_vlc[i], MPC8_RES_BITS,
+                  mpc8_res_len_counts[i], mpc8_res_syms[i], 0);
 
         q2_vlc[i].table = &codes_table[vlc_offsets[2+i]];
         q2_vlc[i].table_allocated = vlc_offsets[3+i] - vlc_offsets[2+i];
-        ff_init_vlc_from_lengths(&q2_vlc[i], MPC8_Q2_BITS, MPC8_Q2_SIZE,
-                                 mpc8_q2_bits[i], 1,
-                                 mpc8_q2_syms[i], 1, 1,
-                                 0, INIT_VLC_USE_NEW_STATIC, NULL);
+        build_vlc(&q2_vlc[i], MPC8_Q2_BITS,
+                  mpc8_q2_len_counts[i], mpc8_q2_syms[i], 0);
 
         quant_vlc[0][i].table = &codes_table[vlc_offsets[4+i]];
         quant_vlc[0][i].table_allocated = vlc_offsets[5+i] - vlc_offsets[4+i];
-        ff_init_vlc_from_lengths(&quant_vlc[0][i], MPC8_Q5_BITS, MPC8_Q5_SIZE,
-                                 mpc8_q5_bits[i], 1,
-                                 mpc8_q5_syms[i], 1, 1,
-                                 MPC8_Q5_OFFSET, INIT_VLC_USE_NEW_STATIC, NULL);
+        build_vlc(&quant_vlc[0][i], MPC8_Q5_BITS,
+                  mpc8_q5_len_counts[i], mpc8_q5_syms[i], MPC8_Q5_OFFSET);
         quant_vlc[1][i].table = &codes_table[vlc_offsets[6+i]];
         quant_vlc[1][i].table_allocated = vlc_offsets[7+i] - vlc_offsets[6+i];
-        ff_init_vlc_from_lengths(&quant_vlc[1][i], MPC8_Q6_BITS, MPC8_Q6_SIZE,
-                                 mpc8_q6_bits[i], 1,
-                                 mpc8_q6_syms[i], 1, 1,
-                                 MPC8_Q6_OFFSET, INIT_VLC_USE_NEW_STATIC, NULL);
+        build_vlc(&quant_vlc[1][i], MPC8_Q6_BITS,
+                  mpc8_q6_len_counts[i], mpc8_q6_syms[i], MPC8_Q6_OFFSET);
         quant_vlc[2][i].table = &codes_table[vlc_offsets[8+i]];
         quant_vlc[2][i].table_allocated = vlc_offsets[9+i] - vlc_offsets[8+i];
-        ff_init_vlc_from_lengths(&quant_vlc[2][i], MPC8_Q7_BITS, MPC8_Q7_SIZE,
-                                 mpc8_q7_bits[i], 1,
-                                 mpc8_q7_syms[i], 1, 1,
-                                 MPC8_Q7_OFFSET, INIT_VLC_USE_NEW_STATIC, NULL);
+        build_vlc(&quant_vlc[2][i], MPC8_Q7_BITS,
+                  mpc8_q7_len_counts[i], mpc8_q7_syms[i], MPC8_Q7_OFFSET);
         quant_vlc[3][i].table = &codes_table[vlc_offsets[10+i]];
         quant_vlc[3][i].table_allocated = vlc_offsets[11+i] - vlc_offsets[10+i];
-        ff_init_vlc_from_lengths(&quant_vlc[3][i], MPC8_Q8_BITS, MPC8_Q8_SIZE,
-                                 mpc8_q8_bits[i], 1,
-                                 mpc8_q8_syms[i], 1, 1,
-                                 MPC8_Q8_OFFSET, INIT_VLC_USE_NEW_STATIC, NULL);
+        build_vlc(&quant_vlc[3][i], MPC8_Q8_BITS,
+                  mpc8_q8_len_counts[i], mpc8_q8_syms[i], MPC8_Q8_OFFSET);
     }
     vlc_initialized = 1;
 
diff --git a/libavcodec/mpc8huff.h b/libavcodec/mpc8huff.h
index 687c9f7e98..8838751fd6 100644
--- a/libavcodec/mpc8huff.h
+++ b/libavcodec/mpc8huff.h
@@ -24,6 +24,8 @@ 
 
 #include <stdint.h>
 
+#define MPC8_MAX_VLC_SIZE 256
+
 #define MPC8_BANDS_SIZE 33
 #define MPC8_BANDS_BITS 9
 
@@ -31,9 +33,8 @@  static const uint8_t mpc8_bands_syms[MPC8_BANDS_SIZE] = {
     13, 19, 10, 11, 12, 14, 15, 16, 17, 18, 20, 21, 22,  9, 23, 24, 25,  8, 26,
     27,  7, 28,  5,  6, 29,  4,  3, 30,  2, 31,  1, 32,  0
 };
-static const uint8_t mpc8_bands_bits[MPC8_BANDS_SIZE] = {
-    13, 13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 10, 10,
-    10,  9,  9,  8,  8,  8,  7,  6,  6,  5,  5,  3,  2,  1
+static const uint8_t mpc8_bands_len_counts[16] = {
+    1,  1,  1,  0,  2,  2,  1,  3,  2,  3,  4, 11,  2,  0,  0,  0
 };
 
 #define MPC8_SCFI0_SIZE 4
@@ -42,9 +43,6 @@  static const uint8_t mpc8_bands_bits[MPC8_BANDS_SIZE] = {
 static const uint8_t mpc8_scfi0_syms[MPC8_SCFI0_SIZE] = {
      0,  1,  3,  2
 };
-static const uint8_t mpc8_scfi0_bits[MPC8_SCFI0_SIZE] = {
-     3,  3,  2,  1
-};
 
 #define MPC8_SCFI1_SIZE 16
 #define MPC8_SCFI1_BITS 7
@@ -52,8 +50,10 @@  static const uint8_t mpc8_scfi0_bits[MPC8_SCFI0_SIZE] = {
 static const uint8_t mpc8_scfi1_syms[MPC8_SCFI1_SIZE] = {
      1,  4,  0,  2,  3,  8, 12,  5,  6,  7,  9, 13, 11, 14, 10, 15
 };
-static const uint8_t mpc8_scfi1_bits[MPC8_SCFI1_SIZE] = {
-     7,  7,  6,  6,  6,  6,  6,  5,  5,  5,  5,  5,  3,  3,  2,  2
+
+static const uint8_t mpc8_scfi_len_counts[2][16] = {
+    {  1,  1,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0  },
+    {  0,  2,  2,  0,  5,  5,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0  },
 };
 
 #define MPC8_DSCF0_SIZE 64
@@ -65,12 +65,6 @@  static const uint8_t mpc8_dscf0_syms[MPC8_DSCF0_SIZE] = {
     17, 18, 41, 42, 43, 19, 20, 21, 22, 40, 23, 24, 38, 39, 25, 28, 37, 26, 27,
     29, 30, 32, 36, 33, 34, 35,
 };
-static const uint8_t mpc8_dscf0_bits[MPC8_DSCF0_SIZE] = {
-    14, 14, 14, 14, 14, 14, 13, 13, 13, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11,
-    11, 10, 10, 10, 10, 10, 10, 10, 10, 10,  9,  9,  9,  9,  9,  9,  9,  8,  8,
-     8,  8,  8,  8,  8,  7,  7,  7,  7,  7,  6,  6,  6,  6,  5,  5,  5,  4,  4,
-     4,  4,  4,  4,  3,  3,  3
-};
 
 #define MPC8_DSCF1_SIZE 65
 #define MPC8_DSCF1_BITS 9
@@ -81,11 +75,10 @@  static const uint8_t mpc8_dscf1_syms[MPC8_DSCF1_SIZE] = {
     44, 45, 18, 19, 20, 41, 42, 21, 22, 39, 40, 23, 24, 38, 25, 37, 26, 35, 36,
     27, 28, 34, 29, 30, 31, 32, 33,
 };
-static const uint8_t mpc8_dscf1_bits[MPC8_DSCF1_SIZE] = {
-    15, 15, 15, 15, 15, 15, 14, 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, 12, 12,
-    12, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10,
-    10, 10,  9,  9,  9,  9,  9,  8,  8,  8,  8,  7,  7,  7,  6,  6,  5,  5,  5,
-     4,  4,  4,  3,  3,  3,  3,  3
+
+static const uint8_t mpc8_dscf_len_counts[2][16] = {
+    {  0,  0,  3,  6,  3,  4,  5,  7,  7,  9,  6,  5,  3,  6,  0,  0  },
+    {  0,  0,  5,  3,  3,  2,  3,  4,  5,  7,  7,  9,  6,  5,  6,  0  },
 };
 
 #define MPC8_RES_SIZE 17
@@ -99,13 +92,10 @@  static const uint8_t mpc8_res_syms[2][MPC8_RES_SIZE] = {
      8,  9, 10, 11,  7, 12,  6, 13,  5,  4, 14,  3, 15,  2,  0,  1, 16,
   }
 };
-static const uint8_t mpc8_res_bits[2][MPC8_RES_SIZE] = {
-  {
-    16, 16, 15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1
-  },
-  {
-    14, 14, 14, 14, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  2,  2
-  }
+
+static const uint8_t mpc8_res_len_counts[2][16] = {
+    {  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2  },
+    {  0,  3,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  0,  4,  0,  0  },
 };
 
 #define MPC8_Q1_SIZE 19
@@ -114,8 +104,9 @@  static const uint8_t mpc8_res_bits[2][MPC8_RES_SIZE] = {
 static const uint8_t mpc8_q1_syms[MPC8_Q1_SIZE] = {
      17, 18, 16, 15, 14, 13, 12,  0, 11,  1,  2,  8,  9, 10,  3,  4,  5,  6,  7,
 };
-static const uint8_t mpc8_q1_bits[MPC8_Q1_SIZE] = {
-    12, 12, 11, 10,  9,  8,  7,  6,  5,  4,  4,  4,  4,  4,  3,  3,  3,  3,  3
+
+static const uint8_t mpc8_q1_len_counts[16] = {
+    0,  0,  5,  5,  1,  1,  1,  1,  1,  1,  1,  2,  0,  0,  0,  0
 };
 
 #define MPC8_Q9UP_SIZE 256
@@ -141,21 +132,9 @@  static const uint8_t mpc8_q9up_syms[MPC8_Q9UP_SIZE] = {
     134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 148, 127,
     128,
 };
-static const uint8_t mpc8_q9up_bits[MPC8_Q9UP_SIZE] = {
-    11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10,  9,  9,  9,  9,  9,  9,  9,  9,
-     9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,
-     9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,
-     9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,
-     9,  9,  9,  9,  9,  9,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
-     7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
-     7,  7,  7,  7,  7,  7,  7,  6,  6
+
+static const uint8_t mpc8_q9up_len_counts[16] = {
+    0,  0,  0,  0,  0,  2, 38, 134, 71,  9,  2,  0,  0,  0,  0,  0
 };
 
 #define MPC8_Q2_SIZE 125
@@ -185,35 +164,18 @@  static const uint8_t mpc8_q2_syms[2][MPC8_Q2_SIZE] = {
      82,  86,  87,  92,  62,
 }
 };
-static const uint8_t mpc8_q2_bits[2][MPC8_Q2_SIZE] = {
-{
-    13, 13, 13, 13, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
-    11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10,
-    10, 10, 10, 10, 10, 10,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,
-     9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  7,  7,  7,
-     7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
-     6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  3
-},
-{
-    12, 12, 12, 12, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
-    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,  9,  9,  9,
-     9,  9,  9,  9,  9,  9,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  7,  7,  7,
-     7,  7,  7,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,  5,  5,  5,  5,
-     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  4
-}
+
+static const uint8_t mpc8_q2_len_counts[2][16] = {
+    {  0,  0,  1,  6,  0, 17,  9, 24, 24,  9, 27,  4,  4,  0,  0,  0  },
+    {  0,  0,  0,  1, 16, 10,  6, 48,  9, 27,  4,  4,  0,  0,  0,  0  },
 };
 
 #define MPC8_Q3_SIZE 49
 #define MPC8_Q3_BITS 9
 #define MPC8_Q3_OFFSET -48
 
-static const uint8_t mpc8_q3_bits[MPC8_Q3_SIZE] = {
-    9,  9,  9,  9,  8,  8,  8,  8,  8,  8,  8,  8,  7,  7,  7,  7,  7,  7,  7,
-    7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,
-    5,  5,  5,  5,  4,  4,  4,  4,  4,  4,  3,
+static const uint8_t mpc8_q3_len_counts[16] = {
+    0,  0,  1,  6,  6, 11, 13,  8,  4,  0,  0,  0,  0,  0,  0,  0
 };
 static const uint8_t mpc8_q3_syms[MPC8_Q3_SIZE] = {
      13,   3, 109,  99,  14,   2,  29,  19,  93,  83, 110,  98,  15,   0,   1,
@@ -226,12 +188,8 @@  static const uint8_t mpc8_q3_syms[MPC8_Q3_SIZE] = {
 #define MPC8_Q4_BITS 9
 #define MPC8_Q4_OFFSET -64
 
-static const uint8_t mpc8_q4_bits[MPC8_Q4_SIZE] = {
-    10, 10, 10, 10,  9,  9,  9,  9,  9,  9,  9,  9,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  7,  7,  7,  7,  7,  7,  7,
-     7,  7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
-     6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,  5,  5,  5,  5,  5,  5,
-     5,  5,  5,  5,  4,
+static const uint8_t mpc8_q4_len_counts[16] = {
+    0,  0,  0,  1, 12, 23, 14, 19,  8,  4,  0,  0,  0,  0,  0,  0
 };
 static const uint8_t mpc8_q4_syms[MPC8_Q4_SIZE] = {
      12,   4, 140, 132,  13,   3,  28,  20, 124, 116, 141, 131,  14,   1,   2,
@@ -254,13 +212,10 @@  static const uint8_t mpc8_q5_syms[2][MPC8_Q5_SIZE] = {
      0,  1, 13, 14,  2, 12,  3,  4, 10, 11,  5,  6,  7,  8,  9,
 }
 };
-static const uint8_t mpc8_q5_bits[2][MPC8_Q5_SIZE] = {
-{
-    7,  7,  7,  7,  6,  6,  5,  5,  4,  4,  3,  3,  3,  3,  2
-},
-{
-    6,  6,  6,  6,  5,  5,  4,  4,  4,  4,  3,  3,  3,  3,  3
-}
+
+static const uint8_t mpc8_q5_len_counts[2][16] = {
+    {  0,  1,  4,  2,  2,  2,  4,  0,  0,  0,  0,  0,  0,  0,  0,  0  },
+    {  0,  0,  5,  4,  2,  4,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0  },
 };
 
 #define MPC8_Q6_SIZE 31
@@ -277,15 +232,10 @@  static const uint8_t mpc8_q6_syms[2][MPC8_Q6_SIZE] = {
      23, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
 }
 };
-static const uint8_t mpc8_q6_bits[2][MPC8_Q6_SIZE] = {
-{
-    9,  9,  9,  9,  9,  9,  9,  9,  8,  8,  8,  8,  7,  7,  6,  6,  6,  6,  6,
-    6,  6,  5,  5,  5,  4,  4,  4,  4,  3,  3,  2
-},
-{
-    8,  8,  8,  8,  7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  5,  5,  5,  5,  5,
-    5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4
-}
+
+static const uint8_t mpc8_q6_len_counts[2][16] = {
+    {  0,  1,  2,  4,  3,  7,  2,  4,  8,  0,  0,  0,  0,  0,  0,  0  },
+    {  0,  0,  0, 11,  6,  4,  6,  4,  0,  0,  0,  0,  0,  0,  0,  0  },
 };
 
 #define MPC8_Q7_SIZE 63
@@ -306,19 +256,10 @@  static const uint8_t mpc8_q7_syms[2][MPC8_Q7_SIZE] = {
      36, 37, 38, 39, 40, 41,
 }
 };
-static const uint8_t mpc8_q7_bits[2][MPC8_Q7_SIZE] = {
-{
-    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,  9,  9,  9,
-     9,  9,  9,  9,  9,  9,  9,  9,  9,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  5,  5,  5,
-     5,  4,  4,  3,  3,  2
-},
-{
-     9,  9,  9,  9,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  7,
-     7,  7,  7,  7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
-     6,  6,  6,  6,  6,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
-     5,  5,  5,  5,  5,  5
-}
+
+static const uint8_t mpc8_q7_len_counts[2][MPC8_Q7_SIZE] = {
+    {  0,  1,  2,  2,  4,  6, 10, 10, 12, 16,  0,  0,  0,  0,  0,  0  },
+    {  0,  0,  0,  0, 20, 15, 10, 14,  4,  0,  0,  0,  0,  0,  0,  0  },
 };
 
 #define MPC8_Q8_SIZE 127
@@ -349,25 +290,10 @@  static const uint8_t mpc8_q8_syms[2][MPC8_Q8_SIZE] = {
      69,  70,  71,  72,  73,  74,  76,
 }
 };
-static const uint8_t mpc8_q8_bits[2][MPC8_Q8_SIZE] = {
-{
-    12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
-    11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
-    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
-    10, 10, 10, 10, 10, 10, 10, 10,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,
-     9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  7,  7,  7,  7,  7,  7,  7,  7,  6,  6,
-     6,  6,  6,  6,  6,  6,  5,  5,  5,  4,  3,  3,  2
-},
-{
-     9,  9,  9,  9,  9,  9,  9,  9,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
-     8,  8,  8,  8,  8,  8,  8,  8,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
-     7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
-     7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
-     7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
-     6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6
-}
+
+static const uint8_t mpc8_q8_len_counts[2][16] = {
+    {  0,  1,  2,  1,  3,  8,  8, 15, 24, 42, 17,  6,  0,  0,  0,  0  },
+    {  0,  0,  0,  0,  0, 26, 55, 38,  8,  0,  0,  0,  0,  0,  0,  0  },
 };
 
 #endif /* AVCODEC_MPC8HUFF_H */