Skip to content

media: Support In-Place WebMStreamParser - #13017

Merged
jasonzhangxx merged 2 commits into
youtube:mainfrom
jasonzhangxx:zero-copy-webm-parser
Oct 9, 2026
Merged

jasonzhangxx merged 2 commits into
youtube:mainfrom
jasonzhangxx:zero-copy-webm-parser

Conversation

@jasonzhangxx

@jasonzhangxx jasonzhangxx commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

When kCobaltInPlaceMediaSourceParser is enabled, Blink retains the buffer of every append and passes a release runner along with it. WebMStreamParser then holds the stream in a SegmentedByteQueue instead of byte_queue_, and parses it in place. The first append with a release runner creates the queue with SegmentedByteQueue::Create(), passing the feature's "borrow_mode" param, so each append is either borrowed or copied into memory the queue owns.

The release runner must not be empty, and the two AppendToParseBuffer() overloads must not be mixed. Both are CHECKed.

Give the WebM parsers overloads that take a SegmentedByteQueue and an offset instead of a pointer:

  • WebMParseElementHeader(), WebMListParser::Parse() and WebMClusterParser::Parse() read element headers in place, linearizing them only when they straddle a segment boundary.
  • WebMClusterParser hands contiguous elements to the pointer-based code. For a SimpleBlock that straddles a boundary, it linearizes only the block header, the Opus TOC and, when encrypted, the encryption header, and hands the frame data to the scatter-gather StreamParserBuffer::CopyFrom() as segments. A straddling Block or BlockAdditional is gathered straight from the segments.
  • WebMCreateDecryptConfig() gains an overload that takes the frame size separately, so it can work from just the encryption header.
  • Info and Tracks are small and are linearized before parsing.

Add a scatter-gather StreamParserBuffer::CopyFrom() overload that takes the frame data as a span of segments and forwards them to ExternalMemoryAllocator::CopyFrom(parts, type), so a block that straddles an append boundary is copied once, straight into the decoder buffer.

Bug: 558350763
TAG=agy
CONV=f7ab1644-7417-41cb-a782-c47dc051b09f

@jasonzhangxx
jasonzhangxx requested a review from a team as a code owner September 30, 2026 03:20
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Gemini Suggested Commit Message


media: Support in-place WebM stream parsing

Implement in-place parsing for WebM data to avoid unnecessary memory
copies into a contiguous ByteQueue when the feature flag
kCobaltInPlaceMediaSourceParser is enabled.

This change introduces WebMSegmentedBuffer to provide random access to
non-contiguous data segments. WebM parsers are updated with overloads
that utilize this interface, allowing them to read data directly from
the source segments. Headers and blocks that straddle segment
boundaries are linearized into a scratch buffer only when necessary.

Bug: 558350763

💡 Pro Tips for a Better Commit Message:

  1. Influence the Result: Want to change the output? You can write custom prompts or instructions directly in the Pull Request description. The model uses that text to generate the message.
  2. Re-run the Generator: Post a comment with: /generate-commit-message
@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor
Context State Description
CI / All Pass ⏳ PENDING Checks are running
CI / android ⏳ PENDING Checks are running
CI / evergreen ⏳ PENDING Checks are running
CI / linux ⏳ PENDING Checks are running
CI / tvos ⏳ PENDING Checks are running

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for zero-copy parsing of WebM media segments when BUILDFLAG(USE_STARBOARD_MEDIA) is enabled. It adds the WebMSegmentedBuffer interface and its implementation WebMSegmentedBufferImpl to allow parsing directly from non-contiguous stream segments, avoiding redundant copies of frame data. Feedback is provided to replace a DCHECK with a runtime parameter validation check in webm_crypto_helpers.cc to prevent a potential integer underflow vulnerability. Additionally, the newly added classes WebMSegmentedBuffer and WebMSegmentedBufferImpl should be updated with class comments documenting their purpose, lifetime, ownership, and threading model to comply with the repository style guide.

