bitstream.h 25 KB

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