Mailing List Archive

AVSync2 Refinements
First, I want to caveat this with the declaration that I am not a
programmer however I do deal with process control as part of my day job as
an engineer.

I have been running the render branch (now master) for a while and using
AVSync2 for audio/video synchronization mostly with LiveTV. In evaluating,
I have found the sync algorithm would only provides acceptably stable video
performance if I use an AVSync2 timing setting of 1ms. Anything more than
this results in jerky video performance along with frequent frame drops on
my systems.

Investigating by spending some time studying the AVSync2 code and looking
in more detail at playback,timestamp logs, I have come to the conclusion
that there might be some potential for refinement of the AVSync2
algorithm.

The current algorithm is much like a proportional control approach
with fixed controller gain of 1 (with each adjustment, try to adjust the
time sync exactly back to equilibrium) with clamping of the maximum
fix_amount limited to the user defined setting of avsync2adjustms. I think
the problem with this approach is that a controller gain of 1 seems to be
too high and there is also occasionally some noise in the calculated sync
measurement. Both of these issues destabilize the AV sync control. With a
larger avsync2adjustms setting, the system never achieves stable control
under many conditions with the sync oscillating over / under target. In
turn, the calculated "lateness" value tends to also swing around causing
more frequent frame drops further upsetting the control.

In an attempt to improve, I manually modified the AVSync2 algorithm as
follows:
1. A control gain value is declared and included in the fix_amount
calculation to allow for a code configured gain value less than 1. In my
case, I am currently using a value of 0.4.
2. A first order exponential smoothing filter is applied to dampen any
noise in the sync measurement. A code configured filter coefficient is set
to a value between 0 and 1. In my case, I am using 0.9.

Note that the 0.4 and 0.9 are the result of some limited eyeball tuning and
are probably not optimum values.

After implementing these changes, my frontends seem to behave MUCH better
in both achieving sync quickly and achieving more stable long term control
of the av sync along with visually improved video smoothness. The modified
approach allows the applied fix_amount to be larger initially when there is
a big sync offset but avoids excessive overshoot / instability /
overcontrol as the sync approaches equilibrium.

I throw this out there to share and to see if others that have looked at
the AVSynce2 code have any thoughts on the approach at least.

In this scenario, the avsync2adjustms is no longer needed. However, if
wanting to have user configurable/tuneable settings, the gain value (
range: 0-1) and the filter coefficient (range: 0-1) could be exposed in the
frontend setup menu under AVSync2 rather than hard-coded.

Excerpted code with modifications shown in mythplayer.cpp AVSync2 function:
.....
auto playspeed1000 = static_cast<int64_t>(1000.0F / play_speed);
bool reset = false;
// controller gain
float av_control_gain = 0.4;
// variable to carry forward prior adjustment for smoothing filter
calculation
float last_fix = 0;
// filter coefficient for the smoothing filter.
float sync_fc = 0.9;
.......
int sign = audio_adjustment < 0 ? -1 : 1;
// Use proportional control with configured gain & add an
exponential smoothing filter.
float fix_amount = (last_fix * sync_fc + (1 - sync_fc) *
audio_adjustment) * sign * av_control_gain;
last_fix = fix_amount;

//disable clamping of adjustment outputs
//if (fix_amount > avsync2adjustms)
// fix_amount = avsync2adjustms;
// Faster catch-up when off by more than 200 ms
//if (audio_adjustment * sign > 200)
// fix the sync within 15 - 20 frames
//fix_amount = audio_adjustment * sign / 15;
auto speedup1000 = static_cast<int64_t>(1000 * play_speed);
rtcbase -= static_cast<int64_t>(1000000 * fix_amount * sign /
speedup1000);
......

-Tim
Re: AVSync2 Refinements [ In reply to ]
On Sat, Dec 07, 2019 at 01:23:46PM -0500, Tim Pletcher wrote:
> First, I want to caveat this with the declaration that I am not a
> programmer however I do deal with process control as part of my day job as
> an engineer.
>
> I have been running the render branch (now master) for a while and using
> AVSync2 for audio/video synchronization mostly with LiveTV. In evaluating,
> I have found the sync algorithm would only provides acceptably stable video
> performance if I use an AVSync2 timing setting of 1ms. Anything more than
> this results in jerky video performance along with frequent frame drops on
> my systems.
>
> Investigating by spending some time studying the AVSync2 code and looking
> in more detail at playback,timestamp logs, I have come to the conclusion
> that there might be some potential for refinement of the AVSync2
> algorithm.
>
> The current algorithm is much like a proportional control approach
> with fixed controller gain of 1 (with each adjustment, try to adjust the
> time sync exactly back to equilibrium) with clamping of the maximum
> fix_amount limited to the user defined setting of avsync2adjustms. I think
> the problem with this approach is that a controller gain of 1 seems to be
> too high and there is also occasionally some noise in the calculated sync
> measurement. Both of these issues destabilize the AV sync control. With a
> larger avsync2adjustms setting, the system never achieves stable control
> under many conditions with the sync oscillating over / under target. In
> turn, the calculated "lateness" value tends to also swing around causing
> more frequent frame drops further upsetting the control.
>
> In an attempt to improve, I manually modified the AVSync2 algorithm as
> follows:
> 1. A control gain value is declared and included in the fix_amount
> calculation to allow for a code configured gain value less than 1. In my
> case, I am currently using a value of 0.4.
> 2. A first order exponential smoothing filter is applied to dampen any
> noise in the sync measurement. A code configured filter coefficient is set
> to a value between 0 and 1. In my case, I am using 0.9.
>
> Note that the 0.4 and 0.9 are the result of some limited eyeball tuning and
> are probably not optimum values.
>
> After implementing these changes, my frontends seem to behave MUCH better
> in both achieving sync quickly and achieving more stable long term control
> of the av sync along with visually improved video smoothness. The modified
> approach allows the applied fix_amount to be larger initially when there is
> a big sync offset but avoids excessive overshoot / instability /
> overcontrol as the sync approaches equilibrium.
>
> I throw this out there to share and to see if others that have looked at
> the AVSynce2 code have any thoughts on the approach at least.
>
> In this scenario, the avsync2adjustms is no longer needed. However, if
> wanting to have user configurable/tuneable settings, the gain value (
> range: 0-1) and the filter coefficient (range: 0-1) could be exposed in the
> frontend setup menu under AVSync2 rather than hard-coded.

Hi Tim,

Your changes sound promising. Perer Bennett wrote the AVSync2 code.
It was a big improvement over what we had before but it still has some
known issue that we could never make better. I'm going to try to give
your changes a whirl tonight or tomorrow.

> Excerpted code with modifications shown in mythplayer.cpp AVSync2 function:

I think I see how to apply your changes. However, can you please
create a standard, unidiff patch for them. Than, please open a new
ticket at https://code.mythtv.org/trac/ and attach your patch.
Thanks.

David

