Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(409)

Side by Side Diff: webrtc/modules/video_coding/decoding_state.cc

Issue 2341713002: Use sps and pps to determine decodability of H.264 frames. (Closed)
Patch Set: comments addressed. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
13 #include "webrtc/modules/include/module_common_types.h" 15 #include "webrtc/modules/include/module_common_types.h"
14 #include "webrtc/modules/video_coding/frame_buffer.h" 16 #include "webrtc/modules/video_coding/frame_buffer.h"
15 #include "webrtc/modules/video_coding/jitter_buffer_common.h" 17 #include "webrtc/modules/video_coding/jitter_buffer_common.h"
16 #include "webrtc/modules/video_coding/packet.h" 18 #include "webrtc/modules/video_coding/packet.h"
17 19
18 namespace webrtc { 20 namespace webrtc {
19 21
20 VCMDecodingState::VCMDecodingState() 22 VCMDecodingState::VCMDecodingState()
21 : sequence_num_(0), 23 : sequence_num_(0),
22 time_stamp_(0), 24 time_stamp_(0),
(...skipping 10 matching lines...) Expand all
33 void VCMDecodingState::Reset() { 35 void VCMDecodingState::Reset() {
34 // TODO(mikhal): Verify - not always would want to reset the sync 36 // TODO(mikhal): Verify - not always would want to reset the sync
35 sequence_num_ = 0; 37 sequence_num_ = 0;
36 time_stamp_ = 0; 38 time_stamp_ = 0;
37 picture_id_ = kNoPictureId; 39 picture_id_ = kNoPictureId;
38 temporal_id_ = kNoTemporalIdx; 40 temporal_id_ = kNoTemporalIdx;
39 tl0_pic_id_ = kNoTl0PicIdx; 41 tl0_pic_id_ = kNoTl0PicIdx;
40 full_sync_ = true; 42 full_sync_ = true;
41 in_initial_state_ = true; 43 in_initial_state_ = true;
42 memset(frame_decoded_, 0, sizeof(frame_decoded_)); 44 memset(frame_decoded_, 0, sizeof(frame_decoded_));
45 received_sps_.clear();
46 received_pps_.clear();
43 } 47 }
44 48
45 uint32_t VCMDecodingState::time_stamp() const { 49 uint32_t VCMDecodingState::time_stamp() const {
46 return time_stamp_; 50 return time_stamp_;
47 } 51 }
48 52
49 uint16_t VCMDecodingState::sequence_num() const { 53 uint16_t VCMDecodingState::sequence_num() const {
50 return sequence_num_; 54 return sequence_num_;
51 } 55 }
52 56
(...skipping 14 matching lines...) Expand all
67 void VCMDecodingState::SetState(const VCMFrameBuffer* frame) { 71 void VCMDecodingState::SetState(const VCMFrameBuffer* frame) {
68 assert(frame != NULL && frame->GetHighSeqNum() >= 0); 72 assert(frame != NULL && frame->GetHighSeqNum() >= 0);
69 if (!UsingFlexibleMode(frame)) 73 if (!UsingFlexibleMode(frame))
70 UpdateSyncState(frame); 74 UpdateSyncState(frame);
71 sequence_num_ = static_cast<uint16_t>(frame->GetHighSeqNum()); 75 sequence_num_ = static_cast<uint16_t>(frame->GetHighSeqNum());
72 time_stamp_ = frame->TimeStamp(); 76 time_stamp_ = frame->TimeStamp();
73 picture_id_ = frame->PictureId(); 77 picture_id_ = frame->PictureId();
74 temporal_id_ = frame->TemporalId(); 78 temporal_id_ = frame->TemporalId();
75 tl0_pic_id_ = frame->Tl0PicId(); 79 tl0_pic_id_ = frame->Tl0PicId();
76 80
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
77 if (UsingFlexibleMode(frame)) { 99 if (UsingFlexibleMode(frame)) {
78 uint16_t frame_index = picture_id_ % kFrameDecodedLength; 100 uint16_t frame_index = picture_id_ % kFrameDecodedLength;
79 if (in_initial_state_) { 101 if (in_initial_state_) {
80 frame_decoded_cleared_to_ = frame_index; 102 frame_decoded_cleared_to_ = frame_index;
81 } else if (frame->FrameType() == kVideoFrameKey) { 103 } else if (frame->FrameType() == kVideoFrameKey) {
82 memset(frame_decoded_, 0, sizeof(frame_decoded_)); 104 memset(frame_decoded_, 0, sizeof(frame_decoded_));
83 frame_decoded_cleared_to_ = frame_index; 105 frame_decoded_cleared_to_ = frame_index;
84 } else { 106 } else {
85 if (AheadOfFramesDecodedClearedTo(frame_index)) { 107 if (AheadOfFramesDecodedClearedTo(frame_index)) {
86 while (frame_decoded_cleared_to_ != frame_index) { 108 while (frame_decoded_cleared_to_ != frame_index) {
(...skipping 12 matching lines...) Expand all
99 void VCMDecodingState::CopyFrom(const VCMDecodingState& state) { 121 void VCMDecodingState::CopyFrom(const VCMDecodingState& state) {
100 sequence_num_ = state.sequence_num_; 122 sequence_num_ = state.sequence_num_;
101 time_stamp_ = state.time_stamp_; 123 time_stamp_ = state.time_stamp_;
102 picture_id_ = state.picture_id_; 124 picture_id_ = state.picture_id_;
103 temporal_id_ = state.temporal_id_; 125 temporal_id_ = state.temporal_id_;
104 tl0_pic_id_ = state.tl0_pic_id_; 126 tl0_pic_id_ = state.tl0_pic_id_;
105 full_sync_ = state.full_sync_; 127 full_sync_ = state.full_sync_;
106 in_initial_state_ = state.in_initial_state_; 128 in_initial_state_ = state.in_initial_state_;
107 frame_decoded_cleared_to_ = state.frame_decoded_cleared_to_; 129 frame_decoded_cleared_to_ = state.frame_decoded_cleared_to_;
108 memcpy(frame_decoded_, state.frame_decoded_, sizeof(frame_decoded_)); 130 memcpy(frame_decoded_, state.frame_decoded_, sizeof(frame_decoded_));
131 received_sps_ = state.received_sps_;
132 received_pps_ = state.received_pps_;
109 } 133 }
110 134
111 bool VCMDecodingState::UpdateEmptyFrame(const VCMFrameBuffer* frame) { 135 bool VCMDecodingState::UpdateEmptyFrame(const VCMFrameBuffer* frame) {
112 bool empty_packet = frame->GetHighSeqNum() == frame->GetLowSeqNum(); 136 bool empty_packet = frame->GetHighSeqNum() == frame->GetLowSeqNum();
113 if (in_initial_state_ && empty_packet) { 137 if (in_initial_state_ && empty_packet) {
114 // Drop empty packets as long as we are in the initial state. 138 // Drop empty packets as long as we are in the initial state.
115 return true; 139 return true;
116 } 140 }
117 if ((empty_packet && ContinuousSeqNum(frame->GetHighSeqNum())) || 141 if ((empty_packet && ContinuousSeqNum(frame->GetHighSeqNum())) ||
118 ContinuousFrame(frame)) { 142 ContinuousFrame(frame)) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 // Check continuity based on the following hierarchy: 200 // Check continuity based on the following hierarchy:
177 // - Temporal layers (stop here if out of sync). 201 // - Temporal layers (stop here if out of sync).
178 // - Picture Id when available. 202 // - Picture Id when available.
179 // - Sequence numbers. 203 // - Sequence numbers.
180 // Return true when in initial state. 204 // Return true when in initial state.
181 // Note that when a method is not applicable it will return false. 205 // Note that when a method is not applicable it will return false.
182 assert(frame != NULL); 206 assert(frame != NULL);
183 // A key frame is always considered continuous as it doesn't refer to any 207 // A key frame is always considered continuous as it doesn't refer to any
184 // frames and therefore won't introduce any errors even if prior frames are 208 // frames and therefore won't introduce any errors even if prior frames are
185 // missing. 209 // missing.
186 if (frame->FrameType() == kVideoFrameKey) 210 if (frame->FrameType() == kVideoFrameKey &&
211 HaveSpsAndPps(frame->GetNaluInfos())) {
187 return true; 212 return true;
213 }
188 // When in the initial state we always require a key frame to start decoding. 214 // When in the initial state we always require a key frame to start decoding.
189 if (in_initial_state_) 215 if (in_initial_state_)
190 return false; 216 return false;
191 if (ContinuousLayer(frame->TemporalId(), frame->Tl0PicId())) 217 if (ContinuousLayer(frame->TemporalId(), frame->Tl0PicId()))
192 return true; 218 return true;
193 // tl0picId is either not used, or should remain unchanged. 219 // tl0picId is either not used, or should remain unchanged.
194 if (frame->Tl0PicId() != tl0_pic_id_) 220 if (frame->Tl0PicId() != tl0_pic_id_)
195 return false; 221 return false;
196 // Base layers are not continuous or temporal layers are inactive. 222 // Base layers are not continuous or temporal layers are inactive.
197 // In the presence of temporal layers, check for Picture ID/sequence number 223 // In the presence of temporal layers, check for Picture ID/sequence number
198 // continuity if sync can be restored by this frame. 224 // continuity if sync can be restored by this frame.
199 if (!full_sync_ && !frame->LayerSync()) 225 if (!full_sync_ && !frame->LayerSync())
200 return false; 226 return false;
201 if (UsingPictureId(frame)) { 227 if (UsingPictureId(frame)) {
202 if (UsingFlexibleMode(frame)) { 228 if (UsingFlexibleMode(frame)) {
203 return ContinuousFrameRefs(frame); 229 return ContinuousFrameRefs(frame);
204 } else { 230 } else {
205 return ContinuousPictureId(frame->PictureId()); 231 return ContinuousPictureId(frame->PictureId());
206 } 232 }
207 } else { 233 } else {
208 return ContinuousSeqNum(static_cast<uint16_t>(frame->GetLowSeqNum())); 234 return ContinuousSeqNum(static_cast<uint16_t>(frame->GetLowSeqNum())) &&
235 HaveSpsAndPps(frame->GetNaluInfos());
209 } 236 }
210 } 237 }
211 238
212 bool VCMDecodingState::ContinuousPictureId(int picture_id) const { 239 bool VCMDecodingState::ContinuousPictureId(int picture_id) const {
213 int next_picture_id = picture_id_ + 1; 240 int next_picture_id = picture_id_ + 1;
214 if (picture_id < picture_id_) { 241 if (picture_id < picture_id_) {
215 // Wrap 242 // Wrap
216 if (picture_id_ >= 0x80) { 243 if (picture_id_ >= 0x80) {
217 // 15 bits used for picture id 244 // 15 bits used for picture id
218 return ((next_picture_id & 0x7FFF) == picture_id); 245 return ((next_picture_id & 0x7FFF) == picture_id);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 // frame_decoded_cleared_to_. We just make the assumption 302 // frame_decoded_cleared_to_. We just make the assumption
276 // that we are not trying to reference back to a very old 303 // that we are not trying to reference back to a very old
277 // index, but instead are referencing a newer index. 304 // index, but instead are referencing a newer index.
278 uint16_t diff = 305 uint16_t diff =
279 index > frame_decoded_cleared_to_ 306 index > frame_decoded_cleared_to_
280 ? kFrameDecodedLength - (index - frame_decoded_cleared_to_) 307 ? kFrameDecodedLength - (index - frame_decoded_cleared_to_)
281 : frame_decoded_cleared_to_ - index; 308 : frame_decoded_cleared_to_ - index;
282 return diff > kFrameDecodedLength / 2; 309 return diff > kFrameDecodedLength / 2;
283 } 310 }
284 311
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
285 } // namespace webrtc 356 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/video_coding/decoding_state.h ('k') | webrtc/modules/video_coding/frame_buffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698