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/avassert.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/fifo.h"
30 #include "buffersink.h"
34 AVBufferSinkParams *av_buffersink_params_alloc(void)
36 static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
37 AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
41 params->pixel_fmts = pixel_fmts;
45 AVABufferSinkParams *av_abuffersink_params_alloc(void)
47 AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
55 AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
56 unsigned warning_limit;
58 /* only used for video */
59 enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
61 /* only used for audio */
62 enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
63 int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
64 int all_channel_counts;
67 #define FIFO_INIT_SIZE 8
69 static av_cold int common_init(AVFilterContext *ctx)
71 BufferSinkContext *buf = ctx->priv;
73 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
75 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
76 return AVERROR(ENOMEM);
78 buf->warning_limit = 100;
82 static av_cold void common_uninit(AVFilterContext *ctx)
84 BufferSinkContext *buf = ctx->priv;
85 AVFilterBufferRef *picref;
88 while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
89 av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
90 avfilter_unref_buffer(picref);
92 av_fifo_free(buf->fifo);
97 static int add_buffer_ref(AVFilterContext *ctx, AVFilterBufferRef *ref)
99 BufferSinkContext *buf = ctx->priv;
101 if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
102 /* realloc fifo size */
103 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
104 av_log(ctx, AV_LOG_ERROR,
105 "Cannot buffer more frames. Consume some available frames "
106 "before adding new ones.\n");
107 return AVERROR(ENOMEM);
112 av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
116 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
118 AVFilterContext *ctx = inlink->dst;
119 BufferSinkContext *buf = inlink->dst->priv;
122 if ((ret = add_buffer_ref(ctx, ref)) < 0)
124 if (buf->warning_limit &&
125 av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
126 av_log(ctx, AV_LOG_WARNING,
127 "%d buffers queued in %s, something may be wrong.\n",
129 (char *)av_x_if_null(ctx->name, ctx->filter->name));
130 buf->warning_limit *= 10;
135 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
137 AVFilterLink *inlink = ctx->inputs[0];
139 inlink->min_samples = inlink->max_samples =
140 inlink->partial_buf_size = frame_size;
143 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
144 AVFilterBufferRef **bufref, int flags)
146 BufferSinkContext *buf = ctx->priv;
147 AVFilterLink *inlink = ctx->inputs[0];
151 av_assert0( !strcmp(ctx->filter->name, "buffersink")
152 || !strcmp(ctx->filter->name, "abuffersink")
153 || !strcmp(ctx->filter->name, "ffbuffersink")
154 || !strcmp(ctx->filter->name, "ffabuffersink"));
156 /* no picref available, fetch it from the filterchain */
157 if (!av_fifo_size(buf->fifo)) {
158 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
159 return AVERROR(EAGAIN);
160 if ((ret = ff_request_frame(inlink)) < 0)
164 if (!av_fifo_size(buf->fifo))
165 return AVERROR(EINVAL);
167 if (flags & AV_BUFFERSINK_FLAG_PEEK)
168 *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
170 av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
175 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
177 av_assert0( !strcmp(ctx->filter->name, "buffersink")
178 || !strcmp(ctx->filter->name, "ffbuffersink"));
180 return ctx->inputs[0]->frame_rate;
183 int av_buffersink_poll_frame(AVFilterContext *ctx)
185 BufferSinkContext *buf = ctx->priv;
186 AVFilterLink *inlink = ctx->inputs[0];
188 av_assert0( !strcmp(ctx->filter->name, "buffersink")
189 || !strcmp(ctx->filter->name, "abuffersink")
190 || !strcmp(ctx->filter->name, "ffbuffersink")
191 || !strcmp(ctx->filter->name, "ffabuffersink"));
193 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
196 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
198 BufferSinkContext *buf = ctx->priv;
199 AVBufferSinkParams *params = opaque;
201 if (params && params->pixel_fmts) {
202 const int *pixel_fmts = params->pixel_fmts;
204 buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
205 if (!buf->pixel_fmts)
206 return AVERROR(ENOMEM);
209 return common_init(ctx);
212 static av_cold void vsink_uninit(AVFilterContext *ctx)
214 BufferSinkContext *buf = ctx->priv;
215 av_freep(&buf->pixel_fmts);
219 static int vsink_query_formats(AVFilterContext *ctx)
221 BufferSinkContext *buf = ctx->priv;
224 ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
226 ff_default_query_formats(ctx);
231 static const AVFilterPad ffbuffersink_inputs[] = {
234 .type = AVMEDIA_TYPE_VIDEO,
235 .filter_frame = filter_frame,
236 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
241 AVFilter avfilter_vsink_ffbuffersink = {
242 .name = "ffbuffersink",
243 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
244 .priv_size = sizeof(BufferSinkContext),
245 .init_opaque = vsink_init,
246 .uninit = vsink_uninit,
248 .query_formats = vsink_query_formats,
249 .inputs = ffbuffersink_inputs,
253 static const AVFilterPad buffersink_inputs[] = {
256 .type = AVMEDIA_TYPE_VIDEO,
257 .filter_frame = filter_frame,
258 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
263 AVFilter avfilter_vsink_buffersink = {
264 .name = "buffersink",
265 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
266 .priv_size = sizeof(BufferSinkContext),
267 .init_opaque = vsink_init,
268 .uninit = vsink_uninit,
270 .query_formats = vsink_query_formats,
271 .inputs = buffersink_inputs,
275 static int64_t *concat_channels_lists(const int64_t *layouts, const int *counts)
277 int nb_layouts = 0, nb_counts = 0, i;
281 for (; layouts[nb_layouts] != -1; nb_layouts++);
283 for (; counts[nb_counts] != -1; nb_counts++);
284 if (nb_counts > INT_MAX - 1 - nb_layouts)
286 if (!(list = av_calloc(nb_layouts + nb_counts + 1, sizeof(*list))))
288 for (i = 0; i < nb_layouts; i++)
289 list[i] = layouts[i];
290 for (i = 0; i < nb_counts; i++)
291 list[nb_layouts + i] = FF_COUNT2LAYOUT(counts[i]);
292 list[nb_layouts + nb_counts] = -1;
296 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
298 BufferSinkContext *buf = ctx->priv;
299 AVABufferSinkParams *params = opaque;
301 if (params && params->sample_fmts) {
302 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
303 if (!buf->sample_fmts)
304 return AVERROR(ENOMEM);
306 if (params && (params->channel_layouts || params->channel_counts)) {
307 if (params->all_channel_counts) {
308 av_log(ctx, AV_LOG_ERROR,
309 "Conflicting all_channel_counts and list in parameters\n");
310 return AVERROR(EINVAL);
312 buf->channel_layouts = concat_channels_lists(params->channel_layouts,
313 params->channel_counts);
314 if (!buf->channel_layouts)
315 return AVERROR(ENOMEM);
318 buf->all_channel_counts = params->all_channel_counts;
319 return common_init(ctx);
322 static av_cold void asink_uninit(AVFilterContext *ctx)
324 BufferSinkContext *buf = ctx->priv;
326 av_freep(&buf->sample_fmts);
327 av_freep(&buf->channel_layouts);
331 static int asink_query_formats(AVFilterContext *ctx)
333 BufferSinkContext *buf = ctx->priv;
334 AVFilterFormats *formats = NULL;
335 AVFilterChannelLayouts *layouts = NULL;
337 if (buf->sample_fmts) {
338 if (!(formats = ff_make_format_list(buf->sample_fmts)))
339 return AVERROR(ENOMEM);
340 ff_set_common_formats(ctx, formats);
343 if (buf->channel_layouts || buf->all_channel_counts) {
344 layouts = buf->all_channel_counts ? ff_all_channel_counts() :
345 avfilter_make_format64_list(buf->channel_layouts);
347 return AVERROR(ENOMEM);
348 ff_set_common_channel_layouts(ctx, layouts);
354 static const AVFilterPad ffabuffersink_inputs[] = {
357 .type = AVMEDIA_TYPE_AUDIO,
358 .filter_frame = filter_frame,
359 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
364 AVFilter avfilter_asink_ffabuffersink = {
365 .name = "ffabuffersink",
366 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
367 .init_opaque = asink_init,
368 .uninit = asink_uninit,
369 .priv_size = sizeof(BufferSinkContext),
370 .query_formats = asink_query_formats,
371 .inputs = ffabuffersink_inputs,
375 static const AVFilterPad abuffersink_inputs[] = {
378 .type = AVMEDIA_TYPE_AUDIO,
379 .filter_frame = filter_frame,
380 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
385 AVFilter avfilter_asink_abuffersink = {
386 .name = "abuffersink",
387 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
388 .init_opaque = asink_init,
389 .uninit = asink_uninit,
390 .priv_size = sizeof(BufferSinkContext),
391 .query_formats = asink_query_formats,
392 .inputs = abuffersink_inputs,
396 /* Libav compatibility API */
398 extern AVFilter avfilter_vsink_buffer;
399 extern AVFilter avfilter_asink_abuffer;
401 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
403 AVFilterBufferRef *tbuf;
406 if (ctx->filter-> inputs[0].start_frame ==
407 avfilter_vsink_buffer. inputs[0].start_frame ||
408 ctx->filter-> inputs[0].filter_frame ==
409 avfilter_asink_abuffer.inputs[0].filter_frame)
410 return ff_buffersink_read_compat(ctx, buf);
411 av_assert0(ctx->filter-> inputs[0].end_frame ==
412 avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
413 ctx->filter-> inputs[0].filter_frame ==
414 avfilter_asink_ffabuffersink.inputs[0].filter_frame);
416 ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
417 buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
426 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
429 BufferSinkContext *sink = ctx->priv;
430 int ret = 0, have_samples = 0, need_samples;
431 AVFilterBufferRef *tbuf, *in_buf;
432 AVFilterLink *link = ctx->inputs[0];
433 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
435 if (ctx->filter-> inputs[0].filter_frame ==
436 avfilter_asink_abuffer.inputs[0].filter_frame)
437 return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
438 av_assert0(ctx->filter-> inputs[0].filter_frame ==
439 avfilter_asink_ffabuffersink.inputs[0].filter_frame);
441 tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
443 return AVERROR(ENOMEM);
445 while (have_samples < nb_samples) {
446 ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
447 AV_BUFFERSINK_FLAG_PEEK);
449 if (ret == AVERROR_EOF && have_samples) {
450 nb_samples = have_samples;
456 need_samples = FFMIN(in_buf->audio->nb_samples,
457 nb_samples - have_samples);
458 av_samples_copy(tbuf->extended_data, in_buf->extended_data,
459 have_samples, 0, need_samples,
460 nb_channels, in_buf->format);
461 have_samples += need_samples;
462 if (need_samples < in_buf->audio->nb_samples) {
463 in_buf->audio->nb_samples -= need_samples;
464 av_samples_copy(in_buf->extended_data, in_buf->extended_data,
465 0, need_samples, in_buf->audio->nb_samples,
466 nb_channels, in_buf->format);
468 av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
469 avfilter_unref_buffer(in_buf);
472 tbuf->audio->nb_samples = have_samples;
475 av_assert0(!av_fifo_size(sink->fifo));
477 add_buffer_ref(ctx, tbuf);
479 avfilter_unref_buffer(tbuf);