Browse code

lavfi: avectorscope filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2013/02/21 19:32:59
Showing 6 changed files
... ...
@@ -39,6 +39,7 @@ version <next>:
39 39
   transcoding audio
40 40
 - Matroska muxer can now put the index at the beginning of the file.
41 41
 - extractplanes filter
42
+- avectorscope filter
42 43
 
43 44
 
44 45
 version 1.2:
... ...
@@ -7324,6 +7324,68 @@ tools.
7324 7324
 
7325 7325
 Below is a description of the currently available multimedia filters.
7326 7326
 
7327
+@section avectorscope
7328
+
7329
+Convert input audio to a video output, representing the audio vector
7330
+scope.
7331
+
7332
+The filter is used to measure the difference between channels of stereo
7333
+audio stream. A monoaural signal, consisting of identical left and right
7334
+signal, results in straight vertical line. Any stereo separation is visible
7335
+as a deviation from this line, creating a Lissajous figure.
7336
+If the straight (or deviation from it) but horizontal line appears this
7337
+indicates that the left and right channels are out of phase.
7338
+
7339
+The filter accepts the following options:
7340
+
7341
+@table @option
7342
+@item mode, m
7343
+Set the vectorscope mode.
7344
+
7345
+Available values are:
7346
+@table @samp
7347
+@item lissajous
7348
+Lissajous rotated by 45 degrees.
7349
+
7350
+@item lissajous_xy
7351
+Same as above but not rotated.
7352
+@end table
7353
+
7354
+Default value is @samp{lissajous}.
7355
+
7356
+@item size, s
7357
+Set the video size for the output. Default value is @code{400x400}.
7358
+
7359
+@item rate, r
7360
+Set the output frame rate. Default value is @code{25}.
7361
+
7362
+@item rc
7363
+@item gc
7364
+@item bc
7365
+Specify the red, green and blue contrast. Default values are @code{40}, @code{160} and @code{80}.
7366
+Allowed range is @code{[0, 255]}.
7367
+
7368
+@item rf
7369
+@item gf
7370
+@item bf
7371
+Specify the red, green and blue fade. Default values are @code{15}, @code{10} and @code{5}.
7372
+Allowed range is @code{[0, 255]}.
7373
+
7374
+@item zoom
7375
+Set the zoom factor. Default value is @code{1}. Allowed range is @code{[1, 10]}.
7376
+@end table
7377
+
7378
+@subsection Examples
7379
+
7380
+@itemize
7381
+@item
7382
+Complete example using @command{ffplay}:
7383
+@example
7384
+ffplay -f lavfi 'amovie=input.mp3, asplit [a][out1];
7385
+             [a] avectorscope=zoom=1.3:rc=2:gc=200:bc=10:rf=1:gf=8:bf=7 [out0]'
7386
+@end example
7387
+@end itemize
7388
+
7327 7389
 @section concat
7328 7390
 
7329 7391
 Concatenate audio and video streams, joining them together one after the
... ...
@@ -223,6 +223,7 @@ OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_uspp.o
223 223
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/pullup.o
224 224
 
225 225
 # multimedia filters
226
+OBJS-$(CONFIG_AVECTORSCOPE_FILTER)           += avf_avectorscope.o
226 227
 OBJS-$(CONFIG_CONCAT_FILTER)                 += avf_concat.o
227 228
 OBJS-$(CONFIG_SHOWSPECTRUM_FILTER)           += avf_showspectrum.o
228 229
 OBJS-$(CONFIG_SHOWWAVES_FILTER)              += avf_showwaves.o
... ...
@@ -199,6 +199,7 @@ void avfilter_register_all(void)
199 199
     REGISTER_FILTER(NULLSINK,       nullsink,       vsink);
200 200
 
201 201
     /* multimedia filters */
202
+    REGISTER_FILTER(AVECTORSCOPE,   avectorscope,   avf);
202 203
     REGISTER_FILTER(CONCAT,         concat,         avf);
203 204
     REGISTER_FILTER(SHOWSPECTRUM,   showspectrum,   avf);
204 205
     REGISTER_FILTER(SHOWWAVES,      showwaves,      avf);
