]> git.sesse.net Git - ffmpeg/blob - libavcodec/common.h
Initial revision
[ffmpeg] / libavcodec / common.h
1 #ifndef COMMON_H
2 #define COMMON_H
3
4 #include "../config.h"
5
6 #ifndef USE_LIBAVCODEC
7 // workaround for typedef conflict in MPlayer
8 typedef unsigned short UINT16;
9 typedef signed short INT16;
10 #endif
11
12 typedef unsigned char UINT8;
13 typedef unsigned int UINT32;
14 typedef unsigned long long UINT64;
15 typedef signed char INT8;
16 typedef signed int INT32;
17 typedef signed long long INT64;
18
19 /* bit output */
20
21 struct PutBitContext;
22
23 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
24
25 typedef struct PutBitContext {
26     UINT8 *buf, *buf_ptr, *buf_end;
27     int bit_cnt;
28     UINT32 bit_buf;
29     long long data_out_size; /* in bytes */
30     void *opaque;
31     WriteDataFunc write_data;
32 } PutBitContext;
33
34 void init_put_bits(PutBitContext *s, 
35                    UINT8 *buffer, int buffer_size,
36                    void *opaque,
37                    void (*write_data)(void *, UINT8 *, int));
38 void put_bits(PutBitContext *s, int n, unsigned int value);
39 long long get_bit_count(PutBitContext *s);
40 void align_put_bits(PutBitContext *s);
41 void flush_put_bits(PutBitContext *s);
42
43 /* jpeg specific put_bits */
44 void jput_bits(PutBitContext *s, int n, unsigned int value);
45 void jflush_put_bits(PutBitContext *s);
46
47 /* bit input */
48
49 typedef struct GetBitContext {
50     UINT8 *buf, *buf_ptr, *buf_end;
51     int bit_cnt;
52     UINT32 bit_buf;
53 } GetBitContext;
54
55 typedef struct VLC {
56     int bits;
57     INT16 *table_codes;
58     INT8 *table_bits;
59     int table_size, table_allocated;
60 } VLC;
61
62 void init_get_bits(GetBitContext *s, 
63                    UINT8 *buffer, int buffer_size);
64
65 unsigned int get_bits(GetBitContext *s, int n);
66 void align_get_bits(GetBitContext *s);
67 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
68              const void *bits, int bits_wrap, int bits_size,
69              const void *codes, int codes_wrap, int codes_size);
70 void free_vlc(VLC *vlc);
71 int get_vlc(GetBitContext *s, VLC *vlc);
72
73 /* macro to go faster */
74 /* n must be <= 24 */
75 /* XXX: optimize buffer end test */
76 #define SHOW_BITS(s, val, n)\
77 {\
78     if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
79         bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
80         bit_cnt += 8;\
81         if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
82             bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
83             bit_cnt += 8;\
84             if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
85                 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
86                 bit_cnt += 8;\
87             }\
88         }\
89     }\
90     val = bit_buf >> (32 - n);\
91 }
92
93 /* SHOW_BITS with n1 >= n must be been done before */
94 #define FLUSH_BITS(n)\
95 {\
96     bit_buf <<= n;\
97     bit_cnt -= n;\
98 }
99
100 #define SAVE_BITS(s) \
101 {\
102     bit_cnt = (s)->bit_cnt;\
103     bit_buf = (s)->bit_buf;\
104     buf_ptr = (s)->buf_ptr;\
105 }
106
107 #define RESTORE_BITS(s) \
108 {\
109     (s)->buf_ptr = buf_ptr;\
110     (s)->bit_buf = bit_buf;\
111     (s)->bit_cnt = bit_cnt;\
112 }
113
114 /* define it to include statistics code (useful only for optimizing
115    codec efficiency */
116 //#define STATS
117
118 #ifdef STATS
119
120 enum {
121     ST_UNKNOWN,
122     ST_DC,
123     ST_INTRA_AC,
124     ST_INTER_AC,
125     ST_INTRA_MB,
126     ST_INTER_MB,
127     ST_MV,
128     ST_NB,
129 };
130
131 extern int st_current_index;
132 extern unsigned int st_bit_counts[ST_NB];
133 extern unsigned int st_out_bit_counts[ST_NB];
134
135 void print_stats(void);
136 #endif
137
138 /* misc math functions */
139
140 extern inline int log2(unsigned int v)
141 {
142     int n;
143
144     n = 0;
145     if (v & 0xffff0000) {
146         v >>= 16;
147         n += 16;
148     }
149     if (v & 0xff00) {
150         v >>= 8;
151         n += 8;
152     }
153     if (v & 0xf0) {
154         v >>= 4;
155         n += 4;
156     }
157     if (v & 0xc) {
158         v >>= 2;
159         n += 2;
160     }
161     if (v & 0x2) {
162         n++;
163     }
164     return n;
165 }
166
167 /* memory */
168 void *av_mallocz(int size);
169
170 #endif