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
27 #include "vsink_buffer.h"
30 AVFilterBufferRef *picref; ///< cached picref
31 enum PixelFormat *pix_fmts; ///< accepted pixel formats, must be terminated with -1
34 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
36 BufferSinkContext *buf = ctx->priv;
39 av_log(ctx, AV_LOG_ERROR, "No opaque field provided, which is required.\n");
40 return AVERROR(EINVAL);
43 buf->pix_fmts = opaque;
47 static av_cold void uninit(AVFilterContext *ctx)
49 BufferSinkContext *buf = ctx->priv;
52 avfilter_unref_buffer(buf->picref);
56 static void end_frame(AVFilterLink *inlink)
58 BufferSinkContext *buf = inlink->dst->priv;
60 if (buf->picref) /* drop the last cached frame */
61 avfilter_unref_buffer(buf->picref);
62 buf->picref = inlink->cur_buf;
65 static int query_formats(AVFilterContext *ctx)
67 BufferSinkContext *buf = ctx->priv;
69 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pix_fmts));
73 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
74 AVFilterBufferRef **picref, int flags)
76 BufferSinkContext *buf = ctx->priv;
77 AVFilterLink *inlink = ctx->inputs[0];
81 /* no picref available, fetch it from the filterchain */
83 if ((ret = avfilter_request_frame(inlink)) < 0)
88 return AVERROR(EINVAL);
90 *picref = buf->picref;
91 if (!(flags & AV_VSINK_BUF_FLAG_PEEK))
97 AVFilter avfilter_vsink_buffersink = {
99 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
100 .priv_size = sizeof(BufferSinkContext),
104 .query_formats = query_formats,
106 .inputs = (AVFilterPad[]) {{ .name = "default",
107 .type = AVMEDIA_TYPE_VIDEO,
108 .end_frame = end_frame,
109 .min_perms = AV_PERM_READ, },
111 .outputs = (AVFilterPad[]) {{ .name = NULL }},