Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: OpenGIS Simple Features Reference Implementation
4 : * Purpose: Implements Basis Universal / KTX2 driver.
5 : * Author: Even Rouault, <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2022, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "gdal_frmts.h"
14 : #include "gdal_pam.h"
15 : #include "common.h"
16 : #include "include_basisu_sdk.h"
17 : #include "ktx2drivercore.h"
18 :
19 : #include <algorithm>
20 : #include <cstdlib>
21 : #include <limits>
22 :
23 : /************************************************************************/
24 : /* KTX2Dataset */
25 : /************************************************************************/
26 :
27 : class KTX2Dataset final : public GDALPamDataset
28 : {
29 : friend class KTX2RasterBand;
30 :
31 : basist::ktx2_transcoder m_transcoder{};
32 : basist::ktx2_transcoder &m_transcoderRef;
33 : bool m_bHasDecodeRun = false;
34 : void *m_pEncodedData = nullptr;
35 : void *m_pDecodedData = nullptr;
36 : uint32_t m_nLineStride = 0;
37 : uint32_t m_iLayer = 0;
38 : uint32_t m_iFace = 0;
39 : uint32_t m_iLevel = 0;
40 : std::vector<std::unique_ptr<KTX2Dataset>> m_apoOverviewsDS{};
41 :
42 : void *GetDecodedData(uint32_t &nLineStride);
43 :
44 : CPL_DISALLOW_COPY_ASSIGN(KTX2Dataset)
45 :
46 : public:
47 : ~KTX2Dataset() override;
48 : KTX2Dataset(uint32_t iLayer, uint32_t iFace, void *pEncodedData);
49 : KTX2Dataset(KTX2Dataset *poParent, uint32_t iLevel);
50 :
51 : static GDALDataset *Open(GDALOpenInfo *poOpenInfo);
52 : static GDALDataset *CreateCopy(const char *pszFilename,
53 : GDALDataset *poSrcDS, int bStrict,
54 : char **papszOptions,
55 : GDALProgressFunc pfnProgress,
56 : void *pProgressData);
57 : };
58 :
59 : /************************************************************************/
60 : /* KTX2RasterBand */
61 : /************************************************************************/
62 :
63 : class KTX2RasterBand final : public GDALPamRasterBand
64 : {
65 : protected:
66 : CPLErr IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage) override;
67 :
68 : public:
69 : KTX2RasterBand(KTX2Dataset *poDSIn, int nBandIn);
70 :
71 : int GetOverviewCount() override;
72 : GDALRasterBand *GetOverview(int nIdx) override;
73 : };
74 :
75 : /************************************************************************/
76 : /* KTX2Dataset() */
77 : /************************************************************************/
78 :
79 49 : KTX2Dataset::KTX2Dataset(uint32_t iLayer, uint32_t iFace, void *pEncodedData)
80 49 : : m_transcoderRef(m_transcoder), m_pEncodedData(pEncodedData),
81 49 : m_iLayer(iLayer), m_iFace(iFace)
82 : {
83 49 : }
84 :
85 : /************************************************************************/
86 : /* KTX2Dataset() */
87 : /************************************************************************/
88 :
89 7 : KTX2Dataset::KTX2Dataset(KTX2Dataset *poParent, uint32_t iLevel)
90 7 : : m_transcoderRef(poParent->m_transcoderRef), m_iLayer(poParent->m_iLayer),
91 7 : m_iFace(poParent->m_iFace), m_iLevel(iLevel)
92 : {
93 : basist::ktx2_image_level_info level_info;
94 7 : CPL_IGNORE_RET_VAL(m_transcoderRef.get_image_level_info(
95 : level_info, m_iLevel, m_iLayer, m_iFace));
96 7 : nRasterXSize = static_cast<int>(level_info.m_orig_width);
97 7 : nRasterYSize = static_cast<int>(level_info.m_orig_height);
98 7 : }
99 :
100 : /************************************************************************/
101 : /* ~KTX2Dataset() */
102 : /************************************************************************/
103 :
104 112 : KTX2Dataset::~KTX2Dataset()
105 : {
106 56 : VSIFree(m_pEncodedData);
107 56 : VSIFree(m_pDecodedData);
108 112 : }
109 :
110 : /************************************************************************/
111 : /* GetDecodedData() */
112 : /************************************************************************/
113 :
114 2220 : void *KTX2Dataset::GetDecodedData(uint32_t &nLineStride)
115 : {
116 2220 : if (m_bHasDecodeRun)
117 : {
118 2214 : nLineStride = m_nLineStride;
119 2214 : return m_pDecodedData;
120 : }
121 6 : m_bHasDecodeRun = true;
122 :
123 6 : GDALInitBasisUTranscoder();
124 :
125 : basist::ktx2_image_level_info level_info;
126 6 : if (!m_transcoderRef.get_image_level_info(level_info, m_iLevel, m_iLayer,
127 : m_iFace))
128 : {
129 0 : CPLError(CE_Failure, CPLE_AppDefined,
130 : "ktx2_transcoder::get_image_level_info() failed!");
131 0 : return nullptr;
132 : }
133 :
134 6 : if (!m_transcoderRef.start_transcoding())
135 : {
136 0 : CPLError(CE_Failure, CPLE_AppDefined,
137 : "ktx2_transcoder::start_transcoding() failed!");
138 0 : return nullptr;
139 : }
140 :
141 6 : m_pDecodedData = VSI_MALLOC3_VERBOSE(level_info.m_orig_width,
142 : level_info.m_orig_height, 4);
143 6 : if (m_pDecodedData == nullptr)
144 0 : return nullptr;
145 :
146 6 : constexpr basist::transcoder_texture_format transcoder_tex_fmt =
147 : basist::transcoder_texture_format::cTFRGBA32;
148 6 : if (!m_transcoderRef.transcode_image_level(
149 : m_iLevel, m_iLayer, m_iFace, m_pDecodedData,
150 6 : level_info.m_orig_width * level_info.m_orig_height * 4,
151 : transcoder_tex_fmt))
152 : {
153 0 : CPLError(CE_Failure, CPLE_AppDefined,
154 : "ktx2_transcoder::transcode_image_level() failed!");
155 0 : VSIFree(m_pDecodedData);
156 0 : m_pDecodedData = nullptr;
157 0 : return nullptr;
158 : }
159 :
160 6 : m_nLineStride = level_info.m_orig_width * 4;
161 6 : nLineStride = m_nLineStride;
162 6 : return m_pDecodedData;
163 : }
164 :
165 : /************************************************************************/
166 : /* KTX2RasterBand() */
167 : /************************************************************************/
168 :
169 204 : KTX2RasterBand::KTX2RasterBand(KTX2Dataset *poDSIn, int nBandIn)
170 : {
171 204 : poDS = poDSIn;
172 204 : nBand = nBandIn;
173 204 : nRasterXSize = poDSIn->GetRasterXSize();
174 204 : nRasterYSize = poDSIn->GetRasterYSize();
175 204 : nBlockXSize = nRasterXSize;
176 204 : nBlockYSize = 1;
177 204 : eDataType = GDT_Byte;
178 204 : SetColorInterpretation(
179 204 : static_cast<GDALColorInterp>(GCI_RedBand + nBandIn - 1));
180 204 : }
181 :
182 : /************************************************************************/
183 : /* IReadBlock() */
184 : /************************************************************************/
185 :
186 2220 : CPLErr KTX2RasterBand::IReadBlock(int /*nBlockXOff*/, int nBlockYOff,
187 : void *pImage)
188 : {
189 2220 : auto poGDS = cpl::down_cast<KTX2Dataset *>(poDS);
190 2220 : uint32_t nLineStride = 0;
191 2220 : void *decoded_data = poGDS->GetDecodedData(nLineStride);
192 2220 : if (decoded_data == nullptr)
193 0 : return CE_Failure;
194 :
195 2220 : GDALCopyWords(static_cast<GByte *>(decoded_data) +
196 2220 : nBlockYOff * nLineStride + nBand - 1,
197 : GDT_Byte, 4, pImage, GDT_Byte, 1, nBlockXSize);
198 2220 : return CE_None;
199 : }
200 :
201 : /************************************************************************/
202 : /* GetOverviewCount() */
203 : /************************************************************************/
204 :
205 6 : int KTX2RasterBand::GetOverviewCount()
206 : {
207 6 : auto poGDS = cpl::down_cast<KTX2Dataset *>(poDS);
208 6 : return static_cast<int>(poGDS->m_apoOverviewsDS.size());
209 : }
210 :
211 : /************************************************************************/
212 : /* GetOverview() */
213 : /************************************************************************/
214 :
215 3 : GDALRasterBand *KTX2RasterBand::GetOverview(int nIdx)
216 : {
217 3 : if (nIdx < 0 || nIdx >= GetOverviewCount())
218 2 : return nullptr;
219 1 : auto poGDS = cpl::down_cast<KTX2Dataset *>(poDS);
220 1 : return poGDS->m_apoOverviewsDS[nIdx]->GetRasterBand(nBand);
221 : }
222 :
223 : /************************************************************************/
224 : /* Open() */
225 : /************************************************************************/
226 :
227 53 : GDALDataset *KTX2Dataset::Open(GDALOpenInfo *poOpenInfo)
228 : {
229 53 : if (!KTX2DriverIdentify(poOpenInfo) || poOpenInfo->eAccess == GA_Update)
230 0 : return nullptr;
231 :
232 53 : VSILFILE *fpL = nullptr;
233 53 : uint32_t nLayer = static_cast<uint32_t>(-1);
234 53 : uint32_t nFace = static_cast<uint32_t>(-1);
235 53 : if (STARTS_WITH(poOpenInfo->pszFilename, "KTX2:"))
236 : {
237 : const CPLStringList aosTokens(CSLTokenizeString2(
238 8 : poOpenInfo->pszFilename, ":", CSLT_HONOURSTRINGS));
239 8 : if (aosTokens.size() != 4)
240 3 : return nullptr;
241 5 : fpL = VSIFOpenL(aosTokens[1], "rb");
242 5 : if (fpL == nullptr)
243 : {
244 1 : CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s", aosTokens[1]);
245 1 : return nullptr;
246 : }
247 4 : nLayer = static_cast<uint32_t>(atoi(aosTokens[2]));
248 4 : nFace = static_cast<uint32_t>(atoi(aosTokens[3]));
249 : }
250 49 : GIntBig nMaxSize = std::strtoull(
251 49 : CPLGetConfigOption("KTX2_MAX_FILE_SIZE", "0"), nullptr, 10);
252 49 : constexpr GIntBig KTX2_LIMIT = std::numeric_limits<uint32_t>::max();
253 49 : if (nMaxSize == 0 || nMaxSize > KTX2_LIMIT)
254 49 : nMaxSize = KTX2_LIMIT;
255 49 : GByte *pabyRet = nullptr;
256 49 : vsi_l_offset nSizeLarge = 0;
257 49 : int nRet = VSIIngestFile(fpL ? fpL : poOpenInfo->fpL, nullptr, &pabyRet,
258 : &nSizeLarge, nMaxSize);
259 49 : if (fpL != nullptr)
260 4 : VSIFCloseL(fpL);
261 49 : if (!nRet)
262 : {
263 0 : return nullptr;
264 : }
265 49 : const uint32_t nSize = static_cast<uint32_t>(nSizeLarge);
266 :
267 : auto poDS = std::make_unique<KTX2Dataset>(
268 49 : nLayer != static_cast<uint32_t>(-1) ? nLayer : 0,
269 147 : nFace != static_cast<uint32_t>(-1) ? nFace : 0, pabyRet);
270 49 : auto &transcoder = poDS->m_transcoder;
271 49 : const bool bInit = transcoder.init(pabyRet, nSize);
272 49 : if (!bInit)
273 : {
274 0 : if (nSize >= sizeof(basist::ktx2_header))
275 : {
276 : #define DEBUG_u32(x) \
277 : CPLDebug("KTX2", #x " = %u", \
278 : static_cast<uint32_t>(transcoder.get_header().m_##x))
279 0 : DEBUG_u32(vk_format);
280 0 : DEBUG_u32(type_size);
281 0 : DEBUG_u32(pixel_width);
282 0 : DEBUG_u32(pixel_height);
283 0 : DEBUG_u32(pixel_depth);
284 0 : DEBUG_u32(layer_count);
285 0 : DEBUG_u32(face_count);
286 0 : DEBUG_u32(level_count);
287 0 : DEBUG_u32(supercompression_scheme);
288 0 : DEBUG_u32(dfd_byte_offset);
289 0 : DEBUG_u32(dfd_byte_length);
290 : }
291 0 : CPLError(CE_Failure, CPLE_AppDefined,
292 : "ktx2_transcoder::init() failed! "
293 : "File either uses an unsupported feature or is invalid");
294 0 : return nullptr;
295 : }
296 :
297 : const uint32_t nLayers =
298 49 : std::max(1U, transcoder.get_layers()); // get_layers() may return 0
299 49 : const uint32_t nFaces = transcoder.get_faces();
300 49 : CPLDebug("KTX2", "levels = %u, faces = %u, layers = %u",
301 : transcoder.get_levels(), nFaces, nLayers);
302 :
303 49 : switch (transcoder.get_format())
304 : {
305 34 : case basist::basis_tex_format::cETC1S:
306 34 : poDS->SetMetadataItem("COMPRESSION", "ETC1S", "IMAGE_STRUCTURE");
307 34 : break;
308 15 : case basist::basis_tex_format::cUASTC4x4:
309 15 : poDS->SetMetadataItem("COMPRESSION", "UASTC", "IMAGE_STRUCTURE");
310 15 : break;
311 : }
312 :
313 49 : if (nLayer == static_cast<uint32_t>(-1) && (nFaces >= 2 || nLayers >= 2))
314 : {
315 2 : CPLStringList aosSubdatasets;
316 1 : int nSubDS = 1;
317 3 : for (uint32_t iLayer = 0; iLayer < nLayers; ++iLayer)
318 : {
319 4 : for (uint32_t iFace = 0; iFace < nFaces; ++iFace)
320 : {
321 : aosSubdatasets.SetNameValue(
322 : CPLSPrintf("SUBDATASET_%d_NAME", nSubDS),
323 : CPLSPrintf("KTX2:\"%s\":%u:%u", poOpenInfo->pszFilename,
324 2 : iLayer, iFace));
325 : aosSubdatasets.SetNameValue(
326 : CPLSPrintf("SUBDATASET_%d_DESC", nSubDS),
327 : CPLSPrintf("Layer %u, face %u of %s", iLayer, iFace,
328 2 : poOpenInfo->pszFilename));
329 2 : nSubDS++;
330 : }
331 : }
332 1 : poDS->nRasterXSize = 0;
333 1 : poDS->nRasterYSize = 0;
334 1 : poDS->SetMetadata(aosSubdatasets.List(), "SUBDATASETS");
335 :
336 1 : poDS->SetPamFlags(poDS->GetPamFlags() & ~GPF_DIRTY);
337 :
338 1 : return poDS.release();
339 : }
340 48 : else if (nLayer != static_cast<uint32_t>(-1) && nLayer >= nLayers)
341 : {
342 1 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid layer number: %u",
343 : nLayer);
344 1 : return nullptr;
345 : }
346 47 : else if (nFace != static_cast<uint32_t>(-1) && nFace >= nFaces)
347 : {
348 1 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid face number: %u", nFace);
349 1 : return nullptr;
350 : }
351 :
352 46 : poDS->nRasterXSize = transcoder.get_width();
353 46 : poDS->nRasterYSize = transcoder.get_height();
354 :
355 46 : const int l_nBands = 3 + (transcoder.get_has_alpha() ? 1 : 0);
356 222 : for (int i = 1; i <= l_nBands; ++i)
357 : {
358 176 : poDS->SetBand(i, new KTX2RasterBand(poDS.get(), i));
359 : }
360 :
361 99 : for (uint32_t level_index = 0; level_index < transcoder.get_levels();
362 : ++level_index)
363 : {
364 : basist::ktx2_image_level_info level_info;
365 53 : uint32_t layer_index = 0;
366 53 : uint32_t face_index = 0;
367 53 : if (transcoder.get_image_level_info(level_info, level_index,
368 : layer_index, face_index))
369 : {
370 53 : CPLDebug(
371 : "KTX2",
372 : "level %u: width=%u, orig_width=%u, height=%u, orig_height=%u",
373 : level_index, level_info.m_width, level_info.m_orig_width,
374 : level_info.m_height, level_info.m_orig_height);
375 :
376 53 : if (level_index > 0)
377 : {
378 : auto poOverviewDS =
379 14 : std::make_unique<KTX2Dataset>(poDS.get(), level_index);
380 35 : for (int i = 1; i <= l_nBands; ++i)
381 : {
382 56 : poOverviewDS->SetBand(
383 28 : i, new KTX2RasterBand(poOverviewDS.get(), i));
384 : }
385 7 : poDS->m_apoOverviewsDS.emplace_back(std::move(poOverviewDS));
386 : }
387 : }
388 : }
389 :
390 46 : poDS->SetPamFlags(poDS->GetPamFlags() & ~GPF_DIRTY);
391 :
392 : // Initialize any PAM information.
393 46 : poDS->SetDescription(poOpenInfo->pszFilename);
394 46 : poDS->TryLoadXML(poOpenInfo->GetSiblingFiles());
395 :
396 46 : return poDS.release();
397 : }
398 :
399 : /************************************************************************/
400 : /* CreateCopy() */
401 : /************************************************************************/
402 :
403 56 : GDALDataset *KTX2Dataset::CreateCopy(const char *pszFilename,
404 : GDALDataset *poSrcDS, int /*bStrict*/,
405 : char **papszOptions,
406 : GDALProgressFunc pfnProgress,
407 : void *pProgressData)
408 : {
409 56 : if (!GDAL_KTX2_BASISU_CreateCopy(pszFilename, poSrcDS,
410 : true, // bIsKTX2
411 : papszOptions, pfnProgress, pProgressData))
412 : {
413 31 : return nullptr;
414 : }
415 50 : GDALOpenInfo oOpenInfo(pszFilename, GA_ReadOnly);
416 25 : return Open(&oOpenInfo);
417 : }
418 :
419 : /************************************************************************/
420 : /* GDALRegister_KTX2() */
421 : /************************************************************************/
422 :
423 11 : void GDALRegister_KTX2()
424 : {
425 11 : if (GDALGetDriverByName(KTX2_DRIVER_NAME) != nullptr)
426 0 : return;
427 :
428 11 : GDALDriver *poDriver = new GDALDriver();
429 11 : KTX2DriverSetCommonMetadata(poDriver);
430 :
431 11 : poDriver->pfnOpen = KTX2Dataset::Open;
432 11 : poDriver->pfnCreateCopy = KTX2Dataset::CreateCopy;
433 :
434 11 : GetGDALDriverManager()->RegisterDriver(poDriver);
435 : }
|