]> git.sesse.net Git - ffmpeg/blob - libavfilter/avf_avectorscope.c
avformat/movenc: add option to use keys/mdta atoms for metadata
[ffmpeg] / libavfilter / avf_avectorscope.c
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 "formats.h"
32 #include "audio.h"
33 #include "video.h"
34 #include "internal.h"
35
36 enum VectorScopeMode {
37     LISSAJOUS,
38     LISSAJOUS_XY,
39     POLAR,
40     MODE_NB,
41 };
42
43 enum VectorScopeDraw {
44     DOT,
45     LINE,
46     DRAW_NB,
47 };
48
49 typedef struct AudioVectorScopeContext {
50     const AVClass *class;
51     AVFrame *outpicref;
52     int w, h;
53     int hw, hh;
54     int mode;
55     int draw;
56     int contrast[4];
57     int fade[4];
58     double zoom;
59     unsigned prev_x, prev_y;
60     AVRational frame_rate;
61 } AudioVectorScopeContext;
62
63 #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
64 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
65
66 static const AVOption avectorscope_options[] = {
67     { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
68     { "m",    "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
69     { "lissajous",    "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS},    0, 0, FLAGS, "mode" },
70     { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
71     { "polar",        "", 0, AV_OPT_TYPE_CONST, {.i64=POLAR},        0, 0, FLAGS, "mode" },
72     { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
73     { "r",    "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
74     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
75     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
76     { "rc", "set red contrast",   OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40},  0, 255, FLAGS },
77     { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
78     { "bc", "set blue contrast",  OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80},  0, 255, FLAGS },
79     { "ac", "set alpha contrast", OFFSET(contrast[3]), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
80     { "rf", "set red fade",       OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
81     { "gf", "set green fade",     OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
82     { "bf", "set blue fade",      OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5},  0, 255, FLAGS },
83     { "af", "set alpha fade",     OFFSET(fade[3]), AV_OPT_TYPE_INT, {.i64=5},  0, 255, FLAGS },
84     { "zoom", "set zoom factor",  OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1},  1, 10, FLAGS },
85     { "draw", "set draw mode", OFFSET(draw), AV_OPT_TYPE_INT, {.i64=DOT}, 0, DRAW_NB-1, FLAGS, "draw" },
86     { "dot",   "", 0, AV_OPT_TYPE_CONST, {.i64=DOT} , 0, 0, FLAGS, "draw" },
87     { "line",  "", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "draw" },
88     { NULL }
89 };
90
91 AVFILTER_DEFINE_CLASS(avectorscope);
92
93 static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
94 {
95     const int linesize = s->outpicref->linesize[0];
96     uint8_t *dst;
97
98     if (s->zoom > 1) {
99         if (y >= s->h || x >= s->w)
100             return;
101     } else {
102         y = FFMIN(y, s->h - 1);
103         x = FFMIN(x, s->w - 1);
104     }
105
106     dst = &s->outpicref->data[0][y * linesize + x * 4];
107     dst[0] = FFMIN(dst[0] + s->contrast[0], 255);
108     dst[1] = FFMIN(dst[1] + s->contrast[1], 255);
109     dst[2] = FFMIN(dst[2] + s->contrast[2], 255);
110     dst[3] = FFMIN(dst[3] + s->contrast[3], 255);
111 }
112
113 static void draw_line(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
114 {
115     int dx = FFABS(x1-x0), sx = x0 < x1 ? 1 : -1;
116     int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
117     int err = (dx>dy ? dx : -dy) / 2, e2;
118
119     for (;;) {
120         draw_dot(s, x0, y0);
121
122         if (x0 == x1 && y0 == y1)
123             break;
124
125         e2 = err;
126
127         if (e2 >-dx) {
128             err -= dy;
129             x0 += sx;
130         }
131
132         if (e2 < dy) {
133             err += dx;
134             y0 += sy;
135         }
136     }
137 }
138
139 static void fade(AudioVectorScopeContext *s)
140 {
141     const int linesize = s->outpicref->linesize[0];
142     int i, j;
143
144     if (s->fade[0] || s->fade[1] || s->fade[2]) {
145         uint8_t *d = s->outpicref->data[0];
146         for (i = 0; i < s->h; i++) {
147             for (j = 0; j < s->w*4; j+=4) {
148                 d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
149                 d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
150                 d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
151                 d[j+3] = FFMAX(d[j+3] - s->fade[3], 0);
152             }
153             d += linesize;
154         }
155     }
156 }
157
158 static int query_formats(AVFilterContext *ctx)
159 {
160     AVFilterFormats *formats = NULL;
161     AVFilterChannelLayouts *layout = NULL;
162     AVFilterLink *inlink = ctx->inputs[0];
163     AVFilterLink *outlink = ctx->outputs[0];
164     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
165     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
166     int ret;
167
168     formats = ff_make_format_list(sample_fmts);
169     if ((ret = ff_formats_ref         (formats, &inlink->out_formats        )) < 0 ||
170         (ret = ff_add_channel_layout  (&layout, AV_CH_LAYOUT_STEREO         )) < 0 ||
171         (ret = ff_channel_layouts_ref (layout , &inlink->out_channel_layouts)) < 0)
172         return ret;
173
174     formats = ff_all_samplerates();
175     if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
176         return ret;
177
178     formats = ff_make_format_list(pix_fmts);
179     if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
180         return ret;
181
182     return 0;
183 }
184
185 static int config_input(AVFilterLink *inlink)
186 {
187     AVFilterContext *ctx = inlink->dst;
188     AudioVectorScopeContext *s = ctx->priv;
189     int nb_samples;
190
191     nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
192     inlink->partial_buf_size =
193     inlink->min_samples =
194     inlink->max_samples = nb_samples;
195
196     return 0;
197 }
198
199 static int config_output(AVFilterLink *outlink)
200 {
201     AudioVectorScopeContext *s = outlink->src->priv;
202
203     outlink->w = s->w;
204     outlink->h = s->h;
205     outlink->sample_aspect_ratio = (AVRational){1,1};
206     outlink->frame_rate = s->frame_rate;
207
208     s->prev_x = s->hw = s->w / 2;
209     s->prev_y = s->hh = s->h / 2;
210
211     return 0;
212 }
213
214 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
215 {
216     AVFilterContext *ctx = inlink->dst;
217     AVFilterLink *outlink = ctx->outputs[0];
218     AudioVectorScopeContext *s = ctx->priv;
219     const int hw = s->hw;
220     const int hh = s->hh;
221     unsigned x, y;
222     unsigned prev_x = s->prev_x, prev_y = s->prev_y;
223     const double zoom = s->zoom;
224     int i;
225
226     if (!s->outpicref || s->outpicref->width  != outlink->w ||
227                          s->outpicref->height != outlink->h) {
228         av_frame_free(&s->outpicref);
229         s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
230         if (!s->outpicref) {
231             av_frame_free(&insamples);
232             return AVERROR(ENOMEM);
233         }
234
235         for (i = 0; i < outlink->h; i++)
236             memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
237     }
238     s->outpicref->pts = insamples->pts;
239
240     fade(s);
241
242     switch (insamples->format) {
243     case AV_SAMPLE_FMT_S16:
244         for (i = 0; i < insamples->nb_samples; i++) {
245             int16_t *src = (int16_t *)insamples->data[0] + i * 2;
246
247             if (s->mode == LISSAJOUS) {
248                 x = ((src[1] - src[0]) * zoom / (float)(UINT16_MAX) + 1) * hw;
249                 y = (1.0 - (src[0] + src[1]) * zoom / (float)UINT16_MAX) * hh;
250             } else if (s->mode == LISSAJOUS_XY) {
251                 x = (src[1] * zoom / (float)INT16_MAX + 1) * hw;
252                 y = (src[0] * zoom / (float)INT16_MAX + 1) * hh;
253             } else {
254                 float sx, sy, cx, cy;
255
256                 sx = src[1] * zoom / (float)INT16_MAX;
257                 sy = src[0] * zoom / (float)INT16_MAX;
258                 cx = sx * sqrtf(1 - 0.5*sy*sy);
259                 cy = sy * sqrtf(1 - 0.5*sx*sx);
260                 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
261                 y = s->h - s->h * fabsf(cx + cy) * .7;
262             }
263
264             if (s->draw == DOT) {
265                 draw_dot(s, x, y);
266             } else {
267                 draw_line(s, x, y, prev_x, prev_y);
268             }
269             prev_x = x;
270             prev_y = y;
271         }
272         break;
273     case AV_SAMPLE_FMT_FLT:
274         for (i = 0; i < insamples->nb_samples; i++) {
275             float *src = (float *)insamples->data[0] + i * 2;
276
277             if (s->mode == LISSAJOUS) {
278                 x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
279                 y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
280             } else if (s->mode == LISSAJOUS_XY){
281                 x = (src[1] * zoom + 1) * hw;
282                 y = (src[0] * zoom + 1) * hh;
283             } else {
284                 float sx, sy, cx, cy;
285
286                 sx = src[1] * zoom;
287                 sy = src[0] * zoom;
288                 cx = sx * sqrtf(1 - 0.5 * sy * sy);
289                 cy = sy * sqrtf(1 - 0.5 * sx * sx);
290                 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
291                 y = s->h - s->h * fabsf(cx + cy) * .7;
292             }
293
294             if (s->draw == DOT) {
295                 draw_dot(s, x, y);
296             } else {
297                 draw_line(s, x, y, prev_x, prev_y);
298             }
299             prev_x = x;
300             prev_y = y;
301         }
302         break;
303     default:
304         av_assert0(0);
305     }
306
307     s->prev_x = x, s->prev_y = y;
308     av_frame_free(&insamples);
309
310     return ff_filter_frame(outlink, av_frame_clone(s->outpicref));
311 }
312
313 static av_cold void uninit(AVFilterContext *ctx)
314 {
315     AudioVectorScopeContext *s = ctx->priv;
316
317     av_frame_free(&s->outpicref);
318 }
319
320 static const AVFilterPad audiovectorscope_inputs[] = {
321     {
322         .name         = "default",
323         .type         = AVMEDIA_TYPE_AUDIO,
324         .config_props = config_input,
325         .filter_frame = filter_frame,
326     },
327     { NULL }
328 };
329
330 static const AVFilterPad audiovectorscope_outputs[] = {
331     {
332         .name         = "default",
333         .type         = AVMEDIA_TYPE_VIDEO,
334         .config_props = config_output,
335     },
336     { NULL }
337 };
338
339 AVFilter ff_avf_avectorscope = {
340     .name          = "avectorscope",
341     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
342     .uninit        = uninit,
343     .query_formats = query_formats,
344     .priv_size     = sizeof(AudioVectorScopeContext),
345     .inputs        = audiovectorscope_inputs,
346     .outputs       = audiovectorscope_outputs,
347     .priv_class    = &avectorscope_class,
348 };