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
152152 return NULL;
153153
154154 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);
156156 if (ret < 0) {
157157 av_free(s);
158158 return NULL;
@@ -162,17 +162,35 @@ RDFTContext *av_rdft_init(int nbits, enum RDFTransformType trans) · 3 unmodified lines
162162 s->len = 1 << nbits;
163163 s->inv = trans == IDFT_C2R;
164164
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+
165172 return (RDFTContext *)s;
166173 }
167174
168175 void av_rdft_calc(RDFTContext *s, FFTSample *data)
169176 {
170177 AVTXWrapper *w = (AVTXWrapper *)s;
171− if (w->inv)
172− FFSWAP(float, data[1], data[w->len]);
173− w->fn(w->ctx, data, (void *)data, w->stride);
174− if (!w->inv)
175− FFSWAP(float, data[1], data[w->len]);
178+ float *src = w->inv ? w->tmp : (float *)data;
179+ float *dst = w->inv ? (float *)data : w->tmp;
180+
181+ if (w->inv) {
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+ }
176194 }
177195
178196 av_cold void av_rdft_end(RDFTContext *s)
@@ -180,6 +198,7 @@ av_cold void av_rdft_end(RDFTContext *s) · 1 unmodified line
180198 if (s) {
181199 AVTXWrapper *w = (AVTXWrapper *)s;
182200 av_tx_uninit(&w->ctx);
201+ av_free(w->tmp);
183202 av_free(w);
184203 }
185204 }

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