> .....
> auto playspeed1000 = static_cast<int64_t>(1000.0F / play_speed);
> bool reset = false;
> // controller gain
> float av_control_gain = 0.4;
> // variable to carry forward prior adjustment for smoothing filter
> calculation
> float last_fix = 0;
> // filter coefficient for the smoothing filter.
> float sync_fc = 0.9;
> .......
> int sign = audio_adjustment < 0 ? -1 : 1;
> // Use proportional control with configured gain & add an
> exponential smoothing filter.
> float fix_amount = (last_fix * sync_fc + (1 - sync_fc) *
> audio_adjustment) * sign * av_control_gain;
> last_fix = fix_amount;
>
> //disable clamping of adjustment outputs
> //if (fix_amount > avsync2adjustms)
> // fix_amount = avsync2adjustms;
> // Faster catch-up when off by more than 200 ms
> //if (audio_adjustment * sign > 200)
> // fix the sync within 15 - 20 frames
> //fix_amount = audio_adjustment * sign / 15;
> auto speedup1000 = static_cast<int64_t>(1000 * play_speed);
> rtcbase -= static_cast<int64_t>(1000000 * fix_amount * sign /
> speedup1000);
> ......
>
> -Tim

> _______________________________________________
> mythtv-dev mailing list
> mythtv-dev@mythtv.org
> http://lists.mythtv.org/mailman/listinfo/mythtv-dev
> http://wiki.mythtv.org/Mailing_List_etiquette
> MythTV Forums: https://forum.mythtv.org


--
David Engel
david@istwok.net
_______________________________________________
mythtv-dev mailing list
mythtv-dev@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-dev
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: AVSync2 Refinements [ In reply to ]
On 12/7/19 1:23 PM, Tim Pletcher wrote:
> First, I want to caveat this with the declaration that I am not a
> programmer however I do deal with process control as part of my day
> job as an engineer.
>
> I have been running the render branch (now master) for a while and
> using AVSync2 for audio/video synchronization mostly with LiveTV. In
> evaluating, I have found the sync algorithm would only provides
> acceptably stable video performance if I use an AVSync2 timing setting
> of 1ms. Anything more than this results in jerky video performance
> along with frequent frame drops on my systems.
>
> Investigating by spending some time studying the AVSync2 code and
> looking in more detail at playback,timestamp logs, I have come to the
> conclusion that there might be some potential for refinement of the
> AVSync2 algorithm.
>
> The current algorithm is much like a proportional control approach
> with fixed controller gain of 1 (with each adjustment, try to adjust
> the time sync exactly back to equilibrium) with clamping of the
> maximum fix_amount limited to the user defined setting of
> avsync2adjustms. I think the problem with this approach is that a
> controller gain of 1 seems to be too high and there is also
> occasionally some noise in the calculated sync measurement. Both of
> these issues destabilize the AV sync control. With a larger
> avsync2adjustms setting, the system never achieves stable control
> under many conditions with the sync oscillating over / under target.
> In turn, the calculated "lateness" value tends to also swing around
> causing more frequent frame drops further upsetting the control.
>
> In an attempt to improve, I manually modified the AVSync2 algorithm as
> follows:
> 1. A control gain value is declared and included in the fix_amount
> calculation to allow for a code configured gain value less than 1.  In
> my case, I am currently using a value of 0.4.
> 2. A first order exponential smoothing filter is applied to dampen any
> noise in the sync measurement. A code configured filter coefficient is
> set to a value between 0 and 1. In my case, I am using 0.9.
>
> Note that the 0.4 and 0.9 are the result of some limited eyeball
> tuning and are probably not optimum values.
>
> After implementing these changes, my frontends seem to behave MUCH
> better in both achieving sync quickly and achieving more stable long
> term control of the av sync along with visually improved video
> smoothness. The modified approach allows the applied fix_amount to be
> larger initially when there is a big sync offset but avoids excessive
> overshoot / instability / overcontrol as the sync approaches equilibrium.
>
> I throw this out there to share and to see if others that have looked
> at the AVSynce2 code have any thoughts on the approach at least.
>
> In this scenario, the avsync2adjustms is no longer needed. However, if
> wanting to have user configurable/tuneable settings, the gain value (
> range: 0-1) and the filter coefficient (range: 0-1) could be exposed
> in the frontend setup menu under AVSync2 rather than hard-coded.
>
> Excerpted code with modifications shown in mythplayer.cpp AVSync2
> function:
> .....
>     auto playspeed1000 = static_cast<int64_t>(1000.0F / play_speed);
>     bool reset = false;
>     // controller gain
>     float av_control_gain = 0.4;
>     // variable to carry forward prior adjustment for smoothing filter
> calculation
>     float last_fix = 0;
>     // filter coefficient for the smoothing filter.
>     float sync_fc = 0.9;
> .......
>             int sign = audio_adjustment < 0 ? -1 : 1;
>             // Use proportional control with configured gain & add an
> exponential smoothing filter.
>             float fix_amount = (last_fix * sync_fc + (1 - sync_fc) *
> audio_adjustment) * sign * av_control_gain;
>             last_fix = fix_amount;
>             //disable clamping of adjustment outputs
>             //if (fix_amount > avsync2adjustms)
>             //    fix_amount = avsync2adjustms;
>             // Faster catch-up when off by more than 200 ms
>             //if (audio_adjustment * sign > 200)
>                 // fix the sync within 15 - 20 frames
>                 //fix_amount = audio_adjustment * sign / 15;
>             auto speedup1000 = static_cast<int64_t>(1000 * play_speed);
>             rtcbase -= static_cast<int64_t>(1000000 * fix_amount *
> sign / speedup1000);
> ......
>
> -Tim
>
>
The audio time is taken from the audio sent to the sound card and
adjusted for the amount of audio in the sound card buffer, as supplied
by the operating system. Depending on the audio setup, this can be
inaccurate and the figure obtained can jump around the correct value. I
suspect that your playback problems may be caused by this, resulting in
video playback slowing down and speeding up inconsistently. When this is
the case, setting the audio adjustment to the smallest value is the best
solution.

I have not yet taken a detailed look at your suggested change. Perhaps,
as you say, smaller values should be allowed, possibly they could be
specified in microseconds or multiples of 100 microseconds.

Peter
Re: AVSync2 Refinements [ In reply to ]
On Sun, Dec 8, 2019 at 11:59 AM Peter Bennett <pb.mythtv@gmail.com> wrote:

