FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avf_showwaves.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Stefano Sabatini
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 filter
24  */
25 
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 
41 };
42 
43 typedef struct {
44  const AVClass *class;
45  int w, h;
47  int buf_idx;
48  int16_t *buf_idy; /* y coordinate of previous sample for each channel */
51  int n;
55  void (*draw_sample)(uint8_t *buf, int height, int linesize,
56  int16_t sample, int16_t *prev_y, int intensity);
58 
59 #define OFFSET(x) offsetof(ShowWavesContext, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61 
62 static const AVOption showwaves_options[] = {
63  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
64  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
65  { "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
66  { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
67  { "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
68  { "p2p", "draw a line between samples", 0, AV_OPT_TYPE_CONST, {.i64=MODE_P2P}, .flags=FLAGS, .unit="mode"},
69  { "cline", "draw a centered line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CENTERED_LINE}, .flags=FLAGS, .unit="mode"},
70  { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
71  { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
72  { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
73  { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
74  { NULL }
75 };
76 
77 AVFILTER_DEFINE_CLASS(showwaves);
78 
79 static av_cold void uninit(AVFilterContext *ctx)
80 {
81  ShowWavesContext *showwaves = ctx->priv;
82 
83  av_frame_free(&showwaves->outpicref);
84  av_freep(&showwaves->buf_idy);
85 }
86 
88 {
89  AVFilterFormats *formats = NULL;
91  AVFilterLink *inlink = ctx->inputs[0];
92  AVFilterLink *outlink = ctx->outputs[0];
94  static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
95 
96  /* set input audio formats */
97  formats = ff_make_format_list(sample_fmts);
98  if (!formats)
99  return AVERROR(ENOMEM);
100  ff_formats_ref(formats, &inlink->out_formats);
101 
102  layouts = ff_all_channel_layouts();
103  if (!layouts)
104  return AVERROR(ENOMEM);
105  ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
106 
107  formats = ff_all_samplerates();
108  if (!formats)
109  return AVERROR(ENOMEM);
110  ff_formats_ref(formats, &inlink->out_samplerates);
111 
112  /* set output video format */
113  formats = ff_make_format_list(pix_fmts);
114  if (!formats)
115  return AVERROR(ENOMEM);
116  ff_formats_ref(formats, &outlink->in_formats);
117 
118  return 0;
119 }
120 
121 static int config_output(AVFilterLink *outlink)
122 {
123  AVFilterContext *ctx = outlink->src;
124  AVFilterLink *inlink = ctx->inputs[0];
125  ShowWavesContext *showwaves = ctx->priv;
126  int nb_channels = inlink->channels;
127 
128  if (!showwaves->n)
129  showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
130 
131  showwaves->buf_idx = 0;
132  if (!(showwaves->buf_idy = av_mallocz_array(nb_channels, sizeof(*showwaves->buf_idy)))) {
133  av_log(ctx, AV_LOG_ERROR, "Could not allocate showwaves buffer\n");
134  return AVERROR(ENOMEM);
135  }
136  outlink->w = showwaves->w;
137  outlink->h = showwaves->h;
138  outlink->sample_aspect_ratio = (AVRational){1,1};
139 
140  outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
141  (AVRational){showwaves->w,1});
142 
143  av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
144  showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
145  return 0;
146 }
147 
148 inline static int push_frame(AVFilterLink *outlink)
149 {
150  AVFilterContext *ctx = outlink->src;
151  AVFilterLink *inlink = ctx->inputs[0];
152  ShowWavesContext *showwaves = outlink->src->priv;
153  int nb_channels = inlink->channels;
154  int ret, i;
155 
156  if ((ret = ff_filter_frame(outlink, showwaves->outpicref)) >= 0)
157  showwaves->req_fullfilled = 1;
158  showwaves->outpicref = NULL;
159  showwaves->buf_idx = 0;
160  for (i = 0; i < nb_channels; i++)
161  showwaves->buf_idy[i] = 0;
162  return ret;
163 }
164 
165 static int request_frame(AVFilterLink *outlink)
166 {
167  ShowWavesContext *showwaves = outlink->src->priv;
168  AVFilterLink *inlink = outlink->src->inputs[0];
169  int ret;
170 
171  showwaves->req_fullfilled = 0;
172  do {
173  ret = ff_request_frame(inlink);
174  } while (!showwaves->req_fullfilled && ret >= 0);
175 
176  if (ret == AVERROR_EOF && showwaves->outpicref)
177  push_frame(outlink);
178  return ret;
179 }
180 
181 static void draw_sample_point(uint8_t *buf, int height, int linesize,
182  int16_t sample, int16_t *prev_y, int intensity)
183 {
184  const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
185  if (h >= 0 && h < height)
186  buf[h * linesize] += intensity;
187 }
188 
189 static void draw_sample_line(uint8_t *buf, int height, int linesize,
190  int16_t sample, int16_t *prev_y, int intensity)
191 {
192  int k;
193  const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
194  int start = height/2;
195  int end = av_clip(h, 0, height-1);
196  if (start > end)
197  FFSWAP(int16_t, start, end);
198  for (k = start; k < end; k++)
199  buf[k * linesize] += intensity;
200 }
201 
202 static void draw_sample_p2p(uint8_t *buf, int height, int linesize,
203  int16_t sample, int16_t *prev_y, int intensity)
204 {
205  int k;
206  const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
207  if (h >= 0 && h < height) {
208  buf[h * linesize] += intensity;
209  if (*prev_y && h != *prev_y) {
210  int start = *prev_y;
211  int end = av_clip(h, 0, height-1);
212  if (start > end)
213  FFSWAP(int16_t, start, end);
214  for (k = start + 1; k < end; k++)
215  buf[k * linesize] += intensity;
216  }
217  }
218  *prev_y = h;
219 }
220 
221 static void draw_sample_cline(uint8_t *buf, int height, int linesize,
222  int16_t sample, int16_t *prev_y, int intensity)
223 {
224  int k;
225  const int h = av_rescale(abs(sample), height, INT16_MAX);
226  const int start = (height - h) / 2;
227  const int end = start + h;
228  for (k = start; k < end; k++)
229  buf[k * linesize] += intensity;
230 }
231 
232 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
233 {
234  AVFilterContext *ctx = inlink->dst;
235  AVFilterLink *outlink = ctx->outputs[0];
236  ShowWavesContext *showwaves = ctx->priv;
237  const int nb_samples = insamples->nb_samples;
238  AVFrame *outpicref = showwaves->outpicref;
239  int linesize = outpicref ? outpicref->linesize[0] : 0;
240  int16_t *p = (int16_t *)insamples->data[0];
241  int nb_channels = inlink->channels;
242  int i, j, ret = 0;
243  const int n = showwaves->n;
244  const int x = 255 / ((showwaves->split_channels ? 1 : nb_channels) * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
245  const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
246 
247  /* draw data in the buffer */
248  for (i = 0; i < nb_samples; i++) {
249  if (!showwaves->outpicref) {
250  showwaves->outpicref = outpicref =
251  ff_get_video_buffer(outlink, outlink->w, outlink->h);
252  if (!outpicref)
253  return AVERROR(ENOMEM);
254  outpicref->width = outlink->w;
255  outpicref->height = outlink->h;
256  outpicref->pts = insamples->pts +
257  av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
258  (AVRational){ 1, inlink->sample_rate },
259  outlink->time_base);
260  linesize = outpicref->linesize[0];
261  for (j = 0; j < outlink->h; j++)
262  memset(outpicref->data[0] + j * linesize, 0, outlink->w);
263  }
264  for (j = 0; j < nb_channels; j++) {
265  uint8_t *buf = outpicref->data[0] + showwaves->buf_idx;
266  if (showwaves->split_channels)
267  buf += j*ch_height*linesize;
268  showwaves->draw_sample(buf, ch_height, linesize, *p++,
269  &showwaves->buf_idy[j], x);
270  }
271 
272  showwaves->sample_count_mod++;
273  if (showwaves->sample_count_mod == n) {
274  showwaves->sample_count_mod = 0;
275  showwaves->buf_idx++;
276  }
277  if (showwaves->buf_idx == showwaves->w)
278  if ((ret = push_frame(outlink)) < 0)
279  break;
280  outpicref = showwaves->outpicref;
281  }
282 
283  av_frame_free(&insamples);
284  return ret;
285 }
286 
287 static av_cold int init(AVFilterContext *ctx)
288 {
289  ShowWavesContext *showwaves = ctx->priv;
290 
291  switch (showwaves->mode) {
292  case MODE_POINT: showwaves->draw_sample = draw_sample_point; break;
293  case MODE_LINE: showwaves->draw_sample = draw_sample_line; break;
294  case MODE_P2P: showwaves->draw_sample = draw_sample_p2p; break;
295  case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline; break;
296  default:
297  return AVERROR_BUG;
298  }
299  return 0;
300 }
301 
302 static const AVFilterPad showwaves_inputs[] = {
303  {
304  .name = "default",
305  .type = AVMEDIA_TYPE_AUDIO,
306  .filter_frame = filter_frame,
307  },
308  { NULL }
309 };
310 
311 static const AVFilterPad showwaves_outputs[] = {
312  {
313  .name = "default",
314  .type = AVMEDIA_TYPE_VIDEO,
315  .config_props = config_output,
316  .request_frame = request_frame,
317  },
318  { NULL }
319 };
320 
322  .name = "showwaves",
323  .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
324  .init = init,
325  .uninit = uninit,
326  .query_formats = query_formats,
327  .priv_size = sizeof(ShowWavesContext),
328  .inputs = showwaves_inputs,
329  .outputs = showwaves_outputs,
330  .priv_class = &showwaves_class,
331 };