]> git.sesse.net Git - ffmpeg/commit
avcodec/utvideodec: Avoid qsort when creating Huffman tables
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Thu, 24 Sep 2020 16:29:39 +0000 (18:29 +0200)
committerAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Sat, 26 Sep 2020 19:10:45 +0000 (21:10 +0200)
commit341914495e5c2f60bc920b0c6f660e5948a47f5a
treed616d06603a5361ab270537b85f8b9bd59c7f76a
parent9c8b85f5fa5f465cfc0a88fbcbea0f4a436ece38
avcodec/utvideodec: Avoid qsort when creating Huffman tables

The Ut video format uses Huffman trees which are only implicitly coded
in the bitstream: Only the lengths of the codes are coded, the rest has
to be inferred by the decoder according to the rule that the longer
codes are to the left of shorter codes in the tree and on each level the
symbols are descending from left to right.

Because longer codes are to the left of shorter codes, one needs to know
how many non-leaf nodes there are on each level in order to know the
code of the next left-most leaf (which belongs to the highest symbol on
that level). The current code does this by sorting the entries to be
ascending according to length and (for entries with the same length)
ascending according to their symbols. This array is then traversed in
reverse order, so that the lowest level is dealt with first, so that the
number of non-leaf nodes of the next higher level is known when
processing said level.

But this can also be calculated without sorting: Simply count how many
leaf nodes there are on each level. Then one can calculate the number of
non-leaf nodes on each level iteratively from the lowest level upwards:
It is just half the number of nodes of the level below.

This improves performance: For the sample from ticket #4044 the amount
of decicycles for one call to build_huff() decreased from 1055489 to
446310 for Clang 10 and from 1080306 to 535155 for GCC 9.

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
libavcodec/utvideo.c
libavcodec/utvideo.h
libavcodec/utvideodec.c