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

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

Issue 2841913002: Stashed frames are now retried in a loop rather than recursively. (Closed)
Patch Set: Apparently RTC_NOTREACHED is not enough for the win compiler. Created 3 years, 8 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
« no previous file with comments | « webrtc/modules/video_coding/rtp_frame_reference_finder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 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
(...skipping 21 matching lines...) Expand all
32 void RtpFrameReferenceFinder::ManageFrame( 32 void RtpFrameReferenceFinder::ManageFrame(
33 std::unique_ptr<RtpFrameObject> frame) { 33 std::unique_ptr<RtpFrameObject> frame) {
34 rtc::CritScope lock(&crit_); 34 rtc::CritScope lock(&crit_);
35 35
36 // If we have cleared past this frame, drop it. 36 // If we have cleared past this frame, drop it.
37 if (cleared_to_seq_num_ != -1 && 37 if (cleared_to_seq_num_ != -1 &&
38 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) { 38 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) {
39 return; 39 return;
40 } 40 }
41 41
42 FrameDecision decision = ManageFrameInternal(frame.get());
43
44 switch (decision) {
45 case kStash:
46 if (stashed_frames_.size() > kMaxStashedFrames)
47 stashed_frames_.pop_back();
48 stashed_frames_.push_front(std::move(frame));
49 break;
50 case kHandOff:
51 frame_callback_->OnCompleteFrame(std::move(frame));
52 RetryStashedFrames();
53 break;
54 case kDrop:
55 break;
56 }
57 }
58
59 void RtpFrameReferenceFinder::RetryStashedFrames() {
60 bool complete_frame = false;
61 do {
62 complete_frame = false;
63 for (auto frame_it = stashed_frames_.begin();
64 frame_it != stashed_frames_.end();) {
65 FrameDecision decision = ManageFrameInternal(frame_it->get());
66
67 switch (decision) {
68 case kStash:
69 ++frame_it;
70 break;
71 case kHandOff:
72 complete_frame = true;
73 frame_callback_->OnCompleteFrame(std::move(*frame_it));
74 FALLTHROUGH();
75 case kDrop:
76 frame_it = stashed_frames_.erase(frame_it);
77 }
78 }
79 } while (complete_frame);
80 }
81
82 RtpFrameReferenceFinder::FrameDecision
83 RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) {
42 switch (frame->codec_type()) { 84 switch (frame->codec_type()) {
43 case kVideoCodecFlexfec: 85 case kVideoCodecFlexfec:
44 case kVideoCodecULPFEC: 86 case kVideoCodecULPFEC:
45 case kVideoCodecRED: 87 case kVideoCodecRED:
46 RTC_NOTREACHED(); 88 RTC_NOTREACHED();
47 break; 89 break;
48 case kVideoCodecVP8: 90 case kVideoCodecVP8:
49 ManageFrameVp8(std::move(frame)); 91 return ManageFrameVp8(frame);
50 break;
51 case kVideoCodecVP9: 92 case kVideoCodecVP9:
52 ManageFrameVp9(std::move(frame)); 93 return ManageFrameVp9(frame);
53 break;
54 // Since the EndToEndTests use kVicdeoCodecUnknow we treat it the same as 94 // Since the EndToEndTests use kVicdeoCodecUnknow we treat it the same as
55 // kVideoCodecGeneric. 95 // kVideoCodecGeneric.
56 // TODO(philipel): Take a look at the EndToEndTests and see if maybe they 96 // TODO(philipel): Take a look at the EndToEndTests and see if maybe they
57 // should be changed to use kVideoCodecGeneric instead. 97 // should be changed to use kVideoCodecGeneric instead.
58 case kVideoCodecUnknown: 98 case kVideoCodecUnknown:
59 case kVideoCodecH264: 99 case kVideoCodecH264:
60 case kVideoCodecI420: 100 case kVideoCodecI420:
61 case kVideoCodecGeneric: 101 case kVideoCodecGeneric:
62 ManageFrameGeneric(std::move(frame), kNoPictureId); 102 return ManageFrameGeneric(frame, kNoPictureId);
63 break;
64 } 103 }
104
105 // If not all code paths return a value it makes the win compiler sad.
106 RTC_NOTREACHED();
107 return kDrop;
65 } 108 }
66 109
67 void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) { 110 void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
68 rtc::CritScope lock(&crit_); 111 rtc::CritScope lock(&crit_);
69 auto clean_padding_to = 112 auto clean_padding_to =
70 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge); 113 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
71 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to); 114 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
72 stashed_padding_.insert(seq_num); 115 stashed_padding_.insert(seq_num);
73 UpdateLastPictureIdWithPadding(seq_num); 116 UpdateLastPictureIdWithPadding(seq_num);
74 RetryStashedFrames(); 117 RetryStashedFrames();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 // for a while there is a risk that new frames will appear to be older than 160 // for a while there is a risk that new frames will appear to be older than
118 // the keyframe they belong to due to wrapping sequence number. In order 161 // the keyframe they belong to due to wrapping sequence number. In order
119 // to prevent this we advance the picture id of the keyframe every so often. 162 // to prevent this we advance the picture id of the keyframe every so often.
120 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) { 163 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
121 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size()); 164 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
122 last_seq_num_gop_[seq_num] = gop_seq_num_it->second; 165 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
123 last_seq_num_gop_.erase(gop_seq_num_it); 166 last_seq_num_gop_.erase(gop_seq_num_it);
124 } 167 }
125 } 168 }
126 169
127 void RtpFrameReferenceFinder::RetryStashedFrames() { 170 RtpFrameReferenceFinder::FrameDecision
128 size_t num_stashed_frames = stashed_frames_.size(); 171 RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame,
129 172 int picture_id) {
130 // Clean up stashed frames if there are too many.
131 while (stashed_frames_.size() > kMaxStashedFrames)
132 stashed_frames_.pop_front();
133
134 // Since frames are stashed if there is not enough data to determine their
135 // frame references we should at most check |stashed_frames_.size()| in
136 // order to not pop and push frames in and endless loop.
137 // NOTE! This function may be called recursively, hence the
138 // "!stashed_frames_.empty()" condition.
139 for (size_t i = 0; i < num_stashed_frames && !stashed_frames_.empty(); ++i) {
140 std::unique_ptr<RtpFrameObject> frame = std::move(stashed_frames_.front());
141 stashed_frames_.pop_front();
142 ManageFrame(std::move(frame));
143 }
144 }
145
146 void RtpFrameReferenceFinder::ManageFrameGeneric(
147 std::unique_ptr<RtpFrameObject> frame,
148 int picture_id) {
149 // If |picture_id| is specified then we use that to set the frame references, 173 // If |picture_id| is specified then we use that to set the frame references,
150 // otherwise we use sequence number. 174 // otherwise we use sequence number.
151 if (picture_id != kNoPictureId) { 175 if (picture_id != kNoPictureId) {
152 if (last_unwrap_ == -1) 176 if (last_unwrap_ == -1)
153 last_unwrap_ = picture_id; 177 last_unwrap_ = picture_id;
154 178
155 frame->picture_id = UnwrapPictureId(picture_id % kPicIdLength); 179 frame->picture_id = UnwrapPictureId(picture_id % kPicIdLength);
156 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1; 180 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1;
157 frame->references[0] = frame->picture_id - 1; 181 frame->references[0] = frame->picture_id - 1;
158 frame_callback_->OnCompleteFrame(std::move(frame)); 182 return kHandOff;
159 return;
160 } 183 }
161 184
162 if (frame->frame_type() == kVideoFrameKey) { 185 if (frame->frame_type() == kVideoFrameKey) {
163 last_seq_num_gop_.insert(std::make_pair( 186 last_seq_num_gop_.insert(std::make_pair(
164 frame->last_seq_num(), 187 frame->last_seq_num(),
165 std::make_pair(frame->last_seq_num(), frame->last_seq_num()))); 188 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
166 } 189 }
167 190
168 // We have received a frame but not yet a keyframe, stash this frame. 191 // We have received a frame but not yet a keyframe, stash this frame.
169 if (last_seq_num_gop_.empty()) { 192 if (last_seq_num_gop_.empty())
170 stashed_frames_.push_back(std::move(frame)); 193 return kStash;
171 return;
172 }
173 194
174 // Clean up info for old keyframes but make sure to keep info 195 // Clean up info for old keyframes but make sure to keep info
175 // for the last keyframe. 196 // for the last keyframe.
176 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100); 197 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
177 for (auto it = last_seq_num_gop_.begin(); 198 for (auto it = last_seq_num_gop_.begin();
178 it != clean_to && last_seq_num_gop_.size() > 1;) { 199 it != clean_to && last_seq_num_gop_.size() > 1;) {
179 it = last_seq_num_gop_.erase(it); 200 it = last_seq_num_gop_.erase(it);
180 } 201 }
181 202
182 // Find the last sequence number of the last frame for the keyframe 203 // Find the last sequence number of the last frame for the keyframe
183 // that this frame indirectly references. 204 // that this frame indirectly references.
184 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num()); 205 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
185 if (seq_num_it == last_seq_num_gop_.begin()) { 206 if (seq_num_it == last_seq_num_gop_.begin()) {
186 LOG(LS_WARNING) << "Generic frame with packet range [" 207 LOG(LS_WARNING) << "Generic frame with packet range ["
187 << frame->first_seq_num() << ", " << frame->last_seq_num() 208 << frame->first_seq_num() << ", " << frame->last_seq_num()
188 << "] has no GoP, dropping frame."; 209 << "] has no GoP, dropping frame.";
189 return; 210 return kDrop;
190 } 211 }
191 seq_num_it--; 212 seq_num_it--;
192 213
193 // Make sure the packet sequence numbers are continuous, otherwise stash 214 // Make sure the packet sequence numbers are continuous, otherwise stash
194 // this frame. 215 // this frame.
195 uint16_t last_picture_id_gop = seq_num_it->second.first; 216 uint16_t last_picture_id_gop = seq_num_it->second.first;
196 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second; 217 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
197 if (frame->frame_type() == kVideoFrameDelta) { 218 if (frame->frame_type() == kVideoFrameDelta) {
198 uint16_t prev_seq_num = frame->first_seq_num() - 1; 219 uint16_t prev_seq_num = frame->first_seq_num() - 1;
199 if (prev_seq_num != last_picture_id_with_padding_gop) { 220
200 stashed_frames_.push_back(std::move(frame)); 221 if (prev_seq_num != last_picture_id_with_padding_gop)
201 return; 222 return kStash;
202 }
203 } 223 }
204 224
205 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first)); 225 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
206 226
207 // Since keyframes can cause reordering we can't simply assign the 227 // Since keyframes can cause reordering we can't simply assign the
208 // picture id according to some incrementing counter. 228 // picture id according to some incrementing counter.
209 frame->picture_id = frame->last_seq_num(); 229 frame->picture_id = frame->last_seq_num();
210 frame->num_references = frame->frame_type() == kVideoFrameDelta; 230 frame->num_references = frame->frame_type() == kVideoFrameDelta;
211 frame->references[0] = last_picture_id_gop; 231 frame->references[0] = last_picture_id_gop;
212 if (AheadOf(frame->picture_id, last_picture_id_gop)) { 232 if (AheadOf(frame->picture_id, last_picture_id_gop)) {
213 seq_num_it->second.first = frame->picture_id; 233 seq_num_it->second.first = frame->picture_id;
214 seq_num_it->second.second = frame->picture_id; 234 seq_num_it->second.second = frame->picture_id;
215 } 235 }
216 236
217 last_picture_id_ = frame->picture_id; 237 last_picture_id_ = frame->picture_id;
218 UpdateLastPictureIdWithPadding(frame->picture_id); 238 UpdateLastPictureIdWithPadding(frame->picture_id);
219 frame_callback_->OnCompleteFrame(std::move(frame)); 239 return kHandOff;
220 RetryStashedFrames();
221 } 240 }
222 241
223 void RtpFrameReferenceFinder::ManageFrameVp8( 242 RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
224 std::unique_ptr<RtpFrameObject> frame) { 243 RtpFrameObject* frame) {
225 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); 244 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
226 if (!rtp_codec_header) 245 if (!rtp_codec_header)
227 return; 246 return kDrop;
228 247
229 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8; 248 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
230 249
231 if (codec_header.pictureId == kNoPictureId || 250 if (codec_header.pictureId == kNoPictureId ||
232 codec_header.temporalIdx == kNoTemporalIdx || 251 codec_header.temporalIdx == kNoTemporalIdx ||
233 codec_header.tl0PicIdx == kNoTl0PicIdx) { 252 codec_header.tl0PicIdx == kNoTl0PicIdx) {
234 ManageFrameGeneric(std::move(frame), codec_header.pictureId); 253 return ManageFrameGeneric(std::move(frame), codec_header.pictureId);
235 return;
236 } 254 }
237 255
238 frame->picture_id = codec_header.pictureId % kPicIdLength; 256 frame->picture_id = codec_header.pictureId % kPicIdLength;
239 257
240 if (last_unwrap_ == -1) 258 if (last_unwrap_ == -1)
241 last_unwrap_ = codec_header.pictureId; 259 last_unwrap_ = codec_header.pictureId;
242 260
243 if (last_picture_id_ == -1) 261 if (last_picture_id_ == -1)
244 last_picture_id_ = frame->picture_id; 262 last_picture_id_ = frame->picture_id;
245 263
(...skipping 15 matching lines...) Expand all
261 // Clean up info about not yet received frames that are too old. 279 // Clean up info about not yet received frames that are too old.
262 uint16_t old_picture_id = 280 uint16_t old_picture_id =
263 Subtract<kPicIdLength>(frame->picture_id, kMaxNotYetReceivedFrames); 281 Subtract<kPicIdLength>(frame->picture_id, kMaxNotYetReceivedFrames);
264 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id); 282 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
265 not_yet_received_frames_.erase(not_yet_received_frames_.begin(), 283 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
266 clean_frames_to); 284 clean_frames_to);
267 285
268 if (frame->frame_type() == kVideoFrameKey) { 286 if (frame->frame_type() == kVideoFrameKey) {
269 frame->num_references = 0; 287 frame->num_references = 0;
270 layer_info_[codec_header.tl0PicIdx].fill(-1); 288 layer_info_[codec_header.tl0PicIdx].fill(-1);
271 CompletedFrameVp8(std::move(frame)); 289 UpdateLayerInfoVp8(frame);
272 return; 290 return kHandOff;
273 } 291 }
274 292
275 auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0 293 auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0
276 ? codec_header.tl0PicIdx - 1 294 ? codec_header.tl0PicIdx - 1
277 : codec_header.tl0PicIdx); 295 : codec_header.tl0PicIdx);
278 296
279 // If we don't have the base layer frame yet, stash this frame. 297 // If we don't have the base layer frame yet, stash this frame.
280 if (layer_info_it == layer_info_.end()) { 298 if (layer_info_it == layer_info_.end())
281 stashed_frames_.push_back(std::move(frame)); 299 return kStash;
282 return;
283 }
284 300
285 // A non keyframe base layer frame has been received, copy the layer info 301 // A non keyframe base layer frame has been received, copy the layer info
286 // from the previous base layer frame and set a reference to the previous 302 // from the previous base layer frame and set a reference to the previous
287 // base layer frame. 303 // base layer frame.
288 if (codec_header.temporalIdx == 0) { 304 if (codec_header.temporalIdx == 0) {
289 layer_info_it = 305 layer_info_it =
290 layer_info_ 306 layer_info_
291 .insert(make_pair(codec_header.tl0PicIdx, layer_info_it->second)) 307 .insert(make_pair(codec_header.tl0PicIdx, layer_info_it->second))
292 .first; 308 .first;
293 frame->num_references = 1; 309 frame->num_references = 1;
294 frame->references[0] = layer_info_it->second[0]; 310 frame->references[0] = layer_info_it->second[0];
295 CompletedFrameVp8(std::move(frame)); 311 UpdateLayerInfoVp8(frame);
296 return; 312 return kHandOff;
297 } 313 }
298 314
299 // Layer sync frame, this frame only references its base layer frame. 315 // Layer sync frame, this frame only references its base layer frame.
300 if (codec_header.layerSync) { 316 if (codec_header.layerSync) {
301 frame->num_references = 1; 317 frame->num_references = 1;
302 frame->references[0] = layer_info_it->second[0]; 318 frame->references[0] = layer_info_it->second[0];
303 319
304 CompletedFrameVp8(std::move(frame)); 320 UpdateLayerInfoVp8(frame);
305 return; 321 return kHandOff;
306 } 322 }
307 323
308 // Find all references for this frame. 324 // Find all references for this frame.
309 frame->num_references = 0; 325 frame->num_references = 0;
310 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) { 326 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
311 // If we have not yet received a previous frame on this temporal layer, 327 // If we have not yet received a previous frame on this temporal layer,
312 // stash this frame. 328 // stash this frame.
313 if (layer_info_it->second[layer] == -1) { 329 if (layer_info_it->second[layer] == -1)
314 stashed_frames_.push_back(std::move(frame)); 330 return kStash;
315 return;
316 }
317 331
318 // If the last frame on this layer is ahead of this frame it means that 332 // If the last frame on this layer is ahead of this frame it means that
319 // a layer sync frame has been received after this frame for the same 333 // a layer sync frame has been received after this frame for the same
320 // base layer frame, drop this frame. 334 // base layer frame, drop this frame.
321 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer], 335 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
322 frame->picture_id)) { 336 frame->picture_id)) {
323 return; 337 return kDrop;
324 } 338 }
325 339
326 // If we have not yet received a frame between this frame and the referenced 340 // If we have not yet received a frame between this frame and the referenced
327 // frame then we have to wait for that frame to be completed first. 341 // frame then we have to wait for that frame to be completed first.
328 auto not_received_frame_it = 342 auto not_received_frame_it =
329 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]); 343 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
330 if (not_received_frame_it != not_yet_received_frames_.end() && 344 if (not_received_frame_it != not_yet_received_frames_.end() &&
331 AheadOf<uint16_t, kPicIdLength>(frame->picture_id, 345 AheadOf<uint16_t, kPicIdLength>(frame->picture_id,
332 *not_received_frame_it)) { 346 *not_received_frame_it)) {
333 stashed_frames_.push_back(std::move(frame)); 347 return kStash;
334 return;
335 } 348 }
336 349
337 if (!(AheadOf<uint16_t, kPicIdLength>(frame->picture_id, 350 if (!(AheadOf<uint16_t, kPicIdLength>(frame->picture_id,
338 layer_info_it->second[layer]))) { 351 layer_info_it->second[layer]))) {
339 LOG(LS_WARNING) << "Frame with picture id " << frame->picture_id 352 LOG(LS_WARNING) << "Frame with picture id " << frame->picture_id
340 << " and packet range [" << frame->first_seq_num() << ", " 353 << " and packet range [" << frame->first_seq_num() << ", "
341 << frame->last_seq_num() << "] already received, " 354 << frame->last_seq_num() << "] already received, "
342 << " dropping frame."; 355 << " dropping frame.";
343 return; 356 return kDrop;
344 } 357 }
345 358
346 ++frame->num_references; 359 ++frame->num_references;
347 frame->references[layer] = layer_info_it->second[layer]; 360 frame->references[layer] = layer_info_it->second[layer];
348 } 361 }
349 362
350 CompletedFrameVp8(std::move(frame)); 363 UpdateLayerInfoVp8(frame);
364 return kHandOff;
351 } 365 }
352 366
353 void RtpFrameReferenceFinder::CompletedFrameVp8( 367 void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame) {
354 std::unique_ptr<RtpFrameObject> frame) {
355 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); 368 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
356 if (!rtp_codec_header) 369 RTC_DCHECK(rtp_codec_header);
357 return;
358
359 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8; 370 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
360 371
361 uint8_t tl0_pic_idx = codec_header.tl0PicIdx; 372 uint8_t tl0_pic_idx = codec_header.tl0PicIdx;
362 uint8_t temporal_index = codec_header.temporalIdx; 373 uint8_t temporal_index = codec_header.temporalIdx;
363 auto layer_info_it = layer_info_.find(tl0_pic_idx); 374 auto layer_info_it = layer_info_.find(tl0_pic_idx);
364 375
365 // Update this layer info and newer. 376 // Update this layer info and newer.
366 while (layer_info_it != layer_info_.end()) { 377 while (layer_info_it != layer_info_.end()) {
367 if (layer_info_it->second[temporal_index] != -1 && 378 if (layer_info_it->second[temporal_index] != -1 &&
368 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_index], 379 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_index],
369 frame->picture_id)) { 380 frame->picture_id)) {
370 // The frame was not newer, then no subsequent layer info have to be 381 // The frame was not newer, then no subsequent layer info have to be
371 // update. 382 // update.
372 break; 383 break;
373 } 384 }
374 385
375 layer_info_it->second[codec_header.temporalIdx] = frame->picture_id; 386 layer_info_it->second[codec_header.temporalIdx] = frame->picture_id;
376 ++tl0_pic_idx; 387 ++tl0_pic_idx;
377 layer_info_it = layer_info_.find(tl0_pic_idx); 388 layer_info_it = layer_info_.find(tl0_pic_idx);
378 } 389 }
379 not_yet_received_frames_.erase(frame->picture_id); 390 not_yet_received_frames_.erase(frame->picture_id);
380 391
381 for (size_t i = 0; i < frame->num_references; ++i) 392 UnwrapPictureIds(frame);
382 frame->references[i] = UnwrapPictureId(frame->references[i]);
383 frame->picture_id = UnwrapPictureId(frame->picture_id);
384
385 frame_callback_->OnCompleteFrame(std::move(frame));
386 RetryStashedFrames();
387 } 393 }
388 394
389 void RtpFrameReferenceFinder::ManageFrameVp9( 395 RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
390 std::unique_ptr<RtpFrameObject> frame) { 396 RtpFrameObject* frame) {
391 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); 397 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
392 if (!rtp_codec_header) 398 RTC_DCHECK(rtp_codec_header);
393 return;
394
395 const RTPVideoHeaderVP9& codec_header = rtp_codec_header->VP9; 399 const RTPVideoHeaderVP9& codec_header = rtp_codec_header->VP9;
396 400
397 bool old_frame = Vp9PidTl0Fix(*frame, &rtp_codec_header->VP9.picture_id, 401 bool old_frame = Vp9PidTl0Fix(*frame, &rtp_codec_header->VP9.picture_id,
398 &rtp_codec_header->VP9.tl0_pic_idx); 402 &rtp_codec_header->VP9.tl0_pic_idx);
399 if (old_frame) 403 if (old_frame)
400 return; 404 return kDrop;
401 405
402 if (codec_header.picture_id == kNoPictureId || 406 if (codec_header.picture_id == kNoPictureId ||
403 codec_header.temporal_idx == kNoTemporalIdx) { 407 codec_header.temporal_idx == kNoTemporalIdx) {
404 ManageFrameGeneric(std::move(frame), codec_header.picture_id); 408 return ManageFrameGeneric(std::move(frame), codec_header.picture_id);
405 return;
406 } 409 }
407 410
408 frame->spatial_layer = codec_header.spatial_idx; 411 frame->spatial_layer = codec_header.spatial_idx;
409 frame->inter_layer_predicted = codec_header.inter_layer_predicted; 412 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
410 frame->picture_id = codec_header.picture_id % kPicIdLength; 413 frame->picture_id = codec_header.picture_id % kPicIdLength;
411 414
412 if (last_unwrap_ == -1) 415 if (last_unwrap_ == -1)
413 last_unwrap_ = codec_header.picture_id; 416 last_unwrap_ = codec_header.picture_id;
414 417
415 if (last_picture_id_ == -1) 418 if (last_picture_id_ == -1)
416 last_picture_id_ = frame->picture_id; 419 last_picture_id_ = frame->picture_id;
417 420
418 if (codec_header.flexible_mode) { 421 if (codec_header.flexible_mode) {
419 frame->num_references = codec_header.num_ref_pics; 422 frame->num_references = codec_header.num_ref_pics;
420 for (size_t i = 0; i < frame->num_references; ++i) { 423 for (size_t i = 0; i < frame->num_references; ++i) {
421 frame->references[i] = 424 frame->references[i] =
422 Subtract<1 << 16>(frame->picture_id, codec_header.pid_diff[i]); 425 Subtract<1 << 16>(frame->picture_id, codec_header.pid_diff[i]);
423 } 426 }
424 427
425 CompletedFrameVp9(std::move(frame)); 428 UnwrapPictureIds(frame);
426 return; 429 return kHandOff;
427 } 430 }
428 431
429 if (codec_header.ss_data_available) { 432 if (codec_header.ss_data_available) {
430 // Scalability structures can only be sent with tl0 frames. 433 // Scalability structures can only be sent with tl0 frames.
431 if (codec_header.temporal_idx != 0) { 434 if (codec_header.temporal_idx != 0) {
432 LOG(LS_WARNING) << "Received scalability structure on a non base layer" 435 LOG(LS_WARNING) << "Received scalability structure on a non base layer"
433 " frame. Scalability structure ignored."; 436 " frame. Scalability structure ignored.";
434 } else { 437 } else {
435 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1); 438 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
436 scalability_structures_[current_ss_idx_] = codec_header.gof; 439 scalability_structures_[current_ss_idx_] = codec_header.gof;
(...skipping 11 matching lines...) Expand all
448 gof_info_.erase(gof_info_.begin(), clean_gof_info_to); 451 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
449 452
450 if (frame->frame_type() == kVideoFrameKey) { 453 if (frame->frame_type() == kVideoFrameKey) {
451 // When using GOF all keyframes must include the scalability structure. 454 // When using GOF all keyframes must include the scalability structure.
452 if (!codec_header.ss_data_available) 455 if (!codec_header.ss_data_available)
453 LOG(LS_WARNING) << "Received keyframe without scalability structure"; 456 LOG(LS_WARNING) << "Received keyframe without scalability structure";
454 457
455 frame->num_references = 0; 458 frame->num_references = 0;
456 GofInfo info = gof_info_.find(codec_header.tl0_pic_idx)->second; 459 GofInfo info = gof_info_.find(codec_header.tl0_pic_idx)->second;
457 FrameReceivedVp9(frame->picture_id, &info); 460 FrameReceivedVp9(frame->picture_id, &info);
458 CompletedFrameVp9(std::move(frame)); 461 UnwrapPictureIds(frame);
459 return; 462 return kHandOff;
460 } 463 }
461 464
462 auto gof_info_it = gof_info_.find( 465 auto gof_info_it = gof_info_.find(
463 (codec_header.temporal_idx == 0 && !codec_header.ss_data_available) 466 (codec_header.temporal_idx == 0 && !codec_header.ss_data_available)
464 ? codec_header.tl0_pic_idx - 1 467 ? codec_header.tl0_pic_idx - 1
465 : codec_header.tl0_pic_idx); 468 : codec_header.tl0_pic_idx);
466 469
467 // Gof info for this frame is not available yet, stash this frame. 470 // Gof info for this frame is not available yet, stash this frame.
468 if (gof_info_it == gof_info_.end()) { 471 if (gof_info_it == gof_info_.end())
469 stashed_frames_.push_back(std::move(frame)); 472 return kStash;
470 return;
471 }
472 473
473 GofInfo* info = &gof_info_it->second; 474 GofInfo* info = &gof_info_it->second;
474 FrameReceivedVp9(frame->picture_id, info); 475 FrameReceivedVp9(frame->picture_id, info);
475 476
476 // Make sure we don't miss any frame that could potentially have the 477 // Make sure we don't miss any frame that could potentially have the
477 // up switch flag set. 478 // up switch flag set.
478 if (MissingRequiredFrameVp9(frame->picture_id, *info)) { 479 if (MissingRequiredFrameVp9(frame->picture_id, *info))
479 stashed_frames_.push_back(std::move(frame)); 480 return kStash;
480 return;
481 }
482 481
483 if (codec_header.temporal_up_switch) { 482 if (codec_header.temporal_up_switch) {
484 auto pid_tidx = 483 auto pid_tidx =
485 std::make_pair(frame->picture_id, codec_header.temporal_idx); 484 std::make_pair(frame->picture_id, codec_header.temporal_idx);
486 up_switch_.insert(pid_tidx); 485 up_switch_.insert(pid_tidx);
487 } 486 }
488 487
489 // If this is a base layer frame that contains a scalability structure 488 // If this is a base layer frame that contains a scalability structure
490 // then gof info has already been inserted earlier, so we only want to 489 // then gof info has already been inserted earlier, so we only want to
491 // insert if we haven't done so already. 490 // insert if we haven't done so already.
(...skipping 18 matching lines...) Expand all
510 frame->picture_id, info->gof->pid_diff[gof_idx][i]); 509 frame->picture_id, info->gof->pid_diff[gof_idx][i]);
511 510
512 // If this is a reference to a frame earlier than the last up switch point, 511 // If this is a reference to a frame earlier than the last up switch point,
513 // then ignore this reference. 512 // then ignore this reference.
514 if (UpSwitchInIntervalVp9(frame->picture_id, codec_header.temporal_idx, 513 if (UpSwitchInIntervalVp9(frame->picture_id, codec_header.temporal_idx,
515 frame->references[i])) { 514 frame->references[i])) {
516 --frame->num_references; 515 --frame->num_references;
517 } 516 }
518 } 517 }
519 518
520 CompletedFrameVp9(std::move(frame)); 519 UnwrapPictureIds(frame);
520 return kHandOff;
521 } 521 }
522 522
523 bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id, 523 bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
524 const GofInfo& info) { 524 const GofInfo& info) {
525 size_t diff = 525 size_t diff =
526 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id); 526 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
527 size_t gof_idx = diff % info.gof->num_frames_in_gof; 527 size_t gof_idx = diff % info.gof->num_frames_in_gof;
528 size_t temporal_idx = info.gof->temporal_idx[gof_idx]; 528 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
529 529
530 // For every reference this frame has, check if there is a frame missing in 530 // For every reference this frame has, check if there is a frame missing in
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 up_switch_it != up_switch_.end() && 582 up_switch_it != up_switch_.end() &&
583 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first); 583 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
584 ++up_switch_it) { 584 ++up_switch_it) {
585 if (up_switch_it->second < temporal_idx) 585 if (up_switch_it->second < temporal_idx)
586 return true; 586 return true;
587 } 587 }
588 588
589 return false; 589 return false;
590 } 590 }
591 591
592 void RtpFrameReferenceFinder::CompletedFrameVp9( 592 void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
593 std::unique_ptr<RtpFrameObject> frame) {
594 for (size_t i = 0; i < frame->num_references; ++i) 593 for (size_t i = 0; i < frame->num_references; ++i)
595 frame->references[i] = UnwrapPictureId(frame->references[i]); 594 frame->references[i] = UnwrapPictureId(frame->references[i]);
596 frame->picture_id = UnwrapPictureId(frame->picture_id); 595 frame->picture_id = UnwrapPictureId(frame->picture_id);
597
598 frame_callback_->OnCompleteFrame(std::move(frame));
599 RetryStashedFrames();
600 } 596 }
601 597
602 uint16_t RtpFrameReferenceFinder::UnwrapPictureId(uint16_t picture_id) { 598 uint16_t RtpFrameReferenceFinder::UnwrapPictureId(uint16_t picture_id) {
603 RTC_DCHECK_NE(-1, last_unwrap_); 599 RTC_DCHECK_NE(-1, last_unwrap_);
604 600
605 uint16_t unwrap_truncated = last_unwrap_ % kPicIdLength; 601 uint16_t unwrap_truncated = last_unwrap_ % kPicIdLength;
606 uint16_t diff = MinDiff<uint16_t, kPicIdLength>(unwrap_truncated, picture_id); 602 uint16_t diff = MinDiff<uint16_t, kPicIdLength>(unwrap_truncated, picture_id);
607 603
608 if (AheadOf<uint16_t, kPicIdLength>(picture_id, unwrap_truncated)) 604 if (AheadOf<uint16_t, kPicIdLength>(picture_id, unwrap_truncated))
609 last_unwrap_ = Add<1 << 16>(last_unwrap_, diff); 605 last_unwrap_ = Add<1 << 16>(last_unwrap_, diff);
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 if (!gof_info_.empty() && 747 if (!gof_info_.empty() &&
752 AheadOf<uint8_t>(gof_info_.begin()->first, fixed_tl0)) { 748 AheadOf<uint8_t>(gof_info_.begin()->first, fixed_tl0)) {
753 return true; 749 return true;
754 } 750 }
755 } 751 }
756 return false; 752 return false;
757 } 753 }
758 754
759 } // namespace video_coding 755 } // namespace video_coding
760 } // namespace webrtc 756 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/video_coding/rtp_frame_reference_finder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698