| 2507 | } |
| 2508 | |
| 2509 | static avifResult avifEncoderWriteMiniBox(avifEncoder * encoder, avifRWStream * s) |
| 2510 | { |
| 2511 | const avifEncoderItem * colorItem = NULL; |
| 2512 | const avifEncoderItem * alphaItem = NULL; |
| 2513 | const avifEncoderItem * gainmapItem = NULL; |
| 2514 | for (uint32_t itemIndex = 0; itemIndex < encoder->data->items.count; ++itemIndex) { |
| 2515 | avifEncoderItem * item = &encoder->data->items.item[itemIndex]; |
| 2516 | if (item->id == encoder->data->primaryItemID) { |
| 2517 | AVIF_ASSERT_OR_RETURN(!colorItem); |
| 2518 | colorItem = item; |
| 2519 | } else if (item->itemCategory == AVIF_ITEM_ALPHA && item->irefToID == encoder->data->primaryItemID) { |
| 2520 | AVIF_ASSERT_OR_RETURN(!alphaItem); |
| 2521 | alphaItem = item; |
| 2522 | } |
| 2523 | if (item->itemCategory == AVIF_ITEM_GAIN_MAP) { |
| 2524 | AVIF_ASSERT_OR_RETURN(!gainmapItem); |
| 2525 | gainmapItem = item; |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | AVIF_ASSERT_OR_RETURN(colorItem); |
| 2530 | const avifRWData * colorData = &colorItem->encodeOutput->samples.sample[0].data; |
| 2531 | const avifRWData * alphaData = alphaItem ? &alphaItem->encodeOutput->samples.sample[0].data : NULL; |
| 2532 | const avifRWData * gainmapData = gainmapItem ? &gainmapItem->encodeOutput->samples.sample[0].data : NULL; |
| 2533 | |
| 2534 | const avifImage * const image = encoder->data->imageMetadata; |
| 2535 | |
| 2536 | const avifBool hasAlpha = alphaItem != NULL; |
| 2537 | const avifBool alphaIsPremultiplied = encoder->data->imageMetadata->alphaPremultiplied; |
| 2538 | const avifBool hasGainmap = gainmapItem != NULL; |
| 2539 | const avifBool hasHdr = hasGainmap; // libavif only supports gainmap-based HDR encoding for now. |
| 2540 | const avifBool hasIcc = image->icc.size != 0; |
| 2541 | const uint32_t chromaSubsampling = image->yuvFormat == AVIF_PIXEL_FORMAT_YUV400 ? 0 |
| 2542 | : image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420 ? 1 |
| 2543 | : image->yuvFormat == AVIF_PIXEL_FORMAT_YUV422 ? 2 |
| 2544 | : 3; |
| 2545 | |
| 2546 | const avifColorPrimaries defaultColorPrimaries = hasIcc ? AVIF_COLOR_PRIMARIES_UNSPECIFIED : AVIF_COLOR_PRIMARIES_BT709; |
| 2547 | const avifTransferCharacteristics defaultTransferCharacteristics = hasIcc ? AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED |
| 2548 | : AVIF_TRANSFER_CHARACTERISTICS_SRGB; |
| 2549 | const avifMatrixCoefficients defaultMatrixCoefficients = chromaSubsampling == 0 ? AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED |
| 2550 | : AVIF_MATRIX_COEFFICIENTS_BT601; |
| 2551 | const avifBool hasExplicitCicp = image->colorPrimaries != defaultColorPrimaries || |
| 2552 | image->transferCharacteristics != defaultTransferCharacteristics || |
| 2553 | image->matrixCoefficients != defaultMatrixCoefficients; |
| 2554 | |
| 2555 | const avifBool floatFlag = AVIF_FALSE; |
| 2556 | const avifBool fullRange = image->yuvRange == AVIF_RANGE_FULL; |
| 2557 | |
| 2558 | // In AV1, the chroma_sample_position syntax element is not present for the YUV 4:2:2 format. |
| 2559 | // Assume that AV1 uses the same 4:2:2 chroma sample location as HEVC and VVC (colocated). |
| 2560 | if (image->yuvFormat != AVIF_PIXEL_FORMAT_YUV420 && image->yuvChromaSamplePosition != AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN) { |
| 2561 | avifDiagnosticsPrintf(&encoder->diag, |
| 2562 | "YUV chroma sample position %d is only supported with 4:2:0 YUV format in AV1", |
| 2563 | image->yuvChromaSamplePosition); |
| 2564 | return AVIF_RESULT_INVALID_ARGUMENT; |
| 2565 | } |
| 2566 | // For the YUV 4:2:0 format, assume centered sample position unless specified otherwise. |
no test coverage detected