>
>
> On 12/7/19 1:23 PM, Tim Pletcher wrote:
>
> First, I want to caveat this with the declaration that I am not a
> programmer however I do deal with process control as part of my day job as
> an engineer.
>
> I have been running the render branch (now master) for a while and using
> AVSync2 for audio/video synchronization mostly with LiveTV. In evaluating,
> I have found the sync algorithm would only provides acceptably stable video
> performance if I use an AVSync2 timing setting of 1ms. Anything more than
> this results in jerky video performance along with frequent frame drops on
> my systems.
>
> Investigating by spending some time studying the AVSync2 code and looking
> in more detail at playback,timestamp logs, I have come to the conclusion
> that there might be some potential for refinement of the AVSync2
> algorithm.
>
> The current algorithm is much like a proportional control approach
> with fixed controller gain of 1 (with each adjustment, try to adjust the
> time sync exactly back to equilibrium) with clamping of the maximum
> fix_amount limited to the user defined setting of avsync2adjustms. I think
> the problem with this approach is that a controller gain of 1 seems to be
> too high and there is also occasionally some noise in the calculated sync
> measurement. Both of these issues destabilize the AV sync control. With a
> larger avsync2adjustms setting, the system never achieves stable control
> under many conditions with the sync oscillating over / under target. In
> turn, the calculated "lateness" value tends to also swing around causing
> more frequent frame drops further upsetting the control.
>
> In an attempt to improve, I manually modified the AVSync2 algorithm as
> follows:
> 1. A control gain value is declared and included in the fix_amount
> calculation to allow for a code configured gain value less than 1. In my
> case, I am currently using a value of 0.4.
> 2. A first order exponential smoothing filter is applied to dampen any
> noise in the sync measurement. A code configured filter coefficient is set
> to a value between 0 and 1. In my case, I am using 0.9.
>
> Note that the 0.4 and 0.9 are the result of some limited eyeball tuning
> and are probably not optimum values.
>
> After implementing these changes, my frontends seem to behave MUCH better
> in both achieving sync quickly and achieving more stable long term control
> of the av sync along with visually improved video smoothness. The modified
> approach allows the applied fix_amount to be larger initially when there is
> a big sync offset but avoids excessive overshoot / instability /
> overcontrol as the sync approaches equilibrium.
>
> I throw this out there to share and to see if others that have looked at
> the AVSynce2 code have any thoughts on the approach at least.
>
> In this scenario, the avsync2adjustms is no longer needed. However, if
> wanting to have user configurable/tuneable settings, the gain value (
> range: 0-1) and the filter coefficient (range: 0-1) could be exposed in the
> frontend setup menu under AVSync2 rather than hard-coded.
>
> Excerpted code with modifications shown in mythplayer.cpp AVSync2 function:
> .....
> auto playspeed1000 = static_cast<int64_t>(1000.0F / play_speed);
> bool reset = false;
> // controller gain
> float av_control_gain = 0.4;
> // variable to carry forward prior adjustment for smoothing filter
> calculation
> float last_fix = 0;
> // filter coefficient for the smoothing filter.
> float sync_fc = 0.9;
> .......
> int sign = audio_adjustment < 0 ? -1 : 1;
> // Use proportional control with configured gain & add an
> exponential smoothing filter.
> float fix_amount = (last_fix * sync_fc + (1 - sync_fc) *
> audio_adjustment) * sign * av_control_gain;
> last_fix = fix_amount;
>
> //disable clamping of adjustment outputs
> //if (fix_amount > avsync2adjustms)
> // fix_amount = avsync2adjustms;
> // Faster catch-up when off by more than 200 ms
> //if (audio_adjustment * sign > 200)
> // fix the sync within 15 - 20 frames
> //fix_amount = audio_adjustment * sign / 15;
> auto speedup1000 = static_cast<int64_t>(1000 * play_speed);
> rtcbase -= static_cast<int64_t>(1000000 * fix_amount * sign /
> speedup1000);
> ......
>
> -Tim
>
>
> The audio time is taken from the audio sent to the sound card and adjusted
> for the amount of audio in the sound card buffer, as supplied by the
> operating system. Depending on the audio setup, this can be inaccurate and
> the figure obtained can jump around the correct value. I suspect that your
> playback problems may be caused by this, resulting in video playback
> slowing down and speeding up inconsistently. When this is the case, setting
> the audio adjustment to the smallest value is the best solution.
>
> I have not yet taken a detailed look at your suggested change. Perhaps, as
> you say, smaller values should be allowed, possibly they could be specified
> in microseconds or multiples of 100 microseconds.
>

Thank you for your insights Peter. I have been testing and debating
whether to actually include any filtering or not. I do think true
proportional control with gain settings less than 1 is better as this
minimizes the control oscillation once near setpoint while still giving
somewhat faster initial sync. I do notice a significant difference in the
amount of variability from my two intel apollo lake based front-ends using
VAAPI versus my combined i5 frontend/backend using an NVidia GT1030 with
NVDEC so I think the proposed change is more impactful on low end hardware.

As David suggests, I will create a ticket and submit a patch.

Tim
Re: AVSync2 Refinements [ In reply to ]
On Mon, 9 Dec 2019 at 00:32, Tim Pletcher <pletchtd@gmail.com> wrote:

> Thank you for your insights Peter. I have been testing and debating whether to actually include any filtering or not. I do think true proportional control with gain settings less than 1 is better as this minimizes the control oscillation once near setpoint while still giving somewhat faster initial sync. I do notice a significant difference in the amount of variability from my two intel apollo lake based front-ends using VAAPI versus my combined i5 frontend/backend using an NVidia GT1030 with NVDEC so I think the proposed change is more impactful on low end hardware.
>
> As David suggests, I will create a ticket and submit a patch.

Tim

Many thanks for this - very handy:)

I recently got caught out by the fluctuating av sync on the raspberry
pi. I've taken the liberty of producing a more extensive patch
(attached). This adds your filtering as well as removing all of the
the old a/v sync code and the settings for AVSync2 - i.e. it moves
over completely to the new av sync code and removes the audio
correction adjustment. (N.B. the patch is against today's latest
master - I pushed some cleanup of MythDVDPlayer and the patch won't
apply cleanly before that).

It appears to work very well. On two (linux) desktop dev machines and
a pi 4 I can't see any issues - playback is nice and smooth. As you
say, initial sync seems to be much better as well.

Certainly gets my seal of approval!

One question - last_fix - should that be carried over between calls to
AVSync()? At the moment it is a local that gets reset between each
call - which I'm not sure is the intended behaviour.

Thanks again and regards
Mark
Re: AVSync2 Refinements [ In reply to ]
Hi Mark,

Thank you for looking at this in detail.

You are exactly right in that last_fix doesn't get carried over between
calls. The last_fix value is actually needed to correctly do the signal
filtering to the control loop. Over the weekend, I eventually figured out
the filtering wasn't actually working after adding more debugging detail to
the logging for better visibility of what was happening mathematically
between calls. This is also the reason I arrived at the conclusion that
filtering is probably not really needed since performance is good anyway
and a filter brings the drawback of adding lag to the control response.

In reality, with the broken filter application, the code was effectively
using a control gain of (0.4) * (1 - 0.9) = (0.04). This setting seems to
still provide rapid initial sync while preventing over-control in response
to natural audio timestamp jitter.

The patch looks great. Based on discussion above, I think we should remove
the filtering and set:

s_av_control_gain = 04;

The last_fix and s_sync_fc references should go away alltogther and the new
control calculation becomes:

float fix_amount = audio_adjustment * sign * s_av_control_gain;

Should I still create a Trac ticket for this?

Tim


On Mon, Dec 9, 2019 at 9:06 AM Mark Kendall <mark.kendall@gmail.com> wrote:

