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.

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