| 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 | #include "av1_bitstream.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | |
| 21 | namespace livekit { |
| 22 | namespace av1 { |
| 23 | namespace { |
| 24 | |
| 25 | constexpr uint8_t kObuSizePresentBit = 0b0'0000'010; |
| 26 | constexpr int kObuTypeSequenceHeader = 1; |
| 27 | constexpr int kObuTypeTemporalDelimiter = 2; |
| 28 | constexpr int kObuTypeTileList = 8; |
| 29 | constexpr int kObuTypePadding = 15; |
| 30 | |
| 31 | bool ObuHasExtension(uint8_t obu_header) { |
| 32 | return (obu_header & 0b0'0000'100) != 0; |
| 33 | } |
| 34 | |
| 35 | bool ObuHasSize(uint8_t obu_header) { |
| 36 | return (obu_header & kObuSizePresentBit) != 0; |
| 37 | } |
| 38 | |
| 39 | int ObuType(uint8_t obu_header) { |
| 40 | return (obu_header & 0b0'1111'000) >> 3; |
| 41 | } |
| 42 | |
| 43 | bool ReadLeb128(const uint8_t* data, size_t len, size_t* offset, uint64_t* value) { |
| 44 | if (!data || !offset || !value || *offset >= len) { |
| 45 | return false; |
| 46 | } |
| 47 | uint64_t result = 0; |
| 48 | int shift = 0; |
| 49 | while (*offset < len) { |
| 50 | const uint8_t byte = data[(*offset)++]; |
| 51 | result |= static_cast<uint64_t>(byte & 0x7F) << shift; |
| 52 | if ((byte & 0x80) == 0) { |
| 53 | *value = result; |
| 54 | return true; |
| 55 | } |
| 56 | shift += 7; |
| 57 | if (shift > 56) { |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | bool ShouldTransferObu(int obu_type) { |
| 65 | return obu_type != kObuTypeTemporalDelimiter && obu_type != kObuTypeTileList && |
| 66 | obu_type != kObuTypePadding; |
| 67 | } |
| 68 | |
| 69 | uint32_t ReadLittleEndianUint32(const std::vector<uint8_t>& data) { |
| 70 | if (data.size() < 4) { |
| 71 | return 0; |
| 72 | } |
| 73 | return static_cast<uint32_t>(data[0]) | |
| 74 | (static_cast<uint32_t>(data[1]) << 8) | |
| 75 | (static_cast<uint32_t>(data[2]) << 16) | |
| 76 | (static_cast<uint32_t>(data[3]) << 24); |
| 77 | } |
| 78 | |
| 79 | bool StripIvfFrameHeader(std::vector<uint8_t>* packet) { |
| 80 | if (!packet || packet->size() < 12) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | const uint32_t declared_size = ReadLittleEndianUint32(*packet); |
| 85 | // A standalone IVF frame header must describe exactly the remaining buffer. |
| 86 | // Otherwise valid low-overhead OBUs can be mistaken for a length prefix. |
| 87 | if (declared_size == 0 || declared_size != packet->size() - 12) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | const uint8_t* payload = packet->data() + 12; |
| 92 | if (ParseObus(payload, declared_size).empty()) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | packet->erase(packet->begin(), packet->begin() + 12); |
| 97 | packet->resize(declared_size); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool ReadBoundedLeb128(const uint8_t* data, |
| 102 | size_t end, |
| 103 | size_t* offset, |
| 104 | uint64_t* value) { |
| 105 | const size_t before = offset ? *offset : 0; |
| 106 | if (!ReadLeb128(data, end, offset, value)) { |
| 107 | return false; |
| 108 | } |
| 109 | return offset && *offset > before && *offset <= end; |
| 110 | } |
| 111 | |
| 112 | bool IsCompleteObu(const uint8_t* data, size_t len); |
| 113 | |
| 114 | // Annex-B wraps one temporal unit in a LEB128 size, then frame units, then |
| 115 | // length-delimited OBUs. WebRTC's RTP packetizer wants the OBU bytes only. |
| 116 | bool AppendAnnexBObus(const uint8_t* data, |
| 117 | size_t len, |
| 118 | std::vector<uint8_t>* low_overhead) { |
| 119 | if (!data || len == 0 || !low_overhead) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | size_t offset = 0; |
| 124 | uint64_t temporal_unit_size = 0; |
| 125 | if (!ReadBoundedLeb128(data, len, &offset, &temporal_unit_size) || |
| 126 | temporal_unit_size == 0 || temporal_unit_size != len - offset) { |
| 127 | return false; |
| 128 | } |
| 129 | const size_t temporal_unit_end = |
| 130 | offset + static_cast<size_t>(temporal_unit_size); |
| 131 | |
| 132 | while (offset < temporal_unit_end) { |
| 133 | uint64_t frame_unit_size = 0; |
| 134 | if (!ReadBoundedLeb128(data, temporal_unit_end, &offset, &frame_unit_size) || |
| 135 | frame_unit_size == 0 || frame_unit_size > temporal_unit_end - offset) { |
| 136 | return false; |
| 137 | } |
| 138 | const size_t frame_unit_end = offset + static_cast<size_t>(frame_unit_size); |
| 139 | |
| 140 | while (offset < frame_unit_end) { |
| 141 | uint64_t obu_size = 0; |
| 142 | if (!ReadBoundedLeb128(data, frame_unit_end, &offset, &obu_size) || |
| 143 | obu_size == 0 || obu_size > frame_unit_end - offset) { |
| 144 | return false; |
| 145 | } |
| 146 | const size_t obu_start = offset; |
| 147 | const size_t obu_end = offset + static_cast<size_t>(obu_size); |
| 148 | if (!IsCompleteObu(data + obu_start, static_cast<size_t>(obu_size))) { |
| 149 | return false; |
| 150 | } |
| 151 | low_overhead->insert(low_overhead->end(), data + obu_start, |
| 152 | data + obu_end); |
| 153 | offset = obu_end; |
| 154 | } |
| 155 | |
| 156 | if (offset != frame_unit_end) { |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return offset == len && !low_overhead->empty() && |
| 162 | !ParseObus(low_overhead->data(), low_overhead->size()).empty(); |
| 163 | } |
| 164 | |
| 165 | bool IsCompleteObu(const uint8_t* data, size_t len) { |
| 166 | if (!data || len == 0) { |
| 167 | return false; |
| 168 | } |
| 169 | size_t offset = 0; |
| 170 | const uint8_t header = data[offset++]; |
| 171 | if (ObuHasExtension(header)) { |
| 172 | if (offset >= len) { |
| 173 | return false; |
| 174 | } |
| 175 | ++offset; |
| 176 | } |
| 177 | if (ObuHasSize(header)) { |
| 178 | uint64_t payload_size = 0; |
| 179 | if (!ReadLeb128(data, len, &offset, &payload_size) || |
| 180 | payload_size > len - offset) { |
| 181 | return false; |
| 182 | } |
| 183 | offset += static_cast<size_t>(payload_size); |
| 184 | } else { |
| 185 | offset = len; |
| 186 | } |
| 187 | return offset == len; |
| 188 | } |
| 189 | |
| 190 | bool ConvertAnnexBToLowOverhead(std::vector<uint8_t>* packet) { |
| 191 | if (!packet || packet->empty()) { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | std::vector<uint8_t> low_overhead; |
| 196 | low_overhead.reserve(packet->size()); |
| 197 | if (!AppendAnnexBObus(packet->data(), packet->size(), &low_overhead)) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | packet->swap(low_overhead); |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | bool StripNonTransferObus(std::vector<uint8_t>* packet) { |
| 206 | if (!packet || packet->empty()) { |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | const std::vector<ObuSpan> obus = ParseObus(packet->data(), packet->size()); |
| 211 | if (obus.empty()) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | size_t transfer_size = 0; |
| 216 | bool already_contiguous = true; |
| 217 | size_t next_offset = 0; |
| 218 | for (const ObuSpan& obu : obus) { |
| 219 | transfer_size += obu.total_size; |
| 220 | already_contiguous = already_contiguous && obu.offset == next_offset; |
| 221 | next_offset = obu.offset + obu.total_size; |
| 222 | } |
| 223 | |
| 224 | if (transfer_size == packet->size() && already_contiguous) { |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | std::vector<uint8_t> filtered; |
| 229 | filtered.reserve(transfer_size); |
| 230 | for (const ObuSpan& obu : obus) { |
| 231 | filtered.insert(filtered.end(), packet->begin() + obu.offset, |
| 232 | packet->begin() + obu.offset + obu.total_size); |
| 233 | } |
| 234 | packet->swap(filtered); |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | } // namespace |
| 239 | |
| 240 | std::vector<ObuSpan> ParseObus(const uint8_t* data, size_t len) { |
| 241 | std::vector<ObuSpan> result; |
| 242 | if (!data || len == 0) { |
| 243 | return result; |
| 244 | } |
| 245 | |
| 246 | size_t offset = 0; |
| 247 | while (offset < len) { |
| 248 | ObuSpan obu; |
| 249 | obu.offset = offset; |
| 250 | const uint8_t header = data[offset++]; |
| 251 | obu.type = ObuType(header); |
| 252 | obu.has_size_field = ObuHasSize(header); |
| 253 | |
| 254 | if (ObuHasExtension(header)) { |
| 255 | if (offset >= len) { |
| 256 | return {}; |
| 257 | } |
| 258 | ++offset; |
| 259 | } |
| 260 | |
| 261 | if (obu.has_size_field) { |
| 262 | uint64_t payload_size = 0; |
| 263 | if (!ReadLeb128(data, len, &offset, &payload_size) || |
| 264 | payload_size > len - offset) { |
| 265 | return {}; |
| 266 | } |
| 267 | offset += static_cast<size_t>(payload_size); |
| 268 | } else { |
| 269 | offset = len; |
| 270 | } |
| 271 | |
| 272 | obu.total_size = offset - obu.offset; |
| 273 | if (ShouldTransferObu(obu.type)) { |
| 274 | result.push_back(obu); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return result; |
| 279 | } |
| 280 | |
| 281 | bool HasSequenceHeaderObu(const uint8_t* data, size_t len) { |
| 282 | for (const ObuSpan& obu : ParseObus(data, len)) { |
| 283 | if (obu.type == kObuTypeSequenceHeader) { |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | bool ExtractSequenceHeaderObu(const uint8_t* data, |
| 291 | size_t len, |
| 292 | std::vector<uint8_t>* out) { |
| 293 | if (!out) { |
| 294 | return false; |
| 295 | } |
| 296 | for (const ObuSpan& obu : ParseObus(data, len)) { |
| 297 | if (obu.type != kObuTypeSequenceHeader) { |
| 298 | continue; |
| 299 | } |
| 300 | if (obu.offset + obu.total_size > len) { |
| 301 | return false; |
| 302 | } |
| 303 | out->assign(data + obu.offset, data + obu.offset + obu.total_size); |
| 304 | return true; |
| 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | void EnsureSequenceHeaderOnKeyframe( |
| 310 | std::vector<uint8_t>* packet, |
| 311 | const std::vector<uint8_t>& cached_seq_header) { |
| 312 | if (!packet || packet->empty() || cached_seq_header.empty()) { |
| 313 | return; |
| 314 | } |
| 315 | if (HasSequenceHeaderObu(packet->data(), packet->size())) { |
| 316 | return; |
| 317 | } |
| 318 | std::vector<uint8_t> merged; |
| 319 | merged.reserve(cached_seq_header.size() + packet->size()); |
| 320 | merged.insert(merged.end(), cached_seq_header.begin(), cached_seq_header.end()); |
| 321 | merged.insert(merged.end(), packet->begin(), packet->end()); |
| 322 | packet->swap(merged); |
| 323 | } |
| 324 | |
| 325 | void StripIvfFrameHeaderIfPresent(std::vector<uint8_t>* packet) { |
| 326 | if (!packet || packet->size() < 12) { |
| 327 | return; |
| 328 | } |
| 329 | // IVF file header starts with the "DKIF" signature. Per-frame IVF headers do |
| 330 | // not include that signature and are not valid OBU streams for WebRTC. |
| 331 | if (packet->size() >= 32 && packet->at(0) == 'D' && packet->at(1) == 'K' && |
| 332 | packet->at(2) == 'I' && packet->at(3) == 'F') { |
| 333 | if (packet->size() <= 32) { |
| 334 | packet->clear(); |
| 335 | return; |
| 336 | } |
| 337 | packet->erase(packet->begin(), packet->begin() + 32); |
| 338 | } |
| 339 | StripIvfFrameHeader(packet); |
| 340 | } |
| 341 | |
| 342 | void ConvertAnnexBToLowOverheadIfPresent(std::vector<uint8_t>* packet) { |
| 343 | ConvertAnnexBToLowOverhead(packet); |
| 344 | } |
| 345 | |
| 346 | void StripNonTransferObusIfPresent(std::vector<uint8_t>* packet) { |
| 347 | StripNonTransferObus(packet); |
| 348 | } |
| 349 | |
| 350 | void NormalizeForRtp(std::vector<uint8_t>* packet) { |
| 351 | StripIvfFrameHeaderIfPresent(packet); |
| 352 | ConvertAnnexBToLowOverheadIfPresent(packet); |
| 353 | StripNonTransferObusIfPresent(packet); |
| 354 | } |
| 355 | |
| 356 | bool IsWebRtcParseable(const uint8_t* data, size_t len) { |
| 357 | if (!data || len == 0) { |
| 358 | return false; |
| 359 | } |
| 360 | const std::vector<ObuSpan> obus = ParseObus(data, len); |
| 361 | return !obus.empty(); |
| 362 | } |
| 363 | |
| 364 | } // namespace av1 |
| 365 | } // namespace livekit |