> On Mon, 9 Dec 2019 at 00:32, Tim Pletcher <pletchtd@gmail.com> wrote:
>
> > Thank you for your insights Peter. I have been testing and debating
> whether to actually include any filtering or not. I do think true
> proportional control with gain settings less than 1 is better as this
> minimizes the control oscillation once near setpoint while still giving
> somewhat faster initial sync. I do notice a significant difference in the
> amount of variability from my two intel apollo lake based front-ends using
> VAAPI versus my combined i5 frontend/backend using an NVidia GT1030 with
> NVDEC so I think the proposed change is more impactful on low end hardware.
> >
> > As David suggests, I will create a ticket and submit a patch.
>
> Tim
>
> Many thanks for this - very handy:)
>
> I recently got caught out by the fluctuating av sync on the raspberry
> pi. I've taken the liberty of producing a more extensive patch
> (attached). This adds your filtering as well as removing all of the
> the old a/v sync code and the settings for AVSync2 - i.e. it moves
> over completely to the new av sync code and removes the audio
> correction adjustment. (N.B. the patch is against today's latest
> master - I pushed some cleanup of MythDVDPlayer and the patch won't
> apply cleanly before that).
>
> It appears to work very well. On two (linux) desktop dev machines and
> a pi 4 I can't see any issues - playback is nice and smooth. As you
> say, initial sync seems to be much better as well.
>
> Certainly gets my seal of approval!
>
> One question - last_fix - should that be carried over between calls to
> AVSync()? At the moment it is a local that gets reset between each
> call - which I'm not sure is the intended behaviour.
>
> Thanks again and regards
> Mark
> _______________________________________________
> mythtv-dev mailing list
> mythtv-dev@mythtv.org
> http://lists.mythtv.org/mailman/listinfo/mythtv-dev
> http://wiki.mythtv.org/Mailing_List_etiquette
> MythTV Forums: https://forum.mythtv.org
>
Re: AVSync2 Refinements [ In reply to ]
On Mon, Dec 9, 2019 at 9:06 AM Mark Kendall <mark.kendall@gmail.com> wrote:

> On Mon, 9 Dec 2019 at 00:32, Tim Pletcher <pletchtd@gmail.com> wrote:
>
> > Thank you for your insights Peter. I have been testing and debating
> whether to actually include any filtering or not. I do think true
> proportional control with gain settings less than 1 is better as this
> minimizes the control oscillation once near setpoint while still giving
> somewhat faster initial sync. I do notice a significant difference in the
> amount of variability from my two intel apollo lake based front-ends using
> VAAPI versus my combined i5 frontend/backend using an NVidia GT1030 with
> NVDEC so I think the proposed change is more impactful on low end hardware.
> >
> > As David suggests, I will create a ticket and submit a patch.
>
> Tim
>
> Many thanks for this - very handy:)
>
> I recently got caught out by the fluctuating av sync on the raspberry
> pi. I've taken the liberty of producing a more extensive patch
> (attached). This adds your filtering as well as removing all of the
> the old a/v sync code and the settings for AVSync2 - i.e. it moves
> over completely to the new av sync code and removes the audio
> correction adjustment. (N.B. the patch is against today's latest
> master - I pushed some cleanup of MythDVDPlayer and the patch won't
> apply cleanly before that).
>
> It appears to work very well. On two (linux) desktop dev machines and
> a pi 4 I can't see any issues - playback is nice and smooth. As you
> say, initial sync seems to be much better as well.
>
> Certainly gets my seal of approval!
>
> One question - last_fix - should that be carried over between calls to
> AVSync()? At the moment it is a local that gets reset between each
> call - which I'm not sure is the intended behaviour.
>
> Thanks again and regards
> Mark
>
>
Hi Mark,

Thank you for looking at this in detail.

You are exactly right in that last_fix doesn't get carried over between
calls. The last_fix value is actually needed to correctly do the signal
filtering to the control loop. Over the weekend, I eventually figured out
the filtering wasn't actually working after adding more debugging detail to
the logging for better visibility of what was happening mathematically
between calls. This is also the reason I arrived at the conclusion that
filtering is probably not really needed since performance is good anyway
and a filter brings the drawback of adding lag to the control response.

In reality, with the broken filter application, the code was effectively
using a control gain of (0.4) * (1 - 0.9) = (0.04). This setting seems to
still provide rapid initial sync while preventing over-control in response
to natural audio timestamp jitter.

The patch looks great. Based on discussion above, I think we should remove
the filtering and set:

s_av_control_gain = 04;

The last_fix and s_sync_fc references should go away alltogther and the new
control calculation becomes:

float fix_amount = audio_adjustment * sign * s_av_control_gain;

Should I still create a Trac ticket for this?
Re: AVSync2 Refinements [ In reply to ]
On 12/9/19 9:03 AM, Mark Kendall wrote:
> Many thanks for this - very handy:)
>
> I recently got caught out by the fluctuating av sync on the raspberry
> pi. I've taken the liberty of producing a more extensive patch
> (attached). This adds your filtering as well as removing all of the
> the old a/v sync code and the settings for AVSync2 - i.e. it moves
> over completely to the new av sync code and removes the audio
> correction adjustment. (N.B. the patch is against today's latest
> master - I pushed some cleanup of MythDVDPlayer and the patch won't
> apply cleanly before that).
>
> It appears to work very well. On two (linux) desktop dev machines and
> a pi 4 I can't see any issues - playback is nice and smooth. As you
> say, initial sync seems to be much better as well.
>
> Certainly gets my seal of approval!
>
> One question - last_fix - should that be carried over between calls to
> AVSync()? At the moment it is a local that gets reset between each
> call - which I'm not sure is the intended behaviour.
>
> Thanks again and regards
> Mark

Hi Mark

I have been wondering for a while whether to remove the old avsync and
with it the setup option for avsync2.  I am always rather cautious about
making a change that affects everybody. Since I have not seen any
complaints about avsync2 and there have been positive comments I would
say go for it.

Peter
Re: AVSync2 Refinements [ In reply to ]
On Mon, Dec 9, 2019 at 10:11 AM Tim Pletcher <pletchtd@gmail.com> wrote:

>
>
> On Mon, Dec 9, 2019 at 9:06 AM Mark Kendall <mark.kendall@gmail.com>
> wrote:
>
>> On Mon, 9 Dec 2019 at 00:32, Tim Pletcher <pletchtd@gmail.com> wrote:
>>
>> > Thank you for your insights Peter. I have been testing and debating
>> whether to actually include any filtering or not. I do think true
>> proportional control with gain settings less than 1 is better as this
>> minimizes the control oscillation once near setpoint while still giving
>> somewhat faster initial sync. I do notice a significant difference in the
>> amount of variability from my two intel apollo lake based front-ends using
>> VAAPI versus my combined i5 frontend/backend using an NVidia GT1030 with
>> NVDEC so I think the proposed change is more impactful on low end hardware.
>> >
>> > As David suggests, I will create a ticket and submit a patch.
>>
>> Tim
>>
>> Many thanks for this - very handy:)
>>
>> I recently got caught out by the fluctuating av sync on the raspberry
>> pi. I've taken the liberty of producing a more extensive patch
>> (attached). This adds your filtering as well as removing all of the
>> the old a/v sync code and the settings for AVSync2 - i.e. it moves
>> over completely to the new av sync code and removes the audio
>> correction adjustment. (N.B. the patch is against today's latest
>> master - I pushed some cleanup of MythDVDPlayer and the patch won't
>> apply cleanly before that).
>>
>> It appears to work very well. On two (linux) desktop dev machines and
>> a pi 4 I can't see any issues - playback is nice and smooth. As you
>> say, initial sync seems to be much better as well.
>>
>> Certainly gets my seal of approval!
>>
>> One question - last_fix - should that be carried over between calls to
>> AVSync()? At the moment it is a local that gets reset between each
>> call - which I'm not sure is the intended behaviour.
>>
>> Thanks again and regards
>> Mark
>>
>>
> Hi Mark,
>
> Thank you for looking at this in detail.
>
> You are exactly right in that last_fix doesn't get carried over between
> calls. The last_fix value is actually needed to correctly do the signal
> filtering to the control loop. Over the weekend, I eventually figured out
> the filtering wasn't actually working after adding more debugging detail to
> the logging for better visibility of what was happening mathematically
> between calls. This is also the reason I arrived at the conclusion that
> filtering is probably not really needed since performance is good anyway
> and a filter brings the drawback of adding lag to the control response.
>
> In reality, with the broken filter application, the code was effectively
> using a control gain of (0.4) * (1 - 0.9) = (0.04). This setting seems to
> still provide rapid initial sync while preventing over-control in response
> to natural audio timestamp jitter.
>
> The patch looks great. Based on discussion above, I think we should
> remove the filtering and set:
>
> s_av_control_gain = 04;
>
> The last_fix and s_sync_fc references should go away alltogther and the
> new control calculation becomes:
>
> float fix_amount = audio_adjustment * sign * s_av_control_gain;
>
> Should I still create a Trac ticket for this?
>

