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.

692 lines
19KB

  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 <stdlib.h>
  28. #include <assert.h>
  29. #include "libavutil/bswap.h"
  30. #include "libavutil/common.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/log.h"
  33. #include "mathops.h"
  34. #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
  35. # define ALT_BITSTREAM_READER
  36. #endif
  37. #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
  38. # if ARCH_ARM && !HAVE_FAST_UNALIGNED
  39. # define A32_BITSTREAM_READER
  40. # else
  41. # define ALT_BITSTREAM_READER
  42. //#define LIBMPEG2_BITSTREAM_READER
  43. //#define A32_BITSTREAM_READER
  44. # endif
  45. #endif
  46. /* bit input */
  47. /* buffer, buffer_end and size_in_bits must be present and used by every reader */
  48. typedef struct GetBitContext {
  49. const uint8_t *buffer, *buffer_end;
  50. #ifdef ALT_BITSTREAM_READER
  51. int index;
  52. #elif defined LIBMPEG2_BITSTREAM_READER
  53. uint8_t *buffer_ptr;
  54. uint32_t cache;
  55. int bit_count;
  56. #elif defined A32_BITSTREAM_READER
  57. uint32_t *buffer_ptr;
  58. uint32_t cache0;
  59. uint32_t cache1;
  60. int bit_count;
  61. #endif
  62. int size_in_bits;
  63. } GetBitContext;
  64. #define VLC_TYPE int16_t
  65. typedef struct VLC {
  66. int bits;
  67. VLC_TYPE (*table)[2]; ///< code, bits
  68. int table_size, table_allocated;
  69. } VLC;
  70. typedef struct RL_VLC_ELEM {
  71. int16_t level;
  72. int8_t len;
  73. uint8_t run;
  74. } RL_VLC_ELEM;
  75. /* Bitstream reader API docs:
  76. name
  77. arbitrary name which is used as prefix for the internal variables
  78. gb
  79. getbitcontext
  80. OPEN_READER(name, gb)
  81. loads gb into local variables
  82. CLOSE_READER(name, gb)
  83. stores local vars in gb
  84. UPDATE_CACHE(name, gb)
  85. refills the internal cache from the bitstream
  86. after this call at least MIN_CACHE_BITS will be available,
  87. GET_CACHE(name, gb)
  88. will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
  89. SHOW_UBITS(name, gb, num)
  90. will return the next num bits
  91. SHOW_SBITS(name, gb, num)
  92. will return the next num bits and do sign extension
  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. SKIP_CACHE(name, gb, num)
  97. will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
  98. SKIP_COUNTER(name, gb, num)
  99. will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
  100. LAST_SKIP_CACHE(name, gb, num)
  101. will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
  102. LAST_SKIP_BITS(name, gb, num)
  103. is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
  104. for examples see get_bits, show_bits, skip_bits, get_vlc
  105. */
  106. #ifdef ALT_BITSTREAM_READER
  107. # define MIN_CACHE_BITS 25
  108. # define OPEN_READER(name, gb)\
  109. unsigned int name##_index= (gb)->index;\
  110. int name##_cache= 0;\
  111. # define CLOSE_READER(name, gb)\
  112. (gb)->index= name##_index;\
  113. # ifdef ALT_BITSTREAM_READER_LE
  114. # define UPDATE_CACHE(name, gb)\
  115. name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
  116. # define SKIP_CACHE(name, gb, num)\
  117. name##_cache >>= (num);
  118. # else
  119. # define UPDATE_CACHE(name, gb)\
  120. name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
  121. # define SKIP_CACHE(name, gb, num)\
  122. name##_cache <<= (num);
  123. # endif
  124. // FIXME name?
  125. # define SKIP_COUNTER(name, gb, num)\
  126. name##_index += (num);\
  127. # define SKIP_BITS(name, gb, num)\
  128. {\
  129. SKIP_CACHE(name, gb, num)\
  130. SKIP_COUNTER(name, gb, num)\
  131. }\
  132. # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  133. # define LAST_SKIP_CACHE(name, gb, num) ;
  134. # ifdef ALT_BITSTREAM_READER_LE
  135. # define SHOW_UBITS(name, gb, num)\
  136. zero_extend(name##_cache, num)
  137. # define SHOW_SBITS(name, gb, num)\
  138. sign_extend(name##_cache, num)
  139. # else
  140. # define SHOW_UBITS(name, gb, num)\
  141. NEG_USR32(name##_cache, num)
  142. # define SHOW_SBITS(name, gb, num)\
  143. NEG_SSR32(name##_cache, num)
  144. # endif
  145. # define GET_CACHE(name, gb)\
  146. ((uint32_t)name##_cache)
  147. static inline int get_bits_count(const GetBitContext *s){
  148. return s->index;
  149. }
  150. static inline void skip_bits_long(GetBitContext *s, int n){
  151. s->index += n;
  152. }
  153. #elif defined LIBMPEG2_BITSTREAM_READER
  154. //libmpeg2 like reader
  155. # define MIN_CACHE_BITS 17
  156. # define OPEN_READER(name, gb)\
  157. int name##_bit_count=(gb)->bit_count;\
  158. int name##_cache= (gb)->cache;\
  159. uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  160. # define CLOSE_READER(name, gb)\
  161. (gb)->bit_count= name##_bit_count;\
  162. (gb)->cache= name##_cache;\
  163. (gb)->buffer_ptr= name##_buffer_ptr;\
  164. # define UPDATE_CACHE(name, gb)\
  165. if(name##_bit_count >= 0){\
  166. name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
  167. name##_buffer_ptr+=2;\
  168. name##_bit_count-= 16;\
  169. }\
  170. # define SKIP_CACHE(name, gb, num)\
  171. name##_cache <<= (num);\
  172. # define SKIP_COUNTER(name, gb, num)\
  173. name##_bit_count += (num);\
  174. # define SKIP_BITS(name, gb, num)\
  175. {\
  176. SKIP_CACHE(name, gb, num)\
  177. SKIP_COUNTER(name, gb, num)\
  178. }\
  179. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  180. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  181. # define SHOW_UBITS(name, gb, num)\
  182. NEG_USR32(name##_cache, num)
  183. # define SHOW_SBITS(name, gb, num)\
  184. NEG_SSR32(name##_cache, num)
  185. # define GET_CACHE(name, gb)\
  186. ((uint32_t)name##_cache)
  187. static inline int get_bits_count(const GetBitContext *s){
  188. return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
  189. }
  190. static inline void skip_bits_long(GetBitContext *s, int n){
  191. OPEN_READER(re, s)
  192. re_bit_count += n;
  193. re_buffer_ptr += 2*(re_bit_count>>4);
  194. re_bit_count &= 15;
  195. re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
  196. UPDATE_CACHE(re, s)
  197. CLOSE_READER(re, s)
  198. }
  199. #elif defined A32_BITSTREAM_READER
  200. # define MIN_CACHE_BITS 32
  201. # define OPEN_READER(name, gb)\
  202. int name##_bit_count=(gb)->bit_count;\
  203. uint32_t name##_cache0= (gb)->cache0;\
  204. uint32_t name##_cache1= (gb)->cache1;\
  205. uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  206. # define CLOSE_READER(name, gb)\
  207. (gb)->bit_count= name##_bit_count;\
  208. (gb)->cache0= name##_cache0;\
  209. (gb)->cache1= name##_cache1;\
  210. (gb)->buffer_ptr= name##_buffer_ptr;\
  211. # define UPDATE_CACHE(name, gb)\
  212. if(name##_bit_count > 0){\
  213. const uint32_t next= av_be2ne32( *name##_buffer_ptr );\
  214. name##_cache0 |= NEG_USR32(next,name##_bit_count);\
  215. name##_cache1 |= next<<name##_bit_count;\
  216. name##_buffer_ptr++;\
  217. name##_bit_count-= 32;\
  218. }\
  219. #if ARCH_X86
  220. # define SKIP_CACHE(name, gb, num)\
  221. __asm__(\
  222. "shldl %2, %1, %0 \n\t"\
  223. "shll %2, %1 \n\t"\
  224. : "+r" (name##_cache0), "+r" (name##_cache1)\
  225. : "Ic" ((uint8_t)(num))\
  226. );
  227. #else
  228. # define SKIP_CACHE(name, gb, num)\
  229. name##_cache0 <<= (num);\
  230. name##_cache0 |= NEG_USR32(name##_cache1,num);\
  231. name##_cache1 <<= (num);
  232. #endif
  233. # define SKIP_COUNTER(name, gb, num)\
  234. name##_bit_count += (num);\
  235. # define SKIP_BITS(name, gb, num)\
  236. {\
  237. SKIP_CACHE(name, gb, num)\
  238. SKIP_COUNTER(name, gb, num)\
  239. }\
  240. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  241. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  242. # define SHOW_UBITS(name, gb, num)\
  243. NEG_USR32(name##_cache0, num)
  244. # define SHOW_SBITS(name, gb, num)\
  245. NEG_SSR32(name##_cache0, num)
  246. # define GET_CACHE(name, gb)\
  247. (name##_cache0)
  248. static inline int get_bits_count(const GetBitContext *s){
  249. return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
  250. }
  251. static inline void skip_bits_long(GetBitContext *s, int n){
  252. OPEN_READER(re, s)
  253. re_bit_count += n;
  254. re_buffer_ptr += re_bit_count>>5;
  255. re_bit_count &= 31;
  256. re_cache0 = av_be2ne32( re_buffer_ptr[-1] ) << re_bit_count;
  257. re_cache1 = 0;
  258. UPDATE_CACHE(re, s)
  259. CLOSE_READER(re, s)
  260. }
  261. #endif
  262. /**
  263. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  264. * if MSB not set it is negative
  265. * @param n length in bits
  266. * @author BERO
  267. */
  268. static inline int get_xbits(GetBitContext *s, int n){
  269. register int sign;
  270. register int32_t cache;
  271. OPEN_READER(re, s)
  272. UPDATE_CACHE(re, s)
  273. cache = GET_CACHE(re,s);
  274. sign=(~cache)>>31;
  275. LAST_SKIP_BITS(re, s, n)
  276. CLOSE_READER(re, s)
  277. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  278. }
  279. static inline int get_sbits(GetBitContext *s, int n){
  280. register int tmp;
  281. OPEN_READER(re, s)
  282. UPDATE_CACHE(re, s)
  283. tmp= SHOW_SBITS(re, s, n);
  284. LAST_SKIP_BITS(re, s, n)
  285. CLOSE_READER(re, s)
  286. return tmp;
  287. }
  288. /**
  289. * reads 1-17 bits.
  290. * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
  291. */
  292. static inline unsigned int get_bits(GetBitContext *s, int n){
  293. register int tmp;
  294. OPEN_READER(re, s)
  295. UPDATE_CACHE(re, s)
  296. tmp= SHOW_UBITS(re, s, n);
  297. LAST_SKIP_BITS(re, s, n)
  298. CLOSE_READER(re, s)
  299. return tmp;
  300. }
  301. /**
  302. * shows 1-17 bits.
  303. * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
  304. */
  305. static inline unsigned int show_bits(GetBitContext *s, int n){
  306. register int tmp;
  307. OPEN_READER(re, s)
  308. UPDATE_CACHE(re, s)
  309. tmp= SHOW_UBITS(re, s, n);
  310. // CLOSE_READER(re, s)
  311. return tmp;
  312. }
  313. static inline void skip_bits(GetBitContext *s, int n){
  314. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  315. OPEN_READER(re, s)
  316. UPDATE_CACHE(re, s)
  317. LAST_SKIP_BITS(re, s, n)
  318. CLOSE_READER(re, s)
  319. }
  320. static inline unsigned int get_bits1(GetBitContext *s){
  321. #ifdef ALT_BITSTREAM_READER
  322. unsigned int index= s->index;
  323. uint8_t result= s->buffer[ index>>3 ];
  324. #ifdef ALT_BITSTREAM_READER_LE
  325. result>>= (index&0x07);
  326. result&= 1;
  327. #else
  328. result<<= (index&0x07);
  329. result>>= 8 - 1;
  330. #endif
  331. index++;
  332. s->index= index;
  333. return result;
  334. #else
  335. return get_bits(s, 1);
  336. #endif
  337. }
  338. static inline unsigned int show_bits1(GetBitContext *s){
  339. return show_bits(s, 1);
  340. }
  341. static inline void skip_bits1(GetBitContext *s){
  342. skip_bits(s, 1);
  343. }
  344. /**
  345. * reads 0-32 bits.
  346. */
  347. static inline unsigned int get_bits_long(GetBitContext *s, int n){
  348. if(n<=MIN_CACHE_BITS) return get_bits(s, n);
  349. else{
  350. #ifdef ALT_BITSTREAM_READER_LE
  351. int ret= get_bits(s, 16);
  352. return ret | (get_bits(s, n-16) << 16);
  353. #else
  354. int ret= get_bits(s, 16) << (n-16);
  355. return ret | get_bits(s, n-16);
  356. #endif
  357. }
  358. }
  359. /**
  360. * reads 0-32 bits as a signed integer.
  361. */
  362. static inline int get_sbits_long(GetBitContext *s, int n) {
  363. return sign_extend(get_bits_long(s, n), n);
  364. }
  365. /**
  366. * shows 0-32 bits.
  367. */
  368. static inline unsigned int show_bits_long(GetBitContext *s, int n){
  369. if(n<=MIN_CACHE_BITS) return show_bits(s, n);
  370. else{
  371. GetBitContext gb= *s;
  372. return get_bits_long(&gb, n);
  373. }
  374. }
  375. static inline int check_marker(GetBitContext *s, const char *msg)
  376. {
  377. int bit= get_bits1(s);
  378. if(!bit)
  379. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  380. return bit;
  381. }
  382. /**
  383. * init GetBitContext.
  384. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
  385. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  386. * @param bit_size the size of the buffer in bits
  387. *
  388. * While GetBitContext stores the buffer size, for performance reasons you are
  389. * responsible for checking for the buffer end yourself (take advantage of the padding)!
  390. */
  391. static inline void init_get_bits(GetBitContext *s,
  392. const uint8_t *buffer, int bit_size)
  393. {
  394. int buffer_size= (bit_size+7)>>3;
  395. if(buffer_size < 0 || bit_size < 0) {
  396. buffer_size = bit_size = 0;
  397. buffer = NULL;
  398. }
  399. s->buffer= buffer;
  400. s->size_in_bits= bit_size;
  401. s->buffer_end= buffer + buffer_size;
  402. #ifdef ALT_BITSTREAM_READER
  403. s->index=0;
  404. #elif defined LIBMPEG2_BITSTREAM_READER
  405. s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
  406. s->bit_count = 16 + 8*((intptr_t)buffer&1);
  407. skip_bits_long(s, 0);
  408. #elif defined A32_BITSTREAM_READER
  409. s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
  410. s->bit_count = 32 + 8*((intptr_t)buffer&3);
  411. skip_bits_long(s, 0);
  412. #endif
  413. }
  414. static inline void align_get_bits(GetBitContext *s)
  415. {
  416. int n= (-get_bits_count(s)) & 7;
  417. if(n) skip_bits(s, n);
  418. }
  419. #define init_vlc(vlc, nb_bits, nb_codes,\
  420. bits, bits_wrap, bits_size,\
  421. codes, codes_wrap, codes_size,\
  422. flags)\
  423. init_vlc_sparse(vlc, nb_bits, nb_codes,\
  424. bits, bits_wrap, bits_size,\
  425. codes, codes_wrap, codes_size,\
  426. NULL, 0, 0, flags)
  427. int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  428. const void *bits, int bits_wrap, int bits_size,
  429. const void *codes, int codes_wrap, int codes_size,
  430. const void *symbols, int symbols_wrap, int symbols_size,
  431. int flags);
  432. #define INIT_VLC_LE 2
  433. #define INIT_VLC_USE_NEW_STATIC 4
  434. void free_vlc(VLC *vlc);
  435. #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
  436. {\
  437. static VLC_TYPE table[static_size][2];\
  438. (vlc)->table= table;\
  439. (vlc)->table_allocated= static_size;\
  440. init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
  441. }
  442. /**
  443. *
  444. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  445. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  446. * is undefined.
  447. */
  448. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  449. {\
  450. int n, nb_bits;\
  451. unsigned int index;\
  452. \
  453. index= SHOW_UBITS(name, gb, bits);\
  454. code = table[index][0];\
  455. n = table[index][1];\
  456. \
  457. if(max_depth > 1 && n < 0){\
  458. LAST_SKIP_BITS(name, gb, bits)\
  459. UPDATE_CACHE(name, gb)\
  460. \
  461. nb_bits = -n;\
  462. \
  463. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  464. code = table[index][0];\
  465. n = table[index][1];\
  466. if(max_depth > 2 && n < 0){\
  467. LAST_SKIP_BITS(name, gb, nb_bits)\
  468. UPDATE_CACHE(name, gb)\
  469. \
  470. nb_bits = -n;\
  471. \
  472. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  473. code = table[index][0];\
  474. n = table[index][1];\
  475. }\
  476. }\
  477. SKIP_BITS(name, gb, n)\
  478. }
  479. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
  480. {\
  481. int n, nb_bits;\
  482. unsigned int index;\
  483. \
  484. index= SHOW_UBITS(name, gb, bits);\
  485. level = table[index].level;\
  486. n = table[index].len;\
  487. \
  488. if(max_depth > 1 && n < 0){\
  489. SKIP_BITS(name, gb, bits)\
  490. if(need_update){\
  491. UPDATE_CACHE(name, gb)\
  492. }\
  493. \
  494. nb_bits = -n;\
  495. \
  496. index= SHOW_UBITS(name, gb, nb_bits) + level;\
  497. level = table[index].level;\
  498. n = table[index].len;\
  499. }\
  500. run= table[index].run;\
  501. SKIP_BITS(name, gb, n)\
  502. }
  503. /**
  504. * parses a vlc code, faster then get_vlc()
  505. * @param bits is the number of bits which will be read at once, must be
  506. * identical to nb_bits in init_vlc()
  507. * @param max_depth is the number of times bits bits must be read to completely
  508. * read the longest vlc code
  509. * = (max_vlc_length + bits - 1) / bits
  510. */
  511. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  512. int bits, int max_depth)
  513. {
  514. int code;
  515. OPEN_READER(re, s)
  516. UPDATE_CACHE(re, s)
  517. GET_VLC(code, re, s, table, bits, max_depth)
  518. CLOSE_READER(re, s)
  519. return code;
  520. }
  521. //#define TRACE
  522. #ifdef TRACE
  523. static inline void print_bin(int bits, int n){
  524. int i;
  525. for(i=n-1; i>=0; i--){
  526. av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
  527. }
  528. for(i=n; i<24; i++)
  529. av_log(NULL, AV_LOG_DEBUG, " ");
  530. }
  531. static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  532. int r= get_bits(s, n);
  533. print_bin(r, n);
  534. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
  535. return r;
  536. }
  537. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
  538. int show= show_bits(s, 24);
  539. int pos= get_bits_count(s);
  540. int r= get_vlc2(s, table, bits, max_depth);
  541. int len= get_bits_count(s) - pos;
  542. int bits2= show>>(24-len);
  543. print_bin(bits2, len);
  544. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
  545. return r;
  546. }
  547. static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  548. int show= show_bits(s, n);
  549. int r= get_xbits(s, n);
  550. print_bin(show, n);
  551. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
  552. return r;
  553. }
  554. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  555. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  556. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  557. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  558. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  559. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  560. #else //TRACE
  561. #define tprintf(p, ...) {}
  562. #endif
  563. static inline int decode012(GetBitContext *gb){
  564. int n;
  565. n = get_bits1(gb);
  566. if (n == 0)
  567. return 0;
  568. else
  569. return get_bits1(gb) + 1;
  570. }
  571. static inline int decode210(GetBitContext *gb){
  572. if (get_bits1(gb))
  573. return 0;
  574. else
  575. return 2 - get_bits1(gb);
  576. }
  577. static inline int get_bits_left(GetBitContext *gb)
  578. {
  579. return gb->size_in_bits - get_bits_count(gb);
  580. }
  581. #endif /* AVCODEC_GET_BITS_H */