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/avassert.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/parseutils.h"
46 struct frame_node *next;
54 int16_t *buf_idy; /* y coordinate of previous sample for each channel */
58 int mode; ///< ShowWavesMode
60 void (*draw_sample)(uint8_t *buf, int height, int linesize,
61 int16_t sample, int16_t *prev_y, int intensity);
65 struct frame_node *audio_frames;
66 struct frame_node *last_frame;
67 int64_t total_samples;
68 int64_t *sum; /* abs sum of the samples per channel */
71 #define OFFSET(x) offsetof(ShowWavesContext, x)
72 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
74 static const AVOption showwaves_options[] = {
75 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
76 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
77 { "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
78 { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
79 { "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
80 { "p2p", "draw a line between samples", 0, AV_OPT_TYPE_CONST, {.i64=MODE_P2P}, .flags=FLAGS, .unit="mode"},
81 { "cline", "draw a centered line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CENTERED_LINE}, .flags=FLAGS, .unit="mode"},
82 { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
83 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
84 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
85 { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
89 AVFILTER_DEFINE_CLASS(showwaves);
91 static av_cold void uninit(AVFilterContext *ctx)
93 ShowWavesContext *showwaves = ctx->priv;
95 av_frame_free(&showwaves->outpicref);
96 av_freep(&showwaves->buf_idy);
98 if (showwaves->single_pic) {
99 struct frame_node *node = showwaves->audio_frames;
101 struct frame_node *tmp = node;
104 av_frame_free(&tmp->frame);
107 av_freep(&showwaves->sum);
108 showwaves->last_frame = NULL;
112 static int query_formats(AVFilterContext *ctx)
114 AVFilterFormats *formats = NULL;
115 AVFilterChannelLayouts *layouts = NULL;
116 AVFilterLink *inlink = ctx->inputs[0];
117 AVFilterLink *outlink = ctx->outputs[0];
118 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
119 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
121 /* set input audio formats */
122 formats = ff_make_format_list(sample_fmts);
124 return AVERROR(ENOMEM);
125 ff_formats_ref(formats, &inlink->out_formats);
127 layouts = ff_all_channel_layouts();
129 return AVERROR(ENOMEM);
130 ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
132 formats = ff_all_samplerates();
134 return AVERROR(ENOMEM);
135 ff_formats_ref(formats, &inlink->out_samplerates);
137 /* set output video format */
138 formats = ff_make_format_list(pix_fmts);
140 return AVERROR(ENOMEM);
141 ff_formats_ref(formats, &outlink->in_formats);
146 static int config_output(AVFilterLink *outlink)
148 AVFilterContext *ctx = outlink->src;
149 AVFilterLink *inlink = ctx->inputs[0];
150 ShowWavesContext *showwaves = ctx->priv;
151 int nb_channels = inlink->channels;
154 showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
156 showwaves->buf_idx = 0;
157 if (!(showwaves->buf_idy = av_mallocz_array(nb_channels, sizeof(*showwaves->buf_idy)))) {
158 av_log(ctx, AV_LOG_ERROR, "Could not allocate showwaves buffer\n");
159 return AVERROR(ENOMEM);
161 outlink->w = showwaves->w;
162 outlink->h = showwaves->h;
163 outlink->sample_aspect_ratio = (AVRational){1,1};
165 outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
166 (AVRational){showwaves->w,1});
168 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
169 showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
173 inline static int push_frame(AVFilterLink *outlink)
175 AVFilterContext *ctx = outlink->src;
176 AVFilterLink *inlink = ctx->inputs[0];
177 ShowWavesContext *showwaves = outlink->src->priv;
178 int nb_channels = inlink->channels;
181 ret = ff_filter_frame(outlink, showwaves->outpicref);
182 showwaves->outpicref = NULL;
183 showwaves->buf_idx = 0;
184 for (i = 0; i < nb_channels; i++)
185 showwaves->buf_idy[i] = 0;
189 static int push_single_pic(AVFilterLink *outlink)
191 AVFilterContext *ctx = outlink->src;
192 AVFilterLink *inlink = ctx->inputs[0];
193 ShowWavesContext *showwaves = ctx->priv;
194 int64_t n = 0, max_samples = showwaves->total_samples / outlink->w;
195 AVFrame *out = showwaves->outpicref;
196 struct frame_node *node;
197 const int nb_channels = inlink->channels;
198 const int x = 255 / (showwaves->split_channels ? 1 : nb_channels);
199 const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
200 const int linesize = out->linesize[0];
202 int64_t *sum = showwaves->sum;
204 if (max_samples == 0) {
205 av_log(ctx, AV_LOG_ERROR, "Too few samples\n");
206 return AVERROR(EINVAL);
209 av_log(ctx, AV_LOG_DEBUG, "Create frame averaging %"PRId64" samples per column\n", max_samples);
211 memset(sum, 0, nb_channels);
213 for (node = showwaves->audio_frames; node; node = node->next) {
215 const AVFrame *frame = node->frame;
216 const int16_t *p = (const int16_t *)frame->data[0];
218 for (i = 0; i < frame->nb_samples; i++) {
221 for (ch = 0; ch < nb_channels; ch++)
222 sum[ch] += abs(p[ch + i*nb_channels]) << 1;
223 if (n++ == max_samples) {
224 for (ch = 0; ch < nb_channels; ch++) {
225 int16_t sample = sum[ch] / max_samples;
226 uint8_t *buf = out->data[0] + col;
227 if (showwaves->split_channels)
228 buf += ch*ch_height*linesize;
229 av_assert0(col < outlink->w);
230 showwaves->draw_sample(buf, ch_height, linesize, sample, &showwaves->buf_idy[ch], x);
239 return push_frame(outlink);
243 static int request_frame(AVFilterLink *outlink)
245 ShowWavesContext *showwaves = outlink->src->priv;
246 AVFilterLink *inlink = outlink->src->inputs[0];
249 ret = ff_request_frame(inlink);
250 if (ret == AVERROR_EOF && showwaves->outpicref) {
251 if (showwaves->single_pic)
252 push_single_pic(outlink);
260 static void draw_sample_point(uint8_t *buf, int height, int linesize,
261 int16_t sample, int16_t *prev_y, int intensity)
263 const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
264 if (h >= 0 && h < height)
265 buf[h * linesize] += intensity;
268 static void draw_sample_line(uint8_t *buf, int height, int linesize,
269 int16_t sample, int16_t *prev_y, int intensity)
272 const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
273 int start = height/2;
274 int end = av_clip(h, 0, height-1);
276 FFSWAP(int16_t, start, end);
277 for (k = start; k < end; k++)
278 buf[k * linesize] += intensity;
281 static void draw_sample_p2p(uint8_t *buf, int height, int linesize,
282 int16_t sample, int16_t *prev_y, int intensity)
285 const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
286 if (h >= 0 && h < height) {
287 buf[h * linesize] += intensity;
288 if (*prev_y && h != *prev_y) {
290 int end = av_clip(h, 0, height-1);
292 FFSWAP(int16_t, start, end);
293 for (k = start + 1; k < end; k++)
294 buf[k * linesize] += intensity;
300 static void draw_sample_cline(uint8_t *buf, int height, int linesize,
301 int16_t sample, int16_t *prev_y, int intensity)
304 const int h = av_rescale(abs(sample), height, INT16_MAX);
305 const int start = (height - h) / 2;
306 const int end = start + h;
307 for (k = start; k < end; k++)
308 buf[k * linesize] += intensity;
311 static int alloc_out_frame(ShowWavesContext *showwaves, const int16_t *p,
312 const AVFilterLink *inlink, AVFilterLink *outlink,
315 if (!showwaves->outpicref) {
317 AVFrame *out = showwaves->outpicref =
318 ff_get_video_buffer(outlink, outlink->w, outlink->h);
320 return AVERROR(ENOMEM);
321 out->width = outlink->w;
322 out->height = outlink->h;
323 out->pts = in->pts + av_rescale_q((p - (int16_t *)in->data[0]) / inlink->channels,
324 av_make_q(1, inlink->sample_rate),
326 for (j = 0; j < outlink->h; j++)
327 memset(out->data[0] + j*out->linesize[0], 0, outlink->w);
332 static av_cold int init(AVFilterContext *ctx)
334 ShowWavesContext *showwaves = ctx->priv;
336 if (!strcmp(ctx->filter->name, "showwavespic")) {
337 showwaves->single_pic = 1;
338 showwaves->mode = MODE_CENTERED_LINE;
341 switch (showwaves->mode) {
342 case MODE_POINT: showwaves->draw_sample = draw_sample_point; break;
343 case MODE_LINE: showwaves->draw_sample = draw_sample_line; break;
344 case MODE_P2P: showwaves->draw_sample = draw_sample_p2p; break;
345 case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline; break;
352 #if CONFIG_SHOWWAVES_FILTER
354 static int showwaves_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
356 AVFilterContext *ctx = inlink->dst;
357 AVFilterLink *outlink = ctx->outputs[0];
358 ShowWavesContext *showwaves = ctx->priv;
359 const int nb_samples = insamples->nb_samples;
360 AVFrame *outpicref = showwaves->outpicref;
361 int16_t *p = (int16_t *)insamples->data[0];
362 int nb_channels = inlink->channels;
364 const int n = showwaves->n;
365 const int x = 255 / ((showwaves->split_channels ? 1 : nb_channels) * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
366 const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
368 /* draw data in the buffer */
369 for (i = 0; i < nb_samples; i++) {
371 ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
374 outpicref = showwaves->outpicref;
376 for (j = 0; j < nb_channels; j++) {
377 uint8_t *buf = outpicref->data[0] + showwaves->buf_idx;
378 const int linesize = outpicref->linesize[0];
379 if (showwaves->split_channels)
380 buf += j*ch_height*linesize;
381 showwaves->draw_sample(buf, ch_height, linesize, *p++,
382 &showwaves->buf_idy[j], x);
385 showwaves->sample_count_mod++;
386 if (showwaves->sample_count_mod == n) {
387 showwaves->sample_count_mod = 0;
388 showwaves->buf_idx++;
390 if (showwaves->buf_idx == showwaves->w)
391 if ((ret = push_frame(outlink)) < 0)
393 outpicref = showwaves->outpicref;
397 av_frame_free(&insamples);
401 static const AVFilterPad showwaves_inputs[] = {
404 .type = AVMEDIA_TYPE_AUDIO,
405 .filter_frame = showwaves_filter_frame,
410 static const AVFilterPad showwaves_outputs[] = {
413 .type = AVMEDIA_TYPE_VIDEO,
414 .config_props = config_output,
415 .request_frame = request_frame,
420 AVFilter ff_avf_showwaves = {
422 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
425 .query_formats = query_formats,
426 .priv_size = sizeof(ShowWavesContext),
427 .inputs = showwaves_inputs,
428 .outputs = showwaves_outputs,
429 .priv_class = &showwaves_class,
432 #endif // CONFIG_SHOWWAVES_FILTER
434 #if CONFIG_SHOWWAVESPIC_FILTER
436 #define OFFSET(x) offsetof(ShowWavesContext, x)
437 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
439 static const AVOption showwavespic_options[] = {
440 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
441 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
442 { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
446 AVFILTER_DEFINE_CLASS(showwavespic);
448 static int showwavespic_config_input(AVFilterLink *inlink)
450 AVFilterContext *ctx = inlink->dst;
451 ShowWavesContext *showwaves = ctx->priv;
453 if (showwaves->single_pic) {
454 showwaves->sum = av_mallocz_array(inlink->channels, sizeof(*showwaves->sum));
456 return AVERROR(ENOMEM);
462 static int showwavespic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
464 AVFilterContext *ctx = inlink->dst;
465 AVFilterLink *outlink = ctx->outputs[0];
466 ShowWavesContext *showwaves = ctx->priv;
467 int16_t *p = (int16_t *)insamples->data[0];
470 if (showwaves->single_pic) {
471 struct frame_node *f;
473 ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
477 /* queue the audio frame */
478 f = av_malloc(sizeof(*f));
480 ret = AVERROR(ENOMEM);
483 f->frame = insamples;
485 if (!showwaves->last_frame) {
486 showwaves->audio_frames =
487 showwaves->last_frame = f;
489 showwaves->last_frame->next = f;
490 showwaves->last_frame = f;
492 showwaves->total_samples += insamples->nb_samples;
498 av_frame_free(&insamples);
502 static const AVFilterPad showwavespic_inputs[] = {
505 .type = AVMEDIA_TYPE_AUDIO,
506 .config_props = showwavespic_config_input,
507 .filter_frame = showwavespic_filter_frame,
512 static const AVFilterPad showwavespic_outputs[] = {
515 .type = AVMEDIA_TYPE_VIDEO,
516 .config_props = config_output,
517 .request_frame = request_frame,
522 AVFilter ff_avf_showwavespic = {
523 .name = "showwavespic",
524 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output single picture."),
527 .query_formats = query_formats,
528 .priv_size = sizeof(ShowWavesContext),
529 .inputs = showwavespic_inputs,
530 .outputs = showwavespic_outputs,
531 .priv_class = &showwavespic_class,
534 #endif // CONFIG_SHOWWAVESPIC_FILTER