[FFmpeg-devel] [PATCH 065/114] avcodec/mjpegdec: Simplify creating VLC table

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Tue Nov 10 12:48:02 EET 2020


ff_init_vlc_from_lengths() can be used to offload the computation
of the codes; it also allows to omit the check whether the codes
are already properly ordered (they are).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/mjpegdec.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 147dd819e5..2a226f63d4 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -50,18 +50,14 @@
 #include "bytestream.h"
 
 
-static void build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
-                                const uint8_t *bits_table)
+static void build_huffman_codes(uint8_t *huff_size, const uint8_t *bits_table)
 {
-    for (int i = 1, code = 0, k = 0; i <= 16; i++) {
+    for (int i = 1, k = 0; i <= 16; i++) {
         int nb = bits_table[i];
         for (int j = 0; j < nb;j++) {
             huff_size[k] = i;
-            huff_code[k] = code;
-            code++;
             k++;
         }
-        code <<= 1;
     }
 }
 
@@ -70,13 +66,12 @@ static int build_vlc(VLC *vlc, const uint8_t *bits_table,
                      int is_ac)
 {
     uint8_t huff_size[256];
-    uint16_t huff_code[256];
     uint16_t huff_sym[256];
     int i;
 
     av_assert0(nb_codes <= 256);
 
-    build_huffman_codes(huff_size, huff_code, bits_table);
+    build_huffman_codes(huff_size, bits_table);
 
     for (i = 0; i < nb_codes; i++) {
         huff_sym[i] = val_table[i] + 16 * is_ac;
@@ -85,8 +80,8 @@ static int build_vlc(VLC *vlc, const uint8_t *bits_table,
             huff_sym[i] = 16 * 256;
     }
 
-    return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
-                              huff_code, 2, 2, huff_sym, 2, 2, 0);
+    return ff_init_vlc_from_lengths(vlc, 9, nb_codes, huff_size, 1,
+                                    huff_sym, 2, 2, 0, 0);
 }
 
 static int init_default_huffman_tables(MJpegDecodeContext *s)
-- 
2.25.1



More information about the ffmpeg-devel mailing list