-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh264_decoder.cc
1548 lines (1309 loc) · 52 KB
/
h264_decoder.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include <limits>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback_helpers.h"
#include "base/macros.h"
#include "base/numerics/safe_conversions.h"
#include "base/optional.h"
#include "base/stl_util.h"
#include "media/gpu/h264_decoder.h"
#include "media/video/h264_level_limits.h"
namespace media {
H264Decoder::H264Accelerator::H264Accelerator() = default;
H264Decoder::H264Accelerator::~H264Accelerator() = default;
H264Decoder::H264Accelerator::Status H264Decoder::H264Accelerator::SetStream(
base::span<const uint8_t> stream,
const DecryptConfig* decrypt_config) {
return H264Decoder::H264Accelerator::Status::kNotSupported;
}
H264Decoder::H264Accelerator::Status
H264Decoder::H264Accelerator::ParseSliceHeader(const H264NALU& slice_nalu,
H264SliceHeader* slice_header) {
return H264Decoder::H264Accelerator::Status::kNotSupported;
}
H264Decoder::H264Decoder(std::unique_ptr<H264Accelerator> accelerator,
const VideoColorSpace& container_color_space)
: state_(kNeedStreamMetadata),
container_color_space_(container_color_space),
max_frame_num_(0),
max_pic_num_(0),
max_long_term_frame_idx_(0),
max_num_reorder_frames_(0),
accelerator_(std::move(accelerator)) {
DCHECK(accelerator_);
Reset();
}
H264Decoder::~H264Decoder() = default;
void H264Decoder::Reset() {
curr_pic_ = nullptr;
curr_nalu_ = nullptr;
curr_slice_hdr_ = nullptr;
curr_sps_id_ = -1;
curr_pps_id_ = -1;
prev_frame_num_ = -1;
prev_ref_frame_num_ = -1;
prev_frame_num_offset_ = -1;
prev_has_memmgmnt5_ = false;
prev_ref_has_memmgmnt5_ = false;
prev_ref_top_field_order_cnt_ = -1;
prev_ref_pic_order_cnt_msb_ = -1;
prev_ref_pic_order_cnt_lsb_ = -1;
prev_ref_field_ = H264Picture::FIELD_NONE;
ref_pic_list_p0_.clear();
ref_pic_list_b0_.clear();
ref_pic_list_b1_.clear();
dpb_.Clear();
parser_.Reset();
accelerator_->Reset();
last_output_poc_ = std::numeric_limits<int>::min();
// If we are in kDecoding, we can resume without processing an SPS.
if (state_ == kDecoding)
state_ = kAfterReset;
}
void H264Decoder::PrepareRefPicLists(const H264SliceHeader* slice_hdr) {
ConstructReferencePicListsP(slice_hdr);
ConstructReferencePicListsB(slice_hdr);
}
bool H264Decoder::ModifyReferencePicLists(const H264SliceHeader* slice_hdr,
H264Picture::Vector* ref_pic_list0,
H264Picture::Vector* ref_pic_list1) {
ref_pic_list0->clear();
ref_pic_list1->clear();
// Fill reference picture lists for B and S/SP slices.
if (slice_hdr->IsPSlice() || slice_hdr->IsSPSlice()) {
*ref_pic_list0 = ref_pic_list_p0_;
return ModifyReferencePicList(slice_hdr, 0, ref_pic_list0);
} else if (slice_hdr->IsBSlice()) {
*ref_pic_list0 = ref_pic_list_b0_;
*ref_pic_list1 = ref_pic_list_b1_;
return ModifyReferencePicList(slice_hdr, 0, ref_pic_list0) &&
ModifyReferencePicList(slice_hdr, 1, ref_pic_list1);
}
return true;
}
H264Decoder::H264Accelerator::Status H264Decoder::DecodePicture() {
DCHECK(curr_pic_.get());
return accelerator_->SubmitDecode(curr_pic_);
}
bool H264Decoder::InitNonexistingPicture(scoped_refptr<H264Picture> pic,
int frame_num) {
pic->nonexisting = true;
pic->nal_ref_idc = 1;
pic->frame_num = pic->pic_num = frame_num;
pic->adaptive_ref_pic_marking_mode_flag = false;
pic->ref = true;
pic->long_term_reference_flag = false;
pic->field = H264Picture::FIELD_NONE;
return CalculatePicOrderCounts(pic);
}
bool H264Decoder::InitCurrPicture(const H264SliceHeader* slice_hdr) {
if (!FillH264PictureFromSliceHeader(parser_.GetSPS(curr_sps_id_), *slice_hdr,
curr_pic_.get())) {
return false;
}
if (!CalculatePicOrderCounts(curr_pic_))
return false;
curr_pic_->long_term_reference_flag = slice_hdr->long_term_reference_flag;
curr_pic_->adaptive_ref_pic_marking_mode_flag =
slice_hdr->adaptive_ref_pic_marking_mode_flag;
// If the slice header indicates we will have to perform reference marking
// process after this picture is decoded, store required data for that
// purpose.
if (slice_hdr->adaptive_ref_pic_marking_mode_flag) {
static_assert(sizeof(curr_pic_->ref_pic_marking) ==
sizeof(slice_hdr->ref_pic_marking),
"Array sizes of ref pic marking do not match.");
memcpy(curr_pic_->ref_pic_marking, slice_hdr->ref_pic_marking,
sizeof(curr_pic_->ref_pic_marking));
}
curr_pic_->set_visible_rect(visible_rect_);
curr_pic_->set_bitstream_id(stream_id_);
return true;
}
bool H264Decoder::CalculatePicOrderCounts(scoped_refptr<H264Picture> pic) {
const H264SPS* sps = parser_.GetSPS(curr_sps_id_);
if (!sps)
return false;
switch (pic->pic_order_cnt_type) {
case 0: {
// See spec 8.2.1.1.
int prev_pic_order_cnt_msb, prev_pic_order_cnt_lsb;
if (pic->idr) {
prev_pic_order_cnt_msb = prev_pic_order_cnt_lsb = 0;
} else {
if (prev_ref_has_memmgmnt5_) {
if (prev_ref_field_ != H264Picture::FIELD_BOTTOM) {
prev_pic_order_cnt_msb = 0;
prev_pic_order_cnt_lsb = prev_ref_top_field_order_cnt_;
} else {
prev_pic_order_cnt_msb = 0;
prev_pic_order_cnt_lsb = 0;
}
} else {
prev_pic_order_cnt_msb = prev_ref_pic_order_cnt_msb_;
prev_pic_order_cnt_lsb = prev_ref_pic_order_cnt_lsb_;
}
}
int max_pic_order_cnt_lsb =
1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
DCHECK_NE(max_pic_order_cnt_lsb, 0);
if ((pic->pic_order_cnt_lsb < prev_pic_order_cnt_lsb) &&
(prev_pic_order_cnt_lsb - pic->pic_order_cnt_lsb >=
max_pic_order_cnt_lsb / 2)) {
pic->pic_order_cnt_msb = prev_pic_order_cnt_msb + max_pic_order_cnt_lsb;
} else if ((pic->pic_order_cnt_lsb > prev_pic_order_cnt_lsb) &&
(pic->pic_order_cnt_lsb - prev_pic_order_cnt_lsb >
max_pic_order_cnt_lsb / 2)) {
pic->pic_order_cnt_msb = prev_pic_order_cnt_msb - max_pic_order_cnt_lsb;
} else {
pic->pic_order_cnt_msb = prev_pic_order_cnt_msb;
}
if (pic->field != H264Picture::FIELD_BOTTOM) {
pic->top_field_order_cnt =
pic->pic_order_cnt_msb + pic->pic_order_cnt_lsb;
}
if (pic->field != H264Picture::FIELD_TOP) {
if (pic->field == H264Picture::FIELD_NONE) {
pic->bottom_field_order_cnt =
pic->top_field_order_cnt + pic->delta_pic_order_cnt_bottom;
} else {
pic->bottom_field_order_cnt =
pic->pic_order_cnt_msb + pic->pic_order_cnt_lsb;
}
}
break;
}
case 1: {
// See spec 8.2.1.2.
if (prev_has_memmgmnt5_)
prev_frame_num_offset_ = 0;
if (pic->idr)
pic->frame_num_offset = 0;
else if (prev_frame_num_ > pic->frame_num)
pic->frame_num_offset = prev_frame_num_offset_ + max_frame_num_;
else
pic->frame_num_offset = prev_frame_num_offset_;
int abs_frame_num = 0;
if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0)
abs_frame_num = pic->frame_num_offset + pic->frame_num;
else
abs_frame_num = 0;
if (pic->nal_ref_idc == 0 && abs_frame_num > 0)
--abs_frame_num;
int expected_pic_order_cnt = 0;
if (abs_frame_num > 0) {
if (sps->num_ref_frames_in_pic_order_cnt_cycle == 0) {
DVLOG(1) << "Invalid num_ref_frames_in_pic_order_cnt_cycle "
<< "in stream";
return false;
}
int pic_order_cnt_cycle_cnt =
(abs_frame_num - 1) / sps->num_ref_frames_in_pic_order_cnt_cycle;
int frame_num_in_pic_order_cnt_cycle =
(abs_frame_num - 1) % sps->num_ref_frames_in_pic_order_cnt_cycle;
expected_pic_order_cnt = pic_order_cnt_cycle_cnt *
sps->expected_delta_per_pic_order_cnt_cycle;
// frame_num_in_pic_order_cnt_cycle is verified < 255 in parser
for (int i = 0; i <= frame_num_in_pic_order_cnt_cycle; ++i)
expected_pic_order_cnt += sps->offset_for_ref_frame[i];
}
if (!pic->nal_ref_idc)
expected_pic_order_cnt += sps->offset_for_non_ref_pic;
if (pic->field == H264Picture::FIELD_NONE) {
pic->top_field_order_cnt =
expected_pic_order_cnt + pic->delta_pic_order_cnt0;
pic->bottom_field_order_cnt = pic->top_field_order_cnt +
sps->offset_for_top_to_bottom_field +
pic->delta_pic_order_cnt1;
} else if (pic->field != H264Picture::FIELD_BOTTOM) {
pic->top_field_order_cnt =
expected_pic_order_cnt + pic->delta_pic_order_cnt0;
} else {
pic->bottom_field_order_cnt = expected_pic_order_cnt +
sps->offset_for_top_to_bottom_field +
pic->delta_pic_order_cnt0;
}
break;
}
case 2: {
// See spec 8.2.1.3.
if (prev_has_memmgmnt5_)
prev_frame_num_offset_ = 0;
if (pic->idr)
pic->frame_num_offset = 0;
else if (prev_frame_num_ > pic->frame_num)
pic->frame_num_offset = prev_frame_num_offset_ + max_frame_num_;
else
pic->frame_num_offset = prev_frame_num_offset_;
int temp_pic_order_cnt;
if (pic->idr) {
temp_pic_order_cnt = 0;
} else if (!pic->nal_ref_idc) {
temp_pic_order_cnt = 2 * (pic->frame_num_offset + pic->frame_num) - 1;
} else {
temp_pic_order_cnt = 2 * (pic->frame_num_offset + pic->frame_num);
}
if (pic->field == H264Picture::FIELD_NONE) {
pic->top_field_order_cnt = temp_pic_order_cnt;
pic->bottom_field_order_cnt = temp_pic_order_cnt;
} else if (pic->field == H264Picture::FIELD_BOTTOM) {
pic->bottom_field_order_cnt = temp_pic_order_cnt;
} else {
pic->top_field_order_cnt = temp_pic_order_cnt;
}
break;
}
default:
DVLOG(1) << "Invalid pic_order_cnt_type: " << sps->pic_order_cnt_type;
return false;
}
switch (pic->field) {
case H264Picture::FIELD_NONE:
pic->pic_order_cnt =
std::min(pic->top_field_order_cnt, pic->bottom_field_order_cnt);
break;
case H264Picture::FIELD_TOP:
pic->pic_order_cnt = pic->top_field_order_cnt;
break;
case H264Picture::FIELD_BOTTOM:
pic->pic_order_cnt = pic->bottom_field_order_cnt;
break;
}
return true;
}
void H264Decoder::UpdatePicNums(int frame_num) {
for (auto& pic : dpb_) {
if (!pic->ref)
continue;
// 8.2.4.1. Assumes non-interlaced stream.
DCHECK_EQ(pic->field, H264Picture::FIELD_NONE);
if (pic->long_term) {
pic->long_term_pic_num = pic->long_term_frame_idx;
} else {
if (pic->frame_num > frame_num)
pic->frame_num_wrap = pic->frame_num - max_frame_num_;
else
pic->frame_num_wrap = pic->frame_num;
pic->pic_num = pic->frame_num_wrap;
}
}
}
struct PicNumDescCompare {
bool operator()(const scoped_refptr<H264Picture>& a,
const scoped_refptr<H264Picture>& b) const {
return a->pic_num > b->pic_num;
}
};
struct LongTermPicNumAscCompare {
bool operator()(const scoped_refptr<H264Picture>& a,
const scoped_refptr<H264Picture>& b) const {
return a->long_term_pic_num < b->long_term_pic_num;
}
};
void H264Decoder::ConstructReferencePicListsP(
const H264SliceHeader* slice_hdr) {
// RefPicList0 (8.2.4.2.1) [[1] [2]], where:
// [1] shortterm ref pics sorted by descending pic_num,
// [2] longterm ref pics by ascending long_term_pic_num.
ref_pic_list_p0_.clear();
// First get the short ref pics...
dpb_.GetShortTermRefPicsAppending(&ref_pic_list_p0_);
size_t num_short_refs = ref_pic_list_p0_.size();
// and sort them to get [1].
std::sort(ref_pic_list_p0_.begin(), ref_pic_list_p0_.end(),
PicNumDescCompare());
// Now get long term pics and sort them by long_term_pic_num to get [2].
dpb_.GetLongTermRefPicsAppending(&ref_pic_list_p0_);
std::sort(ref_pic_list_p0_.begin() + num_short_refs, ref_pic_list_p0_.end(),
LongTermPicNumAscCompare());
}
struct POCAscCompare {
bool operator()(const scoped_refptr<H264Picture>& a,
const scoped_refptr<H264Picture>& b) const {
return a->pic_order_cnt < b->pic_order_cnt;
}
};
struct POCDescCompare {
bool operator()(const scoped_refptr<H264Picture>& a,
const scoped_refptr<H264Picture>& b) const {
return a->pic_order_cnt > b->pic_order_cnt;
}
};
void H264Decoder::ConstructReferencePicListsB(
const H264SliceHeader* slice_hdr) {
// RefPicList0 (8.2.4.2.3) [[1] [2] [3]], where:
// [1] shortterm ref pics with POC < curr_pic's POC sorted by descending POC,
// [2] shortterm ref pics with POC > curr_pic's POC by ascending POC,
// [3] longterm ref pics by ascending long_term_pic_num.
ref_pic_list_b0_.clear();
ref_pic_list_b1_.clear();
dpb_.GetShortTermRefPicsAppending(&ref_pic_list_b0_);
size_t num_short_refs = ref_pic_list_b0_.size();
// First sort ascending, this will put [1] in right place and finish [2].
std::sort(ref_pic_list_b0_.begin(), ref_pic_list_b0_.end(), POCAscCompare());
// Find first with POC > curr_pic's POC to get first element in [2]...
H264Picture::Vector::iterator iter;
iter = std::upper_bound(ref_pic_list_b0_.begin(), ref_pic_list_b0_.end(),
curr_pic_.get(), POCAscCompare());
// and sort [1] descending, thus finishing sequence [1] [2].
std::sort(ref_pic_list_b0_.begin(), iter, POCDescCompare());
// Now add [3] and sort by ascending long_term_pic_num.
dpb_.GetLongTermRefPicsAppending(&ref_pic_list_b0_);
std::sort(ref_pic_list_b0_.begin() + num_short_refs, ref_pic_list_b0_.end(),
LongTermPicNumAscCompare());
// RefPicList1 (8.2.4.2.4) [[1] [2] [3]], where:
// [1] shortterm ref pics with POC > curr_pic's POC sorted by ascending POC,
// [2] shortterm ref pics with POC < curr_pic's POC by descending POC,
// [3] longterm ref pics by ascending long_term_pic_num.
dpb_.GetShortTermRefPicsAppending(&ref_pic_list_b1_);
num_short_refs = ref_pic_list_b1_.size();
// First sort by descending POC.
std::sort(ref_pic_list_b1_.begin(), ref_pic_list_b1_.end(), POCDescCompare());
// Find first with POC < curr_pic's POC to get first element in [2]...
iter = std::upper_bound(ref_pic_list_b1_.begin(), ref_pic_list_b1_.end(),
curr_pic_.get(), POCDescCompare());
// and sort [1] ascending.
std::sort(ref_pic_list_b1_.begin(), iter, POCAscCompare());
// Now add [3] and sort by ascending long_term_pic_num
dpb_.GetShortTermRefPicsAppending(&ref_pic_list_b1_);
std::sort(ref_pic_list_b1_.begin() + num_short_refs, ref_pic_list_b1_.end(),
LongTermPicNumAscCompare());
// If lists identical, swap first two entries in RefPicList1 (spec 8.2.4.2.3)
if (ref_pic_list_b1_.size() > 1 &&
std::equal(ref_pic_list_b0_.begin(), ref_pic_list_b0_.end(),
ref_pic_list_b1_.begin()))
std::swap(ref_pic_list_b1_[0], ref_pic_list_b1_[1]);
}
// See 8.2.4
int H264Decoder::PicNumF(const scoped_refptr<H264Picture>& pic) {
if (!pic)
return -1;
if (!pic->long_term)
return pic->pic_num;
else
return max_pic_num_;
}
// See 8.2.4
int H264Decoder::LongTermPicNumF(const scoped_refptr<H264Picture>& pic) {
if (pic->ref && pic->long_term)
return pic->long_term_pic_num;
else
return 2 * (max_long_term_frame_idx_ + 1);
}
// Shift elements on the |v| starting from |from| to |to|, inclusive,
// one position to the right and insert pic at |from|.
static void ShiftRightAndInsert(H264Picture::Vector* v,
int from,
int to,
const scoped_refptr<H264Picture>& pic) {
// Security checks, do not disable in Debug mode.
CHECK(from <= to);
CHECK(to <= std::numeric_limits<int>::max() - 2);
// Additional checks. Debug mode ok.
DCHECK(v);
DCHECK(pic);
DCHECK((to + 1 == static_cast<int>(v->size())) ||
(to + 2 == static_cast<int>(v->size())));
v->resize(to + 2);
for (int i = to + 1; i > from; --i)
(*v)[i] = (*v)[i - 1];
(*v)[from] = pic;
}
bool H264Decoder::ModifyReferencePicList(const H264SliceHeader* slice_hdr,
int list,
H264Picture::Vector* ref_pic_listx) {
bool ref_pic_list_modification_flag_lX;
int num_ref_idx_lX_active_minus1;
const H264ModificationOfPicNum* list_mod;
// This can process either ref_pic_list0 or ref_pic_list1, depending on
// the list argument. Set up pointers to proper list to be processed here.
if (list == 0) {
ref_pic_list_modification_flag_lX =
slice_hdr->ref_pic_list_modification_flag_l0;
num_ref_idx_lX_active_minus1 = slice_hdr->num_ref_idx_l0_active_minus1;
list_mod = slice_hdr->ref_list_l0_modifications;
} else {
ref_pic_list_modification_flag_lX =
slice_hdr->ref_pic_list_modification_flag_l1;
num_ref_idx_lX_active_minus1 = slice_hdr->num_ref_idx_l1_active_minus1;
list_mod = slice_hdr->ref_list_l1_modifications;
}
// Resize the list to the size requested in the slice header.
// Note that per 8.2.4.2 it's possible for num_ref_idx_lX_active_minus1 to
// indicate there should be more ref pics on list than we constructed.
// Those superfluous ones should be treated as non-reference and will be
// initialized to nullptr, which must be handled by clients.
DCHECK_GE(num_ref_idx_lX_active_minus1, 0);
ref_pic_listx->resize(num_ref_idx_lX_active_minus1 + 1);
if (!ref_pic_list_modification_flag_lX)
return true;
// Spec 8.2.4.3:
// Reorder pictures on the list in a way specified in the stream.
int pic_num_lx_pred = curr_pic_->pic_num;
int ref_idx_lx = 0;
int pic_num_lx_no_wrap;
int pic_num_lx;
bool done = false;
scoped_refptr<H264Picture> pic;
for (int i = 0; i < H264SliceHeader::kRefListModSize && !done; ++i) {
switch (list_mod->modification_of_pic_nums_idc) {
case 0:
case 1:
// Modify short reference picture position.
if (list_mod->modification_of_pic_nums_idc == 0) {
// Subtract given value from predicted PicNum.
pic_num_lx_no_wrap =
pic_num_lx_pred -
(static_cast<int>(list_mod->abs_diff_pic_num_minus1) + 1);
// Wrap around max_pic_num_ if it becomes < 0 as result
// of subtraction.
if (pic_num_lx_no_wrap < 0)
pic_num_lx_no_wrap += max_pic_num_;
} else {
// Add given value to predicted PicNum.
pic_num_lx_no_wrap =
pic_num_lx_pred +
(static_cast<int>(list_mod->abs_diff_pic_num_minus1) + 1);
// Wrap around max_pic_num_ if it becomes >= max_pic_num_ as result
// of the addition.
if (pic_num_lx_no_wrap >= max_pic_num_)
pic_num_lx_no_wrap -= max_pic_num_;
}
// For use in next iteration.
pic_num_lx_pred = pic_num_lx_no_wrap;
if (pic_num_lx_no_wrap > curr_pic_->pic_num)
pic_num_lx = pic_num_lx_no_wrap - max_pic_num_;
else
pic_num_lx = pic_num_lx_no_wrap;
DCHECK_LT(num_ref_idx_lX_active_minus1 + 1,
H264SliceHeader::kRefListModSize);
pic = dpb_.GetShortRefPicByPicNum(pic_num_lx);
if (!pic) {
DVLOG(1) << "Malformed stream, no pic num " << pic_num_lx;
return false;
}
ShiftRightAndInsert(ref_pic_listx, ref_idx_lx,
num_ref_idx_lX_active_minus1, pic);
ref_idx_lx++;
for (int src = ref_idx_lx, dst = ref_idx_lx;
src <= num_ref_idx_lX_active_minus1 + 1; ++src) {
if (PicNumF((*ref_pic_listx)[src]) != pic_num_lx)
(*ref_pic_listx)[dst++] = (*ref_pic_listx)[src];
}
break;
case 2:
// Modify long term reference picture position.
DCHECK_LT(num_ref_idx_lX_active_minus1 + 1,
H264SliceHeader::kRefListModSize);
pic = dpb_.GetLongRefPicByLongTermPicNum(list_mod->long_term_pic_num);
if (!pic) {
DVLOG(1) << "Malformed stream, no pic num "
<< list_mod->long_term_pic_num;
return false;
}
ShiftRightAndInsert(ref_pic_listx, ref_idx_lx,
num_ref_idx_lX_active_minus1, pic);
ref_idx_lx++;
for (int src = ref_idx_lx, dst = ref_idx_lx;
src <= num_ref_idx_lX_active_minus1 + 1; ++src) {
if (LongTermPicNumF((*ref_pic_listx)[src]) !=
static_cast<int>(list_mod->long_term_pic_num))
(*ref_pic_listx)[dst++] = (*ref_pic_listx)[src];
}
break;
case 3:
// End of modification list.
done = true;
break;
default:
// May be recoverable.
DVLOG(1) << "Invalid modification_of_pic_nums_idc="
<< list_mod->modification_of_pic_nums_idc << " in position "
<< i;
break;
}
++list_mod;
}
// Per NOTE 2 in 8.2.4.3.2, the ref_pic_listx size in the above loop is
// temporarily made one element longer than the required final list.
// Resize the list back to its required size.
ref_pic_listx->resize(num_ref_idx_lX_active_minus1 + 1);
return true;
}
void H264Decoder::OutputPic(scoped_refptr<H264Picture> pic) {
DCHECK(!pic->outputted);
pic->outputted = true;
VideoColorSpace colorspace_for_frame = container_color_space_;
const H264SPS* sps = parser_.GetSPS(curr_sps_id_);
if (sps && sps->GetColorSpace().IsSpecified())
colorspace_for_frame = sps->GetColorSpace();
pic->set_colorspace(colorspace_for_frame);
if (pic->nonexisting) {
DVLOG(4) << "Skipping output, non-existing frame_num: " << pic->frame_num;
return;
}
DVLOG_IF(1, pic->pic_order_cnt < last_output_poc_)
<< "Outputting out of order, likely a broken stream: " << last_output_poc_
<< " -> " << pic->pic_order_cnt;
last_output_poc_ = pic->pic_order_cnt;
DVLOG(4) << "Posting output task for POC: " << pic->pic_order_cnt;
accelerator_->OutputPicture(pic);
}
void H264Decoder::ClearDPB() {
// Clear DPB contents, marking the pictures as unused first.
dpb_.Clear();
last_output_poc_ = std::numeric_limits<int>::min();
}
bool H264Decoder::OutputAllRemainingPics() {
// Output all pictures that are waiting to be outputted.
FinishPrevFrameIfPresent();
H264Picture::Vector to_output;
dpb_.GetNotOutputtedPicsAppending(&to_output);
// Sort them by ascending POC to output in order.
std::sort(to_output.begin(), to_output.end(), POCAscCompare());
for (auto& pic : to_output)
OutputPic(pic);
return true;
}
bool H264Decoder::Flush() {
DVLOG(2) << "Decoder flush";
if (!OutputAllRemainingPics())
return false;
ClearDPB();
DVLOG(2) << "Decoder flush finished";
return true;
}
H264Decoder::H264Accelerator::Status H264Decoder::StartNewFrame(
const H264SliceHeader* slice_hdr) {
// TODO posciak: add handling of max_num_ref_frames per spec.
CHECK(curr_pic_.get());
DCHECK(slice_hdr);
curr_pps_id_ = slice_hdr->pic_parameter_set_id;
const H264PPS* pps = parser_.GetPPS(curr_pps_id_);
if (!pps)
return H264Accelerator::Status::kFail;
curr_sps_id_ = pps->seq_parameter_set_id;
const H264SPS* sps = parser_.GetSPS(curr_sps_id_);
if (!sps)
return H264Accelerator::Status::kFail;
max_frame_num_ = 1 << (sps->log2_max_frame_num_minus4 + 4);
int frame_num = slice_hdr->frame_num;
if (slice_hdr->idr_pic_flag)
prev_ref_frame_num_ = 0;
// 7.4.3
if (frame_num != prev_ref_frame_num_ &&
frame_num != (prev_ref_frame_num_ + 1) % max_frame_num_) {
if (!HandleFrameNumGap(frame_num))
return H264Accelerator::Status::kFail;
}
if (!InitCurrPicture(slice_hdr))
return H264Accelerator::Status::kFail;
UpdatePicNums(frame_num);
PrepareRefPicLists(slice_hdr);
return accelerator_->SubmitFrameMetadata(sps, pps, dpb_, ref_pic_list_p0_,
ref_pic_list_b0_, ref_pic_list_b1_,
curr_pic_.get());
}
bool H264Decoder::HandleMemoryManagementOps(scoped_refptr<H264Picture> pic) {
// 8.2.5.4
for (size_t i = 0; i < arraysize(pic->ref_pic_marking); ++i) {
// Code below does not support interlaced stream (per-field pictures).
H264DecRefPicMarking* ref_pic_marking = &pic->ref_pic_marking[i];
scoped_refptr<H264Picture> to_mark;
int pic_num_x;
switch (ref_pic_marking->memory_mgmnt_control_operation) {
case 0:
// Normal end of operations' specification.
return true;
case 1:
// Mark a short term reference picture as unused so it can be removed
// if outputted.
pic_num_x =
pic->pic_num - (ref_pic_marking->difference_of_pic_nums_minus1 + 1);
to_mark = dpb_.GetShortRefPicByPicNum(pic_num_x);
if (to_mark) {
to_mark->ref = false;
} else {
DVLOG(1) << "Invalid short ref pic num to unmark";
return false;
}
break;
case 2:
// Mark a long term reference picture as unused so it can be removed
// if outputted.
to_mark = dpb_.GetLongRefPicByLongTermPicNum(
ref_pic_marking->long_term_pic_num);
if (to_mark) {
to_mark->ref = false;
} else {
DVLOG(1) << "Invalid long term ref pic num to unmark";
return false;
}
break;
case 3:
// Mark a short term reference picture as long term reference.
pic_num_x =
pic->pic_num - (ref_pic_marking->difference_of_pic_nums_minus1 + 1);
to_mark = dpb_.GetShortRefPicByPicNum(pic_num_x);
if (to_mark) {
DCHECK(to_mark->ref && !to_mark->long_term);
to_mark->long_term = true;
to_mark->long_term_frame_idx = ref_pic_marking->long_term_frame_idx;
} else {
DVLOG(1) << "Invalid short term ref pic num to mark as long ref";
return false;
}
break;
case 4: {
// Unmark all reference pictures with long_term_frame_idx over new max.
max_long_term_frame_idx_ =
ref_pic_marking->max_long_term_frame_idx_plus1 - 1;
H264Picture::Vector long_terms;
dpb_.GetLongTermRefPicsAppending(&long_terms);
for (size_t i = 0; i < long_terms.size(); ++i) {
scoped_refptr<H264Picture>& long_term_pic = long_terms[i];
DCHECK(long_term_pic->ref && long_term_pic->long_term);
// Ok to cast, max_long_term_frame_idx is much smaller than 16bit.
if (long_term_pic->long_term_frame_idx >
static_cast<int>(max_long_term_frame_idx_))
long_term_pic->ref = false;
}
break;
}
case 5:
// Unmark all reference pictures.
dpb_.MarkAllUnusedForRef();
max_long_term_frame_idx_ = -1;
pic->mem_mgmt_5 = true;
break;
case 6: {
// Replace long term reference pictures with current picture.
// First unmark if any existing with this long_term_frame_idx...
H264Picture::Vector long_terms;
dpb_.GetLongTermRefPicsAppending(&long_terms);
for (size_t i = 0; i < long_terms.size(); ++i) {
scoped_refptr<H264Picture>& long_term_pic = long_terms[i];
DCHECK(long_term_pic->ref && long_term_pic->long_term);
// Ok to cast, long_term_frame_idx is much smaller than 16bit.
if (long_term_pic->long_term_frame_idx ==
static_cast<int>(ref_pic_marking->long_term_frame_idx))
long_term_pic->ref = false;
}
// and mark the current one instead.
pic->ref = true;
pic->long_term = true;
pic->long_term_frame_idx = ref_pic_marking->long_term_frame_idx;
break;
}
default:
// Would indicate a bug in parser.
NOTREACHED();
}
}
return true;
}
// This method ensures that DPB does not overflow, either by removing
// reference pictures as specified in the stream, or using a sliding window
// procedure to remove the oldest one.
// It also performs marking and unmarking pictures as reference.
// See spac 8.2.5.1.
bool H264Decoder::ReferencePictureMarking(scoped_refptr<H264Picture> pic) {
// If the current picture is an IDR, all reference pictures are unmarked.
if (pic->idr) {
dpb_.MarkAllUnusedForRef();
if (pic->long_term_reference_flag) {
pic->long_term = true;
pic->long_term_frame_idx = 0;
max_long_term_frame_idx_ = 0;
} else {
pic->long_term = false;
max_long_term_frame_idx_ = -1;
}
return true;
}
// Not an IDR. If the stream contains instructions on how to discard pictures
// from DPB and how to mark/unmark existing reference pictures, do so.
// Otherwise, fall back to default sliding window process.
if (pic->adaptive_ref_pic_marking_mode_flag) {
DCHECK(!pic->nonexisting);
return HandleMemoryManagementOps(pic);
} else {
return SlidingWindowPictureMarking();
}
}
bool H264Decoder::SlidingWindowPictureMarking() {
const H264SPS* sps = parser_.GetSPS(curr_sps_id_);
if (!sps)
return false;
// 8.2.5.3. Ensure the DPB doesn't overflow by discarding the oldest picture.
int num_ref_pics = dpb_.CountRefPics();
DCHECK_LE(num_ref_pics, std::max<int>(sps->max_num_ref_frames, 1));
if (num_ref_pics == std::max<int>(sps->max_num_ref_frames, 1)) {
// Max number of reference pics reached, need to remove one of the short
// term ones. Find smallest frame_num_wrap short reference picture and mark
// it as unused.
scoped_refptr<H264Picture> to_unmark =
dpb_.GetLowestFrameNumWrapShortRefPic();
if (!to_unmark) {
DVLOG(1) << "Couldn't find a short ref picture to unmark";
return false;
}
to_unmark->ref = false;
}
return true;
}
bool H264Decoder::FinishPicture(scoped_refptr<H264Picture> pic) {
// Finish processing the picture.
// Start by storing previous picture data for later use.
if (pic->ref) {
ReferencePictureMarking(pic);
prev_ref_has_memmgmnt5_ = pic->mem_mgmt_5;
prev_ref_top_field_order_cnt_ = pic->top_field_order_cnt;
prev_ref_pic_order_cnt_msb_ = pic->pic_order_cnt_msb;
prev_ref_pic_order_cnt_lsb_ = pic->pic_order_cnt_lsb;
prev_ref_field_ = pic->field;
prev_ref_frame_num_ = pic->frame_num;
}
prev_frame_num_ = pic->frame_num;
prev_has_memmgmnt5_ = pic->mem_mgmt_5;
prev_frame_num_offset_ = pic->frame_num_offset;
// Remove unused (for reference or later output) pictures from DPB, marking
// them as such.
dpb_.DeleteUnused();
DVLOG(4) << "Finishing picture frame_num: " << pic->frame_num
<< ", entries in DPB: " << dpb_.size();
// The ownership of pic will either be transferred to DPB - if the picture is
// still needed (for output and/or reference) - or we will release it
// immediately if we manage to output it here and won't have to store it for
// future reference.
// Get all pictures that haven't been outputted yet.
H264Picture::Vector not_outputted;
dpb_.GetNotOutputtedPicsAppending(¬_outputted);
// Include the one we've just decoded.
not_outputted.push_back(pic);
// Sort in output order.
std::sort(not_outputted.begin(), not_outputted.end(), POCAscCompare());
// Try to output as many pictures as we can. A picture can be output,
// if the number of decoded and not yet outputted pictures that would remain
// in DPB afterwards would at least be equal to max_num_reorder_frames.
// If the outputted picture is not a reference picture, it doesn't have
// to remain in the DPB and can be removed.
auto output_candidate = not_outputted.begin();
size_t num_remaining = not_outputted.size();
while (num_remaining > max_num_reorder_frames_ ||
// If the condition below is used, this is an invalid stream. We should
// not be forced to output beyond max_num_reorder_frames in order to
// make room in DPB to store the current picture (if we need to do so).
// However, if this happens, ignore max_num_reorder_frames and try
// to output more. This may cause out-of-order output, but is not
// fatal, and better than failing instead.
((dpb_.IsFull() && (!pic->outputted || pic->ref)) && num_remaining)) {
DVLOG_IF(1, num_remaining <= max_num_reorder_frames_)
<< "Invalid stream: max_num_reorder_frames not preserved";
OutputPic(*output_candidate);
if (!(*output_candidate)->ref) {
// Current picture hasn't been inserted into DPB yet, so don't remove it
// if we managed to output it immediately.
int outputted_poc = (*output_candidate)->pic_order_cnt;
if (outputted_poc != pic->pic_order_cnt)
dpb_.DeleteByPOC(outputted_poc);
}
++output_candidate;
--num_remaining;
}
// If we haven't managed to output the picture that we just decoded, or if
// it's a reference picture, we have to store it in DPB.
if (!pic->outputted || pic->ref) {
if (dpb_.IsFull()) {
// If we haven't managed to output anything to free up space in DPB
// to store this picture, it's an error in the stream.
DVLOG(1) << "Could not free up space in DPB!";
return false;
}
dpb_.StorePic(pic);
}
return true;
}
bool H264Decoder::UpdateMaxNumReorderFrames(const H264SPS* sps) {
if (sps->vui_parameters_present_flag && sps->bitstream_restriction_flag) {
max_num_reorder_frames_ =
base::checked_cast<size_t>(sps->max_num_reorder_frames);
if (max_num_reorder_frames_ > dpb_.max_num_pics()) {
DVLOG(1)
<< "max_num_reorder_frames present, but larger than MaxDpbFrames ("
<< max_num_reorder_frames_ << " > " << dpb_.max_num_pics() << ")";
max_num_reorder_frames_ = 0;
return false;
}
return true;
}
// max_num_reorder_frames not present, infer from profile/constraints
// (see VUI semantics in spec).
if (sps->constraint_set3_flag) {
switch (sps->profile_idc) {
case 44:
case 86:
case 100: