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