[FFmpeg-devel] [PATCH 2/8] avcodec/magicyuv: Avoid copying values around pointlessly

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Tue Sep 1 00:09:24 EEST 2020


When parsing Huffman tables, an array of HuffEntries (a struct
containing a code's bitlength, its bits and its symbol) is used as
intermediate tables in order to sort the entries (the order depends on
both the length of the entries as well on their symbols). After ordering
them, the symbol and len components are copied into other arrays (the
HuffEntries' code has never been set or used, despite using quite a lot
of stack space) and the codes are generated. Afterwards, the VLC is
created.

Yet ff_init_vlc_sparse() can handle non-continuous arrays as input;
there is no need to copy the entries at all. This commit implements
this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/magicyuv.c | 39 ++++++++++++---------------------------
 1 file changed, 12 insertions(+), 27 deletions(-)

diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c
index 32d744add2..f630464162 100644
--- a/libavcodec/magicyuv.c
+++ b/libavcodec/magicyuv.c
@@ -97,9 +97,6 @@ static int huff_cmp_len12(const void *a, const void *b)
 static int huff_build10(VLC *vlc, uint8_t *len)
 {
     HuffEntry he[1024];
-    uint32_t codes[1024];
-    uint8_t bits[1024];
-    uint16_t syms[1024];
     uint32_t code;
     int i;
 
@@ -113,25 +110,20 @@ static int huff_build10(VLC *vlc, uint8_t *len)
 
     code = 1;
     for (i = 1023; i >= 0; i--) {
-        codes[i] = code >> (32 - he[i].len);
-        bits[i]  = he[i].len;
-        syms[i]  = he[i].sym;
+        he[i].code = code >> (32 - he[i].len);
         code += 0x80000000u >> (he[i].len - 1);
     }
 
     ff_free_vlc(vlc);
     return ff_init_vlc_sparse(vlc, FFMIN(he[1023].len, 12), 1024,
-                              bits,  sizeof(*bits),  sizeof(*bits),
-                              codes, sizeof(*codes), sizeof(*codes),
-                              syms,  sizeof(*syms),  sizeof(*syms), 0);
+                              &he[0].len,  sizeof(he[0]), sizeof(he[0].len),
+                              &he[0].code, sizeof(he[0]), sizeof(he[0].code),
+                              &he[0].sym,  sizeof(he[0]), sizeof(he[0].sym),  0);
 }
 
 static int huff_build12(VLC *vlc, uint8_t *len)
 {
     HuffEntry he[4096];
-    uint32_t codes[4096];
-    uint8_t bits[4096];
-    uint16_t syms[4096];
     uint32_t code;
     int i;
 
@@ -145,25 +137,20 @@ static int huff_build12(VLC *vlc, uint8_t *len)
 
     code = 1;
     for (i = 4095; i >= 0; i--) {
-        codes[i] = code >> (32 - he[i].len);
-        bits[i]  = he[i].len;
-        syms[i]  = he[i].sym;
+        he[i].code = code >> (32 - he[i].len);
         code += 0x80000000u >> (he[i].len - 1);
     }
 
     ff_free_vlc(vlc);
     return ff_init_vlc_sparse(vlc, FFMIN(he[4095].len, 12), 4096,
-                              bits,  sizeof(*bits),  sizeof(*bits),
-                              codes, sizeof(*codes), sizeof(*codes),
-                              syms,  sizeof(*syms),  sizeof(*syms), 0);
+                              &he[0].len,  sizeof(he[0]), sizeof(he[0].len),
+                              &he[0].code, sizeof(he[0]), sizeof(he[0].code),
+                              &he[0].sym,  sizeof(he[0]), sizeof(he[0].sym),  0);
 }
 
 static int huff_build(VLC *vlc, uint8_t *len)
 {
     HuffEntry he[256];
-    uint32_t codes[256];
-    uint8_t bits[256];
-    uint8_t syms[256];
     uint32_t code;
     int i;
 
@@ -177,17 +164,15 @@ static int huff_build(VLC *vlc, uint8_t *len)
 
     code = 1;
     for (i = 255; i >= 0; i--) {
-        codes[i] = code >> (32 - he[i].len);
-        bits[i]  = he[i].len;
-        syms[i]  = he[i].sym;
+        he[i].code = code >> (32 - he[i].len);
         code += 0x80000000u >> (he[i].len - 1);
     }
 
     ff_free_vlc(vlc);
     return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256,
-                              bits,  sizeof(*bits),  sizeof(*bits),
-                              codes, sizeof(*codes), sizeof(*codes),
-                              syms,  sizeof(*syms),  sizeof(*syms), 0);
+                              &he[0].len,  sizeof(he[0]), sizeof(he[0].len),
+                              &he[0].code, sizeof(he[0]), sizeof(he[0].code),
+                              &he[0].sym,  sizeof(he[0]), sizeof(he[0].sym),  0);
 }
 
 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
-- 
2.20.1



More information about the ffmpeg-devel mailing list