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.

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