Correction:

It should be:
s_av_control_gain = 0.04;

Tim
Re: AVSync2 Refinements [ In reply to ]
On Mon, Dec 9, 2019 at 10:37 AM Tim Pletcher <pletchtd@gmail.com> wrote:

>
>
> On Mon, Dec 9, 2019 at 10:11 AM Tim Pletcher <pletchtd@gmail.com> wrote:
>
>>
>>
>> On Mon, Dec 9, 2019 at 9:06 AM Mark Kendall <mark.kendall@gmail.com>
>> wrote:
>>
>>> On Mon, 9 Dec 2019 at 00:32, Tim Pletcher <pletchtd@gmail.com> wrote:
>>>
>>> > Thank you for your insights Peter. I have been testing and debating
>>> whether to actually include any filtering or not. I do think true
>>> proportional control with gain settings less than 1 is better as this
>>> minimizes the control oscillation once near setpoint while still giving
>>> somewhat faster initial sync. I do notice a significant difference in the
>>> amount of variability from my two intel apollo lake based front-ends using
>>> VAAPI versus my combined i5 frontend/backend using an NVidia GT1030 with
>>> NVDEC so I think the proposed change is more impactful on low end hardware.
>>> >
>>> > As David suggests, I will create a ticket and submit a patch.
>>>
>>> Tim
>>>
>>> Many thanks for this - very handy:)
>>>
>>> I recently got caught out by the fluctuating av sync on the raspberry
>>> pi. I've taken the liberty of producing a more extensive patch
>>> (attached). This adds your filtering as well as removing all of the
>>> the old a/v sync code and the settings for AVSync2 - i.e. it moves
>>> over completely to the new av sync code and removes the audio
>>> correction adjustment. (N.B. the patch is against today's latest
>>> master - I pushed some cleanup of MythDVDPlayer and the patch won't
>>> apply cleanly before that).
>>>
>>> It appears to work very well. On two (linux) desktop dev machines and
>>> a pi 4 I can't see any issues - playback is nice and smooth. As you
>>> say, initial sync seems to be much better as well.
>>>
>>> Certainly gets my seal of approval!
>>>
>>> One question - last_fix - should that be carried over between calls to
>>> AVSync()? At the moment it is a local that gets reset between each
>>> call - which I'm not sure is the intended behaviour.
>>>
>>> Thanks again and regards
>>> Mark
>>>
>>>
>> Hi Mark,
>>
>> Thank you for looking at this in detail.
>>
>> You are exactly right in that last_fix doesn't get carried over between
>> calls. The last_fix value is actually needed to correctly do the signal
>> filtering to the control loop. Over the weekend, I eventually figured out
>> the filtering wasn't actually working after adding more debugging detail to
>> the logging for better visibility of what was happening mathematically
>> between calls. This is also the reason I arrived at the conclusion that
>> filtering is probably not really needed since performance is good anyway
>> and a filter brings the drawback of adding lag to the control response.
>>
>> In reality, with the broken filter application, the code was effectively
>> using a control gain of (0.4) * (1 - 0.9) = (0.04). This setting seems to
>> still provide rapid initial sync while preventing over-control in response
>> to natural audio timestamp jitter.
>>
>> The patch looks great. Based on discussion above, I think we should
>> remove the filtering and set:
>>
>> s_av_control_gain = 04;
>>
>> The last_fix and s_sync_fc references should go away alltogther and the
>> new control calculation becomes:
>>
>> float fix_amount = audio_adjustment * sign * s_av_control_gain;
>>
>> Should I still create a Trac ticket for this?
>>
>
> Correction:
>
> It should be:
> s_av_control_gain = 0.04;
>
> Tim
>

I revised Mark's patch with the updates I discussed above and created
ticket #13536 <https://code.mythtv.org/trac/ticket/13536>. The updated
patch is attached there.

Everything compiled fine for me and performance looked good in the limited
testing I performed.

Tim
Re: AVSync2 Refinements [ In reply to ]
On Tue, 10 Dec 2019 at 01:05, Tim Pletcher <pletchtd@gmail.com> wrote:
>
> I revised Mark's patch with the updates I discussed above and created ticket #13536. The updated patch is attached there.
>
> Everything compiled fine for me and performance looked good in the limited testing I performed.
>

Tim

I'm curious as to whether using the first version of your patch and
retaining the last_fix value would, in theory at least, be better?

i.e. use both gain and filtering

Just about to test revised patch.

Thanks and regards
Mark
_______________________________________________
mythtv-dev mailing list
mythtv-dev@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-dev
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: AVSync2 Refinements [ In reply to ]
On Tue, Dec 10, 2019 at 8:01 AM Mark Kendall <mark.kendall@gmail.com> wrote:

> On Tue, 10 Dec 2019 at 01:05, Tim Pletcher <pletchtd@gmail.com> wrote:
> >
> > I revised Mark's patch with the updates I discussed above and created
> ticket #13536. The updated patch is attached there.
> >
> > Everything compiled fine for me and performance looked good in the
> limited testing I performed.
> >
>
> Tim
>
> I'm curious as to whether using the first version of your patch and
> retaining the last_fix value would, in theory at least, be better?
>
> i.e. use both gain and filtering
>
> Just about to test revised patch.
>
> Thanks and regards
> Mark
>
> Hi Mark,

I added the filter in with a second patch and posted on the ticket last
evening. I saw this was picked up and rolled into master just a bit ago.

