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.

853 lines
22KB

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