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.

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