2 * Copyright (c) 2012 Stefano Sabatini
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 filter
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
47 AVFilterBufferRef *outpicref;
51 enum ShowWavesMode mode;
54 #define OFFSET(x) offsetof(ShowWavesContext, x)
55 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
57 static const AVOption showwaves_options[] = {
58 { "rate", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
59 { "r", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
60 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
61 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
62 { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
64 {"mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
65 {"point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
66 {"line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
70 AVFILTER_DEFINE_CLASS(showwaves);
72 static av_cold int init(AVFilterContext *ctx, const char *args)
74 ShowWavesContext *showwaves = ctx->priv;
77 showwaves->class = &showwaves_class;
78 av_opt_set_defaults(showwaves);
79 showwaves->buf_idx = 0;
81 if ((err = av_set_options_string(showwaves, args, "=", ":")) < 0)
87 static av_cold void uninit(AVFilterContext *ctx)
89 ShowWavesContext *showwaves = ctx->priv;
91 av_freep(&showwaves->rate_str);
92 avfilter_unref_bufferp(&showwaves->outpicref);
95 static int query_formats(AVFilterContext *ctx)
97 AVFilterFormats *formats = NULL;
98 AVFilterChannelLayouts *layouts = NULL;
99 AVFilterLink *inlink = ctx->inputs[0];
100 AVFilterLink *outlink = ctx->outputs[0];
101 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
102 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
104 /* set input audio formats */
105 formats = ff_make_format_list(sample_fmts);
107 return AVERROR(ENOMEM);
108 ff_formats_ref(formats, &inlink->out_formats);
110 layouts = ff_all_channel_layouts();
112 return AVERROR(ENOMEM);
113 ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
115 formats = ff_all_samplerates();
117 return AVERROR(ENOMEM);
118 ff_formats_ref(formats, &inlink->out_samplerates);
120 /* set output video format */
121 formats = ff_make_format_list(pix_fmts);
123 return AVERROR(ENOMEM);
124 ff_formats_ref(formats, &outlink->in_formats);
129 static int config_output(AVFilterLink *outlink)
131 AVFilterContext *ctx = outlink->src;
132 AVFilterLink *inlink = ctx->inputs[0];
133 ShowWavesContext *showwaves = ctx->priv;
136 if (showwaves->n && showwaves->rate_str) {
137 av_log(ctx, AV_LOG_ERROR, "Options 'n' and 'rate' cannot be set at the same time\n");
138 return AVERROR(EINVAL);
142 if (!showwaves->rate_str)
143 showwaves->rate = (AVRational){25,1}; /* set default value */
144 else if ((err = av_parse_video_rate(&showwaves->rate, showwaves->rate_str)) < 0) {
145 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", showwaves->rate_str);
148 showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
151 outlink->w = showwaves->w;
152 outlink->h = showwaves->h;
153 outlink->sample_aspect_ratio = (AVRational){1,1};
155 outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
156 (AVRational){showwaves->w,1});
158 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
159 showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
163 inline static int push_frame(AVFilterLink *outlink)
165 ShowWavesContext *showwaves = outlink->src->priv;
168 if ((ret = ff_filter_frame(outlink, showwaves->outpicref)) >= 0)
169 showwaves->req_fullfilled = 1;
170 showwaves->outpicref = NULL;
171 showwaves->buf_idx = 0;
175 static int request_frame(AVFilterLink *outlink)
177 ShowWavesContext *showwaves = outlink->src->priv;
178 AVFilterLink *inlink = outlink->src->inputs[0];
181 showwaves->req_fullfilled = 0;
183 ret = ff_request_frame(inlink);
184 } while (!showwaves->req_fullfilled && ret >= 0);
186 if (ret == AVERROR_EOF && showwaves->outpicref)
191 #define MAX_INT16 ((1<<15) -1)
193 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
195 AVFilterContext *ctx = inlink->dst;
196 AVFilterLink *outlink = ctx->outputs[0];
197 ShowWavesContext *showwaves = ctx->priv;
198 const int nb_samples = insamples->audio->nb_samples;
199 AVFilterBufferRef *outpicref = showwaves->outpicref;
200 int linesize = outpicref ? outpicref->linesize[0] : 0;
201 int16_t *p = (int16_t *)insamples->data[0];
202 int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
203 int i, j, k, h, ret = 0;
204 const int n = showwaves->n;
205 const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
207 /* draw data in the buffer */
208 for (i = 0; i < nb_samples; i++) {
209 if (!showwaves->outpicref) {
210 showwaves->outpicref = outpicref =
211 ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN,
212 outlink->w, outlink->h);
214 return AVERROR(ENOMEM);
215 outpicref->video->w = outlink->w;
216 outpicref->video->h = outlink->h;
217 outpicref->pts = insamples->pts +
218 av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
219 (AVRational){ 1, inlink->sample_rate },
221 linesize = outpicref->linesize[0];
222 memset(outpicref->data[0], 0, showwaves->h*linesize);
224 for (j = 0; j < nb_channels; j++) {
225 h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16);
226 switch (showwaves->mode) {
228 if (h >= 0 && h < outlink->h)
229 *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
234 int start = showwaves->h/2, end = av_clip(h, 0, outlink->h-1);
235 if (start > end) FFSWAP(int16_t, start, end);
236 for (k = start; k < end; k++)
237 *(outpicref->data[0] + showwaves->buf_idx + k * linesize) += x;
243 showwaves->sample_count_mod++;
244 if (showwaves->sample_count_mod == n) {
245 showwaves->sample_count_mod = 0;
246 showwaves->buf_idx++;
248 if (showwaves->buf_idx == showwaves->w)
249 if ((ret = push_frame(outlink)) < 0)
251 outpicref = showwaves->outpicref;
254 avfilter_unref_buffer(insamples);
258 static const AVFilterPad showwaves_inputs[] = {
261 .type = AVMEDIA_TYPE_AUDIO,
262 .filter_frame = filter_frame,
263 .min_perms = AV_PERM_READ,
268 static const AVFilterPad showwaves_outputs[] = {
271 .type = AVMEDIA_TYPE_VIDEO,
272 .config_props = config_output,
273 .request_frame = request_frame,
278 AVFilter avfilter_avf_showwaves = {
280 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
283 .query_formats = query_formats,
284 .priv_size = sizeof(ShowWavesContext),
285 .inputs = showwaves_inputs,
286 .outputs = showwaves_outputs,
287 .priv_class = &showwaves_class,