| 1 | /* |
| 2 | * Copyright 2026 LiveKit, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef WEBRTC_AV1_BITSTREAM_H_ |
| 18 | #define WEBRTC_AV1_BITSTREAM_H_ |
| 19 | |
| 20 | #include <cstddef> |
| 21 | #include <cstdint> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace livekit { |
| 25 | namespace av1 { |
| 26 | |
| 27 | /// Parsed span of a single AV1 OBU inside a low-overhead bitstream. |
| 28 | struct ObuSpan { |
| 29 | size_t offset = 0; |
| 30 | size_t total_size = 0; |
| 31 | int type = -1; |
| 32 | bool has_size_field = false; |
| 33 | }; |
| 34 | |
| 35 | /// Parse AV1 OBUs using the same rules as WebRTC's `RtpPacketizerAv1`. |
| 36 | /// Returns an empty vector when the bitstream is malformed. |
| 37 | std::vector<ObuSpan> ParseObus(const uint8_t* data, size_t len); |
| 38 | |
| 39 | /// Returns true when the bitstream contains an `OBU_SEQUENCE_HEADER`. |
| 40 | bool HasSequenceHeaderObu(const uint8_t* data, size_t len); |
| 41 | |
| 42 | /// Extract the first sequence-header OBU bytes, if present. |
| 43 | bool ExtractSequenceHeaderObu(const uint8_t* data, |
| 44 | size_t len, |
| 45 | std::vector<uint8_t>* out); |
| 46 | |
| 47 | /// Prepend a cached sequence-header OBU to a keyframe when the encoder omitted it. |
| 48 | void EnsureSequenceHeaderOnKeyframe(std::vector<uint8_t>* packet, |
| 49 | const std::vector<uint8_t>& cached_seq_header); |
| 50 | |
| 51 | /// Strip a per-frame IVF container header when present. |
| 52 | void StripIvfFrameHeaderIfPresent(std::vector<uint8_t>* packet); |
| 53 | |
| 54 | /// Convert AV1 Annex-B temporal/frame/OBU units to low-overhead OBUs when |
| 55 | /// present. |
| 56 | void ConvertAnnexBToLowOverheadIfPresent(std::vector<uint8_t>* packet); |
| 57 | |
| 58 | /// Strip OBUs that should not be transferred in WebRTC RTP payloads when present. |
| 59 | void StripNonTransferObusIfPresent(std::vector<uint8_t>* packet); |
| 60 | |
| 61 | /// Normalizes an AV1 temporal unit for WebRTC RTP packetization: strips IVF |
| 62 | /// framing, converts Annex-B units to low-overhead OBUs, and strips |
| 63 | /// non-transfer OBUs. Shared by every encoder that emits AV1 into the RTP |
| 64 | /// pipeline so the steps cannot drift apart. |
| 65 | void NormalizeForRtp(std::vector<uint8_t>* packet); |
| 66 | |
| 67 | /// Basic validation that WebRTC's AV1 packetizer can parse the bitstream. |
| 68 | bool IsWebRtcParseable(const uint8_t* data, size_t len); |
| 69 | |
| 70 | } // namespace av1 |
| 71 | } // namespace livekit |
| 72 | |
| 73 | #endif // WEBRTC_AV1_BITSTREAM_H_ |