1
mirror of https://git.videolan.org/git/ffmpeg.git synced 2024-07-25 21:51:29 +02:00
Commit Graph

91 Commits

Author SHA1 Message Date
Andreas Rheinhardt
a247ac640d avcodec: Constify AVCodecs
Given that the AVCodec.next pointer has now been removed, most of the
AVCodecs are not modified at all any more and can therefore be made
const (as this patch does); the only exceptions are the very few codecs
for external libraries that have a init_static_data callback.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2021-04-27 10:43:15 -03:00
Andreas Rheinhardt
c638d1d126 avcodec/utvideodec: Avoid implicit qsort when creating Huffman tables
The Huffman trees used by Ut Video have two important characteristics:
(i) Longer codes are on the left of the tree and (ii) for codes of the
same length, the symbol is descending from left to right in the tree.
Therefore all the information that needs to be transmitted is how long
the code corresponding to a given symbol is; and this is also all that
is transmitted.

Before 341914495e, the decoder used qsort
to sort the (length, symbol) pairs by ascending length and for equal
lengths by ascending symbol. Since said commit, the decoder uses
a first pass over the lengths table to count how many symbols of each
length there are; with (i) one can then easily calculate the code of
the left-most code with a given length in the tree and from there one
can calculate the codes for all entries, using one running counter for
each possible length. This eliminated the explicit qsort in
build_huff().

Yet ff_init_vlc_sparse() sorts the table itself as it has to ensure that
all the entries that will be placed in the same subtable are contiguous.
The tables created now are non-contiguous (they are ordered by symbol
and codes of different length aren't ordered at all; only codes of the
same length are ordered according to (ii)).

This commit therefore modifies the algorithm used to automatically create
tables whose codes are sorted from left to right in the tree. The key to
do so is the observation that the counts obtained in the first pass can
be used to contain the range of the codes of each length in the second
pass: If counts[i] is the count of codes with length i, then the first
counts[32] codes are of length 32, the next counts[31] codes are of
length 31 etc. So one knows the index of the lowest symbol whose code
has length 32 (if any): It is counts[32] - 1 due to (ii), whereas the
index of the lowest symbol whose code has length 31 (if any) is
counts[32] + counts[31] - 1; the index of the second-to-lowest symbol of
length 32 (if existing) is counts[32] - 2 etc.

If one follows the algorithm outlined above, one can switch to
ff_init_vlc_from_lengths() which has no implicit qsort; it also means
that one can offload the computation of the codes.

This turned out to be beneficial for performance: For the sample from
ticket #4044 it decreased the decicycles spent on one call to
build_huff() from 508480 to 340688 (GCC 9.3, looping 10 times over the
file to get enough runs and then repeating this ten times); for another
sample (YUV420p, natural content, 5500 frames, also ten iterations)
the time went down from 382346 to 275533 decicycles.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-12-08 17:51:47 +01:00
Andreas Rheinhardt
341914495e 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>
2020-09-26 21:10:45 +02:00
Andreas Rheinhardt
9c8b85f5fa avcodec/utvideodec: Remove code duplication when creating Huffman tables
Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-09-26 21:10:45 +02:00
Andreas Rheinhardt
099feb9411 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>
2020-09-26 21:10:29 +02:00
Michael Niedermayer
876cfa67f3 avcodec/utvideodec: Fix integer overflow in decode_plane()
Fixes: signed integer overflow: 2147483594 + 142 cannot be represented in type 'int'
Fixes: 20492/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_UTVIDEO_fuzzer-5658568101724160

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2020-05-12 01:00:28 +02:00
Paul B Mahol
022796c82f avcodec/utvideodec: add support for UQY0 2020-02-25 13:09:20 +01:00
Paul B Mahol
78c8a76536 avcodec/get_bits: unbreak get_bits_le() with cached reader 2019-04-19 13:58:54 +02:00
Paul B Mahol
562f00ed07 avcodec/utvideodec: use cached bitstream reader everywhere except on x86_32
From 100x real-time decoding to 138x real-time decoding for 320x240 video.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
2018-08-30 12:00:27 +02:00
Michael Niedermayer
47b7c68ae5 avcodec/utvideodec: Set pro flag based on fourcc
This avoids mixing 8bit variants with pro and 10bit with non pro mode.
Fixes: out of array read
Fixes: poc_03_30.avi

