-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom codec callback in avifEncoder
- Loading branch information
Showing
5 changed files
with
155 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2024 Google LLC | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
|
||
#include <algorithm> | ||
|
||
#include "avif/avif.h" | ||
#include "aviftest_helpers.h" | ||
#include "gtest/gtest.h" | ||
|
||
namespace avif { | ||
namespace { | ||
|
||
avifResult CustomEncodeImageFunc(avifEncoder* encoder, const avifImage*, | ||
const avifEncoderCustomEncodeImageArgs*) { | ||
if (encoder->customEncodeData != NULL) { | ||
return AVIF_RESULT_OK; // Overrides the AV1 codec encoding pipeline. | ||
} else { | ||
return AVIF_RESULT_NO_CONTENT; // Lets libavif encode the image item. | ||
} | ||
} | ||
|
||
avifResult CustomEncodeFinishFunc(avifEncoder* encoder, avifROData* sample) { | ||
avifROData* av1_payload = | ||
reinterpret_cast<avifROData*>(encoder->customEncodeData); | ||
if (av1_payload->size != 0) { | ||
*sample = *av1_payload; | ||
*av1_payload = AVIF_DATA_EMPTY; | ||
return AVIF_RESULT_OK; // Outputs a sample. | ||
} else { | ||
return AVIF_RESULT_NO_IMAGES_REMAINING; // Done. | ||
} | ||
} | ||
|
||
TEST(BasicTest, EncodeDecode) { | ||
ImagePtr image = testutil::CreateImage(12, 34, 8, AVIF_PIXEL_FORMAT_YUV420, | ||
AVIF_PLANES_YUV); | ||
ASSERT_NE(image, nullptr); | ||
testutil::FillImageGradient(image.get()); | ||
|
||
EncoderPtr encoder(avifEncoderCreate()); | ||
ASSERT_NE(encoder, nullptr); | ||
testutil::AvifRwData encoded; | ||
ASSERT_EQ(avifEncoderWrite(encoder.get(), image.get(), &encoded), | ||
AVIF_RESULT_OK); | ||
|
||
const uint8_t* kMdat = reinterpret_cast<const uint8_t*>("mdat"); | ||
const uint8_t* mdat_position = | ||
std::search(encoded.data, encoded.data + encoded.size, kMdat, kMdat + 4); | ||
ASSERT_NE(mdat_position, encoded.data + encoded.size); | ||
avifROData av1_payload{ | ||
mdat_position + 4, | ||
static_cast<size_t>((encoded.data + encoded.size) - (mdat_position + 4))}; | ||
|
||
EncoderPtr encoder_custom(avifEncoderCreate()); | ||
ASSERT_NE(encoder_custom, nullptr); | ||
encoder_custom->customEncodeData = reinterpret_cast<void*>(&av1_payload); | ||
encoder_custom->customEncodeImageFunc = CustomEncodeImageFunc; | ||
encoder_custom->customEncodeFinishFunc = CustomEncodeFinishFunc; | ||
testutil::AvifRwData encoded_custom; | ||
ASSERT_EQ( | ||
avifEncoderWrite(encoder_custom.get(), image.get(), &encoded_custom), | ||
AVIF_RESULT_OK); | ||
|
||
ASSERT_EQ(encoded.size, encoded_custom.size); | ||
EXPECT_TRUE(std::equal(encoded.data, encoded.data + encoded.size, | ||
encoded_custom.data)); | ||
} | ||
|
||
} // namespace | ||
} // namespace avif |