]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsink_buffer.c
Merge commit 'f593628e5868e52a46de666767896c6afcebdae4'
[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 "libavutil/fifo.h"
27 #include "avfilter.h"
28 #include "vsink_buffer.h"
29
30 typedef struct {
31     AVFifoBuffer *fifo;          ///< FIFO buffer of video frame references
32     enum PixelFormat *pix_fmts;  ///< accepted pixel formats, must be terminated with -1
33 } BufferSinkContext;
34
35 #define FIFO_INIT_SIZE 8
36
37 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
38 {
39     BufferSinkContext *buf = ctx->priv;
40
41     if (!opaque) {
42         av_log(ctx, AV_LOG_ERROR, "No opaque field provided, which is required.\n");
43         return AVERROR(EINVAL);
44     }
45
46     buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
47     if (!buf->fifo) {
48         av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
49         return AVERROR(ENOMEM);
50     }
51
52     buf->pix_fmts = opaque;
53     return 0;
54 }
55
56 static av_cold void uninit(AVFilterContext *ctx)
57 {
58     BufferSinkContext *buf = ctx->priv;
59     AVFilterBufferRef *picref;
60
61     if (buf->fifo) {
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);
65         }
66         av_fifo_free(buf->fifo);
67         buf->fifo = NULL;
68     }
69 }
70
71 static void end_frame(AVFilterLink *inlink)
72 {
73     AVFilterContext *ctx = inlink->dst;
74     BufferSinkContext *buf = inlink->dst->priv;
75
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");
82             return;
83         }
84     }
85
86     /* cache frame */
87     av_fifo_generic_write(buf->fifo,
88                           &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
89 }
90
91 static int query_formats(AVFilterContext *ctx)
92 {
93     BufferSinkContext *buf = ctx->priv;
94
95     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pix_fmts));
96     return 0;
97 }
98
99 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
100                                          AVFilterBufferRef **picref, int flags)
101 {
102     BufferSinkContext *buf = ctx->priv;
103     AVFilterLink *inlink = ctx->inputs[0];
104     int ret;
105     *picref = NULL;
106
107     /* no picref available, fetch it from the filterchain */
108     if (!av_fifo_size(buf->fifo)) {
109         if ((ret = avfilter_request_frame(inlink)) < 0)
110             return ret;
111     }
112
113     if (!av_fifo_size(buf->fifo))
114         return AVERROR(EINVAL);
115
116     if (flags & AV_VSINK_BUF_FLAG_PEEK)
117         *picref = (AVFilterBufferRef *)av_fifo_peek2(buf->fifo, 0);
118     else
119         av_fifo_generic_read(buf->fifo, picref, sizeof(*picref), NULL);
120
121     return 0;
122 }
123
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),
128     .init      = init,
129     .uninit    = uninit,
130
131     .query_formats = query_formats,
132
133     .inputs    = (AVFilterPad[]) {{ .name          = "default",
134                                     .type          = AVMEDIA_TYPE_VIDEO,
135                                     .end_frame     = end_frame,
136                                     .min_perms     = AV_PERM_READ, },
137                                   { .name = NULL }},
138     .outputs   = (AVFilterPad[]) {{ .name = NULL }},
139 };