Mercurial > libavcodec.hg
comparison vorbis.c @ 2720:4a5ed2d916f6 libavcodec
1.) LGPL license mentioned
2.) Stack useage decreased by 64kb
3.) One more bug fixed
patch by (Balatoni Denes <dbalatoni.programozo@hu>)
author | michael |
---|---|
date | Sat, 21 May 2005 01:17:20 +0000 |
parents | 040b965f2cd0 |
children | b37add61897a |
comparison
equal
deleted
inserted
replaced
2719:5444d77adcbe | 2720:4a5ed2d916f6 |
---|---|
1 /** | 1 /** |
2 * @file vorbis.c | 2 * @file vorbis.c |
3 * Vorbis I decoder | 3 * Vorbis I decoder |
4 * @author Denes Balatoni ( dbalatoni programozo hu ) | 4 * @author Denes Balatoni ( dbalatoni programozo hu ) |
5 | |
6 * This library is free software; you can redistribute it and/or | |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 2 of the License, or (at your option) any later version. | |
10 * | |
11 * This library is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Lesser General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Lesser General Public | |
17 * License along with this library; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
5 */ | 20 */ |
6 | 21 |
7 #undef V_DEBUG | 22 #undef V_DEBUG |
8 | 23 |
9 #include <math.h> | 24 #include <math.h> |
173 | 188 |
174 // Process codebooks part | 189 // Process codebooks part |
175 | 190 |
176 static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc) { | 191 static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc) { |
177 uint_fast16_t cb; | 192 uint_fast16_t cb; |
193 uint_fast8_t *tmp_vlc_bits; | |
194 uint_fast32_t *tmp_vlc_codes; | |
178 GetBitContext *gb=&vc->gb; | 195 GetBitContext *gb=&vc->gb; |
179 | 196 |
180 vc->codebook_count=get_bits(gb,8)+1; | 197 vc->codebook_count=get_bits(gb,8)+1; |
181 | 198 |
182 AV_DEBUG(" Codebooks: %d \n", vc->codebook_count); | 199 AV_DEBUG(" Codebooks: %d \n", vc->codebook_count); |
183 | 200 |
184 vc->codebooks=(vorbis_codebook *)av_mallocz(vc->codebook_count * sizeof(vorbis_codebook)); | 201 vc->codebooks=(vorbis_codebook *)av_mallocz(vc->codebook_count * sizeof(vorbis_codebook)); |
202 tmp_vlc_bits=(uint_fast8_t *)av_mallocz(V_MAX_VLCS * sizeof(uint_fast8_t)); | |
203 tmp_vlc_codes=(uint_fast32_t *)av_mallocz(V_MAX_VLCS * sizeof(uint_fast32_t)); | |
185 | 204 |
186 for(cb=0;cb<vc->codebook_count;++cb) { | 205 for(cb=0;cb<vc->codebook_count;++cb) { |
187 vorbis_codebook *codebook_setup=&vc->codebooks[cb]; | 206 vorbis_codebook *codebook_setup=&vc->codebooks[cb]; |
188 uint_fast8_t ordered; | 207 uint_fast8_t ordered; |
189 uint_fast32_t t, used_entries=0; | 208 uint_fast32_t t, used_entries=0; |
190 uint_fast32_t entries; | 209 uint_fast32_t entries; |
191 uint_fast8_t tmp_vlc_bits[V_MAX_VLCS]; | |
192 uint_fast32_t tmp_vlc_codes[V_MAX_VLCS]; | |
193 | |
194 // memset(tmp_vlc_bits, 0, sizeof(tmp_vlc_bits)); | |
195 | 210 |
196 AV_DEBUG(" %d. Codebook \n", cb); | 211 AV_DEBUG(" %d. Codebook \n", cb); |
197 | 212 |
198 if (get_bits(gb, 24)!=0x564342) { | 213 if (get_bits(gb, 24)!=0x564342) { |
199 av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook setup data corrupt. \n", cb); | 214 av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook setup data corrupt. \n", cb); |
200 return 1; | 215 goto error; |
201 } | 216 } |
202 | 217 |
203 codebook_setup->dimensions=get_bits(gb, 16); | 218 codebook_setup->dimensions=get_bits(gb, 16); |
204 if (codebook_setup->dimensions>16) { | 219 if (codebook_setup->dimensions>16) { |
205 av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook's dimension is too large (%d). \n", cb, codebook_setup->dimensions); | 220 av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook's dimension is too large (%d). \n", cb, codebook_setup->dimensions); |
206 return 1; | 221 goto error; |
207 } | 222 } |
208 entries=get_bits(gb, 24); | 223 entries=get_bits(gb, 24); |
209 if (entries>V_MAX_VLCS) { | 224 if (entries>V_MAX_VLCS) { |
210 av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook has too many entries (%d). \n", cb, entries); | 225 av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook has too many entries (%d). \n", cb, entries); |
211 return 1; | 226 goto error; |
212 } | 227 } |
213 | 228 |
214 ordered=get_bits1(gb); | 229 ordered=get_bits1(gb); |
215 | 230 |
216 AV_DEBUG(" codebook_dimensions %d, codebook_entries %d \n", codebook_setup->dimensions, entries); | 231 AV_DEBUG(" codebook_dimensions %d, codebook_entries %d \n", codebook_setup->dimensions, entries); |
264 | 279 |
265 current_entry+=number; | 280 current_entry+=number; |
266 } | 281 } |
267 if (current_entry>used_entries) { | 282 if (current_entry>used_entries) { |
268 av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \n"); | 283 av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \n"); |
269 return 1; | 284 goto error; |
270 } | 285 } |
271 } | 286 } |
272 | 287 |
273 codebook_setup->lookup_type=get_bits(gb, 4); | 288 codebook_setup->lookup_type=get_bits(gb, 4); |
274 | 289 |
330 ++j; | 345 ++j; |
331 } | 346 } |
332 } | 347 } |
333 if (j!=used_entries) { | 348 if (j!=used_entries) { |
334 av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n"); | 349 av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n"); |
335 return 1; | 350 goto error; |
336 } | 351 } |
337 entries=used_entries; | 352 entries=used_entries; |
338 } | 353 } |
339 else if (codebook_setup->lookup_type>=2) { | 354 else if (codebook_setup->lookup_type>=2) { |
340 av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \n"); | 355 av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \n"); |
341 return 1; | 356 goto error; |
342 } | 357 } |
343 | 358 |
344 // Initialize VLC table | 359 // Initialize VLC table |
345 if (vorbis_len2vlc(vc, tmp_vlc_bits, tmp_vlc_codes, entries)) { | 360 if (vorbis_len2vlc(vc, tmp_vlc_bits, tmp_vlc_codes, entries)) { |
346 av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n"); | 361 av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n"); |
347 return 1; | 362 goto error; |
348 } | 363 } |
349 codebook_setup->maxdepth=0; | 364 codebook_setup->maxdepth=0; |
350 for(t=0;t<entries;++t) | 365 for(t=0;t<entries;++t) |
351 if (tmp_vlc_bits[t]>=codebook_setup->maxdepth) codebook_setup->maxdepth=tmp_vlc_bits[t]; | 366 if (tmp_vlc_bits[t]>=codebook_setup->maxdepth) codebook_setup->maxdepth=tmp_vlc_bits[t]; |
352 | 367 |
353 codebook_setup->maxdepth=(codebook_setup->maxdepth+V_NB_BITS-1)/V_NB_BITS; | 368 codebook_setup->maxdepth=(codebook_setup->maxdepth+V_NB_BITS-1)/V_NB_BITS; |
354 | 369 |
355 if (init_vlc(&codebook_setup->vlc, V_NB_BITS, entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits), sizeof(*tmp_vlc_bits), tmp_vlc_codes, sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes), INIT_VLC_LE)) { | 370 if (init_vlc(&codebook_setup->vlc, V_NB_BITS, entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits), sizeof(*tmp_vlc_bits), tmp_vlc_codes, sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes), INIT_VLC_LE)) { |
356 av_log(vc->avccontext, AV_LOG_ERROR, " Error generating vlc tables. \n"); | 371 av_log(vc->avccontext, AV_LOG_ERROR, " Error generating vlc tables. \n"); |
357 return 1; | 372 goto error; |
358 } | 373 } |
359 } | 374 } |
375 | |
376 av_free(tmp_vlc_bits); | |
377 av_free(tmp_vlc_codes); | |
360 return 0; | 378 return 0; |
379 | |
380 // Error: | |
381 error: | |
382 av_free(tmp_vlc_bits); | |
383 av_free(tmp_vlc_codes); | |
384 return 1; | |
361 } | 385 } |
362 | 386 |
363 // Process time domain transforms part (unused in Vorbis I) | 387 // Process time domain transforms part (unused in Vorbis I) |
364 | 388 |
365 static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc) { | 389 static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc) { |
1188 if (get_bits1(gb)) { | 1212 if (get_bits1(gb)) { |
1189 av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n"); | 1213 av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n"); |
1190 return -1; // packet type not audio | 1214 return -1; // packet type not audio |
1191 } | 1215 } |
1192 | 1216 |
1193 mode_number=get_bits(gb, ilog(vc->mode_count-1)); | 1217 if (vc->mode_count==1) { |
1218 mode_number=0; | |
1219 } else { | |
1220 mode_number=get_bits(gb, ilog(vc->mode_count-1)); | |
1221 } | |
1194 mapping=&vc->mappings[vc->modes[mode_number].mapping]; | 1222 mapping=&vc->mappings[vc->modes[mode_number].mapping]; |
1195 | 1223 |
1196 AV_DEBUG(" Mode number: %d , mapping: %d , blocktype %d \n", mode_number, vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag); | 1224 AV_DEBUG(" Mode number: %d , mapping: %d , blocktype %d \n", mode_number, vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag); |
1197 | 1225 |
1198 if (vc->modes[mode_number].blockflag) { | 1226 if (vc->modes[mode_number].blockflag) { |