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/internal.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
36 #include "buffersink.h"
41 AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
42 unsigned warning_limit;
44 /* only used for video */
45 enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
48 /* only used for audio */
49 enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
51 int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
52 int channel_layouts_size;
53 int *channel_counts; ///< list of accepted channel counts, terminated by -1
54 int channel_counts_size;
55 int all_channel_counts;
56 int *sample_rates; ///< list of accepted sample rates, terminated by -1
57 int sample_rates_size;
59 /* only used for compat API */
60 AVAudioFifo *audio_fifo; ///< FIFO for audio samples
61 int64_t next_pts; ///< interpolating audio pts
64 #define NB_ITEMS(list) (list ## _size / sizeof(*list))
66 static av_cold void uninit(AVFilterContext *ctx)
68 BufferSinkContext *sink = ctx->priv;
72 av_audio_fifo_free(sink->audio_fifo);
75 while (av_fifo_size(sink->fifo) >= sizeof(AVFilterBufferRef *)) {
76 av_fifo_generic_read(sink->fifo, &frame, sizeof(frame), NULL);
77 av_frame_free(&frame);
79 av_fifo_free(sink->fifo);
84 static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref)
86 BufferSinkContext *buf = ctx->priv;
88 if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
89 /* realloc fifo size */
90 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
91 av_log(ctx, AV_LOG_ERROR,
92 "Cannot buffer more frames. Consume some available frames "
93 "before adding new ones.\n");
94 return AVERROR(ENOMEM);
99 av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
103 static int filter_frame(AVFilterLink *link, AVFrame *frame)
105 AVFilterContext *ctx = link->dst;
106 BufferSinkContext *buf = link->dst->priv;
109 if ((ret = add_buffer_ref(ctx, frame)) < 0)
111 if (buf->warning_limit &&
112 av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
113 av_log(ctx, AV_LOG_WARNING,
114 "%d buffers queued in %s, something may be wrong.\n",
116 (char *)av_x_if_null(ctx->name, ctx->filter->name));
117 buf->warning_limit *= 10;
122 int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
124 return av_buffersink_get_frame_flags(ctx, frame, 0);
127 int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
129 BufferSinkContext *buf = ctx->priv;
130 AVFilterLink *inlink = ctx->inputs[0];
134 /* no picref available, fetch it from the filterchain */
135 if (!av_fifo_size(buf->fifo)) {
136 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
137 return AVERROR(EAGAIN);
138 if ((ret = ff_request_frame(inlink)) < 0)
142 if (!av_fifo_size(buf->fifo))
143 return AVERROR(EINVAL);
145 if (flags & AV_BUFFERSINK_FLAG_PEEK) {
146 cur_frame = *((AVFrame **)av_fifo_peek2(buf->fifo, 0));
147 if ((ret = av_frame_ref(frame, cur_frame)) < 0)
150 av_fifo_generic_read(buf->fifo, &cur_frame, sizeof(cur_frame), NULL);
151 av_frame_move_ref(frame, cur_frame);
152 av_frame_free(&cur_frame);
158 static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
161 BufferSinkContext *s = ctx->priv;
162 AVFilterLink *link = ctx->inputs[0];
165 if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
166 return AVERROR(ENOMEM);
167 av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
169 tmp->pts = s->next_pts;
170 if (s->next_pts != AV_NOPTS_VALUE)
171 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
174 av_frame_move_ref(frame, tmp);
180 int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
181 AVFrame *frame, int nb_samples)
183 BufferSinkContext *s = ctx->priv;
184 AVFilterLink *link = ctx->inputs[0];
188 if (!s->audio_fifo) {
189 int nb_channels = link->channels;
190 if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
191 return AVERROR(ENOMEM);
195 if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
196 return read_from_fifo(ctx, frame, nb_samples);
198 if (!(cur_frame = av_frame_alloc()))
199 return AVERROR(ENOMEM);
200 ret = av_buffersink_get_frame_flags(ctx, cur_frame, 0);
201 if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo)) {
202 av_frame_free(&cur_frame);
203 return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
204 } else if (ret < 0) {
205 av_frame_free(&cur_frame);
209 if (cur_frame->pts != AV_NOPTS_VALUE) {
210 s->next_pts = cur_frame->pts -
211 av_rescale_q(av_audio_fifo_size(s->audio_fifo),
212 (AVRational){ 1, link->sample_rate },
216 ret = av_audio_fifo_write(s->audio_fifo, (void**)cur_frame->extended_data,
217 cur_frame->nb_samples);
218 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;
256 buf->next_pts = AV_NOPTS_VALUE;
260 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
262 AVFilterLink *inlink = ctx->inputs[0];
264 inlink->min_samples = inlink->max_samples =
265 inlink->partial_buf_size = frame_size;
268 #if FF_API_AVFILTERBUFFER
269 FF_DISABLE_DEPRECATION_WARNINGS
270 static void compat_free_buffer(AVFilterBuffer *buf)
272 AVFrame *frame = buf->priv;
273 av_frame_free(&frame);
277 static int compat_read(AVFilterContext *ctx,
278 AVFilterBufferRef **pbuf, int nb_samples, int flags)
280 AVFilterBufferRef *buf;
285 return ff_poll_frame(ctx->inputs[0]);
287 frame = av_frame_alloc();
289 return AVERROR(ENOMEM);
292 ret = av_buffersink_get_frame_flags(ctx, frame, flags);
294 ret = av_buffersink_get_samples(ctx, frame, nb_samples);
299 AV_NOWARN_DEPRECATED(
300 if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
301 buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
303 frame->width, frame->height,
306 buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
307 frame->linesize[0], AV_PERM_READ,
310 frame->channel_layout);
313 ret = AVERROR(ENOMEM);
317 avfilter_copy_frame_props(buf, frame);
320 buf->buf->priv = frame;
321 buf->buf->free = compat_free_buffer;
327 av_frame_free(&frame);
331 int attribute_align_arg av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
333 return compat_read(ctx, buf, 0, 0);
336 int attribute_align_arg av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
339 return compat_read(ctx, buf, nb_samples, 0);
342 int attribute_align_arg av_buffersink_get_buffer_ref(AVFilterContext *ctx,
343 AVFilterBufferRef **bufref, int flags)
347 av_assert0( !strcmp(ctx->filter->name, "buffersink")
348 || !strcmp(ctx->filter->name, "abuffersink")
349 || !strcmp(ctx->filter->name, "ffbuffersink")
350 || !strcmp(ctx->filter->name, "ffabuffersink"));
352 return compat_read(ctx, bufref, 0, flags);
354 FF_ENABLE_DEPRECATION_WARNINGS
357 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
359 av_assert0( !strcmp(ctx->filter->name, "buffersink")
360 || !strcmp(ctx->filter->name, "ffbuffersink"));
362 return ctx->inputs[0]->frame_rate;
365 int attribute_align_arg av_buffersink_poll_frame(AVFilterContext *ctx)
367 BufferSinkContext *buf = ctx->priv;
368 AVFilterLink *inlink = ctx->inputs[0];
370 av_assert0( !strcmp(ctx->filter->name, "buffersink")
371 || !strcmp(ctx->filter->name, "abuffersink")
372 || !strcmp(ctx->filter->name, "ffbuffersink")
373 || !strcmp(ctx->filter->name, "ffabuffersink"));
375 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
378 static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
380 BufferSinkContext *buf = ctx->priv;
381 AVBufferSinkParams *params = opaque;
385 if ((ret = av_opt_set_int_list(buf, "pix_fmts", params->pixel_fmts, AV_PIX_FMT_NONE, 0)) < 0)
389 return common_init(ctx);
392 #define CHECK_LIST_SIZE(field) \
393 if (buf->field ## _size % sizeof(*buf->field)) { \
394 av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
395 "should be multiple of %d\n", \
396 buf->field ## _size, (int)sizeof(*buf->field)); \
397 return AVERROR(EINVAL); \
399 static int vsink_query_formats(AVFilterContext *ctx)
401 BufferSinkContext *buf = ctx->priv;
402 AVFilterFormats *formats = NULL;
406 CHECK_LIST_SIZE(pixel_fmts)
407 if (buf->pixel_fmts_size) {
408 for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
409 if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0) {
410 ff_formats_unref(&formats);
413 ff_set_common_formats(ctx, formats);
415 ff_default_query_formats(ctx);
421 static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
423 BufferSinkContext *buf = ctx->priv;
424 AVABufferSinkParams *params = opaque;
428 if ((ret = av_opt_set_int_list(buf, "sample_fmts", params->sample_fmts, AV_SAMPLE_FMT_NONE, 0)) < 0 ||
429 (ret = av_opt_set_int_list(buf, "sample_rates", params->sample_rates, -1, 0)) < 0 ||
430 (ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
431 (ret = av_opt_set_int_list(buf, "channel_counts", params->channel_counts, -1, 0)) < 0 ||
432 (ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
435 return common_init(ctx);
438 static int asink_query_formats(AVFilterContext *ctx)
440 BufferSinkContext *buf = ctx->priv;
441 AVFilterFormats *formats = NULL;
442 AVFilterChannelLayouts *layouts = NULL;
446 CHECK_LIST_SIZE(sample_fmts)
447 CHECK_LIST_SIZE(sample_rates)
448 CHECK_LIST_SIZE(channel_layouts)
449 CHECK_LIST_SIZE(channel_counts)
451 if (buf->sample_fmts_size) {
452 for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
453 if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0) {
454 ff_formats_unref(&formats);
457 ff_set_common_formats(ctx, formats);
460 if (buf->channel_layouts_size || buf->channel_counts_size ||
461 buf->all_channel_counts) {
462 for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
463 if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0) {
464 ff_channel_layouts_unref(&layouts);
467 for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
468 if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0) {
469 ff_channel_layouts_unref(&layouts);
472 if (buf->all_channel_counts) {
474 av_log(ctx, AV_LOG_WARNING,
475 "Conflicting all_channel_counts and list in options\n");
476 else if (!(layouts = ff_all_channel_counts()))
477 return AVERROR(ENOMEM);
479 ff_set_common_channel_layouts(ctx, layouts);
482 if (buf->sample_rates_size) {
484 for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
485 if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0) {
486 ff_formats_unref(&formats);
489 ff_set_common_samplerates(ctx, formats);
495 #define OFFSET(x) offsetof(BufferSinkContext, x)
496 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
497 static const AVOption buffersink_options[] = {
498 { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
502 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
503 static const AVOption abuffersink_options[] = {
504 { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
505 { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
506 { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
507 { "channel_counts", "set the supported channel counts", OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
508 { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
513 AVFILTER_DEFINE_CLASS(buffersink);
514 AVFILTER_DEFINE_CLASS(abuffersink);
516 #if FF_API_AVFILTERBUFFER
518 #define ffbuffersink_options buffersink_options
519 #define ffabuffersink_options abuffersink_options
520 AVFILTER_DEFINE_CLASS(ffbuffersink);
521 AVFILTER_DEFINE_CLASS(ffabuffersink);
523 static const AVFilterPad ffbuffersink_inputs[] = {
526 .type = AVMEDIA_TYPE_VIDEO,
527 .filter_frame = filter_frame,
532 AVFilter ff_vsink_ffbuffersink = {
533 .name = "ffbuffersink",
534 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
535 .priv_size = sizeof(BufferSinkContext),
536 .priv_class = &ffbuffersink_class,
537 .init_opaque = vsink_init,
540 .query_formats = vsink_query_formats,
541 .inputs = ffbuffersink_inputs,
545 static const AVFilterPad ffabuffersink_inputs[] = {
548 .type = AVMEDIA_TYPE_AUDIO,
549 .filter_frame = filter_frame,
554 AVFilter ff_asink_ffabuffersink = {
555 .name = "ffabuffersink",
556 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
557 .init_opaque = asink_init,
559 .priv_size = sizeof(BufferSinkContext),
560 .priv_class = &ffabuffersink_class,
561 .query_formats = asink_query_formats,
562 .inputs = ffabuffersink_inputs,
565 #endif /* FF_API_AVFILTERBUFFER */
567 static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
570 .type = AVMEDIA_TYPE_VIDEO,
571 .filter_frame = filter_frame,
576 AVFilter ff_vsink_buffer = {
577 .name = "buffersink",
578 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
579 .priv_size = sizeof(BufferSinkContext),
580 .priv_class = &buffersink_class,
581 .init_opaque = vsink_init,
584 .query_formats = vsink_query_formats,
585 .inputs = avfilter_vsink_buffer_inputs,
589 static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
592 .type = AVMEDIA_TYPE_AUDIO,
593 .filter_frame = filter_frame,
598 AVFilter ff_asink_abuffer = {
599 .name = "abuffersink",
600 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
601 .priv_class = &abuffersink_class,
602 .priv_size = sizeof(BufferSinkContext),
603 .init_opaque = asink_init,
606 .query_formats = asink_query_formats,
607 .inputs = avfilter_asink_abuffer_inputs,