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