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/audioconvert.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/fifo.h"
30 #include "buffersink.h"
34 AVBufferSinkParams *av_buffersink_params_alloc(void)
36 static const int pixel_fmts[] = { -1 };
37 AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
41 params->pixel_fmts = pixel_fmts;
45 AVABufferSinkParams *av_abuffersink_params_alloc(void)
47 static const int sample_fmts[] = { -1 };
48 static const int64_t channel_layouts[] = { -1 };
49 AVABufferSinkParams *params = av_malloc(sizeof(AVABufferSinkParams));
54 params->sample_fmts = sample_fmts;
55 params->channel_layouts = channel_layouts;
60 AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
61 unsigned warning_limit;
63 /* only used for video */
64 enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
66 /* only used for audio */
67 enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
68 int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
71 #define FIFO_INIT_SIZE 8
73 static av_cold int common_init(AVFilterContext *ctx)
75 BufferSinkContext *buf = ctx->priv;
77 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
79 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
80 return AVERROR(ENOMEM);
82 buf->warning_limit = 100;
86 static av_cold void common_uninit(AVFilterContext *ctx)
88 BufferSinkContext *buf = ctx->priv;
89 AVFilterBufferRef *picref;
92 while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
93 av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
94 avfilter_unref_buffer(picref);
96 av_fifo_free(buf->fifo);
101 static int add_buffer_ref(AVFilterContext *ctx, AVFilterBufferRef *ref)
103 BufferSinkContext *buf = ctx->priv;
105 if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
106 /* realloc fifo size */
107 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
108 av_log(ctx, AV_LOG_ERROR,
109 "Cannot buffer more frames. Consume some available frames "
110 "before adding new ones.\n");
111 return AVERROR(ENOMEM);
116 av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
120 static int end_frame(AVFilterLink *inlink)
122 AVFilterContext *ctx = inlink->dst;
123 BufferSinkContext *buf = inlink->dst->priv;
126 av_assert1(inlink->cur_buf);
127 if ((ret = add_buffer_ref(ctx, inlink->cur_buf)) < 0)
129 inlink->cur_buf = NULL;
130 if (buf->warning_limit &&
131 av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
132 av_log(ctx, AV_LOG_WARNING,
133 "%d buffers queued in %s, something may be wrong.\n",
135 (char *)av_x_if_null(ctx->name, ctx->filter->name));
136 buf->warning_limit *= 10;
141 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
143 AVFilterLink *inlink = ctx->inputs[0];
145 inlink->min_samples = inlink->max_samples =
146 inlink->partial_buf_size = frame_size;
149 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
150 AVFilterBufferRef **bufref, int flags)
152 BufferSinkContext *buf = ctx->priv;
153 AVFilterLink *inlink = ctx->inputs[0];
157 av_assert0( !strcmp(ctx->filter->name, "buffersink")
158 || !strcmp(ctx->filter->name, "abuffersink")
159 || !strcmp(ctx->filter->name, "ffbuffersink")
160 || !strcmp(ctx->filter->name, "ffabuffersink"));
162 /* no picref available, fetch it from the filterchain */
163 if (!av_fifo_size(buf->fifo)) {
164 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
165 return AVERROR(EAGAIN);
166 if ((ret = ff_request_frame(inlink)) < 0)
170 if (!av_fifo_size(buf->fifo))
171 return AVERROR(EINVAL);
173 if (flags & AV_BUFFERSINK_FLAG_PEEK)
174 *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
176 av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
181 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
183 av_assert0( !strcmp(ctx->filter->name, "buffersink")
184 || !strcmp(ctx->filter->name, "ffbuffersink"));
186 return ctx->inputs[0]->frame_rate;
189 int av_buffersink_poll_frame(AVFilterContext *ctx)
191 BufferSinkContext *buf = ctx->priv;
192 AVFilterLink *inlink = ctx->inputs[0];
194 av_assert0( !strcmp(ctx->filter->name, "buffersink")
195 || !strcmp(ctx->filter->name, "abuffersink")
196 || !strcmp(ctx->filter->name, "ffbuffersink")
197 || !strcmp(ctx->filter->name, "ffabuffersink"));
199 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
202 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
204 BufferSinkContext *buf = ctx->priv;
205 AVBufferSinkParams *params = opaque;
207 if (params && params->pixel_fmts) {
208 const int *pixel_fmts = params->pixel_fmts;
210 buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
211 if (!buf->pixel_fmts)
212 return AVERROR(ENOMEM);
215 return common_init(ctx);
218 static av_cold void vsink_uninit(AVFilterContext *ctx)
220 BufferSinkContext *buf = ctx->priv;
221 av_freep(&buf->pixel_fmts);
225 static int vsink_query_formats(AVFilterContext *ctx)
227 BufferSinkContext *buf = ctx->priv;
230 ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
232 ff_default_query_formats(ctx);
237 AVFilter avfilter_vsink_ffbuffersink = {
238 .name = "ffbuffersink",
239 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
240 .priv_size = sizeof(BufferSinkContext),
241 .init_opaque = vsink_init,
242 .uninit = vsink_uninit,
244 .query_formats = vsink_query_formats,
246 .inputs = (const AVFilterPad[]) {{ .name = "default",
247 .type = AVMEDIA_TYPE_VIDEO,
248 .end_frame = end_frame,
249 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
251 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
254 AVFilter avfilter_vsink_buffersink = {
255 .name = "buffersink",
256 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
257 .priv_size = sizeof(BufferSinkContext),
258 .init_opaque = vsink_init,
259 .uninit = vsink_uninit,
261 .query_formats = vsink_query_formats,
263 .inputs = (const AVFilterPad[]) {{ .name = "default",
264 .type = AVMEDIA_TYPE_VIDEO,
265 .end_frame = end_frame,
266 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
268 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
271 static int filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
277 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
279 BufferSinkContext *buf = ctx->priv;
280 AVABufferSinkParams *params = opaque;
282 if (params && params->sample_fmts) {
283 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
284 if (!buf->sample_fmts)
287 if (params && params->channel_layouts) {
288 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
289 if (!buf->channel_layouts)
292 if (!common_init(ctx))
296 av_freep(&buf->sample_fmts);
297 av_freep(&buf->channel_layouts);
298 return AVERROR(ENOMEM);
301 static av_cold void asink_uninit(AVFilterContext *ctx)
303 BufferSinkContext *buf = ctx->priv;
305 av_freep(&buf->sample_fmts);
306 av_freep(&buf->channel_layouts);
310 static int asink_query_formats(AVFilterContext *ctx)
312 BufferSinkContext *buf = ctx->priv;
313 AVFilterFormats *formats = NULL;
314 AVFilterChannelLayouts *layouts = NULL;
316 if (buf->sample_fmts) {
317 if (!(formats = ff_make_format_list(buf->sample_fmts)))
318 return AVERROR(ENOMEM);
319 ff_set_common_formats(ctx, formats);
322 if (buf->channel_layouts) {
323 if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
324 return AVERROR(ENOMEM);
325 ff_set_common_channel_layouts(ctx, layouts);
331 AVFilter avfilter_asink_ffabuffersink = {
332 .name = "ffabuffersink",
333 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
334 .init_opaque = asink_init,
335 .uninit = asink_uninit,
336 .priv_size = sizeof(BufferSinkContext),
337 .query_formats = asink_query_formats,
339 .inputs = (const AVFilterPad[]) {{ .name = "default",
340 .type = AVMEDIA_TYPE_AUDIO,
341 .filter_samples = filter_samples,
342 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
344 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
347 AVFilter avfilter_asink_abuffersink = {
348 .name = "abuffersink",
349 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
350 .init_opaque = asink_init,
351 .uninit = asink_uninit,
352 .priv_size = sizeof(BufferSinkContext),
353 .query_formats = asink_query_formats,
355 .inputs = (const AVFilterPad[]) {{ .name = "default",
356 .type = AVMEDIA_TYPE_AUDIO,
357 .filter_samples = filter_samples,
358 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
360 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
363 /* Libav compatibility API */
365 extern AVFilter avfilter_vsink_buffer;
366 extern AVFilter avfilter_asink_abuffer;
368 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
370 AVFilterBufferRef *tbuf;
373 if (ctx->filter-> inputs[0].start_frame ==
374 avfilter_vsink_buffer. inputs[0].start_frame ||
375 ctx->filter-> inputs[0].filter_samples ==
376 avfilter_asink_abuffer.inputs[0].filter_samples)
377 return ff_buffersink_read_compat(ctx, buf);
378 av_assert0(ctx->filter-> inputs[0].end_frame ==
379 avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
380 ctx->filter-> inputs[0].filter_samples ==
381 avfilter_asink_ffabuffersink.inputs[0].filter_samples);
383 ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
384 buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
393 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
396 BufferSinkContext *sink = ctx->priv;
397 int ret = 0, have_samples = 0, need_samples;
398 AVFilterBufferRef *tbuf, *in_buf;
399 AVFilterLink *link = ctx->inputs[0];
400 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
402 if (ctx->filter-> inputs[0].filter_samples ==
403 avfilter_asink_abuffer.inputs[0].filter_samples)
404 return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
405 av_assert0(ctx->filter-> inputs[0].filter_samples ==
406 avfilter_asink_ffabuffersink.inputs[0].filter_samples);
408 tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
410 return AVERROR(ENOMEM);
412 while (have_samples < nb_samples) {
413 ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
414 AV_BUFFERSINK_FLAG_PEEK);
416 if (ret == AVERROR_EOF && have_samples) {
417 nb_samples = have_samples;
423 need_samples = FFMIN(in_buf->audio->nb_samples,
424 nb_samples - have_samples);
425 av_samples_copy(tbuf->extended_data, in_buf->extended_data,
426 have_samples, 0, need_samples,
427 nb_channels, in_buf->format);
428 have_samples += need_samples;
429 if (need_samples < in_buf->audio->nb_samples) {
430 in_buf->audio->nb_samples -= need_samples;
431 av_samples_copy(in_buf->extended_data, in_buf->extended_data,
432 0, need_samples, in_buf->audio->nb_samples,
433 nb_channels, in_buf->format);
435 av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
436 avfilter_unref_buffer(in_buf);
439 tbuf->audio->nb_samples = have_samples;
442 av_assert0(!av_fifo_size(sink->fifo));
444 add_buffer_ref(ctx, tbuf);
446 avfilter_unref_buffer(tbuf);