]> git.sesse.net Git - ffmpeg/blob - libavfilter/buffersrc.c
avfilter/vf_waveform: support envelope for all filters
[ffmpeg] / libavfilter / buffersrc.c
1 /*
2  * Copyright (c) 2008 Vitor Sessak
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  * memory buffer source filter
24  */
25
26 #include <float.h>
27
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/common.h"
30 #include "libavutil/fifo.h"
31 #include "libavutil/frame.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/samplefmt.h"
36 #include "audio.h"
37 #include "avfilter.h"
38 #include "buffersrc.h"
39 #include "formats.h"
40 #include "internal.h"
41 #include "video.h"
42 #include "avcodec.h"
43
44 typedef struct BufferSourceContext {
45     const AVClass    *class;
46     AVFifoBuffer     *fifo;
47     AVRational        time_base;     ///< time_base to set in the output link
48     AVRational        frame_rate;    ///< frame_rate to set in the output link
49     unsigned          nb_failed_requests;
50     unsigned          warning_limit;
51
52     /* video only */
53     int               w, h;
54     enum AVPixelFormat  pix_fmt;
55     AVRational        pixel_aspect;
56     char              *sws_param;
57
58     /* audio only */
59     int sample_rate;
60     enum AVSampleFormat sample_fmt;
61     int channels;
62     uint64_t channel_layout;
63     char    *channel_layout_str;
64
65     int eof;
66 } BufferSourceContext;
67
68 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
69     if (c->w != width || c->h != height || c->pix_fmt != format) {\
70         av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
71     }
72
73 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, ch_count, format)\
74     if (c->sample_fmt != format || c->sample_rate != srate ||\
75         c->channel_layout != ch_layout || c->channels != ch_count) {\
76         av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
77         return AVERROR(EINVAL);\
78     }
79
80 int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
81 {
82     return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
83                                         AV_BUFFERSRC_FLAG_KEEP_REF);
84 }
85
86 int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
87 {
88     return av_buffersrc_add_frame_flags(ctx, frame, 0);
89 }
90
91 static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
92                                            AVFrame *frame, int flags);
93
94 int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
95 {
96     AVFrame *copy = NULL;
97     int ret = 0;
98
99     if (frame && frame->channel_layout &&
100         av_get_channel_layout_nb_channels(frame->channel_layout) != av_frame_get_channels(frame)) {
101         av_log(ctx, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
102         return AVERROR(EINVAL);
103     }
104
105     if (!(flags & AV_BUFFERSRC_FLAG_KEEP_REF) || !frame)
106         return av_buffersrc_add_frame_internal(ctx, frame, flags);
107
108     if (!(copy = av_frame_alloc()))
109         return AVERROR(ENOMEM);
110     ret = av_frame_ref(copy, frame);
111     if (ret >= 0)
112         ret = av_buffersrc_add_frame_internal(ctx, copy, flags);
113
114     av_frame_free(&copy);
115     return ret;
116 }
117
118 static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
119                                            AVFrame *frame, int flags)
120 {
121     BufferSourceContext *s = ctx->priv;
122     AVFrame *copy;
123     int refcounted, ret;
124
125     s->nb_failed_requests = 0;
126
127     if (!frame) {
128         s->eof = 1;
129         return 0;
130     } else if (s->eof)
131         return AVERROR(EINVAL);
132
133     refcounted = !!frame->buf[0];
134
135     if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
136
137     switch (ctx->outputs[0]->type) {
138     case AVMEDIA_TYPE_VIDEO:
139         CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
140                                  frame->format);
141         break;
142     case AVMEDIA_TYPE_AUDIO:
143         /* For layouts unknown on input but known on link after negotiation. */
144         if (!frame->channel_layout)
145             frame->channel_layout = s->channel_layout;
146         CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
147                                  av_frame_get_channels(frame), frame->format);
148         break;
149     default:
150         return AVERROR(EINVAL);
151     }
152
153     }
154
155     if (!av_fifo_space(s->fifo) &&
156         (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
157                                          sizeof(copy))) < 0)
158         return ret;
159
160     if (!(copy = av_frame_alloc()))
161         return AVERROR(ENOMEM);
162
163     if (refcounted) {
164         av_frame_move_ref(copy, frame);
165     } else {
166         ret = av_frame_ref(copy, frame);
167         if (ret < 0) {
168             av_frame_free(&copy);
169             return ret;
170         }
171     }
172
173     if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
174         if (refcounted)
175             av_frame_move_ref(frame, copy);
176         av_frame_free(&copy);
177         return ret;
178     }
179
180     if ((flags & AV_BUFFERSRC_FLAG_PUSH))
181         if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
182             return ret;
183
184     return 0;
185 }
186
187 #if FF_API_AVFILTERBUFFER
188 FF_DISABLE_DEPRECATION_WARNINGS
189 static void compat_free_buffer(void *opaque, uint8_t *data)
190 {
191     AVFilterBufferRef *buf = opaque;
192     AV_NOWARN_DEPRECATED(
193     avfilter_unref_buffer(buf);
194     )
195 }
196
197 static void compat_unref_buffer(void *opaque, uint8_t *data)
198 {
199     AVBufferRef *buf = opaque;
200     AV_NOWARN_DEPRECATED(
201     av_buffer_unref(&buf);
202     )
203 }
204
205 int av_buffersrc_add_ref(AVFilterContext *ctx, AVFilterBufferRef *buf,
206                          int flags)
207 {
208     BufferSourceContext *s = ctx->priv;
209     AVFrame *frame = NULL;
210     AVBufferRef *dummy_buf = NULL;
211     int ret = 0, planes, i;
212
213     if (!buf) {
214         s->eof = 1;
215         return 0;
216     } else if (s->eof)
217         return AVERROR(EINVAL);
218
219     frame = av_frame_alloc();
220     if (!frame)
221         return AVERROR(ENOMEM);
222
223     dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf,
224                                  (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY);
225     if (!dummy_buf) {
226         ret = AVERROR(ENOMEM);
227         goto fail;
228     }
229
230     AV_NOWARN_DEPRECATED(
231     if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
232         goto fail;
233     )
234
235 #define WRAP_PLANE(ref_out, data, data_size)                            \
236 do {                                                                    \
237     AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf);                  \
238     if (!dummy_ref) {                                                   \
239         ret = AVERROR(ENOMEM);                                          \
240         goto fail;                                                      \
241     }                                                                   \
242     ref_out = av_buffer_create(data, data_size, compat_unref_buffer,    \
243                                dummy_ref, (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY);                           \
244     if (!ref_out) {                                                     \
245         av_buffer_unref(&dummy_ref);                                    \
246         av_frame_unref(frame);                                          \
247         ret = AVERROR(ENOMEM);                                          \
248         goto fail;                                                      \
249     }                                                                   \
250 } while (0)
251
252     if (ctx->outputs[0]->type  == AVMEDIA_TYPE_VIDEO) {
253         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
254
255         planes = av_pix_fmt_count_planes(frame->format);
256         if (!desc || planes <= 0) {
257             ret = AVERROR(EINVAL);
258             goto fail;
259         }
260
261         for (i = 0; i < planes; i++) {
262             int v_shift    = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
263             int plane_size = (frame->height >> v_shift) * frame->linesize[i];
264
265             WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
266         }
267     } else {
268         int planar = av_sample_fmt_is_planar(frame->format);
269         int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
270
271         planes = planar ? channels : 1;
272
273         if (planes > FF_ARRAY_ELEMS(frame->buf)) {
274             frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
275             frame->extended_buf = av_mallocz_array(sizeof(*frame->extended_buf),
276                                              frame->nb_extended_buf);
277             if (!frame->extended_buf) {
278                 ret = AVERROR(ENOMEM);
279                 goto fail;
280             }
281         }
282
283         for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
284             WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
285
286         for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
287             WRAP_PLANE(frame->extended_buf[i],
288                        frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
289                        frame->linesize[0]);
290     }
291
292     ret = av_buffersrc_add_frame_flags(ctx, frame, flags);
293
294 fail:
295     av_buffer_unref(&dummy_buf);
296     av_frame_free(&frame);
297
298     return ret;
299 }
300 FF_ENABLE_DEPRECATION_WARNINGS
301
302 int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
303 {
304     return av_buffersrc_add_ref(ctx, buf, 0);
305 }
306 #endif
307
308 static av_cold int init_video(AVFilterContext *ctx)
309 {
310     BufferSourceContext *c = ctx->priv;
311
312     if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
313         av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
314         return AVERROR(EINVAL);
315     }
316
317     if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
318         return AVERROR(ENOMEM);
319
320     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d sws_param:%s\n",
321            c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
322            c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
323            c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
324     c->warning_limit = 100;
325     return 0;
326 }
327
328 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
329 {
330     return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
331 }
332
333 #define OFFSET(x) offsetof(BufferSourceContext, x)
334 #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
335 #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
336
337 static const AVOption buffer_options[] = {
338     { "width",         NULL,                     OFFSET(w),                AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
339     { "video_size",    NULL,                     OFFSET(w),                AV_OPT_TYPE_IMAGE_SIZE,                .flags = V },
340     { "height",        NULL,                     OFFSET(h),                AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
341     { "pix_fmt",       NULL,                     OFFSET(pix_fmt),          AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_NONE }, .min = AV_PIX_FMT_NONE, .max = INT_MAX, .flags = V },
342 #if FF_API_OLD_FILTER_OPTS
343     /* those 4 are for compatibility with the old option passing system where each filter
344      * did its own parsing */
345     { "time_base_num", "deprecated, do not use", OFFSET(time_base.num),    AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
346     { "time_base_den", "deprecated, do not use", OFFSET(time_base.den),    AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
347     { "sar_num",       "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
348     { "sar_den",       "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
349 #endif
350     { "sar",           "sample aspect ratio",    OFFSET(pixel_aspect),     AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
351     { "pixel_aspect",  "sample aspect ratio",    OFFSET(pixel_aspect),     AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
352     { "time_base",     NULL,                     OFFSET(time_base),        AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
353     { "frame_rate",    NULL,                     OFFSET(frame_rate),       AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
354     { "sws_param",     NULL,                     OFFSET(sws_param),        AV_OPT_TYPE_STRING,                    .flags = V },
355     { NULL },
356 };
357
358 AVFILTER_DEFINE_CLASS(buffer);
359
360 static const AVOption abuffer_options[] = {
361     { "time_base",      NULL, OFFSET(time_base),           AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
362     { "sample_rate",    NULL, OFFSET(sample_rate),         AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, A },
363     { "sample_fmt",     NULL, OFFSET(sample_fmt),          AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_NONE }, .min = AV_SAMPLE_FMT_NONE, .max = INT_MAX, .flags = A },
364     { "channel_layout", NULL, OFFSET(channel_layout_str),  AV_OPT_TYPE_STRING,             .flags = A },
365     { "channels",       NULL, OFFSET(channels),            AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, A },
366     { NULL },
367 };
368
369 AVFILTER_DEFINE_CLASS(abuffer);
370
371 static av_cold int init_audio(AVFilterContext *ctx)
372 {
373     BufferSourceContext *s = ctx->priv;
374     int ret = 0;
375
376     if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
377         av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
378         return AVERROR(EINVAL);
379     }
380
381     if (s->channel_layout_str) {
382         int n;
383
384         s->channel_layout = av_get_channel_layout(s->channel_layout_str);
385         if (!s->channel_layout) {
386             av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
387                    s->channel_layout_str);
388             return AVERROR(EINVAL);
389         }
390         n = av_get_channel_layout_nb_channels(s->channel_layout);
391         if (s->channels) {
392             if (n != s->channels) {
393                 av_log(ctx, AV_LOG_ERROR,
394                        "Mismatching channel count %d and layout '%s' "
395                        "(%d channels)\n",
396                        s->channels, s->channel_layout_str, n);
397                 return AVERROR(EINVAL);
398             }
399         }
400         s->channels = n;
401     } else if (!s->channels) {
402         av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
403                                   "channel layout specified\n");
404         return AVERROR(EINVAL);
405     }
406
407     if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
408         return AVERROR(ENOMEM);
409
410     if (!s->time_base.num)
411         s->time_base = (AVRational){1, s->sample_rate};
412
413     av_log(ctx, AV_LOG_VERBOSE,
414            "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
415            s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
416            s->sample_rate, s->channel_layout_str);
417     s->warning_limit = 100;
418
419     return ret;
420 }
421
422 static av_cold void uninit(AVFilterContext *ctx)
423 {
424     BufferSourceContext *s = ctx->priv;
425     while (s->fifo && av_fifo_size(s->fifo)) {
426         AVFrame *frame;
427         av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
428         av_frame_free(&frame);
429     }
430     av_fifo_freep(&s->fifo);
431 }
432
433 static int query_formats(AVFilterContext *ctx)
434 {
435     BufferSourceContext *c = ctx->priv;
436     AVFilterChannelLayouts *channel_layouts = NULL;
437     AVFilterFormats *formats = NULL;
438     AVFilterFormats *samplerates = NULL;
439
440     switch (ctx->outputs[0]->type) {
441     case AVMEDIA_TYPE_VIDEO:
442         ff_add_format(&formats, c->pix_fmt);
443         ff_set_common_formats(ctx, formats);
444         break;
445     case AVMEDIA_TYPE_AUDIO:
446         ff_add_format(&formats,           c->sample_fmt);
447         ff_set_common_formats(ctx, formats);
448
449         ff_add_format(&samplerates,       c->sample_rate);
450         ff_set_common_samplerates(ctx, samplerates);
451
452         ff_add_channel_layout(&channel_layouts,
453                               c->channel_layout ? c->channel_layout :
454                               FF_COUNT2LAYOUT(c->channels));
455         ff_set_common_channel_layouts(ctx, channel_layouts);
456         break;
457     default:
458         return AVERROR(EINVAL);
459     }
460
461     return 0;
462 }
463
464 static int config_props(AVFilterLink *link)
465 {
466     BufferSourceContext *c = link->src->priv;
467
468     switch (link->type) {
469     case AVMEDIA_TYPE_VIDEO:
470         link->w = c->w;
471         link->h = c->h;
472         link->sample_aspect_ratio = c->pixel_aspect;
473         break;
474     case AVMEDIA_TYPE_AUDIO:
475         if (!c->channel_layout)
476             c->channel_layout = link->channel_layout;
477         break;
478     default:
479         return AVERROR(EINVAL);
480     }
481
482     link->time_base = c->time_base;
483     link->frame_rate = c->frame_rate;
484     return 0;
485 }
486
487 static int request_frame(AVFilterLink *link)
488 {
489     BufferSourceContext *c = link->src->priv;
490     AVFrame *frame;
491
492     if (!av_fifo_size(c->fifo)) {
493         if (c->eof)
494             return AVERROR_EOF;
495         c->nb_failed_requests++;
496         return AVERROR(EAGAIN);
497     }
498     av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
499
500     return ff_filter_frame(link, frame);
501 }
502
503 static int poll_frame(AVFilterLink *link)
504 {
505     BufferSourceContext *c = link->src->priv;
506     int size = av_fifo_size(c->fifo);
507     if (!size && c->eof)
508         return AVERROR_EOF;
509     return size/sizeof(AVFrame*);
510 }
511
512 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
513     {
514         .name          = "default",
515         .type          = AVMEDIA_TYPE_VIDEO,
516         .request_frame = request_frame,
517         .poll_frame    = poll_frame,
518         .config_props  = config_props,
519     },
520     { NULL }
521 };
522
523 AVFilter ff_vsrc_buffer = {
524     .name      = "buffer",
525     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
526     .priv_size = sizeof(BufferSourceContext),
527     .query_formats = query_formats,
528
529     .init      = init_video,
530     .uninit    = uninit,
531
532     .inputs    = NULL,
533     .outputs   = avfilter_vsrc_buffer_outputs,
534     .priv_class = &buffer_class,
535 };
536
537 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
538     {
539         .name          = "default",
540         .type          = AVMEDIA_TYPE_AUDIO,
541         .request_frame = request_frame,
542         .poll_frame    = poll_frame,
543         .config_props  = config_props,
544     },
545     { NULL }
546 };
547
548 AVFilter ff_asrc_abuffer = {
549     .name          = "abuffer",
550     .description   = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
551     .priv_size     = sizeof(BufferSourceContext),
552     .query_formats = query_formats,
553
554     .init      = init_audio,
555     .uninit    = uninit,
556
557     .inputs    = NULL,
558     .outputs   = avfilter_asrc_abuffer_outputs,
559     .priv_class = &abuffer_class,
560 };