OSDN Git Service

cleanup SoftFFmpegVideo
[android-x86/external-stagefright-plugins.git] / 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 int packing_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     params->packing_fmts = packing_fmts;
55     return params;
56 }
57
58 typedef struct {
59     AVFifoBuffer *fifo;                      ///< FIFO buffer of video frame references
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     int *packing_fmts;                      ///< list of accepted packing formats, terminated by -1
68 } BufferSinkContext;
69
70 #define FIFO_INIT_SIZE 8
71
72 static av_cold int common_init(AVFilterContext *ctx)
73 {
74     BufferSinkContext *buf = ctx->priv;
75
76     buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
77     if (!buf->fifo) {
78         av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
79         return AVERROR(ENOMEM);
80     }
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 void end_frame(AVFilterLink *inlink)
100 {
101     AVFilterContext *ctx = inlink->dst;
102     BufferSinkContext *buf = inlink->dst->priv;
103
104     if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
105         /* realloc fifo size */
106         if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
107             av_log(ctx, AV_LOG_ERROR,
108                    "Cannot buffer more frames. Consume some available frames "
109                    "before adding new ones.\n");
110             return;
111         }
112     }
113
114     /* cache frame */
115     av_fifo_generic_write(buf->fifo,
116                           &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
117 }
118
119 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
120                                   AVFilterBufferRef **bufref, int flags)
121 {
122     BufferSinkContext *buf = ctx->priv;
123     AVFilterLink *inlink = ctx->inputs[0];
124     int ret;
125     *bufref = NULL;
126
127     /* no picref available, fetch it from the filterchain */
128     if (!av_fifo_size(buf->fifo)) {
129         if ((ret = avfilter_request_frame(inlink)) < 0)
130             return ret;
131     }
132
133     if (!av_fifo_size(buf->fifo))
134         return AVERROR(EINVAL);
135
136     if (flags & AV_BUFFERSINK_FLAG_PEEK)
137         *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
138     else
139         av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
140
141     return 0;
142 }
143
144 int av_buffersink_poll_frame(AVFilterContext *ctx)
145 {
146     BufferSinkContext *buf = ctx->priv;
147     AVFilterLink *inlink = ctx->inputs[0];
148
149     return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
150 }
151
152 #if FF_API_OLD_VSINK_API
153 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
154                                          AVFilterBufferRef **picref, int flags)
155 {
156     return av_buffersink_get_buffer_ref(ctx, picref, flags);
157 }
158 #endif
159
160 #if CONFIG_BUFFERSINK_FILTER
161
162 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
163 {
164     BufferSinkContext *buf = ctx->priv;
165     av_unused AVBufferSinkParams *params;
166
167     if (!opaque) {
168         av_log(ctx, AV_LOG_ERROR,
169                "No opaque field provided\n");
170         return AVERROR(EINVAL);
171     } else {
172 #if FF_API_OLD_VSINK_API
173         const int *pixel_fmts = (const enum PixelFormat *)opaque;
174 #else
175         params = (AVBufferSinkParams *)opaque;
176         const int *pixel_fmts = params->pixel_fmts;
177 #endif
178         buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
179         if (!buf->pixel_fmts)
180             return AVERROR(ENOMEM);
181     }
182
183     return common_init(ctx);
184 }
185
186 static av_cold void vsink_uninit(AVFilterContext *ctx)
187 {
188     BufferSinkContext *buf = ctx->priv;
189     av_freep(&buf->pixel_fmts);
190     return common_uninit(ctx);
191 }
192
193 static int vsink_query_formats(AVFilterContext *ctx)
194 {
195     BufferSinkContext *buf = ctx->priv;
196
197     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
198     return 0;
199 }
200
201 AVFilter avfilter_vsink_buffersink = {
202     .name      = "buffersink",
203     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
204     .priv_size = sizeof(BufferSinkContext),
205     .init      = vsink_init,
206     .uninit    = vsink_uninit,
207
208     .query_formats = vsink_query_formats,
209
210     .inputs    = (const AVFilterPad[]) {{ .name    = "default",
211                                     .type          = AVMEDIA_TYPE_VIDEO,
212                                     .end_frame     = end_frame,
213                                     .min_perms     = AV_PERM_READ, },
214                                   { .name = NULL }},
215     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
216 };
217
218 #endif /* CONFIG_BUFFERSINK_FILTER */
219
220 #if CONFIG_ABUFFERSINK_FILTER
221
222 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
223 {
224     end_frame(link);
225 }
226
227 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
228 {
229     BufferSinkContext *buf = ctx->priv;
230     AVABufferSinkParams *params;
231
232     if (!opaque) {
233         av_log(ctx, AV_LOG_ERROR,
234                "No opaque field provided, an AVABufferSinkParams struct is required\n");
235         return AVERROR(EINVAL);
236     } else
237         params = (AVABufferSinkParams *)opaque;
238
239     buf->sample_fmts     = ff_copy_int_list  (params->sample_fmts);
240     buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
241     buf->packing_fmts    = ff_copy_int_list  (params->packing_fmts);
242     if (!buf->sample_fmts || !buf->channel_layouts || !buf->sample_fmts) {
243         av_freep(&buf->sample_fmts);
244         av_freep(&buf->channel_layouts);
245         av_freep(&buf->packing_fmts);
246         return AVERROR(ENOMEM);
247     }
248
249     return common_init(ctx);
250 }
251
252 static av_cold void asink_uninit(AVFilterContext *ctx)
253 {
254     BufferSinkContext *buf = ctx->priv;
255
256     av_freep(&buf->sample_fmts);
257     av_freep(&buf->channel_layouts);
258     av_freep(&buf->packing_fmts);
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
267     if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
268         return AVERROR(ENOMEM);
269     avfilter_set_common_sample_formats(ctx, formats);
270
271     if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
272         return AVERROR(ENOMEM);
273     avfilter_set_common_channel_layouts(ctx, formats);
274
275     if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
276         return AVERROR(ENOMEM);
277     avfilter_set_common_packing_formats(ctx, formats);
278
279     return 0;
280 }
281
282 AVFilter avfilter_asink_abuffersink = {
283     .name      = "abuffersink",
284     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
285     .init      = asink_init,
286     .uninit    = asink_uninit,
287     .priv_size = sizeof(BufferSinkContext),
288     .query_formats = asink_query_formats,
289
290     .inputs    = (const AVFilterPad[]) {{ .name     = "default",
291                                     .type           = AVMEDIA_TYPE_AUDIO,
292                                     .filter_samples = filter_samples,
293                                     .min_perms      = AV_PERM_READ, },
294                                   { .name = NULL }},
295     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
296 };
297
298 #endif /* CONFIG_ABUFFERSINK_FILTER */