Comment thread media/formats/webm/webm_parser.h Resolved
@jasonzhangxx
jasonzhangxx marked this pull request as draft September 30, 2026 19:48
@jasonzhangxx
jasonzhangxx force-pushed the zero-copy-webm-parser branch 2 times, most recently from c4b216f to 2e05712 Compare October 5, 2026 02:13
Comment thread media/base/stream_parser_buffer.cc Resolved
@jasonzhangxx
jasonzhangxx marked this pull request as ready for review October 5, 2026 21:05
@jasonzhangxx
jasonzhangxx requested a review from xiaomings October 5, 2026 21:05

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces SegmentedByteQueue (with borrowed and owned implementations) and integrates it into the WebM stream and cluster parsers under the USE_STARBOARD_MEDIA build flag. This allows parsing of WebM media streams without redundant copying of appended data when borrow mode is enabled. Unit tests for the new queue are also added. The review feedback suggests enhancing the class comment for SegmentedByteQueue to explicitly document its expected lifetime and ownership, ensuring full compliance with the repository's style guide on class documentation.

Add SegmentedByteQueue, the parse buffer interface for the in-place MSE
stream parsers, with two implementations that
SegmentedByteQueue::Create(borrow_mode) chooses between:
- BorrowedSegmentedByteQueue holds appended byte ranges by reference
  instead of copying them. Each Push() becomes its own segment pointing
  directly at the caller's memory, kept alive by a release closure that
  is destroyed once the segment has been entirely popped, or on Reset()
  or destruction.
- OwnedSegmentedByteQueue copies every append into one ByteQueue and
  destroys the release closure before Push() returns.

Reads come in three forms, each taking an optional offset from the
front of the queue:
- PeekContiguousData() returns the run of bytes that is contiguous at an
  offset, without copying.
- PeekSegmentedData() describes a range as an ordered list of runs, so
  that data straddling a segment boundary is returned in full, also
  without copying.
- PeekLinearizedData() returns a range contiguously. The borrowed queue
  reads in place when the range lies within one segment, and otherwise
  gathers it into a scratch ByteQueue that is reused across calls and
  freed only on Reset(). The owned queue always reads in place.

The queue lives in media/formats/common, next to OffsetByteQueue, since
only the stream parsers use it, and is only built with
use_starboard_media. ByteQueue gains a const Data() so that the owned
queue's const reads can return spans.

This is the storage behind the zero-copy MSE parsers enabled by
kCobaltInPlaceMediaSourceParser. The MP4 and WebM parser changes build
on top of this and land separately.

Also add a "borrow_mode" bool param to kCobaltInPlaceMediaSourceParser,
false by default, for the stream parsers to pass to
SegmentedByteQueue::Create(). It chooses between borrowing appends,
which deviates from the MSE specification, and copying each append into
memory that the parser owns while still parsing it in place. Nothing
reads it yet, so until the parsers do, the ArrayBuffer is retained and
borrowed whenever the feature is enabled.

Bug: 558350763
TAG=agy
CONV=1df2b103-4f30-4fb5-9a48-dba07254227f

@xiaomings xiaomings left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approve so we can merge it into the upcoming release.

When kCobaltInPlaceMediaSourceParser is enabled, Blink retains the
buffer of every append and passes a release runner along with it.
WebMStreamParser then holds the stream in a SegmentedByteQueue instead
of `byte_queue_`, and parses it in place. The first append with a
release runner creates the queue with SegmentedByteQueue::Create(),
passing the feature's "borrow_mode" param, so each append is either
borrowed or copied into memory the queue owns.

The release runner must not be empty, and the two AppendToParseBuffer()
overloads must not be mixed. Both are CHECKed.

Give the WebM parsers overloads that take a SegmentedByteQueue and an
offset instead of a pointer:
- WebMParseElementHeader(), WebMListParser::Parse() and
  WebMClusterParser::Parse() read element headers in place, linearizing
  them only when they straddle a segment boundary.
