comparison bitstream.c @ 11713:b9354f343d2d libavcodec

change a variable-length array to a malloc.
author lorenm
date Wed, 12 May 2010 22:38:05 +0000
parents 7dd2a45249a9
children
comparison
equal deleted inserted replaced
11712:25ed71436974 11713:b9354f343d2d
273 const void *bits, int bits_wrap, int bits_size, 273 const void *bits, int bits_wrap, int bits_size,
274 const void *codes, int codes_wrap, int codes_size, 274 const void *codes, int codes_wrap, int codes_size,
275 const void *symbols, int symbols_wrap, int symbols_size, 275 const void *symbols, int symbols_wrap, int symbols_size,
276 int flags) 276 int flags)
277 { 277 {
278 VLCcode buf[nb_codes]; 278 VLCcode *buf;
279 int i, j; 279 int i, j, ret;
280 280
281 vlc->bits = nb_bits; 281 vlc->bits = nb_bits;
282 if(flags & INIT_VLC_USE_NEW_STATIC){ 282 if(flags & INIT_VLC_USE_NEW_STATIC){
283 if(vlc->table_size && vlc->table_size == vlc->table_allocated){ 283 if(vlc->table_size && vlc->table_size == vlc->table_allocated){
284 return 0; 284 return 0;
292 } 292 }
293 293
294 #ifdef DEBUG_VLC 294 #ifdef DEBUG_VLC
295 av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes); 295 av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes);
296 #endif 296 #endif
297
298 buf = av_malloc((nb_codes+1)*sizeof(VLCcode));
297 299
298 assert(symbols_size <= 2 || !symbols); 300 assert(symbols_size <= 2 || !symbols);
299 j = 0; 301 j = 0;
300 #define COPY(condition)\ 302 #define COPY(condition)\
301 for (i = 0; i < nb_codes; i++) {\ 303 for (i = 0; i < nb_codes; i++) {\
317 // qsort is the slowest part of init_vlc, and could probably be improved or avoided 319 // qsort is the slowest part of init_vlc, and could probably be improved or avoided
318 qsort(buf, j, sizeof(VLCcode), compare_vlcspec); 320 qsort(buf, j, sizeof(VLCcode), compare_vlcspec);
319 COPY(buf[j].bits && buf[j].bits <= nb_bits); 321 COPY(buf[j].bits && buf[j].bits <= nb_bits);
320 nb_codes = j; 322 nb_codes = j;
321 323
322 if (build_table(vlc, nb_bits, nb_codes, buf, flags) < 0) { 324 ret = build_table(vlc, nb_bits, nb_codes, buf, flags);
325
326 av_free(buf);
327 if (ret < 0) {
323 av_freep(&vlc->table); 328 av_freep(&vlc->table);
324 return -1; 329 return -1;
325 } 330 }
326 if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size != vlc->table_allocated) 331 if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size != vlc->table_allocated)
327 av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated); 332 av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated);