Kodi Audio Out of Sync playing file or after seeking

You missed: video + video/audio timing component
You enabled only the video/audio timing component.

And please always start playback from beginning on make a log!

Damn! OK Iā€™m preparing a new log then :sweat_smile:

I donā€™t understand. I started from beginning of the file. I pressed play and waited until the end of the video without any intervention whatsoever. Once the video closed itself, I sshā€™ed the box and copied the kodi.log file, nothing else.

Slight problem uploading the log :

# paste kodi_20241122172905_CE-NG-20241122085304.log
Error happen, maybe need to increase file upload sizes in php.ini.

The log is 3738295 bytes

Here is the new log
kodi_20241122172905_CE-NG-20241122085304.log.gz (431.2 KB)

To make sure everything is done according to your needs, here are the steps :

  • Boot the USB stick with your latest image
  • Set debug logging and select both ā€œvideoā€ + ā€œvideo/audio timingā€ components
  • Reboot to get a fresh log without pollution
  • Go straight to the video location
  • Play the file and let it play until the end
  • SSH to the box, copy .kodi/temp/kodi.log to logfiles and rename it to a more useful name
  • Paste the log (or, in this case upload on the forum after GZ compression)

And this was not in sync?
Because kodi says: max about a half frame behind, ~22ms.
One time it got >50ms (64ms) and it skipped one frame.

So how kodi does correct work correct here.
The only item left is this part:

  m_displayLatency = DVD_MSEC_TO_TIME(
      m_latencyTweak +
      static_cast<double>(CServiceBroker::GetWinSystem()->GetGfxContext().GetDisplayLatency()) -
      m_videoDelay -
      static_cast<double>(CServiceBroker::GetWinSystem()->GetFrameLatencyAdjustment()));

So when this value is wrong it might get a offset but kodi think itā€™s in sync!?
But it should have constant offset I guess.
I donā€™t know yet where these values come from.

Nope. The beep is heard at the (-2) blue circle and sometimes I get the impression that it shifts to -3 or even -4 but I hope this is not some kind of illusion (confirmation bias in psychology). One thing is sure, itā€™s never at (0) when the video is 4K.

I have a ā€œLipSyncā€ folder filled with extracts from various movies I own, all in 4K and TrueHD, DTS-MA, DTS-MA + DTS-X and EAC-3, quite exclusively actors close ups in good lighting with clear speech so I can really watch the lips and compare with the sound.

Your latest CE image does better than my usual nigtly (20241109), I can even press ā€œleftā€ once on the remote to rewind 5 seconds and on some movies is falls in sync (DTS-MA in this case) but not all.

On the nightly, no matter if I play normally or rewind a bit, the lips are always out of sync. 2 frames in 23.976 is 83 ms and thatā€™s usually the amount of audio offset I need to set on Kodi on these samples to get the sync back.

I always remove the audio offset afterwards to not compromise the future tests with new CE images and other various tests.

Kodi have a ā€œbugā€ in frame skip.
When itā€™s only one frame off it does not fix it. So this 41ms.
So it only adjust if itā€™s Ā±2 frames. But this is because like maybe 15 frames later itā€™s good again, or maybe never.

I did now add a short work around to sync on really the exact frame.
But this maybe lead to ā€œstutterā€ as much more frames are skipped.

Please test with this new image, watch 20-30s. Then skip backwards when you say itā€™s then in sync. Watch again like 5s, this should be enough.
And same kodi.logā€¦

I quickly tested this image which installed and booted fine but I canā€™t open any of my video files :frowning:

Selecting a file and pressing play freezes Kodi for about a minute until it comes back like a new launch from boot. The contents of .kodi/temp/ confirms a kodi_crash log. I tested several files and they all make kodi crash. After a few crashes, CE rebooted to a safe mode (I didnā€™t even know there was one, nice :smile:) and I rebooted back to my CE-NG config.

Youā€™ll find the crash log here kodi_crashlog_20241122203154.log

PS: I have to leave for a few hours and Iā€™ll make myself available again after

No clue if this helps and can apply to CE
Kodi Self-calibration of max audio/video out-of-sync

https://forum.kodi.tv/showthread.php?tid=371292

This is already inside I guess. With TrueHD I have seen a 100ms and 30ms threshold.

I removed the dirty ā€œskipā€ fix and it should now work again, maybe:

But I will need a new log with this test image, when playback works againā€¦

1 Like

I checked a bit this code and it looks like the latency is hardcoded. Itā€™s number of (video buffer + 1) / frame rate.

Default itā€™s set to 3. So the latency is about 96ms.
So you can play to adjust this value step by step until itā€™s in sync?

It should be found in display settings when in expert mode.

I have been looking around in both the video and audio expert settings for the last half hour and donā€™t remember seeing it but Iā€™ll look again.

Just going by my eyes and ears the audio is still pretty far off by starting a previously paused video, pausing and playing can bring it in a little bit but it doesnā€™t always help the first time.

