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 };
122 /* set input audio formats */
123 formats = ff_make_format_list(sample_fmts);
124 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
127 layouts = ff_all_channel_layouts();
128 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
131 formats = ff_all_samplerates();
132 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
135 /* set output video format */
136 formats = ff_make_format_list(pix_fmts);
137 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
143 static int config_output(AVFilterLink *outlink)
145 AVFilterContext *ctx = outlink->src;
146 AVFilterLink *inlink = ctx->inputs[0];
147 ShowWavesContext *showwaves = ctx->priv;
148 int nb_channels = inlink->channels;
151 showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
153 showwaves->buf_idx = 0;
154 if (!(showwaves->buf_idy = av_mallocz_array(nb_channels, sizeof(*showwaves->buf_idy)))) {
155 av_log(ctx, AV_LOG_ERROR, "Could not allocate showwaves buffer\n");
156 return AVERROR(ENOMEM);
158 outlink->w = showwaves->w;
159 outlink->h = showwaves->h;
160 outlink->sample_aspect_ratio = (AVRational){1,1};
162 outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
163 (AVRational){showwaves->w,1});
165 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
166 showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
170 inline static int push_frame(AVFilterLink *outlink)
172 AVFilterContext *ctx = outlink->src;
173 AVFilterLink *inlink = ctx->inputs[0];
174 ShowWavesContext *showwaves = outlink->src->priv;
175 int nb_channels = inlink->channels;
178 ret = ff_filter_frame(outlink, showwaves->outpicref);
179 showwaves->outpicref = NULL;
180 showwaves->buf_idx = 0;
181 for (i = 0; i < nb_channels; i++)
182 showwaves->buf_idy[i] = 0;
186 static int push_single_pic(AVFilterLink *outlink)
188 AVFilterContext *ctx = outlink->src;
189 AVFilterLink *inlink = ctx->inputs[0];
190 ShowWavesContext *showwaves = ctx->priv;
191 int64_t n = 0, max_samples = showwaves->total_samples / outlink->w;
192 AVFrame *out = showwaves->outpicref;
193 struct frame_node *node;
194 const int nb_channels = inlink->channels;
195 const int x = 255 / (showwaves->split_channels ? 1 : nb_channels);
196 const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
197 const int linesize = out->linesize[0];
199 int64_t *sum = showwaves->sum;
201 if (max_samples == 0) {
202 av_log(ctx, AV_LOG_ERROR, "Too few samples\n");
203 return AVERROR(EINVAL);
206 av_log(ctx, AV_LOG_DEBUG, "Create frame averaging %"PRId64" samples per column\n", max_samples);
208 memset(sum, 0, nb_channels);
210 for (node = showwaves->audio_frames; node; node = node->next) {
212 const AVFrame *frame = node->frame;
213 const int16_t *p = (const int16_t *)frame->data[0];
215 for (i = 0; i < frame->nb_samples; i++) {
218 for (ch = 0; ch < nb_channels; ch++)
219 sum[ch] += abs(p[ch + i*nb_channels]) << 1;
220 if (n++ == max_samples) {
221 for (ch = 0; ch < nb_channels; ch++) {
222 int16_t sample = sum[ch] / max_samples;
223 uint8_t *buf = out->data[0] + col;
224 if (showwaves->split_channels)
225 buf += ch*ch_height*linesize;
226 av_assert0(col < outlink->w);
227 showwaves->draw_sample(buf, ch_height, linesize, sample, &showwaves->buf_idy[ch], x);
236 return push_frame(outlink);
240 static int request_frame(AVFilterLink *outlink)
242 ShowWavesContext *showwaves = outlink->src->priv;
243 AVFilterLink *inlink = outlink->src->inputs[0];
246 ret = ff_request_frame(inlink);
247 if (ret == AVERROR_EOF && showwaves->outpicref) {
248 if (showwaves->single_pic)
249 push_single_pic(outlink);
257 static void draw_sample_point(uint8_t *buf, int height, int linesize,
258 int16_t sample, int16_t *prev_y, int intensity)
260 const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
261 if (h >= 0 && h < height)
262 buf[h * linesize] += intensity;
265 static void draw_sample_line(uint8_t *buf, int height, int linesize,
266 int16_t sample, int16_t *prev_y, int intensity)
269 const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
270 int start = height/2;
271 int end = av_clip(h, 0, height-1);
273 FFSWAP(int16_t, start, end);
274 for (k = start; k < end; k++)
275 buf[k * linesize] += intensity;
278 static void draw_sample_p2p(uint8_t *buf, int height, int linesize,
279 int16_t sample, int16_t *prev_y, int intensity)
282 const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
283 if (h >= 0 && h < height) {
284 buf[h * linesize] += intensity;
285 if (*prev_y && h != *prev_y) {
287 int end = av_clip(h, 0, height-1);
289 FFSWAP(int16_t, start, end);
290 for (k = start + 1; k < end; k++)
291 buf[k * linesize] += intensity;
297 static void draw_sample_cline(uint8_t *buf, int height, int linesize,
298 int16_t sample, int16_t *prev_y, int intensity)
301 const int h = av_rescale(abs(sample), height, INT16_MAX);
302 const int start = (height - h) / 2;
303 const int end = start + h;
304 for (k = start; k < end; k++)
305 buf[k * linesize] += intensity;
308 static int alloc_out_frame(ShowWavesContext *showwaves, const int16_t *p,
309 const AVFilterLink *inlink, AVFilterLink *outlink,
312 if (!showwaves->outpicref) {
314 AVFrame *out = showwaves->outpicref =
315 ff_get_video_buffer(outlink, outlink->w, outlink->h);
317 return AVERROR(ENOMEM);
318 out->width = outlink->w;
319 out->height = outlink->h;
320 out->pts = in->pts + av_rescale_q((p - (int16_t *)in->data[0]) / inlink->channels,
321 av_make_q(1, inlink->sample_rate),
323 for (j = 0; j < outlink->h; j++)
324 memset(out->data[0] + j*out->linesize[0], 0, outlink->w);
329 static av_cold int init(AVFilterContext *ctx)
331 ShowWavesContext *showwaves = ctx->priv;
333 if (!strcmp(ctx->filter->name, "showwavespic")) {
334 showwaves->single_pic = 1;
335 showwaves->mode = MODE_CENTERED_LINE;
338 switch (showwaves->mode) {
339 case MODE_POINT: showwaves->draw_sample = draw_sample_point; break;
340 case MODE_LINE: showwaves->draw_sample = draw_sample_line; break;
341 case MODE_P2P: showwaves->draw_sample = draw_sample_p2p; break;
342 case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline; break;
349 #if CONFIG_SHOWWAVES_FILTER
351 static int showwaves_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
353 AVFilterContext *ctx = inlink->dst;
354 AVFilterLink *outlink = ctx->outputs[0];
355 ShowWavesContext *showwaves = ctx->priv;
356 const int nb_samples = insamples->nb_samples;
357 AVFrame *outpicref = showwaves->outpicref;
358 int16_t *p = (int16_t *)insamples->data[0];
359 int nb_channels = inlink->channels;
361 const int n = showwaves->n;
362 const int x = 255 / ((showwaves->split_channels ? 1 : nb_channels) * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
363 const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
365 /* draw data in the buffer */
366 for (i = 0; i < nb_samples; i++) {
368 ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
371 outpicref = showwaves->outpicref;
373 for (j = 0; j < nb_channels; j++) {
374 uint8_t *buf = outpicref->data[0] + showwaves->buf_idx;
375 const int linesize = outpicref->linesize[0];
376 if (showwaves->split_channels)
377 buf += j*ch_height*linesize;
378 showwaves->draw_sample(buf, ch_height, linesize, *p++,
379 &showwaves->buf_idy[j], x);
382 showwaves->sample_count_mod++;
383 if (showwaves->sample_count_mod == n) {
384 showwaves->sample_count_mod = 0;
385 showwaves->buf_idx++;
387 if (showwaves->buf_idx == showwaves->w)
388 if ((ret = push_frame(outlink)) < 0)
390 outpicref = showwaves->outpicref;
394 av_frame_free(&insamples);
398 static const AVFilterPad showwaves_inputs[] = {
401 .type = AVMEDIA_TYPE_AUDIO,
402 .filter_frame = showwaves_filter_frame,
407 static const AVFilterPad showwaves_outputs[] = {
410 .type = AVMEDIA_TYPE_VIDEO,
411 .config_props = config_output,
412 .request_frame = request_frame,
417 AVFilter ff_avf_showwaves = {
419 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
422 .query_formats = query_formats,
423 .priv_size = sizeof(ShowWavesContext),
424 .inputs = showwaves_inputs,
425 .outputs = showwaves_outputs,
426 .priv_class = &showwaves_class,
429 #endif // CONFIG_SHOWWAVES_FILTER
431 #if CONFIG_SHOWWAVESPIC_FILTER
433 #define OFFSET(x) offsetof(ShowWavesContext, x)
434 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
436 static const AVOption showwavespic_options[] = {
437 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
438 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
439 { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
443 AVFILTER_DEFINE_CLASS(showwavespic);
445 static int showwavespic_config_input(AVFilterLink *inlink)
447 AVFilterContext *ctx = inlink->dst;
448 ShowWavesContext *showwaves = ctx->priv;
450 if (showwaves->single_pic) {
451 showwaves->sum = av_mallocz_array(inlink->channels, sizeof(*showwaves->sum));
453 return AVERROR(ENOMEM);
459 static int showwavespic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
461 AVFilterContext *ctx = inlink->dst;
462 AVFilterLink *outlink = ctx->outputs[0];
463 ShowWavesContext *showwaves = ctx->priv;
464 int16_t *p = (int16_t *)insamples->data[0];
467 if (showwaves->single_pic) {
468 struct frame_node *f;
470 ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
474 /* queue the audio frame */
475 f = av_malloc(sizeof(*f));
477 ret = AVERROR(ENOMEM);
480 f->frame = insamples;
482 if (!showwaves->last_frame) {
483 showwaves->audio_frames =
484 showwaves->last_frame = f;
486 showwaves->last_frame->next = f;
487 showwaves->last_frame = f;
489 showwaves->total_samples += insamples->nb_samples;
495 av_frame_free(&insamples);
499 static const AVFilterPad showwavespic_inputs[] = {
502 .type = AVMEDIA_TYPE_AUDIO,
503 .config_props = showwavespic_config_input,
504 .filter_frame = showwavespic_filter_frame,
509 static const AVFilterPad showwavespic_outputs[] = {
512 .type = AVMEDIA_TYPE_VIDEO,
513 .config_props = config_output,
514 .request_frame = request_frame,
519 AVFilter ff_avf_showwavespic = {
520 .name = "showwavespic",
521 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output single picture."),
524 .query_formats = query_formats,
525 .priv_size = sizeof(ShowWavesContext),
526 .inputs = showwavespic_inputs,
527 .outputs = showwavespic_outputs,
528 .priv_class = &showwavespic_class,
531 #endif // CONFIG_SHOWWAVESPIC_FILTER