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.

948 lines
25KB

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