205 206
new file mode 100644
... ...
@@ -0,0 +1,273 @@
0
+/*
1
+ * Copyright (c) 2013 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * audio to video multimedia vector scope filter
23
+ */
24
+
25
+#include "libavutil/avassert.h"
26
+#include "libavutil/channel_layout.h"
27
+#include "libavutil/opt.h"
28
+#include "libavutil/parseutils.h"
29
+#include "avfilter.h"
30
+#include "formats.h"
31
+#include "audio.h"
32
+#include "video.h"
33
+#include "internal.h"
34
+
35
+enum VectorScopeMode {
36
+    LISSAJOUS,
37
+    LISSAJOUS_XY,
38
+    MODE_NB,
39
+};
40
+
41
+typedef struct AudioVectorScopeContext {
42
+    const AVClass *class;
43
+    AVFrame *outpicref;
44
+    int w, h;
45
+    int hw, hh;
46
+    enum VectorScopeMode mode;
47
+    int contrast[3];
48
+    int fade[3];
49
+    double zoom;
50
+    AVRational frame_rate;
51
+} AudioVectorScopeContext;
52
+
53
+#define OFFSET(x) offsetof(AudioVectorScopeContext, x)
54
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
55
+
56
+static const AVOption avectorscope_options[] = {
57
+    { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
58
+    { "m",    "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
59
+    { "lissajous",    "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS},    0, 0, FLAGS, "mode" },
60
+    { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
61
+    { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
62
+    { "r",    "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
63
+    { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
64
+    { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
65
+    { "rc", "set red contrast",   OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40}, 0, 255, FLAGS },
66
+    { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
67
+    { "bc", "set blue contrast",  OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80}, 0, 255, FLAGS },
68
+    { "rf", "set red fade",       OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
69
+    { "gf", "set green fade",     OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
70
+    { "bf", "set blue fade",      OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
71
+    { "zoom", "set zoom factor",  OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 10, FLAGS },
72
+    {NULL},
73
+};
74
+
75
+AVFILTER_DEFINE_CLASS(avectorscope);
76
+
77
+static void draw_dot(AudioVectorScopeContext *p, unsigned x, unsigned y)
78
+{
79
+    const int linesize = p->outpicref->linesize[0];
80
+    uint8_t *dst;
81
+
82
+    if (p->zoom > 1) {
83
+        if (y >= p->h || x >= p->w)
84
+            return;
85
+    } else {
86
+        y = FFMIN(y, p->h - 1);
87
+        x = FFMIN(x, p->w - 1);
88
+    }
89
+
90
+    dst = &p->outpicref->data[0][y * linesize + x * 4];
91
+    dst[0] = FFMIN(dst[0] + p->contrast[0], 255);
92
+    dst[1] = FFMIN(dst[1] + p->contrast[1], 255);
93
+    dst[2] = FFMIN(dst[2] + p->contrast[2], 255);
94
+}
95
+
96
+static void fade(AudioVectorScopeContext *p)
97
+{
98
+    const int linesize = p->outpicref->linesize[0];
99
+    int i, j;
100
+
101
+    if (p->fade[0] || p->fade[1] || p->fade[2]) {
102
+        uint8_t *d = p->outpicref->data[0];
103
+        for (i = 0; i < p->h; i++) {
104
+            for (j = 0; j < p->w*4; j+=4) {
105
+                d[j+0] = FFMAX(d[j+0] - p->fade[0], 0);
106
+                d[j+1] = FFMAX(d[j+1] - p->fade[1], 0);
107
+                d[j+2] = FFMAX(d[j+2] - p->fade[2], 0);
108
+            }
109
+            d += linesize;
110
+        }
111
+    }
112
+}
113
+
114
+static int query_formats(AVFilterContext *ctx)
115
+{
116
+    AVFilterFormats *formats = NULL;
117
+    AVFilterChannelLayouts *layout = NULL;
118
+    AVFilterLink *inlink = ctx->inputs[0];
119
+    AVFilterLink *outlink = ctx->outputs[0];
120
+    static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
121
+    static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
122
+
123
+    formats = ff_make_format_list(sample_fmts);
124
+    if (!formats)
125
+        return AVERROR(ENOMEM);
126
+    ff_formats_ref(formats, &inlink->out_formats);
127
+
128
+    ff_add_channel_layout(&layout, AV_CH_LAYOUT_STEREO);
129
+    ff_channel_layouts_ref(layout, &inlink->out_channel_layouts);
130
+
131
+    formats = ff_all_samplerates();
132
+    if (!formats)
133
+        return AVERROR(ENOMEM);
134
+    ff_formats_ref(formats, &inlink->out_samplerates);
135
+
136
+    formats = ff_make_format_list(pix_fmts);
137
+    if (!formats)
138
+        return AVERROR(ENOMEM);
139
+    ff_formats_ref(formats, &outlink->in_formats);
140
+
141
+    return 0;
142
+}
143
+
144
+static int config_input(AVFilterLink *inlink)
145
+{
146
+    AVFilterContext *ctx = inlink->dst;
147
+    AudioVectorScopeContext *p = ctx->priv;
148
+    int nb_samples;
149
+
150
+    nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(p->frame_rate)) + 0.5);
151
+    inlink->partial_buf_size =
152
+    inlink->min_samples =
153
+    inlink->max_samples = nb_samples;
154
+
155
+    return 0;
156
+}
157
+
158
+static int config_output(AVFilterLink *outlink)
159
+{
160
+    AudioVectorScopeContext *p = outlink->src->priv;
161
+
162
+    outlink->w = p->w;
163
+    outlink->h = p->h;
164
+    outlink->sample_aspect_ratio = (AVRational){1,1};
165
+    outlink->frame_rate = p->frame_rate;
166
+
167
+    p->hw = p->w / 2;
168
+    p->hh = p->h / 2;
169
+
170
+    return 0;
171
+}
172
+
173
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
174
+{
175
+    AVFilterContext *ctx = inlink->dst;
176
+    AVFilterLink *outlink = ctx->outputs[0];
177
+    AudioVectorScopeContext *p = ctx->priv;
178
+    const int hw = p->hw;
179
+    const int hh = p->hh;
180
+    unsigned x, y;
181
+    const double zoom = p->zoom;
182
+    int i;
183
+
184
+    if (!p->outpicref || p->outpicref->width  != outlink->w ||
185
+                         p->outpicref->height != outlink->h) {
186
+        av_frame_free(&p->outpicref);
187
+        p->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
188
+        if (!p->outpicref)
189
+            av_frame_free(&insamples);
190
+            return AVERROR(ENOMEM);
191
+
192
+        for (i = 0; i < outlink->h; i++)
193
+            memset(p->outpicref->data[0] + i * p->outpicref->linesize[0], 0, outlink->w * 4);
194
+    }
195
+    p->outpicref->pts = insamples->pts;
196
+
197
+    fade(p);
198
+
199
+    switch (insamples->format) {
200
+    case AV_SAMPLE_FMT_S16:
201
+        for (i = 0; i < insamples->nb_samples; i++) {
202
+            int16_t *src = (int16_t *)insamples->data[0] + i * 2;
203
+
204
+            if (p->mode == LISSAJOUS) {
205
+                x = ((src[1] - src[0]) * zoom / (float)(UINT16_MAX) + 1) * hw;
206
+                y = (1.0 - (src[0] + src[1]) * zoom / (float)UINT16_MAX) * hh;
207
+            } else {
208
+                x = (src[1] * zoom / (float)INT16_MAX + 1) * hw;
209
+                y = (src[0] * zoom / (float)INT16_MAX + 1) * hh;
210
+            }
211
+
212
+            draw_dot(p, x, y);
213
+        }
214
+        break;
215
+    case AV_SAMPLE_FMT_FLT:
216
+        for (i = 0; i < insamples->nb_samples; i++) {
217
+            float *src = (float *)insamples->data[0] + i * 2;
218
+
219
+            if (p->mode == LISSAJOUS) {
220
+                x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
221
+                y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
222
+            } else {
223
+                x = (src[1] * zoom + 1) * hw;
224
+                y = (src[0] * zoom + 1) * hh;
225
+            }
226
+
227
+            draw_dot(p, x, y);
228
+        }
229
+        break;
230
+    }
231
+
232
+    av_frame_free(&insamples);
233
+
234
+    return ff_filter_frame(outlink, av_frame_clone(p->outpicref));
235
+}
236
+
237
+static av_cold void uninit(AVFilterContext *ctx)
238
+{
239
+    AudioVectorScopeContext *p = ctx->priv;
240
+
241
+    av_frame_free(&p->outpicref);
242
+}
243
+
244
+static const AVFilterPad audiovectorscope_inputs[] = {
245
+    {
246
+        .name         = "default",
247
+        .type         = AVMEDIA_TYPE_AUDIO,
248
+        .config_props = config_input,
249
+        .filter_frame = filter_frame,
250
+    },
251
+    { NULL }
252
+};
253
+
254
+static const AVFilterPad audiovectorscope_outputs[] = {
255
+    {
256
+        .name          = "default",
257
+        .type          = AVMEDIA_TYPE_VIDEO,
258
+        .config_props  = config_output,
259
+    },
260
+    { NULL }
261
+};
262
+
263
+AVFilter avfilter_avf_avectorscope = {
264
+    .name          = "avectorscope",
265
+    .description   = NULL_IF_CONFIG_SMALL("Display audio vector scope."),
266
+    .uninit        = uninit,
267
+    .query_formats = query_formats,
268
+    .priv_size     = sizeof(AudioVectorScopeContext),
269
+    .inputs        = audiovectorscope_inputs,
270
+    .outputs       = audiovectorscope_outputs,
271
+    .priv_class    = &avectorscope_class,
272
+};
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  63
33
-#define LIBAVFILTER_VERSION_MICRO 101
32
+#define LIBAVFILTER_VERSION_MINOR  64
33
+#define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \