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.

852 lines
19KB

  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. int size;
  151. } GetBitContext;
  152. static inline int get_bits_count(GetBitContext *s);
  153. typedef struct VLC {
  154. int bits;
  155. INT16 *table_codes;
  156. INT8 *table_bits;
  157. int table_size, table_allocated;
  158. } VLC;
  159. /* used to avoid missaligned exceptions on some archs (alpha, ...) */
  160. #ifdef ARCH_X86
  161. #define unaligned32(a) (*(UINT32*)(a))
  162. #else
  163. #ifdef __GNUC__
  164. static inline uint32_t unaligned32(const void *v) {
  165. struct Unaligned {
  166. uint32_t i;
  167. } __attribute__((packed));
  168. return ((const struct Unaligned *) v)->i;
  169. }
  170. #elif defined(__DECC)
  171. static inline uint32_t unaligned32(const void *v) {
  172. return *(const __unaligned uint32_t *) v;
  173. }
  174. #else
  175. static inline uint32_t unaligned32(const void *v) {
  176. return *(const uint32_t *) v;
  177. }
  178. #endif
  179. #endif //!ARCH_X86
  180. #ifndef ALT_BITSTREAM_WRITER
  181. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  182. {
  183. unsigned int bit_buf;
  184. int bit_left;
  185. #ifdef STATS
  186. st_out_bit_counts[st_current_index] += n;
  187. #endif
  188. // printf("put_bits=%d %x\n", n, value);
  189. assert(n == 32 || value < (1U << n));
  190. bit_buf = s->bit_buf;
  191. bit_left = s->bit_left;
  192. // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  193. /* XXX: optimize */
  194. if (n < bit_left) {
  195. bit_buf = (bit_buf<<n) | value;
  196. bit_left-=n;
  197. } else {
  198. bit_buf<<=bit_left;
  199. bit_buf |= value >> (n - bit_left);
  200. *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
  201. //printf("bitbuf = %08x\n", bit_buf);
  202. s->buf_ptr+=4;
  203. bit_left+=32 - n;
  204. bit_buf = value;
  205. }
  206. s->bit_buf = bit_buf;
  207. s->bit_left = bit_left;
  208. }
  209. #endif
  210. #ifdef ALT_BITSTREAM_WRITER
  211. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  212. {
  213. #ifdef ALIGNED_BITSTREAM_WRITER
  214. #ifdef ARCH_X86
  215. asm volatile(
  216. "movl %0, %%ecx \n\t"
  217. "xorl %%eax, %%eax \n\t"
  218. "shrdl %%cl, %1, %%eax \n\t"
  219. "shrl %%cl, %1 \n\t"
  220. "movl %0, %%ecx \n\t"
  221. "shrl $3, %%ecx \n\t"
  222. "andl $0xFFFFFFFC, %%ecx \n\t"
  223. "bswapl %1 \n\t"
  224. "orl %1, (%2, %%ecx) \n\t"
  225. "bswapl %%eax \n\t"
  226. "addl %3, %0 \n\t"
  227. "movl %%eax, 4(%2, %%ecx) \n\t"
  228. : "=&r" (s->index), "=&r" (value)
  229. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
  230. : "%eax", "%ecx"
  231. );
  232. #else
  233. int index= s->index;
  234. uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
  235. value<<= 32-n;
  236. ptr[0] |= be2me_32(value>>(index&31));
  237. ptr[1] = be2me_32(value<<(32-(index&31)));
  238. //if(n>24) printf("%d %d\n", n, value);
  239. index+= n;
  240. s->index= index;
  241. #endif
  242. #else //ALIGNED_BITSTREAM_WRITER
  243. #ifdef ARCH_X86
  244. asm volatile(
  245. "movl $7, %%ecx \n\t"
  246. "andl %0, %%ecx \n\t"
  247. "addl %3, %%ecx \n\t"
  248. "negl %%ecx \n\t"
  249. "shll %%cl, %1 \n\t"
  250. "bswapl %1 \n\t"
  251. "movl %0, %%ecx \n\t"
  252. "shrl $3, %%ecx \n\t"
  253. "orl %1, (%%ecx, %2) \n\t"
  254. "addl %3, %0 \n\t"
  255. "movl $0, 4(%%ecx, %2) \n\t"
  256. : "=&r" (s->index), "=&r" (value)
  257. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
  258. : "%ecx"
  259. );
  260. #else
  261. int index= s->index;
  262. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  263. ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
  264. ptr[1] = 0;
  265. //if(n>24) printf("%d %d\n", n, value);
  266. index+= n;
  267. s->index= index;
  268. #endif
  269. #endif //!ALIGNED_BITSTREAM_WRITER
  270. }
  271. #endif
  272. #ifndef ALT_BITSTREAM_WRITER
  273. /* for jpeg : escape 0xff with 0x00 after it */
  274. static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
  275. {
  276. unsigned int bit_buf, b;
  277. int bit_left, i;
  278. assert(n == 32 || value < (1U << n));
  279. bit_buf = s->bit_buf;
  280. bit_left = s->bit_left;
  281. //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  282. /* XXX: optimize */
  283. if (n < bit_left) {
  284. bit_buf = (bit_buf<<n) | value;
  285. bit_left-=n;
  286. } else {
  287. bit_buf<<=bit_left;
  288. bit_buf |= value >> (n - bit_left);
  289. /* handle escape */
  290. for(i=0;i<4;i++) {
  291. b = (bit_buf >> 24);
  292. *(s->buf_ptr++) = b;
  293. if (b == 0xff)
  294. *(s->buf_ptr++) = 0;
  295. bit_buf <<= 8;
  296. }
  297. bit_left+= 32 - n;
  298. bit_buf = value;
  299. }
  300. s->bit_buf = bit_buf;
  301. s->bit_left = bit_left;
  302. }
  303. #endif
  304. #ifdef ALT_BITSTREAM_WRITER
  305. static inline void jput_bits(PutBitContext *s, int n, int value)
  306. {
  307. int index= s->index;
  308. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  309. int v= ptr[0];
  310. //if(n>24) printf("%d %d\n", n, value);
  311. v |= be2me_32(value<<(32-n-(index&7) ));
  312. if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
  313. {
  314. /* handle idiotic (m)jpeg escapes */
  315. uint8_t *bPtr= (uint8_t*)ptr;
  316. int numChecked= ((index+n)>>3) - (index>>3);
  317. v= be2me_32(v);
  318. *(bPtr++)= v>>24;
  319. if((v&0xFF000000)==0xFF000000 && numChecked>0){
  320. *(bPtr++)= 0x00;
  321. index+=8;
  322. }
  323. *(bPtr++)= (v>>16)&0xFF;
  324. if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
  325. *(bPtr++)= 0x00;
  326. index+=8;
  327. }
  328. *(bPtr++)= (v>>8)&0xFF;
  329. if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
  330. *(bPtr++)= 0x00;
  331. index+=8;
  332. }
  333. *(bPtr++)= v&0xFF;
  334. if((v&0x000000FF)==0x000000FF && numChecked>3){
  335. *(bPtr++)= 0x00;
  336. index+=8;
  337. }
  338. *((uint32_t*)bPtr)= 0;
  339. }
  340. else
  341. {
  342. ptr[0] = v;
  343. ptr[1] = 0;
  344. }
  345. index+= n;
  346. s->index= index;
  347. }
  348. #endif
  349. static inline uint8_t* pbBufPtr(PutBitContext *s)
  350. {
  351. #ifdef ALT_BITSTREAM_WRITER
  352. return s->buf + (s->index>>3);
  353. #else
  354. return s->buf_ptr;
  355. #endif
  356. }
  357. void init_get_bits(GetBitContext *s,
  358. UINT8 *buffer, int buffer_size);
  359. #ifndef ALT_BITSTREAM_READER
  360. unsigned int get_bits_long(GetBitContext *s, int n);
  361. unsigned int show_bits_long(GetBitContext *s, int n);
  362. #endif
  363. static inline unsigned int get_bits(GetBitContext *s, int n){
  364. #ifdef ALT_BITSTREAM_READER
  365. #ifdef ALIGNED_BITSTREAM
  366. int index= s->index;
  367. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  368. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  369. #ifdef ARCH_X86
  370. asm ("shldl %%cl, %2, %0\n\t"
  371. : "=r" (result1)
  372. : "0" (result1), "r" (result2), "c" (index));
  373. #else
  374. result1<<= (index&0x1F);
  375. result2= (result2>>1) >> (31-(index&0x1F));
  376. result1|= result2;
  377. #endif
  378. result1>>= 32 - n;
  379. index+= n;
  380. s->index= index;
  381. return result1;
  382. #else //ALIGNED_BITSTREAM
  383. int index= s->index;
  384. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  385. result<<= (index&0x07);
  386. result>>= 32 - n;
  387. index+= n;
  388. s->index= index;
  389. return result;
  390. #endif //!ALIGNED_BITSTREAM
  391. #else //ALT_BITSTREAM_READER
  392. if(s->bit_cnt>=n){
  393. /* most common case here */
  394. unsigned int val = s->bit_buf >> (32 - n);
  395. s->bit_buf <<= n;
  396. s->bit_cnt -= n;
  397. #ifdef STATS
  398. st_bit_counts[st_current_index] += n;
  399. #endif
  400. return val;
  401. }
  402. return get_bits_long(s,n);
  403. #endif //!ALT_BITSTREAM_READER
  404. }
  405. static inline unsigned int get_bits1(GetBitContext *s){
  406. #ifdef ALT_BITSTREAM_READER
  407. int index= s->index;
  408. uint8_t result= s->buffer[ index>>3 ];
  409. result<<= (index&0x07);
  410. result>>= 8 - 1;
  411. index++;
  412. s->index= index;
  413. return result;
  414. #else
  415. if(s->bit_cnt>0){
  416. /* most common case here */
  417. unsigned int val = s->bit_buf >> 31;
  418. s->bit_buf <<= 1;
  419. s->bit_cnt--;
  420. #ifdef STATS
  421. st_bit_counts[st_current_index]++;
  422. #endif
  423. return val;
  424. }
  425. return get_bits_long(s,1);
  426. #endif
  427. }
  428. /* This function is identical to get_bits(), the only */
  429. /* diference is that it doesn't touch the buffer */
  430. /* it is usefull to see the buffer. */
  431. static inline unsigned int show_bits(GetBitContext *s, int n)
  432. {
  433. #ifdef ALT_BITSTREAM_READER
  434. #ifdef ALIGNED_BITSTREAM
  435. int index= s->index;
  436. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  437. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  438. #ifdef ARCH_X86
  439. asm ("shldl %%cl, %2, %0\n\t"
  440. : "=r" (result1)
  441. : "0" (result1), "r" (result2), "c" (index));
  442. #else
  443. result1<<= (index&0x1F);
  444. result2= (result2>>1) >> (31-(index&0x1F));
  445. result1|= result2;
  446. #endif
  447. result1>>= 32 - n;
  448. return result1;
  449. #else //ALIGNED_BITSTREAM
  450. int index= s->index;
  451. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  452. result<<= (index&0x07);
  453. result>>= 32 - n;
  454. return result;
  455. #endif //!ALIGNED_BITSTREAM
  456. #else //ALT_BITSTREAM_READER
  457. if(s->bit_cnt>=n) {
  458. /* most common case here */
  459. unsigned int val = s->bit_buf >> (32 - n);
  460. return val;
  461. }
  462. return show_bits_long(s,n);
  463. #endif //!ALT_BITSTREAM_READER
  464. }
  465. static inline int show_aligned_bits(GetBitContext *s, int offset, int n)
  466. {
  467. #ifdef ALT_BITSTREAM_READER
  468. #ifdef ALIGNED_BITSTREAM
  469. int index= (s->index + offset + 7)&(~7);
  470. uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  471. uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  472. #ifdef ARCH_X86
  473. asm ("shldl %%cl, %2, %0\n\t"
  474. : "=r" (result1)
  475. : "0" (result1), "r" (result2), "c" (index));
  476. #else
  477. result1<<= (index&0x1F);
  478. result2= (result2>>1) >> (31-(index&0x1F));
  479. result1|= result2;
  480. #endif
  481. result1>>= 32 - n;
  482. return result1;
  483. #else //ALIGNED_BITSTREAM
  484. int index= (s->index + offset + 7)>>3;
  485. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+index ) );
  486. result>>= 32 - n;
  487. return result;
  488. #endif //!ALIGNED_BITSTREAM
  489. #else //ALT_BITSTREAM_READER
  490. int index= (get_bits_count(s) + offset + 7)>>3;
  491. uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buf)+index ) );
  492. result>>= 32 - n;
  493. //printf(" %X %X %d \n", (int)(((uint8_t *)s->buf)+index ), (int)s->buf_ptr, s->bit_cnt);
  494. return result;
  495. #endif //!ALT_BITSTREAM_READER
  496. }
  497. static inline void skip_bits(GetBitContext *s, int n){
  498. #ifdef ALT_BITSTREAM_READER
  499. s->index+= n;
  500. #else
  501. if(s->bit_cnt>=n){
  502. /* most common case here */
  503. s->bit_buf <<= n;
  504. s->bit_cnt -= n;
  505. #ifdef STATS
  506. st_bit_counts[st_current_index] += n;
  507. #endif
  508. } else {
  509. get_bits_long(s,n);
  510. }
  511. #endif
  512. }
  513. static inline void skip_bits1(GetBitContext *s){
  514. #ifdef ALT_BITSTREAM_READER
  515. s->index++;
  516. #else
  517. if(s->bit_cnt>0){
  518. /* most common case here */
  519. s->bit_buf <<= 1;
  520. s->bit_cnt--;
  521. #ifdef STATS
  522. st_bit_counts[st_current_index]++;
  523. #endif
  524. } else {
  525. get_bits_long(s,1);
  526. }
  527. #endif
  528. }
  529. static inline int get_bits_count(GetBitContext *s)
  530. {
  531. #ifdef ALT_BITSTREAM_READER
  532. return s->index;
  533. #else
  534. return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
  535. #endif
  536. }
  537. int check_marker(GetBitContext *s, char *msg);
  538. void align_get_bits(GetBitContext *s);
  539. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  540. const void *bits, int bits_wrap, int bits_size,
  541. const void *codes, int codes_wrap, int codes_size);
  542. void free_vlc(VLC *vlc);
  543. #ifdef ALT_BITSTREAM_READER
  544. #ifdef ALIGNED_BITSTREAM
  545. #ifdef ARCH_X86
  546. #define SHOW_BITS(s, val, n) \
  547. val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  548. {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  549. asm ("shldl %%cl, %2, %0\n\t"\
  550. : "=r" (val)\
  551. : "0" (val), "r" (result2), "c" (bit_cnt));\
  552. ((uint32_t)val)>>= 32 - n;}
  553. #else //ARCH_X86
  554. #define SHOW_BITS(s, val, n) \
  555. val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  556. {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  557. val<<= (bit_cnt&0x1F);\
  558. result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
  559. val|= result2;\
  560. ((uint32_t)val)>>= 32 - n;}
  561. #endif //!ARCH_X86
  562. #else //ALIGNED_BITSTREAM
  563. #define SHOW_BITS(s, val, n) \
  564. val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
  565. val<<= (bit_cnt&0x07);\
  566. ((uint32_t)val)>>= 32 - n;
  567. #endif // !ALIGNED_BITSTREAM
  568. #define FLUSH_BITS(n) bit_cnt+=n;
  569. #define SAVE_BITS(s) bit_cnt= (s)->index;
  570. #define RESTORE_BITS(s) (s)->index= bit_cnt;
  571. #else
  572. /* macro to go faster */
  573. /* n must be <= 24 */
  574. /* XXX: optimize buffer end test */
  575. #define SHOW_BITS(s, val, n)\
  576. {\
  577. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  578. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  579. bit_cnt += 8;\
  580. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  581. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  582. bit_cnt += 8;\
  583. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  584. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  585. bit_cnt += 8;\
  586. }\
  587. }\
  588. }\
  589. val = bit_buf >> (32 - n);\
  590. }
  591. /* SHOW_BITS with n1 >= n must be been done before */
  592. #define FLUSH_BITS(n)\
  593. {\
  594. bit_buf <<= n;\
  595. bit_cnt -= n;\
  596. }
  597. #define SAVE_BITS(s) \
  598. {\
  599. bit_cnt = (s)->bit_cnt;\
  600. bit_buf = (s)->bit_buf;\
  601. buf_ptr = (s)->buf_ptr;\
  602. }
  603. #define RESTORE_BITS(s) \
  604. {\
  605. (s)->buf_ptr = buf_ptr;\
  606. (s)->bit_buf = bit_buf;\
  607. (s)->bit_cnt = bit_cnt;\
  608. }
  609. #endif // !ALT_BITSTREAM_READER
  610. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  611. {
  612. int code, n, nb_bits, index;
  613. INT16 *table_codes;
  614. INT8 *table_bits;
  615. int bit_cnt;
  616. #ifndef ALT_BITSTREAM_READER
  617. UINT32 bit_buf;
  618. UINT8 *buf_ptr;
  619. #endif
  620. SAVE_BITS(s);
  621. nb_bits = vlc->bits;
  622. table_codes = vlc->table_codes;
  623. table_bits = vlc->table_bits;
  624. #ifdef FAST_GET_FIRST_VLC
  625. SHOW_BITS(s, index, nb_bits);
  626. code = table_codes[index];
  627. n = table_bits[index];
  628. if (n > 0) {
  629. /* most common case (90%)*/
  630. FLUSH_BITS(n);
  631. RESTORE_BITS(s);
  632. return code;
  633. } else if (n == 0) {
  634. return -1;
  635. } else {
  636. FLUSH_BITS(nb_bits);
  637. nb_bits = -n;
  638. table_codes = vlc->table_codes + code;
  639. table_bits = vlc->table_bits + code;
  640. }
  641. #endif
  642. for(;;) {
  643. SHOW_BITS(s, index, nb_bits);
  644. code = table_codes[index];
  645. n = table_bits[index];
  646. if (n > 0) {
  647. /* most common case */
  648. FLUSH_BITS(n);
  649. #ifdef STATS
  650. st_bit_counts[st_current_index] += n;
  651. #endif
  652. break;
  653. } else if (n == 0) {
  654. return -1;
  655. } else {
  656. FLUSH_BITS(nb_bits);
  657. #ifdef STATS
  658. st_bit_counts[st_current_index] += nb_bits;
  659. #endif
  660. nb_bits = -n;
  661. table_codes = vlc->table_codes + code;
  662. table_bits = vlc->table_bits + code;
  663. }
  664. }
  665. RESTORE_BITS(s);
  666. return code;
  667. }
  668. /* define it to include statistics code (useful only for optimizing
  669. codec efficiency */
  670. //#define STATS
  671. #ifdef STATS
  672. enum {
  673. ST_UNKNOWN,
  674. ST_DC,
  675. ST_INTRA_AC,
  676. ST_INTER_AC,
  677. ST_INTRA_MB,
  678. ST_INTER_MB,
  679. ST_MV,
  680. ST_NB,
  681. };
  682. extern int st_current_index;
  683. extern unsigned int st_bit_counts[ST_NB];
  684. extern unsigned int st_out_bit_counts[ST_NB];
  685. void print_stats(void);
  686. #endif
  687. /* misc math functions */
  688. static inline int av_log2(unsigned int v)
  689. {
  690. int n;
  691. n = 0;
  692. if (v & 0xffff0000) {
  693. v >>= 16;
  694. n += 16;
  695. }
  696. if (v & 0xff00) {
  697. v >>= 8;
  698. n += 8;
  699. }
  700. if (v & 0xf0) {
  701. v >>= 4;
  702. n += 4;
  703. }
  704. if (v & 0xc) {
  705. v >>= 2;
  706. n += 2;
  707. }
  708. if (v & 0x2) {
  709. n++;
  710. }
  711. return n;
  712. }
  713. /* median of 3 */
  714. static inline int mid_pred(int a, int b, int c)
  715. {
  716. int vmin, vmax;
  717. vmax = vmin = a;
  718. if (b < vmin)
  719. vmin = b;
  720. else
  721. vmax = b;
  722. if (c < vmin)
  723. vmin = c;
  724. else if (c > vmax)
  725. vmax = c;
  726. return a + b + c - vmin - vmax;
  727. }
  728. /* memory */
  729. void *av_mallocz(int size);
  730. #endif