OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <algorithm> | |
6 #include <limits> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/bind_helpers.h" | |
10 #include "base/callback_helpers.h" | |
11 #include "base/macros.h" | |
12 #include "base/numerics/safe_conversions.h" | |
13 #include "base/stl_util.h" | |
14 #include "content/common/gpu/media/h264_decoder.h" | |
15 | |
16 namespace content { | |
17 | |
18 H264Decoder::H264Accelerator::H264Accelerator() { | |
19 } | |
20 | |
21 H264Decoder::H264Accelerator::~H264Accelerator() { | |
22 } | |
23 | |
24 H264Decoder::H264Decoder(H264Accelerator* accelerator) | |
25 : max_frame_num_(0), | |
26 max_pic_num_(0), | |
27 max_long_term_frame_idx_(0), | |
28 max_num_reorder_frames_(0), | |
29 accelerator_(accelerator) { | |
30 DCHECK(accelerator_); | |
31 Reset(); | |
32 state_ = kNeedStreamMetadata; | |
33 } | |
34 | |
35 H264Decoder::~H264Decoder() { | |
36 } | |
37 | |
38 void H264Decoder::Reset() { | |
39 curr_pic_ = nullptr; | |
40 curr_nalu_ = nullptr; | |
41 curr_slice_hdr_ = nullptr; | |
42 curr_sps_id_ = -1; | |
43 curr_pps_id_ = -1; | |
44 | |
45 prev_frame_num_ = -1; | |
46 prev_ref_frame_num_ = -1; | |
47 prev_frame_num_offset_ = -1; | |
48 prev_has_memmgmnt5_ = false; | |
49 | |
50 prev_ref_has_memmgmnt5_ = false; | |
51 prev_ref_top_field_order_cnt_ = -1; | |
52 prev_ref_pic_order_cnt_msb_ = -1; | |
53 prev_ref_pic_order_cnt_lsb_ = -1; | |
54 prev_ref_field_ = H264Picture::FIELD_NONE; | |
55 | |
56 ref_pic_list_p0_.clear(); | |
57 ref_pic_list_b0_.clear(); | |
58 ref_pic_list_b1_.clear(); | |
59 dpb_.Clear(); | |
60 parser_.Reset(); | |
61 accelerator_->Reset(); | |
62 last_output_poc_ = std::numeric_limits<int>::min(); | |
63 | |
64 // If we are in kDecoding, we can resume without processing an SPS. | |
65 if (state_ == kDecoding) | |
66 state_ = kAfterReset; | |
67 } | |
68 | |
69 void H264Decoder::PrepareRefPicLists(const media::H264SliceHeader* slice_hdr) { | |
70 ConstructReferencePicListsP(slice_hdr); | |
71 ConstructReferencePicListsB(slice_hdr); | |
72 } | |
73 | |
74 bool H264Decoder::ModifyReferencePicLists( | |
75 const media::H264SliceHeader* slice_hdr, | |
76 H264Picture::Vector* ref_pic_list0, | |
77 H264Picture::Vector* ref_pic_list1) { | |
78 ref_pic_list0->clear(); | |
79 ref_pic_list1->clear(); | |
80 | |
81 // Fill reference picture lists for B and S/SP slices. | |
82 if (slice_hdr->IsPSlice() || slice_hdr->IsSPSlice()) { | |
83 *ref_pic_list0 = ref_pic_list_p0_; | |
84 return ModifyReferencePicList(slice_hdr, 0, ref_pic_list0); | |
85 } else if (slice_hdr->IsBSlice()) { | |
86 *ref_pic_list0 = ref_pic_list_b0_; | |
87 *ref_pic_list1 = ref_pic_list_b1_; | |
88 return ModifyReferencePicList(slice_hdr, 0, ref_pic_list0) && | |
89 ModifyReferencePicList(slice_hdr, 1, ref_pic_list1); | |
90 } | |
91 | |
92 return true; | |
93 } | |
94 | |
95 bool H264Decoder::DecodePicture() { | |
96 DCHECK(curr_pic_.get()); | |
97 | |
98 DVLOG(4) << "Decoding POC " << curr_pic_->pic_order_cnt; | |
99 return accelerator_->SubmitDecode(curr_pic_); | |
100 } | |
101 | |
102 bool H264Decoder::InitNonexistingPicture(scoped_refptr<H264Picture> pic, | |
103 int frame_num) { | |
104 pic->nonexisting = true; | |
105 pic->nal_ref_idc = 1; | |
106 pic->frame_num = pic->pic_num = frame_num; | |
107 pic->adaptive_ref_pic_marking_mode_flag = false; | |
108 pic->ref = true; | |
109 pic->long_term_reference_flag = false; | |
110 pic->field = H264Picture::FIELD_NONE; | |
111 | |
112 return CalculatePicOrderCounts(pic); | |
113 } | |
114 | |
115 bool H264Decoder::InitCurrPicture(const media::H264SliceHeader* slice_hdr) { | |
116 DCHECK(curr_pic_.get()); | |
117 | |
118 curr_pic_->idr = slice_hdr->idr_pic_flag; | |
119 if (curr_pic_->idr) | |
120 curr_pic_->idr_pic_id = slice_hdr->idr_pic_id; | |
121 | |
122 if (slice_hdr->field_pic_flag) { | |
123 curr_pic_->field = slice_hdr->bottom_field_flag ? H264Picture::FIELD_BOTTOM | |
124 : H264Picture::FIELD_TOP; | |
125 } else { | |
126 curr_pic_->field = H264Picture::FIELD_NONE; | |
127 } | |
128 | |
129 if (curr_pic_->field != H264Picture::FIELD_NONE) { | |
130 DVLOG(1) << "Interlaced video not supported."; | |
131 return false; | |
132 } | |
133 | |
134 curr_pic_->nal_ref_idc = slice_hdr->nal_ref_idc; | |
135 curr_pic_->ref = slice_hdr->nal_ref_idc != 0; | |
136 // This assumes non-interlaced stream. | |
137 curr_pic_->frame_num = curr_pic_->pic_num = slice_hdr->frame_num; | |
138 | |
139 DCHECK_NE(curr_sps_id_, -1); | |
140 const media::H264SPS* sps = parser_.GetSPS(curr_sps_id_); | |
141 if (!sps) | |
142 return false; | |
143 | |
144 curr_pic_->pic_order_cnt_type = sps->pic_order_cnt_type; | |
145 switch (curr_pic_->pic_order_cnt_type) { | |
146 case 0: | |
147 curr_pic_->pic_order_cnt_lsb = slice_hdr->pic_order_cnt_lsb; | |
148 curr_pic_->delta_pic_order_cnt_bottom = | |
149 slice_hdr->delta_pic_order_cnt_bottom; | |
150 break; | |
151 | |
152 case 1: | |
153 curr_pic_->delta_pic_order_cnt0 = slice_hdr->delta_pic_order_cnt0; | |
154 curr_pic_->delta_pic_order_cnt1 = slice_hdr->delta_pic_order_cnt1; | |
155 break; | |
156 | |
157 case 2: | |
158 break; | |
159 | |
160 default: | |
161 NOTREACHED(); | |
162 return false; | |
163 } | |
164 | |
165 if (!CalculatePicOrderCounts(curr_pic_)) | |
166 return false; | |
167 | |
168 curr_pic_->long_term_reference_flag = slice_hdr->long_term_reference_flag; | |
169 curr_pic_->adaptive_ref_pic_marking_mode_flag = | |
170 slice_hdr->adaptive_ref_pic_marking_mode_flag; | |
171 | |
172 // If the slice header indicates we will have to perform reference marking | |
173 // process after this picture is decoded, store required data for that | |
174 // purpose. | |
175 if (slice_hdr->adaptive_ref_pic_marking_mode_flag) { | |
176 static_assert(sizeof(curr_pic_->ref_pic_marking) == | |
177 sizeof(slice_hdr->ref_pic_marking), | |
178 "Array sizes of ref pic marking do not match."); | |
179 memcpy(curr_pic_->ref_pic_marking, slice_hdr->ref_pic_marking, | |
180 sizeof(curr_pic_->ref_pic_marking)); | |
181 } | |
182 | |
183 return true; | |
184 } | |
185 | |
186 bool H264Decoder::CalculatePicOrderCounts(scoped_refptr<H264Picture> pic) { | |
187 const media::H264SPS* sps = parser_.GetSPS(curr_sps_id_); | |
188 if (!sps) | |
189 return false; | |
190 | |
191 switch (pic->pic_order_cnt_type) { | |
192 case 0: { | |
193 // See spec 8.2.1.1. | |
194 int prev_pic_order_cnt_msb, prev_pic_order_cnt_lsb; | |
195 | |
196 if (pic->idr) { | |
197 prev_pic_order_cnt_msb = prev_pic_order_cnt_lsb = 0; | |
198 } else { | |
199 if (prev_ref_has_memmgmnt5_) { | |
200 if (prev_ref_field_ != H264Picture::FIELD_BOTTOM) { | |
201 prev_pic_order_cnt_msb = 0; | |
202 prev_pic_order_cnt_lsb = prev_ref_top_field_order_cnt_; | |
203 } else { | |
204 prev_pic_order_cnt_msb = 0; | |
205 prev_pic_order_cnt_lsb = 0; | |
206 } | |
207 } else { | |
208 prev_pic_order_cnt_msb = prev_ref_pic_order_cnt_msb_; | |
209 prev_pic_order_cnt_lsb = prev_ref_pic_order_cnt_lsb_; | |
210 } | |
211 } | |
212 | |
213 int max_pic_order_cnt_lsb = | |
214 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4); | |
215 DCHECK_NE(max_pic_order_cnt_lsb, 0); | |
216 if ((pic->pic_order_cnt_lsb < prev_pic_order_cnt_lsb) && | |
217 (prev_pic_order_cnt_lsb - pic->pic_order_cnt_lsb >= | |
218 max_pic_order_cnt_lsb / 2)) { | |
219 pic->pic_order_cnt_msb = prev_pic_order_cnt_msb + max_pic_order_cnt_lsb; | |
220 } else if ((pic->pic_order_cnt_lsb > prev_pic_order_cnt_lsb) && | |
221 (pic->pic_order_cnt_lsb - prev_pic_order_cnt_lsb > | |
222 max_pic_order_cnt_lsb / 2)) { | |
223 pic->pic_order_cnt_msb = prev_pic_order_cnt_msb - max_pic_order_cnt_lsb; | |
224 } else { | |
225 pic->pic_order_cnt_msb = prev_pic_order_cnt_msb; | |
226 } | |
227 | |
228 if (pic->field != H264Picture::FIELD_BOTTOM) { | |
229 pic->top_field_order_cnt = | |
230 pic->pic_order_cnt_msb + pic->pic_order_cnt_lsb; | |
231 } | |
232 | |
233 if (pic->field != H264Picture::FIELD_TOP) { | |
234 if (pic->field == H264Picture::FIELD_NONE) { | |
235 pic->bottom_field_order_cnt = | |
236 pic->top_field_order_cnt + pic->delta_pic_order_cnt_bottom; | |
237 } else { | |
238 pic->bottom_field_order_cnt = | |
239 pic->pic_order_cnt_msb + pic->pic_order_cnt_lsb; | |
240 } | |
241 } | |
242 break; | |
243 } | |
244 | |
245 case 1: { | |
246 // See spec 8.2.1.2. | |
247 if (prev_has_memmgmnt5_) | |
248 prev_frame_num_offset_ = 0; | |
249 | |
250 if (pic->idr) | |
251 pic->frame_num_offset = 0; | |
252 else if (prev_frame_num_ > pic->frame_num) | |
253 pic->frame_num_offset = prev_frame_num_offset_ + max_frame_num_; | |
254 else | |
255 pic->frame_num_offset = prev_frame_num_offset_; | |
256 | |
257 int abs_frame_num = 0; | |
258 if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0) | |
259 abs_frame_num = pic->frame_num_offset + pic->frame_num; | |
260 else | |
261 abs_frame_num = 0; | |
262 | |
263 if (pic->nal_ref_idc == 0 && abs_frame_num > 0) | |
264 --abs_frame_num; | |
265 | |
266 int expected_pic_order_cnt = 0; | |
267 if (abs_frame_num > 0) { | |
268 if (sps->num_ref_frames_in_pic_order_cnt_cycle == 0) { | |
269 DVLOG(1) << "Invalid num_ref_frames_in_pic_order_cnt_cycle " | |
270 << "in stream"; | |
271 return false; | |
272 } | |
273 | |
274 int pic_order_cnt_cycle_cnt = (abs_frame_num - 1) / | |
275 sps->num_ref_frames_in_pic_order_cnt_cycle; | |
276 int frame_num_in_pic_order_cnt_cycle = (abs_frame_num - 1) % | |
277 sps->num_ref_frames_in_pic_order_cnt_cycle; | |
278 | |
279 expected_pic_order_cnt = pic_order_cnt_cycle_cnt * | |
280 sps->expected_delta_per_pic_order_cnt_cycle; | |
281 // frame_num_in_pic_order_cnt_cycle is verified < 255 in parser | |
282 for (int i = 0; i <= frame_num_in_pic_order_cnt_cycle; ++i) | |
283 expected_pic_order_cnt += sps->offset_for_ref_frame[i]; | |
284 } | |
285 | |
286 if (!pic->nal_ref_idc) | |
287 expected_pic_order_cnt += sps->offset_for_non_ref_pic; | |
288 | |
289 if (pic->field == H264Picture::FIELD_NONE) { | |
290 pic->top_field_order_cnt = | |
291 expected_pic_order_cnt + pic->delta_pic_order_cnt0; | |
292 pic->bottom_field_order_cnt = pic->top_field_order_cnt + | |
293 sps->offset_for_top_to_bottom_field + | |
294 pic->delta_pic_order_cnt1; | |
295 } else if (pic->field != H264Picture::FIELD_BOTTOM) { | |
296 pic->top_field_order_cnt = | |
297 expected_pic_order_cnt + pic->delta_pic_order_cnt0; | |
298 } else { | |
299 pic->bottom_field_order_cnt = expected_pic_order_cnt + | |
300 sps->offset_for_top_to_bottom_field + | |
301 pic->delta_pic_order_cnt0; | |
302 } | |
303 break; | |
304 } | |
305 | |
306 case 2: { | |
307 // See spec 8.2.1.3. | |
308 if (prev_has_memmgmnt5_) | |
309 prev_frame_num_offset_ = 0; | |
310 | |
311 if (pic->idr) | |
312 pic->frame_num_offset = 0; | |
313 else if (prev_frame_num_ > pic->frame_num) | |
314 pic->frame_num_offset = prev_frame_num_offset_ + max_frame_num_; | |
315 else | |
316 pic->frame_num_offset = prev_frame_num_offset_; | |
317 | |
318 int temp_pic_order_cnt; | |
319 if (pic->idr) { | |
320 temp_pic_order_cnt = 0; | |
321 } else if (!pic->nal_ref_idc) { | |
322 temp_pic_order_cnt = 2 * (pic->frame_num_offset + pic->frame_num) - 1; | |
323 } else { | |
324 temp_pic_order_cnt = 2 * (pic->frame_num_offset + pic->frame_num); | |
325 } | |
326 | |
327 if (pic->field == H264Picture::FIELD_NONE) { | |
328 pic->top_field_order_cnt = temp_pic_order_cnt; | |
329 pic->bottom_field_order_cnt = temp_pic_order_cnt; | |
330 } else if (pic->field == H264Picture::FIELD_BOTTOM) { | |
331 pic->bottom_field_order_cnt = temp_pic_order_cnt; | |
332 } else { | |
333 pic->top_field_order_cnt = temp_pic_order_cnt; | |
334 } | |
335 break; | |
336 } | |
337 | |
338 default: | |
339 DVLOG(1) << "Invalid pic_order_cnt_type: " << sps->pic_order_cnt_type; | |
340 return false; | |
341 } | |
342 | |
343 switch (pic->field) { | |
344 case H264Picture::FIELD_NONE: | |
345 pic->pic_order_cnt = | |
346 std::min(pic->top_field_order_cnt, pic->bottom_field_order_cnt); | |
347 break; | |
348 case H264Picture::FIELD_TOP: | |
349 pic->pic_order_cnt = pic->top_field_order_cnt; | |
350 break; | |
351 case H264Picture::FIELD_BOTTOM: | |
352 pic->pic_order_cnt = pic->bottom_field_order_cnt; | |
353 break; | |
354 } | |
355 | |
356 return true; | |
357 } | |
358 | |
359 void H264Decoder::UpdatePicNums(int frame_num) { | |
360 for (auto& pic : dpb_) { | |
361 if (!pic->ref) | |
362 continue; | |
363 | |
364 // 8.2.4.1. Assumes non-interlaced stream. | |
365 DCHECK_EQ(pic->field, H264Picture::FIELD_NONE); | |
366 if (pic->long_term) { | |
367 pic->long_term_pic_num = pic->long_term_frame_idx; | |
368 } else { | |
369 if (pic->frame_num > frame_num) | |
370 pic->frame_num_wrap = pic->frame_num - max_frame_num_; | |
371 else | |
372 pic->frame_num_wrap = pic->frame_num; | |
373 | |
374 pic->pic_num = pic->frame_num_wrap; | |
375 } | |
376 } | |
377 } | |
378 | |
379 struct PicNumDescCompare { | |
380 bool operator()(const scoped_refptr<H264Picture>& a, | |
381 const scoped_refptr<H264Picture>& b) const { | |
382 return a->pic_num > b->pic_num; | |
383 } | |
384 }; | |
385 | |
386 struct LongTermPicNumAscCompare { | |
387 bool operator()(const scoped_refptr<H264Picture>& a, | |
388 const scoped_refptr<H264Picture>& b) const { | |
389 return a->long_term_pic_num < b->long_term_pic_num; | |
390 } | |
391 }; | |
392 | |
393 void H264Decoder::ConstructReferencePicListsP( | |
394 const media::H264SliceHeader* slice_hdr) { | |
395 // RefPicList0 (8.2.4.2.1) [[1] [2]], where: | |
396 // [1] shortterm ref pics sorted by descending pic_num, | |
397 // [2] longterm ref pics by ascending long_term_pic_num. | |
398 ref_pic_list_p0_.clear(); | |
399 | |
400 // First get the short ref pics... | |
401 dpb_.GetShortTermRefPicsAppending(&ref_pic_list_p0_); | |
402 size_t num_short_refs = ref_pic_list_p0_.size(); | |
403 | |
404 // and sort them to get [1]. | |
405 std::sort(ref_pic_list_p0_.begin(), ref_pic_list_p0_.end(), | |
406 PicNumDescCompare()); | |
407 | |
408 // Now get long term pics and sort them by long_term_pic_num to get [2]. | |
409 dpb_.GetLongTermRefPicsAppending(&ref_pic_list_p0_); | |
410 std::sort(ref_pic_list_p0_.begin() + num_short_refs, ref_pic_list_p0_.end(), | |
411 LongTermPicNumAscCompare()); | |
412 } | |
413 | |
414 struct POCAscCompare { | |
415 bool operator()(const scoped_refptr<H264Picture>& a, | |
416 const scoped_refptr<H264Picture>& b) const { | |
417 return a->pic_order_cnt < b->pic_order_cnt; | |
418 } | |
419 }; | |
420 | |
421 struct POCDescCompare { | |
422 bool operator()(const scoped_refptr<H264Picture>& a, | |
423 const scoped_refptr<H264Picture>& b) const { | |
424 return a->pic_order_cnt > b->pic_order_cnt; | |
425 } | |
426 }; | |
427 | |
428 void H264Decoder::ConstructReferencePicListsB( | |
429 const media::H264SliceHeader* slice_hdr) { | |
430 // RefPicList0 (8.2.4.2.3) [[1] [2] [3]], where: | |
431 // [1] shortterm ref pics with POC < curr_pic's POC sorted by descending POC, | |
432 // [2] shortterm ref pics with POC > curr_pic's POC by ascending POC, | |
433 // [3] longterm ref pics by ascending long_term_pic_num. | |
434 ref_pic_list_b0_.clear(); | |
435 ref_pic_list_b1_.clear(); | |
436 dpb_.GetShortTermRefPicsAppending(&ref_pic_list_b0_); | |
437 size_t num_short_refs = ref_pic_list_b0_.size(); | |
438 | |
439 // First sort ascending, this will put [1] in right place and finish [2]. | |
440 std::sort(ref_pic_list_b0_.begin(), ref_pic_list_b0_.end(), POCAscCompare()); | |
441 | |
442 // Find first with POC > curr_pic's POC to get first element in [2]... | |
443 H264Picture::Vector::iterator iter; | |
444 iter = std::upper_bound(ref_pic_list_b0_.begin(), ref_pic_list_b0_.end(), | |
445 curr_pic_.get(), POCAscCompare()); | |
446 | |
447 // and sort [1] descending, thus finishing sequence [1] [2]. | |
448 std::sort(ref_pic_list_b0_.begin(), iter, POCDescCompare()); | |
449 | |
450 // Now add [3] and sort by ascending long_term_pic_num. | |
451 dpb_.GetLongTermRefPicsAppending(&ref_pic_list_b0_); | |
452 std::sort(ref_pic_list_b0_.begin() + num_short_refs, ref_pic_list_b0_.end(), | |
453 LongTermPicNumAscCompare()); | |
454 | |
455 // RefPicList1 (8.2.4.2.4) [[1] [2] [3]], where: | |
456 // [1] shortterm ref pics with POC > curr_pic's POC sorted by ascending POC, | |
457 // [2] shortterm ref pics with POC < curr_pic's POC by descending POC, | |
458 // [3] longterm ref pics by ascending long_term_pic_num. | |
459 | |
460 dpb_.GetShortTermRefPicsAppending(&ref_pic_list_b1_); | |
461 num_short_refs = ref_pic_list_b1_.size(); | |
462 | |
463 // First sort by descending POC. | |
464 std::sort(ref_pic_list_b1_.begin(), ref_pic_list_b1_.end(), POCDescCompare()); | |
465 | |
466 // Find first with POC < curr_pic's POC to get first element in [2]... | |
467 iter = std::upper_bound(ref_pic_list_b1_.begin(), ref_pic_list_b1_.end(), | |
468 curr_pic_.get(), POCDescCompare()); | |
469 | |
470 // and sort [1] ascending. | |
471 std::sort(ref_pic_list_b1_.begin(), iter, POCAscCompare()); | |
472 | |
473 // Now add [3] and sort by ascending long_term_pic_num | |
474 dpb_.GetShortTermRefPicsAppending(&ref_pic_list_b1_); | |
475 std::sort(ref_pic_list_b1_.begin() + num_short_refs, ref_pic_list_b1_.end(), | |
476 LongTermPicNumAscCompare()); | |
477 | |
478 // If lists identical, swap first two entries in RefPicList1 (spec 8.2.4.2.3) | |
479 if (ref_pic_list_b1_.size() > 1 && | |
480 std::equal(ref_pic_list_b0_.begin(), ref_pic_list_b0_.end(), | |
481 ref_pic_list_b1_.begin())) | |
482 std::swap(ref_pic_list_b1_[0], ref_pic_list_b1_[1]); | |
483 } | |
484 | |
485 // See 8.2.4 | |
486 int H264Decoder::PicNumF(const scoped_refptr<H264Picture>& pic) { | |
487 if (!pic) | |
488 return -1; | |
489 | |
490 if (!pic->long_term) | |
491 return pic->pic_num; | |
492 else | |
493 return max_pic_num_; | |
494 } | |
495 | |
496 // See 8.2.4 | |
497 int H264Decoder::LongTermPicNumF(const scoped_refptr<H264Picture>& pic) { | |
498 if (pic->ref && pic->long_term) | |
499 return pic->long_term_pic_num; | |
500 else | |
501 return 2 * (max_long_term_frame_idx_ + 1); | |
502 } | |
503 | |
504 // Shift elements on the |v| starting from |from| to |to|, inclusive, | |
505 // one position to the right and insert pic at |from|. | |
506 static void ShiftRightAndInsert(H264Picture::Vector* v, | |
507 int from, | |
508 int to, | |
509 const scoped_refptr<H264Picture>& pic) { | |
510 // Security checks, do not disable in Debug mode. | |
511 CHECK(from <= to); | |
512 CHECK(to <= std::numeric_limits<int>::max() - 2); | |
513 // Additional checks. Debug mode ok. | |
514 DCHECK(v); | |
515 DCHECK(pic); | |
516 DCHECK((to + 1 == static_cast<int>(v->size())) || | |
517 (to + 2 == static_cast<int>(v->size()))); | |
518 | |
519 v->resize(to + 2); | |
520 | |
521 for (int i = to + 1; i > from; --i) | |
522 (*v)[i] = (*v)[i - 1]; | |
523 | |
524 (*v)[from] = pic; | |
525 } | |
526 | |
527 bool H264Decoder::ModifyReferencePicList( | |
528 const media::H264SliceHeader* slice_hdr, | |
529 int list, | |
530 H264Picture::Vector* ref_pic_listx) { | |
531 bool ref_pic_list_modification_flag_lX; | |
532 int num_ref_idx_lX_active_minus1; | |
533 const media::H264ModificationOfPicNum* list_mod; | |
534 | |
535 // This can process either ref_pic_list0 or ref_pic_list1, depending on | |
536 // the list argument. Set up pointers to proper list to be processed here. | |
537 if (list == 0) { | |
538 ref_pic_list_modification_flag_lX = | |
539 slice_hdr->ref_pic_list_modification_flag_l0; | |
540 num_ref_idx_lX_active_minus1 = | |
541 slice_hdr->num_ref_idx_l0_active_minus1; | |
542 list_mod = slice_hdr->ref_list_l0_modifications; | |
543 } else { | |
544 ref_pic_list_modification_flag_lX = | |
545 slice_hdr->ref_pic_list_modification_flag_l1; | |
546 num_ref_idx_lX_active_minus1 = | |
547 slice_hdr->num_ref_idx_l1_active_minus1; | |
548 list_mod = slice_hdr->ref_list_l1_modifications; | |
549 } | |
550 | |
551 // Resize the list to the size requested in the slice header. | |
552 // Note that per 8.2.4.2 it's possible for num_ref_idx_lX_active_minus1 to | |
553 // indicate there should be more ref pics on list than we constructed. | |
554 // Those superfluous ones should be treated as non-reference and will be | |
555 // initialized to nullptr, which must be handled by clients. | |
556 DCHECK_GE(num_ref_idx_lX_active_minus1, 0); | |
557 ref_pic_listx->resize(num_ref_idx_lX_active_minus1 + 1); | |
558 | |
559 if (!ref_pic_list_modification_flag_lX) | |
560 return true; | |
561 | |
562 // Spec 8.2.4.3: | |
563 // Reorder pictures on the list in a way specified in the stream. | |
564 int pic_num_lx_pred = curr_pic_->pic_num; | |
565 int ref_idx_lx = 0; | |
566 int pic_num_lx_no_wrap; | |
567 int pic_num_lx; | |
568 bool done = false; | |
569 scoped_refptr<H264Picture> pic; | |
570 for (int i = 0; i < media::H264SliceHeader::kRefListModSize && !done; ++i) { | |
571 switch (list_mod->modification_of_pic_nums_idc) { | |
572 case 0: | |
573 case 1: | |
574 // Modify short reference picture position. | |
575 if (list_mod->modification_of_pic_nums_idc == 0) { | |
576 // Subtract given value from predicted PicNum. | |
577 pic_num_lx_no_wrap = pic_num_lx_pred - | |
578 (static_cast<int>(list_mod->abs_diff_pic_num_minus1) + 1); | |
579 // Wrap around max_pic_num_ if it becomes < 0 as result | |
580 // of subtraction. | |
581 if (pic_num_lx_no_wrap < 0) | |
582 pic_num_lx_no_wrap += max_pic_num_; | |
583 } else { | |
584 // Add given value to predicted PicNum. | |
585 pic_num_lx_no_wrap = pic_num_lx_pred + | |
586 (static_cast<int>(list_mod->abs_diff_pic_num_minus1) + 1); | |
587 // Wrap around max_pic_num_ if it becomes >= max_pic_num_ as result | |
588 // of the addition. | |
589 if (pic_num_lx_no_wrap >= max_pic_num_) | |
590 pic_num_lx_no_wrap -= max_pic_num_; | |
591 } | |
592 | |
593 // For use in next iteration. | |
594 pic_num_lx_pred = pic_num_lx_no_wrap; | |
595 | |
596 if (pic_num_lx_no_wrap > curr_pic_->pic_num) | |
597 pic_num_lx = pic_num_lx_no_wrap - max_pic_num_; | |
598 else | |
599 pic_num_lx = pic_num_lx_no_wrap; | |
600 | |
601 DCHECK_LT(num_ref_idx_lX_active_minus1 + 1, | |
602 media::H264SliceHeader::kRefListModSize); | |
603 pic = dpb_.GetShortRefPicByPicNum(pic_num_lx); | |
604 if (!pic) { | |
605 DVLOG(1) << "Malformed stream, no pic num " << pic_num_lx; | |
606 return false; | |
607 } | |
608 ShiftRightAndInsert(ref_pic_listx, ref_idx_lx, | |
609 num_ref_idx_lX_active_minus1, pic); | |
610 ref_idx_lx++; | |
611 | |
612 for (int src = ref_idx_lx, dst = ref_idx_lx; | |
613 src <= num_ref_idx_lX_active_minus1 + 1; ++src) { | |
614 if (PicNumF((*ref_pic_listx)[src]) != pic_num_lx) | |
615 (*ref_pic_listx)[dst++] = (*ref_pic_listx)[src]; | |
616 } | |
617 break; | |
618 | |
619 case 2: | |
620 // Modify long term reference picture position. | |
621 DCHECK_LT(num_ref_idx_lX_active_minus1 + 1, | |
622 media::H264SliceHeader::kRefListModSize); | |
623 pic = dpb_.GetLongRefPicByLongTermPicNum(list_mod->long_term_pic_num); | |
624 if (!pic) { | |
625 DVLOG(1) << "Malformed stream, no pic num " | |
626 << list_mod->long_term_pic_num; | |
627 return false; | |
628 } | |
629 ShiftRightAndInsert(ref_pic_listx, ref_idx_lx, | |
630 num_ref_idx_lX_active_minus1, pic); | |
631 ref_idx_lx++; | |
632 | |
633 for (int src = ref_idx_lx, dst = ref_idx_lx; | |
634 src <= num_ref_idx_lX_active_minus1 + 1; ++src) { | |
635 if (LongTermPicNumF((*ref_pic_listx)[src]) != | |
636 static_cast<int>(list_mod->long_term_pic_num)) | |
637 (*ref_pic_listx)[dst++] = (*ref_pic_listx)[src]; | |
638 } | |
639 break; | |
640 | |
641 case 3: | |
642 // End of modification list. | |
643 done = true; | |
644 break; | |
645 | |
646 default: | |
647 // May be recoverable. | |
648 DVLOG(1) << "Invalid modification_of_pic_nums_idc=" | |
649 << list_mod->modification_of_pic_nums_idc | |
650 << " in position " << i; | |
651 break; | |
652 } | |
653 | |
654 ++list_mod; | |
655 } | |
656 | |
657 // Per NOTE 2 in 8.2.4.3.2, the ref_pic_listx size in the above loop is | |
658 // temporarily made one element longer than the required final list. | |
659 // Resize the list back to its required size. | |
660 ref_pic_listx->resize(num_ref_idx_lX_active_minus1 + 1); | |
661 | |
662 return true; | |
663 } | |
664 | |
665 void H264Decoder::OutputPic(scoped_refptr<H264Picture> pic) { | |
666 DCHECK(!pic->outputted); | |
667 pic->outputted = true; | |
668 | |
669 if (pic->nonexisting) { | |
670 DVLOG(4) << "Skipping output, non-existing frame_num: " << pic->frame_num; | |
671 return; | |
672 } | |
673 | |
674 DVLOG_IF(1, pic->pic_order_cnt < last_output_poc_) | |
675 << "Outputting out of order, likely a broken stream: " | |
676 << last_output_poc_ << " -> " << pic->pic_order_cnt; | |
677 last_output_poc_ = pic->pic_order_cnt; | |
678 | |
679 DVLOG(4) << "Posting output task for POC: " << pic->pic_order_cnt; | |
680 accelerator_->OutputPicture(pic); | |
681 } | |
682 | |
683 void H264Decoder::ClearDPB() { | |
684 // Clear DPB contents, marking the pictures as unused first. | |
685 dpb_.Clear(); | |
686 last_output_poc_ = std::numeric_limits<int>::min(); | |
687 } | |
688 | |
689 bool H264Decoder::OutputAllRemainingPics() { | |
690 // Output all pictures that are waiting to be outputted. | |
691 FinishPrevFrameIfPresent(); | |
692 H264Picture::Vector to_output; | |
693 dpb_.GetNotOutputtedPicsAppending(&to_output); | |
694 // Sort them by ascending POC to output in order. | |
695 std::sort(to_output.begin(), to_output.end(), POCAscCompare()); | |
696 | |
697 for (auto& pic : to_output) | |
698 OutputPic(pic); | |
699 | |
700 return true; | |
701 } | |
702 | |
703 bool H264Decoder::Flush() { | |
704 DVLOG(2) << "Decoder flush"; | |
705 | |
706 if (!OutputAllRemainingPics()) | |
707 return false; | |
708 | |
709 ClearDPB(); | |
710 DVLOG(2) << "Decoder flush finished"; | |
711 return true; | |
712 } | |
713 | |
714 bool H264Decoder::StartNewFrame(const media::H264SliceHeader* slice_hdr) { | |
715 // TODO posciak: add handling of max_num_ref_frames per spec. | |
716 CHECK(curr_pic_.get()); | |
717 DCHECK(slice_hdr); | |
718 | |
719 curr_pps_id_ = slice_hdr->pic_parameter_set_id; | |
720 const media::H264PPS* pps = parser_.GetPPS(curr_pps_id_); | |
721 if (!pps) | |
722 return false; | |
723 | |
724 curr_sps_id_ = pps->seq_parameter_set_id; | |
725 const media::H264SPS* sps = parser_.GetSPS(curr_sps_id_); | |
726 if (!sps) | |
727 return false; | |
728 | |
729 max_frame_num_ = 1 << (sps->log2_max_frame_num_minus4 + 4); | |
730 int frame_num = slice_hdr->frame_num; | |
731 if (slice_hdr->idr_pic_flag) | |
732 prev_ref_frame_num_ = 0; | |
733 | |
734 // 7.4.3 | |
735 if (frame_num != prev_ref_frame_num_ && | |
736 frame_num != (prev_ref_frame_num_ + 1) % max_frame_num_) { | |
737 if (!HandleFrameNumGap(frame_num)) | |
738 return false; | |
739 } | |
740 | |
741 if (!InitCurrPicture(slice_hdr)) | |
742 return false; | |
743 | |
744 UpdatePicNums(frame_num); | |
745 PrepareRefPicLists(slice_hdr); | |
746 | |
747 if (!accelerator_->SubmitFrameMetadata(sps, pps, dpb_, ref_pic_list_p0_, | |
748 ref_pic_list_b0_, ref_pic_list_b1_, | |
749 curr_pic_.get())) | |
750 return false; | |
751 | |
752 return true; | |
753 } | |
754 | |
755 bool H264Decoder::HandleMemoryManagementOps(scoped_refptr<H264Picture> pic) { | |
756 // 8.2.5.4 | |
757 for (size_t i = 0; i < arraysize(pic->ref_pic_marking); ++i) { | |
758 // Code below does not support interlaced stream (per-field pictures). | |
759 media::H264DecRefPicMarking* ref_pic_marking = &pic->ref_pic_marking[i]; | |
760 scoped_refptr<H264Picture> to_mark; | |
761 int pic_num_x; | |
762 | |
763 switch (ref_pic_marking->memory_mgmnt_control_operation) { | |
764 case 0: | |
765 // Normal end of operations' specification. | |
766 return true; | |
767 | |
768 case 1: | |
769 // Mark a short term reference picture as unused so it can be removed | |
770 // if outputted. | |
771 pic_num_x = | |
772 pic->pic_num - (ref_pic_marking->difference_of_pic_nums_minus1 + 1); | |
773 to_mark = dpb_.GetShortRefPicByPicNum(pic_num_x); | |
774 if (to_mark) { | |
775 to_mark->ref = false; | |
776 } else { | |
777 DVLOG(1) << "Invalid short ref pic num to unmark"; | |
778 return false; | |
779 } | |
780 break; | |
781 | |
782 case 2: | |
783 // Mark a long term reference picture as unused so it can be removed | |
784 // if outputted. | |
785 to_mark = dpb_.GetLongRefPicByLongTermPicNum( | |
786 ref_pic_marking->long_term_pic_num); | |
787 if (to_mark) { | |
788 to_mark->ref = false; | |
789 } else { | |
790 DVLOG(1) << "Invalid long term ref pic num to unmark"; | |
791 return false; | |
792 } | |
793 break; | |
794 | |
795 case 3: | |
796 // Mark a short term reference picture as long term reference. | |
797 pic_num_x = | |
798 pic->pic_num - (ref_pic_marking->difference_of_pic_nums_minus1 + 1); | |
799 to_mark = dpb_.GetShortRefPicByPicNum(pic_num_x); | |
800 if (to_mark) { | |
801 DCHECK(to_mark->ref && !to_mark->long_term); | |
802 to_mark->long_term = true; | |
803 to_mark->long_term_frame_idx = ref_pic_marking->long_term_frame_idx; | |
804 } else { | |
805 DVLOG(1) << "Invalid short term ref pic num to mark as long ref"; | |
806 return false; | |
807 } | |
808 break; | |
809 | |
810 case 4: { | |
811 // Unmark all reference pictures with long_term_frame_idx over new max. | |
812 max_long_term_frame_idx_ = | |
813 ref_pic_marking->max_long_term_frame_idx_plus1 - 1; | |
814 H264Picture::Vector long_terms; | |
815 dpb_.GetLongTermRefPicsAppending(&long_terms); | |
816 for (size_t i = 0; i < long_terms.size(); ++i) { | |
817 scoped_refptr<H264Picture>& long_term_pic = long_terms[i]; | |
818 DCHECK(long_term_pic->ref && long_term_pic->long_term); | |
819 // Ok to cast, max_long_term_frame_idx is much smaller than 16bit. | |
820 if (long_term_pic->long_term_frame_idx > | |
821 static_cast<int>(max_long_term_frame_idx_)) | |
822 long_term_pic->ref = false; | |
823 } | |
824 break; | |
825 } | |
826 | |
827 case 5: | |
828 // Unmark all reference pictures. | |
829 dpb_.MarkAllUnusedForRef(); | |
830 max_long_term_frame_idx_ = -1; | |
831 pic->mem_mgmt_5 = true; | |
832 break; | |
833 | |
834 case 6: { | |
835 // Replace long term reference pictures with current picture. | |
836 // First unmark if any existing with this long_term_frame_idx... | |
837 H264Picture::Vector long_terms; | |
838 dpb_.GetLongTermRefPicsAppending(&long_terms); | |
839 for (size_t i = 0; i < long_terms.size(); ++i) { | |
840 scoped_refptr<H264Picture>& long_term_pic = long_terms[i]; | |
841 DCHECK(long_term_pic->ref && long_term_pic->long_term); | |
842 // Ok to cast, long_term_frame_idx is much smaller than 16bit. | |
843 if (long_term_pic->long_term_frame_idx == | |
844 static_cast<int>(ref_pic_marking->long_term_frame_idx)) | |
845 long_term_pic->ref = false; | |
846 } | |
847 | |
848 // and mark the current one instead. | |
849 pic->ref = true; | |
850 pic->long_term = true; | |
851 pic->long_term_frame_idx = ref_pic_marking->long_term_frame_idx; | |
852 break; | |
853 } | |
854 | |
855 default: | |
856 // Would indicate a bug in parser. | |
857 NOTREACHED(); | |
858 } | |
859 } | |
860 | |
861 return true; | |
862 } | |
863 | |
864 // This method ensures that DPB does not overflow, either by removing | |
865 // reference pictures as specified in the stream, or using a sliding window | |
866 // procedure to remove the oldest one. | |
867 // It also performs marking and unmarking pictures as reference. | |
868 // See spac 8.2.5.1. | |
869 bool H264Decoder::ReferencePictureMarking(scoped_refptr<H264Picture> pic) { | |
870 // If the current picture is an IDR, all reference pictures are unmarked. | |
871 if (pic->idr) { | |
872 dpb_.MarkAllUnusedForRef(); | |
873 | |
874 if (pic->long_term_reference_flag) { | |
875 pic->long_term = true; | |
876 pic->long_term_frame_idx = 0; | |
877 max_long_term_frame_idx_ = 0; | |
878 } else { | |
879 pic->long_term = false; | |
880 max_long_term_frame_idx_ = -1; | |
881 } | |
882 | |
883 return true; | |
884 } | |
885 | |
886 // Not an IDR. If the stream contains instructions on how to discard pictures | |
887 // from DPB and how to mark/unmark existing reference pictures, do so. | |
888 // Otherwise, fall back to default sliding window process. | |
889 if (pic->adaptive_ref_pic_marking_mode_flag) { | |
890 DCHECK(!pic->nonexisting); | |
891 return HandleMemoryManagementOps(pic); | |
892 } else { | |
893 return SlidingWindowPictureMarking(); | |
894 } | |
895 } | |
896 | |
897 bool H264Decoder::SlidingWindowPictureMarking() { | |
898 const media::H264SPS* sps = parser_.GetSPS(curr_sps_id_); | |
899 if (!sps) | |
900 return false; | |
901 | |
902 // 8.2.5.3. Ensure the DPB doesn't overflow by discarding the oldest picture. | |
903 int num_ref_pics = dpb_.CountRefPics(); | |
904 DCHECK_LE(num_ref_pics, std::max<int>(sps->max_num_ref_frames, 1)); | |
905 if (num_ref_pics == std::max<int>(sps->max_num_ref_frames, 1)) { | |
906 // Max number of reference pics reached, need to remove one of the short | |
907 // term ones. Find smallest frame_num_wrap short reference picture and mark | |
908 // it as unused. | |
909 scoped_refptr<H264Picture> to_unmark = | |
910 dpb_.GetLowestFrameNumWrapShortRefPic(); | |
911 if (!to_unmark) { | |
912 DVLOG(1) << "Couldn't find a short ref picture to unmark"; | |
913 return false; | |
914 } | |
915 | |
916 to_unmark->ref = false; | |
917 } | |
918 | |
919 return true; | |
920 } | |
921 | |
922 bool H264Decoder::FinishPicture(scoped_refptr<H264Picture> pic) { | |
923 // Finish processing the picture. | |
924 // Start by storing previous picture data for later use. | |
925 if (pic->ref) { | |
926 ReferencePictureMarking(pic); | |
927 prev_ref_has_memmgmnt5_ = pic->mem_mgmt_5; | |
928 prev_ref_top_field_order_cnt_ = pic->top_field_order_cnt; | |
929 prev_ref_pic_order_cnt_msb_ = pic->pic_order_cnt_msb; | |
930 prev_ref_pic_order_cnt_lsb_ = pic->pic_order_cnt_lsb; | |
931 prev_ref_field_ = pic->field; | |
932 prev_ref_frame_num_ = pic->frame_num; | |
933 } | |
934 prev_frame_num_ = pic->frame_num; | |
935 prev_has_memmgmnt5_ = pic->mem_mgmt_5; | |
936 prev_frame_num_offset_ = pic->frame_num_offset; | |
937 | |
938 // Remove unused (for reference or later output) pictures from DPB, marking | |
939 // them as such. | |
940 dpb_.DeleteUnused(); | |
941 | |
942 DVLOG(4) << "Finishing picture frame_num: " << pic->frame_num | |
943 << ", entries in DPB: " << dpb_.size(); | |
944 | |
945 // The ownership of pic will either be transferred to DPB - if the picture is | |
946 // still needed (for output and/or reference) - or we will release it | |
947 // immediately if we manage to output it here and won't have to store it for | |
948 // future reference. | |
949 | |
950 // Get all pictures that haven't been outputted yet. | |
951 H264Picture::Vector not_outputted; | |
952 dpb_.GetNotOutputtedPicsAppending(¬_outputted); | |
953 // Include the one we've just decoded. | |
954 not_outputted.push_back(pic); | |
955 | |
956 // Sort in output order. | |
957 std::sort(not_outputted.begin(), not_outputted.end(), POCAscCompare()); | |
958 | |
959 // Try to output as many pictures as we can. A picture can be output, | |
960 // if the number of decoded and not yet outputted pictures that would remain | |
961 // in DPB afterwards would at least be equal to max_num_reorder_frames. | |
962 // If the outputted picture is not a reference picture, it doesn't have | |
963 // to remain in the DPB and can be removed. | |
964 H264Picture::Vector::iterator output_candidate = not_outputted.begin(); | |
965 size_t num_remaining = not_outputted.size(); | |
966 while (num_remaining > max_num_reorder_frames_ || | |
967 // If the condition below is used, this is an invalid stream. We should | |
968 // not be forced to output beyond max_num_reorder_frames in order to | |
969 // make room in DPB to store the current picture (if we need to do so). | |
970 // However, if this happens, ignore max_num_reorder_frames and try | |
971 // to output more. This may cause out-of-order output, but is not | |
972 // fatal, and better than failing instead. | |
973 ((dpb_.IsFull() && (!pic->outputted || pic->ref)) && num_remaining)) { | |
974 DVLOG_IF(1, num_remaining <= max_num_reorder_frames_) | |
975 << "Invalid stream: max_num_reorder_frames not preserved"; | |
976 | |
977 OutputPic(*output_candidate); | |
978 | |
979 if (!(*output_candidate)->ref) { | |
980 // Current picture hasn't been inserted into DPB yet, so don't remove it | |
981 // if we managed to output it immediately. | |
982 int outputted_poc = (*output_candidate)->pic_order_cnt; | |
983 if (outputted_poc != pic->pic_order_cnt) | |
984 dpb_.DeleteByPOC(outputted_poc); | |
985 } | |
986 | |
987 ++output_candidate; | |
988 --num_remaining; | |
989 } | |
990 | |
991 // If we haven't managed to output the picture that we just decoded, or if | |
992 // it's a reference picture, we have to store it in DPB. | |
993 if (!pic->outputted || pic->ref) { | |
994 if (dpb_.IsFull()) { | |
995 // If we haven't managed to output anything to free up space in DPB | |
996 // to store this picture, it's an error in the stream. | |
997 DVLOG(1) << "Could not free up space in DPB!"; | |
998 return false; | |
999 } | |
1000 | |
1001 dpb_.StorePic(pic); | |
1002 } | |
1003 | |
1004 return true; | |
1005 } | |
1006 | |
1007 static int LevelToMaxDpbMbs(int level) { | |
1008 // See table A-1 in spec. | |
1009 switch (level) { | |
1010 case 10: return 396; | |
1011 case 11: return 900; | |
1012 case 12: // fallthrough | |
1013 case 13: // fallthrough | |
1014 case 20: return 2376; | |
1015 case 21: return 4752; | |
1016 case 22: // fallthrough | |
1017 case 30: return 8100; | |
1018 case 31: return 18000; | |
1019 case 32: return 20480; | |
1020 case 40: // fallthrough | |
1021 case 41: return 32768; | |
1022 case 42: return 34816; | |
1023 case 50: return 110400; | |
1024 case 51: // fallthrough | |
1025 case 52: return 184320; | |
1026 default: | |
1027 DVLOG(1) << "Invalid codec level (" << level << ")"; | |
1028 return 0; | |
1029 } | |
1030 } | |
1031 | |
1032 bool H264Decoder::UpdateMaxNumReorderFrames(const media::H264SPS* sps) { | |
1033 if (sps->vui_parameters_present_flag && sps->bitstream_restriction_flag) { | |
1034 max_num_reorder_frames_ = | |
1035 base::checked_cast<size_t>(sps->max_num_reorder_frames); | |
1036 if (max_num_reorder_frames_ > dpb_.max_num_pics()) { | |
1037 DVLOG(1) | |
1038 << "max_num_reorder_frames present, but larger than MaxDpbFrames (" | |
1039 << max_num_reorder_frames_ << " > " << dpb_.max_num_pics() << ")"; | |
1040 max_num_reorder_frames_ = 0; | |
1041 return false; | |
1042 } | |
1043 return true; | |
1044 } | |
1045 | |
1046 // max_num_reorder_frames not present, infer from profile/constraints | |
1047 // (see VUI semantics in spec). | |
1048 if (sps->constraint_set3_flag) { | |
1049 switch (sps->profile_idc) { | |
1050 case 44: | |
1051 case 86: | |
1052 case 100: | |
1053 case 110: | |
1054 case 122: | |
1055 case 244: | |
1056 max_num_reorder_frames_ = 0; | |
1057 break; | |
1058 default: | |
1059 max_num_reorder_frames_ = dpb_.max_num_pics(); | |
1060 break; | |
1061 } | |
1062 } else { | |
1063 max_num_reorder_frames_ = dpb_.max_num_pics(); | |
1064 } | |
1065 | |
1066 return true; | |
1067 } | |
1068 | |
1069 bool H264Decoder::ProcessSPS(int sps_id, bool* need_new_buffers) { | |
1070 DVLOG(4) << "Processing SPS id:" << sps_id; | |
1071 | |
1072 const media::H264SPS* sps = parser_.GetSPS(sps_id); | |
1073 if (!sps) | |
1074 return false; | |
1075 | |
1076 *need_new_buffers = false; | |
1077 | |
1078 if (sps->frame_mbs_only_flag == 0) { | |
1079 DVLOG(1) << "frame_mbs_only_flag != 1 not supported"; | |
1080 return false; | |
1081 } | |
1082 | |
1083 // Calculate picture height/width in macroblocks and pixels | |
1084 // (spec 7.4.2.1.1, 7.4.3). | |
1085 int width_mb = sps->pic_width_in_mbs_minus1 + 1; | |
1086 int height_mb = (2 - sps->frame_mbs_only_flag) * | |
1087 (sps->pic_height_in_map_units_minus1 + 1); | |
1088 | |
1089 if (width_mb > std::numeric_limits<int>::max() / 16 || | |
1090 height_mb > std::numeric_limits<int>::max() / 16) { | |
1091 DVLOG(1) << "Picture size is too big: width_mb=" << width_mb | |
1092 << " height_mb=" << height_mb; | |
1093 return false; | |
1094 } | |
1095 | |
1096 gfx::Size new_pic_size(16 * width_mb, 16 * height_mb); | |
1097 if (new_pic_size.IsEmpty()) { | |
1098 DVLOG(1) << "Invalid picture size: " << new_pic_size.ToString(); | |
1099 return false; | |
1100 } | |
1101 | |
1102 if (!pic_size_.IsEmpty() && new_pic_size == pic_size_) { | |
1103 // Already have surfaces and this SPS keeps the same resolution, | |
1104 // no need to request a new set. | |
1105 return true; | |
1106 } | |
1107 | |
1108 pic_size_ = new_pic_size; | |
1109 DVLOG(1) << "New picture size: " << pic_size_.ToString(); | |
1110 | |
1111 int level = sps->level_idc; | |
1112 int max_dpb_mbs = LevelToMaxDpbMbs(level); | |
1113 if (max_dpb_mbs == 0) | |
1114 return false; | |
1115 | |
1116 size_t max_dpb_size = std::min(max_dpb_mbs / (width_mb * height_mb), | |
1117 static_cast<int>(H264DPB::kDPBMaxSize)); | |
1118 DVLOG(1) << "Codec level: " << level << ", DPB size: " << max_dpb_size; | |
1119 if (max_dpb_size == 0) { | |
1120 DVLOG(1) << "Invalid DPB Size"; | |
1121 return false; | |
1122 } | |
1123 | |
1124 dpb_.set_max_num_pics(max_dpb_size); | |
1125 | |
1126 if (!UpdateMaxNumReorderFrames(sps)) | |
1127 return false; | |
1128 DVLOG(1) << "max_num_reorder_frames: " << max_num_reorder_frames_; | |
1129 | |
1130 *need_new_buffers = true; | |
1131 return true; | |
1132 } | |
1133 | |
1134 bool H264Decoder::FinishPrevFrameIfPresent() { | |
1135 // If we already have a frame waiting to be decoded, decode it and finish. | |
1136 if (curr_pic_ != NULL) { | |
1137 if (!DecodePicture()) | |
1138 return false; | |
1139 | |
1140 scoped_refptr<H264Picture> pic = curr_pic_; | |
1141 curr_pic_ = nullptr; | |
1142 return FinishPicture(pic); | |
1143 } | |
1144 | |
1145 return true; | |
1146 } | |
1147 | |
1148 bool H264Decoder::HandleFrameNumGap(int frame_num) { | |
1149 const media::H264SPS* sps = parser_.GetSPS(curr_sps_id_); | |
1150 if (!sps) | |
1151 return false; | |
1152 | |
1153 if (!sps->gaps_in_frame_num_value_allowed_flag) { | |
1154 DVLOG(1) << "Invalid frame_num: " << frame_num; | |
1155 return false; | |
1156 } | |
1157 | |
1158 DVLOG(2) << "Handling frame_num gap: " << prev_ref_frame_num_ << "->" | |
1159 << frame_num; | |
1160 | |
1161 // 7.4.3/7-23 | |
1162 int unused_short_term_frame_num = (prev_ref_frame_num_ + 1) % max_frame_num_; | |
1163 while (unused_short_term_frame_num != frame_num) { | |
1164 scoped_refptr<H264Picture> pic = new H264Picture(); | |
1165 if (!InitNonexistingPicture(pic, unused_short_term_frame_num)) | |
1166 return false; | |
1167 | |
1168 UpdatePicNums(unused_short_term_frame_num); | |
1169 | |
1170 if (!FinishPicture(pic)) | |
1171 return false; | |
1172 | |
1173 unused_short_term_frame_num++; | |
1174 unused_short_term_frame_num %= max_frame_num_; | |
1175 } | |
1176 | |
1177 return true; | |
1178 } | |
1179 | |
1180 bool H264Decoder::IsNewPrimaryCodedPicture( | |
1181 const media::H264SliceHeader* slice_hdr) const { | |
1182 if (!curr_pic_) | |
1183 return true; | |
1184 | |
1185 // 7.4.1.2.4, assumes non-interlaced. | |
1186 if (slice_hdr->frame_num != curr_pic_->frame_num || | |
1187 slice_hdr->pic_parameter_set_id != curr_pps_id_ || | |
1188 slice_hdr->nal_ref_idc != curr_pic_->nal_ref_idc || | |
1189 slice_hdr->idr_pic_flag != curr_pic_->idr || | |
1190 (slice_hdr->idr_pic_flag && | |
1191 slice_hdr->idr_pic_id != curr_pic_->idr_pic_id)) | |
1192 return true; | |
1193 | |
1194 const media::H264SPS* sps = parser_.GetSPS(curr_sps_id_); | |
1195 if (!sps) | |
1196 return false; | |
1197 | |
1198 if (sps->pic_order_cnt_type == curr_pic_->pic_order_cnt_type) { | |
1199 if (curr_pic_->pic_order_cnt_type == 0) { | |
1200 if (slice_hdr->pic_order_cnt_lsb != curr_pic_->pic_order_cnt_lsb || | |
1201 slice_hdr->delta_pic_order_cnt_bottom != | |
1202 curr_pic_->delta_pic_order_cnt_bottom) | |
1203 return true; | |
1204 } else if (curr_pic_->pic_order_cnt_type == 1) { | |
1205 if (slice_hdr->delta_pic_order_cnt0 != curr_pic_->delta_pic_order_cnt0 || | |
1206 slice_hdr->delta_pic_order_cnt1 != curr_pic_->delta_pic_order_cnt1) | |
1207 return true; | |
1208 } | |
1209 } | |
1210 | |
1211 return false; | |
1212 } | |
1213 | |
1214 bool H264Decoder::PreprocessCurrentSlice() { | |
1215 const media::H264SliceHeader* slice_hdr = curr_slice_hdr_.get(); | |
1216 DCHECK(slice_hdr); | |
1217 | |
1218 if (IsNewPrimaryCodedPicture(slice_hdr)) { | |
1219 // New picture, so first finish the previous one before processing it. | |
1220 if (!FinishPrevFrameIfPresent()) | |
1221 return false; | |
1222 | |
1223 DCHECK(!curr_pic_); | |
1224 | |
1225 if (slice_hdr->first_mb_in_slice != 0) { | |
1226 DVLOG(1) << "ASO/invalid stream, first_mb_in_slice: " | |
1227 << slice_hdr->first_mb_in_slice; | |
1228 return false; | |
1229 } | |
1230 | |
1231 // If the new picture is an IDR, flush DPB. | |
1232 if (slice_hdr->idr_pic_flag) { | |
1233 // Output all remaining pictures, unless we are explicitly instructed | |
1234 // not to do so. | |
1235 if (!slice_hdr->no_output_of_prior_pics_flag) { | |
1236 if (!Flush()) | |
1237 return false; | |
1238 } | |
1239 dpb_.Clear(); | |
1240 last_output_poc_ = std::numeric_limits<int>::min(); | |
1241 } | |
1242 } | |
1243 | |
1244 return true; | |
1245 } | |
1246 | |
1247 bool H264Decoder::ProcessCurrentSlice() { | |
1248 DCHECK(curr_pic_); | |
1249 | |
1250 const media::H264SliceHeader* slice_hdr = curr_slice_hdr_.get(); | |
1251 DCHECK(slice_hdr); | |
1252 | |
1253 if (slice_hdr->field_pic_flag == 0) | |
1254 max_pic_num_ = max_frame_num_; | |
1255 else | |
1256 max_pic_num_ = 2 * max_frame_num_; | |
1257 | |
1258 H264Picture::Vector ref_pic_list0, ref_pic_list1; | |
1259 if (!ModifyReferencePicLists(slice_hdr, &ref_pic_list0, &ref_pic_list1)) | |
1260 return false; | |
1261 | |
1262 const media::H264PPS* pps = parser_.GetPPS(curr_pps_id_); | |
1263 if (!pps) | |
1264 return false; | |
1265 | |
1266 if (!accelerator_->SubmitSlice(pps, slice_hdr, ref_pic_list0, ref_pic_list1, | |
1267 curr_pic_.get(), slice_hdr->nalu_data, | |
1268 slice_hdr->nalu_size)) | |
1269 return false; | |
1270 | |
1271 return true; | |
1272 } | |
1273 | |
1274 #define SET_ERROR_AND_RETURN() \ | |
1275 do { \ | |
1276 DVLOG(1) << "Error during decode"; \ | |
1277 state_ = kError; \ | |
1278 return H264Decoder::kDecodeError; \ | |
1279 } while (0) | |
1280 | |
1281 void H264Decoder::SetStream(const uint8_t* ptr, size_t size) { | |
1282 DCHECK(ptr); | |
1283 DCHECK(size); | |
1284 | |
1285 DVLOG(4) << "New input stream at: " << (void*)ptr << " size: " << size; | |
1286 parser_.SetStream(ptr, size); | |
1287 } | |
1288 | |
1289 H264Decoder::DecodeResult H264Decoder::Decode() { | |
1290 if (state_ == kError) { | |
1291 DVLOG(1) << "Decoder in error state"; | |
1292 return kDecodeError; | |
1293 } | |
1294 | |
1295 while (1) { | |
1296 media::H264Parser::Result par_res; | |
1297 | |
1298 if (!curr_nalu_) { | |
1299 curr_nalu_.reset(new media::H264NALU()); | |
1300 par_res = parser_.AdvanceToNextNALU(curr_nalu_.get()); | |
1301 if (par_res == media::H264Parser::kEOStream) | |
1302 return kRanOutOfStreamData; | |
1303 else if (par_res != media::H264Parser::kOk) | |
1304 SET_ERROR_AND_RETURN(); | |
1305 | |
1306 DVLOG(4) << "New NALU: " << static_cast<int>(curr_nalu_->nal_unit_type); | |
1307 } | |
1308 | |
1309 switch (curr_nalu_->nal_unit_type) { | |
1310 case media::H264NALU::kNonIDRSlice: | |
1311 // We can't resume from a non-IDR slice. | |
1312 if (state_ != kDecoding) | |
1313 break; | |
1314 // else fallthrough | |
1315 case media::H264NALU::kIDRSlice: { | |
1316 // TODO(posciak): the IDR may require an SPS that we don't have | |
1317 // available. For now we'd fail if that happens, but ideally we'd like | |
1318 // to keep going until the next SPS in the stream. | |
1319 if (state_ == kNeedStreamMetadata) { | |
1320 // We need an SPS, skip this IDR and keep looking. | |
1321 break; | |
1322 } | |
1323 | |
1324 // If after reset, we should be able to recover from an IDR. | |
1325 state_ = kDecoding; | |
1326 | |
1327 if (!curr_slice_hdr_) { | |
1328 curr_slice_hdr_.reset(new media::H264SliceHeader()); | |
1329 par_res = | |
1330 parser_.ParseSliceHeader(*curr_nalu_, curr_slice_hdr_.get()); | |
1331 if (par_res != media::H264Parser::kOk) | |
1332 SET_ERROR_AND_RETURN(); | |
1333 | |
1334 if (!PreprocessCurrentSlice()) | |
1335 SET_ERROR_AND_RETURN(); | |
1336 } | |
1337 | |
1338 if (!curr_pic_) { | |
1339 // New picture/finished previous one, try to start a new one | |
1340 // or tell the client we need more surfaces. | |
1341 curr_pic_ = accelerator_->CreateH264Picture(); | |
1342 if (!curr_pic_) | |
1343 return kRanOutOfSurfaces; | |
1344 | |
1345 if (!StartNewFrame(curr_slice_hdr_.get())) | |
1346 SET_ERROR_AND_RETURN(); | |
1347 } | |
1348 | |
1349 if (!ProcessCurrentSlice()) | |
1350 SET_ERROR_AND_RETURN(); | |
1351 | |
1352 curr_slice_hdr_.reset(); | |
1353 break; | |
1354 } | |
1355 | |
1356 case media::H264NALU::kSPS: { | |
1357 int sps_id; | |
1358 | |
1359 if (!FinishPrevFrameIfPresent()) | |
1360 SET_ERROR_AND_RETURN(); | |
1361 | |
1362 par_res = parser_.ParseSPS(&sps_id); | |
1363 if (par_res != media::H264Parser::kOk) | |
1364 SET_ERROR_AND_RETURN(); | |
1365 | |
1366 bool need_new_buffers = false; | |
1367 if (!ProcessSPS(sps_id, &need_new_buffers)) | |
1368 SET_ERROR_AND_RETURN(); | |
1369 | |
1370 if (state_ == kNeedStreamMetadata) | |
1371 state_ = kAfterReset; | |
1372 | |
1373 if (need_new_buffers) { | |
1374 if (!Flush()) | |
1375 return kDecodeError; | |
1376 | |
1377 curr_pic_ = nullptr; | |
1378 curr_nalu_ = nullptr; | |
1379 ref_pic_list_p0_.clear(); | |
1380 ref_pic_list_b0_.clear(); | |
1381 ref_pic_list_b1_.clear(); | |
1382 | |
1383 return kAllocateNewSurfaces; | |
1384 } | |
1385 break; | |
1386 } | |
1387 | |
1388 case media::H264NALU::kPPS: { | |
1389 int pps_id; | |
1390 | |
1391 if (!FinishPrevFrameIfPresent()) | |
1392 SET_ERROR_AND_RETURN(); | |
1393 | |
1394 par_res = parser_.ParsePPS(&pps_id); | |
1395 if (par_res != media::H264Parser::kOk) | |
1396 SET_ERROR_AND_RETURN(); | |
1397 | |
1398 break; | |
1399 } | |
1400 | |
1401 case media::H264NALU::kAUD: | |
1402 case media::H264NALU::kEOSeq: | |
1403 case media::H264NALU::kEOStream: | |
1404 if (state_ != kDecoding) | |
1405 break; | |
1406 | |
1407 if (!FinishPrevFrameIfPresent()) | |
1408 SET_ERROR_AND_RETURN(); | |
1409 | |
1410 break; | |
1411 | |
1412 default: | |
1413 DVLOG(4) << "Skipping NALU type: " << curr_nalu_->nal_unit_type; | |
1414 break; | |
1415 } | |
1416 | |
1417 DVLOG(4) << "NALU done"; | |
1418 curr_nalu_.reset(); | |
1419 } | |
1420 } | |
1421 | |
1422 gfx::Size H264Decoder::GetPicSize() const { | |
1423 return pic_size_; | |
1424 } | |
1425 | |
1426 size_t H264Decoder::GetRequiredNumOfPictures() const { | |
1427 return dpb_.max_num_pics() + kPicsInPipeline; | |
1428 } | |
1429 | |
1430 } // namespace content | |
OLD | NEW |