]> git.sesse.net Git - ffmpeg/blob - libavfilter/sink_buffer.c
Merge remote-tracking branch 'qatar/master'
[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 #include "internal.h"
30
31 AVBufferSinkParams *av_buffersink_params_alloc(void)
32 {
33     static const int pixel_fmts[] = { -1 };
34     AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
35     if (!params)
36         return NULL;
37
38     params->pixel_fmts = pixel_fmts;
39     return params;
40 }
41
42 AVABufferSinkParams *av_abuffersink_params_alloc(void)
43 {
44     static const int sample_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     return params;
54 }
55
56 typedef struct {
57     AVFifoBuffer *fifo;                      ///< FIFO buffer of video frame references
58
59     /* only used for video */
60     enum PixelFormat *pixel_fmts;           ///< list of accepted pixel formats, must be terminated with -1
61
62     /* only used for audio */
63     enum AVSampleFormat *sample_fmts;       ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
64     int64_t *channel_layouts;               ///< list of accepted channel layouts, terminated by -1
65 } BufferSinkContext;
66
67 #define FIFO_INIT_SIZE 8
68
69 static av_cold int common_init(AVFilterContext *ctx)
70 {
71     BufferSinkContext *buf = ctx->priv;
72
73     buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
74     if (!buf->fifo) {
75         av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
76         return AVERROR(ENOMEM);
77     }
78     return 0;
79 }
80
81 static av_cold void common_uninit(AVFilterContext *ctx)
82 {
83     BufferSinkContext *buf = ctx->priv;
84     AVFilterBufferRef *picref;
85
86     if (buf->fifo) {
87         while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
88             av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
89             avfilter_unref_buffer(picref);
90         }
91         av_fifo_free(buf->fifo);
92         buf->fifo = NULL;
93     }
94 }
95
96 static void end_frame(AVFilterLink *inlink)
97 {
98     AVFilterContext *ctx = inlink->dst;
99     BufferSinkContext *buf = inlink->dst->priv;
100
101     if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
102         /* realloc fifo size */
103         if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
104             av_log(ctx, AV_LOG_ERROR,
105                    "Cannot buffer more frames. Consume some available frames "
106                    "before adding new ones.\n");
107             return;
108         }
109     }
110
111     /* cache frame */
112     av_fifo_generic_write(buf->fifo,
113                           &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
114 }
115
116 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
117                                   AVFilterBufferRef **bufref, int flags)
118 {
119     BufferSinkContext *buf = ctx->priv;
120     AVFilterLink *inlink = ctx->inputs[0];
121     int ret;
122     *bufref = NULL;
123
124     /* no picref available, fetch it from the filterchain */
125     if (!av_fifo_size(buf->fifo)) {
126         if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
127             return AVERROR(EAGAIN);
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_WARNING,
168                "No opaque field provided\n");
169         buf->pixel_fmts = NULL;
170     } else {
171 #if FF_API_OLD_VSINK_API
172         const int *pixel_fmts = (const enum PixelFormat *)opaque;
173 #else
174         params = (AVBufferSinkParams *)opaque;
175         const int *pixel_fmts = params->pixel_fmts;
176 #endif
177         buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
178         if (!buf->pixel_fmts)
179             return AVERROR(ENOMEM);
180     }
181
182     return common_init(ctx);
183 }
184
185 static av_cold void vsink_uninit(AVFilterContext *ctx)
186 {
187     BufferSinkContext *buf = ctx->priv;
188     av_freep(&buf->pixel_fmts);
189     return common_uninit(ctx);
190 }
191
192 static int vsink_query_formats(AVFilterContext *ctx)
193 {
194     BufferSinkContext *buf = ctx->priv;
195
196     if (buf->pixel_fmts)
197         avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
198     else
199         avfilter_default_query_formats(ctx);
200
201     return 0;
202 }
203
204 AVFilter avfilter_vsink_buffersink = {
205     .name      = "buffersink",
206     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
207     .priv_size = sizeof(BufferSinkContext),
208     .init      = vsink_init,
209     .uninit    = vsink_uninit,
210
211     .query_formats = vsink_query_formats,
212
213     .inputs    = (const AVFilterPad[]) {{ .name    = "default",
214                                     .type          = AVMEDIA_TYPE_VIDEO,
215                                     .end_frame     = end_frame,
216                                     .min_perms     = AV_PERM_READ, },
217                                   { .name = NULL }},
218     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
219 };
220
221 #endif /* CONFIG_BUFFERSINK_FILTER */
222
223 #if CONFIG_ABUFFERSINK_FILTER
224
225 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
226 {
227     end_frame(link);
228 }
229
230 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
231 {
232     BufferSinkContext *buf = ctx->priv;
233     AVABufferSinkParams *params;
234
235     if (!opaque) {
236         av_log(ctx, AV_LOG_ERROR,
237                "No opaque field provided, an AVABufferSinkParams struct is required\n");
238         return AVERROR(EINVAL);
239     } else
240         params = (AVABufferSinkParams *)opaque;
241
242     buf->sample_fmts     = ff_copy_int_list  (params->sample_fmts);
243     buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
244     if (!buf->sample_fmts || !buf->channel_layouts) {
245         av_freep(&buf->sample_fmts);
246         av_freep(&buf->channel_layouts);
247         return AVERROR(ENOMEM);
248     }
249
250     return common_init(ctx);
251 }
252
253 static av_cold void asink_uninit(AVFilterContext *ctx)
254 {
255     BufferSinkContext *buf = ctx->priv;
256
257     av_freep(&buf->sample_fmts);
258     av_freep(&buf->channel_layouts);
259     return common_uninit(ctx);
260 }
261
262 static int asink_query_formats(AVFilterContext *ctx)
263 {
264     BufferSinkContext *buf = ctx->priv;
265     AVFilterFormats *formats = NULL;
266     AVFilterChannelLayouts *layouts = NULL;
267
268     if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
269         return AVERROR(ENOMEM);
270     avfilter_set_common_sample_formats(ctx, formats);
271
272     if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
273         return AVERROR(ENOMEM);
274     ff_set_common_channel_layouts(ctx, layouts);
275     ff_set_common_samplerates          (ctx, ff_all_samplerates());
276
277     return 0;
278 }
279
280 AVFilter avfilter_asink_abuffersink = {
281     .name      = "abuffersink",
282     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
283     .init      = asink_init,
284     .uninit    = asink_uninit,
285     .priv_size = sizeof(BufferSinkContext),
286     .query_formats = asink_query_formats,
287
288     .inputs    = (const AVFilterPad[]) {{ .name     = "default",
289                                     .type           = AVMEDIA_TYPE_AUDIO,
290                                     .filter_samples = filter_samples,
291                                     .min_perms      = AV_PERM_READ, },
292                                   { .name = NULL }},
293     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
294 };
295
296 #endif /* CONFIG_ABUFFERSINK_FILTER */