]> 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/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
60     /* only used for video */
61     enum PixelFormat *pixel_fmts;           ///< list of accepted pixel formats, must be terminated with -1
62
63     /* only used for audio */
64     enum AVSampleFormat *sample_fmts;       ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
65     int64_t *channel_layouts;               ///< list of accepted channel layouts, terminated by -1
66 } BufferSinkContext;
67
68 #define FIFO_INIT_SIZE 8
69
70 static av_cold int common_init(AVFilterContext *ctx)
71 {
72     BufferSinkContext *buf = ctx->priv;
73
74     buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
75     if (!buf->fifo) {
76         av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
77         return AVERROR(ENOMEM);
78     }
79     return 0;
80 }
81
82 static av_cold void common_uninit(AVFilterContext *ctx)
83 {
84     BufferSinkContext *buf = ctx->priv;
85     AVFilterBufferRef *picref;
86
87     if (buf->fifo) {
88         while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
89             av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
90             avfilter_unref_buffer(picref);
91         }
92         av_fifo_free(buf->fifo);
93         buf->fifo = NULL;
94     }
95 }
96
97 static void end_frame(AVFilterLink *inlink)
98 {
99     AVFilterContext *ctx = inlink->dst;
100     BufferSinkContext *buf = inlink->dst->priv;
101
102     av_assert1(inlink->cur_buf);
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     av_assert0(!strcmp(ctx->filter->name, "buffersink") || !strcmp(ctx->filter->name, "abuffersink"));
127
128     /* no picref available, fetch it from the filterchain */
129     if (!av_fifo_size(buf->fifo)) {
130         if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
131             return AVERROR(EAGAIN);
132         if ((ret = ff_request_frame(inlink)) < 0)
133             return ret;
134     }
135
136     if (!av_fifo_size(buf->fifo))
137         return AVERROR(EINVAL);
138
139     if (flags & AV_BUFFERSINK_FLAG_PEEK)
140         *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
141     else
142         av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
143
144     return 0;
145 }
146
147 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
148 {
149     av_assert0(!strcmp(ctx->filter->name, "buffersink"));
150
151     return ctx->inputs[0]->frame_rate;
152 }
153
154 int av_buffersink_poll_frame(AVFilterContext *ctx)
155 {
156     BufferSinkContext *buf = ctx->priv;
157     AVFilterLink *inlink = ctx->inputs[0];
158
159     av_assert0(!strcmp(ctx->filter->name, "buffersink") || !strcmp(ctx->filter->name, "abuffersink"));
160
161     return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
162 }
163
164 #if FF_API_OLD_VSINK_API
165 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
166                                          AVFilterBufferRef **picref, int flags)
167 {
168     return av_buffersink_get_buffer_ref(ctx, picref, flags);
169 }
170 #endif
171
172 #if CONFIG_BUFFERSINK_FILTER
173
174 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
175 {
176     BufferSinkContext *buf = ctx->priv;
177     av_unused AVBufferSinkParams *params;
178
179     if (!opaque) {
180         av_log(ctx, AV_LOG_WARNING,
181                "No opaque field provided\n");
182         buf->pixel_fmts = NULL;
183     } else {
184 #if FF_API_OLD_VSINK_API
185         const int *pixel_fmts = (const enum PixelFormat *)opaque;
186 #else
187         params = (AVBufferSinkParams *)opaque;
188         const int *pixel_fmts = params->pixel_fmts;
189 #endif
190         buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
191         if (!buf->pixel_fmts)
192             return AVERROR(ENOMEM);
193     }
194
195     return common_init(ctx);
196 }
197
198 static av_cold void vsink_uninit(AVFilterContext *ctx)
199 {
200     BufferSinkContext *buf = ctx->priv;
201     av_freep(&buf->pixel_fmts);
202     common_uninit(ctx);
203 }
204
205 static int vsink_query_formats(AVFilterContext *ctx)
206 {
207     BufferSinkContext *buf = ctx->priv;
208
209     if (buf->pixel_fmts)
210         ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
211     else
212         ff_default_query_formats(ctx);
213
214     return 0;
215 }
216
217 AVFilter avfilter_vsink_buffersink = {
218     .name      = "buffersink",
219     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
220     .priv_size = sizeof(BufferSinkContext),
221     .init      = vsink_init,
222     .uninit    = vsink_uninit,
223
224     .query_formats = vsink_query_formats,
225
226     .inputs    = (const AVFilterPad[]) {{ .name    = "default",
227                                     .type          = AVMEDIA_TYPE_VIDEO,
228                                     .end_frame     = end_frame,
229                                     .min_perms     = AV_PERM_READ, },
230                                   { .name = NULL }},
231     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
232 };
233
234 #endif /* CONFIG_BUFFERSINK_FILTER */
235
236 #if CONFIG_ABUFFERSINK_FILTER
237
238 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
239 {
240     end_frame(link);
241 }
242
243 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
244 {
245     BufferSinkContext *buf = ctx->priv;
246     AVABufferSinkParams *params;
247
248     if (!opaque) {
249         av_log(ctx, AV_LOG_ERROR,
250                "No opaque field provided, an AVABufferSinkParams struct is required\n");
251         return AVERROR(EINVAL);
252     } else
253         params = (AVABufferSinkParams *)opaque;
254
255     buf->sample_fmts     = ff_copy_int_list  (params->sample_fmts);
256     buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
257     if (!buf->sample_fmts || !buf->channel_layouts) {
258         av_freep(&buf->sample_fmts);
259         av_freep(&buf->channel_layouts);
260         return AVERROR(ENOMEM);
261     }
262
263     return common_init(ctx);
264 }
265
266 static av_cold void asink_uninit(AVFilterContext *ctx)
267 {
268     BufferSinkContext *buf = ctx->priv;
269
270     av_freep(&buf->sample_fmts);
271     av_freep(&buf->channel_layouts);
272     common_uninit(ctx);
273 }
274
275 static int asink_query_formats(AVFilterContext *ctx)
276 {
277     BufferSinkContext *buf = ctx->priv;
278     AVFilterFormats *formats = NULL;
279     AVFilterChannelLayouts *layouts = NULL;
280
281     if (!(formats = ff_make_format_list(buf->sample_fmts)))
282         return AVERROR(ENOMEM);
283     ff_set_common_formats(ctx, formats);
284
285     if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
286         return AVERROR(ENOMEM);
287     ff_set_common_channel_layouts(ctx, layouts);
288     ff_set_common_samplerates          (ctx, ff_all_samplerates());
289
290     return 0;
291 }
292
293 AVFilter avfilter_asink_abuffersink = {
294     .name      = "abuffersink",
295     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
296     .init      = asink_init,
297     .uninit    = asink_uninit,
298     .priv_size = sizeof(BufferSinkContext),
299     .query_formats = asink_query_formats,
300
301     .inputs    = (const AVFilterPad[]) {{ .name     = "default",
302                                     .type           = AVMEDIA_TYPE_AUDIO,
303                                     .filter_samples = filter_samples,
304                                     .min_perms      = AV_PERM_READ, },
305                                   { .name = NULL }},
306     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
307 };
308
309 #endif /* CONFIG_ABUFFERSINK_FILTER */