blob: c71af6b09770a287e601f495bcf18c51e20ef982 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "absl/types/variant.h"
19#include "api/video/video_timing.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "modules/video_coding/codecs/h264/include/h264_globals.h"
21#include "modules/video_coding/codecs/interface/common_constants.h"
22#include "modules/video_coding/codecs/vp8/include/vp8_globals.h"
23#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Elad Alonf5b216a2019-01-28 14:25:17 +010024#include "rtc_base/arraysize.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020025#include "rtc_base/checks.h"
philipelbf2b6202018-08-27 14:33:18 +020026#include "rtc_base/logging.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020027#include "rtc_base/random.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/time_utils.h"
philipelbf2b6202018-08-27 14:33:18 +020029#include "system_wrappers/include/field_trial.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020030
31namespace webrtc {
32
33namespace {
34void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info,
Niels Möllerd3b8c632018-08-27 15:33:42 +020035 absl::optional<int> spatial_index,
Stefan Holmerf7044682018-07-17 10:16:41 +020036 RTPVideoHeader* rtp) {
37 rtp->codec = info.codecType;
38 switch (info.codecType) {
39 case kVideoCodecVP8: {
Philip Eliassond52a1a62018-09-07 13:03:55 +000040 auto& vp8_header = rtp->video_type_header.emplace<RTPVideoHeaderVP8>();
41 vp8_header.InitRTPVideoHeaderVP8();
42 vp8_header.nonReference = info.codecSpecific.VP8.nonReference;
43 vp8_header.temporalIdx = info.codecSpecific.VP8.temporalIdx;
44 vp8_header.layerSync = info.codecSpecific.VP8.layerSync;
45 vp8_header.keyIdx = info.codecSpecific.VP8.keyIdx;
Niels Möllerd3b8c632018-08-27 15:33:42 +020046 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020047 return;
48 }
49 case kVideoCodecVP9: {
philipel29d88462018-08-08 14:26:00 +020050 auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>();
51 vp9_header.InitRTPVideoHeaderVP9();
52 vp9_header.inter_pic_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020053 info.codecSpecific.VP9.inter_pic_predicted;
philipel29d88462018-08-08 14:26:00 +020054 vp9_header.flexible_mode = info.codecSpecific.VP9.flexible_mode;
55 vp9_header.ss_data_available = info.codecSpecific.VP9.ss_data_available;
56 vp9_header.non_ref_for_inter_layer_pred =
Stefan Holmerf7044682018-07-17 10:16:41 +020057 info.codecSpecific.VP9.non_ref_for_inter_layer_pred;
philipel29d88462018-08-08 14:26:00 +020058 vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx;
philipel29d88462018-08-08 14:26:00 +020059 vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch;
60 vp9_header.inter_layer_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020061 info.codecSpecific.VP9.inter_layer_predicted;
philipel29d88462018-08-08 14:26:00 +020062 vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx;
63 vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers;
Niels Möllerd3b8c632018-08-27 15:33:42 +020064 if (vp9_header.num_spatial_layers > 1) {
65 vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx);
66 } else {
67 vp9_header.spatial_idx = kNoSpatialIdx;
68 }
Stefan Holmerf7044682018-07-17 10:16:41 +020069 if (info.codecSpecific.VP9.ss_data_available) {
philipel29d88462018-08-08 14:26:00 +020070 vp9_header.spatial_layer_resolution_present =
Stefan Holmerf7044682018-07-17 10:16:41 +020071 info.codecSpecific.VP9.spatial_layer_resolution_present;
72 if (info.codecSpecific.VP9.spatial_layer_resolution_present) {
73 for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers;
74 ++i) {
philipel29d88462018-08-08 14:26:00 +020075 vp9_header.width[i] = info.codecSpecific.VP9.width[i];
76 vp9_header.height[i] = info.codecSpecific.VP9.height[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020077 }
78 }
philipel29d88462018-08-08 14:26:00 +020079 vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof);
Stefan Holmerf7044682018-07-17 10:16:41 +020080 }
81
philipel29d88462018-08-08 14:26:00 +020082 vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics;
Stefan Holmerf7044682018-07-17 10:16:41 +020083 for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) {
philipel29d88462018-08-08 14:26:00 +020084 vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020085 }
philipel29d88462018-08-08 14:26:00 +020086 vp9_header.end_of_picture = info.codecSpecific.VP9.end_of_picture;
Stefan Holmerf7044682018-07-17 10:16:41 +020087 return;
88 }
89 case kVideoCodecH264: {
philipel7d745e52018-08-02 14:03:53 +020090 auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>();
91 h264_header.packetization_mode =
Stefan Holmerf7044682018-07-17 10:16:41 +020092 info.codecSpecific.H264.packetization_mode;
Niels Möllerd3b8c632018-08-27 15:33:42 +020093 rtp->simulcastIdx = spatial_index.value_or(0);
Johnny Lee1a1c52b2019-02-08 14:25:40 -050094 rtp->frame_marking.temporal_id = kNoTemporalIdx;
95 if (info.codecSpecific.H264.temporal_idx != kNoTemporalIdx) {
96 rtp->frame_marking.temporal_id = info.codecSpecific.H264.temporal_idx;
97 rtp->frame_marking.layer_id = 0;
98 rtp->frame_marking.independent_frame =
99 info.codecSpecific.H264.idr_frame;
100 rtp->frame_marking.base_layer_sync =
101 info.codecSpecific.H264.base_layer_sync;
102 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200103 return;
104 }
105 case kVideoCodecMultiplex:
106 case kVideoCodecGeneric:
107 rtp->codec = kVideoCodecGeneric;
Niels Möllerd3b8c632018-08-27 15:33:42 +0200108 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +0200109 return;
110 default:
111 return;
112 }
113}
114
115void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) {
116 if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid ||
117 image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) {
118 timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid;
119 return;
120 }
121
122 timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs(
123 image.capture_time_ms_, image.timing_.encode_start_ms);
124 timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs(
125 image.capture_time_ms_, image.timing_.encode_finish_ms);
126 timing->packetization_finish_delta_ms = 0;
127 timing->pacer_exit_delta_ms = 0;
128 timing->network_timestamp_delta_ms = 0;
129 timing->network2_timestamp_delta_ms = 0;
130 timing->flags = image.timing_.flags;
131}
132} // namespace
133
134RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc,
135 const RtpPayloadState* state)
philipelbf2b6202018-08-27 14:33:18 +0200136 : ssrc_(ssrc),
137 generic_picture_id_experiment_(
philipel569397f2018-09-26 12:25:31 +0200138 field_trial::IsEnabled("WebRTC-GenericPictureId")),
139 generic_descriptor_experiment_(
140 field_trial::IsEnabled("WebRTC-GenericDescriptor")) {
philipelbf2b6202018-08-27 14:33:18 +0200141 for (auto& spatial_layer : last_shared_frame_id_)
142 spatial_layer.fill(-1);
143
Elad Alonf5b216a2019-01-28 14:25:17 +0100144 buffer_id_to_frame_id_.fill(-1);
145
Stefan Holmerf7044682018-07-17 10:16:41 +0200146 Random random(rtc::TimeMicros());
147 state_.picture_id =
148 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
149 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
150}
philipelbf2b6202018-08-27 14:33:18 +0200151
152RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
153
Stefan Holmerf7044682018-07-17 10:16:41 +0200154RtpPayloadParams::~RtpPayloadParams() {}
155
156RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
157 const EncodedImage& image,
philipelbf2b6202018-08-27 14:33:18 +0200158 const CodecSpecificInfo* codec_specific_info,
159 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200160 RTPVideoHeader rtp_video_header;
161 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200162 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
163 &rtp_video_header);
Stefan Holmerf7044682018-07-17 10:16:41 +0200164 }
Danil Chapovalov51bf2002019-10-11 10:53:27 +0200165 rtp_video_header.frame_type = image._frameType,
Stefan Holmerf7044682018-07-17 10:16:41 +0200166 rtp_video_header.rotation = image.rotation_;
167 rtp_video_header.content_type = image.content_type_;
168 rtp_video_header.playout_delay = image.playout_delay_;
philipelfab91292018-10-17 14:36:08 +0200169 rtp_video_header.width = image._encodedWidth;
170 rtp_video_header.height = image._encodedHeight;
Johannes Krond0b69a82018-12-03 14:18:53 +0100171 rtp_video_header.color_space = image.ColorSpace()
172 ? absl::make_optional(*image.ColorSpace())
173 : absl::nullopt;
Stefan Holmerf7044682018-07-17 10:16:41 +0200174 SetVideoTiming(image, &rtp_video_header.video_timing);
175
Niels Möller8f7ce222019-03-21 15:43:58 +0100176 const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey;
Stefan Holmerf7044682018-07-17 10:16:41 +0200177 const bool first_frame_in_picture =
178 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
179 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
180 : true;
philipelbf2b6202018-08-27 14:33:18 +0200181
182 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
philipel569397f2018-09-26 12:25:31 +0200183
184 if (generic_descriptor_experiment_)
Elad Alonf5b216a2019-01-28 14:25:17 +0100185 SetGeneric(codec_specific_info, shared_frame_id, is_keyframe,
186 &rtp_video_header);
philipelbf2b6202018-08-27 14:33:18 +0200187
Stefan Holmerf7044682018-07-17 10:16:41 +0200188 return rtp_video_header;
189}
190
191uint32_t RtpPayloadParams::ssrc() const {
192 return ssrc_;
193}
194
195RtpPayloadState RtpPayloadParams::state() const {
196 return state_;
197}
198
philipelbf2b6202018-08-27 14:33:18 +0200199void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
200 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200201 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
202 if (first_frame_in_picture) {
203 state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
204 }
205 if (rtp_video_header->codec == kVideoCodecVP8) {
Philip Eliassond52a1a62018-09-07 13:03:55 +0000206 auto& vp8_header =
207 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
208 vp8_header.pictureId = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200209
Philip Eliassond52a1a62018-09-07 13:03:55 +0000210 if (vp8_header.temporalIdx != kNoTemporalIdx) {
211 if (vp8_header.temporalIdx == 0) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200212 ++state_.tl0_pic_idx;
213 }
Philip Eliassond52a1a62018-09-07 13:03:55 +0000214 vp8_header.tl0PicIdx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200215 }
216 }
217 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 14:26:00 +0200218 auto& vp9_header =
219 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
220 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200221
222 // Note that in the case that we have no temporal layers but we do have
223 // spatial layers, packets will carry layering info with a temporal_idx of
224 // zero, and we then have to set and increment tl0_pic_idx.
philipel29d88462018-08-08 14:26:00 +0200225 if (vp9_header.temporal_idx != kNoTemporalIdx ||
226 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200227 if (first_frame_in_picture &&
philipel29d88462018-08-08 14:26:00 +0200228 (vp9_header.temporal_idx == 0 ||
229 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200230 ++state_.tl0_pic_idx;
231 }
philipel29d88462018-08-08 14:26:00 +0200232 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200233 }
234 }
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500235 if (rtp_video_header->codec == kVideoCodecH264) {
236 if (rtp_video_header->frame_marking.temporal_id != kNoTemporalIdx) {
237 if (rtp_video_header->frame_marking.temporal_id == 0) {
238 ++state_.tl0_pic_idx;
239 }
240 rtp_video_header->frame_marking.tl0_pic_idx = state_.tl0_pic_idx;
241 }
242 }
philipelbf2b6202018-08-27 14:33:18 +0200243 // There are currently two generic descriptors in WebRTC. The old descriptor
244 // can not share a picture id space between simulcast streams, so we use the
245 // |picture_id| in this case. We let the |picture_id| tag along in |frame_id|
246 // until the old generic format can be removed.
247 // TODO(philipel): Remove this when the new generic format has been fully
248 // implemented.
249 if (generic_picture_id_experiment_ &&
250 rtp_video_header->codec == kVideoCodecGeneric) {
251 rtp_video_header->generic.emplace().frame_id = state_.picture_id;
252 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200253}
philipelbf2b6202018-08-27 14:33:18 +0200254
Elad Alonf5b216a2019-01-28 14:25:17 +0100255void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info,
256 int64_t frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200257 bool is_keyframe,
258 RTPVideoHeader* rtp_video_header) {
Elad Alonf5b216a2019-01-28 14:25:17 +0100259 switch (rtp_video_header->codec) {
260 case VideoCodecType::kVideoCodecGeneric:
philipel8aba8fe2019-06-13 15:13:16 +0200261 GenericToGeneric(frame_id, is_keyframe, rtp_video_header);
Elad Alonf5b216a2019-01-28 14:25:17 +0100262 return;
263 case VideoCodecType::kVideoCodecVP8:
264 if (codec_specific_info) {
265 Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id,
266 is_keyframe, rtp_video_header);
267 }
268 return;
269 case VideoCodecType::kVideoCodecVP9:
Danil Chapovalovdc368292019-11-26 14:48:20 +0100270 case VideoCodecType::kVideoCodecAV1:
271 // TODO(philipel): Implement VP9 and AV1 to generic descriptor.
Elad Alonf5b216a2019-01-28 14:25:17 +0100272 return;
273 case VideoCodecType::kVideoCodecH264:
philipel8aba8fe2019-06-13 15:13:16 +0200274 if (codec_specific_info) {
275 H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id,
276 is_keyframe, rtp_video_header);
277 }
278 return;
Elad Alonf5b216a2019-01-28 14:25:17 +0100279 case VideoCodecType::kVideoCodecMultiplex:
280 return;
philipelbf2b6202018-08-27 14:33:18 +0200281 }
Elad Alonf5b216a2019-01-28 14:25:17 +0100282 RTC_NOTREACHED() << "Unsupported codec.";
philipelbf2b6202018-08-27 14:33:18 +0200283}
284
philipel8aba8fe2019-06-13 15:13:16 +0200285void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id,
286 bool is_keyframe,
287 RTPVideoHeader* rtp_video_header) {
288 RTPVideoHeader::GenericDescriptorInfo& generic =
289 rtp_video_header->generic.emplace();
290
291 generic.frame_id = shared_frame_id;
292
293 if (is_keyframe) {
294 last_shared_frame_id_[0].fill(-1);
295 } else {
296 int64_t frame_id = last_shared_frame_id_[0][0];
297 RTC_DCHECK_NE(frame_id, -1);
298 RTC_DCHECK_LT(frame_id, shared_frame_id);
299 generic.dependencies.push_back(frame_id);
300 }
301
302 last_shared_frame_id_[0][0] = shared_frame_id;
303}
304
305void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info,
306 int64_t shared_frame_id,
307 bool is_keyframe,
308 RTPVideoHeader* rtp_video_header) {
309 const int temporal_index =
310 h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0;
311
312 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) {
313 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
314 "used with generic frame descriptor.";
315 return;
316 }
317
318 RTPVideoHeader::GenericDescriptorInfo& generic =
319 rtp_video_header->generic.emplace();
320
321 generic.frame_id = shared_frame_id;
322 generic.temporal_index = temporal_index;
323
324 if (is_keyframe) {
325 RTC_DCHECK_EQ(temporal_index, 0);
326 last_shared_frame_id_[/*spatial index*/ 0].fill(-1);
327 last_shared_frame_id_[/*spatial index*/ 0][temporal_index] =
328 shared_frame_id;
329 return;
330 }
331
332 if (h264_info.base_layer_sync) {
333 int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0];
334
335 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
336 if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) {
337 last_shared_frame_id_[/*spatial index*/ 0][i] = -1;
338 }
339 }
340
341 RTC_DCHECK_GE(tl0_frame_id, 0);
342 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
343 generic.dependencies.push_back(tl0_frame_id);
344 } else {
345 for (int i = 0; i <= temporal_index; ++i) {
346 int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i];
347
348 if (frame_id != -1) {
349 RTC_DCHECK_LT(frame_id, shared_frame_id);
350 generic.dependencies.push_back(frame_id);
351 }
352 }
353 }
354
355 last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id;
356}
357
Elad Alonf5b216a2019-01-28 14:25:17 +0100358void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info,
359 int64_t shared_frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200360 bool is_keyframe,
361 RTPVideoHeader* rtp_video_header) {
362 const auto& vp8_header =
363 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
364 const int spatial_index = 0;
365 const int temporal_index =
366 vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0;
367
368 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers ||
369 spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) {
370 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
371 "used with generic frame descriptor.";
372 return;
373 }
374
375 RTPVideoHeader::GenericDescriptorInfo& generic =
376 rtp_video_header->generic.emplace();
377
378 generic.frame_id = shared_frame_id;
379 generic.spatial_index = spatial_index;
380 generic.temporal_index = temporal_index;
381
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000382 if (vp8_info.useExplicitDependencies) {
383 SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe,
384 vp8_header.layerSync, &generic);
385 } else {
386 SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe,
387 spatial_index, temporal_index,
388 vp8_header.layerSync, &generic);
389 }
390}
391
392void RtpPayloadParams::SetDependenciesVp8Deprecated(
393 const CodecSpecificInfoVP8& vp8_info,
394 int64_t shared_frame_id,
395 bool is_keyframe,
396 int spatial_index,
397 int temporal_index,
398 bool layer_sync,
399 RTPVideoHeader::GenericDescriptorInfo* generic) {
400 RTC_DCHECK(!vp8_info.useExplicitDependencies);
401 RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value());
402 new_version_used_ = false;
403
404 if (is_keyframe) {
405 RTC_DCHECK_EQ(temporal_index, 0);
406 last_shared_frame_id_[spatial_index].fill(-1);
407 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
408 return;
409 }
410
411 if (layer_sync) {
412 int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0];
413
414 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
415 if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) {
416 last_shared_frame_id_[spatial_index][i] = -1;
417 }
418 }
419
420 RTC_DCHECK_GE(tl0_frame_id, 0);
421 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
422 generic->dependencies.push_back(tl0_frame_id);
423 } else {
424 for (int i = 0; i <= temporal_index; ++i) {
425 int64_t frame_id = last_shared_frame_id_[spatial_index][i];
426
427 if (frame_id != -1) {
428 RTC_DCHECK_LT(frame_id, shared_frame_id);
429 generic->dependencies.push_back(frame_id);
430 }
431 }
432 }
433
434 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
435}
436
437void RtpPayloadParams::SetDependenciesVp8New(
438 const CodecSpecificInfoVP8& vp8_info,
439 int64_t shared_frame_id,
440 bool is_keyframe,
441 bool layer_sync,
442 RTPVideoHeader::GenericDescriptorInfo* generic) {
443 RTC_DCHECK(vp8_info.useExplicitDependencies);
444 RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value());
445 new_version_used_ = true;
446
Elad Alonf5b216a2019-01-28 14:25:17 +0100447 if (is_keyframe) {
448 RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u);
449 buffer_id_to_frame_id_.fill(shared_frame_id);
450 return;
451 }
452
453 constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount;
454
455 RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u);
456 RTC_DCHECK_LE(vp8_info.referencedBuffersCount,
457 arraysize(vp8_info.referencedBuffers));
458
459 for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) {
460 const size_t referenced_buffer = vp8_info.referencedBuffers[i];
461 RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8);
462 RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size());
463
464 const int64_t dependency_frame_id =
465 buffer_id_to_frame_id_[referenced_buffer];
466 RTC_DCHECK_GE(dependency_frame_id, 0);
467 RTC_DCHECK_LT(dependency_frame_id, shared_frame_id);
468
469 const bool is_new_dependency =
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000470 std::find(generic->dependencies.begin(), generic->dependencies.end(),
471 dependency_frame_id) == generic->dependencies.end();
Elad Alonf5b216a2019-01-28 14:25:17 +0100472 if (is_new_dependency) {
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000473 generic->dependencies.push_back(dependency_frame_id);
Elad Alonf5b216a2019-01-28 14:25:17 +0100474 }
475 }
476
477 RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8);
478 for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) {
479 const size_t updated_id = vp8_info.updatedBuffers[i];
480 buffer_id_to_frame_id_[updated_id] = shared_frame_id;
481 }
482
483 RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8);
484}
485
Stefan Holmerf7044682018-07-17 10:16:41 +0200486} // namespace webrtc