Mercurial > libavcodec.hg
comparison resample2.c @ 12121:ee69b90df099 libavcodec
resample: replace VLA with malloc/free
| author | mru |
|---|---|
| date | Fri, 09 Jul 2010 16:05:58 +0000 |
| parents | a80468e73475 |
| children |
comparison
equal
deleted
inserted
replaced
| 12120:a80468e73475 | 12121:ee69b90df099 |
|---|---|
| 92 /** | 92 /** |
| 93 * builds a polyphase filterbank. | 93 * builds a polyphase filterbank. |
| 94 * @param factor resampling factor | 94 * @param factor resampling factor |
| 95 * @param scale wanted sum of coefficients for each filter | 95 * @param scale wanted sum of coefficients for each filter |
| 96 * @param type 0->cubic, 1->blackman nuttall windowed sinc, 2..16->kaiser windowed sinc beta=2..16 | 96 * @param type 0->cubic, 1->blackman nuttall windowed sinc, 2..16->kaiser windowed sinc beta=2..16 |
| 97 * @return 0 on success, negative on error | |
| 97 */ | 98 */ |
| 98 static void build_filter(FELEM *filter, double factor, int tap_count, int phase_count, int scale, int type){ | 99 static int build_filter(FELEM *filter, double factor, int tap_count, int phase_count, int scale, int type){ |
| 99 int ph, i; | 100 int ph, i; |
| 100 double x, y, w, tab[tap_count]; | 101 double x, y, w; |
| 102 double *tab = av_malloc(tap_count * sizeof(*tab)); | |
| 101 const int center= (tap_count-1)/2; | 103 const int center= (tap_count-1)/2; |
| 104 | |
| 105 if (!tab) | |
| 106 return AVERROR(ENOMEM); | |
| 102 | 107 |
| 103 /* if upsampling, only need to interpolate, no filter */ | 108 /* if upsampling, only need to interpolate, no filter */ |
| 104 if (factor > 1.0) | 109 if (factor > 1.0) |
| 105 factor = 1.0; | 110 factor = 1.0; |
| 106 | 111 |
| 174 maxff=maxsf= -2; | 179 maxff=maxsf= -2; |
| 175 } | 180 } |
| 176 } | 181 } |
| 177 } | 182 } |
| 178 #endif | 183 #endif |
| 184 | |
| 185 av_free(tab); | |
| 186 return 0; | |
| 179 } | 187 } |
| 180 | 188 |
| 181 AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){ | 189 AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){ |
| 182 AVResampleContext *c= av_mallocz(sizeof(AVResampleContext)); | 190 AVResampleContext *c= av_mallocz(sizeof(AVResampleContext)); |
| 183 double factor= FFMIN(out_rate * cutoff / in_rate, 1.0); | 191 double factor= FFMIN(out_rate * cutoff / in_rate, 1.0); |
| 192 | 200 |
| 193 c->filter_length= FFMAX((int)ceil(filter_size/factor), 1); | 201 c->filter_length= FFMAX((int)ceil(filter_size/factor), 1); |
| 194 c->filter_bank= av_mallocz(c->filter_length*(phase_count+1)*sizeof(FELEM)); | 202 c->filter_bank= av_mallocz(c->filter_length*(phase_count+1)*sizeof(FELEM)); |
| 195 if (!c->filter_bank) | 203 if (!c->filter_bank) |
| 196 goto error; | 204 goto error; |
| 197 build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, WINDOW_TYPE); | 205 if (build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, WINDOW_TYPE)) |
| 206 goto error; | |
| 198 memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM)); | 207 memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM)); |
| 199 c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1]; | 208 c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1]; |
| 200 | 209 |
| 201 c->src_incr= out_rate; | 210 c->src_incr= out_rate; |
| 202 c->ideal_dst_incr= c->dst_incr= in_rate * phase_count; | 211 c->ideal_dst_incr= c->dst_incr= in_rate * phase_count; |
| 203 c->index= -phase_count*((c->filter_length-1)/2); | 212 c->index= -phase_count*((c->filter_length-1)/2); |
| 204 | 213 |
| 205 return c; | 214 return c; |
| 206 error: | 215 error: |
| 216 av_free(c->filter_bank); | |
| 207 av_free(c); | 217 av_free(c); |
| 208 return NULL; | 218 return NULL; |
| 209 } | 219 } |
| 210 | 220 |
| 211 void av_resample_close(AVResampleContext *c){ | 221 void av_resample_close(AVResampleContext *c){ |
