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.

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