Skip to content

Commit

Permalink
Display P3 encoding update
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Mar 28, 2024
1 parent 532a088 commit 85814d6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class MainActivity : AppCompatActivity() {
// HDR EXAMPLES - https://us.zonerama.com/williamskeaguidingphotography/Photo/1000120226/1004888131
lifecycleScope.launch(Dispatchers.IO) {
val coder = HeifCoder()
val buffer = assets.open("pexels_house_wall_p3.jpg").source().buffer().readByteArray()
val buffer = assets.open("screenshot_test.png").source().buffer().readByteArray()
val bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.size)
val encoded = coder.encodeAvif(bitmap, quality = 99, preciseMode = PreciseMode.LOSSLESS)
val decoded = coder.decode(encoded)
Expand Down
6 changes: 2 additions & 4 deletions avif-coder/src/main/cpp/JniEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,8 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
int bitDepth = 8;
if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {
bitDepth = 8;
} else if ((info.format == ANDROID_BITMAP_FORMAT_RGBA_F16 &&
heifCompressionFormat == heif_compression_AV1) ||
(info.format == ANDROID_BITMAP_FORMAT_RGBA_1010102 &&
heifCompressionFormat == heif_compression_AV1)) {
} else if ((info.format == ANDROID_BITMAP_FORMAT_RGBA_F16 && heifCompressionFormat == heif_compression_AV1) ||
(info.format == ANDROID_BITMAP_FORMAT_RGBA_1010102 && heifCompressionFormat == heif_compression_AV1)) {
bitDepth = 10;
}

Expand Down
4 changes: 2 additions & 2 deletions avif-coder/src/main/cpp/colorspace/DataSpaceToNCLX.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ bool colorProfileFromDataSpace(uint8_t *data,
profile->full_range_flag = true;
isResolved = true;
} else if (dataSpace == ADataSpace::ADATASPACE_DISPLAY_P3) {
profile->transfer_characteristics = heif_transfer_characteristic_ITU_R_BT_709_5;
profile->matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_2020_2_non_constant_luminance;
profile->transfer_characteristics = heif_transfer_characteristic_IEC_61966_2_1;
profile->matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_601_6;
profile->color_primaries = heif_color_primaries_SMPTE_EG_432_1;
profile->full_range_flag = true;
isResolved = true;
Expand Down
22 changes: 21 additions & 1 deletion avif-coder/src/main/cpp/colorspace/eotf-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,33 @@ HWY_FAST_MATH_INLINE V SRGBEotf(D d, V v) {
return result;
}

HWY_FAST_MATH_INLINE float TransferFunction(float v,
const float c0,
const float c1,
const float c2,
const float c3,
const float cutoff,
const float gamma,
const float c4,
const float c5) {
if (v < 0) {
return 0.f;
} else if (v >= cutoff) {
return c0 * std::powf(c1 * v + c2, gamma) + c3;
} else if (v < 1) {
return c4 * v + c5;
} else {
return 1.f;
}
}

HWY_FAST_MATH_INLINE float SRGBOetf(const float linear) {
if (linear < 0.0f) {
return 0.0f;
} else if (linear < 0.0030412825601275209f) {
return linear * 12.92f;
} else if (linear < 1.0f) {
return 1.0550107189475866f * powf(linear, 1.0f / 2.4f) - 0.0550107189475866f;
return 1.0550107189475866f * std::powf(linear, 1.0f / 2.4f) - 0.0550107189475866f;
} else {
return 1.0f;
}
Expand Down
32 changes: 4 additions & 28 deletions avif-coder/src/main/cpp/hwy/ops/generic_ops-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2599,38 +2599,14 @@ HWY_API VFromD<DN> ReorderDemote2To(DN dn, V a, V b) {

template<class D, HWY_IF_F32_D(D)>
HWY_API VFromD<D> PromoteTo(D df, Vec<Rebind<uint16_t, D>> v) {
const RebindToUnsigned<decltype(df)> du32;

// Floats have 23 bits of mantissa.
// We want least significant 8 bits to be shifted to [ 0 .. 255 ], therefore need to add 2^23
// See this page for details: https://www.h-schmidt.net/FloatConverter/IEEE754.html
// If you want output floats in [ 0 .. 255.0 / 256.0 ] interval, change into 2^15 = 0x47000000
constexpr uint32_t offsetValue = 0x4b000000;
// Check disassembly & verify your compiler has moved this initialization outside the loop
const auto offsetInt = Set(du32, offsetValue);
// Bitwise is probably slightly faster than addition, delivers same results for our input
auto u32 = PromoteTo(du32, v);
u32 = Or(u32, offsetInt);
// The only FP operation required is subtraction, hopefully faster than UCVTF
return Sub(BitCast(df, u32), BitCast(df, offsetInt));
const RebindToUnsigned<decltype(df)> du32;
return ConvertTo(df, PromoteTo(du32, v));
}

template<class D, HWY_IF_F32_D(D)>
HWY_API VFromD<D> PromoteTo(D df, Vec<Rebind<uint8_t, D>> v) {
const RebindToUnsigned<decltype(df)> du32;

// Floats have 23 bits of mantissa.
// We want least significant 8 bits to be shifted to [ 0 .. 255 ], therefore need to add 2^23
// See this page for details: https://www.h-schmidt.net/FloatConverter/IEEE754.html
// If you want output floats in [ 0 .. 255.0 / 256.0 ] interval, change into 2^15 = 0x47000000
constexpr uint32_t offsetValue = 0x4b000000;
// Check disassembly & verify your compiler has moved this initialization outside the loop
const auto offsetInt = Set(du32, offsetValue);
// Bitwise is probably slightly faster than addition, delivers same results for our input
auto u32 = PromoteTo(du32, v);
u32 = Or(u32, offsetInt);
// The only FP operation required is subtraction, hopefully faster than UCVTF
return Sub(BitCast(df, u32), BitCast(df, offsetInt));
const RebindToUnsigned<decltype(df)> du32;
return ConvertTo(df, PromoteTo(du32, v));
}

template<class D, HWY_IF_U8_D(D)>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.2.0' apply false
id 'com.android.library' version '8.3.0' apply false
id 'com.android.library' version '8.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
id 'com.google.devtools.ksp' version '1.8.10-1.0.9' apply false
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#Wed Dec 28 11:02:12 ICT 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit 85814d6

Please sign in to comment.