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 "vsink_buffer.h"
31 AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
32 enum PixelFormat *pix_fmts; ///< accepted pixel formats, must be terminated with -1
35 #define FIFO_INIT_SIZE 8
37 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
39 BufferSinkContext *buf = ctx->priv;
42 av_log(ctx, AV_LOG_ERROR, "No opaque field provided, which is required.\n");
43 return AVERROR(EINVAL);
46 buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
48 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
49 return AVERROR(ENOMEM);
52 buf->pix_fmts = opaque;
56 static av_cold void uninit(AVFilterContext *ctx)
58 BufferSinkContext *buf = ctx->priv;
59 AVFilterBufferRef *picref;
62 while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
63 av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
64 avfilter_unref_buffer(picref);
66 av_fifo_free(buf->fifo);
71 static void end_frame(AVFilterLink *inlink)
73 AVFilterContext *ctx = inlink->dst;
74 BufferSinkContext *buf = inlink->dst->priv;
76 if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
77 /* realloc fifo size */
78 if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
79 av_log(ctx, AV_LOG_ERROR,
80 "Cannot buffer more frames. Consume some available frames "
81 "before adding new ones.\n");
87 av_fifo_generic_write(buf->fifo,
88 &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
91 static int query_formats(AVFilterContext *ctx)
93 BufferSinkContext *buf = ctx->priv;
95 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pix_fmts));
99 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
100 AVFilterBufferRef **picref, int flags)
102 BufferSinkContext *buf = ctx->priv;
103 AVFilterLink *inlink = ctx->inputs[0];
107 /* no picref available, fetch it from the filterchain */
108 if (!av_fifo_size(buf->fifo)) {
109 if ((ret = avfilter_request_frame(inlink)) < 0)
113 if (!av_fifo_size(buf->fifo))
114 return AVERROR(EINVAL);
116 if (flags & AV_VSINK_BUF_FLAG_PEEK)
117 *picref = (AVFilterBufferRef *)av_fifo_peek2(buf->fifo, 0);
119 av_fifo_generic_read(buf->fifo, picref, sizeof(*picref), NULL);
124 AVFilter avfilter_vsink_buffersink = {
125 .name = "buffersink",
126 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
127 .priv_size = sizeof(BufferSinkContext),
131 .query_formats = query_formats,
133 .inputs = (AVFilterPad[]) {{ .name = "default",
134 .type = AVMEDIA_TYPE_VIDEO,
135 .end_frame = end_frame,
136 .min_perms = AV_PERM_READ, },
138 .outputs = (AVFilterPad[]) {{ .name = NULL }},