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.

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