blob: 599012314a2b00e64455ece66272df87d1afdf99 [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>
Elad Alonf5b216a2019-01-28 14:25:17 +010014#include <algorithm>
Yves Gerey3e707812018-11-28 16:47:49 +010015
16#include "absl/container/inlined_vector.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "absl/types/variant.h"
18#include "api/video/video_timing.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "modules/video_coding/codecs/h264/include/h264_globals.h"
20#include "modules/video_coding/codecs/interface/common_constants.h"
21#include "modules/video_coding/codecs/vp8/include/vp8_globals.h"
22#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Elad Alonf5b216a2019-01-28 14:25:17 +010023#include "rtc_base/arraysize.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020024#include "rtc_base/checks.h"
philipelbf2b6202018-08-27 14:33:18 +020025#include "rtc_base/logging.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020026#include "rtc_base/random.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/time_utils.h"
philipelbf2b6202018-08-27 14:33:18 +020028#include "system_wrappers/include/field_trial.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020029
30namespace webrtc {
31
32namespace {
33void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info,
Niels Möllerd3b8c632018-08-27 15:33:42 +020034 absl::optional<int> spatial_index,
Stefan Holmerf7044682018-07-17 10:16:41 +020035 RTPVideoHeader* rtp) {
36 rtp->codec = info.codecType;
37 switch (info.codecType) {
38 case kVideoCodecVP8: {
Philip Eliassond52a1a62018-09-07 13:03:55 +000039 auto& vp8_header = rtp->video_type_header.emplace<RTPVideoHeaderVP8>();
40 vp8_header.InitRTPVideoHeaderVP8();
41 vp8_header.nonReference = info.codecSpecific.VP8.nonReference;
42 vp8_header.temporalIdx = info.codecSpecific.VP8.temporalIdx;
43 vp8_header.layerSync = info.codecSpecific.VP8.layerSync;
44 vp8_header.keyIdx = info.codecSpecific.VP8.keyIdx;
Niels Möllerd3b8c632018-08-27 15:33:42 +020045 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020046 return;
47 }
48 case kVideoCodecVP9: {
philipel29d88462018-08-08 14:26:00 +020049 auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>();
50 vp9_header.InitRTPVideoHeaderVP9();
51 vp9_header.inter_pic_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020052 info.codecSpecific.VP9.inter_pic_predicted;
philipel29d88462018-08-08 14:26:00 +020053 vp9_header.flexible_mode = info.codecSpecific.VP9.flexible_mode;
54 vp9_header.ss_data_available = info.codecSpecific.VP9.ss_data_available;
55 vp9_header.non_ref_for_inter_layer_pred =
Stefan Holmerf7044682018-07-17 10:16:41 +020056 info.codecSpecific.VP9.non_ref_for_inter_layer_pred;
philipel29d88462018-08-08 14:26:00 +020057 vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx;
philipel29d88462018-08-08 14:26:00 +020058 vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch;
59 vp9_header.inter_layer_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020060 info.codecSpecific.VP9.inter_layer_predicted;
philipel29d88462018-08-08 14:26:00 +020061 vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx;
62 vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers;
Niels Möllerd3b8c632018-08-27 15:33:42 +020063 if (vp9_header.num_spatial_layers > 1) {
64 vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx);
65 } else {
66 vp9_header.spatial_idx = kNoSpatialIdx;
67 }
Stefan Holmerf7044682018-07-17 10:16:41 +020068 if (info.codecSpecific.VP9.ss_data_available) {
philipel29d88462018-08-08 14:26:00 +020069 vp9_header.spatial_layer_resolution_present =
Stefan Holmerf7044682018-07-17 10:16:41 +020070 info.codecSpecific.VP9.spatial_layer_resolution_present;
71 if (info.codecSpecific.VP9.spatial_layer_resolution_present) {
72 for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers;
73 ++i) {
philipel29d88462018-08-08 14:26:00 +020074 vp9_header.width[i] = info.codecSpecific.VP9.width[i];
75 vp9_header.height[i] = info.codecSpecific.VP9.height[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020076 }
77 }
philipel29d88462018-08-08 14:26:00 +020078 vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof);
Stefan Holmerf7044682018-07-17 10:16:41 +020079 }
80
philipel29d88462018-08-08 14:26:00 +020081 vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics;
Stefan Holmerf7044682018-07-17 10:16:41 +020082 for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) {
philipel29d88462018-08-08 14:26:00 +020083 vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020084 }
philipel29d88462018-08-08 14:26:00 +020085 vp9_header.end_of_picture = info.codecSpecific.VP9.end_of_picture;
Stefan Holmerf7044682018-07-17 10:16:41 +020086 return;
87 }
88 case kVideoCodecH264: {
philipel7d745e52018-08-02 14:03:53 +020089 auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>();
90 h264_header.packetization_mode =
Stefan Holmerf7044682018-07-17 10:16:41 +020091 info.codecSpecific.H264.packetization_mode;
Niels Möllerd3b8c632018-08-27 15:33:42 +020092 rtp->simulcastIdx = spatial_index.value_or(0);
Johnny Lee1a1c52b2019-02-08 14:25:40 -050093 rtp->frame_marking.temporal_id = kNoTemporalIdx;
94 if (info.codecSpecific.H264.temporal_idx != kNoTemporalIdx) {
95 rtp->frame_marking.temporal_id = info.codecSpecific.H264.temporal_idx;
96 rtp->frame_marking.layer_id = 0;
97 rtp->frame_marking.independent_frame =
98 info.codecSpecific.H264.idr_frame;
99 rtp->frame_marking.base_layer_sync =
100 info.codecSpecific.H264.base_layer_sync;
101 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200102 return;
103 }
104 case kVideoCodecMultiplex:
105 case kVideoCodecGeneric:
106 rtp->codec = kVideoCodecGeneric;
Niels Möllerd3b8c632018-08-27 15:33:42 +0200107 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +0200108 return;
109 default:
110 return;
111 }
112}
113
114void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) {
115 if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid ||
116 image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) {
117 timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid;
118 return;
119 }
120
121 timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs(
122 image.capture_time_ms_, image.timing_.encode_start_ms);
123 timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs(
124 image.capture_time_ms_, image.timing_.encode_finish_ms);
125 timing->packetization_finish_delta_ms = 0;
126 timing->pacer_exit_delta_ms = 0;
127 timing->network_timestamp_delta_ms = 0;
128 timing->network2_timestamp_delta_ms = 0;
129 timing->flags = image.timing_.flags;
130}
131} // namespace
132
133RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc,
134 const RtpPayloadState* state)
philipelbf2b6202018-08-27 14:33:18 +0200135 : ssrc_(ssrc),
136 generic_picture_id_experiment_(
philipel569397f2018-09-26 12:25:31 +0200137 field_trial::IsEnabled("WebRTC-GenericPictureId")),
138 generic_descriptor_experiment_(
139 field_trial::IsEnabled("WebRTC-GenericDescriptor")) {
philipelbf2b6202018-08-27 14:33:18 +0200140 for (auto& spatial_layer : last_shared_frame_id_)
141 spatial_layer.fill(-1);
142
Elad Alonf5b216a2019-01-28 14:25:17 +0100143 buffer_id_to_frame_id_.fill(-1);
144
Stefan Holmerf7044682018-07-17 10:16:41 +0200145 Random random(rtc::TimeMicros());
146 state_.picture_id =
147 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
148 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
149}
philipelbf2b6202018-08-27 14:33:18 +0200150
151RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
152
Stefan Holmerf7044682018-07-17 10:16:41 +0200153RtpPayloadParams::~RtpPayloadParams() {}
154
155RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
156 const EncodedImage& image,
philipelbf2b6202018-08-27 14:33:18 +0200157 const CodecSpecificInfo* codec_specific_info,
158 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200159 RTPVideoHeader rtp_video_header;
160 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200161 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
162 &rtp_video_header);
Stefan Holmerf7044682018-07-17 10:16:41 +0200163 }
164 rtp_video_header.rotation = image.rotation_;
165 rtp_video_header.content_type = image.content_type_;
166 rtp_video_header.playout_delay = image.playout_delay_;
philipelfab91292018-10-17 14:36:08 +0200167 rtp_video_header.width = image._encodedWidth;
168 rtp_video_header.height = image._encodedHeight;
Johannes Krond0b69a82018-12-03 14:18:53 +0100169 rtp_video_header.color_space = image.ColorSpace()
170 ? absl::make_optional(*image.ColorSpace())
171 : absl::nullopt;
Stefan Holmerf7044682018-07-17 10:16:41 +0200172 SetVideoTiming(image, &rtp_video_header.video_timing);
173
Niels Möller8f7ce222019-03-21 15:43:58 +0100174 const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey;
Stefan Holmerf7044682018-07-17 10:16:41 +0200175 const bool first_frame_in_picture =
176 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
177 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
178 : true;
philipelbf2b6202018-08-27 14:33:18 +0200179
180 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
philipel569397f2018-09-26 12:25:31 +0200181
182 if (generic_descriptor_experiment_)
Elad Alonf5b216a2019-01-28 14:25:17 +0100183 SetGeneric(codec_specific_info, shared_frame_id, is_keyframe,
184 &rtp_video_header);
philipelbf2b6202018-08-27 14:33:18 +0200185
Stefan Holmerf7044682018-07-17 10:16:41 +0200186 return rtp_video_header;
187}
188
189uint32_t RtpPayloadParams::ssrc() const {
190 return ssrc_;
191}
192
193RtpPayloadState RtpPayloadParams::state() const {
194 return state_;
195}
196
philipelbf2b6202018-08-27 14:33:18 +0200197void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
198 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200199 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
200 if (first_frame_in_picture) {
201 state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
202 }
203 if (rtp_video_header->codec == kVideoCodecVP8) {
Philip Eliassond52a1a62018-09-07 13:03:55 +0000204 auto& vp8_header =
205 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
206 vp8_header.pictureId = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200207
Philip Eliassond52a1a62018-09-07 13:03:55 +0000208 if (vp8_header.temporalIdx != kNoTemporalIdx) {
209 if (vp8_header.temporalIdx == 0) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200210 ++state_.tl0_pic_idx;
211 }
Philip Eliassond52a1a62018-09-07 13:03:55 +0000212 vp8_header.tl0PicIdx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200213 }
214 }
215 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 14:26:00 +0200216 auto& vp9_header =
217 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
218 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200219
220 // Note that in the case that we have no temporal layers but we do have
221 // spatial layers, packets will carry layering info with a temporal_idx of
222 // zero, and we then have to set and increment tl0_pic_idx.
philipel29d88462018-08-08 14:26:00 +0200223 if (vp9_header.temporal_idx != kNoTemporalIdx ||
224 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200225 if (first_frame_in_picture &&
philipel29d88462018-08-08 14:26:00 +0200226 (vp9_header.temporal_idx == 0 ||
227 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200228 ++state_.tl0_pic_idx;
229 }
philipel29d88462018-08-08 14:26:00 +0200230 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200231 }
232 }
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500233 if (rtp_video_header->codec == kVideoCodecH264) {
234 if (rtp_video_header->frame_marking.temporal_id != kNoTemporalIdx) {
235 if (rtp_video_header->frame_marking.temporal_id == 0) {
236 ++state_.tl0_pic_idx;
237 }
238 rtp_video_header->frame_marking.tl0_pic_idx = state_.tl0_pic_idx;
239 }
240 }
philipelbf2b6202018-08-27 14:33:18 +0200241 // There are currently two generic descriptors in WebRTC. The old descriptor
242 // can not share a picture id space between simulcast streams, so we use the
243 // |picture_id| in this case. We let the |picture_id| tag along in |frame_id|
244 // until the old generic format can be removed.
245 // TODO(philipel): Remove this when the new generic format has been fully
246 // implemented.
247 if (generic_picture_id_experiment_ &&
248 rtp_video_header->codec == kVideoCodecGeneric) {
249 rtp_video_header->generic.emplace().frame_id = state_.picture_id;
250 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200251}
philipelbf2b6202018-08-27 14:33:18 +0200252
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) {
Elad Alonf5b216a2019-01-28 14:25:17 +0100257 switch (rtp_video_header->codec) {
258 case VideoCodecType::kVideoCodecGeneric:
philipel8aba8fe2019-06-13 15:13:16 +0200259 GenericToGeneric(frame_id, is_keyframe, rtp_video_header);
Elad Alonf5b216a2019-01-28 14:25:17 +0100260 return;
261 case VideoCodecType::kVideoCodecVP8:
262 if (codec_specific_info) {
263 Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id,
264 is_keyframe, rtp_video_header);
265 }
266 return;
267 case VideoCodecType::kVideoCodecVP9:
268 // TODO(philipel): Implement VP9 to new generic descriptor.
269 return;
270 case VideoCodecType::kVideoCodecH264:
philipel8aba8fe2019-06-13 15:13:16 +0200271 if (codec_specific_info) {
272 H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id,
273 is_keyframe, rtp_video_header);
274 }
275 return;
Elad Alonf5b216a2019-01-28 14:25:17 +0100276 case VideoCodecType::kVideoCodecMultiplex:
277 return;
philipelbf2b6202018-08-27 14:33:18 +0200278 }
Elad Alonf5b216a2019-01-28 14:25:17 +0100279 RTC_NOTREACHED() << "Unsupported codec.";
philipelbf2b6202018-08-27 14:33:18 +0200280}
281
philipel8aba8fe2019-06-13 15:13:16 +0200282void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id,
283 bool is_keyframe,
284 RTPVideoHeader* rtp_video_header) {
285 RTPVideoHeader::GenericDescriptorInfo& generic =
286 rtp_video_header->generic.emplace();
287
288 generic.frame_id = shared_frame_id;
289
290 if (is_keyframe) {
291 last_shared_frame_id_[0].fill(-1);
292 } else {
293 int64_t frame_id = last_shared_frame_id_[0][0];
294 RTC_DCHECK_NE(frame_id, -1);
295 RTC_DCHECK_LT(frame_id, shared_frame_id);
296 generic.dependencies.push_back(frame_id);
297 }
298
299 last_shared_frame_id_[0][0] = shared_frame_id;
300}
301
302void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info,
303 int64_t shared_frame_id,
304 bool is_keyframe,
305 RTPVideoHeader* rtp_video_header) {
306 const int temporal_index =
307 h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0;
308
309 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) {
310 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
311 "used with generic frame descriptor.";
312 return;
313 }
314
315 RTPVideoHeader::GenericDescriptorInfo& generic =
316 rtp_video_header->generic.emplace();
317
318 generic.frame_id = shared_frame_id;
319 generic.temporal_index = temporal_index;
320
321 if (is_keyframe) {
322 RTC_DCHECK_EQ(temporal_index, 0);
323 last_shared_frame_id_[/*spatial index*/ 0].fill(-1);
324 last_shared_frame_id_[/*spatial index*/ 0][temporal_index] =
325 shared_frame_id;
326 return;
327 }
328
329 if (h264_info.base_layer_sync) {
330 int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0];
331
332 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
333 if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) {
334 last_shared_frame_id_[/*spatial index*/ 0][i] = -1;
335 }
336 }
337
338 RTC_DCHECK_GE(tl0_frame_id, 0);
339 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
340 generic.dependencies.push_back(tl0_frame_id);
341 } else {
342 for (int i = 0; i <= temporal_index; ++i) {
343 int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i];
344
345 if (frame_id != -1) {
346 RTC_DCHECK_LT(frame_id, shared_frame_id);
347 generic.dependencies.push_back(frame_id);
348 }
349 }
350 }
351
352 last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id;
353}
354
Elad Alonf5b216a2019-01-28 14:25:17 +0100355void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info,
356 int64_t shared_frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200357 bool is_keyframe,
358 RTPVideoHeader* rtp_video_header) {
359 const auto& vp8_header =
360 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
361 const int spatial_index = 0;
362 const int temporal_index =
363 vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0;
364
365 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers ||
366 spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) {
367 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
368 "used with generic frame descriptor.";
369 return;
370 }
371
372 RTPVideoHeader::GenericDescriptorInfo& generic =
373 rtp_video_header->generic.emplace();
374
375 generic.frame_id = shared_frame_id;
376 generic.spatial_index = spatial_index;
377 generic.temporal_index = temporal_index;
378
Elad Alonf5b216a2019-01-28 14:25:17 +0100379 if (vp8_info.useExplicitDependencies) {
380 SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe,
381 vp8_header.layerSync, &generic);
382 } else {
383 SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe,
384 spatial_index, temporal_index,
385 vp8_header.layerSync, &generic);
386 }
387}
388
389void RtpPayloadParams::SetDependenciesVp8Deprecated(
390 const CodecSpecificInfoVP8& vp8_info,
391 int64_t shared_frame_id,
392 bool is_keyframe,
393 int spatial_index,
394 int temporal_index,
395 bool layer_sync,
396 RTPVideoHeader::GenericDescriptorInfo* generic) {
397 RTC_DCHECK(!vp8_info.useExplicitDependencies);
398 RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value());
399 new_version_used_ = false;
400
philipelbf2b6202018-08-27 14:33:18 +0200401 if (is_keyframe) {
402 RTC_DCHECK_EQ(temporal_index, 0);
403 last_shared_frame_id_[spatial_index].fill(-1);
404 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
405 return;
406 }
407
Elad Alonf5b216a2019-01-28 14:25:17 +0100408 if (layer_sync) {
philipelbf2b6202018-08-27 14:33:18 +0200409 int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0];
410
411 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
412 if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) {
413 last_shared_frame_id_[spatial_index][i] = -1;
414 }
415 }
416
417 RTC_DCHECK_GE(tl0_frame_id, 0);
418 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
Elad Alonf5b216a2019-01-28 14:25:17 +0100419 generic->dependencies.push_back(tl0_frame_id);
philipelbf2b6202018-08-27 14:33:18 +0200420 } else {
421 for (int i = 0; i <= temporal_index; ++i) {
422 int64_t frame_id = last_shared_frame_id_[spatial_index][i];
423
424 if (frame_id != -1) {
425 RTC_DCHECK_LT(frame_id, shared_frame_id);
Elad Alonf5b216a2019-01-28 14:25:17 +0100426 generic->dependencies.push_back(frame_id);
philipelbf2b6202018-08-27 14:33:18 +0200427 }
428 }
429 }
430
431 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
432}
433
Elad Alonf5b216a2019-01-28 14:25:17 +0100434void RtpPayloadParams::SetDependenciesVp8New(
435 const CodecSpecificInfoVP8& vp8_info,
436 int64_t shared_frame_id,
437 bool is_keyframe,
438 bool layer_sync,
439 RTPVideoHeader::GenericDescriptorInfo* generic) {
440 RTC_DCHECK(vp8_info.useExplicitDependencies);
441 RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value());
442 new_version_used_ = true;
443
444 if (is_keyframe) {
445 RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u);
446 buffer_id_to_frame_id_.fill(shared_frame_id);
447 return;
448 }
449
450 constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount;
451
452 RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u);
453 RTC_DCHECK_LE(vp8_info.referencedBuffersCount,
454 arraysize(vp8_info.referencedBuffers));
455
456 for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) {
457 const size_t referenced_buffer = vp8_info.referencedBuffers[i];
458 RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8);
459 RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size());
460
461 const int64_t dependency_frame_id =
462 buffer_id_to_frame_id_[referenced_buffer];
463 RTC_DCHECK_GE(dependency_frame_id, 0);
464 RTC_DCHECK_LT(dependency_frame_id, shared_frame_id);
465
466 const bool is_new_dependency =
467 std::find(generic->dependencies.begin(), generic->dependencies.end(),
468 dependency_frame_id) == generic->dependencies.end();
469 if (is_new_dependency) {
470 generic->dependencies.push_back(dependency_frame_id);
471 }
472 }
473
474 RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8);
475 for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) {
476 const size_t updated_id = vp8_info.updatedBuffers[i];
477 buffer_id_to_frame_id_[updated_id] = shared_frame_id;
478 }
479
480 RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8);
481}
482
Stefan Holmerf7044682018-07-17 10:16:41 +0200483} // namespace webrtc