Found-by: GwanYeong Kim <gy741.kim@gmail.com>
Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2018-04-01 02:58:51 +02:00
Michael Niedermayer
7414d0bda7 avcodec/utvideodec: Check subsample factors
Fixes: Out of array read
Fixes: heap_poc

Found-by: GwanYeong Kim <gy741.kim@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2018-02-27 12:23:23 +01:00
Michael Niedermayer
76cc0f0f67 avcodec/utvideodec: Add several out of array read related checks
Fixes: OV_decode_plane.avi

Found-by: GwanYeong Kim <gy741.kim@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2018-02-11 03:04:48 +01:00
Michael Niedermayer
118e1b0b33 avcodec/utvideodec: Fix bytes left check in decode_frame()
Fixes: out of array read
Fixes: poc-2017.avi

Found-by: GwanYeong Kim <gy741.kim@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2018-02-05 02:54:02 +01:00
Paul B Mahol
92b32664cd avcodec/utvideodec: add support for UMH2, UMY2, UMH4, UMY4, UMRA, UMRG
These are new modes which are supposed to be more SIMD friendly.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
2018-01-02 13:41:49 +01:00
Martin Vignali
f2e9156eb6 avcodec/utvideodec : use gradient_pred dsp in interlace decoding 2017-12-19 20:55:08 +01:00
Martin Vignali
630967ef63 avcodec/utvideodec : add SIMD (SSSE3 and AVX2) for gradient_pred 2017-12-09 15:19:03 +01:00
Martin Vignali
9e1c9633cc avcodec/utvideodec : use dsp add_median_pred for second line
process start of the line in scalar, before call dsp
(dsp need align 16)
2017-12-09 15:11:12 +01:00
Martin Vignali
9c71473189 libavcodec/utvideo : simplify decode_plane
the func is only call with step = 1
no need to pass it in the func
2017-11-07 00:51:17 +01:00
James Almer
b428d445a8 Merge commit '7c25523cc8e618e77dc84d960e41e9644eaf8c33'
* commit '7c25523cc8e618e77dc84d960e41e9644eaf8c33':
  utvideodec: Fix decoding odd sizes with interlaced video with some formats

See 9ef21a897c

Merged-by: James Almer <jamrial@gmail.com>
2017-10-30 14:25:15 -03:00
Michael Niedermayer
850c6db97d avcodec/utvideodec: Factor multiply out of inner loop
0.5% faster loop

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Reviewed-by: Steven Liu <lingjiujianke@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-06-28 14:08:21 +02:00
Michael Niedermayer
5eb4701b7d avcodec/utvideodec: bswap directly without memcpy
Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-06-28 14:08:21 +02:00
Michael Niedermayer
676a589c93 avcodec/utvideodec: enable unchecked bitreader
inner reader loop becomes 16% faster

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-06-28 14:08:21 +02:00
Michael Niedermayer
9c604b34d4 avcodec/utvideodec: hardcode vlc bits
2.5% faster vlc decoding

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-06-28 14:08:21 +02:00
Michael Niedermayer
1835c5e7a4 avcodec/utvideodec: Move bitstream end check out of inner loop
This is not needed when the buffer is large enough for the worst case of a line

2% faster vlc reading

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-06-28 14:08:21 +02:00
Paul B Mahol
4ed7c2bbc3 avcodec/utvideodec: add SIMD for restore_rgb_planes
Signed-off-by: Paul B Mahol <onemda@gmail.com>
2017-06-27 09:54:10 +02:00
Paul B Mahol
3594788b71 avcodec/utvideodec: decode to GBR(A)P
This is actually internal utvideo format.
Allows to make use of SIMD for median prediction for rgb(a) formats,
thus speeding up decoding.
Simplifies code, eases further developement and maintenance.

