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.

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