Skip to content

sub/sd_ass: subtitle timing rounding error#18039

Open
kako94321 wants to merge 1 commit into
mpv-player:masterfrom
kako94321:fix#17453
Open

sub/sd_ass: subtitle timing rounding error#18039
kako94321 wants to merge 1 commit into
mpv-player:masterfrom
kako94321:fix#17453

Conversation

@kako94321
Copy link
Copy Markdown

Use nearbyint() when the value is extremely close to an integer and floor() otherwise to prevent forward rounding caused by floating-point precision errors. This avoids early subtitle presentation on boundary frames.

Add test for ASS subtitle timing to ensure correct rendering around boundary timestamps.

Fixes: #17453

Read this before you submit this pull request:
https://github.com/mpv-player/mpv/blob/master/DOCS/contribute.md

Reading this link and following the rules will get your pull request reviewed
and merged faster. Nobody wants lazy pull requests.

Comment thread sub/sd_ass.c Outdated
@kasper93
Copy link
Copy Markdown
Member

@arch1t3cht: Could you do the same in aegi? floor(pts * 1000 + 1e-6); or something? I think it would be prudent to align on the solution. Adding epsilon fixes, the issue mentioned here #17453 (comment). (i.e. floor(518.81 * 1000 + 1e-6) = 518810)

@kasper93
Copy link
Copy Markdown
Member

Please squash the commits. I also think that a test for single math operation is a bit overkill.

@kako94321
Copy link
Copy Markdown
Author

Of course. I'm removing the test file before squashing then

@kako94321 kako94321 force-pushed the fix#17453 branch 2 times, most recently from 7f8462a to ea85a60 Compare May 29, 2026 22:21
Use floor(pts * 1000 + 1e-6) to avoid floating-point precision errors
causing early subtitle presentation on boundary frames.

Fixes: mpv-player#17453
Signed-off-by: Ricardo Fonseca <ricardofonseca94321@tecnico.ulisboa.pt>
Copy link
Copy Markdown
Member

@kasper93 kasper93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@arch1t3cht
Copy link
Copy Markdown
Contributor

Could you do the same in aegi?

Yeah, I will. A hardcoded + 1e-6 does seem a bit hacky to me but there's no real better fully satisfying solution (except reworking the entire pipeline to never use floats lol) and I'm not sure how much it's worth bikeshedding this further.

Copy link
Copy Markdown
Member

@kasper93 kasper93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact there are 3 other places where timestamp is converted. Could you update them too?

@cubicibo
Copy link
Copy Markdown

Why not just use nextafter(double, double) prior to flooring?

@kasper93
Copy link
Copy Markdown
Member

Why not just use nextafter(double, double) prior to flooring?

Having bigger epsilon is safer. If the pts accumulated more error across it's lifetime, it's not certain nextafter will be enough for this.

@kako94321
Copy link
Copy Markdown
Author

In fact there are 3 other places where timestamp is converted. Could you update them too?

Sure. Are you also referring to llrint(pkt->duration * 1000)?

@kasper93
Copy link
Copy Markdown
Member

In fact there are 3 other places where timestamp is converted. Could you update them too?

Sure. Are you also referring to llrint(pkt->duration * 1000)?

Yes, and 2 more. Search for llrint in that file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.ass subtitle start at wrong frame

4 participants