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
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/common.h"
28 #include "libavutil/fifo.h"
29 #include "libavutil/frame.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/samplefmt.h"
35 #include "buffersrc.h"
43 AVRational time_base; ///< time_base to set in the output link
47 enum AVPixelFormat pix_fmt;
48 AVRational pixel_aspect;
52 enum AVSampleFormat sample_fmt;
54 uint64_t channel_layout;
55 char *channel_layout_str;
58 } BufferSourceContext;
60 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
61 if (c->w != width || c->h != height || c->pix_fmt != format) {\
62 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
63 return AVERROR(EINVAL);\
66 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
67 if (c->sample_fmt != format || c->sample_rate != srate ||\
68 c->channel_layout != ch_layout) {\
69 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
70 return AVERROR(EINVAL);\
73 int av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
78 if (!(copy = av_frame_alloc()))
79 return AVERROR(ENOMEM);
80 ret = av_frame_ref(copy, frame);
82 ret = av_buffersrc_add_frame(ctx, copy);
88 int av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
90 BufferSourceContext *s = ctx->priv;
98 return AVERROR(EINVAL);
100 switch (ctx->outputs[0]->type) {
101 case AVMEDIA_TYPE_VIDEO:
102 CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
105 case AVMEDIA_TYPE_AUDIO:
106 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
110 return AVERROR(EINVAL);
113 if (!av_fifo_space(s->fifo) &&
114 (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
118 if (!(copy = av_frame_alloc()))
119 return AVERROR(ENOMEM);
120 av_frame_move_ref(copy, frame);
122 if ((ret = av_fifo_generic_write(s->fifo, ©, sizeof(copy), NULL)) < 0) {
123 av_frame_move_ref(frame, copy);
124 av_frame_free(©);
131 #if FF_API_AVFILTERBUFFER
132 static void compat_free_buffer(void *opaque, uint8_t *data)
134 AVFilterBufferRef *buf = opaque;
135 avfilter_unref_buffer(buf);
138 static void compat_unref_buffer(void *opaque, uint8_t *data)
140 AVBufferRef *buf = opaque;
141 av_buffer_unref(&buf);
144 int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
146 BufferSourceContext *s = ctx->priv;
147 AVFrame *frame = NULL;
148 AVBufferRef *dummy_buf = NULL;
149 int ret = 0, planes, i;
155 return AVERROR(EINVAL);
157 frame = av_frame_alloc();
159 return AVERROR(ENOMEM);
161 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf, 0);
163 ret = AVERROR(ENOMEM);
167 if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
170 #define WRAP_PLANE(ref_out, data, data_size) \
172 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
174 ret = AVERROR(ENOMEM); \
177 ref_out = av_buffer_create(data, data_size, compat_unref_buffer, \
180 av_frame_unref(frame); \
181 ret = AVERROR(ENOMEM); \
186 if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
187 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
190 ret = AVERROR(EINVAL);
193 planes = (desc->flags & PIX_FMT_PLANAR) ? desc->nb_components : 1;
195 for (i = 0; i < planes; i++) {
196 int h_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
197 int plane_size = (frame->width >> h_shift) * frame->linesize[i];
199 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
202 int planar = av_sample_fmt_is_planar(frame->format);
203 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
205 planes = planar ? channels : 1;
207 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
208 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
209 frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
210 frame->nb_extended_buf);
211 if (!frame->extended_buf) {
212 ret = AVERROR(ENOMEM);
217 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
218 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
220 for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
221 WRAP_PLANE(frame->extended_buf[i],
222 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
226 ret = av_buffersrc_add_frame(ctx, frame);
229 av_buffer_unref(&dummy_buf);
230 av_frame_free(&frame);
236 static av_cold int init_video(AVFilterContext *ctx, const char *args)
238 BufferSourceContext *c = ctx->priv;
239 char pix_fmt_str[128];
243 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
244 &c->time_base.num, &c->time_base.den,
245 &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
246 av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
247 return AVERROR(EINVAL);
249 if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == AV_PIX_FMT_NONE) {
251 c->pix_fmt = strtol(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", 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 static const AVOption audio_options[] = {
268 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
269 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
270 { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
271 { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
275 static const AVClass abuffer_class = {
276 .class_name = "abuffer source",
277 .item_name = av_default_item_name,
278 .option = audio_options,
279 .version = LIBAVUTIL_VERSION_INT,
282 static av_cold int init_audio(AVFilterContext *ctx, const char *args)
284 BufferSourceContext *s = ctx->priv;
287 s->class = &abuffer_class;
288 av_opt_set_defaults(s);
290 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
291 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: %s.\n", args);
295 s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
296 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
297 av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
299 ret = AVERROR(EINVAL);
303 s->channel_layout = av_get_channel_layout(s->channel_layout_str);
304 if (!s->channel_layout) {
305 av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
306 s->channel_layout_str);
307 ret = AVERROR(EINVAL);
311 if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*)))) {
312 ret = AVERROR(ENOMEM);
316 if (!s->time_base.num)
317 s->time_base = (AVRational){1, s->sample_rate};
319 av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
320 "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
321 s->sample_rate, s->channel_layout_str);
328 static av_cold void uninit(AVFilterContext *ctx)
330 BufferSourceContext *s = ctx->priv;
331 while (s->fifo && av_fifo_size(s->fifo)) {
333 av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
334 av_frame_free(&frame);
336 av_fifo_free(s->fifo);
340 static int query_formats(AVFilterContext *ctx)
342 BufferSourceContext *c = ctx->priv;
343 AVFilterChannelLayouts *channel_layouts = NULL;
344 AVFilterFormats *formats = NULL;
345 AVFilterFormats *samplerates = NULL;
347 switch (ctx->outputs[0]->type) {
348 case AVMEDIA_TYPE_VIDEO:
349 ff_add_format(&formats, c->pix_fmt);
350 ff_set_common_formats(ctx, formats);
352 case AVMEDIA_TYPE_AUDIO:
353 ff_add_format(&formats, c->sample_fmt);
354 ff_set_common_formats(ctx, formats);
356 ff_add_format(&samplerates, c->sample_rate);
357 ff_set_common_samplerates(ctx, samplerates);
359 ff_add_channel_layout(&channel_layouts, c->channel_layout);
360 ff_set_common_channel_layouts(ctx, channel_layouts);
363 return AVERROR(EINVAL);
369 static int config_props(AVFilterLink *link)
371 BufferSourceContext *c = link->src->priv;
373 switch (link->type) {
374 case AVMEDIA_TYPE_VIDEO:
377 link->sample_aspect_ratio = c->pixel_aspect;
379 case AVMEDIA_TYPE_AUDIO:
380 link->channel_layout = c->channel_layout;
381 link->sample_rate = c->sample_rate;
384 return AVERROR(EINVAL);
387 link->time_base = c->time_base;
391 static int request_frame(AVFilterLink *link)
393 BufferSourceContext *c = link->src->priv;
397 if (!av_fifo_size(c->fifo)) {
400 return AVERROR(EAGAIN);
402 av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
404 ff_filter_frame(link, frame);
409 static int poll_frame(AVFilterLink *link)
411 BufferSourceContext *c = link->src->priv;
412 int size = av_fifo_size(c->fifo);
415 return size/sizeof(AVFrame*);
418 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
421 .type = AVMEDIA_TYPE_VIDEO,
422 .request_frame = request_frame,
423 .poll_frame = poll_frame,
424 .config_props = config_props,
429 AVFilter avfilter_vsrc_buffer = {
431 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
432 .priv_size = sizeof(BufferSourceContext),
433 .query_formats = query_formats,
439 .outputs = avfilter_vsrc_buffer_outputs,
442 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
445 .type = AVMEDIA_TYPE_AUDIO,
446 .request_frame = request_frame,
447 .poll_frame = poll_frame,
448 .config_props = config_props,
453 AVFilter avfilter_asrc_abuffer = {
455 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
456 .priv_size = sizeof(BufferSourceContext),
457 .query_formats = query_formats,
463 .outputs = avfilter_asrc_abuffer_outputs,