]> git.sesse.net Git - ffmpeg/blob - libavcodec/common.h
- Bug fix on inter MCBPC table for inter+q.
[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 unsigned int show_bits_long(GetBitContext *s, int n);
200
201 static inline unsigned int get_bits(GetBitContext *s, int n){
202     if(s->bit_cnt>=n){
203         /* most common case here */
204         unsigned int val = s->bit_buf >> (32 - n);
205         s->bit_buf <<= n;
206         s->bit_cnt -= n;
207 #ifdef STATS
208         st_bit_counts[st_current_index] += n;
209 #endif
210         return val;
211     }
212     return get_bits_long(s,n);
213 }
214
215 static inline unsigned int get_bits1(GetBitContext *s){
216     if(s->bit_cnt>0){
217         /* most common case here */
218         unsigned int val = s->bit_buf >> 31;
219         s->bit_buf <<= 1;
220         s->bit_cnt--;
221 #ifdef STATS
222         st_bit_counts[st_current_index]++;
223 #endif
224         return val;
225     }
226     return get_bits_long(s,1);
227 }
228
229 /* This function is identical to get_bits(), the only */
230 /* diference is that it doesn't touch the buffer      */
231 /* it is usefull to see the buffer.                   */
232 static inline unsigned int show_bits(GetBitContext *s, int n)
233 {
234     if(s->bit_cnt>=n) {
235         /* most common case here */
236         unsigned int val = s->bit_buf >> (32 - n);
237         return val;
238     }
239     return show_bits_long(s,n);
240 }
241
242 static inline void skip_bits(GetBitContext *s, int n){
243     if(s->bit_cnt>=n){
244         /* most common case here */
245         s->bit_buf <<= n;
246         s->bit_cnt -= n;
247 #ifdef STATS
248         st_bit_counts[st_current_index] += n;
249 #endif
250     } else {
251         get_bits_long(s,n);
252     }
253 }
254
255 static inline void skip_bits1(GetBitContext *s){
256     if(s->bit_cnt>0){
257         /* most common case here */
258         s->bit_buf <<= 1;
259         s->bit_cnt--;
260 #ifdef STATS
261         st_bit_counts[st_current_index]++;
262 #endif
263     } else {
264         get_bits_long(s,1);
265     }
266 }
267
268 static inline int get_bits_count(GetBitContext *s)
269 {
270     return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
271 }
272
273 void align_get_bits(GetBitContext *s);
274 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
275              const void *bits, int bits_wrap, int bits_size,
276              const void *codes, int codes_wrap, int codes_size);
277 void free_vlc(VLC *vlc);
278 int get_vlc(GetBitContext *s, VLC *vlc);
279
280 /* macro to go faster */
281 /* n must be <= 24 */
282 /* XXX: optimize buffer end test */
283 #define SHOW_BITS(s, val, n)\
284 {\
285     if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
286         bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
287         bit_cnt += 8;\
288         if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
289             bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
290             bit_cnt += 8;\
291             if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
292                 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
293                 bit_cnt += 8;\
294             }\
295         }\
296     }\
297     val = bit_buf >> (32 - n);\
298 }
299
300 /* SHOW_BITS with n1 >= n must be been done before */
301 #define FLUSH_BITS(n)\
302 {\
303     bit_buf <<= n;\
304     bit_cnt -= n;\
305 }
306
307 #define SAVE_BITS(s) \
308 {\
309     bit_cnt = (s)->bit_cnt;\
310     bit_buf = (s)->bit_buf;\
311     buf_ptr = (s)->buf_ptr;\
312 }
313
314 #define RESTORE_BITS(s) \
315 {\
316     (s)->buf_ptr = buf_ptr;\
317     (s)->bit_buf = bit_buf;\
318     (s)->bit_cnt = bit_cnt;\
319 }
320
321 /* define it to include statistics code (useful only for optimizing
322    codec efficiency */
323 //#define STATS
324
325 #ifdef STATS
326
327 enum {
328     ST_UNKNOWN,
329     ST_DC,
330     ST_INTRA_AC,
331     ST_INTER_AC,
332     ST_INTRA_MB,
333     ST_INTER_MB,
334     ST_MV,
335     ST_NB,
336 };
337
338 extern int st_current_index;
339 extern unsigned int st_bit_counts[ST_NB];
340 extern unsigned int st_out_bit_counts[ST_NB];
341
342 void print_stats(void);
343 #endif
344
345 /* misc math functions */
346
347 extern inline int av_log2(unsigned int v)
348 {
349     int n;
350
351     n = 0;
352     if (v & 0xffff0000) {
353         v >>= 16;
354         n += 16;
355     }
356     if (v & 0xff00) {
357         v >>= 8;
358         n += 8;
359     }
360     if (v & 0xf0) {
361         v >>= 4;
362         n += 4;
363     }
364     if (v & 0xc) {
365         v >>= 2;
366         n += 2;
367     }
368     if (v & 0x2) {
369         n++;
370     }
371     return n;
372 }
373
374 /* memory */
375 void *av_mallocz(int size);
376
377 #endif