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.

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