1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2021 The Khronos Group Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Video Session NV Utils
22 *//*--------------------------------------------------------------------*/
23
24 #include "vktVideoSessionNvUtils.hpp"
25 #include "vkDefs.hpp"
26 #include "vkPlatform.hpp"
27 #include "tcuPlatform.hpp"
28 #include "tcuFunctionLibrary.hpp"
29 #include "deMemory.h"
30
31 #include "extNvidiaVideoParserIf.hpp"
32
33 namespace vkt
34 {
35 namespace video
36 {
37 #if DE_OS == DE_OS_WIN32
38 const char* CreateVulkanVideoDecodeParserFuncName = "?CreateVulkanVideoDecodeParser@@YA_NPEAPEAVVulkanVideoDecodeParser@@W4VkVideoCodecOperationFlagBitsKHR@@PEBUVkExtensionProperties@@P6AXPEBDZZH@Z";
39 #else
40 const char* CreateVulkanVideoDecodeParserFuncName = "_Z29CreateVulkanVideoDecodeParserPP23VulkanVideoDecodeParser32VkVideoCodecOperationFlagBitsKHRPK21VkExtensionPropertiesPFvPKczEi";
41 #endif
42
43 typedef void (*NvidiaParserLogFuncType)(const char* format, ...);
44 typedef bool (*CreateVulkanVideoDecodeParserFunc)(NvidiaVulkanVideoDecodeParser** ppobj, vk::VkVideoCodecOperationFlagBitsKHR eCompression, const vk::VkExtensionProperties* extensionProperty, NvidiaParserLogFuncType pParserLogFunc, int logLevel);
45
NvidiaParserLogFunc(const char * format,...)46 void NvidiaParserLogFunc (const char* format, ...)
47 {
48 DE_UNREF(format);
49 }
50
51 class ClsVulkanVideoDecodeParser : public IfcVulkanVideoDecodeParser
52 {
53 public:
54 ClsVulkanVideoDecodeParser (NvidiaVulkanVideoDecodeParser* vulkanVideoDecodeParser);
55 virtual ~ClsVulkanVideoDecodeParser ();
56
57 public:
58 // IfcVulkanVideoDecodeParser commands
59 virtual bool initialize (NvidiaVulkanParserVideoDecodeClient* nvidiaVulkanParserVideoDecodeClient);
60 virtual bool deinitialize (void);
61 virtual bool parseByteStream (deUint8* pData, deInt64 size);
62
63 protected:
64 NvidiaVulkanVideoDecodeParser* m_vulkanVideoDecodeParser;
65 };
66
ClsVulkanVideoDecodeParser(NvidiaVulkanVideoDecodeParser * vulkanVideoDecodeParser)67 ClsVulkanVideoDecodeParser::ClsVulkanVideoDecodeParser (NvidiaVulkanVideoDecodeParser* vulkanVideoDecodeParser)
68 : m_vulkanVideoDecodeParser (vulkanVideoDecodeParser)
69 {
70 }
71
~ClsVulkanVideoDecodeParser()72 ClsVulkanVideoDecodeParser::~ClsVulkanVideoDecodeParser()
73 {
74 if (m_vulkanVideoDecodeParser != DE_NULL)
75 {
76 m_vulkanVideoDecodeParser->Deinitialize();
77
78 m_vulkanVideoDecodeParser->Release();
79
80 m_vulkanVideoDecodeParser = DE_NULL;
81 }
82 }
83
initialize(NvidiaVulkanParserVideoDecodeClient * nvidiaVulkanParserVideoDecodeClient)84 bool ClsVulkanVideoDecodeParser::initialize (NvidiaVulkanParserVideoDecodeClient* nvidiaVulkanParserVideoDecodeClient)
85 {
86 DE_ASSERT(m_vulkanVideoDecodeParser != DE_NULL);
87
88 NvidiaVulkanParserInitDecodeParameters parameters =
89 {
90 NV_VULKAN_VIDEO_PARSER_API_VERSION,
91 nvidiaVulkanParserVideoDecodeClient,
92 0,
93 0,
94 DE_NULL,
95 true
96 };
97
98 if (m_vulkanVideoDecodeParser->Initialize(¶meters) != vk::VK_SUCCESS)
99 {
100 TCU_THROW(InternalError, "ClsVulkanVideoDecodeParser->Initialize failed");
101 }
102
103 return true;
104 }
105
deinitialize(void)106 bool ClsVulkanVideoDecodeParser::deinitialize (void)
107 {
108 bool result = true;
109
110 if (m_vulkanVideoDecodeParser != DE_NULL)
111 {
112 result = m_vulkanVideoDecodeParser->Deinitialize();
113
114 m_vulkanVideoDecodeParser = DE_NULL;
115 }
116
117 return result;
118 }
119
parseByteStream(deUint8 * pData,deInt64 size)120 bool ClsVulkanVideoDecodeParser::parseByteStream (deUint8* pData, deInt64 size)
121 {
122 DE_ASSERT(m_vulkanVideoDecodeParser != DE_NULL);
123
124 int32_t parsed = 0;
125 NvidiaVulkanParserBitstreamPacket pkt;
126
127 deMemset(&pkt, 0, sizeof(pkt));
128
129 pkt.nDataLength = static_cast<int32_t>(size);
130 pkt.pByteStream = pData;
131 pkt.bEOS = (pData == DE_NULL || size == 0);
132
133 bool result = m_vulkanVideoDecodeParser->ParseByteStream(&pkt, &parsed);
134
135 return result && (parsed > 0);
136 }
137
138
139 class NvFunctions: public IfcNvFunctions
140 {
141 public:
142 NvFunctions (const vk::Platform& platform);
143 virtual IfcVulkanVideoDecodeParser* createIfcVulkanVideoDecodeParser (VkVideoCodecOperationFlagBitsKHR codecOperation, const VkExtensionProperties* stdExtensionVersion);
144
145 private:
146 de::MovePtr<vk::Library> m_library;
147 CreateVulkanVideoDecodeParserFunc m_createVulkanVideoDecodeParserFunc;
148 };
149
NvFunctions(const vk::Platform & platform)150 NvFunctions::NvFunctions (const vk::Platform& platform)
151 #ifdef DE_BUILD_VIDEO
152 : m_library (de::MovePtr<vk::Library>(platform.createLibrary(vk::Platform::LIBRARY_TYPE_VULKAN_VIDEO_DECODE_PARSER, DE_NULL)))
153 #else
154 : m_library (de::MovePtr<vk::Library>(platform.createLibrary()))
155 #endif
156 {
157 const tcu::FunctionLibrary& funcsLibrary = m_library->getFunctionLibrary();
158
159 m_createVulkanVideoDecodeParserFunc = reinterpret_cast<CreateVulkanVideoDecodeParserFunc>(funcsLibrary.getFunction(CreateVulkanVideoDecodeParserFuncName));
160
161 if (m_createVulkanVideoDecodeParserFunc == DE_NULL)
162 TCU_THROW(InternalError, string("Function not found in library: ") + CreateVulkanVideoDecodeParserFuncName);
163 }
164
createIfcVulkanVideoDecodeParser(VkVideoCodecOperationFlagBitsKHR codecOperation,const VkExtensionProperties * stdExtensionVersion)165 IfcVulkanVideoDecodeParser* NvFunctions::createIfcVulkanVideoDecodeParser (VkVideoCodecOperationFlagBitsKHR codecOperation, const VkExtensionProperties* stdExtensionVersion)
166 {
167 DE_ASSERT(m_createVulkanVideoDecodeParserFunc != DE_NULL);
168
169 NvidiaVulkanVideoDecodeParser* pobj = DE_NULL;
170
171 if (!m_createVulkanVideoDecodeParserFunc(&pobj, codecOperation, stdExtensionVersion, NvidiaParserLogFunc, 0) || pobj == DE_NULL)
172 {
173 return DE_NULL;
174 }
175
176 return new ClsVulkanVideoDecodeParser(pobj);
177 }
178
createIfcNvFunctions(const vk::Platform & platform)179 de::MovePtr<IfcNvFunctions> createIfcNvFunctions (const vk::Platform& platform)
180 {
181 return de::MovePtr<IfcNvFunctions>(new NvFunctions(platform));
182 }
183
184 } // video
185 } // vkt
186