FFmpeg
 All Data Structures 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 
35 typedef struct {
36  const AVClass *class;
37  int w, h;
38  char *rate_str;
40  int buf_idx;
43  int n;
46 
47 #define OFFSET(x) offsetof(ShowWavesContext, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49 
50 static const AVOption showwaves_options[] = {
51  { "rate", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
52  { "r", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
53  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
54  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
55  { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
56  { NULL },
57 };
58 
59 AVFILTER_DEFINE_CLASS(showwaves);
60 
61 static av_cold int init(AVFilterContext *ctx, const char *args)
62 {
63  ShowWavesContext *showwaves = ctx->priv;
64  int err;
65 
66  showwaves->class = &showwaves_class;
67  av_opt_set_defaults(showwaves);
68  showwaves->buf_idx = 0;
69 
70  if ((err = av_set_options_string(showwaves, args, "=", ":")) < 0)
71  return err;
72 
73  return 0;
74 }
75 
76 static av_cold void uninit(AVFilterContext *ctx)
77 {
78  ShowWavesContext *showwaves = ctx->priv;
79 
80  av_freep(&showwaves->rate_str);
81  avfilter_unref_bufferp(&showwaves->outpicref);
82 }
83 
85 {
88  AVFilterLink *inlink = ctx->inputs[0];
89  AVFilterLink *outlink = ctx->outputs[0];
91  static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
92 
93  /* set input audio formats */
94  formats = ff_make_format_list(sample_fmts);
95  if (!formats)
96  return AVERROR(ENOMEM);
97  ff_formats_ref(formats, &inlink->out_formats);
98 
99  layouts = ff_all_channel_layouts();
100  if (!layouts)
101  return AVERROR(ENOMEM);
102  ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
103 
104  formats = ff_all_samplerates();
105  if (!formats)
106  return AVERROR(ENOMEM);
107  ff_formats_ref(formats, &inlink->out_samplerates);
108 
109  /* set output video format */
110  formats = ff_make_format_list(pix_fmts);
111  if (!formats)
112  return AVERROR(ENOMEM);
113  ff_formats_ref(formats, &outlink->in_formats);
114 
115  return 0;
116 }
117 
118 static int config_output(AVFilterLink *outlink)
119 {
120  AVFilterContext *ctx = outlink->src;
121  AVFilterLink *inlink = ctx->inputs[0];
122  ShowWavesContext *showwaves = ctx->priv;
123  int err;
124 
125  if (showwaves->n && showwaves->rate_str) {
126  av_log(ctx, AV_LOG_ERROR, "Options 'n' and 'rate' cannot be set at the same time\n");
127  return AVERROR(EINVAL);
128  }
129 
130  if (!showwaves->n) {
131  if (!showwaves->rate_str)
132  showwaves->rate = (AVRational){25,1}; /* set default value */
133  else if ((err = av_parse_video_rate(&showwaves->rate, showwaves->rate_str)) < 0) {
134  av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", showwaves->rate_str);
135  return err;
136  }
137  showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
138  }
139 
140  outlink->w = showwaves->w;
141  outlink->h = showwaves->h;
142  outlink->sample_aspect_ratio = (AVRational){1,1};
143 
144  outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
145  (AVRational){showwaves->w,1});
146 
147  av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
148  showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
149  return 0;
150 }
151 
152 inline static void push_frame(AVFilterLink *outlink)
153 {
154  ShowWavesContext *showwaves = outlink->src->priv;
155 
156  ff_filter_frame(outlink, showwaves->outpicref);
157  showwaves->req_fullfilled = 1;
158  showwaves->outpicref = NULL;
159  showwaves->buf_idx = 0;
160 }
161 
162 static int request_frame(AVFilterLink *outlink)
163 {
164  ShowWavesContext *showwaves = outlink->src->priv;
165  AVFilterLink *inlink = outlink->src->inputs[0];
166  int ret;
167 
168  showwaves->req_fullfilled = 0;
169  do {
170  ret = ff_request_frame(inlink);
171  } while (!showwaves->req_fullfilled && ret >= 0);
172 
173  if (ret == AVERROR_EOF && showwaves->outpicref)
174  push_frame(outlink);
175  return ret;
176 }
177 
178 #define MAX_INT16 ((1<<15) -1)
179 
180 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
181 {
182  AVFilterContext *ctx = inlink->dst;
183  AVFilterLink *outlink = ctx->outputs[0];
184  ShowWavesContext *showwaves = ctx->priv;
185  const int nb_samples = insamples->audio->nb_samples;
186  AVFilterBufferRef *outpicref = showwaves->outpicref;
187  int linesize = outpicref ? outpicref->linesize[0] : 0;
188  int16_t *p = (int16_t *)insamples->data[0];
190  int i, j, h;
191  const int n = showwaves->n;
192  const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
193 
194  /* draw data in the buffer */
195  for (i = 0; i < nb_samples; i++) {
196  if (!outpicref) {
197  showwaves->outpicref = outpicref =
199  outlink->w, outlink->h);
200  if (!outpicref)
201  return AVERROR(ENOMEM);
202  outpicref->video->w = outlink->w;
203  outpicref->video->h = outlink->h;
204  outpicref->pts = insamples->pts +
205  av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
206  (AVRational){ 1, inlink->sample_rate },
207  outlink->time_base);
208  linesize = outpicref->linesize[0];
209  memset(outpicref->data[0], 0, showwaves->h*linesize);
210  }
211  for (j = 0; j < nb_channels; j++) {
212  h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16);
213  if (h >= 0 && h < outlink->h)
214  *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
215  }
216  showwaves->sample_count_mod++;
217  if (showwaves->sample_count_mod == n) {
218  showwaves->sample_count_mod = 0;
219  showwaves->buf_idx++;
220  }
221  if (showwaves->buf_idx == showwaves->w)
222  push_frame(outlink);
223  }
224 
225  avfilter_unref_buffer(insamples);
226  return 0;
227 }
228 
229 static const AVFilterPad showwaves_inputs[] = {
230  {
231  .name = "default",
232  .type = AVMEDIA_TYPE_AUDIO,
233  .filter_frame = filter_frame,
234  .min_perms = AV_PERM_READ,
235  },
236  { NULL }
237 };
238 
239 static const AVFilterPad showwaves_outputs[] = {
240  {
241  .name = "default",
242  .type = AVMEDIA_TYPE_VIDEO,
243  .config_props = config_output,
244  .request_frame = request_frame,
245  },
246  { NULL }
247 };
248 
250  .name = "showwaves",
251  .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
252  .init = init,
253  .uninit = uninit,
254  .query_formats = query_formats,
255  .priv_size = sizeof(ShowWavesContext),
256  .inputs = showwaves_inputs,
257  .outputs = showwaves_outputs,
258  .priv_class = &showwaves_class,
259 };