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