If anyone is interested, I threw together a crude simulation to
theoretically demonstrate the different control responses with the various
control options (plot shown at: https://imgur.com/a/DDBxcQR). On the the
plot, the green line is the gain only implementation and the red line is
the gain + filter implementation provided in the second patch revision. The
control loop will settle in around the process mean which includes the
jitter / noise so it may have a little zero offset but this should not be
enough to be evident for the user.

I have tested with 1280x720_59.94 & 1080i_29.97 live television content
using both VAAPI and NVDEC with no issues thus far.

During my morning commute today, I was thinking about the sync values
displayed in the Playback Data window on the frontend. Currently, we are
showing the raw unfiltered sync measurements as calculated using the
difference of the audio and the video timestamps. In the presence of
jitter, this can be misleading to the user on the true sync quality.
However, the current implementation does make it clearly evident that the
sync measurement is highly variable. As an alternative, we could consider
showing the filtered value on which the sync control loop is acting which
would be more representative of what the system is actually trying to
control against.

I think this would require changing line 1709 in mythplayer.cpp from this:
avsync_avg = static_cast<int>(audio_adjustment * 1000);
to this:
avsync_avg = static_cast<int>(last_fix * 1000 / s_av_control_gain);

Thanks and kind regards,
Tim
Re: AVSync2 Refinements [ In reply to ]
On Wed, 11 Dec 2019 at 14:14, Tim Pletcher <pletchtd@gmail.com> wrote:
> Hi Mark,
>
> I added the filter in with a second patch and posted on the ticket last evening. I saw this was picked up and rolled into master just a bit ago.

Tim

Thanks again for looking at this.

Did you spot my comment in the commit re the change to 0.4 for
s_av_control_gain? Was that the correct call? :)

> If anyone is interested, I threw together a crude simulation to theoretically demonstrate the different control responses with the various control options (plot shown at: https://imgur.com/a/DDBxcQR). On the the plot, the green line is the gain only implementation and the red line is the gain + filter implementation provided in the second patch revision. The control loop will settle in around the process mean which includes the jitter / noise so it may have a little zero offset but this should not be enough to be evident for the user.
>
> I have tested with 1280x720_59.94 & 1080i_29.97 live television content using both VAAPI and NVDEC with no issues thus far.
>
> During my morning commute today, I was thinking about the sync values displayed in the Playback Data window on the frontend. Currently, we are showing the raw unfiltered sync measurements as calculated using the difference of the audio and the video timestamps. In the presence of jitter, this can be misleading to the user on the true sync quality. However, the current implementation does make it clearly evident that the sync measurement is highly variable. As an alternative, we could consider showing the filtered value on which the sync control loop is acting which would be more representative of what the system is actually trying to control against.
>
> I think this would require changing line 1709 in mythplayer.cpp from this:
> avsync_avg = static_cast<int>(audio_adjustment * 1000);
> to this:
> avsync_avg = static_cast<int>(last_fix * 1000 / s_av_control_gain);
>

I think we should be showing the filtered value.

Regards
Mark
_______________________________________________
mythtv-dev mailing list
mythtv-dev@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-dev
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: AVSync2 Refinements [ In reply to ]
On Wed, Dec 11, 2019 at 9:22 AM Mark Kendall <mark.kendall@gmail.com> wrote:

> On Wed, 11 Dec 2019 at 14:14, Tim Pletcher <pletchtd@gmail.com> wrote:
> > Hi Mark,
> >
> > I added the filter in with a second patch and posted on the ticket last
> evening. I saw this was picked up and rolled into master just a bit ago.
>
> Tim
>
> Thanks again for looking at this.
>
> Did you spot my comment in the commit re the change to 0.4 for
> s_av_control_gain? Was that the correct call? :)
>
>
I think this value should remain as 0.04.

Tim

> >
> > I think this would require changing line 1709 in mythplayer.cpp from
> this:
> > avsync_avg = static_cast<int>(audio_adjustment * 1000);
> > to this:
> > avsync_avg = static_cast<int>(last_fix * 1000 / s_av_control_gain);
> >
>
> I think we should be showing the filtered value.
>
>
I also think this makes sense.

Tim
Re: AVSync2 Refinements [ In reply to ]
On Wed, 11 Dec 2019 at 14:29, Tim Pletcher <pletchtd@gmail.com> wrote:
>
> I think this value should remain as 0.04.

OK - as per my last comment on the ticket - I'll do a little more
testing and may adjust the threshold for logging the a/v sync. I'd
prefer not to add another setting.

>> >
>> > I think this would require changing line 1709 in mythplayer.cpp from this:
>> > avsync_avg = static_cast<int>(audio_adjustment * 1000);
>> > to this:
>> > avsync_avg = static_cast<int>(last_fix * 1000 / s_av_control_gain);
>> >
>>
>> I think we should be showing the filtered value.
>>
>
> I also think this makes sense.

I'll try and get this in today.

