History

avfft: avoid overreads with RDFT API users

The new API requires an extra array member at the very end, which old API users did not do. This disables in-place RDFT transforms and instead does the transform out of place by copying once, there shouldn't be a significant loss of speed as our in-place FFT requires a reorder which is likely more expensive in the majority of cases to do.

Lynne committed 2 years ago

Commit 90adef91 parent ce7c90f1 file · +25 −6Browse files

Unified · Split

libavcodec/avfft.c+25−6
@@ -152,7 +152,7 @@ RDFTContext *av_rdft_init(int nbits, enum RDFTransformType trans) · 151 unmodified lines
152 return NULL;152 return NULL;
153 153
154 ret = av_tx_init(&s->ctx, &s->fn, AV_TX_FLOAT_RDFT, trans == IDFT_C2R,154 ret = av_tx_init(&s->ctx, &s->fn, AV_TX_FLOAT_RDFT, trans == IDFT_C2R,
155 1 << nbits, &scale, AV_TX_INPLACE);155 1 << nbits, &scale, 0x0);
156 if (ret < 0) {156 if (ret < 0) {
157 av_free(s);157 av_free(s);
158 return NULL;158 return NULL;
@@ -162,17 +162,35 @@ RDFTContext *av_rdft_init(int nbits, enum RDFTransformType trans) · 3 unmodified lines
162 s->len = 1 << nbits;162 s->len = 1 << nbits;
163 s->inv = trans == IDFT_C2R;163 s->inv = trans == IDFT_C2R;
164 164
165 s->tmp = av_malloc((s->len + 2)*sizeof(float));
166 if (!s->tmp) {
167 av_tx_uninit(&s->ctx);
168 av_free(s);
169 return NULL;
170 }
171
165 return (RDFTContext *)s;172 return (RDFTContext *)s;
166}173}
167 174
168void av_rdft_calc(RDFTContext *s, FFTSample *data)175void av_rdft_calc(RDFTContext *s, FFTSample *data)
169{176{
170 AVTXWrapper *w = (AVTXWrapper *)s;177 AVTXWrapper *w = (AVTXWrapper *)s;
171 if (w->inv)178 float *src = w->inv ? w->tmp : (float *)data;
172 FFSWAP(float, data[1], data[w->len]);179 float *dst = w->inv ? (float *)data : w->tmp;
173 w->fn(w->ctx, data, (void *)data, w->stride);180
174 if (!w->inv)181 if (w->inv) {
175 FFSWAP(float, data[1], data[w->len]);182 memcpy(src, data, w->len*sizeof(float));
183
184 src[w->len] = src[1];
185 src[1] = 0.0f;
186 }
187
188 w->fn(w->ctx, dst, (void *)src, w->stride);
189
190 if (!w->inv) {
191 dst[1] = dst[w->len];
192 memcpy(data, dst, w->len*sizeof(float));
193 }
176}194}
177 195
178av_cold void av_rdft_end(RDFTContext *s)196av_cold void av_rdft_end(RDFTContext *s)
@@ -180,6 +198,7 @@ av_cold void av_rdft_end(RDFTContext *s) · 1 unmodified line
180 if (s) {198 if (s) {
181 AVTXWrapper *w = (AVTXWrapper *)s;199 AVTXWrapper *w = (AVTXWrapper *)s;
182 av_tx_uninit(&w->ctx);200 av_tx_uninit(&w->ctx);
201 av_free(w->tmp);
183 av_free(w);202 av_free(w);
184 }203 }
185}204}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t