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/fifo.h"
28 #include "buffersink.h"
31 AVBufferSinkParams *av_buffersink_params_alloc(void)
33 static const int pixel_fmts[] = { -1 };
34 AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
38 params->pixel_fmts = pixel_fmts;
42 AVABufferSinkParams *av_abuffersink_params_alloc(void)
44 static const int sample_fmts[] = { -1 };
45 static const int packing_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;
54 params->packing_fmts = packing_fmts;
59 AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
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
67 int *packing_fmts; ///< list of accepted packing formats, terminated by -1
70 #define FIFO_INIT_SIZE 8
72 static av_cold int common_init(AVFilterContext *ctx)
74 BufferSinkContext *buf = ctx->priv;
76 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
78 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
79 return AVERROR(ENOMEM);
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 void end_frame(AVFilterLink *inlink)
101 AVFilterContext *ctx = inlink->dst;
102 BufferSinkContext *buf = inlink->dst->priv;
104 if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
105 /* realloc fifo size */
106 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
107 av_log(ctx, AV_LOG_ERROR,
108 "Cannot buffer more frames. Consume some available frames "
109 "before adding new ones.\n");
115 av_fifo_generic_write(buf->fifo,
116 &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
119 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
120 AVFilterBufferRef **bufref, int flags)
122 BufferSinkContext *buf = ctx->priv;
123 AVFilterLink *inlink = ctx->inputs[0];
127 /* no picref available, fetch it from the filterchain */
128 if (!av_fifo_size(buf->fifo)) {
129 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
130 return AVERROR(EAGAIN);
131 if ((ret = avfilter_request_frame(inlink)) < 0)
135 if (!av_fifo_size(buf->fifo))
136 return AVERROR(EINVAL);
138 if (flags & AV_BUFFERSINK_FLAG_PEEK)
139 *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
141 av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
146 int av_buffersink_poll_frame(AVFilterContext *ctx)
148 BufferSinkContext *buf = ctx->priv;
149 AVFilterLink *inlink = ctx->inputs[0];
151 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
154 #if FF_API_OLD_VSINK_API
155 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
156 AVFilterBufferRef **picref, int flags)
158 return av_buffersink_get_buffer_ref(ctx, picref, flags);
162 #if CONFIG_BUFFERSINK_FILTER
164 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
166 BufferSinkContext *buf = ctx->priv;
167 av_unused AVBufferSinkParams *params;
170 av_log(ctx, AV_LOG_WARNING,
171 "No opaque field provided\n");
172 buf->pixel_fmts = NULL;
174 #if FF_API_OLD_VSINK_API
175 const int *pixel_fmts = (const enum PixelFormat *)opaque;
177 params = (AVBufferSinkParams *)opaque;
178 const int *pixel_fmts = params->pixel_fmts;
180 buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
181 if (!buf->pixel_fmts)
182 return AVERROR(ENOMEM);
185 return common_init(ctx);
188 static av_cold void vsink_uninit(AVFilterContext *ctx)
190 BufferSinkContext *buf = ctx->priv;
191 av_freep(&buf->pixel_fmts);
192 return common_uninit(ctx);
195 static int vsink_query_formats(AVFilterContext *ctx)
197 BufferSinkContext *buf = ctx->priv;
200 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
202 avfilter_default_query_formats(ctx);
207 AVFilter avfilter_vsink_buffersink = {
208 .name = "buffersink",
209 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
210 .priv_size = sizeof(BufferSinkContext),
212 .uninit = vsink_uninit,
214 .query_formats = vsink_query_formats,
216 .inputs = (const AVFilterPad[]) {{ .name = "default",
217 .type = AVMEDIA_TYPE_VIDEO,
218 .end_frame = end_frame,
219 .min_perms = AV_PERM_READ, },
221 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
224 #endif /* CONFIG_BUFFERSINK_FILTER */
226 #if CONFIG_ABUFFERSINK_FILTER
228 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
233 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
235 BufferSinkContext *buf = ctx->priv;
236 AVABufferSinkParams *params;
239 av_log(ctx, AV_LOG_ERROR,
240 "No opaque field provided, an AVABufferSinkParams struct is required\n");
241 return AVERROR(EINVAL);
243 params = (AVABufferSinkParams *)opaque;
245 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
246 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
247 buf->packing_fmts = ff_copy_int_list (params->packing_fmts);
248 if (!buf->sample_fmts || !buf->channel_layouts || !buf->sample_fmts) {
249 av_freep(&buf->sample_fmts);
250 av_freep(&buf->channel_layouts);
251 av_freep(&buf->packing_fmts);
252 return AVERROR(ENOMEM);
255 return common_init(ctx);
258 static av_cold void asink_uninit(AVFilterContext *ctx)
260 BufferSinkContext *buf = ctx->priv;
262 av_freep(&buf->sample_fmts);
263 av_freep(&buf->channel_layouts);
264 av_freep(&buf->packing_fmts);
265 return common_uninit(ctx);
268 static int asink_query_formats(AVFilterContext *ctx)
270 BufferSinkContext *buf = ctx->priv;
271 AVFilterFormats *formats = NULL;
273 if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
274 return AVERROR(ENOMEM);
275 avfilter_set_common_sample_formats(ctx, formats);
277 if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
278 return AVERROR(ENOMEM);
279 avfilter_set_common_channel_layouts(ctx, formats);
281 if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
282 return AVERROR(ENOMEM);
283 avfilter_set_common_packing_formats(ctx, formats);
288 AVFilter avfilter_asink_abuffersink = {
289 .name = "abuffersink",
290 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
292 .uninit = asink_uninit,
293 .priv_size = sizeof(BufferSinkContext),
294 .query_formats = asink_query_formats,
296 .inputs = (const AVFilterPad[]) {{ .name = "default",
297 .type = AVMEDIA_TYPE_AUDIO,
298 .filter_samples = filter_samples,
299 .min_perms = AV_PERM_READ, },
301 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
304 #endif /* CONFIG_ABUFFERSINK_FILTER */