]> git.sesse.net Git - ffmpeg/blob - libavfilter/sink_buffer.c
x86: Fix assembly with NASM
[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/audioconvert.h"
27 #include "libavutil/avassert.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[] = { -1 };
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[] = { -1 };
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 AVFilter avfilter_vsink_ffbuffersink = {
238     .name      = "ffbuffersink",
239     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
240     .priv_size = sizeof(BufferSinkContext),
241     .init_opaque = vsink_init,
242     .uninit    = vsink_uninit,
243
244     .query_formats = vsink_query_formats,
245
246     .inputs    = (const AVFilterPad[]) {{ .name    = "default",
247                                     .type          = AVMEDIA_TYPE_VIDEO,
248                                     .end_frame     = end_frame,
249                                     .min_perms     = AV_PERM_READ | AV_PERM_PRESERVE, },
250                                   { .name = NULL }},
251     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
252 };
253
254 AVFilter avfilter_vsink_buffersink = {
255     .name      = "buffersink",
256     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
257     .priv_size = sizeof(BufferSinkContext),
258     .init_opaque = vsink_init,
259     .uninit    = vsink_uninit,
260
261     .query_formats = vsink_query_formats,
262
263     .inputs    = (const AVFilterPad[]) {{ .name    = "default",
264                                     .type          = AVMEDIA_TYPE_VIDEO,
265                                     .end_frame     = end_frame,
266                                     .min_perms     = AV_PERM_READ | AV_PERM_PRESERVE, },
267                                   { .name = NULL }},
268     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
269 };
270
271 static int filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
272 {
273     end_frame(link);
274     return 0;
275 }
276
277 static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
278 {
279     BufferSinkContext *buf = ctx->priv;
280     AVABufferSinkParams *params = opaque;
281
282     if (params && params->sample_fmts) {
283         buf->sample_fmts     = ff_copy_int_list  (params->sample_fmts);
284         if (!buf->sample_fmts)
285             goto fail_enomem;
286     }
287     if (params && params->channel_layouts) {
288         buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
289         if (!buf->channel_layouts)
290             goto fail_enomem;
291     }
292     if (!common_init(ctx))
293         return 0;
294
295 fail_enomem:
296     av_freep(&buf->sample_fmts);
297     av_freep(&buf->channel_layouts);
298     return AVERROR(ENOMEM);
299 }
300
301 static av_cold void asink_uninit(AVFilterContext *ctx)
302 {
303     BufferSinkContext *buf = ctx->priv;
304
305     av_freep(&buf->sample_fmts);
306     av_freep(&buf->channel_layouts);
307     common_uninit(ctx);
308 }
309
310 static int asink_query_formats(AVFilterContext *ctx)
311 {
312     BufferSinkContext *buf = ctx->priv;
313     AVFilterFormats *formats = NULL;
314     AVFilterChannelLayouts *layouts = NULL;
315
316     if (buf->sample_fmts) {
317     if (!(formats = ff_make_format_list(buf->sample_fmts)))
318         return AVERROR(ENOMEM);
319     ff_set_common_formats(ctx, formats);
320     }
321
322     if (buf->channel_layouts) {
323     if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
324         return AVERROR(ENOMEM);
325     ff_set_common_channel_layouts(ctx, layouts);
326     }
327
328     return 0;
329 }
330
331 AVFilter avfilter_asink_ffabuffersink = {
332     .name      = "ffabuffersink",
333     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
334     .init_opaque = asink_init,
335     .uninit    = asink_uninit,
336     .priv_size = sizeof(BufferSinkContext),
337     .query_formats = asink_query_formats,
338
339     .inputs    = (const AVFilterPad[]) {{ .name     = "default",
340                                     .type           = AVMEDIA_TYPE_AUDIO,
341                                     .filter_samples = filter_samples,
342                                     .min_perms      = AV_PERM_READ | AV_PERM_PRESERVE, },
343                                   { .name = NULL }},
344     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
345 };
346
347 AVFilter avfilter_asink_abuffersink = {
348     .name      = "abuffersink",
349     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
350     .init_opaque = asink_init,
351     .uninit    = asink_uninit,
352     .priv_size = sizeof(BufferSinkContext),
353     .query_formats = asink_query_formats,
354
355     .inputs    = (const AVFilterPad[]) {{ .name     = "default",
356                                     .type           = AVMEDIA_TYPE_AUDIO,
357                                     .filter_samples = filter_samples,
358                                     .min_perms      = AV_PERM_READ | AV_PERM_PRESERVE, },
359                                   { .name = NULL }},
360     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
361 };
362
363 /* Libav compatibility API */
364
365 extern AVFilter avfilter_vsink_buffer;
366 extern AVFilter avfilter_asink_abuffer;
367
368 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
369 {
370     AVFilterBufferRef *tbuf;
371     int ret;
372
373     if (ctx->filter->          inputs[0].start_frame ==
374         avfilter_vsink_buffer. inputs[0].start_frame ||
375         ctx->filter->          inputs[0].filter_samples ==
376         avfilter_asink_abuffer.inputs[0].filter_samples)
377         return ff_buffersink_read_compat(ctx, buf);
378     av_assert0(ctx->filter->                inputs[0].end_frame ==
379                avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
380                ctx->filter->                inputs[0].filter_samples ==
381                avfilter_asink_ffabuffersink.inputs[0].filter_samples);
382
383     ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
384                                        buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
385     if (!buf)
386         return ret >= 0;
387     if (ret < 0)
388         return ret;
389     *buf = tbuf;
390     return 0;
391 }
392
393 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
394                                int nb_samples)
395 {
396     BufferSinkContext *sink = ctx->priv;
397     int ret = 0, have_samples = 0, need_samples;
398     AVFilterBufferRef *tbuf, *in_buf;
399     AVFilterLink *link = ctx->inputs[0];
400     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
401
402     if (ctx->filter->          inputs[0].filter_samples ==
403         avfilter_asink_abuffer.inputs[0].filter_samples)
404         return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
405     av_assert0(ctx->filter->                inputs[0].filter_samples ==
406                avfilter_asink_ffabuffersink.inputs[0].filter_samples);
407
408     tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
409     if (!tbuf)
410         return AVERROR(ENOMEM);
411
412     while (have_samples < nb_samples) {
413         ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
414                                            AV_BUFFERSINK_FLAG_PEEK);
415         if (ret < 0) {
416             if (ret == AVERROR_EOF && have_samples) {
417                 nb_samples = have_samples;
418                 ret = 0;
419             }
420             break;
421         }
422
423         need_samples = FFMIN(in_buf->audio->nb_samples,
424                              nb_samples - have_samples);
425         av_samples_copy(tbuf->extended_data, in_buf->extended_data,
426                         have_samples, 0, need_samples,
427                         nb_channels, in_buf->format);
428         have_samples += need_samples;
429         if (need_samples < in_buf->audio->nb_samples) {
430             in_buf->audio->nb_samples -= need_samples;
431             av_samples_copy(in_buf->extended_data, in_buf->extended_data,
432                             0, need_samples, in_buf->audio->nb_samples,
433                             nb_channels, in_buf->format);
434         } else {
435             av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
436             avfilter_unref_buffer(in_buf);
437         }
438     }
439     tbuf->audio->nb_samples = have_samples;
440
441     if (ret < 0) {
442         av_assert0(!av_fifo_size(sink->fifo));
443         if (have_samples)
444             add_buffer_ref(ctx, tbuf);
445         else
446             avfilter_unref_buffer(tbuf);
447         return ret;
448     }
449
450     *buf = tbuf;
451     return 0;
452 }