2 * Copyright (c) 2011 Stefano Sabatini
4 * This file is part of Libav.
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.
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.
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
26 #include "libavutil/audio_fifo.h"
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/mathematics.h"
33 #include "buffersink.h"
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
42 static av_cold void uninit(AVFilterContext *ctx)
44 BufferSinkContext *sink = ctx->priv;
47 av_audio_fifo_free(sink->audio_fifo);
50 static void start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
52 BufferSinkContext *s = link->dst->priv;
54 av_assert0(!s->cur_buf);
59 static int filter_samples(AVFilterLink *link, AVFilterBufferRef *buf)
61 start_frame(link, buf);
65 int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
67 BufferSinkContext *s = ctx->priv;
68 AVFilterLink *link = ctx->inputs[0];
72 return ff_poll_frame(ctx->inputs[0]);
74 if ((ret = ff_request_frame(link)) < 0)
78 return AVERROR(EINVAL);
86 static int read_from_fifo(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
89 BufferSinkContext *s = ctx->priv;
90 AVFilterLink *link = ctx->inputs[0];
91 AVFilterBufferRef *buf;
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);
97 buf->pts = s->next_pts;
98 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
106 int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
109 BufferSinkContext *s = ctx->priv;
110 AVFilterLink *link = ctx->inputs[0];
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);
120 AVFilterBufferRef *buf;
122 if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
123 return read_from_fifo(ctx, pbuf, nb_samples);
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));
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 },
138 ret = av_audio_fifo_write(s->audio_fifo, (void**)buf->extended_data,
139 buf->audio->nb_samples);
140 avfilter_unref_buffer(buf);
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),
152 .inputs = (AVFilterPad[]) {{ .name = "default",
153 .type = AVMEDIA_TYPE_VIDEO,
154 .start_frame = start_frame,
155 .min_perms = AV_PERM_READ,
158 .outputs = (AVFilterPad[]) {{ .name = NULL }},
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),
167 .inputs = (AVFilterPad[]) {{ .name = "default",
168 .type = AVMEDIA_TYPE_AUDIO,
169 .filter_samples = filter_samples,
170 .min_perms = AV_PERM_READ,
173 .outputs = (AVFilterPad[]) {{ .name = NULL }},