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"
39 typedef struct BufferSinkContext {
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))
65 #define FIFO_INIT_SIZE 8
66 #define FIFO_INIT_ELEMENT_SIZE sizeof(void *)
68 static av_cold void uninit(AVFilterContext *ctx)
70 BufferSinkContext *sink = ctx->priv;
74 av_audio_fifo_free(sink->audio_fifo);
77 while (av_fifo_size(sink->fifo) >= FIFO_INIT_ELEMENT_SIZE) {
78 av_fifo_generic_read(sink->fifo, &frame, sizeof(frame), NULL);
79 av_frame_free(&frame);
81 av_fifo_freep(&sink->fifo);
85 static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref)
87 BufferSinkContext *buf = ctx->priv;
89 if (av_fifo_space(buf->fifo) < FIFO_INIT_ELEMENT_SIZE) {
90 /* realloc fifo size */
91 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
92 av_log(ctx, AV_LOG_ERROR,
93 "Cannot buffer more frames. Consume some available frames "
94 "before adding new ones.\n");
95 return AVERROR(ENOMEM);
100 av_fifo_generic_write(buf->fifo, &ref, FIFO_INIT_ELEMENT_SIZE, NULL);
104 static int filter_frame(AVFilterLink *link, AVFrame *frame)
106 AVFilterContext *ctx = link->dst;
107 BufferSinkContext *buf = link->dst->priv;
110 if ((ret = add_buffer_ref(ctx, frame)) < 0)
112 if (buf->warning_limit &&
113 av_fifo_size(buf->fifo) / FIFO_INIT_ELEMENT_SIZE >= buf->warning_limit) {
114 av_log(ctx, AV_LOG_WARNING,
115 "%d buffers queued in %s, something may be wrong.\n",
117 (char *)av_x_if_null(ctx->name, ctx->filter->name));
118 buf->warning_limit *= 10;
123 int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
125 return av_buffersink_get_frame_flags(ctx, frame, 0);
128 int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
130 BufferSinkContext *buf = ctx->priv;
131 AVFilterLink *inlink = ctx->inputs[0];
135 /* no picref available, fetch it from the filterchain */
136 while (!av_fifo_size(buf->fifo)) {
139 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
140 return AVERROR(EAGAIN);
141 if ((ret = ff_request_frame(inlink)) < 0)
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 static av_cold int common_init(AVFilterContext *ctx)
246 BufferSinkContext *buf = ctx->priv;
248 buf->fifo = av_fifo_alloc_array(FIFO_INIT_SIZE, FIFO_INIT_ELEMENT_SIZE);
250 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
251 return AVERROR(ENOMEM);
253 buf->warning_limit = 100;
254 buf->next_pts = AV_NOPTS_VALUE;
258 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
260 AVFilterLink *inlink = ctx->inputs[0];
262 inlink->min_samples = inlink->max_samples =
263 inlink->partial_buf_size = frame_size;
266 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
268 av_assert0( !strcmp(ctx->filter->name, "buffersink")
269 || !strcmp(ctx->filter->name, "ffbuffersink"));
271 return ctx->inputs[0]->frame_rate;
274 static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
276 BufferSinkContext *buf = ctx->priv;
277 AVBufferSinkParams *params = opaque;
281 if ((ret = av_opt_set_int_list(buf, "pix_fmts", params->pixel_fmts, AV_PIX_FMT_NONE, 0)) < 0)
285 return common_init(ctx);
288 #define CHECK_LIST_SIZE(field) \
289 if (buf->field ## _size % sizeof(*buf->field)) { \
290 av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
291 "should be multiple of %d\n", \
292 buf->field ## _size, (int)sizeof(*buf->field)); \
293 return AVERROR(EINVAL); \
295 static int vsink_query_formats(AVFilterContext *ctx)
297 BufferSinkContext *buf = ctx->priv;
298 AVFilterFormats *formats = NULL;
302 CHECK_LIST_SIZE(pixel_fmts)
303 if (buf->pixel_fmts_size) {
304 for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
305 if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0)
307 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
310 if ((ret = ff_default_query_formats(ctx)) < 0)
317 static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
319 BufferSinkContext *buf = ctx->priv;
320 AVABufferSinkParams *params = opaque;
324 if ((ret = av_opt_set_int_list(buf, "sample_fmts", params->sample_fmts, AV_SAMPLE_FMT_NONE, 0)) < 0 ||
325 (ret = av_opt_set_int_list(buf, "sample_rates", params->sample_rates, -1, 0)) < 0 ||
326 (ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
327 (ret = av_opt_set_int_list(buf, "channel_counts", params->channel_counts, -1, 0)) < 0 ||
328 (ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
331 return common_init(ctx);
334 static int asink_query_formats(AVFilterContext *ctx)
336 BufferSinkContext *buf = ctx->priv;
337 AVFilterFormats *formats = NULL;
338 AVFilterChannelLayouts *layouts = NULL;
342 CHECK_LIST_SIZE(sample_fmts)
343 CHECK_LIST_SIZE(sample_rates)
344 CHECK_LIST_SIZE(channel_layouts)
345 CHECK_LIST_SIZE(channel_counts)
347 if (buf->sample_fmts_size) {
348 for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
349 if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)
351 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
355 if (buf->channel_layouts_size || buf->channel_counts_size ||
356 buf->all_channel_counts) {
357 for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
358 if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0)
360 for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
361 if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0)
363 if (buf->all_channel_counts) {
365 av_log(ctx, AV_LOG_WARNING,
366 "Conflicting all_channel_counts and list in options\n");
367 else if (!(layouts = ff_all_channel_counts()))
368 return AVERROR(ENOMEM);
370 if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
374 if (buf->sample_rates_size) {
376 for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
377 if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0)
379 if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
386 #define OFFSET(x) offsetof(BufferSinkContext, x)
387 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
388 static const AVOption buffersink_options[] = {
389 { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
393 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
394 static const AVOption abuffersink_options[] = {
395 { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
396 { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
397 { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
398 { "channel_counts", "set the supported channel counts", OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
399 { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
404 AVFILTER_DEFINE_CLASS(buffersink);
405 AVFILTER_DEFINE_CLASS(abuffersink);
407 static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
410 .type = AVMEDIA_TYPE_VIDEO,
411 .filter_frame = filter_frame,
416 AVFilter ff_vsink_buffer = {
417 .name = "buffersink",
418 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
419 .priv_size = sizeof(BufferSinkContext),
420 .priv_class = &buffersink_class,
421 .init_opaque = vsink_init,
424 .query_formats = vsink_query_formats,
425 .inputs = avfilter_vsink_buffer_inputs,
429 static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
432 .type = AVMEDIA_TYPE_AUDIO,
433 .filter_frame = filter_frame,
438 AVFilter ff_asink_abuffer = {
439 .name = "abuffersink",
440 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
441 .priv_class = &abuffersink_class,
442 .priv_size = sizeof(BufferSinkContext),
443 .init_opaque = asink_init,
446 .query_formats = asink_query_formats,
447 .inputs = avfilter_asink_abuffer_inputs,