OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/video_coding/decoding_state.h" | 11 #include "webrtc/modules/video_coding/decoding_state.h" |
12 | 12 |
13 #include "webrtc/base/logging.h" | |
14 #include "webrtc/common_video/h264/h264_common.h" | |
15 #include "webrtc/modules/include/module_common_types.h" | 13 #include "webrtc/modules/include/module_common_types.h" |
16 #include "webrtc/modules/video_coding/frame_buffer.h" | 14 #include "webrtc/modules/video_coding/frame_buffer.h" |
17 #include "webrtc/modules/video_coding/jitter_buffer_common.h" | 15 #include "webrtc/modules/video_coding/jitter_buffer_common.h" |
18 #include "webrtc/modules/video_coding/packet.h" | 16 #include "webrtc/modules/video_coding/packet.h" |
19 | 17 |
20 namespace webrtc { | 18 namespace webrtc { |
21 | 19 |
22 VCMDecodingState::VCMDecodingState() | 20 VCMDecodingState::VCMDecodingState() |
23 : sequence_num_(0), | 21 : sequence_num_(0), |
24 time_stamp_(0), | 22 time_stamp_(0), |
(...skipping 10 matching lines...) Expand all Loading... |
35 void VCMDecodingState::Reset() { | 33 void VCMDecodingState::Reset() { |
36 // TODO(mikhal): Verify - not always would want to reset the sync | 34 // TODO(mikhal): Verify - not always would want to reset the sync |
37 sequence_num_ = 0; | 35 sequence_num_ = 0; |
38 time_stamp_ = 0; | 36 time_stamp_ = 0; |
39 picture_id_ = kNoPictureId; | 37 picture_id_ = kNoPictureId; |
40 temporal_id_ = kNoTemporalIdx; | 38 temporal_id_ = kNoTemporalIdx; |
41 tl0_pic_id_ = kNoTl0PicIdx; | 39 tl0_pic_id_ = kNoTl0PicIdx; |
42 full_sync_ = true; | 40 full_sync_ = true; |
43 in_initial_state_ = true; | 41 in_initial_state_ = true; |
44 memset(frame_decoded_, 0, sizeof(frame_decoded_)); | 42 memset(frame_decoded_, 0, sizeof(frame_decoded_)); |
45 received_sps_.clear(); | |
46 received_pps_.clear(); | |
47 } | 43 } |
48 | 44 |
49 uint32_t VCMDecodingState::time_stamp() const { | 45 uint32_t VCMDecodingState::time_stamp() const { |
50 return time_stamp_; | 46 return time_stamp_; |
51 } | 47 } |
52 | 48 |
53 uint16_t VCMDecodingState::sequence_num() const { | 49 uint16_t VCMDecodingState::sequence_num() const { |
54 return sequence_num_; | 50 return sequence_num_; |
55 } | 51 } |
56 | 52 |
(...skipping 14 matching lines...) Expand all Loading... |
71 void VCMDecodingState::SetState(const VCMFrameBuffer* frame) { | 67 void VCMDecodingState::SetState(const VCMFrameBuffer* frame) { |
72 assert(frame != NULL && frame->GetHighSeqNum() >= 0); | 68 assert(frame != NULL && frame->GetHighSeqNum() >= 0); |
73 if (!UsingFlexibleMode(frame)) | 69 if (!UsingFlexibleMode(frame)) |
74 UpdateSyncState(frame); | 70 UpdateSyncState(frame); |
75 sequence_num_ = static_cast<uint16_t>(frame->GetHighSeqNum()); | 71 sequence_num_ = static_cast<uint16_t>(frame->GetHighSeqNum()); |
76 time_stamp_ = frame->TimeStamp(); | 72 time_stamp_ = frame->TimeStamp(); |
77 picture_id_ = frame->PictureId(); | 73 picture_id_ = frame->PictureId(); |
78 temporal_id_ = frame->TemporalId(); | 74 temporal_id_ = frame->TemporalId(); |
79 tl0_pic_id_ = frame->Tl0PicId(); | 75 tl0_pic_id_ = frame->Tl0PicId(); |
80 | 76 |
81 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 | |
99 if (UsingFlexibleMode(frame)) { | 77 if (UsingFlexibleMode(frame)) { |
100 uint16_t frame_index = picture_id_ % kFrameDecodedLength; | 78 uint16_t frame_index = picture_id_ % kFrameDecodedLength; |
101 if (in_initial_state_) { | 79 if (in_initial_state_) { |
102 frame_decoded_cleared_to_ = frame_index; | 80 frame_decoded_cleared_to_ = frame_index; |
103 } else if (frame->FrameType() == kVideoFrameKey) { | 81 } else if (frame->FrameType() == kVideoFrameKey) { |
104 memset(frame_decoded_, 0, sizeof(frame_decoded_)); | 82 memset(frame_decoded_, 0, sizeof(frame_decoded_)); |
105 frame_decoded_cleared_to_ = frame_index; | 83 frame_decoded_cleared_to_ = frame_index; |
106 } else { | 84 } else { |
107 if (AheadOfFramesDecodedClearedTo(frame_index)) { | 85 if (AheadOfFramesDecodedClearedTo(frame_index)) { |
108 while (frame_decoded_cleared_to_ != frame_index) { | 86 while (frame_decoded_cleared_to_ != frame_index) { |
(...skipping 12 matching lines...) Expand all Loading... |
121 void VCMDecodingState::CopyFrom(const VCMDecodingState& state) { | 99 void VCMDecodingState::CopyFrom(const VCMDecodingState& state) { |
122 sequence_num_ = state.sequence_num_; | 100 sequence_num_ = state.sequence_num_; |
123 time_stamp_ = state.time_stamp_; | 101 time_stamp_ = state.time_stamp_; |
124 picture_id_ = state.picture_id_; | 102 picture_id_ = state.picture_id_; |
125 temporal_id_ = state.temporal_id_; | 103 temporal_id_ = state.temporal_id_; |
126 tl0_pic_id_ = state.tl0_pic_id_; | 104 tl0_pic_id_ = state.tl0_pic_id_; |
127 full_sync_ = state.full_sync_; | 105 full_sync_ = state.full_sync_; |
128 in_initial_state_ = state.in_initial_state_; | 106 in_initial_state_ = state.in_initial_state_; |
129 frame_decoded_cleared_to_ = state.frame_decoded_cleared_to_; | 107 frame_decoded_cleared_to_ = state.frame_decoded_cleared_to_; |
130 memcpy(frame_decoded_, state.frame_decoded_, sizeof(frame_decoded_)); | 108 memcpy(frame_decoded_, state.frame_decoded_, sizeof(frame_decoded_)); |
131 received_sps_ = state.received_sps_; | |
132 received_pps_ = state.received_pps_; | |
133 } | 109 } |
134 | 110 |
135 bool VCMDecodingState::UpdateEmptyFrame(const VCMFrameBuffer* frame) { | 111 bool VCMDecodingState::UpdateEmptyFrame(const VCMFrameBuffer* frame) { |
136 bool empty_packet = frame->GetHighSeqNum() == frame->GetLowSeqNum(); | 112 bool empty_packet = frame->GetHighSeqNum() == frame->GetLowSeqNum(); |
137 if (in_initial_state_ && empty_packet) { | 113 if (in_initial_state_ && empty_packet) { |
138 // Drop empty packets as long as we are in the initial state. | 114 // Drop empty packets as long as we are in the initial state. |
139 return true; | 115 return true; |
140 } | 116 } |
141 if ((empty_packet && ContinuousSeqNum(frame->GetHighSeqNum())) || | 117 if ((empty_packet && ContinuousSeqNum(frame->GetHighSeqNum())) || |
142 ContinuousFrame(frame)) { | 118 ContinuousFrame(frame)) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 // Check continuity based on the following hierarchy: | 176 // Check continuity based on the following hierarchy: |
201 // - Temporal layers (stop here if out of sync). | 177 // - Temporal layers (stop here if out of sync). |
202 // - Picture Id when available. | 178 // - Picture Id when available. |
203 // - Sequence numbers. | 179 // - Sequence numbers. |
204 // Return true when in initial state. | 180 // Return true when in initial state. |
205 // Note that when a method is not applicable it will return false. | 181 // Note that when a method is not applicable it will return false. |
206 assert(frame != NULL); | 182 assert(frame != NULL); |
207 // A key frame is always considered continuous as it doesn't refer to any | 183 // 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 | 184 // frames and therefore won't introduce any errors even if prior frames are |
209 // missing. | 185 // missing. |
210 if (frame->FrameType() == kVideoFrameKey && | 186 if (frame->FrameType() == kVideoFrameKey) |
211 HaveSpsAndPps(frame->GetNaluInfos())) { | |
212 return true; | 187 return true; |
213 } | |
214 // When in the initial state we always require a key frame to start decoding. | 188 // When in the initial state we always require a key frame to start decoding. |
215 if (in_initial_state_) | 189 if (in_initial_state_) |
216 return false; | 190 return false; |
217 if (ContinuousLayer(frame->TemporalId(), frame->Tl0PicId())) | 191 if (ContinuousLayer(frame->TemporalId(), frame->Tl0PicId())) |
218 return true; | 192 return true; |
219 // tl0picId is either not used, or should remain unchanged. | 193 // tl0picId is either not used, or should remain unchanged. |
220 if (frame->Tl0PicId() != tl0_pic_id_) | 194 if (frame->Tl0PicId() != tl0_pic_id_) |
221 return false; | 195 return false; |
222 // Base layers are not continuous or temporal layers are inactive. | 196 // Base layers are not continuous or temporal layers are inactive. |
223 // In the presence of temporal layers, check for Picture ID/sequence number | 197 // In the presence of temporal layers, check for Picture ID/sequence number |
224 // continuity if sync can be restored by this frame. | 198 // continuity if sync can be restored by this frame. |
225 if (!full_sync_ && !frame->LayerSync()) | 199 if (!full_sync_ && !frame->LayerSync()) |
226 return false; | 200 return false; |
227 if (UsingPictureId(frame)) { | 201 if (UsingPictureId(frame)) { |
228 if (UsingFlexibleMode(frame)) { | 202 if (UsingFlexibleMode(frame)) { |
229 return ContinuousFrameRefs(frame); | 203 return ContinuousFrameRefs(frame); |
230 } else { | 204 } else { |
231 return ContinuousPictureId(frame->PictureId()); | 205 return ContinuousPictureId(frame->PictureId()); |
232 } | 206 } |
233 } else { | 207 } else { |
234 return ContinuousSeqNum(static_cast<uint16_t>(frame->GetLowSeqNum())) && | 208 return ContinuousSeqNum(static_cast<uint16_t>(frame->GetLowSeqNum())); |
235 HaveSpsAndPps(frame->GetNaluInfos()); | |
236 } | 209 } |
237 } | 210 } |
238 | 211 |
239 bool VCMDecodingState::ContinuousPictureId(int picture_id) const { | 212 bool VCMDecodingState::ContinuousPictureId(int picture_id) const { |
240 int next_picture_id = picture_id_ + 1; | 213 int next_picture_id = picture_id_ + 1; |
241 if (picture_id < picture_id_) { | 214 if (picture_id < picture_id_) { |
242 // Wrap | 215 // Wrap |
243 if (picture_id_ >= 0x80) { | 216 if (picture_id_ >= 0x80) { |
244 // 15 bits used for picture id | 217 // 15 bits used for picture id |
245 return ((next_picture_id & 0x7FFF) == picture_id); | 218 return ((next_picture_id & 0x7FFF) == picture_id); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 // frame_decoded_cleared_to_. We just make the assumption | 275 // frame_decoded_cleared_to_. We just make the assumption |
303 // that we are not trying to reference back to a very old | 276 // that we are not trying to reference back to a very old |
304 // index, but instead are referencing a newer index. | 277 // index, but instead are referencing a newer index. |
305 uint16_t diff = | 278 uint16_t diff = |
306 index > frame_decoded_cleared_to_ | 279 index > frame_decoded_cleared_to_ |
307 ? kFrameDecodedLength - (index - frame_decoded_cleared_to_) | 280 ? kFrameDecodedLength - (index - frame_decoded_cleared_to_) |
308 : frame_decoded_cleared_to_ - index; | 281 : frame_decoded_cleared_to_ - index; |
309 return diff > kFrameDecodedLength / 2; | 282 return diff > kFrameDecodedLength / 2; |
310 } | 283 } |
311 | 284 |
312 bool 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 switch (nalu.type) { | |
317 case H264::NaluType::kPps: | |
318 if (nalu.pps_id < 0) { | |
319 LOG(LS_WARNING) << "Received pps without pps id."; | |
320 } else if (nalu.sps_id < 0) { | |
321 LOG(LS_WARNING) << "Received pps without sps id."; | |
322 } else { | |
323 new_pps[nalu.pps_id] = nalu.sps_id; | |
324 } | |
325 break; | |
326 case H264::NaluType::kSps: | |
327 if (nalu.sps_id < 0) { | |
328 LOG(LS_WARNING) << "Received sps without sps id."; | |
329 } else { | |
330 new_sps.insert(nalu.sps_id); | |
331 } | |
332 break; | |
333 default: { | |
334 int sps_needed = -1; | |
335 auto pps_it = new_pps.find(nalu.pps_id); | |
336 if (pps_it != new_pps.end()) { | |
337 sps_needed = pps_it->second; | |
338 } else { | |
339 auto pps_it2 = received_pps_.find(nalu.pps_id); | |
340 if (pps_it2 == received_pps_.end()) { | |
341 return false; | |
342 } | |
343 sps_needed = pps_it2->second; | |
344 } | |
345 if (new_sps.find(sps_needed) == new_sps.end() && | |
346 received_sps_.find(sps_needed) == received_sps_.end()) { | |
347 return false; | |
348 } | |
349 break; | |
350 } | |
351 } | |
352 } | |
353 return true; | |
354 } | |
355 | |
356 } // namespace webrtc | 285 } // namespace webrtc |
OLD | NEW |