| 6970 | } |
| 6971 | |
| 6972 | avifResult avifDecoderNextImage(avifDecoder * decoder) |
| 6973 | { |
| 6974 | avifDiagnosticsClearError(&decoder->diag); |
| 6975 | |
| 6976 | if (!decoder->data || decoder->data->tiles.count == 0) { |
| 6977 | // Nothing has been parsed yet |
| 6978 | return AVIF_RESULT_NO_CONTENT; |
| 6979 | } |
| 6980 | |
| 6981 | if (!decoder->io || !decoder->io->read) { |
| 6982 | return AVIF_RESULT_IO_NOT_SET; |
| 6983 | } |
| 6984 | |
| 6985 | if (avifDecoderDataFrameFullyDecoded(decoder->data)) { |
| 6986 | // A frame was decoded during the last avifDecoderNextImage() call. |
| 6987 | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
| 6988 | decoder->data->tileInfos[c].decodedTileCount = 0; |
| 6989 | } |
| 6990 | } |
| 6991 | |
| 6992 | AVIF_ASSERT_OR_RETURN(decoder->data->tiles.count == (decoder->data->tileInfos[AVIF_ITEM_CATEGORY_COUNT - 1].firstTileIndex + |
| 6993 | decoder->data->tileInfos[AVIF_ITEM_CATEGORY_COUNT - 1].tileCount)); |
| 6994 | |
| 6995 | const uint32_t nextImageIndex = (uint32_t)(decoder->imageIndex + 1); |
| 6996 | |
| 6997 | // Ensure that we have created the codecs before proceeding with the decoding. |
| 6998 | if (!decoder->data->tiles.tile[0].codec) { |
| 6999 | AVIF_CHECKRES(avifDecoderCreateCodecs(decoder)); |
| 7000 | } |
| 7001 | |
| 7002 | // Acquire all sample data for the current image first, allowing for any read call to bail out |
| 7003 | // with AVIF_RESULT_WAITING_ON_IO harmlessly / idempotently, unless decoder->allowIncremental. |
| 7004 | avifResult prepareTileResult[AVIF_ITEM_CATEGORY_COUNT]; |
| 7005 | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
| 7006 | prepareTileResult[c] = avifDecoderPrepareTiles(decoder, nextImageIndex, &decoder->data->tileInfos[c]); |
| 7007 | if (!decoder->allowIncremental || (prepareTileResult[c] != AVIF_RESULT_WAITING_ON_IO)) { |
| 7008 | AVIF_CHECKRES(prepareTileResult[c]); |
| 7009 | } |
| 7010 | } |
| 7011 | |
| 7012 | // Decode all available color tiles now, then all available alpha tiles, then all available bit |
| 7013 | // depth extension tiles. The order of appearance of the tiles in the bitstream is left to the |
| 7014 | // encoder's choice, and decoding as many as possible of each category in parallel is beneficial |
| 7015 | // for incremental decoding, as pixel rows need all channels to be decoded before being |
| 7016 | // accessible to the user. |
| 7017 | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
| 7018 | AVIF_CHECKRES(avifDecoderDecodeTiles(decoder, nextImageIndex, &decoder->data->tileInfos[c])); |
| 7019 | } |
| 7020 | |
| 7021 | if (!avifDecoderDataFrameFullyDecoded(decoder->data)) { |
| 7022 | AVIF_ASSERT_OR_RETURN(decoder->allowIncremental); |
| 7023 | // The image is not completely decoded. There should be no error unrelated to missing bytes, |
| 7024 | // and at least some missing bytes. |
| 7025 | avifResult firstNonOkResult = AVIF_RESULT_OK; |
| 7026 | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
| 7027 | AVIF_ASSERT_OR_RETURN(prepareTileResult[c] == AVIF_RESULT_OK || prepareTileResult[c] == AVIF_RESULT_WAITING_ON_IO); |
| 7028 | if (firstNonOkResult == AVIF_RESULT_OK) { |
| 7029 | firstNonOkResult = prepareTileResult[c]; |