1 /*
2 * Copyright (c) 2013 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file
23 * audio to video multimedia vectorscope filter
24 */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/parseutils.h"
30 #include "avfilter.h"
31 #include "filters.h"
32 #include "formats.h"
33 #include "audio.h"
34 #include "video.h"
35 #include "internal.h"
36
37 enum VectorScopeMode {
38 LISSAJOUS,
39 LISSAJOUS_XY,
40 POLAR,
41 MODE_NB,
42 };
43
44 enum VectorScopeDraw {
45 DOT,
46 LINE,
47 DRAW_NB,
48 };
49
50 enum VectorScopeScale {
51 LIN,
52 SQRT,
53 CBRT,
54 LOG,
55 SCALE_NB,
56 };
57
58 typedef struct AudioVectorScopeContext {
59 const AVClass *class;
60 AVFrame *outpicref;
61 int w, h;
62 int hw, hh;
63 int mode;
64 int draw;
65 int scale;
66 int contrast[4];
67 int fade[4];
68 double zoom;
69 int swap;
70 int mirror;
71 unsigned prev_x, prev_y;
72 AVRational frame_rate;
73 int nb_samples;
74 } AudioVectorScopeContext;
75
76 #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
77 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
78 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
79
80 static const AVOption avectorscope_options[] = {
81 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, TFLAGS, "mode" },
82 { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, TFLAGS, "mode" },
83 { "lissajous", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS}, 0, 0, TFLAGS, "mode" },
84 { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, TFLAGS, "mode" },
85 { "polar", "", 0, AV_OPT_TYPE_CONST, {.i64=POLAR}, 0, 0, TFLAGS, "mode" },
86 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
87 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
88 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
89 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
90 { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40}, 0, 255, TFLAGS },
91 { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, TFLAGS },
92 { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80}, 0, 255, TFLAGS },
93 { "ac", "set alpha contrast", OFFSET(contrast[3]), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, TFLAGS },
94 { "rf", "set red fade", OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, TFLAGS },
95 { "gf", "set green fade", OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, TFLAGS },
96 { "bf", "set blue fade", OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, TFLAGS },
97 { "af", "set alpha fade", OFFSET(fade[3]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, TFLAGS },
98 { "zoom", "set zoom factor", OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 10, TFLAGS },
99 { "draw", "set draw mode", OFFSET(draw), AV_OPT_TYPE_INT, {.i64=DOT}, 0, DRAW_NB-1, TFLAGS, "draw" },
100 { "dot", "", 0, AV_OPT_TYPE_CONST, {.i64=DOT} , 0, 0, TFLAGS, "draw" },
101 { "line", "", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, TFLAGS, "draw" },
102 { "scale", "set amplitude scale mode", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LIN}, 0, SCALE_NB-1, TFLAGS, "scale" },
103 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LIN}, 0, 0, TFLAGS, "scale" },
104 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, TFLAGS, "scale" },
105 { "cbrt", "cube root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, TFLAGS, "scale" },
106 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, TFLAGS, "scale" },
107 { "swap", "swap x axis with y axis", OFFSET(swap), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, TFLAGS },
108 { "mirror", "mirror axis", OFFSET(mirror), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, TFLAGS, "mirror" },
109 { "none", "no mirror", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, TFLAGS, "mirror" },
110 { "x", "mirror x", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, TFLAGS, "mirror" },
111 { "y", "mirror y", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, TFLAGS, "mirror" },
112 { "xy", "mirror both", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, TFLAGS, "mirror" },
113 { NULL }
114 };
115
116 AVFILTER_DEFINE_CLASS(avectorscope);
117
draw_dot(AudioVectorScopeContext * s,unsigned x,unsigned y)118 static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
119 {
120 const int linesize = s->outpicref->linesize[0];
121 uint8_t *dst;
122
123 if (s->zoom > 1) {
124 if (y >= s->h || x >= s->w)
125 return;
126 } else {
127 y = FFMIN(y, s->h - 1);
128 x = FFMIN(x, s->w - 1);
129 }
130
131 dst = &s->outpicref->data[0][y * linesize + x * 4];
132 dst[0] = FFMIN(dst[0] + s->contrast[0], 255);
133 dst[1] = FFMIN(dst[1] + s->contrast[1], 255);
134 dst[2] = FFMIN(dst[2] + s->contrast[2], 255);
135 dst[3] = FFMIN(dst[3] + s->contrast[3], 255);
136 }
137
draw_line(AudioVectorScopeContext * s,int x0,int y0,int x1,int y1)138 static void draw_line(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
139 {
140 int dx = FFABS(x1-x0), sx = x0 < x1 ? 1 : -1;
141 int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
142 int err = (dx>dy ? dx : -dy) / 2, e2;
143
144 for (;;) {
145 draw_dot(s, x0, y0);
146
147 if (x0 == x1 && y0 == y1)
148 break;
149
150 e2 = err;
151
152 if (e2 >-dx) {
153 err -= dy;
154 x0 += sx;
155 }
156
157 if (e2 < dy) {
158 err += dx;
159 y0 += sy;
160 }
161 }
162 }
163
fade(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)164 static int fade(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
165 {
166 AudioVectorScopeContext *s = ctx->priv;
167 const int linesize = s->outpicref->linesize[0];
168 const int height = s->outpicref->height;
169 const int slice_start = (height * jobnr ) / nb_jobs;
170 const int slice_end = (height * (jobnr+1)) / nb_jobs;
171
172 if (s->fade[0] == 255 && s->fade[1] == 255 && s->fade[2] == 255) {
173 for (int i = slice_start; i < slice_end; i++)
174 memset(s->outpicref->data[0] + i * linesize, 0, s->outpicref->width * 4);
175 return 0;
176 }
177
178 if (s->fade[0] || s->fade[1] || s->fade[2]) {
179 uint8_t *d = s->outpicref->data[0] + slice_start * linesize;
180 for (int i = slice_start; i < slice_end; i++) {
181 for (int j = 0; j < s->w*4; j+=4) {
182 if (d[j+0])
183 d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
184 if (d[j+1])
185 d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
186 if (d[j+2])
187 d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
188 if (d[j+3])
189 d[j+3] = FFMAX(d[j+3] - s->fade[3], 0);
190 }
191 d += linesize;
192 }
193 }
194
195 return 0;
196 }
197
query_formats(AVFilterContext * ctx)198 static int query_formats(AVFilterContext *ctx)
199 {
200 AVFilterFormats *formats = NULL;
201 AVFilterChannelLayouts *layout = NULL;
202 AVFilterLink *inlink = ctx->inputs[0];
203 AVFilterLink *outlink = ctx->outputs[0];
204 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
205 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
206 int ret;
207
208 formats = ff_make_format_list(sample_fmts);
209 if ((ret = ff_formats_ref (formats, &inlink->outcfg.formats )) < 0 ||
210 (ret = ff_add_channel_layout (&layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 ||
211 (ret = ff_channel_layouts_ref (layout , &inlink->outcfg.channel_layouts)) < 0)
212 return ret;
213
214 formats = ff_all_samplerates();
215 if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
216 return ret;
217
218 formats = ff_make_format_list(pix_fmts);
219 if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
220 return ret;
221
222 return 0;
223 }
224
config_input(AVFilterLink * inlink)225 static int config_input(AVFilterLink *inlink)
226 {
227 AVFilterContext *ctx = inlink->dst;
228 AudioVectorScopeContext *s = ctx->priv;
229
230 s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
231
232 return 0;
233 }
234
config_output(AVFilterLink * outlink)235 static int config_output(AVFilterLink *outlink)
236 {
237 AudioVectorScopeContext *s = outlink->src->priv;
238
239 outlink->w = s->w;
240 outlink->h = s->h;
241 outlink->sample_aspect_ratio = (AVRational){1,1};
242 outlink->frame_rate = s->frame_rate;
243 outlink->time_base = av_inv_q(outlink->frame_rate);
244
245 s->prev_x = s->hw = s->w / 2;
246 s->prev_y = s->hh = s->mode == POLAR ? s->h - 1 : s->h / 2;
247
248 return 0;
249 }
250
filter_frame(AVFilterLink * inlink,AVFrame * insamples)251 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
252 {
253 AVFilterContext *ctx = inlink->dst;
254 AVFilterLink *outlink = ctx->outputs[0];
255 AudioVectorScopeContext *s = ctx->priv;
256 const int hw = s->hw;
257 const int hh = s->hh;
258 AVFrame *clone;
259 unsigned x, y;
260 unsigned prev_x = s->prev_x, prev_y = s->prev_y;
261 double zoom = s->zoom;
262 int i;
263
264 if (!s->outpicref || s->outpicref->width != outlink->w ||
265 s->outpicref->height != outlink->h) {
266 av_frame_free(&s->outpicref);
267 s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
268 if (!s->outpicref) {
269 av_frame_free(&insamples);
270 return AVERROR(ENOMEM);
271 }
272
273 s->outpicref->sample_aspect_ratio = (AVRational){1,1};
274 for (i = 0; i < outlink->h; i++)
275 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
276 }
277 s->outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
278
279 av_frame_make_writable(s->outpicref);
280 ff_filter_execute(ctx, fade, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
281
282 if (zoom < 1) {
283 float max = 0;
284
285 switch (insamples->format) {
286 case AV_SAMPLE_FMT_S16: {
287 int16_t *samples = (int16_t *)insamples->data[0];
288
289 for (i = 0; i < insamples->nb_samples * 2; i++) {
290 float sample = samples[i] / (float)INT16_MAX;
291 max = FFMAX(FFABS(sample), max);
292 }
293
294 }
295 break;
296 case AV_SAMPLE_FMT_FLT: {
297 float *samples = (float *)insamples->data[0];
298
299 for (i = 0; i < insamples->nb_samples * 2; i++) {
300 max = FFMAX(FFABS(samples[i]), max);
301 }
302 }
303 break;
304 default:
305 av_assert2(0);
306 }
307
308 switch (s->scale) {
309 case SQRT:
310 max = sqrtf(max);
311 break;
312 case CBRT:
313 max = cbrtf(max);
314 break;
315 case LOG:
316 max = logf(1 + max) / logf(2);
317 break;
318 }
319
320 zoom = 1. / max;
321 }
322
323 for (i = 0; i < insamples->nb_samples; i++) {
324 int16_t *samples = (int16_t *)insamples->data[0] + i * 2;
325 float *samplesf = (float *)insamples->data[0] + i * 2;
326 float src[2];
327
328 switch (insamples->format) {
329 case AV_SAMPLE_FMT_S16:
330 src[0] = samples[0] / (float)INT16_MAX;
331 src[1] = samples[1] / (float)INT16_MAX;
332 break;
333 case AV_SAMPLE_FMT_FLT:
334 src[0] = samplesf[0];
335 src[1] = samplesf[1];
336 break;
337 default:
338 av_assert2(0);
339 }
340
341 switch (s->scale) {
342 case SQRT:
343 src[0] = FFSIGN(src[0]) * sqrtf(FFABS(src[0]));
344 src[1] = FFSIGN(src[1]) * sqrtf(FFABS(src[1]));
345 break;
346 case CBRT:
347 src[0] = FFSIGN(src[0]) * cbrtf(FFABS(src[0]));
348 src[1] = FFSIGN(src[1]) * cbrtf(FFABS(src[1]));
349 break;
350 case LOG:
351 src[0] = FFSIGN(src[0]) * logf(1 + FFABS(src[0])) / logf(2);
352 src[1] = FFSIGN(src[1]) * logf(1 + FFABS(src[1])) / logf(2);
353 break;
354 }
355
356 if (s->mirror & 1)
357 src[0] = -src[0];
358
359 if (s->mirror & 2)
360 src[1] = -src[1];
361
362 if (s->swap)
363 FFSWAP(float, src[0], src[1]);
364
365 if (s->mode == LISSAJOUS) {
366 x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
367 y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
368 } else if (s->mode == LISSAJOUS_XY) {
369 x = (src[1] * zoom + 1) * hw;
370 y = (src[0] * zoom + 1) * hh;
371 } else {
372 float sx, sy, cx, cy;
373
374 sx = src[1] * zoom;
375 sy = src[0] * zoom;
376 cx = sx * sqrtf(1 - 0.5 * sy * sy);
377 cy = sy * sqrtf(1 - 0.5 * sx * sx);
378 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
379 y = s->h - s->h * fabsf(cx + cy) * .7;
380 }
381
382 if (s->draw == DOT) {
383 draw_dot(s, x, y);
384 } else {
385 draw_line(s, x, y, prev_x, prev_y);
386 }
387 prev_x = x;
388 prev_y = y;
389 }
390
391 s->prev_x = x, s->prev_y = y;
392 av_frame_free(&insamples);
393
394 clone = av_frame_clone(s->outpicref);
395 if (!clone)
396 return AVERROR(ENOMEM);
397
398 return ff_filter_frame(outlink, clone);
399 }
400
activate(AVFilterContext * ctx)401 static int activate(AVFilterContext *ctx)
402 {
403 AVFilterLink *inlink = ctx->inputs[0];
404 AVFilterLink *outlink = ctx->outputs[0];
405 AudioVectorScopeContext *s = ctx->priv;
406 AVFrame *in;
407 int ret;
408
409 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
410
411 ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
412 if (ret < 0)
413 return ret;
414 if (ret > 0)
415 return filter_frame(inlink, in);
416
417 if (ff_inlink_queued_samples(inlink) >= s->nb_samples) {
418 ff_filter_set_ready(ctx, 10);
419 return 0;
420 }
421
422 FF_FILTER_FORWARD_STATUS(inlink, outlink);
423 FF_FILTER_FORWARD_WANTED(outlink, inlink);
424
425 return FFERROR_NOT_READY;
426 }
427
uninit(AVFilterContext * ctx)428 static av_cold void uninit(AVFilterContext *ctx)
429 {
430 AudioVectorScopeContext *s = ctx->priv;
431
432 av_frame_free(&s->outpicref);
433 }
434
435 static const AVFilterPad audiovectorscope_inputs[] = {
436 {
437 .name = "default",
438 .type = AVMEDIA_TYPE_AUDIO,
439 .config_props = config_input,
440 },
441 };
442
443 static const AVFilterPad audiovectorscope_outputs[] = {
444 {
445 .name = "default",
446 .type = AVMEDIA_TYPE_VIDEO,
447 .config_props = config_output,
448 },
449 };
450
451 const AVFilter ff_avf_avectorscope = {
452 .name = "avectorscope",
453 .description = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
454 .uninit = uninit,
455 .priv_size = sizeof(AudioVectorScopeContext),
456 .activate = activate,
457 FILTER_INPUTS(audiovectorscope_inputs),
458 FILTER_OUTPUTS(audiovectorscope_outputs),
459 FILTER_QUERY_FUNC(query_formats),
460 .priv_class = &avectorscope_class,
461 .flags = AVFILTER_FLAG_SLICE_THREADS,
462 .process_command = ff_filter_process_command,
463 };
464