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