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.

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