]> git.sesse.net Git - ffmpeg/blob - libavfilter/sink_buffer.c
h264: (trivial) make ff_h264_lps_state static
[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 (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
130             return AVERROR(EAGAIN);
131         if ((ret = avfilter_request_frame(inlink)) < 0)
132             return ret;
133     }
134
135     if (!av_fifo_size(buf->fifo))
136         return AVERROR(EINVAL);
137
138     if (flags & AV_BUFFERSINK_FLAG_PEEK)
139         *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
140     else
141         av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
142
143     return 0;
144 }
145
146 int av_buffersink_poll_frame(AVFilterContext *ctx)
147 {
148     BufferSinkContext *buf = ctx->priv;
149     AVFilterLink *inlink = ctx->inputs[0];
150
151     return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
152 }
153
154 #if FF_API_OLD_VSINK_API
155 int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
156                                          AVFilterBufferRef **picref, int flags)
157 {
158     return av_buffersink_get_buffer_ref(ctx, picref, flags);
159 }
160 #endif
161
162 #if CONFIG_BUFFERSINK_FILTER
163
164 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
165 {
166     BufferSinkContext *buf = ctx->priv;
167     av_unused AVBufferSinkParams *params;
168
169     if (!opaque) {
170         av_log(ctx, AV_LOG_WARNING,
171                "No opaque field provided\n");
172         buf->pixel_fmts = NULL;
173     } else {
174 #if FF_API_OLD_VSINK_API
175         const int *pixel_fmts = (const enum PixelFormat *)opaque;
176 #else
177         params = (AVBufferSinkParams *)opaque;
178         const int *pixel_fmts = params->pixel_fmts;
179 #endif
180         buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
181         if (!buf->pixel_fmts)
182             return AVERROR(ENOMEM);
183     }
184
185     return common_init(ctx);
186 }
187
188 static av_cold void vsink_uninit(AVFilterContext *ctx)
189 {
190     BufferSinkContext *buf = ctx->priv;
191     av_freep(&buf->pixel_fmts);
192     return common_uninit(ctx);
193 }
194
195 static int vsink_query_formats(AVFilterContext *ctx)
196 {
197     BufferSinkContext *buf = ctx->priv;
198
199     if (buf->pixel_fmts)
200         avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
201     else
202         avfilter_default_query_formats(ctx);
203
204     return 0;
205 }
206
207 AVFilter avfilter_vsink_buffersink = {
208     .name      = "buffersink",
209     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
210     .priv_size = sizeof(BufferSinkContext),
211     .init      = vsink_init,
212     .uninit    = vsink_uninit,
213
214     .query_formats = vsink_query_formats,
215
216     .inputs    = (const AVFilterPad[]) {{ .name    = "default",
217                                     .type          = AVMEDIA_TYPE_VIDEO,
218                                     .end_frame     = end_frame,
219                                     .min_perms     = AV_PERM_READ, },
220                                   { .name = NULL }},
221     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
222 };
223
224 #endif /* CONFIG_BUFFERSINK_FILTER */
225
226 #if CONFIG_ABUFFERSINK_FILTER
227
228 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
229 {
230     end_frame(link);
231 }
232
233 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
234 {
235     BufferSinkContext *buf = ctx->priv;
236     AVABufferSinkParams *params;
237
238     if (!opaque) {
239         av_log(ctx, AV_LOG_ERROR,
240                "No opaque field provided, an AVABufferSinkParams struct is required\n");
241         return AVERROR(EINVAL);
242     } else
243         params = (AVABufferSinkParams *)opaque;
244
245     buf->sample_fmts     = ff_copy_int_list  (params->sample_fmts);
246     buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
247     buf->packing_fmts    = ff_copy_int_list  (params->packing_fmts);
248     if (!buf->sample_fmts || !buf->channel_layouts || !buf->sample_fmts) {
249         av_freep(&buf->sample_fmts);
250         av_freep(&buf->channel_layouts);
251         av_freep(&buf->packing_fmts);
252         return AVERROR(ENOMEM);
253     }
254
255     return common_init(ctx);
256 }
257
258 static av_cold void asink_uninit(AVFilterContext *ctx)
259 {
260     BufferSinkContext *buf = ctx->priv;
261
262     av_freep(&buf->sample_fmts);
263     av_freep(&buf->channel_layouts);
264     av_freep(&buf->packing_fmts);
265     return common_uninit(ctx);
266 }
267
268 static int asink_query_formats(AVFilterContext *ctx)
269 {
270     BufferSinkContext *buf = ctx->priv;
271     AVFilterFormats *formats = NULL;
272
273     if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
274         return AVERROR(ENOMEM);
275     avfilter_set_common_sample_formats(ctx, formats);
276
277     if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
278         return AVERROR(ENOMEM);
279     avfilter_set_common_channel_layouts(ctx, formats);
280
281     if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
282         return AVERROR(ENOMEM);
283     avfilter_set_common_packing_formats(ctx, formats);
284
285     return 0;
286 }
287
288 AVFilter avfilter_asink_abuffersink = {
289     .name      = "abuffersink",
290     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
291     .init      = asink_init,
292     .uninit    = asink_uninit,
293     .priv_size = sizeof(BufferSinkContext),
294     .query_formats = asink_query_formats,
295
296     .inputs    = (const AVFilterPad[]) {{ .name     = "default",
297                                     .type           = AVMEDIA_TYPE_AUDIO,
298                                     .filter_samples = filter_samples,
299                                     .min_perms      = AV_PERM_READ, },
300                                   { .name = NULL }},
301     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
302 };
303
304 #endif /* CONFIG_ABUFFERSINK_FILTER */