Mercurial > libavcodec.hg
annotate common.c @ 239:16cd8a9c4da4 libavcodec
- Minor changes on bitrate control
author | pulento |
---|---|
date | Fri, 15 Feb 2002 20:46:37 +0000 |
parents | 99a9f903f0e3 |
children | 28c5c62b1c4c |
rev | line source |
---|---|
0 | 1 /* |
2 * Common bit i/o utils | |
3 * Copyright (c) 2000, 2001 Gerard Lantau. | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
18 * |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
19 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at> |
0 | 20 */ |
64 | 21 #include "common.h" |
0 | 22 #include <math.h> |
8 | 23 |
0 | 24 void init_put_bits(PutBitContext *s, |
25 UINT8 *buffer, int buffer_size, | |
26 void *opaque, | |
27 void (*write_data)(void *, UINT8 *, int)) | |
28 { | |
29 s->buf = buffer; | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
30 s->buf_end = s->buf + buffer_size; |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
31 s->data_out_size = 0; |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
32 if(write_data!=NULL) |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
33 { |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
34 fprintf(stderr, "write Data callback is not supported\n"); |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
35 } |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
36 #ifdef ALT_BITSTREAM_WRITER |
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
37 s->index=0; |
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
38 ((uint32_t*)(s->buf))[0]=0; |
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
39 // memset(buffer, 0, buffer_size); |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
40 #else |
0 | 41 s->buf_ptr = s->buf; |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
42 s->bit_left=32; |
0 | 43 s->bit_buf=0; |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
44 #endif |
0 | 45 } |
46 | |
47 /* return the number of bits output */ | |
64 | 48 INT64 get_bit_count(PutBitContext *s) |
0 | 49 { |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
50 #ifdef ALT_BITSTREAM_WRITER |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
51 return s->data_out_size * 8 + s->index; |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
52 #else |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
53 return (s->buf_ptr - s->buf + s->data_out_size) * 8 + 32 - (INT64)s->bit_left; |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
54 #endif |
0 | 55 } |
56 | |
57 void align_put_bits(PutBitContext *s) | |
58 { | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
59 #ifdef ALT_BITSTREAM_WRITER |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
60 put_bits(s,( - s->index) & 7,0); |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
61 #else |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
62 put_bits(s,s->bit_left & 7,0); |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
63 #endif |
0 | 64 } |
65 | |
66 /* pad the end of the output stream with zeros */ | |
67 void flush_put_bits(PutBitContext *s) | |
68 { | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
69 #ifdef ALT_BITSTREAM_WRITER |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
70 align_put_bits(s); |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
71 #else |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
72 s->bit_buf<<= s->bit_left; |
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
73 while (s->bit_left < 32) { |
0 | 74 /* XXX: should test end of buffer */ |
75 *s->buf_ptr++=s->bit_buf >> 24; | |
76 s->bit_buf<<=8; | |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
77 s->bit_left+=8; |
0 | 78 } |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
79 s->bit_left=32; |
0 | 80 s->bit_buf=0; |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
81 #endif |
0 | 82 } |
83 | |
84 /* pad the end of the output stream with zeros */ | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
85 #ifndef ALT_BITSTREAM_WRITER |
0 | 86 void jflush_put_bits(PutBitContext *s) |
87 { | |
88 unsigned int b; | |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
89 s->bit_buf<<= s->bit_left; |
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
90 s->bit_buf |= ~1U >> (32 - s->bit_left); /* set all the unused bits to one */ |
0 | 91 |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
92 while (s->bit_left < 32) { |
0 | 93 b = s->bit_buf >> 24; |
94 *s->buf_ptr++ = b; | |
95 if (b == 0xff) | |
96 *s->buf_ptr++ = 0; | |
97 s->bit_buf<<=8; | |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
98 s->bit_left+=8; |
0 | 99 } |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
100 s->bit_left=32; |
0 | 101 s->bit_buf=0; |
102 } | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
103 #else |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
104 void jflush_put_bits(PutBitContext *s) |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
105 { |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
106 int num= ( - s->index) & 7; |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
107 jput_bits(s, num,0xFF>>(8-num)); |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
108 } |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
109 #endif |
0 | 110 |
111 /* bit input functions */ | |
112 | |
113 void init_get_bits(GetBitContext *s, | |
114 UINT8 *buffer, int buffer_size) | |
115 { | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
116 #ifdef ALT_BITSTREAM_READER |
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
117 s->index=0; |
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
118 s->buffer= buffer; |
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
119 #else |
0 | 120 s->buf = buffer; |
121 s->buf_ptr = buffer; | |
122 s->buf_end = buffer + buffer_size; | |
123 s->bit_cnt = 0; | |
124 s->bit_buf = 0; | |
125 while (s->buf_ptr < s->buf_end && | |
126 s->bit_cnt < 32) { | |
127 s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt)); | |
128 s->bit_cnt += 8; | |
129 } | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
130 #endif |
0 | 131 } |
132 | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
133 #ifndef ALT_BITSTREAM_READER |
0 | 134 /* n must be >= 1 and <= 32 */ |
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
135 /* also true: n > s->bit_cnt */ |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
136 unsigned int get_bits_long(GetBitContext *s, int n) |
0 | 137 { |
138 unsigned int val; | |
139 int bit_cnt; | |
140 unsigned int bit_buf; | |
141 | |
142 #ifdef STATS | |
143 st_bit_counts[st_current_index] += n; | |
144 #endif | |
145 | |
146 bit_buf = s->bit_buf; | |
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
147 bit_cnt = s->bit_cnt - n; |
0 | 148 |
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
149 // if (bit_cnt >= 0) { |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
150 // val = bit_buf >> (32 - n); |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
151 // bit_buf <<= n; |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
152 // } else |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
153 { |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
154 UINT8 *buf_ptr; |
0 | 155 val = bit_buf >> (32 - n); |
156 buf_ptr = s->buf_ptr; | |
157 buf_ptr += 4; | |
158 /* handle common case: we can read everything */ | |
159 if (buf_ptr <= s->buf_end) { | |
8 | 160 #if ARCH_X86 |
161 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
162 #else | |
163 bit_buf = (buf_ptr[-4] << 24) | | |
164 (buf_ptr[-3] << 16) | | |
0 | 165 (buf_ptr[-2] << 8) | |
8 | 166 (buf_ptr[-1]); |
167 #endif | |
0 | 168 } else { |
169 buf_ptr -= 4; | |
170 bit_buf = 0; | |
171 if (buf_ptr < s->buf_end) | |
172 bit_buf |= *buf_ptr++ << 24; | |
173 if (buf_ptr < s->buf_end) | |
174 bit_buf |= *buf_ptr++ << 16; | |
175 if (buf_ptr < s->buf_end) | |
176 bit_buf |= *buf_ptr++ << 8; | |
177 if (buf_ptr < s->buf_end) | |
178 bit_buf |= *buf_ptr++; | |
179 } | |
180 s->buf_ptr = buf_ptr; | |
181 val |= bit_buf >> (32 + bit_cnt); | |
182 bit_buf <<= - bit_cnt; | |
183 bit_cnt += 32; | |
184 } | |
185 s->bit_buf = bit_buf; | |
186 s->bit_cnt = bit_cnt; | |
187 return val; | |
188 } | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
189 #endif |
0 | 190 |
191 void align_get_bits(GetBitContext *s) | |
192 { | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
193 #ifdef ALT_BITSTREAM_READER |
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
194 s->index= (s->index + 7) & (~7); |
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
195 #else |
0 | 196 int n; |
197 n = s->bit_cnt & 7; | |
198 if (n > 0) { | |
199 get_bits(s, n); | |
200 } | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
201 #endif |
0 | 202 } |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
203 |
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
204 #ifndef ALT_BITSTREAM_READER |
144 | 205 /* This function is identical to get_bits_long(), the */ |
206 /* only diference is that it doesn't touch the buffer */ | |
207 /* it is usefull to see the buffer. */ | |
208 | |
209 unsigned int show_bits_long(GetBitContext *s, int n) | |
210 { | |
211 unsigned int val; | |
212 int bit_cnt; | |
213 unsigned int bit_buf; | |
214 UINT8 *buf_ptr; | |
215 | |
216 bit_buf = s->bit_buf; | |
217 bit_cnt = s->bit_cnt - n; | |
218 | |
219 val = bit_buf >> (32 - n); | |
220 buf_ptr = s->buf_ptr; | |
221 buf_ptr += 4; | |
222 | |
223 /* handle common case: we can read everything */ | |
224 if (buf_ptr <= s->buf_end) { | |
225 #ifdef ARCH_X86 | |
226 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
227 #else | |
228 bit_buf = (buf_ptr[-4] << 24) | | |
229 (buf_ptr[-3] << 16) | | |
230 (buf_ptr[-2] << 8) | | |
231 (buf_ptr[-1]); | |
232 #endif | |
233 } else { | |
234 buf_ptr -= 4; | |
235 bit_buf = 0; | |
236 if (buf_ptr < s->buf_end) | |
237 bit_buf |= *buf_ptr++ << 24; | |
238 if (buf_ptr < s->buf_end) | |
239 bit_buf |= *buf_ptr++ << 16; | |
240 if (buf_ptr < s->buf_end) | |
241 bit_buf |= *buf_ptr++ << 8; | |
242 if (buf_ptr < s->buf_end) | |
243 bit_buf |= *buf_ptr++; | |
244 } | |
245 val |= bit_buf >> (32 + bit_cnt); | |
246 bit_buf <<= - bit_cnt; | |
247 bit_cnt += 32; | |
248 | |
249 return val; | |
250 } | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
251 #endif |
0 | 252 |
253 /* VLC decoding */ | |
254 | |
255 //#define DEBUG_VLC | |
256 | |
257 #define GET_DATA(v, table, i, wrap, size) \ | |
258 {\ | |
259 UINT8 *ptr = (UINT8 *)table + i * wrap;\ | |
260 switch(size) {\ | |
261 case 1:\ | |
262 v = *(UINT8 *)ptr;\ | |
263 break;\ | |
264 case 2:\ | |
265 v = *(UINT16 *)ptr;\ | |
266 break;\ | |
267 default:\ | |
268 v = *(UINT32 *)ptr;\ | |
269 break;\ | |
270 }\ | |
271 } | |
272 | |
273 | |
274 static int alloc_table(VLC *vlc, int size) | |
275 { | |
276 int index; | |
277 index = vlc->table_size; | |
278 vlc->table_size += size; | |
279 if (vlc->table_size > vlc->table_allocated) { | |
280 vlc->table_allocated += (1 << vlc->bits); | |
281 vlc->table_bits = realloc(vlc->table_bits, | |
282 sizeof(INT8) * vlc->table_allocated); | |
283 vlc->table_codes = realloc(vlc->table_codes, | |
284 sizeof(INT16) * vlc->table_allocated); | |
285 if (!vlc->table_bits || | |
286 !vlc->table_codes) | |
287 return -1; | |
288 } | |
289 return index; | |
290 } | |
291 | |
292 static int build_table(VLC *vlc, int table_nb_bits, | |
293 int nb_codes, | |
294 const void *bits, int bits_wrap, int bits_size, | |
295 const void *codes, int codes_wrap, int codes_size, | |
296 UINT32 code_prefix, int n_prefix) | |
297 { | |
298 int i, j, k, n, table_size, table_index, nb, n1, index; | |
299 UINT32 code; | |
300 INT8 *table_bits; | |
301 INT16 *table_codes; | |
302 | |
303 table_size = 1 << table_nb_bits; | |
304 table_index = alloc_table(vlc, table_size); | |
305 #ifdef DEBUG_VLC | |
306 printf("new table index=%d size=%d code_prefix=%x n=%d\n", | |
307 table_index, table_size, code_prefix, n_prefix); | |
308 #endif | |
309 if (table_index < 0) | |
310 return -1; | |
311 table_bits = &vlc->table_bits[table_index]; | |
312 table_codes = &vlc->table_codes[table_index]; | |
313 | |
314 for(i=0;i<table_size;i++) { | |
315 table_bits[i] = 0; | |
316 table_codes[i] = -1; | |
317 } | |
318 | |
319 /* first pass: map codes and compute auxillary table sizes */ | |
320 for(i=0;i<nb_codes;i++) { | |
321 GET_DATA(n, bits, i, bits_wrap, bits_size); | |
322 GET_DATA(code, codes, i, codes_wrap, codes_size); | |
323 /* we accept tables with holes */ | |
324 if (n <= 0) | |
325 continue; | |
326 #if defined(DEBUG_VLC) && 0 | |
327 printf("i=%d n=%d code=0x%x\n", i, n, code); | |
328 #endif | |
329 /* if code matches the prefix, it is in the table */ | |
330 n -= n_prefix; | |
331 if (n > 0 && (code >> n) == code_prefix) { | |
332 if (n <= table_nb_bits) { | |
333 /* no need to add another table */ | |
334 j = (code << (table_nb_bits - n)) & (table_size - 1); | |
335 nb = 1 << (table_nb_bits - n); | |
336 for(k=0;k<nb;k++) { | |
337 #ifdef DEBUG_VLC | |
338 printf("%4x: code=%d n=%d\n", | |
339 j, i, n); | |
340 #endif | |
341 if (table_bits[j] != 0) { | |
342 fprintf(stderr, "incorrect codes\n"); | |
343 exit(1); | |
344 } | |
345 table_bits[j] = n; | |
346 table_codes[j] = i; | |
347 j++; | |
348 } | |
349 } else { | |
350 n -= table_nb_bits; | |
351 j = (code >> n) & ((1 << table_nb_bits) - 1); | |
352 #ifdef DEBUG_VLC | |
353 printf("%4x: n=%d (subtable)\n", | |
354 j, n); | |
355 #endif | |
356 /* compute table size */ | |
357 n1 = -table_bits[j]; | |
358 if (n > n1) | |
359 n1 = n; | |
360 table_bits[j] = -n1; | |
361 } | |
362 } | |
363 } | |
364 | |
365 /* second pass : fill auxillary tables recursively */ | |
366 for(i=0;i<table_size;i++) { | |
367 n = table_bits[i]; | |
368 if (n < 0) { | |
369 n = -n; | |
370 if (n > table_nb_bits) { | |
371 n = table_nb_bits; | |
372 table_bits[i] = -n; | |
373 } | |
374 index = build_table(vlc, n, nb_codes, | |
375 bits, bits_wrap, bits_size, | |
376 codes, codes_wrap, codes_size, | |
377 (code_prefix << table_nb_bits) | i, | |
378 n_prefix + table_nb_bits); | |
379 if (index < 0) | |
380 return -1; | |
381 /* note: realloc has been done, so reload tables */ | |
382 table_bits = &vlc->table_bits[table_index]; | |
383 table_codes = &vlc->table_codes[table_index]; | |
384 table_codes[i] = index; | |
385 } | |
386 } | |
387 return table_index; | |
388 } | |
389 | |
390 | |
24 | 391 /* Build VLC decoding tables suitable for use with get_vlc(). |
392 | |
393 'nb_bits' set thee decoding table size (2^nb_bits) entries. The | |
394 bigger it is, the faster is the decoding. But it should not be too | |
395 big to save memory and L1 cache. '9' is a good compromise. | |
396 | |
397 'nb_codes' : number of vlcs codes | |
398 | |
399 'bits' : table which gives the size (in bits) of each vlc code. | |
400 | |
401 'codes' : table which gives the bit pattern of of each vlc code. | |
402 | |
403 'xxx_wrap' : give the number of bytes between each entry of the | |
404 'bits' or 'codes' tables. | |
405 | |
406 'xxx_size' : gives the number of bytes of each entry of the 'bits' | |
407 or 'codes' tables. | |
408 | |
409 'wrap' and 'size' allows to use any memory configuration and types | |
410 (byte/word/long) to store the 'bits' and 'codes' tables. | |
411 */ | |
0 | 412 int init_vlc(VLC *vlc, int nb_bits, int nb_codes, |
413 const void *bits, int bits_wrap, int bits_size, | |
414 const void *codes, int codes_wrap, int codes_size) | |
415 { | |
416 vlc->bits = nb_bits; | |
417 vlc->table_bits = NULL; | |
418 vlc->table_codes = NULL; | |
419 vlc->table_allocated = 0; | |
420 vlc->table_size = 0; | |
421 #ifdef DEBUG_VLC | |
422 printf("build table nb_codes=%d\n", nb_codes); | |
423 #endif | |
424 | |
425 if (build_table(vlc, nb_bits, nb_codes, | |
426 bits, bits_wrap, bits_size, | |
427 codes, codes_wrap, codes_size, | |
428 0, 0) < 0) { | |
429 if (vlc->table_bits) | |
430 free(vlc->table_bits); | |
431 if (vlc->table_codes) | |
432 free(vlc->table_codes); | |
433 return -1; | |
434 } | |
435 return 0; | |
436 } | |
437 | |
438 | |
439 void free_vlc(VLC *vlc) | |
440 { | |
441 free(vlc->table_bits); | |
442 free(vlc->table_codes); | |
443 } | |
444 |