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

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

Merge "ultrahdr: Add more error checks for input arguments of encode API" into...

Merge "ultrahdr: Add more error checks for input arguments of encode API" into udc-dev am: 3a4f049f

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/native/+/23321582



Change-Id: Ibfc047d908886b326b3854093a3233f4eb6c3c36
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents d37fb8dd 3a4f049f
Loading
Loading
Loading
Loading
+31 −4
Original line number Diff line number Diff line
@@ -373,14 +373,41 @@ private:
                     jr_uncompressed_ptr dest);

    /*
     * This method will check the validity of the input images.
     * This method will check the validity of the input arguments.
     *
     * @param uncompressed_p010_image uncompressed HDR image in P010 color format
     * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
     * @return NO_ERROR if the input images are valid, error code is not valid.
     * @param hdr_tf transfer function of the HDR image
     * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
     *             represents the maximum available size of the desitination buffer, and it must be
     *             set before calling this method. If the encoded JPEGR size exceeds
     *             {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
     * @return NO_ERROR if the input args are valid, error code is not valid.
     */
     status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
                                     jr_uncompressed_ptr uncompressed_yuv_420_image,
                                     ultrahdr_transfer_function hdr_tf,
                                     jr_compressed_ptr dest);

    /*
     * This method will check the validity of the input arguments.
     *
     * @param uncompressed_p010_image uncompressed HDR image in P010 color format
     * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
     * @param hdr_tf transfer function of the HDR image
     * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
     *             represents the maximum available size of the desitination buffer, and it must be
     *             set before calling this method. If the encoded JPEGR size exceeds
     *             {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
     * @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is
     *                the highest quality
     * @return NO_ERROR if the input args are valid, error code is not valid.
     */
    status_t areInputImagesValid(jr_uncompressed_ptr uncompressed_p010_image,
                                 jr_uncompressed_ptr uncompressed_yuv_420_image);
     status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
                                     jr_uncompressed_ptr uncompressed_yuv_420_image,
                                     ultrahdr_transfer_function hdr_tf,
                                     jr_compressed_ptr dest,
                                     int quality);
};

} // namespace android::ultrahdr
+2 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ typedef enum {
  ULTRAHDR_COLORGAMUT_BT709,
  ULTRAHDR_COLORGAMUT_P3,
  ULTRAHDR_COLORGAMUT_BT2100,
  ULTRAHDR_COLORGAMUT_MAX = ULTRAHDR_COLORGAMUT_BT2100,
} ultrahdr_color_gamut;

// Transfer functions for image data
@@ -33,6 +34,7 @@ typedef enum {
  ULTRAHDR_TF_HLG = 1,
  ULTRAHDR_TF_PQ = 2,
  ULTRAHDR_TF_SRGB = 3,
  ULTRAHDR_TF_MAX = ULTRAHDR_TF_SRGB,
} ultrahdr_transfer_function;

// Target output formats for decoder
+125 −32
Original line number Diff line number Diff line
@@ -89,23 +89,69 @@ int GetCPUCoreCount() {
  return cpuCoreCount;
}

