History

avfilter/avf_showfreqs: add rate option

Fix possible buffer overflow.

Paul B Mahol committed 4 years ago

Commit d8e8aa91 parent fe06e702 files · +35 −26Browse files

Showing the changes to libavfilter (1 of 2 files). Show all

Unified · Split

libavfilter/avf_showfreqs.c+32−26
@@ -70,6 +70,9 @@ typedef struct ShowFreqsContext { · 69 unmodified lines
7070 int win_size;
7171 float scale;
7272 char *colors;
73+ int64_t pts;
74+ int64_t old_pts;
75+ AVRational frame_rate;
7376 } ShowFreqsContext;
7477
7578 #define OFFSET(x) offsetof(ShowFreqsContext, x)
@@ -78,6 +81,8 @@ typedef struct ShowFreqsContext { · 2 unmodified lines
7881 static const AVOption showfreqs_options[] = {
7982 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
8083 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
84+ { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
85+ { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
8186 { "mode", "set display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=BAR}, 0, NB_MODES-1, FLAGS, "mode" },
8287 { "line", "show lines", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "mode" },
8388 { "bar", "show bars", 0, AV_OPT_TYPE_CONST, {.i64=BAR}, 0, 0, FLAGS, "mode" },
@@ -149,6 +154,7 @@ static int config_output(AVFilterLink *outlink) · 65 unmodified lines
149154 float overlap, scale;
150155 int i, ret;
151156
157+ s->old_pts = AV_NOPTS_VALUE;
152158 s->nb_freq = s->fft_size / 2;
153159 s->win_size = s->fft_size;
154160 av_tx_uninit(&s->fft);
@@ -215,7 +221,7 @@ static int config_output(AVFilterLink *outlink) · 60 unmodified lines
215221 if (!s->window)
216222 return AVERROR(ENOMEM);
217223
218− outlink->frame_rate = av_make_q(inlink->sample_rate, s->hop_size);
224+ outlink->frame_rate = s->frame_rate;
219225 outlink->time_base = av_inv_q(outlink->frame_rate);
220226 outlink->sample_aspect_ratio = (AVRational){1,1};
221227 outlink->w = s->w;
@@ -328,13 +334,13 @@ static inline void plot_freq(ShowFreqsContext *s, int ch, · 106 unmodified lines
328334
329335 switch (s->avg) {
330336 case 0:
331− y = s->avg_data[ch][f] = !outlink->frame_count_in ? y : FFMIN(avg, y);
337+ y = s->avg_data[ch][f] = !outlink->frame_count_in ? y : FFMIN(0, y);
332338 break;
333339 case 1:
334340 break;
335341 default:
336− s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outlink->frame_count_in + 1, s->avg) * y);
337− y = s->avg_data[ch][f];
342+ s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outlink->frame_count_in + 1, s->avg) * (float)y);
343+ y = av_clip(s->avg_data[ch][f], 0, outlink->h - 1);
338344 break;
339345 }
340346
@@ -379,13 +385,6 @@ static int plot_freqs(AVFilterLink *inlink, int64_t pts) · 38 unmodified lines
379385 AVFrame *out;
380386 int ch, n;
381387
382− out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
383− if (!out)
384− return AVERROR(ENOMEM);
385−
386− for (n = 0; n < outlink->h; n++)
387− memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4);
388−
389388 /* fill FFT input with the number of samples available */
390389 for (ch = 0; ch < s->nb_channels; ch++) {
391390 const float *p = (float *)in->extended_data[ch];
@@ -407,16 +406,26 @@ static int plot_freqs(AVFilterLink *inlink, int64_t pts) · 15 unmodified lines
407406 s->tx_fn(s->fft, s->fft_data[ch], s->fft_input[ch], sizeof(float));
408407 }
409408
409+ s->pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
410+ if (s->old_pts >= s->pts)
411+ return 0;
412+ s->old_pts = s->pts;
413+
410414 #define RE(x, ch) s->fft_data[ch][x].re
411415 #define IM(x, ch) s->fft_data[ch][x].im
412416 #define M(a, b) (sqrt((a) * (a) + (b) * (b)))
413417 #define P(a, b) (atan2((b), (a)))
414418
415419 colors = av_strdup(s->colors);
416− if (!colors) {
417− av_frame_free(&out);
420+ if (!colors)
418421 return AVERROR(ENOMEM);
419− }
422+
423+ out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
424+ if (!out)
425+ return AVERROR(ENOMEM);
426+
427+ for (n = 0; n < outlink->h; n++)
428+ memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4);
420429
421430 for (ch = 0; ch < s->nb_channels; ch++) {
422431 uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
@@ -432,29 +441,21 @@ static int plot_freqs(AVFilterLink *inlink, int64_t pts) · 9 unmodified lines
432441
433442 switch (s->data_mode) {
434443 case MAGNITUDE:
435− a = av_clipd(M(RE(0, ch), 0) / s->scale, 0, 1);
436− plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink);
437−
438− for (f = 1; f < s->nb_freq; f++) {
444+ for (f = 0; f < s->nb_freq; f++) {
439445 a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1);
440446
441447 plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
442448 }
443449 break;
444450 case PHASE:
445− a = av_clipd((M_PI + P(RE(0, ch), 0)) / (2. * M_PI), 0, 1);
446− plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink);
447−
448− for (f = 1; f < s->nb_freq; f++) {
451+ for (f = 0; f < s->nb_freq; f++) {
449452 a = av_clipd((M_PI + P(RE(f, ch), IM(f, ch))) / (2. * M_PI), 0, 1);
450453
451454 plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
452455 }
453456 break;
454457 case DELAY:
455− plot_freq(s, ch, 0, 0, fg, &prev_y, out, outlink);
456−
457− for (f = 1; f < s->nb_freq; f++) {
458+ for (f = 0; f < s->nb_freq; f++) {
458459 a = av_clipd((M_PI - P(IM(f, ch) * RE(f-1, ch) - IM(f-1, ch) * RE(f, ch),
459460 RE(f, ch) * RE(f-1, ch) + IM(f, ch) * IM(f-1, ch))) / (2. * M_PI), 0, 1);
460461
@@ -465,7 +466,7 @@ static int plot_freqs(AVFilterLink *inlink, int64_t pts) · 4 unmodified lines
465466 }
466467
467468 av_free(colors);
468− out->pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
469+ out->pts = s->pts;
469470 out->sample_aspect_ratio = (AVRational){1,1};
470471 return ff_filter_frame(outlink, out);
471472 }
@@ -509,6 +510,11 @@ static int activate(AVFilterContext *ctx) · 37 unmodified lines
509510 if (ret < 0)
510511 return ret;
511512
513+ if (ff_inlink_queued_samples(inlink) >= s->hop_size) {
514+ ff_filter_set_ready(ctx, 10);
515+ return 0;
516+ }
517+
512518 FF_FILTER_FORWARD_STATUS(inlink, outlink);
513519 FF_FILTER_FORWARD_WANTED(outlink, inlink);
514520

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