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.

947 lines
25KB

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