forked from nvpro-samples/vk_video_samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVkEncoderDpbH264.cpp
More file actions
1745 lines (1545 loc) · 76.4 KB
/
VkEncoderDpbH264.cpp
File metadata and controls
1745 lines (1545 loc) · 76.4 KB
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 2023 NVIDIA Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This is outside the driver
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <algorithm>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdint.h>
#include "VkEncoderDpbH264.h"
#define VK_DPB_DBG_PRINT(expr) printf expr
#define MARKING_UNUSED 0 // unused for reference
#define MARKING_SHORT 1 // used for short-term reference
#define MARKING_LONG 2 // used for long-term reference
#define INF_MIN ((int32_t)(1 << 31))
#define INF_MAX (~(1 << 31))
enum DpbStateH264 { DPB_EMPTY = 0, DPB_TOP, DPB_BOTTOM, DPB_FRAME };
// helper functions for refpic list intialization and reoirdering
static bool sort_check_short_term_P_frame(const DpbEntryH264 *pDBP, StdVideoH264PocType pic_order_cnt_type, int32_t *pv)
{
*pv = pDBP->topPicNum;
return ((pDBP->top_field_marking == MARKING_SHORT) && (pDBP->bottom_field_marking == MARKING_SHORT));
}
static bool sort_check_short_term_P_field(const DpbEntryH264 *pDBP, StdVideoH264PocType pic_order_cnt_type, int32_t *pv)
{
*pv = pDBP->frameNumWrap;
return (pDBP->top_field_marking == MARKING_SHORT) || (pDBP->bottom_field_marking == MARKING_SHORT);
}
static bool sort_check_short_term_B_frame(const DpbEntryH264 *pDBP, StdVideoH264PocType pic_order_cnt_type, int32_t *pv)
{
*pv = pDBP->picInfo.PicOrderCnt;
return !((pic_order_cnt_type == STD_VIDEO_H264_POC_TYPE_0) && pDBP->not_existing) && (pDBP->top_field_marking == MARKING_SHORT) &&
(pDBP->bottom_field_marking == MARKING_SHORT);
}
static bool sort_check_short_term_B_field(const DpbEntryH264 *pDBP, StdVideoH264PocType pic_order_cnt_type, int32_t *pv)
{
*pv = pDBP->picInfo.PicOrderCnt;
return !((pic_order_cnt_type == STD_VIDEO_H264_POC_TYPE_0) && pDBP->not_existing) &&
((pDBP->top_field_marking == MARKING_SHORT) || (pDBP->bottom_field_marking == MARKING_SHORT));
}
static bool sort_check_long_term_frame(const DpbEntryH264 *pDBP, StdVideoH264PocType pic_order_cnt_type, int32_t *pv)
{
*pv = pDBP->topLongTermPicNum;
return (pDBP->top_field_marking == MARKING_LONG) && (pDBP->bottom_field_marking == MARKING_LONG);
}
static bool sort_check_long_term_field(const DpbEntryH264 *pDBP, StdVideoH264PocType pic_order_cnt_type, int32_t *pv)
{
*pv = pDBP->longTermFrameIdx;
return (pDBP->top_field_marking == MARKING_LONG) || (pDBP->bottom_field_marking == MARKING_LONG);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
VkEncDpbH264::VkEncDpbH264()
: m_maxLongTermFrameIdx(0),
m_max_dpb_size(0),
m_prevPicOrderCntMsb(0),
m_prevPicOrderCntLsb(0),
m_prevFrameNumOffset(0),
m_prevFrameNum(0),
m_PrevRefFrameNum(0),
m_currDpbIdx(0),
m_lastIDRTimeStamp(0)
{
memset(m_max_num_list, 0, sizeof(m_max_num_list));
}
VkEncDpbH264::~VkEncDpbH264() {}
VkEncDpbH264 *VkEncDpbH264::CreateInstance(void)
{
VkEncDpbH264 *pDpb = new VkEncDpbH264();
if (pDpb) {
pDpb->DpbInit();
}
return pDpb;
}
void VkEncDpbH264::ReleaseFrame(VkSharedBaseObj<VulkanVideoImagePoolNode>& dpbImageView)
{
dpbImageView = nullptr;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void VkEncDpbH264::DpbInit()
{
m_max_dpb_size = 0;
m_max_num_list[0] = 0;
m_max_num_list[1] = 0;
m_currDpbIdx = -1;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void VkEncDpbH264::DpbDeinit()
{
m_max_dpb_size = 0;
m_lastIDRTimeStamp = 0;
m_currDpbIdx = -1;
};
void VkEncDpbH264::DpbDestroy()
{
FlushDpb();
DpbDeinit();
delete this;
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
//
// The number of entries DPB_N should at least be equal to the max number of references (R) + decoded pictures that cannot
// be displayed yet + 1 (current picture to be reconstructed). At the end of the reconstruction of the current picture,
// if it is not a reference picture and can be displayed, the picture will not be part of the fullness of the DPB. The number
// of entries DPB_N = dpb_size (as viewed by H264 std) + 1
// returns -1 if err
int32_t VkEncDpbH264::DpbSequenceStart(int32_t userDpbSize)
{
int32_t i;
DpbDeinit();
m_max_dpb_size = userDpbSize;
for (i = 0; i < MAX_DPB_SLOTS + 1; i++) {
m_DPB[i] = DpbEntryH264();
}
if (1) //(!no_output_of_prior_pics_flag)
FlushDpb();
return 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int8_t VkEncDpbH264::DpbPictureStart(const PicInfoH264 *pPicInfo,
const StdVideoH264SequenceParameterSet *sps)
{
FillFrameNumGaps(pPicInfo, sps);
// select decoded picture buffer
// check if this is the second field of a complementary field pair
//
// 3.30 complementary non-reference field pair:
// Two non-reference fields that are in consecutive access units in decoding order as
// - two coded fields of opposite parity where
// - the first field is not already a paired field.
//
// 3.31 complementary reference field pair:
// Two reference fields that are in consecutive access units in decoding order as
// - two coded fields and
// - share the same value of the frame_num syntax element, where
// - the second field in decoding order is not an IDR picture and
// - does not include a memory_management_control_operation syntax element equal to 5.
// Check if there is a current picture (m_currDpbIdx must be valid)
if ((m_currDpbIdx >= 0) && (m_currDpbIdx < MAX_DPB_SLOTS) &&
((m_DPB[m_currDpbIdx].state == DPB_TOP) || (m_DPB[m_currDpbIdx].state == DPB_BOTTOM)) && // contains a single field
pPicInfo->field_pic_flag && // current is a field
(((m_DPB[m_currDpbIdx].state == DPB_TOP) && pPicInfo->bottom_field_flag) ||
((m_DPB[m_currDpbIdx].state == DPB_BOTTOM) && !pPicInfo->bottom_field_flag)) && // opposite parity
((!m_DPB[m_currDpbIdx].reference_picture && // first is a non-reference picture
!pPicInfo->flags.is_reference) // current is a non-reference picture
|| (m_DPB[m_currDpbIdx].reference_picture && // first is reference picture
pPicInfo->flags.is_reference && // current is reference picture
(m_DPB[m_currDpbIdx].picInfo.frame_num == pPicInfo->frame_num) && // same frame_num
!pPicInfo->flags.IdrPicFlag))) { // current is not an IDR picture
// second field
m_DPB[m_currDpbIdx].complementary_field_pair = true;
} else {
m_currDpbIdx = MAX_DPB_SLOTS;
DpbEntryH264 *pCurDPBEntry = &m_DPB[m_currDpbIdx];
if (pCurDPBEntry->state != DPB_EMPTY) {
OutputPicture(m_currDpbIdx, true);
}
// initialize DPB frame buffer
pCurDPBEntry->state = DPB_EMPTY;
pCurDPBEntry->top_needed_for_output = pCurDPBEntry->bottom_needed_for_output = false;
pCurDPBEntry->top_field_marking = pCurDPBEntry->bottom_field_marking = MARKING_UNUSED;
pCurDPBEntry->reference_picture = pPicInfo->flags.is_reference;
pCurDPBEntry->top_decoded_first = !pPicInfo->bottom_field_flag;
pCurDPBEntry->complementary_field_pair = false;
pCurDPBEntry->not_existing = false;
pCurDPBEntry->picInfo.frame_num = pPicInfo->frame_num;
pCurDPBEntry->timeStamp = pPicInfo->timeStamp;
pCurDPBEntry->frame_is_corrupted = false;
if (pPicInfo->flags.IdrPicFlag) {
m_lastIDRTimeStamp = pPicInfo->timeStamp;
}
}
CalculatePOC(pPicInfo, sps);
CalculatePicNum(pPicInfo, sps);
return m_currDpbIdx;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// per picture processing after decoding last slice
int8_t VkEncDpbH264::DpbPictureEnd(const PicInfoH264 *pPicInfo,
VkSharedBaseObj<VulkanVideoImagePoolNode>& dpbImageView,
const StdVideoH264SequenceParameterSet *sps,
const StdVideoEncodeH264SliceHeader *slh,
const StdVideoEncodeH264ReferenceListsInfo *ref,
uint32_t maxMemMgmntCtrlOpsCommands)
{
DpbEntryH264 *pCurDPBEntry = &m_DPB[m_currDpbIdx];
if (pCurDPBEntry->complementary_field_pair) // second field of a CFP
pCurDPBEntry->picInfo.PicOrderCnt = std::min(pCurDPBEntry->topFOC, pCurDPBEntry->bottomFOC);
if (pPicInfo->flags.is_reference) // reference picture
DecodedRefPicMarking(pPicInfo, sps, slh, ref, maxMemMgmntCtrlOpsCommands);
// C.4.4 Removal of pictures from the DPB before possible insertion of the current picture
if (pPicInfo->flags.IdrPicFlag) { // IDR picture
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
m_DPB[i].top_field_marking = MARKING_UNUSED;
m_DPB[i].bottom_field_marking = MARKING_UNUSED;
m_DPB[i].state = DPB_EMPTY;
ReleaseFrame(m_DPB[i].dpbImageView);
}
}
if ((pPicInfo->flags.IdrPicFlag && !pPicInfo->flags.no_output_of_prior_pics_flag)) {
while (!IsDpbEmpty()) DpbBumping(false);
}
// C.4.5
if (pPicInfo->flags.is_reference) { // reference picture
// C.4.5.1
if (pCurDPBEntry->state == DPB_EMPTY) {
while (IsDpbFull()) {
DpbBumping(true);
}
// find an empty DPB entry, copy current to it
for (m_currDpbIdx = 0; m_currDpbIdx < MAX_DPB_SLOTS; m_currDpbIdx++) {
if (m_DPB[m_currDpbIdx].state == DPB_EMPTY)
break;
}
if (pCurDPBEntry != &m_DPB[m_currDpbIdx]) {
ReleaseFrame(m_DPB[m_currDpbIdx].dpbImageView);
m_DPB[m_currDpbIdx] = *pCurDPBEntry;
}
pCurDPBEntry = &m_DPB[m_currDpbIdx];
}
if (!pPicInfo->field_pic_flag || !pPicInfo->bottom_field_flag) {
pCurDPBEntry->state |= DPB_TOP;
pCurDPBEntry->top_needed_for_output = true;
}
if (!pPicInfo->field_pic_flag || pPicInfo->bottom_field_flag) {
pCurDPBEntry->state |= DPB_BOTTOM;
pCurDPBEntry->bottom_needed_for_output = true;
}
} else {
// C.4.5.2
if (pCurDPBEntry->state != DPB_EMPTY) {
if (m_currDpbIdx >= MAX_DPB_SLOTS) {
// output immediately
OutputPicture(m_currDpbIdx, true);
m_DPB[m_currDpbIdx].top_needed_for_output = 0;
m_DPB[m_currDpbIdx].bottom_needed_for_output = 0;
pCurDPBEntry->state = DPB_EMPTY;
} else {
// second field of a complementary non-reference field pair
pCurDPBEntry->state = DPB_FRAME;
pCurDPBEntry->top_needed_for_output = true;
pCurDPBEntry->bottom_needed_for_output = true;
}
} else {
while (1) {
if (IsDpbFull()) {
int32_t i = 0;
// does current have the lowest value of PicOrderCnt?
for (; i < MAX_DPB_SLOTS; i++) {
assert(m_DPB[i].topFOC >= 0);
assert(m_DPB[i].bottomFOC >= 0);
if (((m_DPB[i].state & DPB_TOP) && m_DPB[i].top_needed_for_output &&
((int32_t)m_DPB[i].topFOC) <= pCurDPBEntry->picInfo.PicOrderCnt) ||
((m_DPB[i].state & DPB_BOTTOM) && m_DPB[i].bottom_needed_for_output &&
((int32_t)m_DPB[i].bottomFOC) <= pCurDPBEntry->picInfo.PicOrderCnt))
break;
}
if (i < MAX_DPB_SLOTS) {
DpbBumping(false);
} else {
// If we reach this point, it means, DPB is full and no slot is available for the current picture.
// Vulkan video encoding requires a DPB slot for the current picture whenever VkVideoEncodeFrameInfo::pSetupReferenceSlot
// is being set. This application always populates this field, and without a valid DPB slot the API cannot be
// configured correctly.
// Corner case: DpbBumping() attempts to free a DPB slot when certain conditions are met.
// One of the condition checked is whether the picture is marked as "needed for output".
// As part of this process, it first outputs the picture by marking the it as "not needed for output".
// Before actually clearing the slot, it checks whether the picture is "unused for reference".
// If so, the slot is emptied. However, if the picture is still "used for reference", the function
// only marks the picture as "not needed for output" and stops there.
//
// The issue arises when a picture is still "used for reference" at the moment DpbBumping() process, but later
// during subsequent DPB management, it becomes "unused for reference". Because the picture was already
// marked as "not needed for output" at the time last DpbBumping() process, it will no longer be considered by
// the bumping logic when the next DpbBumping() process is called. This leaves the slot stuck in the DPB
// even though it is no longer needed for output and not needed for reference.
//
// To avoid leaving such stale entries in the DPB, perform an explicit check for any slot that is
// both "not needed for output" and "unused for reference", and remove it when encountered.
//
//
// Because there is no reference‑counting tied to the "needed for output"/"used for reference"
// and "not needed for output"/"unused for reference", simply marking a picture as "unused for reference"
// does not immediately make its dpbImageView safe to reuse. Pictures still in the encode queue may
// reference it, so releasing the dpbImageView too early can lead to invalid references.
// Moving this cleanup logic into DpbBumping() can therefore cause problems if the picture is still
// referenced at that moment. To avoid such issues, execute this logic only when it is clear that no empty
// DPB slot can be found for the current picture.
//
// TODO: As an enhacement,
// 1. the dpb slot with lowest PicOrderCnt value can be emptied first.
// 2. tie the reference count to the markings "needed for output"/"used for reference"
// and "not needed for output"/"unused for reference" for cleaner implementation.
int32_t i = 0;
for (i = 0; i < MAX_DPB_SLOTS; i++) {
if ((m_DPB[i].state & DPB_TOP) && !m_DPB[i].top_needed_for_output && (m_DPB[i].top_field_marking == MARKING_UNUSED) &&
(m_DPB[i].state & DPB_BOTTOM) && !m_DPB[i].bottom_needed_for_output && (m_DPB[i].bottom_field_marking == MARKING_UNUSED)) {
m_DPB[i].state = DPB_EMPTY;
ReleaseFrame(m_DPB[i].dpbImageView); // release the image resource
break;
}
}
if (i >= MAX_DPB_SLOTS) {
// Reaching this point indicates an error in the DPB management logic.
assert(!"Failed to find a free DPB slot");
break; // exit while (1)
}
#if 0
// DPB is full, current has lowest value of PicOrderCnt
if (!pPicInfo->field_pic_flag) {
// frame: output current picture immediately
OutputPicture(m_currDpbIdx, true);
} else {
// field: wait for second field
if (!pPicInfo->bottom_field_flag) {
pCurDPBEntry->state |= DPB_TOP;
pCurDPBEntry->top_needed_for_output = true;
} else {
pCurDPBEntry->state |= DPB_BOTTOM;
pCurDPBEntry->bottom_needed_for_output = true;
}
}
break; // exit while (1)
#endif
}
} else {
for (m_currDpbIdx = 0; m_currDpbIdx < MAX_DPB_SLOTS; m_currDpbIdx++) {
if (m_DPB[m_currDpbIdx].state == DPB_EMPTY) break;
}
if (pCurDPBEntry != &m_DPB[m_currDpbIdx]) {
ReleaseFrame(m_DPB[m_currDpbIdx].dpbImageView);
m_DPB[m_currDpbIdx] = *pCurDPBEntry;
}
pCurDPBEntry = &m_DPB[m_currDpbIdx];
// store current picture
if (!pPicInfo->field_pic_flag || !pPicInfo->bottom_field_flag) {
pCurDPBEntry->state |= DPB_TOP;
pCurDPBEntry->top_needed_for_output = true;
}
if (!pPicInfo->field_pic_flag || pPicInfo->bottom_field_flag) {
pCurDPBEntry->state |= DPB_BOTTOM;
pCurDPBEntry->bottom_needed_for_output = true;
}
break; // exit while (1)
}
}
}
}
assert(pCurDPBEntry);
if (pCurDPBEntry) {
pCurDPBEntry->dpbImageView = dpbImageView;
}
return m_currDpbIdx;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// 8.2.5.2
void VkEncDpbH264::FillFrameNumGaps(const PicInfoH264 *pPicInfo, const StdVideoH264SequenceParameterSet *sps)
{
int32_t maxFrameNum = 1 << (sps->log2_max_frame_num_minus4 + 4);
// 7.4.3
if (pPicInfo->flags.IdrPicFlag) // IDR picture
m_PrevRefFrameNum = 0;
if (pPicInfo->frame_num != m_PrevRefFrameNum) {
PicInfoH264 picSave = *pPicInfo;
// (7-10)
uint32_t unusedShortTermFrameNum = (m_PrevRefFrameNum + 1) % maxFrameNum;
while (unusedShortTermFrameNum != picSave.frame_num) {
VK_DPB_DBG_PRINT(("gaps_in_frame_num: %d ", unusedShortTermFrameNum));
if (!sps->flags.gaps_in_frame_num_value_allowed_flag) {
VK_DPB_DBG_PRINT(("%s (error)::gap in frame_num not allowed\n", __FUNCTION__));
break;
}
picSave.frame_num = unusedShortTermFrameNum;
picSave.field_pic_flag = 0;
picSave.bottom_field_flag = 0;
picSave.flags.is_reference = 1;
picSave.flags.IdrPicFlag = 0;
picSave.flags.adaptive_ref_pic_marking_mode_flag = 0;
// TODO: what else
// DPB handling (C.4.2)
while (IsDpbFull()) DpbBumping(true);
for (m_currDpbIdx = 0; m_currDpbIdx < MAX_DPB_SLOTS; m_currDpbIdx++) {
if (m_DPB[m_currDpbIdx].state == DPB_EMPTY) {
break;
}
}
if (m_currDpbIdx >= MAX_DPB_SLOTS) VK_DPB_DBG_PRINT(("%s (error)::could not allocate a frame buffer\n", __FUNCTION__));
// initialize DPB frame buffer
DpbEntryH264 *pCurDPBEntry = &m_DPB[m_currDpbIdx];
pCurDPBEntry->picInfo.frame_num = pPicInfo->frame_num;
pCurDPBEntry->complementary_field_pair = false;
if (sps->pic_order_cnt_type != STD_VIDEO_H264_POC_TYPE_0) CalculatePOC(&picSave, sps);
CalculatePicNum(&picSave,sps);
SlidingWindowMemoryManagememt(&picSave, sps);
pCurDPBEntry->top_field_marking = pCurDPBEntry->bottom_field_marking = MARKING_SHORT;
pCurDPBEntry->reference_picture = true;
pCurDPBEntry->top_decoded_first = false;
pCurDPBEntry->not_existing = true;
// C.4.2
pCurDPBEntry->top_needed_for_output = pCurDPBEntry->bottom_needed_for_output = false;
pCurDPBEntry->state = DPB_FRAME;
// this differs from the standard
// empty frame buffers marked as "not needed for output" and "unused for reference"
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
if ((!(m_DPB[i].state & DPB_TOP) ||
(!m_DPB[i].top_needed_for_output && m_DPB[i].top_field_marking == MARKING_UNUSED)) &&
(!(m_DPB[i].state & DPB_BOTTOM) ||
(!m_DPB[i].bottom_needed_for_output && m_DPB[i].bottom_field_marking == MARKING_UNUSED))) {
m_DPB[i].state = DPB_EMPTY;
ReleaseFrame(m_DPB[i].dpbImageView);
}
}
// 7.4.3
m_PrevRefFrameNum = pPicInfo->frame_num; // TODO: only if previous picture was a reference picture?
unusedShortTermFrameNum = (unusedShortTermFrameNum + 1) % maxFrameNum;
}
}
// 7.4.3
if (pPicInfo->flags.is_reference) // reference picture
m_PrevRefFrameNum = pPicInfo->frame_num;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// DPB
bool VkEncDpbH264::IsDpbFull()
{
int32_t dpb_fullness, i;
dpb_fullness = 0;
for (i = 0; i < MAX_DPB_SLOTS; i++) {
if (m_DPB[i].state != DPB_EMPTY) dpb_fullness++;
}
return dpb_fullness >= m_max_dpb_size;
}
bool VkEncDpbH264::IsDpbEmpty()
{
int32_t dpb_fullness, i;
dpb_fullness = 0;
for (i = 0; i < MAX_DPB_SLOTS; i++) {
if (m_DPB[i].state != DPB_EMPTY) dpb_fullness++;
}
return dpb_fullness == 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// C.4.5.3
void VkEncDpbH264::DpbBumping(bool alwaysbump)
{
// select the frame buffer that contains the picture having the smallest value
// of PicOrderCnt of all pictures in the DPB marked as "needed for output"
int32_t pocMin = INF_MAX;
int32_t minFoc = -1;
int32_t prevOutputIdx = -1;
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
if ((m_DPB[i].state & DPB_TOP) && m_DPB[i].top_needed_for_output && (m_DPB[i].topFOC < pocMin)) {
pocMin = m_DPB[i].topFOC;
minFoc = i;
}
if ((m_DPB[i].state & DPB_BOTTOM) && m_DPB[i].bottom_needed_for_output && (m_DPB[i].bottomFOC < pocMin)) {
pocMin = m_DPB[i].bottomFOC;
minFoc = i;
}
}
if (minFoc >= 0) {
OutputPicture(minFoc, false);
m_DPB[minFoc].top_needed_for_output = 0;
m_DPB[minFoc].bottom_needed_for_output = 0;
prevOutputIdx = minFoc;
// empty frame buffer
if ((!(m_DPB[minFoc].state & DPB_TOP) || m_DPB[minFoc].top_field_marking == MARKING_UNUSED) &&
(!(m_DPB[minFoc].state & DPB_BOTTOM) || m_DPB[minFoc].bottom_field_marking == MARKING_UNUSED)) {
m_DPB[minFoc].state = DPB_EMPTY;
ReleaseFrame(m_DPB[minFoc].dpbImageView);
}
}
// Special case to avoid deadlocks
if ((prevOutputIdx < 0) && (alwaysbump)) {
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
if ((m_DPB[i].state & DPB_TOP) && (m_DPB[i].topFOC <= pocMin)) {
pocMin = m_DPB[i].topFOC;
minFoc = i;
}
if ((m_DPB[i].state & DPB_BOTTOM) && (m_DPB[i].bottomFOC <= pocMin)) {
pocMin = m_DPB[i].bottomFOC;
minFoc = i;
}
}
// Only access m_DPB if we found a valid entry
if ((minFoc >= 0) && (minFoc < MAX_DPB_SLOTS)) {
m_DPB[minFoc].state = DPB_EMPTY;
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// 8.2.5, 8.2.5.1
void VkEncDpbH264::DecodedRefPicMarking(const PicInfoH264 *pPicInfo,
const StdVideoH264SequenceParameterSet *sps,
const StdVideoEncodeH264SliceHeader *slh,
const StdVideoEncodeH264ReferenceListsInfo *ref,
uint32_t maxMemMgmntCtrlOpsCommands)
{
DpbEntryH264 *pCurDPBEntry = &m_DPB[m_currDpbIdx];
if (pPicInfo->flags.IdrPicFlag) { // IDR picture
// All reference pictures shall be marked as "unused for reference"
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
m_DPB[i].top_field_marking = MARKING_UNUSED;
m_DPB[i].bottom_field_marking = MARKING_UNUSED;
}
if (!pPicInfo->flags.long_term_reference_flag) {
// the IDR picture shall be marked as "used for short-term reference"
if (!pPicInfo->field_pic_flag || !pPicInfo->bottom_field_flag)
pCurDPBEntry->top_field_marking = MARKING_SHORT;
if (!pPicInfo->field_pic_flag || pPicInfo->bottom_field_flag)
pCurDPBEntry->bottom_field_marking = MARKING_SHORT;
// MaxLongTermFrameIdx shall be set equal to "no long-term frame indices".
m_maxLongTermFrameIdx = -1;
} else { // (slh->long_term_reference_flag == 1)
// the IDR picture shall be marked as "used for long-term reference"
if (!pPicInfo->field_pic_flag || !pPicInfo->bottom_field_flag)
pCurDPBEntry->top_field_marking = MARKING_LONG;
if (!pPicInfo->field_pic_flag || pPicInfo->bottom_field_flag)
pCurDPBEntry->bottom_field_marking = MARKING_LONG;
// the LongTermFrameIdx for the IDR picture shall be set equal to 0
pCurDPBEntry->longTermFrameIdx = 0;
// MaxLongTermFrameIdx shall be set equal to 0.
m_maxLongTermFrameIdx = 0;
}
} else {
if (!pPicInfo->flags.adaptive_ref_pic_marking_mode_flag)
SlidingWindowMemoryManagememt(pPicInfo, sps);
else // (slh->adaptive_ref_pic_marking_mode_flag == 1)
AdaptiveMemoryManagement(pPicInfo, ref, maxMemMgmntCtrlOpsCommands);
// mark current as short-term if not marked as long-term (8.2.5.1)
if ((!pPicInfo->field_pic_flag || !pPicInfo->bottom_field_flag) &&
pCurDPBEntry->top_field_marking == MARKING_UNUSED)
pCurDPBEntry->top_field_marking = MARKING_SHORT;
if ((!pPicInfo->field_pic_flag || pPicInfo->bottom_field_flag) &&
pCurDPBEntry->bottom_field_marking == MARKING_UNUSED)
pCurDPBEntry->bottom_field_marking = MARKING_SHORT;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// 8.2.5.3
void VkEncDpbH264::SlidingWindowMemoryManagememt(const PicInfoH264 *pPicInfo, const StdVideoH264SequenceParameterSet *sps)
{
// If the current picture is a coded field that is the second field in decoding order
// of a complementary reference field pair, and the first field has been marked as
// "used for short-term reference", the current picture is also marked as
// "used for short-term reference".
// note: I think this could be simplified as
// if (m_pCurDPBEntry->top_field_marking == MARKING_SHORT || m_pCurDPBEntry->bottom_field_marking == MARKING_SHORT)
DpbEntryH264 *pCurDPBEntry = &m_DPB[m_currDpbIdx];
if (pPicInfo->field_pic_flag &&
((!pPicInfo->bottom_field_flag && pCurDPBEntry->bottom_field_marking == MARKING_SHORT) ||
(pPicInfo->bottom_field_flag && pCurDPBEntry->top_field_marking == MARKING_SHORT))) {
if (!pPicInfo->bottom_field_flag)
pCurDPBEntry->top_field_marking = MARKING_SHORT;
else
pCurDPBEntry->bottom_field_marking = MARKING_SHORT;
} else {
int32_t imin = MAX_DPB_SLOTS;
int32_t minFrameNumWrap = 65536;
int32_t numShortTerm = 0;
int32_t numLongTerm = 0;
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
if ((m_DPB[i].top_field_marking == MARKING_SHORT || m_DPB[i].bottom_field_marking == MARKING_SHORT)) {
numShortTerm++;
if (m_DPB[i].frameNumWrap < minFrameNumWrap) {
imin = i;
minFrameNumWrap = m_DPB[i].frameNumWrap;
}
}
if (m_DPB[i].top_field_marking == MARKING_LONG || m_DPB[i].bottom_field_marking == MARKING_LONG) {
numLongTerm++;
}
}
if ((numShortTerm + numLongTerm) >= sps->max_num_ref_frames) {
if (numShortTerm > 0 && imin < MAX_DPB_SLOTS) {
m_DPB[imin].top_field_marking = MARKING_UNUSED;
m_DPB[imin].bottom_field_marking = MARKING_UNUSED;
} else {
VK_DPB_DBG_PRINT(("Detected DPB violation (%d+%d/%d)!\n", numShortTerm, numLongTerm, sps->max_num_ref_frames));
}
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// 8.2.5.4
void VkEncDpbH264::AdaptiveMemoryManagement(const PicInfoH264 *pPicInfo, const StdVideoEncodeH264ReferenceListsInfo *ref,
uint32_t maxMemMgmntCtrlOpsCommands)
{
const StdVideoEncodeH264RefPicMarkingEntry *mmco = ref->pRefPicMarkingOperations;
int32_t currPicNum = (!pPicInfo->field_pic_flag) ? pPicInfo->frame_num : 2 * pPicInfo->frame_num + 1;
int32_t picNumX = 0;
for (uint32_t k = 0; ((k < maxMemMgmntCtrlOpsCommands) && (mmco[k].memory_management_control_operation != STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_END)); k++) {
switch (mmco[k].memory_management_control_operation) {
case STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_UNMARK_SHORT_TERM:
// 8.2.5.4.1 Marking process of a short-term picture as "unused for reference"
VK_DPB_DBG_PRINT(("%d ", mmco[k].difference_of_pic_nums_minus1));
picNumX = currPicNum - (mmco[k].difference_of_pic_nums_minus1 + 1); // (8-40)
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
if (m_DPB[i].top_field_marking == MARKING_SHORT && m_DPB[i].topPicNum == picNumX)
m_DPB[i].top_field_marking = MARKING_UNUSED;
if (m_DPB[i].bottom_field_marking == MARKING_SHORT && m_DPB[i].bottomPicNum == picNumX)
m_DPB[i].bottom_field_marking = MARKING_UNUSED;
}
break;
case STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_UNMARK_LONG_TERM:
// 8.2.5.4.2 Marking process of a long-term picture as "unused for reference"
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
if (m_DPB[i].top_field_marking == MARKING_LONG && m_DPB[i].topLongTermPicNum == (int32_t)mmco[k].long_term_pic_num)
m_DPB[i].top_field_marking = MARKING_UNUSED;
if (m_DPB[i].bottom_field_marking == MARKING_LONG &&
m_DPB[i].bottomLongTermPicNum == (int32_t)mmco[k].long_term_pic_num)
m_DPB[i].bottom_field_marking = MARKING_UNUSED;
}
break;
case STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_MARK_LONG_TERM:
picNumX = currPicNum - (mmco[k].difference_of_pic_nums_minus1 + 1); // (8-40)
// 8.2.5.4.3 Assignment process of a LongTermFrameIdx to a short-term reference picture
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
if (m_DPB[i].top_field_marking == MARKING_LONG &&
m_DPB[i].longTermFrameIdx == (int32_t)mmco[k].long_term_frame_idx &&
!(m_DPB[i].bottom_field_marking == MARKING_SHORT && m_DPB[i].bottomPicNum == picNumX))
m_DPB[i].top_field_marking = MARKING_UNUSED;
if (m_DPB[i].bottom_field_marking == MARKING_LONG &&
m_DPB[i].longTermFrameIdx == (int32_t)mmco[k].long_term_frame_idx &&
!(m_DPB[i].top_field_marking == MARKING_SHORT && m_DPB[i].topPicNum == picNumX))
m_DPB[i].bottom_field_marking = MARKING_UNUSED;
if (m_DPB[i].top_field_marking == MARKING_SHORT && m_DPB[i].topPicNum == picNumX) {
m_DPB[i].top_field_marking = MARKING_LONG;
m_DPB[i].longTermFrameIdx = mmco[k].long_term_frame_idx;
// update topLongTermPicNum, bottomLongTermPicNum for subsequent mmco 2
if (!pPicInfo->field_pic_flag) {
// frame
m_DPB[i].topLongTermPicNum = m_DPB[i].bottomLongTermPicNum = m_DPB[i].longTermFrameIdx; // (8-30)
} else if (!pPicInfo->bottom_field_flag) {
// top field
m_DPB[i].topLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx + 1; // same parity (8-33)
m_DPB[i].bottomLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx; // opposite parity (8-34)
} else {
// bottom field
m_DPB[i].topLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx; // opposite parity (8-34)
m_DPB[i].bottomLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx + 1; // same parity (8-33)
}
}
if (m_DPB[i].bottom_field_marking == MARKING_SHORT && m_DPB[i].bottomPicNum == picNumX) {
m_DPB[i].bottom_field_marking = MARKING_LONG;
m_DPB[i].longTermFrameIdx = mmco[k].long_term_frame_idx;
// update topLongTermPicNum, bottomLongTermPicNum for subsequent mmco 2
if (!pPicInfo->field_pic_flag) {
// frame
m_DPB[i].topLongTermPicNum = m_DPB[i].bottomLongTermPicNum = m_DPB[i].longTermFrameIdx; // (8-30)
} else if (!pPicInfo->bottom_field_flag) {
// top field
m_DPB[i].topLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx + 1; // same parity (8-33)
m_DPB[i].bottomLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx; // opposite parity (8-34)
} else {
// bottom field
m_DPB[i].topLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx; // opposite parity (8-34)
m_DPB[i].bottomLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx + 1; // same parity (8-33)
}
}
}
break;
case STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_SET_MAX_LONG_TERM_INDEX:
// 8.2.5.4.4 Decoding process for MaxLongTermFrameIdx
m_maxLongTermFrameIdx = mmco[k].max_long_term_frame_idx_plus1 - 1;
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
if (m_DPB[i].top_field_marking == MARKING_LONG && m_DPB[i].longTermFrameIdx > m_maxLongTermFrameIdx)
m_DPB[i].top_field_marking = MARKING_UNUSED;
if (m_DPB[i].bottom_field_marking == MARKING_LONG && m_DPB[i].longTermFrameIdx > m_maxLongTermFrameIdx)
m_DPB[i].bottom_field_marking = MARKING_UNUSED;
}
break;
case STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_UNMARK_ALL:
{
// 8.2.5.4.5 Marking process of all reference pictures as "unused for reference" and setting MaxLongTermFrameIdx to
// "no long-term frame indices"
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
m_DPB[i].top_field_marking = MARKING_UNUSED;
m_DPB[i].bottom_field_marking = MARKING_UNUSED;
}
m_maxLongTermFrameIdx = -1;
DpbEntryH264 *pCurDPBEntry = &m_DPB[m_currDpbIdx];
pCurDPBEntry->picInfo.frame_num = 0; // 7.4.3
// 8.2.1
pCurDPBEntry->topFOC -= pCurDPBEntry->picInfo.PicOrderCnt;
pCurDPBEntry->bottomFOC -= pCurDPBEntry->picInfo.PicOrderCnt;
pCurDPBEntry->picInfo.PicOrderCnt = 0;
break;
}
case STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_MARK_CURRENT_AS_LONG_TERM:
{
// 8.2.5.4.6 Process for assigning a long-term frame index to the current picture
DpbEntryH264 *pCurDPBEntry = &m_DPB[m_currDpbIdx];
VK_DPB_DBG_PRINT(("%d ", mmco[k].long_term_frame_idx));
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
if (i != m_currDpbIdx && m_DPB[i].top_field_marking == MARKING_LONG &&
m_DPB[i].longTermFrameIdx == (int32_t)mmco[k].long_term_frame_idx)
m_DPB[i].top_field_marking = MARKING_UNUSED;
if (i != m_currDpbIdx && m_DPB[i].bottom_field_marking == MARKING_LONG &&
m_DPB[i].longTermFrameIdx == (int32_t)mmco[k].long_term_frame_idx)
m_DPB[i].bottom_field_marking = MARKING_UNUSED;
}
if (!pPicInfo->field_pic_flag || !pPicInfo->bottom_field_flag)
pCurDPBEntry->top_field_marking = MARKING_LONG;
if (!pPicInfo->field_pic_flag || pPicInfo->bottom_field_flag)
pCurDPBEntry->bottom_field_marking = MARKING_LONG;
pCurDPBEntry->longTermFrameIdx = mmco[k].long_term_frame_idx;
// update topLongTermPicNum, bottomLongTermPicNum
// (subsequent mmco 2 is not allowed to reference it, but to avoid accidental matches they have to be updated)
if (!pPicInfo->field_pic_flag) {
// frame
pCurDPBEntry->topLongTermPicNum = pCurDPBEntry->bottomLongTermPicNum =
pCurDPBEntry->longTermFrameIdx; // (8-30)
} else if (!pPicInfo->bottom_field_flag) {
// top field
pCurDPBEntry->topLongTermPicNum = 2 * pCurDPBEntry->longTermFrameIdx + 1; // same parity (8-33)
pCurDPBEntry->bottomLongTermPicNum = 2 * pCurDPBEntry->longTermFrameIdx; // opposite parity (8-34)
} else {
// bottom field
pCurDPBEntry->topLongTermPicNum = 2 * pCurDPBEntry->longTermFrameIdx; // opposite parity (8-34)
pCurDPBEntry->bottomLongTermPicNum = 2 * pCurDPBEntry->longTermFrameIdx + 1; // same parity (8-33)
}
break;
}
case STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_END:
case STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_INVALID:
default:
assert(!"Invalid case");
break;
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// 8.2.1
void VkEncDpbH264::CalculatePOC(const PicInfoH264 *pPicInfo, const StdVideoH264SequenceParameterSet *sps)
{
if (sps->pic_order_cnt_type == STD_VIDEO_H264_POC_TYPE_0) {
CalculatePOCType0(pPicInfo, sps);
} else {
CalculatePOCType2(pPicInfo, sps);
}
// (8-1)
DpbEntryH264 *pCurDPBEntry = &m_DPB[m_currDpbIdx];
if (!pPicInfo->field_pic_flag || pCurDPBEntry->complementary_field_pair) // not second field of a CFP
pCurDPBEntry->picInfo.PicOrderCnt = std::min(pCurDPBEntry->topFOC, pCurDPBEntry->bottomFOC);
else if (!pPicInfo->bottom_field_flag)
pCurDPBEntry->picInfo.PicOrderCnt = pCurDPBEntry->topFOC;
else
pCurDPBEntry->picInfo.PicOrderCnt = pCurDPBEntry->bottomFOC;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// 8.2.1.1
void VkEncDpbH264::CalculatePOCType0(const PicInfoH264 *pPicInfo, const StdVideoH264SequenceParameterSet *sps)
{
if (pPicInfo->flags.IdrPicFlag) { // IDR picture
m_prevPicOrderCntMsb = 0;
m_prevPicOrderCntLsb = 0;
}
int32_t picOrderCntMsb = 0;
int32_t maxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4); // (7-2)
// (8-3)
if ((pPicInfo->PicOrderCnt < m_prevPicOrderCntLsb) &&
((m_prevPicOrderCntLsb - pPicInfo->PicOrderCnt) >= (maxPicOrderCntLsb / 2))) {
picOrderCntMsb = m_prevPicOrderCntMsb + maxPicOrderCntLsb;
} else if ((pPicInfo->PicOrderCnt > m_prevPicOrderCntLsb) &&
((pPicInfo->PicOrderCnt - m_prevPicOrderCntLsb) > (maxPicOrderCntLsb / 2))) {
picOrderCntMsb = m_prevPicOrderCntMsb - maxPicOrderCntLsb;
} else {
picOrderCntMsb = m_prevPicOrderCntMsb;
}
DpbEntryH264 *pCurDPBEntry = &m_DPB[m_currDpbIdx];
// (8-4)
if (!pPicInfo->field_pic_flag || !pPicInfo->bottom_field_flag)
pCurDPBEntry->topFOC = picOrderCntMsb + pPicInfo->PicOrderCnt;
// (8-5)
if (!pPicInfo->field_pic_flag || pPicInfo->bottom_field_flag)
pCurDPBEntry->bottomFOC = picOrderCntMsb + pPicInfo->PicOrderCnt;
if (pPicInfo->flags.is_reference) { // reference picture
m_prevPicOrderCntMsb = picOrderCntMsb;
m_prevPicOrderCntLsb = pPicInfo->PicOrderCnt;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// 8.2.1.2 - Unimplemented because we're not going to handle POC type 1.
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// 8.2.1.3
void VkEncDpbH264::CalculatePOCType2(const PicInfoH264 *pPicInfo, const StdVideoH264SequenceParameterSet *sps)
{
int32_t frameNumOffset, tempPicOrderCnt;
int32_t maxFrameNum = 1 << (sps->log2_max_frame_num_minus4 + 4); // (7-1)
// FrameNumOffset (8-12)
if (pPicInfo->flags.IdrPicFlag)
frameNumOffset = 0;
else if (m_prevFrameNum > pPicInfo->frame_num)
frameNumOffset = m_prevFrameNumOffset + maxFrameNum;
else
frameNumOffset = m_prevFrameNumOffset;
// tempPicOrderCnt (8-13)
if (pPicInfo->flags.IdrPicFlag) {
tempPicOrderCnt = 0;
} else if (!pPicInfo->flags.is_reference) {
tempPicOrderCnt = 2 * (frameNumOffset + pPicInfo->frame_num) - 1;
} else {
tempPicOrderCnt = 2 * (frameNumOffset + pPicInfo->frame_num);
}
DpbEntryH264 *pCurDPBEntry = &m_DPB[m_currDpbIdx];
// topFOC, bottomFOC (8-14)
if (!pPicInfo->field_pic_flag) {
pCurDPBEntry->topFOC = tempPicOrderCnt;
pCurDPBEntry->bottomFOC = tempPicOrderCnt;
} else if (pPicInfo->bottom_field_flag)
pCurDPBEntry->bottomFOC = tempPicOrderCnt;
else
pCurDPBEntry->topFOC = tempPicOrderCnt;
m_prevFrameNumOffset = frameNumOffset;
m_prevFrameNum = pPicInfo->frame_num;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// 8.2.4.1 Derivation of picture numbers
void VkEncDpbH264::CalculatePicNum(const PicInfoH264 *pPicInfo, const StdVideoH264SequenceParameterSet *sps)
{
int32_t maxFrameNum = 1 << (sps->log2_max_frame_num_minus4 + 4); // (7-1)
assert(pPicInfo->frame_num != (uint32_t)(-1));
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
// (8-28)
if (m_DPB[i].picInfo.frame_num > ((uint32_t)pPicInfo->frame_num))
m_DPB[i].frameNumWrap = m_DPB[i].picInfo.frame_num - maxFrameNum;
else
m_DPB[i].frameNumWrap = m_DPB[i].picInfo.frame_num;
if (!pPicInfo->field_pic_flag) {
// frame
m_DPB[i].topPicNum = m_DPB[i].bottomPicNum = m_DPB[i].frameNumWrap; // (8-29)
m_DPB[i].topLongTermPicNum = m_DPB[i].bottomLongTermPicNum = m_DPB[i].longTermFrameIdx; // (8-30)
} else if (!pPicInfo->bottom_field_flag) {
// top field
m_DPB[i].topPicNum = 2 * m_DPB[i].frameNumWrap + 1; // same parity (8-31)
m_DPB[i].bottomPicNum = 2 * m_DPB[i].frameNumWrap; // opposite parity (8-32)
m_DPB[i].topLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx + 1; // same parity (8-33)
m_DPB[i].bottomLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx; // opposite parity (8-34)
} else {
// bottom field
m_DPB[i].topPicNum = 2 * m_DPB[i].frameNumWrap; // opposite parity (8-32)
m_DPB[i].bottomPicNum = 2 * m_DPB[i].frameNumWrap + 1; // same parity (8-31)
m_DPB[i].topLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx; // opposite parity (8-34)
m_DPB[i].bottomLongTermPicNum = 2 * m_DPB[i].longTermFrameIdx + 1; // same parity (8-33)
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void VkEncDpbH264::OutputPicture(int32_t dpb_index, bool release)
{
if (release) {
ReleaseFrame(m_DPB[dpb_index].dpbImageView);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void VkEncDpbH264::FlushDpb()
{
// mark all reference pictures as "unused for reference"
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
m_DPB[i].top_field_marking = MARKING_UNUSED;
m_DPB[i].bottom_field_marking = MARKING_UNUSED;
}
// empty frame buffers marked as "not needed for output" and "unused for reference"
for (int32_t i = 0; i < MAX_DPB_SLOTS; i++) {
if ((!(m_DPB[i].state & DPB_TOP) || (!m_DPB[i].top_needed_for_output && m_DPB[i].top_field_marking == MARKING_UNUSED)) &&
(!(m_DPB[i].state & DPB_BOTTOM) ||
(!m_DPB[i].bottom_needed_for_output && m_DPB[i].bottom_field_marking == MARKING_UNUSED))) {
m_DPB[i].state = DPB_EMPTY;
ReleaseFrame(m_DPB[i].dpbImageView);
}
}
while (!IsDpbEmpty()) DpbBumping(true);
}
bool VkEncDpbH264::GetRefPicture(int8_t dpbIdx, VkSharedBaseObj<VulkanVideoImagePoolNode>& dpbImageView)
{
if ((dpbIdx >= 0) && (dpbIdx <= MAX_DPB_SLOTS)) {
dpbImageView = m_DPB[dpbIdx].dpbImageView;
return (dpbImageView != nullptr) ? true : false;
} else {
VK_DPB_DBG_PRINT(("Error : getFrameType : Wrong picture index %d\n", dpbIdx));