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" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 18 | #include "absl/types/variant.h" |
| 19 | #include "api/video/video_timing.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 20 | #include "modules/video_coding/codecs/h264/include/h264_globals.h" |
| 21 | #include "modules/video_coding/codecs/interface/common_constants.h" |
| 22 | #include "modules/video_coding/codecs/vp8/include/vp8_globals.h" |
| 23 | #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 24 | #include "rtc_base/arraysize.h" |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 26 | #include "rtc_base/logging.h" |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 27 | #include "rtc_base/random.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "rtc_base/time_utils.h" |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 29 | #include "system_wrappers/include/field_trial.h" |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 30 | |
| 31 | namespace webrtc { |
| 32 | |
| 33 | namespace { |
| 34 | void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info, |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 35 | absl::optional<int> spatial_index, |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 36 | RTPVideoHeader* rtp) { |
| 37 | rtp->codec = info.codecType; |
| 38 | switch (info.codecType) { |
| 39 | case kVideoCodecVP8: { |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 | [diff] [blame] | 40 | auto& vp8_header = rtp->video_type_header.emplace<RTPVideoHeaderVP8>(); |
| 41 | vp8_header.InitRTPVideoHeaderVP8(); |
| 42 | vp8_header.nonReference = info.codecSpecific.VP8.nonReference; |
| 43 | vp8_header.temporalIdx = info.codecSpecific.VP8.temporalIdx; |
| 44 | vp8_header.layerSync = info.codecSpecific.VP8.layerSync; |
| 45 | vp8_header.keyIdx = info.codecSpecific.VP8.keyIdx; |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 46 | rtp->simulcastIdx = spatial_index.value_or(0); |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | case kVideoCodecVP9: { |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 50 | auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>(); |
| 51 | vp9_header.InitRTPVideoHeaderVP9(); |
| 52 | vp9_header.inter_pic_predicted = |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 53 | info.codecSpecific.VP9.inter_pic_predicted; |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 54 | vp9_header.flexible_mode = info.codecSpecific.VP9.flexible_mode; |
| 55 | vp9_header.ss_data_available = info.codecSpecific.VP9.ss_data_available; |
| 56 | vp9_header.non_ref_for_inter_layer_pred = |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 57 | info.codecSpecific.VP9.non_ref_for_inter_layer_pred; |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 58 | vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx; |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 59 | vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch; |
| 60 | vp9_header.inter_layer_predicted = |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 61 | info.codecSpecific.VP9.inter_layer_predicted; |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 62 | vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx; |
| 63 | vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers; |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 64 | if (vp9_header.num_spatial_layers > 1) { |
| 65 | vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx); |
| 66 | } else { |
| 67 | vp9_header.spatial_idx = kNoSpatialIdx; |
| 68 | } |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 69 | if (info.codecSpecific.VP9.ss_data_available) { |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 70 | vp9_header.spatial_layer_resolution_present = |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 71 | info.codecSpecific.VP9.spatial_layer_resolution_present; |
| 72 | if (info.codecSpecific.VP9.spatial_layer_resolution_present) { |
| 73 | for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers; |
| 74 | ++i) { |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 75 | vp9_header.width[i] = info.codecSpecific.VP9.width[i]; |
| 76 | vp9_header.height[i] = info.codecSpecific.VP9.height[i]; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 77 | } |
| 78 | } |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 79 | vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof); |
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.num_ref_pics = info.codecSpecific.VP9.num_ref_pics; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 83 | for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) { |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 84 | vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i]; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 85 | } |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 86 | vp9_header.end_of_picture = info.codecSpecific.VP9.end_of_picture; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | case kVideoCodecH264: { |
philipel | 7d745e5 | 2018-08-02 12:03:53 | [diff] [blame] | 90 | auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>(); |
| 91 | h264_header.packetization_mode = |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 92 | info.codecSpecific.H264.packetization_mode; |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 93 | rtp->simulcastIdx = spatial_index.value_or(0); |
Johnny Lee | 1a1c52b | 2019-02-08 19:25:40 | [diff] [blame] | 94 | rtp->frame_marking.temporal_id = kNoTemporalIdx; |
| 95 | if (info.codecSpecific.H264.temporal_idx != kNoTemporalIdx) { |
| 96 | rtp->frame_marking.temporal_id = info.codecSpecific.H264.temporal_idx; |
| 97 | rtp->frame_marking.layer_id = 0; |
| 98 | rtp->frame_marking.independent_frame = |
| 99 | info.codecSpecific.H264.idr_frame; |
| 100 | rtp->frame_marking.base_layer_sync = |
| 101 | info.codecSpecific.H264.base_layer_sync; |
| 102 | } |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | case kVideoCodecMultiplex: |
| 106 | case kVideoCodecGeneric: |
| 107 | rtp->codec = kVideoCodecGeneric; |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 108 | rtp->simulcastIdx = spatial_index.value_or(0); |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 109 | return; |
| 110 | default: |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) { |
| 116 | if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid || |
| 117 | image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) { |
| 118 | timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid; |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs( |
| 123 | image.capture_time_ms_, image.timing_.encode_start_ms); |
| 124 | timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs( |
| 125 | image.capture_time_ms_, image.timing_.encode_finish_ms); |
| 126 | timing->packetization_finish_delta_ms = 0; |
| 127 | timing->pacer_exit_delta_ms = 0; |
| 128 | timing->network_timestamp_delta_ms = 0; |
| 129 | timing->network2_timestamp_delta_ms = 0; |
| 130 | timing->flags = image.timing_.flags; |
| 131 | } |
| 132 | } // namespace |
| 133 | |
| 134 | RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc, |
| 135 | const RtpPayloadState* state) |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 136 | : ssrc_(ssrc), |
| 137 | generic_picture_id_experiment_( |
philipel | 569397f | 2018-09-26 10:25:31 | [diff] [blame] | 138 | field_trial::IsEnabled("WebRTC-GenericPictureId")), |
| 139 | generic_descriptor_experiment_( |
| 140 | field_trial::IsEnabled("WebRTC-GenericDescriptor")) { |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 141 | for (auto& spatial_layer : last_shared_frame_id_) |
| 142 | spatial_layer.fill(-1); |
| 143 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 144 | buffer_id_to_frame_id_.fill(-1); |
| 145 | |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 146 | Random random(rtc::TimeMicros()); |
| 147 | state_.picture_id = |
| 148 | state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF); |
| 149 | state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>()); |
| 150 | } |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 151 | |
| 152 | RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default; |
| 153 | |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 154 | RtpPayloadParams::~RtpPayloadParams() {} |
| 155 | |
| 156 | RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader( |
| 157 | const EncodedImage& image, |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 158 | const CodecSpecificInfo* codec_specific_info, |
| 159 | int64_t shared_frame_id) { |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 160 | RTPVideoHeader rtp_video_header; |
| 161 | if (codec_specific_info) { |
Niels Möller | d3b8c63 | 2018-08-27 13:33:42 | [diff] [blame] | 162 | PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(), |
| 163 | &rtp_video_header); |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 164 | } |
| 165 | rtp_video_header.rotation = image.rotation_; |
| 166 | rtp_video_header.content_type = image.content_type_; |
| 167 | rtp_video_header.playout_delay = image.playout_delay_; |
philipel | fab9129 | 2018-10-17 12:36:08 | [diff] [blame] | 168 | rtp_video_header.width = image._encodedWidth; |
| 169 | rtp_video_header.height = image._encodedHeight; |
Johannes Kron | d0b69a8 | 2018-12-03 13:18:53 | [diff] [blame] | 170 | rtp_video_header.color_space = image.ColorSpace() |
| 171 | ? absl::make_optional(*image.ColorSpace()) |
| 172 | : absl::nullopt; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 173 | SetVideoTiming(image, &rtp_video_header.video_timing); |
| 174 | |
Niels Möller | 8f7ce22 | 2019-03-21 14:43:58 | [diff] [blame] | 175 | const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 176 | const bool first_frame_in_picture = |
| 177 | (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9) |
| 178 | ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture |
| 179 | : true; |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 180 | |
| 181 | SetCodecSpecific(&rtp_video_header, first_frame_in_picture); |
philipel | 569397f | 2018-09-26 10:25:31 | [diff] [blame] | 182 | |
| 183 | if (generic_descriptor_experiment_) |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 184 | SetGeneric(codec_specific_info, shared_frame_id, is_keyframe, |
| 185 | &rtp_video_header); |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 186 | |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 187 | return rtp_video_header; |
| 188 | } |
| 189 | |
| 190 | uint32_t RtpPayloadParams::ssrc() const { |
| 191 | return ssrc_; |
| 192 | } |
| 193 | |
| 194 | RtpPayloadState RtpPayloadParams::state() const { |
| 195 | return state_; |
| 196 | } |
| 197 | |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 198 | void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header, |
| 199 | bool first_frame_in_picture) { |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 200 | // Always set picture id. Set tl0_pic_idx iff temporal index is set. |
| 201 | if (first_frame_in_picture) { |
| 202 | state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF; |
| 203 | } |
| 204 | if (rtp_video_header->codec == kVideoCodecVP8) { |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 | [diff] [blame] | 205 | auto& vp8_header = |
| 206 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 207 | vp8_header.pictureId = state_.picture_id; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 208 | |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 | [diff] [blame] | 209 | if (vp8_header.temporalIdx != kNoTemporalIdx) { |
| 210 | if (vp8_header.temporalIdx == 0) { |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 211 | ++state_.tl0_pic_idx; |
| 212 | } |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 | [diff] [blame] | 213 | vp8_header.tl0PicIdx = state_.tl0_pic_idx; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | if (rtp_video_header->codec == kVideoCodecVP9) { |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 217 | auto& vp9_header = |
| 218 | absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header); |
| 219 | vp9_header.picture_id = state_.picture_id; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 220 | |
| 221 | // Note that in the case that we have no temporal layers but we do have |
| 222 | // spatial layers, packets will carry layering info with a temporal_idx of |
| 223 | // zero, and we then have to set and increment tl0_pic_idx. |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 224 | if (vp9_header.temporal_idx != kNoTemporalIdx || |
| 225 | vp9_header.spatial_idx != kNoSpatialIdx) { |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 226 | if (first_frame_in_picture && |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 227 | (vp9_header.temporal_idx == 0 || |
| 228 | vp9_header.temporal_idx == kNoTemporalIdx)) { |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 229 | ++state_.tl0_pic_idx; |
| 230 | } |
philipel | 29d8846 | 2018-08-08 12:26:00 | [diff] [blame] | 231 | vp9_header.tl0_pic_idx = state_.tl0_pic_idx; |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 232 | } |
| 233 | } |
Johnny Lee | 1a1c52b | 2019-02-08 19:25:40 | [diff] [blame] | 234 | if (rtp_video_header->codec == kVideoCodecH264) { |
| 235 | if (rtp_video_header->frame_marking.temporal_id != kNoTemporalIdx) { |
| 236 | if (rtp_video_header->frame_marking.temporal_id == 0) { |
| 237 | ++state_.tl0_pic_idx; |
| 238 | } |
| 239 | rtp_video_header->frame_marking.tl0_pic_idx = state_.tl0_pic_idx; |
| 240 | } |
| 241 | } |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 242 | // There are currently two generic descriptors in WebRTC. The old descriptor |
| 243 | // can not share a picture id space between simulcast streams, so we use the |
| 244 | // |picture_id| in this case. We let the |picture_id| tag along in |frame_id| |
| 245 | // until the old generic format can be removed. |
| 246 | // TODO(philipel): Remove this when the new generic format has been fully |
| 247 | // implemented. |
| 248 | if (generic_picture_id_experiment_ && |
| 249 | rtp_video_header->codec == kVideoCodecGeneric) { |
| 250 | rtp_video_header->generic.emplace().frame_id = state_.picture_id; |
| 251 | } |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 252 | } |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 253 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 254 | void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info, |
| 255 | int64_t frame_id, |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 256 | bool is_keyframe, |
| 257 | RTPVideoHeader* rtp_video_header) { |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 258 | switch (rtp_video_header->codec) { |
| 259 | case VideoCodecType::kVideoCodecGeneric: |
philipel | 8aba8fe | 2019-06-13 13:13:16 | [diff] [blame] | 260 | GenericToGeneric(frame_id, is_keyframe, rtp_video_header); |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 261 | return; |
| 262 | case VideoCodecType::kVideoCodecVP8: |
| 263 | if (codec_specific_info) { |
| 264 | Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id, |
| 265 | is_keyframe, rtp_video_header); |
| 266 | } |
| 267 | return; |
| 268 | case VideoCodecType::kVideoCodecVP9: |
| 269 | // TODO(philipel): Implement VP9 to new generic descriptor. |
| 270 | return; |
| 271 | case VideoCodecType::kVideoCodecH264: |
philipel | 8aba8fe | 2019-06-13 13:13:16 | [diff] [blame] | 272 | if (codec_specific_info) { |
| 273 | H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id, |
| 274 | is_keyframe, rtp_video_header); |
| 275 | } |
| 276 | return; |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 277 | case VideoCodecType::kVideoCodecMultiplex: |
| 278 | return; |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 279 | } |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 280 | RTC_NOTREACHED() << "Unsupported codec."; |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 281 | } |
| 282 | |
philipel | 8aba8fe | 2019-06-13 13:13:16 | [diff] [blame] | 283 | void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id, |
| 284 | bool is_keyframe, |
| 285 | RTPVideoHeader* rtp_video_header) { |
| 286 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 287 | rtp_video_header->generic.emplace(); |
| 288 | |
| 289 | generic.frame_id = shared_frame_id; |
| 290 | |
| 291 | if (is_keyframe) { |
| 292 | last_shared_frame_id_[0].fill(-1); |
| 293 | } else { |
| 294 | int64_t frame_id = last_shared_frame_id_[0][0]; |
| 295 | RTC_DCHECK_NE(frame_id, -1); |
| 296 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 297 | generic.dependencies.push_back(frame_id); |
| 298 | } |
| 299 | |
| 300 | last_shared_frame_id_[0][0] = shared_frame_id; |
| 301 | } |
| 302 | |
| 303 | void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info, |
| 304 | int64_t shared_frame_id, |
| 305 | bool is_keyframe, |
| 306 | RTPVideoHeader* rtp_video_header) { |
| 307 | const int temporal_index = |
| 308 | h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0; |
| 309 | |
| 310 | if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) { |
| 311 | RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be " |
| 312 | "used with generic frame descriptor."; |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 317 | rtp_video_header->generic.emplace(); |
| 318 | |
| 319 | generic.frame_id = shared_frame_id; |
| 320 | generic.temporal_index = temporal_index; |
| 321 | |
| 322 | if (is_keyframe) { |
| 323 | RTC_DCHECK_EQ(temporal_index, 0); |
| 324 | last_shared_frame_id_[/*spatial index*/ 0].fill(-1); |
| 325 | last_shared_frame_id_[/*spatial index*/ 0][temporal_index] = |
| 326 | shared_frame_id; |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | if (h264_info.base_layer_sync) { |
| 331 | int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0]; |
| 332 | |
| 333 | for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) { |
| 334 | if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) { |
| 335 | last_shared_frame_id_[/*spatial index*/ 0][i] = -1; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | RTC_DCHECK_GE(tl0_frame_id, 0); |
| 340 | RTC_DCHECK_LT(tl0_frame_id, shared_frame_id); |
| 341 | generic.dependencies.push_back(tl0_frame_id); |
| 342 | } else { |
| 343 | for (int i = 0; i <= temporal_index; ++i) { |
| 344 | int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i]; |
| 345 | |
| 346 | if (frame_id != -1) { |
| 347 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 348 | generic.dependencies.push_back(frame_id); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id; |
| 354 | } |
| 355 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 356 | void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info, |
| 357 | int64_t shared_frame_id, |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 358 | bool is_keyframe, |
| 359 | RTPVideoHeader* rtp_video_header) { |
| 360 | const auto& vp8_header = |
| 361 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 362 | const int spatial_index = 0; |
| 363 | const int temporal_index = |
| 364 | vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0; |
| 365 | |
| 366 | if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers || |
| 367 | spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) { |
| 368 | RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be " |
| 369 | "used with generic frame descriptor."; |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 374 | rtp_video_header->generic.emplace(); |
| 375 | |
| 376 | generic.frame_id = shared_frame_id; |
| 377 | generic.spatial_index = spatial_index; |
| 378 | generic.temporal_index = temporal_index; |
| 379 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 380 | if (vp8_info.useExplicitDependencies) { |
| 381 | SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe, |
| 382 | vp8_header.layerSync, &generic); |
| 383 | } else { |
| 384 | SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe, |
| 385 | spatial_index, temporal_index, |
| 386 | vp8_header.layerSync, &generic); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void RtpPayloadParams::SetDependenciesVp8Deprecated( |
| 391 | const CodecSpecificInfoVP8& vp8_info, |
| 392 | int64_t shared_frame_id, |
| 393 | bool is_keyframe, |
| 394 | int spatial_index, |
| 395 | int temporal_index, |
| 396 | bool layer_sync, |
| 397 | RTPVideoHeader::GenericDescriptorInfo* generic) { |
| 398 | RTC_DCHECK(!vp8_info.useExplicitDependencies); |
| 399 | RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value()); |
| 400 | new_version_used_ = false; |
| 401 | |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 402 | if (is_keyframe) { |
| 403 | RTC_DCHECK_EQ(temporal_index, 0); |
| 404 | last_shared_frame_id_[spatial_index].fill(-1); |
| 405 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 406 | return; |
| 407 | } |
| 408 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 409 | if (layer_sync) { |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 410 | int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0]; |
| 411 | |
| 412 | for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) { |
| 413 | if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) { |
| 414 | last_shared_frame_id_[spatial_index][i] = -1; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | RTC_DCHECK_GE(tl0_frame_id, 0); |
| 419 | RTC_DCHECK_LT(tl0_frame_id, shared_frame_id); |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 420 | generic->dependencies.push_back(tl0_frame_id); |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 421 | } else { |
| 422 | for (int i = 0; i <= temporal_index; ++i) { |
| 423 | int64_t frame_id = last_shared_frame_id_[spatial_index][i]; |
| 424 | |
| 425 | if (frame_id != -1) { |
| 426 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 427 | generic->dependencies.push_back(frame_id); |
philipel | bf2b620 | 2018-08-27 12:33:18 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 433 | } |
| 434 | |
Elad Alon | f5b216a | 2019-01-28 13:25:17 | [diff] [blame] | 435 | void RtpPayloadParams::SetDependenciesVp8New( |
| 436 | const CodecSpecificInfoVP8& vp8_info, |
| 437 | int64_t shared_frame_id, |
| 438 | bool is_keyframe, |
| 439 | bool layer_sync, |
| 440 | RTPVideoHeader::GenericDescriptorInfo* generic) { |
| 441 | RTC_DCHECK(vp8_info.useExplicitDependencies); |
| 442 | RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value()); |
| 443 | new_version_used_ = true; |
| 444 | |
| 445 | if (is_keyframe) { |
| 446 | RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u); |
| 447 | buffer_id_to_frame_id_.fill(shared_frame_id); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount; |
| 452 | |
| 453 | RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u); |
| 454 | RTC_DCHECK_LE(vp8_info.referencedBuffersCount, |
| 455 | arraysize(vp8_info.referencedBuffers)); |
| 456 | |
| 457 | for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) { |
| 458 | const size_t referenced_buffer = vp8_info.referencedBuffers[i]; |
| 459 | RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8); |
| 460 | RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size()); |
| 461 | |
| 462 | const int64_t dependency_frame_id = |
| 463 | buffer_id_to_frame_id_[referenced_buffer]; |
| 464 | RTC_DCHECK_GE(dependency_frame_id, 0); |
| 465 | RTC_DCHECK_LT(dependency_frame_id, shared_frame_id); |
| 466 | |
| 467 | const bool is_new_dependency = |
| 468 | std::find(generic->dependencies.begin(), generic->dependencies.end(), |
| 469 | dependency_frame_id) == generic->dependencies.end(); |
| 470 | if (is_new_dependency) { |
| 471 | generic->dependencies.push_back(dependency_frame_id); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8); |
| 476 | for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) { |
| 477 | const size_t updated_id = vp8_info.updatedBuffers[i]; |
| 478 | buffer_id_to_frame_id_[updated_id] = shared_frame_id; |
| 479 | } |
| 480 | |
| 481 | RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8); |
| 482 | } |
| 483 | |
Stefan Holmer | f704468 | 2018-07-17 08:16:41 | [diff] [blame] | 484 | } // namespace webrtc |