h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / fifo.c
CommitLineData
7f1af825 1/*
3fa77bde 2 * Copyright (c) 2007 Bobby Bingham
7f1af825 3 *
2912e87a 4 * This file is part of Libav.
7f1af825 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
7f1af825
SS
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 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
7f1af825
SS
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
2912e87a 17 * License along with Libav; if not, write to the Free Software
7f1af825
SS
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
4a97ff2f 23 * FIFO buffering filter
7f1af825
SS
24 */
25
f75be985 26#include "libavutil/avassert.h"
a903f8f0 27#include "libavutil/channel_layout.h"
1d9c2dc8 28#include "libavutil/common.h"
f75be985
AK
29#include "libavutil/mathematics.h"
30#include "libavutil/samplefmt.h"
31
4a97ff2f 32#include "audio.h"
7f1af825 33#include "avfilter.h"
803391f7 34#include "internal.h"
c04c533f 35#include "video.h"
7f1af825 36
4a97ff2f 37typedef struct Buf {
7e350379 38 AVFrame *frame;
4a97ff2f
AK
39 struct Buf *next;
40} Buf;
7f1af825 41
58400ac1 42typedef struct FifoContext {
4a97ff2f
AK
43 Buf root;
44 Buf *last; ///< last buffered frame
f75be985
AK
45
46 /**
47 * When a specific number of output samples is requested, the partial
48 * buffer is stored here
49 */
7e350379
AK
50 AVFrame *out;
51 int allocated_samples; ///< number of samples out was allocated for
7f1af825
SS
52} FifoContext;
53
c43a7eca 54static av_cold int init(AVFilterContext *ctx)
7f1af825
SS
55{
56 FifoContext *fifo = ctx->priv;
57 fifo->last = &fifo->root;
58
7f1af825
SS
59 return 0;
60}
61
62static av_cold void uninit(AVFilterContext *ctx)
63{
64 FifoContext *fifo = ctx->priv;
4a97ff2f 65 Buf *buf, *tmp;
7f1af825 66
4a97ff2f
AK
67 for (buf = fifo->root.next; buf; buf = tmp) {
68 tmp = buf->next;
7e350379 69 av_frame_free(&buf->frame);
4a97ff2f 70 av_free(buf);
7f1af825 71 }
f75be985 72
7e350379 73 av_frame_free(&fifo->out);
7f1af825
SS
74}
75
7e350379 76static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
7f1af825
SS
77{
78 FifoContext *fifo = inlink->dst->priv;
79
4a97ff2f 80 fifo->last->next = av_mallocz(sizeof(Buf));
cd991462 81 if (!fifo->last->next) {
7e350379 82 av_frame_free(&frame);
cd991462
AK
83 return AVERROR(ENOMEM);
84 }
85
7f1af825 86 fifo->last = fifo->last->next;
7e350379 87 fifo->last->frame = frame;
cd991462
AK
88
89 return 0;
90}
91
f75be985
AK
92static void queue_pop(FifoContext *s)
93{
94 Buf *tmp = s->root.next->next;
95 if (s->last == s->root.next)
96 s->last = &s->root;
97 av_freep(&s->root.next);
98 s->root.next = tmp;
99}
100
f75be985
AK
101/**
102 * Move data pointers and pts offset samples forward.
103 */
7e350379 104static void buffer_offset(AVFilterLink *link, AVFrame *frame,
f75be985
AK
105 int offset)
106{
107 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
108 int planar = av_sample_fmt_is_planar(link->format);
109 int planes = planar ? nb_channels : 1;
110 int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
111 int i;
112
7e350379 113 av_assert0(frame->nb_samples > offset);
f75be985
AK
114
115 for (i = 0; i < planes; i++)
7e350379
AK
116 frame->extended_data[i] += block_align * offset;
117 if (frame->data != frame->extended_data)
118 memcpy(frame->data, frame->extended_data,
119 FFMIN(planes, FF_ARRAY_ELEMS(frame->data)) * sizeof(*frame->data));
120 frame->linesize[0] -= block_align*offset;
121 frame->nb_samples -= offset;
122
123 if (frame->pts != AV_NOPTS_VALUE) {
124 frame->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
125 link->time_base);
f75be985
AK
126 }
127}
128
7e350379 129static int calc_ptr_alignment(AVFrame *frame)
f75be985 130{
7e350379
AK
131 int planes = av_sample_fmt_is_planar(frame->format) ?
132 av_get_channel_layout_nb_channels(frame->channel_layout) : 1;
f75be985
AK
133 int min_align = 128;
134 int p;
135
136 for (p = 0; p < planes; p++) {
137 int cur_align = 128;
7e350379 138 while ((intptr_t)frame->extended_data[p] % cur_align)
f75be985
AK
139 cur_align >>= 1;
140 if (cur_align < min_align)
141 min_align = cur_align;
142 }
143 return min_align;
144}
145
146static int return_audio_frame(AVFilterContext *ctx)
147{
148 AVFilterLink *link = ctx->outputs[0];
149 FifoContext *s = ctx->priv;
3799376d 150 AVFrame *head = s->root.next ? s->root.next->frame : NULL;
7e350379 151 AVFrame *out;
f75be985
AK
152 int ret;
153
3799376d
AK
154 /* if head is NULL then we're flushing the remaining samples in out */
155 if (!head && !s->out)
156 return AVERROR_EOF;
157
7e350379
AK
158 if (!s->out &&
159 head->nb_samples >= link->request_samples &&
f75be985 160 calc_ptr_alignment(head) >= 32) {
7e350379
AK
161 if (head->nb_samples == link->request_samples) {
162 out = head;
f75be985
AK
163 queue_pop(s);
164 } else {
7e350379
AK
165 out = av_frame_clone(head);
166 if (!out)
1dc42050
AK
167 return AVERROR(ENOMEM);
168
7e350379 169 out->nb_samples = link->request_samples;
f75be985
AK
170 buffer_offset(link, head, link->request_samples);
171 }
172 } else {
173 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
174
7e350379
AK
175 if (!s->out) {
176 s->out = ff_get_audio_buffer(link, link->request_samples);
177 if (!s->out)
f75be985
AK
178 return AVERROR(ENOMEM);
179
7e350379
AK
180 s->out->nb_samples = 0;
181 s->out->pts = head->pts;
f75be985
AK
182 s->allocated_samples = link->request_samples;
183 } else if (link->request_samples != s->allocated_samples) {
184 av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
185 "buffer was returned.\n");
186 return AVERROR(EINVAL);
187 }
188
7e350379 189 while (s->out->nb_samples < s->allocated_samples) {
9bfc6e02
AK
190 int len;
191
192 if (!s->root.next) {
193 ret = ff_request_frame(ctx->inputs[0]);
194 if (ret == AVERROR_EOF) {
195 av_samples_set_silence(s->out->extended_data,
196 s->out->nb_samples,
197 s->allocated_samples -
198 s->out->nb_samples,
199 nb_channels, link->format);
200 s->out->nb_samples = s->allocated_samples;
201 break;
202 } else if (ret < 0)
203 return ret;
204 }
205 head = s->root.next->frame;
206
207 len = FFMIN(s->allocated_samples - s->out->nb_samples,
208 head->nb_samples);
f75be985 209
7e350379
AK
210 av_samples_copy(s->out->extended_data, head->extended_data,
211 s->out->nb_samples, 0, len, nb_channels,
f75be985 212 link->format);
7e350379 213 s->out->nb_samples += len;
f75be985 214
7e350379
AK
215 if (len == head->nb_samples) {
216 av_frame_free(&head);
f75be985 217 queue_pop(s);
f75be985
AK
218 } else {
219 buffer_offset(link, head, len);
220 }
221 }
7e350379
AK
222 out = s->out;
223 s->out = NULL;
f75be985 224 }
7e350379 225 return ff_filter_frame(link, out);
f75be985
AK
226}
227
7f1af825
SS
228static int request_frame(AVFilterLink *outlink)
229{
230 FifoContext *fifo = outlink->src->priv;
cd991462 231 int ret = 0;
7f1af825
SS
232
233 if (!fifo->root.next) {
3799376d
AK
234 if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0) {
235 if (ret == AVERROR_EOF && outlink->request_samples)
236 return return_audio_frame(outlink->src);
7f1af825 237 return ret;
3799376d 238 }
7f1af825
SS
239 }
240
565e4993
AK
241 if (outlink->request_samples) {
242 return return_audio_frame(outlink->src);
243 } else {
7e350379 244 ret = ff_filter_frame(outlink, fifo->root.next->frame);
f75be985 245 queue_pop(fifo);
4a97ff2f 246 }
7f1af825 247
cd991462 248 return ret;
7f1af825
SS
249}
250
568c70e7
MR
251static const AVFilterPad avfilter_vf_fifo_inputs[] = {
252 {
253 .name = "default",
254 .type = AVMEDIA_TYPE_VIDEO,
255 .get_video_buffer = ff_null_get_video_buffer,
565e4993 256 .filter_frame = add_to_queue,
568c70e7
MR
257 },
258 { NULL }
259};
260
261static const AVFilterPad avfilter_vf_fifo_outputs[] = {
262 {
263 .name = "default",
264 .type = AVMEDIA_TYPE_VIDEO,
265 .request_frame = request_frame,
266 },
267 { NULL }
268};
269
cd43ca04 270AVFilter ff_vf_fifo = {
7f1af825
SS
271 .name = "fifo",
272 .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
273
274 .init = init,
275 .uninit = uninit,
276
277 .priv_size = sizeof(FifoContext),
278
568c70e7
MR
279 .inputs = avfilter_vf_fifo_inputs,
280 .outputs = avfilter_vf_fifo_outputs,
281};
282
283static const AVFilterPad avfilter_af_afifo_inputs[] = {
284 {
285 .name = "default",
286 .type = AVMEDIA_TYPE_AUDIO,
287 .get_audio_buffer = ff_null_get_audio_buffer,
565e4993 288 .filter_frame = add_to_queue,
568c70e7
MR
289 },
290 { NULL }
291};
292
293static const AVFilterPad avfilter_af_afifo_outputs[] = {
294 {
295 .name = "default",
296 .type = AVMEDIA_TYPE_AUDIO,
297 .request_frame = request_frame,
298 },
299 { NULL }
7f1af825 300};
4a97ff2f 301
cd43ca04 302AVFilter ff_af_afifo = {
4a97ff2f
AK
303 .name = "afifo",
304 .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
305
306 .init = init,
307 .uninit = uninit,
308
309 .priv_size = sizeof(FifoContext),
310
568c70e7
MR
311 .inputs = avfilter_af_afifo_inputs,
312 .outputs = avfilter_af_afifo_outputs,
4a97ff2f 313};