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/fifo.h"
29 #include "buffersink.h"
32 AVBufferSinkParams *av_buffersink_params_alloc(void)
34 static const int pixel_fmts[] = { -1 };
35 AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
39 params->pixel_fmts = pixel_fmts;
43 AVABufferSinkParams *av_abuffersink_params_alloc(void)
45 static const int sample_fmts[] = { -1 };
46 static const int64_t channel_layouts[] = { -1 };
47 AVABufferSinkParams *params = av_malloc(sizeof(AVABufferSinkParams));
52 params->sample_fmts = sample_fmts;
53 params->channel_layouts = channel_layouts;
58 AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
59 unsigned warning_limit;
61 /* only used for video */
62 enum PixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
64 /* only used for audio */
65 enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
66 int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
69 #define FIFO_INIT_SIZE 8
71 static av_cold int common_init(AVFilterContext *ctx)
73 BufferSinkContext *buf = ctx->priv;
75 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
77 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
78 return AVERROR(ENOMEM);
80 buf->warning_limit = 100;
84 static av_cold void common_uninit(AVFilterContext *ctx)
86 BufferSinkContext *buf = ctx->priv;
87 AVFilterBufferRef *picref;
90 while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
91 av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
92 avfilter_unref_buffer(picref);
94 av_fifo_free(buf->fifo);
99 static int end_frame(AVFilterLink *inlink)
101 AVFilterContext *ctx = inlink->dst;
102 BufferSinkContext *buf = inlink->dst->priv;
104 av_assert1(inlink->cur_buf);
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,
117 &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
118 inlink->cur_buf = NULL;
119 if (buf->warning_limit &&
120 av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
121 av_log(ctx, AV_LOG_WARNING,
122 "%d buffers queued in %s, something may be wrong.\n",
124 (char *)av_x_if_null(ctx->name, ctx->filter->name));
125 buf->warning_limit *= 10;
130 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
132 AVFilterLink *inlink = ctx->inputs[0];
134 inlink->min_samples = inlink->max_samples =
135 inlink->partial_buf_size = frame_size;
138 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
139 AVFilterBufferRef **bufref, int flags)
141 BufferSinkContext *buf = ctx->priv;
142 AVFilterLink *inlink = ctx->inputs[0];
146 av_assert0(!strcmp(ctx->filter->name, "buffersink") || !strcmp(ctx->filter->name, "abuffersink"));
148 /* no picref available, fetch it from the filterchain */
149 if (!av_fifo_size(buf->fifo)) {
150 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
151 return AVERROR(EAGAIN);
152 if ((ret = ff_request_frame(inlink)) < 0)
156 if (!av_fifo_size(buf->fifo))
157 return AVERROR(EINVAL);
159 if (flags & AV_BUFFERSINK_FLAG_PEEK)
160 *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
162 av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
167 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
169 av_assert0(!strcmp(ctx->filter->name, "buffersink"));
171 return ctx->inputs[0]->frame_rate;
174 int av_buffersink_poll_frame(AVFilterContext *ctx)
176 BufferSinkContext *buf = ctx->priv;
177 AVFilterLink *inlink = ctx->inputs[0];
179 av_assert0(!strcmp(ctx->filter->name, "buffersink") || !strcmp(ctx->filter->name, "abuffersink"));
181 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
184 #if CONFIG_BUFFERSINK_FILTER
186 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
188 BufferSinkContext *buf = ctx->priv;
189 AVBufferSinkParams *params = opaque;
191 if (params && buf->pixel_fmts) {
192 const int *pixel_fmts = params->pixel_fmts;
194 buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
195 if (!buf->pixel_fmts)
196 return AVERROR(ENOMEM);
199 return common_init(ctx);
202 static av_cold void vsink_uninit(AVFilterContext *ctx)
204 BufferSinkContext *buf = ctx->priv;
205 av_freep(&buf->pixel_fmts);
209 static int vsink_query_formats(AVFilterContext *ctx)
211 BufferSinkContext *buf = ctx->priv;
214 ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
216 ff_default_query_formats(ctx);
221 AVFilter avfilter_vsink_buffersink = {
222 .name = "buffersink",
223 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
224 .priv_size = sizeof(BufferSinkContext),
225 .init_opaque = vsink_init,
226 .uninit = vsink_uninit,
228 .query_formats = vsink_query_formats,
230 .inputs = (const AVFilterPad[]) {{ .name = "default",
231 .type = AVMEDIA_TYPE_VIDEO,
232 .end_frame = end_frame,
233 .min_perms = AV_PERM_READ, },
235 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
238 #endif /* CONFIG_BUFFERSINK_FILTER */
240 #if CONFIG_ABUFFERSINK_FILTER
242 static int filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
248 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
250 BufferSinkContext *buf = ctx->priv;
251 AVABufferSinkParams *params = opaque;
253 if (params && params->sample_fmts) {
254 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
255 if (!buf->sample_fmts)
258 if (params && params->channel_layouts) {
259 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
260 if (!buf->channel_layouts)
263 if (!common_init(ctx))
267 av_freep(&buf->sample_fmts);
268 av_freep(&buf->channel_layouts);
269 return AVERROR(ENOMEM);
272 static av_cold void asink_uninit(AVFilterContext *ctx)
274 BufferSinkContext *buf = ctx->priv;
276 av_freep(&buf->sample_fmts);
277 av_freep(&buf->channel_layouts);
281 static int asink_query_formats(AVFilterContext *ctx)
283 BufferSinkContext *buf = ctx->priv;
284 AVFilterFormats *formats = NULL;
285 AVFilterChannelLayouts *layouts = NULL;
287 if (buf->sample_fmts) {
288 if (!(formats = ff_make_format_list(buf->sample_fmts)))
289 return AVERROR(ENOMEM);
290 ff_set_common_formats(ctx, formats);
293 if (buf->channel_layouts) {
294 if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
295 return AVERROR(ENOMEM);
296 ff_set_common_channel_layouts(ctx, layouts);
302 AVFilter avfilter_asink_abuffersink = {
303 .name = "abuffersink",
304 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
305 .init_opaque = asink_init,
306 .uninit = asink_uninit,
307 .priv_size = sizeof(BufferSinkContext),
308 .query_formats = asink_query_formats,
310 .inputs = (const AVFilterPad[]) {{ .name = "default",
311 .type = AVMEDIA_TYPE_AUDIO,
312 .filter_samples = filter_samples,
313 .min_perms = AV_PERM_READ, },
315 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
318 #endif /* CONFIG_ABUFFERSINK_FILTER */