|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// SPDX-License-Identifier: BSD-2-Clause |
| 3 | + |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include "avif/avif.h" |
| 7 | +#include "aviftest_helpers.h" |
| 8 | +#include "gtest/gtest.h" |
| 9 | + |
| 10 | +namespace avif { |
| 11 | +namespace { |
| 12 | + |
| 13 | +// Used to pass the data folder path to the GoogleTest suites. |
| 14 | +const char* data_path = nullptr; |
| 15 | + |
| 16 | +//------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +TEST(AomTuneMetricTest, GenerateDifferentBitstreams) { |
| 19 | + if (avifCodecName(AVIF_CODEC_CHOICE_AOM, AVIF_CODEC_FLAG_CAN_ENCODE) == |
| 20 | + nullptr || |
| 21 | + !testutil::Av1DecoderAvailable()) { |
| 22 | + GTEST_SKIP() << "Codec unavailable, skip test."; |
| 23 | + } |
| 24 | + |
| 25 | + const ImagePtr image = |
| 26 | + testutil::ReadImage(data_path, "paris_exif_xmp_icc.jpg"); |
| 27 | + ASSERT_NE(image, nullptr); |
| 28 | + // Speed up the test. |
| 29 | + image->width = 64; |
| 30 | + image->height = 64; |
| 31 | + |
| 32 | + std::vector<std::vector<uint8_t>> encoded_bitstreams; |
| 33 | + for (const char* tune : {"psnr", "ssim", "iq"}) { |
| 34 | + EncoderPtr encoder(avifEncoderCreate()); |
| 35 | + ASSERT_NE(encoder, nullptr); |
| 36 | + encoder->codecChoice = AVIF_CODEC_CHOICE_AOM; |
| 37 | + ASSERT_EQ(avifEncoderSetCodecSpecificOption(encoder.get(), "tune", tune), |
| 38 | + AVIF_RESULT_OK); |
| 39 | + testutil::AvifRwData encoded; |
| 40 | + ASSERT_EQ(avifEncoderWrite(encoder.get(), image.get(), &encoded), |
| 41 | + AVIF_RESULT_OK); |
| 42 | + for (const std::vector<uint8_t>& encoded_with_another_tune : |
| 43 | + encoded_bitstreams) { |
| 44 | + const bool is_same = encoded.size == encoded_with_another_tune.size() && |
| 45 | + std::equal(encoded.data, encoded.data + encoded.size, |
| 46 | + encoded_with_another_tune.data()); |
| 47 | + ASSERT_FALSE(is_same); |
| 48 | + } |
| 49 | + |
| 50 | + DecoderPtr decoder(avifDecoderCreate()); |
| 51 | + ASSERT_NE(decoder, nullptr); |
| 52 | + ImagePtr decoded(avifImageCreateEmpty()); |
| 53 | + ASSERT_NE(decoded, nullptr); |
| 54 | + ASSERT_EQ(avifDecoderReadMemory(decoder.get(), decoded.get(), encoded.data, |
| 55 | + encoded.size), |
| 56 | + AVIF_RESULT_OK); |
| 57 | + ASSERT_GT(testutil::GetPsnr(*image, *decoded), 32.0); |
| 58 | + |
| 59 | + encoded_bitstreams.emplace_back(encoded.data, encoded.data + encoded.size); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +TEST(AomSharpnessTest, GenerateDifferentBitstreams) { |
| 64 | + if (avifCodecName(AVIF_CODEC_CHOICE_AOM, AVIF_CODEC_FLAG_CAN_ENCODE) == |
| 65 | + nullptr || |
| 66 | + !testutil::Av1DecoderAvailable()) { |
| 67 | + GTEST_SKIP() << "Codec unavailable, skip test."; |
| 68 | + } |
| 69 | + |
| 70 | + const ImagePtr image = |
| 71 | + testutil::ReadImage(data_path, "paris_exif_xmp_icc.jpg"); |
| 72 | + ASSERT_NE(image, nullptr); |
| 73 | + // Speed up the test. |
| 74 | + image->width = 64; |
| 75 | + image->height = 64; |
| 76 | + |
| 77 | + std::vector<std::vector<uint8_t>> encoded_bitstreams; |
| 78 | + for (bool set_sharpness_to_0 : {false, true}) { |
| 79 | + EncoderPtr encoder(avifEncoderCreate()); |
| 80 | + ASSERT_NE(encoder, nullptr); |
| 81 | + encoder->codecChoice = AVIF_CODEC_CHOICE_AOM; |
| 82 | + if (set_sharpness_to_0) { |
| 83 | + ASSERT_EQ( |
| 84 | + avifEncoderSetCodecSpecificOption(encoder.get(), "sharpness", "0"), |
| 85 | + AVIF_RESULT_OK); |
| 86 | + } |
| 87 | + testutil::AvifRwData encoded; |
| 88 | + ASSERT_EQ(avifEncoderWrite(encoder.get(), image.get(), &encoded), |
| 89 | + AVIF_RESULT_OK); |
| 90 | + for (const std::vector<uint8_t>& encoded_with_another_tune : |
| 91 | + encoded_bitstreams) { |
| 92 | + const bool is_same = encoded.size == encoded_with_another_tune.size() && |
| 93 | + std::equal(encoded.data, encoded.data + encoded.size, |
| 94 | + encoded_with_another_tune.data()); |
| 95 | + ASSERT_FALSE(is_same); |
| 96 | + } |
| 97 | + |
| 98 | + DecoderPtr decoder(avifDecoderCreate()); |
| 99 | + ASSERT_NE(decoder, nullptr); |
| 100 | + ImagePtr decoded(avifImageCreateEmpty()); |
| 101 | + ASSERT_NE(decoded, nullptr); |
| 102 | + ASSERT_EQ(avifDecoderReadMemory(decoder.get(), decoded.get(), encoded.data, |
| 103 | + encoded.size), |
| 104 | + AVIF_RESULT_OK); |
| 105 | + ASSERT_GT(testutil::GetPsnr(*image, *decoded), 32.0); |
| 106 | + |
| 107 | + encoded_bitstreams.emplace_back(encoded.data, encoded.data + encoded.size); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +//------------------------------------------------------------------------------ |
| 112 | + |
| 113 | +} // namespace |
| 114 | +} // namespace avif |
| 115 | + |
| 116 | +int main(int argc, char** argv) { |
| 117 | + ::testing::InitGoogleTest(&argc, argv); |
| 118 | + if (argc != 2) { |
| 119 | + std::cerr << "There must be exactly one argument containing the path to " |
| 120 | + "the test data folder" |
| 121 | + << std::endl; |
| 122 | + return 1; |
| 123 | + } |
| 124 | + avif::data_path = argv[1]; |
| 125 | + return RUN_ALL_TESTS(); |
| 126 | +} |
0 commit comments