]> git.sesse.net Git - ffmpeg/blob - libavfilter/avf_showwaves.c
Merge commit '4db96649ca700db563d9da4ebe70bf9fc4c7a6ba'
[ffmpeg] / libavfilter / avf_showwaves.c
1 /*
2  * Copyright (c) 2012 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * audio to video multimedia filter
24  */
25
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "audio.h"
32 #include "video.h"
33 #include "internal.h"
34
35 enum ShowWavesMode {
36     MODE_POINT,
37     MODE_LINE,
38     MODE_NB,
39 };
40
41 typedef struct {
42     const AVClass *class;
43     int w, h;
44     char *rate_str;
45     AVRational rate;
46     int buf_idx;
47     AVFilterBufferRef *outpicref;
48     int req_fullfilled;
49     int n;
50     int sample_count_mod;
51     enum ShowWavesMode mode;
52 } ShowWavesContext;
53
54 #define OFFSET(x) offsetof(ShowWavesContext, x)
55 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
56
57 static const AVOption showwaves_options[] = {
58     { "rate", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
59     { "r",    "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
60     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
61     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
62     { "n",    "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
63
64     {"mode",  "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
65     {"point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
66     {"line",  "draw a line for each sample",  0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE},  .flags=FLAGS, .unit="mode"},
67     { NULL },
68 };
69
70 AVFILTER_DEFINE_CLASS(showwaves);
71
72 static av_cold int init(AVFilterContext *ctx, const char *args)
73 {
74     ShowWavesContext *showwaves = ctx->priv;
75     int err;
76
77     showwaves->class = &showwaves_class;
78     av_opt_set_defaults(showwaves);
79     showwaves->buf_idx = 0;
80
81     if ((err = av_set_options_string(showwaves, args, "=", ":")) < 0)
82         return err;
83
84     return 0;
85 }
86
87 static av_cold void uninit(AVFilterContext *ctx)
88 {
89     ShowWavesContext *showwaves = ctx->priv;
90
91     av_freep(&showwaves->rate_str);
92     avfilter_unref_bufferp(&showwaves->outpicref);
93 }
94
95 static int query_formats(AVFilterContext *ctx)
96 {
97     AVFilterFormats *formats = NULL;
98     AVFilterChannelLayouts *layouts = NULL;
99     AVFilterLink *inlink = ctx->inputs[0];
100     AVFilterLink *outlink = ctx->outputs[0];
101     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
102     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
103
104     /* set input audio formats */
105     formats = ff_make_format_list(sample_fmts);
106     if (!formats)
107         return AVERROR(ENOMEM);
108     ff_formats_ref(formats, &inlink->out_formats);
109
110     layouts = ff_all_channel_layouts();
111     if (!layouts)
112         return AVERROR(ENOMEM);
113     ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
114
115     formats = ff_all_samplerates();
116     if (!formats)
117         return AVERROR(ENOMEM);
118     ff_formats_ref(formats, &inlink->out_samplerates);
119
120     /* set output video format */
121     formats = ff_make_format_list(pix_fmts);
122     if (!formats)
123         return AVERROR(ENOMEM);
124     ff_formats_ref(formats, &outlink->in_formats);
125
126     return 0;
127 }
128
129 static int config_output(AVFilterLink *outlink)
130 {
131     AVFilterContext *ctx = outlink->src;
132     AVFilterLink *inlink = ctx->inputs[0];
133     ShowWavesContext *showwaves = ctx->priv;
134     int err;
135
136     if (showwaves->n && showwaves->rate_str) {
137         av_log(ctx, AV_LOG_ERROR, "Options 'n' and 'rate' cannot be set at the same time\n");
138         return AVERROR(EINVAL);
139     }
140
141     if (!showwaves->n) {
142         if (!showwaves->rate_str)
143             showwaves->rate = (AVRational){25,1}; /* set default value */
144         else if ((err = av_parse_video_rate(&showwaves->rate, showwaves->rate_str)) < 0) {
145             av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", showwaves->rate_str);
146             return err;
147         }
148         showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
149     }
150
151     outlink->w = showwaves->w;
152     outlink->h = showwaves->h;
153     outlink->sample_aspect_ratio = (AVRational){1,1};
154
155     outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
156                                    (AVRational){showwaves->w,1});
157
158     av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
159            showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
160     return 0;
161 }
162
163 inline static int push_frame(AVFilterLink *outlink)
164 {
165     ShowWavesContext *showwaves = outlink->src->priv;
166     int ret;
167
168     if ((ret = ff_filter_frame(outlink, showwaves->outpicref)) >= 0)
169         showwaves->req_fullfilled = 1;
170     showwaves->outpicref = NULL;
171     showwaves->buf_idx = 0;
172     return ret;
173 }
174
175 static int request_frame(AVFilterLink *outlink)
176 {
177     ShowWavesContext *showwaves = outlink->src->priv;
178     AVFilterLink *inlink = outlink->src->inputs[0];
179     int ret;
180
181     showwaves->req_fullfilled = 0;
182     do {
183         ret = ff_request_frame(inlink);
184     } while (!showwaves->req_fullfilled && ret >= 0);
185
186     if (ret == AVERROR_EOF && showwaves->outpicref)
187         push_frame(outlink);
188     return ret;
189 }
190
191 #define MAX_INT16 ((1<<15) -1)
192
193 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
194 {
195     AVFilterContext *ctx = inlink->dst;
196     AVFilterLink *outlink = ctx->outputs[0];
197     ShowWavesContext *showwaves = ctx->priv;
198     const int nb_samples = insamples->audio->nb_samples;
199     AVFilterBufferRef *outpicref = showwaves->outpicref;
200     int linesize = outpicref ? outpicref->linesize[0] : 0;
201     int16_t *p = (int16_t *)insamples->data[0];
202     int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
203     int i, j, k, h, ret = 0;
204     const int n = showwaves->n;
205     const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
206
207     /* draw data in the buffer */
208     for (i = 0; i < nb_samples; i++) {
209         if (!showwaves->outpicref) {
210             showwaves->outpicref = outpicref =
211                 ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN,
212                                     outlink->w, outlink->h);
213             if (!outpicref)
214                 return AVERROR(ENOMEM);
215             outpicref->video->w = outlink->w;
216             outpicref->video->h = outlink->h;
217             outpicref->pts = insamples->pts +
218                              av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
219                                           (AVRational){ 1, inlink->sample_rate },
220                                           outlink->time_base);
221             linesize = outpicref->linesize[0];
222             memset(outpicref->data[0], 0, showwaves->h*linesize);
223         }
224         for (j = 0; j < nb_channels; j++) {
225             h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16);
226             switch (showwaves->mode) {
227             case MODE_POINT:
228                 if (h >= 0 && h < outlink->h)
229                     *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
230                 break;
231
232             case MODE_LINE:
233             {
234                 int start = showwaves->h/2, end = av_clip(h, 0, outlink->h-1);
235                 if (start > end) FFSWAP(int16_t, start, end);
236                 for (k = start; k < end; k++)
237                     *(outpicref->data[0] + showwaves->buf_idx + k * linesize) += x;
238                 break;
239             }
240             }
241         }
242
243         showwaves->sample_count_mod++;
244         if (showwaves->sample_count_mod == n) {
245             showwaves->sample_count_mod = 0;
246             showwaves->buf_idx++;
247         }
248         if (showwaves->buf_idx == showwaves->w)
249             if ((ret = push_frame(outlink)) < 0)
250                 break;
251         outpicref = showwaves->outpicref;
252     }
253
254     avfilter_unref_buffer(insamples);
255     return ret;
256 }
257
258 static const AVFilterPad showwaves_inputs[] = {
259     {
260         .name         = "default",
261         .type         = AVMEDIA_TYPE_AUDIO,
262         .filter_frame = filter_frame,
263         .min_perms    = AV_PERM_READ,
264     },
265     { NULL }
266 };
267
268 static const AVFilterPad showwaves_outputs[] = {
269     {
270         .name          = "default",
271         .type          = AVMEDIA_TYPE_VIDEO,
272         .config_props  = config_output,
273         .request_frame = request_frame,
274     },
275     { NULL }
276 };
277
278 AVFilter avfilter_avf_showwaves = {
279     .name           = "showwaves",
280     .description    = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
281     .init           = init,
282     .uninit         = uninit,
283     .query_formats  = query_formats,
284     .priv_size      = sizeof(ShowWavesContext),
285     .inputs         = showwaves_inputs,
286     .outputs        = showwaves_outputs,
287     .priv_class     = &showwaves_class,
288 };