imc: make IMDCT support stereo output
[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/fifo.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     AVFifoBuffer *fifo;          ///< FIFO buffer of frame references
38
39     AVAudioFifo  *audio_fifo;    ///< FIFO for audio samples
40     int64_t next_pts;            ///< interpolating audio pts
41 } BufferSinkContext;
42
43 #define FIFO_INIT_SIZE 8
44
45 static av_cold void uninit(AVFilterContext *ctx)
46 {
47     BufferSinkContext *sink = ctx->priv;
48
49     while (sink->fifo && av_fifo_size(sink->fifo)) {
50         AVFilterBufferRef *buf;
51         av_fifo_generic_read(sink->fifo, &buf, sizeof(buf), NULL);
52         avfilter_unref_buffer(buf);
53     }
54     av_fifo_free(sink->fifo);
55
56     if (sink->audio_fifo)
57         av_audio_fifo_free(sink->audio_fifo);
58 }
59
60 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
61 {
62     BufferSinkContext *sink = ctx->priv;
63
64     if (!(sink->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef*)))) {
65         av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
66         return AVERROR(ENOMEM);
67     }
68
69     return 0;
70 }
71
72 static void write_buf(AVFilterContext *ctx, AVFilterBufferRef *buf)
73 {
74     BufferSinkContext *sink = ctx->priv;
75
76     if (av_fifo_space(sink->fifo) < sizeof(AVFilterBufferRef *) &&
77         (av_fifo_realloc2(sink->fifo, av_fifo_size(sink->fifo) * 2) < 0)) {
78             av_log(ctx, AV_LOG_ERROR, "Error reallocating the FIFO.\n");
79             return;
80     }
81
82     av_fifo_generic_write(sink->fifo, &buf, sizeof(buf), NULL);
83 }
84
85 static void end_frame(AVFilterLink *link)
86 {
87     write_buf(link->dst, link->cur_buf);
88     link->cur_buf = NULL;
89 }
90
91 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *buf)
92 {
93     write_buf(link->dst, buf);
94 }
95
96 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
97 {
98     BufferSinkContext *sink = ctx->priv;
99     AVFilterLink      *link = ctx->inputs[0];
100     int ret;
101
102     if (!buf) {
103         if (av_fifo_size(sink->fifo))
104             return av_fifo_size(sink->fifo)/sizeof(*buf);
105         else
106             return ff_poll_frame(ctx->inputs[0]);
107     }
108
109     if (!av_fifo_size(sink->fifo) &&
110         (ret = ff_request_frame(link)) < 0)
111         return ret;
112
113     if (!av_fifo_size(sink->fifo))
114         return AVERROR(EINVAL);
115
116     av_fifo_generic_read(sink->fifo, buf, sizeof(*buf), NULL);
117
118     return 0;
119 }
120
121 static int read_from_fifo(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
122                           int nb_samples)
123 {
124     BufferSinkContext *s = ctx->priv;
125     AVFilterLink   *link = ctx->inputs[0];
126     AVFilterBufferRef *buf;
127
128     if (!(buf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples)))
129         return AVERROR(ENOMEM);
130     av_audio_fifo_read(s->audio_fifo, (void**)buf->extended_data, nb_samples);
131
132     buf->pts = s->next_pts;
133     s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
134                                 link->time_base);
135
136     *pbuf = buf;
137     return 0;
138
139 }
140
141 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
142                                int nb_samples)
143 {
144     BufferSinkContext *s = ctx->priv;
145     AVFilterLink   *link = ctx->inputs[0];
146     int ret = 0;
147
148     if (!s->audio_fifo) {
149         int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
150         if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
151             return AVERROR(ENOMEM);
152     }
153
154     while (ret >= 0) {
155         AVFilterBufferRef *buf;
156
157         if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
158             return read_from_fifo(ctx, pbuf, nb_samples);
159
160         ret = av_buffersink_read(ctx, &buf);
161         if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
162             return read_from_fifo(ctx, pbuf, av_audio_fifo_size(s->audio_fifo));
163         else if (ret < 0)
164             return ret;
165
166         if (buf->pts != AV_NOPTS_VALUE) {
167             s->next_pts = buf->pts -
168                           av_rescale_q(av_audio_fifo_size(s->audio_fifo),
169                                        (AVRational){ 1, link->sample_rate },
170                                        link->time_base);
171         }
172
173         ret = av_audio_fifo_write(s->audio_fifo, (void**)buf->extended_data,
174                                   buf->audio->nb_samples);
175         avfilter_unref_buffer(buf);
176     }
177
178     return ret;
179 }
180
181 AVFilter avfilter_vsink_buffer = {
182     .name      = "buffersink",
183     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
184     .priv_size = sizeof(BufferSinkContext),
185     .init      = init,
186     .uninit    = uninit,
187
188     .inputs    = (AVFilterPad[]) {{ .name          = "default",
189                                     .type          = AVMEDIA_TYPE_VIDEO,
190                                     .end_frame     = end_frame,
191                                     .min_perms     = AV_PERM_READ, },
192                                   { .name = NULL }},
193     .outputs   = (AVFilterPad[]) {{ .name = NULL }},
194 };
195
196 AVFilter avfilter_asink_abuffer = {
197     .name      = "abuffersink",
198     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
199     .priv_size = sizeof(BufferSinkContext),
200     .init      = init,
201     .uninit    = uninit,
202
203     .inputs    = (AVFilterPad[]) {{ .name           = "default",
204                                     .type           = AVMEDIA_TYPE_AUDIO,
205                                     .filter_samples = filter_samples,
206                                     .min_perms      = AV_PERM_READ, },
207                                   { .name = NULL }},
208     .outputs   = (AVFilterPad[]) {{ .name = NULL }},
209 };