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