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