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

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: Feedback 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());
philipel 2017/04/26 12:22:27 I check if the incoming frame will be handed off v
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 }
65 } 104 }
66 105
67 void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) { 106 void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
68 rtc::CritScope lock(&crit_); 107 rtc::CritScope lock(&crit_);
69 auto clean_padding_to = 108 auto clean_padding_to =
70 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge); 109 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
71 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to); 110 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
72 stashed_padding_.insert(seq_num); 111 stashed_padding_.insert(seq_num);
73 UpdateLastPictureIdWithPadding(seq_num); 112 UpdateLastPictureIdWithPadding(seq_num);
(...skipping 43 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 156 // 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 157 // 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. 158 // 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) { 159 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
121 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size()); 160 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
122 last_seq_num_gop_[seq_num] = gop_seq_num_it->second; 161 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
123 last_seq_num_gop_.erase(gop_seq_num_it); 162 last_seq_num_gop_.erase(gop_seq_num_it);
124 } 163 }
125 } 164 }
126 165
127 void RtpFrameReferenceFinder::RetryStashedFrames() { 166 RtpFrameReferenceFinder::FrameDecision
128 size_t num_stashed_frames = stashed_frames_.size(); 167 RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame,
129 168 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, 169 // If |picture_id| is specified then we use that to set the frame references,
150 // otherwise we use sequence number. 170 // otherwise we use sequence number.
151 if (picture_id != kNoPictureId) { 171 if (picture_id != kNoPictureId) {
152 if (last_unwrap_ == -1) 172 if (last_unwrap_ == -1)
153 last_unwrap_ = picture_id; 173 last_unwrap_ = picture_id;
154 174
155 frame->picture_id = UnwrapPictureId(picture_id % kPicIdLength); 175 frame->picture_id = UnwrapPictureId(picture_id % kPicIdLength);
156 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1; 176 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1;
157 frame->references[0] = frame->picture_id - 1; 177 frame->references[0] = frame->picture_id - 1;
158 frame_callback_->OnCompleteFrame(std::move(frame)); 178 return kHandOff;
159 return;
160 } 179 }
161 180
162 if (frame->frame_type() == kVideoFrameKey) { 181 if (frame->frame_type() == kVideoFrameKey) {
163 last_seq_num_gop_.insert(std::make_pair( 182 last_seq_num_gop_.insert(std::make_pair(
164 frame->last_seq_num(), 183 frame->last_seq_num(),
165 std::make_pair(frame->last_seq_num(), frame->last_seq_num()))); 184 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
166 } 185 }
167 186
168 // We have received a frame but not yet a keyframe, stash this frame. 187 // We have received a frame but not yet a keyframe, stash this frame.
169 if (last_seq_num_gop_.empty()) { 188 if (last_seq_num_gop_.empty())
170 stashed_frames_.push_back(std::move(frame)); 189 return kStash;
171 return;
172 }
173 190
174 // Clean up info for old keyframes but make sure to keep info 191 // Clean up info for old keyframes but make sure to keep info
175 // for the last keyframe. 192 // for the last keyframe.
176 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100); 193 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
177 for (auto it = last_seq_num_gop_.begin(); 194 for (auto it = last_seq_num_gop_.begin();
178 it != clean_to && last_seq_num_gop_.size() > 1;) { 195 it != clean_to && last_seq_num_gop_.size() > 1;) {
179 it = last_seq_num_gop_.erase(it); 196 it = last_seq_num_gop_.erase(it);
180 } 197 }
181 198
182 // Find the last sequence number of the last frame for the keyframe 199 // Find the last sequence number of the last frame for the keyframe
183 // that this frame indirectly references. 200 // that this frame indirectly references.
184 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num()); 201 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
185 if (seq_num_it == last_seq_num_gop_.begin()) { 202 if (seq_num_it == last_seq_num_gop_.begin()) {
186 LOG(LS_WARNING) << "Generic frame with packet range [" 203 LOG(LS_WARNING) << "Generic frame with packet range ["
187 << frame->first_seq_num() << ", " << frame->last_seq_num() 204 << frame->first_seq_num() << ", " << frame->last_seq_num()
188 << "] has no GoP, dropping frame."; 205 << "] has no GoP, dropping frame.";
189 return; 206 return kDrop;
190 } 207 }
191 seq_num_it--; 208 seq_num_it--;
192 209
193 // Make sure the packet sequence numbers are continuous, otherwise stash 210 // Make sure the packet sequence numbers are continuous, otherwise stash
194 // this frame. 211 // this frame.
195 uint16_t last_picture_id_gop = seq_num_it->second.first; 212 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; 213 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
197 if (frame->frame_type() == kVideoFrameDelta) { 214 if (frame->frame_type() == kVideoFrameDelta) {
198 uint16_t prev_seq_num = frame->first_seq_num() - 1; 215 uint16_t prev_seq_num = frame->first_seq_num() - 1;
199 if (prev_seq_num != last_picture_id_with_padding_gop) { 216
200 stashed_frames_.push_back(std::move(frame)); 217 if (prev_seq_num != last_picture_id_with_padding_gop)
201 return; 218 return kStash;
202 }
203 } 219 }
204 220
205 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first)); 221 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
206 222
207 // Since keyframes can cause reordering we can't simply assign the 223 // Since keyframes can cause reordering we can't simply assign the
208 // picture id according to some incrementing counter. 224 // picture id according to some incrementing counter.
209 frame->picture_id = frame->last_seq_num(); 225 frame->picture_id = frame->last_seq_num();
210 frame->num_references = frame->frame_type() == kVideoFrameDelta; 226 frame->num_references = frame->frame_type() == kVideoFrameDelta;
211 frame->references[0] = last_picture_id_gop; 227 frame->references[0] = last_picture_id_gop;
212 if (AheadOf(frame->picture_id, last_picture_id_gop)) { 228 if (AheadOf(frame->picture_id, last_picture_id_gop)) {
213 seq_num_it->second.first = frame->picture_id; 229 seq_num_it->second.first = frame->picture_id;
214 seq_num_it->second.second = frame->picture_id; 230 seq_num_it->second.second = frame->picture_id;
215 } 231 }
216 232
217 last_picture_id_ = frame->picture_id; 233 last_picture_id_ = frame->picture_id;
218 UpdateLastPictureIdWithPadding(frame->picture_id); 234 UpdateLastPictureIdWithPadding(frame->picture_id);
219 frame_callback_->OnCompleteFrame(std::move(frame)); 235 return kHandOff;
220 RetryStashedFrames();
221 } 236 }
222 237
223 void RtpFrameReferenceFinder::ManageFrameVp8( 238 RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
224 std::unique_ptr<RtpFrameObject> frame) { 239 RtpFrameObject* frame) {
225 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); 240 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
226 if (!rtp_codec_header) 241 if (!rtp_codec_header)
227 return; 242 return kDrop;
228 243
229 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8; 244 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
230 245
231 if (codec_header.pictureId == kNoPictureId || 246 if (codec_header.pictureId == kNoPictureId ||
232 codec_header.temporalIdx == kNoTemporalIdx || 247 codec_header.temporalIdx == kNoTemporalIdx ||
233 codec_header.tl0PicIdx == kNoTl0PicIdx) { 248 codec_header.tl0PicIdx == kNoTl0PicIdx) {
234 ManageFrameGeneric(std::move(frame), codec_header.pictureId); 249 return ManageFrameGeneric(std::move(frame), codec_header.pictureId);
235 return;
236 } 250 }
237 251
238 frame->picture_id = codec_header.pictureId % kPicIdLength; 252 frame->picture_id = codec_header.pictureId % kPicIdLength;
239 253
240 if (last_unwrap_ == -1) 254 if (last_unwrap_ == -1)
241 last_unwrap_ = codec_header.pictureId; 255 last_unwrap_ = codec_header.pictureId;
242 256
243 if (last_picture_id_ == -1) 257 if (last_picture_id_ == -1)
244 last_picture_id_ = frame->picture_id; 258 last_picture_id_ = frame->picture_id;
245 259
(...skipping 15 matching lines...) Expand all
261 // Clean up info about not yet received frames that are too old. 275 // Clean up info about not yet received frames that are too old.
262 uint16_t old_picture_id = 276 uint16_t old_picture_id =
263 Subtract<kPicIdLength>(frame->picture_id, kMaxNotYetReceivedFrames); 277 Subtract<kPicIdLength>(frame->picture_id, kMaxNotYetReceivedFrames);
264 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id); 278 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
265 not_yet_received_frames_.erase(not_yet_received_frames_.begin(), 279 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
266 clean_frames_to); 280 clean_frames_to);
267 281
268 if (frame->frame_type() == kVideoFrameKey) { 282 if (frame->frame_type() == kVideoFrameKey) {
269 frame->num_references = 0; 283 frame->num_references = 0;
270 layer_info_[codec_header.tl0PicIdx].fill(-1); 284 layer_info_[codec_header.tl0PicIdx].fill(-1);
271 CompletedFrameVp8(std::move(frame)); 285 UpdateLayerInfoVp8(frame);
272 return; 286 return kHandOff;
273 } 287 }
274 288
275 auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0 289 auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0
276 ? codec_header.tl0PicIdx - 1 290 ? codec_header.tl0PicIdx - 1
277 : codec_header.tl0PicIdx); 291 : codec_header.tl0PicIdx);
278 292
279 // If we don't have the base layer frame yet, stash this frame. 293 // If we don't have the base layer frame yet, stash this frame.
280 if (layer_info_it == layer_info_.end()) { 294 if (layer_info_it == layer_info_.end())
281 stashed_frames_.push_back(std::move(frame)); 295 return kStash;
282 return;
283 }
284 296
285 // A non keyframe base layer frame has been received, copy the layer info 297 // 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 298 // from the previous base layer frame and set a reference to the previous
287 // base layer frame. 299 // base layer frame.
288 if (codec_header.temporalIdx == 0) { 300 if (codec_header.temporalIdx == 0) {
289 layer_info_it = 301 layer_info_it =
290 layer_info_ 302 layer_info_
291 .insert(make_pair(codec_header.tl0PicIdx, layer_info_it->second)) 303 .insert(make_pair(codec_header.tl0PicIdx, layer_info_it->second))
292 .first; 304 .first;
293 frame->num_references = 1; 305 frame->num_references = 1;
294 frame->references[0] = layer_info_it->second[0]; 306 frame->references[0] = layer_info_it->second[0];
295 CompletedFrameVp8(std::move(frame)); 307 UpdateLayerInfoVp8(frame);
296 return; 308 return kHandOff;
297 } 309 }
298 310
299 // Layer sync frame, this frame only references its base layer frame. 311 // Layer sync frame, this frame only references its base layer frame.
300 if (codec_header.layerSync) { 312 if (codec_header.layerSync) {
301 frame->num_references = 1; 313 frame->num_references = 1;
302 frame->references[0] = layer_info_it->second[0]; 314 frame->references[0] = layer_info_it->second[0];
303 315
304 CompletedFrameVp8(std::move(frame)); 316 UpdateLayerInfoVp8(frame);
305 return; 317 return kHandOff;
306 } 318 }
307 319
308 // Find all references for this frame. 320 // Find all references for this frame.
309 frame->num_references = 0; 321 frame->num_references = 0;
310 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) { 322 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
311 // If we have not yet received a previous frame on this temporal layer, 323 // If we have not yet received a previous frame on this temporal layer,
312 // stash this frame. 324 // stash this frame.
313 if (layer_info_it->second[layer] == -1) { 325 if (layer_info_it->second[layer] == -1)
314 stashed_frames_.push_back(std::move(frame)); 326 return kStash;
315 return;
316 }
317 327
318 // If the last frame on this layer is ahead of this frame it means that 328 // 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 329 // a layer sync frame has been received after this frame for the same
320 // base layer frame, drop this frame. 330 // base layer frame, drop this frame.
321 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer], 331 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
322 frame->picture_id)) { 332 frame->picture_id)) {
323 return; 333 return kDrop;
324 } 334 }
325 335
326 // If we have not yet received a frame between this frame and the referenced 336 // 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. 337 // frame then we have to wait for that frame to be completed first.
328 auto not_received_frame_it = 338 auto not_received_frame_it =
329 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]); 339 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
330 if (not_received_frame_it != not_yet_received_frames_.end() && 340 if (not_received_frame_it != not_yet_received_frames_.end() &&
331 AheadOf<uint16_t, kPicIdLength>(frame->picture_id, 341 AheadOf<uint16_t, kPicIdLength>(frame->picture_id,
332 *not_received_frame_it)) { 342 *not_received_frame_it)) {
333 stashed_frames_.push_back(std::move(frame)); 343 return kStash;
334 return;
335 } 344 }
336 345
337 if (!(AheadOf<uint16_t, kPicIdLength>(frame->picture_id, 346 if (!(AheadOf<uint16_t, kPicIdLength>(frame->picture_id,
338 layer_info_it->second[layer]))) { 347 layer_info_it->second[layer]))) {
339 LOG(LS_WARNING) << "Frame with picture id " << frame->picture_id 348 LOG(LS_WARNING) << "Frame with picture id " << frame->picture_id
340 << " and packet range [" << frame->first_seq_num() << ", " 349 << " and packet range [" << frame->first_seq_num() << ", "
341 << frame->last_seq_num() << "] already received, " 350 << frame->last_seq_num() << "] already received, "
342 << " dropping frame."; 351 << " dropping frame.";
343 return; 352 return kDrop;
344 } 353 }
345 354
346 ++frame->num_references; 355 ++frame->num_references;
347 frame->references[layer] = layer_info_it->second[layer]; 356 frame->references[layer] = layer_info_it->second[layer];
348 } 357 }
349 358
350 CompletedFrameVp8(std::move(frame)); 359 UpdateLayerInfoVp8(frame);
360 return kHandOff;
351 } 361 }
352 362
353 void RtpFrameReferenceFinder::CompletedFrameVp8( 363 void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame) {
354 std::unique_ptr<RtpFrameObject> frame) {
355 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); 364 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
356 if (!rtp_codec_header) 365 RTC_DCHECK(rtp_codec_header);
357 return;
358
359 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8; 366 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
360 367
361 uint8_t tl0_pic_idx = codec_header.tl0PicIdx; 368 uint8_t tl0_pic_idx = codec_header.tl0PicIdx;
362 uint8_t temporal_index = codec_header.temporalIdx; 369 uint8_t temporal_index = codec_header.temporalIdx;
363 auto layer_info_it = layer_info_.find(tl0_pic_idx); 370 auto layer_info_it = layer_info_.find(tl0_pic_idx);
364 371
365 // Update this layer info and newer. 372 // Update this layer info and newer.
366 while (layer_info_it != layer_info_.end()) { 373 while (layer_info_it != layer_info_.end()) {
367 if (layer_info_it->second[temporal_index] != -1 && 374 if (layer_info_it->second[temporal_index] != -1 &&
368 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_index], 375 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_index],
369 frame->picture_id)) { 376 frame->picture_id)) {
370 // The frame was not newer, then no subsequent layer info have to be 377 // The frame was not newer, then no subsequent layer info have to be
371 // update. 378 // update.
372 break; 379 break;
373 } 380 }
374 381
375 layer_info_it->second[codec_header.temporalIdx] = frame->picture_id; 382 layer_info_it->second[codec_header.temporalIdx] = frame->picture_id;
376 ++tl0_pic_idx; 383 ++tl0_pic_idx;
377 layer_info_it = layer_info_.find(tl0_pic_idx); 384 layer_info_it = layer_info_.find(tl0_pic_idx);
378 } 385 }
379 not_yet_received_frames_.erase(frame->picture_id); 386 not_yet_received_frames_.erase(frame->picture_id);
380 387
381 for (size_t i = 0; i < frame->num_references; ++i) 388 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 } 389 }
388 390
389 void RtpFrameReferenceFinder::ManageFrameVp9( 391 RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
390 std::unique_ptr<RtpFrameObject> frame) { 392 RtpFrameObject* frame) {
391 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); 393 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
392 if (!rtp_codec_header) 394 RTC_DCHECK(rtp_codec_header);
393 return;
394
395 const RTPVideoHeaderVP9& codec_header = rtp_codec_header->VP9; 395 const RTPVideoHeaderVP9& codec_header = rtp_codec_header->VP9;
396 396
397 bool old_frame = Vp9PidTl0Fix(*frame, &rtp_codec_header->VP9.picture_id, 397 bool old_frame = Vp9PidTl0Fix(*frame, &rtp_codec_header->VP9.picture_id,
398 &rtp_codec_header->VP9.tl0_pic_idx); 398 &rtp_codec_header->VP9.tl0_pic_idx);
399 if (old_frame) 399 if (old_frame)
400 return; 400 return kDrop;
401 401
402 if (codec_header.picture_id == kNoPictureId || 402 if (codec_header.picture_id == kNoPictureId ||
403 codec_header.temporal_idx == kNoTemporalIdx) { 403 codec_header.temporal_idx == kNoTemporalIdx) {
404 ManageFrameGeneric(std::move(frame), codec_header.picture_id); 404 return ManageFrameGeneric(std::move(frame), codec_header.picture_id);
405 return;
406 } 405 }
407 406
408 frame->spatial_layer = codec_header.spatial_idx; 407 frame->spatial_layer = codec_header.spatial_idx;
409 frame->inter_layer_predicted = codec_header.inter_layer_predicted; 408 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
410 frame->picture_id = codec_header.picture_id % kPicIdLength; 409 frame->picture_id = codec_header.picture_id % kPicIdLength;
411 410
412 if (last_unwrap_ == -1) 411 if (last_unwrap_ == -1)
413 last_unwrap_ = codec_header.picture_id; 412 last_unwrap_ = codec_header.picture_id;
414 413
415 if (last_picture_id_ == -1) 414 if (last_picture_id_ == -1)
416 last_picture_id_ = frame->picture_id; 415 last_picture_id_ = frame->picture_id;
417 416
418 if (codec_header.flexible_mode) { 417 if (codec_header.flexible_mode) {
419 frame->num_references = codec_header.num_ref_pics; 418 frame->num_references = codec_header.num_ref_pics;
420 for (size_t i = 0; i < frame->num_references; ++i) { 419 for (size_t i = 0; i < frame->num_references; ++i) {
421 frame->references[i] = 420 frame->references[i] =
422 Subtract<1 << 16>(frame->picture_id, codec_header.pid_diff[i]); 421 Subtract<1 << 16>(frame->picture_id, codec_header.pid_diff[i]);
423 } 422 }
424 423
425 CompletedFrameVp9(std::move(frame)); 424 UnwrapPictureIds(frame);
426 return; 425 return kHandOff;
427 } 426 }
428 427
429 if (codec_header.ss_data_available) { 428 if (codec_header.ss_data_available) {
430 // Scalability structures can only be sent with tl0 frames. 429 // Scalability structures can only be sent with tl0 frames.
431 if (codec_header.temporal_idx != 0) { 430 if (codec_header.temporal_idx != 0) {
432 LOG(LS_WARNING) << "Received scalability structure on a non base layer" 431 LOG(LS_WARNING) << "Received scalability structure on a non base layer"
433 " frame. Scalability structure ignored."; 432 " frame. Scalability structure ignored.";
434 } else { 433 } else {
435 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1); 434 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
436 scalability_structures_[current_ss_idx_] = codec_header.gof; 435 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); 447 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
449 448
450 if (frame->frame_type() == kVideoFrameKey) { 449 if (frame->frame_type() == kVideoFrameKey) {
451 // When using GOF all keyframes must include the scalability structure. 450 // When using GOF all keyframes must include the scalability structure.
452 if (!codec_header.ss_data_available) 451 if (!codec_header.ss_data_available)
453 LOG(LS_WARNING) << "Received keyframe without scalability structure"; 452 LOG(LS_WARNING) << "Received keyframe without scalability structure";
454 453
455 frame->num_references = 0; 454 frame->num_references = 0;
456 GofInfo info = gof_info_.find(codec_header.tl0_pic_idx)->second; 455 GofInfo info = gof_info_.find(codec_header.tl0_pic_idx)->second;
457 FrameReceivedVp9(frame->picture_id, &info); 456 FrameReceivedVp9(frame->picture_id, &info);
458 CompletedFrameVp9(std::move(frame)); 457 UnwrapPictureIds(frame);
459 return; 458 return kHandOff;
460 } 459 }
461 460
462 auto gof_info_it = gof_info_.find( 461 auto gof_info_it = gof_info_.find(
463 (codec_header.temporal_idx == 0 && !codec_header.ss_data_available) 462 (codec_header.temporal_idx == 0 && !codec_header.ss_data_available)
464 ? codec_header.tl0_pic_idx - 1 463 ? codec_header.tl0_pic_idx - 1
465 : codec_header.tl0_pic_idx); 464 : codec_header.tl0_pic_idx);
466 465
467 // Gof info for this frame is not available yet, stash this frame. 466 // Gof info for this frame is not available yet, stash this frame.
468 if (gof_info_it == gof_info_.end()) { 467 if (gof_info_it == gof_info_.end())
469 stashed_frames_.push_back(std::move(frame)); 468 return kStash;
470 return;
471 }
472 469
473 GofInfo* info = &gof_info_it->second; 470 GofInfo* info = &gof_info_it->second;
474 FrameReceivedVp9(frame->picture_id, info); 471 FrameReceivedVp9(frame->picture_id, info);
475 472
476 // Make sure we don't miss any frame that could potentially have the 473 // Make sure we don't miss any frame that could potentially have the
477 // up switch flag set. 474 // up switch flag set.
478 if (MissingRequiredFrameVp9(frame->picture_id, *info)) { 475 if (MissingRequiredFrameVp9(frame->picture_id, *info))
479 stashed_frames_.push_back(std::move(frame)); 476 return kStash;
480 return;
481 }
482 477
483 if (codec_header.temporal_up_switch) { 478 if (codec_header.temporal_up_switch) {
484 auto pid_tidx = 479 auto pid_tidx =
485 std::make_pair(frame->picture_id, codec_header.temporal_idx); 480 std::make_pair(frame->picture_id, codec_header.temporal_idx);
486 up_switch_.insert(pid_tidx); 481 up_switch_.insert(pid_tidx);
487 } 482 }
488 483
489 // If this is a base layer frame that contains a scalability structure 484 // 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 485 // then gof info has already been inserted earlier, so we only want to
491 // insert if we haven't done so already. 486 // 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]); 505 frame->picture_id, info->gof->pid_diff[gof_idx][i]);
511 506
512 // If this is a reference to a frame earlier than the last up switch point, 507 // If this is a reference to a frame earlier than the last up switch point,
513 // then ignore this reference. 508 // then ignore this reference.
514 if (UpSwitchInIntervalVp9(frame->picture_id, codec_header.temporal_idx, 509 if (UpSwitchInIntervalVp9(frame->picture_id, codec_header.temporal_idx,
515 frame->references[i])) { 510 frame->references[i])) {
516 --frame->num_references; 511 --frame->num_references;
517 } 512 }
518 } 513 }
519 514
520 CompletedFrameVp9(std::move(frame)); 515 UnwrapPictureIds(frame);
516 return kHandOff;
521 } 517 }
522 518
523 bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id, 519 bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
524 const GofInfo& info) { 520 const GofInfo& info) {
525 size_t diff = 521 size_t diff =
526 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id); 522 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
527 size_t gof_idx = diff % info.gof->num_frames_in_gof; 523 size_t gof_idx = diff % info.gof->num_frames_in_gof;
528 size_t temporal_idx = info.gof->temporal_idx[gof_idx]; 524 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
529 525
530 // For every reference this frame has, check if there is a frame missing in 526 // 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() && 578 up_switch_it != up_switch_.end() &&
583 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first); 579 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
584 ++up_switch_it) { 580 ++up_switch_it) {
585 if (up_switch_it->second < temporal_idx) 581 if (up_switch_it->second < temporal_idx)
586 return true; 582 return true;
587 } 583 }
588 584
589 return false; 585 return false;
590 } 586 }
591 587
592 void RtpFrameReferenceFinder::CompletedFrameVp9( 588 void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
593 std::unique_ptr<RtpFrameObject> frame) {
594 for (size_t i = 0; i < frame->num_references; ++i) 589 for (size_t i = 0; i < frame->num_references; ++i)
595 frame->references[i] = UnwrapPictureId(frame->references[i]); 590 frame->references[i] = UnwrapPictureId(frame->references[i]);
596 frame->picture_id = UnwrapPictureId(frame->picture_id); 591 frame->picture_id = UnwrapPictureId(frame->picture_id);
597
598 frame_callback_->OnCompleteFrame(std::move(frame));
599 RetryStashedFrames();
600 } 592 }
601 593
602 uint16_t RtpFrameReferenceFinder::UnwrapPictureId(uint16_t picture_id) { 594 uint16_t RtpFrameReferenceFinder::UnwrapPictureId(uint16_t picture_id) {
603 RTC_DCHECK_NE(-1, last_unwrap_); 595 RTC_DCHECK_NE(-1, last_unwrap_);
604 596
605 uint16_t unwrap_truncated = last_unwrap_ % kPicIdLength; 597 uint16_t unwrap_truncated = last_unwrap_ % kPicIdLength;
606 uint16_t diff = MinDiff<uint16_t, kPicIdLength>(unwrap_truncated, picture_id); 598 uint16_t diff = MinDiff<uint16_t, kPicIdLength>(unwrap_truncated, picture_id);
607 599
608 if (AheadOf<uint16_t, kPicIdLength>(picture_id, unwrap_truncated)) 600 if (AheadOf<uint16_t, kPicIdLength>(picture_id, unwrap_truncated))
609 last_unwrap_ = Add<1 << 16>(last_unwrap_, diff); 601 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() && 743 if (!gof_info_.empty() &&
752 AheadOf<uint8_t>(gof_info_.begin()->first, fixed_tl0)) { 744 AheadOf<uint8_t>(gof_info_.begin()->first, fixed_tl0)) {
753 return true; 745 return true;
754 } 746 }
755 } 747 }
756 return false; 748 return false;
757 } 749 }
758 750
759 } // namespace video_coding 751 } // namespace video_coding
760 } // namespace webrtc 752 } // 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