]> git.sesse.net Git - ffmpeg/blob - libavfilter/buffersrc.c
Disable deprecation warnings for cases where a replacement is available
[ffmpeg] / libavfilter / buffersrc.c
1 /*
2  * Copyright (c) 2008 Vitor Sessak
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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
43 typedef struct {
44     const AVClass    *class;
45     AVFifoBuffer     *fifo;
46     AVRational        time_base;     ///< time_base to set in the output link
47
48     /* video only */
49     int               h, w;
50     enum AVPixelFormat  pix_fmt;
51     char               *pix_fmt_str;
52     AVRational        pixel_aspect;
53
54     /* audio only */
55     int sample_rate;
56     enum AVSampleFormat sample_fmt;
57     char               *sample_fmt_str;
58     uint64_t channel_layout;
59     char    *channel_layout_str;
60
61     int eof;
62 } BufferSourceContext;
63
64 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
65     if (c->w != width || c->h != height || c->pix_fmt != format) {\
66         av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
67         return AVERROR(EINVAL);\
68     }
69
70 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
71     if (c->sample_fmt != format || c->sample_rate != srate ||\
72         c->channel_layout != ch_layout) {\
73         av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
74         return AVERROR(EINVAL);\
75     }
76
77 int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
78 {
79     AVFrame *copy;
80     int ret = 0;
81
82     if (!(copy = av_frame_alloc()))
83         return AVERROR(ENOMEM);
84     ret = av_frame_ref(copy, frame);
85     if (ret >= 0)
86         ret = av_buffersrc_add_frame(ctx, copy);
87
88     av_frame_free(&copy);
89     return ret;
90 }
91
92 int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx,
93                                                AVFrame *frame)
94 {
95     BufferSourceContext *s = ctx->priv;
96     AVFrame *copy;
97     int ret;
98
99     if (!frame) {
100         s->eof = 1;
101         return 0;
102     } else if (s->eof)
103         return AVERROR(EINVAL);
104
105     switch (ctx->outputs[0]->type) {
106     case AVMEDIA_TYPE_VIDEO:
107         CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
108                                  frame->format);
109         break;
110     case AVMEDIA_TYPE_AUDIO:
111         CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
112                                  frame->format);
113         break;
114     default:
115         return AVERROR(EINVAL);
116     }
117
118     if (!av_fifo_space(s->fifo) &&
119         (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
120                                          sizeof(copy))) < 0)
121         return ret;
122
123     if (!(copy = av_frame_alloc()))
124         return AVERROR(ENOMEM);
125     av_frame_move_ref(copy, frame);
126
127     if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
128         av_frame_move_ref(frame, copy);
129         av_frame_free(&copy);
130         return ret;
131     }
132
133     return 0;
134 }
135
136 #if FF_API_AVFILTERBUFFER
137 FF_DISABLE_DEPRECATION_WARNINGS
138 static void compat_free_buffer(void *opaque, uint8_t *data)
139 {
140     AVFilterBufferRef *buf = opaque;
141     avfilter_unref_buffer(buf);
142 }
143
144 static void compat_unref_buffer(void *opaque, uint8_t *data)
145 {
146     AVBufferRef *buf = opaque;
147     av_buffer_unref(&buf);
148 }
149
150 int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
151 {
152     BufferSourceContext *s = ctx->priv;
153     AVFrame *frame = NULL;
154     AVBufferRef *dummy_buf = NULL;
155     int ret = 0, planes, i;
156
157     if (!buf) {
158         s->eof = 1;
159         return 0;
160     } else if (s->eof)
161         return AVERROR(EINVAL);
162
163     frame = av_frame_alloc();
164     if (!frame)
165         return AVERROR(ENOMEM);
166
167     dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf, 0);
168     if (!dummy_buf) {
169         ret = AVERROR(ENOMEM);
170         goto fail;
171     }
172
173     if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
174         goto fail;
175
176 #define WRAP_PLANE(ref_out, data, data_size)                            \
177 do {                                                                    \
178     AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf);                  \
179     if (!dummy_ref) {                                                   \
180         ret = AVERROR(ENOMEM);                                          \
181         goto fail;                                                      \
182     }                                                                   \
183     ref_out = av_buffer_create(data, data_size, compat_unref_buffer,    \
184                                dummy_ref, 0);                           \
185     if (!ref_out) {                                                     \
186         av_frame_unref(frame);                                          \
187         ret = AVERROR(ENOMEM);                                          \
188         goto fail;                                                      \
189     }                                                                   \
190 } while (0)
191
192     if (ctx->outputs[0]->type  == AVMEDIA_TYPE_VIDEO) {
193         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
194
195         planes = av_pix_fmt_count_planes(frame->format);
196         if (!desc || planes <= 0) {
197             ret = AVERROR(EINVAL);
198             goto fail;
199         }
200
201         for (i = 0; i < planes; i++) {
202             int v_shift    = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
203             int plane_size = (frame->height >> v_shift) * frame->linesize[i];
204
205             WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
206         }
207     } else {
208         int planar = av_sample_fmt_is_planar(frame->format);
209         int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
210
211         planes = planar ? channels : 1;
212
213         if (planes > FF_ARRAY_ELEMS(frame->buf)) {
214             frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
215             frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
216                                              frame->nb_extended_buf);
217             if (!frame->extended_buf) {
218                 ret = AVERROR(ENOMEM);
219                 goto fail;
220             }
221         }
222
223         for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
224             WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
225
226         for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
227             WRAP_PLANE(frame->extended_buf[i],
228                        frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
229                        frame->linesize[0]);
230     }
231
232     ret = av_buffersrc_add_frame(ctx, frame);
233
234 fail:
235     av_buffer_unref(&dummy_buf);
236     av_frame_free(&frame);
237
238     return ret;
239 }
240 FF_ENABLE_DEPRECATION_WARNINGS
241 #endif
242
243 static av_cold int init_video(AVFilterContext *ctx)
244 {
245     BufferSourceContext *c = ctx->priv;
246
247     if (!c->pix_fmt_str || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
248         av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
249         return AVERROR(EINVAL);
250     }
251
252     if ((c->pix_fmt = av_get_pix_fmt(c->pix_fmt_str)) == AV_PIX_FMT_NONE) {
253         char *tail;
254         c->pix_fmt = strtol(c->pix_fmt_str, &tail, 10);
255         if (*tail || c->pix_fmt < 0 || c->pix_fmt >= AV_PIX_FMT_NB) {
256             av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", c->pix_fmt_str);
257             return AVERROR(EINVAL);
258         }
259     }
260
261     if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
262         return AVERROR(ENOMEM);
263
264     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s\n", c->w, c->h, av_get_pix_fmt_name(c->pix_fmt));
265     return 0;
266 }
267
268 #define OFFSET(x) offsetof(BufferSourceContext, x)
269 #define A AV_OPT_FLAG_AUDIO_PARAM
270 #define V AV_OPT_FLAG_VIDEO_PARAM
271
272 static const AVOption video_options[] = {
273     { "width",         NULL,                     OFFSET(w),                AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
274     { "height",        NULL,                     OFFSET(h),                AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
275     { "pix_fmt",       NULL,                     OFFSET(pix_fmt_str),      AV_OPT_TYPE_STRING,                    .flags = V },
276 #if FF_API_OLD_FILTER_OPTS
277     /* those 4 are for compatibility with the old option passing system where each filter
278      * did its own parsing */
279     { "time_base_num", "deprecated, do not use", OFFSET(time_base.num),    AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
280     { "time_base_den", "deprecated, do not use", OFFSET(time_base.den),    AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
281     { "sar_num",       "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
282     { "sar_den",       "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
283 #endif
284     { "sar",           "sample aspect ratio",    OFFSET(pixel_aspect),     AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
285     { "time_base",     NULL,                     OFFSET(time_base),        AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
286     { NULL },
287 };
288
289 static const AVClass buffer_class = {
290     .class_name = "buffer source",
291     .item_name  = av_default_item_name,
292     .option     = video_options,
293     .version    = LIBAVUTIL_VERSION_INT,
294 };
295
296 static const AVOption audio_options[] = {
297     { "time_base",      NULL, OFFSET(time_base),           AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
298     { "sample_rate",    NULL, OFFSET(sample_rate),         AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, A },
299     { "sample_fmt",     NULL, OFFSET(sample_fmt_str),      AV_OPT_TYPE_STRING,             .flags = A },
300     { "channel_layout", NULL, OFFSET(channel_layout_str),  AV_OPT_TYPE_STRING,             .flags = A },
301     { NULL },
302 };
303
304 static const AVClass abuffer_class = {
305     .class_name = "abuffer source",
306     .item_name  = av_default_item_name,
307     .option     = audio_options,
308     .version    = LIBAVUTIL_VERSION_INT,
309 };
310
311 static av_cold int init_audio(AVFilterContext *ctx)
312 {
313     BufferSourceContext *s = ctx->priv;
314     int ret = 0;
315
316     s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
317     if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
318         av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
319                s->sample_fmt_str);
320         return AVERROR(EINVAL);
321     }
322
323     s->channel_layout = av_get_channel_layout(s->channel_layout_str);
324     if (!s->channel_layout) {
325         av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
326                s->channel_layout_str);
327         return AVERROR(EINVAL);
328     }
329
330     if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
331         return AVERROR(ENOMEM);
332
333     if (!s->time_base.num)
334         s->time_base = (AVRational){1, s->sample_rate};
335
336     av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
337            "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
338            s->sample_rate, s->channel_layout_str);
339
340     return ret;
341 }
342
343 static av_cold void uninit(AVFilterContext *ctx)
344 {
345     BufferSourceContext *s = ctx->priv;
346     while (s->fifo && av_fifo_size(s->fifo)) {
347         AVFrame *frame;
348         av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
349         av_frame_free(&frame);
350     }
351     av_fifo_free(s->fifo);
352     s->fifo = NULL;
353 }
354
355 static int query_formats(AVFilterContext *ctx)
356 {
357     BufferSourceContext *c = ctx->priv;
358     AVFilterChannelLayouts *channel_layouts = NULL;
359     AVFilterFormats *formats = NULL;
360     AVFilterFormats *samplerates = NULL;
361
362     switch (ctx->outputs[0]->type) {
363     case AVMEDIA_TYPE_VIDEO:
364         ff_add_format(&formats, c->pix_fmt);
365         ff_set_common_formats(ctx, formats);
366         break;
367     case AVMEDIA_TYPE_AUDIO:
368         ff_add_format(&formats,           c->sample_fmt);
369         ff_set_common_formats(ctx, formats);
370
371         ff_add_format(&samplerates,       c->sample_rate);
372         ff_set_common_samplerates(ctx, samplerates);
373
374         ff_add_channel_layout(&channel_layouts, c->channel_layout);
375         ff_set_common_channel_layouts(ctx, channel_layouts);
376         break;
377     default:
378         return AVERROR(EINVAL);
379     }
380
381     return 0;
382 }
383
384 static int config_props(AVFilterLink *link)
385 {
386     BufferSourceContext *c = link->src->priv;
387
388     switch (link->type) {
389     case AVMEDIA_TYPE_VIDEO:
390         link->w = c->w;
391         link->h = c->h;
392         link->sample_aspect_ratio = c->pixel_aspect;
393         break;
394     case AVMEDIA_TYPE_AUDIO:
395         link->channel_layout = c->channel_layout;
396         link->sample_rate    = c->sample_rate;
397         break;
398     default:
399         return AVERROR(EINVAL);
400     }
401
402     link->time_base = c->time_base;
403     return 0;
404 }
405
406 static int request_frame(AVFilterLink *link)
407 {
408     BufferSourceContext *c = link->src->priv;
409     AVFrame *frame;
410     int ret = 0;
411
412     if (!av_fifo_size(c->fifo)) {
413         if (c->eof)
414             return AVERROR_EOF;
415         return AVERROR(EAGAIN);
416     }
417     av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
418
419     ff_filter_frame(link, frame);
420
421     return ret;
422 }
423
424 static int poll_frame(AVFilterLink *link)
425 {
426     BufferSourceContext *c = link->src->priv;
427     int size = av_fifo_size(c->fifo);
428     if (!size && c->eof)
429         return AVERROR_EOF;
430     return size/sizeof(AVFrame*);
431 }
432
433 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
434     {
435         .name          = "default",
436         .type          = AVMEDIA_TYPE_VIDEO,
437         .request_frame = request_frame,
438         .poll_frame    = poll_frame,
439         .config_props  = config_props,
440     },
441     { NULL }
442 };
443
444 AVFilter avfilter_vsrc_buffer = {
445     .name      = "buffer",
446     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
447     .priv_size = sizeof(BufferSourceContext),
448     .priv_class = &buffer_class,
449     .query_formats = query_formats,
450
451     .init      = init_video,
452     .uninit    = uninit,
453
454     .inputs    = NULL,
455     .outputs   = avfilter_vsrc_buffer_outputs,
456 };
457
458 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
459     {
460         .name          = "default",
461         .type          = AVMEDIA_TYPE_AUDIO,
462         .request_frame = request_frame,
463         .poll_frame    = poll_frame,
464         .config_props  = config_props,
465     },
466     { NULL }
467 };
468
469 AVFilter avfilter_asrc_abuffer = {
470     .name          = "abuffer",
471     .description   = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
472     .priv_size     = sizeof(BufferSourceContext),
473     .priv_class    = &abuffer_class,
474     .query_formats = query_formats,
475
476     .init      = init_audio,
477     .uninit    = uninit,
478
479     .inputs    = NULL,
480     .outputs   = avfilter_asrc_abuffer_outputs,
481 };