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.

892 lines
24KB

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