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.

378 lines
7.8KB

  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. #endif
  67. #else
  68. /* unix */
  69. #include <inttypes.h>
  70. #ifndef __WINE_WINDEF16_H
  71. /* workaround for typedef conflict in MPlayer (wine typedefs) */
  72. typedef unsigned short UINT16;
  73. typedef signed short INT16;
  74. #endif
  75. typedef unsigned char UINT8;
  76. typedef unsigned int UINT32;
  77. typedef unsigned long long UINT64;
  78. typedef signed char INT8;
  79. typedef signed int INT32;
  80. typedef signed long long INT64;
  81. #ifdef HAVE_AV_CONFIG_H
  82. #ifdef __FreeBSD__
  83. #include <sys/param.h>
  84. #endif
  85. #ifndef INT64_C
  86. #define INT64_C(c) (c ## LL)
  87. #define UINT64_C(c) (c ## ULL)
  88. #endif
  89. #include "../bswap.h"
  90. #ifdef USE_FASTMEMCPY
  91. #include "fastmemcpy.h"
  92. #endif
  93. #endif /* HAVE_AV_CONFIG_H */
  94. #endif /* !CONFIG_WIN32 */
  95. /* debug stuff */
  96. #ifdef HAVE_AV_CONFIG_H
  97. #ifndef DEBUG
  98. #define NDEBUG
  99. #endif
  100. #include <assert.h>
  101. /* dprintf macros */
  102. #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
  103. inline void dprintf(const char* fmt,...) {}
  104. #else
  105. #ifdef DEBUG
  106. #define dprintf(fmt,args...) printf(fmt, ## args)
  107. #else
  108. #define dprintf(fmt,args...)
  109. #endif
  110. #endif /* !CONFIG_WIN32 */
  111. #endif /* HAVE_AV_CONFIG_H */
  112. /* bit output */
  113. struct PutBitContext;
  114. typedef void (*WriteDataFunc)(void *, UINT8 *, int);
  115. typedef struct PutBitContext {
  116. UINT32 bit_buf;
  117. int bit_cnt;
  118. UINT8 *buf, *buf_ptr, *buf_end;
  119. INT64 data_out_size; /* in bytes */
  120. void *opaque;
  121. WriteDataFunc write_data;
  122. } PutBitContext;
  123. void init_put_bits(PutBitContext *s,
  124. UINT8 *buffer, int buffer_size,
  125. void *opaque,
  126. void (*write_data)(void *, UINT8 *, int));
  127. void put_bits(PutBitContext *s, int n, unsigned int value);
  128. INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
  129. void align_put_bits(PutBitContext *s);
  130. void flush_put_bits(PutBitContext *s);
  131. /* jpeg specific put_bits */
  132. void jput_bits(PutBitContext *s, int n, unsigned int value);
  133. void jflush_put_bits(PutBitContext *s);
  134. /* bit input */
  135. typedef struct GetBitContext {
  136. UINT32 bit_buf;
  137. int bit_cnt;
  138. UINT8 *buf, *buf_ptr, *buf_end;
  139. } GetBitContext;
  140. typedef struct VLC {
  141. int bits;
  142. INT16 *table_codes;
  143. INT8 *table_bits;
  144. int table_size, table_allocated;
  145. } VLC;
  146. void init_get_bits(GetBitContext *s,
  147. UINT8 *buffer, int buffer_size);
  148. unsigned int get_bits_long(GetBitContext *s, int n);
  149. unsigned int show_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. /* This function is identical to get_bits(), the only */
  177. /* diference is that it doesn't touch the buffer */
  178. /* it is usefull to see the buffer. */
  179. static inline unsigned int show_bits(GetBitContext *s, int n)
  180. {
  181. if(s->bit_cnt>=n) {
  182. /* most common case here */
  183. unsigned int val = s->bit_buf >> (32 - n);
  184. return val;
  185. }
  186. return show_bits_long(s,n);
  187. }
  188. static inline void skip_bits(GetBitContext *s, int n){
  189. if(s->bit_cnt>=n){
  190. /* most common case here */
  191. s->bit_buf <<= n;
  192. s->bit_cnt -= n;
  193. #ifdef STATS
  194. st_bit_counts[st_current_index] += n;
  195. #endif
  196. } else {
  197. get_bits_long(s,n);
  198. }
  199. }
  200. static inline void skip_bits1(GetBitContext *s){
  201. if(s->bit_cnt>0){
  202. /* most common case here */
  203. s->bit_buf <<= 1;
  204. s->bit_cnt--;
  205. #ifdef STATS
  206. st_bit_counts[st_current_index]++;
  207. #endif
  208. } else {
  209. get_bits_long(s,1);
  210. }
  211. }
  212. static inline int get_bits_count(GetBitContext *s)
  213. {
  214. return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
  215. }
  216. void align_get_bits(GetBitContext *s);
  217. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  218. const void *bits, int bits_wrap, int bits_size,
  219. const void *codes, int codes_wrap, int codes_size);
  220. void free_vlc(VLC *vlc);
  221. int get_vlc(GetBitContext *s, VLC *vlc);
  222. /* macro to go faster */
  223. /* n must be <= 24 */
  224. /* XXX: optimize buffer end test */
  225. #define SHOW_BITS(s, val, n)\
  226. {\
  227. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  228. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  229. bit_cnt += 8;\
  230. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  231. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  232. bit_cnt += 8;\
  233. if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
  234. bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
  235. bit_cnt += 8;\
  236. }\
  237. }\
  238. }\
  239. val = bit_buf >> (32 - n);\
  240. }
  241. /* SHOW_BITS with n1 >= n must be been done before */
  242. #define FLUSH_BITS(n)\
  243. {\
  244. bit_buf <<= n;\
  245. bit_cnt -= n;\
  246. }
  247. #define SAVE_BITS(s) \
  248. {\
  249. bit_cnt = (s)->bit_cnt;\
  250. bit_buf = (s)->bit_buf;\
  251. buf_ptr = (s)->buf_ptr;\
  252. }
  253. #define RESTORE_BITS(s) \
  254. {\
  255. (s)->buf_ptr = buf_ptr;\
  256. (s)->bit_buf = bit_buf;\
  257. (s)->bit_cnt = bit_cnt;\
  258. }
  259. /* define it to include statistics code (useful only for optimizing
  260. codec efficiency */
  261. //#define STATS
  262. #ifdef STATS
  263. enum {
  264. ST_UNKNOWN,
  265. ST_DC,
  266. ST_INTRA_AC,
  267. ST_INTER_AC,
  268. ST_INTRA_MB,
  269. ST_INTER_MB,
  270. ST_MV,
  271. ST_NB,
  272. };
  273. extern int st_current_index;
  274. extern unsigned int st_bit_counts[ST_NB];
  275. extern unsigned int st_out_bit_counts[ST_NB];
  276. void print_stats(void);
  277. #endif
  278. /* misc math functions */
  279. static inline int av_log2(unsigned int v)
  280. {
  281. int n;
  282. n = 0;
  283. if (v & 0xffff0000) {
  284. v >>= 16;
  285. n += 16;
  286. }
  287. if (v & 0xff00) {
  288. v >>= 8;
  289. n += 8;
  290. }
  291. if (v & 0xf0) {
  292. v >>= 4;
  293. n += 4;
  294. }
  295. if (v & 0xc) {
  296. v >>= 2;
  297. n += 2;
  298. }
  299. if (v & 0x2) {
  300. n++;
  301. }
  302. return n;
  303. }
  304. /* memory */
  305. void *av_mallocz(int size);
  306. #endif