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