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.

793 lines
17KB

  1. #ifndef COMMON_H
  2. #define COMMON_H
  3. #define FFMPEG_VERSION_INT 0x000406
  4. #define FFMPEG_VERSION "0.4.6"
  5. #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  6. #define CONFIG_WIN32
  7. #endif
  8. //#define ALT_BITSTREAM_WRITER
  9. //#define ALIGNED_BITSTREAM_WRITER
  10. //#define ALT_BITSTREAM_READER
  11. //#define ALIGNED_BITSTREAM
  12. #define FAST_GET_FIRST_VLC
  13. #ifdef HAVE_AV_CONFIG_H
  14. /* only include the following when compiling package */
  15. #include "../config.h"
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #ifndef ENODATA
  21. #define ENODATA 61
  22. #endif
  23. #endif
  24. #ifdef CONFIG_WIN32
  25. /* windows */
  26. typedef unsigned short UINT16;
  27. typedef signed short INT16;
  28. typedef unsigned char UINT8;
  29. typedef unsigned int UINT32;
  30. typedef unsigned __int64 UINT64;
  31. typedef signed char INT8;
  32. typedef signed int INT32;
  33. typedef signed __int64 INT64;
  34. typedef UINT8 uint8_t;
  35. typedef INT8 int8_t;
  36. typedef UINT16 uint16_t;
  37. typedef INT16 int16_t;
  38. typedef UINT32 uint32_t;
  39. typedef INT32 int32_t;
  40. #ifndef __MINGW32__
  41. #define INT64_C(c) (c ## i64)
  42. #define UINT64_C(c) (c ## i64)
  43. #define inline __inline
  44. /*
  45. Disable warning messages:
  46. warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
  47. warning C4305: 'argument' : truncation from 'const double' to 'float'
  48. */
  49. #pragma warning( disable : 4244 )
  50. #pragma warning( disable : 4305 )
  51. #else
  52. #define INT64_C(c) (c ## LL)
  53. #define UINT64_C(c) (c ## ULL)
  54. #endif /* __MINGW32__ */
  55. #define M_PI 3.14159265358979323846
  56. #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  57. #ifdef _DEBUG
  58. #define DEBUG
  59. #endif
  60. // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
  61. #define bswap_32(x) \
  62. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  63. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  64. #define be2me_32(x) bswap_32(x)
  65. #define snprintf _snprintf
  66. #ifndef __MINGW32__
  67. /* no config.h with VC */
  68. #define CONFIG_ENCODERS 1
  69. #define CONFIG_DECODERS 1
  70. #define CONFIG_AC3 1
  71. #endif
  72. #else
  73. /* unix */
  74. #include <inttypes.h>
  75. #ifndef __WINE_WINDEF16_H
  76. /* workaround for typedef conflict in MPlayer (wine typedefs) */
  77. typedef unsigned short UINT16;
  78. typedef signed short INT16;
  79. #endif
  80. typedef unsigned char UINT8;
  81. typedef unsigned int UINT32;
  82. typedef unsigned long long UINT64;
  83. typedef signed char INT8;
  84. typedef signed int INT32;
  85. typedef signed long long INT64;
  86. #ifdef HAVE_AV_CONFIG_H
  87. #ifdef __FreeBSD__
  88. #include <sys/param.h>
  89. #endif
  90. #ifndef INT64_C
  91. #define INT64_C(c) (c ## LL)
  92. #define UINT64_C(c) (c ## ULL)
  93. #endif
  94. #include "../bswap.h"
  95. #ifdef USE_FASTMEMCPY
  96. #include "fastmemcpy.h"
  97. #endif
  98. #endif /* HAVE_AV_CONFIG_H */
  99. #endif /* !CONFIG_WIN32 */
  100. /* debug stuff */
  101. #ifdef HAVE_AV_CONFIG_H
  102. #ifndef DEBUG
  103. #define NDEBUG
  104. #endif
  105. #include <assert.h>
  106. /* dprintf macros */
  107. #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
  108. inline void dprintf(const char* fmt,...) {}
  109. #else
  110. #ifdef DEBUG
  111. #define dprintf(fmt,args...) printf(fmt, ## args)
  112. #else
  113. #define dprintf(fmt,args...)
  114. #endif
  115. #endif /* !CONFIG_WIN32 */
  116. #endif /* HAVE_AV_CONFIG_H */
  117. /* bit output */
  118. struct PutBitContext;
  119. typedef void (*WriteDataFunc)(void *, UINT8 *, int);
  120. typedef struct PutBitContext {
  121. #ifdef ALT_BITSTREAM_WRITER
  122. UINT8 *buf, *buf_end;
  123. int index;
  124. #else
  125. UINT32 bit_buf;
  126. int bit_left;
  127. UINT8 *buf, *buf_ptr, *buf_end;
  128. #endif
  129. INT64 data_out_size; /* in bytes */
  130. } PutBitContext;
  131. void init_put_bits(PutBitContext *s,
  132. UINT8 *buffer, int buffer_size,
  133. void *opaque,
  134. void (*write_data)(void *, UINT8 *, int));
  135. INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
  136. void align_put_bits(PutBitContext *s);
  137. void flush_put_bits(PutBitContext *s);
  138. /* jpeg specific put_bits */
  139. void jflush_put_bits(PutBitContext *s);
  140. /* bit input */
  141. typedef struct GetBitContext {
  142. #ifdef ALT_BITSTREAM_READER
  143. int index;
  144. UINT8 *buffer;
  145. #else
  146. UINT32 bit_buf;
  147. int bit_cnt;
  148. UINT8 *buf, *buf_ptr, *buf_end;
  149. #endif
  150. } GetBitContext;
  151. typedef struct VLC {
  152. int bits;
  153. INT16 *table_codes;
  154. INT8 *table_bits;
  155. int table_size, table_allocated;
  156. } VLC;
  157. /* used to avoid missaligned exceptions on some archs (alpha, ...) */
  158. #ifdef ARCH_X86
  159. #define unaligned32(a) (*(UINT32*)(a))
  160. #else
  161. #ifdef __GNUC__
  162. static inline uint32_t unaligned32(const void *v) {
  163. struct Unaligned {
  164. uint32_t i;
  165. } __attribute__((packed));
  166. return ((const struct Unaligned *) v)->i;
  167. }
  168. #elif defined(__DECC)
  169. static inline uint32_t unaligned32(const void *v) {
  170. return *(const __unaligned uint32_t *) v;
  171. }
  172. #else
  173. static inline uint32_t unaligned32(const void *v) {
  174. return *(const uint32_t *) v;
  175. }
  176. #endif
  177. #endif //!ARCH_X86
  178. #ifndef ALT_BITSTREAM_WRITER
  179. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  180. {
  181. unsigned int bit_buf;
  182. int bit_left;
  183. #ifdef STATS
  184. st_out_bit_counts[st_current_index] += n;
  185. #endif
  186. // printf("put_bits=%d %x\n", n, value);
  187. assert(n == 32 || value < (1U << n));
  188. bit_buf = s->bit_buf;
  189. bit_left = s->bit_left;
  190. // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  191. /* XXX: optimize */
  192. if (n < bit_left) {
  193. bit_buf = (bit_buf<<n) | value;
  194. bit_left-=n;
  195. } else {
  196. bit_buf<<=bit_left;
  197. bit_buf |= value >> (n - bit_left);
  198. *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
  199. //printf("bitbuf = %08x\n", bit_buf);
  200. s->buf_ptr+=4;
  201. bit_left+=32 - n;
  202. bit_buf = value;
  203. }
  204. s->bit_buf = bit_buf;
  205. s->bit_left = bit_left;
  206. }
  207. #endif
  208. #ifdef ALT_BITSTREAM_WRITER
  209. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  210. {
  211. #ifdef ALIGNED_BITSTREAM_WRITER
  212. #ifdef ARCH_X86
  213. asm volatile(
  214. "movl %0, %%ecx \n\t"
  215. "xorl %%eax, %%eax \n\t"
  216. "shrdl %%cl, %1, %%eax \n\t"
  217. "shrl %%cl, %1 \n\t"
  218. "movl %0, %%ecx \n\t"
  219. "shrl $3, %%ecx \n\t"
  220. "andl $0xFFFFFFFC, %%ecx \n\t"
  221. "bswapl %1 \n\t"
  222. "orl %1, (%2, %%ecx) \n\t"
  223. "bswapl %%eax \n\t"
  224. "addl %3, %0 \n\t"
  225. "movl %%eax, 4(%2, %%ecx) \n\t"
  226. : "=&r" (s->index), "=&r" (value)
  227. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
  228. : "%eax", "%ecx"
  229. );
  230. #else
  231. int index= s->index;
  232. uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
  233. value<<= 32-n;
  234. ptr[0] |= be2me_32(value>>(index&31));
  235. ptr[1] = be2me_32(value<<(32-(index&31)));
  236. //if(n>24) printf("%d %d\n", n, value);
  237. index+= n;
  238. s->index= index;
  239. #endif
  240. #else //ALIGNED_BITSTREAM_WRITER
  241. #ifdef ARCH_X86
  242. asm volatile(
  243. "movl $7, %%ecx \n\t"
  244. "andl %0, %%ecx \n\t"
  245. "addl %3, %%ecx \n\t"
  246. "negl %%ecx \n\t"
  247. "shll %%cl, %1 \n\t"
  248. "bswapl %1 \n\t"
  249. "movl %0, %%ecx \n\t"
  250. "shrl $3, %%ecx \n\t"
  251. "orl %1, (%%ecx, %2) \n\t"
  252. "addl %3, %0 \n\t"
  253. "movl $0, 4(%%ecx, %2) \n\t"
  254. : "=&r" (s->index), "=&r" (value)
  255. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
  256. : "%ecx"
  257. );
  258. #else
  259. int index= s->index;
  260. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  261. ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
  262. ptr[1] = 0;
  263. //if(n>24) printf("%d %d\n", n, value);
  264. index+= n;
  265. s->index= index;
  266. #endif
  267. #endif //!ALIGNED_BITSTREAM_WRITER
  268. }
  269. #endif
  270. #ifndef ALT_BITSTREAM_WRITER
  271. /* for jpeg : escape 0xff with 0x00 after it */
  272. static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
  273. {
  274. unsigned int bit_buf, b;
  275. int bit_left, i;
  276. assert(n == 32 || value < (1U << n));
  277. bit_buf = s->bit_buf;
  278. bit_left = s->bit_left;
  279. //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  280. /* XXX: optimize */
  281. if (n < bit_left) {
  282. bit_buf = (bit_buf<<n) | value;
  283. bit_left-=n;
  284. } else {
  285. bit_buf<<=bit_left;
  286. bit_buf |= value >> (n - bit_left);
  287. /* handle escape */
  288. for(i=0;i<4;i++) {
  289. b = (bit_buf >> 24);
  290. *(s->buf_ptr++) = b;
  291. if (b == 0xff)
  292. *(s->buf_ptr++) = 0;
  293. bit_buf <<= 8;
  294. }
  295. bit_left+= 32 - n;
  296. bit_buf = value;
  297. }
  298. s->bit_buf = bit_buf;
  299. s->bit_left = bit_left;
  300. }
  301. #endif
  302. #ifdef ALT_BITSTREAM_WRITER
  303. static inline void jput_bits(PutBitContext *s, int n, int value)
  304. {
  305. int index= s->index;
  306. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  307. int v= ptr[0];
  308. //if(n>24) printf("%d %d\n", n, value);
  309. v |= be2me_32(value<<(32-n-(index&7) ));
  310. if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
  311. {
  312. /* handle idiotic (m)jpeg escapes */
  313. uint8_t *bPtr= (uint8_t*)ptr;
  314. int numChecked= ((index+n)>>3) - (index>>3);
  315. v= be2me_32(v);
  316. *(bPtr++)= v>>24;
  317. if((v&0xFF000000)==0xFF000000 && numChecked>0){
  318. *(bPtr++)= 0x00;
  319. index+=8;
  320. }
  321. *(bPtr++)= (v>>16)&0xFF;
  322. if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
  323. *(bPtr++)= 0x00;
  324. index+=8;
  325. }
  326. *(bPtr++)= (v>>8)&0xFF;
  327. if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
  328. *(bPtr++)= 0x00;
  329. index+=8;
  330. }
  331. *(bPtr++)= v&0xFF;
  332. if((v&0x000000FF)==0x000000FF && numChecked>3){
  333. *(bPtr++)= 0x00;
  334. index+=8;
  335. }
  336. *((uint32_t*)bPtr)= 0;
  337. }
  338. else
  339. {
  340. ptr[0] = v;
  341. ptr[1] = 0;
  342. }
  343. index+= n;
  344. s->index= index;
  345. }
  346. #endif
  347. static inline uint8_t* pbBufPtr(PutBitContext *s)
  348. {
  349. #ifdef ALT_BITSTREAM_WRITER
  350. return s->buf + (s->index>>3);
  351. #else
  352. return s->buf_ptr;
  353. #endif
  354. }
  355. void init_get_bits(GetBitContext *s,
  356. UINT8 *buffer, int buffer_size);
  357. #ifndef ALT_BITSTREAM_READER
  358. unsigned int get_bits_long(GetBitContext *s, int n);
  359. unsigned int show_bits_long(GetBitContext *s, int n);
  360. #endif
  361. static inline unsigned int get_bits(GetBitContext *s, int n){
  362. #ifdef ALT_BITSTREAM_READER
  363. #ifdef ALIGNED_BITSTREAM
  364. int index= s->index;
  365. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  366. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  367. #ifdef ARCH_X86
  368. asm ("shldl %%cl, %2, %0\n\t"
  369. : "=r" (result1)
  370. : "0" (result1), "r" (result2), "c" (index));
  371. #else
  372. result1<<= (index&0x1F);
  373. result2= (result2>>1) >> (31-(index&0x1F));
  374. result1|= result2;
  375. #endif
  376. result1>>= 32 - n;
  377. index+= n;
  378. s->index= index;
  379. return result1;
  380. #else //ALIGNED_BITSTREAM
  381. int index= s->index;
  382. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  383. result<<= (index&0x07);
  384. result>>= 32 - n;
  385. index+= n;
  386. s->index= index;
  387. return result;
  388. #endif //!ALIGNED_BITSTREAM
  389. #else //ALT_BITSTREAM_READER
  390. if(s->bit_cnt>=n){
  391. /* most common case here */
  392. unsigned int val = s->bit_buf >> (32 - n);
  393. s->bit_buf <<= n;
  394. s->bit_cnt -= n;
  395. #ifdef STATS
  396. st_bit_counts[st_current_index] += n;
  397. #endif
  398. return val;
  399. }
  400. return get_bits_long(s,n);
  401. #endif //!ALT_BITSTREAM_READER
  402. }
  403. static inline unsigned int get_bits1(GetBitContext *s){
  404. #ifdef ALT_BITSTREAM_READER
  405. int index= s->index;
  406. uint8_t result= s->buffer[ index>>3 ];
  407. result<<= (index&0x07);
  408. result>>= 8 - 1;
  409. index++;
  410. s->index= index;
  411. return result;
  412. #else
  413. if(s->bit_cnt>0){
  414. /* most common case here */
  415. unsigned int val = s->bit_buf >> 31;
  416. s->bit_buf <<= 1;
  417. s->bit_cnt--;
  418. #ifdef STATS
  419. st_bit_counts[st_current_index]++;
  420. #endif
  421. return val;
  422. }
  423. return get_bits_long(s,1);
  424. #endif
  425. }
  426. /* This function is identical to get_bits(), the only */
  427. /* diference is that it doesn't touch the buffer */
  428. /* it is usefull to see the buffer. */
  429. static inline unsigned int show_bits(GetBitContext *s, int n)
  430. {
  431. #ifdef ALT_BITSTREAM_READER
  432. #ifdef ALIGNED_BITSTREAM
  433. int index= s->index;
  434. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  435. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  436. #ifdef ARCH_X86
  437. asm ("shldl %%cl, %2, %0\n\t"
  438. : "=r" (result1)
  439. : "0" (result1), "r" (result2), "c" (index));
  440. #else
  441. result1<<= (index&0x1F);
  442. result2= (result2>>1) >> (31-(index&0x1F));
  443. result1|= result2;
  444. #endif
  445. result1>>= 32 - n;
  446. return result1;
  447. #else //ALIGNED_BITSTREAM
  448. int index= s->index;
  449. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  450. result<<= (index&0x07);
  451. result>>= 32 - n;
  452. return result;
  453. #endif //!ALIGNED_BITSTREAM
  454. #else //ALT_BITSTREAM_READER
  455. if(s->bit_cnt>=n) {
  456. /* most common case here */
  457. unsigned int val = s->bit_buf >> (32 - n);
  458. return val;
  459. }
  460. return show_bits_long(s,n);
  461. #endif //!ALT_BITSTREAM_READER
  462. }
  463. static inline void skip_bits(GetBitContext *s, int n){
  464. #ifdef ALT_BITSTREAM_READER
  465. s->index+= n;
  466. #else
  467. if(s->bit_cnt>=n){
  468. /* most common case here */
  469. s->bit_buf <<= n;
  470. s->bit_cnt -= n;
  471. #ifdef STATS
  472. st_bit_counts[st_current_index] += n;
  473. #endif
  474. } else {
  475. get_bits_long(s,n);
  476. }
  477. #endif
  478. }
  479. static inline void skip_bits1(GetBitContext *s){
  480. #ifdef ALT_BITSTREAM_READER
  481. s->index++;
  482. #else
  483. if(s->bit_cnt>0){
  484. /* most common case here */
  485. s->bit_buf <<= 1;
  486. s->bit_cnt--;
  487. #ifdef STATS
  488. st_bit_counts[st_current_index]++;
  489. #endif
  490. } else {
  491. get_bits_long(s,1);
  492. }
  493. #endif
  494. }
  495. static inline int get_bits_count(GetBitContext *s)
  496. {
  497. #ifdef ALT_BITSTREAM_READER
  498. return s->index;
  499. #else
  500. return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
  501. #endif
  502. }
  503. void align_get_bits(GetBitContext *s);
  504. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  505. const void *bits, int bits_wrap, int bits_size,
  506. const void *codes, int codes_wrap, int codes_size);
  507. void free_vlc(VLC *vlc);
  508. #ifdef ALT_BITSTREAM_READER
  509. #ifdef ALIGNED_BITSTREAM
  510. #ifdef ARCH_X86
  511. #define SHOW_BITS(s, val, n) \
  512. val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  513. {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  514. asm ("shldl %%cl, %2, %0\n\t"\
  515. : "=r" (val)\
  516. : "0" (val), "r" (result2), "c" (bit_cnt));\
  517. ((uint32_t)val)>>= 32 - n;}
  518. #else //ARCH_X86
  519. #define SHOW_BITS(s, val, n) \
  520. val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  521. {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  522. val<<= (bit_cnt&0x1F);\
  523. result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
  524. val|= result2;\
  525. ((uint32_t)val)>>= 32 - n;}
  526. #endif //!ARCH_X86
  527. #else //ALIGNED_BITSTREAM
  528. #define SHOW_BITS(s, val, n) \
  529. val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
  530. val<<= (bit_cnt&0x07);\
  531. ((uint32_t)val)>>= 32 - n;
  532. #endif // !ALIGNED_BITSTREAM
  533. #define FLUSH_BITS(n) bit_cnt+=n;
  534. #define SAVE_BITS(s) bit_cnt= (s)->index;
  535. #define RESTORE_BITS(s) (s)->index= bit_cnt;
  536. #else
  537. /* macro to go faster */
  538. /* n must be <= 24 */
  539. /* XXX: optimize buffer end test */
  540. #define SHOW_BITS(s, val, n)\
  541. {\
  542. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  543. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  544. bit_cnt += 8;\
  545. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  546. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  547. bit_cnt += 8;\
  548. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  549. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  550. bit_cnt += 8;\
  551. }\
  552. }\
  553. }\
  554. val = bit_buf >> (32 - n);\
  555. }
  556. /* SHOW_BITS with n1 >= n must be been done before */
  557. #define FLUSH_BITS(n)\
  558. {\
  559. bit_buf <<= n;\
  560. bit_cnt -= n;\
  561. }
  562. #define SAVE_BITS(s) \
  563. {\
  564. bit_cnt = (s)->bit_cnt;\
  565. bit_buf = (s)->bit_buf;\
  566. buf_ptr = (s)->buf_ptr;\
  567. }
  568. #define RESTORE_BITS(s) \
  569. {\
  570. (s)->buf_ptr = buf_ptr;\
  571. (s)->bit_buf = bit_buf;\
  572. (s)->bit_cnt = bit_cnt;\
  573. }
  574. #endif // !ALT_BITSTREAM_READER
  575. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  576. {
  577. int code, n, nb_bits, index;
  578. INT16 *table_codes;
  579. INT8 *table_bits;
  580. int bit_cnt;
  581. #ifndef ALT_BITSTREAM_READER
  582. UINT32 bit_buf;
  583. UINT8 *buf_ptr;
  584. #endif
  585. SAVE_BITS(s);
  586. nb_bits = vlc->bits;
  587. table_codes = vlc->table_codes;
  588. table_bits = vlc->table_bits;
  589. #ifdef FAST_GET_FIRST_VLC
  590. SHOW_BITS(s, index, nb_bits);
  591. code = table_codes[index];
  592. n = table_bits[index];
  593. if (n > 0) {
  594. /* most common case (90%)*/
  595. FLUSH_BITS(n);
  596. RESTORE_BITS(s);
  597. return code;
  598. } else if (n == 0) {
  599. return -1;
  600. } else {
  601. FLUSH_BITS(nb_bits);
  602. nb_bits = -n;
  603. table_codes = vlc->table_codes + code;
  604. table_bits = vlc->table_bits + code;
  605. }
  606. #endif
  607. for(;;) {
  608. SHOW_BITS(s, index, nb_bits);
  609. code = table_codes[index];
  610. n = table_bits[index];
  611. if (n > 0) {
  612. /* most common case */
  613. FLUSH_BITS(n);
  614. #ifdef STATS
  615. st_bit_counts[st_current_index] += n;
  616. #endif
  617. break;
  618. } else if (n == 0) {
  619. return -1;
  620. } else {
  621. FLUSH_BITS(nb_bits);
  622. #ifdef STATS
  623. st_bit_counts[st_current_index] += nb_bits;
  624. #endif
  625. nb_bits = -n;
  626. table_codes = vlc->table_codes + code;
  627. table_bits = vlc->table_bits + code;
  628. }
  629. }
  630. RESTORE_BITS(s);
  631. return code;
  632. }
  633. /* define it to include statistics code (useful only for optimizing
  634. codec efficiency */
  635. //#define STATS
  636. #ifdef STATS
  637. enum {
  638. ST_UNKNOWN,
  639. ST_DC,
  640. ST_INTRA_AC,
  641. ST_INTER_AC,
  642. ST_INTRA_MB,
  643. ST_INTER_MB,
  644. ST_MV,
  645. ST_NB,
  646. };
  647. extern int st_current_index;
  648. extern unsigned int st_bit_counts[ST_NB];
  649. extern unsigned int st_out_bit_counts[ST_NB];
  650. void print_stats(void);
  651. #endif
  652. /* misc math functions */
  653. static inline int av_log2(unsigned int v)
  654. {
  655. int n;
  656. n = 0;
  657. if (v & 0xffff0000) {
  658. v >>= 16;
  659. n += 16;
  660. }
  661. if (v & 0xff00) {
  662. v >>= 8;
  663. n += 8;
  664. }
  665. if (v & 0xf0) {
  666. v >>= 4;
  667. n += 4;
  668. }
  669. if (v & 0xc) {
  670. v >>= 2;
  671. n += 2;
  672. }
  673. if (v & 0x2) {
  674. n++;
  675. }
  676. return n;
  677. }
  678. /* memory */
  679. void *av_mallocz(int size);
  680. #endif