blob: ed7620e06c4e7ba17bf3e0b1be2f0bf482107a93 [file] [log] [blame]
Stefan Holmerf7044682018-07-17 08:16:411/*
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 15:47:4913#include <stddef.h>
Jonas Olssona4d87372019-07-05 17:08:3314
Elad Alonf5b216a2019-01-28 13:25:1715#include <algorithm>
Yves Gerey3e707812018-11-28 15:47:4916
17#include "absl/container/inlined_vector.h"
Erik Språngcbc0cba2020-04-18 12:36:5918#include "absl/strings/match.h"
Yves Gerey3e707812018-11-28 15:47:4919#include "absl/types/variant.h"
20#include "api/video/video_timing.h"
Yves Gerey3e707812018-11-28 15:47:4921#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 15:22:5725#include "modules/video_coding/frame_dependencies_calculator.h"
Elad Alonf5b216a2019-01-28 13:25:1726#include "rtc_base/arraysize.h"
Stefan Holmerf7044682018-07-17 08:16:4127#include "rtc_base/checks.h"
philipelbf2b6202018-08-27 12:33:1828#include "rtc_base/logging.h"
Stefan Holmerf7044682018-07-17 08:16:4129#include "rtc_base/random.h"
Steve Anton10542f22019-01-11 17:11:0030#include "rtc_base/time_utils.h"
Stefan Holmerf7044682018-07-17 08:16:4131
32namespace webrtc {
Stefan Holmerf7044682018-07-17 08:16:4133namespace {
Danil Chapovalov5b298ab2022-06-08 09:18:5134
35constexpr int kMaxSimulatedSpatialLayers = 3;
36
Stefan Holmerf7044682018-07-17 08:16:4137void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info,
Niels Möllerd3b8c632018-08-27 13:33:4238 absl::optional<int> spatial_index,
Stefan Holmerf7044682018-07-17 08:16:4139 RTPVideoHeader* rtp) {
40 rtp->codec = info.codecType;
Danil Chapovalov62a9a322020-11-11 15:15:0741 rtp->is_last_frame_in_picture = info.end_of_picture;
Stefan Holmerf7044682018-07-17 08:16:4142 switch (info.codecType) {
43 case kVideoCodecVP8: {
Philip Eliassond52a1a62018-09-07 13:03:5544 auto& vp8_header = rtp->video_type_header.emplace<RTPVideoHeaderVP8>();
45 vp8_header.InitRTPVideoHeaderVP8();
46 vp8_header.nonReference = info.codecSpecific.VP8.nonReference;
47 vp8_header.temporalIdx = info.codecSpecific.VP8.temporalIdx;
48 vp8_header.layerSync = info.codecSpecific.VP8.layerSync;
49 vp8_header.keyIdx = info.codecSpecific.VP8.keyIdx;
Stefan Holmerf7044682018-07-17 08:16:4150 return;
51 }
52 case kVideoCodecVP9: {
philipel29d88462018-08-08 12:26:0053 auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>();
54 vp9_header.InitRTPVideoHeaderVP9();
55 vp9_header.inter_pic_predicted =
Stefan Holmerf7044682018-07-17 08:16:4156 info.codecSpecific.VP9.inter_pic_predicted;
philipel29d88462018-08-08 12:26:0057 vp9_header.flexible_mode = info.codecSpecific.VP9.flexible_mode;
58 vp9_header.ss_data_available = info.codecSpecific.VP9.ss_data_available;
59 vp9_header.non_ref_for_inter_layer_pred =
Stefan Holmerf7044682018-07-17 08:16:4160 info.codecSpecific.VP9.non_ref_for_inter_layer_pred;
philipel29d88462018-08-08 12:26:0061 vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx;
philipel29d88462018-08-08 12:26:0062 vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch;
63 vp9_header.inter_layer_predicted =
Stefan Holmerf7044682018-07-17 08:16:4164 info.codecSpecific.VP9.inter_layer_predicted;
philipel29d88462018-08-08 12:26:0065 vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx;
66 vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers;
Ilya Nikolaevskiyf5d87782020-02-04 10:06:3367 vp9_header.first_active_layer = info.codecSpecific.VP9.first_active_layer;
Niels Möllerd3b8c632018-08-27 13:33:4268 if (vp9_header.num_spatial_layers > 1) {
69 vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx);
70 } else {
71 vp9_header.spatial_idx = kNoSpatialIdx;
72 }
Stefan Holmerf7044682018-07-17 08:16:4173 if (info.codecSpecific.VP9.ss_data_available) {
philipel29d88462018-08-08 12:26:0074 vp9_header.spatial_layer_resolution_present =
Stefan Holmerf7044682018-07-17 08:16:4175 info.codecSpecific.VP9.spatial_layer_resolution_present;
76 if (info.codecSpecific.VP9.spatial_layer_resolution_present) {
77 for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers;
78 ++i) {
philipel29d88462018-08-08 12:26:0079 vp9_header.width[i] = info.codecSpecific.VP9.width[i];
80 vp9_header.height[i] = info.codecSpecific.VP9.height[i];
Stefan Holmerf7044682018-07-17 08:16:4181 }
82 }
philipel29d88462018-08-08 12:26:0083 vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof);
Stefan Holmerf7044682018-07-17 08:16:4184 }
85
philipel29d88462018-08-08 12:26:0086 vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics;
Stefan Holmerf7044682018-07-17 08:16:4187 for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) {
philipel29d88462018-08-08 12:26:0088 vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i];
Stefan Holmerf7044682018-07-17 08:16:4189 }
Danil Chapovalov06bbeb32020-11-11 11:42:5690 vp9_header.end_of_picture = info.end_of_picture;
Stefan Holmerf7044682018-07-17 08:16:4191 return;
92 }
93 case kVideoCodecH264: {
philipel7d745e52018-08-02 12:03:5394 auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>();
95 h264_header.packetization_mode =
Stefan Holmerf7044682018-07-17 08:16:4196 info.codecSpecific.H264.packetization_mode;
Stefan Holmerf7044682018-07-17 08:16:4197 return;
98 }
99 case kVideoCodecMultiplex:
100 case kVideoCodecGeneric:
101 rtp->codec = kVideoCodecGeneric;
Stefan Holmerf7044682018-07-17 08:16:41102 return;
103 default:
104 return;
105 }
106}
107
108void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) {
109 if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid ||
110 image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) {
111 timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid;
112 return;
113 }
114
115 timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs(
116 image.capture_time_ms_, image.timing_.encode_start_ms);
117 timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs(
118 image.capture_time_ms_, image.timing_.encode_finish_ms);
119 timing->packetization_finish_delta_ms = 0;
120 timing->pacer_exit_delta_ms = 0;
121 timing->network_timestamp_delta_ms = 0;
122 timing->network2_timestamp_delta_ms = 0;
123 timing->flags = image.timing_.flags;
124}
Danil Chapovalov5b298ab2022-06-08 09:18:51125
126// Returns structure that aligns with simulated generic info. The templates
127// allow to produce valid dependency descriptor for any stream where
128// `num_spatial_layers` * `num_temporal_layers` <= 32 (limited by
129// https://aomediacodec.github.io/av1-rtp-spec/#a82-syntax, see
130// template_fdiffs()). The set of the templates is not tuned for any paricular
131// structure thus dependency descriptor would use more bytes on the wire than
132// with tuned templates.
133FrameDependencyStructure MinimalisticStructure(int num_spatial_layers,
134 int num_temporal_layers) {
135 RTC_DCHECK_LE(num_spatial_layers, DependencyDescriptor::kMaxSpatialIds);
136 RTC_DCHECK_LE(num_temporal_layers, DependencyDescriptor::kMaxTemporalIds);
137 RTC_DCHECK_LE(num_spatial_layers * num_temporal_layers, 32);
138 FrameDependencyStructure structure;
139 structure.num_decode_targets = num_spatial_layers * num_temporal_layers;
140 structure.num_chains = num_spatial_layers;
141 structure.templates.reserve(num_spatial_layers * num_temporal_layers);
142 for (int sid = 0; sid < num_spatial_layers; ++sid) {
143 for (int tid = 0; tid < num_temporal_layers; ++tid) {
144 FrameDependencyTemplate a_template;
145 a_template.spatial_id = sid;
146 a_template.temporal_id = tid;
147 for (int s = 0; s < num_spatial_layers; ++s) {
148 for (int t = 0; t < num_temporal_layers; ++t) {
149 // Prefer kSwitch indication for frames that is part of the decode
150 // target because dependency descriptor information generated in this
151 // class use kSwitch indications more often that kRequired, increasing
152 // the chance of a good (or complete) template match.
153 a_template.decode_target_indications.push_back(
154 sid <= s && tid <= t ? DecodeTargetIndication::kSwitch
155 : DecodeTargetIndication::kNotPresent);
156 }
157 }
158 a_template.frame_diffs.push_back(tid == 0 ? num_spatial_layers *
159 num_temporal_layers
160 : num_spatial_layers);
161 a_template.chain_diffs.assign(structure.num_chains, 1);
162 structure.templates.push_back(a_template);
163
164 structure.decode_target_protected_by_chain.push_back(sid);
165 }
166 }
167 return structure;
168}
Stefan Holmerf7044682018-07-17 08:16:41169} // namespace
170
171RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc,
Erik Språngcbc0cba2020-04-18 12:36:59172 const RtpPayloadState* state,
Jonas Orelande62c2f22022-03-29 09:04:48173 const FieldTrialsView& trials)
philipelbf2b6202018-08-27 12:33:18174 : ssrc_(ssrc),
175 generic_picture_id_experiment_(
Erik Språngcbc0cba2020-04-18 12:36:59176 absl::StartsWith(trials.Lookup("WebRTC-GenericPictureId"),
Danil Chapovalov5b298ab2022-06-08 09:18:51177 "Enabled")),
178 simulate_generic_structure_(absl::StartsWith(
179 trials.Lookup("WebRTC-GenericCodecDependencyDescriptor"),
180 "Enabled")) {
philipelbf2b6202018-08-27 12:33:18181 for (auto& spatial_layer : last_shared_frame_id_)
182 spatial_layer.fill(-1);
183
Emil Lundmarkadfc7002021-07-30 07:45:10184 chain_last_frame_id_.fill(-1);
Elad Alonf5b216a2019-01-28 13:25:17185 buffer_id_to_frame_id_.fill(-1);
186
Stefan Holmerf7044682018-07-17 08:16:41187 Random random(rtc::TimeMicros());
188 state_.picture_id =
189 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
190 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
191}
philipelbf2b6202018-08-27 12:33:18192
193RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
194
Stefan Holmerf7044682018-07-17 08:16:41195RtpPayloadParams::~RtpPayloadParams() {}
196
197RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
198 const EncodedImage& image,
philipelbf2b6202018-08-27 12:33:18199 const CodecSpecificInfo* codec_specific_info,
200 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 08:16:41201 RTPVideoHeader rtp_video_header;
202 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 13:33:42203 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
204 &rtp_video_header);
Stefan Holmerf7044682018-07-17 08:16:41205 }
Henrik Boströmc5a4c932023-02-21 14:53:43206 rtp_video_header.simulcastIdx = image.SimulcastIndex().value_or(0);
Zhaoliang Maf3dc47e2021-02-05 05:19:02207 rtp_video_header.frame_type = image._frameType;
Stefan Holmerf7044682018-07-17 08:16:41208 rtp_video_header.rotation = image.rotation_;
209 rtp_video_header.content_type = image.content_type_;
210 rtp_video_header.playout_delay = image.playout_delay_;
philipelfab91292018-10-17 12:36:08211 rtp_video_header.width = image._encodedWidth;
212 rtp_video_header.height = image._encodedHeight;
Johannes Krond0b69a82018-12-03 13:18:53213 rtp_video_header.color_space = image.ColorSpace()
214 ? absl::make_optional(*image.ColorSpace())
215 : absl::nullopt;
Jeremy Leconteb258c562021-03-18 12:50:42216 rtp_video_header.video_frame_tracking_id = image.VideoFrameTrackingId();
Stefan Holmerf7044682018-07-17 08:16:41217 SetVideoTiming(image, &rtp_video_header.video_timing);
218
Niels Möller8f7ce222019-03-21 14:43:58219 const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey;
Stefan Holmerf7044682018-07-17 08:16:41220 const bool first_frame_in_picture =
221 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
222 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
223 : true;
philipelbf2b6202018-08-27 12:33:18224
225 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
philipel569397f2018-09-26 10:25:31226
Danil Chapovalov636865e2020-06-03 12:11:26227 SetGeneric(codec_specific_info, shared_frame_id, is_keyframe,
228 &rtp_video_header);
philipelbf2b6202018-08-27 12:33:18229
Stefan Holmerf7044682018-07-17 08:16:41230 return rtp_video_header;
231}
232
233uint32_t RtpPayloadParams::ssrc() const {
234 return ssrc_;
235}
236
237RtpPayloadState RtpPayloadParams::state() const {
238 return state_;
239}
240
philipelbf2b6202018-08-27 12:33:18241void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
242 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 08:16:41243 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
244 if (first_frame_in_picture) {
245 state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
246 }
247 if (rtp_video_header->codec == kVideoCodecVP8) {
Philip Eliassond52a1a62018-09-07 13:03:55248 auto& vp8_header =
249 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
250 vp8_header.pictureId = state_.picture_id;
Stefan Holmerf7044682018-07-17 08:16:41251
Philip Eliassond52a1a62018-09-07 13:03:55252 if (vp8_header.temporalIdx != kNoTemporalIdx) {
253 if (vp8_header.temporalIdx == 0) {
Stefan Holmerf7044682018-07-17 08:16:41254 ++state_.tl0_pic_idx;
255 }
Philip Eliassond52a1a62018-09-07 13:03:55256 vp8_header.tl0PicIdx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 08:16:41257 }
258 }
259 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 12:26:00260 auto& vp9_header =
261 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
262 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 08:16:41263
264 // Note that in the case that we have no temporal layers but we do have
265 // spatial layers, packets will carry layering info with a temporal_idx of
266 // zero, and we then have to set and increment tl0_pic_idx.
philipel29d88462018-08-08 12:26:00267 if (vp9_header.temporal_idx != kNoTemporalIdx ||
268 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 08:16:41269 if (first_frame_in_picture &&
philipel29d88462018-08-08 12:26:00270 (vp9_header.temporal_idx == 0 ||
271 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 08:16:41272 ++state_.tl0_pic_idx;
273 }
philipel29d88462018-08-08 12:26:00274 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 08:16:41275 }
276 }
philipelbf2b6202018-08-27 12:33:18277 if (generic_picture_id_experiment_ &&
278 rtp_video_header->codec == kVideoCodecGeneric) {
Danil Chapovalovb6bf0b22020-01-28 17:36:57279 rtp_video_header->video_type_header.emplace<RTPVideoHeaderLegacyGeneric>()
280 .picture_id = state_.picture_id;
philipelbf2b6202018-08-27 12:33:18281 }
Stefan Holmerf7044682018-07-17 08:16:41282}
philipelbf2b6202018-08-27 12:33:18283
Danil Chapovalov02d71fb2020-02-10 15:22:57284RTPVideoHeader::GenericDescriptorInfo
285RtpPayloadParams::GenericDescriptorFromFrameInfo(
286 const GenericFrameInfo& frame_info,
Danil Chapovalovcf1308f2020-11-18 17:27:37287 int64_t frame_id) {
Danil Chapovalov02d71fb2020-02-10 15:22:57288 RTPVideoHeader::GenericDescriptorInfo generic;
289 generic.frame_id = frame_id;
290 generic.dependencies = dependencies_calculator_.FromBuffersUsage(
Danil Chapovalovcf1308f2020-11-18 17:27:37291 frame_id, frame_info.encoder_buffers);
Danil Chapovalov4b860c12020-05-19 12:48:19292 generic.chain_diffs =
293 chains_calculator_.From(frame_id, frame_info.part_of_chain);
Danil Chapovalov02d71fb2020-02-10 15:22:57294 generic.spatial_index = frame_info.spatial_id;
295 generic.temporal_index = frame_info.temporal_id;
296 generic.decode_target_indications = frame_info.decode_target_indications;
Danil Chapovalove6ac8ff2020-06-26 11:51:08297 generic.active_decode_targets = frame_info.active_decode_targets;
Danil Chapovalov02d71fb2020-02-10 15:22:57298 return generic;
299}
300
Elad Alonf5b216a2019-01-28 13:25:17301void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info,
302 int64_t frame_id,
philipelbf2b6202018-08-27 12:33:18303 bool is_keyframe,
304 RTPVideoHeader* rtp_video_header) {
Danil Chapovalov02d71fb2020-02-10 15:22:57305 if (codec_specific_info && codec_specific_info->generic_frame_info &&
306 !codec_specific_info->generic_frame_info->encoder_buffers.empty()) {
Danil Chapovalov4b860c12020-05-19 12:48:19307 if (is_keyframe) {
308 // Key frame resets all chains it is in.
309 chains_calculator_.Reset(
310 codec_specific_info->generic_frame_info->part_of_chain);
311 }
Danil Chapovalovcf1308f2020-11-18 17:27:37312 rtp_video_header->generic = GenericDescriptorFromFrameInfo(
313 *codec_specific_info->generic_frame_info, frame_id);
Danil Chapovalov02d71fb2020-02-10 15:22:57314 return;
315 }
316
Elad Alonf5b216a2019-01-28 13:25:17317 switch (rtp_video_header->codec) {
318 case VideoCodecType::kVideoCodecGeneric:
philipel8aba8fe2019-06-13 13:13:16319 GenericToGeneric(frame_id, is_keyframe, rtp_video_header);
Elad Alonf5b216a2019-01-28 13:25:17320 return;
321 case VideoCodecType::kVideoCodecVP8:
322 if (codec_specific_info) {
323 Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id,
324 is_keyframe, rtp_video_header);
325 }
326 return;
327 case VideoCodecType::kVideoCodecVP9:
Emil Lundmark823ba0b2021-10-18 09:27:26328 if (codec_specific_info != nullptr) {
Danil Chapovalovaf366442021-04-22 13:20:28329 Vp9ToGeneric(codec_specific_info->codecSpecific.VP9, frame_id,
330 *rtp_video_header);
331 }
332 return;
Danil Chapovalovdc368292019-11-26 13:48:20333 case VideoCodecType::kVideoCodecAV1:
Danil Chapovalovaf366442021-04-22 13:20:28334 // TODO(philipel): Implement AV1 to generic descriptor.
Elad Alonf5b216a2019-01-28 13:25:17335 return;
336 case VideoCodecType::kVideoCodecH264:
philipel8aba8fe2019-06-13 13:13:16337 if (codec_specific_info) {
338 H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id,
339 is_keyframe, rtp_video_header);
340 }
341 return;
Elad Alonf5b216a2019-01-28 13:25:17342 case VideoCodecType::kVideoCodecMultiplex:
343 return;
philipelbf2b6202018-08-27 12:33:18344 }
Artem Titovd3251962021-11-15 15:57:07345 RTC_DCHECK_NOTREACHED() << "Unsupported codec.";
philipelbf2b6202018-08-27 12:33:18346}
347
Danil Chapovalov5b298ab2022-06-08 09:18:51348absl::optional<FrameDependencyStructure> RtpPayloadParams::GenericStructure(
349 const CodecSpecificInfo* codec_specific_info) {
350 if (codec_specific_info == nullptr) {
351 return absl::nullopt;
352 }
353 // This helper shouldn't be used when template structure is specified
354 // explicetly.
355 RTC_DCHECK(!codec_specific_info->template_structure.has_value());
356 switch (codec_specific_info->codecType) {
357 case VideoCodecType::kVideoCodecGeneric:
358 if (simulate_generic_structure_) {
359 return MinimalisticStructure(/*num_spatial_layers=*/1,
360 /*num_temporal_layer=*/1);
361 }
362 return absl::nullopt;
363 case VideoCodecType::kVideoCodecVP8:
364 return MinimalisticStructure(/*num_spatial_layers=*/1,
365 /*num_temporal_layer=*/kMaxTemporalStreams);
366 case VideoCodecType::kVideoCodecVP9: {
367 absl::optional<FrameDependencyStructure> structure =
368 MinimalisticStructure(
369 /*num_spatial_layers=*/kMaxSimulatedSpatialLayers,
370 /*num_temporal_layer=*/kMaxTemporalStreams);
371 const CodecSpecificInfoVP9& vp9 = codec_specific_info->codecSpecific.VP9;
372 if (vp9.ss_data_available && vp9.spatial_layer_resolution_present) {
373 RenderResolution first_valid;
374 RenderResolution last_valid;
375 for (size_t i = 0; i < vp9.num_spatial_layers; ++i) {
376 RenderResolution r(vp9.width[i], vp9.height[i]);
377 if (r.Valid()) {
378 if (!first_valid.Valid()) {
379 first_valid = r;
380 }
381 last_valid = r;
382 }
383 structure->resolutions.push_back(r);
384 }
385 if (!last_valid.Valid()) {
386 // No valid resolution found. Do not send resolutions.
387 structure->resolutions.clear();
388 } else {
389 structure->resolutions.resize(kMaxSimulatedSpatialLayers, last_valid);
390 // VP9 encoder wrapper may disable first few spatial layers by
391 // setting invalid resolution (0,0). `structure->resolutions`
392 // doesn't support invalid resolution, so reset them to something
393 // valid.
394 for (RenderResolution& r : structure->resolutions) {
395 if (!r.Valid()) {
396 r = first_valid;
397 }
398 }
399 }
400 }
401 return structure;
402 }
403 case VideoCodecType::kVideoCodecAV1:
404 case VideoCodecType::kVideoCodecH264:
405 case VideoCodecType::kVideoCodecMultiplex:
406 return absl::nullopt;
407 }
408 RTC_DCHECK_NOTREACHED() << "Unsupported codec.";
409}
410
philipel8aba8fe2019-06-13 13:13:16411void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id,
412 bool is_keyframe,
413 RTPVideoHeader* rtp_video_header) {
414 RTPVideoHeader::GenericDescriptorInfo& generic =
415 rtp_video_header->generic.emplace();
416
417 generic.frame_id = shared_frame_id;
philipel5b231de2021-09-01 13:21:16418 generic.decode_target_indications.push_back(DecodeTargetIndication::kSwitch);
philipel8aba8fe2019-06-13 13:13:16419
420 if (is_keyframe) {
philipel5b231de2021-09-01 13:21:16421 generic.chain_diffs.push_back(0);
philipel8aba8fe2019-06-13 13:13:16422 last_shared_frame_id_[0].fill(-1);
423 } else {
424 int64_t frame_id = last_shared_frame_id_[0][0];
425 RTC_DCHECK_NE(frame_id, -1);
426 RTC_DCHECK_LT(frame_id, shared_frame_id);
philipel5b231de2021-09-01 13:21:16427 generic.chain_diffs.push_back(shared_frame_id - frame_id);
philipel8aba8fe2019-06-13 13:13:16428 generic.dependencies.push_back(frame_id);
429 }
430
431 last_shared_frame_id_[0][0] = shared_frame_id;
432}
433
434void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info,
435 int64_t shared_frame_id,
436 bool is_keyframe,
437 RTPVideoHeader* rtp_video_header) {
438 const int temporal_index =
439 h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0;
440
441 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) {
442 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
443 "used with generic frame descriptor.";
444 return;
445 }
446
447 RTPVideoHeader::GenericDescriptorInfo& generic =
448 rtp_video_header->generic.emplace();
449
450 generic.frame_id = shared_frame_id;
451 generic.temporal_index = temporal_index;
452
453 if (is_keyframe) {
454 RTC_DCHECK_EQ(temporal_index, 0);
455 last_shared_frame_id_[/*spatial index*/ 0].fill(-1);
456 last_shared_frame_id_[/*spatial index*/ 0][temporal_index] =
457 shared_frame_id;
458 return;
459 }
460
461 if (h264_info.base_layer_sync) {
462 int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0];
463
464 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
465 if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) {
466 last_shared_frame_id_[/*spatial index*/ 0][i] = -1;
467 }
468 }
469
470 RTC_DCHECK_GE(tl0_frame_id, 0);
471 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
472 generic.dependencies.push_back(tl0_frame_id);
473 } else {
474 for (int i = 0; i <= temporal_index; ++i) {
475 int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i];
476
477 if (frame_id != -1) {
478 RTC_DCHECK_LT(frame_id, shared_frame_id);
479 generic.dependencies.push_back(frame_id);
480 }
481 }
482 }
483
484 last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id;
485}
486
Elad Alonf5b216a2019-01-28 13:25:17487void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info,
488 int64_t shared_frame_id,
philipelbf2b6202018-08-27 12:33:18489 bool is_keyframe,
490 RTPVideoHeader* rtp_video_header) {
491 const auto& vp8_header =
492 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
493 const int spatial_index = 0;
494 const int temporal_index =
495 vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0;
496
497 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers ||
498 spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) {
499 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
500 "used with generic frame descriptor.";
501 return;
502 }
503
504 RTPVideoHeader::GenericDescriptorInfo& generic =
505 rtp_video_header->generic.emplace();
506
507 generic.frame_id = shared_frame_id;
508 generic.spatial_index = spatial_index;
509 generic.temporal_index = temporal_index;
510
Emil Lundmark6c81a422022-05-18 15:13:34511 // Generate decode target indications.
512 RTC_DCHECK_LT(temporal_index, kMaxTemporalStreams);
513 generic.decode_target_indications.resize(kMaxTemporalStreams);
514 auto it = std::fill_n(generic.decode_target_indications.begin(),
515 temporal_index, DecodeTargetIndication::kNotPresent);
516 std::fill(it, generic.decode_target_indications.end(),
517 DecodeTargetIndication::kSwitch);
518
519 // Frame dependencies.
Qingsi Wang1c1b99e2020-01-07 19:16:33520 if (vp8_info.useExplicitDependencies) {
521 SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe,
522 vp8_header.layerSync, &generic);
523 } else {
524 SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe,
525 spatial_index, temporal_index,
526 vp8_header.layerSync, &generic);
527 }
Emil Lundmark6c81a422022-05-18 15:13:34528
529 // Calculate chains.
530 generic.chain_diffs = {
531 (is_keyframe || chain_last_frame_id_[0] < 0)
532 ? 0
533 : static_cast<int>(shared_frame_id - chain_last_frame_id_[0])};
534 if (temporal_index == 0) {
535 chain_last_frame_id_[0] = shared_frame_id;
536 }
Qingsi Wang1c1b99e2020-01-07 19:16:33537}
538
Danil Chapovalovaf366442021-04-22 13:20:28539void RtpPayloadParams::Vp9ToGeneric(const CodecSpecificInfoVP9& vp9_info,
540 int64_t shared_frame_id,
541 RTPVideoHeader& rtp_video_header) {
542 const auto& vp9_header =
543 absl::get<RTPVideoHeaderVP9>(rtp_video_header.video_type_header);
Danil Chapovalov5b298ab2022-06-08 09:18:51544 const int num_spatial_layers = kMaxSimulatedSpatialLayers;
545 const int num_active_spatial_layers = vp9_header.num_spatial_layers;
Danil Chapovalovaf366442021-04-22 13:20:28546 const int num_temporal_layers = kMaxTemporalStreams;
Danil Chapovalov5b298ab2022-06-08 09:18:51547 static_assert(num_spatial_layers <=
548 RtpGenericFrameDescriptor::kMaxSpatialLayers);
549 static_assert(num_temporal_layers <=
550 RtpGenericFrameDescriptor::kMaxTemporalLayers);
551 static_assert(num_spatial_layers <= DependencyDescriptor::kMaxSpatialIds);
552 static_assert(num_temporal_layers <= DependencyDescriptor::kMaxTemporalIds);
Danil Chapovalovaf366442021-04-22 13:20:28553
554 int spatial_index =
555 vp9_header.spatial_idx != kNoSpatialIdx ? vp9_header.spatial_idx : 0;
556 int temporal_index =
557 vp9_header.temporal_idx != kNoTemporalIdx ? vp9_header.temporal_idx : 0;
558
559 if (spatial_index >= num_spatial_layers ||
560 temporal_index >= num_temporal_layers ||
Danil Chapovalov5b298ab2022-06-08 09:18:51561 num_active_spatial_layers > num_spatial_layers) {
Danil Chapovalovaf366442021-04-22 13:20:28562 // Prefer to generate no generic layering than an inconsistent one.
563 return;
564 }
565
566 RTPVideoHeader::GenericDescriptorInfo& result =
567 rtp_video_header.generic.emplace();
568
569 result.frame_id = shared_frame_id;
570 result.spatial_index = spatial_index;
571 result.temporal_index = temporal_index;
572
573 result.decode_target_indications.reserve(num_spatial_layers *
574 num_temporal_layers);
575 for (int sid = 0; sid < num_spatial_layers; ++sid) {
576 for (int tid = 0; tid < num_temporal_layers; ++tid) {
577 DecodeTargetIndication dti;
578 if (sid < spatial_index || tid < temporal_index) {
579 dti = DecodeTargetIndication::kNotPresent;
580 } else if (spatial_index != sid &&
581 vp9_header.non_ref_for_inter_layer_pred) {
582 dti = DecodeTargetIndication::kNotPresent;
583 } else if (sid == spatial_index && tid == temporal_index) {
584 // Assume that if frame is decodable, all of its own layer is decodable.
585 dti = DecodeTargetIndication::kSwitch;
586 } else if (sid == spatial_index && vp9_header.temporal_up_switch) {
587 dti = DecodeTargetIndication::kSwitch;
588 } else if (!vp9_header.inter_pic_predicted) {
589 // Key frame or spatial upswitch
590 dti = DecodeTargetIndication::kSwitch;
591 } else {
592 // Make no other assumptions. That should be safe, though suboptimal.
593 // To provide more accurate dti, encoder wrapper should fill in
594 // CodecSpecificInfo::generic_frame_info
595 dti = DecodeTargetIndication::kRequired;
596 }
597 result.decode_target_indications.push_back(dti);
598 }
599 }
600
601 // Calculate frame dependencies.
602 static constexpr int kPictureDiffLimit = 128;
603 if (last_vp9_frame_id_.empty()) {
604 // Create the array only if it is ever used.
605 last_vp9_frame_id_.resize(kPictureDiffLimit);
606 }
philipel4e0bf2e2023-06-20 08:29:20607
608 if (vp9_header.flexible_mode) {
609 if (vp9_header.inter_layer_predicted && spatial_index > 0) {
Danil Chapovalovaf366442021-04-22 13:20:28610 result.dependencies.push_back(
philipel4e0bf2e2023-06-20 08:29:20611 last_vp9_frame_id_[vp9_header.picture_id % kPictureDiffLimit]
612 [spatial_index - 1]);
Danil Chapovalovaf366442021-04-22 13:20:28613 }
philipel4e0bf2e2023-06-20 08:29:20614 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]
625 [spatial_index] = shared_frame_id;
626 } else {
627 // Implementing general conversion logic for non-flexible mode requires some
628 // work and we will almost certainly never need it, so for now support only
629 // non-layerd streams.
630 if (spatial_index > 0 || temporal_index > 0) {
631 // Prefer to generate no generic layering than an inconsistent one.
632 rtp_video_header.generic.reset();
633 return;
634 }
635
636 if (vp9_header.inter_pic_predicted) {
637 // Since we only support non-scalable streams we only need to save the
638 // last frame id.
639 result.dependencies.push_back(last_vp9_frame_id_[0][0]);
640 }
641 last_vp9_frame_id_[0][0] = shared_frame_id;
Danil Chapovalovaf366442021-04-22 13:20:28642 }
Danil Chapovalovaf366442021-04-22 13:20:28643
Danil Chapovalov5b298ab2022-06-08 09:18:51644 result.active_decode_targets =
645 ((uint32_t{1} << num_temporal_layers * num_active_spatial_layers) - 1);
646
Danil Chapovalovaf366442021-04-22 13:20:28647 // Calculate chains, asuming chain includes all frames with temporal_id = 0
648 if (!vp9_header.inter_pic_predicted && !vp9_header.inter_layer_predicted) {
649 // Assume frames without dependencies also reset chains.
650 for (int sid = spatial_index; sid < num_spatial_layers; ++sid) {
651 chain_last_frame_id_[sid] = -1;
652 }
653 }
Danil Chapovalov5b298ab2022-06-08 09:18:51654 result.chain_diffs.resize(num_spatial_layers, 0);
655 for (int sid = 0; sid < num_active_spatial_layers; ++sid) {
Danil Chapovalovaf366442021-04-22 13:20:28656 if (chain_last_frame_id_[sid] == -1) {
657 result.chain_diffs[sid] = 0;
658 continue;
659 }
Danil Chapovalov50ca7012023-08-02 09:28:58660 int64_t chain_diff = shared_frame_id - chain_last_frame_id_[sid];
661 if (chain_diff >= 256) {
662 RTC_LOG(LS_ERROR)
663 << "Too many frames since last VP9 T0 frame for spatial layer #"
664 << sid << " at frame#" << shared_frame_id;
665 chain_last_frame_id_[sid] = -1;
666 chain_diff = 0;
667 }
668 result.chain_diffs[sid] = chain_diff;
Danil Chapovalovaf366442021-04-22 13:20:28669 }
670
671 if (temporal_index == 0) {
672 chain_last_frame_id_[spatial_index] = shared_frame_id;
673 if (!vp9_header.non_ref_for_inter_layer_pred) {
674 for (int sid = spatial_index + 1; sid < num_spatial_layers; ++sid) {
675 chain_last_frame_id_[sid] = shared_frame_id;
676 }
677 }
678 }
679}
680
Qingsi Wang1c1b99e2020-01-07 19:16:33681void RtpPayloadParams::SetDependenciesVp8Deprecated(
682 const CodecSpecificInfoVP8& vp8_info,
683 int64_t shared_frame_id,
684 bool is_keyframe,
685 int spatial_index,
686 int temporal_index,
687 bool layer_sync,
688 RTPVideoHeader::GenericDescriptorInfo* generic) {
689 RTC_DCHECK(!vp8_info.useExplicitDependencies);
690 RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value());
691 new_version_used_ = false;
692
693 if (is_keyframe) {
694 RTC_DCHECK_EQ(temporal_index, 0);
695 last_shared_frame_id_[spatial_index].fill(-1);
696 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
697 return;
698 }
699
700 if (layer_sync) {
701 int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0];
702
703 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
704 if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) {
705 last_shared_frame_id_[spatial_index][i] = -1;
706 }
707 }
708
709 RTC_DCHECK_GE(tl0_frame_id, 0);
710 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
711 generic->dependencies.push_back(tl0_frame_id);
712 } else {
713 for (int i = 0; i <= temporal_index; ++i) {
714 int64_t frame_id = last_shared_frame_id_[spatial_index][i];
715
716 if (frame_id != -1) {
717 RTC_DCHECK_LT(frame_id, shared_frame_id);
718 generic->dependencies.push_back(frame_id);
719 }
720 }
721 }
722
723 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
724}
725
726void RtpPayloadParams::SetDependenciesVp8New(
727 const CodecSpecificInfoVP8& vp8_info,
728 int64_t shared_frame_id,
729 bool is_keyframe,
730 bool layer_sync,
731 RTPVideoHeader::GenericDescriptorInfo* generic) {
732 RTC_DCHECK(vp8_info.useExplicitDependencies);
733 RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value());
734 new_version_used_ = true;
735
Elad Alonf5b216a2019-01-28 13:25:17736 if (is_keyframe) {
737 RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u);
738 buffer_id_to_frame_id_.fill(shared_frame_id);
739 return;
740 }
741
742 constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount;
743
744 RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u);
745 RTC_DCHECK_LE(vp8_info.referencedBuffersCount,
746 arraysize(vp8_info.referencedBuffers));
747
748 for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) {
749 const size_t referenced_buffer = vp8_info.referencedBuffers[i];
750 RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8);
751 RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size());
752
753 const int64_t dependency_frame_id =
754 buffer_id_to_frame_id_[referenced_buffer];
755 RTC_DCHECK_GE(dependency_frame_id, 0);
756 RTC_DCHECK_LT(dependency_frame_id, shared_frame_id);
757
758 const bool is_new_dependency =
Qingsi Wang1c1b99e2020-01-07 19:16:33759 std::find(generic->dependencies.begin(), generic->dependencies.end(),
760 dependency_frame_id) == generic->dependencies.end();
Elad Alonf5b216a2019-01-28 13:25:17761 if (is_new_dependency) {
Qingsi Wang1c1b99e2020-01-07 19:16:33762 generic->dependencies.push_back(dependency_frame_id);
Elad Alonf5b216a2019-01-28 13:25:17763 }
764 }
765
766 RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8);
767 for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) {
768 const size_t updated_id = vp8_info.updatedBuffers[i];
769 buffer_id_to_frame_id_[updated_id] = shared_frame_id;
770 }
771
772 RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8);
773}
774
Stefan Holmerf7044682018-07-17 08:16:41775} // namespace webrtc