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

Commit 39cfa1ee authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "ultrahdr: updates to jpegr impl" into main

parents 48b4e68f b2359cd3
Loading
Loading
Loading
Loading
+4 −14
Original line number Diff line number Diff line
@@ -24,21 +24,11 @@
// User include files
#include "ultrahdr/gainmapmath.h"
#include "ultrahdr/jpegencoderhelper.h"
#include "ultrahdr/jpegdecoderhelper.h"
#include "utils/Log.h"

using namespace android::ultrahdr;

// constants
const int kMinWidth = 8;
const int kMaxWidth = 7680;

const int kMinHeight = 8;
const int kMaxHeight = 4320;

const int kScaleFactor = 4;

const int kJpegBlock = 16;

// Color gamuts for image data, sync with ultrahdr.h
const int kCgMin = ULTRAHDR_COLORGAMUT_UNSPECIFIED + 1;
const int kCgMax = ULTRAHDR_COLORGAMUT_MAX;
@@ -164,8 +154,8 @@ void UltraHdrEncFuzzer::process() {
                fillP010Buffer(bufferUV.get(), width, height / 2, uvStride);
            }
        } else {
            int map_width = width / kScaleFactor;
            int map_height = height / kScaleFactor;
            int map_width = width / kMapDimensionScaleFactor;
            int map_height = height / kMapDimensionScaleFactor;
            map_width = static_cast<size_t>(floor((map_width + kJpegBlock - 1) / kJpegBlock)) *
                    kJpegBlock;
            map_height = ((map_height + 1) >> 1) << 1;
@@ -249,7 +239,7 @@ void UltraHdrEncFuzzer::process() {
                        jpegGainMap.data = gainMapEncoder.getCompressedImagePtr();
                        jpegGainMap.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
                        ultrahdr_metadata_struct metadata;
                        metadata.version = "1.0";
                        metadata.version = kJpegrVersion;
                        if (tf == ULTRAHDR_TF_HLG) {
                            metadata.maxContentBoost = kHlgMaxNits / kSdrWhiteNits;
                        } else if (tf == ULTRAHDR_TF_PQ) {
+13 −29
Original line number Diff line number Diff line
@@ -531,21 +531,21 @@ void transformYuv420(jr_uncompressed_ptr image, size_t x_chroma, size_t y_chroma

  Color new_uv = (yuv1 + yuv2 + yuv3 + yuv4) / 4.0f;

  size_t pixel_y1_idx =  x_chroma * 2      +  y_chroma * 2      * image->width;
  size_t pixel_y2_idx = (x_chroma * 2 + 1) +  y_chroma * 2      * image->width;
  size_t pixel_y3_idx =  x_chroma * 2      + (y_chroma * 2 + 1) * image->width;
  size_t pixel_y4_idx = (x_chroma * 2 + 1) + (y_chroma * 2 + 1) * image->width;
  size_t pixel_y1_idx =  x_chroma * 2      +  y_chroma * 2      * image->luma_stride;
  size_t pixel_y2_idx = (x_chroma * 2 + 1) +  y_chroma * 2      * image->luma_stride;
  size_t pixel_y3_idx =  x_chroma * 2      + (y_chroma * 2 + 1) * image->luma_stride;
  size_t pixel_y4_idx = (x_chroma * 2 + 1) + (y_chroma * 2 + 1) * image->luma_stride;

  uint8_t& y1_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y1_idx];
  uint8_t& y2_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y2_idx];
  uint8_t& y3_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y3_idx];
  uint8_t& y4_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y4_idx];

  size_t pixel_count = image->width * image->height;
  size_t pixel_uv_idx = x_chroma + y_chroma * (image->width / 2);
  size_t pixel_count = image->chroma_stride * image->height / 2;
  size_t pixel_uv_idx = x_chroma + y_chroma * (image->chroma_stride);

  uint8_t& u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
  uint8_t& v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
  uint8_t& u_uint = reinterpret_cast<uint8_t*>(image->chroma_data)[pixel_uv_idx];
  uint8_t& v_uint = reinterpret_cast<uint8_t*>(image->chroma_data)[pixel_count + pixel_uv_idx];

  y1_uint = static_cast<uint8_t>(CLIP3((yuv1.y * 255.0f + 0.5f), 0, 255));
  y2_uint = static_cast<uint8_t>(CLIP3((yuv2.y * 255.0f + 0.5f), 0, 255));
