| 1291 | } |
| 1292 | |
| 1293 | static avifResult avifEncoderCreateBitDepthExtensionItems(avifEncoder * encoder, |
| 1294 | uint32_t gridCols, |
| 1295 | uint32_t gridRows, |
| 1296 | uint32_t gridWidth, |
| 1297 | uint32_t gridHeight, |
| 1298 | uint16_t colorItemID) |
| 1299 | { |
| 1300 | AVIF_ASSERT_OR_RETURN(encoder->sampleTransformRecipe == AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_8B_8B || |
| 1301 | encoder->sampleTransformRecipe == AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_12B_4B || |
| 1302 | encoder->sampleTransformRecipe == AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_12B_8B_OVERLAP_4B); |
| 1303 | |
| 1304 | // There are multiple possible ISOBMFF box hierarchies for translucent images, |
| 1305 | // using 'sato' (Sample Transform) derived image items: |
| 1306 | // - a primary 'sato' item uses a main color coded item and a hidden color coded item; each color coded |
| 1307 | // item has an auxiliary alpha coded item; the main color coded item and the 'sato' item are in |
| 1308 | // an 'altr' group (backward-compatible, implemented) |
| 1309 | // - a primary 'sato' item uses a main color coded item and a hidden color coded item; the primary |
| 1310 | // 'sato' item has an auxiliary alpha 'sato' item using two alpha coded items (backward-incompatible) |
| 1311 | // Likewise, there are multiple possible ISOBMFF box hierarchies for bit-depth-extended grids, |
| 1312 | // using 'sato' (Sample Transform) derived image items: |
| 1313 | // - a primary color 'grid', an auxiliary alpha 'grid', a hidden color 'grid', a hidden auxiliary alpha 'grid' |
| 1314 | // and a 'sato' using the two color 'grid's as input items in this order; the primary color item |
| 1315 | // and the 'sato' item being in an 'altr' group (backward-compatible, implemented) |
| 1316 | // - a primary 'grid' of 'sato' cells and an auxiliary alpha 'grid' of 'sato' cells (backward-incompatible) |
| 1317 | avifEncoderItem * sampleTransformItem = avifEncoderDataCreateItem(encoder->data, |
| 1318 | "sato", |
| 1319 | infeNameSampleTransform, |
| 1320 | /*infeNameSize=*/strlen(infeNameSampleTransform) + 1, |
| 1321 | /*cellIndex=*/0); |
| 1322 | AVIF_CHECKRES(avifEncoderWriteSampleTransformPayload(encoder, &sampleTransformItem->metadataPayload)); |
| 1323 | sampleTransformItem->itemCategory = AVIF_ITEM_SAMPLE_TRANSFORM; |
| 1324 | uint16_t sampleTransformItemID = sampleTransformItem->id; |
| 1325 | // 'altr' group |
| 1326 | AVIF_ASSERT_OR_RETURN(encoder->data->alternativeItemIDs.count == 0); |
| 1327 | uint16_t * alternativeItemID = (uint16_t *)avifArrayPush(&encoder->data->alternativeItemIDs); |
| 1328 | AVIF_CHECKERR(alternativeItemID != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
| 1329 | *alternativeItemID = sampleTransformItem->id; |
| 1330 | alternativeItemID = (uint16_t *)avifArrayPush(&encoder->data->alternativeItemIDs); |
| 1331 | AVIF_CHECKERR(alternativeItemID != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
| 1332 | *alternativeItemID = colorItemID; |
| 1333 | |
| 1334 | uint16_t bitDepthExtensionColorItemId; |
| 1335 | AVIF_CHECKRES( |
| 1336 | avifEncoderAddImageItems(encoder, gridCols, gridRows, gridWidth, gridHeight, AVIF_ITEM_SAMPLE_TRANSFORM_INPUT_0_COLOR, &bitDepthExtensionColorItemId)); |
| 1337 | avifEncoderItem * bitDepthExtensionColorItem = avifEncoderDataFindItemByID(encoder->data, bitDepthExtensionColorItemId); |
| 1338 | assert(bitDepthExtensionColorItem); |
| 1339 | bitDepthExtensionColorItem->hiddenImage = AVIF_TRUE; |
| 1340 | |
| 1341 | // Set the color and bit depth extension items' dimgFromID value to point to the sample transform item. |
| 1342 | // The color item shall be first, and the bit depth extension item second. avifEncoderFinish() writes the |
| 1343 | // dimg item references in item id order, so as long as colorItemID < bitDepthExtensionColorItemId, the order |
| 1344 | // will be correct. |
| 1345 | AVIF_ASSERT_OR_RETURN(colorItemID < bitDepthExtensionColorItemId); |
| 1346 | avifEncoderItem * colorItem = avifEncoderDataFindItemByID(encoder->data, colorItemID); |
| 1347 | AVIF_ASSERT_OR_RETURN(colorItem != NULL); |
| 1348 | AVIF_ASSERT_OR_RETURN(colorItem->dimgFromID == 0); // The internal API only allows one dimg value per item. |
| 1349 | colorItem->dimgFromID = sampleTransformItemID; |
| 1350 | bitDepthExtensionColorItem->dimgFromID = sampleTransformItemID; |
no test coverage detected