diff mbox series

[FFmpeg-devel,12/25] avcodec/magicyuv: Don't waste stack space

Message ID 20200926102804.228089-12-andreas.rheinhardt@gmail.com
State Accepted
Commit 116b235a0b86276aea2fc9021c056744977a1527
Headers show
Series [FFmpeg-devel,01/25] avcodec/photocd: Simplify parsing Huffman tables a bit | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Sept. 26, 2020, 10:27 a.m. UTC
Now that the HuffEntries are no longer sorted by the MagicYUV decoder,
their symbols are trivial: The symbol of the element with index i is i.
They can therefore be removed. Furthermore, despite the length of the
codes being in the range 1..32 bits, the actual value of the codes is
<= 4096 (for 12 bit content). The reason for this is that the longer
codes are on the left side of the tree, so that the higher bits of
these codes are simply zero. By using an uint16_t for the codes and
removing the symbols entry, the size of each HuffEntry is decreased from
eight to four, saving 16KB of stack space.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/magicyuv.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

Comments

Paul B Mahol Sept. 26, 2020, 10:58 a.m. UTC | #1
On Sat, Sep 26, 2020 at 12:27:51PM +0200, Andreas Rheinhardt wrote:
> Now that the HuffEntries are no longer sorted by the MagicYUV decoder,
> their symbols are trivial: The symbol of the element with index i is i.
> They can therefore be removed. Furthermore, despite the length of the
> codes being in the range 1..32 bits, the actual value of the codes is
> <= 4096 (for 12 bit content). The reason for this is that the longer
> codes are on the left side of the tree, so that the higher bits of
> these codes are simply zero. By using an uint16_t for the codes and
> removing the symbols entry, the size of each HuffEntry is decreased from
> eight to four, saving 16KB of stack space.

lgtm

> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/magicyuv.c | 14 +++++---------
>  1 file changed, 5 insertions(+), 9 deletions(-)
> 
> diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c
> index 7dded9b457..ea1f727e5c 100644
> --- a/libavcodec/magicyuv.c
> +++ b/libavcodec/magicyuv.c
> @@ -46,9 +46,8 @@ typedef enum Prediction {
>  } Prediction;
>  
>  typedef struct HuffEntry {
> -    uint16_t sym;
>      uint8_t  len;
> -    uint32_t code;
> +    uint16_t code;
>  } HuffEntry;
>  
>  typedef struct MagicYUVContext {
> @@ -90,10 +89,9 @@ static int huff_build(HuffEntry he[], uint16_t codes_count[33],
>          he[i].code = codes_count[he[i].len];
>          codes_count[he[i].len]++;
>      }
> -    return ff_init_vlc_sparse(vlc, FFMIN(max, 12), nb_elems,
> -                              &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);
> +    return init_vlc(vlc, FFMIN(max, 12), nb_elems,
> +                    &he[0].len,  sizeof(he[0]), sizeof(he[0].len),
> +                    &he[0].code, sizeof(he[0]), sizeof(he[0].code), 0);
>  }
>  
>  static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
> @@ -408,10 +406,8 @@ static int build_huffman(AVCodecContext *avctx, const uint8_t *table,
>          }
>  
>          length_count[x] += l;
> -        for (; j < k; j++) {
> -            he[j].sym = j;
> +        for (; j < k; j++)
>              he[j].len = x;
> -        }
>  
>          if (j == max) {
>              j = 0;
> -- 
> 2.25.1
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff mbox series

Patch

diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c
index 7dded9b457..ea1f727e5c 100644
--- a/libavcodec/magicyuv.c
+++ b/libavcodec/magicyuv.c
@@ -46,9 +46,8 @@  typedef enum Prediction {
 } Prediction;
 
 typedef struct HuffEntry {
-    uint16_t sym;
     uint8_t  len;
-    uint32_t code;
+    uint16_t code;
 } HuffEntry;
 
 typedef struct MagicYUVContext {
@@ -90,10 +89,9 @@  static int huff_build(HuffEntry he[], uint16_t codes_count[33],
         he[i].code = codes_count[he[i].len];
         codes_count[he[i].len]++;
     }
-    return ff_init_vlc_sparse(vlc, FFMIN(max, 12), nb_elems,
-                              &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);
+    return init_vlc(vlc, FFMIN(max, 12), nb_elems,
+                    &he[0].len,  sizeof(he[0]), sizeof(he[0].len),
+                    &he[0].code, sizeof(he[0]), sizeof(he[0].code), 0);
 }
 
 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
@@ -408,10 +406,8 @@  static int build_huffman(AVCodecContext *avctx, const uint8_t *table,
         }
 
         length_count[x] += l;
-        for (; j < k; j++) {
-            he[j].sym = j;
+        for (; j < k; j++)
             he[j].len = x;
-        }
 
         if (j == max) {
             j = 0;