wtv: Drop some casts that now are unnecessary
[libav.git] / libavfilter / buffersink.c
CommitLineData
ac712309
AK
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
a2cd9be2 26#include "libavutil/audio_fifo.h"
58b049f2 27#include "libavutil/avassert.h"
a903f8f0 28#include "libavutil/channel_layout.h"
1d9c2dc8 29#include "libavutil/common.h"
a2cd9be2 30#include "libavutil/mathematics.h"
ac712309 31
a2cd9be2 32#include "audio.h"
ac712309
AK
33#include "avfilter.h"
34#include "buffersink.h"
803391f7 35#include "internal.h"
ac712309
AK
36
37typedef struct {
7e350379 38 AVFrame *cur_frame; ///< last frame delivered on the sink
a2cd9be2
AK
39 AVAudioFifo *audio_fifo; ///< FIFO for audio samples
40 int64_t next_pts; ///< interpolating audio pts
ac712309
AK
41} BufferSinkContext;
42
ac712309
AK
43static av_cold void uninit(AVFilterContext *ctx)
44{
45 BufferSinkContext *sink = ctx->priv;
46
a2cd9be2
AK
47 if (sink->audio_fifo)
48 av_audio_fifo_free(sink->audio_fifo);
ac712309
AK
49}
50
7e350379 51static int filter_frame(AVFilterLink *link, AVFrame *frame)
ac712309 52{
58b049f2 53 BufferSinkContext *s = link->dst->priv;
ac712309 54
7e350379
AK
55 av_assert0(!s->cur_frame);
56 s->cur_frame = frame;
a2cd9be2 57
cd991462 58 return 0;
6b776c61 59}
cd991462 60
20c86571
HL
61int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx,
62 AVFrame *frame)
ac712309 63{
58b049f2 64 BufferSinkContext *s = ctx->priv;
ac712309
AK
65 AVFilterLink *link = ctx->inputs[0];
66 int ret;
67
58b049f2 68 if ((ret = ff_request_frame(link)) < 0)
ac712309
AK
69 return ret;
70
7e350379 71 if (!s->cur_frame)
ac712309
AK
72 return AVERROR(EINVAL);
73
7e350379
AK
74 av_frame_move_ref(frame, s->cur_frame);
75 av_frame_free(&s->cur_frame);
ac712309
AK
76
77 return 0;
78}
79
7e350379 80static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
a2cd9be2
AK
81 int nb_samples)
82{
83 BufferSinkContext *s = ctx->priv;
84 AVFilterLink *link = ctx->inputs[0];
7e350379 85 AVFrame *tmp;
a2cd9be2 86
7e350379 87 if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
a2cd9be2 88 return AVERROR(ENOMEM);
7e350379 89 av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
a2cd9be2 90
7e350379 91 tmp->pts = s->next_pts;
a2cd9be2
AK
92 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
93 link->time_base);
94
7e350379
AK
95 av_frame_move_ref(frame, tmp);
96 av_frame_free(&tmp);
97
a2cd9be2
AK
98 return 0;
99
100}
101
20c86571
HL
102int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
103 AVFrame *frame, int nb_samples)
a2cd9be2
AK
104{
105 BufferSinkContext *s = ctx->priv;
106 AVFilterLink *link = ctx->inputs[0];
107 int ret = 0;
108
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);
113 }
114
115 while (ret >= 0) {
a2cd9be2 116 if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
7e350379 117 return read_from_fifo(ctx, frame, nb_samples);
a2cd9be2 118
7e350379 119 ret = ff_request_frame(link);
a2cd9be2 120 if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
7e350379 121 return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
a2cd9be2
AK
122 else if (ret < 0)
123 return ret;
124
7e350379
AK
125 if (s->cur_frame->pts != AV_NOPTS_VALUE) {
126 s->next_pts = s->cur_frame->pts -
a2cd9be2
AK
127 av_rescale_q(av_audio_fifo_size(s->audio_fifo),
128 (AVRational){ 1, link->sample_rate },
129 link->time_base);
130 }
131
7e350379
AK
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);
a2cd9be2
AK
135 }
136
137 return ret;
7e350379
AK
138
139}
140
141#if FF_API_AVFILTERBUFFER
142static void compat_free_buffer(AVFilterBuffer *buf)
143{
144 AVFrame *frame = buf->priv;
145 av_frame_free(&frame);
146 av_free(buf);
147}
148
779e6c2b
HL
149static int compat_read(AVFilterContext *ctx,
150 AVFilterBufferRef **pbuf, int nb_samples)
7e350379
AK
151{
152 AVFilterBufferRef *buf;
153 AVFrame *frame;
154 int ret;
155
156 if (!pbuf)
157 return ff_poll_frame(ctx->inputs[0]);
158
159 frame = av_frame_alloc();
160 if (!frame)
161 return AVERROR(ENOMEM);
162
163 if (!nb_samples)
164 ret = av_buffersink_get_frame(ctx, frame);
165 else
166 ret = av_buffersink_get_samples(ctx, frame, nb_samples);
167
168 if (ret < 0)
169 goto fail;
170
171 if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
172 buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
173 AV_PERM_READ,
174 frame->width, frame->height,
175 frame->format);
176 } else {
177 buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
178 frame->linesize[0], AV_PERM_READ,
179 frame->nb_samples,
180 frame->format,
181 frame->channel_layout);
182 }
183 if (!buf) {
184 ret = AVERROR(ENOMEM);
185 goto fail;
186 }
187
188 avfilter_copy_frame_props(buf, frame);
189
190 buf->buf->priv = frame;
191 buf->buf->free = compat_free_buffer;
192
193 *pbuf = buf;
194
195 return 0;
196fail:
197 av_frame_free(&frame);
198 return ret;
199}
200
779e6c2b 201int attribute_align_arg av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
7e350379
AK
202{
203 return compat_read(ctx, buf, 0);
204}
205
779e6c2b 206int attribute_align_arg av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
7e350379
AK
207 int nb_samples)
208{
209 return compat_read(ctx, buf, nb_samples);
a2cd9be2 210}
7e350379 211#endif
a2cd9be2 212
568c70e7
MR
213static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
214 {
215 .name = "default",
216 .type = AVMEDIA_TYPE_VIDEO,
565e4993 217 .filter_frame = filter_frame,
568c70e7
MR
218 .needs_fifo = 1
219 },
220 { NULL }
221};
222
ac712309
AK
223AVFilter avfilter_vsink_buffer = {
224 .name = "buffersink",
225 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
226 .priv_size = sizeof(BufferSinkContext),
ac712309
AK
227 .uninit = uninit,
228
568c70e7 229 .inputs = avfilter_vsink_buffer_inputs,
1fce361d 230 .outputs = NULL,
ac712309 231};
a2cd9be2 232
568c70e7
MR
233static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
234 {
235 .name = "default",
236 .type = AVMEDIA_TYPE_AUDIO,
565e4993 237 .filter_frame = filter_frame,
568c70e7
MR
238 .needs_fifo = 1
239 },
240 { NULL }
241};
242
a2cd9be2
AK
243AVFilter avfilter_asink_abuffer = {
244 .name = "abuffersink",
245 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
246 .priv_size = sizeof(BufferSinkContext),
a2cd9be2
AK
247 .uninit = uninit,
248
568c70e7 249 .inputs = avfilter_asink_abuffer_inputs,
1fce361d 250 .outputs = NULL,
a2cd9be2 251};