Thanks and regards
Mark
_______________________________________________
mythtv-dev mailing list
mythtv-dev@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-dev
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: AVSync2 Refinements [ In reply to ]
On Wed, 11 Dec 2019 at 14:14, Tim Pletcher <pletchtd@gmail.com> wrote:
> If anyone is interested, I threw together a crude simulation to theoretically demonstrate the different control responses with the various control options (plot shown at: https://imgur.com/a/DDBxcQR). On the the plot, the green line is the gain only implementation and the red line is the gain + filter implementation provided in the second patch revision. The control loop will settle in around the process mean which includes the jitter / noise so it may have a little zero offset but this should not be enough to be evident for the user.

Tim

Out of curiosity - could that zero offset be estimated?

I ask because at some point I would like to test adding a couple of
additional a/v sync adjustments - and clearly the greater the accuracy
the better.

The first adjustment is to account for FFmpeg hardware deinterlacers
(currently only VAAPI VVP - but may add NVDEC soon) that do a little
buffering. As they are processed after a/v sync, we will need to pass
back an offset - probably on the next pass.

The second is to handle a/v sync adjustments that are
required/reported by displays via the EDID. I recently added some EDID
parsing to our code and it only requires a few extra lines to parse
out the sync adjustments required when displaying with interlaced and
progressive modelines.

Regards
Mark
_______________________________________________
mythtv-dev mailing list
mythtv-dev@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-dev
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: AVSync2 Refinements [ In reply to ]
>
>
>
> Out of curiosity - could that zero offset be estimated?
>
> I ask because at some point I would like to test adding a couple of
> additional a/v sync adjustments - and clearly the greater the accuracy
> the better.
>
> The first adjustment is to account for FFmpeg hardware deinterlacers
> (currently only VAAPI VVP - but may add NVDEC soon) that do a little
> buffering. As they are processed after a/v sync, we will need to pass
> back an offset - probably on the next pass.
>
> The second is to handle a/v sync adjustments that are
> required/reported by displays via the EDID. I recently added some EDID
> parsing to our code and it only requires a few extra lines to parse
> out the sync adjustments required when displaying with interlaced and
> progressive modelines.
>
>
I was able to do a little more testing today and figured out how to nicely
parse the log playback,timestamp data into a comma-delimited file to enable
me to look more closely at some real data.

First, I found that a 0.4 gain and 0.9 filter as I initially proposed does
work well. I think you were actually right to leave it there. I didn't
notice increased frame drops or other issues under these settings.

Initially, I was more inclined to highly dampen the control output to
minimize responding to noise but the filtering takes care of that well
enough even with a higher gain. As an example, here (
https://imgur.com/EcKuA8J) is a plot showing some data I collected when
viewing a progressive channel and an interlaced channel successively at the
gain=0.4 & filter=0.9 settings. It is the deinterlacing that adds much
jitter to the measured sync.

The jitter and measurement are also more centered around zero than in my
initial simulation (I incorrectly assumed jitter would manifest mostly as
lag). As a result, there is no corresponding zero offset in the control
under the real conditions that needs to be compensated.

I would be interested to see some of the raw data reported by the EDID as
well as whatever ffmpeg returns with the processed frames. Is the
buffering you speak of the reason that the sync measurement becomes is so
much noisier when deinterlacing is in use?

Regards,
Tim
Re: AVSync2 Refinements [ In reply to ]
> I was able to do a little more testing today and figured out how to nicely
> parse the log playback,timestamp data into a comma-delimited file to enable
> me to look more closely at some real data.
>
> First, I found that a 0.4 gain and 0.9 filter as I initially proposed does
> work well.
>

Apologies Mark.

I just noticed you already committed the 0.04. I don't mean to appear so
indecisive on this :) I think it is probably splitting hairs at this
point as the control seems to work pretty good with either settings.

Tim
Re: AVSync2 Refinements [ In reply to ]
On Fri, 13 Dec 2019 at 18:21, Tim Pletcher <pletchtd@gmail.com> wrote:
>
>
>> I was able to do a little more testing today and figured out how to nicely parse the log playback,timestamp data into a comma-delimited file to enable me to look more closely at some real data.
>>
>> First, I found that a 0.4 gain and 0.9 filter as I initially proposed does work well.
>
>
> Apologies Mark.
>
> I just noticed you already committed the 0.04. I don't mean to appear so indecisive on this :) I think it is probably splitting hairs at this point as the control seems to work pretty good with either settings.

Tim

No problem. I'm minded to go back to 0.4 as it generally seems a
little slow to respond at the moment.

With respect to deinterlacers, hopefully the extra jitter you've
demonstrated is just a result of system load (in this case GPU rather
than CPU).

The VAAPI/VPP deinterlacers use FFmpeg/libavfilter code to run. These
will only return deinterlaced frames when they have seen at least 2
frames as they need multiple frames to work with. Hence there should,
in theory, be a 1 frame delay inherent in the filter. The same applies
to the OpenGL/shader kernel deinterlacer. It is that delay that I want
to feed back to the player.

The EDID data that is present in most modern displays is a set of
static figures that indicate to the connected device how long it takes
to process video frames prior to display. It just needs to be checked
once at the start of playback.

Regards
Mark
_______________________________________________
mythtv-dev mailing list
mythtv-dev@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-dev
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: AVSync2 Refinements [ In reply to ]
On Sat, Dec 14, 2019 at 03:00:24PM +0000, Mark Kendall wrote:
> On Fri, 13 Dec 2019 at 18:21, Tim Pletcher <pletchtd@gmail.com> wrote:
> >
> >
> >> I was able to do a little more testing today and figured out how to nicely parse the log playback,timestamp data into a comma-delimited file to enable me to look more closely at some real data.
> >>
> >> First, I found that a 0.4 gain and 0.9 filter as I initially proposed does work well.
> >
> >
> > Apologies Mark.
> >
> > I just noticed you already committed the 0.04. I don't mean to appear so indecisive on this :) I think it is probably splitting hairs at this point as the control seems to work pretty good with either settings.
>
> Tim
>
> No problem. I'm minded to go back to 0.4 as it generally seems a
> little slow to respond at the moment.

Count me firmly in the 0.4 camp. I didn't see much difference with
progressive content yesterday but did today with interlaced content.
0.04 takes noticably longer to settle down.

David

> With respect to deinterlacers, hopefully the extra jitter you've
> demonstrated is just a result of system load (in this case GPU rather
> than CPU).
>
> The VAAPI/VPP deinterlacers use FFmpeg/libavfilter code to run. These
> will only return deinterlaced frames when they have seen at least 2
> frames as they need multiple frames to work with. Hence there should,
> in theory, be a 1 frame delay inherent in the filter. The same applies
> to the OpenGL/shader kernel deinterlacer. It is that delay that I want
> to feed back to the player.
>
> The EDID data that is present in most modern displays is a set of
> static figures that indicate to the connected device how long it takes
> to process video frames prior to display. It just needs to be checked
> once at the start of playback.
>
> Regards
> Mark
> _______________________________________________
> mythtv-dev mailing list
> mythtv-dev@mythtv.org
> http://lists.mythtv.org/mailman/listinfo/mythtv-dev
> http://wiki.mythtv.org/Mailing_List_etiquette
> MythTV Forums: https://forum.mythtv.org

--
David Engel
david@istwok.net
_______________________________________________
mythtv-dev mailing list
mythtv-dev@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-dev
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: AVSync2 Refinements [ In reply to ]
On 12/16/2019 4:21 PM, David Engel wrote:
> On Sat, Dec 14, 2019 at 03:00:24PM +0000, Mark Kendall wrote:
>> On Fri, 13 Dec 2019 at 18:21, Tim Pletcher <pletchtd@gmail.com> wrote:
>>>
>>>> I was able to do a little more testing today and figured out how to nicely parse the log playback,timestamp data into a comma-delimited file to enable me to look more closely at some real data.
>>>>
>>>> First, I found that a 0.4 gain and 0.9 filter as I initially proposed does work well.
>>>
>>> Apologies Mark.
>>>
>>> I just noticed you already committed the 0.04. I don't mean to appear so indecisive on this :) I think it is probably splitting hairs at this point as the control seems to work pretty good with either settings.
>> Tim
>>
>> No problem. I'm minded to go back to 0.4 as it generally seems a
>> little slow to respond at the moment.
> Count me firmly in the 0.4 camp. I didn't see much difference with
> progressive content yesterday but did today with interlaced content.
> 0.04 takes noticably longer to settle down.
>
You can actually have a fast startup IIR (which is what it is) using a
counter as the inverse of the feedback coeff and saturate it at the
desired long term coeff value

i.e.

Y[n] = Y[n-1] * (1-1/c) + (1/c)*X[n]

c1 = 1 to K, K is where 1/K < 1/A, A is long term input coeff

c = min(c1, 1/A)

This assumes unity gain.

This gets you stable long term single pole filtering with fast startup.

HTH

MarkS


_______________________________________________
mythtv-dev mailing list
mythtv-dev@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-dev
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: AVSync2 Refinements [ In reply to ]
On Mon, Dec 16, 2019 at 12:22 AM David Engel <david@istwok.net> wrote:

> On Sat, Dec 14, 2019 at 03:00:24PM +0000, Mark Kendall wrote:
> > On Fri, 13 Dec 2019 at 18:21, Tim Pletcher <pletchtd@gmail.com> wrote:
> > >
> > >
> > >> I was able to do a little more testing today and figured out how to
> nicely parse the log playback,timestamp data into a comma-delimited file to
> enable me to look more closely at some real data.
> > >>
> > >> First, I found that a 0.4 gain and 0.9 filter as I initially proposed
> does work well.
> > >
> > >
> > > Apologies Mark.
> > >
> > > I just noticed you already committed the 0.04. I don't mean to appear
> so indecisive on this :) I think it is probably splitting hairs at this
> point as the control seems to work pretty good with either settings.
> >
> > Tim
> >
> > No problem. I'm minded to go back to 0.4 as it generally seems a
> > little slow to respond at the moment.
>
> Count me firmly in the 0.4 camp. I didn't see much difference with
> progressive content yesterday but did today with interlaced content.
> 0.04 takes noticably longer to settle down.
>
> David
>
>
For me, the 0.4 gain with a 0.6 filter coefficient causes jumps in video
playback on my low end apollo lake series frontends using VAAPI. Without
sufficient low pass filtering, the adjustments are too much response to the
fairly regular larger spikes in sync measurement when using deinterlacing (
https://imgur.com/YpmvIG7).

Adjustments of more than roughly 5 ms with any regularity do not provide
smooth video playback and this is particularly highlighted when watching
programs with lots of motion such as sports. I provided an example plot
earlier in this thread with a gain of 0.4 and a filter coefficient of 0.9
and those settings provide satisfactory video playback on these apollo lake
machines.

Tim
Re: AVSync2 Refinements [ In reply to ]
On Tue, 17 Dec 2019 at 01:09, Tim Pletcher <pletchtd@gmail.com> wrote:
> For me, the 0.4 gain with a 0.6 filter coefficient causes jumps in video playback on my low end apollo lake series frontends using VAAPI. Without sufficient low pass filtering, the adjustments are too much response to the fairly regular larger spikes in sync measurement when using deinterlacing (https://imgur.com/YpmvIG7).
>
> Adjustments of more than roughly 5 ms with any regularity do not provide smooth video playback and this is particularly highlighted when watching programs with lots of motion such as sports. I provided an example plot earlier in this thread with a gain of 0.4 and a filter coefficient of 0.9 and those settings provide satisfactory video playback on these apollo lake machines.

I've pushed an update to use 0.4 and 0.9.

I'm not seeing any issues with regular video playback on intel
(vaapi/software), nvidia (nvdec,vdpau,software) and Pi4.

There are some issues - and I'll caveat these by saying I'm not sure
whether they result from the recent avsync changes, from the long
standing avsync2 code or other changes I've made recently.

Firstly - playback of audio only files seems a little broken.
Particularly MHEG/DVB radio and seeking. This may be related to a
small fix I added for MHEG only streams - I need to check.

Secondly - video only streams sometimes struggle to settle down at the
start of playback - though I'm starting to wonder whether this is
purely a Raspberry Pi issue.

Thirdly - there is some very strange behaviour on OSX under certain
conditions. Sometimes the video continually speeds up and slows down.
Piotr reported this the other day and I can reproduce on my (very) old
macbook. For me I only see it when using the inbuilt display and
certain interlaced videos using double rate deinterlacing. If I
connect to an external display, everything is fine. Due to the
limitations of the macbook, I can't test heavily before I max it out -
and frames are dropped naturally.

The oddity with OSX internal displays is that they do not have a fixed
refresh rate. Furthermore, vsync always falls back to busy wait - as
there is no DRM. So the rendering code will not block in the same way
when displaying a frame. I presume it will rate limit any refresh
rates to the overall max for the display (presumably 60Hz) - but
otherwise it doesn't appear to wait for OpenGL vsync (but it isn't
tearing). I can't get my Christmas limited brain power around what
might be happening. I should add that CPU load is low and OSX has no
OpenGL performance monitors - so cannot check the GPU.

Anyone have any ideas?

Regards
Mark
_______________________________________________
mythtv-dev mailing list
mythtv-dev@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-dev
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: AVSync2 Refinements [ In reply to ]
>
> I've pushed an update to use 0.4 and 0.9.
>
> I'm not seeing any issues with regular video playback on intel
> (vaapi/software), nvidia (nvdec,vdpau,software) and Pi4.
>
> There are some issues - and I'll caveat these by saying I'm not sure
> whether they result from the recent avsync changes, from the long
> standing avsync2 code or other changes I've made recently.
>
> Firstly - playback of audio only files seems a little broken.
> Particularly MHEG/DVB radio and seeking. This may be related to a
> small fix I added for MHEG only streams - I need to check.
>
> Secondly - video only streams sometimes struggle to settle down at the
> start of playback - though I'm starting to wonder whether this is
> purely a Raspberry Pi issue.
>
> Thirdly - there is some very strange behaviour on OSX under certain
> conditions. Sometimes the video continually speeds up and slows down.
> Piotr reported this the other day and I can reproduce on my (very) old
> macbook. For me I only see it when using the inbuilt display and
> certain interlaced videos using double rate deinterlacing. If I
> connect to an external display, everything is fine. Due to the
> limitations of the macbook, I can't test heavily before I max it out -
> and frames are dropped naturally.
>
> The oddity with OSX internal displays is that they do not have a fixed
> refresh rate. Furthermore, vsync always falls back to busy wait - as
> there is no DRM. So the rendering code will not block in the same way
> when displaying a frame. I presume it will rate limit any refresh
> rates to the overall max for the display (presumably 60Hz) - but
> otherwise it doesn't appear to wait for OpenGL vsync (but it isn't
> tearing). I can't get my Christmas limited brain power around what
> might be happening. I should add that CPU load is low and OSX has no
> OpenGL performance monitors - so cannot check the GPU.
>
> Anyone have any ideas?
>
> Regards
> Mark
>
> Thank you Mark.

