]> git.sesse.net Git - ffmpeg/blob - libavcodec/common.h
added get_bits_count()
[ffmpeg] / libavcodec / common.h
1 #ifndef COMMON_H
2 #define COMMON_H
3
4 #define FFMPEG_VERSION_INT 0x000405
5 #define FFMPEG_VERSION     "0.4.5"
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 #define CONFIG_MPGLIB   1
86 #endif
87
88 #else
89
90 /* unix */
91
92 #include <inttypes.h>
93
94 #ifndef __WINE_WINDEF16_H
95 /* workaround for typedef conflict in MPlayer (wine typedefs) */
96 typedef unsigned short UINT16;
97 typedef signed short INT16;
98 #endif
99
100 typedef unsigned char UINT8;
101 typedef unsigned int UINT32;
102 typedef unsigned long long UINT64;
103 typedef signed char INT8;
104 typedef signed int INT32;
105 typedef signed long long INT64;
106
107 #ifdef HAVE_AV_CONFIG_H
108
109 #ifdef __FreeBSD__
110 #include <sys/param.h>
111 #endif
112
113 #ifndef INT64_C
114 #define INT64_C(c)     (c ## LL)
115 #define UINT64_C(c)    (c ## ULL)
116 #endif
117
118 #include "../bswap.h"
119
120 #ifdef USE_FASTMEMCPY
121 #include "fastmemcpy.h"
122 #endif
123
124 #endif /* HAVE_AV_CONFIG_H */
125
126 #endif /* !CONFIG_WIN32 */
127
128 /* debug stuff */
129 #ifdef HAVE_AV_CONFIG_H
130
131 #ifndef DEBUG
132 #define NDEBUG
133 #endif
134 #include <assert.h>
135
136 /* dprintf macros */
137 #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
138
139 inline void dprintf(const char* fmt,...) {}
140
141 #else
142
143 #ifdef DEBUG
144 #define dprintf(fmt,args...) printf(fmt, ## args)
145 #else
146 #define dprintf(fmt,args...)
147 #endif
148
149 #endif /* !CONFIG_WIN32 */
150
151 #endif /* HAVE_AV_CONFIG_H */
152
153 /* bit output */
154
155 struct PutBitContext;
156
157 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
158
159 typedef struct PutBitContext {
160     UINT32 bit_buf;
161     int bit_cnt;
162     UINT8 *buf, *buf_ptr, *buf_end;
163     INT64 data_out_size; /* in bytes */
164     void *opaque;
165     WriteDataFunc write_data;
166 } PutBitContext;
167
168 void init_put_bits(PutBitContext *s, 
169                    UINT8 *buffer, int buffer_size,
170                    void *opaque,
171                    void (*write_data)(void *, UINT8 *, int));
172 void put_bits(PutBitContext *s, int n, unsigned int value);
173 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
174 void align_put_bits(PutBitContext *s);
175 void flush_put_bits(PutBitContext *s);
176
177 /* jpeg specific put_bits */
178 void jput_bits(PutBitContext *s, int n, unsigned int value);
179 void jflush_put_bits(PutBitContext *s);
180
181 /* bit input */
182
183 typedef struct GetBitContext {
184     UINT32 bit_buf;
185     int bit_cnt;
186     UINT8 *buf, *buf_ptr, *buf_end;
187 } GetBitContext;
188
189 typedef struct VLC {
190     int bits;
191     INT16 *table_codes;
192     INT8 *table_bits;
193     int table_size, table_allocated;
194 } VLC;
195
196 void init_get_bits(GetBitContext *s, 
197                    UINT8 *buffer, int buffer_size);
198
199 unsigned int get_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 static inline void skip_bits(GetBitContext *s, int n){
230     if(s->bit_cnt>=n){
231         /* most common case here */
232         s->bit_buf <<= n;
233         s->bit_cnt -= n;
234 #ifdef STATS
235         st_bit_counts[st_current_index] += n;
236 #endif
237     } else {
238         get_bits_long(s,n);
239     }
240 }
241
242 static inline void skip_bits1(GetBitContext *s){
243     if(s->bit_cnt>0){
244         /* most common case here */
245         s->bit_buf <<= 1;
246         s->bit_cnt--;
247 #ifdef STATS
248         st_bit_counts[st_current_index]++;
249 #endif
250     } else {
251         get_bits_long(s,1);
252     }
253 }
254
255 static inline int get_bits_count(GetBitContext *s)
256 {
257     return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
258 }
259
260 void align_get_bits(GetBitContext *s);
261 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
262              const void *bits, int bits_wrap, int bits_size,
263              const void *codes, int codes_wrap, int codes_size);
264 void free_vlc(VLC *vlc);
265 int get_vlc(GetBitContext *s, VLC *vlc);
266
267 /* macro to go faster */
268 /* n must be <= 24 */
269 /* XXX: optimize buffer end test */
270 #define SHOW_BITS(s, val, n)\
271 {\
272     if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
273         bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
274         bit_cnt += 8;\
275         if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
276             bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
277             bit_cnt += 8;\
278             if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
279                 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
280                 bit_cnt += 8;\
281             }\
282         }\
283     }\
284     val = bit_buf >> (32 - n);\
285 }
286
287 /* SHOW_BITS with n1 >= n must be been done before */
288 #define FLUSH_BITS(n)\
289 {\
290     bit_buf <<= n;\
291     bit_cnt -= n;\
292 }
293
294 #define SAVE_BITS(s) \
295 {\
296     bit_cnt = (s)->bit_cnt;\
297     bit_buf = (s)->bit_buf;\
298     buf_ptr = (s)->buf_ptr;\
299 }
300
301 #define RESTORE_BITS(s) \
302 {\
303     (s)->buf_ptr = buf_ptr;\
304     (s)->bit_buf = bit_buf;\
305     (s)->bit_cnt = bit_cnt;\
306 }
307
308 /* define it to include statistics code (useful only for optimizing
309    codec efficiency */
310 //#define STATS
311
312 #ifdef STATS
313
314 enum {
315     ST_UNKNOWN,
316     ST_DC,
317     ST_INTRA_AC,
318     ST_INTER_AC,
319     ST_INTRA_MB,
320     ST_INTER_MB,
321     ST_MV,
322     ST_NB,
323 };
324
325 extern int st_current_index;
326 extern unsigned int st_bit_counts[ST_NB];
327 extern unsigned int st_out_bit_counts[ST_NB];
328
329 void print_stats(void);
330 #endif
331
332 /* misc math functions */
333
334 extern inline int av_log2(unsigned int v)
335 {
336     int n;
337
338     n = 0;
339     if (v & 0xffff0000) {
340         v >>= 16;
341         n += 16;
342     }
343     if (v & 0xff00) {
344         v >>= 8;
345         n += 8;
346     }
347     if (v & 0xf0) {
348         v >>= 4;
349         n += 4;
350     }
351     if (v & 0xc) {
352         v >>= 2;
353         n += 2;
354     }
355     if (v & 0x2) {
356         n++;
357     }
358     return n;
359 }
360
361 /* memory */
362 void *av_mallocz(int size);
363
364 #endif