Skip to content

decoder: Zero the bytes after the input in the bitstream buffer - #38

Merged
harishdm merged 1 commit into
ittiam-systems:mainfrom
GeekQAQ:pad-input-buffer
Nov 20, 2026
Merged

harishdm merged 1 commit into
ittiam-systems:mainfrom
GeekQAQ:pad-input-buffer

Conversation

@GeekQAQ

@GeekQAQ GeekQAQ commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Each decode call copies its input into pu1_input_buffer, which has MIN_BUFFER_BYTES_AT_EOS spare bytes after MAX_BITSTREAM_BUFFER_SIZE. The bitstream reader prefetches 32-bit words, so when a slice is cut off at the end of the input, the decoder reads the bytes after the input. Those bytes are never cleared; they hold whatever an earlier, longer input left in the buffer, also across IVD_CMD_CTL_RESET.

So the result of decoding a truncated input depends on what was decoded before. For example, after a reset, the first 160 bytes of a stream (sequence header, GOP header and a cut-off first slice) are consumed in one call or in two, and the picture differs, depending on which stream was decoded before the reset; a new decoder gives one result.

The reads stay inside the allocation with or without this change. The change only clears the spare bytes after copying the input in impeg2d_dec_hdr() and impeg2d_dec_frm(), so the bytes the reader sees past the input are always zero.

Tested on main (275c536) with clang 21 and AddressSanitizer: with the change, the truncated input gives the same bytes consumed and the same picture as on a new decoder after each of the four earlier streams (before: after three of them the first call consumed 160 bytes instead of 153, and all four gave a different picture). The program and the streams are in this gist: https://gist.github.com/GeekQAQ/d0a31657d236ac2979a3840821b0abe0 The default CMake build (Release, gcc and clang) has no new warnings, and mpeg2dec gives byte-identical output before and after the change on 16 short test streams with 1 core, and with 4 cores except for a stream that starts with a B picture, whose 4-core output already varies from run to run on main (a separate issue, sent as its own PR).

The input of each decode call is copied into pu1_input_buffer, which has
MIN_BUFFER_BYTES_AT_EOS spare bytes after MAX_BITSTREAM_BUFFER_SIZE. The
bitstream reader prefetches 32-bit words, so when a slice is cut off at
the end of the input it reads the bytes after the input. Those bytes were
whatever an earlier, longer input left in the buffer, and were not
cleared by IVD_CMD_CTL_RESET either: the bytes consumed and the decoded
picture for a truncated input depended on what had been decoded before,
e.g. on the stream decoded before a reset.

Clear the spare bytes after copying the input in impeg2d_dec_hdr() and
impeg2d_dec_frm().
Copilot AI balanced review requested due to automatic review settings November 19, 2026 23:54

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

Thank you for this PR.

@harishdm
harishdm merged commit b670f05 into ittiam-systems:main Nov 20, 2026
10 checks passed
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