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.

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