Currently it will do padding, but it does not properly handle
start-of-stream trimming as documented.
---
libavfilter/af_asyncts.c | 65 ++++++++++++++++++++++++++++++++++++----------
1 files changed, 51 insertions(+), 14 deletions(-)
diff --git a/libavfilter/af_asyncts.c b/libavfilter/af_asyncts.c
index 087692e..43f9e5d 100644
--- a/libavfilter/af_asyncts.c
+++ b/libavfilter/af_asyncts.c
@@ -33,6 +33,8 @@ typedef struct ASyncContext {
AVAudioResampleContext *avr;
int64_t pts; ///< timestamp in samples of the first sample in
fifo
int min_delta; ///< pad/trim min threshold in samples
+ int first_frame; ///< 1 until filter_frame() has processed at least
1 frame with a pts != AV_NOPTS_VALUE
+ int drop_samples; ///< number of samples to drop from the start of
the input
/* options */
int resample;
@@ -75,6 +77,8 @@ static int init(AVFilterContext *ctx, const char *args)
}
av_opt_free(s);
+ s->first_frame = 1;
+
return 0;
}
@@ -173,12 +177,8 @@ static int filter_frame(AVFilterLink *inlink,
AVFilterBufferRef *buf)
int64_t delta;
/* buffer data until we get the first timestamp */
- if (s->pts == AV_NOPTS_VALUE) {
- if (pts != AV_NOPTS_VALUE) {
- s->pts = pts - get_delay(s);
- }
- return write_to_fifo(s, buf);
- }
+ if (s->pts == AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE)
+ s->pts = pts - get_delay(s);
/* now wait for the next timestamp */
if (pts == AV_NOPTS_VALUE) {
@@ -187,10 +187,21 @@ static int filter_frame(AVFilterLink *inlink,
AVFilterBufferRef *buf)
/* when we have two timestamps, compute how many samples would we have
* to add/remove to get proper sync between data and timestamps */
- delta = pts - s->pts - get_delay(s);
+ delta = pts - s->pts - get_delay(s);
+ if (s->first_frame && delta < 0) {
+ int buffered_samples = avresample_available(s->avr);
+ s->drop_samples = -delta;
+ if (buffered_samples) {
+ int drain = FFMIN(s->drop_samples, buffered_samples);
+ avresample_read(s->avr, NULL, drain);
+ s->drop_samples -= drain;
+ delta += drain;
+ av_log(ctx, AV_LOG_VERBOSE, "Trimmed %d samples from start\n",
drain);
+ }
+ }
out_size = avresample_available(s->avr);
- if (labs(delta) > s->min_delta) {
+ if (labs(delta) > s->min_delta || (s->first_frame && delta)) {
av_log(ctx, AV_LOG_VERBOSE, "Discontinuity - %"PRId64" samples.\n",
delta);
out_size = av_clipl_int32((int64_t)out_size + delta);
} else {
@@ -210,18 +221,35 @@ static int filter_frame(AVFilterLink *inlink,
AVFilterBufferRef *buf)
goto fail;
}
- avresample_read(s->avr, buf_out->extended_data, out_size);
- buf_out->pts = s->pts;
+ if (s->first_frame && delta > 0) {
+ int ch;
+ uint8_t **data0;
- if (delta > 0) {
- av_samples_set_silence(buf_out->extended_data, out_size - delta,
- delta, nb_channels, buf->format);
+ av_samples_set_silence(buf_out->extended_data, 0, delta,
+ nb_channels, buf->format);
+
+ data0 = av_mallocz(nb_channels * sizeof(*data0));
+ if (!data0)
+ return AVERROR(ENOMEM);
+ for (ch = 0; ch < nb_channels; ch++)
+ data0[ch] = buf_out->extended_data[ch] + delta;
+ avresample_read(s->avr, data0, out_size);
+
+ free(data0);
+ } else {
+ avresample_read(s->avr, buf_out->extended_data, out_size);
+
+ if (delta > 0) {
+ av_samples_set_silence(buf_out->extended_data, out_size -
delta,
+ delta, nb_channels, buf->format);
+ }
}
+ buf_out->pts = s->pts;
ret = ff_filter_frame(outlink, buf_out);
if (ret < 0)
goto fail;
s->got_output = 1;
- } else {
+ } else if (avresample_available(s->avr)) {
av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping "
"whole buffer.\n");
}
@@ -233,6 +261,15 @@ static int filter_frame(AVFilterLink *inlink,
AVFilterBufferRef *buf)
ret = avresample_convert(s->avr, NULL, 0, 0, buf->extended_data,
buf->linesize[0], buf->audio->nb_samples);
+ if (s->drop_samples > 0) {
+ int drain = FFMIN(s->drop_samples, avresample_available(s->avr));
+ avresample_read(s->avr, NULL, drain);
+ s->drop_samples -= drain;
+ s->pts += drain;
+ av_log(ctx, AV_LOG_VERBOSE, "Trimmed %d samples from start\n", drain);
+ }
+
+ s->first_frame = 0;
fail:
avfilter_unref_buffer(buf);
--
1.7.1
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel