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_ERROR,
169 "No opaque field provided\n");
170 return AVERROR(EINVAL);
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;
197 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
201 AVFilter avfilter_vsink_buffersink = {
202 .name = "buffersink",
203 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
204 .priv_size = sizeof(BufferSinkContext),
206 .uninit = vsink_uninit,
208 .query_formats = vsink_query_formats,
210 .inputs = (const AVFilterPad[]) {{ .name = "default",
211 .type = AVMEDIA_TYPE_VIDEO,
212 .end_frame = end_frame,
213 .min_perms = AV_PERM_READ, },
215 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
218 #endif /* CONFIG_BUFFERSINK_FILTER */
220 #if CONFIG_ABUFFERSINK_FILTER
222 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
227 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
229 BufferSinkContext *buf = ctx->priv;
230 AVABufferSinkParams *params;
233 av_log(ctx, AV_LOG_ERROR,
234 "No opaque field provided, an AVABufferSinkParams struct is required\n");
235 return AVERROR(EINVAL);
237 params = (AVABufferSinkParams *)opaque;
239 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
240 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
241 buf->packing_fmts = ff_copy_int_list (params->packing_fmts);
242 if (!buf->sample_fmts || !buf->channel_layouts || !buf->sample_fmts) {
243 av_freep(&buf->sample_fmts);
244 av_freep(&buf->channel_layouts);
245 av_freep(&buf->packing_fmts);
246 return AVERROR(ENOMEM);
249 return common_init(ctx);
252 static av_cold void asink_uninit(AVFilterContext *ctx)
254 BufferSinkContext *buf = ctx->priv;
256 av_freep(&buf->sample_fmts);
257 av_freep(&buf->channel_layouts);
258 av_freep(&buf->packing_fmts);
259 return common_uninit(ctx);
262 static int asink_query_formats(AVFilterContext *ctx)
264 BufferSinkContext *buf = ctx->priv;
265 AVFilterFormats *formats = NULL;
267 if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
268 return AVERROR(ENOMEM);
269 avfilter_set_common_sample_formats(ctx, formats);
271 if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
272 return AVERROR(ENOMEM);
273 avfilter_set_common_channel_layouts(ctx, formats);
275 if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
276 return AVERROR(ENOMEM);
277 avfilter_set_common_packing_formats(ctx, formats);
282 AVFilter avfilter_asink_abuffersink = {
283 .name = "abuffersink",
284 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
286 .uninit = asink_uninit,
287 .priv_size = sizeof(BufferSinkContext),
288 .query_formats = asink_query_formats,
290 .inputs = (const AVFilterPad[]) {{ .name = "default",
291 .type = AVMEDIA_TYPE_AUDIO,
292 .filter_samples = filter_samples,
293 .min_perms = AV_PERM_READ, },
295 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
298 #endif /* CONFIG_ABUFFERSINK_FILTER */