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.

883 lines
23KB

  1. /**
  2. * @file bitstream.h
  3. * bitstream api header.
  4. */
  5. #ifndef BITSTREAM_H
  6. #define BITSTREAM_H
  7. //#define ALT_BITSTREAM_WRITER
  8. //#define ALIGNED_BITSTREAM_WRITER
  9. #define ALT_BITSTREAM_READER
  10. //#define LIBMPEG2_BITSTREAM_READER
  11. //#define A32_BITSTREAM_READER
  12. #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
  13. extern const uint8_t ff_reverse[256];
  14. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  15. // avoid +32 for shift optimization (gcc should do that ...)
  16. static inline int32_t NEG_SSR32( int32_t a, int8_t s){
  17. asm ("sarl %1, %0\n\t"
  18. : "+r" (a)
  19. : "ic" ((uint8_t)(-s))
  20. );
  21. return a;
  22. }
  23. static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
  24. asm ("shrl %1, %0\n\t"
  25. : "+r" (a)
  26. : "ic" ((uint8_t)(-s))
  27. );
  28. return a;
  29. }
  30. #else
  31. # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
  32. # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
  33. #endif
  34. /* bit output */
  35. /* buf and buf_end must be present and used by every alternative writer. */
  36. typedef struct PutBitContext {
  37. #ifdef ALT_BITSTREAM_WRITER
  38. uint8_t *buf, *buf_end;
  39. int index;
  40. #else
  41. uint32_t bit_buf;
  42. int bit_left;
  43. uint8_t *buf, *buf_ptr, *buf_end;
  44. #endif
  45. } PutBitContext;
  46. static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
  47. {
  48. if(buffer_size < 0) {
  49. buffer_size = 0;
  50. buffer = NULL;
  51. }
  52. s->buf = buffer;
  53. s->buf_end = s->buf + buffer_size;
  54. #ifdef ALT_BITSTREAM_WRITER
  55. s->index=0;
  56. ((uint32_t*)(s->buf))[0]=0;
  57. // memset(buffer, 0, buffer_size);
  58. #else
  59. s->buf_ptr = s->buf;
  60. s->bit_left=32;
  61. s->bit_buf=0;
  62. #endif
  63. }
  64. /* return the number of bits output */
  65. static inline int put_bits_count(PutBitContext *s)
  66. {
  67. #ifdef ALT_BITSTREAM_WRITER
  68. return s->index;
  69. #else
  70. return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
  71. #endif
  72. }
  73. /* pad the end of the output stream with zeros */
  74. static inline void flush_put_bits(PutBitContext *s)
  75. {
  76. #ifdef ALT_BITSTREAM_WRITER
  77. align_put_bits(s);
  78. #else
  79. s->bit_buf<<= s->bit_left;
  80. while (s->bit_left < 32) {
  81. /* XXX: should test end of buffer */
  82. *s->buf_ptr++=s->bit_buf >> 24;
  83. s->bit_buf<<=8;
  84. s->bit_left+=8;
  85. }
  86. s->bit_left=32;
  87. s->bit_buf=0;
  88. #endif
  89. }
  90. void align_put_bits(PutBitContext *s);
  91. void ff_put_string(PutBitContext * pbc, char *s, int put_zero);
  92. /* bit input */
  93. /* buffer, buffer_end and size_in_bits must be present and used by every reader */
  94. typedef struct GetBitContext {
  95. const uint8_t *buffer, *buffer_end;
  96. #ifdef ALT_BITSTREAM_READER
  97. int index;
  98. #elif defined LIBMPEG2_BITSTREAM_READER
  99. uint8_t *buffer_ptr;
  100. uint32_t cache;
  101. int bit_count;
  102. #elif defined A32_BITSTREAM_READER
  103. uint32_t *buffer_ptr;
  104. uint32_t cache0;
  105. uint32_t cache1;
  106. int bit_count;
  107. #endif
  108. int size_in_bits;
  109. } GetBitContext;
  110. #define VLC_TYPE int16_t
  111. typedef struct VLC {
  112. int bits;
  113. VLC_TYPE (*table)[2]; ///< code, bits
  114. int table_size, table_allocated;
  115. } VLC;
  116. typedef struct RL_VLC_ELEM {
  117. int16_t level;
  118. int8_t len;
  119. uint8_t run;
  120. } RL_VLC_ELEM;
  121. #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L)
  122. #define UNALIGNED_STORES_ARE_BAD
  123. #endif
  124. /* used to avoid missaligned exceptions on some archs (alpha, ...) */
  125. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  126. # define 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. #ifdef STATS
  161. st_out_bit_counts[st_current_index] += n;
  162. #endif
  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. #elif defined LIBMPEG2_BITSTREAM_READER
  388. //libmpeg2 like reader
  389. # define MIN_CACHE_BITS 17
  390. # define OPEN_READER(name, gb)\
  391. int name##_bit_count=(gb)->bit_count;\
  392. int name##_cache= (gb)->cache;\
  393. uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  394. # define CLOSE_READER(name, gb)\
  395. (gb)->bit_count= name##_bit_count;\
  396. (gb)->cache= name##_cache;\
  397. (gb)->buffer_ptr= name##_buffer_ptr;\
  398. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  399. # define UPDATE_CACHE(name, gb)\
  400. if(name##_bit_count >= 0){\
  401. name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
  402. name##_buffer_ptr += 2;\
  403. name##_bit_count-= 16;\
  404. }\
  405. #else
  406. # define UPDATE_CACHE(name, gb)\
  407. if(name##_bit_count >= 0){\
  408. name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
  409. name##_buffer_ptr+=2;\
  410. name##_bit_count-= 16;\
  411. }\
  412. #endif
  413. # define SKIP_CACHE(name, gb, num)\
  414. name##_cache <<= (num);\
  415. # define SKIP_COUNTER(name, gb, num)\
  416. name##_bit_count += (num);\
  417. # define SKIP_BITS(name, gb, num)\
  418. {\
  419. SKIP_CACHE(name, gb, num)\
  420. SKIP_COUNTER(name, gb, num)\
  421. }\
  422. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  423. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  424. # define SHOW_UBITS(name, gb, num)\
  425. NEG_USR32(name##_cache, num)
  426. # define SHOW_SBITS(name, gb, num)\
  427. NEG_SSR32(name##_cache, num)
  428. # define GET_CACHE(name, gb)\
  429. ((uint32_t)name##_cache)
  430. static inline int get_bits_count(GetBitContext *s){
  431. return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
  432. }
  433. #elif defined A32_BITSTREAM_READER
  434. # define MIN_CACHE_BITS 32
  435. # define OPEN_READER(name, gb)\
  436. int name##_bit_count=(gb)->bit_count;\
  437. uint32_t name##_cache0= (gb)->cache0;\
  438. uint32_t name##_cache1= (gb)->cache1;\
  439. uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  440. # define CLOSE_READER(name, gb)\
  441. (gb)->bit_count= name##_bit_count;\
  442. (gb)->cache0= name##_cache0;\
  443. (gb)->cache1= name##_cache1;\
  444. (gb)->buffer_ptr= name##_buffer_ptr;\
  445. # define UPDATE_CACHE(name, gb)\
  446. if(name##_bit_count > 0){\
  447. const uint32_t next= be2me_32( *name##_buffer_ptr );\
  448. name##_cache0 |= NEG_USR32(next,name##_bit_count);\
  449. name##_cache1 |= next<<name##_bit_count;\
  450. name##_buffer_ptr++;\
  451. name##_bit_count-= 32;\
  452. }\
  453. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  454. # define SKIP_CACHE(name, gb, num)\
  455. asm(\
  456. "shldl %2, %1, %0 \n\t"\
  457. "shll %2, %1 \n\t"\
  458. : "+r" (name##_cache0), "+r" (name##_cache1)\
  459. : "Ic" ((uint8_t)num)\
  460. );
  461. #else
  462. # define SKIP_CACHE(name, gb, num)\
  463. name##_cache0 <<= (num);\
  464. name##_cache0 |= NEG_USR32(name##_cache1,num);\
  465. name##_cache1 <<= (num);
  466. #endif
  467. # define SKIP_COUNTER(name, gb, num)\
  468. name##_bit_count += (num);\
  469. # define SKIP_BITS(name, gb, num)\
  470. {\
  471. SKIP_CACHE(name, gb, num)\
  472. SKIP_COUNTER(name, gb, num)\
  473. }\
  474. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  475. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  476. # define SHOW_UBITS(name, gb, num)\
  477. NEG_USR32(name##_cache0, num)
  478. # define SHOW_SBITS(name, gb, num)\
  479. NEG_SSR32(name##_cache0, num)
  480. # define GET_CACHE(name, gb)\
  481. (name##_cache0)
  482. static inline int get_bits_count(GetBitContext *s){
  483. return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
  484. }
  485. #endif
  486. /**
  487. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  488. * if MSB not set it is negative
  489. * @param n length in bits
  490. * @author BERO
  491. */
  492. static inline int get_xbits(GetBitContext *s, int n){
  493. register int sign;
  494. register int32_t cache;
  495. OPEN_READER(re, s)
  496. UPDATE_CACHE(re, s)
  497. cache = GET_CACHE(re,s);
  498. sign=(~cache)>>31;
  499. LAST_SKIP_BITS(re, s, n)
  500. CLOSE_READER(re, s)
  501. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  502. }
  503. static inline int get_sbits(GetBitContext *s, int n){
  504. register int tmp;
  505. OPEN_READER(re, s)
  506. UPDATE_CACHE(re, s)
  507. tmp= SHOW_SBITS(re, s, n);
  508. LAST_SKIP_BITS(re, s, n)
  509. CLOSE_READER(re, s)
  510. return tmp;
  511. }
  512. /**
  513. * reads 0-17 bits.
  514. * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
  515. */
  516. static inline unsigned int get_bits(GetBitContext *s, int n){
  517. register int tmp;
  518. OPEN_READER(re, s)
  519. UPDATE_CACHE(re, s)
  520. tmp= SHOW_UBITS(re, s, n);
  521. LAST_SKIP_BITS(re, s, n)
  522. CLOSE_READER(re, s)
  523. return tmp;
  524. }
  525. unsigned int get_bits_long(GetBitContext *s, int n);
  526. /**
  527. * shows 0-17 bits.
  528. * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
  529. */
  530. static inline unsigned int show_bits(GetBitContext *s, int n){
  531. register int tmp;
  532. OPEN_READER(re, s)
  533. UPDATE_CACHE(re, s)
  534. tmp= SHOW_UBITS(re, s, n);
  535. // CLOSE_READER(re, s)
  536. return tmp;
  537. }
  538. unsigned int show_bits_long(GetBitContext *s, int n);
  539. static inline void skip_bits(GetBitContext *s, int n){
  540. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  541. OPEN_READER(re, s)
  542. UPDATE_CACHE(re, s)
  543. LAST_SKIP_BITS(re, s, n)
  544. CLOSE_READER(re, s)
  545. }
  546. static inline unsigned int get_bits1(GetBitContext *s){
  547. #ifdef ALT_BITSTREAM_READER
  548. int index= s->index;
  549. uint8_t result= s->buffer[ index>>3 ];
  550. #ifdef ALT_BITSTREAM_READER_LE
  551. result>>= (index&0x07);
  552. result&= 1;
  553. #else
  554. result<<= (index&0x07);
  555. result>>= 8 - 1;
  556. #endif
  557. index++;
  558. s->index= index;
  559. return result;
  560. #else
  561. return get_bits(s, 1);
  562. #endif
  563. }
  564. static inline unsigned int show_bits1(GetBitContext *s){
  565. return show_bits(s, 1);
  566. }
  567. static inline void skip_bits1(GetBitContext *s){
  568. skip_bits(s, 1);
  569. }
  570. /**
  571. * init GetBitContext.
  572. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
  573. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  574. * @param bit_size the size of the buffer in bits
  575. */
  576. static inline void init_get_bits(GetBitContext *s,
  577. const uint8_t *buffer, int bit_size)
  578. {
  579. int buffer_size= (bit_size+7)>>3;
  580. if(buffer_size < 0 || bit_size < 0) {
  581. buffer_size = bit_size = 0;
  582. buffer = NULL;
  583. }
  584. s->buffer= buffer;
  585. s->size_in_bits= bit_size;
  586. s->buffer_end= buffer + buffer_size;
  587. #ifdef ALT_BITSTREAM_READER
  588. s->index=0;
  589. #elif defined LIBMPEG2_BITSTREAM_READER
  590. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  591. if ((int)buffer&1) {
  592. /* word alignment */
  593. s->cache = (*buffer++)<<24;
  594. s->buffer_ptr = buffer;
  595. s->bit_count = 16-8;
  596. } else
  597. #endif
  598. {
  599. s->buffer_ptr = buffer;
  600. s->bit_count = 16;
  601. s->cache = 0;
  602. }
  603. #elif defined A32_BITSTREAM_READER
  604. s->buffer_ptr = (uint32_t*)buffer;
  605. s->bit_count = 32;
  606. s->cache0 = 0;
  607. s->cache1 = 0;
  608. #endif
  609. {
  610. OPEN_READER(re, s)
  611. UPDATE_CACHE(re, s)
  612. UPDATE_CACHE(re, s)
  613. CLOSE_READER(re, s)
  614. }
  615. #ifdef A32_BITSTREAM_READER
  616. s->cache1 = 0;
  617. #endif
  618. }
  619. int check_marker(GetBitContext *s, const char *msg);
  620. void align_get_bits(GetBitContext *s);
  621. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  622. const void *bits, int bits_wrap, int bits_size,
  623. const void *codes, int codes_wrap, int codes_size,
  624. int flags);
  625. #define INIT_VLC_USE_STATIC 1
  626. #define INIT_VLC_LE 2
  627. void free_vlc(VLC *vlc);
  628. /**
  629. *
  630. * if the vlc code is invalid and max_depth=1 than no bits will be removed
  631. * if the vlc code is invalid and max_depth>1 than the number of bits removed
  632. * is undefined
  633. */
  634. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  635. {\
  636. int n, index, nb_bits;\
  637. \
  638. index= SHOW_UBITS(name, gb, bits);\
  639. code = table[index][0];\
  640. n = table[index][1];\
  641. \
  642. if(max_depth > 1 && n < 0){\
  643. LAST_SKIP_BITS(name, gb, bits)\
  644. UPDATE_CACHE(name, gb)\
  645. \
  646. nb_bits = -n;\
  647. \
  648. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  649. code = table[index][0];\
  650. n = table[index][1];\
  651. if(max_depth > 2 && n < 0){\
  652. LAST_SKIP_BITS(name, gb, nb_bits)\
  653. UPDATE_CACHE(name, gb)\
  654. \
  655. nb_bits = -n;\
  656. \
  657. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  658. code = table[index][0];\
  659. n = table[index][1];\
  660. }\
  661. }\
  662. SKIP_BITS(name, gb, n)\
  663. }
  664. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
  665. {\
  666. int n, index, nb_bits;\
  667. \
  668. index= SHOW_UBITS(name, gb, bits);\
  669. level = table[index].level;\
  670. n = table[index].len;\
  671. \
  672. if(max_depth > 1 && n < 0){\
  673. SKIP_BITS(name, gb, bits)\
  674. if(need_update){\
  675. UPDATE_CACHE(name, gb)\
  676. }\
  677. \
  678. nb_bits = -n;\
  679. \
  680. index= SHOW_UBITS(name, gb, nb_bits) + level;\
  681. level = table[index].level;\
  682. n = table[index].len;\
  683. }\
  684. run= table[index].run;\
  685. SKIP_BITS(name, gb, n)\
  686. }
  687. /**
  688. * parses a vlc code, faster then get_vlc()
  689. * @param bits is the number of bits which will be read at once, must be
  690. * identical to nb_bits in init_vlc()
  691. * @param max_depth is the number of times bits bits must be readed to completly
  692. * read the longest vlc code
  693. * = (max_vlc_length + bits - 1) / bits
  694. */
  695. static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  696. int bits, int max_depth)
  697. {
  698. int code;
  699. OPEN_READER(re, s)
  700. UPDATE_CACHE(re, s)
  701. GET_VLC(code, re, s, table, bits, max_depth)
  702. CLOSE_READER(re, s)
  703. return code;
  704. }
  705. //#define TRACE
  706. #ifdef TRACE
  707. #include "avcodec.h"
  708. static inline void print_bin(int bits, int n){
  709. int i;
  710. for(i=n-1; i>=0; i--){
  711. av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
  712. }
  713. for(i=n; i<24; i++)
  714. av_log(NULL, AV_LOG_DEBUG, " ");
  715. }
  716. static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  717. int r= get_bits(s, n);
  718. print_bin(r, n);
  719. 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);
  720. return r;
  721. }
  722. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
  723. int show= show_bits(s, 24);
  724. int pos= get_bits_count(s);
  725. int r= get_vlc2(s, table, bits, max_depth);
  726. int len= get_bits_count(s) - pos;
  727. int bits2= show>>(24-len);
  728. print_bin(bits2, len);
  729. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
  730. return r;
  731. }
  732. static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  733. int show= show_bits(s, n);
  734. int r= get_xbits(s, n);
  735. print_bin(show, n);
  736. 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);
  737. return r;
  738. }
  739. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  740. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  741. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  742. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  743. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  744. #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
  745. #else //TRACE
  746. #define tprintf(...) {}
  747. #endif
  748. static inline int decode012(GetBitContext *gb){
  749. int n;
  750. n = get_bits1(gb);
  751. if (n == 0)
  752. return 0;
  753. else
  754. return get_bits1(gb) + 1;
  755. }
  756. #endif /* BITSTREAM_H */