History

avfilter/af_speechnorm: fix possible memleak on error to make frame writable

Fix this by adding support for non-writable frames.

Paul B Mahol committed 5 years ago

Commit 3fe49b51 parent 2ee40771 file · +25 −13Browse files

Unified · Split

libavfilter/af_speechnorm.c+25−13
@@ -84,7 +84,7 @@ typedef struct SpeechNormalizerContext { · 83 unmodified lines
8484 void (*analyze_channel)(AVFilterContext *ctx, ChannelContext *cc,
8585 const uint8_t *srcp, int nb_samples);
8686 void (*filter_channels[2])(AVFilterContext *ctx,
87− AVFrame *in, int nb_samples);
87+ AVFrame *in, AVFrame *out, int nb_samples);
8888 } SpeechNormalizerContext;
8989
9090 #define OFFSET(x) offsetof(SpeechNormalizerContext, x)
@@ -295,14 +295,15 @@ ANALYZE_CHANNEL(flt, float, 0.f, (float)MIN_PEAK) · 204 unmodified lines
295295
296296 #define FILTER_CHANNELS(name, ptype) \
297297 static void filter_channels_## name (AVFilterContext *ctx, \
298− AVFrame *in, int nb_samples) \
298+ AVFrame *in, AVFrame *out, int nb_samples) \
299299 { \
300300 SpeechNormalizerContext *s = ctx->priv; \
301301 AVFilterLink *inlink = ctx->inputs[0]; \
302302 \
303303 for (int ch = 0; ch < inlink->channels; ch++) { \
304304 ChannelContext *cc = &s->cc[ch]; \
305− ptype *dst = (ptype *)in->extended_data[ch]; \
305+ const ptype *src = (const ptype *)in->extended_data[ch]; \
306+ ptype *dst = (ptype *)out->extended_data[ch]; \
306307 const int bypass = !(av_channel_layout_extract_channel(inlink->channel_layout, ch) & s->channels); \
307308 int n = 0; \
308309 \
@@ -316,7 +317,7 @@ static void filter_channels_## name (AVFilterContext *ctx, · 7 unmodified lines
316317 gain = cc->gain_state; \
317318 consume_pi(cc, size); \
318319 for (int i = n; !ctx->is_disabled && i < n + size; i++) \
319− dst[i] *= gain; \
320+ dst[i] = src[i] * gain; \
320321 n += size; \
321322 } \
322323 } \
@@ -337,7 +338,8 @@ static float flerp(float min, float max, float mix) · 14 unmodified lines
337338
338339 #define FILTER_LINK_CHANNELS(name, ptype, tlerp) \
339340 static void filter_link_channels_## name (AVFilterContext *ctx, \
340− AVFrame *in, int nb_samples) \
341+ AVFrame *in, AVFrame *out, \
342+ int nb_samples) \
341343 { \
342344 SpeechNormalizerContext *s = ctx->priv; \
343345 AVFilterLink *inlink = ctx->inputs[0]; \
@@ -369,7 +371,8 @@ static void filter_link_channels_## name (AVFilterContext *ctx, · 25 unmodified lines
369371 \
370372 for (int ch = 0; ch < inlink->channels; ch++) { \
371373 ChannelContext *cc = &s->cc[ch]; \
372− ptype *dst = (ptype *)in->extended_data[ch]; \
374+ const ptype *src = (const ptype *)in->extended_data[ch]; \
375+ ptype *dst = (ptype *)out->extended_data[ch]; \
373376 \
374377 consume_pi(cc, min_size); \
375378 if (cc->bypass) \
@@ -377,7 +380,7 @@ static void filter_link_channels_## name (AVFilterContext *ctx, · 1 unmodified line
377380 \
378381 for (int i = n; !ctx->is_disabled && i < n + min_size; i++) { \
379382 ptype g = tlerp(s->prev_gain, gain, (i - n) / (ptype)min_size); \
380− dst[i] *= g; \
383+ dst[i] = src[i] * g; \
381384 } \
382385 } \
383386 \
@@ -398,7 +401,7 @@ static int filter_frame(AVFilterContext *ctx) · 14 unmodified lines
398401
399402 while (s->queue.available > 0) {
400403 int min_pi_nb_samples;
401− AVFrame *in;
404+ AVFrame *in, *out;
402405
403406 in = ff_bufqueue_peek(&s->queue, 0);
404407 if (!in)
@@ -410,16 +413,25 @@ static int filter_frame(AVFilterContext *ctx) · 5 unmodified lines
410413
411414 in = ff_bufqueue_get(&s->queue);
412415
413− ret = av_frame_make_writable(in);
414− if (ret < 0)
415− return ret;
416+ if (av_frame_is_writable(in)) {
417+ out = in;
418+ } else {
419+ out = ff_get_audio_buffer(outlink, in->nb_samples);
420+ if (!out) {
421+ av_frame_free(&in);
422+ return AVERROR(ENOMEM);
423+ }
424+ av_frame_copy_props(out, in);
425+ }
416426
417− s->filter_channels[s->link](ctx, in, in->nb_samples);
427+ s->filter_channels[s->link](ctx, in, out, in->nb_samples);
418428
419429 s->pts = in->pts + av_rescale_q(in->nb_samples, av_make_q(1, outlink->sample_rate),
420430 outlink->time_base);
421431
422− return ff_filter_frame(outlink, in);
432+ if (out != in)
433+ av_frame_free(&in);
434+ return ff_filter_frame(outlink, out);
423435 }
424436
425437 for (int f = 0; f < ff_inlink_queued_frames(inlink); f++) {

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