Commit 6cbbf059 authored by Timothy B. Terriberry's avatar Timothy B. Terriberry Committed by Anton Khirnov

resample: Avoid off-by-1 errors in PTS calcs.

The rounding used in the PTS calculations in filter_frame() does
not actually match the number of samples output by the resampler.
This leads to off-by-1 errors in the timestamps indicating gaps and
underruns, even when the input timestamps are all contiguous.

Bug-Id: 753
Signed-off-by: 's avatarAnton Khirnov <anton@khirnov.net>
parent fb238f82
...@@ -42,6 +42,7 @@ typedef struct ResampleContext { ...@@ -42,6 +42,7 @@ typedef struct ResampleContext {
AVDictionary *options; AVDictionary *options;
int64_t next_pts; int64_t next_pts;
int64_t next_in_pts;
/* set by filter_frame() to signal an output frame to request_frame() */ /* set by filter_frame() to signal an output frame to request_frame() */
int got_output; int got_output;
...@@ -154,6 +155,7 @@ static int config_output(AVFilterLink *outlink) ...@@ -154,6 +155,7 @@ static int config_output(AVFilterLink *outlink)
outlink->time_base = (AVRational){ 1, outlink->sample_rate }; outlink->time_base = (AVRational){ 1, outlink->sample_rate };
s->next_pts = AV_NOPTS_VALUE; s->next_pts = AV_NOPTS_VALUE;
s->next_in_pts = AV_NOPTS_VALUE;
av_get_channel_layout_string(buf1, sizeof(buf1), av_get_channel_layout_string(buf1, sizeof(buf1),
-1, inlink ->channel_layout); -1, inlink ->channel_layout);
...@@ -255,7 +257,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) ...@@ -255,7 +257,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
} }
out->sample_rate = outlink->sample_rate; out->sample_rate = outlink->sample_rate;
if (in->pts != AV_NOPTS_VALUE) { /* Only convert in->pts if there is a discontinuous jump.
This ensures that out->pts tracks the number of samples actually
output by the resampler in the absence of such a jump.
Otherwise, the rounding in av_rescale_q() and av_rescale()
causes off-by-1 errors. */
if (in->pts != AV_NOPTS_VALUE && in->pts != s->next_in_pts) {
out->pts = av_rescale_q(in->pts, inlink->time_base, out->pts = av_rescale_q(in->pts, inlink->time_base,
outlink->time_base) - outlink->time_base) -
av_rescale(delay, outlink->sample_rate, av_rescale(delay, outlink->sample_rate,
...@@ -264,6 +271,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) ...@@ -264,6 +271,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
out->pts = s->next_pts; out->pts = s->next_pts;
s->next_pts = out->pts + out->nb_samples; s->next_pts = out->pts + out->nb_samples;
s->next_in_pts = in->pts + in->nb_samples;
ret = ff_filter_frame(outlink, out); ret = ff_filter_frame(outlink, out);
s->got_output = 1; s->got_output = 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment