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

Commit f954040e authored by Ram Mohan's avatar Ram Mohan Committed by Dichen Zhang
Browse files

ultrahdr: Avoid possible failure during gainmap compression

Allocating memory for compressed gainmap is always tricky because
the size is dependent on the data and quality factor. Too less a size
can cause intermittent errors. Avoid alloc altogether and remove
memcpy

Bug: 283503763
Test: ./libultrahdr_test

Change-Id: Ib57bac8bb25a2d00582c65067608a591f43cdc3a
parent 2aae677f
Loading
Loading
Loading
Loading
+3 −2
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
#ifndef ANDROID_ULTRAHDR_JPEGR_H
#ifndef ANDROID_ULTRAHDR_JPEGR_H
#define ANDROID_ULTRAHDR_JPEGR_H
#define ANDROID_ULTRAHDR_JPEGR_H


#include "jpegencoderhelper.h"
#include "jpegrerrorcode.h"
#include "jpegrerrorcode.h"
#include "ultrahdr.h"
#include "ultrahdr.h"


@@ -312,11 +313,11 @@ private:
     * This method is called in the encoding pipeline. It will encode the gain map.
     * This method is called in the encoding pipeline. It will encode the gain map.
     *
     *
     * @param uncompressed_gain_map uncompressed gain map
     * @param uncompressed_gain_map uncompressed gain map
     * @param dest encoded recover map
     * @param resource to compress gain map
     * @return NO_ERROR if encoding succeeds, error code if error occurs.
     * @return NO_ERROR if encoding succeeds, error code if error occurs.
     */
     */
    status_t compressGainMap(jr_uncompressed_ptr uncompressed_gain_map,
    status_t compressGainMap(jr_uncompressed_ptr uncompressed_gain_map,
                             jr_compressed_ptr dest);
                             JpegEncoderHelper* jpeg_encoder);


    /*
    /*
     * This methoud is called to separate primary image and gain map image from JPEGR
     * This methoud is called to separate primary image and gain map image from JPEGR
+33 −34
Original line number Original line Diff line number Diff line
@@ -244,11 +244,13 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
  std::unique_ptr<uint8_t[]> map_data;
  std::unique_ptr<uint8_t[]> map_data;
  map_data.reset(reinterpret_cast<uint8_t*>(map.data));
  map_data.reset(reinterpret_cast<uint8_t*>(map.data));


  JpegEncoderHelper jpeg_encoder_gainmap;
  JPEGR_CHECK(compressGainMap(&map, &jpeg_encoder_gainmap));
  jpegr_compressed_struct compressed_map;
  jpegr_compressed_struct compressed_map;
  compressed_map.maxLength = map.width * map.height;
  compressed_map.maxLength = jpeg_encoder_gainmap.getCompressedImageSize();
  unique_ptr<uint8_t[]> compressed_map_data = make_unique<uint8_t[]>(compressed_map.maxLength);
  compressed_map.length = compressed_map.maxLength;
  compressed_map.data = compressed_map_data.get();
  compressed_map.data = jpeg_encoder_gainmap.getCompressedImagePtr();
  JPEGR_CHECK(compressGainMap(&map, &compressed_map));
  compressed_map.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;


  sp<DataStruct> icc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB,
  sp<DataStruct> icc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB,
                                                  uncompressed_yuv_420_image.colorGamut);
                                                  uncompressed_yuv_420_image.colorGamut);
@@ -301,11 +303,13 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
  std::unique_ptr<uint8_t[]> map_data;
  std::unique_ptr<uint8_t[]> map_data;
  map_data.reset(reinterpret_cast<uint8_t*>(map.data));
  map_data.reset(reinterpret_cast<uint8_t*>(map.data));


  JpegEncoderHelper jpeg_encoder_gainmap;
  JPEGR_CHECK(compressGainMap(&map, &jpeg_encoder_gainmap));
  jpegr_compressed_struct compressed_map;
  jpegr_compressed_struct compressed_map;
  compressed_map.maxLength = map.width * map.height;
  compressed_map.maxLength = jpeg_encoder_gainmap.getCompressedImageSize();
  unique_ptr<uint8_t[]> compressed_map_data = make_unique<uint8_t[]>(compressed_map.maxLength);
  compressed_map.length = compressed_map.maxLength;
  compressed_map.data = compressed_map_data.get();
  compressed_map.data = jpeg_encoder_gainmap.getCompressedImagePtr();
  JPEGR_CHECK(compressGainMap(&map, &compressed_map));
  compressed_map.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;


  sp<DataStruct> icc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB,
  sp<DataStruct> icc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB,
                                                  uncompressed_yuv_420_image->colorGamut);
                                                  uncompressed_yuv_420_image->colorGamut);
@@ -356,11 +360,13 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
  std::unique_ptr<uint8_t[]> map_data;
  std::unique_ptr<uint8_t[]> map_data;
  map_data.reset(reinterpret_cast<uint8_t*>(map.data));
  map_data.reset(reinterpret_cast<uint8_t*>(map.data));


  JpegEncoderHelper jpeg_encoder_gainmap;
  JPEGR_CHECK(compressGainMap(&map, &jpeg_encoder_gainmap));
  jpegr_compressed_struct compressed_map;
  jpegr_compressed_struct compressed_map;
  compressed_map.maxLength = map.width * map.height;
  compressed_map.maxLength = jpeg_encoder_gainmap.getCompressedImageSize();
  unique_ptr<uint8_t[]> compressed_map_data = make_unique<uint8_t[]>(compressed_map.maxLength);
  compressed_map.length = compressed_map.maxLength;
  compressed_map.data = compressed_map_data.get();
  compressed_map.data = jpeg_encoder_gainmap.getCompressedImagePtr();
  JPEGR_CHECK(compressGainMap(&map, &compressed_map));
  compressed_map.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;


  JPEGR_CHECK(appendGainMap(compressed_jpeg_image, &compressed_map, nullptr, &metadata, dest));
  JPEGR_CHECK(appendGainMap(compressed_jpeg_image, &compressed_map, nullptr, &metadata, dest));


@@ -407,11 +413,13 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
  std::unique_ptr<uint8_t[]> map_data;
  std::unique_ptr<uint8_t[]> map_data;
  map_data.reset(reinterpret_cast<uint8_t*>(map.data));
  map_data.reset(reinterpret_cast<uint8_t*>(map.data));


  JpegEncoderHelper jpeg_encoder_gainmap;
  JPEGR_CHECK(compressGainMap(&map, &jpeg_encoder_gainmap));
  jpegr_compressed_struct compressed_map;
  jpegr_compressed_struct compressed_map;
  compressed_map.maxLength = map.width * map.height;
  compressed_map.maxLength = jpeg_encoder_gainmap.getCompressedImageSize();
  unique_ptr<uint8_t[]> compressed_map_data = make_unique<uint8_t[]>(compressed_map.maxLength);
  compressed_map.length = compressed_map.maxLength;
  compressed_map.data = compressed_map_data.get();
  compressed_map.data = jpeg_encoder_gainmap.getCompressedImagePtr();
  JPEGR_CHECK(compressGainMap(&map, &compressed_map));
  compressed_map.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;


  JPEGR_CHECK(appendGainMap(compressed_jpeg_image, &compressed_map, nullptr, &metadata, dest));
  JPEGR_CHECK(appendGainMap(compressed_jpeg_image, &compressed_map, nullptr, &metadata, dest));


@@ -604,13 +612,12 @@ status_t JpegR::decodeJPEGR(jr_compressed_ptr compressed_jpegr_image,
}
}


status_t JpegR::compressGainMap(jr_uncompressed_ptr uncompressed_gain_map,
status_t JpegR::compressGainMap(jr_uncompressed_ptr uncompressed_gain_map,
                                jr_compressed_ptr dest) {
                                JpegEncoderHelper* jpeg_encoder) {
  if (uncompressed_gain_map == nullptr || dest == nullptr) {
  if (uncompressed_gain_map == nullptr || jpeg_encoder == nullptr) {
    return ERROR_JPEGR_INVALID_NULL_PTR;
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }
  }


  JpegEncoderHelper jpeg_encoder;
  if (!jpeg_encoder->compressImage(uncompressed_gain_map->data,
  if (!jpeg_encoder.compressImage(uncompressed_gain_map->data,
                                   uncompressed_gain_map->width,
                                   uncompressed_gain_map->width,
                                   uncompressed_gain_map->height,
                                   uncompressed_gain_map->height,
                                   kMapCompressQuality,
                                   kMapCompressQuality,
@@ -620,14 +627,6 @@ status_t JpegR::compressGainMap(jr_uncompressed_ptr uncompressed_gain_map,
    return ERROR_JPEGR_ENCODE_ERROR;
    return ERROR_JPEGR_ENCODE_ERROR;
  }
  }


  if (dest->maxLength < jpeg_encoder.getCompressedImageSize()) {
    return ERROR_JPEGR_BUFFER_TOO_SMALL;
  }

  memcpy(dest->data, jpeg_encoder.getCompressedImagePtr(), jpeg_encoder.getCompressedImageSize());
  dest->length = jpeg_encoder.getCompressedImageSize();
  dest->colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;

  return NO_ERROR;
  return NO_ERROR;
}
}