2 * Copyright (c) 2008 Vitor Sessak
4 * This file is part of Libav.
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.
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.
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
23 * memory buffer source filter
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"
38 #include "buffersrc.h"
43 typedef struct BufferSourceContext {
46 AVRational time_base; ///< time_base to set in the output link
50 enum AVPixelFormat pix_fmt;
52 AVRational pixel_aspect;
56 enum AVSampleFormat sample_fmt;
58 uint64_t channel_layout;
59 char *channel_layout_str;
62 } BufferSourceContext;
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);\
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);\
77 int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
82 if (!(copy = av_frame_alloc()))
83 return AVERROR(ENOMEM);
84 ret = av_frame_ref(copy, frame);
86 ret = av_buffersrc_add_frame(ctx, copy);
92 int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx,
95 BufferSourceContext *s = ctx->priv;
103 return AVERROR(EINVAL);
105 refcounted = !!frame->buf[0];
107 switch (ctx->outputs[0]->type) {
108 case AVMEDIA_TYPE_VIDEO:
109 CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
112 case AVMEDIA_TYPE_AUDIO:
113 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
117 return AVERROR(EINVAL);
120 if (!av_fifo_space(s->fifo) &&
121 (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
125 if (!(copy = av_frame_alloc()))
126 return AVERROR(ENOMEM);
129 av_frame_move_ref(copy, frame);
131 ret = av_frame_ref(copy, frame);
133 av_frame_free(©);
138 if ((ret = av_fifo_generic_write(s->fifo, ©, sizeof(copy), NULL)) < 0) {
140 av_frame_move_ref(frame, copy);
141 av_frame_free(©);
148 #if FF_API_AVFILTERBUFFER
149 FF_DISABLE_DEPRECATION_WARNINGS
150 static void compat_free_buffer(void *opaque, uint8_t *data)
152 AVFilterBufferRef *buf = opaque;
153 avfilter_unref_buffer(buf);
156 static void compat_unref_buffer(void *opaque, uint8_t *data)
158 AVBufferRef *buf = opaque;
159 av_buffer_unref(&buf);
162 int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
164 BufferSourceContext *s = ctx->priv;
165 AVFrame *frame = NULL;
166 AVBufferRef *dummy_buf = NULL;
167 int ret = 0, planes, i;
173 return AVERROR(EINVAL);
175 frame = av_frame_alloc();
177 return AVERROR(ENOMEM);
179 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf, 0);
181 ret = AVERROR(ENOMEM);
185 if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
188 #define WRAP_PLANE(ref_out, data, data_size) \
190 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
192 ret = AVERROR(ENOMEM); \
195 ref_out = av_buffer_create(data, data_size, compat_unref_buffer, \
198 av_buffer_unref(&dummy_ref); \
199 av_frame_unref(frame); \
200 ret = AVERROR(ENOMEM); \
205 if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
206 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
208 planes = av_pix_fmt_count_planes(frame->format);
209 if (!desc || planes <= 0) {
210 ret = AVERROR(EINVAL);
214 for (i = 0; i < planes; i++) {
215 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
216 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
218 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
221 int planar = av_sample_fmt_is_planar(frame->format);
222 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
224 planes = planar ? channels : 1;
226 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
227 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
228 frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
229 frame->nb_extended_buf);
230 if (!frame->extended_buf) {
231 ret = AVERROR(ENOMEM);
236 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
237 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
239 for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
240 WRAP_PLANE(frame->extended_buf[i],
241 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
245 ret = av_buffersrc_add_frame(ctx, frame);
248 av_buffer_unref(&dummy_buf);
249 av_frame_free(&frame);
253 FF_ENABLE_DEPRECATION_WARNINGS
256 static av_cold int init_video(AVFilterContext *ctx)
258 BufferSourceContext *c = ctx->priv;
260 if (!c->pix_fmt_str || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
261 av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
262 return AVERROR(EINVAL);
265 if ((c->pix_fmt = av_get_pix_fmt(c->pix_fmt_str)) == AV_PIX_FMT_NONE) {
267 c->pix_fmt = strtol(c->pix_fmt_str, &tail, 10);
268 if (*tail || c->pix_fmt < 0 || !av_pix_fmt_desc_get(c->pix_fmt)) {
269 av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", c->pix_fmt_str);
270 return AVERROR(EINVAL);
274 if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
275 return AVERROR(ENOMEM);
277 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));
281 #define OFFSET(x) offsetof(BufferSourceContext, x)
282 #define A AV_OPT_FLAG_AUDIO_PARAM
283 #define V AV_OPT_FLAG_VIDEO_PARAM
285 static const AVOption video_options[] = {
286 { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
287 { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
288 { "pix_fmt", NULL, OFFSET(pix_fmt_str), AV_OPT_TYPE_STRING, .flags = V },
289 #if FF_API_OLD_FILTER_OPTS
290 /* those 4 are for compatibility with the old option passing system where each filter
291 * did its own parsing */
292 { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
293 { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
294 { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
295 { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
297 { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
298 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
302 static const AVClass buffer_class = {
303 .class_name = "buffer source",
304 .item_name = av_default_item_name,
305 .option = video_options,
306 .version = LIBAVUTIL_VERSION_INT,
309 static const AVOption audio_options[] = {
310 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
311 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
312 { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
313 { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
317 static const AVClass abuffer_class = {
318 .class_name = "abuffer source",
319 .item_name = av_default_item_name,
320 .option = audio_options,
321 .version = LIBAVUTIL_VERSION_INT,
324 static av_cold int init_audio(AVFilterContext *ctx)
326 BufferSourceContext *s = ctx->priv;
329 s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
330 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
331 av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
333 return AVERROR(EINVAL);
336 s->channel_layout = av_get_channel_layout(s->channel_layout_str);
337 if (!s->channel_layout) {
338 av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
339 s->channel_layout_str);
340 return AVERROR(EINVAL);
343 if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
344 return AVERROR(ENOMEM);
346 if (!s->time_base.num)
347 s->time_base = (AVRational){1, s->sample_rate};
349 av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
350 "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
351 s->sample_rate, s->channel_layout_str);
356 static av_cold void uninit(AVFilterContext *ctx)
358 BufferSourceContext *s = ctx->priv;
359 while (s->fifo && av_fifo_size(s->fifo)) {
361 av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
362 av_frame_free(&frame);
364 av_fifo_free(s->fifo);
368 static int query_formats(AVFilterContext *ctx)
370 BufferSourceContext *c = ctx->priv;
371 AVFilterChannelLayouts *channel_layouts = NULL;
372 AVFilterFormats *formats = NULL;
373 AVFilterFormats *samplerates = NULL;
375 switch (ctx->outputs[0]->type) {
376 case AVMEDIA_TYPE_VIDEO:
377 ff_add_format(&formats, c->pix_fmt);
378 ff_set_common_formats(ctx, formats);
380 case AVMEDIA_TYPE_AUDIO:
381 ff_add_format(&formats, c->sample_fmt);
382 ff_set_common_formats(ctx, formats);
384 ff_add_format(&samplerates, c->sample_rate);
385 ff_set_common_samplerates(ctx, samplerates);
387 ff_add_channel_layout(&channel_layouts, c->channel_layout);
388 ff_set_common_channel_layouts(ctx, channel_layouts);
391 return AVERROR(EINVAL);
397 static int config_props(AVFilterLink *link)
399 BufferSourceContext *c = link->src->priv;
401 switch (link->type) {
402 case AVMEDIA_TYPE_VIDEO:
405 link->sample_aspect_ratio = c->pixel_aspect;
407 case AVMEDIA_TYPE_AUDIO:
408 link->channel_layout = c->channel_layout;
409 link->sample_rate = c->sample_rate;
412 return AVERROR(EINVAL);
415 link->time_base = c->time_base;
419 static int request_frame(AVFilterLink *link)
421 BufferSourceContext *c = link->src->priv;
425 if (!av_fifo_size(c->fifo)) {
428 return AVERROR(EAGAIN);
430 av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
432 ff_filter_frame(link, frame);
437 static int poll_frame(AVFilterLink *link)
439 BufferSourceContext *c = link->src->priv;
440 int size = av_fifo_size(c->fifo);
443 return size/sizeof(AVFrame*);
446 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
449 .type = AVMEDIA_TYPE_VIDEO,
450 .request_frame = request_frame,
451 .poll_frame = poll_frame,
452 .config_props = config_props,
457 AVFilter ff_vsrc_buffer = {
459 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
460 .priv_size = sizeof(BufferSourceContext),
461 .priv_class = &buffer_class,
462 .query_formats = query_formats,
468 .outputs = avfilter_vsrc_buffer_outputs,
471 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
474 .type = AVMEDIA_TYPE_AUDIO,
475 .request_frame = request_frame,
476 .poll_frame = poll_frame,
477 .config_props = config_props,
482 AVFilter ff_asrc_abuffer = {
484 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
485 .priv_size = sizeof(BufferSourceContext),
486 .priv_class = &abuffer_class,
487 .query_formats = query_formats,
493 .outputs = avfilter_asrc_abuffer_outputs,