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