Skip to content

Commit

Permalink
Remove AVIF_HEADER_DEFAULT
Browse files Browse the repository at this point in the history
  • Loading branch information
y-guyon committed Aug 20, 2024
1 parent 93bd46d commit 9859239
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 32 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ The changes are relative to the previous release, unless the baseline is specifi
draft.
* Ignore gain maps with unsupported metadata. Handle gain maps with
writer_version > 0 correctly.
* Add AVIF_HEADER_DEFAULT which writes an empty HandlerBox name field instead of
"libavif". Select AVIF_HEADER_DEFAULT by default in avifEncoderCreate().
* Write an empty HandlerBox name field instead of "libavif" (saves 7 bytes).

## [1.1.1] - 2024-07-30

Expand Down
2 changes: 1 addition & 1 deletion apps/avifenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ int main(int argc, char * argv[])
settings.layered = AVIF_FALSE;
settings.layers = 0;
settings.speed = 6;
settings.headerFormat = AVIF_HEADER_DEFAULT;
settings.headerFormat = AVIF_HEADER_FULL;
settings.repetitionCount = AVIF_REPETITION_COUNT_INFINITE;
settings.keyframeInterval = 0;
settings.ignoreExif = AVIF_FALSE;
Expand Down
2 changes: 0 additions & 2 deletions include/avif/avif.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ typedef enum avifHeaderFormat
{
// AVIF file with an "avif" brand, a MetaBox and all its required boxes for maximum compatibility.
AVIF_HEADER_FULL,
// Same as AVIF_HEADER_FULL but the name field of the HandlerBox is empty instead of "libavif".
AVIF_HEADER_DEFAULT,
#if defined(AVIF_ENABLE_EXPERIMENTAL_MINI)
// AVIF file with a "mif3" brand and a MinimizedImageBox to reduce the encoded file size.
// This is based on the w24144 "Low-overhead image file format" MPEG proposal for HEIF.
Expand Down
14 changes: 5 additions & 9 deletions src/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ avifEncoder * avifEncoderCreate(void)
avifEncoderDestroy(encoder);
return NULL;
}
encoder->headerFormat = AVIF_HEADER_DEFAULT;
encoder->headerFormat = AVIF_HEADER_FULL;
#if defined(AVIF_ENABLE_EXPERIMENTAL_SAMPLE_TRANSFORM)
encoder->sampleTransformRecipe = AVIF_SAMPLE_TRANSFORM_NONE;
#endif
Expand Down Expand Up @@ -773,16 +773,12 @@ static avifResult avifEncoderWriteExtendedColorProperties(avifRWStream * dedupSt

static avifResult avifEncoderWriteHandlerBox(avifEncoder * encoder, avifRWStream * s, const char handler_type[4])
{
// ISO/IEC 14496-12, Section 8.4.3.3:
// name gives a human-readable name for the track type (for debugging and inspection purposes).
const char * handlerName = encoder->headerFormat == AVIF_HEADER_FULL ? "libavif" : "";

avifBoxMarker hdlr;
AVIF_CHECKRES(avifRWStreamWriteFullBox(s, "hdlr", AVIF_BOX_SIZE_TBD, 0, 0, &hdlr));
AVIF_CHECKRES(avifRWStreamWriteU32(s, 0)); // unsigned int(32) pre_defined = 0;
AVIF_CHECKRES(avifRWStreamWriteChars(s, handler_type, 4)); // unsigned int(32) handler_type;
AVIF_CHECKRES(avifRWStreamWriteZeros(s, 12)); // const unsigned int(32)[3] reserved = 0;
AVIF_CHECKRES(avifRWStreamWriteChars(s, handlerName, strlen(handlerName) + 1)); // string name; (writing null terminator)
AVIF_CHECKRES(avifRWStreamWriteU32(s, 0)); // unsigned int(32) pre_defined = 0;
AVIF_CHECKRES(avifRWStreamWriteChars(s, handler_type, 4)); // unsigned int(32) handler_type;
AVIF_CHECKRES(avifRWStreamWriteZeros(s, 12)); // const unsigned int(32)[3] reserved = 0;
AVIF_CHECKRES(avifRWStreamWriteChars(s, "", 1)); // string name; (writing null terminator)
avifRWStreamFinishBox(s, hdlr);
return AVIF_RESULT_OK;
}
Expand Down
37 changes: 19 additions & 18 deletions tests/gtest/avifheadertest.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024 Google LLC
// SPDX-License-Identifier: BSD-2-Clause

#include <algorithm>

#include "avif/avif.h"
#include "aviftest_helpers.h"
#include "gtest/gtest.h"
Expand All @@ -13,25 +15,24 @@ TEST(BasicTest, EncodeDecode) {
AVIF_PLANES_ALL);
ASSERT_NE(image, nullptr);
testutil::FillImageGradient(image.get());
const testutil::AvifRwData avif = testutil::Encode(image.get());
ASSERT_NE(avif.data, nullptr);

EncoderPtr encoder_header_full(avifEncoderCreate());
ASSERT_NE(encoder_header_full, nullptr);
encoder_header_full->headerFormat = AVIF_HEADER_FULL;
testutil::AvifRwData encoded_header_full;
ASSERT_EQ(avifEncoderWrite(encoder_header_full.get(), image.get(),
&encoded_header_full),
AVIF_RESULT_OK);

EncoderPtr encoder_header_default(avifEncoderCreate());
ASSERT_NE(encoder_header_default, nullptr);
encoder_header_default->headerFormat = AVIF_HEADER_DEFAULT;
testutil::AvifRwData encoded_header_default;
ASSERT_EQ(avifEncoderWrite(encoder_header_default.get(), image.get(),
&encoded_header_default),
AVIF_RESULT_OK);

// AVIF_HEADER_DEFAULT saves 7 bytes by omitting "libavif" as 'hdlr' name.
EXPECT_EQ(encoded_header_full.size, encoded_header_default.size + 7);
// Make sure the HandlerBox is as small as possible, meaning its name field is
// empty.
const uint8_t* kHdlr = reinterpret_cast<const uint8_t*>("hdlr");
uint8_t* hdlr_position =
std::search(avif.data, avif.data + avif.size, kHdlr, kHdlr + 4);
ASSERT_NE(hdlr_position, avif.data + avif.size);
// The previous four bytes represent the size of the box as a big endian
// unsigned integer.
constexpr uint8_t kExpectedHdlrSize =
/*size field*/ 4 + /*"hdlr"*/ 4 + /*version*/ 3 + /*flags*/ 1 +
/*pre_defined*/ 4 + /*handler_type*/ 4 + /*reserved*/ 4 * 3 + /*name*/ 1;
ASSERT_EQ(hdlr_position[-4], 0);
ASSERT_EQ(hdlr_position[-3], 0);
ASSERT_EQ(hdlr_position[-2], 0);
ASSERT_EQ(hdlr_position[-1], kExpectedHdlrSize);
}

} // namespace
Expand Down

0 comments on commit 9859239

Please sign in to comment.