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