| 1 | /* -*- tab-width: 4; -*- */ |
| 2 | /* vi: set sw=2 ts=4 expandtab: */ |
| 3 | |
| 4 | /* Copyright 2019-2020 The Khronos Group Inc. |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * @file |
| 10 | * @~English |
| 11 | * @brief Utilities for querying info from a data format descriptor. |
| 12 | * @author Mark Callow |
| 13 | */ |
| 14 | |
| 15 | #include <stdint.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <KHR/khr_df.h> |
| 20 | #include "dfd.h" |
| 21 | |
| 22 | /** |
| 23 | * @~English |
| 24 | * @brief Get the number and size of the image components from a DFD. |
| 25 | * |
| 26 | * This simplified function is for use only with the DFDs for unpacked |
| 27 | * formats which means all components have the same size. |
| 28 | * |
| 29 | * @param DFD Pointer to a Data Format Descriptor to interpret, |
| 30 | described as 32-bit words in native endianness. |
| 31 | Note that this is the whole descriptor, not just |
| 32 | the basic descriptor block. |
| 33 | * @param numComponents pointer to a 32-bit word in which the number of |
| 34 | components will be written. |
| 35 | * @param componentByteLength pointer to a 32-bit word in which the size of |
| 36 | a component in bytes will be written. |
| 37 | */ |
| 38 | void |
| 39 | getDFDComponentInfoUnpacked(const uint32_t* DFD, uint32_t* numComponents, |
| 40 | uint32_t* componentByteLength) |
| 41 | { |
| 42 | const uint32_t *BDFDB = DFD+1; |
| 43 | uint32_t numSamples = KHR_DFDSAMPLECOUNT(BDFDB); |
| 44 | uint32_t sampleNumber; |
| 45 | uint32_t currentChannel = ~0U; /* Don't start matched. */ |
| 46 | |
| 47 | /* This is specifically for unpacked formats which means the size of */ |
| 48 | /* each component is the same. */ |
| 49 | *numComponents = 0; |
| 50 | for (sampleNumber = 0; sampleNumber < numSamples; ++sampleNumber) { |
| 51 | uint32_t sampleByteLength = (KHR_DFDSVAL(BDFDB, sampleNumber, BITLENGTH) + 1) >> 3U; |
| 52 | uint32_t sampleChannel = KHR_DFDSVAL(BDFDB, sampleNumber, CHANNELID); |
| 53 | |
| 54 | if (sampleChannel == currentChannel) { |
| 55 | /* Continuation of the same channel. */ |
| 56 | /* Accumulate the byte length. */ |
| 57 | *componentByteLength += sampleByteLength; |
| 58 | } else { |
| 59 | /* Everything is new. Hopefully. */ |
| 60 | currentChannel = sampleChannel; |
| 61 | (*numComponents)++; |
| 62 | *componentByteLength = sampleByteLength; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @~English |
| 69 | * @brief Return the number of "components" in the data. |
| 70 | * |
| 71 | * Calculates the number of uniques samples in the DFD by combining |
| 72 | * multiple samples for the same channel. For uncompressed colorModels |
| 73 | * this is the same as the number of components in the image data. For |
| 74 | * block-compressed color models this is the number of samples in |
| 75 | * the color model, typically 1 and in a few cases 2. |
| 76 | * |
| 77 | * @param DFD Pointer to a Data Format Descriptor for which, |
| 78 | * described as 32-bit words in native endianness. |
| 79 | * Note that this is the whole descriptor, not just |
| 80 | * the basic descriptor block. |
| 81 | */ |
| 82 | uint32_t getDFDNumComponents(const uint32_t* DFD) |
| 83 | { |
| 84 | const uint32_t *BDFDB = DFD+1; |
| 85 | uint32_t currentChannel = ~0U; /* Don't start matched. */ |
| 86 | uint32_t numComponents = 0; |
| 87 | uint32_t numSamples = KHR_DFDSAMPLECOUNT(BDFDB); |
| 88 | uint32_t sampleNumber; |
| 89 | |
| 90 | for (sampleNumber = 0; sampleNumber < numSamples; ++sampleNumber) { |
| 91 | uint32_t sampleChannel = KHR_DFDSVAL(BDFDB, sampleNumber, CHANNELID); |
| 92 | if (sampleChannel != currentChannel) { |
| 93 | numComponents++; |
| 94 | currentChannel = sampleChannel; |
| 95 | } |
| 96 | } |
| 97 | return numComponents; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @~English |
| 102 | * @brief Reconstruct the value of bytesPlane0 from sample info. |
| 103 | * |
| 104 | * @deprecated Use reconstructDFDBytesPlanesFromSamples. This does not handle |
| 105 | * the possible second plane of the ETC1S model. |
| 106 | * |
| 107 | * Reconstruct the value for data that has been variable-rate compressed |
| 108 | * and and whose bytesPlane0 value has been set to 0. For DFDs that |
| 109 | * are valid for KTX files. Little-endian data only and no multi-plane models |
| 110 | * except ETC1S. |
| 111 | * |
| 112 | * @param DFD Pointer to the Data Format Descriptor for which to provide |
| 113 | * the value described as 32-bit words in native endianness. |
| 114 | * Note that this is the whole descriptor, not just |
| 115 | * the basic descriptor block. |
| 116 | * @return The number of bytes a pixel occupies in bytesPlane0. |
| 117 | */ |
| 118 | uint32_t |
| 119 | reconstructDFDBytesPlane0FromSamples(const uint32_t* DFD) |
| 120 | { |
| 121 | const uint32_t *BDFDB = DFD+1; |
| 122 | uint32_t numSamples = KHR_DFDSAMPLECOUNT(BDFDB); |
| 123 | uint32_t sampleNumber; |
| 124 | |
| 125 | uint32_t bitsPlane0 = 0; |
| 126 | int32_t largestOffset = 0; |
| 127 | uint32_t sampleNumberWithLargestOffset = 0; |
| 128 | |
| 129 | // Special case these depth{,-stencil} formats. The unused bits are |
| 130 | // in the MSBs so have no visibility in the DFD therefore the max offset |
| 131 | // algorithm below returns a value that is too small. |
| 132 | if (KHR_DFDSVAL(BDFDB, 0, CHANNELID) == KHR_DF_CHANNEL_COMMON_DEPTH) { |
| 133 | if (numSamples == 1) { |
| 134 | if (KHR_DFDSVAL(BDFDB, 0, BITLENGTH) + 1 == 24) { |
| 135 | // X8_D24_UNORM_PACK32, |
| 136 | return 4; |
| 137 | } |
| 138 | } else if (numSamples == 2) { |
| 139 | if (KHR_DFDSVAL(BDFDB, 0, BITLENGTH) + 1 == 16) { |
| 140 | // D16_UNORM_S8_UINT |
| 141 | return 4; |
| 142 | } |
| 143 | if (KHR_DFDSVAL(BDFDB, 0, BITLENGTH) + 1 == 32 |
| 144 | && KHR_DFDSVAL(BDFDB, 1, CHANNELID) == KHR_DF_CHANNEL_COMMON_STENCIL) { |
| 145 | // D32_SFLOAT_S8_UINT |
| 146 | return 8; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | if (KHR_DFDVAL(BDFDB, MODEL) == KHR_DF_MODEL_ETC1S) { |
| 151 | // Size of the first plane. |
| 152 | return 8; |
| 153 | } |
| 154 | for (sampleNumber = 0; sampleNumber < numSamples; ++sampleNumber) { |
| 155 | int32_t sampleBitOffset = KHR_DFDSVAL(BDFDB, sampleNumber, BITOFFSET); |
| 156 | if (sampleBitOffset > largestOffset) { |
| 157 | largestOffset = sampleBitOffset; |
| 158 | sampleNumberWithLargestOffset = sampleNumber; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /* The sample bitLength field stores the bit length - 1. */ |
| 163 | uint32_t sampleBitLength = KHR_DFDSVAL(BDFDB, sampleNumberWithLargestOffset, BITLENGTH) + 1; |
| 164 | bitsPlane0 = largestOffset + sampleBitLength; |
| 165 | return bitsPlane0 >> 3U; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @~English |
| 170 | * @brief Reconstruct the values of bytesPlane[01] from sample info. |
| 171 | * |
| 172 | * Reconstruct the values for data that has been variable-rate compressed |
| 173 | * and whose bytesPlane[01] values have been set to 0 and update the |
| 174 | * fields of the target DFD. For DFDs that are valid for KTX files. |
| 175 | * Little-endian data only and no multi-plane models except ETC1S hence |
| 176 | * only looking at bytesPlane0 abd bytesPlane1. |
| 177 | * |
| 178 | * @param DFD Pointer to a Data Format Descriptor for which, |
| 179 | * described as 32-bit words in native endianness. |
| 180 | * Note that this is the whole descriptor, not just |
| 181 | * the basic descriptor block. |
| 182 | */ |
| 183 | |
| 184 | void |
| 185 | reconstructDFDBytesPlanesFromSamples(uint32_t* DFD) |
| 186 | { |
| 187 | uint32_t *BDFDB = DFD+1; |
| 188 | |
| 189 | KHR_DFDSETVAL(BDFDB, BYTESPLANE0, reconstructDFDBytesPlane0FromSamples(DFD)); |
| 190 | if (KHR_DFDVAL(BDFDB, MODEL) == KHR_DF_MODEL_ETC1S) { |
| 191 | if (KHR_DFDSAMPLECOUNT(BDFDB) == 2) |
| 192 | KHR_DFDSETVAL(BDFDB, BYTESPLANE1, 8); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @~English |
| 198 | * @brief Reconstruct the value of bytesPlane0 from sample info. |
| 199 | * |
| 200 | * @see reconstructDFDBytesPlane0FromSamples for details. |
| 201 | * @deprecated For backward comparibility only. Use |
| 202 | * reconstructDFDBytesPlanesFromSamples. |
| 203 | * |
| 204 | * @param DFD Pointer to the Data Format Descriptor for which to provide |
| 205 | * the value described as 32-bit words in native endianness. |
| 206 | * Note that this is the whole descriptor, not just |
| 207 | * the basic descriptor block. |
| 208 | * @param bytesPlane0 pointer to a 32-bit word in which the recreated |
| 209 | * value of bytesPlane0 will be written. |
| 210 | */ |
| 211 | void |
| 212 | recreateBytesPlane0FromSampleInfo(const uint32_t* DFD, uint32_t* bytesPlane0) |
| 213 | { |
| 214 | *bytesPlane0 = reconstructDFDBytesPlane0FromSamples(DFD); |
| 215 | } |