blob: 229555287c826a6c8f9a9b99588e5cda53a5ddda [file] [log] [blame]
mikhal@webrtc.org832caca2011-12-13 21:15:05 +00001/*
2 * Copyright (c) 2011 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
Henrik Kjellander2557b862015-11-18 22:00:21 +010011#include "webrtc/modules/video_coding/decoding_state.h"
stefan@webrtc.org39670f62011-12-23 09:08:51 +000012
stefana669a3a2016-10-06 05:04:52 -070013#include "webrtc/base/logging.h"
14#include "webrtc/common_video/h264/h264_common.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010015#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010016#include "webrtc/modules/video_coding/frame_buffer.h"
17#include "webrtc/modules/video_coding/jitter_buffer_common.h"
18#include "webrtc/modules/video_coding/packet.h"
mikhal@webrtc.org832caca2011-12-13 21:15:05 +000019
20namespace webrtc {
21
22VCMDecodingState::VCMDecodingState()
23 : sequence_num_(0),
24 time_stamp_(0),
25 picture_id_(kNoPictureId),
26 temporal_id_(kNoTemporalIdx),
27 tl0_pic_id_(kNoTl0PicIdx),
28 full_sync_(true),
philipelcfc319b2015-11-10 07:17:23 -080029 in_initial_state_(true) {
30 memset(frame_decoded_, 0, sizeof(frame_decoded_));
31}
mikhal@webrtc.org832caca2011-12-13 21:15:05 +000032
33VCMDecodingState::~VCMDecodingState() {}
34
35void VCMDecodingState::Reset() {
36 // TODO(mikhal): Verify - not always would want to reset the sync
37 sequence_num_ = 0;
38 time_stamp_ = 0;
39 picture_id_ = kNoPictureId;
40 temporal_id_ = kNoTemporalIdx;
41 tl0_pic_id_ = kNoTl0PicIdx;
42 full_sync_ = true;
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000043 in_initial_state_ = true;
philipelcfc319b2015-11-10 07:17:23 -080044 memset(frame_decoded_, 0, sizeof(frame_decoded_));
stefana669a3a2016-10-06 05:04:52 -070045 received_sps_.clear();
46 received_pps_.clear();
mikhal@webrtc.org832caca2011-12-13 21:15:05 +000047}
48
49uint32_t VCMDecodingState::time_stamp() const {
50 return time_stamp_;
51}
52
53uint16_t VCMDecodingState::sequence_num() const {
54 return sequence_num_;
55}
56
57bool VCMDecodingState::IsOldFrame(const VCMFrameBuffer* frame) const {
58 assert(frame != NULL);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000059 if (in_initial_state_)
mikhal@webrtc.org832caca2011-12-13 21:15:05 +000060 return false;
stefan@webrtc.org7bc465b2013-04-11 17:48:02 +000061 return !IsNewerTimestamp(frame->TimeStamp(), time_stamp_);
mikhal@webrtc.org832caca2011-12-13 21:15:05 +000062}
63
64bool VCMDecodingState::IsOldPacket(const VCMPacket* packet) const {
65 assert(packet != NULL);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000066 if (in_initial_state_)
mikhal@webrtc.org832caca2011-12-13 21:15:05 +000067 return false;
stefan@webrtc.org7bc465b2013-04-11 17:48:02 +000068 return !IsNewerTimestamp(packet->timestamp, time_stamp_);
mikhal@webrtc.org832caca2011-12-13 21:15:05 +000069}
70
71void VCMDecodingState::SetState(const VCMFrameBuffer* frame) {
72 assert(frame != NULL && frame->GetHighSeqNum() >= 0);
philipelcfc319b2015-11-10 07:17:23 -080073 if (!UsingFlexibleMode(frame))
74 UpdateSyncState(frame);
mikhal@webrtc.org832caca2011-12-13 21:15:05 +000075 sequence_num_ = static_cast<uint16_t>(frame->GetHighSeqNum());
76 time_stamp_ = frame->TimeStamp();
77 picture_id_ = frame->PictureId();
78 temporal_id_ = frame->TemporalId();
79 tl0_pic_id_ = frame->Tl0PicId();
philipelcfc319b2015-11-10 07:17:23 -080080
stefana669a3a2016-10-06 05:04:52 -070081 for (const NaluInfo& nalu : frame->GetNaluInfos()) {
82 if (nalu.type == H264::NaluType::kPps) {
83 if (nalu.pps_id < 0) {
84 LOG(LS_WARNING) << "Received pps without pps id.";
85 } else if (nalu.sps_id < 0) {
86 LOG(LS_WARNING) << "Received pps without sps id.";
87 } else {
88 received_pps_[nalu.pps_id] = nalu.sps_id;
89 }
90 } else if (nalu.type == H264::NaluType::kSps) {
91 if (nalu.sps_id < 0) {
92 LOG(LS_WARNING) << "Received sps without sps id.";
93 } else {
94 received_sps_.insert(nalu.sps_id);
95 }
96 }
97 }
98
philipelcfc319b2015-11-10 07:17:23 -080099 if (UsingFlexibleMode(frame)) {
100 uint16_t frame_index = picture_id_ % kFrameDecodedLength;
101 if (in_initial_state_) {
102 frame_decoded_cleared_to_ = frame_index;
103 } else if (frame->FrameType() == kVideoFrameKey) {
104 memset(frame_decoded_, 0, sizeof(frame_decoded_));
105 frame_decoded_cleared_to_ = frame_index;
106 } else {
107 if (AheadOfFramesDecodedClearedTo(frame_index)) {
108 while (frame_decoded_cleared_to_ != frame_index) {
109 frame_decoded_cleared_to_ =
110 (frame_decoded_cleared_to_ + 1) % kFrameDecodedLength;
111 frame_decoded_[frame_decoded_cleared_to_] = false;
112 }
113 }
114 }
115 frame_decoded_[frame_index] = true;
116 }
117
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000118 in_initial_state_ = false;
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000119}
120
mikhal@webrtc.org381da4b2013-04-25 21:45:29 +0000121void VCMDecodingState::CopyFrom(const VCMDecodingState& state) {
122 sequence_num_ = state.sequence_num_;
123 time_stamp_ = state.time_stamp_;
124 picture_id_ = state.picture_id_;
125 temporal_id_ = state.temporal_id_;
126 tl0_pic_id_ = state.tl0_pic_id_;
127 full_sync_ = state.full_sync_;
128 in_initial_state_ = state.in_initial_state_;
philipelcfc319b2015-11-10 07:17:23 -0800129 frame_decoded_cleared_to_ = state.frame_decoded_cleared_to_;
130 memcpy(frame_decoded_, state.frame_decoded_, sizeof(frame_decoded_));
stefana669a3a2016-10-06 05:04:52 -0700131 received_sps_ = state.received_sps_;
132 received_pps_ = state.received_pps_;
mikhal@webrtc.org381da4b2013-04-25 21:45:29 +0000133}
134
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000135bool VCMDecodingState::UpdateEmptyFrame(const VCMFrameBuffer* frame) {
136 bool empty_packet = frame->GetHighSeqNum() == frame->GetLowSeqNum();
137 if (in_initial_state_ && empty_packet) {
138 // Drop empty packets as long as we are in the initial state.
139 return true;
stefan@webrtc.org7f3f8bc2013-05-27 07:02:45 +0000140 }
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000141 if ((empty_packet && ContinuousSeqNum(frame->GetHighSeqNum())) ||
142 ContinuousFrame(frame)) {
143 // Continuous empty packets or continuous frames can be dropped if we
144 // advance the sequence number.
145 sequence_num_ = frame->GetHighSeqNum();
146 time_stamp_ = frame->TimeStamp();
147 return true;
148 }
149 return false;
stefan@webrtc.orgbd941d32012-11-29 14:37:18 +0000150}
151
mikhal@webrtc.org77c425b2012-01-03 20:35:25 +0000152void VCMDecodingState::UpdateOldPacket(const VCMPacket* packet) {
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000153 assert(packet != NULL);
mikhal@webrtc.org77c425b2012-01-03 20:35:25 +0000154 if (packet->timestamp == time_stamp_) {
155 // Late packet belonging to the last decoded frame - make sure we update the
156 // last decoded sequence number.
stefan@webrtc.org7bc465b2013-04-11 17:48:02 +0000157 sequence_num_ = LatestSequenceNumber(packet->seqNum, sequence_num_);
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000158 }
159}
160
161void VCMDecodingState::SetSeqNum(uint16_t new_seq_num) {
162 sequence_num_ = new_seq_num;
163}
164
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000165bool VCMDecodingState::in_initial_state() const {
166 return in_initial_state_;
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000167}
168
169bool VCMDecodingState::full_sync() const {
170 return full_sync_;
171}
172
173void VCMDecodingState::UpdateSyncState(const VCMFrameBuffer* frame) {
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000174 if (in_initial_state_)
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000175 return;
176 if (frame->TemporalId() == kNoTemporalIdx ||
177 frame->Tl0PicId() == kNoTl0PicIdx) {
178 full_sync_ = true;
mikhal@webrtc.org884d8e72011-12-19 18:53:05 +0000179 } else if (frame->FrameType() == kVideoFrameKey || frame->LayerSync()) {
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000180 full_sync_ = true;
181 } else if (full_sync_) {
182 // Verify that we are still in sync.
183 // Sync will be broken if continuity is true for layers but not for the
184 // other methods (PictureId and SeqNum).
stefan@webrtc.orge72e9ee2012-09-19 11:08:05 +0000185 if (UsingPictureId(frame)) {
mikhal@webrtc.org0aeb22e2013-10-28 22:26:14 +0000186 // First check for a valid tl0PicId.
187 if (frame->Tl0PicId() - tl0_pic_id_ > 1) {
188 full_sync_ = false;
189 } else {
190 full_sync_ = ContinuousPictureId(frame->PictureId());
191 }
stefan@webrtc.orge72e9ee2012-09-19 11:08:05 +0000192 } else {
philipel9d3ab612015-12-21 04:12:39 -0800193 full_sync_ =
194 ContinuousSeqNum(static_cast<uint16_t>(frame->GetLowSeqNum()));
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000195 }
196 }
197}
198
199bool VCMDecodingState::ContinuousFrame(const VCMFrameBuffer* frame) const {
200 // Check continuity based on the following hierarchy:
201 // - Temporal layers (stop here if out of sync).
202 // - Picture Id when available.
203 // - Sequence numbers.
204 // Return true when in initial state.
205 // Note that when a method is not applicable it will return false.
206 assert(frame != NULL);
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000207 // A key frame is always considered continuous as it doesn't refer to any
208 // frames and therefore won't introduce any errors even if prior frames are
209 // missing.
stefana669a3a2016-10-06 05:04:52 -0700210 if (frame->FrameType() == kVideoFrameKey &&
211 HaveSpsAndPps(frame->GetNaluInfos())) {
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000212 return true;
stefana669a3a2016-10-06 05:04:52 -0700213 }
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000214 // When in the initial state we always require a key frame to start decoding.
215 if (in_initial_state_)
mikhal@webrtc.org8392cd92013-04-25 21:30:50 +0000216 return false;
mikhal@webrtc.org0aeb22e2013-10-28 22:26:14 +0000217 if (ContinuousLayer(frame->TemporalId(), frame->Tl0PicId()))
218 return true;
219 // tl0picId is either not used, or should remain unchanged.
220 if (frame->Tl0PicId() != tl0_pic_id_)
221 return false;
222 // Base layers are not continuous or temporal layers are inactive.
223 // In the presence of temporal layers, check for Picture ID/sequence number
224 // continuity if sync can be restored by this frame.
225 if (!full_sync_ && !frame->LayerSync())
226 return false;
227 if (UsingPictureId(frame)) {
philipelcfc319b2015-11-10 07:17:23 -0800228 if (UsingFlexibleMode(frame)) {
229 return ContinuousFrameRefs(frame);
230 } else {
231 return ContinuousPictureId(frame->PictureId());
232 }
mikhal@webrtc.org0aeb22e2013-10-28 22:26:14 +0000233 } else {
stefana669a3a2016-10-06 05:04:52 -0700234 return ContinuousSeqNum(static_cast<uint16_t>(frame->GetLowSeqNum())) &&
235 HaveSpsAndPps(frame->GetNaluInfos());
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000236 }
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000237}
238
239bool VCMDecodingState::ContinuousPictureId(int picture_id) const {
stefan@webrtc.org39670f62011-12-23 09:08:51 +0000240 int next_picture_id = picture_id_ + 1;
241 if (picture_id < picture_id_) {
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000242 // Wrap
stefan@webrtc.org39670f62011-12-23 09:08:51 +0000243 if (picture_id_ >= 0x80) {
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000244 // 15 bits used for picture id
stefan@webrtc.org39670f62011-12-23 09:08:51 +0000245 return ((next_picture_id & 0x7FFF) == picture_id);
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000246 } else {
247 // 7 bits used for picture id
stefan@webrtc.org39670f62011-12-23 09:08:51 +0000248 return ((next_picture_id & 0x7F) == picture_id);
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000249 }
250 }
251 // No wrap
stefan@webrtc.org39670f62011-12-23 09:08:51 +0000252 return (next_picture_id == picture_id);
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000253}
254
255bool VCMDecodingState::ContinuousSeqNum(uint16_t seq_num) const {
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000256 return seq_num == static_cast<uint16_t>(sequence_num_ + 1);
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000257}
258
philipel9d3ab612015-12-21 04:12:39 -0800259bool VCMDecodingState::ContinuousLayer(int temporal_id, int tl0_pic_id) const {
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000260 // First, check if applicable.
261 if (temporal_id == kNoTemporalIdx || tl0_pic_id == kNoTl0PicIdx)
262 return false;
263 // If this is the first frame to use temporal layers, make sure we start
264 // from base.
265 else if (tl0_pic_id_ == kNoTl0PicIdx && temporal_id_ == kNoTemporalIdx &&
266 temporal_id == 0)
267 return true;
268
mikhal@webrtc.org884d8e72011-12-19 18:53:05 +0000269 // Current implementation: Look for base layer continuity.
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000270 if (temporal_id != 0)
271 return false;
stefan@webrtc.org39670f62011-12-23 09:08:51 +0000272 return (static_cast<uint8_t>(tl0_pic_id_ + 1) == tl0_pic_id);
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000273}
274
philipelcfc319b2015-11-10 07:17:23 -0800275bool VCMDecodingState::ContinuousFrameRefs(const VCMFrameBuffer* frame) const {
276 uint8_t num_refs = frame->CodecSpecific()->codecSpecific.VP9.num_ref_pics;
277 for (uint8_t r = 0; r < num_refs; ++r) {
278 uint16_t frame_ref = frame->PictureId() -
279 frame->CodecSpecific()->codecSpecific.VP9.p_diff[r];
280 uint16_t frame_index = frame_ref % kFrameDecodedLength;
281 if (AheadOfFramesDecodedClearedTo(frame_index) ||
282 !frame_decoded_[frame_index]) {
283 return false;
284 }
285 }
286 return true;
287}
288
stefan@webrtc.orge72e9ee2012-09-19 11:08:05 +0000289bool VCMDecodingState::UsingPictureId(const VCMFrameBuffer* frame) const {
290 return (frame->PictureId() != kNoPictureId && picture_id_ != kNoPictureId);
291}
292
philipelcfc319b2015-11-10 07:17:23 -0800293bool VCMDecodingState::UsingFlexibleMode(const VCMFrameBuffer* frame) const {
294 return frame->CodecSpecific()->codecType == kVideoCodecVP9 &&
295 frame->CodecSpecific()->codecSpecific.VP9.flexible_mode;
296}
297
298// TODO(philipel): change how check work, this check practially
299// limits the max p_diff to 64.
300bool VCMDecodingState::AheadOfFramesDecodedClearedTo(uint16_t index) const {
301 // No way of knowing for sure if we are actually ahead of
302 // frame_decoded_cleared_to_. We just make the assumption
303 // that we are not trying to reference back to a very old
304 // index, but instead are referencing a newer index.
305 uint16_t diff =
306 index > frame_decoded_cleared_to_
307 ? kFrameDecodedLength - (index - frame_decoded_cleared_to_)
308 : frame_decoded_cleared_to_ - index;
309 return diff > kFrameDecodedLength / 2;
310}
311
stefana669a3a2016-10-06 05:04:52 -0700312bool VCMDecodingState::HaveSpsAndPps(const std::vector<NaluInfo>& nalus) const {
313 std::set<int> new_sps;
314 std::map<int, int> new_pps;
315 for (const NaluInfo& nalu : nalus) {
316 // Check if this nalu actually contains sps/pps information or dependencies.
317 if (nalu.sps_id == -1 && nalu.pps_id == -1)
318 continue;
319 switch (nalu.type) {
320 case H264::NaluType::kPps:
321 if (nalu.pps_id < 0) {
322 LOG(LS_WARNING) << "Received pps without pps id.";
323 } else if (nalu.sps_id < 0) {
324 LOG(LS_WARNING) << "Received pps without sps id.";
325 } else {
326 new_pps[nalu.pps_id] = nalu.sps_id;
327 }
328 break;
329 case H264::NaluType::kSps:
330 if (nalu.sps_id < 0) {
331 LOG(LS_WARNING) << "Received sps without sps id.";
332 } else {
333 new_sps.insert(nalu.sps_id);
334 }
335 break;
336 default: {
337 int needed_sps = -1;
338 auto pps_it = new_pps.find(nalu.pps_id);
339 if (pps_it != new_pps.end()) {
340 needed_sps = pps_it->second;
341 } else {
342 auto pps_it2 = received_pps_.find(nalu.pps_id);
343 if (pps_it2 == received_pps_.end()) {
344 return false;
345 }
346 needed_sps = pps_it2->second;
347 }
348 if (new_sps.find(needed_sps) == new_sps.end() &&
349 received_sps_.find(needed_sps) == received_sps_.end()) {
350 return false;
351 }
352 break;
353 }
354 }
355 }
356 return true;
357}
358
mikhal@webrtc.org832caca2011-12-13 21:15:05 +0000359} // namespace webrtc