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 transmedia filter
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
41 AVFilterBufferRef *outpicref;
47 #define OFFSET(x) offsetof(ShowWavesContext, x)
49 static const AVOption showwaves_options[] = {
50 { "rate", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
51 { "r", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
52 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0 },
53 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0 },
54 { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX},
58 AVFILTER_DEFINE_CLASS(showwaves);
60 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
62 ShowWavesContext *showwaves = ctx->priv;
65 showwaves->class = &showwaves_class;
66 av_opt_set_defaults(showwaves);
67 showwaves->buf_idx = 0;
69 if ((err = av_set_options_string(showwaves, args, "=", ":")) < 0) {
70 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
77 static av_cold void uninit(AVFilterContext *ctx)
79 ShowWavesContext *showwaves = ctx->priv;
81 av_freep(&showwaves->rate_str);
82 avfilter_unref_bufferp(&showwaves->outpicref);
85 static int query_formats(AVFilterContext *ctx)
87 AVFilterFormats *formats = NULL;
88 AVFilterChannelLayouts *layouts = NULL;
89 AVFilterLink *inlink = ctx->inputs[0];
90 AVFilterLink *outlink = ctx->outputs[0];
91 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
92 static const enum PixelFormat pix_fmts[] = { PIX_FMT_GRAY8, -1 };
94 /* set input audio formats */
95 formats = ff_make_format_list(sample_fmts);
97 return AVERROR(ENOMEM);
98 ff_formats_ref(formats, &inlink->out_formats);
100 layouts = ff_all_channel_layouts();
102 return AVERROR(ENOMEM);
103 ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
105 formats = ff_all_samplerates();
107 return AVERROR(ENOMEM);
108 ff_formats_ref(formats, &inlink->out_samplerates);
110 /* set output video format */
111 formats = ff_make_format_list(pix_fmts);
113 return AVERROR(ENOMEM);
114 ff_formats_ref(formats, &outlink->in_formats);
119 static int config_output(AVFilterLink *outlink)
121 AVFilterContext *ctx = outlink->src;
122 AVFilterLink *inlink = ctx->inputs[0];
123 ShowWavesContext *showwaves = ctx->priv;
126 if (showwaves->n && showwaves->rate_str) {
127 av_log(ctx, AV_LOG_ERROR, "Options 'n' and 'rate' cannot be set at the same time\n");
128 return AVERROR(EINVAL);
132 if (!showwaves->rate_str)
133 showwaves->rate = (AVRational){25,1}; /* set default value */
134 else if ((err = av_parse_video_rate(&showwaves->rate, showwaves->rate_str)) < 0) {
135 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", showwaves->rate_str);
138 showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
141 outlink->w = showwaves->w;
142 outlink->h = showwaves->h;
143 outlink->sample_aspect_ratio = (AVRational){1,1};
145 outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
146 (AVRational){showwaves->w,1});
148 av_log(ctx, AV_LOG_INFO, "s:%dx%d r:%f n:%d\n",
149 showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
153 inline static void push_frame(AVFilterLink *outlink)
155 ShowWavesContext *showwaves = outlink->src->priv;
157 ff_start_frame(outlink, showwaves->outpicref);
158 ff_draw_slice(outlink, 0, outlink->h, 1);
159 ff_end_frame(outlink);
160 showwaves->req_fullfilled = 1;
161 showwaves->outpicref = NULL;
162 showwaves->buf_idx = 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 #define MAX_INT16 ((1<<15) -1)
183 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
185 AVFilterContext *ctx = inlink->dst;
186 AVFilterLink *outlink = ctx->outputs[0];
187 ShowWavesContext *showwaves = ctx->priv;
188 const int nb_samples = insamples->audio->nb_samples;
189 AVFilterBufferRef *outpicref = showwaves->outpicref;
190 int linesize = outpicref ? outpicref->linesize[0] : 0;
191 int16_t *p = (int16_t *)insamples->data[0];
192 int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
194 const int n = showwaves->n;
195 const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
197 /* draw data in the buffer */
198 for (i = 0; i < nb_samples; i++) {
199 if (showwaves->buf_idx == 0 && showwaves->sample_count_mod == 0) {
200 showwaves->outpicref = outpicref =
201 ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN,
202 outlink->w, outlink->h);
203 outpicref->video->w = outlink->w;
204 outpicref->video->h = outlink->h;
205 outpicref->pts = insamples->pts +
206 av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
207 (AVRational){ 1, inlink->sample_rate },
209 outlink->out_buf = outpicref;
210 linesize = outpicref->linesize[0];
211 memset(outpicref->data[0], 0, showwaves->h*linesize);
213 for (j = 0; j < nb_channels; j++) {
214 h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16);
215 if (h >= 0 && h < outlink->h)
216 *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
218 showwaves->sample_count_mod++;
219 if (showwaves->sample_count_mod == n) {
220 showwaves->sample_count_mod = 0;
221 showwaves->buf_idx++;
223 if (showwaves->buf_idx == showwaves->w)
227 avfilter_unref_buffer(insamples);
230 AVFilter avfilter_avf_showwaves = {
232 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
235 .query_formats = query_formats,
236 .priv_size = sizeof(ShowWavesContext),
238 .inputs = (const AVFilterPad[]) {
241 .type = AVMEDIA_TYPE_AUDIO,
242 .filter_samples = filter_samples,
243 .min_perms = AV_PERM_READ,
248 .outputs = (const AVFilterPad[]) {
251 .type = AVMEDIA_TYPE_VIDEO,
252 .config_props = config_output,
253 .request_frame = request_frame,