OSDN Git Service

Merge remote branch 'official/master'
[coroid/ffmpeg_saccubus.git] / 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 #if FF_API_OLD_VSINK_API
144 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
145                                          AVFilterBufferRef **picref, int flags)
146 {
147     return av_buffersink_get_buffer_ref(ctx, picref, flags);
148 }
149 #endif
150
151 #if CONFIG_BUFFERSINK_FILTER
152
153 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
154 {
155     BufferSinkContext *buf = ctx->priv;
156     av_unused AVBufferSinkParams *params;
157
158     if (!opaque) {
159         av_log(ctx, AV_LOG_ERROR,
160                "No opaque field provided\n");
161         return AVERROR(EINVAL);
162     } else {
163 #if FF_API_OLD_VSINK_API
164         buf->pixel_fmts = (const enum PixelFormats *)opaque;
165 #else
166         params = (AVBufferSinkParams *)opaque;
167         buf->pixel_fmts = params->pixel_fmts;
168 #endif
169     }
170
171     return common_init(ctx);
172 }
173
174 static int vsink_query_formats(AVFilterContext *ctx)
175 {
176     BufferSinkContext *buf = ctx->priv;
177
178     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
179     return 0;
180 }
181
182 AVFilter avfilter_vsink_buffersink = {
183     .name      = "buffersink",
184     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
185     .priv_size = sizeof(BufferSinkContext),
186     .init      = vsink_init,
187     .uninit    = common_uninit,
188
189     .query_formats = vsink_query_formats,
190
191     .inputs    = (AVFilterPad[]) {{ .name          = "default",
192                                     .type          = AVMEDIA_TYPE_VIDEO,
193                                     .end_frame     = end_frame,
194                                     .min_perms     = AV_PERM_READ, },
195                                   { .name = NULL }},
196     .outputs   = (AVFilterPad[]) {{ .name = NULL }},
197 };
198
199 #endif /* CONFIG_BUFFERSINK_FILTER */
200
201 #if CONFIG_ABUFFERSINK_FILTER
202
203 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
204 {
205     end_frame(link);
206 }
207
208 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
209 {
210     BufferSinkContext *buf = ctx->priv;
211     AVABufferSinkParams *params;
212
213     if (!opaque) {
214         av_log(ctx, AV_LOG_ERROR,
215                "No opaque field provided, an AVABufferSinkParams struct is required\n");
216         return AVERROR(EINVAL);
217     } else
218         params = (AVABufferSinkParams *)opaque;
219
220     buf->sample_fmts     = params->sample_fmts;
221     buf->channel_layouts = params->channel_layouts;
222     buf->packing_fmts    = params->packing_fmts;
223
224     return common_init(ctx);
225 }
226
227 static int asink_query_formats(AVFilterContext *ctx)
228 {
229     BufferSinkContext *buf = ctx->priv;
230     AVFilterFormats *formats = NULL;
231
232     if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
233         return AVERROR(ENOMEM);
234     avfilter_set_common_sample_formats(ctx, formats);
235
236     if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
237         return AVERROR(ENOMEM);
238     avfilter_set_common_channel_layouts(ctx, formats);
239
240     if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
241         return AVERROR(ENOMEM);
242     avfilter_set_common_packing_formats(ctx, formats);
243
244     return 0;
245 }
246
247 AVFilter avfilter_asink_abuffersink = {
248     .name      = "abuffersink",
249     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
250     .init      = asink_init,
251     .uninit    = common_uninit,
252     .priv_size = sizeof(BufferSinkContext),
253     .query_formats = asink_query_formats,
254
255     .inputs    = (AVFilterPad[]) {{ .name           = "default",
256                                     .type           = AVMEDIA_TYPE_AUDIO,
257                                     .filter_samples = filter_samples,
258                                     .min_perms      = AV_PERM_READ, },
259                                   { .name = NULL }},
260     .outputs   = (AVFilterPad[]) {{ .name = NULL }},
261 };
262
263 #endif /* CONFIG_ABUFFERSINK_FILTER */