blob: 31cb743d9d93676e1c6ee68509b65585af144e04 [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
Danil Chapovalov02d71fb2020-02-10 16:22:57 +010017#include "absl/algorithm/container.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "absl/container/inlined_vector.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"
philipelbf2b6202018-08-27 14:33:18 +020031#include "system_wrappers/include/field_trial.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020032
33namespace webrtc {
34
35namespace {
36void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info,
Niels Möllerd3b8c632018-08-27 15:33:42 +020037 absl::optional<int> spatial_index,
Stefan Holmerf7044682018-07-17 10:16:41 +020038 RTPVideoHeader* rtp) {
39 rtp->codec = info.codecType;
40 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 }
philipel29d88462018-08-08 14:26:00 +020089 vp9_header.end_of_picture = info.codecSpecific.VP9.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);
Johnny Lee1a1c52b2019-02-08 14:25:40 -050097 rtp->frame_marking.temporal_id = kNoTemporalIdx;
98 if (info.codecSpecific.H264.temporal_idx != kNoTemporalIdx) {
99 rtp->frame_marking.temporal_id = info.codecSpecific.H264.temporal_idx;
100 rtp->frame_marking.layer_id = 0;
101 rtp->frame_marking.independent_frame =
102 info.codecSpecific.H264.idr_frame;
103 rtp->frame_marking.base_layer_sync =
104 info.codecSpecific.H264.base_layer_sync;
105 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200106 return;
107 }
108 case kVideoCodecMultiplex:
109 case kVideoCodecGeneric:
110 rtp->codec = kVideoCodecGeneric;
Niels Möllerd3b8c632018-08-27 15:33:42 +0200111 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +0200112 return;
113 default:
114 return;
115 }
116}
117
118void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) {
119 if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid ||
120 image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) {
121 timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid;
122 return;
123 }
124
125 timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs(
126 image.capture_time_ms_, image.timing_.encode_start_ms);
127 timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs(
128 image.capture_time_ms_, image.timing_.encode_finish_ms);
129 timing->packetization_finish_delta_ms = 0;
130 timing->pacer_exit_delta_ms = 0;
131 timing->network_timestamp_delta_ms = 0;
132 timing->network2_timestamp_delta_ms = 0;
133 timing->flags = image.timing_.flags;
134}
135} // namespace
136
137RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc,
138 const RtpPayloadState* state)
philipelbf2b6202018-08-27 14:33:18 +0200139 : ssrc_(ssrc),
140 generic_picture_id_experiment_(
philipel569397f2018-09-26 12:25:31 +0200141 field_trial::IsEnabled("WebRTC-GenericPictureId")),
142 generic_descriptor_experiment_(
Markus Handellc1cbf6b2020-02-17 20:03:57 +0100143 !field_trial::IsDisabled("WebRTC-GenericDescriptor")) {
philipelbf2b6202018-08-27 14:33:18 +0200144 for (auto& spatial_layer : last_shared_frame_id_)
145 spatial_layer.fill(-1);
146
Elad Alonf5b216a2019-01-28 14:25:17 +0100147 buffer_id_to_frame_id_.fill(-1);
148
Stefan Holmerf7044682018-07-17 10:16:41 +0200149 Random random(rtc::TimeMicros());
150 state_.picture_id =
151 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
152 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
153}
philipelbf2b6202018-08-27 14:33:18 +0200154
155RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
156
Stefan Holmerf7044682018-07-17 10:16:41 +0200157RtpPayloadParams::~RtpPayloadParams() {}
158
159RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
160 const EncodedImage& image,
philipelbf2b6202018-08-27 14:33:18 +0200161 const CodecSpecificInfo* codec_specific_info,
162 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200163 RTPVideoHeader rtp_video_header;
164 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200165 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
166 &rtp_video_header);
Stefan Holmerf7044682018-07-17 10:16:41 +0200167 }
Danil Chapovalov51bf2002019-10-11 10:53:27 +0200168 rtp_video_header.frame_type = image._frameType,
Stefan Holmerf7044682018-07-17 10:16:41 +0200169 rtp_video_header.rotation = image.rotation_;
170 rtp_video_header.content_type = image.content_type_;
171 rtp_video_header.playout_delay = image.playout_delay_;
philipelfab91292018-10-17 14:36:08 +0200172 rtp_video_header.width = image._encodedWidth;
173 rtp_video_header.height = image._encodedHeight;
Johannes Krond0b69a82018-12-03 14:18:53 +0100174 rtp_video_header.color_space = image.ColorSpace()
175 ? absl::make_optional(*image.ColorSpace())
176 : absl::nullopt;
Stefan Holmerf7044682018-07-17 10:16:41 +0200177 SetVideoTiming(image, &rtp_video_header.video_timing);
178
Niels Möller8f7ce222019-03-21 15:43:58 +0100179 const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey;
Stefan Holmerf7044682018-07-17 10:16:41 +0200180 const bool first_frame_in_picture =
181 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
182 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
183 : true;
philipelbf2b6202018-08-27 14:33:18 +0200184
185 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
philipel569397f2018-09-26 12:25:31 +0200186
187 if (generic_descriptor_experiment_)
Elad Alonf5b216a2019-01-28 14:25:17 +0100188 SetGeneric(codec_specific_info, shared_frame_id, is_keyframe,
189 &rtp_video_header);
philipelbf2b6202018-08-27 14:33:18 +0200190
Stefan Holmerf7044682018-07-17 10:16:41 +0200191 return rtp_video_header;
192}
193
194uint32_t RtpPayloadParams::ssrc() const {
195 return ssrc_;
196}
197
198RtpPayloadState RtpPayloadParams::state() const {
199 return state_;
200}
201
philipelbf2b6202018-08-27 14:33:18 +0200202void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
203 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200204 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
205 if (first_frame_in_picture) {
206 state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
207 }
208 if (rtp_video_header->codec == kVideoCodecVP8) {
Philip Eliassond52a1a62018-09-07 13:03:55 +0000209 auto& vp8_header =
210 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
211 vp8_header.pictureId = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200212
Philip Eliassond52a1a62018-09-07 13:03:55 +0000213 if (vp8_header.temporalIdx != kNoTemporalIdx) {
214 if (vp8_header.temporalIdx == 0) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200215 ++state_.tl0_pic_idx;
216 }
Philip Eliassond52a1a62018-09-07 13:03:55 +0000217 vp8_header.tl0PicIdx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200218 }
219 }
220 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 14:26:00 +0200221 auto& vp9_header =
222 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
223 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200224
225 // Note that in the case that we have no temporal layers but we do have
226 // spatial layers, packets will carry layering info with a temporal_idx of
227 // zero, and we then have to set and increment tl0_pic_idx.
philipel29d88462018-08-08 14:26:00 +0200228 if (vp9_header.temporal_idx != kNoTemporalIdx ||
229 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200230 if (first_frame_in_picture &&
philipel29d88462018-08-08 14:26:00 +0200231 (vp9_header.temporal_idx == 0 ||
232 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200233 ++state_.tl0_pic_idx;
234 }
philipel29d88462018-08-08 14:26:00 +0200235 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200236 }
237 }
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500238 if (rtp_video_header->codec == kVideoCodecH264) {
239 if (rtp_video_header->frame_marking.temporal_id != kNoTemporalIdx) {
240 if (rtp_video_header->frame_marking.temporal_id == 0) {
241 ++state_.tl0_pic_idx;
242 }
243 rtp_video_header->frame_marking.tl0_pic_idx = state_.tl0_pic_idx;
244 }
245 }
philipelbf2b6202018-08-27 14:33:18 +0200246 if (generic_picture_id_experiment_ &&
247 rtp_video_header->codec == kVideoCodecGeneric) {
Danil Chapovalovb6bf0b22020-01-28 18:36:57 +0100248 rtp_video_header->video_type_header.emplace<RTPVideoHeaderLegacyGeneric>()
249 .picture_id = state_.picture_id;
philipelbf2b6202018-08-27 14:33:18 +0200250 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200251}
philipelbf2b6202018-08-27 14:33:18 +0200252
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100253RTPVideoHeader::GenericDescriptorInfo
254RtpPayloadParams::GenericDescriptorFromFrameInfo(
255 const GenericFrameInfo& frame_info,
256 int64_t frame_id,
257 VideoFrameType frame_type) {
258 RTPVideoHeader::GenericDescriptorInfo generic;
259 generic.frame_id = frame_id;
260 generic.dependencies = dependencies_calculator_.FromBuffersUsage(
261 frame_type, frame_id, frame_info.encoder_buffers);
262 generic.spatial_index = frame_info.spatial_id;
263 generic.temporal_index = frame_info.temporal_id;
264 generic.decode_target_indications = frame_info.decode_target_indications;
265 generic.discardable =
266 absl::c_linear_search(frame_info.decode_target_indications,
267 DecodeTargetIndication::kDiscardable);
268 return generic;
269}
270
Elad Alonf5b216a2019-01-28 14:25:17 +0100271void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info,
272 int64_t frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200273 bool is_keyframe,
274 RTPVideoHeader* rtp_video_header) {
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100275 if (codec_specific_info && codec_specific_info->generic_frame_info &&
276 !codec_specific_info->generic_frame_info->encoder_buffers.empty()) {
277 rtp_video_header->generic =
278 GenericDescriptorFromFrameInfo(*codec_specific_info->generic_frame_info,
279 frame_id, rtp_video_header->frame_type);
280 return;
281 }
282
Elad Alonf5b216a2019-01-28 14:25:17 +0100283 switch (rtp_video_header->codec) {
284 case VideoCodecType::kVideoCodecGeneric:
philipel8aba8fe2019-06-13 15:13:16 +0200285 GenericToGeneric(frame_id, is_keyframe, rtp_video_header);
Elad Alonf5b216a2019-01-28 14:25:17 +0100286 return;
287 case VideoCodecType::kVideoCodecVP8:
288 if (codec_specific_info) {
289 Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id,
290 is_keyframe, rtp_video_header);
291 }
292 return;
293 case VideoCodecType::kVideoCodecVP9:
Danil Chapovalovdc368292019-11-26 14:48:20 +0100294 case VideoCodecType::kVideoCodecAV1:
295 // TODO(philipel): Implement VP9 and AV1 to generic descriptor.
Elad Alonf5b216a2019-01-28 14:25:17 +0100296 return;
297 case VideoCodecType::kVideoCodecH264:
philipel8aba8fe2019-06-13 15:13:16 +0200298 if (codec_specific_info) {
299 H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id,
300 is_keyframe, rtp_video_header);
301 }
302 return;
Elad Alonf5b216a2019-01-28 14:25:17 +0100303 case VideoCodecType::kVideoCodecMultiplex:
304 return;
philipelbf2b6202018-08-27 14:33:18 +0200305 }
Elad Alonf5b216a2019-01-28 14:25:17 +0100306 RTC_NOTREACHED() << "Unsupported codec.";
philipelbf2b6202018-08-27 14:33:18 +0200307}
308
philipel8aba8fe2019-06-13 15:13:16 +0200309void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id,
310 bool is_keyframe,
311 RTPVideoHeader* rtp_video_header) {
312 RTPVideoHeader::GenericDescriptorInfo& generic =
313 rtp_video_header->generic.emplace();
314
315 generic.frame_id = shared_frame_id;
316
317 if (is_keyframe) {
318 last_shared_frame_id_[0].fill(-1);
319 } else {
320 int64_t frame_id = last_shared_frame_id_[0][0];
321 RTC_DCHECK_NE(frame_id, -1);
322 RTC_DCHECK_LT(frame_id, shared_frame_id);
323 generic.dependencies.push_back(frame_id);
324 }
325
326 last_shared_frame_id_[0][0] = shared_frame_id;
327}
328
329void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info,
330 int64_t shared_frame_id,
331 bool is_keyframe,
332 RTPVideoHeader* rtp_video_header) {
333 const int temporal_index =
334 h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0;
335
336 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) {
337 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
338 "used with generic frame descriptor.";
339 return;
340 }
341
342 RTPVideoHeader::GenericDescriptorInfo& generic =
343 rtp_video_header->generic.emplace();
344
345 generic.frame_id = shared_frame_id;
346 generic.temporal_index = temporal_index;
347
348 if (is_keyframe) {
349 RTC_DCHECK_EQ(temporal_index, 0);
350 last_shared_frame_id_[/*spatial index*/ 0].fill(-1);
351 last_shared_frame_id_[/*spatial index*/ 0][temporal_index] =
352 shared_frame_id;
353 return;
354 }
355
356 if (h264_info.base_layer_sync) {
357 int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0];
358
359 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
360 if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) {
361 last_shared_frame_id_[/*spatial index*/ 0][i] = -1;
362 }
363 }
364
365 RTC_DCHECK_GE(tl0_frame_id, 0);
366 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
367 generic.dependencies.push_back(tl0_frame_id);
368 } else {
369 for (int i = 0; i <= temporal_index; ++i) {
370 int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i];
371
372 if (frame_id != -1) {
373 RTC_DCHECK_LT(frame_id, shared_frame_id);
374 generic.dependencies.push_back(frame_id);
375 }
376 }
377 }
378
379 last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id;
380}
381
Elad Alonf5b216a2019-01-28 14:25:17 +0100382void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info,
383 int64_t shared_frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200384 bool is_keyframe,
385 RTPVideoHeader* rtp_video_header) {
386 const auto& vp8_header =
387 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
388 const int spatial_index = 0;
389 const int temporal_index =
390 vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0;
391
392 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers ||
393 spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) {
394 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
395 "used with generic frame descriptor.";
396 return;
397 }
398
399 RTPVideoHeader::GenericDescriptorInfo& generic =
400 rtp_video_header->generic.emplace();
401
402 generic.frame_id = shared_frame_id;
403 generic.spatial_index = spatial_index;
404 generic.temporal_index = temporal_index;
405
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000406 if (vp8_info.useExplicitDependencies) {
407 SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe,
408 vp8_header.layerSync, &generic);
409 } else {
410 SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe,
411 spatial_index, temporal_index,
412 vp8_header.layerSync, &generic);
413 }
414}
415
416void RtpPayloadParams::SetDependenciesVp8Deprecated(
417 const CodecSpecificInfoVP8& vp8_info,
418 int64_t shared_frame_id,
419 bool is_keyframe,
420 int spatial_index,
421 int temporal_index,
422 bool layer_sync,
423 RTPVideoHeader::GenericDescriptorInfo* generic) {
424 RTC_DCHECK(!vp8_info.useExplicitDependencies);
425 RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value());
426 new_version_used_ = false;
427
428 if (is_keyframe) {
429 RTC_DCHECK_EQ(temporal_index, 0);
430 last_shared_frame_id_[spatial_index].fill(-1);
431 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
432 return;
433 }
434
435 if (layer_sync) {
436 int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0];
437
438 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
439 if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) {
440 last_shared_frame_id_[spatial_index][i] = -1;
441 }
442 }
443
444 RTC_DCHECK_GE(tl0_frame_id, 0);
445 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
446 generic->dependencies.push_back(tl0_frame_id);
447 } else {
448 for (int i = 0; i <= temporal_index; ++i) {
449 int64_t frame_id = last_shared_frame_id_[spatial_index][i];
450
451 if (frame_id != -1) {
452 RTC_DCHECK_LT(frame_id, shared_frame_id);
453 generic->dependencies.push_back(frame_id);
454 }
455 }
456 }
457
458 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
459}
460
461void RtpPayloadParams::SetDependenciesVp8New(
462 const CodecSpecificInfoVP8& vp8_info,
463 int64_t shared_frame_id,
464 bool is_keyframe,
465 bool layer_sync,
466 RTPVideoHeader::GenericDescriptorInfo* generic) {
467 RTC_DCHECK(vp8_info.useExplicitDependencies);
468 RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value());
469 new_version_used_ = true;
470
Elad Alonf5b216a2019-01-28 14:25:17 +0100471 if (is_keyframe) {
472 RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u);
473 buffer_id_to_frame_id_.fill(shared_frame_id);
474 return;
475 }
476
477 constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount;
478
479 RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u);
480 RTC_DCHECK_LE(vp8_info.referencedBuffersCount,
481 arraysize(vp8_info.referencedBuffers));
482
483 for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) {
484 const size_t referenced_buffer = vp8_info.referencedBuffers[i];
485 RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8);
486 RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size());
487
488 const int64_t dependency_frame_id =
489 buffer_id_to_frame_id_[referenced_buffer];
490 RTC_DCHECK_GE(dependency_frame_id, 0);
491 RTC_DCHECK_LT(dependency_frame_id, shared_frame_id);
492
493 const bool is_new_dependency =
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000494 std::find(generic->dependencies.begin(), generic->dependencies.end(),
495 dependency_frame_id) == generic->dependencies.end();
Elad Alonf5b216a2019-01-28 14:25:17 +0100496 if (is_new_dependency) {
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000497 generic->dependencies.push_back(dependency_frame_id);
Elad Alonf5b216a2019-01-28 14:25:17 +0100498 }
499 }
500
501 RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8);
502 for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) {
503 const size_t updated_id = vp8_info.updatedBuffers[i];
504 buffer_id_to_frame_id_[updated_id] = shared_frame_id;
505 }
506
507 RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8);
508}
509
Stefan Holmerf7044682018-07-17 10:16:41 +0200510} // namespace webrtc