878
|
1 /*
|
|
2 * Common bit i/o utils
|
|
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
|
|
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
|
|
5 * Copyright (c) 2004 Roman Bogorodskiy (bmp-wma specific stuff)
|
|
6 *
|
|
7 * This library is free software; you can redistribute it and/or
|
|
8 * modify it under the terms of the GNU Lesser General Public
|
|
9 * License as published by the Free Software Foundation; either
|
|
10 * version 2 of the License, or (at your option) any later version.
|
|
11 *
|
|
12 * This library is distributed in the hope that it will be useful,
|
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 * Lesser General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU Lesser General Public
|
|
18 * License along with this library; if not, write to the Free Software
|
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
20 *
|
|
21 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
|
|
22 */
|
|
23
|
|
24 /**
|
|
25 * @file common.c
|
|
26 * common internal api.
|
|
27 */
|
|
28
|
|
29 #include "avcodec.h"
|
|
30
|
|
31 const uint8_t ff_sqrt_tab[128]={
|
|
32 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
|
|
33 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
34 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
|
|
35 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
|
|
36 };
|
|
37
|
|
38 const uint8_t ff_log2_tab[256]={
|
|
39 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
|
|
40 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
|
41 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
|
42 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
|
43 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
|
44 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
|
45 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
|
46 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
|
|
47 };
|
|
48
|
|
49 void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
|
|
50 {
|
|
51 s->buf = buffer;
|
|
52 s->buf_end = s->buf + buffer_size;
|
|
53 s->buf_ptr = s->buf;
|
|
54 s->bit_left=32;
|
|
55 s->bit_buf=0;
|
|
56 }
|
|
57
|
|
58 /**
|
|
59 * init GetBitContext.
|
|
60 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
|
|
61 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
|
|
62 * @param bit_size the size of the buffer in bits
|
|
63 */
|
|
64 void init_get_bits(GetBitContext *s,
|
|
65 const uint8_t *buffer, int bit_size)
|
|
66 {
|
|
67 const int buffer_size= (bit_size+7)>>3;
|
|
68
|
|
69 s->buffer= buffer;
|
|
70 s->size_in_bits= bit_size;
|
|
71 s->buffer_end= buffer + buffer_size;
|
|
72 #ifdef ALT_BITSTREAM_READER
|
|
73 s->index=0;
|
|
74 #elif defined LIBMPEG2_BITSTREAM_READER
|
|
75 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
|
|
76 if ((int)buffer&1) {
|
|
77 /* word alignment */
|
|
78 s->cache = (*buffer++)<<24;
|
|
79 s->buffer_ptr = buffer;
|
|
80 s->bit_count = 16-8;
|
|
81 } else
|
|
82 #endif
|
|
83 {
|
|
84 s->buffer_ptr = buffer;
|
|
85 s->bit_count = 16;
|
|
86 s->cache = 0;
|
|
87 }
|
|
88 #elif defined A32_BITSTREAM_READER
|
|
89 s->buffer_ptr = (uint32_t*)buffer;
|
|
90 s->bit_count = 32;
|
|
91 s->cache0 = 0;
|
|
92 s->cache1 = 0;
|
|
93 #endif
|
|
94 {
|
|
95 OPEN_READER(re, s)
|
|
96 UPDATE_CACHE(re, s)
|
|
97 UPDATE_CACHE(re, s)
|
|
98 CLOSE_READER(re, s)
|
|
99 }
|
|
100 #ifdef A32_BITSTREAM_READER
|
|
101 s->cache1 = 0;
|
|
102 #endif
|
|
103 }
|
|
104
|
|
105 /**
|
|
106 * reads 0-32 bits.
|
|
107 */
|
|
108 unsigned int get_bits_long(GetBitContext *s, int n){
|
|
109 if(n<=17) return get_bits(s, n);
|
|
110 else{
|
|
111 int ret= get_bits(s, 16) << (n-16);
|
|
112 return ret | get_bits(s, n-16);
|
|
113 }
|
|
114 }
|
|
115
|
|
116 /**
|
|
117 * shows 0-32 bits.
|
|
118 */
|
|
119 unsigned int show_bits_long(GetBitContext *s, int n){
|
|
120 if(n<=17) return show_bits(s, n);
|
|
121 else{
|
|
122 GetBitContext gb= *s;
|
|
123 int ret= get_bits_long(s, n);
|
|
124 *s= gb;
|
|
125 return ret;
|
|
126 }
|
|
127 }
|
|
128
|
|
129 void align_get_bits(GetBitContext *s)
|
|
130 {
|
|
131 int n= (-get_bits_count(s)) & 7;
|
|
132 if(n) skip_bits(s, n);
|
|
133 }
|
|
134
|
|
135 int check_marker(GetBitContext *s, const char *msg)
|
|
136 {
|
|
137 int bit= get_bits1(s);
|
|
138 if(!bit)
|
|
139 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
|
|
140
|
|
141 return bit;
|
|
142 }
|
|
143
|
|
144 /* VLC decoding */
|
|
145
|
|
146 //#define DEBUG_VLC
|
|
147
|
|
148 #define GET_DATA(v, table, i, wrap, size) \
|
|
149 {\
|
|
150 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
|
|
151 switch(size) {\
|
|
152 case 1:\
|
|
153 v = *(const uint8_t *)ptr;\
|
|
154 break;\
|
|
155 case 2:\
|
|
156 v = *(const uint16_t *)ptr;\
|
|
157 break;\
|
|
158 default:\
|
|
159 v = *(const uint32_t *)ptr;\
|
|
160 break;\
|
|
161 }\
|
|
162 }
|
|
163
|
|
164
|
|
165 static int alloc_table(VLC *vlc, int size)
|
|
166 {
|
|
167 int index;
|
|
168 index = vlc->table_size;
|
|
169 vlc->table_size += size;
|
|
170 if (vlc->table_size > vlc->table_allocated) {
|
|
171 vlc->table_allocated += (1 << vlc->bits);
|
|
172 vlc->table = realloc(vlc->table,
|
|
173 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
|
|
174 if (!vlc->table)
|
|
175 return -1;
|
|
176 }
|
|
177 return index;
|
|
178 }
|
|
179
|
|
180 static int build_table(VLC *vlc, int table_nb_bits,
|
|
181 int nb_codes,
|
|
182 const void *bits, int bits_wrap, int bits_size,
|
|
183 const void *codes, int codes_wrap, int codes_size,
|
|
184 uint32_t code_prefix, int n_prefix)
|
|
185 {
|
|
186 int i, j, k, n, table_size, table_index, nb, n1, index;
|
|
187 uint32_t code;
|
|
188 VLC_TYPE (*table)[2];
|
|
189
|
|
190 table_size = 1 << table_nb_bits;
|
|
191 table_index = alloc_table(vlc, table_size);
|
|
192
|
|
193 if (table_index < 0)
|
|
194 return -1;
|
|
195 table = &vlc->table[table_index];
|
|
196
|
|
197 for(i=0;i<table_size;i++) {
|
|
198 table[i][1] = 0; //bits
|
|
199 table[i][0] = -1; //codes
|
|
200 }
|
|
201
|
|
202 /* first pass: map codes and compute auxillary table sizes */
|
|
203 for(i=0;i<nb_codes;i++) {
|
|
204 GET_DATA(n, bits, i, bits_wrap, bits_size);
|
|
205 GET_DATA(code, codes, i, codes_wrap, codes_size);
|
|
206 /* we accept tables with holes */
|
|
207 if (n <= 0)
|
|
208 continue;
|
|
209 /* if code matches the prefix, it is in the table */
|
|
210 n -= n_prefix;
|
|
211 if (n > 0 && (code >> n) == code_prefix) {
|
|
212 if (n <= table_nb_bits) {
|
|
213 /* no need to add another table */
|
|
214 j = (code << (table_nb_bits - n)) & (table_size - 1);
|
|
215 nb = 1 << (table_nb_bits - n);
|
|
216 for(k=0;k<nb;k++) {
|
|
217 if (table[j][1] /*bits*/ != 0) {
|
|
218 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
|
|
219 av_abort();
|
|
220 }
|
|
221 table[j][1] = n; //bits
|
|
222 table[j][0] = i; //code
|
|
223 j++;
|
|
224 }
|
|
225 } else {
|
|
226 n -= table_nb_bits;
|
|
227 j = (code >> n) & ((1 << table_nb_bits) - 1);
|
|
228 /* compute table size */
|
|
229 n1 = -table[j][1]; //bits
|
|
230 if (n > n1)
|
|
231 n1 = n;
|
|
232 table[j][1] = -n1; //bits
|
|
233 }
|
|
234 }
|
|
235 }
|
|
236
|
|
237 /* second pass : fill auxillary tables recursively */
|
|
238 for(i=0;i<table_size;i++) {
|
|
239 n = table[i][1]; //bits
|
|
240 if (n < 0) {
|
|
241 n = -n;
|
|
242 if (n > table_nb_bits) {
|
|
243 n = table_nb_bits;
|
|
244 table[i][1] = -n; //bits
|
|
245 }
|
|
246 index = build_table(vlc, n, nb_codes,
|
|
247 bits, bits_wrap, bits_size,
|
|
248 codes, codes_wrap, codes_size,
|
|
249 (code_prefix << table_nb_bits) | i,
|
|
250 n_prefix + table_nb_bits);
|
|
251 if (index < 0)
|
|
252 return -1;
|
|
253 /* note: realloc has been done, so reload tables */
|
|
254 table = &vlc->table[table_index];
|
|
255 table[i][0] = index; //code
|
|
256 }
|
|
257 }
|
|
258 return table_index;
|
|
259 }
|
|
260
|
|
261
|
|
262 /* Build VLC decoding tables suitable for use with get_vlc().
|
|
263
|
|
264 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
|
|
265 bigger it is, the faster is the decoding. But it should not be too
|
|
266 big to save memory and L1 cache. '9' is a good compromise.
|
|
267
|
|
268 'nb_codes' : number of vlcs codes
|
|
269
|
|
270 'bits' : table which gives the size (in bits) of each vlc code.
|
|
271
|
|
272 'codes' : table which gives the bit pattern of of each vlc code.
|
|
273
|
|
274 'xxx_wrap' : give the number of bytes between each entry of the
|
|
275 'bits' or 'codes' tables.
|
|
276
|
|
277 'xxx_size' : gives the number of bytes of each entry of the 'bits'
|
|
278 or 'codes' tables.
|
|
279
|
|
280 'wrap' and 'size' allows to use any memory configuration and types
|
|
281 (byte/word/long) to store the 'bits' and 'codes' tables.
|
|
282 */
|
|
283 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
|
|
284 const void *bits, int bits_wrap, int bits_size,
|
|
285 const void *codes, int codes_wrap, int codes_size)
|
|
286 {
|
|
287 vlc->bits = nb_bits;
|
|
288 vlc->table = NULL;
|
|
289 vlc->table_allocated = 0;
|
|
290 vlc->table_size = 0;
|
|
291
|
|
292 if (build_table(vlc, nb_bits, nb_codes,
|
|
293 bits, bits_wrap, bits_size,
|
|
294 codes, codes_wrap, codes_size,
|
|
295 0, 0) < 0) {
|
|
296 free(vlc->table);
|
|
297 return -1;
|
|
298 }
|
|
299 return 0;
|
|
300 }
|
|
301
|
|
302
|
|
303 void free_vlc(VLC *vlc)
|
|
304 {
|
|
305 free(vlc->table);
|
|
306 }
|
|
307
|
|
308 int64_t ff_gcd(int64_t a, int64_t b){
|
|
309 if(b) return ff_gcd(b, a%b);
|
|
310 else return a;
|
|
311 }
|