FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avf_avectorscope.c
Go to the documentation of this file.
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"
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 
41 };
42 
44  DOT,
47 };
48 
50  LIN,
53  LOG,
55 };
56 
57 typedef struct AudioVectorScopeContext {
58  const AVClass *class;
60  int w, h;
61  int hw, hh;
62  int mode;
63  int draw;
64  int scale;
65  int contrast[4];
66  int fade[4];
67  double zoom;
68  unsigned prev_x, prev_y;
71 
72 #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
73 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
74 
75 static const AVOption avectorscope_options[] = {
76  { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
77  { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
78  { "lissajous", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS}, 0, 0, FLAGS, "mode" },
79  { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
80  { "polar", "", 0, AV_OPT_TYPE_CONST, {.i64=POLAR}, 0, 0, FLAGS, "mode" },
81  { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
82  { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
83  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
84  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
85  { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40}, 0, 255, FLAGS },
86  { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
87  { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80}, 0, 255, FLAGS },
88  { "ac", "set alpha contrast", OFFSET(contrast[3]), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
89  { "rf", "set red fade", OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
90  { "gf", "set green fade", OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
91  { "bf", "set blue fade", OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
92  { "af", "set alpha fade", OFFSET(fade[3]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
93  { "zoom", "set zoom factor", OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 10, FLAGS },
94  { "draw", "set draw mode", OFFSET(draw), AV_OPT_TYPE_INT, {.i64=DOT}, 0, DRAW_NB-1, FLAGS, "draw" },
95  { "dot", "", 0, AV_OPT_TYPE_CONST, {.i64=DOT} , 0, 0, FLAGS, "draw" },
96  { "line", "", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "draw" },
97  { "scale", "set amplitude scale mode", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LIN}, 0, SCALE_NB-1, FLAGS, "scale" },
98  { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LIN}, 0, 0, FLAGS, "scale" },
99  { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
100  { "cbrt", "cube root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
101  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
102  { NULL }
103 };
104 
105 AVFILTER_DEFINE_CLASS(avectorscope);
106 
107 static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
108 {
109  const int linesize = s->outpicref->linesize[0];
110  uint8_t *dst;
111 
112  if (s->zoom > 1) {
113  if (y >= s->h || x >= s->w)
114  return;
115  } else {
116  y = FFMIN(y, s->h - 1);
117  x = FFMIN(x, s->w - 1);
118  }
119 
120  dst = &s->outpicref->data[0][y * linesize + x * 4];
121  dst[0] = FFMIN(dst[0] + s->contrast[0], 255);
122  dst[1] = FFMIN(dst[1] + s->contrast[1], 255);
123  dst[2] = FFMIN(dst[2] + s->contrast[2], 255);
124  dst[3] = FFMIN(dst[3] + s->contrast[3], 255);
125 }
126 
127 static void draw_line(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
128 {
129  int dx = FFABS(x1-x0), sx = x0 < x1 ? 1 : -1;
130  int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
131  int err = (dx>dy ? dx : -dy) / 2, e2;
132 
133  for (;;) {
134  draw_dot(s, x0, y0);
135 
136  if (x0 == x1 && y0 == y1)
137  break;
138 
139  e2 = err;
140 
141  if (e2 >-dx) {
142  err -= dy;
143  x0 += sx;
144  }
145 
146  if (e2 < dy) {
147  err += dx;
148  y0 += sy;
149  }
150  }
151 }
152 
154 {
155  const int linesize = s->outpicref->linesize[0];
156  int i, j;
157 
158  if (s->fade[0] || s->fade[1] || s->fade[2]) {
159  uint8_t *d = s->outpicref->data[0];
160  for (i = 0; i < s->h; i++) {
161  for (j = 0; j < s->w*4; j+=4) {
162  d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
163  d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
164  d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
165  d[j+3] = FFMAX(d[j+3] - s->fade[3], 0);
166  }
167  d += linesize;
168  }
169  }
170 }
171 
173 {
176  AVFilterLink *inlink = ctx->inputs[0];
177  AVFilterLink *outlink = ctx->outputs[0];
179  static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
180  int ret;
181 
182  formats = ff_make_format_list(sample_fmts);
183  if ((ret = ff_formats_ref (formats, &inlink->out_formats )) < 0 ||
184  (ret = ff_add_channel_layout (&layout, AV_CH_LAYOUT_STEREO )) < 0 ||
185  (ret = ff_channel_layouts_ref (layout , &inlink->out_channel_layouts)) < 0)
186  return ret;
187 
188  formats = ff_all_samplerates();
189  if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
190  return ret;
191 
192  formats = ff_make_format_list(pix_fmts);
193  if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
194  return ret;
195 
196  return 0;
197 }
198 
199 static int config_input(AVFilterLink *inlink)
200 {
201  AVFilterContext *ctx = inlink->dst;
203  int nb_samples;
204 
205  nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
206  inlink->partial_buf_size =
207  inlink->min_samples =
208  inlink->max_samples = nb_samples;
209 
210  return 0;
211 }
212 
213 static int config_output(AVFilterLink *outlink)
214 {
215  AudioVectorScopeContext *s = outlink->src->priv;
216 
217  outlink->w = s->w;
218  outlink->h = s->h;
219  outlink->sample_aspect_ratio = (AVRational){1,1};
220  outlink->frame_rate = s->frame_rate;
221 
222  s->prev_x = s->hw = s->w / 2;
223  s->prev_y = s->hh = s->mode == POLAR ? s->h - 1 : s->h / 2;
224 
225  return 0;
226 }
227 
228 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
229 {
230  AVFilterContext *ctx = inlink->dst;
231  AVFilterLink *outlink = ctx->outputs[0];
233  const int hw = s->hw;
234  const int hh = s->hh;
235  unsigned x, y;
236  unsigned prev_x = s->prev_x, prev_y = s->prev_y;
237  double zoom = s->zoom;
238  int i;
239 
240  if (!s->outpicref || s->outpicref->width != outlink->w ||
241  s->outpicref->height != outlink->h) {
243  s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
244  if (!s->outpicref) {
245  av_frame_free(&insamples);
246  return AVERROR(ENOMEM);
247  }
248 
250  for (i = 0; i < outlink->h; i++)
251  memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
252  }
253  s->outpicref->pts = insamples->pts;
254 
255  fade(s);
256 
257  if (zoom < 1) {
258  float max = 0;
259 
260  switch (insamples->format) {
261  case AV_SAMPLE_FMT_S16: {
262  int16_t *samples = (int16_t *)insamples->data[0];
263 
264  for (i = 0; i < insamples->nb_samples * 2; i++) {
265  float sample = samples[i] / (float)INT16_MAX;
266  max = FFMAX(FFABS(sample), max);
267  }
268 
269  }
270  break;
271  case AV_SAMPLE_FMT_FLT: {
272  float *samples = (float *)insamples->data[0];
273 
274  for (i = 0; i < insamples->nb_samples * 2; i++) {
275  max = FFMAX(FFABS(samples[i]), max);
276  }
277  }
278  break;
279  default:
280  av_assert2(0);
281  }
282 
283  zoom = 1. / max;
284  }
285 
286  for (i = 0; i < insamples->nb_samples; i++) {
287  int16_t *samples = (int16_t *)insamples->data[0] + i * 2;
288  float *samplesf = (float *)insamples->data[0] + i * 2;
289  float src[2];
290 
291  switch (insamples->format) {
292  case AV_SAMPLE_FMT_S16:
293  src[0] = samples[0] / (float)INT16_MAX;
294  src[1] = samples[1] / (float)INT16_MAX;
295  break;
296  case AV_SAMPLE_FMT_FLT:
297  src[0] = samplesf[0];
298  src[1] = samplesf[1];
299  break;
300  default:
301  av_assert2(0);
302  }
303 
304  switch (s->scale) {
305  case SQRT:
306  src[0] = FFSIGN(src[0]) * sqrtf(FFABS(src[0]));
307  src[1] = FFSIGN(src[1]) * sqrtf(FFABS(src[1]));
308  break;
309  case CBRT:
310  src[0] = FFSIGN(src[0]) * cbrtf(FFABS(src[0]));
311  src[1] = FFSIGN(src[1]) * cbrtf(FFABS(src[1]));
312  break;
313  case LOG:
314  src[0] = FFSIGN(src[0]) * logf(1 + FFABS(src[0])) / logf(2);
315  src[1] = FFSIGN(src[1]) * logf(1 + FFABS(src[1])) / logf(2);
316  break;
317  }
318 
319  if (s->mode == LISSAJOUS) {
320  x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
321  y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
322  } else if (s->mode == LISSAJOUS_XY) {
323  x = (src[1] * zoom + 1) * hw;
324  y = (src[0] * zoom + 1) * hh;
325  } else {
326  float sx, sy, cx, cy;
327 
328  sx = src[1] * zoom;
329  sy = src[0] * zoom;
330  cx = sx * sqrtf(1 - 0.5 * sy * sy);
331  cy = sy * sqrtf(1 - 0.5 * sx * sx);
332  x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
333  y = s->h - s->h * fabsf(cx + cy) * .7;
334  }
335 
336  if (s->draw == DOT) {
337  draw_dot(s, x, y);
338  } else {
339  draw_line(s, x, y, prev_x, prev_y);
340  }
341  prev_x = x;
342  prev_y = y;
343  }
344 
345  s->prev_x = x, s->prev_y = y;
346  av_frame_free(&insamples);
347 
348  return ff_filter_frame(outlink, av_frame_clone(s->outpicref));
349 }
350 
352 {
354 
356 }
357 
359  {
360  .name = "default",
361  .type = AVMEDIA_TYPE_AUDIO,
362  .config_props = config_input,
363  .filter_frame = filter_frame,
364  },
365  { NULL }
366 };
367 
369  {
370  .name = "default",
371  .type = AVMEDIA_TYPE_VIDEO,
372  .config_props = config_output,
373  },
374  { NULL }
375 };
376 
378  .name = "avectorscope",
379  .description = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
380  .uninit = uninit,
381  .query_formats = query_formats,
382  .priv_size = sizeof(AudioVectorScopeContext),
383  .inputs = audiovectorscope_inputs,
384  .outputs = audiovectorscope_outputs,
385  .priv_class = &avectorscope_class,
386 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
AVOption.
Definition: opt.h:246
Main libavfilter public API header.
static void draw_line(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
static av_cold void uninit(AVFilterContext *ctx)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:92
#define AV_CH_LAYOUT_STEREO
#define src
Definition: vp8dsp.c:254
#define sample
AVFilter ff_avf_avectorscope
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:435
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1151
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:294
static const AVFilterPad audiovectorscope_outputs[]
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
A filter pad used for either input or output.
Definition: internal.h:54
#define FLAGS
int width
Definition: frame.h:259
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:343
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
static int config_input(AVFilterLink *inlink)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
void * priv
private data for use by the filter
Definition: avfilter.h:353
simple assert() macros that are a bit more flexible than ISO C assert().
static const AVOption avectorscope_options[]
VectorScopeScale
#define FFMAX(a, b)
Definition: common.h:94
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
VectorScopeMode
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:96
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
#define FFSIGN(a)
Definition: common.h:73
AVFormatContext * ctx
Definition: movenc.c:48
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static const AVFilterPad audiovectorscope_inputs[]
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:389
static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:492
AVFILTER_DEFINE_CLASS(avectorscope)
static int config_output(AVFilterLink *outlink)
A list of supported channel layouts.
Definition: formats.h:85
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:274
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:379
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:289
static av_always_inline float cbrtf(float x)
Definition: libm.h:61
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
Rational number (pair of numerator and denominator).
Definition: rational.h:58
offset must point to AVRational
Definition: opt.h:236
const char * name
Filter name.
Definition: avfilter.h:148
offset must point to two consecutive integers
Definition: opt.h:233
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
static int query_formats(AVFilterContext *ctx)
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
signed 16 bits
Definition: samplefmt.h:61
A list of supported formats for one end of a filter link.
Definition: formats.h:64
uint64_t layout
VectorScopeDraw
An instance of a filter.
Definition: avfilter.h:338
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
int height
Definition: frame.h:259
formats
Definition: signature.h:48
internal API functions
static void fade(AudioVectorScopeContext *s)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:267
for(j=16;j >0;--j)
#define OFFSET(x)