status_t JpegR::areInputImagesValid(jr_uncompressed_ptr uncompressed_p010_image,
                                    jr_uncompressed_ptr uncompressed_yuv_420_image) {
  if (uncompressed_p010_image == nullptr) {
status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
                                       jr_uncompressed_ptr uncompressed_yuv_420_image,
                                       ultrahdr_transfer_function hdr_tf,
                                       jr_compressed_ptr dest) {
  if (uncompressed_p010_image == nullptr || uncompressed_p010_image->data == nullptr) {
    ALOGE("received nullptr for uncompressed p010 image");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (uncompressed_p010_image->width % 2 != 0
          || uncompressed_p010_image->height % 2 != 0) {
    ALOGE("Image dimensions cannot be odd, image dimensions %dx%d",
          uncompressed_p010_image->width, uncompressed_p010_image->height);
    return ERROR_JPEGR_INVALID_INPUT_TYPE;
  }

  if (uncompressed_p010_image->width == 0
          || uncompressed_p010_image->height == 0) {
    ALOGE("Image dimensions cannot be zero, image dimensions %dx%d",
          uncompressed_p010_image->width, uncompressed_p010_image->height);
    return ERROR_JPEGR_INVALID_INPUT_TYPE;
  }

  if (uncompressed_p010_image->colorGamut <= ULTRAHDR_COLORGAMUT_UNSPECIFIED
          || uncompressed_p010_image->colorGamut > ULTRAHDR_COLORGAMUT_MAX) {
    ALOGE("Unrecognized p010 color gamut %d", uncompressed_p010_image->colorGamut);
    return ERROR_JPEGR_INVALID_INPUT_TYPE;
  }

  if (uncompressed_p010_image->luma_stride != 0
          && uncompressed_p010_image->luma_stride < uncompressed_p010_image->width) {
    ALOGE("Image stride can not be smaller than width, stride=%d, width=%d",
    ALOGE("Luma stride can not be smaller than width, stride=%d, width=%d",
                uncompressed_p010_image->luma_stride, uncompressed_p010_image->width);
    return ERROR_JPEGR_INVALID_INPUT_TYPE;
  }

  if (uncompressed_p010_image->chroma_data != nullptr
          && uncompressed_p010_image->chroma_stride < uncompressed_p010_image->width) {
    ALOGE("Chroma stride can not be smaller than width, stride=%d, width=%d",
          uncompressed_p010_image->chroma_stride,
          uncompressed_p010_image->width);
    return ERROR_JPEGR_INVALID_INPUT_TYPE;
  }

  if (dest == nullptr || dest->data == nullptr) {
    ALOGE("received nullptr for destination");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (hdr_tf <= ULTRAHDR_TF_UNSPECIFIED || hdr_tf > ULTRAHDR_TF_MAX) {
    ALOGE("Invalid hdr transfer function %d", hdr_tf);
    return ERROR_JPEGR_INVALID_INPUT_TYPE;
  }

  if (uncompressed_yuv_420_image == nullptr) {
    return NO_ERROR;
  }

  if (uncompressed_yuv_420_image->data == nullptr) {
    ALOGE("received nullptr for uncompressed 420 image");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (uncompressed_yuv_420_image->luma_stride != 0) {
    ALOGE("Stride is not supported for YUV420 image");
    return ERROR_JPEGR_UNSUPPORTED_FEATURE;
@@ -127,28 +173,50 @@ status_t JpegR::areInputImagesValid(jr_uncompressed_ptr uncompressed_p010_image,
    return ERROR_JPEGR_RESOLUTION_MISMATCH;
  }

  if (uncompressed_yuv_420_image->colorGamut <= ULTRAHDR_COLORGAMUT_UNSPECIFIED
          || uncompressed_yuv_420_image->colorGamut > ULTRAHDR_COLORGAMUT_MAX) {
    ALOGE("Unrecognized 420 color gamut %d", uncompressed_yuv_420_image->colorGamut);
    return ERROR_JPEGR_INVALID_INPUT_TYPE;
  }

  return NO_ERROR;
}

/* Encode API-0 */
status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
                                       jr_uncompressed_ptr uncompressed_yuv_420_image,
                                       ultrahdr_transfer_function hdr_tf,
                                       jr_compressed_ptr dest,
                            int quality,
                            jr_exif_ptr exif) {
  if (uncompressed_p010_image == nullptr || dest == nullptr) {
    return ERROR_JPEGR_INVALID_NULL_PTR;
                                       int quality) {
  if (status_t ret = areInputArgumentsValid(
          uncompressed_p010_image, uncompressed_yuv_420_image, hdr_tf, dest) != NO_ERROR) {
    return ret;
  }

  if (quality < 0 || quality > 100) {
    ALOGE("quality factor is out side range [0-100], quality factor : %d", quality);
    return ERROR_JPEGR_INVALID_INPUT_TYPE;
  }

  if (status_t ret = areInputImagesValid(
          uncompressed_p010_image, /* uncompressed_yuv_420_image */ nullptr) != NO_ERROR) {
  return NO_ERROR;
}

/* Encode API-0 */
status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
                            ultrahdr_transfer_function hdr_tf,
                            jr_compressed_ptr dest,
                            int quality,
                            jr_exif_ptr exif) {
  if (status_t ret = areInputArgumentsValid(
          uncompressed_p010_image, /* uncompressed_yuv_420_image */ nullptr,
          hdr_tf, dest, quality) != NO_ERROR) {
    return ret;
  }

  if (exif != nullptr && exif->data == nullptr) {
    ALOGE("received nullptr for exif metadata");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  ultrahdr_metadata_struct metadata;
  metadata.version = kJpegrVersion;

@@ -201,18 +269,19 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
                            jr_compressed_ptr dest,
                            int quality,
                            jr_exif_ptr exif) {
  if (uncompressed_p010_image == nullptr
   || uncompressed_yuv_420_image == nullptr
   || dest == nullptr) {
  if (uncompressed_yuv_420_image == nullptr) {
    ALOGE("received nullptr for uncompressed 420 image");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (quality < 0 || quality > 100) {
    return ERROR_JPEGR_INVALID_INPUT_TYPE;
  if (exif != nullptr && exif->data == nullptr) {
    ALOGE("received nullptr for exif metadata");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (status_t ret = areInputImagesValid(
          uncompressed_p010_image, uncompressed_yuv_420_image) != NO_ERROR) {
  if (status_t ret = areInputArgumentsValid(
          uncompressed_p010_image, uncompressed_yuv_420_image, hdr_tf,
          dest, quality) != NO_ERROR) {
    return ret;
  }

@@ -256,15 +325,18 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
                            jr_compressed_ptr compressed_jpeg_image,
                            ultrahdr_transfer_function hdr_tf,
                            jr_compressed_ptr dest) {
  if (uncompressed_p010_image == nullptr
   || uncompressed_yuv_420_image == nullptr
   || compressed_jpeg_image == nullptr
   || dest == nullptr) {
  if (uncompressed_yuv_420_image == nullptr) {
    ALOGE("received nullptr for uncompressed 420 image");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (status_t ret = areInputImagesValid(
          uncompressed_p010_image, uncompressed_yuv_420_image) != NO_ERROR) {
  if (compressed_jpeg_image == nullptr || compressed_jpeg_image->data == nullptr) {
    ALOGE("received nullptr for compressed jpeg image");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (status_t ret = areInputArgumentsValid(
          uncompressed_p010_image, uncompressed_yuv_420_image, hdr_tf, dest) != NO_ERROR) {
    return ret;
  }

@@ -293,14 +365,14 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
                            jr_compressed_ptr compressed_jpeg_image,
                            ultrahdr_transfer_function hdr_tf,
                            jr_compressed_ptr dest) {
  if (uncompressed_p010_image == nullptr
   || compressed_jpeg_image == nullptr
   || dest == nullptr) {
  if (compressed_jpeg_image == nullptr || compressed_jpeg_image->data == nullptr) {
    ALOGE("received nullptr for compressed jpeg image");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (status_t ret = areInputImagesValid(
          uncompressed_p010_image, /* uncompressed_yuv_420_image */ nullptr) != NO_ERROR) {
  if (status_t ret = areInputArgumentsValid(
          uncompressed_p010_image, /* uncompressed_yuv_420_image */ nullptr,
          hdr_tf, dest) != NO_ERROR) {
    return ret;
  }

@@ -344,13 +416,34 @@ status_t JpegR::encodeJPEGR(jr_compressed_ptr compressed_jpeg_image,
                            jr_compressed_ptr compressed_gainmap,
                            ultrahdr_metadata_ptr metadata,
                            jr_compressed_ptr dest) {
  if (compressed_jpeg_image == nullptr || compressed_jpeg_image->data == nullptr) {
    ALOGE("received nullptr for compressed jpeg image");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (compressed_gainmap == nullptr || compressed_gainmap->data == nullptr) {
    ALOGE("received nullptr for compressed gain map");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (dest == nullptr || dest->data == nullptr) {
    ALOGE("received nullptr for destination");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  JPEGR_CHECK(appendGainMap(compressed_jpeg_image, compressed_gainmap, /* exif */ nullptr,
          metadata, dest));
  return NO_ERROR;
}

status_t JpegR::getJPEGRInfo(jr_compressed_ptr compressed_jpegr_image, jr_info_ptr jpegr_info) {
  if (compressed_jpegr_image == nullptr || jpegr_info == nullptr) {
  if (compressed_jpegr_image == nullptr || compressed_jpegr_image->data == nullptr) {
    ALOGE("received nullptr for compressed jpegr image");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }

  if (jpegr_info == nullptr) {
    ALOGE("received nullptr for compressed jpegr info struct");
    return ERROR_JPEGR_INVALID_NULL_PTR;
  }