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