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/opt.h"
34 #include "libavutil/samplefmt.h"
37 #include "buffersrc.h"
45 AVRational time_base; ///< time_base to set in the output link
49 enum AVPixelFormat pix_fmt;
51 AVRational pixel_aspect;
55 enum AVSampleFormat sample_fmt;
57 uint64_t channel_layout;
58 char *channel_layout_str;
61 } BufferSourceContext;
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);\
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);\
76 int av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
81 if (!(copy = av_frame_alloc()))
82 return AVERROR(ENOMEM);
83 ret = av_frame_ref(copy, frame);
85 ret = av_buffersrc_add_frame(ctx, copy);
91 int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx,
94 BufferSourceContext *s = ctx->priv;
102 return AVERROR(EINVAL);
104 switch (ctx->outputs[0]->type) {
105 case AVMEDIA_TYPE_VIDEO:
106 CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
109 case AVMEDIA_TYPE_AUDIO:
110 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
114 return AVERROR(EINVAL);
117 if (!av_fifo_space(s->fifo) &&
118 (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
122 if (!(copy = av_frame_alloc()))
123 return AVERROR(ENOMEM);
124 av_frame_move_ref(copy, frame);
126 if ((ret = av_fifo_generic_write(s->fifo, ©, sizeof(copy), NULL)) < 0) {
127 av_frame_move_ref(frame, copy);
128 av_frame_free(©);
135 #if FF_API_AVFILTERBUFFER
136 static void compat_free_buffer(void *opaque, uint8_t *data)
138 AVFilterBufferRef *buf = opaque;
139 avfilter_unref_buffer(buf);
142 static void compat_unref_buffer(void *opaque, uint8_t *data)
144 AVBufferRef *buf = opaque;
145 av_buffer_unref(&buf);
148 int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
150 BufferSourceContext *s = ctx->priv;
151 AVFrame *frame = NULL;
152 AVBufferRef *dummy_buf = NULL;
153 int ret = 0, planes, i;
159 return AVERROR(EINVAL);
161 frame = av_frame_alloc();
163 return AVERROR(ENOMEM);
165 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf, 0);
167 ret = AVERROR(ENOMEM);
171 if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
174 #define WRAP_PLANE(ref_out, data, data_size) \
176 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
178 ret = AVERROR(ENOMEM); \
181 ref_out = av_buffer_create(data, data_size, compat_unref_buffer, \
184 av_frame_unref(frame); \
185 ret = AVERROR(ENOMEM); \
190 if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
191 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
193 planes = av_pix_fmt_count_planes(frame->format);
194 if (!desc || planes <= 0) {
195 ret = AVERROR(EINVAL);
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];
203 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
206 int planar = av_sample_fmt_is_planar(frame->format);
207 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
209 planes = planar ? channels : 1;
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);
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]);
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)],
230 ret = av_buffersrc_add_frame(ctx, frame);
233 av_buffer_unref(&dummy_buf);
234 av_frame_free(&frame);
240 static av_cold int init_video(AVFilterContext *ctx)
242 BufferSourceContext *c = ctx->priv;
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);
249 if ((c->pix_fmt = av_get_pix_fmt(c->pix_fmt_str)) == AV_PIX_FMT_NONE) {
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);
258 if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
259 return AVERROR(ENOMEM);
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));
265 #define OFFSET(x) offsetof(BufferSourceContext, x)
266 #define A AV_OPT_FLAG_AUDIO_PARAM
267 #define V AV_OPT_FLAG_VIDEO_PARAM
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 },
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 },
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,
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 },
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,
308 static av_cold int init_audio(AVFilterContext *ctx)
310 BufferSourceContext *s = ctx->priv;
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",
317 return AVERROR(EINVAL);
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);
327 if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
328 return AVERROR(ENOMEM);
330 if (!s->time_base.num)
331 s->time_base = (AVRational){1, s->sample_rate};
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);
340 static av_cold void uninit(AVFilterContext *ctx)
342 BufferSourceContext *s = ctx->priv;
343 while (s->fifo && av_fifo_size(s->fifo)) {
345 av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
346 av_frame_free(&frame);
348 av_fifo_free(s->fifo);
352 static int query_formats(AVFilterContext *ctx)
354 BufferSourceContext *c = ctx->priv;
355 AVFilterChannelLayouts *channel_layouts = NULL;
356 AVFilterFormats *formats = NULL;
357 AVFilterFormats *samplerates = NULL;
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);
364 case AVMEDIA_TYPE_AUDIO:
365 ff_add_format(&formats, c->sample_fmt);
366 ff_set_common_formats(ctx, formats);
368 ff_add_format(&samplerates, c->sample_rate);
369 ff_set_common_samplerates(ctx, samplerates);
371 ff_add_channel_layout(&channel_layouts, c->channel_layout);
372 ff_set_common_channel_layouts(ctx, channel_layouts);
375 return AVERROR(EINVAL);
381 static int config_props(AVFilterLink *link)
383 BufferSourceContext *c = link->src->priv;
385 switch (link->type) {
386 case AVMEDIA_TYPE_VIDEO:
389 link->sample_aspect_ratio = c->pixel_aspect;
391 case AVMEDIA_TYPE_AUDIO:
392 link->channel_layout = c->channel_layout;
393 link->sample_rate = c->sample_rate;
396 return AVERROR(EINVAL);
399 link->time_base = c->time_base;
403 static int request_frame(AVFilterLink *link)
405 BufferSourceContext *c = link->src->priv;
409 if (!av_fifo_size(c->fifo)) {
412 return AVERROR(EAGAIN);
414 av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
416 ff_filter_frame(link, frame);
421 static int poll_frame(AVFilterLink *link)
423 BufferSourceContext *c = link->src->priv;
424 int size = av_fifo_size(c->fifo);
427 return size/sizeof(AVFrame*);
430 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
433 .type = AVMEDIA_TYPE_VIDEO,
434 .request_frame = request_frame,
435 .poll_frame = poll_frame,
436 .config_props = config_props,
441 AVFilter avfilter_vsrc_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,
452 .outputs = avfilter_vsrc_buffer_outputs,
455 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
458 .type = AVMEDIA_TYPE_AUDIO,
459 .request_frame = request_frame,
460 .poll_frame = poll_frame,
461 .config_props = config_props,
466 AVFilter avfilter_asrc_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,
477 .outputs = avfilter_asrc_abuffer_outputs,