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 ((ret = avfilter_request_frame(inlink)) < 0)
133 if (!av_fifo_size(buf->fifo))
134 return AVERROR(EINVAL);
136 if (flags & AV_BUFFERSINK_FLAG_PEEK)
137 *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
139 av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
144 int av_buffersink_poll_frame(AVFilterContext *ctx)
146 BufferSinkContext *buf = ctx->priv;
147 AVFilterLink *inlink = ctx->inputs[0];
149 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
152 #if FF_API_OLD_VSINK_API
153 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
154 AVFilterBufferRef **picref, int flags)
156 return av_buffersink_get_buffer_ref(ctx, picref, flags);
160 #if CONFIG_BUFFERSINK_FILTER
162 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
164 BufferSinkContext *buf = ctx->priv;
165 av_unused AVBufferSinkParams *params;
168 av_log(ctx, AV_LOG_WARNING,
169 "No opaque field provided\n");
170 buf->pixel_fmts = NULL;
172 #if FF_API_OLD_VSINK_API
173 const int *pixel_fmts = (const enum PixelFormat *)opaque;
175 params = (AVBufferSinkParams *)opaque;
176 const int *pixel_fmts = params->pixel_fmts;
178 buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
179 if (!buf->pixel_fmts)
180 return AVERROR(ENOMEM);
183 return common_init(ctx);
186 static av_cold void vsink_uninit(AVFilterContext *ctx)
188 BufferSinkContext *buf = ctx->priv;
189 av_freep(&buf->pixel_fmts);
190 return common_uninit(ctx);
193 static int vsink_query_formats(AVFilterContext *ctx)
195 BufferSinkContext *buf = ctx->priv;
198 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
200 avfilter_default_query_formats(ctx);
205 AVFilter avfilter_vsink_buffersink = {
206 .name = "buffersink",
207 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
208 .priv_size = sizeof(BufferSinkContext),
210 .uninit = vsink_uninit,
212 .query_formats = vsink_query_formats,
214 .inputs = (const AVFilterPad[]) {{ .name = "default",
215 .type = AVMEDIA_TYPE_VIDEO,
216 .end_frame = end_frame,
217 .min_perms = AV_PERM_READ, },
219 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
222 #endif /* CONFIG_BUFFERSINK_FILTER */
224 #if CONFIG_ABUFFERSINK_FILTER
226 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
231 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
233 BufferSinkContext *buf = ctx->priv;
234 AVABufferSinkParams *params;
237 av_log(ctx, AV_LOG_ERROR,
238 "No opaque field provided, an AVABufferSinkParams struct is required\n");
239 return AVERROR(EINVAL);
241 params = (AVABufferSinkParams *)opaque;
243 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
244 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
245 buf->packing_fmts = ff_copy_int_list (params->packing_fmts);
246 if (!buf->sample_fmts || !buf->channel_layouts || !buf->sample_fmts) {
247 av_freep(&buf->sample_fmts);
248 av_freep(&buf->channel_layouts);
249 av_freep(&buf->packing_fmts);
250 return AVERROR(ENOMEM);
253 return common_init(ctx);
256 static av_cold void asink_uninit(AVFilterContext *ctx)
258 BufferSinkContext *buf = ctx->priv;
260 av_freep(&buf->sample_fmts);
261 av_freep(&buf->channel_layouts);
262 av_freep(&buf->packing_fmts);
263 return common_uninit(ctx);
266 static int asink_query_formats(AVFilterContext *ctx)
268 BufferSinkContext *buf = ctx->priv;
269 AVFilterFormats *formats = NULL;
271 if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
272 return AVERROR(ENOMEM);
273 avfilter_set_common_sample_formats(ctx, formats);
275 if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
276 return AVERROR(ENOMEM);
277 avfilter_set_common_channel_layouts(ctx, formats);
279 if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
280 return AVERROR(ENOMEM);
281 avfilter_set_common_packing_formats(ctx, formats);
286 AVFilter avfilter_asink_abuffersink = {
287 .name = "abuffersink",
288 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
290 .uninit = asink_uninit,
291 .priv_size = sizeof(BufferSinkContext),
292 .query_formats = asink_query_formats,
294 .inputs = (const AVFilterPad[]) {{ .name = "default",
295 .type = AVMEDIA_TYPE_AUDIO,
296 .filter_samples = filter_samples,
297 .min_perms = AV_PERM_READ, },
299 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
302 #endif /* CONFIG_ABUFFERSINK_FILTER */