[FFmpeg-devel] [PATCH 059/114] avcodec/mss4: Simplify creating VLC tables

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Tue Nov 10 12:47:56 EET 2020


The lengths of the codes used by the mss4 decoder are ascending from
left to right and therefore the lengths can be run-length encoded and
the codes can be easily derived from them. And this is how it is indeed
done. Yet some things can nevertheless be improved:

a) The number of entries of the current VLC is implicitly contained in
the run-length table and needn't be externally prescribed.
b) The maximum length of a code is just the length of the last code
(given that the lengths are ascending), so there is no point in setting
max_bits in the loop itself.
c) One can offload the actual calculation of the codes to
ff_init_vlc_from_lengths().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/mss4.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/libavcodec/mss4.c b/libavcodec/mss4.c
index c421a07c5d..a459503417 100644
--- a/libavcodec/mss4.c
+++ b/libavcodec/mss4.c
@@ -143,25 +143,21 @@ typedef struct MSS4Context {
 } MSS4Context;
 
 static av_cold int mss4_init_vlc(VLC *vlc, const uint8_t *lens,
-                                 const uint8_t *syms, int num_syms)
+                                 const uint8_t *syms)
 {
     uint8_t  bits[MAX_ENTRIES];
-    uint16_t codes[MAX_ENTRIES];
     int i, j;
-    int prefix = 0, max_bits = 0, idx = 0;
+    int idx = 0;
 
     for (i = 0; i < 16; i++) {
         for (j = 0; j < lens[i]; j++) {
             bits[idx]  = i + 1;
-            codes[idx] = prefix++;
-            max_bits   = i + 1;
             idx++;
         }
-        prefix <<= 1;
     }
 
-    return ff_init_vlc_sparse(vlc, FFMIN(max_bits, 9), num_syms, bits, 1, 1,
-                              codes, 2, 2, syms, 1, 1, 0);
+    return ff_init_vlc_from_lengths(vlc, FFMIN(bits[idx - 1], 9), idx,
+                                    bits, 1, syms, 1, 1, 0, 0);
 }
 
 static av_cold int mss4_init_vlcs(MSS4Context *ctx)
@@ -169,15 +165,15 @@ static av_cold int mss4_init_vlcs(MSS4Context *ctx)
     int ret, i;
 
     for (i = 0; i < 2; i++) {
-        ret = mss4_init_vlc(&ctx->dc_vlc[i], mss4_dc_vlc_lens[i], NULL, 12);
+        ret = mss4_init_vlc(&ctx->dc_vlc[i], mss4_dc_vlc_lens[i], NULL);
         if (ret)
             return ret;
         ret = mss4_init_vlc(&ctx->ac_vlc[i], mss4_ac_vlc_lens[i],
-                            mss4_ac_vlc_syms[i], 162);
+                            mss4_ac_vlc_syms[i]);
         if (ret)
             return ret;
         ret = mss4_init_vlc(&ctx->vec_entry_vlc[i], mss4_vec_entry_vlc_lens[i],
-                            mss4_vec_entry_vlc_syms[i], 9);
+                            mss4_vec_entry_vlc_syms[i]);
         if (ret)
             return ret;
     }
-- 
2.25.1



More information about the ffmpeg-devel mailing list