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 filter_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
122 AVFilterContext *ctx = inlink->dst;
123 BufferSinkContext *buf = inlink->dst->priv;
126 if ((ret = add_buffer_ref(ctx, ref)) < 0)
128 if (buf->warning_limit &&
129 av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
130 av_log(ctx, AV_LOG_WARNING,
131 "%d buffers queued in %s, something may be wrong.\n",
133 (char *)av_x_if_null(ctx->name, ctx->filter->name));
134 buf->warning_limit *= 10;
139 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
141 AVFilterLink *inlink = ctx->inputs[0];
143 inlink->min_samples = inlink->max_samples =
144 inlink->partial_buf_size = frame_size;
147 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
148 AVFilterBufferRef **bufref, int flags)
150 BufferSinkContext *buf = ctx->priv;
151 AVFilterLink *inlink = ctx->inputs[0];
155 av_assert0( !strcmp(ctx->filter->name, "buffersink")
156 || !strcmp(ctx->filter->name, "abuffersink")
157 || !strcmp(ctx->filter->name, "ffbuffersink")
158 || !strcmp(ctx->filter->name, "ffabuffersink"));
160 /* no picref available, fetch it from the filterchain */
161 if (!av_fifo_size(buf->fifo)) {
162 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
163 return AVERROR(EAGAIN);
164 if ((ret = ff_request_frame(inlink)) < 0)
168 if (!av_fifo_size(buf->fifo))
169 return AVERROR(EINVAL);
171 if (flags & AV_BUFFERSINK_FLAG_PEEK)
172 *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
174 av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
179 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
181 av_assert0( !strcmp(ctx->filter->name, "buffersink")
182 || !strcmp(ctx->filter->name, "ffbuffersink"));
184 return ctx->inputs[0]->frame_rate;
187 int av_buffersink_poll_frame(AVFilterContext *ctx)
189 BufferSinkContext *buf = ctx->priv;
190 AVFilterLink *inlink = ctx->inputs[0];
192 av_assert0( !strcmp(ctx->filter->name, "buffersink")
193 || !strcmp(ctx->filter->name, "abuffersink")
194 || !strcmp(ctx->filter->name, "ffbuffersink")
195 || !strcmp(ctx->filter->name, "ffabuffersink"));
197 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
200 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
202 BufferSinkContext *buf = ctx->priv;
203 AVBufferSinkParams *params = opaque;
205 if (params && params->pixel_fmts) {
206 const int *pixel_fmts = params->pixel_fmts;
208 buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
209 if (!buf->pixel_fmts)
210 return AVERROR(ENOMEM);
213 return common_init(ctx);
216 static av_cold void vsink_uninit(AVFilterContext *ctx)
218 BufferSinkContext *buf = ctx->priv;
219 av_freep(&buf->pixel_fmts);
223 static int vsink_query_formats(AVFilterContext *ctx)
225 BufferSinkContext *buf = ctx->priv;
228 ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
230 ff_default_query_formats(ctx);
235 static const AVFilterPad ffbuffersink_inputs[] = {
238 .type = AVMEDIA_TYPE_VIDEO,
239 .filter_frame = filter_frame,
240 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
245 AVFilter avfilter_vsink_ffbuffersink = {
246 .name = "ffbuffersink",
247 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
248 .priv_size = sizeof(BufferSinkContext),
249 .init_opaque = vsink_init,
250 .uninit = vsink_uninit,
252 .query_formats = vsink_query_formats,
253 .inputs = ffbuffersink_inputs,
257 static const AVFilterPad buffersink_inputs[] = {
260 .type = AVMEDIA_TYPE_VIDEO,
261 .filter_frame = filter_frame,
262 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
267 AVFilter avfilter_vsink_buffersink = {
268 .name = "buffersink",
269 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
270 .priv_size = sizeof(BufferSinkContext),
271 .init_opaque = vsink_init,
272 .uninit = vsink_uninit,
274 .query_formats = vsink_query_formats,
275 .inputs = buffersink_inputs,
279 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
281 BufferSinkContext *buf = ctx->priv;
282 AVABufferSinkParams *params = opaque;
284 if (params && params->sample_fmts) {
285 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
286 if (!buf->sample_fmts)
289 if (params && params->channel_layouts) {
290 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
291 if (!buf->channel_layouts)
294 if (!common_init(ctx))
298 av_freep(&buf->sample_fmts);
299 av_freep(&buf->channel_layouts);
300 return AVERROR(ENOMEM);
303 static av_cold void asink_uninit(AVFilterContext *ctx)
305 BufferSinkContext *buf = ctx->priv;
307 av_freep(&buf->sample_fmts);
308 av_freep(&buf->channel_layouts);
312 static int asink_query_formats(AVFilterContext *ctx)
314 BufferSinkContext *buf = ctx->priv;
315 AVFilterFormats *formats = NULL;
316 AVFilterChannelLayouts *layouts = NULL;
318 if (buf->sample_fmts) {
319 if (!(formats = ff_make_format_list(buf->sample_fmts)))
320 return AVERROR(ENOMEM);
321 ff_set_common_formats(ctx, formats);
324 if (buf->channel_layouts) {
325 if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
326 return AVERROR(ENOMEM);
327 ff_set_common_channel_layouts(ctx, layouts);
333 static const AVFilterPad ffabuffersink_inputs[] = {
336 .type = AVMEDIA_TYPE_AUDIO,
337 .filter_frame = filter_frame,
338 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
343 AVFilter avfilter_asink_ffabuffersink = {
344 .name = "ffabuffersink",
345 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
346 .init_opaque = asink_init,
347 .uninit = asink_uninit,
348 .priv_size = sizeof(BufferSinkContext),
349 .query_formats = asink_query_formats,
350 .inputs = ffabuffersink_inputs,
354 static const AVFilterPad abuffersink_inputs[] = {
357 .type = AVMEDIA_TYPE_AUDIO,
358 .filter_frame = filter_frame,
359 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
364 AVFilter avfilter_asink_abuffersink = {
365 .name = "abuffersink",
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 = abuffersink_inputs,
375 /* Libav compatibility API */
377 extern AVFilter avfilter_vsink_buffer;
378 extern AVFilter avfilter_asink_abuffer;
380 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
382 AVFilterBufferRef *tbuf;
385 if (ctx->filter-> inputs[0].start_frame ==
386 avfilter_vsink_buffer. inputs[0].start_frame ||
387 ctx->filter-> inputs[0].filter_frame ==
388 avfilter_asink_abuffer.inputs[0].filter_frame)
389 return ff_buffersink_read_compat(ctx, buf);
390 av_assert0(ctx->filter-> inputs[0].end_frame ==
391 avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
392 ctx->filter-> inputs[0].filter_frame ==
393 avfilter_asink_ffabuffersink.inputs[0].filter_frame);
395 ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
396 buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
405 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
408 BufferSinkContext *sink = ctx->priv;
409 int ret = 0, have_samples = 0, need_samples;
410 AVFilterBufferRef *tbuf, *in_buf;
411 AVFilterLink *link = ctx->inputs[0];
412 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
414 if (ctx->filter-> inputs[0].filter_frame ==
415 avfilter_asink_abuffer.inputs[0].filter_frame)
416 return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
417 av_assert0(ctx->filter-> inputs[0].filter_frame ==
418 avfilter_asink_ffabuffersink.inputs[0].filter_frame);
420 tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
422 return AVERROR(ENOMEM);
424 while (have_samples < nb_samples) {
425 ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
426 AV_BUFFERSINK_FLAG_PEEK);
428 if (ret == AVERROR_EOF && have_samples) {
429 nb_samples = have_samples;
435 need_samples = FFMIN(in_buf->audio->nb_samples,
436 nb_samples - have_samples);
437 av_samples_copy(tbuf->extended_data, in_buf->extended_data,
438 have_samples, 0, need_samples,
439 nb_channels, in_buf->format);
440 have_samples += need_samples;
441 if (need_samples < in_buf->audio->nb_samples) {
442 in_buf->audio->nb_samples -= need_samples;
443 av_samples_copy(in_buf->extended_data, in_buf->extended_data,
444 0, need_samples, in_buf->audio->nb_samples,
445 nb_channels, in_buf->format);
447 av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
448 avfilter_unref_buffer(in_buf);
451 tbuf->audio->nb_samples = have_samples;
454 av_assert0(!av_fifo_size(sink->fifo));
456 add_buffer_ref(ctx, tbuf);
458 avfilter_unref_buffer(tbuf);