blob: 635ed52c1aa95cb676c4371a18d4d8951111aab8 [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"
Erik Språngcbc0cba2020-04-18 14:36:59 +020018#include "absl/strings/match.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"
Stefan Holmerf7044682018-07-17 10:16:41 +020031
32namespace webrtc {
33
34namespace {
35void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info,
Niels Möllerd3b8c632018-08-27 15:33:42 +020036 absl::optional<int> spatial_index,
Stefan Holmerf7044682018-07-17 10:16:41 +020037 RTPVideoHeader* rtp) {
38 rtp->codec = info.codecType;
39 switch (info.codecType) {
40 case kVideoCodecVP8: {
Philip Eliassond52a1a62018-09-07 13:03:55 +000041 auto& vp8_header = rtp->video_type_header.emplace<RTPVideoHeaderVP8>();
42 vp8_header.InitRTPVideoHeaderVP8();
43 vp8_header.nonReference = info.codecSpecific.VP8.nonReference;
44 vp8_header.temporalIdx = info.codecSpecific.VP8.temporalIdx;
45 vp8_header.layerSync = info.codecSpecific.VP8.layerSync;
46 vp8_header.keyIdx = info.codecSpecific.VP8.keyIdx;
Niels Möllerd3b8c632018-08-27 15:33:42 +020047 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020048 return;
49 }
50 case kVideoCodecVP9: {
philipel29d88462018-08-08 14:26:00 +020051 auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>();
52 vp9_header.InitRTPVideoHeaderVP9();
53 vp9_header.inter_pic_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020054 info.codecSpecific.VP9.inter_pic_predicted;
philipel29d88462018-08-08 14:26:00 +020055 vp9_header.flexible_mode = info.codecSpecific.VP9.flexible_mode;
56 vp9_header.ss_data_available = info.codecSpecific.VP9.ss_data_available;
57 vp9_header.non_ref_for_inter_layer_pred =
Stefan Holmerf7044682018-07-17 10:16:41 +020058 info.codecSpecific.VP9.non_ref_for_inter_layer_pred;
philipel29d88462018-08-08 14:26:00 +020059 vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx;
philipel29d88462018-08-08 14:26:00 +020060 vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch;
61 vp9_header.inter_layer_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020062 info.codecSpecific.VP9.inter_layer_predicted;
philipel29d88462018-08-08 14:26:00 +020063 vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx;
64 vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers;
Ilya Nikolaevskiyf5d87782020-02-04 10:06:33 +000065 vp9_header.first_active_layer = info.codecSpecific.VP9.first_active_layer;
Niels Möllerd3b8c632018-08-27 15:33:42 +020066 if (vp9_header.num_spatial_layers > 1) {
67 vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx);
68 } else {
69 vp9_header.spatial_idx = kNoSpatialIdx;
70 }
Stefan Holmerf7044682018-07-17 10:16:41 +020071 if (info.codecSpecific.VP9.ss_data_available) {
philipel29d88462018-08-08 14:26:00 +020072 vp9_header.spatial_layer_resolution_present =
Stefan Holmerf7044682018-07-17 10:16:41 +020073 info.codecSpecific.VP9.spatial_layer_resolution_present;
74 if (info.codecSpecific.VP9.spatial_layer_resolution_present) {
75 for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers;
76 ++i) {
philipel29d88462018-08-08 14:26:00 +020077 vp9_header.width[i] = info.codecSpecific.VP9.width[i];
78 vp9_header.height[i] = info.codecSpecific.VP9.height[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020079 }
80 }
philipel29d88462018-08-08 14:26:00 +020081 vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof);
Stefan Holmerf7044682018-07-17 10:16:41 +020082 }
83
philipel29d88462018-08-08 14:26:00 +020084 vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics;
Stefan Holmerf7044682018-07-17 10:16:41 +020085 for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) {
philipel29d88462018-08-08 14:26:00 +020086 vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020087 }
philipel29d88462018-08-08 14:26:00 +020088 vp9_header.end_of_picture = info.codecSpecific.VP9.end_of_picture;
Stefan Holmerf7044682018-07-17 10:16:41 +020089 return;
90 }
91 case kVideoCodecH264: {
philipel7d745e52018-08-02 14:03:53 +020092 auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>();
93 h264_header.packetization_mode =
Stefan Holmerf7044682018-07-17 10:16:41 +020094 info.codecSpecific.H264.packetization_mode;
Niels Möllerd3b8c632018-08-27 15:33:42 +020095 rtp->simulcastIdx = spatial_index.value_or(0);
Johnny Lee1a1c52b2019-02-08 14:25:40 -050096 rtp->frame_marking.temporal_id = kNoTemporalIdx;
97 if (info.codecSpecific.H264.temporal_idx != kNoTemporalIdx) {
98 rtp->frame_marking.temporal_id = info.codecSpecific.H264.temporal_idx;
99 rtp->frame_marking.layer_id = 0;
100 rtp->frame_marking.independent_frame =
101 info.codecSpecific.H264.idr_frame;
102 rtp->frame_marking.base_layer_sync =
103 info.codecSpecific.H264.base_layer_sync;
104 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200105 return;
106 }
107 case kVideoCodecMultiplex:
108 case kVideoCodecGeneric:
109 rtp->codec = kVideoCodecGeneric;
Niels Möllerd3b8c632018-08-27 15:33:42 +0200110 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +0200111 return;
112 default:
113 return;
114 }
115}
116
117void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) {
118 if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid ||
119 image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) {
120 timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid;
121 return;
122 }
123
124 timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs(
125 image.capture_time_ms_, image.timing_.encode_start_ms);
126 timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs(
127 image.capture_time_ms_, image.timing_.encode_finish_ms);
128 timing->packetization_finish_delta_ms = 0;
129 timing->pacer_exit_delta_ms = 0;
130 timing->network_timestamp_delta_ms = 0;
131 timing->network2_timestamp_delta_ms = 0;
132 timing->flags = image.timing_.flags;
133}
134} // namespace
135
136RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc,
Erik Språngcbc0cba2020-04-18 14:36:59 +0200137 const RtpPayloadState* state,
138 const WebRtcKeyValueConfig& trials)
philipelbf2b6202018-08-27 14:33:18 +0200139 : ssrc_(ssrc),
140 generic_picture_id_experiment_(
Erik Språngcbc0cba2020-04-18 14:36:59 +0200141 absl::StartsWith(trials.Lookup("WebRTC-GenericPictureId"),
142 "Enabled")),
philipel569397f2018-09-26 12:25:31 +0200143 generic_descriptor_experiment_(
Erik Språngcbc0cba2020-04-18 14:36:59 +0200144 !absl::StartsWith(trials.Lookup("WebRTC-GenericDescriptor"),
145 "Disabled")) {
philipelbf2b6202018-08-27 14:33:18 +0200146 for (auto& spatial_layer : last_shared_frame_id_)
147 spatial_layer.fill(-1);
148
Elad Alonf5b216a2019-01-28 14:25:17 +0100149 buffer_id_to_frame_id_.fill(-1);
150
Stefan Holmerf7044682018-07-17 10:16:41 +0200151 Random random(rtc::TimeMicros());
152 state_.picture_id =
153 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
154 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
155}
philipelbf2b6202018-08-27 14:33:18 +0200156
157RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
158
Stefan Holmerf7044682018-07-17 10:16:41 +0200159RtpPayloadParams::~RtpPayloadParams() {}
160
161RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
162 const EncodedImage& image,
philipelbf2b6202018-08-27 14:33:18 +0200163 const CodecSpecificInfo* codec_specific_info,
164 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200165 RTPVideoHeader rtp_video_header;
166 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200167 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
168 &rtp_video_header);
Stefan Holmerf7044682018-07-17 10:16:41 +0200169 }
Danil Chapovalov51bf2002019-10-11 10:53:27 +0200170 rtp_video_header.frame_type = image._frameType,
Stefan Holmerf7044682018-07-17 10:16:41 +0200171 rtp_video_header.rotation = image.rotation_;
172 rtp_video_header.content_type = image.content_type_;
173 rtp_video_header.playout_delay = image.playout_delay_;
philipelfab91292018-10-17 14:36:08 +0200174 rtp_video_header.width = image._encodedWidth;
175 rtp_video_header.height = image._encodedHeight;
Johannes Krond0b69a82018-12-03 14:18:53 +0100176 rtp_video_header.color_space = image.ColorSpace()
177 ? absl::make_optional(*image.ColorSpace())
178 : absl::nullopt;
Stefan Holmerf7044682018-07-17 10:16:41 +0200179 SetVideoTiming(image, &rtp_video_header.video_timing);
180
Niels Möller8f7ce222019-03-21 15:43:58 +0100181 const bool is_keyframe = image._frameType == VideoFrameType::kVideoFrameKey;
Stefan Holmerf7044682018-07-17 10:16:41 +0200182 const bool first_frame_in_picture =
183 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
184 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
185 : true;
philipelbf2b6202018-08-27 14:33:18 +0200186
187 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
philipel569397f2018-09-26 12:25:31 +0200188
189 if (generic_descriptor_experiment_)
Elad Alonf5b216a2019-01-28 14:25:17 +0100190 SetGeneric(codec_specific_info, shared_frame_id, is_keyframe,
191 &rtp_video_header);
philipelbf2b6202018-08-27 14:33:18 +0200192
Stefan Holmerf7044682018-07-17 10:16:41 +0200193 return rtp_video_header;
194}
195
196uint32_t RtpPayloadParams::ssrc() const {
197 return ssrc_;
198}
199
200RtpPayloadState RtpPayloadParams::state() const {
201 return state_;
202}
203
philipelbf2b6202018-08-27 14:33:18 +0200204void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
205 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200206 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
207 if (first_frame_in_picture) {
208 state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
209 }
210 if (rtp_video_header->codec == kVideoCodecVP8) {
Philip Eliassond52a1a62018-09-07 13:03:55 +0000211 auto& vp8_header =
212 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
213 vp8_header.pictureId = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200214
Philip Eliassond52a1a62018-09-07 13:03:55 +0000215 if (vp8_header.temporalIdx != kNoTemporalIdx) {
216 if (vp8_header.temporalIdx == 0) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200217 ++state_.tl0_pic_idx;
218 }
Philip Eliassond52a1a62018-09-07 13:03:55 +0000219 vp8_header.tl0PicIdx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200220 }
221 }
222 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 14:26:00 +0200223 auto& vp9_header =
224 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
225 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200226
227 // Note that in the case that we have no temporal layers but we do have
228 // spatial layers, packets will carry layering info with a temporal_idx of
229 // zero, and we then have to set and increment tl0_pic_idx.
philipel29d88462018-08-08 14:26:00 +0200230 if (vp9_header.temporal_idx != kNoTemporalIdx ||
231 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200232 if (first_frame_in_picture &&
philipel29d88462018-08-08 14:26:00 +0200233 (vp9_header.temporal_idx == 0 ||
234 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200235 ++state_.tl0_pic_idx;
236 }
philipel29d88462018-08-08 14:26:00 +0200237 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200238 }
239 }
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500240 if (rtp_video_header->codec == kVideoCodecH264) {
241 if (rtp_video_header->frame_marking.temporal_id != kNoTemporalIdx) {
242 if (rtp_video_header->frame_marking.temporal_id == 0) {
243 ++state_.tl0_pic_idx;
244 }
245 rtp_video_header->frame_marking.tl0_pic_idx = state_.tl0_pic_idx;
246 }
247 }
philipelbf2b6202018-08-27 14:33:18 +0200248 if (generic_picture_id_experiment_ &&
249 rtp_video_header->codec == kVideoCodecGeneric) {
Danil Chapovalovb6bf0b22020-01-28 18:36:57 +0100250 rtp_video_header->video_type_header.emplace<RTPVideoHeaderLegacyGeneric>()
251 .picture_id = state_.picture_id;
philipelbf2b6202018-08-27 14:33:18 +0200252 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200253}
philipelbf2b6202018-08-27 14:33:18 +0200254
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100255RTPVideoHeader::GenericDescriptorInfo
256RtpPayloadParams::GenericDescriptorFromFrameInfo(
257 const GenericFrameInfo& frame_info,
258 int64_t frame_id,
259 VideoFrameType frame_type) {
260 RTPVideoHeader::GenericDescriptorInfo generic;
261 generic.frame_id = frame_id;
262 generic.dependencies = dependencies_calculator_.FromBuffersUsage(
263 frame_type, frame_id, frame_info.encoder_buffers);
Danil Chapovalov4b860c12020-05-19 14:48:19 +0200264 generic.chain_diffs =
265 chains_calculator_.From(frame_id, frame_info.part_of_chain);
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100266 generic.spatial_index = frame_info.spatial_id;
267 generic.temporal_index = frame_info.temporal_id;
268 generic.decode_target_indications = frame_info.decode_target_indications;
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100269 return generic;
270}
271
Elad Alonf5b216a2019-01-28 14:25:17 +0100272void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info,
273 int64_t frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200274 bool is_keyframe,
275 RTPVideoHeader* rtp_video_header) {
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100276 if (codec_specific_info && codec_specific_info->generic_frame_info &&
277 !codec_specific_info->generic_frame_info->encoder_buffers.empty()) {
Danil Chapovalov4b860c12020-05-19 14:48:19 +0200278 if (is_keyframe) {
279 // Key frame resets all chains it is in.
280 chains_calculator_.Reset(
281 codec_specific_info->generic_frame_info->part_of_chain);
282 }
Danil Chapovalov02d71fb2020-02-10 16:22:57 +0100283 rtp_video_header->generic =
284 GenericDescriptorFromFrameInfo(*codec_specific_info->generic_frame_info,
285 frame_id, rtp_video_header->frame_type);
286 return;
287 }
288
Elad Alonf5b216a2019-01-28 14:25:17 +0100289 switch (rtp_video_header->codec) {
290 case VideoCodecType::kVideoCodecGeneric:
philipel8aba8fe2019-06-13 15:13:16 +0200291 GenericToGeneric(frame_id, is_keyframe, rtp_video_header);
Elad Alonf5b216a2019-01-28 14:25:17 +0100292 return;
293 case VideoCodecType::kVideoCodecVP8:
294 if (codec_specific_info) {
295 Vp8ToGeneric(codec_specific_info->codecSpecific.VP8, frame_id,
296 is_keyframe, rtp_video_header);
297 }
298 return;
299 case VideoCodecType::kVideoCodecVP9:
Danil Chapovalovdc368292019-11-26 14:48:20 +0100300 case VideoCodecType::kVideoCodecAV1:
301 // TODO(philipel): Implement VP9 and AV1 to generic descriptor.
Elad Alonf5b216a2019-01-28 14:25:17 +0100302 return;
303 case VideoCodecType::kVideoCodecH264:
philipel8aba8fe2019-06-13 15:13:16 +0200304 if (codec_specific_info) {
305 H264ToGeneric(codec_specific_info->codecSpecific.H264, frame_id,
306 is_keyframe, rtp_video_header);
307 }
308 return;
Elad Alonf5b216a2019-01-28 14:25:17 +0100309 case VideoCodecType::kVideoCodecMultiplex:
310 return;
philipelbf2b6202018-08-27 14:33:18 +0200311 }
Elad Alonf5b216a2019-01-28 14:25:17 +0100312 RTC_NOTREACHED() << "Unsupported codec.";
philipelbf2b6202018-08-27 14:33:18 +0200313}
314
philipel8aba8fe2019-06-13 15:13:16 +0200315void RtpPayloadParams::GenericToGeneric(int64_t shared_frame_id,
316 bool is_keyframe,
317 RTPVideoHeader* rtp_video_header) {
318 RTPVideoHeader::GenericDescriptorInfo& generic =
319 rtp_video_header->generic.emplace();
320
321 generic.frame_id = shared_frame_id;
322
323 if (is_keyframe) {
324 last_shared_frame_id_[0].fill(-1);
325 } else {
326 int64_t frame_id = last_shared_frame_id_[0][0];
327 RTC_DCHECK_NE(frame_id, -1);
328 RTC_DCHECK_LT(frame_id, shared_frame_id);
329 generic.dependencies.push_back(frame_id);
330 }
331
332 last_shared_frame_id_[0][0] = shared_frame_id;
333}
334
335void RtpPayloadParams::H264ToGeneric(const CodecSpecificInfoH264& h264_info,
336 int64_t shared_frame_id,
337 bool is_keyframe,
338 RTPVideoHeader* rtp_video_header) {
339 const int temporal_index =
340 h264_info.temporal_idx != kNoTemporalIdx ? h264_info.temporal_idx : 0;
341
342 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers) {
343 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
344 "used with generic frame descriptor.";
345 return;
346 }
347
348 RTPVideoHeader::GenericDescriptorInfo& generic =
349 rtp_video_header->generic.emplace();
350
351 generic.frame_id = shared_frame_id;
352 generic.temporal_index = temporal_index;
353
354 if (is_keyframe) {
355 RTC_DCHECK_EQ(temporal_index, 0);
356 last_shared_frame_id_[/*spatial index*/ 0].fill(-1);
357 last_shared_frame_id_[/*spatial index*/ 0][temporal_index] =
358 shared_frame_id;
359 return;
360 }
361
362 if (h264_info.base_layer_sync) {
363 int64_t tl0_frame_id = last_shared_frame_id_[/*spatial index*/ 0][0];
364
365 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
366 if (last_shared_frame_id_[/*spatial index*/ 0][i] < tl0_frame_id) {
367 last_shared_frame_id_[/*spatial index*/ 0][i] = -1;
368 }
369 }
370
371 RTC_DCHECK_GE(tl0_frame_id, 0);
372 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
373 generic.dependencies.push_back(tl0_frame_id);
374 } else {
375 for (int i = 0; i <= temporal_index; ++i) {
376 int64_t frame_id = last_shared_frame_id_[/*spatial index*/ 0][i];
377
378 if (frame_id != -1) {
379 RTC_DCHECK_LT(frame_id, shared_frame_id);
380 generic.dependencies.push_back(frame_id);
381 }
382 }
383 }
384
385 last_shared_frame_id_[/*spatial_index*/ 0][temporal_index] = shared_frame_id;
386}
387
Elad Alonf5b216a2019-01-28 14:25:17 +0100388void RtpPayloadParams::Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info,
389 int64_t shared_frame_id,
philipelbf2b6202018-08-27 14:33:18 +0200390 bool is_keyframe,
391 RTPVideoHeader* rtp_video_header) {
392 const auto& vp8_header =
393 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
394 const int spatial_index = 0;
395 const int temporal_index =
396 vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0;
397
398 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers ||
399 spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) {
400 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
401 "used with generic frame descriptor.";
402 return;
403 }
404
405 RTPVideoHeader::GenericDescriptorInfo& generic =
406 rtp_video_header->generic.emplace();
407
408 generic.frame_id = shared_frame_id;
409 generic.spatial_index = spatial_index;
410 generic.temporal_index = temporal_index;
411
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000412 if (vp8_info.useExplicitDependencies) {
413 SetDependenciesVp8New(vp8_info, shared_frame_id, is_keyframe,
414 vp8_header.layerSync, &generic);
415 } else {
416 SetDependenciesVp8Deprecated(vp8_info, shared_frame_id, is_keyframe,
417 spatial_index, temporal_index,
418 vp8_header.layerSync, &generic);
419 }
420}
421
422void RtpPayloadParams::SetDependenciesVp8Deprecated(
423 const CodecSpecificInfoVP8& vp8_info,
424 int64_t shared_frame_id,
425 bool is_keyframe,
426 int spatial_index,
427 int temporal_index,
428 bool layer_sync,
429 RTPVideoHeader::GenericDescriptorInfo* generic) {
430 RTC_DCHECK(!vp8_info.useExplicitDependencies);
431 RTC_DCHECK(!new_version_used_.has_value() || !new_version_used_.value());
432 new_version_used_ = false;
433
434 if (is_keyframe) {
435 RTC_DCHECK_EQ(temporal_index, 0);
436 last_shared_frame_id_[spatial_index].fill(-1);
437 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
438 return;
439 }
440
441 if (layer_sync) {
442 int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0];
443
444 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
445 if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) {
446 last_shared_frame_id_[spatial_index][i] = -1;
447 }
448 }
449
450 RTC_DCHECK_GE(tl0_frame_id, 0);
451 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
452 generic->dependencies.push_back(tl0_frame_id);
453 } else {
454 for (int i = 0; i <= temporal_index; ++i) {
455 int64_t frame_id = last_shared_frame_id_[spatial_index][i];
456
457 if (frame_id != -1) {
458 RTC_DCHECK_LT(frame_id, shared_frame_id);
459 generic->dependencies.push_back(frame_id);
460 }
461 }
462 }
463
464 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
465}
466
467void RtpPayloadParams::SetDependenciesVp8New(
468 const CodecSpecificInfoVP8& vp8_info,
469 int64_t shared_frame_id,
470 bool is_keyframe,
471 bool layer_sync,
472 RTPVideoHeader::GenericDescriptorInfo* generic) {
473 RTC_DCHECK(vp8_info.useExplicitDependencies);
474 RTC_DCHECK(!new_version_used_.has_value() || new_version_used_.value());
475 new_version_used_ = true;
476
Elad Alonf5b216a2019-01-28 14:25:17 +0100477 if (is_keyframe) {
478 RTC_DCHECK_EQ(vp8_info.referencedBuffersCount, 0u);
479 buffer_id_to_frame_id_.fill(shared_frame_id);
480 return;
481 }
482
483 constexpr size_t kBuffersCountVp8 = CodecSpecificInfoVP8::kBuffersCount;
484
485 RTC_DCHECK_GT(vp8_info.referencedBuffersCount, 0u);
486 RTC_DCHECK_LE(vp8_info.referencedBuffersCount,
487 arraysize(vp8_info.referencedBuffers));
488
489 for (size_t i = 0; i < vp8_info.referencedBuffersCount; ++i) {
490 const size_t referenced_buffer = vp8_info.referencedBuffers[i];
491 RTC_DCHECK_LT(referenced_buffer, kBuffersCountVp8);
492 RTC_DCHECK_LT(referenced_buffer, buffer_id_to_frame_id_.size());
493
494 const int64_t dependency_frame_id =
495 buffer_id_to_frame_id_[referenced_buffer];
496 RTC_DCHECK_GE(dependency_frame_id, 0);
497 RTC_DCHECK_LT(dependency_frame_id, shared_frame_id);
498
499 const bool is_new_dependency =
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000500 std::find(generic->dependencies.begin(), generic->dependencies.end(),
501 dependency_frame_id) == generic->dependencies.end();
Elad Alonf5b216a2019-01-28 14:25:17 +0100502 if (is_new_dependency) {
Qingsi Wang1c1b99e2020-01-07 19:16:33 +0000503 generic->dependencies.push_back(dependency_frame_id);
Elad Alonf5b216a2019-01-28 14:25:17 +0100504 }
505 }
506
507 RTC_DCHECK_LE(vp8_info.updatedBuffersCount, kBuffersCountVp8);
508 for (size_t i = 0; i < vp8_info.updatedBuffersCount; ++i) {
509 const size_t updated_id = vp8_info.updatedBuffers[i];
510 buffer_id_to_frame_id_[updated_id] = shared_frame_id;
511 }
512
513 RTC_DCHECK_LE(buffer_id_to_frame_id_.size(), kBuffersCountVp8);
514}
515
Stefan Holmerf7044682018-07-17 10:16:41 +0200516} // namespace webrtc