mss2: simplify loop in decode_rle()
[libav.git] / libavfilter / af_asyncts.c
CommitLineData
9f26421b
AK
1/*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "libavresample/avresample.h"
20#include "libavutil/audio_fifo.h"
1d9c2dc8 21#include "libavutil/common.h"
9f26421b
AK
22#include "libavutil/mathematics.h"
23#include "libavutil/opt.h"
24#include "libavutil/samplefmt.h"
25
26#include "audio.h"
27#include "avfilter.h"
803391f7 28#include "internal.h"
9f26421b
AK
29
30typedef struct ASyncContext {
31 const AVClass *class;
32
33 AVAudioResampleContext *avr;
34 int64_t pts; ///< timestamp in samples of the first sample in fifo
35 int min_delta; ///< pad/trim min threshold in samples
36
37 /* options */
38 int resample;
39 float min_delta_sec;
40 int max_comp;
6f834293
AK
41
42 /* set by filter_samples() to signal an output frame to request_frame() */
43 int got_output;
9f26421b
AK
44} ASyncContext;
45
46#define OFFSET(x) offsetof(ASyncContext, x)
47#define A AV_OPT_FLAG_AUDIO_PARAM
48static const AVOption options[] = {
e6153f17 49 { "compensate", "Stretch/squeeze the data to make it match the timestamps", OFFSET(resample), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, A },
9f26421b 50 { "min_delta", "Minimum difference between timestamps and audio data "
c7b610aa 51 "(in seconds) to trigger padding/trimmin the data.", OFFSET(min_delta_sec), AV_OPT_TYPE_FLOAT, { .dbl = 0.1 }, 0, INT_MAX, A },
e6153f17 52 { "max_comp", "Maximum compensation in samples per second.", OFFSET(max_comp), AV_OPT_TYPE_INT, { .i64 = 500 }, 0, INT_MAX, A },
4d7adec8 53 { "first_pts", "Assume the first pts should be this value.", OFFSET(pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, A },
9f26421b
AK
54 { NULL },
55};
56
57static const AVClass async_class = {
58 .class_name = "asyncts filter",
59 .item_name = av_default_item_name,
60 .option = options,
61 .version = LIBAVUTIL_VERSION_INT,
62};
63
a5e8c41c 64static int init(AVFilterContext *ctx, const char *args)
9f26421b
AK
65{
66 ASyncContext *s = ctx->priv;
67 int ret;
68
69 s->class = &async_class;
70 av_opt_set_defaults(s);
71
72 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
73 av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
74 return ret;
75 }
76 av_opt_free(s);
77
9f26421b
AK
78 return 0;
79}
80
81static void uninit(AVFilterContext *ctx)
82{
83 ASyncContext *s = ctx->priv;
84
85 if (s->avr) {
86 avresample_close(s->avr);
87 avresample_free(&s->avr);
88 }
89}
90
91static int config_props(AVFilterLink *link)
92{
93 ASyncContext *s = link->src->priv;
94 int ret;
95
96 s->min_delta = s->min_delta_sec * link->sample_rate;
97 link->time_base = (AVRational){1, link->sample_rate};
98
99 s->avr = avresample_alloc_context();
100 if (!s->avr)
101 return AVERROR(ENOMEM);
102
103 av_opt_set_int(s->avr, "in_channel_layout", link->channel_layout, 0);
104 av_opt_set_int(s->avr, "out_channel_layout", link->channel_layout, 0);
105 av_opt_set_int(s->avr, "in_sample_fmt", link->format, 0);
106 av_opt_set_int(s->avr, "out_sample_fmt", link->format, 0);
107 av_opt_set_int(s->avr, "in_sample_rate", link->sample_rate, 0);
108 av_opt_set_int(s->avr, "out_sample_rate", link->sample_rate, 0);
109
110 if (s->resample)
111 av_opt_set_int(s->avr, "force_resampling", 1, 0);
112
113 if ((ret = avresample_open(s->avr)) < 0)
114 return ret;
115
116 return 0;
117}
118
119static int request_frame(AVFilterLink *link)
120{
121 AVFilterContext *ctx = link->src;
122 ASyncContext *s = ctx->priv;
6f834293 123 int ret = 0;
9f26421b
AK
124 int nb_samples;
125
6f834293
AK
126 s->got_output = 0;
127 while (ret >= 0 && !s->got_output)
128 ret = ff_request_frame(ctx->inputs[0]);
129
9f26421b
AK
130 /* flush the fifo */
131 if (ret == AVERROR_EOF && (nb_samples = avresample_get_delay(s->avr))) {
132 AVFilterBufferRef *buf = ff_get_audio_buffer(link, AV_PERM_WRITE,
133 nb_samples);
134 if (!buf)
135 return AVERROR(ENOMEM);
136 avresample_convert(s->avr, (void**)buf->extended_data, buf->linesize[0],
137 nb_samples, NULL, 0, 0);
138 buf->pts = s->pts;
cd991462 139 return ff_filter_samples(link, buf);
9f26421b
AK
140 }
141
142 return ret;
143}
144
cd991462 145static int write_to_fifo(ASyncContext *s, AVFilterBufferRef *buf)
9f26421b 146{
cd991462
AK
147 int ret = avresample_convert(s->avr, NULL, 0, 0, (void**)buf->extended_data,
148 buf->linesize[0], buf->audio->nb_samples);
9f26421b 149 avfilter_unref_buffer(buf);
cd991462 150 return ret;
9f26421b
AK
151}
152
153/* get amount of data currently buffered, in samples */
154static int64_t get_delay(ASyncContext *s)
155{
156 return avresample_available(s->avr) + avresample_get_delay(s->avr);
157}
158
cd991462 159static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
9f26421b
AK
160{
161 AVFilterContext *ctx = inlink->dst;
162 ASyncContext *s = ctx->priv;
163 AVFilterLink *outlink = ctx->outputs[0];
164 int nb_channels = av_get_channel_layout_nb_channels(buf->audio->channel_layout);
165 int64_t pts = (buf->pts == AV_NOPTS_VALUE) ? buf->pts :
166 av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
cd991462 167 int out_size, ret;
9f26421b
AK
168 int64_t delta;
169
170 /* buffer data until we get the first timestamp */
171 if (s->pts == AV_NOPTS_VALUE) {
172 if (pts != AV_NOPTS_VALUE) {
173 s->pts = pts - get_delay(s);
174 }
cd991462 175 return write_to_fifo(s, buf);
9f26421b
AK
176 }
177
178 /* now wait for the next timestamp */
179 if (pts == AV_NOPTS_VALUE) {
cd991462 180 return write_to_fifo(s, buf);
9f26421b
AK
181 }
182
183 /* when we have two timestamps, compute how many samples would we have
184 * to add/remove to get proper sync between data and timestamps */
185 delta = pts - s->pts - get_delay(s);
186 out_size = avresample_available(s->avr);
187
188 if (labs(delta) > s->min_delta) {
189 av_log(ctx, AV_LOG_VERBOSE, "Discontinuity - %"PRId64" samples.\n", delta);
be51e589 190 out_size = av_clipl_int32((int64_t)out_size + delta);
f297dd38
AK
191 } else {
192 if (s->resample) {
193 int comp = av_clip(delta, -s->max_comp, s->max_comp);
194 av_log(ctx, AV_LOG_VERBOSE, "Compensating %d samples per second.\n", comp);
195 avresample_set_compensation(s->avr, delta, inlink->sample_rate);
196 }
197 delta = 0;
9f26421b
AK
198 }
199
200 if (out_size > 0) {
201 AVFilterBufferRef *buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
202 out_size);
cd991462
AK
203 if (!buf_out) {
204 ret = AVERROR(ENOMEM);
205 goto fail;
206 }
9f26421b
AK
207
208 avresample_read(s->avr, (void**)buf_out->extended_data, out_size);
209 buf_out->pts = s->pts;
210
211 if (delta > 0) {
212 av_samples_set_silence(buf_out->extended_data, out_size - delta,
213 delta, nb_channels, buf->format);
214 }
cd991462
AK
215 ret = ff_filter_samples(outlink, buf_out);
216 if (ret < 0)
217 goto fail;
6f834293 218 s->got_output = 1;
9f26421b
AK
219 } else {
220 av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping "
221 "whole buffer.\n");
222 }
223
224 /* drain any remaining buffered data */
225 avresample_read(s->avr, NULL, avresample_available(s->avr));
226
227 s->pts = pts - avresample_get_delay(s->avr);
cd991462
AK
228 ret = avresample_convert(s->avr, NULL, 0, 0, (void**)buf->extended_data,
229 buf->linesize[0], buf->audio->nb_samples);
230
231fail:
9f26421b 232 avfilter_unref_buffer(buf);
cd991462
AK
233
234 return ret;
9f26421b
AK
235}
236
237AVFilter avfilter_af_asyncts = {
238 .name = "asyncts",
239 .description = NULL_IF_CONFIG_SMALL("Sync audio data to timestamps"),
240
241 .init = init,
242 .uninit = uninit,
243
244 .priv_size = sizeof(ASyncContext),
245
246 .inputs = (const AVFilterPad[]) {{ .name = "default",
247 .type = AVMEDIA_TYPE_AUDIO,
248 .filter_samples = filter_samples },
249 { NULL }},
250 .outputs = (const AVFilterPad[]) {{ .name = "default",
251 .type = AVMEDIA_TYPE_AUDIO,
252 .config_props = config_props,
253 .request_frame = request_frame },
254 { NULL }},
255};