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.

828 lines
21KB

  1. /**
  2. * @file bitstream.h
  3. * bitstream api header.
  4. */
  5. #ifndef BITSTREAM_H
  6. #define BITSTREAM_H
  7. //#define ALT_BITSTREAM_WRITER
  8. //#define ALIGNED_BITSTREAM_WRITER
  9. #define ALT_BITSTREAM_READER
  10. //#define LIBMPEG2_BITSTREAM_READER
  11. //#define A32_BITSTREAM_READER
  12. #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
  13. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  14. // avoid +32 for shift optimization (gcc should do that ...)
  15. static inline int32_t NEG_SSR32( int32_t a, int8_t s){
  16. asm ("sarl %1, %0\n\t"
  17. : "+r" (a)
  18. : "ic" ((uint8_t)(-s))
  19. );
  20. return a;
  21. }
  22. static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
  23. asm ("shrl %1, %0\n\t"
  24. : "+r" (a)
  25. : "ic" ((uint8_t)(-s))
  26. );
  27. return a;
  28. }
  29. #else
  30. # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
  31. # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
  32. #endif
  33. /* bit output */
  34. /* buf and buf_end must be present and used by every alternative writer. */
  35. typedef struct PutBitContext {
  36. #ifdef ALT_BITSTREAM_WRITER
  37. uint8_t *buf, *buf_end;
  38. int index;
  39. #else
  40. uint32_t bit_buf;
  41. int bit_left;
  42. uint8_t *buf, *buf_ptr, *buf_end;
  43. #endif
  44. } PutBitContext;
  45. static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
  46. {
  47. s->buf = buffer;
  48. s->buf_end = s->buf + buffer_size;
  49. #ifdef ALT_BITSTREAM_WRITER
  50. s->index=0;
  51. ((uint32_t*)(s->buf))[0]=0;
  52. // memset(buffer, 0, buffer_size);
  53. #else
  54. s->buf_ptr = s->buf;
  55. s->bit_left=32;
  56. s->bit_buf=0;
  57. #endif
  58. }
  59. /* return the number of bits output */
  60. static inline int put_bits_count(PutBitContext *s)
  61. {
  62. #ifdef ALT_BITSTREAM_WRITER
  63. return s->index;
  64. #else
  65. return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
  66. #endif
  67. }
  68. /* pad the end of the output stream with zeros */
  69. static inline void flush_put_bits(PutBitContext *s)
  70. {
  71. #ifdef ALT_BITSTREAM_WRITER
  72. align_put_bits(s);
  73. #else
  74. s->bit_buf<<= s->bit_left;
  75. while (s->bit_left < 32) {
  76. /* XXX: should test end of buffer */
  77. *s->buf_ptr++=s->bit_buf >> 24;
  78. s->bit_buf<<=8;
  79. s->bit_left+=8;
  80. }
  81. s->bit_left=32;
  82. s->bit_buf=0;
  83. #endif
  84. }
  85. void align_put_bits(PutBitContext *s);
  86. void put_string(PutBitContext * pbc, char *s, int put_zero);
  87. /* bit input */
  88. /* buffer, buffer_end and size_in_bits must be present and used by every reader */
  89. typedef struct GetBitContext {
  90. const uint8_t *buffer, *buffer_end;
  91. #ifdef ALT_BITSTREAM_READER
  92. int index;
  93. #elif defined LIBMPEG2_BITSTREAM_READER
  94. uint8_t *buffer_ptr;
  95. uint32_t cache;
  96. int bit_count;
  97. #elif defined A32_BITSTREAM_READER
  98. uint32_t *buffer_ptr;
  99. uint32_t cache0;
  100. uint32_t cache1;
  101. int bit_count;
  102. #endif
  103. int size_in_bits;
  104. } GetBitContext;
  105. #define VLC_TYPE int16_t
  106. typedef struct VLC {
  107. int bits;
  108. VLC_TYPE (*table)[2]; ///< code, bits
  109. int table_size, table_allocated;
  110. } VLC;
  111. typedef struct RL_VLC_ELEM {
  112. int16_t level;
  113. int8_t len;
  114. uint8_t run;
  115. } RL_VLC_ELEM;
  116. #ifdef ARCH_SPARC
  117. #define UNALIGNED_STORES_ARE_BAD
  118. #endif
  119. /* used to avoid missaligned exceptions on some archs (alpha, ...) */
  120. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  121. # define unaligned32(a) (*(const uint32_t*)(a))
  122. #else
  123. # ifdef __GNUC__
  124. static inline uint32_t unaligned32(const void *v) {
  125. struct Unaligned {
  126. uint32_t i;
  127. } __attribute__((packed));
  128. return ((const struct Unaligned *) v)->i;
  129. }
  130. # elif defined(__DECC)
  131. static inline uint32_t unaligned32(const void *v) {
  132. return *(const __unaligned uint32_t *) v;
  133. }
  134. # else
  135. static inline uint32_t unaligned32(const void *v) {
  136. return *(const uint32_t *) v;
  137. }
  138. # endif
  139. #endif //!ARCH_X86
  140. #ifndef ALT_BITSTREAM_WRITER
  141. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  142. {
  143. unsigned int bit_buf;
  144. int bit_left;
  145. #ifdef STATS
  146. st_out_bit_counts[st_current_index] += n;
  147. #endif
  148. // printf("put_bits=%d %x\n", n, value);
  149. assert(n == 32 || value < (1U << n));
  150. bit_buf = s->bit_buf;
  151. bit_left = s->bit_left;
  152. // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  153. /* XXX: optimize */
  154. if (n < bit_left) {
  155. bit_buf = (bit_buf<<n) | value;
  156. bit_left-=n;
  157. } else {
  158. bit_buf<<=bit_left;
  159. bit_buf |= value >> (n - bit_left);
  160. #ifdef UNALIGNED_STORES_ARE_BAD
  161. if (3 & (intptr_t) s->buf_ptr) {
  162. s->buf_ptr[0] = bit_buf >> 24;
  163. s->buf_ptr[1] = bit_buf >> 16;
  164. s->buf_ptr[2] = bit_buf >> 8;
  165. s->buf_ptr[3] = bit_buf ;
  166. } else
  167. #endif
  168. *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
  169. //printf("bitbuf = %08x\n", bit_buf);
  170. s->buf_ptr+=4;
  171. bit_left+=32 - n;
  172. bit_buf = value;
  173. }
  174. s->bit_buf = bit_buf;
  175. s->bit_left = bit_left;
  176. }
  177. #endif
  178. #ifdef ALT_BITSTREAM_WRITER
  179. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  180. {
  181. # ifdef ALIGNED_BITSTREAM_WRITER
  182. # if defined(ARCH_X86) || defined(ARCH_X86_64)
  183. asm volatile(
  184. "movl %0, %%ecx \n\t"
  185. "xorl %%eax, %%eax \n\t"
  186. "shrdl %%cl, %1, %%eax \n\t"
  187. "shrl %%cl, %1 \n\t"
  188. "movl %0, %%ecx \n\t"
  189. "shrl $3, %%ecx \n\t"
  190. "andl $0xFFFFFFFC, %%ecx \n\t"
  191. "bswapl %1 \n\t"
  192. "orl %1, (%2, %%ecx) \n\t"
  193. "bswapl %%eax \n\t"
  194. "addl %3, %0 \n\t"
  195. "movl %%eax, 4(%2, %%ecx) \n\t"
  196. : "=&r" (s->index), "=&r" (value)
  197. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
  198. : "%eax", "%ecx"
  199. );
  200. # else
  201. int index= s->index;
  202. uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
  203. value<<= 32-n;
  204. ptr[0] |= be2me_32(value>>(index&31));
  205. ptr[1] = be2me_32(value<<(32-(index&31)));
  206. //if(n>24) printf("%d %d\n", n, value);
  207. index+= n;
  208. s->index= index;
  209. # endif
  210. # else //ALIGNED_BITSTREAM_WRITER
  211. # if defined(ARCH_X86) || defined(ARCH_X86_64)
  212. asm volatile(
  213. "movl $7, %%ecx \n\t"
  214. "andl %0, %%ecx \n\t"
  215. "addl %3, %%ecx \n\t"
  216. "negl %%ecx \n\t"
  217. "shll %%cl, %1 \n\t"
  218. "bswapl %1 \n\t"
  219. "movl %0, %%ecx \n\t"
  220. "shrl $3, %%ecx \n\t"
  221. "orl %1, (%%ecx, %2) \n\t"
  222. "addl %3, %0 \n\t"
  223. "movl $0, 4(%%ecx, %2) \n\t"
  224. : "=&r" (s->index), "=&r" (value)
  225. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
  226. : "%ecx"
  227. );
  228. # else
  229. int index= s->index;
  230. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  231. ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
  232. ptr[1] = 0;
  233. //if(n>24) printf("%d %d\n", n, value);
  234. index+= n;
  235. s->index= index;
  236. # endif
  237. # endif //!ALIGNED_BITSTREAM_WRITER
  238. }
  239. #endif
  240. static inline uint8_t* pbBufPtr(PutBitContext *s)
  241. {
  242. #ifdef ALT_BITSTREAM_WRITER
  243. return s->buf + (s->index>>3);
  244. #else
  245. return s->buf_ptr;
  246. #endif
  247. }
  248. /**
  249. *
  250. * PutBitContext must be flushed & aligned to a byte boundary before calling this.
  251. */
  252. static inline void skip_put_bytes(PutBitContext *s, int n){
  253. assert((put_bits_count(s)&7)==0);
  254. #ifdef ALT_BITSTREAM_WRITER
  255. FIXME may need some cleaning of the buffer
  256. s->index += n<<3;
  257. #else
  258. assert(s->bit_left==32);
  259. s->buf_ptr += n;
  260. #endif
  261. }
  262. /**
  263. * Changes the end of the buffer.
  264. */
  265. static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
  266. s->buf_end= s->buf + size;
  267. }
  268. /* Bitstream reader API docs:
  269. name
  270. abritary name which is used as prefix for the internal variables
  271. gb
  272. getbitcontext
  273. OPEN_READER(name, gb)
  274. loads gb into local variables
  275. CLOSE_READER(name, gb)
  276. stores local vars in gb
  277. UPDATE_CACHE(name, gb)
  278. refills the internal cache from the bitstream
  279. after this call at least MIN_CACHE_BITS will be available,
  280. GET_CACHE(name, gb)
  281. will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
  282. SHOW_UBITS(name, gb, num)
  283. will return the nest num bits
  284. SHOW_SBITS(name, gb, num)
  285. will return the nest num bits and do sign extension
  286. SKIP_BITS(name, gb, num)
  287. will skip over the next num bits
  288. note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
  289. SKIP_CACHE(name, gb, num)
  290. will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
  291. SKIP_COUNTER(name, gb, num)
  292. will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
  293. LAST_SKIP_CACHE(name, gb, num)
  294. will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
  295. LAST_SKIP_BITS(name, gb, num)
  296. is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
  297. for examples see get_bits, show_bits, skip_bits, get_vlc
  298. */
  299. static inline int unaligned32_be(const void *v)
  300. {
  301. #ifdef CONFIG_ALIGN
  302. const uint8_t *p=v;
  303. return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
  304. #else
  305. return be2me_32( unaligned32(v)); //original
  306. #endif
  307. }
  308. #ifdef ALT_BITSTREAM_READER
  309. # define MIN_CACHE_BITS 25
  310. # define OPEN_READER(name, gb)\
  311. int name##_index= (gb)->index;\
  312. int name##_cache= 0;\
  313. # define CLOSE_READER(name, gb)\
  314. (gb)->index= name##_index;\
  315. # define UPDATE_CACHE(name, gb)\
  316. name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
  317. # define SKIP_CACHE(name, gb, num)\
  318. name##_cache <<= (num);\
  319. // FIXME name?
  320. # define SKIP_COUNTER(name, gb, num)\
  321. name##_index += (num);\
  322. # define SKIP_BITS(name, gb, num)\
  323. {\
  324. SKIP_CACHE(name, gb, num)\
  325. SKIP_COUNTER(name, gb, num)\
  326. }\
  327. # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  328. # define LAST_SKIP_CACHE(name, gb, num) ;
  329. # define SHOW_UBITS(name, gb, num)\
  330. NEG_USR32(name##_cache, num)
  331. # define SHOW_SBITS(name, gb, num)\
  332. NEG_SSR32(name##_cache, num)
  333. # define GET_CACHE(name, gb)\
  334. ((uint32_t)name##_cache)
  335. static inline int get_bits_count(GetBitContext *s){
  336. return s->index;
  337. }
  338. #elif defined LIBMPEG2_BITSTREAM_READER
  339. //libmpeg2 like reader
  340. # define MIN_CACHE_BITS 17
  341. # define OPEN_READER(name, gb)\
  342. int name##_bit_count=(gb)->bit_count;\
  343. int name##_cache= (gb)->cache;\
  344. uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  345. # define CLOSE_READER(name, gb)\
  346. (gb)->bit_count= name##_bit_count;\
  347. (gb)->cache= name##_cache;\
  348. (gb)->buffer_ptr= name##_buffer_ptr;\
  349. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  350. # define UPDATE_CACHE(name, gb)\
  351. if(name##_bit_count >= 0){\
  352. name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
  353. ((uint16_t*)name##_buffer_ptr)++;\
  354. name##_bit_count-= 16;\
  355. }\
  356. #else
  357. # define UPDATE_CACHE(name, gb)\
  358. if(name##_bit_count >= 0){\
  359. name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
  360. name##_buffer_ptr+=2;\
  361. name##_bit_count-= 16;\
  362. }\
  363. #endif
  364. # define SKIP_CACHE(name, gb, num)\
  365. name##_cache <<= (num);\
  366. # define SKIP_COUNTER(name, gb, num)\
  367. name##_bit_count += (num);\
  368. # define SKIP_BITS(name, gb, num)\
  369. {\
  370. SKIP_CACHE(name, gb, num)\
  371. SKIP_COUNTER(name, gb, num)\
  372. }\
  373. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  374. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  375. # define SHOW_UBITS(name, gb, num)\
  376. NEG_USR32(name##_cache, num)
  377. # define SHOW_SBITS(name, gb, num)\
  378. NEG_SSR32(name##_cache, num)
  379. # define GET_CACHE(name, gb)\
  380. ((uint32_t)name##_cache)
  381. static inline int get_bits_count(GetBitContext *s){
  382. return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
  383. }
  384. #elif defined A32_BITSTREAM_READER
  385. # define MIN_CACHE_BITS 32
  386. # define OPEN_READER(name, gb)\
  387. int name##_bit_count=(gb)->bit_count;\
  388. uint32_t name##_cache0= (gb)->cache0;\
  389. uint32_t name##_cache1= (gb)->cache1;\
  390. uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  391. # define CLOSE_READER(name, gb)\
  392. (gb)->bit_count= name##_bit_count;\
  393. (gb)->cache0= name##_cache0;\
  394. (gb)->cache1= name##_cache1;\
  395. (gb)->buffer_ptr= name##_buffer_ptr;\
  396. # define UPDATE_CACHE(name, gb)\
  397. if(name##_bit_count > 0){\
  398. const uint32_t next= be2me_32( *name##_buffer_ptr );\
  399. name##_cache0 |= NEG_USR32(next,name##_bit_count);\
  400. name##_cache1 |= next<<name##_bit_count;\
  401. name##_buffer_ptr++;\
  402. name##_bit_count-= 32;\
  403. }\
  404. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  405. # define SKIP_CACHE(name, gb, num)\
  406. asm(\
  407. "shldl %2, %1, %0 \n\t"\
  408. "shll %2, %1 \n\t"\
  409. : "+r" (name##_cache0), "+r" (name##_cache1)\
  410. : "Ic" ((uint8_t)num)\
  411. );
  412. #else
  413. # define SKIP_CACHE(name, gb, num)\
  414. name##_cache0 <<= (num);\
  415. name##_cache0 |= NEG_USR32(name##_cache1,num);\
  416. name##_cache1 <<= (num);
  417. #endif
  418. # define SKIP_COUNTER(name, gb, num)\
  419. name##_bit_count += (num);\
  420. # define SKIP_BITS(name, gb, num)\
  421. {\
  422. SKIP_CACHE(name, gb, num)\
  423. SKIP_COUNTER(name, gb, num)\
  424. }\
  425. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  426. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  427. # define SHOW_UBITS(name, gb, num)\
  428. NEG_USR32(name##_cache0, num)
  429. # define SHOW_SBITS(name, gb, num)\
  430. NEG_SSR32(name##_cache0, num)
  431. # define GET_CACHE(name, gb)\
  432. (name##_cache0)
  433. static inline int get_bits_count(GetBitContext *s){
  434. return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
  435. }
  436. #endif
  437. /**
  438. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  439. * if MSB not set it is negative
  440. * @param n length in bits
  441. * @author BERO
  442. */
  443. static inline int get_xbits(GetBitContext *s, int n){
  444. register int tmp;
  445. register int32_t cache;
  446. OPEN_READER(re, s)
  447. UPDATE_CACHE(re, s)
  448. cache = GET_CACHE(re,s);
  449. if ((int32_t)cache<0) { //MSB=1
  450. tmp = NEG_USR32(cache,n);
  451. } else {
  452. // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
  453. // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
  454. tmp = - NEG_USR32(~cache,n);
  455. }
  456. LAST_SKIP_BITS(re, s, n)
  457. CLOSE_READER(re, s)
  458. return tmp;
  459. }
  460. static inline int get_sbits(GetBitContext *s, int n){
  461. register int tmp;
  462. OPEN_READER(re, s)
  463. UPDATE_CACHE(re, s)
  464. tmp= SHOW_SBITS(re, s, n);
  465. LAST_SKIP_BITS(re, s, n)
  466. CLOSE_READER(re, s)
  467. return tmp;
  468. }
  469. /**
  470. * reads 0-17 bits.
  471. * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
  472. */
  473. static inline unsigned int get_bits(GetBitContext *s, int n){
  474. register int tmp;
  475. OPEN_READER(re, s)
  476. UPDATE_CACHE(re, s)
  477. tmp= SHOW_UBITS(re, s, n);
  478. LAST_SKIP_BITS(re, s, n)
  479. CLOSE_READER(re, s)
  480. return tmp;
  481. }
  482. unsigned int get_bits_long(GetBitContext *s, int n);
  483. /**
  484. * shows 0-17 bits.
  485. * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
  486. */
  487. static inline unsigned int show_bits(GetBitContext *s, int n){
  488. register int tmp;
  489. OPEN_READER(re, s)
  490. UPDATE_CACHE(re, s)
  491. tmp= SHOW_UBITS(re, s, n);
  492. // CLOSE_READER(re, s)
  493. return tmp;
  494. }
  495. unsigned int show_bits_long(GetBitContext *s, int n);
  496. static inline void skip_bits(GetBitContext *s, int n){
  497. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  498. OPEN_READER(re, s)
  499. UPDATE_CACHE(re, s)
  500. LAST_SKIP_BITS(re, s, n)
  501. CLOSE_READER(re, s)
  502. }
  503. static inline unsigned int get_bits1(GetBitContext *s){
  504. #ifdef ALT_BITSTREAM_READER
  505. int index= s->index;
  506. uint8_t result= s->buffer[ index>>3 ];
  507. result<<= (index&0x07);
  508. result>>= 8 - 1;
  509. index++;
  510. s->index= index;
  511. return result;
  512. #else
  513. return get_bits(s, 1);
  514. #endif
  515. }
  516. static inline unsigned int show_bits1(GetBitContext *s){
  517. return show_bits(s, 1);
  518. }
  519. static inline void skip_bits1(GetBitContext *s){
  520. skip_bits(s, 1);
  521. }
  522. /**
  523. * init GetBitContext.
  524. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
  525. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  526. * @param bit_size the size of the buffer in bits
  527. */
  528. static inline void init_get_bits(GetBitContext *s,
  529. const uint8_t *buffer, int bit_size)
  530. {
  531. const int buffer_size= (bit_size+7)>>3;
  532. s->buffer= buffer;
  533. s->size_in_bits= bit_size;
  534. s->buffer_end= buffer + buffer_size;
  535. #ifdef ALT_BITSTREAM_READER
  536. s->index=0;
  537. #elif defined LIBMPEG2_BITSTREAM_READER
  538. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  539. if ((int)buffer&1) {
  540. /* word alignment */
  541. s->cache = (*buffer++)<<24;
  542. s->buffer_ptr = buffer;
  543. s->bit_count = 16-8;
  544. } else
  545. #endif
  546. {
  547. s->buffer_ptr = buffer;
  548. s->bit_count = 16;
  549. s->cache = 0;
  550. }
  551. #elif defined A32_BITSTREAM_READER
  552. s->buffer_ptr = (uint32_t*)buffer;
  553. s->bit_count = 32;
  554. s->cache0 = 0;
  555. s->cache1 = 0;
  556. #endif
  557. {
  558. OPEN_READER(re, s)
  559. UPDATE_CACHE(re, s)
  560. UPDATE_CACHE(re, s)
  561. CLOSE_READER(re, s)
  562. }
  563. #ifdef A32_BITSTREAM_READER
  564. s->cache1 = 0;
  565. #endif
  566. }
  567. int check_marker(GetBitContext *s, const char *msg);
  568. void align_get_bits(GetBitContext *s);
  569. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  570. const void *bits, int bits_wrap, int bits_size,
  571. const void *codes, int codes_wrap, int codes_size,
  572. int use_static);
  573. void free_vlc(VLC *vlc);
  574. /**
  575. *
  576. * if the vlc code is invalid and max_depth=1 than no bits will be removed
  577. * if the vlc code is invalid and max_depth>1 than the number of bits removed
  578. * is undefined
  579. */
  580. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  581. {\
  582. int n, index, nb_bits;\
  583. \
  584. index= SHOW_UBITS(name, gb, bits);\
  585. code = table[index][0];\
  586. n = table[index][1];\
  587. \
  588. if(max_depth > 1 && n < 0){\
  589. LAST_SKIP_BITS(name, gb, bits)\
  590. UPDATE_CACHE(name, gb)\
  591. \
  592. nb_bits = -n;\
  593. \
  594. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  595. code = table[index][0];\
  596. n = table[index][1];\
  597. if(max_depth > 2 && n < 0){\
  598. LAST_SKIP_BITS(name, gb, nb_bits)\
  599. UPDATE_CACHE(name, gb)\
  600. \
  601. nb_bits = -n;\
  602. \
  603. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  604. code = table[index][0];\
  605. n = table[index][1];\
  606. }\
  607. }\
  608. SKIP_BITS(name, gb, n)\
  609. }
  610. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
  611. {\
  612. int n, index, nb_bits;\
  613. \
  614. index= SHOW_UBITS(name, gb, bits);\
  615. level = table[index].level;\
  616. n = table[index].len;\
  617. \
  618. if(max_depth > 1 && n < 0){\
  619. LAST_SKIP_BITS(name, gb, bits)\
  620. UPDATE_CACHE(name, gb)\
  621. \
  622. nb_bits = -n;\
  623. \
  624. index= SHOW_UBITS(name, gb, nb_bits) + level;\
  625. level = table[index].level;\
  626. n = table[index].len;\
  627. }\
  628. run= table[index].run;\
  629. SKIP_BITS(name, gb, n)\
  630. }
  631. // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
  632. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  633. {
  634. int code;
  635. VLC_TYPE (*table)[2]= vlc->table;
  636. OPEN_READER(re, s)
  637. UPDATE_CACHE(re, s)
  638. GET_VLC(code, re, s, table, vlc->bits, 3)
  639. CLOSE_READER(re, s)
  640. return code;
  641. }
  642. /**
  643. * parses a vlc code, faster then get_vlc()
  644. * @param bits is the number of bits which will be read at once, must be
  645. * identical to nb_bits in init_vlc()
  646. * @param max_depth is the number of times bits bits must be readed to completly
  647. * read the longest vlc code
  648. * = (max_vlc_length + bits - 1) / bits
  649. */
  650. static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  651. int bits, int max_depth)
  652. {
  653. int code;
  654. OPEN_READER(re, s)
  655. UPDATE_CACHE(re, s)
  656. GET_VLC(code, re, s, table, bits, max_depth)
  657. CLOSE_READER(re, s)
  658. return code;
  659. }
  660. //#define TRACE
  661. #ifdef TRACE
  662. #include "avcodec.h"
  663. static inline void print_bin(int bits, int n){
  664. int i;
  665. for(i=n-1; i>=0; i--){
  666. av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
  667. }
  668. for(i=n; i<24; i++)
  669. av_log(NULL, AV_LOG_DEBUG, " ");
  670. }
  671. static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  672. int r= get_bits(s, n);
  673. print_bin(r, n);
  674. 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);
  675. return r;
  676. }
  677. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
  678. int show= show_bits(s, 24);
  679. int pos= get_bits_count(s);
  680. int r= get_vlc2(s, table, bits, max_depth);
  681. int len= get_bits_count(s) - pos;
  682. int bits2= show>>(24-len);
  683. print_bin(bits2, len);
  684. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
  685. return r;
  686. }
  687. static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  688. int show= show_bits(s, n);
  689. int r= get_xbits(s, n);
  690. print_bin(show, n);
  691. 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);
  692. return r;
  693. }
  694. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  695. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  696. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  697. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  698. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  699. #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
  700. #else //TRACE
  701. #define tprintf(...) {}
  702. #endif
  703. #endif /* BITSTREAM_H */