- WebMClusterParser hands contiguous elements to the pointer-based code.
  For a SimpleBlock that straddles a boundary, it linearizes only the
  block header, the Opus TOC and, when encrypted, the encryption header,
  and hands the frame data to the scatter-gather
  StreamParserBuffer::CopyFrom() as segments. A straddling Block or
  BlockAdditional is gathered straight from the segments.
- WebMCreateDecryptConfig() gains an overload that takes the frame size
  separately, so it can work from just the encryption header.
- Info and Tracks are small and are linearized before parsing.

Add a scatter-gather StreamParserBuffer::CopyFrom() overload that takes
the frame data as a span of segments and forwards them to
ExternalMemoryAllocator::CopyFrom(parts, type), so a block that
straddles an append boundary is copied once, straight into the decoder
buffer.

Bug: 558350763
TAG=agy
CONV=f7ab1644-7417-41cb-a782-c47dc051b09f
@jasonzhangxx
jasonzhangxx force-pushed the zero-copy-webm-parser branch from 6dc2ca7 to a3f21e8 Compare October 8, 2026 21:58
@jasonzhangxx
jasonzhangxx merged commit 10c5e9d into youtube:main Oct 9, 2026
679 of 686 checks passed
@jasonzhangxx
jasonzhangxx deleted the zero-copy-webm-parser branch October 9, 2026 18:10
briantting pushed a commit that referenced this pull request Oct 13, 2026
Refer to original PR: #13017

When kCobaltInPlaceMediaSourceParser is enabled, Blink retains the
buffer of every append and passes a release runner along with it.
WebMStreamParser then holds the stream in a SegmentedByteQueue instead
of `byte_queue_`, and parses it in place. The first append with a
release runner creates the queue with SegmentedByteQueue::Create(),
passing the feature's "borrow_mode" param, so each append is either
borrowed or copied into memory the queue owns.

The release runner must not be empty, and the two AppendToParseBuffer()
overloads must not be mixed. Both are CHECKed.

Give the WebM parsers overloads that take a SegmentedByteQueue and an
offset instead of a pointer:
- WebMParseElementHeader(), WebMListParser::Parse() and
WebMClusterParser::Parse() read element headers in place, linearizing
them only when they straddle a segment boundary.
- WebMClusterParser hands contiguous elements to the pointer-based code.
For a SimpleBlock that straddles a boundary, it linearizes only the
block header, the Opus TOC and, when encrypted, the encryption header,
and hands the frame data to the scatter-gather
StreamParserBuffer::CopyFrom() as segments. A straddling Block or
BlockAdditional is gathered straight from the segments.
- WebMCreateDecryptConfig() gains an overload that takes the frame size
separately, so it can work from just the encryption header.
- Info and Tracks are small and are linearized before parsing.

Add a scatter-gather StreamParserBuffer::CopyFrom() overload that takes
the frame data as a span of segments and forwards them to
ExternalMemoryAllocator::CopyFrom(parts, type), so a block that
straddles an append boundary is copied once, straight into the decoder
buffer.

Bug: 558350763
TAG=agy
CONV=f7ab1644-7417-41cb-a782-c47dc051b09f

