lavfi: add error handling to filter_samples().
[ffmpeg.git] / libavfilter / buffersink.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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/audio_fifo.h"
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/mathematics.h"
30
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "buffersink.h"
34 #include "internal.h"
35
36 typedef struct {
37     AVFilterBufferRef *cur_buf;  ///< last buffer delivered on the sink
38     AVAudioFifo  *audio_fifo;    ///< FIFO for audio samples
39     int64_t next_pts;            ///< interpolating audio pts
40 } BufferSinkContext;
41
42 static av_cold void uninit(AVFilterContext *ctx)
43 {
44     BufferSinkContext *sink = ctx->priv;
45
46     if (sink->audio_fifo)
47         av_audio_fifo_free(sink->audio_fifo);
48 }
49
50 static void start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
51 {
52     BufferSinkContext *s = link->dst->priv;
53
54     av_assert0(!s->cur_buf);
55     s->cur_buf    = buf;
56     link->cur_buf = NULL;
57 };
58
59 static int filter_samples(AVFilterLink *link, AVFilterBufferRef *buf)
60 {
61     start_frame(link, buf);
62     return 0;
63 }
64
65 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
66 {
67     BufferSinkContext *s    = ctx->priv;
68     AVFilterLink      *link = ctx->inputs[0];
69     int ret;
70
71     if (!buf)
72         return ff_poll_frame(ctx->inputs[0]);
73
74     if ((ret = ff_request_frame(link)) < 0)
75         return ret;
76
77     if (!s->cur_buf)
78         return AVERROR(EINVAL);
79
80     *buf       = s->cur_buf;
81     s->cur_buf = NULL;
82
83     return 0;
84 }
85
86 static int read_from_fifo(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
87                           int nb_samples)
88 {
89     BufferSinkContext *s = ctx->priv;
90     AVFilterLink   *link = ctx->inputs[0];
91     AVFilterBufferRef *buf;
92
93     if (!(buf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples)))
94         return AVERROR(ENOMEM);
95     av_audio_fifo_read(s->audio_fifo, (void**)buf->extended_data, nb_samples);
96
97     buf->pts = s->next_pts;
98     s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
99                                 link->time_base);
100
101     *pbuf = buf;
102     return 0;
103
104 }
105
106 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
107                                int nb_samples)
108 {
109     BufferSinkContext *s = ctx->priv;
110     AVFilterLink   *link = ctx->inputs[0];
111     int ret = 0;
112
113     if (!s->audio_fifo) {
114         int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
115         if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
116             return AVERROR(ENOMEM);
117     }
118
119     while (ret >= 0) {
120         AVFilterBufferRef *buf;
121
122         if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
123             return read_from_fifo(ctx, pbuf, nb_samples);
124
125         ret = av_buffersink_read(ctx, &buf);
126         if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
127             return read_from_fifo(ctx, pbuf, av_audio_fifo_size(s->audio_fifo));
128         else if (ret < 0)
129             return ret;
130
131         if (buf->pts != AV_NOPTS_VALUE) {
132             s->next_pts = buf->pts -
133                           av_rescale_q(av_audio_fifo_size(s->audio_fifo),
134                                        (AVRational){ 1, link->sample_rate },
135                                        link->time_base);
136         }
137
138         ret = av_audio_fifo_write(s->audio_fifo, (void**)buf->extended_data,
139                                   buf->audio->nb_samples);
140         avfilter_unref_buffer(buf);
141     }
142
143     return ret;
144 }
145
146 AVFilter avfilter_vsink_buffer = {
147     .name      = "buffersink",
148     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
149     .priv_size = sizeof(BufferSinkContext),
150     .uninit    = uninit,
151
152     .inputs    = (AVFilterPad[]) {{ .name          = "default",
153                                     .type          = AVMEDIA_TYPE_VIDEO,
154                                     .start_frame   = start_frame,
155                                     .min_perms     = AV_PERM_READ,
156                                     .needs_fifo    = 1 },
157                                   { .name = NULL }},
158     .outputs   = (AVFilterPad[]) {{ .name = NULL }},
159 };
160
161 AVFilter avfilter_asink_abuffer = {
162     .name      = "abuffersink",
163     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
164     .priv_size = sizeof(BufferSinkContext),
165     .uninit    = uninit,
166
167     .inputs    = (AVFilterPad[]) {{ .name           = "default",
168                                     .type           = AVMEDIA_TYPE_AUDIO,
169                                     .filter_samples = filter_samples,
170                                     .min_perms      = AV_PERM_READ,
171                                     .needs_fifo     = 1 },
172                                   { .name = NULL }},
173     .outputs   = (AVFilterPad[]) {{ .name = NULL }},
174 };