Browse code

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: Anton Khirnov <anton@khirnov.net>

Timothy B. Terriberry authored on 2014/10/14 09:46:00
Showing 1 changed files
... ...
@@ -42,6 +42,7 @@ typedef struct ResampleContext {
42 42
     AVDictionary *options;
43 43
 
44 44
     int64_t next_pts;
45
+    int64_t next_in_pts;
45 46
 
46 47
     /* set by filter_frame() to signal an output frame to request_frame() */
47 48
     int got_output;
... ...
@@ -154,6 +155,7 @@ static int config_output(AVFilterLink *outlink)
154 154
 
155 155
     outlink->time_base = (AVRational){ 1, outlink->sample_rate };
156 156
     s->next_pts        = AV_NOPTS_VALUE;
157
+    s->next_in_pts     = AV_NOPTS_VALUE;
157 158
 
158 159
     av_get_channel_layout_string(buf1, sizeof(buf1),
159 160
                                  -1, inlink ->channel_layout);
... ...
@@ -255,7 +257,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
255 255
             }
256 256
 
257 257
             out->sample_rate = outlink->sample_rate;
258
-            if (in->pts != AV_NOPTS_VALUE) {
258
+            /* Only convert in->pts if there is a discontinuous jump.
259
+               This ensures that out->pts tracks the number of samples actually
260
+               output by the resampler in the absence of such a jump.
261
+               Otherwise, the rounding in av_rescale_q() and av_rescale()
262
+               causes off-by-1 errors. */
263
+            if (in->pts != AV_NOPTS_VALUE && in->pts != s->next_in_pts) {
259 264
                 out->pts = av_rescale_q(in->pts, inlink->time_base,
260 265
                                             outlink->time_base) -
261 266
                                av_rescale(delay, outlink->sample_rate,
... ...
@@ -264,6 +271,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
264 264
                 out->pts = s->next_pts;
265 265
 
266 266
             s->next_pts = out->pts + out->nb_samples;
267
+            s->next_in_pts = in->pts + in->nb_samples;
267 268
 
268 269
             ret = ff_filter_frame(outlink, out);
269 270
             s->got_output = 1;