right green line bugfix for width not %8==0 (untested -vo vesa doesnt work)
[libav.git] / libavcodec / common.h
CommitLineData
de6d9b64
FB
1#ifndef COMMON_H
2#define COMMON_H
3
73120e55
FB
4#define FFMPEG_VERSION_INT 0x000406
5#define FFMPEG_VERSION "0.4.6"
1a565432 6
a74127c0 7#if defined(WIN32) && !defined(__MINGW32__)
1a565432
FB
8#define CONFIG_WIN32
9#endif
10
420b073b 11#ifdef HAVE_AV_CONFIG_H
1a565432 12/* only include the following when compiling package */
de6d9b64 13#include "../config.h"
1a565432
FB
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
30typedef unsigned short UINT16;
31typedef signed short INT16;
32typedef unsigned char UINT8;
33typedef unsigned int UINT32;
34typedef unsigned __int64 UINT64;
35typedef signed char INT8;
36typedef signed int INT32;
37typedef signed __int64 INT64;
38
39typedef UINT8 uint8_t;
40typedef INT8 int8_t;
41typedef UINT16 uint16_t;
42typedef INT16 int16_t;
43typedef UINT32 uint32_t;
44typedef INT32 int32_t;
45
a74127c0 46#ifndef __MINGW32__
1a565432
FB
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
a74127c0
FB
60#else
61#define INT64_C(c) (c ## LL)
62#define UINT64_C(c) (c ## ULL)
63#endif /* __MINGW32__ */
64
1a565432
FB
65#define M_PI 3.14159265358979323846
66#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
67
68#ifdef _DEBUG
69#define DEBUG
420b073b 70#endif
de6d9b64 71
1a565432
FB
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
a74127c0
FB
80#ifndef __MINGW32__
81/* no config.h with VC */
1a565432
FB
82#define CONFIG_ENCODERS 1
83#define CONFIG_DECODERS 1
84#define CONFIG_AC3 1
a74127c0 85#endif
1a565432
FB
86
87#else
88
89/* unix */
90
91#include <inttypes.h>
92
980fc7b8
FB
93#ifndef __WINE_WINDEF16_H
94/* workaround for typedef conflict in MPlayer (wine typedefs) */
de6d9b64
FB
95typedef unsigned short UINT16;
96typedef signed short INT16;
97#endif
98
99typedef unsigned char UINT8;
100typedef unsigned int UINT32;
101typedef unsigned long long UINT64;
102typedef signed char INT8;
103typedef signed int INT32;
104typedef signed long long INT64;
105
1a565432
FB
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
a74127c0
FB
123#endif /* HAVE_AV_CONFIG_H */
124
125#endif /* !CONFIG_WIN32 */
126
127/* debug stuff */
128#ifdef HAVE_AV_CONFIG_H
129
1a565432
FB
130#ifndef DEBUG
131#define NDEBUG
132#endif
133#include <assert.h>
134
a74127c0
FB
135/* dprintf macros */
136#if defined(CONFIG_WIN32) && !defined(__MINGW32__)
137
138inline 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
1a565432
FB
147
148#endif /* !CONFIG_WIN32 */
149
a74127c0
FB
150#endif /* HAVE_AV_CONFIG_H */
151
de6d9b64
FB
152/* bit output */
153
154struct PutBitContext;
155
156typedef void (*WriteDataFunc)(void *, UINT8 *, int);
157
158typedef struct PutBitContext {
de6d9b64 159 UINT32 bit_buf;
2931ecb9
A
160 int bit_cnt;
161 UINT8 *buf, *buf_ptr, *buf_end;
1a565432 162 INT64 data_out_size; /* in bytes */
de6d9b64
FB
163 void *opaque;
164 WriteDataFunc write_data;
165} PutBitContext;
166
167void init_put_bits(PutBitContext *s,
168 UINT8 *buffer, int buffer_size,
169 void *opaque,
170 void (*write_data)(void *, UINT8 *, int));
171void put_bits(PutBitContext *s, int n, unsigned int value);
9dbf9389 172INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
de6d9b64
FB
173void align_put_bits(PutBitContext *s);
174void flush_put_bits(PutBitContext *s);
175
176/* jpeg specific put_bits */
177void jput_bits(PutBitContext *s, int n, unsigned int value);
178void jflush_put_bits(PutBitContext *s);
179
180/* bit input */
181
182typedef struct GetBitContext {
de6d9b64 183 UINT32 bit_buf;
2931ecb9
A
184 int bit_cnt;
185 UINT8 *buf, *buf_ptr, *buf_end;
de6d9b64
FB
186} GetBitContext;
187
188typedef struct VLC {
189 int bits;
190 INT16 *table_codes;
191 INT8 *table_bits;
192 int table_size, table_allocated;
193} VLC;
194
195void init_get_bits(GetBitContext *s,
196 UINT8 *buffer, int buffer_size);
197
2931ecb9
A
198unsigned int get_bits_long(GetBitContext *s, int n);
199
200static 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
612476ef
A
214static 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
228static 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
241static 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
9dbf9389
FB
254static inline int get_bits_count(GetBitContext *s)
255{
256 return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
257}
612476ef 258
de6d9b64
FB
259void align_get_bits(GetBitContext *s);
260int 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);
263void free_vlc(VLC *vlc);
264int 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
313enum {
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
324extern int st_current_index;
325extern unsigned int st_bit_counts[ST_NB];
326extern unsigned int st_out_bit_counts[ST_NB];
327
328void print_stats(void);
329#endif
330
331/* misc math functions */
332
0617e073 333extern inline int av_log2(unsigned int v)
de6d9b64
FB
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 */
361void *av_mallocz(int size);
362
363#endif