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.

365 lines
7.3KB

  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__)
  6. #define CONFIG_WIN32
  7. #endif
  8. #ifdef HAVE_AV_CONFIG_H
  9. /* only include the following when compiling package */
  10. #include "../config.h"
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #ifndef ENODATA
  16. #define ENODATA 61
  17. #endif
  18. #endif
  19. #ifdef CONFIG_WIN32
  20. /* windows */
  21. typedef unsigned short UINT16;
  22. typedef signed short INT16;
  23. typedef unsigned char UINT8;
  24. typedef unsigned int UINT32;
  25. typedef unsigned __int64 UINT64;
  26. typedef signed char INT8;
  27. typedef signed int INT32;
  28. typedef signed __int64 INT64;
  29. typedef UINT8 uint8_t;
  30. typedef INT8 int8_t;
  31. typedef UINT16 uint16_t;
  32. typedef INT16 int16_t;
  33. typedef UINT32 uint32_t;
  34. typedef INT32 int32_t;
  35. #ifndef __MINGW32__
  36. #define INT64_C(c) (c ## i64)
  37. #define UINT64_C(c) (c ## i64)
  38. #define inline __inline
  39. /*
  40. Disable warning messages:
  41. warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
  42. warning C4305: 'argument' : truncation from 'const double' to 'float'
  43. */
  44. #pragma warning( disable : 4244 )
  45. #pragma warning( disable : 4305 )
  46. #else
  47. #define INT64_C(c) (c ## LL)
  48. #define UINT64_C(c) (c ## ULL)
  49. #endif /* __MINGW32__ */
  50. #define M_PI 3.14159265358979323846
  51. #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  52. #ifdef _DEBUG
  53. #define DEBUG
  54. #endif
  55. // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
  56. #define bswap_32(x) \
  57. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  58. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  59. #define be2me_32(x) bswap_32(x)
  60. #define snprintf _snprintf
  61. #ifndef __MINGW32__
  62. /* no config.h with VC */
  63. #define CONFIG_ENCODERS 1
  64. #define CONFIG_DECODERS 1
  65. #define CONFIG_AC3 1
  66. #define CONFIG_MPGLIB 1
  67. #endif
  68. #else
  69. /* unix */
  70. #include <inttypes.h>
  71. #ifndef __WINE_WINDEF16_H
  72. /* workaround for typedef conflict in MPlayer (wine typedefs) */
  73. typedef unsigned short UINT16;
  74. typedef signed short INT16;
  75. #endif
  76. typedef unsigned char UINT8;
  77. typedef unsigned int UINT32;
  78. typedef unsigned long long UINT64;
  79. typedef signed char INT8;
  80. typedef signed int INT32;
  81. typedef signed long long INT64;
  82. #ifdef HAVE_AV_CONFIG_H
  83. #ifdef __FreeBSD__
  84. #include <sys/param.h>
  85. #endif
  86. #ifndef INT64_C
  87. #define INT64_C(c) (c ## LL)
  88. #define UINT64_C(c) (c ## ULL)
  89. #endif
  90. #include "../bswap.h"
  91. #ifdef USE_FASTMEMCPY
  92. #include "fastmemcpy.h"
  93. #endif
  94. #endif /* HAVE_AV_CONFIG_H */
  95. #endif /* !CONFIG_WIN32 */
  96. /* debug stuff */
  97. #ifdef HAVE_AV_CONFIG_H
  98. #ifndef DEBUG
  99. #define NDEBUG
  100. #endif
  101. #include <assert.h>
  102. /* dprintf macros */
  103. #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
  104. inline void dprintf(const char* fmt,...) {}
  105. #else
  106. #ifdef DEBUG
  107. #define dprintf(fmt,args...) printf(fmt, ## args)
  108. #else
  109. #define dprintf(fmt,args...)
  110. #endif
  111. #endif /* !CONFIG_WIN32 */
  112. #endif /* HAVE_AV_CONFIG_H */
  113. /* bit output */
  114. struct PutBitContext;
  115. typedef void (*WriteDataFunc)(void *, UINT8 *, int);
  116. typedef struct PutBitContext {
  117. UINT32 bit_buf;
  118. int bit_cnt;
  119. UINT8 *buf, *buf_ptr, *buf_end;
  120. INT64 data_out_size; /* in bytes */
  121. void *opaque;
  122. WriteDataFunc write_data;
  123. } PutBitContext;
  124. void init_put_bits(PutBitContext *s,
  125. UINT8 *buffer, int buffer_size,
  126. void *opaque,
  127. void (*write_data)(void *, UINT8 *, int));
  128. void put_bits(PutBitContext *s, int n, unsigned int value);
  129. INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
  130. void align_put_bits(PutBitContext *s);
  131. void flush_put_bits(PutBitContext *s);
  132. /* jpeg specific put_bits */
  133. void jput_bits(PutBitContext *s, int n, unsigned int value);
  134. void jflush_put_bits(PutBitContext *s);
  135. /* bit input */
  136. typedef struct GetBitContext {
  137. UINT32 bit_buf;
  138. int bit_cnt;
  139. UINT8 *buf, *buf_ptr, *buf_end;
  140. } GetBitContext;
  141. typedef struct VLC {
  142. int bits;
  143. INT16 *table_codes;
  144. INT8 *table_bits;
  145. int table_size, table_allocated;
  146. } VLC;
  147. void init_get_bits(GetBitContext *s,
  148. UINT8 *buffer, int buffer_size);
  149. unsigned int get_bits_long(GetBitContext *s, int n);
  150. static inline unsigned int get_bits(GetBitContext *s, int n){
  151. if(s->bit_cnt>=n){
  152. /* most common case here */
  153. unsigned int val = s->bit_buf >> (32 - n);
  154. s->bit_buf <<= n;
  155. s->bit_cnt -= n;
  156. #ifdef STATS
  157. st_bit_counts[st_current_index] += n;
  158. #endif
  159. return val;
  160. }
  161. return get_bits_long(s,n);
  162. }
  163. static inline unsigned int get_bits1(GetBitContext *s){
  164. if(s->bit_cnt>0){
  165. /* most common case here */
  166. unsigned int val = s->bit_buf >> 31;
  167. s->bit_buf <<= 1;
  168. s->bit_cnt--;
  169. #ifdef STATS
  170. st_bit_counts[st_current_index]++;
  171. #endif
  172. return val;
  173. }
  174. return get_bits_long(s,1);
  175. }
  176. static inline void skip_bits(GetBitContext *s, int n){
  177. if(s->bit_cnt>=n){
  178. /* most common case here */
  179. s->bit_buf <<= n;
  180. s->bit_cnt -= n;
  181. #ifdef STATS
  182. st_bit_counts[st_current_index] += n;
  183. #endif
  184. } else {
  185. get_bits_long(s,n);
  186. }
  187. }
  188. static inline void skip_bits1(GetBitContext *s){
  189. if(s->bit_cnt>0){
  190. /* most common case here */
  191. s->bit_buf <<= 1;
  192. s->bit_cnt--;
  193. #ifdef STATS
  194. st_bit_counts[st_current_index]++;
  195. #endif
  196. } else {
  197. get_bits_long(s,1);
  198. }
  199. }
  200. static inline int get_bits_count(GetBitContext *s)
  201. {
  202. return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
  203. }
  204. void align_get_bits(GetBitContext *s);
  205. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  206. const void *bits, int bits_wrap, int bits_size,
  207. const void *codes, int codes_wrap, int codes_size);
  208. void free_vlc(VLC *vlc);
  209. int get_vlc(GetBitContext *s, VLC *vlc);
  210. /* macro to go faster */
  211. /* n must be <= 24 */
  212. /* XXX: optimize buffer end test */
  213. #define SHOW_BITS(s, val, n)\
  214. {\
  215. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  216. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  217. bit_cnt += 8;\
  218. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  219. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  220. bit_cnt += 8;\
  221. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  222. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  223. bit_cnt += 8;\
  224. }\
  225. }\
  226. }\
  227. val = bit_buf >> (32 - n);\
  228. }
  229. /* SHOW_BITS with n1 >= n must be been done before */
  230. #define FLUSH_BITS(n)\
  231. {\
  232. bit_buf <<= n;\
  233. bit_cnt -= n;\
  234. }
  235. #define SAVE_BITS(s) \
  236. {\
  237. bit_cnt = (s)->bit_cnt;\
  238. bit_buf = (s)->bit_buf;\
  239. buf_ptr = (s)->buf_ptr;\
  240. }
  241. #define RESTORE_BITS(s) \
  242. {\
  243. (s)->buf_ptr = buf_ptr;\
  244. (s)->bit_buf = bit_buf;\
  245. (s)->bit_cnt = bit_cnt;\
  246. }
  247. /* define it to include statistics code (useful only for optimizing
  248. codec efficiency */
  249. //#define STATS
  250. #ifdef STATS
  251. enum {
  252. ST_UNKNOWN,
  253. ST_DC,
  254. ST_INTRA_AC,
  255. ST_INTER_AC,
  256. ST_INTRA_MB,
  257. ST_INTER_MB,
  258. ST_MV,
  259. ST_NB,
  260. };
  261. extern int st_current_index;
  262. extern unsigned int st_bit_counts[ST_NB];
  263. extern unsigned int st_out_bit_counts[ST_NB];
  264. void print_stats(void);
  265. #endif
  266. /* misc math functions */
  267. extern inline int av_log2(unsigned int v)
  268. {
  269. int n;
  270. n = 0;
  271. if (v & 0xffff0000) {
  272. v >>= 16;
  273. n += 16;
  274. }
  275. if (v & 0xff00) {
  276. v >>= 8;
  277. n += 8;
  278. }
  279. if (v & 0xf0) {
  280. v >>= 4;
  281. n += 4;
  282. }
  283. if (v & 0xc) {
  284. v >>= 2;
  285. n += 2;
  286. }
  287. if (v & 0x2) {
  288. n++;
  289. }
  290. return n;
  291. }
  292. /* memory */
  293. void *av_mallocz(int size);
  294. #endif