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 static const int sample_fmts[] = { AV_SAMPLE_FMT_NONE };
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 static const AVFilterPad ffbuffersink_inputs[] = {
240 .type = AVMEDIA_TYPE_VIDEO,
241 .end_frame = end_frame,
242 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
247 AVFilter avfilter_vsink_ffbuffersink = {
248 .name = "ffbuffersink",
249 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
250 .priv_size = sizeof(BufferSinkContext),
251 .init_opaque = vsink_init,
252 .uninit = vsink_uninit,
254 .query_formats = vsink_query_formats,
255 .inputs = ffbuffersink_inputs,
259 static const AVFilterPad buffersink_inputs[] = {
262 .type = AVMEDIA_TYPE_VIDEO,
263 .end_frame = end_frame,
264 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
269 AVFilter avfilter_vsink_buffersink = {
270 .name = "buffersink",
271 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
272 .priv_size = sizeof(BufferSinkContext),
273 .init_opaque = vsink_init,
274 .uninit = vsink_uninit,
276 .query_formats = vsink_query_formats,
277 .inputs = buffersink_inputs,
281 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *samplesref)
287 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
289 BufferSinkContext *buf = ctx->priv;
290 AVABufferSinkParams *params = opaque;
292 if (params && params->sample_fmts) {
293 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
294 if (!buf->sample_fmts)
297 if (params && params->channel_layouts) {
298 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
299 if (!buf->channel_layouts)
302 if (!common_init(ctx))
306 av_freep(&buf->sample_fmts);
307 av_freep(&buf->channel_layouts);
308 return AVERROR(ENOMEM);
311 static av_cold void asink_uninit(AVFilterContext *ctx)
313 BufferSinkContext *buf = ctx->priv;
315 av_freep(&buf->sample_fmts);
316 av_freep(&buf->channel_layouts);
320 static int asink_query_formats(AVFilterContext *ctx)
322 BufferSinkContext *buf = ctx->priv;
323 AVFilterFormats *formats = NULL;
324 AVFilterChannelLayouts *layouts = NULL;
326 if (buf->sample_fmts) {
327 if (!(formats = ff_make_format_list(buf->sample_fmts)))
328 return AVERROR(ENOMEM);
329 ff_set_common_formats(ctx, formats);
332 if (buf->channel_layouts) {
333 if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
334 return AVERROR(ENOMEM);
335 ff_set_common_channel_layouts(ctx, layouts);
341 static const AVFilterPad ffabuffersink_inputs[] = {
344 .type = AVMEDIA_TYPE_AUDIO,
345 .filter_frame = filter_frame,
346 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
351 AVFilter avfilter_asink_ffabuffersink = {
352 .name = "ffabuffersink",
353 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
354 .init_opaque = asink_init,
355 .uninit = asink_uninit,
356 .priv_size = sizeof(BufferSinkContext),
357 .query_formats = asink_query_formats,
358 .inputs = ffabuffersink_inputs,
362 static const AVFilterPad abuffersink_inputs[] = {
365 .type = AVMEDIA_TYPE_AUDIO,
366 .filter_frame = filter_frame,
367 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
372 AVFilter avfilter_asink_abuffersink = {
373 .name = "abuffersink",
374 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
375 .init_opaque = asink_init,
376 .uninit = asink_uninit,
377 .priv_size = sizeof(BufferSinkContext),
378 .query_formats = asink_query_formats,
379 .inputs = abuffersink_inputs,
383 /* Libav compatibility API */
385 extern AVFilter avfilter_vsink_buffer;
386 extern AVFilter avfilter_asink_abuffer;
388 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
390 AVFilterBufferRef *tbuf;
393 if (ctx->filter-> inputs[0].start_frame ==
394 avfilter_vsink_buffer. inputs[0].start_frame ||
395 ctx->filter-> inputs[0].filter_frame ==
396 avfilter_asink_abuffer.inputs[0].filter_frame)
397 return ff_buffersink_read_compat(ctx, buf);
398 av_assert0(ctx->filter-> inputs[0].end_frame ==
399 avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
400 ctx->filter-> inputs[0].filter_frame ==
401 avfilter_asink_ffabuffersink.inputs[0].filter_frame);
403 ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
404 buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
413 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
416 BufferSinkContext *sink = ctx->priv;
417 int ret = 0, have_samples = 0, need_samples;
418 AVFilterBufferRef *tbuf, *in_buf;
419 AVFilterLink *link = ctx->inputs[0];
420 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
422 if (ctx->filter-> inputs[0].filter_frame ==
423 avfilter_asink_abuffer.inputs[0].filter_frame)
424 return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
425 av_assert0(ctx->filter-> inputs[0].filter_frame ==
426 avfilter_asink_ffabuffersink.inputs[0].filter_frame);
428 tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
430 return AVERROR(ENOMEM);
432 while (have_samples < nb_samples) {
433 ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
434 AV_BUFFERSINK_FLAG_PEEK);
436 if (ret == AVERROR_EOF && have_samples) {
437 nb_samples = have_samples;
443 need_samples = FFMIN(in_buf->audio->nb_samples,
444 nb_samples - have_samples);
445 av_samples_copy(tbuf->extended_data, in_buf->extended_data,
446 have_samples, 0, need_samples,
447 nb_channels, in_buf->format);
448 have_samples += need_samples;
449 if (need_samples < in_buf->audio->nb_samples) {
450 in_buf->audio->nb_samples -= need_samples;
451 av_samples_copy(in_buf->extended_data, in_buf->extended_data,
452 0, need_samples, in_buf->audio->nb_samples,
453 nb_channels, in_buf->format);
455 av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
456 avfilter_unref_buffer(in_buf);
459 tbuf->audio->nb_samples = have_samples;
462 av_assert0(!av_fifo_size(sink->fifo));
464 add_buffer_ref(ctx, tbuf);
466 avfilter_unref_buffer(tbuf);