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.

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