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"
36 enum VectorScopeMode {
43 typedef struct AudioVectorScopeContext {
52 AVRational frame_rate;
53 } AudioVectorScopeContext;
55 #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
56 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
58 static const AVOption avectorscope_options[] = {
59 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
60 { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
61 { "lissajous", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS}, 0, 0, FLAGS, "mode" },
62 { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
63 { "polar", "", 0, AV_OPT_TYPE_CONST, {.i64=POLAR}, 0, 0, FLAGS, "mode" },
64 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
65 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
66 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
67 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
68 { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40}, 0, 255, FLAGS },
69 { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
70 { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80}, 0, 255, FLAGS },
71 { "ac", "set alpha contrast", OFFSET(contrast[3]), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
72 { "rf", "set red fade", OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
73 { "gf", "set green fade", OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
74 { "bf", "set blue fade", OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
75 { "af", "set alpha fade", OFFSET(fade[3]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
76 { "zoom", "set zoom factor", OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 10, FLAGS },
80 AVFILTER_DEFINE_CLASS(avectorscope);
82 static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
84 const int linesize = s->outpicref->linesize[0];
88 if (y >= s->h || x >= s->w)
91 y = FFMIN(y, s->h - 1);
92 x = FFMIN(x, s->w - 1);
95 dst = &s->outpicref->data[0][y * linesize + x * 4];
96 dst[0] = FFMIN(dst[0] + s->contrast[0], 255);
97 dst[1] = FFMIN(dst[1] + s->contrast[1], 255);
98 dst[2] = FFMIN(dst[2] + s->contrast[2], 255);
99 dst[3] = FFMIN(dst[3] + s->contrast[3], 255);
102 static void fade(AudioVectorScopeContext *s)
104 const int linesize = s->outpicref->linesize[0];
107 if (s->fade[0] || s->fade[1] || s->fade[2]) {
108 uint8_t *d = s->outpicref->data[0];
109 for (i = 0; i < s->h; i++) {
110 for (j = 0; j < s->w*4; j+=4) {
111 d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
112 d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
113 d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
114 d[j+3] = FFMAX(d[j+3] - s->fade[3], 0);
121 static int query_formats(AVFilterContext *ctx)
123 AVFilterFormats *formats = NULL;
124 AVFilterChannelLayouts *layout = NULL;
125 AVFilterLink *inlink = ctx->inputs[0];
126 AVFilterLink *outlink = ctx->outputs[0];
127 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
128 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
131 formats = ff_make_format_list(sample_fmts);
132 if ((ret = ff_formats_ref (formats, &inlink->out_formats )) < 0 ||
133 (ret = ff_add_channel_layout (&layout, AV_CH_LAYOUT_STEREO )) < 0 ||
134 (ret = ff_channel_layouts_ref (layout , &inlink->out_channel_layouts)) < 0)
137 formats = ff_all_samplerates();
138 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
141 formats = ff_make_format_list(pix_fmts);
142 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
148 static int config_input(AVFilterLink *inlink)
150 AVFilterContext *ctx = inlink->dst;
151 AudioVectorScopeContext *s = ctx->priv;
154 nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
155 inlink->partial_buf_size =
156 inlink->min_samples =
157 inlink->max_samples = nb_samples;
162 static int config_output(AVFilterLink *outlink)
164 AudioVectorScopeContext *s = outlink->src->priv;
168 outlink->sample_aspect_ratio = (AVRational){1,1};
169 outlink->frame_rate = s->frame_rate;
177 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
179 AVFilterContext *ctx = inlink->dst;
180 AVFilterLink *outlink = ctx->outputs[0];
181 AudioVectorScopeContext *s = ctx->priv;
182 const int hw = s->hw;
183 const int hh = s->hh;
185 const double zoom = s->zoom;
188 if (!s->outpicref || s->outpicref->width != outlink->w ||
189 s->outpicref->height != outlink->h) {
190 av_frame_free(&s->outpicref);
191 s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
193 av_frame_free(&insamples);
194 return AVERROR(ENOMEM);
197 for (i = 0; i < outlink->h; i++)
198 memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
200 s->outpicref->pts = insamples->pts;
204 switch (insamples->format) {
205 case AV_SAMPLE_FMT_S16:
206 for (i = 0; i < insamples->nb_samples; i++) {
207 int16_t *src = (int16_t *)insamples->data[0] + i * 2;
209 if (s->mode == LISSAJOUS) {
210 x = ((src[1] - src[0]) * zoom / (float)(UINT16_MAX) + 1) * hw;
211 y = (1.0 - (src[0] + src[1]) * zoom / (float)UINT16_MAX) * hh;
212 } else if (s->mode == LISSAJOUS_XY) {
213 x = (src[1] * zoom / (float)INT16_MAX + 1) * hw;
214 y = (src[0] * zoom / (float)INT16_MAX + 1) * hh;
216 float sx, sy, cx, cy;
218 sx = src[1] * zoom / (float)INT16_MAX;
219 sy = src[0] * zoom / (float)INT16_MAX;
220 cx = sx * sqrtf(1 - 0.5*sy*sy);
221 cy = sy * sqrtf(1 - 0.5*sx*sx);
222 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
223 y = s->h - s->h * fabsf(cx + cy) * .7;
229 case AV_SAMPLE_FMT_FLT:
230 for (i = 0; i < insamples->nb_samples; i++) {
231 float *src = (float *)insamples->data[0] + i * 2;
233 if (s->mode == LISSAJOUS) {
234 x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
235 y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
236 } else if (s->mode == LISSAJOUS_XY){
237 x = (src[1] * zoom + 1) * hw;
238 y = (src[0] * zoom + 1) * hh;
240 float sx, sy, cx, cy;
244 cx = sx * sqrtf(1 - 0.5 * sy * sy);
245 cy = sy * sqrtf(1 - 0.5 * sx * sx);
246 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
247 y = s->h - s->h * fabsf(cx + cy) * .7;
255 av_frame_free(&insamples);
257 return ff_filter_frame(outlink, av_frame_clone(s->outpicref));
260 static av_cold void uninit(AVFilterContext *ctx)
262 AudioVectorScopeContext *s = ctx->priv;
264 av_frame_free(&s->outpicref);
267 static const AVFilterPad audiovectorscope_inputs[] = {
270 .type = AVMEDIA_TYPE_AUDIO,
271 .config_props = config_input,
272 .filter_frame = filter_frame,
277 static const AVFilterPad audiovectorscope_outputs[] = {
280 .type = AVMEDIA_TYPE_VIDEO,
281 .config_props = config_output,
286 AVFilter ff_avf_avectorscope = {
287 .name = "avectorscope",
288 .description = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
290 .query_formats = query_formats,
291 .priv_size = sizeof(AudioVectorScopeContext),
292 .inputs = audiovectorscope_inputs,
293 .outputs = audiovectorscope_outputs,
294 .priv_class = &avectorscope_class,