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.

922 lines
25KB

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