added HAVE_AV_CONFIG_H
[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 {
28 UINT8 *buf, *buf_ptr, *buf_end;
29 int bit_cnt;
30 UINT32 bit_buf;
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 {
52 UINT8 *buf, *buf_ptr, *buf_end;
53 int bit_cnt;
54 UINT32 bit_buf;
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
67unsigned int get_bits(GetBitContext *s, int n);
68void align_get_bits(GetBitContext *s);
69int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
70 const void *bits, int bits_wrap, int bits_size,
71 const void *codes, int codes_wrap, int codes_size);
72void free_vlc(VLC *vlc);
73int get_vlc(GetBitContext *s, VLC *vlc);
74
75/* macro to go faster */
76/* n must be <= 24 */
77/* XXX: optimize buffer end test */
78#define SHOW_BITS(s, val, n)\
79{\
80 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
81 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
82 bit_cnt += 8;\
83 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
84 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
85 bit_cnt += 8;\
86 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
87 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
88 bit_cnt += 8;\
89 }\
90 }\
91 }\
92 val = bit_buf >> (32 - n);\
93}
94
95/* SHOW_BITS with n1 >= n must be been done before */
96#define FLUSH_BITS(n)\
97{\
98 bit_buf <<= n;\
99 bit_cnt -= n;\
100}
101
102#define SAVE_BITS(s) \
103{\
104 bit_cnt = (s)->bit_cnt;\
105 bit_buf = (s)->bit_buf;\
106 buf_ptr = (s)->buf_ptr;\
107}
108
109#define RESTORE_BITS(s) \
110{\
111 (s)->buf_ptr = buf_ptr;\
112 (s)->bit_buf = bit_buf;\
113 (s)->bit_cnt = bit_cnt;\
114}
115
116/* define it to include statistics code (useful only for optimizing
117 codec efficiency */
118//#define STATS
119
120#ifdef STATS
121
122enum {
123 ST_UNKNOWN,
124 ST_DC,
125 ST_INTRA_AC,
126 ST_INTER_AC,
127 ST_INTRA_MB,
128 ST_INTER_MB,
129 ST_MV,
130 ST_NB,
131};
132
133extern int st_current_index;
134extern unsigned int st_bit_counts[ST_NB];
135extern unsigned int st_out_bit_counts[ST_NB];
136
137void print_stats(void);
138#endif
139
140/* misc math functions */
141
142extern inline int log2(unsigned int v)
143{
144 int n;
145
146 n = 0;
147 if (v & 0xffff0000) {
148 v >>= 16;
149 n += 16;
150 }
151 if (v & 0xff00) {
152 v >>= 8;
153 n += 8;
154 }
155 if (v & 0xf0) {
156 v >>= 4;
157 n += 4;
158 }
159 if (v & 0xc) {
160 v >>= 2;
161 n += 2;
162 }
163 if (v & 0x2) {
164 n++;
165 }
166 return n;
167}
168
169/* memory */
170void *av_mallocz(int size);
171
172#endif