blob: f8d6776c12948e4a2ff5b5e94524d8813788ca6b [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 {
33
34namespace {
35void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info,
Niels Möllerd3b8c632018-08-27 15:33:42 +020036 absl::optional<int> spatial_index,
Stefan Holmerf7044682018-07-17 10:16:41 +020037 RTPVideoHeader* rtp) {
38 rtp->codec = info.codecType;
Danil Chapovalov62a9a322020-11-11 16:15:07 +010039 rtp->is_last_frame_in_picture = info.end_of_picture;
Stefan Holmerf7044682018-07-17 10:16:41 +020040 switch (info.codecType) {
41 case kVideoCodecVP8: {
Philip Eliassond52a1a62018-09-07 13:03:55 +000042 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öllerd3b8c632018-08-27 15:33:42 +020048 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020049 return;
50 }
51 case kVideoCodecVP9: {
philipel29d88462018-08-08 14:26:00 +020052 auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>();
53 vp9_header.InitRTPVideoHeaderVP9();
54 vp9_header.inter_pic_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020055 info.codecSpecific.VP9.inter_pic_predicted;
philipel29d88462018-08-08 14:26:00 +020056 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 Holmerf7044682018-07-17 10:16:41 +020059 info.codecSpecific.VP9.non_ref_for_inter_layer_pred;
philipel29d88462018-08-08 14:26:00 +020060 vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx;
philipel29d88462018-08-08 14:26:00 +020061 vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch;
62 vp9_header.inter_layer_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020063 info.codecSpecific.VP9.inter_layer_predicted;
philipel29d88462018-08-08 14:26:00 +020064 vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx;
65 vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers;
Ilya Nikolaevskiyf5d87782020-02-04 10:06:33 +000066 vp9_header.first_active_layer = info.codecSpecific.VP9.first_active_layer;
Niels Möllerd3b8c632018-08-27 15:33:42 +020067 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 Holmerf7044682018-07-17 10:16:41 +020072 if (info.codecSpecific.VP9.ss_data_available) {
philipel29d88462018-08-08 14:26:00 +020073 vp9_header.spatial_layer_resolution_present =
Stefan Holmerf7044682018-07-17 10:16:41 +020074 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) {
philipel29d88462018-08-08 14:26:00 +020078 vp9_header.width[i] = info.codecSpecific.VP9.width[i];
79 vp9_header.height[i] = info.codecSpecific.VP9.height[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020080 }
81 }
philipel29d88462018-08-08 14:26:00 +020082 vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof);
Stefan Holmerf7044682018-07-17 10:16:41 +020083 }
84
philipel29d88462018-08-08 14:26:00 +020085 vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics;
Stefan Holmerf7044682018-07-17 10:16:41 +020086 for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) {
philipel29d88462018-08-08 14:26:00 +020087 vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020088 }
Danil Chapovalov06bbeb32020-11-11 12:42:56 +010089 vp9_header.end_of_picture = info.end_of_picture;
Stefan Holmerf7044682018-07-17 10:16:41 +020090 return;
91 }
92 case kVideoCodecH264: {
philipel7d745e52018-08-02 14:03:53 +020093 auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>();
94 h264_header.packetization_mode =
Stefan Holmerf7044682018-07-17 10:16:41 +020095 info.codecSpecific.H264.packetization_mode;
Niels Möllerd3b8c632018-08-27 15:33:42 +020096 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020097 return;
98 }
99 case kVideoCodecMultiplex:
100 case kVideoCodecGeneric:
101 rtp->codec = kVideoCodecGeneric;
Niels Möllerd3b8c632018-08-27 15:33:42 +0200102 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +0200103 return;
104 default:
105 return;
106 }
107}
108
109void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) {
110 if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid ||
111 image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) {
112 timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid;
113 return;
114 }
115
116 timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs(
117 image.capture_time_ms_, image.timing_.encode_start_ms);
118 timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs(
119 image.capture_time_ms_, image.timing_.encode_finish_ms);
120 timing->packetization_finish_delta_ms = 0;
121 timing->pacer_exit_delta_ms = 0;
122 timing->network_timestamp_delta_ms = 0;
123 timing->network2_timestamp_delta_ms = 0;
124 timing->flags = image.timing_.flags;
125}
126} // namespace
127
128RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc,
Erik Språngcbc0cba2020-04-18 14:36:59 +0200129 const RtpPayloadState* state,
130 const WebRtcKeyValueConfig& trials)
philipelbf2b6202018-08-27 14:33:18 +0200131 : ssrc_(ssrc),
132 generic_picture_id_experiment_(
Erik Språngcbc0cba2020-04-18 14:36:59 +0200133 absl::StartsWith(trials.Lookup("WebRTC-GenericPictureId"),
Danil Chapovalov636865e2020-06-03 14:11:26 +0200134 "Enabled")) {
philipelbf2b6202018-08-27 14:33:18 +0200135 for (auto& spatial_layer : last_shared_frame_id_)
136 spatial_layer.fill(-1);
137
Elad Alonf5b216a2019-01-28 14:25:17 +0100138 buffer_id_to_frame_id_.fill(-1);
139
Stefan Holmerf7044682018-07-17 10:16:41 +0200140 Random random(rtc::TimeMicros());
141 state_.picture_id =
142 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
143 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
144}
philipelbf2b6202018-08-27 14:33:18 +0200145
146RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
147
Stefan Holmerf7044682018-07-17 10:16:41 +0200148RtpPayloadParams::~RtpPayloadParams() {}
149
150RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
151 const EncodedImage& image,
philipelbf2b6202018-08-27 14:33:18 +0200152 const CodecSpecificInfo* codec_specific_info,
153 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200154 RTPVideoHeader rtp_video_header;
155 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200156 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
157 &rtp_video_header);
Stefan Holmerf7044682018-07-17 10:16:41 +0200158 }
Danil Chapovalov51bf2002019-10-11 10:53:27 +0200159 rtp_video_header.frame_type = image._frameType,
Stefan Holmerf7044682018-07-17 10:16:41 +0200160 rtp_video_header.rotation = image.rotation_;
161 rtp_video_header.content_type = image.content_type_;
162 rtp_video_header.playout_delay = image.playout_delay_;
philipelfab91292018-10-17 14:36:08 +0200163 rtp_video_header.width = image._encodedWidth;
164 rtp_video_header.height = image._encodedHeight;
Johannes Krond0b69a82018-12-03 14:18:53 +0100165 rtp_video_header.color_space = image.ColorSpace()
166 ? absl::make_optional(*image.ColorSpace())
167 : absl::nullopt;
Stefan Holmerf7044682018-07-17 10:16:41 +0200168 SetVideoTiming(image, &rtp_video_header.video_timing);
169
Niels Möller8f7ce222019-03-21 15:43:58 +0100170 const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey;
Stefan Holmerf7044682018-07-17 10:16:41 +0200171 const bool first_frame_in_picture =
172 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
173 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
174 : true;
philipelbf2b6202018-08-27 14:33:18 +0200175
176 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
philipel569397f2018-09-26 12:25:31 +0200177
Danil Chapovalov636865e2020-06-03 14:11:26 +0200178 SetGeneric(codec_specific_info, shared_frame_id, is_keyframe,
179 &rtp_video_header);
philipelbf2b6202018-08-27 14:33:18 +0200180
Stefan Holmerf7044682018-07-17 10:16:41 +0200181 return rtp_video_header;
182}
183
184uint32_t RtpPayloadParams::ssrc() const {
185 return ssrc_;
186}
187
188RtpPayloadState RtpPayloadParams::state() const {
189 return state_;
190}
191
philipelbf2b6202018-08-27 14:33:18 +0200192void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
193 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200194 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
195 if (first_frame_in_picture) {
196 state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
197 }
198 if (rtp_video_header->codec == kVideoCodecVP8) {
Philip Eliassond52a1a62018-09-07 13:03:55 +0000199 auto& vp8_header =
200 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
201 vp8_header.pictureId = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200202
Philip Eliassond52a1a62018-09-07 13:03:55 +0000203 if (vp8_header.temporalIdx != kNoTemporalIdx) {
204 if (vp8_header.temporalIdx == 0) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200205 ++state_.tl0_pic_idx;
206 }
Philip Eliassond52a1a62018-09-07 13:03:55 +0000207 vp8_header.tl0PicIdx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200208 }
209 }
210 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 14:26:00 +0200211 auto& vp9_header =
212 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
213 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200214
215 // Note that in the case that we have no temporal layers but we do have
216 // spatial layers, packets will carry layering info with a temporal_idx of
217 // zero, and we then have to set and increment tl0_pic_idx.
philipel29d88462018-08-08 14:26:00 +0200218 if (vp9_header.temporal_idx != kNoTemporalIdx ||
219 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200220 if (first_frame_in_picture &&
philipel29d88462018-08-08 14:26:00 +0200221 (vp9_header.temporal_idx == 0 ||
222 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200223 ++state_.tl0_pic_idx;
224 }
philipel29d88462018-08-08 14:26:00 +0200225 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200226 }
227 }
philipelbf2b6202018-08-27 14:33:18 +0200228 if (generic_picture_id_experiment_ &&
229 rtp_video_header->codec == kVideoCodecGeneric) {
Danil Chapovalovb6bf0b22020-01-28 18:36:57 +0100230 rtp_video_header->video_type_header.emplace<RTPVideoHeaderLegacyGeneric>()
231 .picture_id = state_.picture_id;
philipelbf2b6202018-08-27 14:33:18 +0200232 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200233}
philipelbf2b6202018-08-27 14:33:18 +0200234
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100235RTPVideoHeader::GenericDescriptorInfo
236RtpPayloadParams::GenericDescriptorFromFrameInfo(
237 const GenericFrameInfo& frame_info,
238 int64_t frame_id,
239 VideoFrameType frame_type) {
240 RTPVideoHeader::GenericDescriptorInfo generic;
241 generic.frame_id = frame_id;
242 generic.dependencies = dependencies_calculator_.FromBuffersUsage(
243 frame_type, frame_id, frame_info.encoder_buffers);
Danil Chapovalov4b860c12020-05-19 14:48:19 +0200244 generic.chain_diffs =
245 chains_calculator_.From(frame_id, frame_info.part_of_chain);
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100246 generic.spatial_index = frame_info.spatial_id;
247 generic.temporal_index = frame_info.temporal_id;
248 generic.decode_target_indications = frame_info.decode_target_indications;
Danil Chapovalove6ac8ff2020-06-26 13:51:08 +0200249 generic.active_decode_targets = frame_info.active_decode_targets;
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100250 return generic;
251}
252
Elad Alonf5b216a2019-01-28 14:25:17 +0100253void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info,
254 int64_t frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200255 bool is_keyframe,
256 RTPVideoHeader* rtp_video_header) {
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100257 if (codec_specific_info && codec_specific_info->generic_frame_info &&
258 !codec_specific_info->generic_frame_info->encoder_buffers.empty()) {
Danil Chapovalov4b860c12020-05-19 14:48:19 +0200259 if (is_keyframe) {
260 // Key frame resets all chains it is in.
261 chains_calculator_.Reset(
262 codec_specific_info->generic_frame_info->part_of_chain);
263 }
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100264 rtp_video_header->generic =
265 GenericDescriptorFromFrameInfo(*codec_specific_info->generic_frame_info,
266 frame_id, rtp_video_header->frame_type);
267 return;
268 }
269
Elad Alonf5b216a2019-01-28 14:25:17 +0100270 switch (rtp_video_header->codec) {
271 case VideoCodecType::kVideoCodecGeneric:
philipel8aba8fe2019-06-13 15:13:16 +0200272 GenericToGeneric(frame_id, is_keyframe, rtp_video_header);
Elad Alonf5b216a2019-01-28 14:25:17 +0100273 return;
274 case VideoCodecType::kVideoCodecVP8:
275 if (codec_specific_info) {
276 Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id,
277 is_keyframe, rtp_video_header);
278 }
279 return;
280 case VideoCodecType::kVideoCodecVP9:
Danil Chapovalovdc368292019-11-26 14:48:20 +0100281 case VideoCodecType::kVideoCodecAV1:
282 // TODO(philipel): Implement VP9 and AV1 to generic descriptor.
Elad Alonf5b216a2019-01-28 14:25:17 +0100283 return;
284 case VideoCodecType::kVideoCodecH264:
philipel8aba8fe2019-06-13 15:13:16 +0200285 if (codec_specific_info) {
286 H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id,
287 is_keyframe, rtp_video_header);
288 }
289 return;
Elad Alonf5b216a2019-01-28 14:25:17 +0100290 case VideoCodecType::kVideoCodecMultiplex:
291 return;
philipelbf2b6202018-08-27 14:33:18 +0200292 }
Elad Alonf5b216a2019-01-28 14:25:17 +0100293 RTC_NOTREACHED() << "Unsupported codec.";
philipelbf2b6202018-08-27 14:33:18 +0200294}
295
philipel8aba8fe2019-06-13 15:13:16 +0200296void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id,
297 bool is_keyframe,
298 RTPVideoHeader* rtp_video_header) {
299 RTPVideoHeader::GenericDescriptorInfo& generic =
300 rtp_video_header->generic.emplace();
301
302 generic.frame_id = shared_frame_id;
303
304 if (is_keyframe) {
305 last_shared_frame_id_[0].fill(-1);
306 } else {
307 int64_t frame_id = last_shared_frame_id_[0][0];
308 RTC_DCHECK_NE(frame_id, -1);
309 RTC_DCHECK_LT(frame_id, shared_frame_id);
310 generic.dependencies.push_back(frame_id);
311 }
312
313 last_shared_frame_id_[0][0] = shared_frame_id;
314}
315
316void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info,
317 int64_t shared_frame_id,
318 bool is_keyframe,
319 RTPVideoHeader* rtp_video_header) {
320 const int temporal_index =
321 h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0;
322
323 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) {
324 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
325 "used with generic frame descriptor.";
326 return;
327 }
328
329 RTPVideoHeader::GenericDescriptorInfo& generic =
330 rtp_video_header->generic.emplace();
331
332 generic.frame_id = shared_frame_id;
333 generic.temporal_index = temporal_index;
334
335 if (is_keyframe) {
336 RTC_DCHECK_EQ(temporal_index, 0);
337 last_shared_frame_id_[/*spatial index*/ 0].fill(-1);
338 last_shared_frame_id_[/*spatial index*/ 0][temporal_index] =
339 shared_frame_id;
340 return;
341 }
342
343 if (h264_info.base_layer_sync) {
344 int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0];
345
346 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
347 if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) {
348 last_shared_frame_id_[/*spatial index*/ 0][i] = -1;
349 }
350 }
351
352 RTC_DCHECK_GE(tl0_frame_id, 0);
353 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
354 generic.dependencies.push_back(tl0_frame_id);
355 } else {
356 for (int i = 0; i <= temporal_index; ++i) {
357 int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i];
358
359 if (frame_id != -1) {
360 RTC_DCHECK_LT(frame_id, shared_frame_id);
361 generic.dependencies.push_back(frame_id);
362 }
363 }
364 }
365
366 last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id;
367}
368
Elad Alonf5b216a2019-01-28 14:25:17 +0100369void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info,
370 int64_t shared_frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200371 bool is_keyframe,
372 RTPVideoHeader* rtp_video_header) {
373 const auto& vp8_header =
374 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
375 const int spatial_index = 0;
376 const int temporal_index =
377 vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0;
378
379 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers ||
380 spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) {
381 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
382 "used with generic frame descriptor.";
383 return;
384 }
385
386 RTPVideoHeader::GenericDescriptorInfo& generic =
387 rtp_video_header->generic.emplace();
388
389 generic.frame_id = shared_frame_id;
390 generic.spatial_index = spatial_index;
391 generic.temporal_index = temporal_index;
392
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000393 if (vp8_info.useExplicitDependencies) {
394 SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe,
395 vp8_header.layerSync, &generic);
396 } else {
397 SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe,
398 spatial_index, temporal_index,
399 vp8_header.layerSync, &generic);
400 }
401}
402
403void RtpPayloadParams::SetDependenciesVp8Deprecated(
404 const CodecSpecificInfoVP8& vp8_info,
405 int64_t shared_frame_id,
406 bool is_keyframe,
407 int spatial_index,
408 int temporal_index,
409 bool layer_sync,
410 RTPVideoHeader::GenericDescriptorInfo* generic) {
411 RTC_DCHECK(!vp8_info.useExplicitDependencies);
412 RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value());
413 new_version_used_ = false;
414
415 if (is_keyframe) {
416 RTC_DCHECK_EQ(temporal_index, 0);
417 last_shared_frame_id_[spatial_index].fill(-1);
418 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
419 return;
420 }
421
422 if (layer_sync) {
423 int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0];
424
425 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
426 if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) {
427 last_shared_frame_id_[spatial_index][i] = -1;
428 }
429 }
430
431 RTC_DCHECK_GE(tl0_frame_id, 0);
432 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
433 generic->dependencies.push_back(tl0_frame_id);
434 } else {
435 for (int i = 0; i <= temporal_index; ++i) {
436 int64_t frame_id = last_shared_frame_id_[spatial_index][i];
437
438 if (frame_id != -1) {
439 RTC_DCHECK_LT(frame_id, shared_frame_id);
440 generic->dependencies.push_back(frame_id);
441 }
442 }
443 }
444
445 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
446}
447
448void RtpPayloadParams::SetDependenciesVp8New(
449 const CodecSpecificInfoVP8& vp8_info,
450 int64_t shared_frame_id,
451 bool is_keyframe,
452 bool layer_sync,
453 RTPVideoHeader::GenericDescriptorInfo* generic) {
454 RTC_DCHECK(vp8_info.useExplicitDependencies);
455 RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value());
456 new_version_used_ = true;
457
Elad Alonf5b216a2019-01-28 14:25:17 +0100458 if (is_keyframe) {
459 RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u);
460 buffer_id_to_frame_id_.fill(shared_frame_id);
461 return;
462 }
463
464 constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount;
465
466 RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u);
467 RTC_DCHECK_LE(vp8_info.referencedBuffersCount,
468 arraysize(vp8_info.referencedBuffers));
469
470 for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) {
471 const size_t referenced_buffer = vp8_info.referencedBuffers[i];
472 RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8);
473 RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size());
474
475 const int64_t dependency_frame_id =
476 buffer_id_to_frame_id_[referenced_buffer];
477 RTC_DCHECK_GE(dependency_frame_id, 0);
478 RTC_DCHECK_LT(dependency_frame_id, shared_frame_id);
479
480 const bool is_new_dependency =
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000481 std::find(generic->dependencies.begin(), generic->dependencies.end(),
482 dependency_frame_id) == generic->dependencies.end();
Elad Alonf5b216a2019-01-28 14:25:17 +0100483 if (is_new_dependency) {
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000484 generic->dependencies.push_back(dependency_frame_id);
Elad Alonf5b216a2019-01-28 14:25:17 +0100485 }
486 }
487
488 RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8);
489 for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) {
490 const size_t updated_id = vp8_info.updatedBuffers[i];
491 buffer_id_to_frame_id_[updated_id] = shared_frame_id;
492 }
493
494 RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8);
495}
496
Stefan Holmerf7044682018-07-17 10:16:41 +0200497} // namespace webrtc