Skip to content

Commit

Permalink
Compensate slope on start
Browse files Browse the repository at this point in the history
The first frames are typically received and decoded with more delay than
the others, causing a wrong slope estimation on start.

To compensate, assume an initial slope of 1, then progressively use the
estimated slope.
  • Loading branch information
rom1v committed Jul 13, 2021
1 parent 19eb080 commit 1d2c85b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app/src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static void
sc_clock_estimate(struct sc_clock *clock,
double *out_slope, sc_tick *out_offset) {
assert(clock->count > 1); // two points are necessary

struct sc_clock_point left_avg = {
.system = clock->left_sum.system / (clock->count / 2),
.stream = clock->left_sum.stream / (clock->count / 2),
Expand All @@ -27,15 +28,26 @@ sc_clock_estimate(struct sc_clock *clock,
.system = clock->right_sum.system / ((clock->count + 1) / 2),
.stream = clock->right_sum.stream / ((clock->count + 1) / 2),
};

double slope = (double) (right_avg.system - left_avg.system)
/ (right_avg.stream - left_avg.stream);

if (clock->count < SC_CLOCK_RANGE) {
/* The first frames are typically received and decoded with more delay
* than the others, causing a wrong slope estimation on start. To
* compensate, assume an initial slope of 1, then progressively use the
* estimated slope. */
slope = (clock->count * slope + (SC_CLOCK_RANGE - clock->count))
/ SC_CLOCK_RANGE;
}

struct sc_clock_point global_avg = {
.system = (clock->left_sum.system + clock->right_sum.system)
/ clock->count,
.stream = (clock->left_sum.stream + clock->right_sum.stream)
/ clock->count,
};

double slope = (double) (right_avg.system - left_avg.system)
/ (right_avg.stream - left_avg.stream);
sc_tick offset = global_avg.system - (sc_tick) (global_avg.stream * slope);

*out_slope = slope;
Expand Down

0 comments on commit 1d2c85b

Please sign in to comment.