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.

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