]> 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 sink
24  */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/fifo.h"
28 #include "avfilter.h"
29 #include "buffersink.h"
30 #include "internal.h"
31
32 AVBufferSinkParams *av_buffersink_params_alloc(void)
33 {
34     static const int pixel_fmts[] = { -1 };
35     AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
36     if (!params)
37         return NULL;
38
39     params->pixel_fmts = pixel_fmts;
40     return params;
41 }
42
43 AVABufferSinkParams *av_abuffersink_params_alloc(void)
44 {
45     static const int sample_fmts[] = { -1 };
46     static const int64_t channel_layouts[] = { -1 };
47     AVABufferSinkParams *params = av_malloc(sizeof(AVABufferSinkParams));
48
49     if (!params)
50         return NULL;
51
52     params->sample_fmts = sample_fmts;
53     params->channel_layouts = channel_layouts;
54     return params;
55 }
56
57 typedef struct {
58     AVFifoBuffer *fifo;                      ///< FIFO buffer of video frame references
59     unsigned warning_limit;
60
61     /* only used for video */
62     enum PixelFormat *pixel_fmts;           ///< list of accepted pixel formats, must be terminated with -1
63
64     /* only used for audio */
65     enum AVSampleFormat *sample_fmts;       ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
66     int64_t *channel_layouts;               ///< list of accepted channel layouts, 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     buf->warning_limit = 100;
81     return 0;
82 }
83
84 static av_cold void common_uninit(AVFilterContext *ctx)
85 {
86     BufferSinkContext *buf = ctx->priv;
87     AVFilterBufferRef *picref;
88
89     if (buf->fifo) {
90         while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
91             av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
92             avfilter_unref_buffer(picref);
93         }
94         av_fifo_free(buf->fifo);
95         buf->fifo = NULL;
96     }
97 }
98
99 static int end_frame(AVFilterLink *inlink)
100 {
101     AVFilterContext *ctx = inlink->dst;
102     BufferSinkContext *buf = inlink->dst->priv;
103
104     av_assert1(inlink->cur_buf);
105     if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
106         /* realloc fifo size */
107         if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
108             av_log(ctx, AV_LOG_ERROR,
109                    "Cannot buffer more frames. Consume some available frames "
110                    "before adding new ones.\n");
111             return AVERROR(ENOMEM);
112         }
113     }
114
115     /* cache frame */
116     av_fifo_generic_write(buf->fifo,
117                           &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
118     inlink->cur_buf = NULL;
119     if (buf->warning_limit &&
120         av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
121         av_log(ctx, AV_LOG_WARNING,
122                "%d buffers queued in %s, something may be wrong.\n",
123                buf->warning_limit,
124                (char *)av_x_if_null(ctx->name, ctx->filter->name));
125         buf->warning_limit *= 10;
126     }
127     return 0;
128 }
129
130 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
131 {
132     AVFilterLink *inlink = ctx->inputs[0];
133
134     inlink->min_samples = inlink->max_samples =
135     inlink->partial_buf_size = frame_size;
136 }
137
138 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
139                                   AVFilterBufferRef **bufref, int flags)
140 {
141     BufferSinkContext *buf = ctx->priv;
142     AVFilterLink *inlink = ctx->inputs[0];
143     int ret;
144     *bufref = NULL;
145
146     av_assert0(    !strcmp(ctx->filter->name, "buffersink")
147                 || !strcmp(ctx->filter->name, "abuffersink")
148                 || !strcmp(ctx->filter->name, "ffbuffersink")
149                 || !strcmp(ctx->filter->name, "ffabuffersink"));
150
151     /* no picref available, fetch it from the filterchain */
152     if (!av_fifo_size(buf->fifo)) {
153         if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
154             return AVERROR(EAGAIN);
155         if ((ret = ff_request_frame(inlink)) < 0)
156             return ret;
157     }
158
159     if (!av_fifo_size(buf->fifo))
160         return AVERROR(EINVAL);
161
162     if (flags & AV_BUFFERSINK_FLAG_PEEK)
163         *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
164     else
165         av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
166
167     return 0;
168 }
169
170 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
171 {
172     av_assert0(   !strcmp(ctx->filter->name, "buffersink")
173                || !strcmp(ctx->filter->name, "ffbuffersink"));
174
175     return ctx->inputs[0]->frame_rate;
176 }
177
178 int av_buffersink_poll_frame(AVFilterContext *ctx)
179 {
180     BufferSinkContext *buf = ctx->priv;
181     AVFilterLink *inlink = ctx->inputs[0];
182
183     av_assert0(   !strcmp(ctx->filter->name, "buffersink")
184                || !strcmp(ctx->filter->name, "abuffersink")
185                || !strcmp(ctx->filter->name, "ffbuffersink")
186                || !strcmp(ctx->filter->name, "ffabuffersink"));
187
188     return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
189 }
190
191 #if CONFIG_BUFFERSINK_FILTER || CONFIG_FFBUFFERSINK_FILTER
192
193 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
194 {
195     BufferSinkContext *buf = ctx->priv;
196     AVBufferSinkParams *params = opaque;
197
198     if (params && params->pixel_fmts) {
199         const int *pixel_fmts = params->pixel_fmts;
200
201         buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
202         if (!buf->pixel_fmts)
203             return AVERROR(ENOMEM);
204     }
205
206     return common_init(ctx);
207 }
208
209 static av_cold void vsink_uninit(AVFilterContext *ctx)
210 {
211     BufferSinkContext *buf = ctx->priv;
212     av_freep(&buf->pixel_fmts);
213     common_uninit(ctx);
214 }
215
216 static int vsink_query_formats(AVFilterContext *ctx)
217 {
218     BufferSinkContext *buf = ctx->priv;
219
220     if (buf->pixel_fmts)
221         ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
222     else
223         ff_default_query_formats(ctx);
224
225     return 0;
226 }
227
228 AVFilter avfilter_vsink_ffbuffersink = {
229     .name      = "ffbuffersink",
230     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
231     .priv_size = sizeof(BufferSinkContext),
232     .init_opaque = vsink_init,
233     .uninit    = vsink_uninit,
234
235     .query_formats = vsink_query_formats,
236
237     .inputs    = (const AVFilterPad[]) {{ .name    = "default",
238                                     .type          = AVMEDIA_TYPE_VIDEO,
239                                     .end_frame     = end_frame,
240                                     .min_perms     = AV_PERM_READ | AV_PERM_PRESERVE, },
241                                   { .name = NULL }},
242     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
243 };
244
245 AVFilter avfilter_vsink_buffersink = {
246     .name      = "buffersink",
247     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
248     .priv_size = sizeof(BufferSinkContext),
249     .init_opaque = vsink_init,
250     .uninit    = vsink_uninit,
251
252     .query_formats = vsink_query_formats,
253
254     .inputs    = (const AVFilterPad[]) {{ .name    = "default",
255                                     .type          = AVMEDIA_TYPE_VIDEO,
256                                     .end_frame     = end_frame,
257                                     .min_perms     = AV_PERM_READ | AV_PERM_PRESERVE, },
258                                   { .name = NULL }},
259     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
260 };
261
262 #endif /* CONFIG_BUFFERSINK_FILTER */
263
264 #if CONFIG_ABUFFERSINK_FILTER || CONFIG_FFABUFFERSINK_FILTER
265
266 static int filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
267 {
268     end_frame(link);
269     return 0;
270 }
271
272 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
273 {
274     BufferSinkContext *buf = ctx->priv;
275     AVABufferSinkParams *params = opaque;
276
277     if (params && params->sample_fmts) {
278         buf->sample_fmts     = ff_copy_int_list  (params->sample_fmts);
279         if (!buf->sample_fmts)
280             goto fail_enomem;
281     }
282     if (params && params->channel_layouts) {
283         buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
284         if (!buf->channel_layouts)
285             goto fail_enomem;
286     }
287     if (!common_init(ctx))
288         return 0;
289
290 fail_enomem:
291     av_freep(&buf->sample_fmts);
292     av_freep(&buf->channel_layouts);
293     return AVERROR(ENOMEM);
294 }
295
296 static av_cold void asink_uninit(AVFilterContext *ctx)
297 {
298     BufferSinkContext *buf = ctx->priv;
299
300     av_freep(&buf->sample_fmts);
301     av_freep(&buf->channel_layouts);
302     common_uninit(ctx);
303 }
304
305 static int asink_query_formats(AVFilterContext *ctx)
306 {
307     BufferSinkContext *buf = ctx->priv;
308     AVFilterFormats *formats = NULL;
309     AVFilterChannelLayouts *layouts = NULL;
310
311     if (buf->sample_fmts) {
312     if (!(formats = ff_make_format_list(buf->sample_fmts)))
313         return AVERROR(ENOMEM);
314     ff_set_common_formats(ctx, formats);
315     }
316
317     if (buf->channel_layouts) {
318     if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
319         return AVERROR(ENOMEM);
320     ff_set_common_channel_layouts(ctx, layouts);
321     }
322
323     return 0;
324 }
325
326 AVFilter avfilter_asink_ffabuffersink = {
327     .name      = "ffabuffersink",
328     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
329     .init_opaque = asink_init,
330     .uninit    = asink_uninit,
331     .priv_size = sizeof(BufferSinkContext),
332     .query_formats = asink_query_formats,
333
334     .inputs    = (const AVFilterPad[]) {{ .name     = "default",
335                                     .type           = AVMEDIA_TYPE_AUDIO,
336                                     .filter_samples = filter_samples,
337                                     .min_perms      = AV_PERM_READ | AV_PERM_PRESERVE, },
338                                   { .name = NULL }},
339     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
340 };
341
342 AVFilter avfilter_asink_abuffersink = {
343     .name      = "abuffersink",
344     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
345     .init_opaque = asink_init,
346     .uninit    = asink_uninit,
347     .priv_size = sizeof(BufferSinkContext),
348     .query_formats = asink_query_formats,
349
350     .inputs    = (const AVFilterPad[]) {{ .name     = "default",
351                                     .type           = AVMEDIA_TYPE_AUDIO,
352                                     .filter_samples = filter_samples,
353                                     .min_perms      = AV_PERM_READ | AV_PERM_PRESERVE, },
354                                   { .name = NULL }},
355     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
356 };
357
358 #endif /* CONFIG_ABUFFERSINK_FILTER */