I know this isnā€™t very helpful in the grand scheme of things because I havenā€™t uploaded logs but I just wanted to give a seat of the pants impression.

Iā€™ll try to review this thread tomorrow or Sunday to be able to contribute meaningful logs.

I find it really strange that so many people seem to have audio sync issues and others (like me) donā€™t have any issues.

I donā€™t know if it has anything to do with it, but Iā€™ve been using this setting in advancedsettings.xml ever since audio rework was done by a couple of developers in Kodi for Android:

  <audio>
    <maxpassthroughoffsyncduration>50</maxpassthroughoffsyncduration>
  </audio>

Initially, I had it set to 96 but after even more rework with regards to the MAT packer, the developerā€™s advice was to set it to 50.

If you donā€™t set it manually, I believe Kodi tries to determine the required value by itself during the first 30 seconds of playing a video.

I have no clue if this code is also active in CE, but since I never have audio sync issues, I just left it in there.

1 Like

This was discussed earlier in this thread. As you say, the calculation is (number_of_buffers + 1) / frame_rate. For a 23.97ā€¦fps video though with the default 3 buffers, this does not equal 96ms, but rather 166.8333 ms. This calculation matches the result shown in the player process debug info under latency. I have checked this against different frame rates and number of buffers.

My setup is CE device ā†’ TV ā†’ eARC ā†’ sound system with CE set to passthrough everything - in theory this should have no lipsync issues due to how eARC works. When I was testing however, the consistent offset I saw was the audio 5 frames late.

From looking at the kodi code, my impression is that kodi is trying to ā€˜compensateā€™ for the latency of the display by shifting output audio to be late compared to the video by a fixed amount of frames. The hdmi standard is very clear on this - this is not how source devices should behave. Valid options to take depend on the Video_Latency and Audio_Latency sent by the sink device (i.e, TV) in the edid:

  • If not indicated, audio and video streams shall be transmitted with under +/- 2ms of delay.
  • If indicated, and Video_Latency > Audio_Latency either:
    ** Transmit with streams in sync as above, or
    ** Delay the audio by Video_Latency - Audio_Latency
  • If Video_Latency < Audio_Latency, a situation that is strongly discouraged, the source device shall transmit with audio and video streams in sync as per the first point.

Any other potential sources of latency (i.e. of the display, different processing modes of the TV, or repeaters (AVRā€™s etc)) are subsequently handled the other devices in the chain - not the source device.

As such, I believe what kodi is trying do is incorrect and would result in the audio being delayed by the shown latency value. This explains 4 of the 5 frames delay in audio I am seeing.

I think that latency value should be removed entirely from kodiā€™s code. On the code that is currently available, I have tested doing so and it did reduce the audio delays by 4 frames - as expected based on the default 3 buffers.

1 Like

This has since been removed from kodi. It is not currently in CE.

According to this PR:

The setting is still present, but it defaults to 50 and there is no self-learning anymore. So I guess me setting it manually to 50 has become unnecessary :smiley:

Here are the new logs
kodi_20241123043114_CE-NG-20241122212602.zip (390.8 KB)

Youā€™ll find :

  • kodi_VidBuf2_20241123041434_CE-NG-20241122212602.log
  • kodi_VidBuf3_20241123041758_CE-NG-20241122212602.log

VidBuf# is Settings ā†’ System ā†’ Display ā†’ Number of buffers used by graphics driver (Iā€™m always in Expert mode). I can only choose between 2 and 3, defaulting to 3. When this setting was changed, a reboot was performed to get a clean start and unpolluted log.

As requested, the file was played for about 20~30 seconds then a single ā€œLeftā€ press to rewind 5 seconds and let it play 5~7 seconds more by eye.

Ok, I made some tests with this setting.

  • Both on my day-to-day nightly (20241109) and the last img (20241122212602) thereā€™s no difference between 3 (default) and 2 playing my tests files without further intervention
  • All my test files, 4K HEVC with various audio (TrueHD, DTS-MA, DTS:X and EAC-3) still are audio lagged upon playing them. All these files have multi-channel audio except of course the PhotoJoseph 23.98 test which is stereo.
  • Manually setting the audio delay in Kodi per file around 150~175 looks like the right setting to get the lips in sync with the audio in all my movie extract files which makes it about 4 frames 167 ms in 23.976. This is a bit contradictory with the visual feedback I get with the PhotoJoseph 23.98 test file where I could have sworn the beep occurs at the -2 mark which is 83 ms. Setting 75-100 ms with Kodi on my lipsync test files is clearly not good, thereā€™s still a visible delay to me

When this happened which audio format were you using?

From memory when I briefly tested, I think I was seeing a difference in offsets between formats that could be passed though as RAW (TrueHD, DTS, eac3, and ac3) and those that couldnā€™t (pcm and flac formats).

I think the default track on that samples was one that couldnā€™t be passed through as RAW - which would be different to all your other samples.