AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
[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 198unsigned int get_bits_long(GetBitContext *s, int n);
4949028f 199unsigned int show_bits_long(GetBitContext *s, int n);
2931ecb9
A
200
201static 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
612476ef
A
215static 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
4949028f
J
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. */
232static 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
612476ef
A
242static 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
255static 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
9dbf9389
FB
268static inline int get_bits_count(GetBitContext *s)
269{
270 return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
271}
612476ef 272
de6d9b64
FB
273void align_get_bits(GetBitContext *s);
274int 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);
277void free_vlc(VLC *vlc);
278int 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
327enum {
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
338extern int st_current_index;
339extern unsigned int st_bit_counts[ST_NB];
340extern unsigned int st_out_bit_counts[ST_NB];
341
342void print_stats(void);
343#endif
344
345/* misc math functions */
346
a822a479 347static inline int av_log2(unsigned int v)
de6d9b64
FB
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 */
375void *av_mallocz(int size);
376
377#endif