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