]> git.sesse.net Git - ffmpeg/blob - libavfilter/avf_showwaves.c
Merge commit 'c1aac39eaccd32dc3b74ccfcce701d3d888fbc6b'
[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/avassert.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/parseutils.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "audio.h"
33 #include "video.h"
34 #include "internal.h"
35
36 enum ShowWavesMode {
37     MODE_POINT,
38     MODE_LINE,
39     MODE_P2P,
40     MODE_CENTERED_LINE,
41     MODE_NB,
42 };
43
44 struct frame_node {
45     AVFrame *frame;
46     struct frame_node *next;
47 };
48
49 typedef struct {
50     const AVClass *class;
51     int w, h;
52     AVRational rate;
53     int buf_idx;
54     int16_t *buf_idy;    /* y coordinate of previous sample for each channel */
55     AVFrame *outpicref;
56     int n;
57     int sample_count_mod;
58     int mode;                   ///< ShowWavesMode
59     int split_channels;
60     void (*draw_sample)(uint8_t *buf, int height, int linesize,
61                         int16_t sample, int16_t *prev_y, int intensity);
62
63     /* single picture */
64     int single_pic;
65     struct frame_node *audio_frames;
66     struct frame_node *last_frame;
67     int64_t total_samples;
68     int64_t *sum; /* abs sum of the samples per channel */
69 } ShowWavesContext;
70
71 #define OFFSET(x) offsetof(ShowWavesContext, x)
72 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
73
74 static const AVOption showwaves_options[] = {
75     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
76     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
77     { "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
78         { "point", "draw a point for each sample",         0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT},         .flags=FLAGS, .unit="mode"},
79         { "line",  "draw a line for each sample",          0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE},          .flags=FLAGS, .unit="mode"},
80         { "p2p",   "draw a line between samples",          0, AV_OPT_TYPE_CONST, {.i64=MODE_P2P},           .flags=FLAGS, .unit="mode"},
81         { "cline", "draw a centered line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CENTERED_LINE}, .flags=FLAGS, .unit="mode"},
82     { "n",    "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
83     { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
84     { "r",    "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
85     { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
86     { NULL }
87 };
88
89 AVFILTER_DEFINE_CLASS(showwaves);
90
91 static av_cold void uninit(AVFilterContext *ctx)
92 {
93     ShowWavesContext *showwaves = ctx->priv;
94
95     av_frame_free(&showwaves->outpicref);
96     av_freep(&showwaves->buf_idy);
97
98     if (showwaves->single_pic) {
99         struct frame_node *node = showwaves->audio_frames;
100         while (node) {
101             struct frame_node *tmp = node;
102
103             node = node->next;
104             av_frame_free(&tmp->frame);
105             av_freep(&tmp);
106         }
107         av_freep(&showwaves->sum);
108         showwaves->last_frame = NULL;
109     }
110 }
111
112 static int query_formats(AVFilterContext *ctx)
113 {
114     AVFilterFormats *formats = NULL;
115     AVFilterChannelLayouts *layouts = NULL;
116     AVFilterLink *inlink = ctx->inputs[0];
117     AVFilterLink *outlink = ctx->outputs[0];
118     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
119     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
120
121     /* set input audio formats */
122     formats = ff_make_format_list(sample_fmts);
123     if (!formats)
124         return AVERROR(ENOMEM);
125     ff_formats_ref(formats, &inlink->out_formats);
126
127     layouts = ff_all_channel_layouts();
128     if (!layouts)
129         return AVERROR(ENOMEM);
130     ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
131
132     formats = ff_all_samplerates();
133     if (!formats)
134         return AVERROR(ENOMEM);
135     ff_formats_ref(formats, &inlink->out_samplerates);
136
137     /* set output video format */
138     formats = ff_make_format_list(pix_fmts);
139     if (!formats)
140         return AVERROR(ENOMEM);
141     ff_formats_ref(formats, &outlink->in_formats);
142
143     return 0;
144 }
145
146 static int config_output(AVFilterLink *outlink)
147 {
148     AVFilterContext *ctx = outlink->src;
149     AVFilterLink *inlink = ctx->inputs[0];
150     ShowWavesContext *showwaves = ctx->priv;
151     int nb_channels = inlink->channels;
152
153     if (!showwaves->n)
154         showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
155
156     showwaves->buf_idx = 0;
157     if (!(showwaves->buf_idy = av_mallocz_array(nb_channels, sizeof(*showwaves->buf_idy)))) {
158         av_log(ctx, AV_LOG_ERROR, "Could not allocate showwaves buffer\n");
159         return AVERROR(ENOMEM);
160     }
161     outlink->w = showwaves->w;
162     outlink->h = showwaves->h;
163     outlink->sample_aspect_ratio = (AVRational){1,1};
164
165     outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
166                                    (AVRational){showwaves->w,1});
167
168     av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
169            showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
170     return 0;
171 }
172
173 inline static int push_frame(AVFilterLink *outlink)
174 {
175     AVFilterContext *ctx = outlink->src;
176     AVFilterLink *inlink = ctx->inputs[0];
177     ShowWavesContext *showwaves = outlink->src->priv;
178     int nb_channels = inlink->channels;
179     int ret, i;
180
181     ret = ff_filter_frame(outlink, showwaves->outpicref);
182     showwaves->outpicref = NULL;
183     showwaves->buf_idx = 0;
184     for (i = 0; i < nb_channels; i++)
185         showwaves->buf_idy[i] = 0;
186     return ret;
187 }
188
189 static int push_single_pic(AVFilterLink *outlink)
190 {
191     AVFilterContext *ctx = outlink->src;
192     AVFilterLink *inlink = ctx->inputs[0];
193     ShowWavesContext *showwaves = ctx->priv;
194     int64_t n = 0, max_samples = showwaves->total_samples / outlink->w;
195     AVFrame *out = showwaves->outpicref;
196     struct frame_node *node;
197     const int nb_channels = inlink->channels;
198     const int x = 255 / (showwaves->split_channels ? 1 : nb_channels);
199     const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
200     const int linesize = out->linesize[0];
201     int col = 0;
202     int64_t *sum = showwaves->sum;
203
204     if (max_samples == 0) {
205         av_log(ctx, AV_LOG_ERROR, "Too few samples\n");
206         return AVERROR(EINVAL);
207     }
208
209     av_log(ctx, AV_LOG_DEBUG, "Create frame averaging %"PRId64" samples per column\n", max_samples);
210
211     memset(sum, 0, nb_channels);
212
213     for (node = showwaves->audio_frames; node; node = node->next) {
214         int i;
215         const AVFrame *frame = node->frame;
216         const int16_t *p = (const int16_t *)frame->data[0];
217
218         for (i = 0; i < frame->nb_samples; i++) {
219             int ch;
220
221             for (ch = 0; ch < nb_channels; ch++)
222                 sum[ch] += abs(p[ch + i*nb_channels]) << 1;
223             if (n++ == max_samples) {
224                 for (ch = 0; ch < nb_channels; ch++) {
225                     int16_t sample = sum[ch] / max_samples;
226                     uint8_t *buf = out->data[0] + col;
227                     if (showwaves->split_channels)
228                         buf += ch*ch_height*linesize;
229                     av_assert0(col < outlink->w);
230                     showwaves->draw_sample(buf, ch_height, linesize, sample, &showwaves->buf_idy[ch], x);
231                     sum[ch] = 0;
232                 }
233                 col++;
234                 n = 0;
235             }
236         }
237     }
238
239     return push_frame(outlink);
240 }
241
242
243 static int request_frame(AVFilterLink *outlink)
244 {
245     ShowWavesContext *showwaves = outlink->src->priv;
246     AVFilterLink *inlink = outlink->src->inputs[0];
247     int ret;
248
249     ret = ff_request_frame(inlink);
250     if (ret == AVERROR_EOF && showwaves->outpicref) {
251         if (showwaves->single_pic)
252             push_single_pic(outlink);
253         else
254             push_frame(outlink);
255     }
256
257     return ret;
258 }
259
260 static void draw_sample_point(uint8_t *buf, int height, int linesize,
261                               int16_t sample, int16_t *prev_y, int intensity)
262 {
263     const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
264     if (h >= 0 && h < height)
265         buf[h * linesize] += intensity;
266 }
267
268 static void draw_sample_line(uint8_t *buf, int height, int linesize,
269                              int16_t sample, int16_t *prev_y, int intensity)
270 {
271     int k;
272     const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
273     int start   = height/2;
274     int end     = av_clip(h, 0, height-1);
275     if (start > end)
276         FFSWAP(int16_t, start, end);
277     for (k = start; k < end; k++)
278         buf[k * linesize] += intensity;
279 }
280
281 static void draw_sample_p2p(uint8_t *buf, int height, int linesize,
282                             int16_t sample, int16_t *prev_y, int intensity)
283 {
284     int k;
285     const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
286     if (h >= 0 && h < height) {
287         buf[h * linesize] += intensity;
288         if (*prev_y && h != *prev_y) {
289             int start = *prev_y;
290             int end = av_clip(h, 0, height-1);
291             if (start > end)
292                 FFSWAP(int16_t, start, end);
293             for (k = start + 1; k < end; k++)
294                 buf[k * linesize] += intensity;
295         }
296     }
297     *prev_y = h;
298 }
299
300 static void draw_sample_cline(uint8_t *buf, int height, int linesize,
301                               int16_t sample, int16_t *prev_y, int intensity)
302 {
303     int k;
304     const int h     = av_rescale(abs(sample), height, INT16_MAX);
305     const int start = (height - h) / 2;
306     const int end   = start + h;
307     for (k = start; k < end; k++)
308         buf[k * linesize] += intensity;
309 }
310
311 static int alloc_out_frame(ShowWavesContext *showwaves, const int16_t *p,
312                            const AVFilterLink *inlink, AVFilterLink *outlink,
313                            const AVFrame *in)
314 {
315     if (!showwaves->outpicref) {
316         int j;
317         AVFrame *out = showwaves->outpicref =
318             ff_get_video_buffer(outlink, outlink->w, outlink->h);
319         if (!out)
320             return AVERROR(ENOMEM);
321         out->width  = outlink->w;
322         out->height = outlink->h;
323         out->pts = in->pts + av_rescale_q((p - (int16_t *)in->data[0]) / inlink->channels,
324                                           av_make_q(1, inlink->sample_rate),
325                                           outlink->time_base);
326         for (j = 0; j < outlink->h; j++)
327             memset(out->data[0] + j*out->linesize[0], 0, outlink->w);
328     }
329     return 0;
330 }
331
332 static av_cold int init(AVFilterContext *ctx)
333 {
334     ShowWavesContext *showwaves = ctx->priv;
335
336     if (!strcmp(ctx->filter->name, "showwavespic")) {
337         showwaves->single_pic = 1;
338         showwaves->mode = MODE_CENTERED_LINE;
339     }
340
341     switch (showwaves->mode) {
342     case MODE_POINT:         showwaves->draw_sample = draw_sample_point; break;
343     case MODE_LINE:          showwaves->draw_sample = draw_sample_line;  break;
344     case MODE_P2P:           showwaves->draw_sample = draw_sample_p2p;   break;
345     case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline; break;
346     default:
347         return AVERROR_BUG;
348     }
349     return 0;
350 }
351
352 #if CONFIG_SHOWWAVES_FILTER
353
354 static int showwaves_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
355 {
356     AVFilterContext *ctx = inlink->dst;
357     AVFilterLink *outlink = ctx->outputs[0];
358     ShowWavesContext *showwaves = ctx->priv;
359     const int nb_samples = insamples->nb_samples;
360     AVFrame *outpicref = showwaves->outpicref;
361     int16_t *p = (int16_t *)insamples->data[0];
362     int nb_channels = inlink->channels;
363     int i, j, ret = 0;
364     const int n = showwaves->n;
365     const int x = 255 / ((showwaves->split_channels ? 1 : nb_channels) * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
366     const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
367
368     /* draw data in the buffer */
369     for (i = 0; i < nb_samples; i++) {
370
371         ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
372         if (ret < 0)
373             goto end;
374         outpicref = showwaves->outpicref;
375
376         for (j = 0; j < nb_channels; j++) {
377             uint8_t *buf = outpicref->data[0] + showwaves->buf_idx;
378             const int linesize = outpicref->linesize[0];
379             if (showwaves->split_channels)
380                 buf += j*ch_height*linesize;
381             showwaves->draw_sample(buf, ch_height, linesize, *p++,
382                                    &showwaves->buf_idy[j], x);
383         }
384
385         showwaves->sample_count_mod++;
386         if (showwaves->sample_count_mod == n) {
387             showwaves->sample_count_mod = 0;
388             showwaves->buf_idx++;
389         }
390         if (showwaves->buf_idx == showwaves->w)
391             if ((ret = push_frame(outlink)) < 0)
392                 break;
393         outpicref = showwaves->outpicref;
394     }
395
396 end:
397     av_frame_free(&insamples);
398     return ret;
399 }
400
401 static const AVFilterPad showwaves_inputs[] = {
402     {
403         .name         = "default",
404         .type         = AVMEDIA_TYPE_AUDIO,
405         .filter_frame = showwaves_filter_frame,
406     },
407     { NULL }
408 };
409
410 static const AVFilterPad showwaves_outputs[] = {
411     {
412         .name          = "default",
413         .type          = AVMEDIA_TYPE_VIDEO,
414         .config_props  = config_output,
415         .request_frame = request_frame,
416     },
417     { NULL }
418 };
419
420 AVFilter ff_avf_showwaves = {
421     .name          = "showwaves",
422     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
423     .init          = init,
424     .uninit        = uninit,
425     .query_formats = query_formats,
426     .priv_size     = sizeof(ShowWavesContext),
427     .inputs        = showwaves_inputs,
428     .outputs       = showwaves_outputs,
429     .priv_class    = &showwaves_class,
430 };
431
432 #endif // CONFIG_SHOWWAVES_FILTER
433
434 #if CONFIG_SHOWWAVESPIC_FILTER
435
436 #define OFFSET(x) offsetof(ShowWavesContext, x)
437 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
438
439 static const AVOption showwavespic_options[] = {
440     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
441     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
442     { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
443     { NULL }
444 };
445
446 AVFILTER_DEFINE_CLASS(showwavespic);
447
448 static int showwavespic_config_input(AVFilterLink *inlink)
449 {
450     AVFilterContext *ctx = inlink->dst;
451     ShowWavesContext *showwaves = ctx->priv;
452
453     if (showwaves->single_pic) {
454         showwaves->sum = av_mallocz_array(inlink->channels, sizeof(*showwaves->sum));
455         if (!showwaves->sum)
456             return AVERROR(ENOMEM);
457     }
458
459     return 0;
460 }
461
462 static int showwavespic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
463 {
464     AVFilterContext *ctx = inlink->dst;
465     AVFilterLink *outlink = ctx->outputs[0];
466     ShowWavesContext *showwaves = ctx->priv;
467     int16_t *p = (int16_t *)insamples->data[0];
468     int ret = 0;
469
470     if (showwaves->single_pic) {
471         struct frame_node *f;
472
473         ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
474         if (ret < 0)
475             goto end;
476
477         /* queue the audio frame */
478         f = av_malloc(sizeof(*f));
479         if (!f) {
480             ret = AVERROR(ENOMEM);
481             goto end;
482         }
483         f->frame = insamples;
484         f->next = NULL;
485         if (!showwaves->last_frame) {
486             showwaves->audio_frames =
487             showwaves->last_frame   = f;
488         } else {
489             showwaves->last_frame->next = f;
490             showwaves->last_frame = f;
491         }
492         showwaves->total_samples += insamples->nb_samples;
493
494         return 0;
495     }
496
497 end:
498     av_frame_free(&insamples);
499     return ret;
500 }
501
502 static const AVFilterPad showwavespic_inputs[] = {
503     {
504         .name         = "default",
505         .type         = AVMEDIA_TYPE_AUDIO,
506         .config_props = showwavespic_config_input,
507         .filter_frame = showwavespic_filter_frame,
508     },
509     { NULL }
510 };
511
512 static const AVFilterPad showwavespic_outputs[] = {
513     {
514         .name          = "default",
515         .type          = AVMEDIA_TYPE_VIDEO,
516         .config_props  = config_output,
517         .request_frame = request_frame,
518     },
519     { NULL }
520 };
521
522 AVFilter ff_avf_showwavespic = {
523     .name          = "showwavespic",
524     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to a video output single picture."),
525     .init          = init,
526     .uninit        = uninit,
527     .query_formats = query_formats,
528     .priv_size     = sizeof(ShowWavesContext),
529     .inputs        = showwavespic_inputs,
530     .outputs       = showwavespic_outputs,
531     .priv_class    = &showwavespic_class,
532 };
533
534 #endif // CONFIG_SHOWWAVESPIC_FILTER