Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9bd4bd17 authored by Dichen Zhang's avatar Dichen Zhang Committed by Automerger Merge Worker
Browse files

JPEG/R refactor: rename jpegrecoverymap library to ultrahdr am: dbceb0e2 am: d2aa3aa7

parents dd863af3 d2aa3aa7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ package {
}

cc_library {
    name: "libjpegrecoverymap",
    name: "libultrahdr",
    host_supported: true,
    vendor_available: true,
    export_include_dirs: ["include"],
+0 −0

File moved.

+26 −25
Original line number Diff line number Diff line
@@ -16,9 +16,9 @@

#include <cmath>
#include <vector>
#include <jpegrecoverymap/gainmapmath.h>
#include <ultrahdr/gainmapmath.h>

namespace android::jpegrecoverymap {
namespace android::ultrahdr {

static const std::vector<float> kPqOETF = [] {
    std::vector<float> result;
@@ -396,45 +396,46 @@ Color bt2100ToP3(Color e) {

// TODO: confirm we always want to convert like this before calculating
// luminance.
ColorTransformFn getHdrConversionFn(jpegr_color_gamut sdr_gamut, jpegr_color_gamut hdr_gamut) {
ColorTransformFn getHdrConversionFn(ultrahdr_color_gamut sdr_gamut,
                                    ultrahdr_color_gamut hdr_gamut) {
  switch (sdr_gamut) {
    case JPEGR_COLORGAMUT_BT709:
    case ULTRAHDR_COLORGAMUT_BT709:
      switch (hdr_gamut) {
        case JPEGR_COLORGAMUT_BT709:
        case ULTRAHDR_COLORGAMUT_BT709:
          return identityConversion;
        case JPEGR_COLORGAMUT_P3:
        case ULTRAHDR_COLORGAMUT_P3:
          return p3ToBt709;
        case JPEGR_COLORGAMUT_BT2100:
        case ULTRAHDR_COLORGAMUT_BT2100:
          return bt2100ToBt709;
        case JPEGR_COLORGAMUT_UNSPECIFIED:
        case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
          return nullptr;
      }
      break;
    case JPEGR_COLORGAMUT_P3:
    case ULTRAHDR_COLORGAMUT_P3:
      switch (hdr_gamut) {
        case JPEGR_COLORGAMUT_BT709:
        case ULTRAHDR_COLORGAMUT_BT709:
          return bt709ToP3;
        case JPEGR_COLORGAMUT_P3:
        case ULTRAHDR_COLORGAMUT_P3:
          return identityConversion;
        case JPEGR_COLORGAMUT_BT2100:
        case ULTRAHDR_COLORGAMUT_BT2100:
          return bt2100ToP3;
        case JPEGR_COLORGAMUT_UNSPECIFIED:
        case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
          return nullptr;
      }
      break;
    case JPEGR_COLORGAMUT_BT2100:
    case ULTRAHDR_COLORGAMUT_BT2100:
      switch (hdr_gamut) {
        case JPEGR_COLORGAMUT_BT709:
        case ULTRAHDR_COLORGAMUT_BT709:
          return bt709ToBt2100;
        case JPEGR_COLORGAMUT_P3:
        case ULTRAHDR_COLORGAMUT_P3:
          return p3ToBt2100;
        case JPEGR_COLORGAMUT_BT2100:
        case ULTRAHDR_COLORGAMUT_BT2100:
          return identityConversion;
        case JPEGR_COLORGAMUT_UNSPECIFIED:
        case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
          return nullptr;
      }
      break;
    case JPEGR_COLORGAMUT_UNSPECIFIED:
    case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
      return nullptr;
  }
}
@@ -442,12 +443,12 @@ ColorTransformFn getHdrConversionFn(jpegr_color_gamut sdr_gamut, jpegr_color_gam

////////////////////////////////////////////////////////////////////////////////
// Gain map calculations
uint8_t encodeGain(float y_sdr, float y_hdr, jr_metadata_ptr metadata) {
uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata) {
  return encodeGain(y_sdr, y_hdr, metadata,
                    log2(metadata->minContentBoost), log2(metadata->maxContentBoost));
}

uint8_t encodeGain(float y_sdr, float y_hdr, jr_metadata_ptr metadata,
uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata,
                   float log2MinContentBoost, float log2MaxContentBoost) {
  float gain = 1.0f;
  if (y_sdr > 0.0f) {
@@ -462,14 +463,14 @@ uint8_t encodeGain(float y_sdr, float y_hdr, jr_metadata_ptr metadata,
                            * 255.0f);
}

Color applyGain(Color e, float gain, jr_metadata_ptr metadata) {
Color applyGain(Color e, float gain, ultrahdr_metadata_ptr metadata) {
  float logBoost = log2(metadata->minContentBoost) * (1.0f - gain)
                 + log2(metadata->maxContentBoost) * gain;
  float gainFactor = exp2(logBoost);
  return e * gainFactor;
}

Color applyGain(Color e, float gain, jr_metadata_ptr metadata, float displayBoost) {
Color applyGain(Color e, float gain, ultrahdr_metadata_ptr metadata, float displayBoost) {
  float logBoost = log2(metadata->minContentBoost) * (1.0f - gain)
                 + log2(metadata->maxContentBoost) * gain;
  float gainFactor = exp2(logBoost * displayBoost / metadata->maxContentBoost);
@@ -511,7 +512,7 @@ Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
    chroma_stride = luma_stride;
  }
  if (chroma_data == nullptr) {
    chroma_data = &reinterpret_cast<uint16_t*>(image->data)[image->luma_stride * image->height];
    chroma_data = &reinterpret_cast<uint16_t*>(image->data)[luma_stride * image->height];
  }

  size_t pixel_y_idx = y * luma_stride + x;
@@ -662,4 +663,4 @@ uint64_t colorToRgbaF16(Color e_gamma) {
       | (((uint64_t) floatToHalf(1.0f)) << 48);
}

} // namespace android::jpegrecoverymap
} // namespace android::ultrahdr
+34 −33
Original line number Diff line number Diff line
@@ -14,8 +14,8 @@
 * limitations under the License.
 */

#include <jpegrecoverymap/icc.h>
#include <jpegrecoverymap/gainmapmath.h>
#include <ultrahdr/icc.h>
#include <ultrahdr/gainmapmath.h>
#include <vector>
#include <utils/Log.h>

@@ -23,7 +23,7 @@
#define FLT_MAX 0x1.fffffep127f
#endif

namespace android::jpegrecoverymap {
namespace android::ultrahdr {
static void Matrix3x3_apply(const Matrix3x3* m, float* x) {
    float y0 = x[0] * m->vals[0][0] + x[1] * m->vals[0][1] + x[2] * m->vals[0][2];
    float y1 = x[0] * m->vals[1][0] + x[1] * m->vals[1][1] + x[2] * m->vals[1][2];
@@ -127,17 +127,17 @@ static void float_XYZD50_to_grid16_lab(const float* xyz_float, uint8_t* grid16_l
    }
}

std::string IccHelper::get_desc_string(const jpegr_transfer_function tf,
                                       const jpegr_color_gamut gamut) {
std::string IccHelper::get_desc_string(const ultrahdr_transfer_function tf,
                                       const ultrahdr_color_gamut gamut) {
    std::string result;
    switch (gamut) {
        case JPEGR_COLORGAMUT_BT709:
        case ULTRAHDR_COLORGAMUT_BT709:
            result += "sRGB";
            break;
        case JPEGR_COLORGAMUT_P3:
        case ULTRAHDR_COLORGAMUT_P3:
            result += "Display P3";
            break;
        case JPEGR_COLORGAMUT_BT2100:
        case ULTRAHDR_COLORGAMUT_BT2100:
            result += "Rec2020";
            break;
        default:
@@ -146,16 +146,16 @@ std::string IccHelper::get_desc_string(const jpegr_transfer_function tf,
    }
    result += " Gamut with ";
    switch (tf) {
        case JPEGR_TF_SRGB:
        case ULTRAHDR_TF_SRGB:
            result += "sRGB";
            break;
        case JPEGR_TF_LINEAR:
        case ULTRAHDR_TF_LINEAR:
            result += "Linear";
            break;
        case JPEGR_TF_PQ:
        case ULTRAHDR_TF_PQ:
            result += "PQ";
            break;
        case JPEGR_TF_HLG:
        case ULTRAHDR_TF_HLG:
            result += "HLG";
            break;
        default:
@@ -234,11 +234,11 @@ sp<DataStruct> IccHelper::write_trc_tag_for_linear() {
    return dataStruct;
}

float IccHelper::compute_tone_map_gain(const jpegr_transfer_function tf, float L) {
float IccHelper::compute_tone_map_gain(const ultrahdr_transfer_function tf, float L) {
    if (L <= 0.f) {
        return 1.f;
    }
    if (tf == JPEGR_TF_PQ) {
    if (tf == ULTRAHDR_TF_PQ) {
        // The PQ transfer function will map to the range [0, 1]. Linearly scale
        // it up to the range [0, 10,000/203]. We will then tone map that back
        // down to [0, 1].
@@ -251,7 +251,7 @@ float IccHelper::compute_tone_map_gain(const jpegr_transfer_function tf, float L
        constexpr float kToneMapB = 1.f / kOutputMaxLuminance;
        return kInputMaxLuminance * (1.f + kToneMapA * L) / (1.f + kToneMapB * L);
    }
    if (tf == JPEGR_TF_HLG) {
    if (tf == ULTRAHDR_TF_HLG) {
        // Let Lw be the brightness of the display in nits.
        constexpr float Lw = 203.f;
        const float gamma = 1.2f + 0.42f * std::log(Lw / 1000.f) / std::log(10.f);
@@ -295,7 +295,7 @@ void IccHelper::compute_lut_entry(const Matrix3x3& src_to_XYZD50, float rgb[3])
    float L = bt2100Luminance({{{rgb[0], rgb[1], rgb[2]}}});

    // Compute the tone map gain based on the luminance.
    float tone_map_gain = compute_tone_map_gain(JPEGR_TF_PQ, L);
    float tone_map_gain = compute_tone_map_gain(ULTRAHDR_TF_PQ, L);

    // Apply the tone map gain.
    for (size_t i = 0; i < kNumChannels; ++i) {
@@ -397,7 +397,8 @@ sp<DataStruct> IccHelper::write_mAB_or_mBA_tag(uint32_t type,
    return dataStruct;
}

sp<DataStruct> IccHelper::writeIccProfile(jpegr_transfer_function tf, jpegr_color_gamut gamut) {
sp<DataStruct> IccHelper::writeIccProfile(ultrahdr_transfer_function tf,
                                          ultrahdr_color_gamut gamut) {
    ICCHeader header;

    std::vector<std::pair<uint32_t, sp<DataStruct>>> tags;
@@ -409,13 +410,13 @@ sp<DataStruct> IccHelper::writeIccProfile(jpegr_transfer_function tf, jpegr_colo

    Matrix3x3 toXYZD50;
    switch (gamut) {
        case JPEGR_COLORGAMUT_BT709:
        case ULTRAHDR_COLORGAMUT_BT709:
            toXYZD50 = kSRGB;
            break;
        case JPEGR_COLORGAMUT_P3:
        case ULTRAHDR_COLORGAMUT_P3:
            toXYZD50 = kDisplayP3;
            break;
        case JPEGR_COLORGAMUT_BT2100:
        case ULTRAHDR_COLORGAMUT_BT2100:
            toXYZD50 = kRec2020;
            break;
        default:
@@ -437,8 +438,8 @@ sp<DataStruct> IccHelper::writeIccProfile(jpegr_transfer_function tf, jpegr_colo
    tags.emplace_back(kTAG_wtpt, write_xyz_tag(kD50_x, kD50_y, kD50_z));

    // Compute transfer curves.
    if (tf != JPEGR_TF_PQ) {
        if (tf == JPEGR_TF_HLG) {
    if (tf != ULTRAHDR_TF_PQ) {
        if (tf == ULTRAHDR_TF_HLG) {
            std::vector<uint8_t> trc_table;
            trc_table.resize(kTrcTableSize * 2);
            for (uint32_t i = 0; i < kTrcTableSize; ++i) {
@@ -462,32 +463,32 @@ sp<DataStruct> IccHelper::writeIccProfile(jpegr_transfer_function tf, jpegr_colo
    }

    // Compute CICP.
    if (tf == JPEGR_TF_HLG || tf == JPEGR_TF_PQ) {
    if (tf == ULTRAHDR_TF_HLG || tf == ULTRAHDR_TF_PQ) {
        // The CICP tag is present in ICC 4.4, so update the header's version.
        header.version = Endian_SwapBE32(0x04400000);

        uint32_t color_primaries = 0;
        if (gamut == JPEGR_COLORGAMUT_BT709) {
        if (gamut == ULTRAHDR_COLORGAMUT_BT709) {
            color_primaries = kCICPPrimariesSRGB;
        } else if (gamut == JPEGR_COLORGAMUT_P3) {
        } else if (gamut == ULTRAHDR_COLORGAMUT_P3) {
            color_primaries = kCICPPrimariesP3;
        }

        uint32_t transfer_characteristics = 0;
        if (tf == JPEGR_TF_SRGB) {
        if (tf == ULTRAHDR_TF_SRGB) {
            transfer_characteristics = kCICPTrfnSRGB;
        } else if (tf == JPEGR_TF_LINEAR) {
        } else if (tf == ULTRAHDR_TF_LINEAR) {
            transfer_characteristics = kCICPTrfnLinear;
        } else if (tf == JPEGR_TF_PQ) {
        } else if (tf == ULTRAHDR_TF_PQ) {
            transfer_characteristics = kCICPTrfnPQ;
        } else if (tf == JPEGR_TF_HLG) {
        } else if (tf == ULTRAHDR_TF_HLG) {
            transfer_characteristics = kCICPTrfnHLG;
        }
        tags.emplace_back(kTAG_cicp, write_cicp_tag(color_primaries, transfer_characteristics));
    }

    // Compute A2B0.
    if (tf == JPEGR_TF_PQ) {
    if (tf == ULTRAHDR_TF_PQ) {
        std::vector<uint8_t> a2b_grid;
        a2b_grid.resize(kGridSize * kGridSize * kGridSize * kNumChannels * 2);
        size_t a2b_grid_index = 0;
@@ -520,7 +521,7 @@ sp<DataStruct> IccHelper::writeIccProfile(jpegr_transfer_function tf, jpegr_colo
    }

    // Compute B2A0.
    if (tf == JPEGR_TF_PQ) {
    if (tf == ULTRAHDR_TF_PQ) {
        auto b2a_data = write_mAB_or_mBA_tag(kTAG_mBAType,
                                             /* has_a_curves */ false,
                                             /* grid_points */ nullptr,
@@ -541,7 +542,7 @@ sp<DataStruct> IccHelper::writeIccProfile(jpegr_transfer_function tf, jpegr_colo

    // Write the header.
    header.data_color_space = Endian_SwapBE32(Signature_RGB);
    header.pcs = Endian_SwapBE32(tf == JPEGR_TF_PQ ? Signature_Lab : Signature_XYZ);
    header.pcs = Endian_SwapBE32(tf == ULTRAHDR_TF_PQ ? Signature_Lab : Signature_XYZ);
    header.size = Endian_SwapBE32(profile_size);
    header.tag_count = Endian_SwapBE32(tags.size());

@@ -581,4 +582,4 @@ sp<DataStruct> IccHelper::writeIccProfile(jpegr_transfer_function tf, jpegr_colo
    return dataStruct;
}

} // namespace android::jpegrecoverymap
 No newline at end of file
} // namespace android::ultrahdr
 No newline at end of file
+13 −13
Original line number Diff line number Diff line
@@ -14,15 +14,15 @@
 * limitations under the License.
 */

#ifndef ANDROID_JPEGRECOVERYMAP_RECOVERYMAPMATH_H
#define ANDROID_JPEGRECOVERYMAP_RECOVERYMAPMATH_H
#ifndef ANDROID_ULTRAHDR_RECOVERYMAPMATH_H
#define ANDROID_ULTRAHDR_RECOVERYMAPMATH_H

#include <cmath>
#include <stdint.h>

#include <jpegrecoverymap/jpegr.h>
#include <ultrahdr/jpegr.h>

namespace android::jpegrecoverymap {
namespace android::ultrahdr {

#define CLIP3(x, min, max) ((x) < (min)) ? (min) : ((x) > (max)) ? (max) : (x)

@@ -132,7 +132,7 @@ inline uint16_t floatToHalf(float f) {
constexpr size_t kGainFactorPrecision = 10;
constexpr size_t kGainFactorNumEntries = 1 << kGainFactorPrecision;
struct GainLUT {
  GainLUT(jr_metadata_ptr metadata) {
  GainLUT(ultrahdr_metadata_ptr metadata) {
    for (int idx = 0; idx < kGainFactorNumEntries; idx++) {
      float value = static_cast<float>(idx) / static_cast<float>(kGainFactorNumEntries - 1);
      float logBoost = log2(metadata->minContentBoost) * (1.0f - value)
@@ -141,7 +141,7 @@ struct GainLUT {
    }
  }

  GainLUT(jr_metadata_ptr metadata, float displayBoost) {
  GainLUT(ultrahdr_metadata_ptr metadata, float displayBoost) {
    float boostFactor = displayBoost > 0 ? displayBoost / metadata->maxContentBoost : 1.0f;
    for (int idx = 0; idx < kGainFactorNumEntries; idx++) {
      float value = static_cast<float>(idx) / static_cast<float>(kGainFactorNumEntries - 1);
@@ -356,7 +356,7 @@ inline Color identityConversion(Color e) { return e; }
/*
 * Get the conversion to apply to the HDR image for gain map generation
 */
ColorTransformFn getHdrConversionFn(jpegr_color_gamut sdr_gamut, jpegr_color_gamut hdr_gamut);
ColorTransformFn getHdrConversionFn(ultrahdr_color_gamut sdr_gamut, ultrahdr_color_gamut hdr_gamut);


////////////////////////////////////////////////////////////////////////////////
@@ -366,16 +366,16 @@ ColorTransformFn getHdrConversionFn(jpegr_color_gamut sdr_gamut, jpegr_color_gam
 * Calculate the 8-bit unsigned integer gain value for the given SDR and HDR
 * luminances in linear space, and the hdr ratio to encode against.
 */
uint8_t encodeGain(float y_sdr, float y_hdr, jr_metadata_ptr metadata);
uint8_t encodeGain(float y_sdr, float y_hdr, jr_metadata_ptr metadata,
uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata);
uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata,
                   float log2MinContentBoost, float log2MaxContentBoost);

/*
 * Calculates the linear luminance in nits after applying the given gain
 * value, with the given hdr ratio, to the given sdr input in the range [0, 1].
 */
Color applyGain(Color e, float gain, jr_metadata_ptr metadata);
Color applyGain(Color e, float gain, jr_metadata_ptr metadata, float displayBoost);
Color applyGain(Color e, float gain, ultrahdr_metadata_ptr metadata);
Color applyGain(Color e, float gain, ultrahdr_metadata_ptr metadata, float displayBoost);
Color applyGainLUT(Color e, float gain, GainLUT& gainLUT);

/*
@@ -426,6 +426,6 @@ uint32_t colorToRgba1010102(Color e_gamma);
 */
uint64_t colorToRgbaF16(Color e_gamma);

} // namespace android::jpegrecoverymap
} // namespace android::ultrahdr

#endif // ANDROID_JPEGRECOVERYMAP_RECOVERYMAPMATH_H
#endif // ANDROID_ULTRAHDR_RECOVERYMAPMATH_H
Loading