diff mbox series

[FFmpeg-devel,v2,076/162] avcodec/mss4: Simplify creating VLC tables

Message ID 20201120072116.818090-77-andreas.rheinhardt@gmail.com
State Accepted
Commit 70bbb4cce80ec2255b023b8f9e964dcf5d2ea9f6
Headers show
Series VLC, esp. init_vlc patches | expand

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
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@gmail.com>
---
 libavcodec/mss4.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/mss4.c b/libavcodec/mss4.c
index c421a07c5d..baf3c14d02 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, NULL);
 }
 
 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;
     }