[FFmpeg-cvslog] avcodec/magicyuv: Avoid copying values around pointlessly

Andreas Rheinhardt git at videolan.org
Tue Sep 1 12:41:09 EEST 2020


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Mon Aug 31 18:55:19 2020 +0200| [d0b0fc7cb94f02a7de0d67cb3ba06a243b323a1d] | committer: Andreas Rheinhardt

avcodec/magicyuv: Avoid copying values around pointlessly

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 as on their symbols). After sorting
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.

Reviewed-by: Paul B Mahol <onemda at gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=d0b0fc7cb94f02a7de0d67cb3ba06a243b323a1d
---

 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,



More information about the ffmpeg-cvslog mailing list