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.

954 lines
25KB

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