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.

898 lines
24KB

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