From 3ea3e32e03eb0cffa088c737230e4a18850a0860 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini Date: Sun, 25 Dec 2011 17:37:53 +0100 Subject: [PATCH] lavfi: add showwaves filter --- Changelog | 1 + doc/filters.texi | 45 +++++++ libavfilter/Makefile | 2 + libavfilter/allfilters.c | 3 + libavfilter/avf_showwaves.c | 259 ++++++++++++++++++++++++++++++++++++ libavfilter/version.h | 2 +- 6 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 libavfilter/avf_showwaves.c diff --git a/Changelog b/Changelog index 0f4168a02c..93e350ba19 100644 --- a/Changelog +++ b/Changelog @@ -11,6 +11,7 @@ version next: - ffprobe -show_data option - RTMPT protocol support - iLBC encoding/decoding via libilbc +- showwaves filter version 0.11: diff --git a/doc/filters.texi b/doc/filters.texi index 566fbfcd73..bf7b12b610 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -3826,3 +3826,48 @@ mainly useful as a template and to be employed in analysis / debugging tools. @c man end VIDEO SINKS + +@chapter Transmedia Filters +@c man begin TRANSMEDIA FILTERS + +Below is a description of the currently available transmedia filters. + +@section showwaves + +Convert input audio to a video output, representing the samples waves. + +The filter accepts the following named parameters: +@table @option + +@item n +Set the number of samples which are printed on the same column. A +larger value will decrease the frame rate. Must be a positive +integer. This option can be set only if the value for @var{rate} +is not explicitly specified. + +@item rate, r +Set the (approximate) output frame rate. This is done by setting the +option @var{n}. Default value is "25". + +@item size, s +Specify the video size for the output. Default value is "600x240". +@end table + +Some examples follow. +@itemize +@item +Output the input file audio and the corresponding video representation +at the same time: +@example +amovie=a.mp3,asplit[out0],showwaves[out1] +@end example + +@item +Create a synthetic signal and show it with showwaves, forcing a +framerate of 30 frames per second: +@example +aevalsrc=sin(1*2*PI*t)*sin(880*2*PI*t):cos(2*PI*200*t),asplit[out0],showwaves=r=30[out1] +@end example +@end itemize + +@c man end TRANSMEDIA FILTERS diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 72bb6409db..6de6ef4baf 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -193,6 +193,8 @@ OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_yuvcsp.o OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_yvu9.o OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/pullup.o +# transmedia filters +OBJS-$(CONFIG_SHOWWAVES_FILTER) += avf_showwaves.o TESTPROGS = drawutils formats diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 35065d5b12..6d2d6cf2e9 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -129,6 +129,9 @@ void avfilter_register_all(void) REGISTER_FILTER (BUFFERSINK, buffersink, vsink); REGISTER_FILTER (NULLSINK, nullsink, vsink); + /* transmedia filters */ + REGISTER_FILTER (SHOWWAVES, showwaves, avf); + /* those filters are part of public or internal API => registered * unconditionally */ { diff --git a/libavfilter/avf_showwaves.c b/libavfilter/avf_showwaves.c new file mode 100644 index 0000000000..319f832c64 --- /dev/null +++ b/libavfilter/avf_showwaves.c @@ -0,0 +1,259 @@ +/* + * Copyright (c) 2012 Stefano Sabatini + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * audio to video transmedia filter + */ + +#include "libavutil/audioconvert.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "avfilter.h" +#include "formats.h" +#include "audio.h" +#include "video.h" + +typedef struct { + const AVClass *class; + int w, h; + char *rate_str; + AVRational rate; + int buf_idx; + AVFilterBufferRef *outpicref; + int req_fullfilled; + int n; + int sample_count_mod; +} ShowWavesContext; + +#define OFFSET(x) offsetof(ShowWavesContext, x) + +static const AVOption showwaves_options[] = { + { "rate", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 }, + { "r", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 }, + { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0 }, + { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0 }, + { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX}, + { NULL }, +}; + +static const AVClass showwaves_class = { + .class_name = "showwaves", + .item_name = av_default_item_name, + .option = showwaves_options, + .version = LIBAVUTIL_VERSION_INT, + .category = AV_CLASS_CATEGORY_FILTER, +}; + +static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) +{ + ShowWavesContext *showwaves = ctx->priv; + int err; + + showwaves->class = &showwaves_class; + av_opt_set_defaults(showwaves); + showwaves->buf_idx = 0; + + if ((err = av_set_options_string(showwaves, args, "=", ":")) < 0) { + av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args); + return err; + } + + return 0; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + ShowWavesContext *showwaves = ctx->priv; + + av_freep(&showwaves->rate_str); + avfilter_unref_bufferp(&showwaves->outpicref); +} + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats = NULL; + AVFilterChannelLayouts *layouts = NULL; + AVFilterLink *inlink = ctx->inputs[0]; + AVFilterLink *outlink = ctx->outputs[0]; + static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 }; + static const enum PixelFormat pix_fmts[] = { PIX_FMT_GRAY8, -1 }; + + /* set input audio formats */ + formats = ff_make_format_list(sample_fmts); + if (!formats) + return AVERROR(ENOMEM); + ff_formats_ref(formats, &inlink->out_formats); + + layouts = ff_all_channel_layouts(); + if (!layouts) + return AVERROR(ENOMEM); + ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts); + + formats = ff_all_samplerates(); + if (!formats) + return AVERROR(ENOMEM); + ff_formats_ref(formats, &inlink->out_samplerates); + + /* set output video format */ + formats = ff_make_format_list(pix_fmts); + if (!formats) + return AVERROR(ENOMEM); + ff_formats_ref(formats, &outlink->in_formats); + + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + AVFilterLink *inlink = ctx->inputs[0]; + ShowWavesContext *showwaves = ctx->priv; + int err; + + if (showwaves->n && showwaves->rate_str) { + av_log(ctx, AV_LOG_ERROR, "Options 'n' and 'rate' cannot be set at the same time\n"); + return AVERROR(EINVAL); + } + + if (!showwaves->n) { + if (!showwaves->rate_str) + showwaves->rate = (AVRational){25,1}; /* set default value */ + else if ((err = av_parse_video_rate(&showwaves->rate, showwaves->rate_str)) < 0) { + av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", showwaves->rate_str); + return err; + } + showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5); + } + + outlink->w = showwaves->w; + outlink->h = showwaves->h; + outlink->sample_aspect_ratio = (AVRational){1,1}; + + outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n}, + (AVRational){showwaves->w,1}); + + av_log(ctx, AV_LOG_INFO, "s:%dx%d r:%f n:%d\n", + showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n); + return 0; +} + +inline static void push_frame(AVFilterLink *outlink) +{ + ShowWavesContext *showwaves = outlink->src->priv; + + ff_start_frame(outlink, showwaves->outpicref); + ff_draw_slice(outlink, 0, outlink->h, 1); + ff_end_frame(outlink); + showwaves->req_fullfilled = 1; + showwaves->outpicref = NULL; + showwaves->buf_idx = 0; +} + +static int request_frame(AVFilterLink *outlink) +{ + ShowWavesContext *showwaves = outlink->src->priv; + AVFilterLink *inlink = outlink->src->inputs[0]; + int ret; + + showwaves->req_fullfilled = 0; + do { + ret = avfilter_request_frame(inlink); + } while (!showwaves->req_fullfilled && ret >= 0); + + if (ret == AVERROR_EOF && showwaves->outpicref) + push_frame(outlink); + return ret; +} + +#define MAX_INT16 ((1<<15) -1) + +static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + ShowWavesContext *showwaves = ctx->priv; + const int nb_samples = insamples->audio->nb_samples; + AVFilterBufferRef *outpicref = showwaves->outpicref; + int linesize = outpicref ? outpicref->linesize[0] : 0; + int16_t *p = (int16_t *)insamples->data[0]; + int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout); + int i, j, h; + const int n = showwaves->n; + const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */ + + /* draw data in the buffer */ + for (i = 0; i < nb_samples; i++) { + if (showwaves->buf_idx == 0) { + showwaves->outpicref = outpicref = + ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, + outlink->w, outlink->h); + outpicref->video->w = outlink->w; + outpicref->video->h = outlink->h; + outpicref->pts = insamples->pts; + outlink->out_buf = outpicref; + linesize = outpicref->linesize[0]; + memset(outpicref->data[0], 0, showwaves->h*linesize); + } + for (j = 0; j < nb_channels; j++) { + h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16); + if (h >= 0 && h < outlink->h) + *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x; + } + showwaves->sample_count_mod++; + if (showwaves->sample_count_mod == n) { + showwaves->sample_count_mod = 0; + showwaves->buf_idx++; + } + if (showwaves->buf_idx == showwaves->w) + push_frame(outlink); + } + + avfilter_unref_buffer(insamples); +} + +AVFilter avfilter_avf_showwaves = { + .name = "showwaves", + .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."), + .init = init, + .uninit = uninit, + .query_formats = query_formats, + .priv_size = sizeof(ShowWavesContext), + + .inputs = (const AVFilterPad[]) { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_samples = filter_samples, + .min_perms = AV_PERM_READ, + }, + { .name = NULL } + }, + + .outputs = (const AVFilterPad[]) { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + .request_frame = request_frame, + }, + { .name = NULL } + }, +}; diff --git a/libavfilter/version.h b/libavfilter/version.h index 8b3265f7f3..221a0de041 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -29,7 +29,7 @@ #include "libavutil/avutil.h" #define LIBAVFILTER_VERSION_MAJOR 2 -#define LIBAVFILTER_VERSION_MINOR 81 +#define LIBAVFILTER_VERSION_MINOR 82 #define LIBAVFILTER_VERSION_MICRO 100 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \