/
VulkanVideoParser.cpp
2047 lines (1783 loc) · 81.7 KB
/
VulkanVideoParser.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
/*
* Copyright 2021 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.
*/
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <algorithm>
#include <atomic>
#include <iostream>
#include <queue> // std::queue
#include "vkvideo_parser/VulkanVideoParserIf.h"
#include "NvVideoParser/nvVulkanVideoParser.h"
#include "NvVideoParser/nvVulkanVideoUtils.h"
#include "vkvideo_parser/PictureBufferBase.h"
#include "VkVideoCore/VkVideoCoreProfile.h"
#include "vkvideo_parser/StdVideoPictureParametersSet.h"
#include "vkvideo_parser/VulkanVideoParser.h"
#undef min
#undef max
static const uint32_t topFieldShift = 0;
static const uint32_t topFieldMask = (1 << topFieldShift);
static const uint32_t bottomFieldShift = 1;
static const uint32_t bottomFieldMask = (1 << bottomFieldShift);
static const uint32_t fieldIsReferenceMask = (topFieldMask | bottomFieldMask);
static const uint32_t MAX_DPB_REF_SLOTS = 16;
static const uint32_t MAX_DPB_REF_AND_SETUP_SLOTS = MAX_DPB_REF_SLOTS + 1; // plus 1 for the current picture (h.264 only)
#define COPYFIELD(pout, pin, name) pout->name = pin->name
namespace NvVulkanDecoder
{
struct nvVideoDecodeH264DpbSlotInfo {
VkVideoDecodeH264DpbSlotInfoKHR dpbSlotInfo;
StdVideoDecodeH264ReferenceInfo stdReferenceInfo;
nvVideoDecodeH264DpbSlotInfo()
: dpbSlotInfo()
, stdReferenceInfo()
{
}
const VkVideoDecodeH264DpbSlotInfoKHR* Init(int8_t slotIndex)
{
assert((slotIndex >= 0) && (slotIndex < (int8_t)MAX_DPB_REF_AND_SETUP_SLOTS));
dpbSlotInfo.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_DPB_SLOT_INFO_KHR;
dpbSlotInfo.pNext = NULL;
dpbSlotInfo.pStdReferenceInfo = &stdReferenceInfo;
return &dpbSlotInfo;
}
bool IsReference() const
{
return (dpbSlotInfo.pStdReferenceInfo == &stdReferenceInfo);
}
operator bool() const { return IsReference(); }
void Invalidate() { memset(this, 0x00, sizeof(*this)); }
};
struct nvVideoDecodeH265DpbSlotInfo {
VkVideoDecodeH265DpbSlotInfoKHR dpbSlotInfo;
StdVideoDecodeH265ReferenceInfo stdReferenceInfo;
nvVideoDecodeH265DpbSlotInfo()
: dpbSlotInfo()
, stdReferenceInfo()
{
}
const VkVideoDecodeH265DpbSlotInfoKHR* Init(int8_t slotIndex)
{
assert((slotIndex >= 0) && (slotIndex < (int8_t)MAX_DPB_REF_SLOTS));
dpbSlotInfo.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_DPB_SLOT_INFO_KHR;
dpbSlotInfo.pNext = NULL;
dpbSlotInfo.pStdReferenceInfo = &stdReferenceInfo;
return &dpbSlotInfo;
}
bool IsReference() const
{
return (dpbSlotInfo.pStdReferenceInfo == &stdReferenceInfo);
}
operator bool() const { return IsReference(); }
void Invalidate() { memset(this, 0x00, sizeof(*this)); }
};
/******************************************************/
//! \struct nvVideoH264PicParameters
//! H.264 picture parameters
/******************************************************/
struct nvVideoH264PicParameters {
enum { MAX_REF_PICTURES_LIST_ENTRIES = 16 };
StdVideoDecodeH264PictureInfo stdPictureInfo;
VkVideoDecodeH264PictureInfoKHR pictureInfo;
VkVideoDecodeH264SessionParametersAddInfoKHR pictureParameters;
nvVideoDecodeH264DpbSlotInfo currentDpbSlotInfo;
nvVideoDecodeH264DpbSlotInfo dpbRefList[MAX_REF_PICTURES_LIST_ENTRIES];
};
/*******************************************************/
//! \struct nvVideoH265PicParameters
//! HEVC picture parameters
/*******************************************************/
struct nvVideoH265PicParameters {
enum { MAX_REF_PICTURES_LIST_ENTRIES = 16 };
StdVideoDecodeH265PictureInfo stdPictureInfo;
VkVideoDecodeH265PictureInfoKHR pictureInfo;
VkVideoDecodeH265SessionParametersAddInfoKHR pictureParameters;
nvVideoDecodeH265DpbSlotInfo dpbRefList[MAX_REF_PICTURES_LIST_ENTRIES];
};
static vkPicBuffBase* GetPic(VkPicIf* pPicBuf)
{
return (vkPicBuffBase*)pPicBuf;
}
// Keeps track of data associated with active internal reference frames
class DpbSlot {
public:
bool isInUse() { return (m_reserved || m_inUse); }
bool isAvailable() { return !isInUse(); }
bool Invalidate()
{
bool wasInUse = isInUse();
if (m_picBuf) {
m_picBuf->Release();
m_picBuf = NULL;
}
m_reserved = m_inUse = false;
return wasInUse;
}
vkPicBuffBase* getPictureResource() { return m_picBuf; }
vkPicBuffBase* setPictureResource(vkPicBuffBase* picBuf, int32_t age = 0)
{
vkPicBuffBase* oldPic = m_picBuf;
if (picBuf) {
picBuf->AddRef();
}
m_picBuf = picBuf;
if (oldPic) {
oldPic->Release();
}
m_pictureId = age;
return oldPic;
}
void Reserve() { m_reserved = true; }
void MarkInUse(int32_t age = 0)
{
m_pictureId = age;
m_inUse = true;
}
int32_t getAge() { return m_pictureId; }
private:
int32_t m_pictureId; // PictureID at map time (age)
vkPicBuffBase* m_picBuf; // Associated resource
int32_t m_reserved : 1;
int32_t m_inUse : 1;
};
class DpbSlots {
public:
DpbSlots(uint32_t dpbMaxSize)
: m_dpbMaxSize(0)
, m_slotInUseMask(0)
, m_dpb(m_dpbMaxSize)
, m_dpbSlotsAvailable()
{
Init(dpbMaxSize, false);
}
int32_t Init(uint32_t newDpbMaxSize, bool reconfigure)
{
assert(newDpbMaxSize <= MAX_DPB_REF_AND_SETUP_SLOTS);
if (!reconfigure) {
Deinit();
}
if (reconfigure && (newDpbMaxSize < m_dpbMaxSize)) {
return m_dpbMaxSize;
}
uint32_t oldDpbMaxSize = reconfigure ? m_dpbMaxSize : 0;
m_dpbMaxSize = newDpbMaxSize;
m_dpb.resize(m_dpbMaxSize);
for (uint32_t ndx = oldDpbMaxSize; ndx < m_dpbMaxSize; ndx++) {
m_dpb[ndx].Invalidate();
}
for (uint8_t dpbIndx = oldDpbMaxSize; dpbIndx < m_dpbMaxSize; dpbIndx++) {
m_dpbSlotsAvailable.push(dpbIndx);
}
return m_dpbMaxSize;
}
void Deinit()
{
for (uint32_t ndx = 0; ndx < m_dpbMaxSize; ndx++) {
m_dpb[ndx].Invalidate();
}
while (!m_dpbSlotsAvailable.empty()) {
m_dpbSlotsAvailable.pop();
}
m_dpbMaxSize = 0;
m_slotInUseMask = 0;
}
~DpbSlots() { Deinit(); }
int8_t AllocateSlot()
{
if (m_dpbSlotsAvailable.empty()) {
assert(!"No more h.264/5 DPB slots are available");
return -1;
}
int8_t slot = (int8_t)m_dpbSlotsAvailable.front();
assert((slot >= 0) && ((uint8_t)slot < m_dpbMaxSize));
m_slotInUseMask |= (1 << slot);
m_dpbSlotsAvailable.pop();
m_dpb[slot].Reserve();
return slot;
}
void FreeSlot(int8_t slot)
{
assert((uint8_t)slot < m_dpbMaxSize);
assert(m_dpb[slot].isInUse());
assert(m_slotInUseMask & (1 << slot));
m_dpb[slot].Invalidate();
m_dpbSlotsAvailable.push(slot);
m_slotInUseMask &= ~(1 << slot);
}
DpbSlot& operator[](uint32_t slot)
{
assert(slot < m_dpbMaxSize);
return m_dpb[slot];
}
// Return the remapped index given an external decode render target index
int8_t GetSlotOfPictureResource(vkPicBuffBase* pPic)
{
for (int8_t i = 0; i < (int8_t)m_dpbMaxSize; i++) {
if ((m_slotInUseMask & (1 << i)) && m_dpb[i].isInUse() && (pPic == m_dpb[i].getPictureResource())) {
return i;
}
}
return -1; // not found
}
void MapPictureResource(vkPicBuffBase* pPic, int32_t dpbSlot,
int32_t age = 0)
{
for (uint32_t slot = 0; slot < m_dpbMaxSize; slot++) {
if ((uint8_t)slot == dpbSlot) {
m_dpb[slot].setPictureResource(pPic, age);
} else if (pPic) {
if (m_dpb[slot].getPictureResource() == pPic) {
FreeSlot(slot);
}
}
}
}
uint32_t getSlotInUseMask() { return m_slotInUseMask; }
uint32_t getMaxSize() { return m_dpbMaxSize; }
private:
uint32_t m_dpbMaxSize;
uint32_t m_slotInUseMask;
std::vector<DpbSlot> m_dpb;
std::queue<uint8_t> m_dpbSlotsAvailable;
};
class VulkanVideoParser : public VkParserVideoDecodeClient,
public IVulkanVideoParser {
friend class IVulkanVideoParser;
public:
enum { MAX_FRM_CNT = 32 };
enum { HEVC_MAX_DPB_SLOTS = 16 };
enum { AVC_MAX_DPB_SLOTS = 17 };
enum { MAX_REMAPPED_ENTRIES = 20 };
// H.264 internal DPB structure
typedef struct dpbH264Entry {
int8_t dpbSlot;
// bit0(used_for_reference)=1: top field used for reference,
// bit1(used_for_reference)=1: bottom field used for reference
uint32_t used_for_reference : 2;
uint32_t is_long_term : 1; // 0 = short-term, 1 = long-term
uint32_t is_non_existing : 1; // 1 = marked as non-existing
uint32_t is_field_ref : 1; // set if unpaired field or complementary field pair
union {
int16_t FieldOrderCnt[2]; // h.264 : 2*32 [top/bottom].
int32_t PicOrderCnt; // HEVC PicOrderCnt
};
union {
int16_t FrameIdx; // : 16 short-term: FrameNum (16 bits), long-term:
// LongTermFrameIdx (4 bits)
int8_t originalDpbIndex; // Original Dpb source Index.
};
vkPicBuffBase* m_picBuff; // internal picture reference
void setReferenceAndTopBottomField(
bool isReference, bool nonExisting, bool isLongTerm, bool isFieldRef,
bool topFieldIsReference, bool bottomFieldIsReference, int16_t frameIdx,
const int16_t fieldOrderCntList[2], vkPicBuffBase* picBuff)
{
is_non_existing = nonExisting;
is_long_term = isLongTerm;
is_field_ref = isFieldRef;
if (isReference && isFieldRef) {
used_for_reference = (bottomFieldIsReference << bottomFieldShift) | (topFieldIsReference << topFieldShift);
} else {
used_for_reference = isReference ? 3 : 0;
}
FrameIdx = frameIdx;
FieldOrderCnt[0] = fieldOrderCntList[used_for_reference == 2]; // 0: for progressive and top reference; 1: for
// bottom reference only.
FieldOrderCnt[1] = fieldOrderCntList[used_for_reference != 1]; // 0: for top reference only; 1: for bottom
// reference and progressive.
dpbSlot = -1;
m_picBuff = picBuff;
}
void setReference(bool isLongTerm, int32_t picOrderCnt,
vkPicBuffBase* picBuff)
{
is_non_existing = (picBuff == NULL);
is_long_term = isLongTerm;
is_field_ref = false;
used_for_reference = (picBuff != NULL) ? 3 : 0;
PicOrderCnt = picOrderCnt;
dpbSlot = -1;
m_picBuff = picBuff;
originalDpbIndex = -1;
}
bool isRef() { return (used_for_reference != 0); }
StdVideoDecodeH264ReferenceInfoFlags getPictureFlag(bool currentPictureIsProgressive)
{
StdVideoDecodeH264ReferenceInfoFlags picFlags = StdVideoDecodeH264ReferenceInfoFlags();
if (m_dumpParserData)
std::cout << "\t\t Flags: ";
if (used_for_reference) {
if (m_dumpParserData)
std::cout << "FRAME_IS_REFERENCE ";
// picFlags.is_reference = true;
}
if (is_long_term) {
if (m_dumpParserData)
std::cout << "IS_LONG_TERM ";
picFlags.used_for_long_term_reference = true;
}
if (is_non_existing) {
if (m_dumpParserData)
std::cout << "IS_NON_EXISTING ";
picFlags.is_non_existing = true;
}
if (is_field_ref) {
if (m_dumpParserData)
std::cout << "IS_FIELD ";
// picFlags.field_pic_flag = true;
}
if (!currentPictureIsProgressive && (used_for_reference & topFieldMask)) {
if (m_dumpParserData)
std::cout << "TOP_FIELD_IS_REF ";
picFlags.top_field_flag = true;
}
if (!currentPictureIsProgressive && (used_for_reference & bottomFieldMask)) {
if (m_dumpParserData)
std::cout << "BOTTOM_FIELD_IS_REF ";
picFlags.bottom_field_flag = true;
}
return picFlags;
}
void setH264PictureData(nvVideoDecodeH264DpbSlotInfo* pDpbRefList,
VkVideoReferenceSlotInfoKHR* pReferenceSlots,
uint32_t dpbEntryIdx, uint32_t dpbSlotIndex,
bool currentPictureIsProgressive)
{
assert(dpbEntryIdx < AVC_MAX_DPB_SLOTS);
assert(dpbSlotIndex < AVC_MAX_DPB_SLOTS);
assert((dpbSlotIndex == (uint32_t)dpbSlot) || is_non_existing);
pReferenceSlots[dpbEntryIdx].sType = VK_STRUCTURE_TYPE_VIDEO_REFERENCE_SLOT_INFO_KHR;
pReferenceSlots[dpbEntryIdx].slotIndex = dpbSlotIndex;
pReferenceSlots[dpbEntryIdx].pNext = pDpbRefList[dpbEntryIdx].Init(dpbSlotIndex);
StdVideoDecodeH264ReferenceInfo* pRefPicInfo = &pDpbRefList[dpbEntryIdx].stdReferenceInfo;
pRefPicInfo->FrameNum = FrameIdx;
if (m_dumpParserData) {
std::cout << "\tdpbEntryIdx: " << dpbEntryIdx
<< "dpbSlotIndex: " << dpbSlotIndex
<< " FrameIdx: " << (int32_t)FrameIdx;
}
pRefPicInfo->flags = getPictureFlag(currentPictureIsProgressive);
pRefPicInfo->PicOrderCnt[0] = FieldOrderCnt[0];
pRefPicInfo->PicOrderCnt[1] = FieldOrderCnt[1];
if (m_dumpParserData)
std::cout << " fieldOrderCnt[0]: " << pRefPicInfo->PicOrderCnt[0]
<< " fieldOrderCnt[1]: " << pRefPicInfo->PicOrderCnt[1]
<< std::endl;
}
void setH265PictureData(nvVideoDecodeH265DpbSlotInfo* pDpbSlotInfo,
VkVideoReferenceSlotInfoKHR* pReferenceSlots,
uint32_t dpbEntryIdx, uint32_t dpbSlotIndex)
{
assert(dpbEntryIdx < HEVC_MAX_DPB_SLOTS);
assert(dpbSlotIndex < HEVC_MAX_DPB_SLOTS);
assert(isRef());
assert((dpbSlotIndex == (uint32_t)dpbSlot) || is_non_existing);
pReferenceSlots[dpbEntryIdx].sType = VK_STRUCTURE_TYPE_VIDEO_REFERENCE_SLOT_INFO_KHR;
pReferenceSlots[dpbEntryIdx].slotIndex = dpbSlotIndex;
pReferenceSlots[dpbEntryIdx].pNext = pDpbSlotInfo[dpbEntryIdx].Init(dpbSlotIndex);
StdVideoDecodeH265ReferenceInfo* pRefPicInfo = &pDpbSlotInfo[dpbEntryIdx].stdReferenceInfo;
pRefPicInfo->PicOrderCntVal = PicOrderCnt;
pRefPicInfo->flags.used_for_long_term_reference = is_long_term;
if (m_dumpParserData) {
std::cout << "\tdpbIndex: " << dpbSlotIndex
<< " picOrderCntValList: " << PicOrderCnt;
std::cout << "\t\t Flags: ";
std::cout << "FRAME IS REFERENCE ";
if (pRefPicInfo->flags.used_for_long_term_reference) {
std::cout << "IS LONG TERM ";
}
std::cout << std::endl;
}
}
} dpbH264Entry;
virtual int32_t AddRef();
virtual int32_t Release();
// INvVideoDecoderClient
virtual VkResult ParseVideoData(VkParserSourceDataPacket* pPacket,
size_t* pParsedBytes,
bool doPartialParsing = false);
// Interface to allow decoder to communicate with the client implementing
// INvVideoDecoderClient
virtual int32_t BeginSequence(
const VkParserSequenceInfo* pnvsi); // Returns max number of reference frames (always
// at least 2 for MPEG-2)
virtual bool AllocPictureBuffer(
VkPicIf** ppPicBuf); // Returns a new VkPicIf interface
virtual bool DecodePicture(
VkParserPictureData* pParserPictureData); // Called when a picture is ready to be decoded
virtual bool DisplayPicture(
VkPicIf* pPicBuf,
int64_t llPTS); // Called when a picture is ready to be displayed
virtual void UnhandledNALU(
const uint8_t* pbData, size_t cbData) {}; // Called for custom NAL parsing (not required)
virtual uint32_t GetDecodeCaps()
{
// FIXME: Add MVC / SVC support
uint32_t decode_caps = 0; // NVD_CAPS_MVC | NVD_CAPS_SVC; // !!!
return decode_caps;
};
virtual VkDeviceSize GetBitstreamBuffer(VkDeviceSize size,
VkDeviceSize minBitstreamBufferOffsetAlignment,
VkDeviceSize minBitstreamBufferSizeAlignment,
const uint8_t* pInitializeBufferMemory,
VkDeviceSize initializeBufferMemorySize,
VkSharedBaseObj<VulkanBitstreamBuffer>& bitstreamBuffer);
const IVulkanVideoDecoderHandler* GetDecoderHandler()
{
return m_decoderHandler;
}
IVulkanVideoFrameBufferParserCb* GetFrameBufferParserCb()
{
return m_videoFrameBufferCb;
}
uint32_t GetNumNumDecodeSurfaces() { return m_maxNumDecodeSurfaces; }
protected:
void Deinitialize();
VkResult Initialize(
VkSharedBaseObj<IVulkanVideoDecoderHandler>& decoderHandler,
VkSharedBaseObj<IVulkanVideoFrameBufferParserCb>& videoFrameBufferCb,
uint32_t defaultMinBufferSize,
uint32_t bufferOffsetAlignment,
uint32_t bufferSizeAlignment,
bool outOfBandPictureParameters,
uint32_t errorThreshold);
VulkanVideoParser(VkVideoCodecOperationFlagBitsKHR codecType,
uint32_t maxNumDecodeSurfaces, uint32_t maxNumDpbSurfaces,
uint64_t clockRate);
virtual ~VulkanVideoParser() { Deinitialize(); }
bool UpdatePictureParameters(VkSharedBaseObj<StdVideoPictureParametersSet>& pictureParametersObject,
VkSharedBaseObj<VkVideoRefCountBase>& client);
bool DecodePicture(VkParserPictureData* pParserPictureData,
vkPicBuffBase* pVkPicBuff,
VkParserDecodePictureInfo* pDecodePictureInfo);
int8_t GetPicIdx(vkPicBuffBase*);
int8_t GetPicIdx(VkPicIf* pPicBuf);
int8_t GetPicDpbSlot(vkPicBuffBase*);
int8_t GetPicDpbSlot(int8_t picIndex);
int8_t SetPicDpbSlot(vkPicBuffBase*, int8_t dpbSlot);
int8_t SetPicDpbSlot(int8_t picIndex, int8_t dpbSlot);
uint32_t ResetPicDpbSlots(uint32_t picIndexSlotValidMask);
bool GetFieldPicFlag(int8_t picIndex);
bool SetFieldPicFlag(int8_t picIndex, bool fieldPicFlag);
uint32_t FillDpbH264State(const VkParserPictureData* pd,
const VkParserH264DpbEntry* dpbIn,
uint32_t maxDpbInSlotsInUse,
nvVideoDecodeH264DpbSlotInfo* pDpbRefList,
uint32_t maxRefPictures,
VkVideoReferenceSlotInfoKHR* pReferenceSlots,
int8_t* pGopReferenceImagesIndexes,
StdVideoDecodeH264PictureInfoFlags currPicFlags,
int32_t* pCurrAllocatedSlotIndex);
uint32_t FillDpbH265State(const VkParserPictureData* pd,
const VkParserHevcPictureData* pin,
nvVideoDecodeH265DpbSlotInfo* pDpbSlotInfo,
StdVideoDecodeH265PictureInfo* pStdPictureInfo,
uint32_t maxRefPictures,
VkVideoReferenceSlotInfoKHR* pReferenceSlots,
int8_t* pGopReferenceImagesIndexes,
int32_t* pCurrAllocatedSlotIndex);
int8_t AllocateDpbSlotForCurrentH264(
vkPicBuffBase* pPic, StdVideoDecodeH264PictureInfoFlags currPicFlags,
int8_t presetDpbSlot);
int8_t AllocateDpbSlotForCurrentH265(vkPicBuffBase* pPic, bool isReference,
int8_t presetDpbSlot);
protected:
VkSharedBaseObj<VulkanVideoDecodeParser> m_vkParser;
VkSharedBaseObj<IVulkanVideoDecoderHandler> m_decoderHandler;
VkSharedBaseObj<IVulkanVideoFrameBufferParserCb> m_videoFrameBufferCb;
std::atomic<int32_t> m_refCount;
VkVideoCodecOperationFlagBitsKHR m_codecType;
uint32_t m_maxNumDecodeSurfaces;
uint32_t m_maxNumDpbSlots;
uint64_t m_clockRate;
VkParserSequenceInfo m_nvsi;
int32_t m_nCurrentPictureID;
uint32_t m_dpbSlotsMask;
uint32_t m_fieldPicFlagMask;
DpbSlots m_dpb;
uint32_t m_outOfBandPictureParameters : 1;
uint32_t m_inlinedPictureParametersUseBeginCoding : 1;
int8_t m_pictureToDpbSlotMap[MAX_FRM_CNT];
public:
static bool m_dumpParserData;
static bool m_dumpDpbData;
};
bool VulkanVideoParser::m_dumpParserData = false;
bool VulkanVideoParser::m_dumpDpbData = false;
bool VulkanVideoParser::DecodePicture(VkParserPictureData* pd)
{
bool result = false;
if (!pd->pCurrPic) {
return result;
}
vkPicBuffBase* pVkPicBuff = GetPic(pd->pCurrPic);
const int32_t picIdx = pVkPicBuff ? pVkPicBuff->m_picIdx : -1;
if (picIdx >= VulkanVideoParser::MAX_FRM_CNT) {
assert(0);
return result;
}
if (m_dumpParserData) {
std::cout
<< "\t ==> VulkanVideoParser::DecodePicture " << picIdx << std::endl
<< "\t\t progressive: " << (bool)pd->progressive_frame
<< // Frame is progressive
"\t\t field: " << (bool)pd->field_pic_flag << std::endl
<< // 0 = frame picture, 1 = field picture
"\t\t\t bottom_field: " << (bool)pd->bottom_field_flag
<< // 0 = top field, 1 = bottom field (ignored if field_pic_flag=0)
"\t\t\t second_field: " << (bool)pd->second_field
<< // Second field of a complementary field pair
"\t\t\t top_field: " << (bool)pd->top_field_first << std::endl
<< // Frame pictures only
"\t\t repeat_first: " << pd->repeat_first_field
<< // For 3:2 pulldown (number of additional fields, 2 = frame
// doubling, 4 = frame tripling)
"\t\t ref_pic: " << (bool)pd->ref_pic_flag
<< std::endl; // Frame is a reference frame
}
VkParserDecodePictureInfo decodePictureInfo = VkParserDecodePictureInfo();
decodePictureInfo.pictureIndex = picIdx;
decodePictureInfo.flags.progressiveFrame = pd->progressive_frame;
decodePictureInfo.flags.fieldPic = pd->field_pic_flag; // 0 = frame picture, 1 = field picture
decodePictureInfo.flags.repeatFirstField = pd->repeat_first_field; // For 3:2 pulldown (number of additional fields,
// 2 = frame doubling, 4 = frame tripling)
decodePictureInfo.flags.refPic = pd->ref_pic_flag; // Frame is a reference frame
// Mark the first field as unpaired Detect unpaired fields
if (pd->field_pic_flag) {
decodePictureInfo.flags.bottomField = pd->bottom_field_flag; // 0 = top field, 1 = bottom field (ignored if
// field_pic_flag=0)
decodePictureInfo.flags.secondField = pd->second_field; // Second field of a complementary field pair
decodePictureInfo.flags.topFieldFirst = pd->top_field_first; // Frame pictures only
if (!pd->second_field) {
decodePictureInfo.flags.unpairedField = true; // Incomplete (half) frame.
} else {
if (decodePictureInfo.flags.unpairedField) {
decodePictureInfo.flags.syncToFirstField = true;
decodePictureInfo.flags.unpairedField = false;
}
}
}
decodePictureInfo.frameSyncinfo.unpairedField = decodePictureInfo.flags.unpairedField;
decodePictureInfo.frameSyncinfo.syncToFirstField = decodePictureInfo.flags.syncToFirstField;
return DecodePicture(pd, pVkPicBuff, &decodePictureInfo);
}
bool VulkanVideoParser::DisplayPicture(VkPicIf* pPicBuff, int64_t timestamp)
{
bool result = false;
vkPicBuffBase* pVkPicBuff = GetPic(pPicBuff);
assert(pVkPicBuff);
int32_t picIdx = pVkPicBuff ? pVkPicBuff->m_picIdx : -1;
if (m_dumpParserData) {
std::cout << "\t ======================< " << picIdx
<< " >============================" << std::endl;
std::cout << "\t ==> VulkanVideoParser::DisplayPicture " << picIdx
<< std::endl;
}
assert(picIdx != -1);
assert(m_videoFrameBufferCb);
if (m_videoFrameBufferCb && (picIdx != -1)) {
VulkanVideoDisplayPictureInfo dispInfo = VulkanVideoDisplayPictureInfo();
dispInfo.timestamp = (VkVideotimestamp)timestamp;
int32_t retVal = m_videoFrameBufferCb->QueueDecodedPictureForDisplay(
(int8_t)picIdx, &dispInfo);
if (picIdx == retVal) {
result = true;
} else {
assert(!"QueueDecodedPictureForDisplay failed");
}
}
if (m_dumpParserData) {
std::cout << "\t <== VulkanVideoParser::DisplayPicture " << picIdx
<< std::endl;
std::cout << "\t ======================< " << picIdx
<< " >============================" << std::endl;
}
return result;
}
bool VulkanVideoParser::AllocPictureBuffer(VkPicIf** ppPicBuff)
{
bool result = false;
assert(m_videoFrameBufferCb);
if (m_videoFrameBufferCb) {
*ppPicBuff = m_videoFrameBufferCb->ReservePictureBuffer();
if (*ppPicBuff) {
result = true;
}
}
if (!result) {
*ppPicBuff = (VkPicIf*)NULL;
}
return result;
}
VkDeviceSize VulkanVideoParser::GetBitstreamBuffer(VkDeviceSize size,
VkDeviceSize minBitstreamBufferOffsetAlignment,
VkDeviceSize minBitstreamBufferSizeAlignment,
const uint8_t* pInitializeBufferMemory,
VkDeviceSize initializeBufferMemorySize,
VkSharedBaseObj<VulkanBitstreamBuffer>& bitstreamBuffer)
{
// Forward the request to the Vulkan decoder handler
return m_decoderHandler->GetBitstreamBuffer(size,
minBitstreamBufferOffsetAlignment,
minBitstreamBufferSizeAlignment,
pInitializeBufferMemory,
initializeBufferMemorySize,
bitstreamBuffer);
}
int32_t VulkanVideoParser::AddRef() { return ++m_refCount; }
int32_t VulkanVideoParser::Release()
{
uint32_t ret;
ret = --m_refCount;
// Destroy the device if refcount reaches zero
if (ret == 0) {
delete this;
}
return ret;
}
VulkanVideoParser::VulkanVideoParser(VkVideoCodecOperationFlagBitsKHR codecType,
uint32_t maxNumDecodeSurfaces,
uint32_t maxNumDpbSurfaces,
uint64_t clockRate)
: m_vkParser()
, m_decoderHandler()
, m_videoFrameBufferCb()
, m_refCount(0)
, m_codecType(codecType)
, m_maxNumDecodeSurfaces(maxNumDecodeSurfaces)
, m_maxNumDpbSlots(maxNumDpbSurfaces)
, m_clockRate(clockRate)
, m_nCurrentPictureID(0)
, m_dpbSlotsMask(0)
, m_fieldPicFlagMask(0)
, m_dpb(3)
, m_outOfBandPictureParameters(true)
, m_inlinedPictureParametersUseBeginCoding(false)
{
memset(&m_nvsi, 0, sizeof(m_nvsi));
for (uint32_t picId = 0; picId < MAX_FRM_CNT; picId++) {
m_pictureToDpbSlotMap[picId] = -1;
}
}
static void nvParserLog(const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
printf(format, argptr);
va_end(argptr);
}
VkResult VulkanVideoParser::Initialize(
VkSharedBaseObj<IVulkanVideoDecoderHandler>& decoderHandler,
VkSharedBaseObj<IVulkanVideoFrameBufferParserCb>& videoFrameBufferCb,
uint32_t defaultMinBufferSize,
uint32_t bufferOffsetAlignment,
uint32_t bufferSizeAlignment,
bool outOfBandPictureParameters,
uint32_t errorThreshold)
{
Deinitialize();
m_outOfBandPictureParameters = outOfBandPictureParameters;
m_decoderHandler = decoderHandler;
m_videoFrameBufferCb = videoFrameBufferCb;
memset(&m_nvsi, 0, sizeof(m_nvsi));
VkParserInitDecodeParameters nvdp;
memset(&nvdp, 0, sizeof(nvdp));
nvdp.interfaceVersion = NV_VULKAN_VIDEO_PARSER_API_VERSION;
nvdp.pClient = reinterpret_cast<VkParserVideoDecodeClient*>(this);
nvdp.defaultMinBufferSize = defaultMinBufferSize;
nvdp.bufferOffsetAlignment = bufferOffsetAlignment;
nvdp.bufferSizeAlignment = bufferSizeAlignment;
nvdp.referenceClockRate = m_clockRate;
nvdp.errorThreshold = errorThreshold;
nvdp.outOfBandPictureParameters = outOfBandPictureParameters;
static const VkExtensionProperties h264StdExtensionVersion = { VK_STD_VULKAN_VIDEO_CODEC_H264_DECODE_EXTENSION_NAME, VK_STD_VULKAN_VIDEO_CODEC_H264_DECODE_SPEC_VERSION };
static const VkExtensionProperties h265StdExtensionVersion = { VK_STD_VULKAN_VIDEO_CODEC_H265_DECODE_EXTENSION_NAME, VK_STD_VULKAN_VIDEO_CODEC_H265_DECODE_SPEC_VERSION };
const VkExtensionProperties* pStdExtensionVersion = NULL;
if (m_codecType == VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR) {
pStdExtensionVersion = &h264StdExtensionVersion;
} else if (m_codecType == VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR) {
pStdExtensionVersion = &h265StdExtensionVersion;
} else {
assert(!"Unsupported codec type");
return VK_ERROR_VIDEO_PROFILE_CODEC_NOT_SUPPORTED_KHR;
}
return CreateVulkanVideoDecodeParser(m_codecType, pStdExtensionVersion, &nvParserLog, 1, &nvdp, m_vkParser);
}
void VulkanVideoParser::Deinitialize()
{
m_vkParser = nullptr;
m_decoderHandler = nullptr;
m_videoFrameBufferCb = nullptr;
}
VkResult VulkanVideoParser::ParseVideoData(VkParserSourceDataPacket* pPacket,
size_t *pParsedBytes,
bool doPartialParsing)
{
VkParserBitstreamPacket pkt;
VkResult result;
memset(&pkt, 0, sizeof(pkt));
if (pPacket->flags & VK_PARSER_PKT_DISCONTINUITY) {
// Handle discontinuity separately, in order to flush before any new
// content
pkt.bDiscontinuity = true;
m_vkParser->ParseByteStream(&pkt); // Send a NULL discontinuity packet
}
pkt.pByteStream = pPacket->payload;
pkt.nDataLength = pPacket->payload_size;
pkt.bEOS = !!(pPacket->flags & VK_PARSER_PKT_ENDOFSTREAM);
pkt.bEOP = !!(pPacket->flags & VK_PARSER_PKT_ENDOFPICTURE);
pkt.bPTSValid = !!(pPacket->flags & VK_PARSER_PKT_TIMESTAMP);
pkt.llPTS = pPacket->timestamp;
pkt.bPartialParsing = doPartialParsing;
if (m_vkParser->ParseByteStream(&pkt, pParsedBytes)) {
result = VK_SUCCESS;
} else {
result = VK_ERROR_INITIALIZATION_FAILED;
}
if (pkt.bEOS) {
// Flush any pending frames after EOS
}
return result;
}
int8_t VulkanVideoParser::GetPicIdx(vkPicBuffBase* pPicBuf)
{
if (pPicBuf) {
int32_t picIndex = pPicBuf->m_picIdx;
if ((picIndex >= 0) && ((uint32_t)picIndex < m_maxNumDecodeSurfaces)) {
return (int8_t)picIndex;
}
}
return -1;
}
int8_t VulkanVideoParser::GetPicIdx(VkPicIf* pPicBuf)
{
return GetPicIdx(GetPic(pPicBuf));
}
int8_t VulkanVideoParser::GetPicDpbSlot(int8_t picIndex)
{
return m_pictureToDpbSlotMap[picIndex];
}
int8_t VulkanVideoParser::GetPicDpbSlot(vkPicBuffBase* pPicBuf)
{
int8_t picIndex = GetPicIdx(pPicBuf);
assert((picIndex >= 0) && ((uint32_t)picIndex < m_maxNumDecodeSurfaces));
return GetPicDpbSlot(picIndex);
}
bool VulkanVideoParser::GetFieldPicFlag(int8_t picIndex)
{
assert((picIndex >= 0) && ((uint32_t)picIndex < m_maxNumDecodeSurfaces));
return (m_fieldPicFlagMask & (1 << (uint32_t)picIndex));
}
bool VulkanVideoParser::SetFieldPicFlag(int8_t picIndex, bool fieldPicFlag)
{
assert((picIndex >= 0) && ((uint32_t)picIndex < m_maxNumDecodeSurfaces));
bool oldFieldPicFlag = GetFieldPicFlag(picIndex);
if (fieldPicFlag) {
m_fieldPicFlagMask |= (1 << (uint32_t)picIndex);
} else {
m_fieldPicFlagMask &= ~(1 << (uint32_t)picIndex);
}
return oldFieldPicFlag;
}
int8_t VulkanVideoParser::SetPicDpbSlot(int8_t picIndex, int8_t dpbSlot)
{
int8_t oldDpbSlot = m_pictureToDpbSlotMap[picIndex];
m_pictureToDpbSlotMap[picIndex] = dpbSlot;
if (dpbSlot >= 0) {
m_dpbSlotsMask |= (1 << picIndex);
} else {
m_dpbSlotsMask &= ~(1 << picIndex);
if (oldDpbSlot >= 0) {
m_dpb.FreeSlot(oldDpbSlot);
}
}
return oldDpbSlot;
}
int8_t VulkanVideoParser::SetPicDpbSlot(vkPicBuffBase* pPicBuf,
int8_t dpbSlot)
{
int8_t picIndex = GetPicIdx(pPicBuf);
assert((picIndex >= 0) && ((uint32_t)picIndex < m_maxNumDecodeSurfaces));
return SetPicDpbSlot(picIndex, dpbSlot);
}
uint32_t VulkanVideoParser::ResetPicDpbSlots(uint32_t picIndexSlotValidMask)
{
uint32_t resetSlotsMask = ~(picIndexSlotValidMask | ~m_dpbSlotsMask);
if (resetSlotsMask != 0) {
for (uint32_t picIdx = 0;
((picIdx < m_maxNumDecodeSurfaces) && resetSlotsMask); picIdx++) {
if (resetSlotsMask & (1 << picIdx)) {
resetSlotsMask &= ~(1 << picIdx);
SetPicDpbSlot(picIdx, -1);
}
}
}
return m_dpbSlotsMask;
}
int32_t VulkanVideoParser::BeginSequence(const VkParserSequenceInfo* pnvsi)
{
bool sequenceUpdate = ((m_nvsi.nMaxWidth != 0) && (m_nvsi.nMaxHeight != 0)) ? true : false;
const uint32_t maxDpbSlots = (pnvsi->eCodec == VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR) ?
MAX_DPB_REF_AND_SETUP_SLOTS : MAX_DPB_REF_SLOTS;
uint32_t configDpbSlots = (pnvsi->nMinNumDpbSlots > 0) ? pnvsi->nMinNumDpbSlots : maxDpbSlots;
configDpbSlots = std::min<uint32_t>(configDpbSlots, maxDpbSlots);
bool sequenceReconfigureFormat = false;