FFmpeg  2.1.1
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"
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 
40 };
41 
42 typedef struct AudioVectorScopeContext {
43  const AVClass *class;
45  int w, h;
46  int hw, hh;
48  int contrast[3];
49  int fade[3];
50  double zoom;
53 
54 #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
55 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
56 
57 static const AVOption avectorscope_options[] = {
58  { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
59  { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
60  { "lissajous", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS}, 0, 0, FLAGS, "mode" },
61  { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
62  { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
63  { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
64  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
65  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
66  { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40}, 0, 255, FLAGS },
67  { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
68  { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80}, 0, 255, FLAGS },
69  { "rf", "set red fade", OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
70  { "gf", "set green fade", OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
71  { "bf", "set blue fade", OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
72  { "zoom", "set zoom factor", OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 10, FLAGS },
73  { NULL }
74 };
75 
76 AVFILTER_DEFINE_CLASS(avectorscope);
77 
78 static void draw_dot(AudioVectorScopeContext *p, unsigned x, unsigned y)
79 {
80  const int linesize = p->outpicref->linesize[0];
81  uint8_t *dst;
82 
83  if (p->zoom > 1) {
84  if (y >= p->h || x >= p->w)
85  return;
86  } else {
87  y = FFMIN(y, p->h - 1);
88  x = FFMIN(x, p->w - 1);
89  }
90 
91  dst = &p->outpicref->data[0][y * linesize + x * 4];
92  dst[0] = FFMIN(dst[0] + p->contrast[0], 255);
93  dst[1] = FFMIN(dst[1] + p->contrast[1], 255);
94  dst[2] = FFMIN(dst[2] + p->contrast[2], 255);
95 }
96 
98 {
99  const int linesize = p->outpicref->linesize[0];
100  int i, j;
101 
102  if (p->fade[0] || p->fade[1] || p->fade[2]) {
103  uint8_t *d = p->outpicref->data[0];
104  for (i = 0; i < p->h; i++) {
105  for (j = 0; j < p->w*4; j+=4) {
106  d[j+0] = FFMAX(d[j+0] - p->fade[0], 0);
107  d[j+1] = FFMAX(d[j+1] - p->fade[1], 0);
108  d[j+2] = FFMAX(d[j+2] - p->fade[2], 0);
109  }
110  d += linesize;
111  }
112  }
113 }
114 
116 {
117  AVFilterFormats *formats = NULL;
119  AVFilterLink *inlink = ctx->inputs[0];
120  AVFilterLink *outlink = ctx->outputs[0];
122  static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
123 
124  formats = ff_make_format_list(sample_fmts);
125  if (!formats)
126  return AVERROR(ENOMEM);
127  ff_formats_ref(formats, &inlink->out_formats);
128 
131 
132  formats = ff_all_samplerates();
133  if (!formats)
134  return AVERROR(ENOMEM);
135  ff_formats_ref(formats, &inlink->out_samplerates);
136 
137  formats = ff_make_format_list(pix_fmts);
138  if (!formats)
139  return AVERROR(ENOMEM);
140  ff_formats_ref(formats, &outlink->in_formats);
141 
142  return 0;
143 }
144 
145 static int config_input(AVFilterLink *inlink)
146 {
147  AVFilterContext *ctx = inlink->dst;
148  AudioVectorScopeContext *p = ctx->priv;
149  int nb_samples;
150 
151  nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(p->frame_rate)) + 0.5);
152  inlink->partial_buf_size =
153  inlink->min_samples =
154  inlink->max_samples = nb_samples;
155 
156  return 0;
157 }
158 
159 static int config_output(AVFilterLink *outlink)
160 {
161  AudioVectorScopeContext *p = outlink->src->priv;
162 
163  outlink->w = p->w;
164  outlink->h = p->h;
165  outlink->sample_aspect_ratio = (AVRational){1,1};
166  outlink->frame_rate = p->frame_rate;
167 
168  p->hw = p->w / 2;
169  p->hh = p->h / 2;
170 
171  return 0;
172 }
173 
174 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
175 {
176  AVFilterContext *ctx = inlink->dst;
177  AVFilterLink *outlink = ctx->outputs[0];
178  AudioVectorScopeContext *p = ctx->priv;
179  const int hw = p->hw;
180  const int hh = p->hh;
181  unsigned x, y;
182  const double zoom = p->zoom;
183  int i;
184 
185  if (!p->outpicref || p->outpicref->width != outlink->w ||
186  p->outpicref->height != outlink->h) {
188  p->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
189  if (!p->outpicref)
190  av_frame_free(&insamples);
191  return AVERROR(ENOMEM);
192 
193  for (i = 0; i < outlink->h; i++)
194  memset(p->outpicref->data[0] + i * p->outpicref->linesize[0], 0, outlink->w * 4);
195  }
196  p->outpicref->pts = insamples->pts;
197 
198  fade(p);
199 
200  switch (insamples->format) {
201  case AV_SAMPLE_FMT_S16:
202  for (i = 0; i < insamples->nb_samples; i++) {
203  int16_t *src = (int16_t *)insamples->data[0] + i * 2;
204 
205  if (p->mode == LISSAJOUS) {
206  x = ((src[1] - src[0]) * zoom / (float)(UINT16_MAX) + 1) * hw;
207  y = (1.0 - (src[0] + src[1]) * zoom / (float)UINT16_MAX) * hh;
208  } else {
209  x = (src[1] * zoom / (float)INT16_MAX + 1) * hw;
210  y = (src[0] * zoom / (float)INT16_MAX + 1) * hh;
211  }
212 
213  draw_dot(p, x, y);
214  }
215  break;
216  case AV_SAMPLE_FMT_FLT:
217  for (i = 0; i < insamples->nb_samples; i++) {
218  float *src = (float *)insamples->data[0] + i * 2;
219 
220  if (p->mode == LISSAJOUS) {
221  x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
222  y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
223  } else {
224  x = (src[1] * zoom + 1) * hw;
225  y = (src[0] * zoom + 1) * hh;
226  }
227 
228  draw_dot(p, x, y);
229  }
230  break;
231  }
232 
233  av_frame_free(&insamples);
234 
235  return ff_filter_frame(outlink, av_frame_clone(p->outpicref));
236 }
237 
238 static av_cold void uninit(AVFilterContext *ctx)
239 {
240  AudioVectorScopeContext *p = ctx->priv;
241 
243 }
244 
246  {
247  .name = "default",
248  .type = AVMEDIA_TYPE_AUDIO,
249  .config_props = config_input,
250  .filter_frame = filter_frame,
251  },
252  { NULL }
253 };
254 
256  {
257  .name = "default",
258  .type = AVMEDIA_TYPE_VIDEO,
259  .config_props = config_output,
260  },
261  { NULL }
262 };
263 
265  .name = "avectorscope",
266  .description = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
267  .uninit = uninit,
268  .query_formats = query_formats,
269  .priv_size = sizeof(AudioVectorScopeContext),
270  .inputs = audiovectorscope_inputs,
271  .outputs = audiovectorscope_outputs,
272  .priv_class = &avectorscope_class,
273 };
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
AVOption.
Definition: opt.h:253
const char * name
Filter name.
Definition: avfilter.h:468
void * priv
private data for use by the filter
Definition: avfilter.h:648
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:111
static av_cold void uninit(AVFilterContext *ctx)
static enum AVSampleFormat formats[]
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:109
#define AV_CH_LAYOUT_STEREO
Pixel format.
Definition: avcodec.h:4533
#define av_cold
Definition: avcodec.h:653
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
const char * name
Pad name.
Definition: internal.h:66
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1118
uint8_t
mode
Definition: f_perms.c:27
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:182
void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:413
static const AVFilterPad audiovectorscope_outputs[]
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
signed 16 bits
Definition: samplefmt.h:52
A filter pad used for either input or output.
Definition: internal.h:60
#define FLAGS
int width
width and height of the video frame
Definition: frame.h:145
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:336
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:151
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
static const AVOption avectorscope_options[]
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
enum VectorScopeMode mode
VectorScopeMode
float y
#define FFMIN(a, b)
Definition: avcodec.h:925
static const AVFilterPad audiovectorscope_inputs[]
AVFilter avfilter_avf_avectorscope
static int config_output(AVFilterLink *outlink)
A list of supported channel layouts.
Definition: formats.h:85
Main libavfilter public API header.
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:642
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:157
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
AVS_Value src
Definition: avisynth_c.h:523
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:418
#define FFMAX(a, b)
Definition: avcodec.h:923
static void draw_dot(AudioVectorScopeContext *p, unsigned x, unsigned y)
static void fade(AudioVectorScopeContext *p)
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:464
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:102
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:635
rational number numerator/denominator
Definition: rational.h:43
offset must point to AVRational
Definition: opt.h:233
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
offset must point to two consecutive integers
Definition: opt.h:230
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:382
AVFrame * av_frame_clone(AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:338
static int query_formats(AVFilterContext *ctx)
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:301
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: avcodec.h:4563
A list of supported formats for one end of a filter link.
Definition: formats.h:64
#define AVERROR(e)
uint64_t layout
An instance of a filter.
Definition: avfilter.h:627
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
int height
Definition: frame.h:145
internal API functions
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:150
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
#define OFFSET(x)