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.

954 lines
26KB

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