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"
30 AVBufferSinkParams *av_buffersink_params_alloc(void)
32 static const int pixel_fmts[] = { -1 };
33 AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
37 params->pixel_fmts = pixel_fmts;
41 AVABufferSinkParams *av_abuffersink_params_alloc(void)
43 static const int sample_fmts[] = { -1 };
44 static const int packing_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;
53 params->packing_fmts = packing_fmts;
58 AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
60 /* only used for video */
61 const enum PixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
63 /* only used for audio */
64 const enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
65 const int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
66 const int *packing_fmts; ///< list of accepted packing formats, terminated by -1
69 #define FIFO_INIT_SIZE 8
71 static av_cold int common_init(AVFilterContext *ctx)
73 BufferSinkContext *buf = ctx->priv;
75 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
77 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
78 return AVERROR(ENOMEM);
83 static av_cold void common_uninit(AVFilterContext *ctx)
85 BufferSinkContext *buf = ctx->priv;
86 AVFilterBufferRef *picref;
89 while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
90 av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
91 avfilter_unref_buffer(picref);
93 av_fifo_free(buf->fifo);
98 static void end_frame(AVFilterLink *inlink)
100 AVFilterContext *ctx = inlink->dst;
101 BufferSinkContext *buf = inlink->dst->priv;
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 ((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 #if FF_API_OLD_VSINK_API
144 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
145 AVFilterBufferRef **picref, int flags)
147 return av_buffersink_get_buffer_ref(ctx, picref, flags);
151 #if CONFIG_BUFFERSINK_FILTER
153 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
155 BufferSinkContext *buf = ctx->priv;
156 av_unused AVBufferSinkParams *params;
159 av_log(ctx, AV_LOG_ERROR,
160 "No opaque field provided\n");
161 return AVERROR(EINVAL);
163 #if FF_API_OLD_VSINK_API
164 buf->pixel_fmts = (const enum PixelFormat *)opaque;
166 params = (AVBufferSinkParams *)opaque;
167 buf->pixel_fmts = params->pixel_fmts;
171 return common_init(ctx);
174 static int vsink_query_formats(AVFilterContext *ctx)
176 BufferSinkContext *buf = ctx->priv;
178 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
182 AVFilter avfilter_vsink_buffersink = {
183 .name = "buffersink",
184 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
185 .priv_size = sizeof(BufferSinkContext),
187 .uninit = common_uninit,
189 .query_formats = vsink_query_formats,
191 .inputs = (AVFilterPad[]) {{ .name = "default",
192 .type = AVMEDIA_TYPE_VIDEO,
193 .end_frame = end_frame,
194 .min_perms = AV_PERM_READ, },
196 .outputs = (AVFilterPad[]) {{ .name = NULL }},
199 #endif /* CONFIG_BUFFERSINK_FILTER */
201 #if CONFIG_ABUFFERSINK_FILTER
203 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
208 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
210 BufferSinkContext *buf = ctx->priv;
211 AVABufferSinkParams *params;
214 av_log(ctx, AV_LOG_ERROR,
215 "No opaque field provided, an AVABufferSinkParams struct is required\n");
216 return AVERROR(EINVAL);
218 params = (AVABufferSinkParams *)opaque;
220 buf->sample_fmts = params->sample_fmts;
221 buf->channel_layouts = params->channel_layouts;
222 buf->packing_fmts = params->packing_fmts;
224 return common_init(ctx);
227 static int asink_query_formats(AVFilterContext *ctx)
229 BufferSinkContext *buf = ctx->priv;
230 AVFilterFormats *formats = NULL;
232 if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
233 return AVERROR(ENOMEM);
234 avfilter_set_common_sample_formats(ctx, formats);
236 if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
237 return AVERROR(ENOMEM);
238 avfilter_set_common_channel_layouts(ctx, formats);
240 if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
241 return AVERROR(ENOMEM);
242 avfilter_set_common_packing_formats(ctx, formats);
247 AVFilter avfilter_asink_abuffersink = {
248 .name = "abuffersink",
249 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
251 .uninit = common_uninit,
252 .priv_size = sizeof(BufferSinkContext),
253 .query_formats = asink_query_formats,
255 .inputs = (AVFilterPad[]) {{ .name = "default",
256 .type = AVMEDIA_TYPE_AUDIO,
257 .filter_samples = filter_samples,
258 .min_perms = AV_PERM_READ, },
260 .outputs = (AVFilterPad[]) {{ .name = NULL }},
263 #endif /* CONFIG_ABUFFERSINK_FILTER */