]> git.sesse.net Git - ffmpeg/blob - libavfilter/avf_showwaves.c
Remove "Error parsing options string [...]" messages.
[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/audioconvert.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 typedef struct {
36     const AVClass *class;
37     int w, h;
38     char *rate_str;
39     AVRational rate;
40     int buf_idx;
41     AVFilterBufferRef *outpicref;
42     int req_fullfilled;
43     int n;
44     int sample_count_mod;
45 } ShowWavesContext;
46
47 #define OFFSET(x) offsetof(ShowWavesContext, x)
48
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},
55     { NULL },
56 };
57
58 AVFILTER_DEFINE_CLASS(showwaves);
59
60 static av_cold int init(AVFilterContext *ctx, const char *args)
61 {
62     ShowWavesContext *showwaves = ctx->priv;
63     int err;
64
65     showwaves->class = &showwaves_class;
66     av_opt_set_defaults(showwaves);
67     showwaves->buf_idx = 0;
68
69     if ((err = av_set_options_string(showwaves, args, "=", ":")) < 0)
70         return err;
71
72     return 0;
73 }
74
75 static av_cold void uninit(AVFilterContext *ctx)
76 {
77     ShowWavesContext *showwaves = ctx->priv;
78
79     av_freep(&showwaves->rate_str);
80     avfilter_unref_bufferp(&showwaves->outpicref);
81 }
82
83 static int query_formats(AVFilterContext *ctx)
84 {
85     AVFilterFormats *formats = NULL;
86     AVFilterChannelLayouts *layouts = NULL;
87     AVFilterLink *inlink = ctx->inputs[0];
88     AVFilterLink *outlink = ctx->outputs[0];
89     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
90     static const enum PixelFormat pix_fmts[] = { PIX_FMT_GRAY8, -1 };
91
92     /* set input audio formats */
93     formats = ff_make_format_list(sample_fmts);
94     if (!formats)
95         return AVERROR(ENOMEM);
96     ff_formats_ref(formats, &inlink->out_formats);
97
98     layouts = ff_all_channel_layouts();
99     if (!layouts)
100         return AVERROR(ENOMEM);
101     ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
102
103     formats = ff_all_samplerates();
104     if (!formats)
105         return AVERROR(ENOMEM);
106     ff_formats_ref(formats, &inlink->out_samplerates);
107
108     /* set output video format */
109     formats = ff_make_format_list(pix_fmts);
110     if (!formats)
111         return AVERROR(ENOMEM);
112     ff_formats_ref(formats, &outlink->in_formats);
113
114     return 0;
115 }
116
117 static int config_output(AVFilterLink *outlink)
118 {
119     AVFilterContext *ctx = outlink->src;
120     AVFilterLink *inlink = ctx->inputs[0];
121     ShowWavesContext *showwaves = ctx->priv;
122     int err;
123
124     if (showwaves->n && showwaves->rate_str) {
125         av_log(ctx, AV_LOG_ERROR, "Options 'n' and 'rate' cannot be set at the same time\n");
126         return AVERROR(EINVAL);
127     }
128
129     if (!showwaves->n) {
130         if (!showwaves->rate_str)
131             showwaves->rate = (AVRational){25,1}; /* set default value */
132         else if ((err = av_parse_video_rate(&showwaves->rate, showwaves->rate_str)) < 0) {
133             av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", showwaves->rate_str);
134             return err;
135         }
136         showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
137     }
138
139     outlink->w = showwaves->w;
140     outlink->h = showwaves->h;
141     outlink->sample_aspect_ratio = (AVRational){1,1};
142
143     outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
144                                    (AVRational){showwaves->w,1});
145
146     av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
147            showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
148     return 0;
149 }
150
151 inline static void push_frame(AVFilterLink *outlink)
152 {
153     ShowWavesContext *showwaves = outlink->src->priv;
154
155     ff_start_frame(outlink, showwaves->outpicref);
156     ff_draw_slice(outlink, 0, outlink->h, 1);
157     ff_end_frame(outlink);
158     showwaves->req_fullfilled = 1;
159     showwaves->outpicref = NULL;
160     showwaves->buf_idx = 0;
161 }
162
163 static int request_frame(AVFilterLink *outlink)
164 {
165     ShowWavesContext *showwaves = outlink->src->priv;
166     AVFilterLink *inlink = outlink->src->inputs[0];
167     int ret;
168
169     showwaves->req_fullfilled = 0;
170     do {
171         ret = ff_request_frame(inlink);
172     } while (!showwaves->req_fullfilled && ret >= 0);
173
174     if (ret == AVERROR_EOF && showwaves->outpicref)
175         push_frame(outlink);
176     return ret;
177 }
178
179 #define MAX_INT16 ((1<<15) -1)
180
181 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
182 {
183     AVFilterContext *ctx = inlink->dst;
184     AVFilterLink *outlink = ctx->outputs[0];
185     ShowWavesContext *showwaves = ctx->priv;
186     const int nb_samples = insamples->audio->nb_samples;
187     AVFilterBufferRef *outpicref = showwaves->outpicref;
188     int linesize = outpicref ? outpicref->linesize[0] : 0;
189     int16_t *p = (int16_t *)insamples->data[0];
190     int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
191     int i, j, h;
192     const int n = showwaves->n;
193     const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
194
195     /* draw data in the buffer */
196     for (i = 0; i < nb_samples; i++) {
197         if (showwaves->buf_idx == 0 && showwaves->sample_count_mod == 0) {
198             showwaves->outpicref = outpicref =
199                 ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN,
200                                     outlink->w, outlink->h);
201             outpicref->video->w = outlink->w;
202             outpicref->video->h = outlink->h;
203             outpicref->pts = insamples->pts +
204                              av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
205                                           (AVRational){ 1, inlink->sample_rate },
206                                           outlink->time_base);
207             linesize = outpicref->linesize[0];
208             memset(outpicref->data[0], 0, showwaves->h*linesize);
209         }
210         for (j = 0; j < nb_channels; j++) {
211             h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16);
212             if (h >= 0 && h < outlink->h)
213                 *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
214         }
215         showwaves->sample_count_mod++;
216         if (showwaves->sample_count_mod == n) {
217             showwaves->sample_count_mod = 0;
218             showwaves->buf_idx++;
219         }
220         if (showwaves->buf_idx == showwaves->w)
221             push_frame(outlink);
222     }
223
224     avfilter_unref_buffer(insamples);
225     return 0;
226 }
227
228 AVFilter avfilter_avf_showwaves = {
229     .name           = "showwaves",
230     .description    = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
231     .init           = init,
232     .uninit         = uninit,
233     .query_formats  = query_formats,
234     .priv_size      = sizeof(ShowWavesContext),
235
236     .inputs  = (const AVFilterPad[]) {
237         {
238             .name           = "default",
239             .type           = AVMEDIA_TYPE_AUDIO,
240             .filter_samples = filter_samples,
241             .min_perms      = AV_PERM_READ,
242         },
243         { .name = NULL }
244     },
245
246     .outputs = (const AVFilterPad[]) {
247         {
248             .name           = "default",
249             .type           = AVMEDIA_TYPE_VIDEO,
250             .config_props   = config_output,
251             .request_frame  = request_frame,
252         },
253         { .name = NULL }
254     },
255 };