Skip to content

vd_lavc: ensure decoder state is valid after reinit#17490

Open
kasper93 wants to merge 2 commits intompv-player:masterfrom
kasper93:hwdec_fix
Open

vd_lavc: ensure decoder state is valid after reinit#17490
kasper93 wants to merge 2 commits intompv-player:masterfrom
kasper93:hwdec_fix

Conversation

@kasper93
Copy link
Member

@kasper93 kasper93 commented Mar 2, 2026

This fixes dynamic hwdec switching, which would often fails, because hw decoder were feed data without context, which caused decoding errors, which in turn disabled hwdec.

command.c does queue_seek() after decoder reinit, but it may already be too late, by the time seek to keyframe finishes.

Fix this, by just discarding the data until we have safe point to start decoding again.

This should also fix cases, when media file is cut in a way that would cause decoding errors at the start.

Fixes: #17484

@kasper93 kasper93 force-pushed the hwdec_fix branch 2 times, most recently from ceb2a77 to 31943d7 Compare March 2, 2026 12:04
Copy link
Member

@philipl philipl left a comment

Choose a reason for hiding this comment

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

Nice!

I have been running into a problem when using hwdec=vulkan and trying to seek through formats like mpeg-ts where you aren't guaranteed to seek to a key frame.

I am basically guaranteed in this situation that the hwdec would freeze and I'd need to ctrl-c mpv. I'm sure there's some nvidia specific issue there, but with this patch, I can't get it to freeze and I can seek as much as I want with no problems. And it makes sense that if the issue was feeding unusable frames into the decoder, that this would fix it.

if (!ctx->avctx)
return AVERROR_EOF;

if (ctx->wait_for_keyframe && pkt) {
Copy link
Member

Choose a reason for hiding this comment

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

stupid question but is there guaranteed to be a keyframe at some point?

Choose a reason for hiding this comment

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

In theory I believe you can craft a file that's only P frames, has no reference picture.

Copy link
Member Author

Choose a reason for hiding this comment

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

stupid question but is there guaranteed to be a keyframe at some point?

That was my concern too. Not sure how to handle such possibility.

In theory I believe you can craft a file that's only P frames, has no reference picture.

Please provide a sample file, thanks.

Choose a reason for hiding this comment

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

test.mp4

maybe? lol

Choose a reason for hiding this comment

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

Can't even download it back now.

Copy link
Member Author

Choose a reason for hiding this comment

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

test.mp4

maybe? lol

[in#0 @ 000001c198281e80] Format mov,mp4,m4a,3gp,3g2,mj2 detected only with low score of 1, misdetection possible!
[in#0 @ 000001c198281e80] moov atom not found
[in#0 @ 000001c19824c780] Error opening input: Invalid data found when processing input

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess, we will merge, break everyones files and see.

Copy link
Member

Choose a reason for hiding this comment

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

thought 1:

  • remember where the last keyframe was and seek back to that location, start decoding from there and only emit frames once we have reached the old timestamp
    • (or does the player core already have logic that handles this? it sounds familiar)

thought 2:

  • put some hardcoded upper limit at least

Copy link
Member Author

Choose a reason for hiding this comment

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

thought 1

This is already done, our seeking is always seeking on key frames, but seeking is not synchronized with decoder reinit

thought 2

I think I will do that or limit this only to hwdec, where at least the codecs supported by hwdec should have parsers in ffmpeg, so we get keyframe info. And let some some obscure formats be decoded as is

thought 3

sync the reinit sequence with seek, but this looks like major pita and hacks.

This fixes dynamic hwdec switching, which would often fails, because
hw decoder were feed data without context, which caused decoding errors,
which in turn disabled hwdec.

command.c does queue_seek() after decoder reinit, but it may already be
too late, by the time seek to keyframe finishes.

Fix this, by just discarding the data until we have safe point to start
decoding again.

This should also fix cases, when media file is cut in a way that would
cause decoding errors at the start.

Fixes: mpv-player#17484
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.

Hardware decoding (Video Decode) not activating on initial playback of 8K 60fps videos, requires switching to another file and back to trigger

4 participants