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.

901 lines
20KB

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