h264_metadata: Add option to delete filler data
[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"
7950e519 30#include "libavutil/internal.h"
a2cd9be2 31#include "libavutil/mathematics.h"
ac712309 32
a2cd9be2 33#include "audio.h"
ac712309
AK
34#include "avfilter.h"
35#include "buffersink.h"
803391f7 36#include "internal.h"
ac712309 37
58400ac1 38typedef struct BufferSinkContext {
7e350379 39 AVFrame *cur_frame; ///< last frame delivered on the sink
96668944 40 AVAudioFifo *audio_fifo; ///< FIFO for audio samples
a2cd9be2 41 int64_t next_pts; ///< interpolating audio pts
ac712309
AK
42} BufferSinkContext;
43
ac712309
AK
44static av_cold void uninit(AVFilterContext *ctx)
45{
46 BufferSinkContext *sink = ctx->priv;
47
a2cd9be2
AK
48 if (sink->audio_fifo)
49 av_audio_fifo_free(sink->audio_fifo);
ac712309
AK
50}
51
7e350379 52static int filter_frame(AVFilterLink *link, AVFrame *frame)
ac712309 53{
58b049f2 54 BufferSinkContext *s = link->dst->priv;
ac712309 55
7e350379
AK
56 av_assert0(!s->cur_frame);
57 s->cur_frame = frame;
a2cd9be2 58
cd991462 59 return 0;
6b776c61 60}
cd991462 61
20c86571
HL
62int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx,
63 AVFrame *frame)
ac712309 64{
58b049f2 65 BufferSinkContext *s = ctx->priv;
ac712309
AK
66 AVFilterLink *link = ctx->inputs[0];
67 int ret;
68
58b049f2 69 if ((ret = ff_request_frame(link)) < 0)
ac712309
AK
70 return ret;
71
7e350379 72 if (!s->cur_frame)
ac712309
AK
73 return AVERROR(EINVAL);
74
7e350379
AK
75 av_frame_move_ref(frame, s->cur_frame);
76 av_frame_free(&s->cur_frame);
ac712309
AK
77
78 return 0;
79}
80
7e350379 81static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
a2cd9be2
AK
82 int nb_samples)
83{
84 BufferSinkContext *s = ctx->priv;
85 AVFilterLink *link = ctx->inputs[0];
7e350379 86 AVFrame *tmp;
a2cd9be2 87
7e350379 88 if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
a2cd9be2 89 return AVERROR(ENOMEM);
7e350379 90 av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
a2cd9be2 91
7e350379 92 tmp->pts = s->next_pts;
a2cd9be2
AK
93 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
94 link->time_base);
95
7e350379
AK
96 av_frame_move_ref(frame, tmp);
97 av_frame_free(&tmp);
98
a2cd9be2 99 return 0;
a2cd9be2
AK
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
568c70e7
MR
140static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
141 {
96668944
DB
142 .name = "default",
143 .type = AVMEDIA_TYPE_VIDEO,
565e4993 144 .filter_frame = filter_frame,
96668944 145 .needs_fifo = 1
568c70e7
MR
146 },
147 { NULL }
148};
149
cd43ca04 150AVFilter ff_vsink_buffer = {
96668944 151 .name = "buffersink",
ac712309 152 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
96668944
DB
153 .priv_size = sizeof(BufferSinkContext),
154 .uninit = uninit,
ac712309 155
96668944
DB
156 .inputs = avfilter_vsink_buffer_inputs,
157 .outputs = NULL,
ac712309 158};
a2cd9be2 159
568c70e7
MR
160static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
161 {
96668944
DB
162 .name = "default",
163 .type = AVMEDIA_TYPE_AUDIO,
164 .filter_frame = filter_frame,
165 .needs_fifo = 1
568c70e7
MR
166 },
167 { NULL }
168};
169
cd43ca04 170AVFilter ff_asink_abuffer = {
96668944 171 .name = "abuffersink",
a2cd9be2 172 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
96668944
DB
173 .priv_size = sizeof(BufferSinkContext),
174 .uninit = uninit,
a2cd9be2 175
96668944
DB
176 .inputs = avfilter_asink_abuffer_inputs,
177 .outputs = NULL,
a2cd9be2 178};