]> git.sesse.net Git - ffmpeg/blob - libavfilter/sink_buffer.c
Merge commit 'f1d8763a02b5fce9a7d9789e049d74a45b15e1e8'
[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/channel_layout.h"
28 #include "libavutil/fifo.h"
29 #include "avfilter.h"
30 #include "buffersink.h"
31 #include "audio.h"
32 #include "internal.h"
33
34 AVBufferSinkParams *av_buffersink_params_alloc(void)
35 {
36     static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
37     AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
38     if (!params)
39         return NULL;
40
41     params->pixel_fmts = pixel_fmts;
42     return params;
43 }
44
45 AVABufferSinkParams *av_abuffersink_params_alloc(void)
46 {
47     static const int sample_fmts[] = { AV_SAMPLE_FMT_NONE };
48     static const int64_t channel_layouts[] = { -1 };
49     AVABufferSinkParams *params = av_malloc(sizeof(AVABufferSinkParams));
50
51     if (!params)
52         return NULL;
53
54     params->sample_fmts = sample_fmts;
55     params->channel_layouts = channel_layouts;
56     return params;
57 }
58
59 typedef struct {
60     AVFifoBuffer *fifo;                      ///< FIFO buffer of video frame references
61     unsigned warning_limit;
62
63     /* only used for video */
64     enum AVPixelFormat *pixel_fmts;           ///< list of accepted pixel formats, must be terminated with -1
65
66     /* only used for audio */
67     enum AVSampleFormat *sample_fmts;       ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
68     int64_t *channel_layouts;               ///< list of accepted channel layouts, terminated by -1
69 } BufferSinkContext;
70
71 #define FIFO_INIT_SIZE 8
72
73 static av_cold int common_init(AVFilterContext *ctx)
74 {
75     BufferSinkContext *buf = ctx->priv;
76
77     buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
78     if (!buf->fifo) {
79         av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
80         return AVERROR(ENOMEM);
81     }
82     buf->warning_limit = 100;
83     return 0;
84 }
85
86 static av_cold void common_uninit(AVFilterContext *ctx)
87 {
88     BufferSinkContext *buf = ctx->priv;
89     AVFilterBufferRef *picref;
90
91     if (buf->fifo) {
92         while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
93             av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
94             avfilter_unref_buffer(picref);
95         }
96         av_fifo_free(buf->fifo);
97         buf->fifo = NULL;
98     }
99 }
100
101 static int add_buffer_ref(AVFilterContext *ctx, AVFilterBufferRef *ref)
102 {
103     BufferSinkContext *buf = ctx->priv;
104
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, &ref, sizeof(AVFilterBufferRef *), NULL);
117     return 0;
118 }
119
120 static int end_frame(AVFilterLink *inlink)
121 {
122     AVFilterContext *ctx = inlink->dst;
123     BufferSinkContext *buf = inlink->dst->priv;
124     int ret;
125
126     av_assert1(inlink->cur_buf);
127     if ((ret = add_buffer_ref(ctx, inlink->cur_buf)) < 0)
128         return ret;
129     inlink->cur_buf = NULL;
130     if (buf->warning_limit &&
131         av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
132         av_log(ctx, AV_LOG_WARNING,
133                "%d buffers queued in %s, something may be wrong.\n",
134                buf->warning_limit,
135                (char *)av_x_if_null(ctx->name, ctx->filter->name));
136         buf->warning_limit *= 10;
137     }
138     return 0;
139 }
140
141 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
142 {
143     AVFilterLink *inlink = ctx->inputs[0];
144
145     inlink->min_samples = inlink->max_samples =
146     inlink->partial_buf_size = frame_size;
147 }
148
149 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
150                                   AVFilterBufferRef **bufref, int flags)
151 {
152     BufferSinkContext *buf = ctx->priv;
153     AVFilterLink *inlink = ctx->inputs[0];
154     int ret;
155     *bufref = NULL;
156
157     av_assert0(    !strcmp(ctx->filter->name, "buffersink")
158                 || !strcmp(ctx->filter->name, "abuffersink")
159                 || !strcmp(ctx->filter->name, "ffbuffersink")
160                 || !strcmp(ctx->filter->name, "ffabuffersink"));
161
162     /* no picref available, fetch it from the filterchain */
163     if (!av_fifo_size(buf->fifo)) {
164         if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
165             return AVERROR(EAGAIN);
166         if ((ret = ff_request_frame(inlink)) < 0)
167             return ret;
168     }
169
170     if (!av_fifo_size(buf->fifo))
171         return AVERROR(EINVAL);
172
173     if (flags & AV_BUFFERSINK_FLAG_PEEK)
174         *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
175     else
176         av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
177
178     return 0;
179 }
180
181 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
182 {
183     av_assert0(   !strcmp(ctx->filter->name, "buffersink")
184                || !strcmp(ctx->filter->name, "ffbuffersink"));
185
186     return ctx->inputs[0]->frame_rate;
187 }
188
189 int av_buffersink_poll_frame(AVFilterContext *ctx)
190 {
191     BufferSinkContext *buf = ctx->priv;
192     AVFilterLink *inlink = ctx->inputs[0];
193
194     av_assert0(   !strcmp(ctx->filter->name, "buffersink")
195                || !strcmp(ctx->filter->name, "abuffersink")
196                || !strcmp(ctx->filter->name, "ffbuffersink")
197                || !strcmp(ctx->filter->name, "ffabuffersink"));
198
199     return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
200 }
201
202 static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
203 {
204     BufferSinkContext *buf = ctx->priv;
205     AVBufferSinkParams *params = opaque;
206
207     if (params && params->pixel_fmts) {
208         const int *pixel_fmts = params->pixel_fmts;
209
210         buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
211         if (!buf->pixel_fmts)
212             return AVERROR(ENOMEM);
213     }
214
215     return common_init(ctx);
216 }
217
218 static av_cold void vsink_uninit(AVFilterContext *ctx)
219 {
220     BufferSinkContext *buf = ctx->priv;
221     av_freep(&buf->pixel_fmts);
222     common_uninit(ctx);
223 }
224
225 static int vsink_query_formats(AVFilterContext *ctx)
226 {
227     BufferSinkContext *buf = ctx->priv;
228
229     if (buf->pixel_fmts)
230         ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
231     else
232         ff_default_query_formats(ctx);
233
234     return 0;
235 }
236
237 static const AVFilterPad ffbuffersink_inputs[] = {
238     {
239         .name      = "default",
240         .type      = AVMEDIA_TYPE_VIDEO,
241         .end_frame = end_frame,
242         .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
243     },
244     { NULL },
245 };
246
247 AVFilter avfilter_vsink_ffbuffersink = {
248     .name      = "ffbuffersink",
249     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
250     .priv_size = sizeof(BufferSinkContext),
251     .init_opaque = vsink_init,
252     .uninit    = vsink_uninit,
253
254     .query_formats = vsink_query_formats,
255     .inputs        = ffbuffersink_inputs,
256     .outputs       = NULL,
257 };
258
259 static const AVFilterPad buffersink_inputs[] = {
260     {
261         .name      = "default",
262         .type      = AVMEDIA_TYPE_VIDEO,
263         .end_frame = end_frame,
264         .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
265     },
266     { NULL },
267 };
268
269 AVFilter avfilter_vsink_buffersink = {
270     .name      = "buffersink",
271     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
272     .priv_size = sizeof(BufferSinkContext),
273     .init_opaque = vsink_init,
274     .uninit    = vsink_uninit,
275
276     .query_formats = vsink_query_formats,
277     .inputs        = buffersink_inputs,
278     .outputs       = NULL,
279 };
280
281 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *samplesref)
282 {
283     end_frame(link);
284     return 0;
285 }
286
287 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
288 {
289     BufferSinkContext *buf = ctx->priv;
290     AVABufferSinkParams *params = opaque;
291
292     if (params && params->sample_fmts) {
293         buf->sample_fmts     = ff_copy_int_list  (params->sample_fmts);
294         if (!buf->sample_fmts)
295             goto fail_enomem;
296     }
297     if (params && params->channel_layouts) {
298         buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
299         if (!buf->channel_layouts)
300             goto fail_enomem;
301     }
302     if (!common_init(ctx))
303         return 0;
304
305 fail_enomem:
306     av_freep(&buf->sample_fmts);
307     av_freep(&buf->channel_layouts);
308     return AVERROR(ENOMEM);
309 }
310
311 static av_cold void asink_uninit(AVFilterContext *ctx)
312 {
313     BufferSinkContext *buf = ctx->priv;
314
315     av_freep(&buf->sample_fmts);
316     av_freep(&buf->channel_layouts);
317     common_uninit(ctx);
318 }
319
320 static int asink_query_formats(AVFilterContext *ctx)
321 {
322     BufferSinkContext *buf = ctx->priv;
323     AVFilterFormats *formats = NULL;
324     AVFilterChannelLayouts *layouts = NULL;
325
326     if (buf->sample_fmts) {
327     if (!(formats = ff_make_format_list(buf->sample_fmts)))
328         return AVERROR(ENOMEM);
329     ff_set_common_formats(ctx, formats);
330     }
331
332     if (buf->channel_layouts) {
333     if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
334         return AVERROR(ENOMEM);
335     ff_set_common_channel_layouts(ctx, layouts);
336     }
337
338     return 0;
339 }
340
341 static const AVFilterPad ffabuffersink_inputs[] = {
342     {
343         .name           = "default",
344         .type           = AVMEDIA_TYPE_AUDIO,
345         .filter_frame   = filter_frame,
346         .min_perms      = AV_PERM_READ | AV_PERM_PRESERVE,
347     },
348     { NULL },
349 };
350
351 AVFilter avfilter_asink_ffabuffersink = {
352     .name      = "ffabuffersink",
353     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
354     .init_opaque = asink_init,
355     .uninit    = asink_uninit,
356     .priv_size = sizeof(BufferSinkContext),
357     .query_formats = asink_query_formats,
358     .inputs        = ffabuffersink_inputs,
359     .outputs       = NULL,
360 };
361
362 static const AVFilterPad abuffersink_inputs[] = {
363     {
364         .name           = "default",
365         .type           = AVMEDIA_TYPE_AUDIO,
366         .filter_frame   = filter_frame,
367         .min_perms      = AV_PERM_READ | AV_PERM_PRESERVE,
368     },
369     { NULL },
370 };
371
372 AVFilter avfilter_asink_abuffersink = {
373     .name      = "abuffersink",
374     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
375     .init_opaque = asink_init,
376     .uninit    = asink_uninit,
377     .priv_size = sizeof(BufferSinkContext),
378     .query_formats = asink_query_formats,
379     .inputs        = abuffersink_inputs,
380     .outputs       = NULL,
381 };
382
383 /* Libav compatibility API */
384
385 extern AVFilter avfilter_vsink_buffer;
386 extern AVFilter avfilter_asink_abuffer;
387
388 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
389 {
390     AVFilterBufferRef *tbuf;
391     int ret;
392
393     if (ctx->filter->          inputs[0].start_frame ==
394         avfilter_vsink_buffer. inputs[0].start_frame ||
395         ctx->filter->          inputs[0].filter_frame ==
396         avfilter_asink_abuffer.inputs[0].filter_frame)
397         return ff_buffersink_read_compat(ctx, buf);
398     av_assert0(ctx->filter->                inputs[0].end_frame ==
399                avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
400                ctx->filter->                inputs[0].filter_frame ==
401                avfilter_asink_ffabuffersink.inputs[0].filter_frame);
402
403     ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
404                                        buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
405     if (!buf)
406         return ret >= 0;
407     if (ret < 0)
408         return ret;
409     *buf = tbuf;
410     return 0;
411 }
412
413 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
414                                int nb_samples)
415 {
416     BufferSinkContext *sink = ctx->priv;
417     int ret = 0, have_samples = 0, need_samples;
418     AVFilterBufferRef *tbuf, *in_buf;
419     AVFilterLink *link = ctx->inputs[0];
420     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
421
422     if (ctx->filter->          inputs[0].filter_frame ==
423         avfilter_asink_abuffer.inputs[0].filter_frame)
424         return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
425     av_assert0(ctx->filter->                inputs[0].filter_frame ==
426                avfilter_asink_ffabuffersink.inputs[0].filter_frame);
427
428     tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
429     if (!tbuf)
430         return AVERROR(ENOMEM);
431
432     while (have_samples < nb_samples) {
433         ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
434                                            AV_BUFFERSINK_FLAG_PEEK);
435         if (ret < 0) {
436             if (ret == AVERROR_EOF && have_samples) {
437                 nb_samples = have_samples;
438                 ret = 0;
439             }
440             break;
441         }
442
443         need_samples = FFMIN(in_buf->audio->nb_samples,
444                              nb_samples - have_samples);
445         av_samples_copy(tbuf->extended_data, in_buf->extended_data,
446                         have_samples, 0, need_samples,
447                         nb_channels, in_buf->format);
448         have_samples += need_samples;
449         if (need_samples < in_buf->audio->nb_samples) {
450             in_buf->audio->nb_samples -= need_samples;
451             av_samples_copy(in_buf->extended_data, in_buf->extended_data,
452                             0, need_samples, in_buf->audio->nb_samples,
453                             nb_channels, in_buf->format);
454         } else {
455             av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
456             avfilter_unref_buffer(in_buf);
457         }
458     }
459     tbuf->audio->nb_samples = have_samples;
460
461     if (ret < 0) {
462         av_assert0(!av_fifo_size(sink->fifo));
463         if (have_samples)
464             add_buffer_ref(ctx, tbuf);
465         else
466             avfilter_unref_buffer(tbuf);
467         return ret;
468     }
469
470     *buf = tbuf;
471     return 0;
472 }