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.

957 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)
  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)
  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 /* defined(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)
  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)
  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. # define SHOW_SBITS(name, gb, num)\
  403. NEG_SSR32((name##_cache)<<(32-(num)), num)
  404. # else
  405. # define SHOW_UBITS(name, gb, num)\
  406. NEG_USR32(name##_cache, num)
  407. # define SHOW_SBITS(name, gb, num)\
  408. NEG_SSR32(name##_cache, num)
  409. # endif
  410. # define GET_CACHE(name, gb)\
  411. ((uint32_t)name##_cache)
  412. static inline int get_bits_count(GetBitContext *s){
  413. return s->index;
  414. }
  415. static inline void skip_bits_long(GetBitContext *s, int n){
  416. s->index += n;
  417. }
  418. #elif defined LIBMPEG2_BITSTREAM_READER
  419. //libmpeg2 like reader
  420. # define MIN_CACHE_BITS 17
  421. # define OPEN_READER(name, gb)\
  422. int name##_bit_count=(gb)->bit_count;\
  423. int name##_cache= (gb)->cache;\
  424. uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  425. # define CLOSE_READER(name, gb)\
  426. (gb)->bit_count= name##_bit_count;\
  427. (gb)->cache= name##_cache;\
  428. (gb)->buffer_ptr= name##_buffer_ptr;\
  429. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  430. # define UPDATE_CACHE(name, gb)\
  431. if(name##_bit_count >= 0){\
  432. name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
  433. name##_buffer_ptr += 2;\
  434. name##_bit_count-= 16;\
  435. }\
  436. #else
  437. # define UPDATE_CACHE(name, gb)\
  438. if(name##_bit_count >= 0){\
  439. name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
  440. name##_buffer_ptr+=2;\
  441. name##_bit_count-= 16;\
  442. }\
  443. #endif
  444. # define SKIP_CACHE(name, gb, num)\
  445. name##_cache <<= (num);\
  446. # define SKIP_COUNTER(name, gb, num)\
  447. name##_bit_count += (num);\
  448. # define SKIP_BITS(name, gb, num)\
  449. {\
  450. SKIP_CACHE(name, gb, num)\
  451. SKIP_COUNTER(name, gb, num)\
  452. }\
  453. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  454. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  455. # define SHOW_UBITS(name, gb, num)\
  456. NEG_USR32(name##_cache, num)
  457. # define SHOW_SBITS(name, gb, num)\
  458. NEG_SSR32(name##_cache, num)
  459. # define GET_CACHE(name, gb)\
  460. ((uint32_t)name##_cache)
  461. static inline int get_bits_count(GetBitContext *s){
  462. return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
  463. }
  464. static inline void skip_bits_long(GetBitContext *s, int n){
  465. OPEN_READER(re, s)
  466. re_bit_count += n;
  467. re_buffer_ptr += 2*(re_bit_count>>4);
  468. re_bit_count &= 15;
  469. re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
  470. UPDATE_CACHE(re, s)
  471. CLOSE_READER(re, s)
  472. }
  473. #elif defined A32_BITSTREAM_READER
  474. # define MIN_CACHE_BITS 32
  475. # define OPEN_READER(name, gb)\
  476. int name##_bit_count=(gb)->bit_count;\
  477. uint32_t name##_cache0= (gb)->cache0;\
  478. uint32_t name##_cache1= (gb)->cache1;\
  479. uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  480. # define CLOSE_READER(name, gb)\
  481. (gb)->bit_count= name##_bit_count;\
  482. (gb)->cache0= name##_cache0;\
  483. (gb)->cache1= name##_cache1;\
  484. (gb)->buffer_ptr= name##_buffer_ptr;\
  485. # define UPDATE_CACHE(name, gb)\
  486. if(name##_bit_count > 0){\
  487. const uint32_t next= be2me_32( *name##_buffer_ptr );\
  488. name##_cache0 |= NEG_USR32(next,name##_bit_count);\
  489. name##_cache1 |= next<<name##_bit_count;\
  490. name##_buffer_ptr++;\
  491. name##_bit_count-= 32;\
  492. }\
  493. #if defined(ARCH_X86)
  494. # define SKIP_CACHE(name, gb, num)\
  495. asm(\
  496. "shldl %2, %1, %0 \n\t"\
  497. "shll %2, %1 \n\t"\
  498. : "+r" (name##_cache0), "+r" (name##_cache1)\
  499. : "Ic" ((uint8_t)(num))\
  500. );
  501. #else
  502. # define SKIP_CACHE(name, gb, num)\
  503. name##_cache0 <<= (num);\
  504. name##_cache0 |= NEG_USR32(name##_cache1,num);\
  505. name##_cache1 <<= (num);
  506. #endif
  507. # define SKIP_COUNTER(name, gb, num)\
  508. name##_bit_count += (num);\
  509. # define SKIP_BITS(name, gb, num)\
  510. {\
  511. SKIP_CACHE(name, gb, num)\
  512. SKIP_COUNTER(name, gb, num)\
  513. }\
  514. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  515. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  516. # define SHOW_UBITS(name, gb, num)\
  517. NEG_USR32(name##_cache0, num)
  518. # define SHOW_SBITS(name, gb, num)\
  519. NEG_SSR32(name##_cache0, num)
  520. # define GET_CACHE(name, gb)\
  521. (name##_cache0)
  522. static inline int get_bits_count(GetBitContext *s){
  523. return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
  524. }
  525. static inline void skip_bits_long(GetBitContext *s, int n){
  526. OPEN_READER(re, s)
  527. re_bit_count += n;
  528. re_buffer_ptr += re_bit_count>>5;
  529. re_bit_count &= 31;
  530. re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
  531. re_cache1 = 0;
  532. UPDATE_CACHE(re, s)
  533. CLOSE_READER(re, s)
  534. }
  535. #endif
  536. /**
  537. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  538. * if MSB not set it is negative
  539. * @param n length in bits
  540. * @author BERO
  541. */
  542. static inline int get_xbits(GetBitContext *s, int n){
  543. register int sign;
  544. register int32_t cache;
  545. OPEN_READER(re, s)
  546. UPDATE_CACHE(re, s)
  547. cache = GET_CACHE(re,s);
  548. sign=(~cache)>>31;
  549. LAST_SKIP_BITS(re, s, n)
  550. CLOSE_READER(re, s)
  551. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  552. }
  553. static inline int get_sbits(GetBitContext *s, int n){
  554. register int tmp;
  555. OPEN_READER(re, s)
  556. UPDATE_CACHE(re, s)
  557. tmp= SHOW_SBITS(re, s, n);
  558. LAST_SKIP_BITS(re, s, n)
  559. CLOSE_READER(re, s)
  560. return tmp;
  561. }
  562. /**
  563. * reads 1-17 bits.
  564. * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
  565. */
  566. static inline unsigned int get_bits(GetBitContext *s, int n){
  567. register int tmp;
  568. OPEN_READER(re, s)
  569. UPDATE_CACHE(re, s)
  570. tmp= SHOW_UBITS(re, s, n);
  571. LAST_SKIP_BITS(re, s, n)
  572. CLOSE_READER(re, s)
  573. return tmp;
  574. }
  575. /**
  576. * shows 1-17 bits.
  577. * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
  578. */
  579. static inline unsigned int show_bits(GetBitContext *s, int n){
  580. register int tmp;
  581. OPEN_READER(re, s)
  582. UPDATE_CACHE(re, s)
  583. tmp= SHOW_UBITS(re, s, n);
  584. // CLOSE_READER(re, s)
  585. return tmp;
  586. }
  587. static inline void skip_bits(GetBitContext *s, int n){
  588. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  589. OPEN_READER(re, s)
  590. UPDATE_CACHE(re, s)
  591. LAST_SKIP_BITS(re, s, n)
  592. CLOSE_READER(re, s)
  593. }
  594. static inline unsigned int get_bits1(GetBitContext *s){
  595. #ifdef ALT_BITSTREAM_READER
  596. int index= s->index;
  597. uint8_t result= s->buffer[ index>>3 ];
  598. #ifdef ALT_BITSTREAM_READER_LE
  599. result>>= (index&0x07);
  600. result&= 1;
  601. #else
  602. result<<= (index&0x07);
  603. result>>= 8 - 1;
  604. #endif
  605. index++;
  606. s->index= index;
  607. return result;
  608. #else
  609. return get_bits(s, 1);
  610. #endif
  611. }
  612. static inline unsigned int show_bits1(GetBitContext *s){
  613. return show_bits(s, 1);
  614. }
  615. static inline void skip_bits1(GetBitContext *s){
  616. skip_bits(s, 1);
  617. }
  618. /**
  619. * reads 0-32 bits.
  620. */
  621. static inline unsigned int get_bits_long(GetBitContext *s, int n){
  622. if(n<=17) return get_bits(s, n);
  623. else{
  624. #ifdef ALT_BITSTREAM_READER_LE
  625. int ret= get_bits(s, 16);
  626. return ret | (get_bits(s, n-16) << 16);
  627. #else
  628. int ret= get_bits(s, 16) << (n-16);
  629. return ret | get_bits(s, n-16);
  630. #endif
  631. }
  632. }
  633. /**
  634. * shows 0-32 bits.
  635. */
  636. static inline unsigned int show_bits_long(GetBitContext *s, int n){
  637. if(n<=17) return show_bits(s, n);
  638. else{
  639. GetBitContext gb= *s;
  640. int ret= get_bits_long(s, n);
  641. *s= gb;
  642. return ret;
  643. }
  644. }
  645. static inline int check_marker(GetBitContext *s, const char *msg)
  646. {
  647. int bit= get_bits1(s);
  648. if(!bit)
  649. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  650. return bit;
  651. }
  652. /**
  653. * init GetBitContext.
  654. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
  655. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  656. * @param bit_size the size of the buffer in bits
  657. */
  658. static inline void init_get_bits(GetBitContext *s,
  659. const uint8_t *buffer, int bit_size)
  660. {
  661. int buffer_size= (bit_size+7)>>3;
  662. if(buffer_size < 0 || bit_size < 0) {
  663. buffer_size = bit_size = 0;
  664. buffer = NULL;
  665. }
  666. s->buffer= buffer;
  667. s->size_in_bits= bit_size;
  668. s->buffer_end= buffer + buffer_size;
  669. #ifdef ALT_BITSTREAM_READER
  670. s->index=0;
  671. #elif defined LIBMPEG2_BITSTREAM_READER
  672. s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
  673. s->bit_count = 16 + 8*((intptr_t)buffer&1);
  674. skip_bits_long(s, 0);
  675. #elif defined A32_BITSTREAM_READER
  676. s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
  677. s->bit_count = 32 + 8*((intptr_t)buffer&3);
  678. skip_bits_long(s, 0);
  679. #endif
  680. }
  681. static inline void align_get_bits(GetBitContext *s)
  682. {
  683. int n= (-get_bits_count(s)) & 7;
  684. if(n) skip_bits(s, n);
  685. }
  686. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  687. const void *bits, int bits_wrap, int bits_size,
  688. const void *codes, int codes_wrap, int codes_size,
  689. int flags);
  690. #define INIT_VLC_USE_STATIC 1
  691. #define INIT_VLC_LE 2
  692. void free_vlc(VLC *vlc);
  693. /**
  694. *
  695. * if the vlc code is invalid and max_depth=1 than no bits will be removed
  696. * if the vlc code is invalid and max_depth>1 than the number of bits removed
  697. * is undefined
  698. */
  699. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  700. {\
  701. int n, index, nb_bits;\
  702. \
  703. index= SHOW_UBITS(name, gb, bits);\
  704. code = table[index][0];\
  705. n = table[index][1];\
  706. \
  707. if(max_depth > 1 && n < 0){\
  708. LAST_SKIP_BITS(name, gb, 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. if(max_depth > 2 && n < 0){\
  717. LAST_SKIP_BITS(name, gb, nb_bits)\
  718. UPDATE_CACHE(name, gb)\
  719. \
  720. nb_bits = -n;\
  721. \
  722. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  723. code = table[index][0];\
  724. n = table[index][1];\
  725. }\
  726. }\
  727. SKIP_BITS(name, gb, n)\
  728. }
  729. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
  730. {\
  731. int n, index, nb_bits;\
  732. \
  733. index= SHOW_UBITS(name, gb, bits);\
  734. level = table[index].level;\
  735. n = table[index].len;\
  736. \
  737. if(max_depth > 1 && n < 0){\
  738. SKIP_BITS(name, gb, bits)\
  739. if(need_update){\
  740. UPDATE_CACHE(name, gb)\
  741. }\
  742. \
  743. nb_bits = -n;\
  744. \
  745. index= SHOW_UBITS(name, gb, nb_bits) + level;\
  746. level = table[index].level;\
  747. n = table[index].len;\
  748. }\
  749. run= table[index].run;\
  750. SKIP_BITS(name, gb, n)\
  751. }
  752. /**
  753. * parses a vlc code, faster then get_vlc()
  754. * @param bits is the number of bits which will be read at once, must be
  755. * identical to nb_bits in init_vlc()
  756. * @param max_depth is the number of times bits bits must be readed to completly
  757. * read the longest vlc code
  758. * = (max_vlc_length + bits - 1) / bits
  759. */
  760. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  761. int bits, int max_depth)
  762. {
  763. int code;
  764. OPEN_READER(re, s)
  765. UPDATE_CACHE(re, s)
  766. GET_VLC(code, re, s, table, bits, max_depth)
  767. CLOSE_READER(re, s)
  768. return code;
  769. }
  770. //#define TRACE
  771. #ifdef TRACE
  772. static inline void print_bin(int bits, int n){
  773. int i;
  774. for(i=n-1; i>=0; i--){
  775. av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
  776. }
  777. for(i=n; i<24; i++)
  778. av_log(NULL, AV_LOG_DEBUG, " ");
  779. }
  780. static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  781. int r= get_bits(s, n);
  782. print_bin(r, n);
  783. 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);
  784. return r;
  785. }
  786. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
  787. int show= show_bits(s, 24);
  788. int pos= get_bits_count(s);
  789. int r= get_vlc2(s, table, bits, max_depth);
  790. int len= get_bits_count(s) - pos;
  791. int bits2= show>>(24-len);
  792. print_bin(bits2, len);
  793. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
  794. return r;
  795. }
  796. static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  797. int show= show_bits(s, n);
  798. int r= get_xbits(s, n);
  799. print_bin(show, n);
  800. 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);
  801. return r;
  802. }
  803. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  804. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  805. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  806. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  807. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  808. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  809. #else //TRACE
  810. #define tprintf(p, ...) {}
  811. #endif
  812. static inline int decode012(GetBitContext *gb){
  813. int n;
  814. n = get_bits1(gb);
  815. if (n == 0)
  816. return 0;
  817. else
  818. return get_bits1(gb) + 1;
  819. }
  820. #endif /* BITSTREAM_H */