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 {
50 enum VectorScopeScale {
58 typedef struct AudioVectorScopeContext {
71 unsigned prev_x, prev_y;
72 AVRational frame_rate;
74 } AudioVectorScopeContext;
76 #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
77 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
79 static const AVOption avectorscope_options[] = {
80 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
81 { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
82 { "lissajous", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS}, 0, 0, FLAGS, "mode" },
83 { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
84 { "polar", "", 0, AV_OPT_TYPE_CONST, {.i64=POLAR}, 0, 0, FLAGS, "mode" },
85 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
86 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
87 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
88 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
89 { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40}, 0, 255, FLAGS },
90 { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
91 { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80}, 0, 255, FLAGS },
92 { "ac", "set alpha contrast", OFFSET(contrast[3]), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
93 { "rf", "set red fade", OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
94 { "gf", "set green fade", OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
95 { "bf", "set blue fade", OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
96 { "af", "set alpha fade", OFFSET(fade[3]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
97 { "zoom", "set zoom factor", OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 10, FLAGS },
98 { "draw", "set draw mode", OFFSET(draw), AV_OPT_TYPE_INT, {.i64=DOT}, 0, DRAW_NB-1, FLAGS, "draw" },
99 { "dot", "", 0, AV_OPT_TYPE_CONST, {.i64=DOT} , 0, 0, FLAGS, "draw" },
100 { "line", "", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "draw" },
101 { "scale", "set amplitude scale mode", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LIN}, 0, SCALE_NB-1, FLAGS, "scale" },
102 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LIN}, 0, 0, FLAGS, "scale" },
103 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
104 { "cbrt", "cube root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
105 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
106 { "swap", "swap x axis with y axis", OFFSET(swap), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
107 { "mirror", "mirror axis", OFFSET(mirror), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "mirror" },
108 { "none", "no mirror", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mirror" },
109 { "x", "mirror x", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mirror" },
110 { "y", "mirror y", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mirror" },
111 { "xy", "mirror both", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "mirror" },
115 AVFILTER_DEFINE_CLASS(avectorscope);
117 static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
119 const int linesize = s->outpicref->linesize[0];
123 if (y >= s->h || x >= s->w)
126 y = FFMIN(y, s->h - 1);
127 x = FFMIN(x, s->w - 1);
130 dst = &s->outpicref->data[0][y * linesize + x * 4];
131 dst[0] = FFMIN(dst[0] + s->contrast[0], 255);
132 dst[1] = FFMIN(dst[1] + s->contrast[1], 255);
133 dst[2] = FFMIN(dst[2] + s->contrast[2], 255);
134 dst[3] = FFMIN(dst[3] + s->contrast[3], 255);
137 static void draw_line(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
139 int dx = FFABS(x1-x0), sx = x0 < x1 ? 1 : -1;
140 int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
141 int err = (dx>dy ? dx : -dy) / 2, e2;
146 if (x0 == x1 && y0 == y1)
163 static void fade(AudioVectorScopeContext *s)
165 const int linesize = s->outpicref->linesize[0];
168 if (s->fade[0] || s->fade[1] || s->fade[2]) {
169 uint8_t *d = s->outpicref->data[0];
170 for (i = 0; i < s->h; i++) {
171 for (j = 0; j < s->w*4; j+=4) {
172 d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
173 d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
174 d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
175 d[j+3] = FFMAX(d[j+3] - s->fade[3], 0);
182 static int query_formats(AVFilterContext *ctx)
184 AVFilterFormats *formats = NULL;
185 AVFilterChannelLayouts *layout = NULL;
186 AVFilterLink *inlink = ctx->inputs[0];
187 AVFilterLink *outlink = ctx->outputs[0];
188 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
189 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
192 formats = ff_make_format_list(sample_fmts);
193 if ((ret = ff_formats_ref (formats, &inlink->outcfg.formats )) < 0 ||
194 (ret = ff_add_channel_layout (&layout, AV_CH_LAYOUT_STEREO )) < 0 ||
195 (ret = ff_channel_layouts_ref (layout , &inlink->outcfg.channel_layouts)) < 0)
198 formats = ff_all_samplerates();
199 if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
202 formats = ff_make_format_list(pix_fmts);
203 if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
209 static int config_input(AVFilterLink *inlink)
211 AVFilterContext *ctx = inlink->dst;
212 AudioVectorScopeContext *s = ctx->priv;
214 s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
219 static int config_output(AVFilterLink *outlink)
221 AudioVectorScopeContext *s = outlink->src->priv;
225 outlink->sample_aspect_ratio = (AVRational){1,1};
226 outlink->frame_rate = s->frame_rate;
228 s->prev_x = s->hw = s->w / 2;
229 s->prev_y = s->hh = s->mode == POLAR ? s->h - 1 : s->h / 2;
234 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
236 AVFilterContext *ctx = inlink->dst;
237 AVFilterLink *outlink = ctx->outputs[0];
238 AudioVectorScopeContext *s = ctx->priv;
239 const int hw = s->hw;
240 const int hh = s->hh;
243 unsigned prev_x = s->prev_x, prev_y = s->prev_y;
244 double zoom = s->zoom;
247 if (!s->outpicref || s->outpicref->width != outlink->w ||
248 s->outpicref->height != outlink->h) {
249 av_frame_free(&s->outpicref);
250 s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
252 av_frame_free(&insamples);
253 return AVERROR(ENOMEM);
256 s->outpicref->sample_aspect_ratio = (AVRational){1,1};
257 for (i = 0; i < outlink->h; i++)
258 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
260 s->outpicref->pts = insamples->pts;
267 switch (insamples->format) {
268 case AV_SAMPLE_FMT_S16: {
269 int16_t *samples = (int16_t *)insamples->data[0];
271 for (i = 0; i < insamples->nb_samples * 2; i++) {
272 float sample = samples[i] / (float)INT16_MAX;
273 max = FFMAX(FFABS(sample), max);
278 case AV_SAMPLE_FMT_FLT: {
279 float *samples = (float *)insamples->data[0];
281 for (i = 0; i < insamples->nb_samples * 2; i++) {
282 max = FFMAX(FFABS(samples[i]), max);
293 for (i = 0; i < insamples->nb_samples; i++) {
294 int16_t *samples = (int16_t *)insamples->data[0] + i * 2;
295 float *samplesf = (float *)insamples->data[0] + i * 2;
298 switch (insamples->format) {
299 case AV_SAMPLE_FMT_S16:
300 src[0] = samples[0] / (float)INT16_MAX;
301 src[1] = samples[1] / (float)INT16_MAX;
303 case AV_SAMPLE_FMT_FLT:
304 src[0] = samplesf[0];
305 src[1] = samplesf[1];
313 src[0] = FFSIGN(src[0]) * sqrtf(FFABS(src[0]));
314 src[1] = FFSIGN(src[1]) * sqrtf(FFABS(src[1]));
317 src[0] = FFSIGN(src[0]) * cbrtf(FFABS(src[0]));
318 src[1] = FFSIGN(src[1]) * cbrtf(FFABS(src[1]));
321 src[0] = FFSIGN(src[0]) * logf(1 + FFABS(src[0])) / logf(2);
322 src[1] = FFSIGN(src[1]) * logf(1 + FFABS(src[1])) / logf(2);
333 FFSWAP(float, src[0], src[1]);
335 if (s->mode == LISSAJOUS) {
336 x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
337 y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
338 } else if (s->mode == LISSAJOUS_XY) {
339 x = (src[1] * zoom + 1) * hw;
340 y = (src[0] * zoom + 1) * hh;
342 float sx, sy, cx, cy;
346 cx = sx * sqrtf(1 - 0.5 * sy * sy);
347 cy = sy * sqrtf(1 - 0.5 * sx * sx);
348 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
349 y = s->h - s->h * fabsf(cx + cy) * .7;
352 if (s->draw == DOT) {
355 draw_line(s, x, y, prev_x, prev_y);
361 s->prev_x = x, s->prev_y = y;
362 av_frame_free(&insamples);
364 clone = av_frame_clone(s->outpicref);
366 return AVERROR(ENOMEM);
368 return ff_filter_frame(outlink, clone);
371 static int activate(AVFilterContext *ctx)
373 AVFilterLink *inlink = ctx->inputs[0];
374 AVFilterLink *outlink = ctx->outputs[0];
375 AudioVectorScopeContext *s = ctx->priv;
379 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
381 ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
385 return filter_frame(inlink, in);
387 FF_FILTER_FORWARD_STATUS(inlink, outlink);
388 FF_FILTER_FORWARD_WANTED(outlink, inlink);
390 return FFERROR_NOT_READY;
393 static av_cold void uninit(AVFilterContext *ctx)
395 AudioVectorScopeContext *s = ctx->priv;
397 av_frame_free(&s->outpicref);
400 static const AVFilterPad audiovectorscope_inputs[] = {
403 .type = AVMEDIA_TYPE_AUDIO,
404 .config_props = config_input,
409 static const AVFilterPad audiovectorscope_outputs[] = {
412 .type = AVMEDIA_TYPE_VIDEO,
413 .config_props = config_output,
418 const AVFilter ff_avf_avectorscope = {
419 .name = "avectorscope",
420 .description = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
422 .query_formats = query_formats,
423 .priv_size = sizeof(AudioVectorScopeContext),
424 .activate = activate,
425 .inputs = audiovectorscope_inputs,
426 .outputs = audiovectorscope_outputs,
427 .priv_class = &avectorscope_class,