blob: bce3c13055afa8aa8f1160586238ea464e2bc81a [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"
Erik Språngcbc0cba2020-04-18 14:36:59 +020019#include "absl/strings/match.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "absl/types/variant.h"
21#include "api/video/video_timing.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "modules/video_coding/codecs/h264/include/h264_globals.h"
23#include "modules/video_coding/codecs/interface/common_constants.h"
24#include "modules/video_coding/codecs/vp8/include/vp8_globals.h"
25#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Danil Chapovalov02d71fb2020-02-10 16:22:57 +010026#include "modules/video_coding/frame_dependencies_calculator.h"
Elad Alonf5b216a2019-01-28 14:25:17 +010027#include "rtc_base/arraysize.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020028#include "rtc_base/checks.h"
philipelbf2b6202018-08-27 14:33:18 +020029#include "rtc_base/logging.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020030#include "rtc_base/random.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/time_utils.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,
Erik Språngcbc0cba2020-04-18 14:36:59 +0200138 const RtpPayloadState* state,
139 const WebRtcKeyValueConfig& trials)
philipelbf2b6202018-08-27 14:33:18 +0200140 : ssrc_(ssrc),
141 generic_picture_id_experiment_(
Erik Språngcbc0cba2020-04-18 14:36:59 +0200142 absl::StartsWith(trials.Lookup("WebRTC-GenericPictureId"),
143 "Enabled")),
philipel569397f2018-09-26 12:25:31 +0200144 generic_descriptor_experiment_(
Erik Språngcbc0cba2020-04-18 14:36:59 +0200145 !absl::StartsWith(trials.Lookup("WebRTC-GenericDescriptor"),
146 "Disabled")) {
philipelbf2b6202018-08-27 14:33:18 +0200147 for (auto& spatial_layer : last_shared_frame_id_)
148 spatial_layer.fill(-1);
149
Elad Alonf5b216a2019-01-28 14:25:17 +0100150 buffer_id_to_frame_id_.fill(-1);
151
Stefan Holmerf7044682018-07-17 10:16:41 +0200152 Random random(rtc::TimeMicros());
153 state_.picture_id =
154 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
155 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
156}
philipelbf2b6202018-08-27 14:33:18 +0200157
158RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
159
Stefan Holmerf7044682018-07-17 10:16:41 +0200160RtpPayloadParams::~RtpPayloadParams() {}
161
162RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
163 const EncodedImage& image,
philipelbf2b6202018-08-27 14:33:18 +0200164 const CodecSpecificInfo* codec_specific_info,
165 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200166 RTPVideoHeader rtp_video_header;
167 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200168 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
169 &rtp_video_header);
Stefan Holmerf7044682018-07-17 10:16:41 +0200170 }
Danil Chapovalov51bf2002019-10-11 10:53:27 +0200171 rtp_video_header.frame_type = image._frameType,
Stefan Holmerf7044682018-07-17 10:16:41 +0200172 rtp_video_header.rotation = image.rotation_;
173 rtp_video_header.content_type = image.content_type_;
174 rtp_video_header.playout_delay = image.playout_delay_;
philipelfab91292018-10-17 14:36:08 +0200175 rtp_video_header.width = image._encodedWidth;
176 rtp_video_header.height = image._encodedHeight;
Johannes Krond0b69a82018-12-03 14:18:53 +0100177 rtp_video_header.color_space = image.ColorSpace()
178 ? absl::make_optional(*image.ColorSpace())
179 : absl::nullopt;
Stefan Holmerf7044682018-07-17 10:16:41 +0200180 SetVideoTiming(image, &rtp_video_header.video_timing);
181
Niels Möller8f7ce222019-03-21 15:43:58 +0100182 const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey;
Stefan Holmerf7044682018-07-17 10:16:41 +0200183 const bool first_frame_in_picture =
184 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
185 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
186 : true;
philipelbf2b6202018-08-27 14:33:18 +0200187
188 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
philipel569397f2018-09-26 12:25:31 +0200189
190 if (generic_descriptor_experiment_)
Elad Alonf5b216a2019-01-28 14:25:17 +0100191 SetGeneric(codec_specific_info, shared_frame_id, is_keyframe,
192 &rtp_video_header);
philipelbf2b6202018-08-27 14:33:18 +0200193
Stefan Holmerf7044682018-07-17 10:16:41 +0200194 return rtp_video_header;
195}
196
197uint32_t RtpPayloadParams::ssrc() const {
198 return ssrc_;
199}
200
201RtpPayloadState RtpPayloadParams::state() const {
202 return state_;
203}
204
philipelbf2b6202018-08-27 14:33:18 +0200205void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
206 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200207 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
208 if (first_frame_in_picture) {
209 state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
210 }
211 if (rtp_video_header->codec == kVideoCodecVP8) {
Philip Eliassond52a1a62018-09-07 13:03:55 +0000212 auto& vp8_header =
213 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
214 vp8_header.pictureId = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200215
Philip Eliassond52a1a62018-09-07 13:03:55 +0000216 if (vp8_header.temporalIdx != kNoTemporalIdx) {
217 if (vp8_header.temporalIdx == 0) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200218 ++state_.tl0_pic_idx;
219 }
Philip Eliassond52a1a62018-09-07 13:03:55 +0000220 vp8_header.tl0PicIdx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200221 }
222 }
223 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 14:26:00 +0200224 auto& vp9_header =
225 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
226 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200227
228 // Note that in the case that we have no temporal layers but we do have
229 // spatial layers, packets will carry layering info with a temporal_idx of
230 // zero, and we then have to set and increment tl0_pic_idx.
philipel29d88462018-08-08 14:26:00 +0200231 if (vp9_header.temporal_idx != kNoTemporalIdx ||
232 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200233 if (first_frame_in_picture &&
philipel29d88462018-08-08 14:26:00 +0200234 (vp9_header.temporal_idx == 0 ||
235 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200236 ++state_.tl0_pic_idx;
237 }
philipel29d88462018-08-08 14:26:00 +0200238 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200239 }
240 }
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500241 if (rtp_video_header->codec == kVideoCodecH264) {
242 if (rtp_video_header->frame_marking.temporal_id != kNoTemporalIdx) {
243 if (rtp_video_header->frame_marking.temporal_id == 0) {
244 ++state_.tl0_pic_idx;
245 }
246 rtp_video_header->frame_marking.tl0_pic_idx = state_.tl0_pic_idx;
247 }
248 }
philipelbf2b6202018-08-27 14:33:18 +0200249 if (generic_picture_id_experiment_ &&
250 rtp_video_header->codec == kVideoCodecGeneric) {
Danil Chapovalovb6bf0b22020-01-28 18:36:57 +0100251 rtp_video_header->video_type_header.emplace<RTPVideoHeaderLegacyGeneric>()
252 .picture_id = state_.picture_id;
philipelbf2b6202018-08-27 14:33:18 +0200253 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200254}
philipelbf2b6202018-08-27 14:33:18 +0200255
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100256RTPVideoHeader::GenericDescriptorInfo
257RtpPayloadParams::GenericDescriptorFromFrameInfo(
258 const GenericFrameInfo& frame_info,
259 int64_t frame_id,
260 VideoFrameType frame_type) {
261 RTPVideoHeader::GenericDescriptorInfo generic;
262 generic.frame_id = frame_id;
263 generic.dependencies = dependencies_calculator_.FromBuffersUsage(
264 frame_type, frame_id, frame_info.encoder_buffers);
265 generic.spatial_index = frame_info.spatial_id;
266 generic.temporal_index = frame_info.temporal_id;
267 generic.decode_target_indications = frame_info.decode_target_indications;
268 generic.discardable =
269 absl::c_linear_search(frame_info.decode_target_indications,
270 DecodeTargetIndication::kDiscardable);
271 return generic;
272}
273
Elad Alonf5b216a2019-01-28 14:25:17 +0100274void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info,
275 int64_t frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200276 bool is_keyframe,
277 RTPVideoHeader* rtp_video_header) {
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100278 if (codec_specific_info && codec_specific_info->generic_frame_info &&
279 !codec_specific_info->generic_frame_info->encoder_buffers.empty()) {
280 rtp_video_header->generic =
281 GenericDescriptorFromFrameInfo(*codec_specific_info->generic_frame_info,
282 frame_id, rtp_video_header->frame_type);
283 return;
284 }
285
Elad Alonf5b216a2019-01-28 14:25:17 +0100286 switch (rtp_video_header->codec) {
287 case VideoCodecType::kVideoCodecGeneric:
philipel8aba8fe2019-06-13 15:13:16 +0200288 GenericToGeneric(frame_id, is_keyframe, rtp_video_header);
Elad Alonf5b216a2019-01-28 14:25:17 +0100289 return;
290 case VideoCodecType::kVideoCodecVP8:
291 if (codec_specific_info) {
292 Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id,
293 is_keyframe, rtp_video_header);
294 }
295 return;
296 case VideoCodecType::kVideoCodecVP9:
Danil Chapovalovdc368292019-11-26 14:48:20 +0100297 case VideoCodecType::kVideoCodecAV1:
298 // TODO(philipel): Implement VP9 and AV1 to generic descriptor.
Elad Alonf5b216a2019-01-28 14:25:17 +0100299 return;
300 case VideoCodecType::kVideoCodecH264:
philipel8aba8fe2019-06-13 15:13:16 +0200301 if (codec_specific_info) {
302 H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id,
303 is_keyframe, rtp_video_header);
304 }
305 return;
Elad Alonf5b216a2019-01-28 14:25:17 +0100306 case VideoCodecType::kVideoCodecMultiplex:
307 return;
philipelbf2b6202018-08-27 14:33:18 +0200308 }
Elad Alonf5b216a2019-01-28 14:25:17 +0100309 RTC_NOTREACHED() << "Unsupported codec.";
philipelbf2b6202018-08-27 14:33:18 +0200310}
311
philipel8aba8fe2019-06-13 15:13:16 +0200312void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id,
313 bool is_keyframe,
314 RTPVideoHeader* rtp_video_header) {
315 RTPVideoHeader::GenericDescriptorInfo& generic =
316 rtp_video_header->generic.emplace();
317
318 generic.frame_id = shared_frame_id;
319
320 if (is_keyframe) {
321 last_shared_frame_id_[0].fill(-1);
322 } else {
323 int64_t frame_id = last_shared_frame_id_[0][0];
324 RTC_DCHECK_NE(frame_id, -1);
325 RTC_DCHECK_LT(frame_id, shared_frame_id);
326 generic.dependencies.push_back(frame_id);
327 }
328
329 last_shared_frame_id_[0][0] = shared_frame_id;
330}
331
332void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info,
333 int64_t shared_frame_id,
334 bool is_keyframe,
335 RTPVideoHeader* rtp_video_header) {
336 const int temporal_index =
337 h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0;
338
339 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) {
340 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
341 "used with generic frame descriptor.";
342 return;
343 }
344
345 RTPVideoHeader::GenericDescriptorInfo& generic =
346 rtp_video_header->generic.emplace();
347
348 generic.frame_id = shared_frame_id;
349 generic.temporal_index = temporal_index;
350
351 if (is_keyframe) {
352 RTC_DCHECK_EQ(temporal_index, 0);
353 last_shared_frame_id_[/*spatial index*/ 0].fill(-1);
354 last_shared_frame_id_[/*spatial index*/ 0][temporal_index] =
355 shared_frame_id;
356 return;
357 }
358
359 if (h264_info.base_layer_sync) {
360 int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0];
361
362 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
363 if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) {
364 last_shared_frame_id_[/*spatial index*/ 0][i] = -1;
365 }
366 }
367
368 RTC_DCHECK_GE(tl0_frame_id, 0);
369 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
370 generic.dependencies.push_back(tl0_frame_id);
371 } else {
372 for (int i = 0; i <= temporal_index; ++i) {
373 int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i];
374
375 if (frame_id != -1) {
376 RTC_DCHECK_LT(frame_id, shared_frame_id);
377 generic.dependencies.push_back(frame_id);
378 }
379 }
380 }
381
382 last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id;
383}
384
Elad Alonf5b216a2019-01-28 14:25:17 +0100385void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info,
386 int64_t shared_frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200387 bool is_keyframe,
388 RTPVideoHeader* rtp_video_header) {
389 const auto& vp8_header =
390 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
391 const int spatial_index = 0;
392 const int temporal_index =
393 vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0;
394
395 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers ||
396 spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) {
397 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
398 "used with generic frame descriptor.";
399 return;
400 }
401
402 RTPVideoHeader::GenericDescriptorInfo& generic =
403 rtp_video_header->generic.emplace();
404
405 generic.frame_id = shared_frame_id;
406 generic.spatial_index = spatial_index;
407 generic.temporal_index = temporal_index;
408
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000409 if (vp8_info.useExplicitDependencies) {
410 SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe,
411 vp8_header.layerSync, &generic);
412 } else {
413 SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe,
414 spatial_index, temporal_index,
415 vp8_header.layerSync, &generic);
416 }
417}
418
419void RtpPayloadParams::SetDependenciesVp8Deprecated(
420 const CodecSpecificInfoVP8& vp8_info,
421 int64_t shared_frame_id,
422 bool is_keyframe,
423 int spatial_index,
424 int temporal_index,
425 bool layer_sync,
426 RTPVideoHeader::GenericDescriptorInfo* generic) {
427 RTC_DCHECK(!vp8_info.useExplicitDependencies);
428 RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value());
429 new_version_used_ = false;
430
431 if (is_keyframe) {
432 RTC_DCHECK_EQ(temporal_index, 0);
433 last_shared_frame_id_[spatial_index].fill(-1);
434 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
435 return;
436 }
437
438 if (layer_sync) {
439 int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0];
440
441 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
442 if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) {
443 last_shared_frame_id_[spatial_index][i] = -1;
444 }
445 }
446
447 RTC_DCHECK_GE(tl0_frame_id, 0);
448 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
449 generic->dependencies.push_back(tl0_frame_id);
450 } else {
451 for (int i = 0; i <= temporal_index; ++i) {
452 int64_t frame_id = last_shared_frame_id_[spatial_index][i];
453
454 if (frame_id != -1) {
455 RTC_DCHECK_LT(frame_id, shared_frame_id);
456 generic->dependencies.push_back(frame_id);
457 }
458 }
459 }
460
461 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
462}
463
464void RtpPayloadParams::SetDependenciesVp8New(
465 const CodecSpecificInfoVP8& vp8_info,
466 int64_t shared_frame_id,
467 bool is_keyframe,
468 bool layer_sync,
469 RTPVideoHeader::GenericDescriptorInfo* generic) {
470 RTC_DCHECK(vp8_info.useExplicitDependencies);
471 RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value());
472 new_version_used_ = true;
473
Elad Alonf5b216a2019-01-28 14:25:17 +0100474 if (is_keyframe) {
475 RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u);
476 buffer_id_to_frame_id_.fill(shared_frame_id);
477 return;
478 }
479
480 constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount;
481
482 RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u);
483 RTC_DCHECK_LE(vp8_info.referencedBuffersCount,
484 arraysize(vp8_info.referencedBuffers));
485
486 for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) {
487 const size_t referenced_buffer = vp8_info.referencedBuffers[i];
488 RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8);
489 RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size());
490
491 const int64_t dependency_frame_id =
492 buffer_id_to_frame_id_[referenced_buffer];
493 RTC_DCHECK_GE(dependency_frame_id, 0);
494 RTC_DCHECK_LT(dependency_frame_id, shared_frame_id);
495
496 const bool is_new_dependency =
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000497 std::find(generic->dependencies.begin(), generic->dependencies.end(),
498 dependency_frame_id) == generic->dependencies.end();
Elad Alonf5b216a2019-01-28 14:25:17 +0100499 if (is_new_dependency) {
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000500 generic->dependencies.push_back(dependency_frame_id);
Elad Alonf5b216a2019-01-28 14:25:17 +0100501 }
502 }
503
504 RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8);
505 for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) {
506 const size_t updated_id = vp8_info.updatedBuffers[i];
507 buffer_id_to_frame_id_[updated_id] = shared_frame_id;
508 }
509
510 RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8);
511}
512
Stefan Holmerf7044682018-07-17 10:16:41 +0200513} // namespace webrtc