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/avassert.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/common.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/mathematics.h"
35 #include "buffersink.h"
38 typedef struct BufferSinkContext {
39 AVFrame *cur_frame; ///< last frame delivered on the sink
40 AVAudioFifo *audio_fifo; ///< FIFO for audio samples
41 int64_t next_pts; ///< interpolating audio pts
44 static av_cold void uninit(AVFilterContext *ctx)
46 BufferSinkContext *sink = ctx->priv;
49 av_audio_fifo_free(sink->audio_fifo);
52 static int filter_frame(AVFilterLink *link, AVFrame *frame)
54 BufferSinkContext *s = link->dst->priv;
56 av_assert0(!s->cur_frame);
62 int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx,
65 BufferSinkContext *s = ctx->priv;
66 AVFilterLink *link = ctx->inputs[0];
69 if ((ret = ff_request_frame(link)) < 0)
73 return AVERROR(EINVAL);
75 av_frame_move_ref(frame, s->cur_frame);
76 av_frame_free(&s->cur_frame);
81 static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
84 BufferSinkContext *s = ctx->priv;
85 AVFilterLink *link = ctx->inputs[0];
88 if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
89 return AVERROR(ENOMEM);
90 av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
92 tmp->pts = s->next_pts;
93 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
96 av_frame_move_ref(frame, tmp);
102 int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
103 AVFrame *frame, int nb_samples)
105 BufferSinkContext *s = ctx->priv;
106 AVFilterLink *link = ctx->inputs[0];
109 if (!s->audio_fifo) {
110 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
111 if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
112 return AVERROR(ENOMEM);
116 if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
117 return read_from_fifo(ctx, frame, nb_samples);
119 ret = ff_request_frame(link);
120 if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
121 return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
125 if (s->cur_frame->pts != AV_NOPTS_VALUE) {
126 s->next_pts = s->cur_frame->pts -
127 av_rescale_q(av_audio_fifo_size(s->audio_fifo),
128 (AVRational){ 1, link->sample_rate },
132 ret = av_audio_fifo_write(s->audio_fifo, (void**)s->cur_frame->extended_data,
133 s->cur_frame->nb_samples);
134 av_frame_free(&s->cur_frame);
140 #if FF_API_AVFILTERBUFFER
141 FF_DISABLE_DEPRECATION_WARNINGS
142 static void compat_free_buffer(AVFilterBuffer *buf)
144 AVFrame *frame = buf->priv;
145 av_frame_free(&frame);
149 static int compat_read(AVFilterContext *ctx,
150 AVFilterBufferRef **pbuf, int nb_samples)
152 AVFilterBufferRef *buf;
157 return ff_poll_frame(ctx->inputs[0]);
159 frame = av_frame_alloc();
161 return AVERROR(ENOMEM);
164 ret = av_buffersink_get_frame(ctx, frame);
166 ret = av_buffersink_get_samples(ctx, frame, nb_samples);
171 if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
172 buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
174 frame->width, frame->height,
177 buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
178 frame->linesize[0], AV_PERM_READ,
181 frame->channel_layout);
184 ret = AVERROR(ENOMEM);
188 avfilter_copy_frame_props(buf, frame);
190 buf->buf->priv = frame;
191 buf->buf->free = compat_free_buffer;
197 av_frame_free(&frame);
201 int attribute_align_arg av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
203 return compat_read(ctx, buf, 0);
206 int attribute_align_arg av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
209 return compat_read(ctx, buf, nb_samples);
211 FF_ENABLE_DEPRECATION_WARNINGS
214 static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
217 .type = AVMEDIA_TYPE_VIDEO,
218 .filter_frame = filter_frame,
224 AVFilter ff_vsink_buffer = {
225 .name = "buffersink",
226 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
227 .priv_size = sizeof(BufferSinkContext),
230 .inputs = avfilter_vsink_buffer_inputs,
234 static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
237 .type = AVMEDIA_TYPE_AUDIO,
238 .filter_frame = filter_frame,
244 AVFilter ff_asink_abuffer = {
245 .name = "abuffersink",
246 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
247 .priv_size = sizeof(BufferSinkContext),
250 .inputs = avfilter_asink_abuffer_inputs,