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 int64_t channel_layouts[] = { -1 };
46 AVABufferSinkParams *params = av_malloc(sizeof(AVABufferSinkParams));
51 params->sample_fmts = sample_fmts;
52 params->channel_layouts = channel_layouts;
57 AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
59 /* only used for video */
60 enum PixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
62 /* only used for audio */
63 enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
64 int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
67 #define FIFO_INIT_SIZE 8
69 static av_cold int common_init(AVFilterContext *ctx)
71 BufferSinkContext *buf = ctx->priv;
73 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
75 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
76 return AVERROR(ENOMEM);
81 static av_cold void common_uninit(AVFilterContext *ctx)
83 BufferSinkContext *buf = ctx->priv;
84 AVFilterBufferRef *picref;
87 while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
88 av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
89 avfilter_unref_buffer(picref);
91 av_fifo_free(buf->fifo);
96 static void end_frame(AVFilterLink *inlink)
98 AVFilterContext *ctx = inlink->dst;
99 BufferSinkContext *buf = inlink->dst->priv;
101 if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
102 /* realloc fifo size */
103 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
104 av_log(ctx, AV_LOG_ERROR,
105 "Cannot buffer more frames. Consume some available frames "
106 "before adding new ones.\n");
112 av_fifo_generic_write(buf->fifo,
113 &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
116 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
117 AVFilterBufferRef **bufref, int flags)
119 BufferSinkContext *buf = ctx->priv;
120 AVFilterLink *inlink = ctx->inputs[0];
124 /* no picref available, fetch it from the filterchain */
125 if (!av_fifo_size(buf->fifo)) {
126 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
127 return AVERROR(EAGAIN);
128 if ((ret = avfilter_request_frame(inlink)) < 0)
132 if (!av_fifo_size(buf->fifo))
133 return AVERROR(EINVAL);
135 if (flags & AV_BUFFERSINK_FLAG_PEEK)
136 *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
138 av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
143 int av_buffersink_poll_frame(AVFilterContext *ctx)
145 BufferSinkContext *buf = ctx->priv;
146 AVFilterLink *inlink = ctx->inputs[0];
148 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
151 #if FF_API_OLD_VSINK_API
152 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
153 AVFilterBufferRef **picref, int flags)
155 return av_buffersink_get_buffer_ref(ctx, picref, flags);
159 #if CONFIG_BUFFERSINK_FILTER
161 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
163 BufferSinkContext *buf = ctx->priv;
164 av_unused AVBufferSinkParams *params;
167 av_log(ctx, AV_LOG_WARNING,
168 "No opaque field provided\n");
169 buf->pixel_fmts = NULL;
171 #if FF_API_OLD_VSINK_API
172 const int *pixel_fmts = (const enum PixelFormat *)opaque;
174 params = (AVBufferSinkParams *)opaque;
175 const int *pixel_fmts = params->pixel_fmts;
177 buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
178 if (!buf->pixel_fmts)
179 return AVERROR(ENOMEM);
182 return common_init(ctx);
185 static av_cold void vsink_uninit(AVFilterContext *ctx)
187 BufferSinkContext *buf = ctx->priv;
188 av_freep(&buf->pixel_fmts);
189 return common_uninit(ctx);
192 static int vsink_query_formats(AVFilterContext *ctx)
194 BufferSinkContext *buf = ctx->priv;
197 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
199 avfilter_default_query_formats(ctx);
204 AVFilter avfilter_vsink_buffersink = {
205 .name = "buffersink",
206 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
207 .priv_size = sizeof(BufferSinkContext),
209 .uninit = vsink_uninit,
211 .query_formats = vsink_query_formats,
213 .inputs = (const AVFilterPad[]) {{ .name = "default",
214 .type = AVMEDIA_TYPE_VIDEO,
215 .end_frame = end_frame,
216 .min_perms = AV_PERM_READ, },
218 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
221 #endif /* CONFIG_BUFFERSINK_FILTER */
223 #if CONFIG_ABUFFERSINK_FILTER
225 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
230 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
232 BufferSinkContext *buf = ctx->priv;
233 AVABufferSinkParams *params;
236 av_log(ctx, AV_LOG_ERROR,
237 "No opaque field provided, an AVABufferSinkParams struct is required\n");
238 return AVERROR(EINVAL);
240 params = (AVABufferSinkParams *)opaque;
242 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
243 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
244 if (!buf->sample_fmts || !buf->channel_layouts) {
245 av_freep(&buf->sample_fmts);
246 av_freep(&buf->channel_layouts);
247 return AVERROR(ENOMEM);
250 return common_init(ctx);
253 static av_cold void asink_uninit(AVFilterContext *ctx)
255 BufferSinkContext *buf = ctx->priv;
257 av_freep(&buf->sample_fmts);
258 av_freep(&buf->channel_layouts);
259 return common_uninit(ctx);
262 static int asink_query_formats(AVFilterContext *ctx)
264 BufferSinkContext *buf = ctx->priv;
265 AVFilterFormats *formats = NULL;
266 AVFilterChannelLayouts *layouts = NULL;
268 if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
269 return AVERROR(ENOMEM);
270 avfilter_set_common_sample_formats(ctx, formats);
272 if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
273 return AVERROR(ENOMEM);
274 ff_set_common_channel_layouts(ctx, layouts);
275 ff_set_common_samplerates (ctx, ff_all_samplerates());
280 AVFilter avfilter_asink_abuffersink = {
281 .name = "abuffersink",
282 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
284 .uninit = asink_uninit,
285 .priv_size = sizeof(BufferSinkContext),
286 .query_formats = asink_query_formats,
288 .inputs = (const AVFilterPad[]) {{ .name = "default",
289 .type = AVMEDIA_TYPE_AUDIO,
290 .filter_samples = filter_samples,
291 .min_perms = AV_PERM_READ, },
293 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
296 #endif /* CONFIG_ABUFFERSINK_FILTER */