wmabitstream.h 26 KB

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