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"
50 enum ShowWavesMode mode;
53 #define OFFSET(x) offsetof(ShowWavesContext, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
56 static const AVOption showwaves_options[] = {
57 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
58 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
59 { "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
60 { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
61 { "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
62 { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
63 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
64 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
68 AVFILTER_DEFINE_CLASS(showwaves);
70 static av_cold void uninit(AVFilterContext *ctx)
72 ShowWavesContext *showwaves = ctx->priv;
74 av_frame_free(&showwaves->outpicref);
77 static int query_formats(AVFilterContext *ctx)
79 AVFilterFormats *formats = NULL;
80 AVFilterChannelLayouts *layouts = NULL;
81 AVFilterLink *inlink = ctx->inputs[0];
82 AVFilterLink *outlink = ctx->outputs[0];
83 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
84 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
86 /* set input audio formats */
87 formats = ff_make_format_list(sample_fmts);
89 return AVERROR(ENOMEM);
90 ff_formats_ref(formats, &inlink->out_formats);
92 layouts = ff_all_channel_layouts();
94 return AVERROR(ENOMEM);
95 ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
97 formats = ff_all_samplerates();
99 return AVERROR(ENOMEM);
100 ff_formats_ref(formats, &inlink->out_samplerates);
102 /* set output video format */
103 formats = ff_make_format_list(pix_fmts);
105 return AVERROR(ENOMEM);
106 ff_formats_ref(formats, &outlink->in_formats);
111 static int config_output(AVFilterLink *outlink)
113 AVFilterContext *ctx = outlink->src;
114 AVFilterLink *inlink = ctx->inputs[0];
115 ShowWavesContext *showwaves = ctx->priv;
118 showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
120 showwaves->buf_idx = 0;
121 outlink->w = showwaves->w;
122 outlink->h = showwaves->h;
123 outlink->sample_aspect_ratio = (AVRational){1,1};
125 outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
126 (AVRational){showwaves->w,1});
128 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
129 showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
133 inline static int push_frame(AVFilterLink *outlink)
135 ShowWavesContext *showwaves = outlink->src->priv;
138 if ((ret = ff_filter_frame(outlink, showwaves->outpicref)) >= 0)
139 showwaves->req_fullfilled = 1;
140 showwaves->outpicref = NULL;
141 showwaves->buf_idx = 0;
145 static int request_frame(AVFilterLink *outlink)
147 ShowWavesContext *showwaves = outlink->src->priv;
148 AVFilterLink *inlink = outlink->src->inputs[0];
151 showwaves->req_fullfilled = 0;
153 ret = ff_request_frame(inlink);
154 } while (!showwaves->req_fullfilled && ret >= 0);
156 if (ret == AVERROR_EOF && showwaves->outpicref)
161 #define MAX_INT16 ((1<<15) -1)
163 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
165 AVFilterContext *ctx = inlink->dst;
166 AVFilterLink *outlink = ctx->outputs[0];
167 ShowWavesContext *showwaves = ctx->priv;
168 const int nb_samples = insamples->nb_samples;
169 AVFrame *outpicref = showwaves->outpicref;
170 int linesize = outpicref ? outpicref->linesize[0] : 0;
171 int16_t *p = (int16_t *)insamples->data[0];
172 int nb_channels = av_get_channel_layout_nb_channels(insamples->channel_layout);
173 int i, j, k, h, ret = 0;
174 const int n = showwaves->n;
175 const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
177 /* draw data in the buffer */
178 for (i = 0; i < nb_samples; i++) {
179 if (!showwaves->outpicref) {
180 showwaves->outpicref = outpicref =
181 ff_get_video_buffer(outlink, outlink->w, outlink->h);
183 return AVERROR(ENOMEM);
184 outpicref->width = outlink->w;
185 outpicref->height = outlink->h;
186 outpicref->pts = insamples->pts +
187 av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
188 (AVRational){ 1, inlink->sample_rate },
190 linesize = outpicref->linesize[0];
191 memset(outpicref->data[0], 0, showwaves->h*linesize);
193 for (j = 0; j < nb_channels; j++) {
194 h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16);
195 switch (showwaves->mode) {
197 if (h >= 0 && h < outlink->h)
198 *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
203 int start = showwaves->h/2, end = av_clip(h, 0, outlink->h-1);
204 if (start > end) FFSWAP(int16_t, start, end);
205 for (k = start; k < end; k++)
206 *(outpicref->data[0] + showwaves->buf_idx + k * linesize) += x;
212 showwaves->sample_count_mod++;
213 if (showwaves->sample_count_mod == n) {
214 showwaves->sample_count_mod = 0;
215 showwaves->buf_idx++;
217 if (showwaves->buf_idx == showwaves->w)
218 if ((ret = push_frame(outlink)) < 0)
220 outpicref = showwaves->outpicref;
223 av_frame_free(&insamples);
227 static const AVFilterPad showwaves_inputs[] = {
230 .type = AVMEDIA_TYPE_AUDIO,
231 .filter_frame = filter_frame,
236 static const AVFilterPad showwaves_outputs[] = {
239 .type = AVMEDIA_TYPE_VIDEO,
240 .config_props = config_output,
241 .request_frame = request_frame,
246 AVFilter avfilter_avf_showwaves = {
248 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
250 .query_formats = query_formats,
251 .priv_size = sizeof(ShowWavesContext),
252 .inputs = showwaves_inputs,
253 .outputs = showwaves_outputs,
254 .priv_class = &showwaves_class,