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