In the second and third cases you mention above, does the timestamp data
provide any clues as to what is occuring?

To provide better visibility of what is going on, I have been patching
mythplayer lib to also emit the lastfix value with the timestamp data when
I wish to troubleshoot. I also hacked together this simple bash script
(attached) to parse the data into a comma delimited text file for easier
viewing of the data in a spreadsheet program.

I have been thinking about implementing a simple adaptive filtering /
control algorithm which would dynamically adjust the filtering coefficient
between an upper and lower bound based on the standard deviation of the
sync calculated over a shifting window of frames. I believe the comments
from Mark S earlier in this thread also essentially propose an adaptive
filter approach but I need spend some time examining. On the flip side, I
am hesitant to add complexity if not needed / really valuable.

If you send me some frontend log data with timestamps, I'd be happy to look
over in order to see if the data suggests these are control problems or
caused by something else.

Tim
Re: AVSync2 Refinements [ In reply to ]
On Wed, 18 Dec 2019 at 14:48, Tim Pletcher <pletchtd@gmail.com> wrote:

Tim

So the first issue was self inflicted - I've added a better fix for
MHEG only files that doesn't break audio only.

> In the second and third cases you mention above, does the timestamp data provide any clues as to what is occuring?
>
> To provide better visibility of what is going on, I have been patching mythplayer lib to also emit the lastfix value with the timestamp data when I wish to troubleshoot. I also hacked together this simple bash script (attached) to parse the data into a comma delimited text file for easier viewing of the data in a spreadsheet program.

Thanks - I forgot to ask if I could get that script. I'll try and pull
out some data when I can - visitors arriving for Christmas today...

> I have been thinking about implementing a simple adaptive filtering / control algorithm which would dynamically adjust the filtering coefficient between an upper and lower bound based on the standard deviation of the sync calculated over a shifting window of frames. I believe the comments from Mark S earlier in this thread also essentially propose an adaptive filter approach but I need spend some time examining. On the flip side, I am hesitant to add complexity if not needed / really valuable.

I'm open to further improvements but I think we need to let the
current code settle for a while - and if there are no reported issues,
perhaps refine it after 0.31 is out.

> If you send me some frontend log data with timestamps, I'd be happy to look over in order to see if the data suggests these are control problems or caused by something else.

Thanks - I will do that:)

Regards
Mark
_______________________________________________
mythtv-dev mailing list
mythtv-dev@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-dev
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org