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.

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