(cherry picked from commit 10c5e9d)
briantting pushed a commit that referenced this pull request Oct 13, 2026
…arser (#13319)

Automated cherry-pick roll to staging.

Original pull requests:
- #13017

CONFLICTED files:
```
media/base/byte_queue.h
```

⚠️ This PR has conflicts that must be resolved manually (don't forget to
update the `.github/AUTOROLL` file). Use the following commands to
checkout the branch locally and resolve the conflicts in a separate
commit:

```bash
gh pr checkout <pull_request_number> -R youtube/cobalt

# Resolve conflicts and update .github/AUTOROLL then
git add <resolved_files>

# Copy CONFLICTED commit's message and metadata
# Replace CONFLICTED in title with RESOLVED
git commit -c <conflicted_sha>

git push origin autoroll-main-to-staging
```

---------

Co-authored-by: Jason <jasonzh@google.com>
@oxve oxve added the cp-27.lts Cherry Pick to the 27.lts branch label Oct 13, 2026
cobalt-github-releaser-bot pushed a commit that referenced this pull request Oct 13, 2026
…arser

Refer to original PR: #13017

When kCobaltInPlaceMediaSourceParser is enabled, Blink retains the
buffer of every append and passes a release runner along with it.
WebMStreamParser then holds the stream in a SegmentedByteQueue instead
of `byte_queue_`, and parses it in place. The first append with a
release runner creates the queue with SegmentedByteQueue::Create(),
passing the feature's "borrow_mode" param, so each append is either
borrowed or copied into memory the queue owns.

The release runner must not be empty, and the two AppendToParseBuffer()
overloads must not be mixed. Both are CHECKed.

Give the WebM parsers overloads that take a SegmentedByteQueue and an
offset instead of a pointer:
- WebMParseElementHeader(), WebMListParser::Parse() and
WebMClusterParser::Parse() read element headers in place, linearizing
them only when they straddle a segment boundary.
- WebMClusterParser hands contiguous elements to the pointer-based code.
For a SimpleBlock that straddles a boundary, it linearizes only the
block header, the Opus TOC and, when encrypted, the encryption header,
and hands the frame data to the scatter-gather
StreamParserBuffer::CopyFrom() as segments. A straddling Block or
BlockAdditional is gathered straight from the segments.
- WebMCreateDecryptConfig() gains an overload that takes the frame size
separately, so it can work from just the encryption header.
- Info and Tracks are small and are linearized before parsing.

Add a scatter-gather StreamParserBuffer::CopyFrom() overload that takes
the frame data as a span of segments and forwards them to
ExternalMemoryAllocator::CopyFrom(parts, type), so a block that
straddles an append boundary is copied once, straight into the decoder
buffer.

Bug: 558350763
TAG=agy
CONV=f7ab1644-7417-41cb-a782-c47dc051b09f

(cherry picked from commit 10c5e9d)
@oxve oxve removed the cp-27.lts Cherry Pick to the 27.lts branch label Oct 13, 2026
briantting pushed a commit that referenced this pull request Oct 16, 2026
Refer to original PR: #13017

When kCobaltInPlaceMediaSourceParser is enabled, Blink retains the
buffer of every append and passes a release runner along with it.
WebMStreamParser then holds the stream in a SegmentedByteQueue instead
of `byte_queue_`, and parses it in place. The first append with a
release runner creates the queue with SegmentedByteQueue::Create(),
passing the feature's "borrow_mode" param, so each append is either
borrowed or copied into memory the queue owns.

The release runner must not be empty, and the two AppendToParseBuffer()
overloads must not be mixed. Both are CHECKed.

Give the WebM parsers overloads that take a SegmentedByteQueue and an
offset instead of a pointer:
- WebMParseElementHeader(), WebMListParser::Parse() and
WebMClusterParser::Parse() read element headers in place, linearizing
them only when they straddle a segment boundary.
- WebMClusterParser hands contiguous elements to the pointer-based code.
For a SimpleBlock that straddles a boundary, it linearizes only the
block header, the Opus TOC and, when encrypted, the encryption header,
and hands the frame data to the scatter-gather
StreamParserBuffer::CopyFrom() as segments. A straddling Block or
BlockAdditional is gathered straight from the segments.
- WebMCreateDecryptConfig() gains an overload that takes the frame size
separately, so it can work from just the encryption header.
- Info and Tracks are small and are linearized before parsing.

Add a scatter-gather StreamParserBuffer::CopyFrom() overload that takes
the frame data as a span of segments and forwards them to
ExternalMemoryAllocator::CopyFrom(parts, type), so a block that
straddles an append boundary is copied once, straight into the decoder
buffer.

Bug: 558350763
TAG=agy
CONV=f7ab1644-7417-41cb-a782-c47dc051b09f

(cherry picked from commit 10c5e9d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants