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.

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