diff mbox series

[FFmpeg-devel,1/3] avcodec/webp: Use uint8_t for code lengths

Message ID 20201022145624.657457-1-andreas.rheinhardt@gmail.com
State Accepted
Commit d4cce1514edf023145fb5a8af9c52bb8e95bede1
Headers show
Series [FFmpeg-devel,1/3] avcodec/webp: Use uint8_t for code lengths
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Oct. 22, 2020, 2:56 p.m. UTC
They are always in the range 0..15, so using an int is not necessary.
Furthermore, using an int would not work if sizeof(int) == 4 as
ff_init_vlc_sparse() can only handle uint8_t, uint16_t and uint32_t
lengths.

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

Comments

Andreas Rheinhardt Oct. 26, 2020, 7:24 a.m. UTC | #1
Andreas Rheinhardt:
> They are always in the range 0..15, so using an int is not necessary.
> Furthermore, using an int would not work if sizeof(int) == 4 as
> ff_init_vlc_sparse() can only handle uint8_t, uint16_t and uint32_t
> lengths.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/webp.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/libavcodec/webp.c b/libavcodec/webp.c
> index 6140c7ea4a..6b53a7f17c 100644
> --- a/libavcodec/webp.c
> +++ b/libavcodec/webp.c
> @@ -243,7 +243,7 @@ static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb)
>          return get_vlc2(gb, r->vlc.table, 8, 2);
>  }
>  
> -static int huff_reader_build_canonical(HuffReader *r, int *code_lengths,
> +static int huff_reader_build_canonical(HuffReader *r, const uint8_t *code_lengths,
>                                         int alphabet_size)
>  {
>      int len = 0, sym, code = 0, ret;
> @@ -324,8 +324,8 @@ static int read_huffman_code_normal(WebPContext *s, HuffReader *hc,
>                                      int alphabet_size)
>  {
>      HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
> -    int *code_lengths = NULL;
> -    int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
> +    uint8_t *code_lengths = NULL;
> +    uint8_t code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
>      int i, symbol, max_symbol, prev_code_len, ret;
>      int num_codes = 4 + get_bits(&s->gb, 4);
>  
> @@ -340,7 +340,7 @@ static int read_huffman_code_normal(WebPContext *s, HuffReader *hc,
>      if (ret < 0)
>          goto finish;
>  
> -    code_lengths = av_mallocz_array(alphabet_size, sizeof(*code_lengths));
> +    code_lengths = av_mallocz(alphabet_size);
>      if (!code_lengths) {
>          ret = AVERROR(ENOMEM);
>          goto finish;
> 
Will apply this patchset later today unless there are objections.

- Andreas
Zhao Zhili Oct. 26, 2020, 8:17 a.m. UTC | #2
> On Oct 26, 2020, at 3:24 PM, Andreas Rheinhardt <andreas.rheinhardt@gmail.com> wrote:
> 
> Andreas Rheinhardt:
>> They are always in the range 0..15, so using an int is not necessary.
>> Furthermore, using an int would not work if sizeof(int) == 4 as

You mean if sizeof(int) != 4 ?

>> ff_init_vlc_sparse() can only handle uint8_t, uint16_t and uint32_t
>> lengths.
>> 
>> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
>> ---
>> libavcodec/webp.c | 8 ++++----
>> 1 file changed, 4 insertions(+), 4 deletions(-)
>> 
>> diff --git a/libavcodec/webp.c b/libavcodec/webp.c
>> index 6140c7ea4a..6b53a7f17c 100644
>> --- a/libavcodec/webp.c
>> +++ b/libavcodec/webp.c
>> @@ -243,7 +243,7 @@ static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb)
>>         return get_vlc2(gb, r->vlc.table, 8, 2);
>> }
>> 
>> -static int huff_reader_build_canonical(HuffReader *r, int *code_lengths,
>> +static int huff_reader_build_canonical(HuffReader *r, const uint8_t *code_lengths,
>>                                        int alphabet_size)
>> {
>>     int len = 0, sym, code = 0, ret;
>> @@ -324,8 +324,8 @@ static int read_huffman_code_normal(WebPContext *s, HuffReader *hc,
>>                                     int alphabet_size)
>> {
>>     HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
>> -    int *code_lengths = NULL;
>> -    int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
>> +    uint8_t *code_lengths = NULL;
>> +    uint8_t code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
>>     int i, symbol, max_symbol, prev_code_len, ret;
>>     int num_codes = 4 + get_bits(&s->gb, 4);
>> 
>> @@ -340,7 +340,7 @@ static int read_huffman_code_normal(WebPContext *s, HuffReader *hc,
>>     if (ret < 0)
>>         goto finish;
>> 
>> -    code_lengths = av_mallocz_array(alphabet_size, sizeof(*code_lengths));
>> +    code_lengths = av_mallocz(alphabet_size);
>>     if (!code_lengths) {
>>         ret = AVERROR(ENOMEM);
>>         goto finish;
>> 
> Will apply this patchset later today unless there are objections.
> 
> - Andreas
> _______________________________________________
> 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".
Andreas Rheinhardt Oct. 26, 2020, 8:31 a.m. UTC | #3
"zhilizhao(赵志立)":
> 
> 
>> On Oct 26, 2020, at 3:24 PM, Andreas Rheinhardt <andreas.rheinhardt@gmail.com> wrote:
>>
>> Andreas Rheinhardt:
>>> They are always in the range 0..15, so using an int is not necessary.
>>> Furthermore, using an int would not work if sizeof(int) == 4 as
> 
> You mean if sizeof(int) != 4 ?
> 

Good catch. Fixed locally. Thanks.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/webp.c b/libavcodec/webp.c
index 6140c7ea4a..6b53a7f17c 100644
--- a/libavcodec/webp.c
+++ b/libavcodec/webp.c
@@ -243,7 +243,7 @@  static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb)
         return get_vlc2(gb, r->vlc.table, 8, 2);
 }
 
-static int huff_reader_build_canonical(HuffReader *r, int *code_lengths,
+static int huff_reader_build_canonical(HuffReader *r, const uint8_t *code_lengths,
                                        int alphabet_size)
 {
     int len = 0, sym, code = 0, ret;
@@ -324,8 +324,8 @@  static int read_huffman_code_normal(WebPContext *s, HuffReader *hc,
                                     int alphabet_size)
 {
     HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
-    int *code_lengths = NULL;
-    int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
+    uint8_t *code_lengths = NULL;
+    uint8_t code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
     int i, symbol, max_symbol, prev_code_len, ret;
     int num_codes = 4 + get_bits(&s->gb, 4);
 
@@ -340,7 +340,7 @@  static int read_huffman_code_normal(WebPContext *s, HuffReader *hc,
     if (ret < 0)
         goto finish;
 
-    code_lengths = av_mallocz_array(alphabet_size, sizeof(*code_lengths));
+    code_lengths = av_mallocz(alphabet_size);
     if (!code_lengths) {
         ret = AVERROR(ENOMEM);
         goto finish;