Repository navigation
Add LengthPrefixedAccessUnitParser for length-prefixed H.264/H.265 #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
haffi96
wants to merge
1
commit into
livekit:main
Choose a base branch
from
haffi96:feat/h265-pre-encoded-length-prefixed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+627
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Copyright 2026 LiveKit | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an “AS IS” BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <vector> | ||
|
|
||
| #include "livekit/encoded_video_source.h" | ||
| #include "livekit/video_codec.h" | ||
| #include "livekit/visibility.h" | ||
|
|
||
| namespace livekit { | ||
|
|
||
| /// @brief Splits a length-prefixed H.264 (AVC) or H.265 (HVCC) NAL unit | ||
| /// stream into access units ready for EncodedVideoSource::captureFrame. | ||
| /// | ||
| /// This is the framing of `stream-format=avc3` (H.264) and `hev1` (H.265), | ||
| /// with parameter sets in-band. Each access unit is converted to Annex-B, | ||
| /// which the pre-encoded passthrough requires. | ||
| /// | ||
| /// Push bytes as they arrive (any chunking), then drain complete access | ||
| /// units with next(). Call flush() once at the end of the stream. Not | ||
| /// thread-safe. | ||
| class LIVEKIT_API LengthPrefixedAccessUnitParser final { | ||
| public: | ||
| struct Options { | ||
| /// Size of each big-endian NAL length prefix, 1 to 4 bytes | ||
| /// (`lengthSizeMinusOne + 1` from `avcC`/`hvcC`; 4 for most encoders). | ||
| std::uint8_t nal_length_size = 4; | ||
| /// Complete each access unit as soon as its first slice arrives, instead | ||
| /// of when the next access unit begins. | ||
| /// | ||
| /// The end of an access unit is otherwise only known from the NAL that | ||
| /// starts the next one, which holds every picture for a full frame | ||
| /// interval unless the encoder terminates access units with an AUD. | ||
| /// Enable this only for streams with one slice per picture, which is | ||
| /// what hardware encoders typically emit. | ||
| bool single_slice_flush = false; | ||
| /// Fill Frame::metadata from an LKTS `user_data_unregistered` SEI ahead | ||
| /// of the slice (the packet-trailer convention of the LiveKit Go SDK). | ||
| bool frame_metadata_from_sei = false; | ||
| }; | ||
|
|
||
| /// @throws std::invalid_argument if the codec is not H.264 or H.265, or | ||
| /// nal_length_size is not 1 to 4. | ||
| explicit LengthPrefixedAccessUnitParser(VideoCodec codec); | ||
| /// @throws std::invalid_argument if the codec is not H.264 or H.265, or | ||
| /// nal_length_size is not 1 to 4. | ||
| LengthPrefixedAccessUnitParser(VideoCodec codec, Options options); | ||
|
|
||
| /// @brief Appends stream bytes. | ||
| void push(const std::uint8_t* data, std::size_t size); | ||
|
|
||
| /// @brief Returns the next complete access unit, if one is buffered. | ||
| /// | ||
| /// Frame::width/height are left at zero (the source resolution) and | ||
| /// Frame::timestamp_us at zero (capture time). | ||
| /// @throws std::runtime_error on malformed input, or when an access unit | ||
| /// exceeds 32 MiB. | ||
| std::optional<EncodedVideoSource::Frame> next(); | ||
|
|
||
| /// @brief Returns the remaining buffered NAL units as the final access unit. | ||
| /// Call repeatedly until it returns nullopt. | ||
| /// @throws std::runtime_error on a truncated final NAL unit. | ||
| std::optional<EncodedVideoSource::Frame> flush(); | ||
|
|
||
| private: | ||
| struct NalRange { | ||
| std::size_t start; | ||
| std::size_t end; | ||
| }; | ||
|
|
||
| void scanPending(); | ||
| std::optional<std::size_t> splitIndex() const; | ||
| EncodedVideoSource::Frame takeAccessUnit(std::size_t nal_count); | ||
|
|
||
| VideoCodec codec_; | ||
| Options options_; | ||
| std::vector<std::uint8_t> pending_; | ||
| // Complete NAL units of the access unit being assembled, as payload | ||
| // ranges (after the length prefix) into pending_. | ||
| std::vector<NalRange> nals_; | ||
| // Offset of the first unparsed length prefix in pending_. | ||
| std::size_t scan_cursor_ = 0; | ||
| }; | ||
|
|
||
| } // namespace livekit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Public parser omits required API tags
The new
Optionsstruct lacks@brief; constructors lack@param, andnext()andflush()lack@return. These omissions violate the repository's public API documentation rules.Learn more
The repository's public API documentation rules require
@brieffor structs and methods,@paramfor parameters, and@returnfor non-void methods. The newOptionsstruct has no@brief. Both constructors lack@briefand@param,pushlacks@param, andnext/flushlack@return.Example: A consumer viewing the generated reference for
pushcannot find the required documentation for itsdataorsizearguments.Recommended fix: Add the missing Doxygen tags to each new public declaration, including both constructor overloads and the
pusharguments.Was this helpful? React with 👍 or 👎 to provide feedback.