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 { |
| 33 | |
| 34 | namespace { |
| 35 | void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info, |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 36 | absl::optional<int> spatial_index, |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 37 | RTPVideoHeader* rtp) { |
| 38 | rtp->codec = info.codecType; |
Danil Chapovalov | 62a9a32 | 2020-11-11 15:15:07 | [diff] [blame] | 39 | rtp->is_last_frame_in_picture = info.end_of_picture; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 40 | switch (info.codecType) { |
| 41 | case kVideoCodecVP8: { |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 | [diff] [blame] | 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; |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 48 | rtp->simulcastIdx = spatial_index.value_or(0); |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | case kVideoCodecVP9: { |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 52 | auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>(); |
| 53 | vp9_header.InitRTPVideoHeaderVP9(); |
| 54 | vp9_header.inter_pic_predicted = |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 55 | info.codecSpecific.VP9.inter_pic_predicted; |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 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 = |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 59 | info.codecSpecific.VP9.non_ref_for_inter_layer_pred; |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 60 | vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx; |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 61 | vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch; |
| 62 | vp9_header.inter_layer_predicted = |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 63 | info.codecSpecific.VP9.inter_layer_predicted; |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 64 | vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx; |
| 65 | vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers; |
Ilya Nikolaevskiy | f5d8778 | 2020-02-04 10:06:33 | [diff] [blame] | 66 | vp9_header.first_active_layer = info.codecSpecific.VP9.first_active_layer; |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 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 | } |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 72 | if (info.codecSpecific.VP9.ss_data_available) { |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 73 | vp9_header.spatial_layer_resolution_present = |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 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) { |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 78 | vp9_header.width[i] = info.codecSpecific.VP9.width[i]; |
| 79 | vp9_header.height[i] = info.codecSpecific.VP9.height[i]; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 80 | } |
| 81 | } |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 82 | vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof); |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 83 | } |
| 84 | |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 85 | vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 86 | for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) { |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 87 | vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i]; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 88 | } |
Danil Chapovalov | 06bbeb3 | 2020-11-11 11:42:56 | [diff] [blame] | 89 | vp9_header.end_of_picture = info.end_of_picture; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | case kVideoCodecH264: { |
philipel | 7d745e5 | 2018-08-02 12:03:53 | [diff] [blame] | 93 | auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>(); |
| 94 | h264_header.packetization_mode = |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 95 | info.codecSpecific.H264.packetization_mode; |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 96 | rtp->simulcastIdx = spatial_index.value_or(0); |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | case kVideoCodecMultiplex: |
| 100 | case kVideoCodecGeneric: |
| 101 | rtp->codec = kVideoCodecGeneric; |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 102 | rtp->simulcastIdx = spatial_index.value_or(0); |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 103 | return; |
| 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 | } |
| 126 | } // namespace |
| 127 | |
| 128 | RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc, |
Erik Språng | cbc0cba | 2020-04-18 12:36:59 | [diff] [blame] | 129 | const RtpPayloadState* state, |
| 130 | const WebRtcKeyValueConfig& trials) |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 131 | : ssrc_(ssrc), |
| 132 | generic_picture_id_experiment_( |
Erik Språng | cbc0cba | 2020-04-18 12:36:59 | [diff] [blame] | 133 | absl::StartsWith(trials.Lookup("WebRTC-GenericPictureId"), |
Danil Chapovalov | 636865e | 2020-06-03 12:11:26 | [diff] [blame] | 134 | "Enabled")) { |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 135 | for (auto& spatial_layer : last_shared_frame_id_) |
| 136 | spatial_layer.fill(-1); |
| 137 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 138 | buffer_id_to_frame_id_.fill(-1); |
| 139 | |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 140 | Random random(rtc::TimeMicros()); |
| 141 | state_.picture_id = |
| 142 | state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF); |
| 143 | state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>()); |
| 144 | } |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 145 | |
| 146 | RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default; |
| 147 | |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 148 | RtpPayloadParams::~RtpPayloadParams() {} |
| 149 | |
| 150 | RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader( |
| 151 | const EncodedImage& image, |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 152 | const CodecSpecificInfo* codec_specific_info, |
| 153 | int64_t shared_frame_id) { |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 154 | RTPVideoHeader rtp_video_header; |
| 155 | if (codec_specific_info) { |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 156 | PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(), |
| 157 | &rtp_video_header); |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 158 | } |
Danil Chapovalov | 51bf200 | 2019-10-11 08:53:27 | [diff] [blame] | 159 | rtp_video_header.frame_type = image._frameType, |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 160 | rtp_video_header.rotation = image.rotation_; |
| 161 | rtp_video_header.content_type = image.content_type_; |
| 162 | rtp_video_header.playout_delay = image.playout_delay_; |
philipel | fab9129 | 2018-10-17 12:36:08 | [diff] [blame] | 163 | rtp_video_header.width = image._encodedWidth; |
| 164 | rtp_video_header.height = image._encodedHeight; |
Johannes Kron | d0b69a8 | 2018-12-03 13:18:53 | [diff] [blame] | 165 | rtp_video_header.color_space = image.ColorSpace() |
| 166 | ? absl::make_optional(*image.ColorSpace()) |
| 167 | : absl::nullopt; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 168 | SetVideoTiming(image, &rtp_video_header.video_timing); |
| 169 | |
Niels Möller | 8f7ce22 | 2019-03-21 14:43:58 | [diff] [blame] | 170 | const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 171 | const bool first_frame_in_picture = |
| 172 | (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9) |
| 173 | ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture |
| 174 | : true; |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 175 | |
| 176 | SetCodecSpecific(&rtp_video_header, first_frame_in_picture); |
philipel | 569397f | 2018-09-26 10:25:31 | [diff] [blame] | 177 | |
Danil Chapovalov | 636865e | 2020-06-03 12:11:26 | [diff] [blame] | 178 | SetGeneric(codec_specific_info, shared_frame_id, is_keyframe, |
| 179 | &rtp_video_header); |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 180 | |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 181 | return rtp_video_header; |
| 182 | } |
| 183 | |
| 184 | uint32_t RtpPayloadParams::ssrc() const { |
| 185 | return ssrc_; |
| 186 | } |
| 187 | |
| 188 | RtpPayloadState RtpPayloadParams::state() const { |
| 189 | return state_; |
| 190 | } |
| 191 | |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 192 | void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header, |
| 193 | bool first_frame_in_picture) { |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 194 | // Always set picture id. Set tl0_pic_idx iff temporal index is set. |
| 195 | if (first_frame_in_picture) { |
| 196 | state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF; |
| 197 | } |
| 198 | if (rtp_video_header->codec == kVideoCodecVP8) { |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 | [diff] [blame] | 199 | auto& vp8_header = |
| 200 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 201 | vp8_header.pictureId = state_.picture_id; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 202 | |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 | [diff] [blame] | 203 | if (vp8_header.temporalIdx != kNoTemporalIdx) { |
| 204 | if (vp8_header.temporalIdx == 0) { |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 205 | ++state_.tl0_pic_idx; |
| 206 | } |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 | [diff] [blame] | 207 | vp8_header.tl0PicIdx = state_.tl0_pic_idx; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | if (rtp_video_header->codec == kVideoCodecVP9) { |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 211 | auto& vp9_header = |
| 212 | absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header); |
| 213 | vp9_header.picture_id = state_.picture_id; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 214 | |
| 215 | // Note that in the case that we have no temporal layers but we do have |
| 216 | // spatial layers, packets will carry layering info with a temporal_idx of |
| 217 | // zero, and we then have to set and increment tl0_pic_idx. |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 218 | if (vp9_header.temporal_idx != kNoTemporalIdx || |
| 219 | vp9_header.spatial_idx != kNoSpatialIdx) { |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 220 | if (first_frame_in_picture && |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 221 | (vp9_header.temporal_idx == 0 || |
| 222 | vp9_header.temporal_idx == kNoTemporalIdx)) { |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 223 | ++state_.tl0_pic_idx; |
| 224 | } |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 225 | vp9_header.tl0_pic_idx = state_.tl0_pic_idx; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 226 | } |
| 227 | } |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 228 | if (generic_picture_id_experiment_ && |
| 229 | rtp_video_header->codec == kVideoCodecGeneric) { |
Danil Chapovalov | b6bf0b2 | 2020-01-28 17:36:57 | [diff] [blame] | 230 | rtp_video_header->video_type_header.emplace<RTPVideoHeaderLegacyGeneric>() |
| 231 | .picture_id = state_.picture_id; |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 232 | } |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 233 | } |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 234 | |
Danil Chapovalov | 02d71fb | 2020-02-10 15:22:57 | [diff] [blame] | 235 | RTPVideoHeader::GenericDescriptorInfo |
| 236 | RtpPayloadParams::GenericDescriptorFromFrameInfo( |
| 237 | const GenericFrameInfo& frame_info, |
Danil Chapovalov | cf1308f | 2020-11-18 17:27:37 | [diff] [blame] | 238 | int64_t frame_id) { |
Danil Chapovalov | 02d71fb | 2020-02-10 15:22:57 | [diff] [blame] | 239 | RTPVideoHeader::GenericDescriptorInfo generic; |
| 240 | generic.frame_id = frame_id; |
| 241 | generic.dependencies = dependencies_calculator_.FromBuffersUsage( |
Danil Chapovalov | cf1308f | 2020-11-18 17:27:37 | [diff] [blame] | 242 | frame_id, frame_info.encoder_buffers); |
Danil Chapovalov | 4b860c1 | 2020-05-19 12:48:19 | [diff] [blame] | 243 | generic.chain_diffs = |
| 244 | chains_calculator_.From(frame_id, frame_info.part_of_chain); |
Danil Chapovalov | 02d71fb | 2020-02-10 15:22:57 | [diff] [blame] | 245 | generic.spatial_index = frame_info.spatial_id; |
| 246 | generic.temporal_index = frame_info.temporal_id; |
| 247 | generic.decode_target_indications = frame_info.decode_target_indications; |
Danil Chapovalov | e6ac8ff | 2020-06-26 11:51:08 | [diff] [blame] | 248 | generic.active_decode_targets = frame_info.active_decode_targets; |
Danil Chapovalov | 02d71fb | 2020-02-10 15:22:57 | [diff] [blame] | 249 | return generic; |
| 250 | } |
| 251 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 252 | void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info, |
| 253 | int64_t frame_id, |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 254 | bool is_keyframe, |
| 255 | RTPVideoHeader* rtp_video_header) { |
Danil Chapovalov | 02d71fb | 2020-02-10 15:22:57 | [diff] [blame] | 256 | if (codec_specific_info && codec_specific_info->generic_frame_info && |
| 257 | !codec_specific_info->generic_frame_info->encoder_buffers.empty()) { |
Danil Chapovalov | 4b860c1 | 2020-05-19 12:48:19 | [diff] [blame] | 258 | if (is_keyframe) { |
| 259 | // Key frame resets all chains it is in. |
| 260 | chains_calculator_.Reset( |
| 261 | codec_specific_info->generic_frame_info->part_of_chain); |
| 262 | } |
Danil Chapovalov | cf1308f | 2020-11-18 17:27:37 | [diff] [blame] | 263 | rtp_video_header->generic = GenericDescriptorFromFrameInfo( |
| 264 | *codec_specific_info->generic_frame_info, frame_id); |
Danil Chapovalov | 02d71fb | 2020-02-10 15:22:57 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 268 | switch (rtp_video_header->codec) { |
| 269 | case VideoCodecType::kVideoCodecGeneric: |
philipel | 8aba8fe | 2019-06-13 13:13:16 | [diff] [blame] | 270 | GenericToGeneric(frame_id, is_keyframe, rtp_video_header); |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 271 | return; |
| 272 | case VideoCodecType::kVideoCodecVP8: |
| 273 | if (codec_specific_info) { |
| 274 | Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id, |
| 275 | is_keyframe, rtp_video_header); |
| 276 | } |
| 277 | return; |
| 278 | case VideoCodecType::kVideoCodecVP9: |
Danil Chapovalov | dc36829 | 2019-11-26 13:48:20 | [diff] [blame] | 279 | case VideoCodecType::kVideoCodecAV1: |
| 280 | // TODO(philipel): Implement VP9 and AV1 to generic descriptor. |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 281 | return; |
| 282 | case VideoCodecType::kVideoCodecH264: |
philipel | 8aba8fe | 2019-06-13 13:13:16 | [diff] [blame] | 283 | if (codec_specific_info) { |
| 284 | H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id, |
| 285 | is_keyframe, rtp_video_header); |
| 286 | } |
| 287 | return; |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 288 | case VideoCodecType::kVideoCodecMultiplex: |
| 289 | return; |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 290 | } |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 291 | RTC_NOTREACHED() << "Unsupported codec."; |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 292 | } |
| 293 | |
philipel | 8aba8fe | 2019-06-13 13:13:16 | [diff] [blame] | 294 | void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id, |
| 295 | bool is_keyframe, |
| 296 | RTPVideoHeader* rtp_video_header) { |
| 297 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 298 | rtp_video_header->generic.emplace(); |
| 299 | |
| 300 | generic.frame_id = shared_frame_id; |
| 301 | |
| 302 | if (is_keyframe) { |
| 303 | last_shared_frame_id_[0].fill(-1); |
| 304 | } else { |
| 305 | int64_t frame_id = last_shared_frame_id_[0][0]; |
| 306 | RTC_DCHECK_NE(frame_id, -1); |
| 307 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 308 | generic.dependencies.push_back(frame_id); |
| 309 | } |
| 310 | |
| 311 | last_shared_frame_id_[0][0] = shared_frame_id; |
| 312 | } |
| 313 | |
| 314 | void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info, |
| 315 | int64_t shared_frame_id, |
| 316 | bool is_keyframe, |
| 317 | RTPVideoHeader* rtp_video_header) { |
| 318 | const int temporal_index = |
| 319 | h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0; |
| 320 | |
| 321 | if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) { |
| 322 | RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be " |
| 323 | "used with generic frame descriptor."; |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 328 | rtp_video_header->generic.emplace(); |
| 329 | |
| 330 | generic.frame_id = shared_frame_id; |
| 331 | generic.temporal_index = temporal_index; |
| 332 | |
| 333 | if (is_keyframe) { |
| 334 | RTC_DCHECK_EQ(temporal_index, 0); |
| 335 | last_shared_frame_id_[/*spatial index*/ 0].fill(-1); |
| 336 | last_shared_frame_id_[/*spatial index*/ 0][temporal_index] = |
| 337 | shared_frame_id; |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | if (h264_info.base_layer_sync) { |
| 342 | int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0]; |
| 343 | |
| 344 | for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) { |
| 345 | if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) { |
| 346 | last_shared_frame_id_[/*spatial index*/ 0][i] = -1; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | RTC_DCHECK_GE(tl0_frame_id, 0); |
| 351 | RTC_DCHECK_LT(tl0_frame_id, shared_frame_id); |
| 352 | generic.dependencies.push_back(tl0_frame_id); |
| 353 | } else { |
| 354 | for (int i = 0; i <= temporal_index; ++i) { |
| 355 | int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i]; |
| 356 | |
| 357 | if (frame_id != -1) { |
| 358 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 359 | generic.dependencies.push_back(frame_id); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id; |
| 365 | } |
| 366 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 367 | void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info, |
| 368 | int64_t shared_frame_id, |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 369 | bool is_keyframe, |
| 370 | RTPVideoHeader* rtp_video_header) { |
| 371 | const auto& vp8_header = |
| 372 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 373 | const int spatial_index = 0; |
| 374 | const int temporal_index = |
| 375 | vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0; |
| 376 | |
| 377 | if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers || |
| 378 | spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) { |
| 379 | RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be " |
| 380 | "used with generic frame descriptor."; |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 385 | rtp_video_header->generic.emplace(); |
| 386 | |
| 387 | generic.frame_id = shared_frame_id; |
| 388 | generic.spatial_index = spatial_index; |
| 389 | generic.temporal_index = temporal_index; |
| 390 | |
Qingsi Wang | 1c1b99e | 2020-01-07 19:16:33 | [diff] [blame] | 391 | if (vp8_info.useExplicitDependencies) { |
| 392 | SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe, |
| 393 | vp8_header.layerSync, &generic); |
| 394 | } else { |
| 395 | SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe, |
| 396 | spatial_index, temporal_index, |
| 397 | vp8_header.layerSync, &generic); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | void RtpPayloadParams::SetDependenciesVp8Deprecated( |
| 402 | const CodecSpecificInfoVP8& vp8_info, |
| 403 | int64_t shared_frame_id, |
| 404 | bool is_keyframe, |
| 405 | int spatial_index, |
| 406 | int temporal_index, |
| 407 | bool layer_sync, |
| 408 | RTPVideoHeader::GenericDescriptorInfo* generic) { |
| 409 | RTC_DCHECK(!vp8_info.useExplicitDependencies); |
| 410 | RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value()); |
| 411 | new_version_used_ = false; |
| 412 | |
| 413 | if (is_keyframe) { |
| 414 | RTC_DCHECK_EQ(temporal_index, 0); |
| 415 | last_shared_frame_id_[spatial_index].fill(-1); |
| 416 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | if (layer_sync) { |
| 421 | int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0]; |
| 422 | |
| 423 | for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) { |
| 424 | if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) { |
| 425 | last_shared_frame_id_[spatial_index][i] = -1; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | RTC_DCHECK_GE(tl0_frame_id, 0); |
| 430 | RTC_DCHECK_LT(tl0_frame_id, shared_frame_id); |
| 431 | generic->dependencies.push_back(tl0_frame_id); |
| 432 | } else { |
| 433 | for (int i = 0; i <= temporal_index; ++i) { |
| 434 | int64_t frame_id = last_shared_frame_id_[spatial_index][i]; |
| 435 | |
| 436 | if (frame_id != -1) { |
| 437 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 438 | generic->dependencies.push_back(frame_id); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 444 | } |
| 445 | |
| 446 | void RtpPayloadParams::SetDependenciesVp8New( |
| 447 | const CodecSpecificInfoVP8& vp8_info, |
| 448 | int64_t shared_frame_id, |
| 449 | bool is_keyframe, |
| 450 | bool layer_sync, |
| 451 | RTPVideoHeader::GenericDescriptorInfo* generic) { |
| 452 | RTC_DCHECK(vp8_info.useExplicitDependencies); |
| 453 | RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value()); |
| 454 | new_version_used_ = true; |
| 455 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 456 | if (is_keyframe) { |
| 457 | RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u); |
| 458 | buffer_id_to_frame_id_.fill(shared_frame_id); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount; |
| 463 | |
| 464 | RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u); |
| 465 | RTC_DCHECK_LE(vp8_info.referencedBuffersCount, |
| 466 | arraysize(vp8_info.referencedBuffers)); |
| 467 | |
| 468 | for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) { |
| 469 | const size_t referenced_buffer = vp8_info.referencedBuffers[i]; |
| 470 | RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8); |
| 471 | RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size()); |
| 472 | |
| 473 | const int64_t dependency_frame_id = |
| 474 | buffer_id_to_frame_id_[referenced_buffer]; |
| 475 | RTC_DCHECK_GE(dependency_frame_id, 0); |
| 476 | RTC_DCHECK_LT(dependency_frame_id, shared_frame_id); |
| 477 | |
| 478 | const bool is_new_dependency = |
Qingsi Wang | 1c1b99e | 2020-01-07 19:16:33 | [diff] [blame] | 479 | std::find(generic->dependencies.begin(), generic->dependencies.end(), |
| 480 | dependency_frame_id) == generic->dependencies.end(); |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 481 | if (is_new_dependency) { |
Qingsi Wang | 1c1b99e | 2020-01-07 19:16:33 | [diff] [blame] | 482 | generic->dependencies.push_back(dependency_frame_id); |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8); |
| 487 | for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) { |
| 488 | const size_t updated_id = vp8_info.updatedBuffers[i]; |
| 489 | buffer_id_to_frame_id_[updated_id] = shared_frame_id; |
| 490 | } |
| 491 | |
| 492 | RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8); |
| 493 | } |
| 494 | |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 495 | } // namespace webrtc |