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
60 /* only used for video */
61 enum PixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
63 /* only used for audio */
64 enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
65 int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
68 #define FIFO_INIT_SIZE 8
70 static av_cold int common_init(AVFilterContext *ctx)
72 BufferSinkContext *buf = ctx->priv;
74 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
76 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
77 return AVERROR(ENOMEM);
82 static av_cold void common_uninit(AVFilterContext *ctx)
84 BufferSinkContext *buf = ctx->priv;
85 AVFilterBufferRef *picref;
88 while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
89 av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
90 avfilter_unref_buffer(picref);
92 av_fifo_free(buf->fifo);
97 static void end_frame(AVFilterLink *inlink)
99 AVFilterContext *ctx = inlink->dst;
100 BufferSinkContext *buf = inlink->dst->priv;
102 av_assert1(inlink->cur_buf);
103 if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
104 /* realloc fifo size */
105 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
106 av_log(ctx, AV_LOG_ERROR,
107 "Cannot buffer more frames. Consume some available frames "
108 "before adding new ones.\n");
114 av_fifo_generic_write(buf->fifo,
115 &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
118 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
119 AVFilterBufferRef **bufref, int flags)
121 BufferSinkContext *buf = ctx->priv;
122 AVFilterLink *inlink = ctx->inputs[0];
126 /* no picref available, fetch it from the filterchain */
127 if (!av_fifo_size(buf->fifo)) {
128 if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
129 return AVERROR(EAGAIN);
130 if ((ret = avfilter_request_frame(inlink)) < 0)
134 if (!av_fifo_size(buf->fifo))
135 return AVERROR(EINVAL);
137 if (flags & AV_BUFFERSINK_FLAG_PEEK)
138 *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
140 av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
145 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
147 return ctx->inputs[0]->frame_rate;
150 int av_buffersink_poll_frame(AVFilterContext *ctx)
152 BufferSinkContext *buf = ctx->priv;
153 AVFilterLink *inlink = ctx->inputs[0];
155 return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
158 #if FF_API_OLD_VSINK_API
159 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
160 AVFilterBufferRef **picref, int flags)
162 return av_buffersink_get_buffer_ref(ctx, picref, flags);
166 #if CONFIG_BUFFERSINK_FILTER
168 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
170 BufferSinkContext *buf = ctx->priv;
171 av_unused AVBufferSinkParams *params;
174 av_log(ctx, AV_LOG_WARNING,
175 "No opaque field provided\n");
176 buf->pixel_fmts = NULL;
178 #if FF_API_OLD_VSINK_API
179 const int *pixel_fmts = (const enum PixelFormat *)opaque;
181 params = (AVBufferSinkParams *)opaque;
182 const int *pixel_fmts = params->pixel_fmts;
184 buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
185 if (!buf->pixel_fmts)
186 return AVERROR(ENOMEM);
189 return common_init(ctx);
192 static av_cold void vsink_uninit(AVFilterContext *ctx)
194 BufferSinkContext *buf = ctx->priv;
195 av_freep(&buf->pixel_fmts);
199 static int vsink_query_formats(AVFilterContext *ctx)
201 BufferSinkContext *buf = ctx->priv;
204 ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
206 ff_default_query_formats(ctx);
211 AVFilter avfilter_vsink_buffersink = {
212 .name = "buffersink",
213 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
214 .priv_size = sizeof(BufferSinkContext),
216 .uninit = vsink_uninit,
218 .query_formats = vsink_query_formats,
220 .inputs = (const AVFilterPad[]) {{ .name = "default",
221 .type = AVMEDIA_TYPE_VIDEO,
222 .end_frame = end_frame,
223 .min_perms = AV_PERM_READ, },
225 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
228 #endif /* CONFIG_BUFFERSINK_FILTER */
230 #if CONFIG_ABUFFERSINK_FILTER
232 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
237 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
239 BufferSinkContext *buf = ctx->priv;
240 AVABufferSinkParams *params;
243 av_log(ctx, AV_LOG_ERROR,
244 "No opaque field provided, an AVABufferSinkParams struct is required\n");
245 return AVERROR(EINVAL);
247 params = (AVABufferSinkParams *)opaque;
249 buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
250 buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
251 if (!buf->sample_fmts || !buf->channel_layouts) {
252 av_freep(&buf->sample_fmts);
253 av_freep(&buf->channel_layouts);
254 return AVERROR(ENOMEM);
257 return common_init(ctx);
260 static av_cold void asink_uninit(AVFilterContext *ctx)
262 BufferSinkContext *buf = ctx->priv;
264 av_freep(&buf->sample_fmts);
265 av_freep(&buf->channel_layouts);
269 static int asink_query_formats(AVFilterContext *ctx)
271 BufferSinkContext *buf = ctx->priv;
272 AVFilterFormats *formats = NULL;
273 AVFilterChannelLayouts *layouts = NULL;
275 if (!(formats = ff_make_format_list(buf->sample_fmts)))
276 return AVERROR(ENOMEM);
277 ff_set_common_formats(ctx, formats);
279 if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
280 return AVERROR(ENOMEM);
281 ff_set_common_channel_layouts(ctx, layouts);
282 ff_set_common_samplerates (ctx, ff_all_samplerates());
287 AVFilter avfilter_asink_abuffersink = {
288 .name = "abuffersink",
289 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
291 .uninit = asink_uninit,
292 .priv_size = sizeof(BufferSinkContext),
293 .query_formats = asink_query_formats,
295 .inputs = (const AVFilterPad[]) {{ .name = "default",
296 .type = AVMEDIA_TYPE_AUDIO,
297 .filter_samples = filter_samples,
298 .min_perms = AV_PERM_READ, },
300 .outputs = (const AVFilterPad[]) {{ .name = NULL }},
303 #endif /* CONFIG_ABUFFERSINK_FILTER */