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/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/parseutils.h"
54 enum ShowWavesDrawMode {
60 enum ShowWavesFilterMode {
68 struct frame_node *next;
71 typedef struct ShowWavesContext {
77 int16_t *buf_idy; /* y coordinate of previous sample for each channel */
82 int mode; ///< ShowWavesMode
83 int scale; ///< ShowWavesScale
84 int draw_mode; ///< ShowWavesDrawMode
89 int (*get_h)(int16_t sample, int height);
90 void (*draw_sample)(uint8_t *buf, int height, int linesize,
91 int16_t *prev_y, const uint8_t color[4], int h);
95 struct frame_node *audio_frames;
96 struct frame_node *last_frame;
97 int64_t total_samples;
98 int64_t *sum; /* abs sum of the samples per channel */
101 #define OFFSET(x) offsetof(ShowWavesContext, x)
102 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
104 static const AVOption showwaves_options[] = {
105 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
106 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
107 { "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
108 { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
109 { "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
110 { "p2p", "draw a line between samples", 0, AV_OPT_TYPE_CONST, {.i64=MODE_P2P}, .flags=FLAGS, .unit="mode"},
111 { "cline", "draw a centered line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CENTERED_LINE}, .flags=FLAGS, .unit="mode"},
112 { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
113 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
114 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
115 { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
116 { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
117 { "scale", "set amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SCALE_NB-1, FLAGS, .unit="scale" },
118 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LIN}, .flags=FLAGS, .unit="scale"},
119 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LOG}, .flags=FLAGS, .unit="scale"},
120 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_SQRT}, .flags=FLAGS, .unit="scale"},
121 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_CBRT}, .flags=FLAGS, .unit="scale"},
122 { "draw", "set draw mode", OFFSET(draw_mode), AV_OPT_TYPE_INT, {.i64 = DRAW_SCALE}, 0, DRAW_NB-1, FLAGS, .unit="draw" },
123 { "scale", "scale pixel values for each drawn sample", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_SCALE}, .flags=FLAGS, .unit="draw"},
124 { "full", "draw every pixel for sample directly", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_FULL}, .flags=FLAGS, .unit="draw"},
128 AVFILTER_DEFINE_CLASS(showwaves);
130 static av_cold void uninit(AVFilterContext *ctx)
132 ShowWavesContext *showwaves = ctx->priv;
134 av_frame_free(&showwaves->outpicref);
135 av_freep(&showwaves->buf_idy);
136 av_freep(&showwaves->fg);
138 if (showwaves->single_pic) {
139 struct frame_node *node = showwaves->audio_frames;
141 struct frame_node *tmp = node;
144 av_frame_free(&tmp->frame);
147 av_freep(&showwaves->sum);
148 showwaves->last_frame = NULL;
152 static int query_formats(AVFilterContext *ctx)
154 AVFilterFormats *formats = NULL;
155 AVFilterChannelLayouts *layouts = NULL;
156 AVFilterLink *inlink = ctx->inputs[0];
157 AVFilterLink *outlink = ctx->outputs[0];
158 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
159 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
162 /* set input audio formats */
163 formats = ff_make_format_list(sample_fmts);
164 if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0)
167 layouts = ff_all_channel_layouts();
168 if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
171 formats = ff_all_samplerates();
172 if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
175 /* set output video format */
176 formats = ff_make_format_list(pix_fmts);
177 if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
183 static int get_lin_h(int16_t sample, int height)
185 return height/2 - av_rescale(sample, height/2, INT16_MAX);
188 static int get_lin_h2(int16_t sample, int height)
190 return av_rescale(FFABS(sample), height, INT16_MAX);
193 static int get_log_h(int16_t sample, int height)
195 return height/2 - FFSIGN(sample) * (log10(1 + FFABS(sample)) * (height/2) / log10(1 + INT16_MAX));
198 static int get_log_h2(int16_t sample, int height)
200 return log10(1 + FFABS(sample)) * height / log10(1 + INT16_MAX);
203 static int get_sqrt_h(int16_t sample, int height)
205 return height/2 - FFSIGN(sample) * (sqrt(FFABS(sample)) * (height/2) / sqrt(INT16_MAX));
208 static int get_sqrt_h2(int16_t sample, int height)
210 return sqrt(FFABS(sample)) * height / sqrt(INT16_MAX);
213 static int get_cbrt_h(int16_t sample, int height)
215 return height/2 - FFSIGN(sample) * (cbrt(FFABS(sample)) * (height/2) / cbrt(INT16_MAX));
218 static int get_cbrt_h2(int16_t sample, int height)
220 return cbrt(FFABS(sample)) * height / cbrt(INT16_MAX);
223 static void draw_sample_point_rgba_scale(uint8_t *buf, int height, int linesize,
225 const uint8_t color[4], int h)
227 if (h >= 0 && h < height) {
228 buf[h * linesize + 0] += color[0];
229 buf[h * linesize + 1] += color[1];
230 buf[h * linesize + 2] += color[2];
231 buf[h * linesize + 3] += color[3];
235 static void draw_sample_point_rgba_full(uint8_t *buf, int height, int linesize,
237 const uint8_t color[4], int h)
239 if (h >= 0 && h < height) {
240 buf[h * linesize + 0] = color[0];
241 buf[h * linesize + 1] = color[1];
242 buf[h * linesize + 2] = color[2];
243 buf[h * linesize + 3] = color[3];
247 static void draw_sample_line_rgba_scale(uint8_t *buf, int height, int linesize,
249 const uint8_t color[4], int h)
252 int start = height/2;
253 int end = av_clip(h, 0, height-1);
255 FFSWAP(int16_t, start, end);
256 for (k = start; k < end; k++) {
257 buf[k * linesize + 0] += color[0];
258 buf[k * linesize + 1] += color[1];
259 buf[k * linesize + 2] += color[2];
260 buf[k * linesize + 3] += color[3];
264 static void draw_sample_line_rgba_full(uint8_t *buf, int height, int linesize,
266 const uint8_t color[4], int h)
269 int start = height/2;
270 int end = av_clip(h, 0, height-1);
272 FFSWAP(int16_t, start, end);
273 for (k = start; k < end; k++) {
274 buf[k * linesize + 0] = color[0];
275 buf[k * linesize + 1] = color[1];
276 buf[k * linesize + 2] = color[2];
277 buf[k * linesize + 3] = color[3];
281 static void draw_sample_p2p_rgba_scale(uint8_t *buf, int height, int linesize,
283 const uint8_t color[4], int h)
286 if (h >= 0 && h < height) {
287 buf[h * linesize + 0] += color[0];
288 buf[h * linesize + 1] += color[1];
289 buf[h * linesize + 2] += color[2];
290 buf[h * linesize + 3] += color[3];
291 if (*prev_y && h != *prev_y) {
293 int end = av_clip(h, 0, height-1);
295 FFSWAP(int16_t, start, end);
296 for (k = start + 1; k < end; k++) {
297 buf[k * linesize + 0] += color[0];
298 buf[k * linesize + 1] += color[1];
299 buf[k * linesize + 2] += color[2];
300 buf[k * linesize + 3] += color[3];
307 static void draw_sample_p2p_rgba_full(uint8_t *buf, int height, int linesize,
309 const uint8_t color[4], int h)
312 if (h >= 0 && h < height) {
313 buf[h * linesize + 0] = color[0];
314 buf[h * linesize + 1] = color[1];
315 buf[h * linesize + 2] = color[2];
316 buf[h * linesize + 3] = color[3];
317 if (*prev_y && h != *prev_y) {
319 int end = av_clip(h, 0, height-1);
321 FFSWAP(int16_t, start, end);
322 for (k = start + 1; k < end; k++) {
323 buf[k * linesize + 0] = color[0];
324 buf[k * linesize + 1] = color[1];
325 buf[k * linesize + 2] = color[2];
326 buf[k * linesize + 3] = color[3];
333 static void draw_sample_cline_rgba_scale(uint8_t *buf, int height, int linesize,
335 const uint8_t color[4], int h)
338 const int start = (height - h) / 2;
339 const int end = start + h;
340 for (k = start; k < end; k++) {
341 buf[k * linesize + 0] += color[0];
342 buf[k * linesize + 1] += color[1];
343 buf[k * linesize + 2] += color[2];
344 buf[k * linesize + 3] += color[3];
347 static void draw_sample_cline_rgba_full(uint8_t *buf, int height, int linesize,
349 const uint8_t color[4], int h)
352 const int start = (height - h) / 2;
353 const int end = start + h;
354 for (k = start; k < end; k++) {
355 buf[k * linesize + 0] = color[0];
356 buf[k * linesize + 1] = color[1];
357 buf[k * linesize + 2] = color[2];
358 buf[k * linesize + 3] = color[3];
362 static void draw_sample_point_gray(uint8_t *buf, int height, int linesize,
364 const uint8_t color[4], int h)
366 if (h >= 0 && h < height)
367 buf[h * linesize] += color[0];
370 static void draw_sample_line_gray(uint8_t *buf, int height, int linesize,
372 const uint8_t color[4], int h)
375 int start = height/2;
376 int end = av_clip(h, 0, height-1);
378 FFSWAP(int16_t, start, end);
379 for (k = start; k < end; k++)
380 buf[k * linesize] += color[0];
383 static void draw_sample_p2p_gray(uint8_t *buf, int height, int linesize,
385 const uint8_t color[4], int h)
388 if (h >= 0 && h < height) {
389 buf[h * linesize] += color[0];
390 if (*prev_y && h != *prev_y) {
392 int end = av_clip(h, 0, height-1);
394 FFSWAP(int16_t, start, end);
395 for (k = start + 1; k < end; k++)
396 buf[k * linesize] += color[0];
402 static void draw_sample_cline_gray(uint8_t *buf, int height, int linesize,
404 const uint8_t color[4], int h)
407 const int start = (height - h) / 2;
408 const int end = start + h;
409 for (k = start; k < end; k++)
410 buf[k * linesize] += color[0];
413 static int config_output(AVFilterLink *outlink)
415 AVFilterContext *ctx = outlink->src;
416 AVFilterLink *inlink = ctx->inputs[0];
417 ShowWavesContext *showwaves = ctx->priv;
418 int nb_channels = inlink->channels;
419 char *colors, *saveptr = NULL;
423 if (showwaves->single_pic)
427 showwaves->n = FFMAX(1, av_rescale_q(inlink->sample_rate, av_make_q(1, showwaves->w), showwaves->rate));
429 showwaves->buf_idx = 0;
430 if (!(showwaves->buf_idy = av_mallocz_array(nb_channels, sizeof(*showwaves->buf_idy)))) {
431 av_log(ctx, AV_LOG_ERROR, "Could not allocate showwaves buffer\n");
432 return AVERROR(ENOMEM);
434 outlink->w = showwaves->w;
435 outlink->h = showwaves->h;
436 outlink->sample_aspect_ratio = (AVRational){1,1};
438 outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
439 (AVRational){showwaves->w,1});
441 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
442 showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
444 switch (outlink->format) {
445 case AV_PIX_FMT_GRAY8:
446 switch (showwaves->mode) {
447 case MODE_POINT: showwaves->draw_sample = draw_sample_point_gray; break;
448 case MODE_LINE: showwaves->draw_sample = draw_sample_line_gray; break;
449 case MODE_P2P: showwaves->draw_sample = draw_sample_p2p_gray; break;
450 case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline_gray; break;
454 showwaves->pixstep = 1;
456 case AV_PIX_FMT_RGBA:
457 switch (showwaves->mode) {
458 case MODE_POINT: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_point_rgba_scale : draw_sample_point_rgba_full; break;
459 case MODE_LINE: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_line_rgba_scale : draw_sample_line_rgba_full; break;
460 case MODE_P2P: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_p2p_rgba_scale : draw_sample_p2p_rgba_full; break;
461 case MODE_CENTERED_LINE: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_cline_rgba_scale : draw_sample_cline_rgba_full; break;
465 showwaves->pixstep = 4;
469 switch (showwaves->scale) {
471 switch (showwaves->mode) {
474 case MODE_P2P: showwaves->get_h = get_lin_h; break;
475 case MODE_CENTERED_LINE: showwaves->get_h = get_lin_h2; break;
481 switch (showwaves->mode) {
484 case MODE_P2P: showwaves->get_h = get_log_h; break;
485 case MODE_CENTERED_LINE: showwaves->get_h = get_log_h2; break;
491 switch (showwaves->mode) {
494 case MODE_P2P: showwaves->get_h = get_sqrt_h; break;
495 case MODE_CENTERED_LINE: showwaves->get_h = get_sqrt_h2; break;
501 switch (showwaves->mode) {
504 case MODE_P2P: showwaves->get_h = get_cbrt_h; break;
505 case MODE_CENTERED_LINE: showwaves->get_h = get_cbrt_h2; break;
512 showwaves->fg = av_malloc_array(nb_channels, 4 * sizeof(*showwaves->fg));
514 return AVERROR(ENOMEM);
516 colors = av_strdup(showwaves->colors);
518 return AVERROR(ENOMEM);
520 if (showwaves->draw_mode == DRAW_SCALE) {
521 /* multiplication factor, pre-computed to avoid in-loop divisions */
522 x = 255 / ((showwaves->split_channels ? 1 : nb_channels) * showwaves->n);
526 if (outlink->format == AV_PIX_FMT_RGBA) {
527 uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
529 for (ch = 0; ch < nb_channels; ch++) {
532 color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
534 av_parse_color(fg, color, -1, ctx);
535 showwaves->fg[4*ch + 0] = fg[0] * x / 255.;
536 showwaves->fg[4*ch + 1] = fg[1] * x / 255.;
537 showwaves->fg[4*ch + 2] = fg[2] * x / 255.;
538 showwaves->fg[4*ch + 3] = fg[3] * x / 255.;
541 for (ch = 0; ch < nb_channels; ch++)
542 showwaves->fg[4 * ch + 0] = x;
549 inline static int push_frame(AVFilterLink *outlink)
551 AVFilterContext *ctx = outlink->src;
552 AVFilterLink *inlink = ctx->inputs[0];
553 ShowWavesContext *showwaves = outlink->src->priv;
554 int nb_channels = inlink->channels;
557 ret = ff_filter_frame(outlink, showwaves->outpicref);
558 showwaves->outpicref = NULL;
559 showwaves->buf_idx = 0;
560 for (i = 0; i < nb_channels; i++)
561 showwaves->buf_idy[i] = 0;
565 static int push_single_pic(AVFilterLink *outlink)
567 AVFilterContext *ctx = outlink->src;
568 AVFilterLink *inlink = ctx->inputs[0];
569 ShowWavesContext *showwaves = ctx->priv;
570 int64_t n = 0, column_max_samples = showwaves->total_samples / outlink->w;
571 int64_t remaining_samples = showwaves->total_samples - (column_max_samples * outlink->w);
572 int64_t last_column_samples = column_max_samples + remaining_samples;
573 AVFrame *out = showwaves->outpicref;
574 struct frame_node *node;
575 const int nb_channels = inlink->channels;
576 const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
577 const int linesize = out->linesize[0];
578 const int pixstep = showwaves->pixstep;
580 int64_t *sum = showwaves->sum;
582 if (column_max_samples == 0) {
583 av_log(ctx, AV_LOG_ERROR, "Too few samples\n");
584 return AVERROR(EINVAL);
587 av_log(ctx, AV_LOG_DEBUG, "Create frame averaging %"PRId64" samples per column\n", column_max_samples);
589 memset(sum, 0, nb_channels);
591 for (node = showwaves->audio_frames; node; node = node->next) {
593 const AVFrame *frame = node->frame;
594 const int16_t *p = (const int16_t *)frame->data[0];
596 for (i = 0; i < frame->nb_samples; i++) {
597 int64_t max_samples = col == outlink->w - 1 ? last_column_samples: column_max_samples;
600 switch (showwaves->filter_mode) {
602 for (ch = 0; ch < nb_channels; ch++)
603 sum[ch] += abs(p[ch + i*nb_channels]) << 1;
606 for (ch = 0; ch < nb_channels; ch++)
607 sum[ch] = FFMAX(sum[ch], abs(p[ch + i*nb_channels]));
612 if (n == max_samples) {
613 for (ch = 0; ch < nb_channels; ch++) {
614 int16_t sample = sum[ch] / (showwaves->filter_mode == FILTER_AVERAGE ? max_samples : 1);
615 uint8_t *buf = out->data[0] + col * pixstep;
618 if (showwaves->split_channels)
619 buf += ch*ch_height*linesize;
620 av_assert0(col < outlink->w);
621 h = showwaves->get_h(sample, ch_height);
622 showwaves->draw_sample(buf, ch_height, linesize, &showwaves->buf_idy[ch], &showwaves->fg[ch * 4], h);
631 return push_frame(outlink);
635 static int request_frame(AVFilterLink *outlink)
637 ShowWavesContext *showwaves = outlink->src->priv;
638 AVFilterLink *inlink = outlink->src->inputs[0];
641 ret = ff_request_frame(inlink);
642 if (ret == AVERROR_EOF && showwaves->outpicref) {
643 if (showwaves->single_pic)
644 push_single_pic(outlink);
652 static int alloc_out_frame(ShowWavesContext *showwaves, const int16_t *p,
653 const AVFilterLink *inlink, AVFilterLink *outlink,
656 if (!showwaves->outpicref) {
658 AVFrame *out = showwaves->outpicref =
659 ff_get_video_buffer(outlink, outlink->w, outlink->h);
661 return AVERROR(ENOMEM);
662 out->width = outlink->w;
663 out->height = outlink->h;
664 out->pts = in->pts + av_rescale_q((p - (int16_t *)in->data[0]) / inlink->channels,
665 av_make_q(1, inlink->sample_rate),
667 for (j = 0; j < outlink->h; j++)
668 memset(out->data[0] + j*out->linesize[0], 0, outlink->w * showwaves->pixstep);
673 static av_cold int init(AVFilterContext *ctx)
675 ShowWavesContext *showwaves = ctx->priv;
677 if (!strcmp(ctx->filter->name, "showwavespic")) {
678 showwaves->single_pic = 1;
679 showwaves->mode = MODE_CENTERED_LINE;
685 #if CONFIG_SHOWWAVES_FILTER
687 static int showwaves_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
689 AVFilterContext *ctx = inlink->dst;
690 AVFilterLink *outlink = ctx->outputs[0];
691 ShowWavesContext *showwaves = ctx->priv;
692 const int nb_samples = insamples->nb_samples;
693 AVFrame *outpicref = showwaves->outpicref;
694 int16_t *p = (int16_t *)insamples->data[0];
695 int nb_channels = inlink->channels;
697 const int pixstep = showwaves->pixstep;
698 const int n = showwaves->n;
699 const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
701 /* draw data in the buffer */
702 for (i = 0; i < nb_samples; i++) {
704 ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
707 outpicref = showwaves->outpicref;
709 for (j = 0; j < nb_channels; j++) {
710 uint8_t *buf = outpicref->data[0] + showwaves->buf_idx * pixstep;
711 const int linesize = outpicref->linesize[0];
714 if (showwaves->split_channels)
715 buf += j*ch_height*linesize;
716 h = showwaves->get_h(*p++, ch_height);
717 showwaves->draw_sample(buf, ch_height, linesize,
718 &showwaves->buf_idy[j], &showwaves->fg[j * 4], h);
721 showwaves->sample_count_mod++;
722 if (showwaves->sample_count_mod == n) {
723 showwaves->sample_count_mod = 0;
724 showwaves->buf_idx++;
726 if (showwaves->buf_idx == showwaves->w ||
727 (ff_outlink_get_status(inlink) && i == nb_samples - 1))
728 if ((ret = push_frame(outlink)) < 0)
730 outpicref = showwaves->outpicref;
734 av_frame_free(&insamples);
738 static int activate(AVFilterContext *ctx)
740 AVFilterLink *inlink = ctx->inputs[0];
741 AVFilterLink *outlink = ctx->outputs[0];
742 ShowWavesContext *showwaves = ctx->priv;
744 const int nb_samples = showwaves->n * outlink->w;
747 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
749 ret = ff_inlink_consume_samples(inlink, nb_samples, nb_samples, &in);
753 return showwaves_filter_frame(inlink, in);
755 FF_FILTER_FORWARD_STATUS(inlink, outlink);
756 FF_FILTER_FORWARD_WANTED(outlink, inlink);
758 return FFERROR_NOT_READY;
761 static const AVFilterPad showwaves_inputs[] = {
764 .type = AVMEDIA_TYPE_AUDIO,
769 static const AVFilterPad showwaves_outputs[] = {
772 .type = AVMEDIA_TYPE_VIDEO,
773 .config_props = config_output,
778 const AVFilter ff_avf_showwaves = {
780 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
783 .query_formats = query_formats,
784 .priv_size = sizeof(ShowWavesContext),
785 .inputs = showwaves_inputs,
786 .activate = activate,
787 .outputs = showwaves_outputs,
788 .priv_class = &showwaves_class,
791 #endif // CONFIG_SHOWWAVES_FILTER
793 #if CONFIG_SHOWWAVESPIC_FILTER
795 #define OFFSET(x) offsetof(ShowWavesContext, x)
796 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
798 static const AVOption showwavespic_options[] = {
799 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
800 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
801 { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
802 { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
803 { "scale", "set amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SCALE_NB-1, FLAGS, .unit="scale" },
804 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LIN}, .flags=FLAGS, .unit="scale"},
805 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LOG}, .flags=FLAGS, .unit="scale"},
806 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_SQRT}, .flags=FLAGS, .unit="scale"},
807 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_CBRT}, .flags=FLAGS, .unit="scale"},
808 { "draw", "set draw mode", OFFSET(draw_mode), AV_OPT_TYPE_INT, {.i64 = DRAW_SCALE}, 0, DRAW_NB-1, FLAGS, .unit="draw" },
809 { "scale", "scale pixel values for each drawn sample", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_SCALE}, .flags=FLAGS, .unit="draw"},
810 { "full", "draw every pixel for sample directly", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_FULL}, .flags=FLAGS, .unit="draw"},
811 { "filter", "set filter mode", OFFSET(filter_mode), AV_OPT_TYPE_INT, {.i64 = FILTER_AVERAGE}, 0, FILTER_NB-1, FLAGS, .unit="filter" },
812 { "average", "use average samples", 0, AV_OPT_TYPE_CONST, {.i64=FILTER_AVERAGE}, .flags=FLAGS, .unit="filter"},
813 { "peak", "use peak samples", 0, AV_OPT_TYPE_CONST, {.i64=FILTER_PEAK}, .flags=FLAGS, .unit="filter"},
817 AVFILTER_DEFINE_CLASS(showwavespic);
819 static int showwavespic_config_input(AVFilterLink *inlink)
821 AVFilterContext *ctx = inlink->dst;
822 ShowWavesContext *showwaves = ctx->priv;
824 if (showwaves->single_pic) {
825 showwaves->sum = av_mallocz_array(inlink->channels, sizeof(*showwaves->sum));
827 return AVERROR(ENOMEM);
833 static int showwavespic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
835 AVFilterContext *ctx = inlink->dst;
836 AVFilterLink *outlink = ctx->outputs[0];
837 ShowWavesContext *showwaves = ctx->priv;
838 int16_t *p = (int16_t *)insamples->data[0];
841 if (showwaves->single_pic) {
842 struct frame_node *f;
844 ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
848 /* queue the audio frame */
849 f = av_malloc(sizeof(*f));
851 ret = AVERROR(ENOMEM);
854 f->frame = insamples;
856 if (!showwaves->last_frame) {
857 showwaves->audio_frames =
858 showwaves->last_frame = f;
860 showwaves->last_frame->next = f;
861 showwaves->last_frame = f;
863 showwaves->total_samples += insamples->nb_samples;
869 av_frame_free(&insamples);
873 static const AVFilterPad showwavespic_inputs[] = {
876 .type = AVMEDIA_TYPE_AUDIO,
877 .config_props = showwavespic_config_input,
878 .filter_frame = showwavespic_filter_frame,
883 static const AVFilterPad showwavespic_outputs[] = {
886 .type = AVMEDIA_TYPE_VIDEO,
887 .config_props = config_output,
888 .request_frame = request_frame,
893 const AVFilter ff_avf_showwavespic = {
894 .name = "showwavespic",
895 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output single picture."),
898 .query_formats = query_formats,
899 .priv_size = sizeof(ShowWavesContext),
900 .inputs = showwavespic_inputs,
901 .outputs = showwavespic_outputs,
902 .priv_class = &showwavespic_class,
905 #endif // CONFIG_SHOWWAVESPIC_FILTER