]> git.sesse.net Git - ffmpeg/blob - libavcodec/common.h
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer...
[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 //#define ALT_BITSTREAM_READER
12
13 #ifdef HAVE_AV_CONFIG_H
14 /* only include the following when compiling package */
15 #include "../config.h"
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
32 typedef unsigned short UINT16;
33 typedef signed short INT16;
34 typedef unsigned char UINT8;
35 typedef unsigned int UINT32;
36 typedef unsigned __int64 UINT64;
37 typedef signed char INT8;
38 typedef signed int INT32;
39 typedef signed __int64 INT64;
40
41 typedef UINT8 uint8_t;
42 typedef INT8 int8_t;
43 typedef UINT16 uint16_t;
44 typedef INT16 int16_t;
45 typedef UINT32 uint32_t;
46 typedef INT32 int32_t;
47
48 #ifndef __MINGW32__
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
62 #else
63 #define INT64_C(c)     (c ## LL)
64 #define UINT64_C(c)    (c ## ULL)
65 #endif /* __MINGW32__ */
66
67 #define M_PI    3.14159265358979323846
68 #define M_SQRT2 1.41421356237309504880  /* sqrt(2) */
69
70 #ifdef _DEBUG
71 #define DEBUG
72 #endif
73
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
82 #ifndef __MINGW32__
83 /* no config.h with VC */
84 #define CONFIG_ENCODERS 1
85 #define CONFIG_DECODERS 1
86 #define CONFIG_AC3      1
87 #endif
88
89 #else
90
91 /* unix */
92
93 #include <inttypes.h>
94
95 #ifndef __WINE_WINDEF16_H
96 /* workaround for typedef conflict in MPlayer (wine typedefs) */
97 typedef unsigned short UINT16;
98 typedef signed short INT16;
99 #endif
100
101 typedef unsigned char UINT8;
102 typedef unsigned int UINT32;
103 typedef unsigned long long UINT64;
104 typedef signed char INT8;
105 typedef signed int INT32;
106 typedef signed long long INT64;
107
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
125 #endif /* HAVE_AV_CONFIG_H */
126
127 #endif /* !CONFIG_WIN32 */
128
129
130 /* debug stuff */
131 #ifdef HAVE_AV_CONFIG_H
132
133 #ifndef DEBUG
134 #define NDEBUG
135 #endif
136 #include <assert.h>
137
138 /* dprintf macros */
139 #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
140
141 inline 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
150
151 #endif /* !CONFIG_WIN32 */
152
153 #endif /* HAVE_AV_CONFIG_H */
154
155 /* bit output */
156
157 struct PutBitContext;
158
159 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
160
161 typedef struct PutBitContext {
162     UINT32 bit_buf;
163     int bit_cnt;
164     UINT8 *buf, *buf_ptr, *buf_end;
165     INT64 data_out_size; /* in bytes */
166     void *opaque;
167     WriteDataFunc write_data;
168 } PutBitContext;
169
170 void init_put_bits(PutBitContext *s, 
171                    UINT8 *buffer, int buffer_size,
172                    void *opaque,
173                    void (*write_data)(void *, UINT8 *, int));
174 void put_bits(PutBitContext *s, int n, unsigned int value);
175 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
176 void align_put_bits(PutBitContext *s);
177 void flush_put_bits(PutBitContext *s);
178
179 /* jpeg specific put_bits */
180 void jput_bits(PutBitContext *s, int n, unsigned int value);
181 void jflush_put_bits(PutBitContext *s);
182
183 /* bit input */
184
185 typedef struct GetBitContext {
186 #ifdef ALT_BITSTREAM_READER
187     int index;
188     UINT8 *buffer;
189 #else
190     UINT32 bit_buf;
191     int bit_cnt;
192     UINT8 *buf, *buf_ptr, *buf_end;
193 #endif
194 } GetBitContext;
195
196 typedef struct VLC {
197     int bits;
198     INT16 *table_codes;
199     INT8 *table_bits;
200     int table_size, table_allocated;
201 } VLC;
202
203 void init_get_bits(GetBitContext *s, 
204                    UINT8 *buffer, int buffer_size);
205
206 #ifndef ALT_BITSTREAM_READER
207 unsigned int get_bits_long(GetBitContext *s, int n);
208 unsigned int show_bits_long(GetBitContext *s, int n);
209 #endif
210
211 static inline unsigned int get_bits(GetBitContext *s, int n){
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
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);
234 #endif
235 }
236
237 static inline unsigned int get_bits1(GetBitContext *s){
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
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);
260 #endif
261 }
262
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.                   */
266 static inline unsigned int show_bits(GetBitContext *s, int n)
267 {
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
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);
283 #endif
284 }
285
286 static inline void skip_bits(GetBitContext *s, int n){
287 #ifdef ALT_BITSTREAM_READER
288     s->index+= n;
289 #else
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     }
300 #endif
301 }
302
303 static inline void skip_bits1(GetBitContext *s){
304 #ifdef ALT_BITSTREAM_READER
305     s->index++;
306 #else
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     }
317 #endif
318 }
319
320 static inline int get_bits_count(GetBitContext *s)
321 {
322 #ifdef ALT_BITSTREAM_READER
323     return s->index;
324 #else
325     return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
326 #endif
327 }
328
329 void align_get_bits(GetBitContext *s);
330 int 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);
333 void free_vlc(VLC *vlc);
334 int get_vlc(GetBitContext *s, VLC *vlc);
335
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
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 }
383 #endif // !ALT_BITSTREAM_READER
384 /* define it to include statistics code (useful only for optimizing
385    codec efficiency */
386 //#define STATS
387
388 #ifdef STATS
389
390 enum {
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
401 extern int st_current_index;
402 extern unsigned int st_bit_counts[ST_NB];
403 extern unsigned int st_out_bit_counts[ST_NB];
404
405 void print_stats(void);
406 #endif
407
408 /* misc math functions */
409
410 static inline int av_log2(unsigned int v)
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 */
438 void *av_mallocz(int size);
439
440 #endif