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 : CSLConstList 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_UInt8;
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_UInt8, 4, pImage, GDT_UInt8, 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(GDALMD_COMPRESSION, "ETC1S",
307 34 : GDAL_MDD_IMAGE_STRUCTURE);
308 34 : break;
309 15 : case basist::basis_tex_format::cUASTC4x4:
310 15 : poDS->SetMetadataItem(GDALMD_COMPRESSION, "UASTC",
311 15 : GDAL_MDD_IMAGE_STRUCTURE);
312 15 : break;
313 : }
314 :
315 49 : if (nLayer == static_cast<uint32_t>(-1) && (nFaces >= 2 || nLayers >= 2))
316 : {
317 2 : CPLStringList aosSubdatasets;
318 1 : int nSubDS = 1;
319 3 : for (uint32_t iLayer = 0; iLayer < nLayers; ++iLayer)
320 : {
321 4 : for (uint32_t iFace = 0; iFace < nFaces; ++iFace)
322 : {
323 : aosSubdatasets.SetNameValue(
324 : CPLSPrintf("SUBDATASET_%d_NAME", nSubDS),
325 : CPLSPrintf("KTX2:\"%s\":%u:%u", poOpenInfo->pszFilename,
326 2 : iLayer, iFace));
327 : aosSubdatasets.SetNameValue(
328 : CPLSPrintf("SUBDATASET_%d_DESC", nSubDS),
329 : CPLSPrintf("Layer %u, face %u of %s", iLayer, iFace,
330 2 : poOpenInfo->pszFilename));
331 2 : nSubDS++;
332 : }
333 : }
334 1 : poDS->nRasterXSize = 0;
335 1 : poDS->nRasterYSize = 0;
336 1 : poDS->SetMetadata(aosSubdatasets.List(), GDAL_MDD_SUBDATASETS);
337 :
338 1 : poDS->SetPamFlags(poDS->GetPamFlags() & ~GPF_DIRTY);
339 :
340 1 : return poDS.release();
341 : }
342 48 : else if (nLayer != static_cast<uint32_t>(-1) && nLayer >= nLayers)
343 : {
344 1 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid layer number: %u",
345 : nLayer);
346 1 : return nullptr;
347 : }
348 47 : else if (nFace != static_cast<uint32_t>(-1) && nFace >= nFaces)
349 : {
350 1 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid face number: %u", nFace);
351 1 : return nullptr;
352 : }
353 :
354 46 : poDS->nRasterXSize = transcoder.get_width();
355 46 : poDS->nRasterYSize = transcoder.get_height();
356 :
357 46 : const int l_nBands = 3 + (transcoder.get_has_alpha() ? 1 : 0);
358 222 : for (int i = 1; i <= l_nBands; ++i)
359 : {
360 176 : poDS->SetBand(i, new KTX2RasterBand(poDS.get(), i));
361 : }
362 :
363 99 : for (uint32_t level_index = 0; level_index < transcoder.get_levels();
364 : ++level_index)
365 : {
366 : basist::ktx2_image_level_info level_info;
367 53 : uint32_t layer_index = 0;
368 53 : uint32_t face_index = 0;
369 53 : if (transcoder.get_image_level_info(level_info, level_index,
370 : layer_index, face_index))
371 : {
372 53 : CPLDebug(
373 : "KTX2",
374 : "level %u: width=%u, orig_width=%u, height=%u, orig_height=%u",
375 : level_index, level_info.m_width, level_info.m_orig_width,
376 : level_info.m_height, level_info.m_orig_height);
377 :
378 53 : if (level_index > 0)
379 : {
380 : auto poOverviewDS =
381 14 : std::make_unique<KTX2Dataset>(poDS.get(), level_index);
382 35 : for (int i = 1; i <= l_nBands; ++i)
383 : {
384 56 : poOverviewDS->SetBand(
385 28 : i, new KTX2RasterBand(poOverviewDS.get(), i));
386 : }
387 7 : poDS->m_apoOverviewsDS.emplace_back(std::move(poOverviewDS));
388 : }
389 : }
390 : }
391 :
392 46 : poDS->SetPamFlags(poDS->GetPamFlags() & ~GPF_DIRTY);
393 :
394 : // Initialize any PAM information.
395 46 : poDS->SetDescription(poOpenInfo->pszFilename);
396 46 : poDS->TryLoadXML(poOpenInfo->GetSiblingFiles());
397 :
398 46 : return poDS.release();
399 : }
400 :
401 : /************************************************************************/
402 : /* CreateCopy() */
403 : /************************************************************************/
404 :
405 56 : GDALDataset *KTX2Dataset::CreateCopy(const char *pszFilename,
406 : GDALDataset *poSrcDS, int /*bStrict*/,
407 : CSLConstList papszOptions,
408 : GDALProgressFunc pfnProgress,
409 : void *pProgressData)
410 : {
411 56 : if (!GDAL_KTX2_BASISU_CreateCopy(pszFilename, poSrcDS,
412 : true, // bIsKTX2
413 : papszOptions, pfnProgress, pProgressData))
414 : {
415 31 : return nullptr;
416 : }
417 50 : GDALOpenInfo oOpenInfo(pszFilename, GA_ReadOnly);
418 25 : return Open(&oOpenInfo);
419 : }
420 :
421 : /************************************************************************/
422 : /* GDALRegister_KTX2() */
423 : /************************************************************************/
424 :
425 11 : void GDALRegister_KTX2()
426 : {
427 11 : if (GDALGetDriverByName(KTX2_DRIVER_NAME) != nullptr)
428 0 : return;
429 :
430 11 : GDALDriver *poDriver = new GDALDriver();
431 11 : KTX2DriverSetCommonMetadata(poDriver);
432 :
433 11 : poDriver->pfnOpen = KTX2Dataset::Open;
434 11 : poDriver->pfnCreateCopy = KTX2Dataset::CreateCopy;
435 :
436 11 : GetGDALDriverManager()->RegisterDriver(poDriver);
437 : }
|