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.

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