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

Commit 129cb607 authored by Dichen Zhang's avatar Dichen Zhang Committed by Android (Google) Code Review
Browse files

Merge "JPEG/R refactor: rename "recovery map" to "gain map"" into udc-dev

parents 187a18c4 10959a4e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ cc_library {
    srcs: [
        "icc.cpp",
        "jpegr.cpp",
        "recoverymapmath.cpp",
        "gainmapmath.cpp",
        "jpegrutils.cpp",
        "multipictureformat.cpp",
    ],
+20 −20
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

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

namespace android::jpegrecoverymap {

@@ -441,13 +441,13 @@ ColorTransformFn getHdrConversionFn(jpegr_color_gamut sdr_gamut, jpegr_color_gam


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

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

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

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

Color applyRecoveryLUT(Color e, float recovery, RecoveryLUT& recoveryLUT) {
  float recoveryFactor = recoveryLUT.getRecoveryFactor(recovery);
  return e * recoveryFactor;
Color applyGainLUT(Color e, float gain, GainLUT& gainLUT) {
  float gainFactor = gainLUT.getGainFactor(gain);
  return e * gainFactor;
}

Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
 */

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

+30 −30
Original line number Diff line number Diff line
@@ -129,40 +129,40 @@ inline uint16_t floatToHalf(float f) {
            | (e > 143) * 0x7FFF;
}

constexpr size_t kRecoveryFactorPrecision = 10;
constexpr size_t kRecoveryFactorNumEntries = 1 << kRecoveryFactorPrecision;
struct RecoveryLUT {
  RecoveryLUT(jr_metadata_ptr metadata) {
    for (int idx = 0; idx < kRecoveryFactorNumEntries; idx++) {
      float value = static_cast<float>(idx) / static_cast<float>(kRecoveryFactorNumEntries - 1);
constexpr size_t kGainFactorPrecision = 10;
constexpr size_t kGainFactorNumEntries = 1 << kGainFactorPrecision;
struct GainLUT {
  GainLUT(jr_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)
                     + log2(metadata->maxContentBoost) * value;
      mRecoveryTable[idx] = exp2(logBoost);
      mGainTable[idx] = exp2(logBoost);
    }
  }

  RecoveryLUT(jr_metadata_ptr metadata, float displayBoost) {
  GainLUT(jr_metadata_ptr metadata, float displayBoost) {
    float boostFactor = displayBoost > 0 ? displayBoost / metadata->maxContentBoost : 1.0f;
    for (int idx = 0; idx < kRecoveryFactorNumEntries; idx++) {
      float value = static_cast<float>(idx) / static_cast<float>(kRecoveryFactorNumEntries - 1);
    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)
                     + log2(metadata->maxContentBoost) * value;
      mRecoveryTable[idx] = exp2(logBoost * boostFactor);
      mGainTable[idx] = exp2(logBoost * boostFactor);
    }
  }

  ~RecoveryLUT() {
  ~GainLUT() {
  }

  float getRecoveryFactor(float recovery) {
    uint32_t idx = static_cast<uint32_t>(recovery * (kRecoveryFactorNumEntries - 1));
  float getGainFactor(float gain) {
    uint32_t idx = static_cast<uint32_t>(gain * (kGainFactorNumEntries - 1));
    //TODO() : Remove once conversion modules have appropriate clamping in place
    idx = CLIP3(idx, 0, kRecoveryFactorNumEntries - 1);
    return mRecoveryTable[idx];
    idx = CLIP3(idx, 0, kGainFactorNumEntries - 1);
    return mGainTable[idx];
  }

private:
  float mRecoveryTable[kRecoveryFactorNumEntries];
  float mGainTable[kGainFactorNumEntries];
};

struct ShepardsIDW {
@@ -195,11 +195,11 @@ struct ShepardsIDW {
  // p60 p61 p62 p63 p64 p65 p66 p67
  // p70 p71 p72 p73 p74 p75 p76 p77

  // Recovery Map (for 4 scale factor) :-
  // Gain Map (for 4 scale factor) :-
  // m00 p01
  // m10 m11

  // Recovery sample of curr 4x4, right 4x4, bottom 4x4, bottom right 4x4 are used during
  // Gain sample of curr 4x4, right 4x4, bottom 4x4, bottom right 4x4 are used during
  // reconstruction. hence table weight size is 4.
  float* mWeights;
  // TODO: check if its ok to mWeights at places
@@ -354,29 +354,29 @@ Color bt2100ToP3(Color e);
inline Color identityConversion(Color e) { return e; }

/*
 * Get the conversion to apply to the HDR image for recovery map generation
 * 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);


////////////////////////////////////////////////////////////////////////////////
// Recovery map calculations
// Gain map calculations

/*
 * Calculate the 8-bit unsigned integer recovery value for the given SDR and HDR
 * 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 encodeRecovery(float y_sdr, float y_hdr, jr_metadata_ptr metadata);
uint8_t encodeRecovery(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, jr_metadata_ptr metadata,
                   float log2MinContentBoost, float log2MaxContentBoost);

/*
 * Calculates the linear luminance in nits after applying the given recovery
 * 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 applyRecovery(Color e, float recovery, jr_metadata_ptr metadata);
Color applyRecovery(Color e, float recovery, jr_metadata_ptr metadata, float displayBoost);
Color applyRecoveryLUT(Color e, float recovery, RecoveryLUT& recoveryLUT);
Color applyGain(Color e, float gain, jr_metadata_ptr metadata);
Color applyGain(Color e, float gain, jr_metadata_ptr metadata, float displayBoost);
Color applyGainLUT(Color e, float gain, GainLUT& gainLUT);

/*
 * Helper for sampling from YUV 420 images.
@@ -405,7 +405,7 @@ Color sampleYuv420(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, s
Color sampleP010(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y);

/*
 * Sample the recovery value for the map from a given x,y coordinate on a scale
 * Sample the gain value for the map from a given x,y coordinate on a scale
 * that is map scale factor larger than the map size.
 */
float sampleMap(jr_uncompressed_ptr map, float map_scale_factor, size_t x, size_t y);
+51 −51
Original line number Diff line number Diff line
@@ -58,14 +58,14 @@ struct jpegr_info_struct {
};

/*
 * Holds information for uncompressed image or recovery map.
 * Holds information for uncompressed image or gain map.
 */
struct jpegr_uncompressed_struct {
    // Pointer to the data location.
    void* data;
    // Width of the recovery map or the luma plane of the image in pixels.
    // Width of the gain map or the luma plane of the image in pixels.
    int width;
    // Height of the recovery map or the luma plane of the image in pixels.
    // Height of the gain map or the luma plane of the image in pixels.
    int height;
    // Color gamut.
    jpegr_color_gamut colorGamut;
@@ -86,7 +86,7 @@ struct jpegr_uncompressed_struct {
};

/*
 * Holds information for compressed image or recovery map.
 * Holds information for compressed image or gain map.
 */
struct jpegr_compressed_struct {
    // Pointer to the data location.
@@ -110,7 +110,7 @@ struct jpegr_exif_struct {
};

/*
 * Holds information for recovery map related metadata.
 * Holds information for gain map related metadata.
 */
struct jpegr_metadata_struct {
  // JPEG/R version
@@ -135,8 +135,8 @@ public:
     * Encode API-0
     * Compress JPEGR image from 10-bit HDR YUV.
     *
     * Tonemap the HDR input to a SDR image, generate recovery map from the HDR and SDR images,
     * compress SDR YUV to 8-bit JPEG and append the recovery map to the end of the compressed
     * Tonemap the HDR input to a SDR image, generate gain map from the HDR and SDR images,
     * compress SDR YUV to 8-bit JPEG and append the gain map to the end of the compressed
     * JPEG.
     * @param uncompressed_p010_image uncompressed HDR image in P010 color format
     * @param hdr_tf transfer function of the HDR image
@@ -156,8 +156,8 @@ public:
     * Encode API-1
     * Compress JPEGR image from 10-bit HDR YUV and 8-bit SDR YUV.
     *
     * Generate recovery map from the HDR and SDR inputs, compress SDR YUV to 8-bit JPEG and append
     * the recovery map to the end of the compressed JPEG. HDR and SDR inputs must be the same
     * Generate gain map from the HDR and SDR inputs, compress SDR YUV to 8-bit JPEG and append
     * the gain map to the end of the compressed JPEG. HDR and SDR inputs must be the same
     * resolution.
     * @param uncompressed_p010_image uncompressed HDR image in P010 color format
     * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
@@ -181,7 +181,7 @@ public:
     *
     * This method requires HAL Hardware JPEG encoder.
     *
     * Generate recovery map from the HDR and SDR inputs, append the recovery map to the end of the
     * Generate gain map from the HDR and SDR inputs, append the gain map to the end of the
     * compressed JPEG. HDR and SDR inputs must be the same resolution and color space.
     * @param uncompressed_p010_image uncompressed HDR image in P010 color format
     * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
@@ -204,8 +204,8 @@ public:
     *
     * This method requires HAL Hardware JPEG encoder.
     *
     * Decode the compressed 8-bit JPEG image to YUV SDR, generate recovery map from the HDR input
     * and the decoded SDR result, append the recovery map to the end of the compressed JPEG. HDR
     * Decode the compressed 8-bit JPEG image to YUV SDR, generate gain map from the HDR input
     * and the decoded SDR result, append the gain map to the end of the compressed JPEG. HDR
     * and SDR inputs must be the same resolution.
     * @param uncompressed_p010_image uncompressed HDR image in P010 color format
     * @param compressed_jpeg_image compressed 8-bit JPEG image
@@ -242,9 +242,9 @@ public:
                            ----------------------------------------------------------------------
                            |   JPEGR_OUTPUT_HDR_HLG   |            RGBA_1010102 HLG             |
                            ----------------------------------------------------------------------
     * @param recovery_map destination of the decoded recovery map. The default value is NULL where
     * @param gain_map destination of the decoded gain map. The default value is NULL where
                           the decoder will do nothing about it. If configured not NULL the decoder
                           will write the decoded recovery_map data into this structure. The format
                           will write the decoded gain_map data into this structure. The format
                           is defined in {@code jpegr_uncompressed_struct}.
     * @param metadata destination of the decoded metadata. The default value is NULL where the
                       decoder will do nothing about it. If configured not NULL the decoder will
@@ -257,7 +257,7 @@ public:
                         float max_display_boost = FLT_MAX,
                         jr_exif_ptr exif = nullptr,
                         jpegr_output_format output_format = JPEGR_OUTPUT_HDR_LINEAR,
                         jr_uncompressed_ptr recovery_map = nullptr,
                         jr_uncompressed_ptr gain_map = nullptr,
                         jr_metadata_ptr metadata = nullptr);

    /*
@@ -274,17 +274,17 @@ public:
protected:
    /*
     * This method is called in the encoding pipeline. It will take the uncompressed 8-bit and
     * 10-bit yuv images as input, and calculate the uncompressed recovery map. The input images
     * 10-bit yuv images as input, and calculate the uncompressed gain map. The input images
     * must be the same resolution.
     *
     * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
     * @param uncompressed_p010_image uncompressed HDR image in P010 color format
     * @param hdr_tf transfer function of the HDR image
     * @param dest recovery map; caller responsible for memory of data
     * @param dest gain map; caller responsible for memory of data
     * @param metadata max_content_boost is filled in
     * @return NO_ERROR if calculation succeeds, error code if error occurs.
     */
    status_t generateRecoveryMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
    status_t generateGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
                             jr_uncompressed_ptr uncompressed_p010_image,
                             jpegr_transfer_function hdr_tf,
                             jr_metadata_ptr metadata,
@@ -292,12 +292,12 @@ protected:

    /*
     * This method is called in the decoding pipeline. It will take the uncompressed (decoded)
     * 8-bit yuv image, the uncompressed (decoded) recovery map, and extracted JPEG/R metadata as
     * 8-bit yuv image, the uncompressed (decoded) gain map, and extracted JPEG/R metadata as
     * input, and calculate the 10-bit recovered image. The recovered output image is the same
     * color gamut as the SDR image, with HLG transfer function, and is in RGBA1010102 data format.
     *
     * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
     * @param uncompressed_recovery_map uncompressed recovery map
     * @param uncompressed_gain_map uncompressed gain map
     * @param metadata JPEG/R metadata extracted from XMP.
     * @param output_format flag for setting output color format. if set to
     *                      {@code JPEGR_OUTPUT_SDR}, decoder will only decode the primary image
@@ -306,8 +306,8 @@ protected:
     * @param dest reconstructed HDR image
     * @return NO_ERROR if calculation succeeds, error code if error occurs.
     */
    status_t applyRecoveryMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
                              jr_uncompressed_ptr uncompressed_recovery_map,
    status_t applyGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
                          jr_uncompressed_ptr uncompressed_gain_map,
                          jr_metadata_ptr metadata,
                          jpegr_output_format output_format,
                          float max_display_boost,
@@ -315,55 +315,55 @@ protected:

private:
    /*
     * This method is called in the encoding pipeline. It will encode the recovery map.
     * This method is called in the encoding pipeline. It will encode the gain map.
     *
     * @param uncompressed_recovery_map uncompressed recovery map
     * @param uncompressed_gain_map uncompressed gain map
     * @param dest encoded recover map
     * @return NO_ERROR if encoding succeeds, error code if error occurs.
     */
    status_t compressRecoveryMap(jr_uncompressed_ptr uncompressed_recovery_map,
    status_t compressGainMap(jr_uncompressed_ptr uncompressed_gain_map,
                             jr_compressed_ptr dest);

    /*
     * This methoud is called to separate primary image and recovery map image from JPEGR
     * This methoud is called to separate primary image and gain map image from JPEGR
     *
     * @param compressed_jpegr_image compressed JPEGR image
     * @param primary_image destination of primary image
     * @param recovery_map destination of compressed recovery map
     * @param gain_map destination of compressed gain map
     * @return NO_ERROR if calculation succeeds, error code if error occurs.
    */
    status_t extractPrimaryImageAndRecoveryMap(jr_compressed_ptr compressed_jpegr_image,
    status_t extractPrimaryImageAndGainMap(jr_compressed_ptr compressed_jpegr_image,
                                           jr_compressed_ptr primary_image,
                                               jr_compressed_ptr recovery_map);
                                           jr_compressed_ptr gain_map);
    /*
     * This method is called in the decoding pipeline. It will read XMP metadata to find the start
     * position of the compressed recovery map, and will extract the compressed recovery map.
     * position of the compressed gain map, and will extract the compressed gain map.
     *
     * @param compressed_jpegr_image compressed JPEGR image
     * @param dest destination of compressed recovery map
     * @param dest destination of compressed gain map
     * @return NO_ERROR if calculation succeeds, error code if error occurs.
     */
    status_t extractRecoveryMap(jr_compressed_ptr compressed_jpegr_image,
    status_t extractGainMap(jr_compressed_ptr compressed_jpegr_image,
                            jr_compressed_ptr dest);

    /*
     * This method is called in the encoding pipeline. It will take the standard 8-bit JPEG image,
     * the compressed recovery map and optionally the exif package as inputs, and generate the XMP
     * the compressed gain map and optionally the exif package as inputs, and generate the XMP
     * metadata, and finally append everything in the order of:
     *     SOI, APP2(EXIF) (if EXIF is from outside), APP2(XMP), primary image, recovery map
     *     SOI, APP2(EXIF) (if EXIF is from outside), APP2(XMP), primary image, gain map
     * Note that EXIF package is only available for encoding API-0 and API-1. For encoding API-2 and
     * API-3 this parameter is null, but the primary image in JPEG/R may still have EXIF as long as
     * the input JPEG has EXIF.
     *
     * @param compressed_jpeg_image compressed 8-bit JPEG image
     * @param compress_recovery_map compressed recover map
     * @param compress_gain_map compressed recover map
     * @param (nullable) exif EXIF package
     * @param metadata JPEG/R metadata to encode in XMP of the jpeg
     * @param dest compressed JPEGR image
     * @return NO_ERROR if calculation succeeds, error code if error occurs.
     */
    status_t appendRecoveryMap(jr_compressed_ptr compressed_jpeg_image,
                               jr_compressed_ptr compressed_recovery_map,
    status_t appendGainMap(jr_compressed_ptr compressed_jpeg_image,
                           jr_compressed_ptr compressed_gain_map,
                           jr_exif_ptr exif,
                           jr_metadata_ptr metadata,
                           jr_compressed_ptr dest);
Loading