Update FATE because of pixel format switch.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
2017-06-26 20:11:58 +02:00
Paul B Mahol
7c25523cc8 utvideodec: Fix decoding odd sizes with interlaced video with some formats
Signed-off-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
2017-04-25 00:38:07 +02:00
Paul B Mahol
4925537004 avcodec/utvideodec: fix gradient prediction when stride does not match width
Fixes #6340.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
2017-04-21 21:46:00 +02:00
Paul B Mahol
9ef21a897c avcodec/utvideodec: fix decoding odd sizes with interlaced video with some formats
Fixes #6316.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
2017-04-21 12:55:30 +02:00
Paul B Mahol
378460fef1 utvideodec: Support for gradient prediction
Introduced with utvideo 18.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
2017-04-15 15:37:18 +02:00
Paul B Mahol
9227bd8ac2 utvideodec: Reuse the huffyuv add_left
~10% faster when simd is available.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
2017-04-15 15:37:18 +02:00
Paul B Mahol
4f33d9d41a utvideodec: Support ULY4 and ULH4
Signed-off-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
2017-04-15 15:37:18 +02:00
Paul B Mahol
a93faf30d6 utvideodec: Support UQRA and UQRG 2017-04-15 15:37:18 +02:00
Paul B Mahol
c523095564 utvideodec: Support UQY2
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
2017-04-13 14:09:07 +02:00
Ganesh Ajjanagadde
1fe858136b utvideodec: Prevent possible signed overflow
Doing slice_end - slice_start is unsafe and can lead to undefined behavior
until slice_end has been properly sanitized.

Reviewed-by: Ronald S. Bultje <rsbultje@gmail.com>
Signed-off-by: Ganesh Ajjanagadde <gajjanag@gmail.com>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
2017-04-13 13:37:10 +02:00
Paul B Mahol
faa94a576f avcodec/utvideodec: add support for gradient prediction
Signed-off-by: Paul B Mahol <onemda@gmail.com>
2017-04-07 20:11:23 +02:00
Clément Bœsch
eed8ccde3e Merge commit '131a85a1fed9966bbd38517f76abfac0237e39dc'
* commit '131a85a1fed9966bbd38517f76abfac0237e39dc':
  utvideo: Change type of array stride parameters to ptrdiff_t

Merged-by: Clément Bœsch <u@pkh.me>
2017-03-20 11:33:48 +01:00
Martin Storsjö
bc25897630 utvideodec: Add a missing include
This was missing from 77c23704c7, fixing building.

Signed-off-by: Martin Storsjö <martin@martin.st>
2017-02-10 09:31:49 +02:00
Derek Buitenhuis
77c23704c7 avcodec: Mark some codecs with threadsafe init as such
Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
2017-02-09 23:28:18 +01:00
Derek Buitenhuis
91ed4e7196 avcodec: Mark some codecs with threadsafe init as such
Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-02-07 23:31:25 +01:00
Diego Biurrun
a25dac976a Use bitstream_init8() where appropriate 2017-02-07 18:27:21 +01:00
James Almer
47f212329e huffyuvdsp: move functions only used by huffyuv from lossless_videodsp
Signed-off-by: James Almer <jamrial@gmail.com>
2017-01-12 22:53:05 -03:00
James Almer
5ac1dd8e23 lossless_videodsp: move shared functions from huffyuvdsp
Several codecs other than huffyuv use them.

Signed-off-by: James Almer <jamrial@gmail.com>
2017-01-12 22:53:04 -03:00
Paul B Mahol
68e5598e22 avcodec/utvideo: fix mistake using wrong arguments for left and lefttop pixel components
Signed-off-by: Paul B Mahol <onemda@gmail.com>
2016-12-24 10:59:26 +01:00
Paul B Mahol
ea93052db3 avcodec/utvideodec: add SIMD support for median prediction for planar formats
~10% faster overall.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
2016-12-23 17:44:01 +01:00
Alexandra Hájková
104a4289f9 utvideodec: Convert to the new bitstream reader 2016-11-24 11:22:12 +01:00
Diego Biurrun
131a85a1fe utvideo: Change type of array stride parameters to ptrdiff_t
ptrdiff_t is the correct type for array strides and similar.
2016-09-08 13:51:30 +02:00
Paul B Mahol
c62cb9bf5a avcodec/utvideodec: add support for ULY4 and ULH4
Signed-off-by: Paul B Mahol <onemda@gmail.com>
2016-09-04 01:57:50 +02:00
Paul B Mahol
84efdabc94 avcodec/utvideodec: add support for UQRG and UQRA formats
Signed-off-by: Paul B Mahol <onemda@gmail.com>
2016-06-12 01:07:23 +02:00
Paul B Mahol
3ecc59bc35 avcodec/utvideodec: fix multiple slices for UQY2 and other issues
Signed-off-by: Paul B Mahol <onemda@gmail.com>
2016-06-11 21:31:49 +02:00