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.

851 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. #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. * skips the given number of bits.
  264. * must only be used if the actual values in the bitstream dont matter
  265. */
  266. static inline void skip_put_bits(PutBitContext *s, int n){
  267. #ifdef ALT_BITSTREAM_WRITER
  268. s->index += n;
  269. #else
  270. s->bit_left -= n;
  271. s->buf_ptr-= s->bit_left>>5;
  272. s->bit_left &= 31;
  273. #endif
  274. }
  275. /**
  276. * Changes the end of the buffer.
  277. */
  278. static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
  279. s->buf_end= s->buf + size;
  280. }
  281. /* Bitstream reader API docs:
  282. name
  283. abritary name which is used as prefix for the internal variables
  284. gb
  285. getbitcontext
  286. OPEN_READER(name, gb)
  287. loads gb into local variables
  288. CLOSE_READER(name, gb)
  289. stores local vars in gb
  290. UPDATE_CACHE(name, gb)
  291. refills the internal cache from the bitstream
  292. after this call at least MIN_CACHE_BITS will be available,
  293. GET_CACHE(name, gb)
  294. will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
  295. SHOW_UBITS(name, gb, num)
  296. will return the nest num bits
  297. SHOW_SBITS(name, gb, num)
  298. will return the nest num bits and do sign extension
  299. SKIP_BITS(name, gb, num)
  300. will skip over the next num bits
  301. note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
  302. SKIP_CACHE(name, gb, num)
  303. will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
  304. SKIP_COUNTER(name, gb, num)
  305. will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
  306. LAST_SKIP_CACHE(name, gb, num)
  307. will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
  308. LAST_SKIP_BITS(name, gb, num)
  309. is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
  310. for examples see get_bits, show_bits, skip_bits, get_vlc
  311. */
  312. static inline int unaligned32_be(const void *v)
  313. {
  314. #ifdef CONFIG_ALIGN
  315. const uint8_t *p=v;
  316. return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
  317. #else
  318. return be2me_32( unaligned32(v)); //original
  319. #endif
  320. }
  321. #ifdef ALT_BITSTREAM_READER
  322. # define MIN_CACHE_BITS 25
  323. # define OPEN_READER(name, gb)\
  324. int name##_index= (gb)->index;\
  325. int name##_cache= 0;\
  326. # define CLOSE_READER(name, gb)\
  327. (gb)->index= name##_index;\
  328. # define UPDATE_CACHE(name, gb)\
  329. name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
  330. # define SKIP_CACHE(name, gb, num)\
  331. name##_cache <<= (num);\
  332. // FIXME name?
  333. # define SKIP_COUNTER(name, gb, num)\
  334. name##_index += (num);\
  335. # define SKIP_BITS(name, gb, num)\
  336. {\
  337. SKIP_CACHE(name, gb, num)\
  338. SKIP_COUNTER(name, gb, num)\
  339. }\
  340. # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  341. # define LAST_SKIP_CACHE(name, gb, num) ;
  342. # define SHOW_UBITS(name, gb, num)\
  343. NEG_USR32(name##_cache, num)
  344. # define SHOW_SBITS(name, gb, num)\
  345. NEG_SSR32(name##_cache, num)
  346. # define GET_CACHE(name, gb)\
  347. ((uint32_t)name##_cache)
  348. static inline int get_bits_count(GetBitContext *s){
  349. return s->index;
  350. }
  351. #elif defined LIBMPEG2_BITSTREAM_READER
  352. //libmpeg2 like reader
  353. # define MIN_CACHE_BITS 17
  354. # define OPEN_READER(name, gb)\
  355. int name##_bit_count=(gb)->bit_count;\
  356. int name##_cache= (gb)->cache;\
  357. uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  358. # define CLOSE_READER(name, gb)\
  359. (gb)->bit_count= name##_bit_count;\
  360. (gb)->cache= name##_cache;\
  361. (gb)->buffer_ptr= name##_buffer_ptr;\
  362. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  363. # define UPDATE_CACHE(name, gb)\
  364. if(name##_bit_count >= 0){\
  365. name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
  366. ((uint16_t*)name##_buffer_ptr)++;\
  367. name##_bit_count-= 16;\
  368. }\
  369. #else
  370. # define UPDATE_CACHE(name, gb)\
  371. if(name##_bit_count >= 0){\
  372. name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
  373. name##_buffer_ptr+=2;\
  374. name##_bit_count-= 16;\
  375. }\
  376. #endif
  377. # define SKIP_CACHE(name, gb, num)\
  378. name##_cache <<= (num);\
  379. # define SKIP_COUNTER(name, gb, num)\
  380. name##_bit_count += (num);\
  381. # define SKIP_BITS(name, gb, num)\
  382. {\
  383. SKIP_CACHE(name, gb, num)\
  384. SKIP_COUNTER(name, gb, num)\
  385. }\
  386. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  387. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  388. # define SHOW_UBITS(name, gb, num)\
  389. NEG_USR32(name##_cache, num)
  390. # define SHOW_SBITS(name, gb, num)\
  391. NEG_SSR32(name##_cache, num)
  392. # define GET_CACHE(name, gb)\
  393. ((uint32_t)name##_cache)
  394. static inline int get_bits_count(GetBitContext *s){
  395. return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
  396. }
  397. #elif defined A32_BITSTREAM_READER
  398. # define MIN_CACHE_BITS 32
  399. # define OPEN_READER(name, gb)\
  400. int name##_bit_count=(gb)->bit_count;\
  401. uint32_t name##_cache0= (gb)->cache0;\
  402. uint32_t name##_cache1= (gb)->cache1;\
  403. uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  404. # define CLOSE_READER(name, gb)\
  405. (gb)->bit_count= name##_bit_count;\
  406. (gb)->cache0= name##_cache0;\
  407. (gb)->cache1= name##_cache1;\
  408. (gb)->buffer_ptr= name##_buffer_ptr;\
  409. # define UPDATE_CACHE(name, gb)\
  410. if(name##_bit_count > 0){\
  411. const uint32_t next= be2me_32( *name##_buffer_ptr );\
  412. name##_cache0 |= NEG_USR32(next,name##_bit_count);\
  413. name##_cache1 |= next<<name##_bit_count;\
  414. name##_buffer_ptr++;\
  415. name##_bit_count-= 32;\
  416. }\
  417. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  418. # define SKIP_CACHE(name, gb, num)\
  419. asm(\
  420. "shldl %2, %1, %0 \n\t"\
  421. "shll %2, %1 \n\t"\
  422. : "+r" (name##_cache0), "+r" (name##_cache1)\
  423. : "Ic" ((uint8_t)num)\
  424. );
  425. #else
  426. # define SKIP_CACHE(name, gb, num)\
  427. name##_cache0 <<= (num);\
  428. name##_cache0 |= NEG_USR32(name##_cache1,num);\
  429. name##_cache1 <<= (num);
  430. #endif
  431. # define SKIP_COUNTER(name, gb, num)\
  432. name##_bit_count += (num);\
  433. # define SKIP_BITS(name, gb, num)\
  434. {\
  435. SKIP_CACHE(name, gb, num)\
  436. SKIP_COUNTER(name, gb, num)\
  437. }\
  438. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  439. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  440. # define SHOW_UBITS(name, gb, num)\
  441. NEG_USR32(name##_cache0, num)
  442. # define SHOW_SBITS(name, gb, num)\
  443. NEG_SSR32(name##_cache0, num)
  444. # define GET_CACHE(name, gb)\
  445. (name##_cache0)
  446. static inline int get_bits_count(GetBitContext *s){
  447. return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
  448. }
  449. #endif
  450. /**
  451. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  452. * if MSB not set it is negative
  453. * @param n length in bits
  454. * @author BERO
  455. */
  456. static inline int get_xbits(GetBitContext *s, int n){
  457. register int tmp;
  458. register int32_t cache;
  459. OPEN_READER(re, s)
  460. UPDATE_CACHE(re, s)
  461. cache = GET_CACHE(re,s);
  462. if ((int32_t)cache<0) { //MSB=1
  463. tmp = NEG_USR32(cache,n);
  464. } else {
  465. // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
  466. // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
  467. tmp = - NEG_USR32(~cache,n);
  468. }
  469. LAST_SKIP_BITS(re, s, n)
  470. CLOSE_READER(re, s)
  471. return tmp;
  472. }
  473. static inline int get_sbits(GetBitContext *s, int n){
  474. register int tmp;
  475. OPEN_READER(re, s)
  476. UPDATE_CACHE(re, s)
  477. tmp= SHOW_SBITS(re, s, n);
  478. LAST_SKIP_BITS(re, s, n)
  479. CLOSE_READER(re, s)
  480. return tmp;
  481. }
  482. /**
  483. * reads 0-17 bits.
  484. * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
  485. */
  486. static inline unsigned int get_bits(GetBitContext *s, int n){
  487. register int tmp;
  488. OPEN_READER(re, s)
  489. UPDATE_CACHE(re, s)
  490. tmp= SHOW_UBITS(re, s, n);
  491. LAST_SKIP_BITS(re, s, n)
  492. CLOSE_READER(re, s)
  493. return tmp;
  494. }
  495. unsigned int get_bits_long(GetBitContext *s, int n);
  496. /**
  497. * shows 0-17 bits.
  498. * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
  499. */
  500. static inline unsigned int show_bits(GetBitContext *s, int n){
  501. register int tmp;
  502. OPEN_READER(re, s)
  503. UPDATE_CACHE(re, s)
  504. tmp= SHOW_UBITS(re, s, n);
  505. // CLOSE_READER(re, s)
  506. return tmp;
  507. }
  508. unsigned int show_bits_long(GetBitContext *s, int n);
  509. static inline void skip_bits(GetBitContext *s, int n){
  510. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  511. OPEN_READER(re, s)
  512. UPDATE_CACHE(re, s)
  513. LAST_SKIP_BITS(re, s, n)
  514. CLOSE_READER(re, s)
  515. }
  516. static inline unsigned int get_bits1(GetBitContext *s){
  517. #ifdef ALT_BITSTREAM_READER
  518. int index= s->index;
  519. uint8_t result= s->buffer[ index>>3 ];
  520. result<<= (index&0x07);
  521. result>>= 8 - 1;
  522. index++;
  523. s->index= index;
  524. return result;
  525. #else
  526. return get_bits(s, 1);
  527. #endif
  528. }
  529. static inline unsigned int show_bits1(GetBitContext *s){
  530. return show_bits(s, 1);
  531. }
  532. static inline void skip_bits1(GetBitContext *s){
  533. skip_bits(s, 1);
  534. }
  535. /**
  536. * init GetBitContext.
  537. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
  538. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  539. * @param bit_size the size of the buffer in bits
  540. */
  541. static inline void init_get_bits(GetBitContext *s,
  542. const uint8_t *buffer, int bit_size)
  543. {
  544. const int buffer_size= (bit_size+7)>>3;
  545. s->buffer= buffer;
  546. s->size_in_bits= bit_size;
  547. s->buffer_end= buffer + buffer_size;
  548. #ifdef ALT_BITSTREAM_READER
  549. s->index=0;
  550. #elif defined LIBMPEG2_BITSTREAM_READER
  551. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  552. if ((int)buffer&1) {
  553. /* word alignment */
  554. s->cache = (*buffer++)<<24;
  555. s->buffer_ptr = buffer;
  556. s->bit_count = 16-8;
  557. } else
  558. #endif
  559. {
  560. s->buffer_ptr = buffer;
  561. s->bit_count = 16;
  562. s->cache = 0;
  563. }
  564. #elif defined A32_BITSTREAM_READER
  565. s->buffer_ptr = (uint32_t*)buffer;
  566. s->bit_count = 32;
  567. s->cache0 = 0;
  568. s->cache1 = 0;
  569. #endif
  570. {
  571. OPEN_READER(re, s)
  572. UPDATE_CACHE(re, s)
  573. UPDATE_CACHE(re, s)
  574. CLOSE_READER(re, s)
  575. }
  576. #ifdef A32_BITSTREAM_READER
  577. s->cache1 = 0;
  578. #endif
  579. }
  580. int check_marker(GetBitContext *s, const char *msg);
  581. void align_get_bits(GetBitContext *s);
  582. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  583. const void *bits, int bits_wrap, int bits_size,
  584. const void *codes, int codes_wrap, int codes_size,
  585. int use_static);
  586. void free_vlc(VLC *vlc);
  587. /**
  588. *
  589. * if the vlc code is invalid and max_depth=1 than no bits will be removed
  590. * if the vlc code is invalid and max_depth>1 than the number of bits removed
  591. * is undefined
  592. */
  593. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  594. {\
  595. int n, index, nb_bits;\
  596. \
  597. index= SHOW_UBITS(name, gb, bits);\
  598. code = table[index][0];\
  599. n = table[index][1];\
  600. \
  601. if(max_depth > 1 && n < 0){\
  602. LAST_SKIP_BITS(name, gb, bits)\
  603. UPDATE_CACHE(name, gb)\
  604. \
  605. nb_bits = -n;\
  606. \
  607. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  608. code = table[index][0];\
  609. n = table[index][1];\
  610. if(max_depth > 2 && n < 0){\
  611. LAST_SKIP_BITS(name, gb, nb_bits)\
  612. UPDATE_CACHE(name, gb)\
  613. \
  614. nb_bits = -n;\
  615. \
  616. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  617. code = table[index][0];\
  618. n = table[index][1];\
  619. }\
  620. }\
  621. SKIP_BITS(name, gb, n)\
  622. }
  623. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
  624. {\
  625. int n, index, nb_bits;\
  626. \
  627. index= SHOW_UBITS(name, gb, bits);\
  628. level = table[index].level;\
  629. n = table[index].len;\
  630. \
  631. if(max_depth > 1 && n < 0){\
  632. LAST_SKIP_BITS(name, gb, bits)\
  633. UPDATE_CACHE(name, gb)\
  634. \
  635. nb_bits = -n;\
  636. \
  637. index= SHOW_UBITS(name, gb, nb_bits) + level;\
  638. level = table[index].level;\
  639. n = table[index].len;\
  640. }\
  641. run= table[index].run;\
  642. SKIP_BITS(name, gb, n)\
  643. }
  644. // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
  645. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  646. {
  647. int code;
  648. VLC_TYPE (*table)[2]= vlc->table;
  649. OPEN_READER(re, s)
  650. UPDATE_CACHE(re, s)
  651. GET_VLC(code, re, s, table, vlc->bits, 3)
  652. CLOSE_READER(re, s)
  653. return code;
  654. }
  655. /**
  656. * parses a vlc code, faster then get_vlc()
  657. * @param bits is the number of bits which will be read at once, must be
  658. * identical to nb_bits in init_vlc()
  659. * @param max_depth is the number of times bits bits must be readed to completly
  660. * read the longest vlc code
  661. * = (max_vlc_length + bits - 1) / bits
  662. */
  663. static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  664. int bits, int max_depth)
  665. {
  666. int code;
  667. OPEN_READER(re, s)
  668. UPDATE_CACHE(re, s)
  669. GET_VLC(code, re, s, table, bits, max_depth)
  670. CLOSE_READER(re, s)
  671. return code;
  672. }
  673. //#define TRACE
  674. #ifdef TRACE
  675. #include "avcodec.h"
  676. static inline void print_bin(int bits, int n){
  677. int i;
  678. for(i=n-1; i>=0; i--){
  679. av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
  680. }
  681. for(i=n; i<24; i++)
  682. av_log(NULL, AV_LOG_DEBUG, " ");
  683. }
  684. static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  685. int r= get_bits(s, n);
  686. print_bin(r, n);
  687. 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);
  688. return r;
  689. }
  690. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
  691. int show= show_bits(s, 24);
  692. int pos= get_bits_count(s);
  693. int r= get_vlc2(s, table, bits, max_depth);
  694. int len= get_bits_count(s) - pos;
  695. int bits2= show>>(24-len);
  696. print_bin(bits2, len);
  697. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
  698. return r;
  699. }
  700. static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  701. int show= show_bits(s, n);
  702. int r= get_xbits(s, n);
  703. print_bin(show, n);
  704. 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);
  705. return r;
  706. }
  707. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  708. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  709. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  710. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  711. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  712. #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
  713. #else //TRACE
  714. #define tprintf(...) {}
  715. #endif
  716. static inline int decode012(GetBitContext *gb){
  717. int n;
  718. n = get_bits1(gb);
  719. if (n == 0)
  720. return 0;
  721. else
  722. return get_bits1(gb) + 1;
  723. }
  724. #endif /* BITSTREAM_H */