-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathH264SliceDecodingProcess.cpp
1732 lines (1667 loc) · 69.8 KB
/
H264SliceDecodingProcess.cpp
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
#include "H264SliceDecodingProcess.h"
#include <cstdint>
#include <deque>
#include <sstream>
#include <cassert>
#include <iostream>
#include <algorithm>
#include "H26xUltis.h"
namespace Mmp
{
namespace Codec
{
#ifndef ENABLE_MMP_SD_DEBUG
#define ENABLE_MMP_SD_DEBUG 0
#endif /* ENABLE_MMP_SD_DEBUG */
#if ENABLE_MMP_SD_DEBUG
#if defined(_WIN32)
#define MPP_H264_SD_LOG(fmt, ...) do {\
char buf[512] = {0};\
sprintf_s(buf, fmt, ## __VA_ARGS__);\
H26x_LOG_INFO << buf << H26x_LOG_TERMINATOR;\
} while(0);
#else
#define MPP_H264_SD_LOG(fmt, ...) do {\
char buf[512] = {0};\
sprintf(buf, fmt, ## __VA_ARGS__);\
H26x_LOG_INFO << buf << H26x_LOG_TERMINATOR;\
} while(0);
#endif
#else
#define MPP_H264_SD_LOG(fmt, ...)
#endif /* ENABLE_MMP_SD_DEBUG */
// Reference: openH264(cfbd5896606b91638c8871ee91776dee31625bd5)
// int32_t WelsMarkAsRef (PWelsDecoderContext pCtx, PPicture pLastDec)
constexpr int64_t no_long_term_frame_indices = -1;
H264PictureContext::H264PictureContext()
{
id = 0;
id = 0;
field_pic_flag = 0;
bottom_field_flag = 0;
pic_order_cnt_lsb = 0;
long_term_frame_idx = 0;
TopFieldOrderCnt = INT32_MAX;
BottomFieldOrderCnt = INT32_MAX;
has_memory_management_control_operation_5 = false;
prevPicOrderCntMsb = 0;
FrameNumOffset = 0;
MaxFrameNum = 0;
FrameNum = 0;
FrameNumWrap = 0;
PicNum = 0;
referenceFlag = 0;
MaxLongTermFrameIdx = 0;
LongTermFrameIdx = 0;
LongTermPicNum = 0;
}
static bool PictureIsSecondField(H264PictureContext::ptr picture)
{
return picture->bottom_field_flag == 1;
}
static void UpdateReferenceFlag(H264PictureContext::ptr picture, uint8_t flag, bool append = true)
{
if (append)
{
picture->referenceFlag |= flag;
}
else
{
picture->referenceFlag = flag;
}
}
static H264PictureContext::ptr /* complementary picture */ FindComplementaryPicture(H264PictureContext::cache pictures, H264PictureContext::ptr picture)
{
H264PictureContext::ptr compPicture = nullptr;
if (picture->field_pic_flag == 0)
{
}
else if (PictureIsSecondField(picture)) // second field
{
for (auto& _picture : pictures)
{
if (_picture->PicNum /* second field */ == picture->PicNum - 1 /* first field */)
{
compPicture = _picture;
break;
}
}
}
else if (picture->bottom_field_flag == 0 /* && picture->bottom_field_flag == 1 */) // first field
{
for (auto& _picture : pictures)
{
if (_picture->PicNum /* first field */ == picture->PicNum + 1 /* second field */)
{
compPicture = _picture;
break;
}
}
}
return compPicture;
}
static int32_t GetPicNumX(H264SliceHeaderSyntax::ptr slice, uint32_t difference_of_pic_nums_minus1)
{
int32_t picNumX = 0;
{
// The variable CurrPicNum is derived as follows:
// - If field_pic_flag is equal to 0, CurrPicNum is set equal to frame_num.
// - Otherwise (field_pic_flag is equal to 1), CurrPicNum is set equal to 2 * frame_num + 1
uint64_t CurrPicNum = 0;
if (slice->field_pic_flag == 0)
{
CurrPicNum = slice->frame_num;
}
else if (slice->field_pic_flag == 1)
{
// CurrPicNum = 2 * slice->frame_num + 1;
// Hint : not support for now
assert(false);
}
picNumX = (int32_t)(CurrPicNum - (difference_of_pic_nums_minus1 + 1)); // (8-39)
}
return picNumX;
}
static H264PictureContext::ptr FindPictureByPicNum(H264PictureContext::cache pictures, int64_t PicNum)
{
H264PictureContext::ptr picture = nullptr;
for (auto _picture : pictures)
{
if (_picture->referenceFlag & H264PictureContext::used_for_short_term_reference )
{
if (PicNum == _picture->PicNum)
{
picture = _picture;
break;
}
}
}
assert(picture);
return picture;
}
static H264PictureContext::ptr FindPictureByLongTermPicNum(H264PictureContext::cache pictures, uint64_t LongTermPicNum)
{
H264PictureContext::ptr picture = nullptr;
for (auto _picture : pictures)
{
if (_picture->referenceFlag & H264PictureContext::used_for_long_term_reference)
{
if (LongTermPicNum == _picture->LongTermPicNum)
{
picture = _picture;
break;
}
}
}
assert(picture);
return picture;
}
static void UnMarkUsedForShortTermReference(H264PictureContext::cache pictures, int32_t picNumX)
{
for (auto _picture : pictures)
{
if (_picture->PicNum == picNumX && _picture->referenceFlag & H264PictureContext::used_for_short_term_reference)
{
if (_picture->field_pic_flag == 0)
{
UpdateReferenceFlag(_picture, H264PictureContext::unused_for_reference, false);
}
else if (_picture->field_pic_flag == 1)
{
// _picture->referenceFlag = _picture->referenceFlag ^ H264PictureContext::used_for_short_term_reference;
// H264PictureContext::ptr compPicture = FindComplementaryPicture(pictures, _picture);
// if (compPicture)
// {
// compPicture->referenceFlag = compPicture->referenceFlag ^ H264PictureContext::used_for_short_term_reference;
// }
// Hint : not support for now
assert(false);
}
MPP_H264_SD_LOG("[RF] UnMarkUsedForShortTermReference, PicNum(%lld) FrameNum(%d)", _picture->PicNum, _picture->FrameNum);
// Hint : 参考 FFmpeg 6.x 以及 openh264 , 此处应当只 umark 一个 short term picture, 按照 DPB 的顺序
// 同时 ISO 中也存在 `a short-term reference picture` 而非 `all short-term refernce pictures`
break;
}
}
}
static void UnMarkUsedForLongTermReference(H264PictureContext::cache pictures, uint32_t long_term_pic_num)
{
for (auto _picture : pictures)
{
if (long_term_pic_num == _picture->LongTermPicNum)
{
if (_picture->field_pic_flag == 0)
{
UpdateReferenceFlag(_picture, H264PictureContext::unused_for_reference, false);
H264PictureContext::ptr compPicture = FindComplementaryPicture(pictures, _picture);
if (compPicture)
{
UpdateReferenceFlag(compPicture, H264PictureContext::unused_for_reference, false);
}
}
else if (_picture->field_pic_flag == 1)
{
// _picture->referenceFlag = _picture->referenceFlag ^ H264PictureContext::used_for_long_term_reference;
// H264PictureContext::ptr compPicture = FindComplementaryPicture(pictures, _picture);
// if (compPicture)
// {
// compPicture->referenceFlag = compPicture->referenceFlag ^ H264PictureContext::used_for_long_term_reference;
// }
// Hint : not support for now
assert(false);
}
}
}
}
static void UnMarkUsedForReference(H264PictureContext::cache pictures, uint32_t long_term_pic_num)
{
for (auto _picture : pictures)
{
if (long_term_pic_num == _picture->LongTermPicNum && _picture->referenceFlag & H264PictureContext::used_for_long_term_reference)
{
UpdateReferenceFlag(_picture, H264PictureContext::unused_for_reference, false);
H264PictureContext::ptr compPicture = FindComplementaryPicture(pictures, _picture);
if (compPicture)
{
UpdateReferenceFlag(compPicture, H264PictureContext::unused_for_reference, false);
}
}
}
}
static void MarkShortTermReferenceToLongTermReference(H264PictureContext::cache pictures, int32_t picNumX, uint32_t long_term_frame_idx)
{
for (auto _picture : pictures)
{
if (_picture->field_pic_flag == 0)
{
if (_picture->referenceFlag & H264PictureContext::used_for_short_term_reference && _picture->PicNum == picNumX)
{
H264PictureContext::ptr compPicture = FindComplementaryPicture(pictures, _picture);
if (compPicture)
{
UpdateReferenceFlag(compPicture, H264PictureContext::used_for_long_term_reference, false);
compPicture->LongTermFrameIdx = long_term_frame_idx;
}
UpdateReferenceFlag(_picture, H264PictureContext::used_for_long_term_reference, false);
_picture->LongTermFrameIdx = long_term_frame_idx;
}
}
else if (_picture->field_pic_flag == 1)
{
// Hint : not support for now
assert(false);
}
}
}
static int32_t PicOrderCnt(H264PictureContext::ptr picX) // (8-1)
{
if (picX->field_pic_flag == 0)
{
return std::min(picX->TopFieldOrderCnt, picX->BottomFieldOrderCnt);
}
else if (picX->bottom_field_flag == 0 /* && picX->field_pic_flag == 1 */)
{
return picX->TopFieldOrderCnt;
}
else if (picX->bottom_field_flag == 1 /* && picX->field_pic_flag == 1 */)
{
return picX->BottomFieldOrderCnt;
}
else
{
assert(false);
return 0;
}
}
#if 0
static int32_t DiffPicOrderCnt(H264PictureContext::ptr picA, H264PictureContext::ptr picB) // (8-2)
{
return PicOrderCnt(picA) - PicOrderCnt(picB);
}
#endif
/*************************************** 8.2.1 Decoding process for picture order count(Begin) ******************************************/
/**
* @sa
* 1 - ISO 14496/10(2020) - 8.2.1 Decoding process for picture order count
* 2 - ISO 14496/10(2020) - 7.4.1.2 Order of NAL units and association to coded pictures, access units, and video sequences
* @note picture order count (3.1.110)
* variable that is associated with each coded field and each field of a coded frame and has a value that is non-decreasing
* with increasing field position in output order relative to the first output field of the previous IDR picture in decoding
* order or relative to the first output field of the previous picture, in decoding order, that contains a memory management
* control operation that marks all reference pictures as "unused for reference"
*/
void H264SliceDecodingProcess::DecodingProcessForPictureOrderCount(H264NalSyntax::ptr nal, H264SpsSyntax::ptr sps, H264PpsSyntax::ptr pps, H264SliceHeaderSyntax::ptr slice, uint8_t nal_ref_idc, H264PictureContext::ptr picture)
{
if (sps->pic_order_cnt_type > 2)
{
assert(false);
return;
}
if (sps->pic_order_cnt_type == 0)
{
DecodeH264PictureOrderCountType0(_prevPicture, sps, pps, slice, nal_ref_idc, picture);
}
else if (sps->pic_order_cnt_type == 1)
{
DecodeH264PictureOrderCountType1(_prevPicture, nal, sps, slice, nal_ref_idc, picture);
}
else if (sps->pic_order_cnt_type == 2)
{
DecodeH264PictureOrderCountType2(_prevPicture, sps, slice, nal_ref_idc, picture);
}
// Hint :
// When the current picture includes a
// memory_management_control_operation equal to 5, after the decoding of the current picture, tempPicOrderCnt is set
// equal to PicOrderCnt( CurrPic ), TopFieldOrderCnt of the current picture (if any) is set equal to
// TopFieldOrderCnt − tempPicOrderCnt, and BottomFieldOrderCnt of the current picture (if any) is set equal to
// BottomFieldOrderCnt − tempPicOrderCnt.
_endTasks.push_back([picture]()
{
if (picture->has_memory_management_control_operation_5)
{
int32_t tempPicOrderCnt = PicOrderCnt(picture);
picture->TopFieldOrderCnt = picture->TopFieldOrderCnt - tempPicOrderCnt;
picture->BottomFieldOrderCnt = picture->BottomFieldOrderCnt - tempPicOrderCnt;
}
});
}
/**
* @sa ISO 14496/10(2020) - 8.2.1.1 Decoding process for picture order count type 0
*/
void H264SliceDecodingProcess::DecodeH264PictureOrderCountType0(H264PictureContext::ptr prevPictrue, H264SpsSyntax::ptr sps, H264PpsSyntax::ptr pps, H264SliceHeaderSyntax::ptr slice, uint8_t nal_ref_idc, H264PictureContext::ptr picture)
{
int32_t prevPicOrderCntMsb = 0;
uint32_t prevPicOrderCntLsb = 0;
int32_t PicOrderCntMsb = 0;
// determine prevPicOrderCntMsb and prevPicOrderCntLsb
{
if (slice->slice_type == H264SliceType::MMP_H264_I_SLICE)
{
prevPicOrderCntMsb = 0;
prevPicOrderCntLsb = 0;
}
else
{
if (prevPictrue->has_memory_management_control_operation_5)
{
if (!prevPictrue->bottom_field_flag)
{
prevPicOrderCntMsb = 0;
prevPicOrderCntLsb = prevPictrue->TopFieldOrderCnt;
}
else
{
prevPicOrderCntMsb = 0;
prevPicOrderCntLsb = 0;
}
}
else
{
prevPicOrderCntMsb = prevPictrue->prevPicOrderCntMsb;
prevPicOrderCntLsb = prevPictrue->pic_order_cnt_lsb;
}
}
}
// determine PicOrderCntMsb (8-3)
{
uint32_t MaxPicOrderCntLsb = sps->context->MaxPicOrderCntLsb;
if ((slice->pic_order_cnt_lsb < prevPicOrderCntLsb) &&
((prevPicOrderCntLsb - slice->pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2))
)
{
PicOrderCntMsb = (int32_t)(prevPicOrderCntMsb + MaxPicOrderCntLsb);
}
else if ((slice->pic_order_cnt_lsb > prevPicOrderCntLsb) &&
((slice->pic_order_cnt_lsb - prevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2))
)
{
PicOrderCntMsb = (int32_t)(prevPicOrderCntMsb - MaxPicOrderCntLsb);
}
else
{
PicOrderCntMsb = prevPicOrderCntMsb;
}
}
// determine TopFieldOrderCnt and BottomFieldOrderCnt (8-4) and (8-5)
{
picture->TopFieldOrderCnt = PicOrderCntMsb + slice->pic_order_cnt_lsb;
if (pps->bottom_field_pic_order_in_frame_present_flag && !slice->field_pic_flag)
{
picture->BottomFieldOrderCnt = picture->TopFieldOrderCnt + slice->delta_pic_order_cnt_bottom;
}
else
{
picture->BottomFieldOrderCnt = PicOrderCntMsb + slice->pic_order_cnt_lsb;
}
}
// update context
if (nal_ref_idc != 0)
{
picture->prevPicOrderCntMsb = PicOrderCntMsb;
}
else
{
picture->prevPicOrderCntMsb = prevPictrue->prevPicOrderCntMsb;
}
}
/**
* @sa ISO 14496/10(2020) - 8.2.1.2 Decoding process for picture order count type 1
*/
void H264SliceDecodingProcess::DecodeH264PictureOrderCountType1(H264PictureContext::ptr prevPictrue, H264NalSyntax::ptr nal, H264SpsSyntax::ptr sps, H264SliceHeaderSyntax::ptr slice, uint8_t nal_ref_idc, H264PictureContext::ptr picture)
{
uint32_t prevFrameNum = prevPictrue ? prevPictrue->FrameNum : 0;
int32_t prevFrameNumOffset = 0;
int64_t absFrameNum = 0;
int64_t picOrderCntCycleCnt = 0;
int64_t frameNumInPicOrderCntCycle = 0;
int64_t expectedPicOrderCnt;
// determine prevFrameNumOffset
{
if (slice->slice_type != H264SliceType::MMP_H264_I_SLICE)
{
if (prevPictrue->has_memory_management_control_operation_5)
{
prevFrameNumOffset = 0;
}
else
{
prevFrameNumOffset = (int32_t)(prevPictrue->FrameNumOffset);
}
}
}
// determine FrameNumOffset (8-6)
{
if (nal->nal_unit_type == H264NaluType::MMP_H264_NALU_TYPE_IDR) // (7-0)
{
picture->FrameNumOffset = 0;
}
else if (prevFrameNum > slice->frame_num)
{
uint32_t MaxFrameNum = sps->context->MaxFrameNum;
picture->FrameNumOffset = prevFrameNumOffset + MaxFrameNum;
}
else
{
picture->FrameNumOffset = prevFrameNumOffset;
}
}
// determine absFrameNum (8-7)
{
if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0)
{
absFrameNum = picture->FrameNumOffset + slice->frame_num;
}
else
{
absFrameNum = 0;
}
if (nal_ref_idc == 0 && absFrameNum > 0)
{
absFrameNum = absFrameNum - 1;
}
}
// determine picOrderCntCycleCnt and frameNumInPicOrderCntCycle (8-8)
{
if (absFrameNum > 0)
{
picOrderCntCycleCnt = (absFrameNum - 1) / sps->num_ref_frames_in_pic_order_cnt_cycle;
frameNumInPicOrderCntCycle = (absFrameNum - 1) % sps->num_ref_frames_in_pic_order_cnt_cycle;
}
}
// determine expectedPicOrderCnt (8-9)
{
if (absFrameNum > 0)
{
int64_t ExpectedDeltaPerPicOrderCntCycle = sps->context->ExpectedDeltaPerPicOrderCntCycle;
expectedPicOrderCnt = picOrderCntCycleCnt * ExpectedDeltaPerPicOrderCntCycle;
for (int64_t i=0; i<=frameNumInPicOrderCntCycle; i++)
{
expectedPicOrderCnt = expectedPicOrderCnt + sps->offset_for_ref_frame[(size_t)i];
}
}
else
{
expectedPicOrderCnt = 0;
}
if (nal_ref_idc == 0)
{
expectedPicOrderCnt = expectedPicOrderCnt + sps->offset_for_non_ref_pic;
}
}
// determine TopFieldOrderCnt and BottomFieldOrderCnt (8-10)
{
if (!slice->field_pic_flag)
{
picture->TopFieldOrderCnt = (int32_t)(expectedPicOrderCnt + slice->delta_pic_order_cnt[0]);
picture->BottomFieldOrderCnt = picture->TopFieldOrderCnt + sps->offset_for_top_to_bottom_field + slice->delta_pic_order_cnt[1];
}
else if (!slice->bottom_field_flag)
{
picture->TopFieldOrderCnt = (int32_t)(expectedPicOrderCnt + slice->delta_pic_order_cnt[0]);
}
else
{
picture->BottomFieldOrderCnt = (int32_t)(expectedPicOrderCnt + sps->offset_for_top_to_bottom_field + slice->delta_pic_order_cnt[0]);
}
}
}
/**
* @sa ISO 14496/10(2020) - 8.2.1.3 Decoding process for picture order count type 2
*/
void H264SliceDecodingProcess::DecodeH264PictureOrderCountType2(H264PictureContext::ptr prevPictrue, H264SpsSyntax::ptr sps, H264SliceHeaderSyntax::ptr slice, uint8_t nal_ref_idc, H264PictureContext::ptr picture)
{
int64_t FrameNumOffset = 0;
int64_t tempPicOrderCnt = 0;
uint64_t prevFrameNumOffset = 0;
// determine prevFrameNumOffset
{
if (slice->slice_type != H264SliceType::MMP_H264_I_SLICE)
{
if (prevPictrue->has_memory_management_control_operation_5)
{
prevFrameNumOffset = 0;
}
else
{
prevFrameNumOffset = prevPictrue->FrameNumOffset;
}
}
}
// determine FrameNumOffset (8-11)
{
if (slice->slice_type == H264SliceType::MMP_H264_I_SLICE)
{
FrameNumOffset = 0;
}
else if (prevFrameNumOffset > slice->frame_num)
{
uint32_t MaxFrameNum = sps->context->MaxFrameNum;
FrameNumOffset = prevFrameNumOffset + MaxFrameNum;
}
else
{
FrameNumOffset = prevFrameNumOffset;
}
picture->FrameNumOffset = FrameNumOffset;
}
// determine tempPicOrderCnt (8-12)
{
if (slice->slice_type == H264SliceType::MMP_H264_I_SLICE)
{
tempPicOrderCnt = 0;
}
else if (nal_ref_idc == 0)
{
tempPicOrderCnt = 2 * (FrameNumOffset + slice->frame_num) - 1;
}
else
{
tempPicOrderCnt = 2 * (FrameNumOffset + slice->frame_num);
}
}
// determine TopFieldOrderCnt and BottomFieldOrderCnt (8-13)
{
if (!slice->field_pic_flag)
{
picture->TopFieldOrderCnt = (int32_t)tempPicOrderCnt;
picture->BottomFieldOrderCnt = (int32_t)tempPicOrderCnt;
}
else if (slice->bottom_field_flag)
{
picture->BottomFieldOrderCnt = (int32_t)tempPicOrderCnt;
}
else
{
picture->TopFieldOrderCnt = (int32_t)tempPicOrderCnt;
}
}
}
/*************************************** 8.2.1 Decoding process for picture order count(End) ******************************************/
/*************************************** 8.2.4 Decoding process for reference picture lists construction(Begin) ******************************************/
/**
* @sa ISO 14496/10(2020) - 8.2.4 Decoding process for reference picture lists construction
*/
void H264SliceDecodingProcess::DecodingProcessForReferencePictureListsConstruction(H264SliceHeaderSyntax::ptr slice, H264SpsSyntax::ptr sps, H264PictureContext::cache pictures, H264PictureContext::ptr picture)
{
DecodingProcessForPictureNumbers(slice, sps, pictures, picture);
InitializationProcessForReferencePictureLists(slice, pictures, picture);
if (slice->rplm->ref_pic_list_modification_flag_l0 ||
(slice->rplm->ref_pic_list_modification_flag_l1 && slice->slice_type == H264SliceType::MMP_H264_B_SLICE)
)
{
ModificationProcessForReferencePictureLists(slice, sps, pictures, picture);
}
}
/**
* @sa ISO 14496/10(2020) - 8.2.4.1 Decoding process for picture numbers
*/
void H264SliceDecodingProcess::DecodingProcessForPictureNumbers(H264SliceHeaderSyntax::ptr slice, H264SpsSyntax::ptr sps, H264PictureContext::cache pictures, H264PictureContext::ptr picture)
{
// determine FrameNumWrap (8-27)
{
uint32_t MaxFrameNum = sps->context->MaxFrameNum;
for (auto _picture : pictures)
{
if (_picture->referenceFlag & H264PictureContext::used_for_short_term_reference)
{
if (_picture->FrameNum > slice->frame_num)
{
_picture->FrameNumWrap = (int32_t)_picture->FrameNum - (int32_t)MaxFrameNum;
}
else
{
_picture->FrameNumWrap = _picture->FrameNum;
}
}
}
}
// determine PicNum and LongTermPicNum
{
for (auto _picture : pictures)
{
if (_picture->field_pic_flag == 0)
{
if (_picture->referenceFlag & H264PictureContext::used_for_short_term_reference)
{
_picture->PicNum = _picture->FrameNumWrap; // (8-28)
}
if (_picture->referenceFlag & H264PictureContext::used_for_long_term_reference)
{
_picture->LongTermPicNum = (uint32_t)_picture->LongTermFrameIdx; // (8-29)
}
}
else if (_picture->field_pic_flag == 1)
{
// TOCHECK : top field 一定出现在 bottom field 之前吗, 如何确定 same parity 和 oppsite partity
// WORKAROUND :
// 1) 是不是直接不支持场编码问题就都解决了, 当前 h264 场编码已经应用得比较少了;
// 支持场编码一方面逻辑异常复杂,另一方面也不好进行测试验证
// 2) 确认一下 FFmpeg 6.x (FFmpeg/libavcodec/h264_refs.c) 这部分的代码逻辑 (,但是看起来不是很好确认 ...)
// if (_picture->referenceFlag & H264PictureContext::used_for_short_term_reference)
// {
// if (_picture->bottom_field_flag)
// {
// _picture->PicNum = 2 * _picture->FrameNumWrap + 1; // (8-30)
// }
// else
// {
// _picture->PicNum = 2 * _picture->FrameNumWrap; // (8-31)
// }
// }
// if (_picture->referenceFlag & H264PictureContext::used_for_long_term_reference)
// {
// if (_picture->bottom_field_flag)
// {
// _picture->LongTermPicNum = 2 * _picture->LongTermFrameIdx + 1;
// }
// else
// {
// _picture->LongTermPicNum = 2 * picture->LongTermFrameIdx;
// }
// }
// Hint : not support for now
assert(false);
}
}
}
}
/**
* @sa ISO 14496/10(2020) - 8.2.4.2 Initialization process for reference picture lists
*/
void H264SliceDecodingProcess::InitializationProcessForReferencePictureLists(H264SliceHeaderSyntax::ptr slice, H264PictureContext::cache pictures, H264PictureContext::ptr picture)
{
{
_RefPicList0.clear();
_RefPicList1.clear();
}
// 8.2.4.2.1 Initialization process for the reference picture list for P and SP slices in frames
if ((slice->slice_type == H264SliceType::MMP_H264_P_SLICE || slice->slice_type == H264SliceType::MMP_H264_SP_SLICE) && slice->field_pic_flag == 0)
{
std::vector<H264PictureContext::ptr> shortTermRefPicList; // the highest PicNum value in descending order
std::vector<H264PictureContext::ptr> longTermRefList; // the lowest LongTermPicNum value in ascending order
for (auto picture : pictures)
{
if (picture->referenceFlag & H264PictureContext::used_for_short_term_reference)
{
shortTermRefPicList.push_back(picture);
}
if (picture->referenceFlag & H264PictureContext::used_for_long_term_reference)
{
longTermRefList.push_back(picture);
}
}
std::sort(shortTermRefPicList.begin(), shortTermRefPicList.end(), [](const H264PictureContext::ptr& left, const H264PictureContext::ptr& right) -> bool
{
return left->PicNum > right->PicNum;
});
std::sort(longTermRefList.begin(), longTermRefList.end(), [](const H264PictureContext::ptr& left, const H264PictureContext::ptr& right) -> bool
{
return left->LongTermPicNum < right->LongTermPicNum;
});
MPP_H264_SD_LOG("-- shortTermRefPicList(%d) longTermRefList(%d)", (uint32_t)shortTermRefPicList.size(), (uint32_t)longTermRefList.size());
for (auto& shortTermPicture : shortTermRefPicList)
{
_RefPicList0.push_back(shortTermPicture);
}
for (auto& longTermPicture : longTermRefList)
{
_RefPicList0.push_back(longTermPicture);
}
}
// 8.2.4.2.2 Initialization process for the reference picture list for P and SP slices in fields
else if ((slice->slice_type == H264SliceType::MMP_H264_P_SLICE || slice->slice_type == H264SliceType::MMP_H264_SP_SLICE) && slice->field_pic_flag == 1)
{
// Hint : not support field for now
assert(false);
}
// 8.2.4.2.3 Initialization process for reference picture lists for B slices in frames
else if ((slice->slice_type == H264SliceType::MMP_H264_B_SLICE) && slice->field_pic_flag == 0)
{
int32_t curPoc = PicOrderCnt(picture);
{
std::deque<H264PictureContext::ptr> RefPicList01; // short term : PicOrderCnt( entryShortTerm ) less than PicOrderCnt( CurrPic ) in descending order
std::vector<H264PictureContext::ptr> RefPicList02; // short term : others in ascending order
std::vector<H264PictureContext::ptr> RefPicList03; // long term : the lowest LongTermPicNum value in ascending order
{
H264PictureContext::ptr minPicture = nullptr;
for (auto __picture : _pictures)
{
if (__picture->referenceFlag & H264PictureContext::used_for_short_term_reference)
{
if (PicOrderCnt(__picture) < curPoc)
{
RefPicList01.push_back(__picture);
}
}
}
std::sort(RefPicList01.begin(), RefPicList01.end(), [](const H264PictureContext::ptr& left, const H264PictureContext::ptr& right)->bool
{
return PicOrderCnt(left) > PicOrderCnt(right);
});
}
{
for (auto __picture : _pictures)
{
if (__picture->referenceFlag & H264PictureContext::used_for_short_term_reference)
{
if (PicOrderCnt(__picture) > curPoc)
{
RefPicList02.push_back(__picture);
}
}
}
std::sort(RefPicList02.begin(), RefPicList02.end(), [](const H264PictureContext::ptr& left, const H264PictureContext::ptr& right)->bool
{
return PicOrderCnt(left) < PicOrderCnt(right);
});
}
{
for (auto picture : pictures)
{
if (picture->referenceFlag & H264PictureContext::used_for_long_term_reference)
{
RefPicList03.push_back(picture);
}
}
std::sort(RefPicList03.begin(), RefPicList03.end(), [](const H264PictureContext::ptr& left, const H264PictureContext::ptr& right) -> bool
{
return left->LongTermPicNum < right->LongTermPicNum;
});
}
MPP_H264_SD_LOG("-- RefPicList01(%d) RefPicList02(%d) RefPicList03(%d)", (uint32_t)RefPicList01.size(), (uint32_t)RefPicList02.size(), (uint32_t)RefPicList03.size());
for (const auto& RefPic : RefPicList01)
{
_RefPicList0.push_back(RefPic);
}
for (const auto& RefPic : RefPicList02)
{
_RefPicList0.push_back(RefPic);
}
for (const auto& RefPic : RefPicList03)
{
_RefPicList0.push_back(RefPic);
}
}
{
std::vector<H264PictureContext::ptr> RefPicList11; // short term : PicOrderCnt( entryShortTerm ) greater than PicOrderCnt( CurrPic ) in descending order
std::vector<H264PictureContext::ptr> RefPicList12; // short term : others in ascending order
std::vector<H264PictureContext::ptr> RefPicList13; // long term : the lowest LongTermPicNum value in ascending order
{
for (auto __picture : _pictures)
{
if (__picture->referenceFlag & H264PictureContext::used_for_short_term_reference)
{
int32_t picPoc = PicOrderCnt(__picture);
if (picPoc > curPoc)
{
RefPicList11.push_back(__picture);
}
}
}
std::sort(RefPicList11.begin(), RefPicList11.end(), [](const H264PictureContext::ptr& left, const H264PictureContext::ptr& right)->bool
{
return PicOrderCnt(left) < PicOrderCnt(right);
});
}
{
for (auto __picture : _pictures)
{
if (__picture->referenceFlag & H264PictureContext::used_for_short_term_reference)
{
int32_t picPoc = PicOrderCnt(__picture);
if (picPoc < curPoc)
{
RefPicList12.push_back(__picture);
}
}
}
std::sort(RefPicList12.begin(), RefPicList12.end(), [](const H264PictureContext::ptr& left, const H264PictureContext::ptr& right)->bool
{
return PicOrderCnt(left) > PicOrderCnt(right);
});
}
{
for (auto picture : pictures)
{
if (picture->referenceFlag & H264PictureContext::used_for_long_term_reference)
{
RefPicList13.push_back(picture);
}
}
std::sort(RefPicList13.begin(), RefPicList13.end(), [](const H264PictureContext::ptr& left, const H264PictureContext::ptr& right) -> bool
{
return left->LongTermPicNum > right->LongTermPicNum;
});
}
MPP_H264_SD_LOG("-- RefPicList11(%d) RefPicList12(%d) RefPicList13(%d)", (uint32_t)RefPicList11.size(), (uint32_t)RefPicList12.size(), (uint32_t)RefPicList13.size());
for (const auto& RefPic : RefPicList11)
{
_RefPicList1.push_back(RefPic);
}
for (const auto& RefPic : RefPicList12)
{
_RefPicList1.push_back(RefPic);
}
for (const auto& RefPic : RefPicList13)
{
_RefPicList1.push_back(RefPic);
}
}
}
else if ((slice->slice_type == H264SliceType::MMP_H264_B_SLICE) && slice->field_pic_flag == 1)
{
// Hint : not support for now
assert(false);
}
{
_RefPicList0.resize(slice->num_ref_idx_l0_active_minus1 + 1);
_RefPicList1.resize(slice->num_ref_idx_l1_active_minus1 + 1);
}
#ifdef ENABLE_MMP_SD_DEBUG
static auto refTypeToStr = [](uint64_t referenceFlag) -> std::string
{
if (referenceFlag & H264PictureContext::used_for_short_term_reference)
{
return "ST";
}
else if (referenceFlag & H264PictureContext::used_for_long_term_reference)
{
return "LT";
}
else
{
assert(false);
return "?";
}
};
{
std::stringstream ss;
ss << std::endl << "[IPRC] RefPicList0[" << _RefPicList0.size() << "]" << std::endl;
for (size_t i=0; i<_RefPicList0.size() && _RefPicList0[i]; i++)
{
ss << "-- (" << i << ") Type(" << refTypeToStr(_RefPicList0[i]->referenceFlag) << ") "
<< "FrameNum(" << _RefPicList0[i]->FrameNum << ")";
if (_RefPicList0[i]->referenceFlag & H264PictureContext::used_for_short_term_reference)
{
ss << " PicOrderCnt(" << PicOrderCnt(_RefPicList0[i]) << ") PicNum(" << _RefPicList0[i]->PicNum << ")";
}
else if (_RefPicList0[i]->referenceFlag & H264PictureContext::used_for_long_term_reference)
{
ss << " LongTermPicNum(" << _RefPicList0[i]->LongTermPicNum << ")";
}
if (i + 1 != _RefPicList0.size())
{
ss << std::endl;
}
}
std::string log = ss.str();
MPP_H264_SD_LOG("%s", log.c_str());
}
{
std::stringstream ss;
ss << std::endl << "[IPRC] RefPicList1[" << _RefPicList1.size() << "]" << std::endl;
for (size_t i=0; i<_RefPicList1.size() && _RefPicList1[i]; i++)
{
ss << "-- (" << i << ") Type(" << refTypeToStr(_RefPicList1[i]->referenceFlag) << ") "
<< "FrameNum(" << _RefPicList1[i]->FrameNum << ")";
if (_RefPicList1[i]->referenceFlag & H264PictureContext::used_for_short_term_reference)
{
ss << " PicOrderCnt(" << PicOrderCnt(_RefPicList1[i]) << ") PicNum(" << _RefPicList1[i]->PicNum << ")";
}
else if (_RefPicList1[i]->referenceFlag & H264PictureContext::used_for_long_term_reference)
{
ss << " LongTermPicNum(" << _RefPicList1[i]->LongTermPicNum << ")";
}
if (i + 1 != _RefPicList1.size())
{
ss << std::endl;
}
}
std::string log = ss.str();
MPP_H264_SD_LOG("%s", log.c_str());
}
#endif /* ENABLE_MMP_SD_DEBUG */
}
/**
* @sa ISO 14496/10(2020) - 8.2.4.3 Modification process for reference picture lists
*/
void H264SliceDecodingProcess::ModificationProcessForReferencePictureLists(H264SliceHeaderSyntax::ptr slice, H264SpsSyntax::ptr sps, H264PictureContext::cache pictures, H264PictureContext::ptr picture)
{
uint64_t MaxPicNum = 0;
uint64_t CurrPicNum = 0;
int64_t picNumLX = 0;
size_t index = 0;
// determine CurrPicNum
// The variable CurrPicNum is derived as follows:
// - If field_pic_flag is equal to 0, CurrPicNum is set equal to frame_num.
// - Otherwise (field_pic_flag is equal to 1), CurrPicNum is set equal to 2 * frame_num + 1.
{
if (slice->field_pic_flag == 0)
{
CurrPicNum = slice->frame_num;
}
else if (slice->field_pic_flag == 1)
{
// CurrPicNum = 2 * slice->frame_num + 1;
// Hint : not support for now
assert(false);
}
}
// determine MaxPicNum
// The variable MaxPicNum is derived as follows:
// - If field_pic_flag is equal to 0, MaxPicNum is set equal to MaxFrameNum.
// - Otherwise (field_pic_flag is equal to 1), MaxPicNum is set equal to 2*MaxFrameNum.
{
uint32_t MaxFrameNum = sps->context->MaxFrameNum;
if (slice->field_pic_flag == 0)
{
MaxPicNum = MaxFrameNum;
}
else if (slice->field_pic_flag == 1)
{
// MaxPicNum = 2 * MaxFrameNum;
// Hint : not support for now