Commit Graph

145 Commits

Author SHA1 Message Date
Andreas Rheinhardt e60debb93e avcodec/bitstream: Don't pretend VLCs to be initialized concurrently
Since the MPEG-4 parser no longer initializes some MPEG-4 VLCs,
no VLC is initialized concurrently by multiple threads
(initializing static VLCs is guarded by locks and nonstatic VLCs
never posed an issue in this regard). So remove the code
in bitstream.c that only exists because of this possibility.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-01-09 09:39:09 +01:00
Andreas Rheinhardt 25c8507818 Remove/replace some unnecessary avcodec.h inclusions
Also remove other unnecessary headers and include headers directly while
at it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-07-22 15:29:46 +02:00
Andreas Rheinhardt c197e3fe12 avcodec/bitstream: Remove avpriv PutBits API functions
Scheduled for removal in 717503f716.
Also remove PutBitContext.size_in_bits which has been scheduled
for removal in e7cbbd9026.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2021-04-27 10:43:03 -03:00
Andreas Rheinhardt 3bf07b1a2d avcodec/bitstream: Rewrite code to avoid triggering compiler warning
Clang infers from the existence of a default case that said case can be
taken. In case of libavcodec/bitstream.c said default case consisted of
an av_assert1 that evaluates to nothing in case of the ordinary assert
level. In this case (that doesn't happen) a variable wouldn't be
initialized, so Clang emitted Wsometimes-uninitialized warnings.
Solve this by making sure that the default path also initializes
the aforementioned variable.

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-24 07:50:56 +01:00
Andreas Rheinhardt b629deab3d avcodec/bitstream: Allow static VLC tables to be bigger than needed
Right now the allocated size of the VLC table of a static VLC has to
exactly match the size actually used for the VLC: If it is not enough,
abort is called; if it is more than enough, an error message is
emitted. This is no problem when one wants to initialize an individual
VLC via one of the INIT_VLC macros as one just hardcodes the needed
size. Yet it is an obstacle when one wants to initialize several VLCs
in a loop as one then needs to add an array for the sizes/offsets of
the VLC tables (unless max_depth of all arrays is one in which case
the sizes are derivable from the number of bits used).

Yet said size array is not necessary if one disables the warning for too
big buffers. The reason is that the amount of entries needed for the
table is of course generated as a byproduct of initializing the VLC.
To this end a flag that disables the warning has been added.
So one can proceed as follows:

static VLC vlcs[NUM];
static VLC_TYPE vlc_table[BUF_SIZE][2];

for (int i = 0, offset = 0; i < NUM; i++) {
    vlcs[i].table           = &vlc_table[offset];
    vlcs[i].table_allocated = BUF_SIZE - offset;
    init_vlc(); /* With INIT_VLC_STATIC_OVERLONG flag */
    offset += vlcs[i].table_size;
}

Of course, BUF_SIZE should be equal to the number of entries actually
needed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-12-08 17:51:44 +01:00
Andreas Rheinhardt f55da066ec avcodec/bitstream: Add second function to create VLCs
When using ff_init_vlc_sparse() to create a VLC, three input tables are
used: A table for lengths, one for codes and one for symbols; the latter
one can be omitted, then a default one will be used. These input tables
will be traversed twice, once to get the long codes (which will be put
into subtables) and once for the small codes. The long codes are then
sorted so that entries that should be in the same subtable are
contiguous.

This commit adds an alternative to ff_init_vlc_sparse():
ff_init_vlc_from_lengths(). It is based upon the observation that if
lengths, codes and symbols tables are permuted (in the same way) so that
the codes are ordered from left to right in the corresponding tree and
if said tree is complete (i.e. every non-leaf node has two children),
the codes can be easily computed from the lengths and are therefore
redundant. This means that if one initializes such a VLC with explicitly
coded lengths, codes and symbols, the codes can be avoided; and even if
one has no explicitly coded symbols, it might still be beneficial to
remove the codes even when one has to add a new symbol table, because
codes are typically longer than symbols so that the latter often fit
into a smaller type, saving space.

Furthermore, given that the codes here are by definition ordered from
left to right, it is unnecessary to sort them again; for the same
reason, one does not have to traverse the input twice. This function
proved to be faster than ff_init_vlc_sparse() whenever it has been
benchmarked.

This function is usable for static tables (they can simply be permuted
once) as well as in scenarios where the tables are naturally ordered
from left to right in the tree; the latter e.g. happens with Smacker,
Theora and several other formats.

In order to make it also usable for (static) tables with incomplete trees,
negative lengths are used to indicate that there is an open end of a
certain length.

Finally, ff_init_vlc_from_lengths() has one downside compared to
ff_init_vlc_sparse(): The latter uses tables that can be reused by
encoders. Of course, one could calculate the needed table at runtime
if one so wishes, but it is nevertheless an obstacle.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-12-08 17:51:44 +01:00
Anton Khirnov dc1099442e put_bits: make avpriv_copy_bits() lavc-local
It is not used outside of lavc anymore. Keep the avpriv exported symbol
around until the next bump to preserve ABI compatibility.
2020-10-28 13:53:23 +01:00
Anton Khirnov 944ba30db0 put_bits: make avpriv_put_string() lavc-local
It has not been used outside of libavcodec since
20f325f320
2020-10-28 13:53:23 +01:00
Anton Khirnov 717503f716 put_bits: make avpriv_align_put_bits() inline
This function is so extremely simple that it is preferable to make it
inline rather than deal with all the complications arising from it being
an exported symbol.

Keep avpriv_align_put_bits() around until the next major bump to
preserve ABI compatibility.
2020-10-28 13:53:23 +01:00
Andreas Rheinhardt 81d4b8fb3c avcodec/bitstream: Stop allocating one VLCcode more than needed
Allocating one temporary entry more than needed was made necessary by
the COPY loop below writing an element before having checked that it
should be written at all. But given that this behaviour changed, the
need for overallocating is gone.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-28 11:28:24 +01:00
Andreas Rheinhardt e75b6ec43b avcodec/bitstream: Check code length before truncating to uint8_t
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-28 11:23:33 +01:00
Andreas Rheinhardt df6ec7f83b avcodec/bitstream: Consistently treat symbol as VLC_TYPE
If a static VLC table gets initialized a second time (or concurrently by
two threads) and if said VLC table uses symbols that have the sign bit
of VLC_TYPE (a typedef for int16_t) set, initializing the VLC fails. The
reason is that the type of the symbol in the temporary array is an
uint16_t and so comparing it to the symbol read from the VLC table will
fail, because only the lower 16bits coincide. Said failure triggers an
assert.

Reviewed-by: Lynne <dev@lynne.ee>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-27 10:34:23 +01:00
Andreas Rheinhardt 9eb7d8b45d avcodec/vlc, bitstream: Allow to use BE codes to initialize LE VLC
This is easily possible because ff_init_vlc_sparse() already transforms
both LE as well as BE codes to a normal form internally before
processing them further. This will be used in subsequent commits.

Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-12 22:20:37 +02:00
Andreas Rheinhardt 6fad76b51d avcodec/bitstream: Remove outdated comment
The comment referred to the INIT_VLC_USE_STATIC flag which has been
removed in 2009 in 595324e143b57a52e2329eb47b84395c70f93087; the
function it referred to was removed even earlier in commit
83422c1940 in 2008.

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-09-01 09:07:58 +02:00
Andreas Rheinhardt d7ad70e33b avcodec/bitstream: Avoid allocation when creating VLC tables
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-06-27 23:31:34 +02:00
Andreas Rheinhardt 5e196dac22 avcodec/bitstream: Don't check for undefined behaviour after it happened
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-06-27 23:30:54 +02:00
Michael Niedermayer a7e3b271fc avcodec/bitstream: Check for more conflicting codes in build_table()
Fixes: out of array read
Fixes: 14563/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_AGM_fuzzer-5646451545210880

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2019-06-14 21:36:39 +02:00
Michael Niedermayer e78b0f8374 avcodec/bitstream: Check for integer code truncation in build_table()
Fixes: out of array read
Fixes: 14563/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_AGM_fuzzer-5646451545210880

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2019-06-14 21:36:39 +02:00
Michael Niedermayer 057328b29d avcodec/bitstream: Return specific error codes when building vlc tables
Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2019-01-01 21:11:47 +01:00
Clément Bœsch 549045254c Fix all -Wformat warnings raised by DJGPP 2017-03-29 14:49:29 +02:00
Clément Bœsch cb763a9ba8 lavc/bitstream: remove unused atomic.h include 2017-03-22 18:16:58 +01:00
Steinar H. Gunderson eaff1aa09e avcodec: move bitswap_32() into a header file
Allows more codecs than mpeg12video to make use of it.
2017-01-11 15:40:01 +01:00
Michael Niedermayer 7ca2a23aaa avcodec/bitstream: Document the values supported for *_size in ff_init_vlc_sparse()
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-01-05 12:08:24 +01:00
Michael Niedermayer 8f1d18a91b avcodec/bitstream: assert that *_size in ff_init_vlc_sparse() is valid
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-01-05 12:08:23 +01:00
Clément Bœsch bcafc41a5a Merge commit 'ffa190d0479d2370dd89c95692f822cbff2cc24c'
* commit 'ffa190d0479d2370dd89c95692f822cbff2cc24c':
  Move VLC and RL_VLC_ELEM structure definitions to a separate header

Merged-by: Clément Bœsch <u@pkh.me>
2016-06-23 00:29:25 +02:00
Clément Bœsch 8ef57a0d61 Merge commit '41ed7ab45fc693f7d7fc35664c0233f4c32d69bb'
* commit '41ed7ab45fc693f7d7fc35664c0233f4c32d69bb':
  cosmetics: Fix spelling mistakes

Merged-by: Clément Bœsch <u@pkh.me>
2016-06-21 21:55:34 +02:00
Alexandra Hájková ffa190d047 Move VLC and RL_VLC_ELEM structure definitions to a separate header
Use the newly created vlc.h directly instead of including get_bits when needed.
The VLC and RL_VLC_ELEM structures are independent from the bitreader.

Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
2016-05-17 10:29:27 +02:00
Vittorio Giovara 41ed7ab45f cosmetics: Fix spelling mistakes
Signed-off-by: Diego Biurrun <diego@biurrun.de>
2016-05-04 18:16:21 +02:00
Lou Logan 06eef96b69 fix some a/an typos
Signed-off-by: Lou Logan <lou@lrcd.com>
2016-03-28 14:13:17 -08:00
Reimar Döffinger 5f5e6033cd bitstream.c: improve init_vlc error messages.
Makes it far easier to spot the issue if e.g.
caused by a typo in the code table.

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger@gmx.de>
2016-03-06 12:31:40 +01:00
Ganesh Ajjanagadde e11e32686f avcodec/bitstream: replace qsort with AV_QSORT
Commit 3a0a2f33a6 claims large performance
advantages for AV_QSORT over libc's qsort. The reason is that I suspect
that libc's qsort (at least on non LTO builds, like the typical FFmpeg config)
can't inline the comparison callback:
https://stackoverflow.com/questions/5290695/is-there-any-way-a-c-c-compiler-can-inline-a-c-callback-function.
AV_QSORT has two things going for it:
1. The guaranteed inlining of qsort itself. This yields a negligible
boost that may be ignored.
2. The more serious possibility of potentially allowing the comparison
function to be inlined - this is likely responsible for the large boosts
reported.

There is a comment explaining that this is a place that could use some
performance improvement. Thus AV_QSORT is used to achieve that.

Benchmarks deemed unnecessary due to existing claims about AV_QSORT.
Tested with FATE.

Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: Ganesh Ajjanagadde <gajjanagadde@gmail.com>
2015-10-18 09:22:49 -04:00
Andreas Cadhalpun 28efeb6502 doc: avoid incorrect phrase 'allows to'
Also fix typo found by Lou Logan:
Sacrifying -> Sacrificing

Reviewed-by: Lou Logan <lou@lrcd.com>
Signed-off-by: Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
2015-06-16 21:48:51 +02:00
Andreas Cadhalpun ed0b1db640 doc: fix spelling errors
Neccessary -> Necessary
formated   -> formatted
thee       -> the
eventhough -> even though
seperately -> separately

Reviewed-by: Michael Niedermayer <michaelni@gmx.at>
Signed-off-by: Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
2015-06-14 15:09:33 +02:00
Michael Niedermayer 291ad5cc9c avcodec/bitstream: Assert that there is enough space left in avpriv_copy_bits()
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2015-05-25 05:14:02 +02:00
Michael Niedermayer 8f7b022c8c Merge commit '6a85dfc830f51f1f5c2d36d4182d265c1ea3ba25'
* commit '6a85dfc830f51f1f5c2d36d4182d265c1ea3ba25':
  lavc: Replace av_dlog and tprintf with internal macros

Conflicts:
	libavcodec/aacdec.c
	libavcodec/audio_frame_queue.c
	libavcodec/bitstream.c
	libavcodec/dcadec.c
	libavcodec/dnxhddec.c
	libavcodec/dvbsubdec.c
	libavcodec/dvdec.c
	libavcodec/dvdsubdec.c
	libavcodec/get_bits.h
	libavcodec/gifdec.c
	libavcodec/h264.h
	libavcodec/h264_cabac.c
	libavcodec/h264_cavlc.c
	libavcodec/h264_loopfilter.c
	libavcodec/h264_refs.c
	libavcodec/imc.c
	libavcodec/interplayvideo.c
	libavcodec/jpeglsdec.c
	libavcodec/libopencore-amr.c
	libavcodec/mjpegdec.c
	libavcodec/mpeg12dec.c
	libavcodec/mpegvideo_enc.c
	libavcodec/mpegvideo_parser.c
	libavcodec/pngdec.c
	libavcodec/ratecontrol.c
	libavcodec/rv10.c
	libavcodec/svq1dec.c
	libavcodec/vqavideo.c
	libavcodec/wmadec.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
2015-04-20 04:10:10 +02:00
Vittorio Giovara 6a85dfc830 lavc: Replace av_dlog and tprintf with internal macros 2015-04-19 12:41:59 +01:00
Michael Niedermayer a94eba6f0c Merge commit '7f9f771eac0d37a632e0ed9bd89961d57fcfb7e0'
* commit '7f9f771eac0d37a632e0ed9bd89961d57fcfb7e0':
  avcodec: Don't anonymously typedef structs

Conflicts:
	libavcodec/alac.c
	libavcodec/cinepak.c
	libavcodec/cscd.c
	libavcodec/dcadec.c
	libavcodec/g723_1.c
	libavcodec/gif.c
	libavcodec/iff.c
	libavcodec/kgv1dec.c
	libavcodec/libopenjpegenc.c
	libavcodec/libspeexenc.c
	libavcodec/ra288.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
2015-02-14 21:18:17 +01:00
Diego Biurrun 7f9f771eac avcodec: Don't anonymously typedef structs 2015-02-14 10:13:49 -08:00
Michael Niedermayer d266ecff4b avcodec/bitstream: remove trivial assert
Fixed CID1224273

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-07-02 16:25:40 +02:00
Michael Niedermayer a2de7b1bd5 avcodec/bitstream: document the double volatile
Suggested-by: Reimar Döffinger <Reimar.Doeffinger@gmx.de>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-06-21 14:55:32 +02:00
Michael Niedermayer 329898aa45 avcodec/bitstream: try to workaround internal compiler bug in gcc 4.2
gcc 4.2 seems not maintained anymore so theres no option besides
just working around it.

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-06-20 21:54:23 +02:00
Michael Niedermayer e6f9fc4adc avcodec/bitstream: try to make vlc init code inherently thread safe
also remove spinlock, it doesnt work on AIX

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-06-16 20:09:21 +02:00
Michael Niedermayer f7f96cf4bc avcodec/bitstream: fill invalid vlc tables entries as last pass instead of first
This avoids writing entries twice

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-06-16 19:10:32 +02:00
Michael Niedermayer 600cbf3672 avcodec/bitstream: zero vlc tables on allocation
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-06-16 19:10:32 +02:00
Michael Niedermayer e20e854ca0 vcodec/bitstream: use av_malloc_array()
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-04-08 16:18:29 +02:00
Michael Niedermayer 622d463000 avcodec/bitstream: assert that no integer overflow happened when writing codes in build_table()
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-01-23 01:43:00 +01:00
Michael Niedermayer 0f65503799 avcodec/bitstream: remove unused variable
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2013-12-11 14:34:45 +01:00
Michael Niedermayer 8c677a9f06 Merge commit '9b8d11a76ae7bca8bbb58abb822138f8b42c776c'
* commit '9b8d11a76ae7bca8bbb58abb822138f8b42c776c':
  avcodec: Use av_reallocp where suitable

Conflicts:
	libavcodec/bitstream.c
	libavcodec/eatgv.c
	libavcodec/flashsv.c
	libavcodec/libtheoraenc.c
	libavcodec/libvpxenc.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-12-09 20:31:29 +01:00
Alexandra Khirnova 9b8d11a76a avcodec: Use av_reallocp where suitable
Signed-off-by: Martin Storsjö <martin@martin.st>
2013-12-09 12:27:51 +02:00
Diego Biurrun 29c455ce3d bitstream: Check the result of av_malloc() 2013-10-15 18:01:12 +02:00