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.

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