home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wvis0626.zip / warpvision_20020626.zip / libavcodec / common.h < prev    next >
C/C++ Source or Header  |  2002-06-24  |  23KB  |  970 lines

  1. #ifndef COMMON_H
  2. #define COMMON_H
  3.  
  4. #define FFMPEG_VERSION_INT 0x000406
  5. #define FFMPEG_VERSION     "0.4.6"
  6.  
  7. #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  8. #define CONFIG_WIN32
  9. #endif
  10.  
  11. //#define ALT_BITSTREAM_WRITER
  12. //#define ALIGNED_BITSTREAM_WRITER
  13. //#define ALT_BITSTREAM_READER
  14. //#define ALIGNED_BITSTREAM
  15. #define FAST_GET_FIRST_VLC
  16. //#define DUMP_STREAM // only works with the ALT_BITSTREAM_READER
  17.  
  18. #ifdef HAVE_AV_CONFIG_H
  19. /* only include the following when compiling package */
  20. #include "config.h"
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <math.h>
  27.  
  28. #ifndef ENODATA
  29. #define ENODATA  61
  30. #endif
  31.  
  32. #ifdef OS2
  33. #ifndef EIO
  34. #define EIO  5
  35. #define EPIPE  32
  36. #endif
  37. #endif
  38.  
  39. #endif /* HAVE_AV_CONFIG_H */
  40.  
  41. #if defined (CONFIG_WIN32) || defined (OS2)
  42.  
  43. /* windows | os2 */
  44.  
  45. typedef unsigned short UINT16;
  46. typedef signed short INT16;
  47. typedef unsigned char UINT8;
  48. typedef unsigned int UINT32;
  49. #ifdef OS2
  50. typedef unsigned long long UINT64;
  51. #else
  52. typedef unsigned __int64 UINT64;
  53. #endif
  54. typedef signed char INT8;
  55. typedef signed int INT32;
  56. #ifdef OS2
  57. typedef signed long long INT64;
  58. #else
  59. typedef signed __int64 INT64;
  60. #endif
  61.  
  62. typedef UINT8 uint8_t;
  63. typedef INT8 int8_t;
  64. typedef UINT16 uint16_t;
  65. typedef INT16 int16_t;
  66. typedef UINT32 uint32_t;
  67. typedef INT32 int32_t;
  68. typedef UINT64 uint64_t;
  69. typedef INT64 int64_t;
  70.  
  71. #ifndef OS2
  72. #ifndef __MINGW32__
  73. #define INT64_C(c)     (c ## i64)
  74. #define UINT64_C(c)    (c ## i64)
  75.  
  76. #define inline __inline
  77.  
  78. #else
  79. #define INT64_C(c)     (c ## LL)
  80. #define UINT64_C(c)    (c ## ULL)
  81. #endif /* __MINGW32__ */
  82. #else
  83. #ifndef INT64_C
  84. #define INT64_C(c)     (c ## LL)
  85. #endif
  86. #ifndef UINT64_C
  87. #define UINT64_C(c)    (c ## ULL)
  88. #endif
  89.  
  90. #ifndef __cplusplus
  91. #define inline __inline
  92. #endif
  93.  
  94. #if defined (__IBMC__) || defined (__IBMCPP__)
  95. #define optlink _Optlink
  96. #else
  97. #define optlink _optlink
  98. #endif
  99.  
  100. #endif  /* OS2 */
  101.  
  102. #define M_PI    3.14159265358979323846
  103. #define M_SQRT2 1.41421356237309504880  /* sqrt(2) */
  104.  
  105. #ifdef _DEBUG
  106. #define DEBUG
  107. #endif
  108.  
  109. #if defined(OS2) && !defined(__EMX__)
  110. #define snprintf(str,num,fmt,args) sprintf(str,fmt,##args)
  111. #else
  112. #define snprintf _snprintf
  113. #endif
  114.  
  115. #else /* CONFIG_WIN32 */
  116.  
  117. /* unix */
  118.  
  119. #include <inttypes.h>
  120.  
  121. #ifndef __WINE_WINDEF16_H
  122. /* workaround for typedef conflict in MPlayer (wine typedefs) */
  123. typedef unsigned short UINT16;
  124. typedef signed short INT16;
  125. #endif
  126.  
  127. typedef unsigned char UINT8;
  128. typedef unsigned int UINT32;
  129. typedef unsigned long long UINT64;
  130. typedef signed char INT8;
  131. typedef signed int INT32;
  132. typedef signed long long INT64;
  133.  
  134. #ifdef HAVE_AV_CONFIG_H
  135.  
  136. #ifdef __FreeBSD__
  137. #include <sys/param.h>
  138. #endif
  139.  
  140. #ifndef INT64_C
  141. #define INT64_C(c)     (c ## LL)
  142. #define UINT64_C(c)    (c ## ULL)
  143. #endif
  144.  
  145. #ifdef USE_FASTMEMCPY
  146. #include "fastmemcpy.h"
  147. #endif
  148.  
  149. #endif /* HAVE_AV_CONFIG_H */
  150.  
  151. #endif /* !CONFIG_WIN32 */
  152.  
  153. #ifdef HAVE_AV_CONFIG_H
  154.  
  155. #include "bswap.h"
  156.  
  157. #if defined(__MINGW32__) || defined(__CYGWIN__) || \
  158.     defined(__OS2__) || defined (__OpenBSD__)
  159. #define MANGLE(a) "_" #a
  160. #else
  161. #define MANGLE(a) #a
  162. #endif
  163.  
  164. /* debug stuff */
  165.  
  166. #ifndef DEBUG
  167. #define NDEBUG
  168. #endif
  169. #include <assert.h>
  170.  
  171. /* dprintf macros */
  172. #if defined(CONFIG_WIN32) && !defined(__MINGW32__) || defined(OS2) && !defined(__EMX__)
  173.  
  174. inline void dprintf(const char* fmt,...) {}
  175.  
  176. #else
  177.  
  178. #ifdef DEBUG
  179. #define dprintf(fmt,args...) printf(fmt, ## args)
  180. #else
  181. #define dprintf(fmt,args...)
  182. #endif
  183.  
  184. #endif /* !CONFIG_WIN32 */
  185.  
  186. #define av_abort()      do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  187.  
  188. /* assume b>0 */
  189. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  190. #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
  191. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  192. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  193.  
  194. /* bit output */
  195.  
  196. struct PutBitContext;
  197.  
  198. typedef void (*WriteDataFunc)(void *, UINT8 *, int);
  199.  
  200. typedef struct PutBitContext {
  201. #ifdef ALT_BITSTREAM_WRITER
  202.     UINT8 *buf, *buf_end;
  203.     int index;
  204. #else
  205.     UINT32 bit_buf;
  206.     int bit_left;
  207.     UINT8 *buf, *buf_ptr, *buf_end;
  208. #endif
  209.     INT64 data_out_size; /* in bytes */
  210. } PutBitContext;
  211.  
  212. void init_put_bits(PutBitContext *s, 
  213.                    UINT8 *buffer, int buffer_size,
  214.                    void *opaque,
  215.                    void (*write_data)(void *, UINT8 *, int));
  216.  
  217. INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
  218. void align_put_bits(PutBitContext *s);
  219. void flush_put_bits(PutBitContext *s);
  220. void put_string(PutBitContext * pbc, char *s);
  221.  
  222. /* jpeg specific put_bits */
  223. void jflush_put_bits(PutBitContext *s);
  224.  
  225. /* bit input */
  226.  
  227. typedef struct GetBitContext {
  228. #ifdef ALT_BITSTREAM_READER
  229.     int index;
  230.     UINT8 *buffer;
  231. #else
  232.     UINT32 bit_buf;
  233.     int bit_cnt;
  234.     UINT8 *buf, *buf_ptr, *buf_end;
  235. #endif
  236.     int size;
  237. } GetBitContext;
  238.  
  239. static inline int get_bits_count(GetBitContext *s);
  240.  
  241. typedef struct VLC {
  242.     int bits;
  243.     INT16 *table_codes;
  244.     INT8 *table_bits;
  245.     int table_size, table_allocated;
  246. } VLC;
  247.  
  248. /* used to avoid missaligned exceptions on some archs (alpha, ...) */
  249. #ifdef ARCH_X86
  250. #define unaligned32(a) (*(UINT32*)(a))
  251. #else
  252. #ifdef __GNUC__
  253. static inline uint32_t unaligned32(const void *v) {
  254.     struct Unaligned {
  255.     uint32_t i;
  256.     } __attribute__((packed));
  257.  
  258.     return ((const struct Unaligned *) v)->i;
  259. }
  260. #elif defined(__DECC)
  261. static inline uint32_t unaligned32(const void *v) {
  262.     return *(const __unaligned uint32_t *) v;
  263. }
  264. #else
  265. static inline uint32_t unaligned32(const void *v) {
  266.     return *(const uint32_t *) v;
  267. }
  268. #endif
  269. #endif //!ARCH_X86
  270.  
  271. #ifndef ALT_BITSTREAM_WRITER
  272. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  273. {
  274.     unsigned int bit_buf;
  275.     int bit_left;
  276.  
  277. #ifdef STATS
  278.     st_out_bit_counts[st_current_index] += n;
  279. #endif
  280.     //    printf("put_bits=%d %x\n", n, value);
  281.     assert(n == 32 || value < (1U << n));
  282.     
  283.     bit_buf = s->bit_buf;
  284.     bit_left = s->bit_left;
  285.  
  286.     //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  287.     /* XXX: optimize */
  288.     if (n < bit_left) {
  289.         bit_buf = (bit_buf<<n) | value;
  290.         bit_left-=n;
  291.     } else {
  292.     bit_buf<<=bit_left;
  293.         bit_buf |= value >> (n - bit_left);
  294.         *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
  295.         //printf("bitbuf = %08x\n", bit_buf);
  296.         s->buf_ptr+=4;
  297.     bit_left+=32 - n;
  298.         bit_buf = value;
  299.     }
  300.  
  301.     s->bit_buf = bit_buf;
  302.     s->bit_left = bit_left;
  303. }
  304. #endif
  305.  
  306.  
  307. #ifdef ALT_BITSTREAM_WRITER
  308. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  309. {
  310. #ifdef ALIGNED_BITSTREAM_WRITER
  311. #ifdef ARCH_X86
  312.     asm volatile(
  313.     "movl %0, %%ecx            \n\t"
  314.     "xorl %%eax, %%eax        \n\t"
  315.     "shrdl %%cl, %1, %%eax        \n\t"
  316.     "shrl %%cl, %1            \n\t"
  317.     "movl %0, %%ecx            \n\t"
  318.     "shrl $3, %%ecx            \n\t"
  319.     "andl $0xFFFFFFFC, %%ecx    \n\t"
  320.     "bswapl %1            \n\t"
  321.     "orl %1, (%2, %%ecx)        \n\t"
  322.     "bswapl %%eax            \n\t"
  323.     "addl %3, %0            \n\t"
  324.     "movl %%eax, 4(%2, %%ecx)    \n\t"
  325.     : "=&r" (s->index), "=&r" (value)
  326.     : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
  327.     : "%eax", "%ecx"
  328.     );
  329. #else
  330.     int index= s->index;
  331.     uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
  332.     
  333.     value<<= 32-n; 
  334.     
  335.     ptr[0] |= be2me_32(value>>(index&31));
  336.     ptr[1]  = be2me_32(value<<(32-(index&31)));
  337. //if(n>24) printf("%d %d\n", n, value);
  338.     index+= n;
  339.     s->index= index;
  340. #endif
  341. #else //ALIGNED_BITSTREAM_WRITER
  342. #ifdef ARCH_X86
  343.     asm volatile(
  344.     "movl $7, %%ecx            \n\t"
  345.     "andl %0, %%ecx            \n\t"
  346.     "addl %3, %%ecx            \n\t"
  347.     "negl %%ecx            \n\t"
  348.     "shll %%cl, %1            \n\t"
  349.     "bswapl %1            \n\t"
  350.     "movl %0, %%ecx            \n\t"
  351.     "shrl $3, %%ecx            \n\t"
  352.     "orl %1, (%%ecx, %2)        \n\t"
  353.     "addl %3, %0            \n\t"
  354.     "movl $0, 4(%%ecx, %2)        \n\t"
  355.     : "=&r" (s->index), "=&r" (value)
  356.     : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
  357.     : "%ecx"
  358.     );
  359. #else
  360.     int index= s->index;
  361.     uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  362.     
  363.     ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
  364.     ptr[1] = 0;
  365. //if(n>24) printf("%d %d\n", n, value);
  366.     index+= n;
  367.     s->index= index;
  368. #endif
  369. #endif //!ALIGNED_BITSTREAM_WRITER
  370. }
  371. #endif
  372.  
  373. #ifndef ALT_BITSTREAM_WRITER
  374. /* for jpeg : escape 0xff with 0x00 after it */
  375. static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
  376. {
  377.     unsigned int bit_buf, b;
  378.     int bit_left, i;
  379.     
  380.     assert(n == 32 || value < (1U << n));
  381.  
  382.     bit_buf = s->bit_buf;
  383.     bit_left = s->bit_left;
  384.  
  385.     //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  386.     /* XXX: optimize */
  387.     if (n < bit_left) {
  388.         bit_buf = (bit_buf<<n) | value;
  389.         bit_left-=n;
  390.     } else {
  391.     bit_buf<<=bit_left;
  392.         bit_buf |= value >> (n - bit_left);
  393.         /* handle escape */
  394.         for(i=0;i<4;i++) {
  395.             b = (bit_buf >> 24);
  396.             *(s->buf_ptr++) = b;
  397.             if (b == 0xff)
  398.                 *(s->buf_ptr++) = 0;
  399.             bit_buf <<= 8;
  400.         }
  401.  
  402.     bit_left+= 32 - n;
  403.         bit_buf = value;
  404.     }
  405.     
  406.     s->bit_buf = bit_buf;
  407.     s->bit_left = bit_left;
  408. }
  409. #endif
  410.  
  411.  
  412. #ifdef ALT_BITSTREAM_WRITER
  413. static inline void jput_bits(PutBitContext *s, int n, int value)
  414. {
  415.     int index= s->index;
  416.     uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  417.     int v= ptr[0];
  418. //if(n>24) printf("%d %d\n", n, value);
  419.     
  420.     v |= be2me_32(value<<(32-n-(index&7) ));
  421.     if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
  422.     {
  423.     /* handle idiotic (m)jpeg escapes */
  424.     uint8_t *bPtr= (uint8_t*)ptr;
  425.     int numChecked= ((index+n)>>3) - (index>>3);
  426.     
  427.     v= be2me_32(v);
  428.  
  429.     *(bPtr++)= v>>24;
  430.     if((v&0xFF000000)==0xFF000000 && numChecked>0){
  431.         *(bPtr++)= 0x00;
  432.         index+=8;
  433.     }
  434.     *(bPtr++)= (v>>16)&0xFF;
  435.     if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
  436.         *(bPtr++)= 0x00;
  437.         index+=8;
  438.     }
  439.     *(bPtr++)= (v>>8)&0xFF;
  440.     if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
  441.         *(bPtr++)= 0x00;
  442.         index+=8;
  443.     }
  444.     *(bPtr++)= v&0xFF;
  445.     if((v&0x000000FF)==0x000000FF && numChecked>3){
  446.         *(bPtr++)= 0x00;
  447.         index+=8;
  448.     }
  449.     *((uint32_t*)bPtr)= 0;
  450.     }
  451.     else
  452.     {
  453.     ptr[0] = v;
  454.     ptr[1] = 0;
  455.     }
  456.  
  457.     index+= n;
  458.     s->index= index;
  459.  }
  460. #endif
  461.  
  462. static inline uint8_t* pbBufPtr(PutBitContext *s)
  463. {
  464. #ifdef ALT_BITSTREAM_WRITER
  465.     return s->buf + (s->index>>3);
  466. #else
  467.     return s->buf_ptr;
  468. #endif
  469. }
  470.  
  471. void init_get_bits(GetBitContext *s, 
  472.                    UINT8 *buffer, int buffer_size);
  473.  
  474. #ifndef ALT_BITSTREAM_READER
  475. unsigned int get_bits_long(GetBitContext *s, int n);
  476. unsigned int show_bits_long(GetBitContext *s, int n);
  477. #endif
  478.  
  479. static inline unsigned int get_bits(GetBitContext *s, int n){
  480. #ifdef ALT_BITSTREAM_READER
  481. #ifdef ALIGNED_BITSTREAM
  482.     int index= s->index;
  483.     uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  484.     uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  485. #ifdef ARCH_X86
  486.     asm ("shldl %%cl, %2, %0\n\t"
  487.          : "=r" (result1)
  488.      : "0" (result1), "r" (result2), "c" (index));
  489. #else
  490.     result1<<= (index&0x1F);
  491.     result2= (result2>>1) >> (31-(index&0x1F));
  492.     result1|= result2;
  493. #endif
  494.     result1>>= 32 - n;
  495.     index+= n;
  496.     s->index= index;
  497.     
  498.     return result1;
  499. #else //ALIGNED_BITSTREAM
  500.     int index= s->index;
  501.     uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  502.  
  503.     result<<= (index&0x07);
  504.     result>>= 32 - n;
  505.     index+= n;
  506.     s->index= index;
  507. #ifdef DUMP_STREAM
  508.     while(n){
  509.         printf("%d", (result>>(n-1))&1);
  510.         n--;
  511.     }
  512.     printf(" ");
  513. #endif
  514.     return result;
  515. #endif //!ALIGNED_BITSTREAM
  516. #else //ALT_BITSTREAM_READER
  517.     if(s->bit_cnt>=n){
  518.         /* most common case here */
  519.         unsigned int val = s->bit_buf >> (32 - n);
  520.         s->bit_buf <<= n;
  521.     s->bit_cnt -= n;
  522. #ifdef STATS
  523.     st_bit_counts[st_current_index] += n;
  524. #endif
  525.     return val;
  526.     }
  527.     return get_bits_long(s,n);
  528. #endif //!ALT_BITSTREAM_READER
  529. }
  530.  
  531. static inline unsigned int get_bits1(GetBitContext *s){
  532. #ifdef ALT_BITSTREAM_READER
  533.     int index= s->index;
  534.     uint8_t result= s->buffer[ index>>3 ];
  535.     result<<= (index&0x07);
  536.     result>>= 8 - 1;
  537.     index++;
  538.     s->index= index;
  539.     
  540. #ifdef DUMP_STREAM
  541.     printf("%d ", result);
  542. #endif
  543.     return result;
  544. #else
  545.     if(s->bit_cnt>0){
  546.         /* most common case here */
  547.         unsigned int val = s->bit_buf >> 31;
  548.         s->bit_buf <<= 1;
  549.     s->bit_cnt--;
  550. #ifdef STATS
  551.     st_bit_counts[st_current_index]++;
  552. #endif
  553.     return val;
  554.     }
  555.     return get_bits_long(s,1);
  556. #endif
  557. }
  558.  
  559. /* This function is identical to get_bits(), the only */
  560. /* diference is that it doesn't touch the buffer      */
  561. /* it is usefull to see the buffer.                   */
  562. static inline unsigned int show_bits(GetBitContext *s, int n)
  563. {
  564. #ifdef ALT_BITSTREAM_READER
  565. #ifdef ALIGNED_BITSTREAM
  566.     int index= s->index;
  567.     uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  568.     uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  569. #ifdef ARCH_X86
  570.     asm ("shldl %%cl, %2, %0\n\t"
  571.          : "=r" (result1)
  572.      : "0" (result1), "r" (result2), "c" (index));
  573. #else
  574.     result1<<= (index&0x1F);
  575.     result2= (result2>>1) >> (31-(index&0x1F));
  576.     result1|= result2;
  577. #endif
  578.     result1>>= 32 - n;
  579.     
  580.     return result1;
  581. #else //ALIGNED_BITSTREAM
  582.     int index= s->index;
  583.     uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
  584.  
  585.     result<<= (index&0x07);
  586.     result>>= 32 - n;
  587.     
  588.     return result;
  589. #endif //!ALIGNED_BITSTREAM
  590. #else //ALT_BITSTREAM_READER
  591.     if(s->bit_cnt>=n) {
  592.         /* most common case here */
  593.         unsigned int val = s->bit_buf >> (32 - n);
  594.         return val;
  595.     }
  596.     return show_bits_long(s,n);
  597. #endif //!ALT_BITSTREAM_READER
  598. }
  599.  
  600. static inline int show_aligned_bits(GetBitContext *s, int offset, int n)
  601. {
  602. #ifdef ALT_BITSTREAM_READER
  603. #ifdef ALIGNED_BITSTREAM
  604.     int index= (s->index + offset + 7)&(~7);
  605.     uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
  606.     uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
  607. #ifdef ARCH_X86
  608.     asm ("shldl %%cl, %2, %0\n\t"
  609.          : "=r" (result1)
  610.      : "0" (result1), "r" (result2), "c" (index));
  611. #else
  612.     result1<<= (index&0x1F);
  613.     result2= (result2>>1) >> (31-(index&0x1F));
  614.     result1|= result2;
  615. #endif
  616.     result1>>= 32 - n;
  617.     
  618.     return result1;
  619. #else //ALIGNED_BITSTREAM
  620.     int index= (s->index + offset + 7)>>3;
  621.     uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+index ) );
  622.  
  623.     result>>= 32 - n;
  624.     
  625.     return result;
  626. #endif //!ALIGNED_BITSTREAM
  627. #else //ALT_BITSTREAM_READER
  628.     int index= (get_bits_count(s) + offset + 7)>>3;
  629.     uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buf)+index ) );
  630.  
  631.     result>>= 32 - n;
  632. //printf(" %X %X %d \n", (int)(((uint8_t *)s->buf)+index ), (int)s->buf_ptr, s->bit_cnt);    
  633.     return result;
  634. #endif //!ALT_BITSTREAM_READER
  635. }
  636.  
  637. static inline void skip_bits(GetBitContext *s, int n){
  638. #ifdef ALT_BITSTREAM_READER
  639.     s->index+= n;
  640. #ifdef DUMP_STREAM
  641.     {
  642.         int result;
  643.         s->index-= n;
  644.         result= get_bits(s, n);
  645.     }
  646. #endif
  647.  
  648. #else
  649.     if(s->bit_cnt>=n){
  650.         /* most common case here */
  651.         s->bit_buf <<= n;
  652.     s->bit_cnt -= n;
  653. #ifdef STATS
  654.     st_bit_counts[st_current_index] += n;
  655. #endif
  656.     } else {
  657.     get_bits_long(s,n);
  658.     }
  659. #endif
  660. }
  661.  
  662. static inline void skip_bits1(GetBitContext *s){
  663. #ifdef ALT_BITSTREAM_READER
  664.     s->index++;
  665. #ifdef DUMP_STREAM
  666.     s->index--;
  667.     printf("%d ", get_bits1(s));
  668. #endif
  669. #else
  670.     if(s->bit_cnt>0){
  671.         /* most common case here */
  672.         s->bit_buf <<= 1;
  673.     s->bit_cnt--;
  674. #ifdef STATS
  675.     st_bit_counts[st_current_index]++;
  676. #endif
  677.     } else {
  678.     get_bits_long(s,1);
  679.     }
  680. #endif
  681. }
  682.  
  683. static inline int get_bits_count(GetBitContext *s)
  684. {
  685. #ifdef ALT_BITSTREAM_READER
  686.     return s->index;
  687. #else
  688.     return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
  689. #endif
  690. }
  691.  
  692. int check_marker(GetBitContext *s, char *msg);
  693. void align_get_bits(GetBitContext *s);
  694. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  695.              const void *bits, int bits_wrap, int bits_size,
  696.              const void *codes, int codes_wrap, int codes_size);
  697. void free_vlc(VLC *vlc);
  698.  
  699. #ifdef ALT_BITSTREAM_READER
  700. #ifdef ALIGNED_BITSTREAM
  701. #ifdef ARCH_X86
  702. #define SHOW_BITS(s, val, n) \
  703.     val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  704.     {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  705.     asm ("shldl %%cl, %2, %0\n\t"\
  706.          : "=r" (val)\
  707.          : "0" (val), "r" (result2), "c" (bit_cnt));\
  708.     ((uint32_t)val)>>= 32 - n;}
  709. #else //ARCH_X86
  710. #define SHOW_BITS(s, val, n) \
  711.     val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
  712.     {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
  713.     val<<= (bit_cnt&0x1F);\
  714.     result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
  715.     val|= result2;\
  716.     ((uint32_t)val)>>= 32 - n;}
  717. #endif //!ARCH_X86
  718. #else //ALIGNED_BITSTREAM
  719. #define SHOW_BITS(s, val, n) \
  720.     val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
  721.     val<<= (bit_cnt&0x07);\
  722.     ((uint32_t)val)>>= 32 - n;
  723. #endif // !ALIGNED_BITSTREAM
  724. #define FLUSH_BITS(n) bit_cnt+=n; 
  725. #define SAVE_BITS(s) bit_cnt= (s)->index;
  726. #define RESTORE_BITS(s) (s)->index= bit_cnt;
  727. #else
  728.  
  729. /* macro to go faster */
  730. /* n must be <= 24 */
  731. /* XXX: optimize buffer end test */
  732. #define SHOW_BITS(s, val, n)\
  733. {\
  734.     if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  735.         bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  736.         bit_cnt += 8;\
  737.         if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  738.             bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  739.             bit_cnt += 8;\
  740.             if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  741.                 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  742.                 bit_cnt += 8;\
  743.             }\
  744.         }\
  745.     }\
  746.     val = bit_buf >> (32 - n);\
  747. }
  748.  
  749. /* SHOW_BITS with n1 >= n must be been done before */
  750. #define FLUSH_BITS(n)\
  751. {\
  752.     bit_buf <<= n;\
  753.     bit_cnt -= n;\
  754. }
  755.  
  756. #define SAVE_BITS(s) \
  757. {\
  758.     bit_cnt = (s)->bit_cnt;\
  759.     bit_buf = (s)->bit_buf;\
  760.     buf_ptr = (s)->buf_ptr;\
  761. }
  762.  
  763. #define RESTORE_BITS(s) \
  764. {\
  765.     (s)->buf_ptr = buf_ptr;\
  766.     (s)->bit_buf = bit_buf;\
  767.     (s)->bit_cnt = bit_cnt;\
  768. }
  769. #endif // !ALT_BITSTREAM_READER
  770.  
  771. static inline int get_vlc(GetBitContext *s, VLC *vlc)
  772. {
  773.     int code, n, nb_bits, index;
  774.     INT16 *table_codes;
  775.     INT8 *table_bits;
  776.     int bit_cnt;
  777. #ifndef ALT_BITSTREAM_READER
  778.     UINT32 bit_buf;
  779.     UINT8 *buf_ptr;
  780. #endif
  781.  
  782.     SAVE_BITS(s);
  783.     nb_bits = vlc->bits;
  784.     table_codes = vlc->table_codes;
  785.     table_bits = vlc->table_bits;
  786.  
  787. #ifdef FAST_GET_FIRST_VLC
  788.     SHOW_BITS(s, index, nb_bits);
  789.     code = table_codes[index];
  790.     n = table_bits[index];
  791.     if (n > 0) {
  792.         /* most common case (90%)*/
  793.         FLUSH_BITS(n);
  794. #ifdef DUMP_STREAM
  795.         {
  796.             int n= bit_cnt - s->index;
  797.             skip_bits(s, n);
  798.             RESTORE_BITS(s);
  799.         }
  800. #endif
  801.         RESTORE_BITS(s);
  802.         return code;
  803.     } else if (n == 0) {
  804.         return -1;
  805.     } else {
  806.         FLUSH_BITS(nb_bits);
  807.         nb_bits = -n;
  808.         table_codes = vlc->table_codes + code;
  809.         table_bits = vlc->table_bits + code;
  810.     }
  811. #endif
  812.     for(;;) {
  813.         SHOW_BITS(s, index, nb_bits);
  814.         code = table_codes[index];
  815.         n = table_bits[index];
  816.         if (n > 0) {
  817.             /* most common case */
  818.             FLUSH_BITS(n);
  819. #ifdef STATS
  820.             st_bit_counts[st_current_index] += n;
  821. #endif
  822.             break;
  823.         } else if (n == 0) {
  824.             return -1;
  825.         } else {
  826.             FLUSH_BITS(nb_bits);
  827. #ifdef STATS
  828.             st_bit_counts[st_current_index] += nb_bits;
  829. #endif
  830.             nb_bits = -n;
  831.             table_codes = vlc->table_codes + code;
  832.             table_bits = vlc->table_bits + code;
  833.         }
  834.     }
  835. #ifdef DUMP_STREAM
  836.     {
  837.         int n= bit_cnt - s->index;
  838.         skip_bits(s, n);
  839.         RESTORE_BITS(s);
  840.     }
  841. #endif
  842.     RESTORE_BITS(s);
  843.     return code;
  844. }
  845.  
  846.  
  847. /* define it to include statistics code (useful only for optimizing
  848.    codec efficiency */
  849. //#define STATS
  850.  
  851. #ifdef STATS
  852.  
  853. enum {
  854.     ST_UNKNOWN,
  855.     ST_DC,
  856.     ST_INTRA_AC,
  857.     ST_INTER_AC,
  858.     ST_INTRA_MB,
  859.     ST_INTER_MB,
  860.     ST_MV,
  861.     ST_NB,
  862. };
  863.  
  864. extern int st_current_index;
  865. extern unsigned int st_bit_counts[ST_NB];
  866. extern unsigned int st_out_bit_counts[ST_NB];
  867.  
  868. void print_stats(void);
  869. #endif
  870.  
  871. /* misc math functions */
  872.  
  873. static inline int av_log2(unsigned int v)
  874. {
  875.     int n;
  876.  
  877.     n = 0;
  878.     if (v & 0xffff0000) {
  879.         v >>= 16;
  880.         n += 16;
  881.     }
  882.     if (v & 0xff00) {
  883.         v >>= 8;
  884.         n += 8;
  885.     }
  886.     if (v & 0xf0) {
  887.         v >>= 4;
  888.         n += 4;
  889.     }
  890.     if (v & 0xc) {
  891.         v >>= 2;
  892.         n += 2;
  893.     }
  894.     if (v & 0x2) {
  895.         n++;
  896.     }
  897.     return n;
  898. }
  899.  
  900. /* median of 3 */
  901. static inline int mid_pred(int a, int b, int c)
  902. {
  903.     int vmin, vmax;
  904.     vmax = vmin = a;
  905.     if (b < vmin)
  906.         vmin = b;
  907.     else
  908.     vmax = b;
  909.  
  910.     if (c < vmin)
  911.         vmin = c;
  912.     else if (c > vmax)
  913.         vmax = c;
  914.  
  915.     return a + b + c - vmin - vmax;
  916. }
  917.  
  918. static inline int clip(int a, int amin, int amax)
  919. {
  920.     if (a < amin)
  921.         return amin;
  922.     else if (a > amax)
  923.         return amax;
  924.     else
  925.         return a;
  926. }
  927.  
  928. /* math */
  929. int ff_gcd(int a, int b);
  930.  
  931. static inline int ff_sqrt(int a)
  932. {
  933.     int ret=0;
  934.     int s;
  935.     int ret_sq=0;
  936.  
  937.     for(s=15; s>=0; s--){
  938.         int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
  939.         if(b<=a){
  940.             ret_sq=b;
  941.             ret+= 1<<s;
  942.         }
  943.     }
  944.     return ret;
  945. }
  946. #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
  947. #define COPY3_IF_LT(x,y,a,b,c,d)\
  948. asm volatile (\
  949.     "cmpl %0, %3    \n\t"\
  950.     "cmovl %3, %0    \n\t"\
  951.     "cmovl %4, %1    \n\t"\
  952.     "cmovl %5, %2    \n\t"\
  953.     : "+r" (x), "+r" (a), "+r" (c)\
  954.     : "r" (y), "r" (b), "r" (d)\
  955. );
  956. #else
  957. #define COPY3_IF_LT(x,y,a,b,c,d)\
  958. if((y)<(x)){\
  959.      (x)=(y);\
  960.      (a)=(b);\
  961.      (c)=(d);\
  962. }
  963. #endif
  964.  
  965. #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
  966.  
  967. #endif /* HAVE_AV_CONFIG_H */
  968.  
  969. #endif /* COMMON_H */
  970.