2 * Copyright (c) 2013 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * audio to video multimedia vectorscope filter
26 #include "libavutil/avassert.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/parseutils.h"
37 enum VectorScopeMode {
44 enum VectorScopeDraw {
51 enum VectorScopeScale {
59 typedef struct AudioVectorScopeContext {
72 unsigned prev_x, prev_y;
73 AVRational frame_rate;
75 } AudioVectorScopeContext;
77 #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
78 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
79 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
81 static const AVOption avectorscope_options[] = {
82 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, TFLAGS, "mode" },
83 { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, TFLAGS, "mode" },
84 { "lissajous", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS}, 0, 0, TFLAGS, "mode" },
85 { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, TFLAGS, "mode" },
86 { "polar", "", 0, AV_OPT_TYPE_CONST, {.i64=POLAR}, 0, 0, TFLAGS, "mode" },
87 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
88 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
89 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
90 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
91 { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40}, 0, 255, TFLAGS },
92 { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, TFLAGS },
93 { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80}, 0, 255, TFLAGS },
94 { "ac", "set alpha contrast", OFFSET(contrast[3]), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, TFLAGS },
95 { "rf", "set red fade", OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, TFLAGS },
96 { "gf", "set green fade", OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, TFLAGS },
97 { "bf", "set blue fade", OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, TFLAGS },
98 { "af", "set alpha fade", OFFSET(fade[3]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, TFLAGS },
99 { "zoom", "set zoom factor", OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 10, TFLAGS },
100 { "draw", "set draw mode", OFFSET(draw), AV_OPT_TYPE_INT, {.i64=DOT}, 0, DRAW_NB-1, TFLAGS, "draw" },
101 { "dot", "draw dots", 0, AV_OPT_TYPE_CONST, {.i64=DOT} , 0, 0, TFLAGS, "draw" },
102 { "line", "draw lines", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, TFLAGS, "draw" },
103 { "aaline","draw anti-aliased lines", 0, AV_OPT_TYPE_CONST, {.i64=AALINE},0,0, TFLAGS, "draw" },
104 { "scale", "set amplitude scale mode", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LIN}, 0, SCALE_NB-1, TFLAGS, "scale" },
105 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LIN}, 0, 0, TFLAGS, "scale" },
106 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, TFLAGS, "scale" },
107 { "cbrt", "cube root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, TFLAGS, "scale" },
108 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, TFLAGS, "scale" },
109 { "swap", "swap x axis with y axis", OFFSET(swap), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, TFLAGS },
110 { "mirror", "mirror axis", OFFSET(mirror), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, TFLAGS, "mirror" },
111 { "none", "no mirror", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, TFLAGS, "mirror" },
112 { "x", "mirror x", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, TFLAGS, "mirror" },
113 { "y", "mirror y", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, TFLAGS, "mirror" },
114 { "xy", "mirror both", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, TFLAGS, "mirror" },
118 AVFILTER_DEFINE_CLASS(avectorscope);
120 static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y, int value)
122 const ptrdiff_t linesize = s->outpicref->linesize[0];
126 if (y >= s->h || x >= s->w)
129 y = FFMIN(y, s->h - 1);
130 x = FFMIN(x, s->w - 1);
133 dst = s->outpicref->data[0] + y * linesize + x * 4;
134 dst[0] = FFMIN(dst[0] + s->contrast[0], value);
135 dst[1] = FFMIN(dst[1] + s->contrast[1], value);
136 dst[2] = FFMIN(dst[2] + s->contrast[2], value);
137 dst[3] = FFMIN(dst[3] + s->contrast[3], value);
140 static void draw_line(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
142 int dx = FFABS(x1-x0), sx = x0 < x1 ? 1 : -1;
143 int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
144 int err = (dx>dy ? dx : -dy) / 2, e2;
147 draw_dot(s, x0, y0, 255);
149 if (x0 == x1 && y0 == y1)
166 static void draw_aaline(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
168 int sx = x0 < x1 ? 1 : -1, sy = y0 < y1 ? 1 : -1, x2;
169 int dx = FFABS(x1-x0), dy = FFABS(y1-y0), err = dx * dx + dy * dy;
170 int e2 = err == 0 ? 1 : 0xffffff / (dx + dy);
177 draw_dot(s, x0, y0, 255-(FFABS(err - dx + dy) >> 16));
183 if (e2 + dy < 0xff0000)
184 draw_dot(s, x0, y0 + sy, 255-((e2 + dy) >> 16));
192 if (dx - e2 < 0xff0000)
193 draw_dot(s, x2 + sx, y0, 255-((dx - e2) >> 16));
200 static int fade(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
202 AudioVectorScopeContext *s = ctx->priv;
203 const int linesize = s->outpicref->linesize[0];
204 const int height = s->outpicref->height;
205 const int slice_start = (height * jobnr ) / nb_jobs;
206 const int slice_end = (height * (jobnr+1)) / nb_jobs;
208 if (s->fade[0] == 255 && s->fade[1] == 255 && s->fade[2] == 255) {
209 for (int i = slice_start; i < slice_end; i++)
210 memset(s->outpicref->data[0] + i * linesize, 0, s->outpicref->width * 4);
214 if (s->fade[0] || s->fade[1] || s->fade[2]) {
215 uint8_t *d = s->outpicref->data[0] + slice_start * linesize;
216 for (int i = slice_start; i < slice_end; i++) {
217 for (int j = 0; j < s->w*4; j+=4) {
219 d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
221 d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
223 d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
225 d[j+3] = FFMAX(d[j+3] - s->fade[3], 0);
234 static int query_formats(AVFilterContext *ctx)
236 AVFilterFormats *formats = NULL;
237 AVFilterChannelLayouts *layout = NULL;
238 AVFilterLink *inlink = ctx->inputs[0];
239 AVFilterLink *outlink = ctx->outputs[0];
240 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
241 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
244 formats = ff_make_format_list(sample_fmts);
245 if ((ret = ff_formats_ref (formats, &inlink->outcfg.formats )) < 0 ||
246 (ret = ff_add_channel_layout (&layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 ||
247 (ret = ff_channel_layouts_ref (layout , &inlink->outcfg.channel_layouts)) < 0)
250 formats = ff_all_samplerates();
251 if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
254 formats = ff_make_format_list(pix_fmts);
255 if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
261 static int config_input(AVFilterLink *inlink)
263 AVFilterContext *ctx = inlink->dst;
264 AudioVectorScopeContext *s = ctx->priv;
266 s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
271 static int config_output(AVFilterLink *outlink)
273 AudioVectorScopeContext *s = outlink->src->priv;
277 outlink->sample_aspect_ratio = (AVRational){1,1};
278 outlink->frame_rate = s->frame_rate;
279 outlink->time_base = av_inv_q(outlink->frame_rate);
281 s->prev_x = s->hw = s->w / 2;
282 s->prev_y = s->hh = s->mode == POLAR ? s->h - 1 : s->h / 2;
287 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
289 AVFilterContext *ctx = inlink->dst;
290 AVFilterLink *outlink = ctx->outputs[0];
291 const int16_t *samples = (const int16_t *)insamples->data[0];
292 const float *samplesf = (const float *)insamples->data[0];
293 AudioVectorScopeContext *s = ctx->priv;
294 const int hw = s->hw;
295 const int hh = s->hh;
298 unsigned prev_x = s->prev_x, prev_y = s->prev_y;
299 double zoom = s->zoom;
302 if (!s->outpicref || s->outpicref->width != outlink->w ||
303 s->outpicref->height != outlink->h) {
304 av_frame_free(&s->outpicref);
305 s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
307 av_frame_free(&insamples);
308 return AVERROR(ENOMEM);
311 s->outpicref->sample_aspect_ratio = (AVRational){1,1};
312 for (int i = 0; i < outlink->h; i++)
313 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
315 s->outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
316 s->outpicref->duration = 1;
318 ret = ff_inlink_make_frame_writable(outlink, &s->outpicref);
320 av_frame_free(&insamples);
323 ff_filter_execute(ctx, fade, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
328 switch (insamples->format) {
329 case AV_SAMPLE_FMT_S16:
330 for (int i = 0; i < insamples->nb_samples * 2; i++) {
331 float sample = samples[i] / (float)INT16_MAX;
332 max = FFMAX(FFABS(sample), max);
335 case AV_SAMPLE_FMT_FLT:
336 for (int i = 0; i < insamples->nb_samples * 2; i++) {
337 max = FFMAX(FFABS(samplesf[i]), max);
352 max = logf(1 + max) / logf(2);
360 for (int i = 0; i < insamples->nb_samples; i++) {
363 switch (insamples->format) {
364 case AV_SAMPLE_FMT_S16:
365 src[0] = samples[i*2+0] / (float)INT16_MAX;
366 src[1] = samples[i*2+1] / (float)INT16_MAX;
368 case AV_SAMPLE_FMT_FLT:
369 src[0] = samplesf[i*2+0];
370 src[1] = samplesf[i*2+1];
378 src[0] = FFSIGN(src[0]) * sqrtf(FFABS(src[0]));
379 src[1] = FFSIGN(src[1]) * sqrtf(FFABS(src[1]));
382 src[0] = FFSIGN(src[0]) * cbrtf(FFABS(src[0]));
383 src[1] = FFSIGN(src[1]) * cbrtf(FFABS(src[1]));
386 src[0] = FFSIGN(src[0]) * logf(1 + FFABS(src[0])) / logf(2);
387 src[1] = FFSIGN(src[1]) * logf(1 + FFABS(src[1])) / logf(2);
398 FFSWAP(float, src[0], src[1]);
400 if (s->mode == LISSAJOUS) {
401 x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
402 y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
403 } else if (s->mode == LISSAJOUS_XY) {
404 x = (src[1] * zoom + 1) * hw;
405 y = (src[0] * zoom + 1) * hh;
407 float sx, sy, cx, cy;
411 cx = sx * sqrtf(1 - 0.5 * sy * sy);
412 cy = sy * sqrtf(1 - 0.5 * sx * sx);
413 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
414 y = s->h - s->h * fabsf(cx + cy) * .7;
417 if (s->draw == DOT) {
418 draw_dot(s, x, y, 255);
419 } else if (s->draw == LINE) {
420 draw_line(s, x, y, prev_x, prev_y);
422 draw_aaline(s, x, y, prev_x, prev_y);
428 s->prev_x = x, s->prev_y = y;
429 av_frame_free(&insamples);
431 clone = av_frame_clone(s->outpicref);
433 return AVERROR(ENOMEM);
435 return ff_filter_frame(outlink, clone);
438 static int activate(AVFilterContext *ctx)
440 AVFilterLink *inlink = ctx->inputs[0];
441 AVFilterLink *outlink = ctx->outputs[0];
442 AudioVectorScopeContext *s = ctx->priv;
446 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
448 ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
452 return filter_frame(inlink, in);
454 if (ff_inlink_queued_samples(inlink) >= s->nb_samples) {
455 ff_filter_set_ready(ctx, 10);
459 FF_FILTER_FORWARD_STATUS(inlink, outlink);
460 FF_FILTER_FORWARD_WANTED(outlink, inlink);
462 return FFERROR_NOT_READY;
465 static av_cold void uninit(AVFilterContext *ctx)
467 AudioVectorScopeContext *s = ctx->priv;
469 av_frame_free(&s->outpicref);
472 static const AVFilterPad audiovectorscope_inputs[] = {
475 .type = AVMEDIA_TYPE_AUDIO,
476 .config_props = config_input,
480 static const AVFilterPad audiovectorscope_outputs[] = {
483 .type = AVMEDIA_TYPE_VIDEO,
484 .config_props = config_output,
488 const AVFilter ff_avf_avectorscope = {
489 .name = "avectorscope",
490 .description = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
492 .priv_size = sizeof(AudioVectorScopeContext),
493 .activate = activate,
494 FILTER_INPUTS(audiovectorscope_inputs),
495 FILTER_OUTPUTS(audiovectorscope_outputs),
496 FILTER_QUERY_FUNC(query_formats),
497 .priv_class = &avectorscope_class,
498 .flags = AVFILTER_FLAG_SLICE_THREADS,
499 .process_command = ff_filter_process_command,