Skip to content

Commit

Permalink
Read the first two bytes of mini box directly
Browse files Browse the repository at this point in the history
The first two bytes of the mini box can be read directly without
checking any condition. Parse the fields in the first two bytes using
elementary operations (bitwise AND and right shift). No need to use
avifROStreamReadBits() yet.
  • Loading branch information
wantehchang committed Aug 23, 2024
1 parent 7176120 commit b2cac33
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -3585,26 +3585,51 @@ static avifResult avifParseMinimizedImageBox(avifMeta * meta,

meta->fromMiniBox = AVIF_TRUE;

uint32_t version;
AVIF_CHECKERR(avifROStreamReadBits(&s, &version, 2), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(2) version = 0;
// Read and parse the first two bytes, which contain no conditional constructs.
uint8_t data[2];
AVIF_CHECKERR(avifROStreamRead(&s, data, 2), AVIF_RESULT_BMFF_PARSE_FAILED);

// bit(2) version = 0;
//
// // flags
// bit(1) explicit_codec_types_flag;
// bit(1) float_flag;
// bit(1) full_range_flag;
// bit(1) alpha_flag;
// bit(1) explicit_cicp_flag;
// bit(1) hdr_flag;
int byte = data[0];
const avifBool hasHdr = byte & 0x1;
byte >>= 1;
const avifBool hasExplicitCicp = byte & 0x1;
byte >>= 1;
const avifBool hasAlpha = byte & 0x1;
byte >>= 1;
const avifBool fullRange = byte & 0x1;
byte >>= 1;
const avifBool floatFlag = byte & 0x1;
byte >>= 1;
const avifBool hasExplicitCodecTypes = byte & 0x1;
byte >>= 1;
const uint32_t version = byte;
AVIF_CHECKERR(version == 0, AVIF_RESULT_BMFF_PARSE_FAILED);

// flags
uint32_t hasExplicitCodecTypes, floatFlag, fullRange, hasAlpha, hasExplicitCicp, hasHdr, hasIcc, hasExif, hasXmp;
AVIF_CHECKERR(avifROStreamReadBits(&s, &hasExplicitCodecTypes, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) explicit_codec_types_flag;
AVIF_CHECKERR(avifROStreamReadBits(&s, &floatFlag, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) float_flag;
AVIF_CHECKERR(avifROStreamReadBits(&s, &fullRange, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) full_range_flag;
AVIF_CHECKERR(avifROStreamReadBits(&s, &hasAlpha, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) alpha_flag;
AVIF_CHECKERR(avifROStreamReadBits(&s, &hasExplicitCicp, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) explicit_cicp_flag;
AVIF_CHECKERR(avifROStreamReadBits(&s, &hasHdr, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) hdr_flag;
AVIF_CHECKERR(avifROStreamReadBits(&s, &hasIcc, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) icc_flag;
AVIF_CHECKERR(avifROStreamReadBits(&s, &hasExif, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) exif_flag;
AVIF_CHECKERR(avifROStreamReadBits(&s, &hasXmp, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) xmp_flag;

uint32_t chromaSubsampling, orientation;
AVIF_CHECKERR(avifROStreamReadBits(&s, &chromaSubsampling, 2), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(2) chroma_subsampling;
AVIF_CHECKERR(avifROStreamReadBits(&s, &orientation, 3), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(3) orientation_minus1;
++orientation;
// bit(1) icc_flag;
// bit(1) exif_flag;
// bit(1) xmp_flag;
//
// bit(2) chroma_subsampling;
// bit(3) orientation_minus1;
byte = data[1];
const uint32_t orientation = (byte & 0x7) + 1;
byte >>= 3;
const uint32_t chromaSubsampling = byte & 0x3;
byte >>= 2;
const avifBool hasXmp = byte & 0x1;
byte >>= 1;
const avifBool hasExif = byte & 0x1;
byte >>= 1;
const avifBool hasIcc = byte;

// Spatial extents
uint32_t smallDimensionsFlag, width, height;
Expand Down

0 comments on commit b2cac33

Please sign in to comment.