2 * Copyright (c) 2008 Vitor Sessak
4 * This file is part of FFmpeg.
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
47 AVRational time_base; ///< time_base to set in the output link
48 AVRational frame_rate; ///< frame_rate to set in the output link
49 unsigned nb_failed_requests;
50 unsigned warning_limit;
54 enum AVPixelFormat pix_fmt;
55 AVRational pixel_aspect;
60 enum AVSampleFormat sample_fmt;
63 uint64_t channel_layout;
64 char *channel_layout_str;
67 } BufferSourceContext;
69 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
70 if (c->w != width || c->h != height || c->pix_fmt != format) {\
71 av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
74 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, ch_count, format)\
75 if (c->sample_fmt != format || c->sample_rate != srate ||\
76 c->channel_layout != ch_layout || c->channels != ch_count) {\
77 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
78 return AVERROR(EINVAL);\
81 int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
83 return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
84 AV_BUFFERSRC_FLAG_KEEP_REF);
87 int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
89 return av_buffersrc_add_frame_flags(ctx, frame, 0);
92 static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
93 AVFrame *frame, int flags);
95 int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
100 if (frame && frame->channel_layout &&
101 av_get_channel_layout_nb_channels(frame->channel_layout) != av_frame_get_channels(frame)) {
102 av_log(0, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
103 return AVERROR(EINVAL);
106 if (!(flags & AV_BUFFERSRC_FLAG_KEEP_REF) || !frame)
107 return av_buffersrc_add_frame_internal(ctx, frame, flags);
109 if (!(copy = av_frame_alloc()))
110 return AVERROR(ENOMEM);
111 ret = av_frame_ref(copy, frame);
113 ret = av_buffersrc_add_frame_internal(ctx, copy, flags);
115 av_frame_free(©);
119 static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
120 AVFrame *frame, int flags)
122 BufferSourceContext *s = ctx->priv;
126 s->nb_failed_requests = 0;
132 return AVERROR(EINVAL);
134 if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
136 switch (ctx->outputs[0]->type) {
137 case AVMEDIA_TYPE_VIDEO:
138 CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
141 case AVMEDIA_TYPE_AUDIO:
142 /* For layouts unknown on input but known on link after negotiation. */
143 if (!frame->channel_layout)
144 frame->channel_layout = s->channel_layout;
145 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
146 av_frame_get_channels(frame), frame->format);
149 return AVERROR(EINVAL);
154 if (!av_fifo_space(s->fifo) &&
155 (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
159 if (!(copy = av_frame_alloc()))
160 return AVERROR(ENOMEM);
161 av_frame_move_ref(copy, frame);
163 if ((ret = av_fifo_generic_write(s->fifo, ©, sizeof(copy), NULL)) < 0) {
164 av_frame_move_ref(frame, copy);
165 av_frame_free(©);
169 if ((flags & AV_BUFFERSRC_FLAG_PUSH))
170 if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
176 #if FF_API_AVFILTERBUFFER
177 FF_DISABLE_DEPRECATION_WARNINGS
178 static void compat_free_buffer(void *opaque, uint8_t *data)
180 AVFilterBufferRef *buf = opaque;
181 AV_NOWARN_DEPRECATED(
182 avfilter_unref_buffer(buf);
186 static void compat_unref_buffer(void *opaque, uint8_t *data)
188 AVBufferRef *buf = opaque;
189 AV_NOWARN_DEPRECATED(
190 av_buffer_unref(&buf);
194 int av_buffersrc_add_ref(AVFilterContext *ctx, AVFilterBufferRef *buf,
197 BufferSourceContext *s = ctx->priv;
198 AVFrame *frame = NULL;
199 AVBufferRef *dummy_buf = NULL;
200 int ret = 0, planes, i;
206 return AVERROR(EINVAL);
208 frame = av_frame_alloc();
210 return AVERROR(ENOMEM);
212 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf,
213 (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY);
215 ret = AVERROR(ENOMEM);
219 AV_NOWARN_DEPRECATED(
220 if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
224 #define WRAP_PLANE(ref_out, data, data_size) \
226 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
228 ret = AVERROR(ENOMEM); \
231 ref_out = av_buffer_create(data, data_size, compat_unref_buffer, \
232 dummy_ref, (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY); \
234 av_frame_unref(frame); \
235 ret = AVERROR(ENOMEM); \
240 if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
241 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
243 planes = av_pix_fmt_count_planes(frame->format);
244 if (!desc || planes <= 0) {
245 ret = AVERROR(EINVAL);
249 for (i = 0; i < planes; i++) {
250 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
251 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
253 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
256 int planar = av_sample_fmt_is_planar(frame->format);
257 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
259 planes = planar ? channels : 1;
261 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
262 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
263 frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
264 frame->nb_extended_buf);
265 if (!frame->extended_buf) {
266 ret = AVERROR(ENOMEM);
271 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
272 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
274 for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
275 WRAP_PLANE(frame->extended_buf[i],
276 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
280 ret = av_buffersrc_add_frame_flags(ctx, frame, flags);
283 av_buffer_unref(&dummy_buf);
284 av_frame_free(&frame);
288 FF_ENABLE_DEPRECATION_WARNINGS
290 int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
292 return av_buffersrc_add_ref(ctx, buf, 0);
296 static av_cold int init_video(AVFilterContext *ctx)
298 BufferSourceContext *c = ctx->priv;
300 if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
301 av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
302 return AVERROR(EINVAL);
305 if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
306 return AVERROR(ENOMEM);
308 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d sws_param:%s\n",
309 c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
310 c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
311 c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
312 c->warning_limit = 100;
316 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
318 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
321 #define OFFSET(x) offsetof(BufferSourceContext, x)
322 #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
323 #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
325 static const AVOption buffer_options[] = {
326 { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
327 { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
328 { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
329 { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = V },
330 #if FF_API_OLD_FILTER_OPTS
331 /* those 4 are for compatibility with the old option passing system where each filter
332 * did its own parsing */
333 { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
334 { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
335 { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
336 { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
338 { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
339 { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
340 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
341 { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
342 { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V },
346 AVFILTER_DEFINE_CLASS(buffer);
348 static const AVOption abuffer_options[] = {
349 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
350 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
351 { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
352 { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
353 { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
357 AVFILTER_DEFINE_CLASS(abuffer);
359 static av_cold int init_audio(AVFilterContext *ctx)
361 BufferSourceContext *s = ctx->priv;
364 s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
365 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
366 av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s\n",
368 return AVERROR(EINVAL);
371 if (s->channel_layout_str) {
374 s->channel_layout = av_get_channel_layout(s->channel_layout_str);
375 if (!s->channel_layout) {
376 av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
377 s->channel_layout_str);
378 return AVERROR(EINVAL);
380 n = av_get_channel_layout_nb_channels(s->channel_layout);
382 if (n != s->channels) {
383 av_log(ctx, AV_LOG_ERROR,
384 "Mismatching channel count %d and layout '%s' "
386 s->channels, s->channel_layout_str, n);
387 return AVERROR(EINVAL);
391 } else if (!s->channels) {
392 av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
393 "channel layout specified\n");
394 return AVERROR(EINVAL);
397 if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
398 return AVERROR(ENOMEM);
400 if (!s->time_base.num)
401 s->time_base = (AVRational){1, s->sample_rate};
403 av_log(ctx, AV_LOG_VERBOSE,
404 "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
405 s->time_base.num, s->time_base.den, s->sample_fmt_str,
406 s->sample_rate, s->channel_layout_str);
407 s->warning_limit = 100;
412 static av_cold void uninit(AVFilterContext *ctx)
414 BufferSourceContext *s = ctx->priv;
415 while (s->fifo && av_fifo_size(s->fifo)) {
417 av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
418 av_frame_free(&frame);
420 av_fifo_free(s->fifo);
424 static int query_formats(AVFilterContext *ctx)
426 BufferSourceContext *c = ctx->priv;
427 AVFilterChannelLayouts *channel_layouts = NULL;
428 AVFilterFormats *formats = NULL;
429 AVFilterFormats *samplerates = NULL;
431 switch (ctx->outputs[0]->type) {
432 case AVMEDIA_TYPE_VIDEO:
433 ff_add_format(&formats, c->pix_fmt);
434 ff_set_common_formats(ctx, formats);
436 case AVMEDIA_TYPE_AUDIO:
437 ff_add_format(&formats, c->sample_fmt);
438 ff_set_common_formats(ctx, formats);
440 ff_add_format(&samplerates, c->sample_rate);
441 ff_set_common_samplerates(ctx, samplerates);
443 ff_add_channel_layout(&channel_layouts,
444 c->channel_layout ? c->channel_layout :
445 FF_COUNT2LAYOUT(c->channels));
446 ff_set_common_channel_layouts(ctx, channel_layouts);
449 return AVERROR(EINVAL);
455 static int config_props(AVFilterLink *link)
457 BufferSourceContext *c = link->src->priv;
459 switch (link->type) {
460 case AVMEDIA_TYPE_VIDEO:
463 link->sample_aspect_ratio = c->pixel_aspect;
465 case AVMEDIA_TYPE_AUDIO:
466 if (!c->channel_layout)
467 c->channel_layout = link->channel_layout;
470 return AVERROR(EINVAL);
473 link->time_base = c->time_base;
474 link->frame_rate = c->frame_rate;
478 static int request_frame(AVFilterLink *link)
480 BufferSourceContext *c = link->src->priv;
483 if (!av_fifo_size(c->fifo)) {
486 c->nb_failed_requests++;
487 return AVERROR(EAGAIN);
489 av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
491 return ff_filter_frame(link, frame);
494 static int poll_frame(AVFilterLink *link)
496 BufferSourceContext *c = link->src->priv;
497 int size = av_fifo_size(c->fifo);
500 return size/sizeof(AVFrame*);
503 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
506 .type = AVMEDIA_TYPE_VIDEO,
507 .request_frame = request_frame,
508 .poll_frame = poll_frame,
509 .config_props = config_props,
514 AVFilter ff_vsrc_buffer = {
516 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
517 .priv_size = sizeof(BufferSourceContext),
518 .query_formats = query_formats,
524 .outputs = avfilter_vsrc_buffer_outputs,
525 .priv_class = &buffer_class,
528 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
531 .type = AVMEDIA_TYPE_AUDIO,
532 .request_frame = request_frame,
533 .poll_frame = poll_frame,
534 .config_props = config_props,
539 AVFilter ff_asrc_abuffer = {
541 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
542 .priv_size = sizeof(BufferSourceContext),
543 .query_formats = query_formats,
549 .outputs = avfilter_asrc_abuffer_outputs,
550 .priv_class = &abuffer_class,