blob: 6ff754990132852b2b946145bbceb309a44c628e [file] [log] [blame]
Stefan Holmerf7044682018-07-17 10:16:41 +02001/*
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 Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Elad Alonf5b216a2019-01-28 14:25:17 +010015#include <algorithm>
Yves Gerey3e707812018-11-28 16:47:49 +010016
17#include "absl/container/inlined_vector.h"
Erik Språngcbc0cba2020-04-18 14:36:59 +020018#include "absl/strings/match.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "absl/types/variant.h"
20#include "api/video/video_timing.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#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 Chapovalov02d71fb2020-02-10 16:22:57 +010025#include "modules/video_coding/frame_dependencies_calculator.h"
Elad Alonf5b216a2019-01-28 14:25:17 +010026#include "rtc_base/arraysize.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020027#include "rtc_base/checks.h"
philipelbf2b6202018-08-27 14:33:18 +020028#include "rtc_base/logging.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020029#include "rtc_base/random.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "rtc_base/time_utils.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020031
32namespace webrtc {
Stefan Holmerf7044682018-07-17 10:16:41 +020033namespace {
Danil Chapovalov5b298ab2022-06-08 11:18:51 +020034
35constexpr int kMaxSimulatedSpatialLayers = 3;
36
Stefan Holmerf7044682018-07-17 10:16:41 +020037void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info,
Niels Möllerd3b8c632018-08-27 15:33:42 +020038 absl::optional<int> spatial_index,
Stefan Holmerf7044682018-07-17 10:16:41 +020039 RTPVideoHeader* rtp) {
40 rtp->codec = info.codecType;
Danil Chapovalov62a9a322020-11-11 16:15:07 +010041 rtp->is_last_frame_in_picture = info.end_of_picture;
Stefan Holmerf7044682018-07-17 10:16:41 +020042 switch (info.codecType) {
43 case kVideoCodecVP8: {
Philip Eliassond52a1a62018-09-07 13:03:55 +000044 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;
Niels Möllerd3b8c632018-08-27 15:33:42 +020050 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020051 return;
52 }
53 case kVideoCodecVP9: {
philipel29d88462018-08-08 14:26:00 +020054 auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>();
55 vp9_header.InitRTPVideoHeaderVP9();
56 vp9_header.inter_pic_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020057 info.codecSpecific.VP9.inter_pic_predicted;
philipel29d88462018-08-08 14:26:00 +020058 vp9_header.flexible_mode = info.codecSpecific.VP9.flexible_mode;
59 vp9_header.ss_data_available = info.codecSpecific.VP9.ss_data_available;
60 vp9_header.non_ref_for_inter_layer_pred =
Stefan Holmerf7044682018-07-17 10:16:41 +020061 info.codecSpecific.VP9.non_ref_for_inter_layer_pred;
philipel29d88462018-08-08 14:26:00 +020062 vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx;
philipel29d88462018-08-08 14:26:00 +020063 vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch;
64 vp9_header.inter_layer_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020065 info.codecSpecific.VP9.inter_layer_predicted;
philipel29d88462018-08-08 14:26:00 +020066 vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx;
67 vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers;
Ilya Nikolaevskiyf5d87782020-02-04 10:06:33 +000068 vp9_header.first_active_layer = info.codecSpecific.VP9.first_active_layer;
Niels Möllerd3b8c632018-08-27 15:33:42 +020069 if (vp9_header.num_spatial_layers > 1) {
70 vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx);
71 } else {
72 vp9_header.spatial_idx = kNoSpatialIdx;
73 }
Stefan Holmerf7044682018-07-17 10:16:41 +020074 if (info.codecSpecific.VP9.ss_data_available) {
philipel29d88462018-08-08 14:26:00 +020075 vp9_header.spatial_layer_resolution_present =
Stefan Holmerf7044682018-07-17 10:16:41 +020076 info.codecSpecific.VP9.spatial_layer_resolution_present;
77 if (info.codecSpecific.VP9.spatial_layer_resolution_present) {
78 for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers;
79 ++i) {
philipel29d88462018-08-08 14:26:00 +020080 vp9_header.width[i] = info.codecSpecific.VP9.width[i];
81 vp9_header.height[i] = info.codecSpecific.VP9.height[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020082 }
83 }
philipel29d88462018-08-08 14:26:00 +020084 vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof);
Stefan Holmerf7044682018-07-17 10:16:41 +020085 }
86
philipel29d88462018-08-08 14:26:00 +020087 vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics;
Stefan Holmerf7044682018-07-17 10:16:41 +020088 for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) {
philipel29d88462018-08-08 14:26:00 +020089 vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020090 }
Danil Chapovalov06bbeb32020-11-11 12:42:56 +010091 vp9_header.end_of_picture = info.end_of_picture;
Stefan Holmerf7044682018-07-17 10:16:41 +020092 return;
93 }
94 case kVideoCodecH264: {
philipel7d745e52018-08-02 14:03:53 +020095 auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>();
96 h264_header.packetization_mode =
Stefan Holmerf7044682018-07-17 10:16:41 +020097 info.codecSpecific.H264.packetization_mode;
Niels Möllerd3b8c632018-08-27 15:33:42 +020098 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020099 return;
100 }
101 case kVideoCodecMultiplex:
102 case kVideoCodecGeneric:
103 rtp->codec = kVideoCodecGeneric;
Niels Möllerd3b8c632018-08-27 15:33:42 +0200104 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +0200105 return;
106 default:
107 return;
108 }
109}
110
111void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) {
112 if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid ||
113 image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) {
114 timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid;
115 return;
116 }
117
118 timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs(
119 image.capture_time_ms_, image.timing_.encode_start_ms);
120 timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs(
121 image.capture_time_ms_, image.timing_.encode_finish_ms);
122 timing->packetization_finish_delta_ms = 0;
123 timing->pacer_exit_delta_ms = 0;
124 timing->network_timestamp_delta_ms = 0;
125 timing->network2_timestamp_delta_ms = 0;
126 timing->flags = image.timing_.flags;
127}
Danil Chapovalov5b298ab2022-06-08 11:18:51 +0200128
129// Returns structure that aligns with simulated generic info. The templates
130// allow to produce valid dependency descriptor for any stream where
131// `num_spatial_layers` * `num_temporal_layers` <= 32 (limited by
132// https://aomediacodec.github.io/av1-rtp-spec/#a82-syntax, see
133// template_fdiffs()). The set of the templates is not tuned for any paricular
134// structure thus dependency descriptor would use more bytes on the wire than
135// with tuned templates.
136FrameDependencyStructure MinimalisticStructure(int num_spatial_layers,
137 int num_temporal_layers) {
138 RTC_DCHECK_LE(num_spatial_layers, DependencyDescriptor::kMaxSpatialIds);
139 RTC_DCHECK_LE(num_temporal_layers, DependencyDescriptor::kMaxTemporalIds);
140 RTC_DCHECK_LE(num_spatial_layers * num_temporal_layers, 32);
141 FrameDependencyStructure structure;
142 structure.num_decode_targets = num_spatial_layers * num_temporal_layers;
143 structure.num_chains = num_spatial_layers;
144 structure.templates.reserve(num_spatial_layers * num_temporal_layers);
145 for (int sid = 0; sid < num_spatial_layers; ++sid) {
146 for (int tid = 0; tid < num_temporal_layers; ++tid) {
147 FrameDependencyTemplate a_template;
148 a_template.spatial_id = sid;
149 a_template.temporal_id = tid;
150 for (int s = 0; s < num_spatial_layers; ++s) {
151 for (int t = 0; t < num_temporal_layers; ++t) {
152 // Prefer kSwitch indication for frames that is part of the decode
153 // target because dependency descriptor information generated in this
154 // class use kSwitch indications more often that kRequired, increasing
155 // the chance of a good (or complete) template match.
156 a_template.decode_target_indications.push_back(
157 sid <= s && tid <= t ? DecodeTargetIndication::kSwitch
158 : DecodeTargetIndication::kNotPresent);
159 }
160 }
161 a_template.frame_diffs.push_back(tid == 0 ? num_spatial_layers *
162 num_temporal_layers
163 : num_spatial_layers);
164 a_template.chain_diffs.assign(structure.num_chains, 1);
165 structure.templates.push_back(a_template);
166
167 structure.decode_target_protected_by_chain.push_back(sid);
168 }
169 }
170 return structure;
171}
Stefan Holmerf7044682018-07-17 10:16:41 +0200172} // namespace
173
174RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc,
Erik Språngcbc0cba2020-04-18 14:36:59 +0200175 const RtpPayloadState* state,
Jonas Orelande62c2f22022-03-29 11:04:48 +0200176 const FieldTrialsView& trials)
philipelbf2b6202018-08-27 14:33:18 +0200177 : ssrc_(ssrc),
178 generic_picture_id_experiment_(
Erik Språngcbc0cba2020-04-18 14:36:59 +0200179 absl::StartsWith(trials.Lookup("WebRTC-GenericPictureId"),
Danil Chapovalov5b298ab2022-06-08 11:18:51 +0200180 "Enabled")),
181 simulate_generic_structure_(absl::StartsWith(
182 trials.Lookup("WebRTC-GenericCodecDependencyDescriptor"),
183 "Enabled")) {
philipelbf2b6202018-08-27 14:33:18 +0200184 for (auto& spatial_layer : last_shared_frame_id_)
185 spatial_layer.fill(-1);
186
Emil Lundmarkadfc7002021-07-30 09:45:10 +0200187 chain_last_frame_id_.fill(-1);
Elad Alonf5b216a2019-01-28 14:25:17 +0100188 buffer_id_to_frame_id_.fill(-1);
189
Stefan Holmerf7044682018-07-17 10:16:41 +0200190 Random random(rtc::TimeMicros());
191 state_.picture_id =
192 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
193 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
194}
philipelbf2b6202018-08-27 14:33:18 +0200195
196RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
197
Stefan Holmerf7044682018-07-17 10:16:41 +0200198RtpPayloadParams::~RtpPayloadParams() {}
199
200RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
201 const EncodedImage& image,
philipelbf2b6202018-08-27 14:33:18 +0200202 const CodecSpecificInfo* codec_specific_info,
203 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200204 RTPVideoHeader rtp_video_header;
205 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200206 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
207 &rtp_video_header);
Stefan Holmerf7044682018-07-17 10:16:41 +0200208 }
Zhaoliang Maf3dc47e2021-02-05 13:19:02 +0800209 rtp_video_header.frame_type = image._frameType;
Stefan Holmerf7044682018-07-17 10:16:41 +0200210 rtp_video_header.rotation = image.rotation_;
211 rtp_video_header.content_type = image.content_type_;
212 rtp_video_header.playout_delay = image.playout_delay_;
philipelfab91292018-10-17 14:36:08 +0200213 rtp_video_header.width = image._encodedWidth;
214 rtp_video_header.height = image._encodedHeight;
Johannes Krond0b69a82018-12-03 14:18:53 +0100215 rtp_video_header.color_space = image.ColorSpace()
216 ? absl::make_optional(*image.ColorSpace())
217 : absl::nullopt;
Jeremy Leconteb258c562021-03-18 13:50:42 +0100218 rtp_video_header.video_frame_tracking_id = image.VideoFrameTrackingId();
Stefan Holmerf7044682018-07-17 10:16:41 +0200219 SetVideoTiming(image, &rtp_video_header.video_timing);
220
Niels Möller8f7ce222019-03-21 15:43:58 +0100221 const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey;
Stefan Holmerf7044682018-07-17 10:16:41 +0200222 const bool first_frame_in_picture =
223 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
224 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
225 : true;
philipelbf2b6202018-08-27 14:33:18 +0200226
227 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
philipel569397f2018-09-26 12:25:31 +0200228
Danil Chapovalov636865e2020-06-03 14:11:26 +0200229 SetGeneric(codec_specific_info, shared_frame_id, is_keyframe,
230 &rtp_video_header);
philipelbf2b6202018-08-27 14:33:18 +0200231
Stefan Holmerf7044682018-07-17 10:16:41 +0200232 return rtp_video_header;
233}
234
235uint32_t RtpPayloadParams::ssrc() const {
236 return ssrc_;
237}
238
239RtpPayloadState RtpPayloadParams::state() const {
240 return state_;
241}
242
philipelbf2b6202018-08-27 14:33:18 +0200243void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
244 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200245 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
246 if (first_frame_in_picture) {
247 state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
248 }
249 if (rtp_video_header->codec == kVideoCodecVP8) {
Philip Eliassond52a1a62018-09-07 13:03:55 +0000250 auto& vp8_header =
251 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
252 vp8_header.pictureId = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200253
Philip Eliassond52a1a62018-09-07 13:03:55 +0000254 if (vp8_header.temporalIdx != kNoTemporalIdx) {
255 if (vp8_header.temporalIdx == 0) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200256 ++state_.tl0_pic_idx;
257 }
Philip Eliassond52a1a62018-09-07 13:03:55 +0000258 vp8_header.tl0PicIdx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200259 }
260 }
261 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 14:26:00 +0200262 auto& vp9_header =
263 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
264 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200265
266 // Note that in the case that we have no temporal layers but we do have
267 // spatial layers, packets will carry layering info with a temporal_idx of
268 // zero, and we then have to set and increment tl0_pic_idx.
philipel29d88462018-08-08 14:26:00 +0200269 if (vp9_header.temporal_idx != kNoTemporalIdx ||
270 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200271 if (first_frame_in_picture &&
philipel29d88462018-08-08 14:26:00 +0200272 (vp9_header.temporal_idx == 0 ||
273 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200274 ++state_.tl0_pic_idx;
275 }
philipel29d88462018-08-08 14:26:00 +0200276 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200277 }
278 }
philipelbf2b6202018-08-27 14:33:18 +0200279 if (generic_picture_id_experiment_ &&
280 rtp_video_header->codec == kVideoCodecGeneric) {
Danil Chapovalovb6bf0b22020-01-28 18:36:57 +0100281 rtp_video_header->video_type_header.emplace<RTPVideoHeaderLegacyGeneric>()
282 .picture_id = state_.picture_id;
philipelbf2b6202018-08-27 14:33:18 +0200283 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200284}
philipelbf2b6202018-08-27 14:33:18 +0200285
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100286RTPVideoHeader::GenericDescriptorInfo
287RtpPayloadParams::GenericDescriptorFromFrameInfo(
288 const GenericFrameInfo& frame_info,
Danil Chapovalovcf1308f2020-11-18 18:27:37 +0100289 int64_t frame_id) {
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100290 RTPVideoHeader::GenericDescriptorInfo generic;
291 generic.frame_id = frame_id;
292 generic.dependencies = dependencies_calculator_.FromBuffersUsage(
Danil Chapovalovcf1308f2020-11-18 18:27:37 +0100293 frame_id, frame_info.encoder_buffers);
Danil Chapovalov4b860c12020-05-19 14:48:19 +0200294 generic.chain_diffs =
295 chains_calculator_.From(frame_id, frame_info.part_of_chain);
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100296 generic.spatial_index = frame_info.spatial_id;
297 generic.temporal_index = frame_info.temporal_id;
298 generic.decode_target_indications = frame_info.decode_target_indications;
Danil Chapovalove6ac8ff2020-06-26 13:51:08 +0200299 generic.active_decode_targets = frame_info.active_decode_targets;
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100300 return generic;
301}
302
Elad Alonf5b216a2019-01-28 14:25:17 +0100303void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info,
304 int64_t frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200305 bool is_keyframe,
306 RTPVideoHeader* rtp_video_header) {
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100307 if (codec_specific_info && codec_specific_info->generic_frame_info &&
308 !codec_specific_info->generic_frame_info->encoder_buffers.empty()) {
Danil Chapovalov4b860c12020-05-19 14:48:19 +0200309 if (is_keyframe) {
310 // Key frame resets all chains it is in.
311 chains_calculator_.Reset(
312 codec_specific_info->generic_frame_info->part_of_chain);
313 }
Danil Chapovalovcf1308f2020-11-18 18:27:37 +0100314 rtp_video_header->generic = GenericDescriptorFromFrameInfo(
315 *codec_specific_info->generic_frame_info, frame_id);
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100316 return;
317 }
318
Elad Alonf5b216a2019-01-28 14:25:17 +0100319 switch (rtp_video_header->codec) {
320 case VideoCodecType::kVideoCodecGeneric:
philipel8aba8fe2019-06-13 15:13:16 +0200321 GenericToGeneric(frame_id, is_keyframe, rtp_video_header);
Elad Alonf5b216a2019-01-28 14:25:17 +0100322 return;
323 case VideoCodecType::kVideoCodecVP8:
324 if (codec_specific_info) {
325 Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id,
326 is_keyframe, rtp_video_header);
327 }
328 return;
329 case VideoCodecType::kVideoCodecVP9:
Emil Lundmark823ba0b2021-10-18 11:27:26 +0200330 if (codec_specific_info != nullptr) {
Danil Chapovalovaf366442021-04-22 15:20:28 +0200331 Vp9ToGeneric(codec_specific_info->codecSpecific.VP9, frame_id,
332 *rtp_video_header);
333 }
334 return;
Danil Chapovalovdc368292019-11-26 14:48:20 +0100335 case VideoCodecType::kVideoCodecAV1:
Danil Chapovalovaf366442021-04-22 15:20:28 +0200336 // TODO(philipel): Implement AV1 to generic descriptor.
Elad Alonf5b216a2019-01-28 14:25:17 +0100337 return;
338 case VideoCodecType::kVideoCodecH264:
philipel8aba8fe2019-06-13 15:13:16 +0200339 if (codec_specific_info) {
340 H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id,
341 is_keyframe, rtp_video_header);
342 }
343 return;
Elad Alonf5b216a2019-01-28 14:25:17 +0100344 case VideoCodecType::kVideoCodecMultiplex:
345 return;
philipelbf2b6202018-08-27 14:33:18 +0200346 }
Artem Titovd3251962021-11-15 16:57:07 +0100347 RTC_DCHECK_NOTREACHED() << "Unsupported codec.";
philipelbf2b6202018-08-27 14:33:18 +0200348}
349
Danil Chapovalov5b298ab2022-06-08 11:18:51 +0200350absl::optional<FrameDependencyStructure> RtpPayloadParams::GenericStructure(
351 const CodecSpecificInfo* codec_specific_info) {
352 if (codec_specific_info == nullptr) {
353 return absl::nullopt;
354 }
355 // This helper shouldn't be used when template structure is specified
356 // explicetly.
357 RTC_DCHECK(!codec_specific_info->template_structure.has_value());
358 switch (codec_specific_info->codecType) {
359 case VideoCodecType::kVideoCodecGeneric:
360 if (simulate_generic_structure_) {
361 return MinimalisticStructure(/*num_spatial_layers=*/1,
362 /*num_temporal_layer=*/1);
363 }
364 return absl::nullopt;
365 case VideoCodecType::kVideoCodecVP8:
366 return MinimalisticStructure(/*num_spatial_layers=*/1,
367 /*num_temporal_layer=*/kMaxTemporalStreams);
368 case VideoCodecType::kVideoCodecVP9: {
369 absl::optional<FrameDependencyStructure> structure =
370 MinimalisticStructure(
371 /*num_spatial_layers=*/kMaxSimulatedSpatialLayers,
372 /*num_temporal_layer=*/kMaxTemporalStreams);
373 const CodecSpecificInfoVP9& vp9 = codec_specific_info->codecSpecific.VP9;
374 if (vp9.ss_data_available && vp9.spatial_layer_resolution_present) {
375 RenderResolution first_valid;
376 RenderResolution last_valid;
377 for (size_t i = 0; i < vp9.num_spatial_layers; ++i) {
378 RenderResolution r(vp9.width[i], vp9.height[i]);
379 if (r.Valid()) {
380 if (!first_valid.Valid()) {
381 first_valid = r;
382 }
383 last_valid = r;
384 }
385 structure->resolutions.push_back(r);
386 }
387 if (!last_valid.Valid()) {
388 // No valid resolution found. Do not send resolutions.
389 structure->resolutions.clear();
390 } else {
391 structure->resolutions.resize(kMaxSimulatedSpatialLayers, last_valid);
392 // VP9 encoder wrapper may disable first few spatial layers by
393 // setting invalid resolution (0,0). `structure->resolutions`
394 // doesn't support invalid resolution, so reset them to something
395 // valid.
396 for (RenderResolution& r : structure->resolutions) {
397 if (!r.Valid()) {
398 r = first_valid;
399 }
400 }
401 }
402 }
403 return structure;
404 }
405 case VideoCodecType::kVideoCodecAV1:
406 case VideoCodecType::kVideoCodecH264:
407 case VideoCodecType::kVideoCodecMultiplex:
408 return absl::nullopt;
409 }
410 RTC_DCHECK_NOTREACHED() << "Unsupported codec.";
411}
412
philipel8aba8fe2019-06-13 15:13:16 +0200413void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id,
414 bool is_keyframe,
415 RTPVideoHeader* rtp_video_header) {
416 RTPVideoHeader::GenericDescriptorInfo& generic =
417 rtp_video_header->generic.emplace();
418
419 generic.frame_id = shared_frame_id;
philipel5b231de2021-09-01 15:21:16 +0200420 generic.decode_target_indications.push_back(DecodeTargetIndication::kSwitch);
philipel8aba8fe2019-06-13 15:13:16 +0200421
422 if (is_keyframe) {
philipel5b231de2021-09-01 15:21:16 +0200423 generic.chain_diffs.push_back(0);
philipel8aba8fe2019-06-13 15:13:16 +0200424 last_shared_frame_id_[0].fill(-1);
425 } else {
426 int64_t frame_id = last_shared_frame_id_[0][0];
427 RTC_DCHECK_NE(frame_id, -1);
428 RTC_DCHECK_LT(frame_id, shared_frame_id);
philipel5b231de2021-09-01 15:21:16 +0200429 generic.chain_diffs.push_back(shared_frame_id - frame_id);
philipel8aba8fe2019-06-13 15:13:16 +0200430 generic.dependencies.push_back(frame_id);
431 }
432
433 last_shared_frame_id_[0][0] = shared_frame_id;
434}
435
436void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info,
437 int64_t shared_frame_id,
438 bool is_keyframe,
439 RTPVideoHeader* rtp_video_header) {
440 const int temporal_index =
441 h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0;
442
443 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) {
444 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
445 "used with generic frame descriptor.";
446 return;
447 }
448
449 RTPVideoHeader::GenericDescriptorInfo& generic =
450 rtp_video_header->generic.emplace();
451
452 generic.frame_id = shared_frame_id;
453 generic.temporal_index = temporal_index;
454
455 if (is_keyframe) {
456 RTC_DCHECK_EQ(temporal_index, 0);
457 last_shared_frame_id_[/*spatial index*/ 0].fill(-1);
458 last_shared_frame_id_[/*spatial index*/ 0][temporal_index] =
459 shared_frame_id;
460 return;
461 }
462
463 if (h264_info.base_layer_sync) {
464 int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0];
465
466 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
467 if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) {
468 last_shared_frame_id_[/*spatial index*/ 0][i] = -1;
469 }
470 }
471
472 RTC_DCHECK_GE(tl0_frame_id, 0);
473 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
474 generic.dependencies.push_back(tl0_frame_id);
475 } else {
476 for (int i = 0; i <= temporal_index; ++i) {
477 int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i];
478
479 if (frame_id != -1) {
480 RTC_DCHECK_LT(frame_id, shared_frame_id);
481 generic.dependencies.push_back(frame_id);
482 }
483 }
484 }
485
486 last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id;
487}
488
Elad Alonf5b216a2019-01-28 14:25:17 +0100489void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info,
490 int64_t shared_frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200491 bool is_keyframe,
492 RTPVideoHeader* rtp_video_header) {
493 const auto& vp8_header =
494 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
495 const int spatial_index = 0;
496 const int temporal_index =
497 vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0;
498
499 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers ||
500 spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) {
501 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
502 "used with generic frame descriptor.";
503 return;
504 }
505
506 RTPVideoHeader::GenericDescriptorInfo& generic =
507 rtp_video_header->generic.emplace();
508
509 generic.frame_id = shared_frame_id;
510 generic.spatial_index = spatial_index;
511 generic.temporal_index = temporal_index;
512
Emil Lundmark6c81a422022-05-18 17:13:34 +0200513 // Generate decode target indications.
514 RTC_DCHECK_LT(temporal_index, kMaxTemporalStreams);
515 generic.decode_target_indications.resize(kMaxTemporalStreams);
516 auto it = std::fill_n(generic.decode_target_indications.begin(),
517 temporal_index, DecodeTargetIndication::kNotPresent);
518 std::fill(it, generic.decode_target_indications.end(),
519 DecodeTargetIndication::kSwitch);
520
521 // Frame dependencies.
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000522 if (vp8_info.useExplicitDependencies) {
523 SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe,
524 vp8_header.layerSync, &generic);
525 } else {
526 SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe,
527 spatial_index, temporal_index,
528 vp8_header.layerSync, &generic);
529 }
Emil Lundmark6c81a422022-05-18 17:13:34 +0200530
531 // Calculate chains.
532 generic.chain_diffs = {
533 (is_keyframe || chain_last_frame_id_[0] < 0)
534 ? 0
535 : static_cast<int>(shared_frame_id - chain_last_frame_id_[0])};
536 if (temporal_index == 0) {
537 chain_last_frame_id_[0] = shared_frame_id;
538 }
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000539}
540
Danil Chapovalovaf366442021-04-22 15:20:28 +0200541void RtpPayloadParams::Vp9ToGeneric(const CodecSpecificInfoVP9& vp9_info,
542 int64_t shared_frame_id,
543 RTPVideoHeader& rtp_video_header) {
544 const auto& vp9_header =
545 absl::get<RTPVideoHeaderVP9>(rtp_video_header.video_type_header);
Danil Chapovalov5b298ab2022-06-08 11:18:51 +0200546 const int num_spatial_layers = kMaxSimulatedSpatialLayers;
547 const int num_active_spatial_layers = vp9_header.num_spatial_layers;
Danil Chapovalovaf366442021-04-22 15:20:28 +0200548 const int num_temporal_layers = kMaxTemporalStreams;
Danil Chapovalov5b298ab2022-06-08 11:18:51 +0200549 static_assert(num_spatial_layers <=
550 RtpGenericFrameDescriptor::kMaxSpatialLayers);
551 static_assert(num_temporal_layers <=
552 RtpGenericFrameDescriptor::kMaxTemporalLayers);
553 static_assert(num_spatial_layers <= DependencyDescriptor::kMaxSpatialIds);
554 static_assert(num_temporal_layers <= DependencyDescriptor::kMaxTemporalIds);
Danil Chapovalovaf366442021-04-22 15:20:28 +0200555
556 int spatial_index =
557 vp9_header.spatial_idx != kNoSpatialIdx ? vp9_header.spatial_idx : 0;
558 int temporal_index =
559 vp9_header.temporal_idx != kNoTemporalIdx ? vp9_header.temporal_idx : 0;
560
561 if (spatial_index >= num_spatial_layers ||
562 temporal_index >= num_temporal_layers ||
Danil Chapovalov5b298ab2022-06-08 11:18:51 +0200563 num_active_spatial_layers > num_spatial_layers) {
Danil Chapovalovaf366442021-04-22 15:20:28 +0200564 // Prefer to generate no generic layering than an inconsistent one.
565 return;
566 }
567
568 RTPVideoHeader::GenericDescriptorInfo& result =
569 rtp_video_header.generic.emplace();
570
571 result.frame_id = shared_frame_id;
572 result.spatial_index = spatial_index;
573 result.temporal_index = temporal_index;
574
575 result.decode_target_indications.reserve(num_spatial_layers *
576 num_temporal_layers);
577 for (int sid = 0; sid < num_spatial_layers; ++sid) {
578 for (int tid = 0; tid < num_temporal_layers; ++tid) {
579 DecodeTargetIndication dti;
580 if (sid < spatial_index || tid < temporal_index) {
581 dti = DecodeTargetIndication::kNotPresent;
582 } else if (spatial_index != sid &&
583 vp9_header.non_ref_for_inter_layer_pred) {
584 dti = DecodeTargetIndication::kNotPresent;
585 } else if (sid == spatial_index && tid == temporal_index) {
586 // Assume that if frame is decodable, all of its own layer is decodable.
587 dti = DecodeTargetIndication::kSwitch;
588 } else if (sid == spatial_index && vp9_header.temporal_up_switch) {
589 dti = DecodeTargetIndication::kSwitch;
590 } else if (!vp9_header.inter_pic_predicted) {
591 // Key frame or spatial upswitch
592 dti = DecodeTargetIndication::kSwitch;
593 } else {
594 // Make no other assumptions. That should be safe, though suboptimal.
595 // To provide more accurate dti, encoder wrapper should fill in
596 // CodecSpecificInfo::generic_frame_info
597 dti = DecodeTargetIndication::kRequired;
598 }
599 result.decode_target_indications.push_back(dti);
600 }
601 }
602
603 // Calculate frame dependencies.
604 static constexpr int kPictureDiffLimit = 128;
605 if (last_vp9_frame_id_.empty()) {
606 // Create the array only if it is ever used.
607 last_vp9_frame_id_.resize(kPictureDiffLimit);
608 }
609 if (vp9_header.inter_layer_predicted && spatial_index > 0) {
610 result.dependencies.push_back(
611 last_vp9_frame_id_[vp9_header.picture_id % kPictureDiffLimit]
612 [spatial_index - 1]);
613 }
614 if (vp9_header.inter_pic_predicted) {
615 for (size_t i = 0; i < vp9_header.num_ref_pics; ++i) {
616 // picture_id is 15 bit number that wraps around. Though undeflow may
617 // produce picture that exceeds 2^15, it is ok because in this
618 // code block only last 7 bits of the picture_id are used.
619 uint16_t depend_on = vp9_header.picture_id - vp9_header.pid_diff[i];
620 result.dependencies.push_back(
621 last_vp9_frame_id_[depend_on % kPictureDiffLimit][spatial_index]);
622 }
623 }
624 last_vp9_frame_id_[vp9_header.picture_id % kPictureDiffLimit][spatial_index] =
625 shared_frame_id;
626
Danil Chapovalov5b298ab2022-06-08 11:18:51 +0200627 result.active_decode_targets =
628 ((uint32_t{1} << num_temporal_layers * num_active_spatial_layers) - 1);
629
Danil Chapovalovaf366442021-04-22 15:20:28 +0200630 // Calculate chains, asuming chain includes all frames with temporal_id = 0
631 if (!vp9_header.inter_pic_predicted && !vp9_header.inter_layer_predicted) {
632 // Assume frames without dependencies also reset chains.
633 for (int sid = spatial_index; sid < num_spatial_layers; ++sid) {
634 chain_last_frame_id_[sid] = -1;
635 }
636 }
Danil Chapovalov5b298ab2022-06-08 11:18:51 +0200637 result.chain_diffs.resize(num_spatial_layers, 0);
638 for (int sid = 0; sid < num_active_spatial_layers; ++sid) {
Danil Chapovalovaf366442021-04-22 15:20:28 +0200639 if (chain_last_frame_id_[sid] == -1) {
640 result.chain_diffs[sid] = 0;
641 continue;
642 }
643 result.chain_diffs[sid] = shared_frame_id - chain_last_frame_id_[sid];
644 }
645
646 if (temporal_index == 0) {
647 chain_last_frame_id_[spatial_index] = shared_frame_id;
648 if (!vp9_header.non_ref_for_inter_layer_pred) {
649 for (int sid = spatial_index + 1; sid < num_spatial_layers; ++sid) {
650 chain_last_frame_id_[sid] = shared_frame_id;
651 }
652 }
653 }
654}
655
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000656void RtpPayloadParams::SetDependenciesVp8Deprecated(
657 const CodecSpecificInfoVP8& vp8_info,
658 int64_t shared_frame_id,
659 bool is_keyframe,
660 int spatial_index,
661 int temporal_index,
662 bool layer_sync,
663 RTPVideoHeader::GenericDescriptorInfo* generic) {
664 RTC_DCHECK(!vp8_info.useExplicitDependencies);
665 RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value());
666 new_version_used_ = false;
667
668 if (is_keyframe) {
669 RTC_DCHECK_EQ(temporal_index, 0);
670 last_shared_frame_id_[spatial_index].fill(-1);
671 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
672 return;
673 }
674
675 if (layer_sync) {
676 int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0];
677
678 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
679 if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) {
680 last_shared_frame_id_[spatial_index][i] = -1;
681 }
682 }
683
684 RTC_DCHECK_GE(tl0_frame_id, 0);
685 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
686 generic->dependencies.push_back(tl0_frame_id);
687 } else {
688 for (int i = 0; i <= temporal_index; ++i) {
689 int64_t frame_id = last_shared_frame_id_[spatial_index][i];
690
691 if (frame_id != -1) {
692 RTC_DCHECK_LT(frame_id, shared_frame_id);
693 generic->dependencies.push_back(frame_id);
694 }
695 }
696 }
697
698 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
699}
700
701void RtpPayloadParams::SetDependenciesVp8New(
702 const CodecSpecificInfoVP8& vp8_info,
703 int64_t shared_frame_id,
704 bool is_keyframe,
705 bool layer_sync,
706 RTPVideoHeader::GenericDescriptorInfo* generic) {
707 RTC_DCHECK(vp8_info.useExplicitDependencies);
708 RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value());
709 new_version_used_ = true;
710
Elad Alonf5b216a2019-01-28 14:25:17 +0100711 if (is_keyframe) {
712 RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u);
713 buffer_id_to_frame_id_.fill(shared_frame_id);
714 return;
715 }
716
717 constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount;
718
719 RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u);
720 RTC_DCHECK_LE(vp8_info.referencedBuffersCount,
721 arraysize(vp8_info.referencedBuffers));
722
723 for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) {
724 const size_t referenced_buffer = vp8_info.referencedBuffers[i];
725 RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8);
726 RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size());
727
728 const int64_t dependency_frame_id =
729 buffer_id_to_frame_id_[referenced_buffer];
730 RTC_DCHECK_GE(dependency_frame_id, 0);
731 RTC_DCHECK_LT(dependency_frame_id, shared_frame_id);
732
733 const bool is_new_dependency =
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000734 std::find(generic->dependencies.begin(), generic->dependencies.end(),
735 dependency_frame_id) == generic->dependencies.end();
Elad Alonf5b216a2019-01-28 14:25:17 +0100736 if (is_new_dependency) {
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000737 generic->dependencies.push_back(dependency_frame_id);
Elad Alonf5b216a2019-01-28 14:25:17 +0100738 }
739 }
740
741 RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8);
742 for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) {
743 const size_t updated_id = vp8_info.updatedBuffers[i];
744 buffer_id_to_frame_id_[updated_id] = shared_frame_id;
745 }
746
747 RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8);
748}
749
Stefan Holmerf7044682018-07-17 10:16:41 +0200750} // namespace webrtc