Created
December 12, 2012 17:18
-
-
Save justinruggles/4269715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 44696102f58adf000013b48e37a738b1a1d17b79 Mon Sep 17 00:00:00 2001 | |
From: Justin Ruggles <[email protected]> | |
Date: Tue, 11 Dec 2012 17:36:09 -0500 | |
Subject: [PATCH] WIP: fix the asyncts first_pts option | |
--- | |
libavfilter/af_asyncts.c | 79 +++++++++++++++++++++++++++++++++------------ | |
1 files changed, 58 insertions(+), 21 deletions(-) | |
diff --git a/libavfilter/af_asyncts.c b/libavfilter/af_asyncts.c | |
index 5d009f0..433b44a 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; | |
} | |
@@ -116,6 +120,12 @@ static int config_props(AVFilterLink *link) | |
return 0; | |
} | |
+/* get amount of data currently buffered, in samples */ | |
+static int64_t get_delay(ASyncContext *s) | |
+{ | |
+ return avresample_available(s->avr) + avresample_get_delay(s->avr); | |
+} | |
+ | |
static int request_frame(AVFilterLink *link) | |
{ | |
AVFilterContext *ctx = link->src; | |
@@ -128,7 +138,7 @@ static int request_frame(AVFilterLink *link) | |
ret = ff_request_frame(ctx->inputs[0]); | |
/* flush the fifo */ | |
- if (ret == AVERROR_EOF && (nb_samples = avresample_get_delay(s->avr))) { | |
+ if (ret == AVERROR_EOF && (nb_samples = get_delay(s))) { | |
AVFilterBufferRef *buf = ff_get_audio_buffer(link, AV_PERM_WRITE, | |
nb_samples); | |
if (!buf) | |
@@ -155,12 +165,6 @@ static int write_to_fifo(ASyncContext *s, AVFilterBufferRef *buf) | |
return ret; | |
} | |
-/* get amount of data currently buffered, in samples */ | |
-static int64_t get_delay(ASyncContext *s) | |
-{ | |
- return avresample_available(s->avr) + avresample_get_delay(s->avr); | |
-} | |
- | |
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf) | |
{ | |
AVFilterContext *ctx = inlink->dst; | |
@@ -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; | |
+ | |
+ av_samples_set_silence(buf_out->extended_data, 0, delta, | |
+ nb_channels, buf->format); | |
- if (delta > 0) { | |
- av_samples_set_silence(buf_out->extended_data, out_size - delta, | |
- 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment