better vol header parsing for mpeg4
[libav.git] / libavcodec / common.h
CommitLineData
de6d9b64
FB
1#ifndef COMMON_H
2#define COMMON_H
3
420b073b 4#ifdef HAVE_AV_CONFIG_H
de6d9b64 5#include "../config.h"
420b073b 6#endif
de6d9b64 7
980fc7b8
FB
8#ifndef __WINE_WINDEF16_H
9/* workaround for typedef conflict in MPlayer (wine typedefs) */
de6d9b64
FB
10typedef unsigned short UINT16;
11typedef signed short INT16;
12#endif
13
14typedef unsigned char UINT8;
15typedef unsigned int UINT32;
16typedef unsigned long long UINT64;
17typedef signed char INT8;
18typedef signed int INT32;
19typedef signed long long INT64;
20
21/* bit output */
22
23struct PutBitContext;
24
25typedef void (*WriteDataFunc)(void *, UINT8 *, int);
26
27typedef struct PutBitContext {
de6d9b64 28 UINT32 bit_buf;
2931ecb9
A
29 int bit_cnt;
30 UINT8 *buf, *buf_ptr, *buf_end;
de6d9b64
FB
31 long long data_out_size; /* in bytes */
32 void *opaque;
33 WriteDataFunc write_data;
34} PutBitContext;
35
36void init_put_bits(PutBitContext *s,
37 UINT8 *buffer, int buffer_size,
38 void *opaque,
39 void (*write_data)(void *, UINT8 *, int));
40void put_bits(PutBitContext *s, int n, unsigned int value);
41long long get_bit_count(PutBitContext *s);
42void align_put_bits(PutBitContext *s);
43void flush_put_bits(PutBitContext *s);
44
45/* jpeg specific put_bits */
46void jput_bits(PutBitContext *s, int n, unsigned int value);
47void jflush_put_bits(PutBitContext *s);
48
49/* bit input */
50
51typedef struct GetBitContext {
de6d9b64 52 UINT32 bit_buf;
2931ecb9
A
53 int bit_cnt;
54 UINT8 *buf, *buf_ptr, *buf_end;
de6d9b64
FB
55} GetBitContext;
56
57typedef struct VLC {
58 int bits;
59 INT16 *table_codes;
60 INT8 *table_bits;
61 int table_size, table_allocated;
62} VLC;
63
64void init_get_bits(GetBitContext *s,
65 UINT8 *buffer, int buffer_size);
66
2931ecb9
A
67unsigned int get_bits_long(GetBitContext *s, int n);
68
69static inline unsigned int get_bits(GetBitContext *s, int n){
70 if(s->bit_cnt>=n){
71 /* most common case here */
72 unsigned int val = s->bit_buf >> (32 - n);
73 s->bit_buf <<= n;
74 s->bit_cnt -= n;
75#ifdef STATS
76 st_bit_counts[st_current_index] += n;
77#endif
78 return val;
79 }
80 return get_bits_long(s,n);
81}
82
612476ef
A
83static inline unsigned int get_bits1(GetBitContext *s){
84 if(s->bit_cnt>0){
85 /* most common case here */
86 unsigned int val = s->bit_buf >> 31;
87 s->bit_buf <<= 1;
88 s->bit_cnt--;
89#ifdef STATS
90 st_bit_counts[st_current_index]++;
91#endif
92 return val;
93 }
94 return get_bits_long(s,1);
95}
96
97static inline void skip_bits(GetBitContext *s, int n){
98 if(s->bit_cnt>=n){
99 /* most common case here */
100 s->bit_buf <<= n;
101 s->bit_cnt -= n;
102#ifdef STATS
103 st_bit_counts[st_current_index] += n;
104#endif
105 } else {
106 get_bits_long(s,n);
107 }
108}
109
110static inline void skip_bits1(GetBitContext *s){
111 if(s->bit_cnt>0){
112 /* most common case here */
113 s->bit_buf <<= 1;
114 s->bit_cnt--;
115#ifdef STATS
116 st_bit_counts[st_current_index]++;
117#endif
118 } else {
119 get_bits_long(s,1);
120 }
121}
122
123
de6d9b64
FB
124void align_get_bits(GetBitContext *s);
125int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
126 const void *bits, int bits_wrap, int bits_size,
127 const void *codes, int codes_wrap, int codes_size);
128void free_vlc(VLC *vlc);
129int get_vlc(GetBitContext *s, VLC *vlc);
130
131/* macro to go faster */
132/* n must be <= 24 */
133/* XXX: optimize buffer end test */
134#define SHOW_BITS(s, val, n)\
135{\
136 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
137 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
138 bit_cnt += 8;\
139 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
140 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
141 bit_cnt += 8;\
142 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
143 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
144 bit_cnt += 8;\
145 }\
146 }\
147 }\
148 val = bit_buf >> (32 - n);\
149}
150
151/* SHOW_BITS with n1 >= n must be been done before */
152#define FLUSH_BITS(n)\
153{\
154 bit_buf <<= n;\
155 bit_cnt -= n;\
156}
157
158#define SAVE_BITS(s) \
159{\
160 bit_cnt = (s)->bit_cnt;\
161 bit_buf = (s)->bit_buf;\
162 buf_ptr = (s)->buf_ptr;\
163}
164
165#define RESTORE_BITS(s) \
166{\
167 (s)->buf_ptr = buf_ptr;\
168 (s)->bit_buf = bit_buf;\
169 (s)->bit_cnt = bit_cnt;\
170}
171
172/* define it to include statistics code (useful only for optimizing
173 codec efficiency */
174//#define STATS
175
176#ifdef STATS
177
178enum {
179 ST_UNKNOWN,
180 ST_DC,
181 ST_INTRA_AC,
182 ST_INTER_AC,
183 ST_INTRA_MB,
184 ST_INTER_MB,
185 ST_MV,
186 ST_NB,
187};
188
189extern int st_current_index;
190extern unsigned int st_bit_counts[ST_NB];
191extern unsigned int st_out_bit_counts[ST_NB];
192
193void print_stats(void);
194#endif
195
196/* misc math functions */
197
198extern inline int log2(unsigned int v)
199{
200 int n;
201
202 n = 0;
203 if (v & 0xffff0000) {
204 v >>= 16;
205 n += 16;
206 }
207 if (v & 0xff00) {
208 v >>= 8;
209 n += 8;
210 }
211 if (v & 0xf0) {
212 v >>= 4;
213 n += 4;
214 }
215 if (v & 0xc) {
216 v >>= 2;
217 n += 2;
218 }
219 if (v & 0x2) {
220 n++;
221 }
222 return n;
223}
224
225/* memory */
226void *av_mallocz(int size);
227
228#endif