| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <stdint.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <KHR/khr_df.h> |
| 18 |
#include "dfd.h" |
| 19 |
|
| 20 |
static uint32_t bit_ceil(uint32_t x) { |
| 21 |
x -= 1; |
| 22 |
for (uint32_t i = 0; i < sizeof(x) * 8; ++i) |
| 23 |
if (1u << i > x) |
| 24 |
return 1u << i; |
| 25 |
return 0; |
| 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 |
enum InterpretDFDResult interpretDFD(const uint32_t *DFD, |
| 65 |
InterpretedDFDChannel *R, |
| 66 |
InterpretedDFDChannel *G, |
| 67 |
InterpretedDFDChannel *B, |
| 68 |
InterpretedDFDChannel *A, |
| 69 |
uint32_t *wordBytes) |
| 70 |
{ |
| 71 |
|
| 72 |
|
| 73 |
const uint32_t *BDFDB = DFD+1; |
| 74 |
|
| 75 |
uint32_t numSamples = KHR_DFDSAMPLECOUNT(BDFDB); |
| 76 |
if (numSamples == 0) |
| 77 |
return i_UNSUPPORTED_CHANNEL_TYPES; |
| 78 |
|
| 79 |
int determinedEndianness = 0; |
| 80 |
enum InterpretDFDResult result = 0; |
| 81 |
|
| 82 |
bool isDepthStencil = false; |
| 83 |
|
| 84 |
|
| 85 |
R->offset = R->size = 0; |
| 86 |
G->offset = G->size = 0; |
| 87 |
B->offset = B->size = 0; |
| 88 |
A->offset = A->size = 0; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if ((BDFDB[KHR_DF_WORD_BYTESPLANE0] & ~KHR_DF_MASK_BYTESPLANE0) |
| 96 |
|| BDFDB[KHR_DF_WORD_BYTESPLANE4]) return i_UNSUPPORTED_MULTIPLE_PLANES; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
for (uint32_t sampleCounter = 0; sampleCounter < numSamples; ++sampleCounter) { |
| 105 |
uint32_t offset = KHR_DFDSVAL(BDFDB, sampleCounter, BITOFFSET); |
| 106 |
uint32_t length = KHR_DFDSVAL(BDFDB, sampleCounter, BITLENGTH) + 1; |
| 107 |
if ((offset & 0x7U) || ((offset + length) & 0x7U)) { |
| 108 |
result |= i_PACKED_FORMAT_BIT; |
| 109 |
|
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
bool hasSigned = false; |
| 116 |
bool hasFloat = false; |
| 117 |
bool hasNormalized = false; |
| 118 |
bool hasFixed = false; |
| 119 |
khr_df_model_e model = KHR_DFDVAL(BDFDB, MODEL); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
for (uint32_t i = 0; i < numSamples; ++i) { |
| 124 |
const bool isSigned = (KHR_DFDSVAL(BDFDB, i, QUALIFIERS) & KHR_DF_SAMPLE_DATATYPE_SIGNED) != 0; |
| 125 |
const bool isFloat = (KHR_DFDSVAL(BDFDB, i, QUALIFIERS) & KHR_DF_SAMPLE_DATATYPE_FLOAT) != 0; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
bool isFixed; |
| 133 |
bool isNormalized; |
| 134 |
if (isFloat) { |
| 135 |
isNormalized = *(float*) (void*) &BDFDB[KHR_DF_WORD_SAMPLESTART + |
| 136 |
KHR_DF_WORD_SAMPLEWORDS * i + |
| 137 |
KHR_DF_SAMPLEWORD_SAMPLEUPPER] != 1.0f; |
| 138 |
isFixed = false; |
| 139 |
} else { |
| 140 |
uint32_t sampleUpper = KHR_DFDSVAL(BDFDB, i, SAMPLEUPPER); |
| 141 |
uint32_t maxVal = 1U << KHR_DFDSVAL(BDFDB, i, BITLENGTH); |
| 142 |
if (!isSigned) maxVal <<= 1; |
| 143 |
maxVal--; |
| 144 |
isFixed = 1U < sampleUpper && sampleUpper < maxVal; |
| 145 |
isNormalized = !isFixed && sampleUpper != 1U; |
| 146 |
} |
| 147 |
hasSigned |= isSigned; |
| 148 |
hasFixed |= isFixed; |
| 149 |
hasFloat |= isFloat; |
| 150 |
|
| 151 |
|
| 152 |
if (KHR_DFDSVAL(BDFDB, i, BITLENGTH) > 0) |
| 153 |
hasNormalized |= isNormalized; |
| 154 |
} |
| 155 |
result |= hasSigned ? i_SIGNED_FORMAT_BIT : 0; |
| 156 |
result |= hasFloat ? i_FLOAT_FORMAT_BIT : 0; |
| 157 |
result |= hasNormalized ? i_NORMALIZED_FORMAT_BIT : 0; |
| 158 |
result |= hasFixed ? i_FIXED_FORMAT_BIT : 0; |
| 159 |
|
| 160 |
|
| 161 |
if (model == KHR_DF_MODEL_YUVSDA) { |
| 162 |
result |= i_NORMALIZED_FORMAT_BIT; |
| 163 |
result |= i_COMPRESSED_FORMAT_BIT; |
| 164 |
result |= i_YUVSDA_FORMAT_BIT; |
| 165 |
|
| 166 |
for (uint32_t i = 0; i < numSamples; ++i) { |
| 167 |
switch (KHR_DFDSVAL(BDFDB, i, CHANNELID)) { |
| 168 |
case KHR_DF_CHANNEL_YUVSDA_Y: |
| 169 |
case KHR_DF_CHANNEL_YUVSDA_U: |
| 170 |
case KHR_DF_CHANNEL_YUVSDA_V: |
| 171 |
case KHR_DF_CHANNEL_YUVSDA_A: |
| 172 |
break; |
| 173 |
case KHR_DF_CHANNEL_YUVSDA_DEPTH: |
| 174 |
case KHR_DF_CHANNEL_YUVSDA_STENCIL: |
| 175 |
isDepthStencil = true; |
| 176 |
break; |
| 177 |
default: |
| 178 |
return i_UNSUPPORTED_CHANNEL_TYPES; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
uint32_t largestSampleSize = 0; |
| 184 |
for (uint32_t i = 0; i < numSamples; ++i) { |
| 185 |
uint32_t length = KHR_DFDSVAL(BDFDB, i, BITLENGTH) + 1; |
| 186 |
if (largestSampleSize < length) |
| 187 |
largestSampleSize = length; |
| 188 |
} |
| 189 |
*wordBytes = ((result & i_PACKED_FORMAT_BIT) ? 4 : 1) * bit_ceil(largestSampleSize) / 8; |
| 190 |
|
| 191 |
} else if (KHR_DFDVAL(BDFDB, MODEL) == KHR_DF_MODEL_RGBSDA) { |
| 192 |
|
| 193 |
if (KHR_DFDVAL(BDFDB, TRANSFER) == KHR_DF_TRANSFER_SRGB) result |= i_SRGB_FORMAT_BIT; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
for (uint32_t sampleCounter = 0; sampleCounter < numSamples; ++sampleCounter) { |
| 199 |
if (KHR_DFDSVAL(BDFDB, sampleCounter, SAMPLEPOSITION_ALL)) |
| 200 |
return i_UNSUPPORTED_MULTIPLE_SAMPLE_LOCATIONS; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if (model == KHR_DF_MODEL_RGBSDA || model == KHR_DF_MODEL_YUVSDA) { |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
for (uint32_t sampleCounter = 0; sampleCounter < numSamples; ++sampleCounter) { |
| 209 |
switch (KHR_DFDSVAL(BDFDB, sampleCounter, CHANNELID)) { |
| 210 |
case KHR_DF_CHANNEL_RGBSDA_DEPTH: |
| 211 |
case KHR_DF_CHANNEL_RGBSDA_STENCIL: |
| 212 |
isDepthStencil = true; |
| 213 |
break; |
| 214 |
default: |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
if (!isDepthStencil) { |
| 221 |
for (uint32_t i = 0; i < numSamples; ++i) { |
| 222 |
const bool isSigned = (KHR_DFDSVAL(BDFDB, i, QUALIFIERS) & KHR_DF_SAMPLE_DATATYPE_SIGNED) != 0; |
| 223 |
const bool isFloat = (KHR_DFDSVAL(BDFDB, i, QUALIFIERS) & KHR_DF_SAMPLE_DATATYPE_FLOAT) != 0; |
| 224 |
|
| 225 |
if (isSigned != hasSigned) |
| 226 |
return i_UNSUPPORTED_MIXED_CHANNELS; |
| 227 |
if (isFloat != hasFloat) |
| 228 |
return i_UNSUPPORTED_MIXED_CHANNELS; |
| 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 |
if ((result & i_PACKED_FORMAT_BIT)) { |
| 255 |
|
| 256 |
uint32_t currentChannel = ~0U; |
| 257 |
uint32_t currentBitOffset = 0; |
| 258 |
uint32_t currentByteOffset = 0; |
| 259 |
uint32_t currentBitLength = 0; |
| 260 |
*wordBytes = (BDFDB[KHR_DF_WORD_BYTESPLANE0] & 0xFFU); |
| 261 |
for (uint32_t sampleCounter = 0; sampleCounter < numSamples; ++sampleCounter) { |
| 262 |
uint32_t sampleBitOffset = KHR_DFDSVAL(BDFDB, sampleCounter, BITOFFSET); |
| 263 |
uint32_t sampleByteOffset = sampleBitOffset >> 3U; |
| 264 |
|
| 265 |
uint32_t sampleBitLength = KHR_DFDSVAL(BDFDB, sampleCounter, BITLENGTH) + 1; |
| 266 |
uint32_t sampleChannel = KHR_DFDSVAL(BDFDB, sampleCounter, CHANNELID); |
| 267 |
InterpretedDFDChannel *sampleChannelPtr; |
| 268 |
switch (sampleChannel) { |
| 269 |
case KHR_DF_CHANNEL_RGBSDA_RED: |
| 270 |
sampleChannelPtr = R; |
| 271 |
break; |
| 272 |
case KHR_DF_CHANNEL_RGBSDA_GREEN: |
| 273 |
sampleChannelPtr = G; |
| 274 |
break; |
| 275 |
case KHR_DF_CHANNEL_RGBSDA_BLUE: |
| 276 |
sampleChannelPtr = B; |
| 277 |
break; |
| 278 |
case KHR_DF_CHANNEL_RGBSDA_DEPTH: |
| 279 |
sampleChannelPtr = R; |
| 280 |
break; |
| 281 |
case KHR_DF_CHANNEL_RGBSDA_STENCIL: |
| 282 |
sampleChannelPtr = G; |
| 283 |
break; |
| 284 |
case KHR_DF_CHANNEL_RGBSDA_ALPHA: |
| 285 |
sampleChannelPtr = A; |
| 286 |
break; |
| 287 |
default: |
| 288 |
return i_UNSUPPORTED_CHANNEL_TYPES; |
| 289 |
} |
| 290 |
if (sampleChannel == currentChannel) { |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
if (sampleByteOffset == currentByteOffset - 1U && |
| 296 |
((currentBitOffset + currentBitLength) & 7U) == 0 && |
| 297 |
(sampleBitOffset & 7U) == 0) { |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
result |= i_BIG_ENDIAN_FORMAT_BIT; |
| 302 |
determinedEndianness = 1; |
| 303 |
} else { |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
return i_UNSUPPORTED_NONTRIVIAL_ENDIANNESS; |
| 308 |
} |
| 309 |
|
| 310 |
currentBitOffset = sampleBitOffset; |
| 311 |
currentByteOffset = sampleByteOffset; |
| 312 |
currentBitLength = sampleBitLength; |
| 313 |
|
| 314 |
sampleChannelPtr->size += sampleBitLength; |
| 315 |
} else { |
| 316 |
|
| 317 |
currentChannel = sampleChannel; |
| 318 |
currentBitOffset = sampleBitOffset; |
| 319 |
currentByteOffset = sampleByteOffset; |
| 320 |
currentBitLength = sampleBitLength; |
| 321 |
if (sampleChannelPtr->size) { |
| 322 |
if (model == KHR_DF_MODEL_YUVSDA && sampleChannel == KHR_DF_CHANNEL_YUVSDA_Y) { |
| 323 |
if (sampleChannelPtr == R) { |
| 324 |
|
| 325 |
if (A->size == 0) { |
| 326 |
sampleChannelPtr = A; |
| 327 |
} else { |
| 328 |
|
| 329 |
return i_UNSUPPORTED_CHANNEL_TYPES; |
| 330 |
} |
| 331 |
} |
| 332 |
} else { |
| 333 |
|
| 334 |
return i_UNSUPPORTED_NONTRIVIAL_ENDIANNESS; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
sampleChannelPtr->offset = sampleBitOffset; |
| 340 |
sampleChannelPtr->size = sampleBitLength; |
| 341 |
} |
| 342 |
} |
| 343 |
if ((result & i_BIG_ENDIAN_FORMAT_BIT)) { |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
uint32_t offsetMask = (*wordBytes - 1U) << 3U; |
| 348 |
R->offset ^= offsetMask; |
| 349 |
G->offset ^= offsetMask; |
| 350 |
B->offset ^= offsetMask; |
| 351 |
A->offset ^= offsetMask; |
| 352 |
} |
| 353 |
} else { |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
uint32_t currentChannel = ~0U; |
| 358 |
uint32_t currentByteOffset = 0; |
| 359 |
uint32_t currentByteLength = 0; |
| 360 |
for (uint32_t sampleCounter = 0; sampleCounter < numSamples; ++sampleCounter) { |
| 361 |
uint32_t sampleByteOffset = KHR_DFDSVAL(BDFDB, sampleCounter, BITOFFSET) >> 3U; |
| 362 |
uint32_t sampleByteLength = (KHR_DFDSVAL(BDFDB, sampleCounter, BITLENGTH) + 1) >> 3U; |
| 363 |
uint32_t sampleChannel = KHR_DFDSVAL(BDFDB, sampleCounter, CHANNELID); |
| 364 |
InterpretedDFDChannel *sampleChannelPtr; |
| 365 |
switch (sampleChannel) { |
| 366 |
case KHR_DF_CHANNEL_RGBSDA_RED: |
| 367 |
sampleChannelPtr = R; |
| 368 |
break; |
| 369 |
case KHR_DF_CHANNEL_RGBSDA_GREEN: |
| 370 |
sampleChannelPtr = G; |
| 371 |
break; |
| 372 |
case KHR_DF_CHANNEL_RGBSDA_BLUE: |
| 373 |
sampleChannelPtr = B; |
| 374 |
break; |
| 375 |
case KHR_DF_CHANNEL_RGBSDA_DEPTH: |
| 376 |
sampleChannelPtr = R; |
| 377 |
break; |
| 378 |
case KHR_DF_CHANNEL_RGBSDA_STENCIL: |
| 379 |
sampleChannelPtr = G; |
| 380 |
break; |
| 381 |
case KHR_DF_CHANNEL_RGBSDA_ALPHA: |
| 382 |
sampleChannelPtr = A; |
| 383 |
break; |
| 384 |
default: |
| 385 |
return i_UNSUPPORTED_CHANNEL_TYPES; |
| 386 |
} |
| 387 |
if (sampleChannel == currentChannel) { |
| 388 |
|
| 389 |
|
| 390 |
if (sampleByteOffset == currentByteOffset - 1) { |
| 391 |
if (determinedEndianness && !(result & i_BIG_ENDIAN_FORMAT_BIT)) { |
| 392 |
return i_UNSUPPORTED_NONTRIVIAL_ENDIANNESS; |
| 393 |
} |
| 394 |
|
| 395 |
result |= i_BIG_ENDIAN_FORMAT_BIT; |
| 396 |
determinedEndianness = 1; |
| 397 |
|
| 398 |
sampleChannelPtr->offset = sampleByteOffset; |
| 399 |
} else if (sampleByteOffset == currentByteOffset + currentByteLength) { |
| 400 |
if (determinedEndianness && (result & i_BIG_ENDIAN_FORMAT_BIT)) { |
| 401 |
return i_UNSUPPORTED_NONTRIVIAL_ENDIANNESS; |
| 402 |
} |
| 403 |
|
| 404 |
determinedEndianness = 1; |
| 405 |
} else { |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
return i_UNSUPPORTED_NONTRIVIAL_ENDIANNESS; |
| 410 |
} |
| 411 |
|
| 412 |
currentByteOffset = sampleByteOffset; |
| 413 |
currentByteLength = sampleByteLength; |
| 414 |
|
| 415 |
sampleChannelPtr->size += sampleByteLength; |
| 416 |
|
| 417 |
*wordBytes = sampleChannelPtr->size; |
| 418 |
} else { |
| 419 |
|
| 420 |
currentChannel = sampleChannel; |
| 421 |
currentByteOffset = sampleByteOffset; |
| 422 |
currentByteLength = sampleByteLength; |
| 423 |
if (sampleChannelPtr->size) { |
| 424 |
if (model == KHR_DF_MODEL_YUVSDA && sampleChannel == KHR_DF_CHANNEL_YUVSDA_Y) { |
| 425 |
if (sampleChannelPtr == R) { |
| 426 |
|
| 427 |
if (A->size == 0) { |
| 428 |
sampleChannelPtr = A; |
| 429 |
} else { |
| 430 |
|
| 431 |
return i_UNSUPPORTED_CHANNEL_TYPES; |
| 432 |
} |
| 433 |
} |
| 434 |
} else { |
| 435 |
|
| 436 |
return i_UNSUPPORTED_NONTRIVIAL_ENDIANNESS; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
sampleChannelPtr->offset = sampleByteOffset; |
| 442 |
sampleChannelPtr->size = sampleByteLength; |
| 443 |
|
| 444 |
*wordBytes = sampleByteLength; |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
} else { |
| 449 |
return i_UNSUPPORTED_CHANNEL_TYPES; |
| 450 |
} |
| 451 |
|
| 452 |
if (isDepthStencil) { |
| 453 |
|
| 454 |
|
| 455 |
uint32_t largerSize = R->size > G->size ? R->size : G->size; |
| 456 |
*wordBytes = bit_ceil(largerSize); |
| 457 |
} |
| 458 |
|
| 459 |
return result; |
| 460 |
} |