You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

830 lines
23KB

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