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"
48 int16_t *buf_idy; /* y coordinate of previous sample for each channel */
53 int mode; ///< ShowWavesMode
55 void (*draw_sample)(uint8_t *buf, int height, int linesize,
56 int16_t sample, int16_t *prev_y, int intensity);
59 #define OFFSET(x) offsetof(ShowWavesContext, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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 },
77 AVFILTER_DEFINE_CLASS(showwaves);
79 static av_cold void uninit(AVFilterContext *ctx)
81 ShowWavesContext *showwaves = ctx->priv;
83 av_frame_free(&showwaves->outpicref);
84 av_freep(&showwaves->buf_idy);
87 static int query_formats(AVFilterContext *ctx)
89 AVFilterFormats *formats = NULL;
90 AVFilterChannelLayouts *layouts = NULL;
91 AVFilterLink *inlink = ctx->inputs[0];
92 AVFilterLink *outlink = ctx->outputs[0];
93 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
94 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
96 /* set input audio formats */
97 formats = ff_make_format_list(sample_fmts);
99 return AVERROR(ENOMEM);
100 ff_formats_ref(formats, &inlink->out_formats);
102 layouts = ff_all_channel_layouts();
104 return AVERROR(ENOMEM);
105 ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
107 formats = ff_all_samplerates();
109 return AVERROR(ENOMEM);
110 ff_formats_ref(formats, &inlink->out_samplerates);
112 /* set output video format */
113 formats = ff_make_format_list(pix_fmts);
115 return AVERROR(ENOMEM);
116 ff_formats_ref(formats, &outlink->in_formats);
121 static int config_output(AVFilterLink *outlink)
123 AVFilterContext *ctx = outlink->src;
124 AVFilterLink *inlink = ctx->inputs[0];
125 ShowWavesContext *showwaves = ctx->priv;
126 int nb_channels = inlink->channels;
129 showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
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);
136 outlink->w = showwaves->w;
137 outlink->h = showwaves->h;
138 outlink->sample_aspect_ratio = (AVRational){1,1};
140 outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
141 (AVRational){showwaves->w,1});
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);
148 inline static int push_frame(AVFilterLink *outlink)
150 AVFilterContext *ctx = outlink->src;
151 AVFilterLink *inlink = ctx->inputs[0];
152 ShowWavesContext *showwaves = outlink->src->priv;
153 int nb_channels = inlink->channels;
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;
165 static int request_frame(AVFilterLink *outlink)
167 ShowWavesContext *showwaves = outlink->src->priv;
168 AVFilterLink *inlink = outlink->src->inputs[0];
171 showwaves->req_fullfilled = 0;
173 ret = ff_request_frame(inlink);
174 } while (!showwaves->req_fullfilled && ret >= 0);
176 if (ret == AVERROR_EOF && showwaves->outpicref)
181 static void draw_sample_point(uint8_t *buf, int height, int linesize,
182 int16_t sample, int16_t *prev_y, int intensity)
184 const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
185 if (h >= 0 && h < height)
186 buf[h * linesize] += intensity;
189 static void draw_sample_line(uint8_t *buf, int height, int linesize,
190 int16_t sample, int16_t *prev_y, int intensity)
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);
197 FFSWAP(int16_t, start, end);
198 for (k = start; k < end; k++)
199 buf[k * linesize] += intensity;
202 static void draw_sample_p2p(uint8_t *buf, int height, int linesize,
203 int16_t sample, int16_t *prev_y, int intensity)
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) {
211 int end = av_clip(h, 0, height-1);
213 FFSWAP(int16_t, start, end);
214 for (k = start + 1; k < end; k++)
215 buf[k * linesize] += intensity;
221 static void draw_sample_cline(uint8_t *buf, int height, int linesize,
222 int16_t sample, int16_t *prev_y, int intensity)
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;
232 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
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;
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;
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);
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 },
260 linesize = outpicref->linesize[0];
261 for (j = 0; j < outlink->h; j++)
262 memset(outpicref->data[0] + j * linesize, 0, outlink->w);
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);
272 showwaves->sample_count_mod++;
273 if (showwaves->sample_count_mod == n) {
274 showwaves->sample_count_mod = 0;
275 showwaves->buf_idx++;
277 if (showwaves->buf_idx == showwaves->w)
278 if ((ret = push_frame(outlink)) < 0)
280 outpicref = showwaves->outpicref;
283 av_frame_free(&insamples);
287 static av_cold int init(AVFilterContext *ctx)
289 ShowWavesContext *showwaves = ctx->priv;
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;
302 static const AVFilterPad showwaves_inputs[] = {
305 .type = AVMEDIA_TYPE_AUDIO,
306 .filter_frame = filter_frame,
311 static const AVFilterPad showwaves_outputs[] = {
314 .type = AVMEDIA_TYPE_VIDEO,
315 .config_props = config_output,
316 .request_frame = request_frame,
321 AVFilter ff_avf_showwaves = {
323 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
326 .query_formats = query_formats,
327 .priv_size = sizeof(ShowWavesContext),
328 .inputs = showwaves_inputs,
329 .outputs = showwaves_outputs,
330 .priv_class = &showwaves_class,