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.

718 lines
22KB

  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/log.h"
  30. #include "libavutil/avassert.h"
  31. #include "avcodec.h"
  32. #include "mathops.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. typedef struct GetBitContext {
  50. const uint8_t *buffer, *buffer_end;
  51. int index;
  52. int size_in_bits;
  53. int size_in_bits_plus8;
  54. } GetBitContext;
  55. #define VLC_TYPE int16_t
  56. typedef struct VLC {
  57. int bits;
  58. VLC_TYPE (*table)[2]; ///< code, bits
  59. int table_size, table_allocated;
  60. } VLC;
  61. typedef struct RL_VLC_ELEM {
  62. int16_t level;
  63. int8_t len;
  64. uint8_t run;
  65. } RL_VLC_ELEM;
  66. /* Bitstream reader API docs:
  67. * name
  68. * arbitrary name which is used as prefix for the internal variables
  69. *
  70. * gb
  71. * getbitcontext
  72. *
  73. * OPEN_READER(name, gb)
  74. * load gb into local variables
  75. *
  76. * CLOSE_READER(name, gb)
  77. * store local vars in gb
  78. *
  79. * UPDATE_CACHE(name, gb)
  80. * Refill the internal cache from the bitstream.
  81. * After this call at least MIN_CACHE_BITS will be available.
  82. *
  83. * GET_CACHE(name, gb)
  84. * Will output the contents of the internal cache,
  85. * next bit is MSB of 32 or 64 bit (FIXME 64bit).
  86. *
  87. * SHOW_UBITS(name, gb, num)
  88. * Will return the next num bits.
  89. *
  90. * SHOW_SBITS(name, gb, num)
  91. * Will return the next num bits and do sign extension.
  92. *
  93. * SKIP_BITS(name, gb, num)
  94. * Will skip over the next num bits.
  95. * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
  96. *
  97. * SKIP_CACHE(name, gb, num)
  98. * Will remove the next num bits from the cache (note SKIP_COUNTER
  99. * MUST be called before UPDATE_CACHE / CLOSE_READER).
  100. *
  101. * SKIP_COUNTER(name, gb, num)
  102. * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
  103. *
  104. * LAST_SKIP_BITS(name, gb, num)
  105. * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
  106. *
  107. * BITS_LEFT(name, gb)
  108. * Return the number of bits left
  109. *
  110. * For examples see get_bits, show_bits, skip_bits, get_vlc.
  111. */
  112. #ifdef LONG_BITSTREAM_READER
  113. # define MIN_CACHE_BITS 32
  114. #else
  115. # define MIN_CACHE_BITS 25
  116. #endif
  117. #define OPEN_READER_NOSIZE(name, gb) \
  118. unsigned int name ## _index = (gb)->index; \
  119. unsigned int av_unused name ## _cache
  120. #if UNCHECKED_BITSTREAM_READER
  121. #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
  122. #define BITS_AVAILABLE(name, gb) 1
  123. #else
  124. #define OPEN_READER(name, gb) \
  125. OPEN_READER_NOSIZE(name, gb); \
  126. unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
  127. #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
  128. #endif
  129. #define CLOSE_READER(name, gb) (gb)->index = name ## _index
  130. # ifdef LONG_BITSTREAM_READER
  131. # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
  132. AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
  133. # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
  134. AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
  135. #else
  136. # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
  137. AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
  138. # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
  139. AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
  140. #endif
  141. #ifdef BITSTREAM_READER_LE
  142. # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
  143. # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
  144. #else
  145. # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
  146. # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
  147. #endif
  148. #if UNCHECKED_BITSTREAM_READER
  149. # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
  150. #else
  151. # define SKIP_COUNTER(name, gb, num) \
  152. name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
  153. #endif
  154. #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
  155. #define SKIP_BITS(name, gb, num) \
  156. do { \
  157. SKIP_CACHE(name, gb, num); \
  158. SKIP_COUNTER(name, gb, num); \
  159. } while (0)
  160. #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  161. #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
  162. #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
  163. #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
  164. #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
  165. #ifdef BITSTREAM_READER_LE
  166. # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
  167. # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
  168. #else
  169. # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
  170. # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
  171. #endif
  172. #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
  173. static inline int get_bits_count(const GetBitContext *s)
  174. {
  175. return s->index;
  176. }
  177. static inline void skip_bits_long(GetBitContext *s, int n)
  178. {
  179. #if UNCHECKED_BITSTREAM_READER
  180. s->index += n;
  181. #else
  182. s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
  183. #endif
  184. }
  185. /**
  186. * read mpeg1 dc style vlc (sign bit + mantissa with no MSB).
  187. * if MSB not set it is negative
  188. * @param n length in bits
  189. */
  190. static inline int get_xbits(GetBitContext *s, int n)
  191. {
  192. register int sign;
  193. register int32_t cache;
  194. OPEN_READER(re, s);
  195. av_assert2(n>0 && n<=25);
  196. UPDATE_CACHE(re, s);
  197. cache = GET_CACHE(re, s);
  198. sign = ~cache >> 31;
  199. LAST_SKIP_BITS(re, s, n);
  200. CLOSE_READER(re, s);
  201. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  202. }
  203. static inline int get_sbits(GetBitContext *s, int n)
  204. {
  205. register int tmp;
  206. OPEN_READER(re, s);
  207. av_assert2(n>0 && n<=25);
  208. UPDATE_CACHE(re, s);
  209. tmp = SHOW_SBITS(re, s, n);
  210. LAST_SKIP_BITS(re, s, n);
  211. CLOSE_READER(re, s);
  212. return tmp;
  213. }
  214. /**
  215. * Read 1-25 bits.
  216. */
  217. static inline unsigned int get_bits(GetBitContext *s, int n)
  218. {
  219. register int tmp;
  220. OPEN_READER(re, s);
  221. av_assert2(n>0 && n<=25);
  222. UPDATE_CACHE(re, s);
  223. tmp = SHOW_UBITS(re, s, n);
  224. LAST_SKIP_BITS(re, s, n);
  225. CLOSE_READER(re, s);
  226. return tmp;
  227. }
  228. /**
  229. * Read 0-25 bits.
  230. */
  231. static av_always_inline int get_bitsz(GetBitContext *s, int n)
  232. {
  233. return n ? get_bits(s, n) : 0;
  234. }
  235. static inline unsigned int get_bits_le(GetBitContext *s, int n)
  236. {
  237. register int tmp;
  238. OPEN_READER(re, s);
  239. av_assert2(n>0 && n<=25);
  240. UPDATE_CACHE_LE(re, s);
  241. tmp = SHOW_UBITS_LE(re, s, n);
  242. LAST_SKIP_BITS(re, s, n);
  243. CLOSE_READER(re, s);
  244. return tmp;
  245. }
  246. /**
  247. * Show 1-25 bits.
  248. */
  249. static inline unsigned int show_bits(GetBitContext *s, int n)
  250. {
  251. register int tmp;
  252. OPEN_READER_NOSIZE(re, s);
  253. av_assert2(n>0 && n<=25);
  254. UPDATE_CACHE(re, s);
  255. tmp = SHOW_UBITS(re, s, n);
  256. return tmp;
  257. }
  258. static inline void skip_bits(GetBitContext *s, int n)
  259. {
  260. OPEN_READER(re, s);
  261. LAST_SKIP_BITS(re, s, n);
  262. CLOSE_READER(re, s);
  263. }
  264. static inline unsigned int get_bits1(GetBitContext *s)
  265. {
  266. unsigned int index = s->index;
  267. uint8_t result = s->buffer[index >> 3];
  268. #ifdef BITSTREAM_READER_LE
  269. result >>= index & 7;
  270. result &= 1;
  271. #else
  272. result <<= index & 7;
  273. result >>= 8 - 1;
  274. #endif
  275. #if !UNCHECKED_BITSTREAM_READER
  276. if (s->index < s->size_in_bits_plus8)
  277. #endif
  278. index++;
  279. s->index = index;
  280. return result;
  281. }
  282. static inline unsigned int show_bits1(GetBitContext *s)
  283. {
  284. return show_bits(s, 1);
  285. }
  286. static inline void skip_bits1(GetBitContext *s)
  287. {
  288. skip_bits(s, 1);
  289. }
  290. /**
  291. * Read 0-32 bits.
  292. */
  293. static inline unsigned int get_bits_long(GetBitContext *s, int n)
  294. {
  295. if (!n) {
  296. return 0;
  297. } else if (n <= MIN_CACHE_BITS) {
  298. return get_bits(s, n);
  299. } else {
  300. #ifdef BITSTREAM_READER_LE
  301. unsigned ret = get_bits(s, 16);
  302. return ret | (get_bits(s, n - 16) << 16);
  303. #else
  304. unsigned ret = get_bits(s, 16) << (n - 16);
  305. return ret | get_bits(s, n - 16);
  306. #endif
  307. }
  308. }
  309. /**
  310. * Read 0-64 bits.
  311. */
  312. static inline uint64_t get_bits64(GetBitContext *s, int n)
  313. {
  314. if (n <= 32) {
  315. return get_bits_long(s, n);
  316. } else {
  317. #ifdef BITSTREAM_READER_LE
  318. uint64_t ret = get_bits_long(s, 32);
  319. return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
  320. #else
  321. uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
  322. return ret | get_bits_long(s, 32);
  323. #endif
  324. }
  325. }
  326. /**
  327. * Read 0-32 bits as a signed integer.
  328. */
  329. static inline int get_sbits_long(GetBitContext *s, int n)
  330. {
  331. // sign_extend(x, 0) is undefined
  332. if (!n)
  333. return 0;
  334. return sign_extend(get_bits_long(s, n), n);
  335. }
  336. /**
  337. * Show 0-32 bits.
  338. */
  339. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  340. {
  341. if (n <= MIN_CACHE_BITS) {
  342. return show_bits(s, n);
  343. } else {
  344. GetBitContext gb = *s;
  345. return get_bits_long(&gb, n);
  346. }
  347. }
  348. static inline int check_marker(GetBitContext *s, const char *msg)
  349. {
  350. int bit = get_bits1(s);
  351. if (!bit)
  352. av_log(NULL, AV_LOG_INFO, "Marker bit missing at %d of %d %s\n", get_bits_count(s) - 1, s->size_in_bits, msg);
  353. return bit;
  354. }
  355. /**
  356. * Initialize GetBitContext.
  357. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  358. * larger than the actual read bits because some optimized bitstream
  359. * readers read 32 or 64 bit at once and could read over the end
  360. * @param bit_size the size of the buffer in bits
  361. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  362. */
  363. static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
  364. int bit_size)
  365. {
  366. int buffer_size;
  367. int ret = 0;
  368. if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) {
  369. bit_size = 0;
  370. buffer = NULL;
  371. ret = AVERROR_INVALIDDATA;
  372. }
  373. buffer_size = (bit_size + 7) >> 3;
  374. s->buffer = buffer;
  375. s->size_in_bits = bit_size;
  376. s->size_in_bits_plus8 = bit_size + 8;
  377. s->buffer_end = buffer + buffer_size;
  378. s->index = 0;
  379. return ret;
  380. }
  381. /**
  382. * Initialize GetBitContext.
  383. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  384. * larger than the actual read bits because some optimized bitstream
  385. * readers read 32 or 64 bit at once and could read over the end
  386. * @param byte_size the size of the buffer in bytes
  387. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  388. */
  389. static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
  390. int byte_size)
  391. {
  392. if (byte_size > INT_MAX / 8 || byte_size < 0)
  393. byte_size = -1;
  394. return init_get_bits(s, buffer, byte_size * 8);
  395. }
  396. static inline const uint8_t *align_get_bits(GetBitContext *s)
  397. {
  398. int n = -get_bits_count(s) & 7;
  399. if (n)
  400. skip_bits(s, n);
  401. return s->buffer + (s->index >> 3);
  402. }
  403. #define init_vlc(vlc, nb_bits, nb_codes, \
  404. bits, bits_wrap, bits_size, \
  405. codes, codes_wrap, codes_size, \
  406. flags) \
  407. ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
  408. bits, bits_wrap, bits_size, \
  409. codes, codes_wrap, codes_size, \
  410. NULL, 0, 0, flags)
  411. int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  412. const void *bits, int bits_wrap, int bits_size,
  413. const void *codes, int codes_wrap, int codes_size,
  414. const void *symbols, int symbols_wrap, int symbols_size,
  415. int flags);
  416. void ff_free_vlc(VLC *vlc);
  417. #define INIT_VLC_LE 2
  418. #define INIT_VLC_USE_NEW_STATIC 4
  419. #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
  420. do { \
  421. static VLC_TYPE table[static_size][2]; \
  422. (vlc)->table = table; \
  423. (vlc)->table_allocated = static_size; \
  424. init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
  425. } while (0)
  426. /**
  427. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  428. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  429. * is undefined.
  430. */
  431. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  432. do { \
  433. int n, nb_bits; \
  434. unsigned int index; \
  435. \
  436. index = SHOW_UBITS(name, gb, bits); \
  437. code = table[index][0]; \
  438. n = table[index][1]; \
  439. \
  440. if (max_depth > 1 && n < 0) { \
  441. LAST_SKIP_BITS(name, gb, bits); \
  442. UPDATE_CACHE(name, gb); \
  443. \
  444. nb_bits = -n; \
  445. \
  446. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  447. code = table[index][0]; \
  448. n = table[index][1]; \
  449. if (max_depth > 2 && n < 0) { \
  450. LAST_SKIP_BITS(name, gb, nb_bits); \
  451. UPDATE_CACHE(name, gb); \
  452. \
  453. nb_bits = -n; \
  454. \
  455. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  456. code = table[index][0]; \
  457. n = table[index][1]; \
  458. } \
  459. } \
  460. SKIP_BITS(name, gb, n); \
  461. } while (0)
  462. #define GET_RL_VLC_INTERNAL(level, run, name, gb, table, bits, \
  463. max_depth, need_update) \
  464. do { \
  465. int n, nb_bits; \
  466. unsigned int index; \
  467. \
  468. index = SHOW_UBITS(name, gb, bits); \
  469. level = table[index].level; \
  470. n = table[index].len; \
  471. \
  472. if (max_depth > 1 && n < 0) { \
  473. SKIP_BITS(name, gb, bits); \
  474. if (need_update) { \
  475. UPDATE_CACHE(name, gb); \
  476. } \
  477. \
  478. nb_bits = -n; \
  479. \
  480. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  481. level = table[index].level; \
  482. n = table[index].len; \
  483. if (max_depth > 2 && n < 0) { \
  484. LAST_SKIP_BITS(name, gb, nb_bits); \
  485. if (need_update) { \
  486. UPDATE_CACHE(name, gb); \
  487. } \
  488. nb_bits = -n; \
  489. \
  490. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  491. level = table[index].level; \
  492. n = table[index].len; \
  493. } \
  494. } \
  495. run = table[index].run; \
  496. SKIP_BITS(name, gb, n); \
  497. } while (0)
  498. /**
  499. * Parse a vlc code.
  500. * @param bits is the number of bits which will be read at once, must be
  501. * identical to nb_bits in init_vlc()
  502. * @param max_depth is the number of times bits bits must be read to completely
  503. * read the longest vlc code
  504. * = (max_vlc_length + bits - 1) / bits
  505. * @returns the code parsed or -1 if no vlc matches
  506. */
  507. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  508. int bits, int max_depth)
  509. {
  510. int code;
  511. OPEN_READER(re, s);
  512. UPDATE_CACHE(re, s);
  513. GET_VLC(code, re, s, table, bits, max_depth);
  514. CLOSE_READER(re, s);
  515. return code;
  516. }
  517. static inline int decode012(GetBitContext *gb)
  518. {
  519. int n;
  520. n = get_bits1(gb);
  521. if (n == 0)
  522. return 0;
  523. else
  524. return get_bits1(gb) + 1;
  525. }
  526. static inline int decode210(GetBitContext *gb)
  527. {
  528. if (get_bits1(gb))
  529. return 0;
  530. else
  531. return 2 - get_bits1(gb);
  532. }
  533. static inline int get_bits_left(GetBitContext *gb)
  534. {
  535. return gb->size_in_bits - get_bits_count(gb);
  536. }
  537. static inline int skip_1stop_8data_bits(GetBitContext *gb)
  538. {
  539. if (get_bits_left(gb) <= 0)
  540. return AVERROR_INVALIDDATA;
  541. while (get_bits1(gb)) {
  542. skip_bits(gb, 8);
  543. if (get_bits_left(gb) <= 0)
  544. return AVERROR_INVALIDDATA;
  545. }
  546. return 0;
  547. }
  548. //#define TRACE
  549. #ifdef TRACE
  550. static inline void print_bin(int bits, int n)
  551. {
  552. int i;
  553. for (i = n - 1; i >= 0; i--)
  554. av_log(NULL, AV_LOG_DEBUG, "%d", (bits >> i) & 1);
  555. for (i = n; i < 24; i++)
  556. av_log(NULL, AV_LOG_DEBUG, " ");
  557. }
  558. static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
  559. const char *func, int line)
  560. {
  561. int r = get_bits(s, n);
  562. print_bin(r, n);
  563. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
  564. r, n, r, get_bits_count(s) - n, file, func, line);
  565. return r;
  566. }
  567. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
  568. int bits, int max_depth, const char *file,
  569. const char *func, int line)
  570. {
  571. int show = show_bits(s, 24);
  572. int pos = get_bits_count(s);
  573. int r = get_vlc2(s, table, bits, max_depth);
  574. int len = get_bits_count(s) - pos;
  575. int bits2 = show >> (24 - len);
  576. print_bin(bits2, len);
  577. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
  578. bits2, len, r, pos, file, func, line);
  579. return r;
  580. }
  581. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  582. max_depth, need_update) \
  583. do { \
  584. int show = SHOW_UBITS(name, gb, 24); \
  585. int len; \
  586. int pos = name ## _index; \
  587. \
  588. GET_RL_VLC_INTERNAL(level, run, name, gb, table, bits,max_depth, need_update); \
  589. \
  590. len = name ## _index - pos + 1; \
  591. show = show >> (24 - len); \
  592. \
  593. print_bin(show, len); \
  594. \
  595. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d/%-3d rlv @%5d in %s %s:%d\n",\
  596. show, len, run-1, level, pos, __FILE__, __PRETTY_FUNCTION__, __LINE__);\
  597. } while (0) \
  598. static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
  599. const char *func, int line)
  600. {
  601. int show = show_bits(s, n);
  602. int r = get_xbits(s, n);
  603. print_bin(show, n);
  604. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
  605. show, n, r, get_bits_count(s) - n, file, func, line);
  606. return r;
  607. }
  608. #define get_bits(s, n) get_bits_trace(s , n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  609. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  610. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  611. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  612. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  613. #else //TRACE
  614. #define GET_RL_VLC GET_RL_VLC_INTERNAL
  615. #endif
  616. #endif /* AVCODEC_GET_BITS_H */