get_bits.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * bitstream reader API header.
  23. */
  24. #ifndef AVCODEC_GET_BITS_H
  25. #define AVCODEC_GET_BITS_H
  26. #include <stdint.h>
  27. #include "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/avassert.h"
  30. #include "defs.h"
  31. #include "mathops.h"
  32. #include "vlc.h"
  33. /*
  34. * Safe bitstream reading:
  35. * optionally, the get_bits API can check to ensure that we
  36. * don't read past input buffer boundaries. This is protected
  37. * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
  38. * then below that with UNCHECKED_BITSTREAM_READER at the per-
  39. * decoder level. This means that decoders that check internally
  40. * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
  41. * overread checks.
  42. * Boundary checking causes a minor performance penalty so for
  43. * applications that won't want/need this, it can be disabled
  44. * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
  45. */
  46. #ifndef UNCHECKED_BITSTREAM_READER
  47. #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
  48. #endif
  49. #ifndef CACHED_BITSTREAM_READER
  50. #define CACHED_BITSTREAM_READER 0
  51. #endif
  52. #if CACHED_BITSTREAM_READER
  53. // we always want the LE implementation, to provide get_bits_le()
  54. #define BITSTREAM_LE
  55. #ifndef BITSTREAM_READER_LE
  56. # define BITSTREAM_BE
  57. # define BITSTREAM_DEFAULT_BE
  58. #endif
  59. #include "bitstream.h"
  60. #undef BITSTREAM_LE
  61. #undef BITSTREAM_BE
  62. #undef BITSTREAM_DEFAULT_BE
  63. typedef BitstreamContext GetBitContext;
  64. #define get_bits_count bits_tell
  65. #define get_bits_bytesize bits_bytesize
  66. #define get_bits_left bits_left
  67. #define skip_bits_long bits_skip
  68. #define skip_bits bits_skip
  69. #define get_bits bits_read_nz
  70. #define get_bitsz bits_read
  71. #define get_bits_long bits_read
  72. #define get_bits1 bits_read_bit
  73. #define get_bits64 bits_read_64
  74. #define get_xbits bits_read_xbits
  75. #define get_sbits bits_read_signed_nz
  76. #define get_sbits_long bits_read_signed
  77. #define show_bits bits_peek
  78. #define show_bits_long bits_peek
  79. #define init_get_bits bits_init
  80. #define init_get_bits8 bits_init8
  81. #define align_get_bits bits_align
  82. #define get_vlc2 bits_read_vlc
  83. #define get_vlc_multi bits_read_vlc_multi
  84. #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size)
  85. #define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n)
  86. #define show_bits1(s) bits_peek(s, 1)
  87. #define skip_bits1(s) bits_skip(s, 1)
  88. #define skip_1stop_8data_bits bits_skip_1stop_8data
  89. #else // CACHED_BITSTREAM_READER
  90. typedef struct GetBitContext {
  91. const uint8_t *buffer;
  92. int index;
  93. int size_in_bits;
  94. int size_in_bits_plus8;
  95. } GetBitContext;
  96. static inline unsigned int get_bits(GetBitContext *s, int n);
  97. static inline void skip_bits(GetBitContext *s, int n);
  98. static inline unsigned int show_bits(GetBitContext *s, int n);
  99. /* Bitstream reader API docs:
  100. * name
  101. * arbitrary name which is used as prefix for the internal variables
  102. *
  103. * gb
  104. * getbitcontext
  105. *
  106. * OPEN_READER(name, gb)
  107. * load gb into local variables
  108. *
  109. * CLOSE_READER(name, gb)
  110. * store local vars in gb
  111. *
  112. * UPDATE_CACHE(name, gb)
  113. * Refill the internal cache from the bitstream.
  114. * After this call at least MIN_CACHE_BITS will be available.
  115. *
  116. * GET_CACHE(name, gb)
  117. * Will output the contents of the internal cache,
  118. * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
  119. *
  120. * SHOW_UBITS(name, gb, num)
  121. * Will return the next num bits.
  122. *
  123. * SHOW_SBITS(name, gb, num)
  124. * Will return the next num bits and do sign extension.
  125. *
  126. * SKIP_BITS(name, gb, num)
  127. * Will skip over the next num bits.
  128. * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
  129. *
  130. * SKIP_CACHE(name, gb, num)
  131. * Will remove the next num bits from the cache (note SKIP_COUNTER
  132. * MUST be called before UPDATE_CACHE / CLOSE_READER).
  133. *
  134. * SKIP_COUNTER(name, gb, num)
  135. * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
  136. *
  137. * LAST_SKIP_BITS(name, gb, num)
  138. * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
  139. *
  140. * BITS_LEFT(name, gb)
  141. * Return the number of bits left
  142. *
  143. * For examples see get_bits, show_bits, skip_bits, get_vlc.
  144. */
  145. #define MIN_CACHE_BITS 25
  146. #define OPEN_READER_NOSIZE(name, gb) \
  147. unsigned int name ## _index = (gb)->index; \
  148. av_unused unsigned int name ## _cache
  149. #if UNCHECKED_BITSTREAM_READER
  150. #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
  151. #define BITS_AVAILABLE(name, gb) 1
  152. #else
  153. #define OPEN_READER(name, gb) \
  154. OPEN_READER_NOSIZE(name, gb); \
  155. unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
  156. #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
  157. #endif
  158. #define CLOSE_READER(name, gb) (gb)->index = name ## _index
  159. #define UPDATE_CACHE_BE_EXT(name, gb, bits, dst_bits) name ## _cache = \
  160. AV_RB ## bits((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) >> (bits - dst_bits)
  161. #define UPDATE_CACHE_LE_EXT(name, gb, bits, dst_bits) name ## _cache = \
  162. (uint ## dst_bits ## _t)(AV_RL ## bits((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7))
  163. /* Using these two macros ensures that 32 bits are available. */
  164. # define UPDATE_CACHE_LE_32(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 64, 32)
  165. # define UPDATE_CACHE_BE_32(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 64, 32)
  166. # define UPDATE_CACHE_LE(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 32, 32)
  167. # define UPDATE_CACHE_BE(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 32, 32)
  168. #ifdef BITSTREAM_READER_LE
  169. # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
  170. # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_LE_32(name, (gb))
  171. # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
  172. #else
  173. # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
  174. # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_BE_32(name, (gb))
  175. # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
  176. #endif
  177. #if UNCHECKED_BITSTREAM_READER
  178. # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
  179. #else
  180. # define SKIP_COUNTER(name, gb, num) \
  181. name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
  182. #endif
  183. #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
  184. #define SKIP_BITS(name, gb, num) \
  185. do { \
  186. SKIP_CACHE(name, gb, num); \
  187. SKIP_COUNTER(name, gb, num); \
  188. } while (0)
  189. #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  190. #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
  191. #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
  192. #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
  193. #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
  194. #ifdef BITSTREAM_READER_LE
  195. # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
  196. # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
  197. #else
  198. # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
  199. # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
  200. #endif
  201. #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
  202. static inline int get_bits_count(const GetBitContext *s)
  203. {
  204. return s->index;
  205. }
  206. /**
  207. * Get the size of the GetBitContext's buffer in bytes.
  208. *
  209. * @param s the GetBitContext
  210. * @param round_up If set, the number of bits will be rounded up to full bytes;
  211. * this does not matter if the number of bits is known to be
  212. * a multiple of eight, e.g. if the GetBitContext has been
  213. * initialized with init_get_bits8.
  214. */
  215. static inline int get_bits_bytesize(const GetBitContext *s, int round_up)
  216. {
  217. return (s->size_in_bits + (round_up ? 7 : 0)) >> 3;
  218. }
  219. /**
  220. * Skips the specified number of bits.
  221. * @param n the number of bits to skip,
  222. * For the UNCHECKED_BITSTREAM_READER this must not cause the distance
  223. * from the start to overflow int32_t. Staying within the bitstream + padding
  224. * is sufficient, too.
  225. */
  226. static inline void skip_bits_long(GetBitContext *s, int n)
  227. {
  228. #if UNCHECKED_BITSTREAM_READER
  229. s->index += n;
  230. #else
  231. s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
  232. #endif
  233. }
  234. /**
  235. * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
  236. * if MSB not set it is negative
  237. * @param n length in bits
  238. */
  239. static inline int get_xbits(GetBitContext *s, int n)
  240. {
  241. register int sign;
  242. register int32_t cache;
  243. OPEN_READER(re, s);
  244. av_assert2(n>0 && n<=25);
  245. UPDATE_CACHE(re, s);
  246. cache = GET_CACHE(re, s);
  247. sign = ~cache >> 31;
  248. LAST_SKIP_BITS(re, s, n);
  249. CLOSE_READER(re, s);
  250. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  251. }
  252. static inline int get_xbits_le(GetBitContext *s, int n)
  253. {
  254. register int sign;
  255. register int32_t cache;
  256. OPEN_READER(re, s);
  257. av_assert2(n>0 && n<=25);
  258. UPDATE_CACHE_LE(re, s);
  259. cache = GET_CACHE(re, s);
  260. sign = sign_extend(~cache, n) >> 31;
  261. LAST_SKIP_BITS(re, s, n);
  262. CLOSE_READER(re, s);
  263. return (zero_extend(sign ^ cache, n) ^ sign) - sign;
  264. }
  265. static inline int get_sbits(GetBitContext *s, int n)
  266. {
  267. register int tmp;
  268. OPEN_READER(re, s);
  269. av_assert2(n>0 && n<=25);
  270. UPDATE_CACHE(re, s);
  271. tmp = SHOW_SBITS(re, s, n);
  272. LAST_SKIP_BITS(re, s, n);
  273. CLOSE_READER(re, s);
  274. return tmp;
  275. }
  276. /**
  277. * Read 1-25 bits.
  278. */
  279. static inline unsigned int get_bits(GetBitContext *s, int n)
  280. {
  281. register unsigned int tmp;
  282. OPEN_READER(re, s);
  283. av_assert2(n>0 && n<=25);
  284. UPDATE_CACHE(re, s);
  285. tmp = SHOW_UBITS(re, s, n);
  286. LAST_SKIP_BITS(re, s, n);
  287. CLOSE_READER(re, s);
  288. av_assert2(tmp < UINT64_C(1) << n);
  289. return tmp;
  290. }
  291. /**
  292. * Read 0-25 bits.
  293. */
  294. static av_always_inline int get_bitsz(GetBitContext *s, int n)
  295. {
  296. return n ? get_bits(s, n) : 0;
  297. }
  298. static inline unsigned int get_bits_le(GetBitContext *s, int n)
  299. {
  300. register int tmp;
  301. OPEN_READER(re, s);
  302. av_assert2(n>0 && n<=25);
  303. UPDATE_CACHE_LE(re, s);
  304. tmp = SHOW_UBITS_LE(re, s, n);
  305. LAST_SKIP_BITS(re, s, n);
  306. CLOSE_READER(re, s);
  307. return tmp;
  308. }
  309. /**
  310. * Show 1-25 bits.
  311. */
  312. static inline unsigned int show_bits(GetBitContext *s, int n)
  313. {
  314. register unsigned int tmp;
  315. OPEN_READER_NOSIZE(re, s);
  316. av_assert2(n>0 && n<=25);
  317. UPDATE_CACHE(re, s);
  318. tmp = SHOW_UBITS(re, s, n);
  319. return tmp;
  320. }
  321. static inline void skip_bits(GetBitContext *s, int n)
  322. {
  323. OPEN_READER(re, s);
  324. LAST_SKIP_BITS(re, s, n);
  325. CLOSE_READER(re, s);
  326. }
  327. static inline unsigned int get_bits1(GetBitContext *s)
  328. {
  329. unsigned int index = s->index;
  330. uint8_t result = s->buffer[index >> 3];
  331. #ifdef BITSTREAM_READER_LE
  332. result >>= index & 7;
  333. result &= 1;
  334. #else
  335. result <<= index & 7;
  336. result >>= 8 - 1;
  337. #endif
  338. #if !UNCHECKED_BITSTREAM_READER
  339. if (s->index < s->size_in_bits_plus8)
  340. #endif
  341. index++;
  342. s->index = index;
  343. return result;
  344. }
  345. static inline unsigned int show_bits1(GetBitContext *s)
  346. {
  347. return show_bits(s, 1);
  348. }
  349. static inline void skip_bits1(GetBitContext *s)
  350. {
  351. skip_bits(s, 1);
  352. }
  353. /**
  354. * Read 0-32 bits.
  355. */
  356. static inline unsigned int get_bits_long(GetBitContext *s, int n)
  357. {
  358. av_assert2(n>=0 && n<=32);
  359. if (!n) {
  360. return 0;
  361. } else if ((!HAVE_FAST_64BIT || av_builtin_constant_p(n <= MIN_CACHE_BITS))
  362. && n <= MIN_CACHE_BITS) {
  363. return get_bits(s, n);
  364. } else {
  365. #if HAVE_FAST_64BIT
  366. unsigned tmp;
  367. OPEN_READER(re, s);
  368. UPDATE_CACHE_32(re, s);
  369. tmp = SHOW_UBITS(re, s, n);
  370. LAST_SKIP_BITS(re, s, n);
  371. CLOSE_READER(re, s);
  372. return tmp;
  373. #else
  374. #ifdef BITSTREAM_READER_LE
  375. unsigned ret = get_bits(s, 16);
  376. return ret | (get_bits(s, n - 16) << 16);
  377. #else
  378. unsigned ret = get_bits(s, 16) << (n - 16);
  379. return ret | get_bits(s, n - 16);
  380. #endif
  381. #endif
  382. }
  383. }
  384. /**
  385. * Read 0-64 bits.
  386. */
  387. static inline uint64_t get_bits64(GetBitContext *s, int n)
  388. {
  389. if (n <= 32) {
  390. return get_bits_long(s, n);
  391. } else {
  392. #ifdef BITSTREAM_READER_LE
  393. uint64_t ret = get_bits_long(s, 32);
  394. return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
  395. #else
  396. uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
  397. return ret | get_bits_long(s, 32);
  398. #endif
  399. }
  400. }
  401. /**
  402. * Read 0-32 bits as a signed integer.
  403. */
  404. static inline int get_sbits_long(GetBitContext *s, int n)
  405. {
  406. // sign_extend(x, 0) is undefined
  407. if (!n)
  408. return 0;
  409. return sign_extend(get_bits_long(s, n), n);
  410. }
  411. /**
  412. * Read 0-64 bits as a signed integer.
  413. */
  414. static inline int64_t get_sbits64(GetBitContext *s, int n)
  415. {
  416. // sign_extend(x, 0) is undefined
  417. if (!n)
  418. return 0;
  419. return sign_extend64(get_bits64(s, n), n);
  420. }
  421. /**
  422. * Show 0-32 bits.
  423. */
  424. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  425. {
  426. if (n <= MIN_CACHE_BITS) {
  427. return show_bits(s, n);
  428. } else {
  429. GetBitContext gb = *s;
  430. return get_bits_long(&gb, n);
  431. }
  432. }
  433. /**
  434. * Initialize GetBitContext.
  435. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  436. * larger than the actual read bits because some optimized bitstream
  437. * readers read 32 or 64 bit at once and could read over the end
  438. * @param bit_size the size of the buffer in bits
  439. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  440. */
  441. static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
  442. int bit_size)
  443. {
  444. int ret = 0;
  445. if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) {
  446. bit_size = 0;
  447. buffer = NULL;
  448. ret = AVERROR_INVALIDDATA;
  449. }
  450. s->buffer = buffer;
  451. s->size_in_bits = bit_size;
  452. s->size_in_bits_plus8 = bit_size + 8;
  453. s->index = 0;
  454. return ret;
  455. }
  456. /**
  457. * Initialize GetBitContext.
  458. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  459. * larger than the actual read bits because some optimized bitstream
  460. * readers read 32 or 64 bit at once and could read over the end
  461. * @param byte_size the size of the buffer in bytes
  462. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  463. */
  464. static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
  465. int byte_size)
  466. {
  467. if (byte_size > INT_MAX / 8 || byte_size < 0)
  468. byte_size = -1;
  469. return init_get_bits(s, buffer, byte_size * 8);
  470. }
  471. static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer,
  472. int byte_size)
  473. {
  474. if (byte_size > INT_MAX / 8 || byte_size < 0)
  475. byte_size = -1;
  476. return init_get_bits(s, buffer, byte_size * 8);
  477. }
  478. static inline const uint8_t *align_get_bits(GetBitContext *s)
  479. {
  480. int n = -get_bits_count(s) & 7;
  481. if (n)
  482. skip_bits(s, n);
  483. return s->buffer + (s->index >> 3);
  484. }
  485. /**
  486. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  487. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  488. * is undefined.
  489. */
  490. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  491. do { \
  492. int n, nb_bits; \
  493. unsigned int index; \
  494. \
  495. index = SHOW_UBITS(name, gb, bits); \
  496. code = table[index].sym; \
  497. n = table[index].len; \
  498. \
  499. if (max_depth > 1 && n < 0) { \
  500. LAST_SKIP_BITS(name, gb, bits); \
  501. UPDATE_CACHE(name, gb); \
  502. \
  503. nb_bits = -n; \
  504. \
  505. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  506. code = table[index].sym; \
  507. n = table[index].len; \
  508. if (max_depth > 2 && n < 0) { \
  509. LAST_SKIP_BITS(name, gb, nb_bits); \
  510. UPDATE_CACHE(name, gb); \
  511. \
  512. nb_bits = -n; \
  513. \
  514. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  515. code = table[index].sym; \
  516. n = table[index].len; \
  517. } \
  518. } \
  519. SKIP_BITS(name, gb, n); \
  520. } while (0)
  521. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  522. max_depth, need_update) \
  523. do { \
  524. int n, nb_bits; \
  525. unsigned int index; \
  526. \
  527. index = SHOW_UBITS(name, gb, bits); \
  528. level = table[index].level; \
  529. n = table[index].len8; \
  530. \
  531. if (max_depth > 1 && n < 0) { \
  532. SKIP_BITS(name, gb, bits); \
  533. if (need_update) { \
  534. UPDATE_CACHE(name, gb); \
  535. } \
  536. \
  537. nb_bits = -n; \
  538. \
  539. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  540. level = table[index].level; \
  541. n = table[index].len8; \
  542. if (max_depth > 2 && n < 0) { \
  543. LAST_SKIP_BITS(name, gb, nb_bits); \
  544. if (need_update) { \
  545. UPDATE_CACHE(name, gb); \
  546. } \
  547. nb_bits = -n; \
  548. \
  549. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  550. level = table[index].level; \
  551. n = table[index].len8; \
  552. } \
  553. } \
  554. run = table[index].run; \
  555. SKIP_BITS(name, gb, n); \
  556. } while (0)
  557. /**
  558. * Parse a vlc code.
  559. * @param bits is the number of bits which will be read at once, must be
  560. * identical to nb_bits in vlc_init()
  561. * @param max_depth is the number of times bits bits must be read to completely
  562. * read the longest vlc code
  563. * = (max_vlc_length + bits - 1) / bits
  564. * @returns the code parsed or -1 if no vlc matches
  565. */
  566. static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table,
  567. int bits, int max_depth)
  568. {
  569. int code;
  570. OPEN_READER(re, s);
  571. UPDATE_CACHE(re, s);
  572. GET_VLC(code, re, s, table, bits, max_depth);
  573. CLOSE_READER(re, s);
  574. return code;
  575. }
  576. static inline int get_vlc_multi(GetBitContext *s, uint8_t *dst,
  577. const VLC_MULTI_ELEM *const Jtable,
  578. const VLCElem *const table,
  579. const int bits, const int max_depth,
  580. const int symbols_size)
  581. {
  582. dst[0] = get_vlc2(s, table, bits, max_depth);
  583. return 1;
  584. }
  585. static inline int decode012(GetBitContext *gb)
  586. {
  587. int n;
  588. n = get_bits1(gb);
  589. if (n == 0)
  590. return 0;
  591. else
  592. return get_bits1(gb) + 1;
  593. }
  594. static inline int decode210(GetBitContext *gb)
  595. {
  596. if (get_bits1(gb))
  597. return 0;
  598. else
  599. return 2 - get_bits1(gb);
  600. }
  601. static inline int get_bits_left(GetBitContext *gb)
  602. {
  603. return gb->size_in_bits - get_bits_count(gb);
  604. }
  605. static inline int skip_1stop_8data_bits(GetBitContext *gb)
  606. {
  607. if (get_bits_left(gb) <= 0)
  608. return AVERROR_INVALIDDATA;
  609. while (get_bits1(gb)) {
  610. skip_bits(gb, 8);
  611. if (get_bits_left(gb) <= 0)
  612. return AVERROR_INVALIDDATA;
  613. }
  614. return 0;
  615. }
  616. #endif // CACHED_BITSTREAM_READER
  617. #endif /* AVCODEC_GET_BITS_H */