]> git.sesse.net Git - ffmpeg/blob - libavfilter/sink_buffer.c
life: add mold, mold_color, life_color and death_color options.
[ffmpeg] / libavfilter / sink_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 "buffersink.h"
29
30 AVBufferSinkParams *av_buffersink_params_alloc(void)
31 {
32     static const int pixel_fmts[] = { -1 };
33     AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
34     if (!params)
35         return NULL;
36
37     params->pixel_fmts = pixel_fmts;
38     return params;
39 }
40
41 AVABufferSinkParams *av_abuffersink_params_alloc(void)
42 {
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));
47
48     if (!params)
49         return NULL;
50
51     params->sample_fmts = sample_fmts;
52     params->channel_layouts = channel_layouts;
53     params->packing_fmts = packing_fmts;
54     return params;
55 }
56
57 typedef struct {
58     AVFifoBuffer *fifo;                      ///< FIFO buffer of video frame references
59
60     /* only used for video */
61     const enum PixelFormat *pixel_fmts;     ///< list of accepted pixel formats, must be terminated with -1
62
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
67 } BufferSinkContext;
68
69 #define FIFO_INIT_SIZE 8
70
71 static av_cold int common_init(AVFilterContext *ctx)
72 {
73     BufferSinkContext *buf = ctx->priv;
74
75     buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
76     if (!buf->fifo) {
77         av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
78         return AVERROR(ENOMEM);
79     }
80     return 0;
81 }
82
83 static av_cold void common_uninit(AVFilterContext *ctx)
84 {
85     BufferSinkContext *buf = ctx->priv;
86     AVFilterBufferRef *picref;
87
88     if (buf->fifo) {
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);
92         }
93         av_fifo_free(buf->fifo);
94         buf->fifo = NULL;
95     }
96 }
97
98 static void end_frame(AVFilterLink *inlink)
99 {
100     AVFilterContext *ctx = inlink->dst;
101     BufferSinkContext *buf = inlink->dst->priv;
102
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");
109             return;
110         }
111     }
112
113     /* cache frame */
114     av_fifo_generic_write(buf->fifo,
115                           &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
116 }
117
118 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
119                                   AVFilterBufferRef **bufref, int flags)
120 {
121     BufferSinkContext *buf = ctx->priv;
122     AVFilterLink *inlink = ctx->inputs[0];
123     int ret;
124     *bufref = NULL;
125
126     /* no picref available, fetch it from the filterchain */
127     if (!av_fifo_size(buf->fifo)) {
128         if ((ret = avfilter_request_frame(inlink)) < 0)
129             return ret;
130     }
131
132     if (!av_fifo_size(buf->fifo))
133         return AVERROR(EINVAL);
134
135     if (flags & AV_BUFFERSINK_FLAG_PEEK)
136         *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
137     else
138         av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
139
140     return 0;
141 }
142
143 int av_buffersink_poll_frame(AVFilterContext *ctx)
144 {
145     BufferSinkContext *buf = ctx->priv;
146     AVFilterLink *inlink = ctx->inputs[0];
147
148     return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
149 }
150
151 #if FF_API_OLD_VSINK_API
152 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
153                                          AVFilterBufferRef **picref, int flags)
154 {
155     return av_buffersink_get_buffer_ref(ctx, picref, flags);
156 }
157 #endif
158
159 #if CONFIG_BUFFERSINK_FILTER
160
161 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
162 {
163     BufferSinkContext *buf = ctx->priv;
164     av_unused AVBufferSinkParams *params;
165
166     if (!opaque) {
167         av_log(ctx, AV_LOG_ERROR,
168                "No opaque field provided\n");
169         return AVERROR(EINVAL);
170     } else {
171 #if FF_API_OLD_VSINK_API
172         buf->pixel_fmts = (const enum PixelFormat *)opaque;
173 #else
174         params = (AVBufferSinkParams *)opaque;
175         buf->pixel_fmts = params->pixel_fmts;
176 #endif
177     }
178
179     return common_init(ctx);
180 }
181
182 static int vsink_query_formats(AVFilterContext *ctx)
183 {
184     BufferSinkContext *buf = ctx->priv;
185
186     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
187     return 0;
188 }
189
190 AVFilter avfilter_vsink_buffersink = {
191     .name      = "buffersink",
192     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
193     .priv_size = sizeof(BufferSinkContext),
194     .init      = vsink_init,
195     .uninit    = common_uninit,
196
197     .query_formats = vsink_query_formats,
198
199     .inputs    = (const AVFilterPad[]) {{ .name    = "default",
200                                     .type          = AVMEDIA_TYPE_VIDEO,
201                                     .end_frame     = end_frame,
202                                     .min_perms     = AV_PERM_READ, },
203                                   { .name = NULL }},
204     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
205 };
206
207 #endif /* CONFIG_BUFFERSINK_FILTER */
208
209 #if CONFIG_ABUFFERSINK_FILTER
210
211 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
212 {
213     end_frame(link);
214 }
215
216 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
217 {
218     BufferSinkContext *buf = ctx->priv;
219     AVABufferSinkParams *params;
220
221     if (!opaque) {
222         av_log(ctx, AV_LOG_ERROR,
223                "No opaque field provided, an AVABufferSinkParams struct is required\n");
224         return AVERROR(EINVAL);
225     } else
226         params = (AVABufferSinkParams *)opaque;
227
228     buf->sample_fmts     = params->sample_fmts;
229     buf->channel_layouts = params->channel_layouts;
230     buf->packing_fmts    = params->packing_fmts;
231
232     return common_init(ctx);
233 }
234
235 static int asink_query_formats(AVFilterContext *ctx)
236 {
237     BufferSinkContext *buf = ctx->priv;
238     AVFilterFormats *formats = NULL;
239
240     if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
241         return AVERROR(ENOMEM);
242     avfilter_set_common_sample_formats(ctx, formats);
243
244     if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
245         return AVERROR(ENOMEM);
246     avfilter_set_common_channel_layouts(ctx, formats);
247
248     if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
249         return AVERROR(ENOMEM);
250     avfilter_set_common_packing_formats(ctx, formats);
251
252     return 0;
253 }
254
255 AVFilter avfilter_asink_abuffersink = {
256     .name      = "abuffersink",
257     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
258     .init      = asink_init,
259     .uninit    = common_uninit,
260     .priv_size = sizeof(BufferSinkContext),
261     .query_formats = asink_query_formats,
262
263     .inputs    = (const AVFilterPad[]) {{ .name     = "default",
264                                     .type           = AVMEDIA_TYPE_AUDIO,
265                                     .filter_samples = filter_samples,
266                                     .min_perms      = AV_PERM_READ, },
267                                   { .name = NULL }},
268     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
269 };
270
271 #endif /* CONFIG_ABUFFERSINK_FILTER */