You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

957 lines
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. * If \p n is 0 the behavior is undefined.
  307. */
  308. static inline void skip_put_bits(PutBitContext *s, int n){
  309. #ifdef ALT_BITSTREAM_WRITER
  310. s->index += n;
  311. #else
  312. s->bit_left -= n;
  313. s->buf_ptr-= s->bit_left>>5;
  314. s->bit_left &= 31;
  315. #endif
  316. }
  317. /**
  318. * Changes the end of the buffer.
  319. */
  320. static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
  321. s->buf_end= s->buf + size;
  322. }
  323. /* Bitstream reader API docs:
  324. name
  325. arbitrary name which is used as prefix for the internal variables
  326. gb
  327. getbitcontext
  328. OPEN_READER(name, gb)
  329. loads gb into local variables
  330. CLOSE_READER(name, gb)
  331. stores local vars in gb
  332. UPDATE_CACHE(name, gb)
  333. refills the internal cache from the bitstream
  334. after this call at least MIN_CACHE_BITS will be available,
  335. GET_CACHE(name, gb)
  336. will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
  337. SHOW_UBITS(name, gb, num)
  338. will return the next num bits
  339. SHOW_SBITS(name, gb, num)
  340. will return the next num bits and do sign extension
  341. SKIP_BITS(name, gb, num)
  342. will skip over the next num bits
  343. note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
  344. SKIP_CACHE(name, gb, num)
  345. will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
  346. SKIP_COUNTER(name, gb, num)
  347. will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
  348. LAST_SKIP_CACHE(name, gb, num)
  349. will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
  350. LAST_SKIP_BITS(name, gb, num)
  351. is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
  352. for examples see get_bits, show_bits, skip_bits, get_vlc
  353. */
  354. #ifdef ALT_BITSTREAM_READER
  355. # define MIN_CACHE_BITS 25
  356. # define OPEN_READER(name, gb)\
  357. int name##_index= (gb)->index;\
  358. int name##_cache= 0;\
  359. # define CLOSE_READER(name, gb)\
  360. (gb)->index= name##_index;\
  361. # ifdef ALT_BITSTREAM_READER_LE
  362. # define UPDATE_CACHE(name, gb)\
  363. name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
  364. # define SKIP_CACHE(name, gb, num)\
  365. name##_cache >>= (num);
  366. # else
  367. # define UPDATE_CACHE(name, gb)\
  368. name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
  369. # define SKIP_CACHE(name, gb, num)\
  370. name##_cache <<= (num);
  371. # endif
  372. // FIXME name?
  373. # define SKIP_COUNTER(name, gb, num)\
  374. name##_index += (num);\
  375. # define SKIP_BITS(name, gb, num)\
  376. {\
  377. SKIP_CACHE(name, gb, num)\
  378. SKIP_COUNTER(name, gb, num)\
  379. }\
  380. # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  381. # define LAST_SKIP_CACHE(name, gb, num) ;
  382. # ifdef ALT_BITSTREAM_READER_LE
  383. # define SHOW_UBITS(name, gb, num)\
  384. ((name##_cache) & (NEG_USR32(0xffffffff,num)))
  385. # define SHOW_SBITS(name, gb, num)\
  386. NEG_SSR32((name##_cache)<<(32-(num)), num)
  387. # else
  388. # define SHOW_UBITS(name, gb, num)\
  389. NEG_USR32(name##_cache, num)
  390. # define SHOW_SBITS(name, gb, num)\
  391. NEG_SSR32(name##_cache, num)
  392. # endif
  393. # define GET_CACHE(name, gb)\
  394. ((uint32_t)name##_cache)
  395. static inline int get_bits_count(GetBitContext *s){
  396. return s->index;
  397. }
  398. static inline void skip_bits_long(GetBitContext *s, int n){
  399. s->index += n;
  400. }
  401. #elif defined LIBMPEG2_BITSTREAM_READER
  402. //libmpeg2 like reader
  403. # define MIN_CACHE_BITS 17
  404. # define OPEN_READER(name, gb)\
  405. int name##_bit_count=(gb)->bit_count;\
  406. int name##_cache= (gb)->cache;\
  407. uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  408. # define CLOSE_READER(name, gb)\
  409. (gb)->bit_count= name##_bit_count;\
  410. (gb)->cache= name##_cache;\
  411. (gb)->buffer_ptr= name##_buffer_ptr;\
  412. # define UPDATE_CACHE(name, gb)\
  413. if(name##_bit_count >= 0){\
  414. name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
  415. name##_buffer_ptr+=2;\
  416. name##_bit_count-= 16;\
  417. }\
  418. # define SKIP_CACHE(name, gb, num)\
  419. name##_cache <<= (num);\
  420. # define SKIP_COUNTER(name, gb, num)\
  421. name##_bit_count += (num);\
  422. # define SKIP_BITS(name, gb, num)\
  423. {\
  424. SKIP_CACHE(name, gb, num)\
  425. SKIP_COUNTER(name, gb, num)\
  426. }\
  427. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  428. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  429. # define SHOW_UBITS(name, gb, num)\
  430. NEG_USR32(name##_cache, num)
  431. # define SHOW_SBITS(name, gb, num)\
  432. NEG_SSR32(name##_cache, num)
  433. # define GET_CACHE(name, gb)\
  434. ((uint32_t)name##_cache)
  435. static inline int get_bits_count(GetBitContext *s){
  436. return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
  437. }
  438. static inline void skip_bits_long(GetBitContext *s, int n){
  439. OPEN_READER(re, s)
  440. re_bit_count += n;
  441. re_buffer_ptr += 2*(re_bit_count>>4);
  442. re_bit_count &= 15;
  443. re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
  444. UPDATE_CACHE(re, s)
  445. CLOSE_READER(re, s)
  446. }
  447. #elif defined A32_BITSTREAM_READER
  448. # define MIN_CACHE_BITS 32
  449. # define OPEN_READER(name, gb)\
  450. int name##_bit_count=(gb)->bit_count;\
  451. uint32_t name##_cache0= (gb)->cache0;\
  452. uint32_t name##_cache1= (gb)->cache1;\
  453. uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  454. # define CLOSE_READER(name, gb)\
  455. (gb)->bit_count= name##_bit_count;\
  456. (gb)->cache0= name##_cache0;\
  457. (gb)->cache1= name##_cache1;\
  458. (gb)->buffer_ptr= name##_buffer_ptr;\
  459. # define UPDATE_CACHE(name, gb)\
  460. if(name##_bit_count > 0){\
  461. const uint32_t next= be2me_32( *name##_buffer_ptr );\
  462. name##_cache0 |= NEG_USR32(next,name##_bit_count);\
  463. name##_cache1 |= next<<name##_bit_count;\
  464. name##_buffer_ptr++;\
  465. name##_bit_count-= 32;\
  466. }\
  467. #if ARCH_X86
  468. # define SKIP_CACHE(name, gb, num)\
  469. __asm__(\
  470. "shldl %2, %1, %0 \n\t"\
  471. "shll %2, %1 \n\t"\
  472. : "+r" (name##_cache0), "+r" (name##_cache1)\
  473. : "Ic" ((uint8_t)(num))\
  474. );
  475. #else
  476. # define SKIP_CACHE(name, gb, num)\
  477. name##_cache0 <<= (num);\
  478. name##_cache0 |= NEG_USR32(name##_cache1,num);\
  479. name##_cache1 <<= (num);
  480. #endif
  481. # define SKIP_COUNTER(name, gb, num)\
  482. name##_bit_count += (num);\
  483. # define SKIP_BITS(name, gb, num)\
  484. {\
  485. SKIP_CACHE(name, gb, num)\
  486. SKIP_COUNTER(name, gb, num)\
  487. }\
  488. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  489. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  490. # define SHOW_UBITS(name, gb, num)\
  491. NEG_USR32(name##_cache0, num)
  492. # define SHOW_SBITS(name, gb, num)\
  493. NEG_SSR32(name##_cache0, num)
  494. # define GET_CACHE(name, gb)\
  495. (name##_cache0)
  496. static inline int get_bits_count(GetBitContext *s){
  497. return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
  498. }
  499. static inline void skip_bits_long(GetBitContext *s, int n){
  500. OPEN_READER(re, s)
  501. re_bit_count += n;
  502. re_buffer_ptr += re_bit_count>>5;
  503. re_bit_count &= 31;
  504. re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
  505. re_cache1 = 0;
  506. UPDATE_CACHE(re, s)
  507. CLOSE_READER(re, s)
  508. }
  509. #endif
  510. /**
  511. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  512. * if MSB not set it is negative
  513. * @param n length in bits
  514. * @author BERO
  515. */
  516. static inline int get_xbits(GetBitContext *s, int n){
  517. register int sign;
  518. register int32_t cache;
  519. OPEN_READER(re, s)
  520. UPDATE_CACHE(re, s)
  521. cache = GET_CACHE(re,s);
  522. sign=(~cache)>>31;
  523. LAST_SKIP_BITS(re, s, n)
  524. CLOSE_READER(re, s)
  525. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  526. }
  527. static inline int get_sbits(GetBitContext *s, int n){
  528. register int tmp;
  529. OPEN_READER(re, s)
  530. UPDATE_CACHE(re, s)
  531. tmp= SHOW_SBITS(re, s, n);
  532. LAST_SKIP_BITS(re, s, n)
  533. CLOSE_READER(re, s)
  534. return tmp;
  535. }
  536. /**
  537. * reads 1-17 bits.
  538. * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
  539. */
  540. static inline unsigned int get_bits(GetBitContext *s, int n){
  541. register int tmp;
  542. OPEN_READER(re, s)
  543. UPDATE_CACHE(re, s)
  544. tmp= SHOW_UBITS(re, s, n);
  545. LAST_SKIP_BITS(re, s, n)
  546. CLOSE_READER(re, s)
  547. return tmp;
  548. }
  549. /**
  550. * shows 1-17 bits.
  551. * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
  552. */
  553. static inline unsigned int show_bits(GetBitContext *s, int n){
  554. register int tmp;
  555. OPEN_READER(re, s)
  556. UPDATE_CACHE(re, s)
  557. tmp= SHOW_UBITS(re, s, n);
  558. // CLOSE_READER(re, s)
  559. return tmp;
  560. }
  561. static inline void skip_bits(GetBitContext *s, int n){
  562. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  563. OPEN_READER(re, s)
  564. UPDATE_CACHE(re, s)
  565. LAST_SKIP_BITS(re, s, n)
  566. CLOSE_READER(re, s)
  567. }
  568. static inline unsigned int get_bits1(GetBitContext *s){
  569. #ifdef ALT_BITSTREAM_READER
  570. int index= s->index;
  571. uint8_t result= s->buffer[ index>>3 ];
  572. #ifdef ALT_BITSTREAM_READER_LE
  573. result>>= (index&0x07);
  574. result&= 1;
  575. #else
  576. result<<= (index&0x07);
  577. result>>= 8 - 1;
  578. #endif
  579. index++;
  580. s->index= index;
  581. return result;
  582. #else
  583. return get_bits(s, 1);
  584. #endif
  585. }
  586. static inline unsigned int show_bits1(GetBitContext *s){
  587. return show_bits(s, 1);
  588. }
  589. static inline void skip_bits1(GetBitContext *s){
  590. skip_bits(s, 1);
  591. }
  592. /**
  593. * reads 0-32 bits.
  594. */
  595. static inline unsigned int get_bits_long(GetBitContext *s, int n){
  596. if(n<=17) return get_bits(s, n);
  597. else{
  598. #ifdef ALT_BITSTREAM_READER_LE
  599. int ret= get_bits(s, 16);
  600. return ret | (get_bits(s, n-16) << 16);
  601. #else
  602. int ret= get_bits(s, 16) << (n-16);
  603. return ret | get_bits(s, n-16);
  604. #endif
  605. }
  606. }
  607. /**
  608. * reads 0-32 bits as a signed integer.
  609. */
  610. static inline int get_sbits_long(GetBitContext *s, int n) {
  611. return sign_extend(get_bits_long(s, n), n);
  612. }
  613. /**
  614. * shows 0-32 bits.
  615. */
  616. static inline unsigned int show_bits_long(GetBitContext *s, int n){
  617. if(n<=17) return show_bits(s, n);
  618. else{
  619. GetBitContext gb= *s;
  620. return get_bits_long(&gb, n);
  621. }
  622. }
  623. static inline int check_marker(GetBitContext *s, const char *msg)
  624. {
  625. int bit= get_bits1(s);
  626. if(!bit)
  627. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  628. return bit;
  629. }
  630. /**
  631. * init GetBitContext.
  632. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
  633. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  634. * @param bit_size the size of the buffer in bits
  635. */
  636. static inline void init_get_bits(GetBitContext *s,
  637. const uint8_t *buffer, int bit_size)
  638. {
  639. int buffer_size= (bit_size+7)>>3;
  640. if(buffer_size < 0 || bit_size < 0) {
  641. buffer_size = bit_size = 0;
  642. buffer = NULL;
  643. }
  644. s->buffer= buffer;
  645. s->size_in_bits= bit_size;
  646. s->buffer_end= buffer + buffer_size;
  647. #ifdef ALT_BITSTREAM_READER
  648. s->index=0;
  649. #elif defined LIBMPEG2_BITSTREAM_READER
  650. s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
  651. s->bit_count = 16 + 8*((intptr_t)buffer&1);
  652. skip_bits_long(s, 0);
  653. #elif defined A32_BITSTREAM_READER
  654. s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
  655. s->bit_count = 32 + 8*((intptr_t)buffer&3);
  656. skip_bits_long(s, 0);
  657. #endif
  658. }
  659. static inline void align_get_bits(GetBitContext *s)
  660. {
  661. int n= (-get_bits_count(s)) & 7;
  662. if(n) skip_bits(s, n);
  663. }
  664. #define init_vlc(vlc, nb_bits, nb_codes,\
  665. bits, bits_wrap, bits_size,\
  666. codes, codes_wrap, codes_size,\
  667. flags)\
  668. init_vlc_sparse(vlc, nb_bits, nb_codes,\
  669. bits, bits_wrap, bits_size,\
  670. codes, codes_wrap, codes_size,\
  671. NULL, 0, 0, flags)
  672. int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  673. const void *bits, int bits_wrap, int bits_size,
  674. const void *codes, int codes_wrap, int codes_size,
  675. const void *symbols, int symbols_wrap, int symbols_size,
  676. int flags);
  677. #define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
  678. #define INIT_VLC_LE 2
  679. #define INIT_VLC_USE_NEW_STATIC 4
  680. void free_vlc(VLC *vlc);
  681. #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
  682. {\
  683. static VLC_TYPE table[static_size][2];\
  684. (vlc)->table= table;\
  685. (vlc)->table_allocated= static_size;\
  686. init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
  687. }
  688. /**
  689. *
  690. * if the vlc code is invalid and max_depth=1 than no bits will be removed
  691. * if the vlc code is invalid and max_depth>1 than the number of bits removed
  692. * is undefined
  693. */
  694. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  695. {\
  696. int n, index, nb_bits;\
  697. \
  698. index= SHOW_UBITS(name, gb, bits);\
  699. code = table[index][0];\
  700. n = table[index][1];\
  701. \
  702. if(max_depth > 1 && n < 0){\
  703. LAST_SKIP_BITS(name, gb, bits)\
  704. UPDATE_CACHE(name, gb)\
  705. \
  706. nb_bits = -n;\
  707. \
  708. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  709. code = table[index][0];\
  710. n = table[index][1];\
  711. if(max_depth > 2 && n < 0){\
  712. LAST_SKIP_BITS(name, gb, nb_bits)\
  713. UPDATE_CACHE(name, gb)\
  714. \
  715. nb_bits = -n;\
  716. \
  717. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  718. code = table[index][0];\
  719. n = table[index][1];\
  720. }\
  721. }\
  722. SKIP_BITS(name, gb, n)\
  723. }
  724. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
  725. {\
  726. int n, index, nb_bits;\
  727. \
  728. index= SHOW_UBITS(name, gb, bits);\
  729. level = table[index].level;\
  730. n = table[index].len;\
  731. \
  732. if(max_depth > 1 && n < 0){\
  733. SKIP_BITS(name, gb, bits)\
  734. if(need_update){\
  735. UPDATE_CACHE(name, gb)\
  736. }\
  737. \
  738. nb_bits = -n;\
  739. \
  740. index= SHOW_UBITS(name, gb, nb_bits) + level;\
  741. level = table[index].level;\
  742. n = table[index].len;\
  743. }\
  744. run= table[index].run;\
  745. SKIP_BITS(name, gb, n)\
  746. }
  747. /**
  748. * parses a vlc code, faster then get_vlc()
  749. * @param bits is the number of bits which will be read at once, must be
  750. * identical to nb_bits in init_vlc()
  751. * @param max_depth is the number of times bits bits must be read to completely
  752. * read the longest vlc code
  753. * = (max_vlc_length + bits - 1) / bits
  754. */
  755. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  756. int bits, int max_depth)
  757. {
  758. int code;
  759. OPEN_READER(re, s)
  760. UPDATE_CACHE(re, s)
  761. GET_VLC(code, re, s, table, bits, max_depth)
  762. CLOSE_READER(re, s)
  763. return code;
  764. }
  765. //#define TRACE
  766. #ifdef TRACE
  767. static inline void print_bin(int bits, int n){
  768. int i;
  769. for(i=n-1; i>=0; i--){
  770. av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
  771. }
  772. for(i=n; i<24; i++)
  773. av_log(NULL, AV_LOG_DEBUG, " ");
  774. }
  775. static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  776. int r= get_bits(s, n);
  777. print_bin(r, n);
  778. 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);
  779. return r;
  780. }
  781. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
  782. int show= show_bits(s, 24);
  783. int pos= get_bits_count(s);
  784. int r= get_vlc2(s, table, bits, max_depth);
  785. int len= get_bits_count(s) - pos;
  786. int bits2= show>>(24-len);
  787. print_bin(bits2, len);
  788. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
  789. return r;
  790. }
  791. static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  792. int show= show_bits(s, n);
  793. int r= get_xbits(s, n);
  794. print_bin(show, n);
  795. 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);
  796. return r;
  797. }
  798. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  799. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  800. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  801. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  802. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  803. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  804. #else //TRACE
  805. #define tprintf(p, ...) {}
  806. #endif
  807. static inline int decode012(GetBitContext *gb){
  808. int n;
  809. n = get_bits1(gb);
  810. if (n == 0)
  811. return 0;
  812. else
  813. return get_bits1(gb) + 1;
  814. }
  815. static inline int decode210(GetBitContext *gb){
  816. if (get_bits1(gb))
  817. return 0;
  818. else
  819. return 2 - get_bits1(gb);
  820. }
  821. #endif /* AVCODEC_BITSTREAM_H */