| 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 |
| 34 | namespace { |
| 35 | void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info, |
| 36 | absl::optional<int> spatial_index, |
| 37 | RTPVideoHeader* rtp) { |
| 38 | rtp->codec = info.codecType; |
| 39 | rtp->is_last_frame_in_picture = info.end_of_picture; |
| 40 | switch (info.codecType) { |
| 41 | case kVideoCodecVP8: { |
| 42 | auto& vp8_header = rtp->video_type_header.emplace<RTPVideoHeaderVP8>(); |
| 43 | vp8_header.InitRTPVideoHeaderVP8(); |
| 44 | vp8_header.nonReference = info.codecSpecific.VP8.nonReference; |
| 45 | vp8_header.temporalIdx = info.codecSpecific.VP8.temporalIdx; |
| 46 | vp8_header.layerSync = info.codecSpecific.VP8.layerSync; |
| 47 | vp8_header.keyIdx = info.codecSpecific.VP8.keyIdx; |
| 48 | rtp->simulcastIdx = spatial_index.value_or(0); |
| 49 | return; |
| 50 | } |
| 51 | case kVideoCodecVP9: { |
| 52 | auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>(); |
| 53 | vp9_header.InitRTPVideoHeaderVP9(); |
| 54 | vp9_header.inter_pic_predicted = |
| 55 | info.codecSpecific.VP9.inter_pic_predicted; |
| 56 | vp9_header.flexible_mode = info.codecSpecific.VP9.flexible_mode; |
| 57 | vp9_header.ss_data_available = info.codecSpecific.VP9.ss_data_available; |
| 58 | vp9_header.non_ref_for_inter_layer_pred = |
| 59 | info.codecSpecific.VP9.non_ref_for_inter_layer_pred; |
| 60 | vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx; |
| 61 | vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch; |
| 62 | vp9_header.inter_layer_predicted = |
| 63 | info.codecSpecific.VP9.inter_layer_predicted; |
| 64 | vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx; |
| 65 | vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers; |
| 66 | vp9_header.first_active_layer = info.codecSpecific.VP9.first_active_layer; |
| 67 | if (vp9_header.num_spatial_layers > 1) { |
| 68 | vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx); |
| 69 | } else { |
| 70 | vp9_header.spatial_idx = kNoSpatialIdx; |
| 71 | } |
| 72 | if (info.codecSpecific.VP9.ss_data_available) { |
| 73 | vp9_header.spatial_layer_resolution_present = |
| 74 | info.codecSpecific.VP9.spatial_layer_resolution_present; |
| 75 | if (info.codecSpecific.VP9.spatial_layer_resolution_present) { |
| 76 | for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers; |
| 77 | ++i) { |
| 78 | vp9_header.width[i] = info.codecSpecific.VP9.width[i]; |
| 79 | vp9_header.height[i] = info.codecSpecific.VP9.height[i]; |
| 80 | } |
| 81 | } |
| 82 | vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof); |
| 83 | } |
| 84 |
| 85 | vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics; |
| 86 | for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) { |
| 87 | vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i]; |
| 88 | } |
| 89 | vp9_header.end_of_picture = info.end_of_picture; |
| 90 | return; |
| 91 | } |
| 92 | case kVideoCodecH264: { |
| 93 | auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>(); |
| 94 | h264_header.packetization_mode = |
| 95 | info.codecSpecific.H264.packetization_mode; |
| 96 | rtp->simulcastIdx = spatial_index.value_or(0); |
| 97 | return; |
| 98 | } |
| 99 | #ifndef DISABLE_H265 |
| 100 | case kVideoCodecH265: { |
| 101 | auto& h265_header = rtp->video_type_header.emplace<RTPVideoHeaderH265>(); |
| 102 | h265_header.packetization_mode = |
| 103 | info.codecSpecific.H265.packetization_mode; |
| 104 | } |
| 105 | return; |
| 106 | #endif |
| 107 | case kVideoCodecMultiplex: |
| 108 | case kVideoCodecGeneric: |
| 109 | rtp->codec = kVideoCodecGeneric; |
| 110 | rtp->simulcastIdx = spatial_index.value_or(0); |
| 111 | return; |
| 112 | default: |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 |
| 117 | void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) { |
| 118 | if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid || |
| 119 | image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) { |
| 120 | timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid; |
| 121 | return; |
| 122 | } |
| 123 |
| 124 | timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs( |
| 125 | image.capture_time_ms_, image.timing_.encode_start_ms); |
| 126 | timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs( |
| 127 | image.capture_time_ms_, image.timing_.encode_finish_ms); |
| 128 | timing->packetization_finish_delta_ms = 0; |
| 129 | timing->pacer_exit_delta_ms = 0; |
| 130 | timing->network_timestamp_delta_ms = 0; |
| 131 | timing->network2_timestamp_delta_ms = 0; |
| 132 | timing->flags = image.timing_.flags; |
| 133 | } |
| 134 | } // namespace |
| 135 |
| 136 | RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc, |
| 137 | const RtpPayloadState* state, |
| 138 | const WebRtcKeyValueConfig& trials) |
| 139 | : ssrc_(ssrc), |
| 140 | generic_picture_id_experiment_( |
| 141 | absl::StartsWith(trials.Lookup("WebRTC-GenericPictureId"), |
| 142 | "Enabled")), |
| 143 | simulate_generic_vp9_( |
| 144 | absl::StartsWith(trials.Lookup("WebRTC-Vp9DependencyDescriptor"), |
| 145 | "Enabled")) { |
| 146 | for (auto& spatial_layer : last_shared_frame_id_) |
| 147 | spatial_layer.fill(-1); |
| 148 |
| 149 | buffer_id_to_frame_id_.fill(-1); |
| 150 |
| 151 | Random random(rtc::TimeMicros()); |
| 152 | state_.picture_id = |
| 153 | state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF); |
| 154 | state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>()); |
| 155 | } |
| 156 |
| 157 | RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default; |
| 158 |
| 159 | RtpPayloadParams::~RtpPayloadParams() {} |
| 160 |
| 161 | RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader( |
| 162 | const EncodedImage& image, |
| 163 | const CodecSpecificInfo* codec_specific_info, |
| 164 | int64_t shared_frame_id) { |
| 165 | RTPVideoHeader rtp_video_header; |
| 166 | if (codec_specific_info) { |
| 167 | PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(), |
| 168 | &rtp_video_header); |
| 169 | } |
| 170 | rtp_video_header.frame_type = image._frameType; |
| 171 | rtp_video_header.rotation = image.rotation_; |
| 172 | rtp_video_header.content_type = image.content_type_; |
| 173 | rtp_video_header.playout_delay = image.playout_delay_; |
| 174 | rtp_video_header.width = image._encodedWidth; |
| 175 | rtp_video_header.height = image._encodedHeight; |
| 176 | rtp_video_header.color_space = image.ColorSpace() |
| 177 | ? absl::make_optional(*image.ColorSpace()) |
| 178 | : absl::nullopt; |
| 179 | rtp_video_header.video_frame_tracking_id = image.VideoFrameTrackingId(); |
| 180 | SetVideoTiming(image, &rtp_video_header.video_timing); |
| 181 |
| 182 | const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey; |
| 183 | const bool first_frame_in_picture = |
| 184 | (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9) |
| 185 | ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture |
| 186 | : true; |
| 187 |
| 188 | SetCodecSpecific(&rtp_video_header, first_frame_in_picture); |
| 189 |
| 190 | SetGeneric(codec_specific_info, shared_frame_id, is_keyframe, |
| 191 | &rtp_video_header); |
| 192 |
| 193 | return rtp_video_header; |
| 194 | } |
| 195 |
| 196 | uint32_t RtpPayloadParams::ssrc() const { |
| 197 | return ssrc_; |
| 198 | } |
| 199 |
| 200 | RtpPayloadState RtpPayloadParams::state() const { |
| 201 | return state_; |
| 202 | } |
| 203 |
| 204 | void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header, |
| 205 | bool first_frame_in_picture) { |
| 206 | // Always set picture id. Set tl0_pic_idx iff temporal index is set. |
| 207 | if (first_frame_in_picture) { |
| 208 | state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF; |
| 209 | } |
| 210 | if (rtp_video_header->codec == kVideoCodecVP8) { |
| 211 | auto& vp8_header = |
| 212 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 213 | vp8_header.pictureId = state_.picture_id; |
| 214 |
| 215 | if (vp8_header.temporalIdx != kNoTemporalIdx) { |
| 216 | if (vp8_header.temporalIdx == 0) { |
| 217 | ++state_.tl0_pic_idx; |
| 218 | } |
| 219 | vp8_header.tl0PicIdx = state_.tl0_pic_idx; |
| 220 | } |
| 221 | } |
| 222 | if (rtp_video_header->codec == kVideoCodecVP9) { |
| 223 | auto& vp9_header = |
| 224 | absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header); |
| 225 | vp9_header.picture_id = state_.picture_id; |
| 226 |
| 227 | // Note that in the case that we have no temporal layers but we do have |
| 228 | // spatial layers, packets will carry layering info with a temporal_idx of |
| 229 | // zero, and we then have to set and increment tl0_pic_idx. |
| 230 | if (vp9_header.temporal_idx != kNoTemporalIdx || |
| 231 | vp9_header.spatial_idx != kNoSpatialIdx) { |
| 232 | if (first_frame_in_picture && |
| 233 | (vp9_header.temporal_idx == 0 || |
| 234 | vp9_header.temporal_idx == kNoTemporalIdx)) { |
| 235 | ++state_.tl0_pic_idx; |
| 236 | } |
| 237 | vp9_header.tl0_pic_idx = state_.tl0_pic_idx; |
| 238 | } |
| 239 | } |
| 240 | if (generic_picture_id_experiment_ && |
| 241 | rtp_video_header->codec == kVideoCodecGeneric) { |
| 242 | rtp_video_header->video_type_header.emplace<RTPVideoHeaderLegacyGeneric>() |
| 243 | .picture_id = state_.picture_id; |
| 244 | } |
| 245 | } |
| 246 |
| 247 | RTPVideoHeader::GenericDescriptorInfo |
| 248 | RtpPayloadParams::GenericDescriptorFromFrameInfo( |
| 249 | const GenericFrameInfo& frame_info, |
| 250 | int64_t frame_id) { |
| 251 | RTPVideoHeader::GenericDescriptorInfo generic; |
| 252 | generic.frame_id = frame_id; |
| 253 | generic.dependencies = dependencies_calculator_.FromBuffersUsage( |
| 254 | frame_id, frame_info.encoder_buffers); |
| 255 | generic.chain_diffs = |
| 256 | chains_calculator_.From(frame_id, frame_info.part_of_chain); |
| 257 | generic.spatial_index = frame_info.spatial_id; |
| 258 | generic.temporal_index = frame_info.temporal_id; |
| 259 | generic.decode_target_indications = frame_info.decode_target_indications; |
| 260 | generic.active_decode_targets = frame_info.active_decode_targets; |
| 261 | return generic; |
| 262 | } |
| 263 |
| 264 | void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info, |
| 265 | int64_t frame_id, |
| 266 | bool is_keyframe, |
| 267 | RTPVideoHeader* rtp_video_header) { |
| 268 | if (codec_specific_info && codec_specific_info->generic_frame_info && |
| 269 | !codec_specific_info->generic_frame_info->encoder_buffers.empty()) { |
| 270 | if (is_keyframe) { |
| 271 | // Key frame resets all chains it is in. |
| 272 | chains_calculator_.Reset( |
| 273 | codec_specific_info->generic_frame_info->part_of_chain); |
| 274 | } |
| 275 | rtp_video_header->generic = GenericDescriptorFromFrameInfo( |
| 276 | *codec_specific_info->generic_frame_info, frame_id); |
| 277 | return; |
| 278 | } |
| 279 |
| 280 | switch (rtp_video_header->codec) { |
| 281 | case VideoCodecType::kVideoCodecGeneric: |
| 282 | GenericToGeneric(frame_id, is_keyframe, rtp_video_header); |
| 283 | return; |
| 284 | case VideoCodecType::kVideoCodecVP8: |
| 285 | if (codec_specific_info) { |
| 286 | Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id, |
| 287 | is_keyframe, rtp_video_header); |
| 288 | } |
| 289 | return; |
| 290 | case VideoCodecType::kVideoCodecVP9: |
| 291 | if (simulate_generic_vp9_ && codec_specific_info != nullptr) { |
| 292 | Vp9ToGeneric(codec_specific_info->codecSpecific.VP9, frame_id, |
| 293 | *rtp_video_header); |
| 294 | } |
| 295 | return; |
| 296 | case VideoCodecType::kVideoCodecAV1: |
| 297 | // TODO(philipel): Implement AV1 to generic descriptor. |
| 298 | return; |
| 299 | case VideoCodecType::kVideoCodecH264: |
| 300 | if (codec_specific_info) { |
| 301 | H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id, |
| 302 | is_keyframe, rtp_video_header); |
| 303 | } |
| 304 | return; |
| 305 | #ifndef DISABLE_H265 |
| 306 | case VideoCodecType::kVideoCodecH265: |
| 307 | #endif |
| 308 | case VideoCodecType::kVideoCodecMultiplex: |
| 309 | return; |
| 310 | } |
| 311 | RTC_NOTREACHED() << "Unsupported codec."; |
| 312 | } |
| 313 |
| 314 | void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id, |
| 315 | bool is_keyframe, |
| 316 | RTPVideoHeader* rtp_video_header) { |
| 317 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 318 | rtp_video_header->generic.emplace(); |
| 319 |
| 320 | generic.frame_id = shared_frame_id; |
| 321 |
| 322 | if (is_keyframe) { |
| 323 | last_shared_frame_id_[0].fill(-1); |
| 324 | } else { |
| 325 | int64_t frame_id = last_shared_frame_id_[0][0]; |
| 326 | RTC_DCHECK_NE(frame_id, -1); |
| 327 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 328 | generic.dependencies.push_back(frame_id); |
| 329 | } |
| 330 |
| 331 | last_shared_frame_id_[0][0] = shared_frame_id; |
| 332 | } |
| 333 |
| 334 | void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info, |
| 335 | int64_t shared_frame_id, |
| 336 | bool is_keyframe, |
| 337 | RTPVideoHeader* rtp_video_header) { |
| 338 | const int temporal_index = |
| 339 | h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0; |
| 340 |
| 341 | if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) { |
| 342 | RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be " |
| 343 | "used with generic frame descriptor."; |
| 344 | return; |
| 345 | } |
| 346 |
| 347 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 348 | rtp_video_header->generic.emplace(); |
| 349 |
| 350 | generic.frame_id = shared_frame_id; |
| 351 | generic.temporal_index = temporal_index; |
| 352 |
| 353 | if (is_keyframe) { |
| 354 | RTC_DCHECK_EQ(temporal_index, 0); |
| 355 | last_shared_frame_id_[/*spatial index*/ 0].fill(-1); |
| 356 | last_shared_frame_id_[/*spatial index*/ 0][temporal_index] = |
| 357 | shared_frame_id; |
| 358 | return; |
| 359 | } |
| 360 |
| 361 | if (h264_info.base_layer_sync) { |
| 362 | int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0]; |
| 363 |
| 364 | for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) { |
| 365 | if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) { |
| 366 | last_shared_frame_id_[/*spatial index*/ 0][i] = -1; |
| 367 | } |
| 368 | } |
| 369 |
| 370 | RTC_DCHECK_GE(tl0_frame_id, 0); |
| 371 | RTC_DCHECK_LT(tl0_frame_id, shared_frame_id); |
| 372 | generic.dependencies.push_back(tl0_frame_id); |
| 373 | } else { |
| 374 | for (int i = 0; i <= temporal_index; ++i) { |
| 375 | int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i]; |
| 376 |
| 377 | if (frame_id != -1) { |
| 378 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 379 | generic.dependencies.push_back(frame_id); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 |
| 384 | last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id; |
| 385 | } |
| 386 |
| 387 | void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info, |
| 388 | int64_t shared_frame_id, |
| 389 | bool is_keyframe, |
| 390 | RTPVideoHeader* rtp_video_header) { |
| 391 | const auto& vp8_header = |
| 392 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 393 | const int spatial_index = 0; |
| 394 | const int temporal_index = |
| 395 | vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0; |
| 396 |
| 397 | if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers || |
| 398 | spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) { |
| 399 | RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be " |
| 400 | "used with generic frame descriptor."; |
| 401 | return; |
| 402 | } |
| 403 |
| 404 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 405 | rtp_video_header->generic.emplace(); |
| 406 |
| 407 | generic.frame_id = shared_frame_id; |
| 408 | generic.spatial_index = spatial_index; |
| 409 | generic.temporal_index = temporal_index; |
| 410 |
| 411 | if (vp8_info.useExplicitDependencies) { |
| 412 | SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe, |
| 413 | vp8_header.layerSync, &generic); |
| 414 | } else { |
| 415 | SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe, |
| 416 | spatial_index, temporal_index, |
| 417 | vp8_header.layerSync, &generic); |
| 418 | } |
| 419 | } |
| 420 |
| 421 | FrameDependencyStructure RtpPayloadParams::MinimalisticVp9Structure( |
| 422 | const CodecSpecificInfoVP9& vp9) { |
| 423 | const int num_spatial_layers = vp9.num_spatial_layers; |
| 424 | const int num_temporal_layers = kMaxTemporalStreams; |
| 425 | FrameDependencyStructure structure; |
| 426 | structure.num_decode_targets = num_spatial_layers * num_temporal_layers; |
| 427 | structure.num_chains = num_spatial_layers; |
| 428 | structure.templates.reserve(num_spatial_layers * num_temporal_layers); |
| 429 | for (int sid = 0; sid < num_spatial_layers; ++sid) { |
| 430 | for (int tid = 0; tid < num_temporal_layers; ++tid) { |
| 431 | FrameDependencyTemplate a_template; |
| 432 | a_template.spatial_id = sid; |
| 433 | a_template.temporal_id = tid; |
| 434 | for (int s = 0; s < num_spatial_layers; ++s) { |
| 435 | for (int t = 0; t < num_temporal_layers; ++t) { |
| 436 | // Prefer kSwitch for indication frame is part of the decode target |
| 437 | // because RtpPayloadParams::Vp9ToGeneric uses that indication more |
| 438 | // often that kRequired, increasing chance custom dti need not to |
| 439 | // use more bits in dependency descriptor on the wire. |
| 440 | a_template.decode_target_indications.push_back( |
| 441 | sid <= s && tid <= t ? DecodeTargetIndication::kSwitch |
| 442 | : DecodeTargetIndication::kNotPresent); |
| 443 | } |
| 444 | } |
| 445 | a_template.frame_diffs.push_back(tid == 0 ? num_spatial_layers * |
| 446 | num_temporal_layers |
| 447 | : num_spatial_layers); |
| 448 | a_template.chain_diffs.assign(structure.num_chains, 1); |
| 449 | structure.templates.push_back(a_template); |
| 450 |
| 451 | structure.decode_target_protected_by_chain.push_back(sid); |
| 452 | } |
| 453 | if (vp9.ss_data_available && vp9.spatial_layer_resolution_present) { |
| 454 | structure.resolutions.emplace_back(vp9.width[sid], vp9.height[sid]); |
| 455 | } |
| 456 | } |
| 457 | return structure; |
| 458 | } |
| 459 |
| 460 | void RtpPayloadParams::Vp9ToGeneric(const CodecSpecificInfoVP9& vp9_info, |
| 461 | int64_t shared_frame_id, |
| 462 | RTPVideoHeader& rtp_video_header) { |
| 463 | const auto& vp9_header = |
| 464 | absl::get<RTPVideoHeaderVP9>(rtp_video_header.video_type_header); |
| 465 | const int num_spatial_layers = vp9_header.num_spatial_layers; |
| 466 | const int num_temporal_layers = kMaxTemporalStreams; |
| 467 |
| 468 | int spatial_index = |
| 469 | vp9_header.spatial_idx != kNoSpatialIdx ? vp9_header.spatial_idx : 0; |
| 470 | int temporal_index = |
| 471 | vp9_header.temporal_idx != kNoTemporalIdx ? vp9_header.temporal_idx : 0; |
| 472 |
| 473 | if (spatial_index >= num_spatial_layers || |
| 474 | temporal_index >= num_temporal_layers || |
| 475 | num_spatial_layers > RtpGenericFrameDescriptor::kMaxSpatialLayers) { |
| 476 | // Prefer to generate no generic layering than an inconsistent one. |
| 477 | return; |
| 478 | } |
| 479 |
| 480 | RTPVideoHeader::GenericDescriptorInfo& result = |
| 481 | rtp_video_header.generic.emplace(); |
| 482 |
| 483 | result.frame_id = shared_frame_id; |
| 484 | result.spatial_index = spatial_index; |
| 485 | result.temporal_index = temporal_index; |
| 486 |
| 487 | result.decode_target_indications.reserve(num_spatial_layers * |
| 488 | num_temporal_layers); |
| 489 | for (int sid = 0; sid < num_spatial_layers; ++sid) { |
| 490 | for (int tid = 0; tid < num_temporal_layers; ++tid) { |
| 491 | DecodeTargetIndication dti; |
| 492 | if (sid < spatial_index || tid < temporal_index) { |
| 493 | dti = DecodeTargetIndication::kNotPresent; |
| 494 | } else if (spatial_index != sid && |
| 495 | vp9_header.non_ref_for_inter_layer_pred) { |
| 496 | dti = DecodeTargetIndication::kNotPresent; |
| 497 | } else if (sid == spatial_index && tid == temporal_index) { |
| 498 | // Assume that if frame is decodable, all of its own layer is decodable. |
| 499 | dti = DecodeTargetIndication::kSwitch; |
| 500 | } else if (sid == spatial_index && vp9_header.temporal_up_switch) { |
| 501 | dti = DecodeTargetIndication::kSwitch; |
| 502 | } else if (!vp9_header.inter_pic_predicted) { |
| 503 | // Key frame or spatial upswitch |
| 504 | dti = DecodeTargetIndication::kSwitch; |
| 505 | } else { |
| 506 | // Make no other assumptions. That should be safe, though suboptimal. |
| 507 | // To provide more accurate dti, encoder wrapper should fill in |
| 508 | // CodecSpecificInfo::generic_frame_info |
| 509 | dti = DecodeTargetIndication::kRequired; |
| 510 | } |
| 511 | result.decode_target_indications.push_back(dti); |
| 512 | } |
| 513 | } |
| 514 |
| 515 | // Calculate frame dependencies. |
| 516 | static constexpr int kPictureDiffLimit = 128; |
| 517 | if (last_vp9_frame_id_.empty()) { |
| 518 | // Create the array only if it is ever used. |
| 519 | last_vp9_frame_id_.resize(kPictureDiffLimit); |
| 520 | } |
| 521 | if (vp9_header.inter_layer_predicted && spatial_index > 0) { |
| 522 | result.dependencies.push_back( |
| 523 | last_vp9_frame_id_[vp9_header.picture_id % kPictureDiffLimit] |
| 524 | [spatial_index - 1]); |
| 525 | } |
| 526 | if (vp9_header.inter_pic_predicted) { |
| 527 | for (size_t i = 0; i < vp9_header.num_ref_pics; ++i) { |
| 528 | // picture_id is 15 bit number that wraps around. Though undeflow may |
| 529 | // produce picture that exceeds 2^15, it is ok because in this |
| 530 | // code block only last 7 bits of the picture_id are used. |
| 531 | uint16_t depend_on = vp9_header.picture_id - vp9_header.pid_diff[i]; |
| 532 | result.dependencies.push_back( |
| 533 | last_vp9_frame_id_[depend_on % kPictureDiffLimit][spatial_index]); |
| 534 | } |
| 535 | } |
| 536 | last_vp9_frame_id_[vp9_header.picture_id % kPictureDiffLimit][spatial_index] = |
| 537 | shared_frame_id; |
| 538 |
| 539 | // Calculate chains, asuming chain includes all frames with temporal_id = 0 |
| 540 | if (!vp9_header.inter_pic_predicted && !vp9_header.inter_layer_predicted) { |
| 541 | // Assume frames without dependencies also reset chains. |
| 542 | for (int sid = spatial_index; sid < num_spatial_layers; ++sid) { |
| 543 | chain_last_frame_id_[sid] = -1; |
| 544 | } |
| 545 | } |
| 546 | result.chain_diffs.resize(num_spatial_layers); |
| 547 | for (int sid = 0; sid < num_spatial_layers; ++sid) { |
| 548 | if (chain_last_frame_id_[sid] == -1) { |
| 549 | result.chain_diffs[sid] = 0; |
| 550 | continue; |
| 551 | } |
| 552 | result.chain_diffs[sid] = shared_frame_id - chain_last_frame_id_[sid]; |
| 553 | } |
| 554 |
| 555 | if (temporal_index == 0) { |
| 556 | chain_last_frame_id_[spatial_index] = shared_frame_id; |
| 557 | if (!vp9_header.non_ref_for_inter_layer_pred) { |
| 558 | for (int sid = spatial_index + 1; sid < num_spatial_layers; ++sid) { |
| 559 | chain_last_frame_id_[sid] = shared_frame_id; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 |
| 565 | void RtpPayloadParams::SetDependenciesVp8Deprecated( |
| 566 | const CodecSpecificInfoVP8& vp8_info, |
| 567 | int64_t shared_frame_id, |
| 568 | bool is_keyframe, |
| 569 | int spatial_index, |
| 570 | int temporal_index, |
| 571 | bool layer_sync, |
| 572 | RTPVideoHeader::GenericDescriptorInfo* generic) { |
| 573 | RTC_DCHECK(!vp8_info.useExplicitDependencies); |
| 574 | RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value()); |
| 575 | new_version_used_ = false; |
| 576 |
| 577 | if (is_keyframe) { |
| 578 | RTC_DCHECK_EQ(temporal_index, 0); |
| 579 | last_shared_frame_id_[spatial_index].fill(-1); |
| 580 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 581 | return; |
| 582 | } |
| 583 |
| 584 | if (layer_sync) { |
| 585 | int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0]; |
| 586 |
| 587 | for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) { |
| 588 | if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) { |
| 589 | last_shared_frame_id_[spatial_index][i] = -1; |
| 590 | } |
| 591 | } |
| 592 |
| 593 | RTC_DCHECK_GE(tl0_frame_id, 0); |
| 594 | RTC_DCHECK_LT(tl0_frame_id, shared_frame_id); |
| 595 | generic->dependencies.push_back(tl0_frame_id); |
| 596 | } else { |
| 597 | for (int i = 0; i <= temporal_index; ++i) { |
| 598 | int64_t frame_id = last_shared_frame_id_[spatial_index][i]; |
| 599 |
| 600 | if (frame_id != -1) { |
| 601 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 602 | generic->dependencies.push_back(frame_id); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 |
| 607 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 608 | } |
| 609 |
| 610 | void RtpPayloadParams::SetDependenciesVp8New( |
| 611 | const CodecSpecificInfoVP8& vp8_info, |
| 612 | int64_t shared_frame_id, |
| 613 | bool is_keyframe, |
| 614 | bool layer_sync, |
| 615 | RTPVideoHeader::GenericDescriptorInfo* generic) { |
| 616 | RTC_DCHECK(vp8_info.useExplicitDependencies); |
| 617 | RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value()); |
| 618 | new_version_used_ = true; |
| 619 |
| 620 | if (is_keyframe) { |
| 621 | RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u); |
| 622 | buffer_id_to_frame_id_.fill(shared_frame_id); |
| 623 | return; |
| 624 | } |
| 625 |
| 626 | constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount; |
| 627 |
| 628 | RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u); |
| 629 | RTC_DCHECK_LE(vp8_info.referencedBuffersCount, |
| 630 | arraysize(vp8_info.referencedBuffers)); |
| 631 |
| 632 | for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) { |
| 633 | const size_t referenced_buffer = vp8_info.referencedBuffers[i]; |
| 634 | RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8); |
| 635 | RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size()); |
| 636 |
| 637 | const int64_t dependency_frame_id = |
| 638 | buffer_id_to_frame_id_[referenced_buffer]; |
| 639 | RTC_DCHECK_GE(dependency_frame_id, 0); |
| 640 | RTC_DCHECK_LT(dependency_frame_id, shared_frame_id); |
| 641 |
| 642 | const bool is_new_dependency = |
| 643 | std::find(generic->dependencies.begin(), generic->dependencies.end(), |
| 644 | dependency_frame_id) == generic->dependencies.end(); |
| 645 | if (is_new_dependency) { |
| 646 | generic->dependencies.push_back(dependency_frame_id); |
| 647 | } |
| 648 | } |
| 649 |
| 650 | RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8); |
| 651 | for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) { |
| 652 | const size_t updated_id = vp8_info.updatedBuffers[i]; |
| 653 | buffer_id_to_frame_id_[updated_id] = shared_frame_id; |
| 654 | } |
| 655 |
| 656 | RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8); |
| 657 | } |
| 658 |
| 659 | } // namespace webrtc |
| 660 |