| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "call/rtp_payload_params.h" |
| 12 |
| 13 | #include <stddef.h> |
| 14 |
| 15 | #include <algorithm> |
| 16 |
| 17 | #include "absl/container/inlined_vector.h" |
| 18 | #include "absl/strings/match.h" |
| 19 | #include "absl/types/variant.h" |
| 20 | #include "api/video/video_timing.h" |
| 21 | #include "modules/video_coding/codecs/h264/include/h264_globals.h" |
| 22 | #include "modules/video_coding/codecs/interface/common_constants.h" |
| 23 | #include "modules/video_coding/codecs/vp8/include/vp8_globals.h" |
| 24 | #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" |
| 25 | #include "modules/video_coding/frame_dependencies_calculator.h" |
| 26 | #include "rtc_base/arraysize.h" |
| 27 | #include "rtc_base/checks.h" |
| 28 | #include "rtc_base/logging.h" |
| 29 | #include "rtc_base/random.h" |
| 30 | #include "rtc_base/time_utils.h" |
| 31 |
| 32 | namespace webrtc { |
| 33 | namespace { |
| 34 |
| 35 | constexpr int kMaxSimulatedSpatialLayers = 3; |
| 36 |
| 37 | void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info, |
| 38 | absl::optional<int> spatial_index, |
| 39 | RTPVideoHeader* rtp) { |
| 40 | rtp->codec = info.codecType; |
| 41 | rtp->is_last_frame_in_picture = info.end_of_picture; |
| 42 | switch (info.codecType) { |
| 43 | case kVideoCodecVP8: { |
| 44 | auto& vp8_header = rtp->video_type_header.emplace<RTPVideoHeaderVP8>(); |
| 45 | vp8_header.InitRTPVideoHeaderVP8(); |
| 46 | vp8_header.nonReference = info.codecSpecific.VP8.nonReference; |
| 47 | vp8_header.temporalIdx = info.codecSpecific.VP8.temporalIdx; |
| 48 | vp8_header.layerSync = info.codecSpecific.VP8.layerSync; |
| 49 | vp8_header.keyIdx = info.codecSpecific.VP8.keyIdx; |
| 50 | return; |
| 51 | } |
| 52 | case kVideoCodecVP9: { |
| 53 | auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>(); |
| 54 | vp9_header.InitRTPVideoHeaderVP9(); |
| 55 | vp9_header.inter_pic_predicted = |
| 56 | info.codecSpecific.VP9.inter_pic_predicted; |
| 57 | vp9_header.flexible_mode = info.codecSpecific.VP9.flexible_mode; |
| 58 | vp9_header.ss_data_available = info.codecSpecific.VP9.ss_data_available; |
| 59 | vp9_header.non_ref_for_inter_layer_pred = |
| 60 | info.codecSpecific.VP9.non_ref_for_inter_layer_pred; |
| 61 | vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx; |
| 62 | vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch; |
| 63 | vp9_header.inter_layer_predicted = |
| 64 | info.codecSpecific.VP9.inter_layer_predicted; |
| 65 | vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx; |
| 66 | vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers; |
| 67 | vp9_header.first_active_layer = info.codecSpecific.VP9.first_active_layer; |
| 68 | if (vp9_header.num_spatial_layers > 1) { |
| 69 | vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx); |
| 70 | } else { |
| 71 | vp9_header.spatial_idx = kNoSpatialIdx; |
| 72 | } |
| 73 | if (info.codecSpecific.VP9.ss_data_available) { |
| 74 | vp9_header.spatial_layer_resolution_present = |
| 75 | info.codecSpecific.VP9.spatial_layer_resolution_present; |
| 76 | if (info.codecSpecific.VP9.spatial_layer_resolution_present) { |
| 77 | for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers; |
| 78 | ++i) { |
| 79 | vp9_header.width[i] = info.codecSpecific.VP9.width[i]; |
| 80 | vp9_header.height[i] = info.codecSpecific.VP9.height[i]; |
| 81 | } |
| 82 | } |
| 83 | vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof); |
| 84 | } |
| 85 |
| 86 | vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics; |
| 87 | for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) { |
| 88 | vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i]; |
| 89 | } |
| 90 | vp9_header.end_of_picture = info.end_of_picture; |
| 91 | return; |
| 92 | } |
| 93 | case kVideoCodecH264: { |
| 94 | auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>(); |
| 95 | h264_header.packetization_mode = |
| 96 | info.codecSpecific.H264.packetization_mode; |
| 97 | return; |
| 98 | } |
| 99 | case kVideoCodecH265: { |
| 100 | auto& h265_header = rtp->video_type_header.emplace<RTPVideoHeaderH265>(); |
| 101 | h265_header.packetization_mode = |
| 102 | info.codecSpecific.H265.packetization_mode; |
| 103 | } |
| 104 | return; |
| 105 | case kVideoCodecMultiplex: |
| 106 | case kVideoCodecGeneric: |
| 107 | rtp->codec = kVideoCodecGeneric; |
| 108 | return; |
| 109 | default: |
| 110 | return; |
| 111 | } |
| 112 | } |
| 113 |
| 114 | void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) { |
| 115 | if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid || |
| 116 | image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) { |
| 117 | timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid; |
| 118 | return; |
| 119 | } |
| 120 |
| 121 | timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs( |
| 122 | image.capture_time_ms_, image.timing_.encode_start_ms); |
| 123 | timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs( |
| 124 | image.capture_time_ms_, image.timing_.encode_finish_ms); |
| 125 | timing->packetization_finish_delta_ms = 0; |
| 126 | timing->pacer_exit_delta_ms = 0; |
| 127 | timing->network_timestamp_delta_ms = 0; |
| 128 | timing->network2_timestamp_delta_ms = 0; |
| 129 | timing->flags = image.timing_.flags; |
| 130 | } |
| 131 |
| 132 | // Returns structure that aligns with simulated generic info. The templates |
| 133 | // allow to produce valid dependency descriptor for any stream where |
| 134 | // `num_spatial_layers` * `num_temporal_layers` <= 32 (limited by |
| 135 | // https://aomediacodec.github.io/av1-rtp-spec/#a82-syntax, see |
| 136 | // template_fdiffs()). The set of the templates is not tuned for any paricular |
| 137 | // structure thus dependency descriptor would use more bytes on the wire than |
| 138 | // with tuned templates. |
| 139 | FrameDependencyStructure MinimalisticStructure(int num_spatial_layers, |
| 140 | int num_temporal_layers) { |
| 141 | RTC_DCHECK_LE(num_spatial_layers, DependencyDescriptor::kMaxSpatialIds); |
| 142 | RTC_DCHECK_LE(num_temporal_layers, DependencyDescriptor::kMaxTemporalIds); |
| 143 | RTC_DCHECK_LE(num_spatial_layers * num_temporal_layers, 32); |
| 144 | FrameDependencyStructure structure; |
| 145 | structure.num_decode_targets = num_spatial_layers * num_temporal_layers; |
| 146 | structure.num_chains = num_spatial_layers; |
| 147 | structure.templates.reserve(num_spatial_layers * num_temporal_layers); |
| 148 | for (int sid = 0; sid < num_spatial_layers; ++sid) { |
| 149 | for (int tid = 0; tid < num_temporal_layers; ++tid) { |
| 150 | FrameDependencyTemplate a_template; |
| 151 | a_template.spatial_id = sid; |
| 152 | a_template.temporal_id = tid; |
| 153 | for (int s = 0; s < num_spatial_layers; ++s) { |
| 154 | for (int t = 0; t < num_temporal_layers; ++t) { |
| 155 | // Prefer kSwitch indication for frames that is part of the decode |
| 156 | // target because dependency descriptor information generated in this |
| 157 | // class use kSwitch indications more often that kRequired, increasing |
| 158 | // the chance of a good (or complete) template match. |
| 159 | a_template.decode_target_indications.push_back( |
| 160 | sid <= s && tid <= t ? DecodeTargetIndication::kSwitch |
| 161 | : DecodeTargetIndication::kNotPresent); |
| 162 | } |
| 163 | } |
| 164 | a_template.frame_diffs.push_back(tid == 0 ? num_spatial_layers * |
| 165 | num_temporal_layers |
| 166 | : num_spatial_layers); |
| 167 | a_template.chain_diffs.assign(structure.num_chains, 1); |
| 168 | structure.templates.push_back(a_template); |
| 169 |
| 170 | structure.decode_target_protected_by_chain.push_back(sid); |
| 171 | } |
| 172 | } |
| 173 | return structure; |
| 174 | } |
| 175 | } // namespace |
| 176 |
| 177 | RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc, |
| 178 | const RtpPayloadState* state, |
| 179 | const FieldTrialsView& trials) |
| 180 | : ssrc_(ssrc), |
| 181 | generic_picture_id_experiment_( |
| 182 | absl::StartsWith(trials.Lookup("WebRTC-GenericPictureId"), |
| 183 | "Enabled")), |
| 184 | simulate_generic_structure_(absl::StartsWith( |
| 185 | trials.Lookup("WebRTC-GenericCodecDependencyDescriptor"), |
| 186 | "Enabled")) { |
| 187 | for (auto& spatial_layer : last_shared_frame_id_) |
| 188 | spatial_layer.fill(-1); |
| 189 |
| 190 | chain_last_frame_id_.fill(-1); |
| 191 | buffer_id_to_frame_id_.fill(-1); |
| 192 |
| 193 | Random random(rtc::TimeMicros()); |
| 194 | state_.picture_id = |
| 195 | state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF); |
| 196 | state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>()); |
| 197 | } |
| 198 |
| 199 | RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default; |
| 200 |
| 201 | RtpPayloadParams::~RtpPayloadParams() {} |
| 202 |
| 203 | RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader( |
| 204 | const EncodedImage& image, |
| 205 | const CodecSpecificInfo* codec_specific_info, |
| 206 | int64_t shared_frame_id) { |
| 207 | RTPVideoHeader rtp_video_header; |
| 208 | if (codec_specific_info) { |
| 209 | PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(), |
| 210 | &rtp_video_header); |
| 211 | } |
| 212 | rtp_video_header.simulcastIdx = image.SimulcastIndex().value_or(0); |
| 213 | rtp_video_header.frame_type = image._frameType; |
| 214 | rtp_video_header.rotation = image.rotation_; |
| 215 | rtp_video_header.content_type = image.content_type_; |
| 216 | rtp_video_header.playout_delay = image.PlayoutDelay(); |
| 217 | rtp_video_header.width = image._encodedWidth; |
| 218 | rtp_video_header.height = image._encodedHeight; |
| 219 | rtp_video_header.color_space = image.ColorSpace() |
| 220 | ? absl::make_optional(*image.ColorSpace()) |
| 221 | : absl::nullopt; |
| 222 | rtp_video_header.video_frame_tracking_id = image.VideoFrameTrackingId(); |
| 223 | SetVideoTiming(image, &rtp_video_header.video_timing); |
| 224 |
| 225 | const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey; |
| 226 | const bool first_frame_in_picture = |
| 227 | (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9) |
| 228 | ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture |
| 229 | : true; |
| 230 |
| 231 | SetCodecSpecific(&rtp_video_header, first_frame_in_picture); |
| 232 |
| 233 | SetGeneric(codec_specific_info, shared_frame_id, is_keyframe, |
| 234 | &rtp_video_header); |
| 235 |
| 236 | return rtp_video_header; |
| 237 | } |
| 238 |
| 239 | uint32_t RtpPayloadParams::ssrc() const { |
| 240 | return ssrc_; |
| 241 | } |
| 242 |
| 243 | RtpPayloadState RtpPayloadParams::state() const { |
| 244 | return state_; |
| 245 | } |
| 246 |
| 247 | void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header, |
| 248 | bool first_frame_in_picture) { |
| 249 | // Always set picture id. Set tl0_pic_idx iff temporal index is set. |
| 250 | if (first_frame_in_picture) { |
| 251 | state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF; |
| 252 | } |
| 253 | if (rtp_video_header->codec == kVideoCodecVP8) { |
| 254 | auto& vp8_header = |
| 255 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 256 | vp8_header.pictureId = state_.picture_id; |
| 257 |
| 258 | if (vp8_header.temporalIdx != kNoTemporalIdx) { |
| 259 | if (vp8_header.temporalIdx == 0) { |
| 260 | ++state_.tl0_pic_idx; |
| 261 | } |
| 262 | vp8_header.tl0PicIdx = state_.tl0_pic_idx; |
| 263 | } |
| 264 | } |
| 265 | if (rtp_video_header->codec == kVideoCodecVP9) { |
| 266 | auto& vp9_header = |
| 267 | absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header); |
| 268 | vp9_header.picture_id = state_.picture_id; |
| 269 |
| 270 | // Note that in the case that we have no temporal layers but we do have |
| 271 | // spatial layers, packets will carry layering info with a temporal_idx of |
| 272 | // zero, and we then have to set and increment tl0_pic_idx. |
| 273 | if (vp9_header.temporal_idx != kNoTemporalIdx || |
| 274 | vp9_header.spatial_idx != kNoSpatialIdx) { |
| 275 | if (first_frame_in_picture && |
| 276 | (vp9_header.temporal_idx == 0 || |
| 277 | vp9_header.temporal_idx == kNoTemporalIdx)) { |
| 278 | ++state_.tl0_pic_idx; |
| 279 | } |
| 280 | vp9_header.tl0_pic_idx = state_.tl0_pic_idx; |
| 281 | } |
| 282 | } |
| 283 | if (generic_picture_id_experiment_ && |
| 284 | rtp_video_header->codec == kVideoCodecGeneric) { |
| 285 | rtp_video_header->video_type_header.emplace<RTPVideoHeaderLegacyGeneric>() |
| 286 | .picture_id = state_.picture_id; |
| 287 | } |
| 288 | } |
| 289 |
| 290 | RTPVideoHeader::GenericDescriptorInfo |
| 291 | RtpPayloadParams::GenericDescriptorFromFrameInfo( |
| 292 | const GenericFrameInfo& frame_info, |
| 293 | int64_t frame_id) { |
| 294 | RTPVideoHeader::GenericDescriptorInfo generic; |
| 295 | generic.frame_id = frame_id; |
| 296 | generic.dependencies = dependencies_calculator_.FromBuffersUsage( |
| 297 | frame_id, frame_info.encoder_buffers); |
| 298 | generic.chain_diffs = |
| 299 | chains_calculator_.From(frame_id, frame_info.part_of_chain); |
| 300 | generic.spatial_index = frame_info.spatial_id; |
| 301 | generic.temporal_index = frame_info.temporal_id; |
| 302 | generic.decode_target_indications = frame_info.decode_target_indications; |
| 303 | generic.active_decode_targets = frame_info.active_decode_targets; |
| 304 | return generic; |
| 305 | } |
| 306 |
| 307 | void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info, |
| 308 | int64_t frame_id, |
| 309 | bool is_keyframe, |
| 310 | RTPVideoHeader* rtp_video_header) { |
| 311 | if (codec_specific_info && codec_specific_info->generic_frame_info && |
| 312 | !codec_specific_info->generic_frame_info->encoder_buffers.empty()) { |
| 313 | if (is_keyframe) { |
| 314 | // Key frame resets all chains it is in. |
| 315 | chains_calculator_.Reset( |
| 316 | codec_specific_info->generic_frame_info->part_of_chain); |
| 317 | } |
| 318 | rtp_video_header->generic = GenericDescriptorFromFrameInfo( |
| 319 | *codec_specific_info->generic_frame_info, frame_id); |
| 320 | return; |
| 321 | } |
| 322 |
| 323 | switch (rtp_video_header->codec) { |
| 324 | case VideoCodecType::kVideoCodecGeneric: |
| 325 | GenericToGeneric(frame_id, is_keyframe, rtp_video_header); |
| 326 | return; |
| 327 | case VideoCodecType::kVideoCodecVP8: |
| 328 | if (codec_specific_info) { |
| 329 | Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id, |
| 330 | is_keyframe, rtp_video_header); |
| 331 | } |
| 332 | return; |
| 333 | case VideoCodecType::kVideoCodecVP9: |
| 334 | if (codec_specific_info != nullptr) { |
| 335 | Vp9ToGeneric(codec_specific_info->codecSpecific.VP9, frame_id, |
| 336 | *rtp_video_header); |
| 337 | } |
| 338 | return; |
| 339 | case VideoCodecType::kVideoCodecAV1: |
| 340 | // TODO(philipel): Implement AV1 to generic descriptor. |
| 341 | return; |
| 342 | case VideoCodecType::kVideoCodecH264: |
| 343 | if (codec_specific_info) { |
| 344 | H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id, |
| 345 | is_keyframe, rtp_video_header); |
| 346 | } |
| 347 | return; |
| 348 | case VideoCodecType::kVideoCodecMultiplex: |
| 349 | return; |
| 350 | case VideoCodecType::kVideoCodecH265: |
| 351 | // TODO(bugs.webrtc.org/13485): Implement H265 to generic descriptor. |
| 352 | return; |
| 353 | } |
| 354 | RTC_DCHECK_NOTREACHED() << "Unsupported codec."; |
| 355 | } |
| 356 |
| 357 | absl::optional<FrameDependencyStructure> RtpPayloadParams::GenericStructure( |
| 358 | const CodecSpecificInfo* codec_specific_info) { |
| 359 | if (codec_specific_info == nullptr) { |
| 360 | return absl::nullopt; |
| 361 | } |
| 362 | // This helper shouldn't be used when template structure is specified |
| 363 | // explicetly. |
| 364 | RTC_DCHECK(!codec_specific_info->template_structure.has_value()); |
| 365 | switch (codec_specific_info->codecType) { |
| 366 | case VideoCodecType::kVideoCodecGeneric: |
| 367 | if (simulate_generic_structure_) { |
| 368 | return MinimalisticStructure(/*num_spatial_layers=*/1, |
| 369 | /*num_temporal_layer=*/1); |
| 370 | } |
| 371 | return absl::nullopt; |
| 372 | case VideoCodecType::kVideoCodecVP8: |
| 373 | return MinimalisticStructure(/*num_spatial_layers=*/1, |
| 374 | /*num_temporal_layer=*/kMaxTemporalStreams); |
| 375 | case VideoCodecType::kVideoCodecVP9: { |
| 376 | absl::optional<FrameDependencyStructure> structure = |
| 377 | MinimalisticStructure( |
| 378 | /*num_spatial_layers=*/kMaxSimulatedSpatialLayers, |
| 379 | /*num_temporal_layer=*/kMaxTemporalStreams); |
| 380 | const CodecSpecificInfoVP9& vp9 = codec_specific_info->codecSpecific.VP9; |
| 381 | if (vp9.ss_data_available && vp9.spatial_layer_resolution_present) { |
| 382 | RenderResolution first_valid; |
| 383 | RenderResolution last_valid; |
| 384 | for (size_t i = 0; i < vp9.num_spatial_layers; ++i) { |
| 385 | RenderResolution r(vp9.width[i], vp9.height[i]); |
| 386 | if (r.Valid()) { |
| 387 | if (!first_valid.Valid()) { |
| 388 | first_valid = r; |
| 389 | } |
| 390 | last_valid = r; |
| 391 | } |
| 392 | structure->resolutions.push_back(r); |
| 393 | } |
| 394 | if (!last_valid.Valid()) { |
| 395 | // No valid resolution found. Do not send resolutions. |
| 396 | structure->resolutions.clear(); |
| 397 | } else { |
| 398 | structure->resolutions.resize(kMaxSimulatedSpatialLayers, last_valid); |
| 399 | // VP9 encoder wrapper may disable first few spatial layers by |
| 400 | // setting invalid resolution (0,0). `structure->resolutions` |
| 401 | // doesn't support invalid resolution, so reset them to something |
| 402 | // valid. |
| 403 | for (RenderResolution& r : structure->resolutions) { |
| 404 | if (!r.Valid()) { |
| 405 | r = first_valid; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | return structure; |
| 411 | } |
| 412 | case VideoCodecType::kVideoCodecAV1: |
| 413 | case VideoCodecType::kVideoCodecH264: |
| 414 | case VideoCodecType::kVideoCodecH265: |
| 415 | case VideoCodecType::kVideoCodecMultiplex: |
| 416 | return absl::nullopt; |
| 417 | } |
| 418 | RTC_DCHECK_NOTREACHED() << "Unsupported codec."; |
| 419 | } |
| 420 |
| 421 | void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id, |
| 422 | bool is_keyframe, |
| 423 | RTPVideoHeader* rtp_video_header) { |
| 424 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 425 | rtp_video_header->generic.emplace(); |
| 426 |
| 427 | generic.frame_id = shared_frame_id; |
| 428 | generic.decode_target_indications.push_back(DecodeTargetIndication::kSwitch); |
| 429 |
| 430 | if (is_keyframe) { |
| 431 | generic.chain_diffs.push_back(0); |
| 432 | last_shared_frame_id_[0].fill(-1); |
| 433 | } else { |
| 434 | int64_t frame_id = last_shared_frame_id_[0][0]; |
| 435 | RTC_DCHECK_NE(frame_id, -1); |
| 436 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 437 | generic.chain_diffs.push_back(shared_frame_id - frame_id); |
| 438 | generic.dependencies.push_back(frame_id); |
| 439 | } |
| 440 |
| 441 | last_shared_frame_id_[0][0] = shared_frame_id; |
| 442 | } |
| 443 |
| 444 | void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info, |
| 445 | int64_t shared_frame_id, |
| 446 | bool is_keyframe, |
| 447 | RTPVideoHeader* rtp_video_header) { |
| 448 | const int temporal_index = |
| 449 | h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0; |
| 450 |
| 451 | if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) { |
| 452 | RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be " |
| 453 | "used with generic frame descriptor."; |
| 454 | return; |
| 455 | } |
| 456 |
| 457 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 458 | rtp_video_header->generic.emplace(); |
| 459 |
| 460 | generic.frame_id = shared_frame_id; |
| 461 | generic.temporal_index = temporal_index; |
| 462 |
| 463 | if (is_keyframe) { |
| 464 | RTC_DCHECK_EQ(temporal_index, 0); |
| 465 | last_shared_frame_id_[/*spatial index*/ 0].fill(-1); |
| 466 | last_shared_frame_id_[/*spatial index*/ 0][temporal_index] = |
| 467 | shared_frame_id; |
| 468 | return; |
| 469 | } |
| 470 |
| 471 | if (h264_info.base_layer_sync) { |
| 472 | int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0]; |
| 473 |
| 474 | for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) { |
| 475 | if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) { |
| 476 | last_shared_frame_id_[/*spatial index*/ 0][i] = -1; |
| 477 | } |
| 478 | } |
| 479 |
| 480 | RTC_DCHECK_GE(tl0_frame_id, 0); |
| 481 | RTC_DCHECK_LT(tl0_frame_id, shared_frame_id); |
| 482 | generic.dependencies.push_back(tl0_frame_id); |
| 483 | } else { |
| 484 | for (int i = 0; i <= temporal_index; ++i) { |
| 485 | int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i]; |
| 486 |
| 487 | if (frame_id != -1) { |
| 488 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 489 | generic.dependencies.push_back(frame_id); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 |
| 494 | last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id; |
| 495 | } |
| 496 |
| 497 | void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info, |
| 498 | int64_t shared_frame_id, |
| 499 | bool is_keyframe, |
| 500 | RTPVideoHeader* rtp_video_header) { |
| 501 | const auto& vp8_header = |
| 502 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 503 | const int spatial_index = 0; |
| 504 | const int temporal_index = |
| 505 | vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0; |
| 506 |
| 507 | if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers || |
| 508 | spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) { |
| 509 | RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be " |
| 510 | "used with generic frame descriptor."; |
| 511 | return; |
| 512 | } |
| 513 |
| 514 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 515 | rtp_video_header->generic.emplace(); |
| 516 |
| 517 | generic.frame_id = shared_frame_id; |
| 518 | generic.spatial_index = spatial_index; |
| 519 | generic.temporal_index = temporal_index; |
| 520 |
| 521 | // Generate decode target indications. |
| 522 | RTC_DCHECK_LT(temporal_index, kMaxTemporalStreams); |
| 523 | generic.decode_target_indications.resize(kMaxTemporalStreams); |
| 524 | auto it = std::fill_n(generic.decode_target_indications.begin(), |
| 525 | temporal_index, DecodeTargetIndication::kNotPresent); |
| 526 | std::fill(it, generic.decode_target_indications.end(), |
| 527 | DecodeTargetIndication::kSwitch); |
| 528 |
| 529 | // Frame dependencies. |
| 530 | if (vp8_info.useExplicitDependencies) { |
| 531 | SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe, |
| 532 | vp8_header.layerSync, &generic); |
| 533 | } else { |
| 534 | SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe, |
| 535 | spatial_index, temporal_index, |
| 536 | vp8_header.layerSync, &generic); |
| 537 | } |
| 538 |
| 539 | // Calculate chains. |
| 540 | generic.chain_diffs = { |
| 541 | (is_keyframe || chain_last_frame_id_[0] < 0) |
| 542 | ? 0 |
| 543 | : static_cast<int>(shared_frame_id - chain_last_frame_id_[0])}; |
| 544 | if (temporal_index == 0) { |
| 545 | chain_last_frame_id_[0] = shared_frame_id; |
| 546 | } |
| 547 | } |
| 548 |
| 549 | void RtpPayloadParams::Vp9ToGeneric(const CodecSpecificInfoVP9& vp9_info, |
| 550 | int64_t shared_frame_id, |
| 551 | RTPVideoHeader& rtp_video_header) { |
| 552 | const auto& vp9_header = |
| 553 | absl::get<RTPVideoHeaderVP9>(rtp_video_header.video_type_header); |
| 554 | const int num_spatial_layers = kMaxSimulatedSpatialLayers; |
| 555 | const int first_active_spatial_id = vp9_header.first_active_layer; |
| 556 | const int last_active_spatial_id = vp9_header.num_spatial_layers - 1; |
| 557 | const int num_temporal_layers = kMaxTemporalStreams; |
| 558 | static_assert(num_spatial_layers <= |
| 559 | RtpGenericFrameDescriptor::kMaxSpatialLayers); |
| 560 | static_assert(num_temporal_layers <= |
| 561 | RtpGenericFrameDescriptor::kMaxTemporalLayers); |
| 562 | static_assert(num_spatial_layers <= DependencyDescriptor::kMaxSpatialIds); |
| 563 | static_assert(num_temporal_layers <= DependencyDescriptor::kMaxTemporalIds); |
| 564 |
| 565 | int spatial_index = |
| 566 | vp9_header.spatial_idx != kNoSpatialIdx ? vp9_header.spatial_idx : 0; |
| 567 | int temporal_index = |
| 568 | vp9_header.temporal_idx != kNoTemporalIdx ? vp9_header.temporal_idx : 0; |
| 569 |
| 570 | if (!(temporal_index < num_temporal_layers && |
| 571 | first_active_spatial_id <= spatial_index && |
| 572 | spatial_index <= last_active_spatial_id && |
| 573 | last_active_spatial_id < num_spatial_layers)) { |
| 574 | // Prefer to generate no generic layering than an inconsistent one. |
| 575 | RTC_LOG(LS_ERROR) << "Inconsistent layer id sid=" << spatial_index |
| 576 | << ",tid=" << temporal_index |
| 577 | << " in VP9 header. Active spatial ids: [" |
| 578 | << first_active_spatial_id << "," |
| 579 | << last_active_spatial_id << "]"; |
| 580 | return; |
| 581 | } |
| 582 |
| 583 | RTPVideoHeader::GenericDescriptorInfo& result = |
| 584 | rtp_video_header.generic.emplace(); |
| 585 |
| 586 | result.frame_id = shared_frame_id; |
| 587 | result.spatial_index = spatial_index; |
| 588 | result.temporal_index = temporal_index; |
| 589 |
| 590 | result.decode_target_indications.reserve(num_spatial_layers * |
| 591 | num_temporal_layers); |
| 592 | for (int sid = 0; sid < num_spatial_layers; ++sid) { |
| 593 | for (int tid = 0; tid < num_temporal_layers; ++tid) { |
| 594 | DecodeTargetIndication dti; |
| 595 | if (sid < spatial_index || tid < temporal_index) { |
| 596 | dti = DecodeTargetIndication::kNotPresent; |
| 597 | } else if (spatial_index != sid && |
| 598 | vp9_header.non_ref_for_inter_layer_pred) { |
| 599 | dti = DecodeTargetIndication::kNotPresent; |
| 600 | } else if (sid == spatial_index && tid == temporal_index) { |
| 601 | // Assume that if frame is decodable, all of its own layer is decodable. |
| 602 | dti = DecodeTargetIndication::kSwitch; |
| 603 | } else if (sid == spatial_index && vp9_header.temporal_up_switch) { |
| 604 | dti = DecodeTargetIndication::kSwitch; |
| 605 | } else if (!vp9_header.inter_pic_predicted) { |
| 606 | // Key frame or spatial upswitch |
| 607 | dti = DecodeTargetIndication::kSwitch; |
| 608 | } else { |
| 609 | // Make no other assumptions. That should be safe, though suboptimal. |
| 610 | // To provide more accurate dti, encoder wrapper should fill in |
| 611 | // CodecSpecificInfo::generic_frame_info |
| 612 | dti = DecodeTargetIndication::kRequired; |
| 613 | } |
| 614 | result.decode_target_indications.push_back(dti); |
| 615 | } |
| 616 | } |
| 617 |
| 618 | // Calculate frame dependencies. |
| 619 | static constexpr int kPictureDiffLimit = 128; |
| 620 | if (last_vp9_frame_id_.empty()) { |
| 621 | // Create the array only if it is ever used. |
| 622 | last_vp9_frame_id_.resize(kPictureDiffLimit); |
| 623 | } |
| 624 |
| 625 | if (vp9_header.flexible_mode) { |
| 626 | if (vp9_header.inter_layer_predicted && spatial_index > 0) { |
| 627 | result.dependencies.push_back( |
| 628 | last_vp9_frame_id_[vp9_header.picture_id % kPictureDiffLimit] |
| 629 | [spatial_index - 1]); |
| 630 | } |
| 631 | if (vp9_header.inter_pic_predicted) { |
| 632 | for (size_t i = 0; i < vp9_header.num_ref_pics; ++i) { |
| 633 | // picture_id is 15 bit number that wraps around. Though undeflow may |
| 634 | // produce picture that exceeds 2^15, it is ok because in this |
| 635 | // code block only last 7 bits of the picture_id are used. |
| 636 | uint16_t depend_on = vp9_header.picture_id - vp9_header.pid_diff[i]; |
| 637 | result.dependencies.push_back( |
| 638 | last_vp9_frame_id_[depend_on % kPictureDiffLimit][spatial_index]); |
| 639 | } |
| 640 | } |
| 641 | last_vp9_frame_id_[vp9_header.picture_id % kPictureDiffLimit] |
| 642 | [spatial_index] = shared_frame_id; |
| 643 | } else { |
| 644 | // Implementing general conversion logic for non-flexible mode requires some |
| 645 | // work and we will almost certainly never need it, so for now support only |
| 646 | // non-layerd streams. |
| 647 | if (spatial_index > 0 || temporal_index > 0) { |
| 648 | // Prefer to generate no generic layering than an inconsistent one. |
| 649 | rtp_video_header.generic.reset(); |
| 650 | return; |
| 651 | } |
| 652 |
| 653 | if (vp9_header.inter_pic_predicted) { |
| 654 | // Since we only support non-scalable streams we only need to save the |
| 655 | // last frame id. |
| 656 | result.dependencies.push_back(last_vp9_frame_id_[0][0]); |
| 657 | } |
| 658 | last_vp9_frame_id_[0][0] = shared_frame_id; |
| 659 | } |
| 660 |
| 661 | result.active_decode_targets = |
| 662 | ((uint32_t{1} << num_temporal_layers * (last_active_spatial_id + 1)) - |
| 663 | 1) ^ |
| 664 | ((uint32_t{1} << num_temporal_layers * first_active_spatial_id) - 1); |
| 665 |
| 666 | // Calculate chains, asuming chain includes all frames with temporal_id = 0 |
| 667 | if (!vp9_header.inter_pic_predicted && !vp9_header.inter_layer_predicted) { |
| 668 | // Assume frames without dependencies also reset chains. |
| 669 | for (int sid = spatial_index; sid <= last_active_spatial_id; ++sid) { |
| 670 | chain_last_frame_id_[sid] = -1; |
| 671 | } |
| 672 | } |
| 673 | result.chain_diffs.resize(num_spatial_layers, 0); |
| 674 | for (int sid = first_active_spatial_id; sid <= last_active_spatial_id; |
| 675 | ++sid) { |
| 676 | if (chain_last_frame_id_[sid] == -1) { |
| 677 | result.chain_diffs[sid] = 0; |
| 678 | continue; |
| 679 | } |
| 680 | int64_t chain_diff = shared_frame_id - chain_last_frame_id_[sid]; |
| 681 | if (chain_diff >= 256) { |
| 682 | RTC_LOG(LS_ERROR) |
| 683 | << "Too many frames since last VP9 T0 frame for spatial layer #" |
| 684 | << sid << " at frame#" << shared_frame_id; |
| 685 | chain_last_frame_id_[sid] = -1; |
| 686 | chain_diff = 0; |
| 687 | } |
| 688 | result.chain_diffs[sid] = chain_diff; |
| 689 | } |
| 690 |
| 691 | if (temporal_index == 0) { |
| 692 | chain_last_frame_id_[spatial_index] = shared_frame_id; |
| 693 | if (!vp9_header.non_ref_for_inter_layer_pred) { |
| 694 | for (int sid = spatial_index + 1; sid <= last_active_spatial_id; ++sid) { |
| 695 | chain_last_frame_id_[sid] = shared_frame_id; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 |
| 701 | void RtpPayloadParams::SetDependenciesVp8Deprecated( |
| 702 | const CodecSpecificInfoVP8& vp8_info, |
| 703 | int64_t shared_frame_id, |
| 704 | bool is_keyframe, |
| 705 | int spatial_index, |
| 706 | int temporal_index, |
| 707 | bool layer_sync, |
| 708 | RTPVideoHeader::GenericDescriptorInfo* generic) { |
| 709 | RTC_DCHECK(!vp8_info.useExplicitDependencies); |
| 710 | RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value()); |
| 711 | new_version_used_ = false; |
| 712 |
| 713 | if (is_keyframe) { |
| 714 | RTC_DCHECK_EQ(temporal_index, 0); |
| 715 | last_shared_frame_id_[spatial_index].fill(-1); |
| 716 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 717 | return; |
| 718 | } |
| 719 |
| 720 | if (layer_sync) { |
| 721 | int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0]; |
| 722 |
| 723 | for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) { |
| 724 | if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) { |
| 725 | last_shared_frame_id_[spatial_index][i] = -1; |
| 726 | } |
| 727 | } |
| 728 |
| 729 | RTC_DCHECK_GE(tl0_frame_id, 0); |
| 730 | RTC_DCHECK_LT(tl0_frame_id, shared_frame_id); |
| 731 | generic->dependencies.push_back(tl0_frame_id); |
| 732 | } else { |
| 733 | for (int i = 0; i <= temporal_index; ++i) { |
| 734 | int64_t frame_id = last_shared_frame_id_[spatial_index][i]; |
| 735 |
| 736 | if (frame_id != -1) { |
| 737 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 738 | generic->dependencies.push_back(frame_id); |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 |
| 743 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 744 | } |
| 745 |
| 746 | void RtpPayloadParams::SetDependenciesVp8New( |
| 747 | const CodecSpecificInfoVP8& vp8_info, |
| 748 | int64_t shared_frame_id, |
| 749 | bool is_keyframe, |
| 750 | bool layer_sync, |
| 751 | RTPVideoHeader::GenericDescriptorInfo* generic) { |
| 752 | RTC_DCHECK(vp8_info.useExplicitDependencies); |
| 753 | RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value()); |
| 754 | new_version_used_ = true; |
| 755 |
| 756 | if (is_keyframe) { |
| 757 | RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u); |
| 758 | buffer_id_to_frame_id_.fill(shared_frame_id); |
| 759 | return; |
| 760 | } |
| 761 |
| 762 | constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount; |
| 763 |
| 764 | RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u); |
| 765 | RTC_DCHECK_LE(vp8_info.referencedBuffersCount, |
| 766 | arraysize(vp8_info.referencedBuffers)); |
| 767 |
| 768 | for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) { |
| 769 | const size_t referenced_buffer = vp8_info.referencedBuffers[i]; |
| 770 | RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8); |
| 771 | RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size()); |
| 772 |
| 773 | const int64_t dependency_frame_id = |
| 774 | buffer_id_to_frame_id_[referenced_buffer]; |
| 775 | RTC_DCHECK_GE(dependency_frame_id, 0); |
| 776 | RTC_DCHECK_LT(dependency_frame_id, shared_frame_id); |
| 777 |
| 778 | const bool is_new_dependency = |
| 779 | std::find(generic->dependencies.begin(), generic->dependencies.end(), |
| 780 | dependency_frame_id) == generic->dependencies.end(); |
| 781 | if (is_new_dependency) { |
| 782 | generic->dependencies.push_back(dependency_frame_id); |
| 783 | } |
| 784 | } |
| 785 |
| 786 | RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8); |
| 787 | for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) { |
| 788 | const size_t updated_id = vp8_info.updatedBuffers[i]; |
| 789 | buffer_id_to_frame_id_[updated_id] = shared_frame_id; |
| 790 | } |
| 791 |
| 792 | RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8); |
| 793 | } |
| 794 |
| 795 | } // namespace webrtc |
| 796 |