Browse Source

avcodec/utvideodec/enc: Fix edge case of creating Huffman table

The Ut Video format stores Huffman tables in its bitstream by coding
the length of a given symbol; it does not code the actual code directly,
instead this is to be inferred by the rule that a symbol is to the left
of every shorter symbol in the Huffman tree and that for symbols of the
same length the symbol is descending from left to right. With one
exception, this is also what our de- and encoder did.

The exception only matters when there are codes of length 32, because
in this case the first symbol of this length did not get the code 0,
but 1; this is tantamount to pretending that there is a (nonexistent)
leaf of length 32. This is simply false. The reference software agrees
with this [1].

[1]: 2700a471a7/utv_core/HuffmanCode.cpp (L280)

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
tags/n4.4
Andreas Rheinhardt 5 years ago
parent
commit
099feb9411
2 changed files with 3 additions and 3 deletions
  1. +2
    -2
      libavcodec/utvideodec.c
  2. +1
    -1
      libavcodec/utvideoenc.c

+ 2
- 2
libavcodec/utvideodec.c View File

@@ -70,7 +70,7 @@ static int build_huff10(const uint8_t *src, VLC *vlc, int *fsym)
return -1;
}

code = 1;
code = 0;
for (i = last; i >= 0; i--) {
codes[i] = code >> (32 - he[i].len);
bits[i] = he[i].len;
@@ -113,7 +113,7 @@ static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
if (he[last].len > 32)
return -1;

code = 1;
code = 0;
for (i = last; i >= 0; i--) {
codes[i] = code >> (32 - he[i].len);
bits[i] = he[i].len;


+ 1
- 1
libavcodec/utvideoenc.c View File

@@ -346,7 +346,7 @@ static void calculate_codes(HuffEntry *he)
while (he[last].len == 255 && last)
last--;

code = 1;
code = 0;
for (i = last; i >= 0; i--) {
he[i].code = code >> (32 - he[i].len);
code += 0x80000000u >> (he[i].len - 1);


Loading…
Cancel
Save