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.

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