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.

885 lines
23KB

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