Skip to content

decoder: Decode a picture on one thread when it is its own reference - #42

Closed
GeekQAQ wants to merge 1 commit into
ittiam-systems:mainfrom
GeekQAQ:self-ref-single-thread
Closed

GeekQAQ wants to merge 1 commit into
ittiam-systems:mainfrom
GeekQAQ:self-ref-single-thread

Conversation

@GeekQAQ

@GeekQAQ GeekQAQ commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

When a P picture has no forward reference or a B picture has no references (a stream starting with a B picture, the leading B pictures of an open GOP, the first P picture after a reset, corrupted data), impeg2d_pre_pic_dec_proc() uses the current picture buffer as the reference. With more than one core, impeg2d_dec_pic_data() decodes the slices of that picture on several threads at once, so motion compensation in one thread reads rows of the picture that another thread is writing. The output then depends on thread scheduling: decoding the same stream that starts with a B picture 50 times with 4 cores gave 4 to 6 different outputs (always 1 with 1 core), and ThreadSanitizer reports data races between the impeg2_mc_*_sse42 writes and reads.

This change decodes such a picture on the calling thread only. Both loops (thread start and wait) use the reduced count, so it also works with u4_keep_threads_active = 1.

Tested on main (275c536) with clang 21, with u4_keep_threads_active 0 and 1: with 4 cores the 50 decodes now give a single output, and ThreadSanitizer reports no races (before: 4 to 6 outputs, 15 to 35 race reports). The program and the streams are in this gist: https://gist.github.com/GeekQAQ/5ed01c5783f91f35cdf4c74d964cb269 The default CMake build (Release, gcc and clang) has no new warnings. mpeg2dec --num_cores 4 on the stream that starts with a B picture gave 3 different outputs in 12 runs on main and the same output in 12 of 12 runs with the change (the 1-core output); on the other 15 short test streams mpeg2dec gives byte-identical output before and after the change with 1 and 4 cores.

When a P picture has no forward reference or a B picture has no
references (a stream starting with a B picture, the leading B pictures
of an open GOP, the first P picture after a reset, corrupted data),
impeg2d_pre_pic_dec_proc() uses the current picture buffer as the
reference. With more than one core, impeg2d_dec_pic_data() then decodes
the slices of that picture on several threads at once, and motion
compensation in one thread reads rows of the picture that another thread
is writing. The output depends on thread scheduling, and ThreadSanitizer
reports data races between impeg2_mc_*_sse42 writes and reads.

Decode such a picture on the calling thread only.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 23:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@harishdm

Copy link
Copy Markdown
Member

The scenario described above is an error case and decoder output is not guaranteed to produce same output in each run.
Decoder only ensures that it doesn't end up with any OOB read or writes.

Given this, can we close this PR?

@GeekQAQ

GeekQAQ commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the explanation, that makes sense. I'm fine with closing this PR.

@harishdm harishdm closed this Nov 20, 2026
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.

3 participants