-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make sure the gain map metadata is towards the start of the file. #2479
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,8 +80,8 @@ void ComparePartialYuva(const avifImage& image1, const avifImage& image2, | |
// byte_count were given to the decoder, for an image of height rows, split into | ||
// cells of cell_height rows. | ||
uint32_t GetMinDecodedRowCount(uint32_t height, uint32_t cell_height, | ||
bool has_alpha, size_t available_byte_count, | ||
size_t byte_count, | ||
bool has_alpha, bool has_gain_map, | ||
size_t available_byte_count, size_t byte_count, | ||
bool enable_fine_incremental_check) { | ||
// The whole image should be available when the full input is. | ||
if (available_byte_count >= byte_count) { | ||
|
@@ -104,15 +104,14 @@ uint32_t GetMinDecodedRowCount(uint32_t height, uint32_t cell_height, | |
} | ||
available_byte_count -= 500; | ||
byte_count -= 500; | ||
// Alpha, if any, is assumed to be located before the other planes and to | ||
// represent at most 50% of the payload. | ||
if (has_alpha) { | ||
if (available_byte_count <= (byte_count / 2)) { | ||
return 0; | ||
} | ||
available_byte_count -= byte_count / 2; | ||
byte_count -= byte_count / 2; | ||
// Extra planes (alpha, gain map), if any, are assumed to be located before | ||
// the other planes and to represent at most 50% of the payload. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this still holds with monochrome+alpha+gainmap, but this function is hard to tune anyway. Feel free to change or remove conditions if they are cumbersome. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I changed it a bit randomly, but after a while I realized the problem was not this formula but actually this part: // There is no valid AV1 payload smaller than 10 bytes, so all but one cell
// should be decoded if at most 10 bytes are missing.
if ((available_byte_count + 10) >= byte_count) { Because my image had tiles that were all identical, and so all the tiles pointed to the same blob in |
||
const int num_extra_planes = (has_alpha ? 1 : 0) + (has_gain_map ? 1 : 0); | ||
if (available_byte_count <= ((byte_count / 2) * num_extra_planes)) { | ||
return 0; | ||
} | ||
available_byte_count -= (byte_count / 2) * num_extra_planes; | ||
byte_count -= (byte_count / 2) * num_extra_planes; | ||
// Linearly map the input availability ratio to the decoded row ratio. | ||
const uint32_t min_decoded_cell_row_count = static_cast<uint32_t>( | ||
(height / cell_height) * available_byte_count / byte_count); | ||
|
@@ -332,6 +331,13 @@ avifResult DecodeIncrementally(const avifRWData& encoded_avif, | |
data.available.size = std::min(data.available.size + step, data.full_size); | ||
parse_result = avifDecoderParse(decoder); | ||
} | ||
if (data.available.size == data.full_size) { | ||
// Can happen if the data is in 'idat', or if some metadata is at the end of | ||
// the file. But ideally this should be avoided. | ||
printf( | ||
"WARNING: had to provide the whole file for avifDecoderParse() to " | ||
"succeed\n"); | ||
} | ||
maryla-uc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AVIF_CHECKRES(parse_result); | ||
|
||
// Decoding is incremental. | ||
|
@@ -347,13 +353,30 @@ avifResult DecodeIncrementally(const avifRWData& encoded_avif, | |
return AVIF_RESULT_INVALID_ARGUMENT; | ||
} | ||
const uint32_t decoded_row_count = avifDecoderDecodedRowCount(decoder); | ||
AVIF_CHECKERR(decoded_row_count >= previously_decoded_row_count, | ||
AVIF_RESULT_INVALID_ARGUMENT); | ||
if (decoded_row_count < previously_decoded_row_count) { | ||
printf("ERROR: decoded row count decreased from %d to %d\n", | ||
previously_decoded_row_count, decoded_row_count); | ||
return AVIF_RESULT_INVALID_ARGUMENT; | ||
maryla-uc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
bool has_gain_map = false; | ||
#if defined(AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP) | ||
has_gain_map = (reference.gainMap != nullptr); | ||
#endif | ||
const uint32_t min_decoded_row_count = GetMinDecodedRowCount( | ||
reference.height, cell_height, reference.alphaPlane != nullptr, | ||
data.available.size, data.full_size, enable_fine_incremental_check); | ||
AVIF_CHECKERR(decoded_row_count >= min_decoded_row_count, | ||
AVIF_RESULT_INVALID_ARGUMENT); | ||
has_gain_map, data.available.size, data.full_size, | ||
enable_fine_incremental_check); | ||
if (decoded_row_count != previously_decoded_row_count) { | ||
printf("bytes %zu decoded %d lines (expected min %d)\n", | ||
data.available.size, decoded_row_count, min_decoded_row_count); | ||
maryla-uc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if (decoded_row_count < min_decoded_row_count) { | ||
printf( | ||
"ERROR: expected to have decoded at least %d rows with %zu available " | ||
"bytes, but only %d were decoded\n", | ||
min_decoded_row_count, data.available.size, decoded_row_count); | ||
return AVIF_RESULT_INVALID_ARGUMENT; | ||
} | ||
ComparePartialYuva(reference, *decoder->image, decoded_row_count); | ||
|
||
previously_decoded_row_count = decoded_row_count; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR: Shouldn't Exif/XMP be at the end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same as the gain map metadata: if we put it at the end of the file, avifDecoderParse() will block untill it receives the whole file, unless
ignoreExif
andignoreXMP
are set. In the documentation for those fields we saysome of these payloads are (unfortunately) packed at the end of the file
so we consider this to be a bad thing.