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.

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