]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsink_buffer.c
lavfi: add audio channel packing negotiation fields
[ffmpeg] / libavfilter / vsink_buffer.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * buffer video sink
24  */
25
26 #include "avfilter.h"
27 #include "vsink_buffer.h"
28
29 typedef struct {
30     AVFilterBufferRef *picref;   ///< cached picref
31     enum PixelFormat *pix_fmts;  ///< accepted pixel formats, must be terminated with -1
32 } BufferSinkContext;
33
34 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
35 {
36     BufferSinkContext *buf = ctx->priv;
37
38     if (!opaque) {
39         av_log(ctx, AV_LOG_ERROR, "No opaque field provided, which is required.\n");
40         return AVERROR(EINVAL);
41     }
42
43     buf->pix_fmts = opaque;
44     return 0;
45 }
46
47 static av_cold void uninit(AVFilterContext *ctx)
48 {
49     BufferSinkContext *buf = ctx->priv;
50
51     if (buf->picref)
52         avfilter_unref_buffer(buf->picref);
53     buf->picref = NULL;
54 }
55
56 static void end_frame(AVFilterLink *inlink)
57 {
58     BufferSinkContext *buf = inlink->dst->priv;
59
60     if (buf->picref)            /* drop the last cached frame */
61         avfilter_unref_buffer(buf->picref);
62     buf->picref = inlink->cur_buf;
63 }
64
65 static int query_formats(AVFilterContext *ctx)
66 {
67     BufferSinkContext *buf = ctx->priv;
68
69     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pix_fmts));
70     return 0;
71 }
72
73 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
74                                          AVFilterBufferRef **picref, int flags)
75 {
76     BufferSinkContext *buf = ctx->priv;
77     AVFilterLink *inlink = ctx->inputs[0];
78     int ret;
79     *picref = NULL;
80
81     /* no picref available, fetch it from the filterchain */
82     if (!buf->picref) {
83         if ((ret = avfilter_request_frame(inlink)) < 0)
84             return ret;
85     }
86
87     if (!buf->picref)
88         return AVERROR(EINVAL);
89
90     *picref = buf->picref;
91     if (!(flags & AV_VSINK_BUF_FLAG_PEEK))
92         buf->picref = NULL;
93
94     return 0;
95 }
96
97 AVFilter avfilter_vsink_buffersink = {
98     .name      = "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),
101     .init      = init,
102     .uninit    = uninit,
103
104     .query_formats = query_formats,
105
106     .inputs    = (AVFilterPad[]) {{ .name          = "default",
107                                     .type          = AVMEDIA_TYPE_VIDEO,
108                                     .end_frame     = end_frame,
109                                     .min_perms     = AV_PERM_READ, },
110                                   { .name = NULL }},
111     .outputs   = (AVFilterPad[]) {{ .name = NULL }},
112 };