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.

973 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 BITSTREAM_H
  25. #define BITSTREAM_H
  26. #include <stdint.h>
  27. #include <stdlib.h>
  28. #include <assert.h>
  29. #include "common.h"
  30. #include "bswap.h"
  31. #include "log.h"
  32. #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
  33. # define ALT_BITSTREAM_READER
  34. #endif
  35. //#define ALT_BITSTREAM_WRITER
  36. //#define ALIGNED_BITSTREAM_WRITER
  37. #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
  38. # ifdef ARCH_ARMV4L
  39. # define A32_BITSTREAM_READER
  40. # else
  41. # define ALT_BITSTREAM_READER
  42. //#define LIBMPEG2_BITSTREAM_READER
  43. //#define A32_BITSTREAM_READER
  44. # endif
  45. #endif
  46. #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
  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, char *s, int put_zero);
  126. void ff_copy_bits(PutBitContext *pb, 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. /* used to avoid misaligned exceptions on some archs (alpha, ...) */
  160. #if defined(ARCH_X86)
  161. # define unaligned16(a) (*(const uint16_t*)(a))
  162. # define unaligned32(a) (*(const uint32_t*)(a))
  163. # define unaligned64(a) (*(const uint64_t*)(a))
  164. #else
  165. # ifdef __GNUC__
  166. # define unaligned(x) \
  167. static inline uint##x##_t unaligned##x(const void *v) { \
  168. struct Unaligned { \
  169. uint##x##_t i; \
  170. } __attribute__((packed)); \
  171. \
  172. return ((const struct Unaligned *) v)->i; \
  173. }
  174. # elif defined(__DECC)
  175. # define unaligned(x) \
  176. static inline uint##x##_t unaligned##x(const void *v) { \
  177. return *(const __unaligned uint##x##_t *) v; \
  178. }
  179. # else
  180. # define unaligned(x) \
  181. static inline uint##x##_t unaligned##x(const void *v) { \
  182. return *(const uint##x##_t *) v; \
  183. }
  184. # endif
  185. unaligned(16)
  186. unaligned(32)
  187. unaligned(64)
  188. #undef unaligned
  189. #endif /* defined(ARCH_X86) */
  190. #ifndef ALT_BITSTREAM_WRITER
  191. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  192. {
  193. unsigned int bit_buf;
  194. int bit_left;
  195. // printf("put_bits=%d %x\n", n, value);
  196. assert(n == 32 || value < (1U << n));
  197. bit_buf = s->bit_buf;
  198. bit_left = s->bit_left;
  199. // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  200. /* XXX: optimize */
  201. if (n < bit_left) {
  202. bit_buf = (bit_buf<<n) | value;
  203. bit_left-=n;
  204. } else {
  205. bit_buf<<=bit_left;
  206. bit_buf |= value >> (n - bit_left);
  207. #ifdef UNALIGNED_STORES_ARE_BAD
  208. if (3 & (intptr_t) s->buf_ptr) {
  209. s->buf_ptr[0] = bit_buf >> 24;
  210. s->buf_ptr[1] = bit_buf >> 16;
  211. s->buf_ptr[2] = bit_buf >> 8;
  212. s->buf_ptr[3] = bit_buf ;
  213. } else
  214. #endif
  215. *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
  216. //printf("bitbuf = %08x\n", bit_buf);
  217. s->buf_ptr+=4;
  218. bit_left+=32 - n;
  219. bit_buf = value;
  220. }
  221. s->bit_buf = bit_buf;
  222. s->bit_left = bit_left;
  223. }
  224. #endif
  225. #ifdef ALT_BITSTREAM_WRITER
  226. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  227. {
  228. # ifdef ALIGNED_BITSTREAM_WRITER
  229. # if defined(ARCH_X86)
  230. asm volatile(
  231. "movl %0, %%ecx \n\t"
  232. "xorl %%eax, %%eax \n\t"
  233. "shrdl %%cl, %1, %%eax \n\t"
  234. "shrl %%cl, %1 \n\t"
  235. "movl %0, %%ecx \n\t"
  236. "shrl $3, %%ecx \n\t"
  237. "andl $0xFFFFFFFC, %%ecx \n\t"
  238. "bswapl %1 \n\t"
  239. "orl %1, (%2, %%ecx) \n\t"
  240. "bswapl %%eax \n\t"
  241. "addl %3, %0 \n\t"
  242. "movl %%eax, 4(%2, %%ecx) \n\t"
  243. : "=&r" (s->index), "=&r" (value)
  244. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
  245. : "%eax", "%ecx"
  246. );
  247. # else
  248. int index= s->index;
  249. uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
  250. value<<= 32-n;
  251. ptr[0] |= be2me_32(value>>(index&31));
  252. ptr[1] = be2me_32(value<<(32-(index&31)));
  253. //if(n>24) printf("%d %d\n", n, value);
  254. index+= n;
  255. s->index= index;
  256. # endif
  257. # else //ALIGNED_BITSTREAM_WRITER
  258. # if defined(ARCH_X86)
  259. asm volatile(
  260. "movl $7, %%ecx \n\t"
  261. "andl %0, %%ecx \n\t"
  262. "addl %3, %%ecx \n\t"
  263. "negl %%ecx \n\t"
  264. "shll %%cl, %1 \n\t"
  265. "bswapl %1 \n\t"
  266. "movl %0, %%ecx \n\t"
  267. "shrl $3, %%ecx \n\t"
  268. "orl %1, (%%ecx, %2) \n\t"
  269. "addl %3, %0 \n\t"
  270. "movl $0, 4(%%ecx, %2) \n\t"
  271. : "=&r" (s->index), "=&r" (value)
  272. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
  273. : "%ecx"
  274. );
  275. # else
  276. int index= s->index;
  277. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  278. ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
  279. ptr[1] = 0;
  280. //if(n>24) printf("%d %d\n", n, value);
  281. index+= n;
  282. s->index= index;
  283. # endif
  284. # endif //!ALIGNED_BITSTREAM_WRITER
  285. }
  286. #endif
  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. abritary 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. static inline int unaligned32_be(const void *v)
  360. {
  361. #ifdef CONFIG_ALIGN
  362. const uint8_t *p=v;
  363. return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
  364. #else
  365. return be2me_32( unaligned32(v)); //original
  366. #endif
  367. }
  368. static inline int unaligned32_le(const void *v)
  369. {
  370. #ifdef CONFIG_ALIGN
  371. const uint8_t *p=v;
  372. return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
  373. #else
  374. return le2me_32( unaligned32(v)); //original
  375. #endif
  376. }
  377. #ifdef ALT_BITSTREAM_READER
  378. # define MIN_CACHE_BITS 25
  379. # define OPEN_READER(name, gb)\
  380. int name##_index= (gb)->index;\
  381. int name##_cache= 0;\
  382. # define CLOSE_READER(name, gb)\
  383. (gb)->index= name##_index;\
  384. # ifdef ALT_BITSTREAM_READER_LE
  385. # define UPDATE_CACHE(name, gb)\
  386. name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
  387. # define SKIP_CACHE(name, gb, num)\
  388. name##_cache >>= (num);
  389. # else
  390. # define UPDATE_CACHE(name, gb)\
  391. name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
  392. # define SKIP_CACHE(name, gb, num)\
  393. name##_cache <<= (num);
  394. # endif
  395. // FIXME name?
  396. # define SKIP_COUNTER(name, gb, num)\
  397. name##_index += (num);\
  398. # define SKIP_BITS(name, gb, num)\
  399. {\
  400. SKIP_CACHE(name, gb, num)\
  401. SKIP_COUNTER(name, gb, num)\
  402. }\
  403. # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  404. # define LAST_SKIP_CACHE(name, gb, num) ;
  405. # ifdef ALT_BITSTREAM_READER_LE
  406. # define SHOW_UBITS(name, gb, num)\
  407. ((name##_cache) & (NEG_USR32(0xffffffff,num)))
  408. # define SHOW_SBITS(name, gb, num)\
  409. NEG_SSR32((name##_cache)<<(32-(num)), num)
  410. # else
  411. # define SHOW_UBITS(name, gb, num)\
  412. NEG_USR32(name##_cache, num)
  413. # define SHOW_SBITS(name, gb, num)\
  414. NEG_SSR32(name##_cache, num)
  415. # endif
  416. # define GET_CACHE(name, gb)\
  417. ((uint32_t)name##_cache)
  418. static inline int get_bits_count(GetBitContext *s){
  419. return s->index;
  420. }
  421. static inline void skip_bits_long(GetBitContext *s, int n){
  422. s->index += n;
  423. }
  424. #elif defined LIBMPEG2_BITSTREAM_READER
  425. //libmpeg2 like reader
  426. # define MIN_CACHE_BITS 17
  427. # define OPEN_READER(name, gb)\
  428. int name##_bit_count=(gb)->bit_count;\
  429. int name##_cache= (gb)->cache;\
  430. uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  431. # define CLOSE_READER(name, gb)\
  432. (gb)->bit_count= name##_bit_count;\
  433. (gb)->cache= name##_cache;\
  434. (gb)->buffer_ptr= name##_buffer_ptr;\
  435. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  436. # define UPDATE_CACHE(name, gb)\
  437. if(name##_bit_count >= 0){\
  438. name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
  439. name##_buffer_ptr += 2;\
  440. name##_bit_count-= 16;\
  441. }\
  442. #else
  443. # define UPDATE_CACHE(name, gb)\
  444. if(name##_bit_count >= 0){\
  445. name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
  446. name##_buffer_ptr+=2;\
  447. name##_bit_count-= 16;\
  448. }\
  449. #endif
  450. # define SKIP_CACHE(name, gb, num)\
  451. name##_cache <<= (num);\
  452. # define SKIP_COUNTER(name, gb, num)\
  453. name##_bit_count += (num);\
  454. # define SKIP_BITS(name, gb, num)\
  455. {\
  456. SKIP_CACHE(name, gb, num)\
  457. SKIP_COUNTER(name, gb, num)\
  458. }\
  459. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  460. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  461. # define SHOW_UBITS(name, gb, num)\
  462. NEG_USR32(name##_cache, num)
  463. # define SHOW_SBITS(name, gb, num)\
  464. NEG_SSR32(name##_cache, num)
  465. # define GET_CACHE(name, gb)\
  466. ((uint32_t)name##_cache)
  467. static inline int get_bits_count(GetBitContext *s){
  468. return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
  469. }
  470. static inline void skip_bits_long(GetBitContext *s, int n){
  471. OPEN_READER(re, s)
  472. re_bit_count += n;
  473. re_buffer_ptr += 2*(re_bit_count>>4);
  474. re_bit_count &= 15;
  475. re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
  476. UPDATE_CACHE(re, s)
  477. CLOSE_READER(re, s)
  478. }
  479. #elif defined A32_BITSTREAM_READER
  480. # define MIN_CACHE_BITS 32
  481. # define OPEN_READER(name, gb)\
  482. int name##_bit_count=(gb)->bit_count;\
  483. uint32_t name##_cache0= (gb)->cache0;\
  484. uint32_t name##_cache1= (gb)->cache1;\
  485. uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
  486. # define CLOSE_READER(name, gb)\
  487. (gb)->bit_count= name##_bit_count;\
  488. (gb)->cache0= name##_cache0;\
  489. (gb)->cache1= name##_cache1;\
  490. (gb)->buffer_ptr= name##_buffer_ptr;\
  491. # define UPDATE_CACHE(name, gb)\
  492. if(name##_bit_count > 0){\
  493. const uint32_t next= be2me_32( *name##_buffer_ptr );\
  494. name##_cache0 |= NEG_USR32(next,name##_bit_count);\
  495. name##_cache1 |= next<<name##_bit_count;\
  496. name##_buffer_ptr++;\
  497. name##_bit_count-= 32;\
  498. }\
  499. #if defined(ARCH_X86)
  500. # define SKIP_CACHE(name, gb, num)\
  501. asm(\
  502. "shldl %2, %1, %0 \n\t"\
  503. "shll %2, %1 \n\t"\
  504. : "+r" (name##_cache0), "+r" (name##_cache1)\
  505. : "Ic" ((uint8_t)(num))\
  506. );
  507. #else
  508. # define SKIP_CACHE(name, gb, num)\
  509. name##_cache0 <<= (num);\
  510. name##_cache0 |= NEG_USR32(name##_cache1,num);\
  511. name##_cache1 <<= (num);
  512. #endif
  513. # define SKIP_COUNTER(name, gb, num)\
  514. name##_bit_count += (num);\
  515. # define SKIP_BITS(name, gb, num)\
  516. {\
  517. SKIP_CACHE(name, gb, num)\
  518. SKIP_COUNTER(name, gb, num)\
  519. }\
  520. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  521. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  522. # define SHOW_UBITS(name, gb, num)\
  523. NEG_USR32(name##_cache0, num)
  524. # define SHOW_SBITS(name, gb, num)\
  525. NEG_SSR32(name##_cache0, num)
  526. # define GET_CACHE(name, gb)\
  527. (name##_cache0)
  528. static inline int get_bits_count(GetBitContext *s){
  529. return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
  530. }
  531. static inline void skip_bits_long(GetBitContext *s, int n){
  532. OPEN_READER(re, s)
  533. re_bit_count += n;
  534. re_buffer_ptr += re_bit_count>>5;
  535. re_bit_count &= 31;
  536. re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
  537. re_cache1 = 0;
  538. UPDATE_CACHE(re, s)
  539. CLOSE_READER(re, s)
  540. }
  541. #endif
  542. /**
  543. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  544. * if MSB not set it is negative
  545. * @param n length in bits
  546. * @author BERO
  547. */
  548. static inline int get_xbits(GetBitContext *s, int n){
  549. register int sign;
  550. register int32_t cache;
  551. OPEN_READER(re, s)
  552. UPDATE_CACHE(re, s)
  553. cache = GET_CACHE(re,s);
  554. sign=(~cache)>>31;
  555. LAST_SKIP_BITS(re, s, n)
  556. CLOSE_READER(re, s)
  557. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  558. }
  559. static inline int get_sbits(GetBitContext *s, int n){
  560. register int tmp;
  561. OPEN_READER(re, s)
  562. UPDATE_CACHE(re, s)
  563. tmp= SHOW_SBITS(re, s, n);
  564. LAST_SKIP_BITS(re, s, n)
  565. CLOSE_READER(re, s)
  566. return tmp;
  567. }
  568. /**
  569. * reads 1-17 bits.
  570. * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
  571. */
  572. static inline unsigned int get_bits(GetBitContext *s, int n){
  573. register int tmp;
  574. OPEN_READER(re, s)
  575. UPDATE_CACHE(re, s)
  576. tmp= SHOW_UBITS(re, s, n);
  577. LAST_SKIP_BITS(re, s, n)
  578. CLOSE_READER(re, s)
  579. return tmp;
  580. }
  581. /**
  582. * shows 1-17 bits.
  583. * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
  584. */
  585. static inline unsigned int show_bits(GetBitContext *s, int n){
  586. register int tmp;
  587. OPEN_READER(re, s)
  588. UPDATE_CACHE(re, s)
  589. tmp= SHOW_UBITS(re, s, n);
  590. // CLOSE_READER(re, s)
  591. return tmp;
  592. }
  593. static inline void skip_bits(GetBitContext *s, int n){
  594. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  595. OPEN_READER(re, s)
  596. UPDATE_CACHE(re, s)
  597. LAST_SKIP_BITS(re, s, n)
  598. CLOSE_READER(re, s)
  599. }
  600. static inline unsigned int get_bits1(GetBitContext *s){
  601. #ifdef ALT_BITSTREAM_READER
  602. int index= s->index;
  603. uint8_t result= s->buffer[ index>>3 ];
  604. #ifdef ALT_BITSTREAM_READER_LE
  605. result>>= (index&0x07);
  606. result&= 1;
  607. #else
  608. result<<= (index&0x07);
  609. result>>= 8 - 1;
  610. #endif
  611. index++;
  612. s->index= index;
  613. return result;
  614. #else
  615. return get_bits(s, 1);
  616. #endif
  617. }
  618. static inline unsigned int show_bits1(GetBitContext *s){
  619. return show_bits(s, 1);
  620. }
  621. static inline void skip_bits1(GetBitContext *s){
  622. skip_bits(s, 1);
  623. }
  624. /**
  625. * reads 0-32 bits.
  626. */
  627. static inline unsigned int get_bits_long(GetBitContext *s, int n){
  628. if(n<=17) return get_bits(s, n);
  629. else{
  630. #ifdef ALT_BITSTREAM_READER_LE
  631. int ret= get_bits(s, 16);
  632. return ret | (get_bits(s, n-16) << 16);
  633. #else
  634. int ret= get_bits(s, 16) << (n-16);
  635. return ret | get_bits(s, n-16);
  636. #endif
  637. }
  638. }
  639. /**
  640. * shows 0-32 bits.
  641. */
  642. static inline unsigned int show_bits_long(GetBitContext *s, int n){
  643. if(n<=17) return show_bits(s, n);
  644. else{
  645. GetBitContext gb= *s;
  646. int ret= get_bits_long(s, n);
  647. *s= gb;
  648. return ret;
  649. }
  650. }
  651. static inline int check_marker(GetBitContext *s, const char *msg)
  652. {
  653. int bit= get_bits1(s);
  654. if(!bit)
  655. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  656. return bit;
  657. }
  658. /**
  659. * init GetBitContext.
  660. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
  661. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  662. * @param bit_size the size of the buffer in bits
  663. */
  664. static inline void init_get_bits(GetBitContext *s,
  665. const uint8_t *buffer, int bit_size)
  666. {
  667. int buffer_size= (bit_size+7)>>3;
  668. if(buffer_size < 0 || bit_size < 0) {
  669. buffer_size = bit_size = 0;
  670. buffer = NULL;
  671. }
  672. s->buffer= buffer;
  673. s->size_in_bits= bit_size;
  674. s->buffer_end= buffer + buffer_size;
  675. #ifdef ALT_BITSTREAM_READER
  676. s->index=0;
  677. #elif defined LIBMPEG2_BITSTREAM_READER
  678. s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
  679. s->bit_count = 16 + 8*((intptr_t)buffer&1);
  680. skip_bits_long(s, 0);
  681. #elif defined A32_BITSTREAM_READER
  682. s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
  683. s->bit_count = 32 + 8*((intptr_t)buffer&3);
  684. skip_bits_long(s, 0);
  685. #endif
  686. }
  687. static inline void align_get_bits(GetBitContext *s)
  688. {
  689. int n= (-get_bits_count(s)) & 7;
  690. if(n) skip_bits(s, n);
  691. }
  692. #define init_vlc(vlc, nb_bits, nb_codes,\
  693. bits, bits_wrap, bits_size,\
  694. codes, codes_wrap, codes_size,\
  695. flags)\
  696. init_vlc_sparse(vlc, nb_bits, nb_codes,\
  697. bits, bits_wrap, bits_size,\
  698. codes, codes_wrap, codes_size,\
  699. NULL, 0, 0, flags)
  700. int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  701. const void *bits, int bits_wrap, int bits_size,
  702. const void *codes, int codes_wrap, int codes_size,
  703. const void *symbols, int symbols_wrap, int symbols_size,
  704. int flags);
  705. #define INIT_VLC_USE_STATIC 1
  706. #define INIT_VLC_LE 2
  707. void free_vlc(VLC *vlc);
  708. /**
  709. *
  710. * if the vlc code is invalid and max_depth=1 than no bits will be removed
  711. * if the vlc code is invalid and max_depth>1 than the number of bits removed
  712. * is undefined
  713. */
  714. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  715. {\
  716. int n, index, nb_bits;\
  717. \
  718. index= SHOW_UBITS(name, gb, bits);\
  719. code = table[index][0];\
  720. n = table[index][1];\
  721. \
  722. if(max_depth > 1 && n < 0){\
  723. LAST_SKIP_BITS(name, gb, bits)\
  724. UPDATE_CACHE(name, gb)\
  725. \
  726. nb_bits = -n;\
  727. \
  728. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  729. code = table[index][0];\
  730. n = table[index][1];\
  731. if(max_depth > 2 && n < 0){\
  732. LAST_SKIP_BITS(name, gb, nb_bits)\
  733. UPDATE_CACHE(name, gb)\
  734. \
  735. nb_bits = -n;\
  736. \
  737. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  738. code = table[index][0];\
  739. n = table[index][1];\
  740. }\
  741. }\
  742. SKIP_BITS(name, gb, n)\
  743. }
  744. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
  745. {\
  746. int n, index, nb_bits;\
  747. \
  748. index= SHOW_UBITS(name, gb, bits);\
  749. level = table[index].level;\
  750. n = table[index].len;\
  751. \
  752. if(max_depth > 1 && n < 0){\
  753. SKIP_BITS(name, gb, bits)\
  754. if(need_update){\
  755. UPDATE_CACHE(name, gb)\
  756. }\
  757. \
  758. nb_bits = -n;\
  759. \
  760. index= SHOW_UBITS(name, gb, nb_bits) + level;\
  761. level = table[index].level;\
  762. n = table[index].len;\
  763. }\
  764. run= table[index].run;\
  765. SKIP_BITS(name, gb, n)\
  766. }
  767. /**
  768. * parses a vlc code, faster then get_vlc()
  769. * @param bits is the number of bits which will be read at once, must be
  770. * identical to nb_bits in init_vlc()
  771. * @param max_depth is the number of times bits bits must be read to completely
  772. * read the longest vlc code
  773. * = (max_vlc_length + bits - 1) / bits
  774. */
  775. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  776. int bits, int max_depth)
  777. {
  778. int code;
  779. OPEN_READER(re, s)
  780. UPDATE_CACHE(re, s)
  781. GET_VLC(code, re, s, table, bits, max_depth)
  782. CLOSE_READER(re, s)
  783. return code;
  784. }
  785. //#define TRACE
  786. #ifdef TRACE
  787. static inline void print_bin(int bits, int n){
  788. int i;
  789. for(i=n-1; i>=0; i--){
  790. av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
  791. }
  792. for(i=n; i<24; i++)
  793. av_log(NULL, AV_LOG_DEBUG, " ");
  794. }
  795. static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  796. int r= get_bits(s, n);
  797. print_bin(r, n);
  798. 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);
  799. return r;
  800. }
  801. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
  802. int show= show_bits(s, 24);
  803. int pos= get_bits_count(s);
  804. int r= get_vlc2(s, table, bits, max_depth);
  805. int len= get_bits_count(s) - pos;
  806. int bits2= show>>(24-len);
  807. print_bin(bits2, len);
  808. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
  809. return r;
  810. }
  811. static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
  812. int show= show_bits(s, n);
  813. int r= get_xbits(s, n);
  814. print_bin(show, n);
  815. 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);
  816. return r;
  817. }
  818. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  819. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  820. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  821. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  822. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  823. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  824. #else //TRACE
  825. #define tprintf(p, ...) {}
  826. #endif
  827. static inline int decode012(GetBitContext *gb){
  828. int n;
  829. n = get_bits1(gb);
  830. if (n == 0)
  831. return 0;
  832. else
  833. return get_bits1(gb) + 1;
  834. }
  835. #endif /* BITSTREAM_H */