blob: 408a2a85f6ca689279275089a1f36f598d2b3a76 [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;
Ilya Nikolaevskiy2e73a3d2020-01-29 16:21:51 +010064 vp9_header.first_active_layer = info.codecSpecific.VP9.first_active_layer;
Niels Möllerd3b8c632018-08-27 15:33:42 +020065 if (vp9_header.num_spatial_layers > 1) {
66 vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx);
67 } else {
68 vp9_header.spatial_idx = kNoSpatialIdx;
69 }
Stefan Holmerf7044682018-07-17 10:16:41 +020070 if (info.codecSpecific.VP9.ss_data_available) {
philipel29d88462018-08-08 14:26:00 +020071 vp9_header.spatial_layer_resolution_present =
Stefan Holmerf7044682018-07-17 10:16:41 +020072 info.codecSpecific.VP9.spatial_layer_resolution_present;
73 if (info.codecSpecific.VP9.spatial_layer_resolution_present) {
74 for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers;
75 ++i) {
philipel29d88462018-08-08 14:26:00 +020076 vp9_header.width[i] = info.codecSpecific.VP9.width[i];
77 vp9_header.height[i] = info.codecSpecific.VP9.height[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020078 }
79 }
philipel29d88462018-08-08 14:26:00 +020080 vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof);
Stefan Holmerf7044682018-07-17 10:16:41 +020081 }
82
philipel29d88462018-08-08 14:26:00 +020083 vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics;
Stefan Holmerf7044682018-07-17 10:16:41 +020084 for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) {
philipel29d88462018-08-08 14:26:00 +020085 vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020086 }
philipel29d88462018-08-08 14:26:00 +020087 vp9_header.end_of_picture = info.codecSpecific.VP9.end_of_picture;
Stefan Holmerf7044682018-07-17 10:16:41 +020088 return;
89 }
90 case kVideoCodecH264: {
philipel7d745e52018-08-02 14:03:53 +020091 auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>();
92 h264_header.packetization_mode =
Stefan Holmerf7044682018-07-17 10:16:41 +020093 info.codecSpecific.H264.packetization_mode;
Niels Möllerd3b8c632018-08-27 15:33:42 +020094 rtp->simulcastIdx = spatial_index.value_or(0);
Johnny Lee1a1c52b2019-02-08 14:25:40 -050095 rtp->frame_marking.temporal_id = kNoTemporalIdx;
96 if (info.codecSpecific.H264.temporal_idx != kNoTemporalIdx) {
97 rtp->frame_marking.temporal_id = info.codecSpecific.H264.temporal_idx;
98 rtp->frame_marking.layer_id = 0;
99 rtp->frame_marking.independent_frame =
100 info.codecSpecific.H264.idr_frame;
101 rtp->frame_marking.base_layer_sync =
102 info.codecSpecific.H264.base_layer_sync;
103 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200104 return;
105 }
106 case kVideoCodecMultiplex:
107 case kVideoCodecGeneric:
108 rtp->codec = kVideoCodecGeneric;
Niels Möllerd3b8c632018-08-27 15:33:42 +0200109 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +0200110 return;
111 default:
112 return;
113 }
114}
115
116void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) {
117 if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid ||
118 image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) {
119 timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid;
120 return;
121 }
122
123 timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs(
124 image.capture_time_ms_, image.timing_.encode_start_ms);
125 timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs(
126 image.capture_time_ms_, image.timing_.encode_finish_ms);
127 timing->packetization_finish_delta_ms = 0;
128 timing->pacer_exit_delta_ms = 0;
129 timing->network_timestamp_delta_ms = 0;
130 timing->network2_timestamp_delta_ms = 0;
131 timing->flags = image.timing_.flags;
132}
133} // namespace
134
135RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc,
136 const RtpPayloadState* state)
philipelbf2b6202018-08-27 14:33:18 +0200137 : ssrc_(ssrc),
138 generic_picture_id_experiment_(
philipel569397f2018-09-26 12:25:31 +0200139 field_trial::IsEnabled("WebRTC-GenericPictureId")),
140 generic_descriptor_experiment_(
141 field_trial::IsEnabled("WebRTC-GenericDescriptor")) {
philipelbf2b6202018-08-27 14:33:18 +0200142 for (auto& spatial_layer : last_shared_frame_id_)
143 spatial_layer.fill(-1);
144
Elad Alonf5b216a2019-01-28 14:25:17 +0100145 buffer_id_to_frame_id_.fill(-1);
146
Stefan Holmerf7044682018-07-17 10:16:41 +0200147 Random random(rtc::TimeMicros());
148 state_.picture_id =
149 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
150 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
151}
philipelbf2b6202018-08-27 14:33:18 +0200152
153RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
154
Stefan Holmerf7044682018-07-17 10:16:41 +0200155RtpPayloadParams::~RtpPayloadParams() {}
156
157RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
158 const EncodedImage& image,
philipelbf2b6202018-08-27 14:33:18 +0200159 const CodecSpecificInfo* codec_specific_info,
160 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200161 RTPVideoHeader rtp_video_header;
162 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200163 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
164 &rtp_video_header);
Stefan Holmerf7044682018-07-17 10:16:41 +0200165 }
Danil Chapovalov51bf2002019-10-11 10:53:27 +0200166 rtp_video_header.frame_type = image._frameType,
Stefan Holmerf7044682018-07-17 10:16:41 +0200167 rtp_video_header.rotation = image.rotation_;
168 rtp_video_header.content_type = image.content_type_;
169 rtp_video_header.playout_delay = image.playout_delay_;
philipelfab91292018-10-17 14:36:08 +0200170 rtp_video_header.width = image._encodedWidth;
171 rtp_video_header.height = image._encodedHeight;
Johannes Krond0b69a82018-12-03 14:18:53 +0100172 rtp_video_header.color_space = image.ColorSpace()
173 ? absl::make_optional(*image.ColorSpace())
174 : absl::nullopt;
Stefan Holmerf7044682018-07-17 10:16:41 +0200175 SetVideoTiming(image, &rtp_video_header.video_timing);
176
Niels Möller8f7ce222019-03-21 15:43:58 +0100177 const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey;
Stefan Holmerf7044682018-07-17 10:16:41 +0200178 const bool first_frame_in_picture =
179 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
180 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
181 : true;
philipelbf2b6202018-08-27 14:33:18 +0200182
183 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
philipel569397f2018-09-26 12:25:31 +0200184
185 if (generic_descriptor_experiment_)
Elad Alonf5b216a2019-01-28 14:25:17 +0100186 SetGeneric(codec_specific_info, shared_frame_id, is_keyframe,
187 &rtp_video_header);
philipelbf2b6202018-08-27 14:33:18 +0200188
Stefan Holmerf7044682018-07-17 10:16:41 +0200189 return rtp_video_header;
190}
191
192uint32_t RtpPayloadParams::ssrc() const {
193 return ssrc_;
194}
195
196RtpPayloadState RtpPayloadParams::state() const {
197 return state_;
198}
199
philipelbf2b6202018-08-27 14:33:18 +0200200void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
201 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200202 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
203 if (first_frame_in_picture) {
204 state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
205 }
206 if (rtp_video_header->codec == kVideoCodecVP8) {
Philip Eliassond52a1a62018-09-07 13:03:55 +0000207 auto& vp8_header =
208 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
209 vp8_header.pictureId = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200210
Philip Eliassond52a1a62018-09-07 13:03:55 +0000211 if (vp8_header.temporalIdx != kNoTemporalIdx) {
212 if (vp8_header.temporalIdx == 0) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200213 ++state_.tl0_pic_idx;
214 }
Philip Eliassond52a1a62018-09-07 13:03:55 +0000215 vp8_header.tl0PicIdx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200216 }
217 }
218 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 14:26:00 +0200219 auto& vp9_header =
220 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
221 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200222
223 // Note that in the case that we have no temporal layers but we do have
224 // spatial layers, packets will carry layering info with a temporal_idx of
225 // zero, and we then have to set and increment tl0_pic_idx.
philipel29d88462018-08-08 14:26:00 +0200226 if (vp9_header.temporal_idx != kNoTemporalIdx ||
227 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200228 if (first_frame_in_picture &&
philipel29d88462018-08-08 14:26:00 +0200229 (vp9_header.temporal_idx == 0 ||
230 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200231 ++state_.tl0_pic_idx;
232 }
philipel29d88462018-08-08 14:26:00 +0200233 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200234 }
235 }
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500236 if (rtp_video_header->codec == kVideoCodecH264) {
237 if (rtp_video_header->frame_marking.temporal_id != kNoTemporalIdx) {
238 if (rtp_video_header->frame_marking.temporal_id == 0) {
239 ++state_.tl0_pic_idx;
240 }
241 rtp_video_header->frame_marking.tl0_pic_idx = state_.tl0_pic_idx;
242 }
243 }
philipelbf2b6202018-08-27 14:33:18 +0200244 if (generic_picture_id_experiment_ &&
245 rtp_video_header->codec == kVideoCodecGeneric) {
Danil Chapovalovb6bf0b22020-01-28 18:36:57 +0100246 rtp_video_header->video_type_header.emplace<RTPVideoHeaderLegacyGeneric>()
247 .picture_id = state_.picture_id;
philipelbf2b6202018-08-27 14:33:18 +0200248 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200249}
philipelbf2b6202018-08-27 14:33:18 +0200250
Elad Alonf5b216a2019-01-28 14:25:17 +0100251void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info,
252 int64_t frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200253 bool is_keyframe,
254 RTPVideoHeader* rtp_video_header) {
Elad Alonf5b216a2019-01-28 14:25:17 +0100255 switch (rtp_video_header->codec) {
256 case VideoCodecType::kVideoCodecGeneric:
philipel8aba8fe2019-06-13 15:13:16 +0200257 GenericToGeneric(frame_id, is_keyframe, rtp_video_header);
Elad Alonf5b216a2019-01-28 14:25:17 +0100258 return;
259 case VideoCodecType::kVideoCodecVP8:
260 if (codec_specific_info) {
261 Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id,
262 is_keyframe, rtp_video_header);
263 }
264 return;
265 case VideoCodecType::kVideoCodecVP9:
Danil Chapovalovdc368292019-11-26 14:48:20 +0100266 case VideoCodecType::kVideoCodecAV1:
267 // TODO(philipel): Implement VP9 and AV1 to generic descriptor.
Elad Alonf5b216a2019-01-28 14:25:17 +0100268 return;
269 case VideoCodecType::kVideoCodecH264:
philipel8aba8fe2019-06-13 15:13:16 +0200270 if (codec_specific_info) {
271 H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id,
272 is_keyframe, rtp_video_header);
273 }
274 return;
Elad Alonf5b216a2019-01-28 14:25:17 +0100275 case VideoCodecType::kVideoCodecMultiplex:
276 return;
philipelbf2b6202018-08-27 14:33:18 +0200277 }
Elad Alonf5b216a2019-01-28 14:25:17 +0100278 RTC_NOTREACHED() << "Unsupported codec.";
philipelbf2b6202018-08-27 14:33:18 +0200279}
280
philipel8aba8fe2019-06-13 15:13:16 +0200281void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id,
282 bool is_keyframe,
283 RTPVideoHeader* rtp_video_header) {
284 RTPVideoHeader::GenericDescriptorInfo& generic =
285 rtp_video_header->generic.emplace();
286
287 generic.frame_id = shared_frame_id;
288
289 if (is_keyframe) {
290 last_shared_frame_id_[0].fill(-1);
291 } else {
292 int64_t frame_id = last_shared_frame_id_[0][0];
293 RTC_DCHECK_NE(frame_id, -1);
294 RTC_DCHECK_LT(frame_id, shared_frame_id);
295 generic.dependencies.push_back(frame_id);
296 }
297
298 last_shared_frame_id_[0][0] = shared_frame_id;
299}
300
301void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info,
302 int64_t shared_frame_id,
303 bool is_keyframe,
304 RTPVideoHeader* rtp_video_header) {
305 const int temporal_index =
306 h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0;
307
308 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) {
309 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
310 "used with generic frame descriptor.";
311 return;
312 }
313
314 RTPVideoHeader::GenericDescriptorInfo& generic =
315 rtp_video_header->generic.emplace();
316
317 generic.frame_id = shared_frame_id;
318 generic.temporal_index = temporal_index;
319
320 if (is_keyframe) {
321 RTC_DCHECK_EQ(temporal_index, 0);
322 last_shared_frame_id_[/*spatial index*/ 0].fill(-1);
323 last_shared_frame_id_[/*spatial index*/ 0][temporal_index] =
324 shared_frame_id;
325 return;
326 }
327
328 if (h264_info.base_layer_sync) {
329 int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0];
330
331 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
332 if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) {
333 last_shared_frame_id_[/*spatial index*/ 0][i] = -1;
334 }
335 }
336
337 RTC_DCHECK_GE(tl0_frame_id, 0);
338 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
339 generic.dependencies.push_back(tl0_frame_id);
340 } else {
341 for (int i = 0; i <= temporal_index; ++i) {
342 int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i];
343
344 if (frame_id != -1) {
345 RTC_DCHECK_LT(frame_id, shared_frame_id);
346 generic.dependencies.push_back(frame_id);
347 }
348 }
349 }
350
351 last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id;
352}
353
Elad Alonf5b216a2019-01-28 14:25:17 +0100354void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info,
355 int64_t shared_frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200356 bool is_keyframe,
357 RTPVideoHeader* rtp_video_header) {
358 const auto& vp8_header =
359 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
360 const int spatial_index = 0;
361 const int temporal_index =
362 vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0;
363
364 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers ||
365 spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) {
366 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
367 "used with generic frame descriptor.";
368 return;
369 }
370
371 RTPVideoHeader::GenericDescriptorInfo& generic =
372 rtp_video_header->generic.emplace();
373
374 generic.frame_id = shared_frame_id;
375 generic.spatial_index = spatial_index;
376 generic.temporal_index = temporal_index;
377
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000378 if (vp8_info.useExplicitDependencies) {
379 SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe,
380 vp8_header.layerSync, &generic);
381 } else {
382 SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe,
383 spatial_index, temporal_index,
384 vp8_header.layerSync, &generic);
385 }
386}
387
388void RtpPayloadParams::SetDependenciesVp8Deprecated(
389 const CodecSpecificInfoVP8& vp8_info,
390 int64_t shared_frame_id,
391 bool is_keyframe,
392 int spatial_index,
393 int temporal_index,
394 bool layer_sync,
395 RTPVideoHeader::GenericDescriptorInfo* generic) {
396 RTC_DCHECK(!vp8_info.useExplicitDependencies);
397 RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value());
398 new_version_used_ = false;
399
400 if (is_keyframe) {
401 RTC_DCHECK_EQ(temporal_index, 0);
402 last_shared_frame_id_[spatial_index].fill(-1);
403 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
404 return;
405 }
406
407 if (layer_sync) {
408 int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0];
409
410 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
411 if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) {
412 last_shared_frame_id_[spatial_index][i] = -1;
413 }
414 }
415
416 RTC_DCHECK_GE(tl0_frame_id, 0);
417 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
418 generic->dependencies.push_back(tl0_frame_id);
419 } else {
420 for (int i = 0; i <= temporal_index; ++i) {
421 int64_t frame_id = last_shared_frame_id_[spatial_index][i];
422
423 if (frame_id != -1) {
424 RTC_DCHECK_LT(frame_id, shared_frame_id);
425 generic->dependencies.push_back(frame_id);
426 }
427 }
428 }
429
430 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
431}
432
433void RtpPayloadParams::SetDependenciesVp8New(
434 const CodecSpecificInfoVP8& vp8_info,
435 int64_t shared_frame_id,
436 bool is_keyframe,
437 bool layer_sync,
438 RTPVideoHeader::GenericDescriptorInfo* generic) {
439 RTC_DCHECK(vp8_info.useExplicitDependencies);
440 RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value());
441 new_version_used_ = true;
442
Elad Alonf5b216a2019-01-28 14:25:17 +0100443 if (is_keyframe) {
444 RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u);
445 buffer_id_to_frame_id_.fill(shared_frame_id);
446 return;
447 }
448
449 constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount;
450
451 RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u);
452 RTC_DCHECK_LE(vp8_info.referencedBuffersCount,
453 arraysize(vp8_info.referencedBuffers));
454
455 for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) {
456 const size_t referenced_buffer = vp8_info.referencedBuffers[i];
457 RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8);
458 RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size());
459
460 const int64_t dependency_frame_id =
461 buffer_id_to_frame_id_[referenced_buffer];
462 RTC_DCHECK_GE(dependency_frame_id, 0);
463 RTC_DCHECK_LT(dependency_frame_id, shared_frame_id);
464
465 const bool is_new_dependency =
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000466 std::find(generic->dependencies.begin(), generic->dependencies.end(),
467 dependency_frame_id) == generic->dependencies.end();
Elad Alonf5b216a2019-01-28 14:25:17 +0100468 if (is_new_dependency) {
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000469 generic->dependencies.push_back(dependency_frame_id);
Elad Alonf5b216a2019-01-28 14:25:17 +0100470 }
471 }
472
473 RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8);
474 for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) {
475 const size_t updated_id = vp8_info.updatedBuffers[i];
476 buffer_id_to_frame_id_[updated_id] = shared_frame_id;
477 }
478
479 RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8);
480}
481
Stefan Holmerf7044682018-07-17 10:16:41 +0200482} // namespace webrtc