@@ -599,17 +599,9 @@ Color applyGainLUT(Color e, float gain, GainLUT& gainLUT) {

Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
  uint8_t* luma_data = reinterpret_cast<uint8_t*>(image->data);
  size_t luma_stride = image->luma_stride == 0 ? image->width : image->luma_stride;

  uint8_t* chroma_data;
  size_t chroma_stride;
  if (image->chroma_data == nullptr) {
     chroma_stride = luma_stride / 2;
     chroma_data = &reinterpret_cast<uint8_t*>(image->data)[luma_stride * image->height];
  } else {
     chroma_stride = image->chroma_stride;
     chroma_data = reinterpret_cast<uint8_t*>(image->chroma_data);
  }
  size_t luma_stride = image->luma_stride;
  uint8_t* chroma_data = reinterpret_cast<uint8_t*>(image->chroma_data);
  size_t chroma_stride = image->chroma_stride;

  size_t offset_cr = chroma_stride * (image->height / 2);
  size_t pixel_y_idx = x + y * luma_stride;
@@ -629,16 +621,8 @@ Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
  uint16_t* luma_data = reinterpret_cast<uint16_t*>(image->data);
  size_t luma_stride = image->luma_stride == 0 ? image->width : image->luma_stride;

  uint16_t* chroma_data;
  size_t chroma_stride;
  if (image->chroma_data == nullptr) {
     chroma_stride = luma_stride;
     chroma_data = &reinterpret_cast<uint16_t*>(image->data)[luma_stride * image->height];
  } else {
     chroma_stride = image->chroma_stride;
     chroma_data = reinterpret_cast<uint16_t*>(image->chroma_data);
  }
  uint16_t* chroma_data = reinterpret_cast<uint16_t*>(image->chroma_data);
  size_t chroma_stride = image->chroma_stride;

  size_t pixel_y_idx = y * luma_stride + x;
  size_t pixel_u_idx = (y >> 1) * chroma_stride + (x & ~0x1);
+123 −119

File changed.

Preview size limit exceeded, changes collapsed.

+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
#ifndef ANDROID_ULTRAHDR_ULTRAHDR_H
#define ANDROID_ULTRAHDR_ULTRAHDR_H

#include <string>

namespace android::ultrahdr {
// Color gamuts for image data
typedef enum {
+30 −46
Original line number Diff line number Diff line
@@ -14,28 +14,26 @@
 * limitations under the License.
 */

#include <ultrahdr/jpegencoderhelper.h>
#include <cstring>
#include <memory>
#include <vector>

#include <ultrahdr/jpegencoderhelper.h>
#include <utils/Log.h>

#include <errno.h>

namespace android::ultrahdr {

#define ALIGNM(x, m) ((((x) + ((m)-1)) / (m)) * (m))

// The destination manager that can access |mResultBuffer| in JpegEncoderHelper.
struct destination_mgr {
public:
    struct jpeg_destination_mgr mgr;
    JpegEncoderHelper* encoder;
};

JpegEncoderHelper::JpegEncoderHelper() {
}
JpegEncoderHelper::JpegEncoderHelper() {}

JpegEncoderHelper::~JpegEncoderHelper() {
}
JpegEncoderHelper::~JpegEncoderHelper() {}

bool JpegEncoderHelper::compressImage(const void* image, int width, int height, int quality,
                                      const void* iccBuffer, unsigned int iccSize,
@@ -44,8 +42,8 @@ bool JpegEncoderHelper::compressImage(const void* image, int width, int height,
    if (!encode(image, width, height, quality, iccBuffer, iccSize, isSingleChannel)) {
        return false;
    }
    ALOGI("Compressed JPEG: %d[%dx%d] -> %zu bytes",
        (width * height * 12) / 8, width, height, mResultBuffer.size());
    ALOGI("Compressed JPEG: %d[%dx%d] -> %zu bytes", (width * height * 12) / 8, width, height,
          mResultBuffer.size());
    return true;
}

@@ -115,8 +113,9 @@ bool JpegEncoderHelper::encode(const void* image, int width, int height, int jpe
}

void JpegEncoderHelper::setJpegDestination(jpeg_compress_struct* cinfo) {
    destination_mgr* dest = static_cast<struct destination_mgr *>((*cinfo->mem->alloc_small) (
            (j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(destination_mgr)));
    destination_mgr* dest = static_cast<struct destination_mgr*>(
            (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT,
                                       sizeof(destination_mgr)));
    dest->encoder = this;
    dest->mgr.init_destination = &initDestination;
    dest->mgr.empty_output_buffer = &emptyOutputBuffer;
@@ -128,38 +127,23 @@ void JpegEncoderHelper::setJpegCompressStruct(int width, int height, int quality
                                              jpeg_compress_struct* cinfo, bool isSingleChannel) {
    cinfo->image_width = width;
    cinfo->image_height = height;
    if (isSingleChannel) {
        cinfo->input_components = 1;
        cinfo->in_color_space = JCS_GRAYSCALE;
    } else {
        cinfo->input_components = 3;
        cinfo->in_color_space = JCS_YCbCr;
    }
    cinfo->input_components = isSingleChannel ? 1 : 3;
    cinfo->in_color_space = isSingleChannel ? JCS_GRAYSCALE : JCS_YCbCr;
    jpeg_set_defaults(cinfo);

    jpeg_set_quality(cinfo, quality, TRUE);
    jpeg_set_colorspace(cinfo, isSingleChannel ? JCS_GRAYSCALE : JCS_YCbCr);
    cinfo->raw_data_in = TRUE;
    cinfo->dct_method = JDCT_ISLOW;

    if (!isSingleChannel) {
        // Configure sampling factors. The sampling factor is JPEG subsampling 420 because the
        // source format is YUV420.
        cinfo->comp_info[0].h_samp_factor = 2;
        cinfo->comp_info[0].v_samp_factor = 2;
        cinfo->comp_info[1].h_samp_factor = 1;
        cinfo->comp_info[1].v_samp_factor = 1;
        cinfo->comp_info[2].h_samp_factor = 1;
        cinfo->comp_info[2].v_samp_factor = 1;
    cinfo->comp_info[0].h_samp_factor = cinfo->in_color_space == JCS_GRAYSCALE ? 1 : 2;
    cinfo->comp_info[0].v_samp_factor = cinfo->in_color_space == JCS_GRAYSCALE ? 1 : 2;
    for (int i = 1; i < cinfo->num_components; i++) {
        cinfo->comp_info[i].h_samp_factor = 1;
        cinfo->comp_info[i].v_samp_factor = 1;
    }
}

bool JpegEncoderHelper::compress(
        jpeg_compress_struct* cinfo, const uint8_t* image, bool isSingleChannel) {
    if (isSingleChannel) {
        return compressSingleChannel(cinfo, image);
    }
    return compressYuv(cinfo, image);
bool JpegEncoderHelper::compress(jpeg_compress_struct* cinfo, const uint8_t* image,
                                 bool isSingleChannel) {
    return isSingleChannel ? compressSingleChannel(cinfo, image) : compressYuv(cinfo, image);
}

bool JpegEncoderHelper::compressYuv(jpeg_compress_struct* cinfo, const uint8_t* yuv) {
@@ -291,4 +275,4 @@ bool JpegEncoderHelper::compressSingleChannel(jpeg_compress_struct* cinfo, const
    return true;
}

} // namespace ultrahdr
} // namespace android::ultrahdr
Loading