2 * Copyright (c) 2011 Stefano Sabatini
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
26 #include "libavutil/audio_fifo.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/common.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
35 #include "buffersink.h"
40 AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
41 unsigned warning_limit;
43 /* only used for video */
44 enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
47 /* only used for audio */
48 enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
50 int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
51 int channel_layouts_size;
52 int *channel_counts; ///< list of accepted channel counts, terminated by -1
53 int channel_counts_size;
54 int all_channel_counts;
55 int *sample_rates; ///< list of accepted sample rates, terminated by -1
56 int sample_rates_size;
58 /* only used for compat API */
59 AVAudioFifo *audio_fifo; ///< FIFO for audio samples
60 int64_t next_pts; ///< interpolating audio pts
63 #define NB_ITEMS(list) (list ## _size / sizeof(*list))
65 static av_cold void uninit(AVFilterContext *ctx)
67 BufferSinkContext *sink = ctx->priv;
71 av_audio_fifo_free(sink->audio_fifo);
74 while (av_fifo_size(sink->fifo) >= sizeof(AVFilterBufferRef *)) {
75 av_fifo_generic_read(sink->fifo, &frame, sizeof(frame), NULL);
76 av_frame_free(&frame);
78 av_fifo_free(sink->fifo);
83 static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref)
85 BufferSinkContext *buf = ctx->priv;
87 if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
88 /* realloc fifo size */
89 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
90 av_log(ctx, AV_LOG_ERROR,
91 "Cannot buffer more frames. Consume some available frames "
92 "before adding new ones.\n");
93 return AVERROR(ENOMEM);
98 av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
102 static int filter_frame(AVFilterLink *link, AVFrame *frame)
104 AVFilterContext *ctx = link->dst;
105 BufferSinkContext *buf = link->dst->priv;
108 if ((ret = add_buffer_ref(ctx, frame)) < 0)
110 if (buf->warning_limit &&
111 av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
112 av_log(ctx, AV_LOG_WARNING,
113 "%d buffers queued in %s, something may be wrong.\n",
115 (char *)av_x_if_null(ctx->name, ctx->filter->name));
116 buf->warning_limit *= 10;
121 int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
123 return av_buffersink_get_frame_flags(ctx, frame, 0);
126 int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
128 BufferSinkContext *buf = ctx->priv;
129 AVFilterLink *inlink = ctx->inputs[0];
133 /* no picref available, fetch it from the filterchain */
134 if (!av_fifo_size(buf->fifo)) {
135 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
136 return AVERROR(EAGAIN);
137 if ((ret = ff_request_frame(inlink)) < 0)
141 if (!av_fifo_size(buf->fifo))
142 return AVERROR(EINVAL);
144 if (flags & AV_BUFFERSINK_FLAG_PEEK) {
145 cur_frame = *((AVFrame **)av_fifo_peek2(buf->fifo, 0));
146 if ((ret = av_frame_ref(frame, cur_frame)) < 0)
149 av_fifo_generic_read(buf->fifo, &cur_frame, sizeof(cur_frame), NULL);
150 av_frame_move_ref(frame, cur_frame);
151 av_frame_free(&cur_frame);
157 static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
160 BufferSinkContext *s = ctx->priv;
161 AVFilterLink *link = ctx->inputs[0];
164 if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
165 return AVERROR(ENOMEM);
166 av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
168 tmp->pts = s->next_pts;
169 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
172 av_frame_move_ref(frame, tmp);
179 int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
180 AVFrame *frame, int nb_samples)
182 BufferSinkContext *s = ctx->priv;
183 AVFilterLink *link = ctx->inputs[0];
187 if (!s->audio_fifo) {
188 int nb_channels = link->channels;
189 if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
190 return AVERROR(ENOMEM);
194 if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
195 return read_from_fifo(ctx, frame, nb_samples);
197 if (!(cur_frame = av_frame_alloc()))
198 return AVERROR(ENOMEM);
199 ret = av_buffersink_get_frame_flags(ctx, cur_frame, 0);
200 if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo)) {
201 av_frame_free(&cur_frame);
202 return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
203 } else if (ret < 0) {
204 av_frame_free(&cur_frame);
208 if (cur_frame->pts != AV_NOPTS_VALUE) {
209 s->next_pts = cur_frame->pts -
210 av_rescale_q(av_audio_fifo_size(s->audio_fifo),
211 (AVRational){ 1, link->sample_rate },
215 ret = av_audio_fifo_write(s->audio_fifo, (void**)cur_frame->extended_data,
216 cur_frame->nb_samples);
217 av_frame_free(&cur_frame);
224 AVBufferSinkParams *av_buffersink_params_alloc(void)
226 static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
227 AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
231 params->pixel_fmts = pixel_fmts;
235 AVABufferSinkParams *av_abuffersink_params_alloc(void)
237 AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
244 #define FIFO_INIT_SIZE 8
246 static av_cold int common_init(AVFilterContext *ctx)
248 BufferSinkContext *buf = ctx->priv;
250 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
252 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
253 return AVERROR(ENOMEM);
255 buf->warning_limit = 100;
259 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
261 AVFilterLink *inlink = ctx->inputs[0];
263 inlink->min_samples = inlink->max_samples =
264 inlink->partial_buf_size = frame_size;
267 #if FF_API_AVFILTERBUFFER
268 static void compat_free_buffer(AVFilterBuffer *buf)
270 AVFrame *frame = buf->priv;
271 av_frame_free(&frame);
275 static int attribute_align_arg compat_read(AVFilterContext *ctx,
276 AVFilterBufferRef **pbuf, int nb_samples, int flags)
278 AVFilterBufferRef *buf;
283 return ff_poll_frame(ctx->inputs[0]);
285 frame = av_frame_alloc();
287 return AVERROR(ENOMEM);
290 ret = av_buffersink_get_frame_flags(ctx, frame, flags);
292 ret = av_buffersink_get_samples(ctx, frame, nb_samples);
297 AV_NOWARN_DEPRECATED(
298 if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
299 buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
301 frame->width, frame->height,
304 buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
305 frame->linesize[0], AV_PERM_READ,
308 frame->channel_layout);
311 ret = AVERROR(ENOMEM);
315 avfilter_copy_frame_props(buf, frame);
318 buf->buf->priv = frame;
319 buf->buf->free = compat_free_buffer;
325 av_frame_free(&frame);
329 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
331 return compat_read(ctx, buf, 0, 0);
334 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
337 return compat_read(ctx, buf, nb_samples, 0);
340 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
341 AVFilterBufferRef **bufref, int flags)
345 av_assert0( !strcmp(ctx->filter->name, "buffersink")
346 || !strcmp(ctx->filter->name, "abuffersink")
347 || !strcmp(ctx->filter->name, "ffbuffersink")
348 || !strcmp(ctx->filter->name, "ffabuffersink"));
350 return compat_read(ctx, bufref, 0, flags);
354 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
356 av_assert0( !strcmp(ctx->filter->name, "buffersink")
357 || !strcmp(ctx->filter->name, "ffbuffersink"));
359 return ctx->inputs[0]->frame_rate;
362 int attribute_align_arg av_buffersink_poll_frame(AVFilterContext *ctx)
364 BufferSinkContext *buf = ctx->priv;
365 AVFilterLink *inlink = ctx->inputs[0];
367 av_assert0( !strcmp(ctx->filter->name, "buffersink")
368 || !strcmp(ctx->filter->name, "abuffersink")
369 || !strcmp(ctx->filter->name, "ffbuffersink")
370 || !strcmp(ctx->filter->name, "ffabuffersink"));
372 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
375 static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
377 BufferSinkContext *buf = ctx->priv;
378 AVBufferSinkParams *params = opaque;
382 if ((ret = av_opt_set_int_list(buf, "pix_fmts", params->pixel_fmts, AV_PIX_FMT_NONE, 0)) < 0)
386 return common_init(ctx);
389 #define CHECK_LIST_SIZE(field) \
390 if (buf->field ## _size % sizeof(*buf->field)) { \
391 av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
392 "should be multiple of %d\n", \
393 buf->field ## _size, (int)sizeof(*buf->field)); \
394 return AVERROR(EINVAL); \
396 static int vsink_query_formats(AVFilterContext *ctx)
398 BufferSinkContext *buf = ctx->priv;
399 AVFilterFormats *formats = NULL;
403 CHECK_LIST_SIZE(pixel_fmts)
404 if (buf->pixel_fmts_size) {
405 for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
406 if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0) {
407 ff_formats_unref(&formats);
410 ff_set_common_formats(ctx, formats);
412 ff_default_query_formats(ctx);
418 static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
420 BufferSinkContext *buf = ctx->priv;
421 AVABufferSinkParams *params = opaque;
425 if ((ret = av_opt_set_int_list(buf, "sample_fmts", params->sample_fmts, AV_SAMPLE_FMT_NONE, 0)) < 0 ||
426 (ret = av_opt_set_int_list(buf, "sample_rates", params->sample_rates, -1, 0)) < 0 ||
427 (ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
428 (ret = av_opt_set_int_list(buf, "channel_counts", params->channel_counts, -1, 0)) < 0 ||
429 (ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
432 return common_init(ctx);
435 static int asink_query_formats(AVFilterContext *ctx)
437 BufferSinkContext *buf = ctx->priv;
438 AVFilterFormats *formats = NULL;
439 AVFilterChannelLayouts *layouts = NULL;
443 CHECK_LIST_SIZE(sample_fmts)
444 CHECK_LIST_SIZE(sample_rates)
445 CHECK_LIST_SIZE(channel_layouts)
446 CHECK_LIST_SIZE(channel_counts)
448 if (buf->sample_fmts_size) {
449 for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
450 if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0) {
451 ff_formats_unref(&formats);
454 ff_set_common_formats(ctx, formats);
457 if (buf->channel_layouts_size || buf->channel_counts_size ||
458 buf->all_channel_counts) {
459 for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
460 if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0) {
461 ff_channel_layouts_unref(&layouts);
464 for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
465 if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0) {
466 ff_channel_layouts_unref(&layouts);
469 if (buf->all_channel_counts) {
471 av_log(ctx, AV_LOG_WARNING,
472 "Conflicting all_channel_counts and list in options\n");
473 else if (!(layouts = ff_all_channel_counts()))
474 return AVERROR(ENOMEM);
476 ff_set_common_channel_layouts(ctx, layouts);
479 if (buf->sample_rates_size) {
481 for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
482 if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0) {
483 ff_formats_unref(&formats);
486 ff_set_common_samplerates(ctx, formats);
492 #define OFFSET(x) offsetof(BufferSinkContext, x)
493 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
494 static const AVOption buffersink_options[] = {
495 { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
499 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
500 static const AVOption abuffersink_options[] = {
501 { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
502 { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
503 { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
504 { "channel_counts", "set the supported channel counts", OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
505 { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
510 AVFILTER_DEFINE_CLASS(buffersink);
511 AVFILTER_DEFINE_CLASS(abuffersink);
513 #if FF_API_AVFILTERBUFFER
515 #define ffbuffersink_options buffersink_options
516 #define ffabuffersink_options abuffersink_options
517 AVFILTER_DEFINE_CLASS(ffbuffersink);
518 AVFILTER_DEFINE_CLASS(ffabuffersink);
520 static const AVFilterPad ffbuffersink_inputs[] = {
523 .type = AVMEDIA_TYPE_VIDEO,
524 .filter_frame = filter_frame,
529 AVFilter avfilter_vsink_ffbuffersink = {
530 .name = "ffbuffersink",
531 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
532 .priv_size = sizeof(BufferSinkContext),
533 .priv_class = &ffbuffersink_class,
534 .init_opaque = vsink_init,
537 .query_formats = vsink_query_formats,
538 .inputs = ffbuffersink_inputs,
542 static const AVFilterPad ffabuffersink_inputs[] = {
545 .type = AVMEDIA_TYPE_AUDIO,
546 .filter_frame = filter_frame,
551 AVFilter avfilter_asink_ffabuffersink = {
552 .name = "ffabuffersink",
553 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
554 .init_opaque = asink_init,
556 .priv_size = sizeof(BufferSinkContext),
557 .priv_class = &ffabuffersink_class,
558 .query_formats = asink_query_formats,
559 .inputs = ffabuffersink_inputs,
562 #endif /* FF_API_AVFILTERBUFFER */
564 static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
567 .type = AVMEDIA_TYPE_VIDEO,
568 .filter_frame = filter_frame,
573 AVFilter avfilter_vsink_buffer = {
574 .name = "buffersink",
575 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
576 .priv_size = sizeof(BufferSinkContext),
577 .priv_class = &buffersink_class,
578 .init_opaque = vsink_init,
581 .query_formats = vsink_query_formats,
582 .inputs = avfilter_vsink_buffer_inputs,
586 static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
589 .type = AVMEDIA_TYPE_AUDIO,
590 .filter_frame = filter_frame,
595 AVFilter avfilter_asink_abuffer = {
596 .name = "abuffersink",
597 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
598 .priv_class = &abuffersink_class,
599 .priv_size = sizeof(BufferSinkContext),
600 .init_opaque = asink_init,
603 .query_formats = asink_query_formats,
604 .inputs = avfilter_asink_abuffer_inputs,