| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 17 | #ifndef ANDROID_ULTRAHDR_JPEGR_H |
| 18 | #define ANDROID_ULTRAHDR_JPEGR_H |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 19 | |
| Ram Mohan | f954040 | 2023-05-18 20:08:55 +0530 | [diff] [blame] | 20 | #include "jpegencoderhelper.h" |
| Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 21 | #include "jpegrerrorcode.h" |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 22 | #include "ultrahdr.h" |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 23 | |
| Dichen Zhang | b96ba8f | 2023-03-21 23:33:41 +0000 | [diff] [blame] | 24 | #ifndef FLT_MAX |
| 25 | #define FLT_MAX 0x1.fffffep127f |
| 26 | #endif |
| 27 | |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 28 | namespace android::ultrahdr { |
| Dichen Zhang | 3e5798c | 2023-03-01 22:30:43 +0000 | [diff] [blame] | 29 | |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 30 | struct jpegr_info_struct { |
| 31 | size_t width; |
| 32 | size_t height; |
| 33 | std::vector<uint8_t>* iccData; |
| 34 | std::vector<uint8_t>* exifData; |
| 35 | }; |
| 36 | |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 37 | /* |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 38 | * Holds information for uncompressed image or gain map. |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 39 | */ |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 40 | struct jpegr_uncompressed_struct { |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 41 | // Pointer to the data location. |
| 42 | void* data; |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 43 | // Width of the gain map or the luma plane of the image in pixels. |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 44 | int width; |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 45 | // Height of the gain map or the luma plane of the image in pixels. |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 46 | int height; |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 47 | // Color gamut. |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 48 | ultrahdr_color_gamut colorGamut; |
| Dichen Zhang | 66ca6e3 | 2023-04-05 12:22:54 -0700 | [diff] [blame] | 49 | |
| 50 | // Values below are optional |
| 51 | // Pointer to chroma data, if it's NULL, chroma plane is considered to be immediately |
| 52 | // following after the luma plane. |
| 53 | // Note: currently this feature is only supported for P010 image (HDR input). |
| 54 | void* chroma_data = nullptr; |
| 55 | // Strides of Y plane in number of pixels, using 0 to present uninitialized, must be |
| 56 | // larger than or equal to luma width. |
| 57 | // Note: currently this feature is only supported for P010 image (HDR input). |
| 58 | int luma_stride = 0; |
| 59 | // Strides of UV plane in number of pixels, using 0 to present uninitialized, must be |
| 60 | // larger than or equal to chroma width. |
| 61 | // Note: currently this feature is only supported for P010 image (HDR input). |
| 62 | int chroma_stride = 0; |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /* |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 66 | * Holds information for compressed image or gain map. |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 67 | */ |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 68 | struct jpegr_compressed_struct { |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 69 | // Pointer to the data location. |
| 70 | void* data; |
| Dichen Zhang | 0b9f7de | 2022-11-18 06:52:46 +0000 | [diff] [blame] | 71 | // Used data length in bytes. |
| Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 72 | int length; |
| Dichen Zhang | 0b9f7de | 2022-11-18 06:52:46 +0000 | [diff] [blame] | 73 | // Maximum available data length in bytes. |
| 74 | int maxLength; |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 75 | // Color gamut. |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 76 | ultrahdr_color_gamut colorGamut; |
| Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /* |
| 80 | * Holds information for EXIF metadata. |
| 81 | */ |
| 82 | struct jpegr_exif_struct { |
| 83 | // Pointer to the data location. |
| 84 | void* data; |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 85 | // Data length; |
| 86 | int length; |
| 87 | }; |
| 88 | |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 89 | typedef struct jpegr_uncompressed_struct* jr_uncompressed_ptr; |
| 90 | typedef struct jpegr_compressed_struct* jr_compressed_ptr; |
| Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 91 | typedef struct jpegr_exif_struct* jr_exif_ptr; |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 92 | typedef struct jpegr_info_struct* jr_info_ptr; |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 93 | |
| Dichen Zhang | 761b52d | 2023-02-10 22:38:38 +0000 | [diff] [blame] | 94 | class JpegR { |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 95 | public: |
| 96 | /* |
| Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 97 | * Experimental only |
| 98 | * |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 99 | * Encode API-0 |
| 100 | * Compress JPEGR image from 10-bit HDR YUV. |
| 101 | * |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 102 | * Tonemap the HDR input to a SDR image, generate gain map from the HDR and SDR images, |
| 103 | * compress SDR YUV to 8-bit JPEG and append the gain map to the end of the compressed |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 104 | * JPEG. |
| 105 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| 106 | * @param hdr_tf transfer function of the HDR image |
| Dichen Zhang | 92e6c6b | 2023-04-14 20:20:14 +0000 | [diff] [blame] | 107 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 108 | * represents the maximum available size of the desitination buffer, and it must be |
| 109 | * set before calling this method. If the encoded JPEGR size exceeds |
| 110 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 111 | * @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is |
| 112 | * the highest quality |
| 113 | * @param exif pointer to the exif metadata. |
| 114 | * @return NO_ERROR if encoding succeeds, error code if error occurs. |
| 115 | */ |
| 116 | status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 117 | ultrahdr_transfer_function hdr_tf, |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 118 | jr_compressed_ptr dest, |
| 119 | int quality, |
| 120 | jr_exif_ptr exif); |
| 121 | |
| 122 | /* |
| 123 | * Encode API-1 |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 124 | * Compress JPEGR image from 10-bit HDR YUV and 8-bit SDR YUV. |
| 125 | * |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 126 | * Generate gain map from the HDR and SDR inputs, compress SDR YUV to 8-bit JPEG and append |
| 127 | * the gain map to the end of the compressed JPEG. HDR and SDR inputs must be the same |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 128 | * resolution. SDR input is assumed to use the sRGB transfer function. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 129 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| 130 | * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 131 | * @param hdr_tf transfer function of the HDR image |
| Dichen Zhang | 92e6c6b | 2023-04-14 20:20:14 +0000 | [diff] [blame] | 132 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 133 | * represents the maximum available size of the desitination buffer, and it must be |
| 134 | * set before calling this method. If the encoded JPEGR size exceeds |
| 135 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 136 | * @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is |
| 137 | * the highest quality |
| 138 | * @param exif pointer to the exif metadata. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 139 | * @return NO_ERROR if encoding succeeds, error code if error occurs. |
| 140 | */ |
| 141 | status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image, |
| 142 | jr_uncompressed_ptr uncompressed_yuv_420_image, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 143 | ultrahdr_transfer_function hdr_tf, |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 144 | jr_compressed_ptr dest, |
| Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 145 | int quality, |
| Dichen Zhang | 95cbb9f | 2022-11-07 18:32:05 +0000 | [diff] [blame] | 146 | jr_exif_ptr exif); |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 147 | |
| 148 | /* |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 149 | * Encode API-2 |
| Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 150 | * Compress JPEGR image from 10-bit HDR YUV, 8-bit SDR YUV and compressed 8-bit JPEG. |
| 151 | * |
| 152 | * This method requires HAL Hardware JPEG encoder. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 153 | * |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 154 | * Generate gain map from the HDR and SDR inputs, append the gain map to the end of the |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 155 | * compressed JPEG. Adds an ICC profile if one isn't present in the input JPEG image. HDR and |
| 156 | * SDR inputs must be the same resolution and color space. SDR image is assumed to use the sRGB |
| 157 | * transfer function. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 158 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| 159 | * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 160 | * Note: the SDR image must be the decoded version of the JPEG |
| 161 | * input |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 162 | * @param compressed_jpeg_image compressed 8-bit JPEG image |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 163 | * @param hdr_tf transfer function of the HDR image |
| Dichen Zhang | 92e6c6b | 2023-04-14 20:20:14 +0000 | [diff] [blame] | 164 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 165 | * represents the maximum available size of the desitination buffer, and it must be |
| 166 | * set before calling this method. If the encoded JPEGR size exceeds |
| 167 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 168 | * @return NO_ERROR if encoding succeeds, error code if error occurs. |
| 169 | */ |
| 170 | status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image, |
| 171 | jr_uncompressed_ptr uncompressed_yuv_420_image, |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 172 | jr_compressed_ptr compressed_jpeg_image, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 173 | ultrahdr_transfer_function hdr_tf, |
| Dichen Zhang | 95cbb9f | 2022-11-07 18:32:05 +0000 | [diff] [blame] | 174 | jr_compressed_ptr dest); |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 175 | |
| 176 | /* |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 177 | * Encode API-3 |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 178 | * Compress JPEGR image from 10-bit HDR YUV and 8-bit SDR YUV. |
| 179 | * |
| Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 180 | * This method requires HAL Hardware JPEG encoder. |
| 181 | * |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 182 | * Decode the compressed 8-bit JPEG image to YUV SDR, generate gain map from the HDR input |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 183 | * and the decoded SDR result, append the gain map to the end of the compressed JPEG. Adds an |
| 184 | * ICC profile if one isn't present in the input JPEG image. HDR and SDR inputs must be the same |
| 185 | * resolution. JPEG image is assumed to use the sRGB transfer function. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 186 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| 187 | * @param compressed_jpeg_image compressed 8-bit JPEG image |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 188 | * @param hdr_tf transfer function of the HDR image |
| Dichen Zhang | 92e6c6b | 2023-04-14 20:20:14 +0000 | [diff] [blame] | 189 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 190 | * represents the maximum available size of the desitination buffer, and it must be |
| 191 | * set before calling this method. If the encoded JPEGR size exceeds |
| 192 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 193 | * @return NO_ERROR if encoding succeeds, error code if error occurs. |
| 194 | */ |
| 195 | status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image, |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 196 | jr_compressed_ptr compressed_jpeg_image, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 197 | ultrahdr_transfer_function hdr_tf, |
| Dichen Zhang | 95cbb9f | 2022-11-07 18:32:05 +0000 | [diff] [blame] | 198 | jr_compressed_ptr dest); |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 199 | |
| 200 | /* |
| Dichen Zhang | 92e6c6b | 2023-04-14 20:20:14 +0000 | [diff] [blame] | 201 | * Encode API-4 |
| 202 | * Assemble JPEGR image from SDR JPEG and gainmap JPEG. |
| 203 | * |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 204 | * Assemble the primary JPEG image, the gain map and the metadata to JPEG/R format. Adds an ICC |
| 205 | * profile if one isn't present in the input JPEG image. |
| Dichen Zhang | 92e6c6b | 2023-04-14 20:20:14 +0000 | [diff] [blame] | 206 | * @param compressed_jpeg_image compressed 8-bit JPEG image |
| 207 | * @param compressed_gainmap compressed 8-bit JPEG single channel image |
| 208 | * @param metadata metadata to be written in XMP of the primary jpeg |
| 209 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 210 | * represents the maximum available size of the desitination buffer, and it must be |
| 211 | * set before calling this method. If the encoded JPEGR size exceeds |
| 212 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| 213 | * @return NO_ERROR if encoding succeeds, error code if error occurs. |
| 214 | */ |
| 215 | status_t encodeJPEGR(jr_compressed_ptr compressed_jpeg_image, |
| 216 | jr_compressed_ptr compressed_gainmap, |
| 217 | ultrahdr_metadata_ptr metadata, |
| 218 | jr_compressed_ptr dest); |
| 219 | |
| 220 | /* |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 221 | * Decode API |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 222 | * Decompress JPEGR image. |
| 223 | * |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 224 | * This method assumes that the JPEGR image contains an ICC profile with primaries that match |
| Nick Deakin | 094946b | 2023-06-09 11:58:41 -0400 | [diff] [blame] | 225 | * those of a color gamut that this library is aware of; Bt.709, Display-P3, or Bt.2100. It also |
| 226 | * assumes the base image uses the sRGB transfer function. |
| 227 | * |
| 228 | * This method only supports single gain map metadata values for fields that allow multi-channel |
| 229 | * metadata values. |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 230 | * |
| Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 231 | * @param compressed_jpegr_image compressed JPEGR image. |
| 232 | * @param dest destination of the uncompressed JPEGR image. |
| Dichen Zhang | b96ba8f | 2023-03-21 23:33:41 +0000 | [diff] [blame] | 233 | * @param max_display_boost (optional) the maximum available boost supported by a display, |
| 234 | * the value must be greater than or equal to 1.0. |
| Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 235 | * @param exif destination of the decoded EXIF metadata. The default value is NULL where the |
| 236 | decoder will do nothing about it. If configured not NULL the decoder will write |
| 237 | EXIF data into this structure. The format is defined in {@code jpegr_exif_struct} |
| 238 | * @param output_format flag for setting output color format. Its value configures the output |
| 239 | color format. The default value is {@code JPEGR_OUTPUT_HDR_LINEAR}. |
| 240 | ---------------------------------------------------------------------- |
| Dichen Zhang | c660570 | 2023-03-15 18:40:55 -0700 | [diff] [blame] | 241 | | output_format | decoded color format to be written | |
| Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 242 | ---------------------------------------------------------------------- |
| 243 | | JPEGR_OUTPUT_SDR | RGBA_8888 | |
| 244 | ---------------------------------------------------------------------- |
| 245 | | JPEGR_OUTPUT_HDR_LINEAR | (default)RGBA_F16 linear | |
| 246 | ---------------------------------------------------------------------- |
| 247 | | JPEGR_OUTPUT_HDR_PQ | RGBA_1010102 PQ | |
| 248 | ---------------------------------------------------------------------- |
| 249 | | JPEGR_OUTPUT_HDR_HLG | RGBA_1010102 HLG | |
| 250 | ---------------------------------------------------------------------- |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 251 | * @param gain_map destination of the decoded gain map. The default value is NULL where |
| Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 252 | the decoder will do nothing about it. If configured not NULL the decoder |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 253 | will write the decoded gain_map data into this structure. The format |
| Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 254 | is defined in {@code jpegr_uncompressed_struct}. |
| 255 | * @param metadata destination of the decoded metadata. The default value is NULL where the |
| 256 | decoder will do nothing about it. If configured not NULL the decoder will |
| 257 | write metadata into this structure. the format of metadata is defined in |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 258 | {@code ultrahdr_metadata_struct}. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 259 | * @return NO_ERROR if decoding succeeds, error code if error occurs. |
| 260 | */ |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 261 | status_t decodeJPEGR(jr_compressed_ptr compressed_jpegr_image, |
| Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 262 | jr_uncompressed_ptr dest, |
| Dichen Zhang | b96ba8f | 2023-03-21 23:33:41 +0000 | [diff] [blame] | 263 | float max_display_boost = FLT_MAX, |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 264 | jr_exif_ptr exif = nullptr, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 265 | ultrahdr_output_format output_format = ULTRAHDR_OUTPUT_HDR_LINEAR, |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 266 | jr_uncompressed_ptr gain_map = nullptr, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 267 | ultrahdr_metadata_ptr metadata = nullptr); |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 268 | |
| 269 | /* |
| 270 | * Gets Info from JPEGR file without decoding it. |
| 271 | * |
| Nick Deakin | 094946b | 2023-06-09 11:58:41 -0400 | [diff] [blame] | 272 | * This method only supports single gain map metadata values for fields that allow multi-channel |
| 273 | * metadata values. |
| 274 | * |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 275 | * The output is filled jpegr_info structure |
| 276 | * @param compressed_jpegr_image compressed JPEGR image |
| Fyodor Kyslov | a90ee00 | 2023-01-10 23:41:41 +0000 | [diff] [blame] | 277 | * @param jpegr_info pointer to output JPEGR info. Members of jpegr_info |
| 278 | * are owned by the caller |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 279 | * @return NO_ERROR if JPEGR parsing succeeds, error code otherwise |
| 280 | */ |
| 281 | status_t getJPEGRInfo(jr_compressed_ptr compressed_jpegr_image, |
| 282 | jr_info_ptr jpegr_info); |
| Nick Deakin | d19e576 | 2023-02-10 15:39:08 -0500 | [diff] [blame] | 283 | protected: |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 284 | /* |
| 285 | * This method is called in the encoding pipeline. It will take the uncompressed 8-bit and |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 286 | * 10-bit yuv images as input, and calculate the uncompressed gain map. The input images |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 287 | * must be the same resolution. The SDR input is assumed to use the sRGB transfer function. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 288 | * |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 289 | * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format |
| 290 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 291 | * @param hdr_tf transfer function of the HDR image |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 292 | * @param dest gain map; caller responsible for memory of data |
| Dichen Zhang | 761b52d | 2023-02-10 22:38:38 +0000 | [diff] [blame] | 293 | * @param metadata max_content_boost is filled in |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 294 | * @param sdr_is_601 if true, then use BT.601 decoding of YUV regardless of SDR image gamut |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 295 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 296 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 297 | status_t generateGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image, |
| 298 | jr_uncompressed_ptr uncompressed_p010_image, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 299 | ultrahdr_transfer_function hdr_tf, |
| 300 | ultrahdr_metadata_ptr metadata, |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 301 | jr_uncompressed_ptr dest, |
| 302 | bool sdr_is_601 = false); |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 303 | |
| 304 | /* |
| 305 | * This method is called in the decoding pipeline. It will take the uncompressed (decoded) |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 306 | * 8-bit yuv image, the uncompressed (decoded) gain map, and extracted JPEG/R metadata as |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 307 | * input, and calculate the 10-bit recovered image. The recovered output image is the same |
| Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 308 | * color gamut as the SDR image, with HLG transfer function, and is in RGBA1010102 data format. |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 309 | * The SDR image is assumed to use the sRGB transfer function. The SDR image is also assumed to |
| 310 | * be a decoded JPEG for the purpose of YUV interpration. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 311 | * |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 312 | * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 313 | * @param uncompressed_gain_map uncompressed gain map |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 314 | * @param metadata JPEG/R metadata extracted from XMP. |
| Dichen Zhang | 3e5798c | 2023-03-01 22:30:43 +0000 | [diff] [blame] | 315 | * @param output_format flag for setting output color format. if set to |
| 316 | * {@code JPEGR_OUTPUT_SDR}, decoder will only decode the primary image |
| 317 | * which is SDR. Default value is JPEGR_OUTPUT_HDR_LINEAR. |
| Dichen Zhang | c660570 | 2023-03-15 18:40:55 -0700 | [diff] [blame] | 318 | * @param max_display_boost the maximum available boost supported by a display |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 319 | * @param dest reconstructed HDR image |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 320 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 321 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 322 | status_t applyGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image, |
| 323 | jr_uncompressed_ptr uncompressed_gain_map, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 324 | ultrahdr_metadata_ptr metadata, |
| 325 | ultrahdr_output_format output_format, |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 326 | float max_display_boost, |
| 327 | jr_uncompressed_ptr dest); |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 328 | |
| Nick Deakin | d19e576 | 2023-02-10 15:39:08 -0500 | [diff] [blame] | 329 | private: |
| 330 | /* |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 331 | * This method is called in the encoding pipeline. It will encode the gain map. |
| Nick Deakin | d19e576 | 2023-02-10 15:39:08 -0500 | [diff] [blame] | 332 | * |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 333 | * @param uncompressed_gain_map uncompressed gain map |
| Ram Mohan | f954040 | 2023-05-18 20:08:55 +0530 | [diff] [blame] | 334 | * @param resource to compress gain map |
| Nick Deakin | d19e576 | 2023-02-10 15:39:08 -0500 | [diff] [blame] | 335 | * @return NO_ERROR if encoding succeeds, error code if error occurs. |
| 336 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 337 | status_t compressGainMap(jr_uncompressed_ptr uncompressed_gain_map, |
| Ram Mohan | f954040 | 2023-05-18 20:08:55 +0530 | [diff] [blame] | 338 | JpegEncoderHelper* jpeg_encoder); |
| Nick Deakin | d19e576 | 2023-02-10 15:39:08 -0500 | [diff] [blame] | 339 | |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 340 | /* |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 341 | * This methoud is called to separate primary image and gain map image from JPEGR |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 342 | * |
| 343 | * @param compressed_jpegr_image compressed JPEGR image |
| 344 | * @param primary_image destination of primary image |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 345 | * @param gain_map destination of compressed gain map |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 346 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| 347 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 348 | status_t extractPrimaryImageAndGainMap(jr_compressed_ptr compressed_jpegr_image, |
| 349 | jr_compressed_ptr primary_image, |
| 350 | jr_compressed_ptr gain_map); |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 351 | /* |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 352 | * This method is called in the decoding pipeline. It will read XMP metadata to find the start |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 353 | * position of the compressed gain map, and will extract the compressed gain map. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 354 | * |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 355 | * @param compressed_jpegr_image compressed JPEGR image |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 356 | * @param dest destination of compressed gain map |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 357 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 358 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 359 | status_t extractGainMap(jr_compressed_ptr compressed_jpegr_image, |
| 360 | jr_compressed_ptr dest); |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 361 | |
| 362 | /* |
| Dichen Zhang | 50ff129 | 2023-01-27 18:03:43 +0000 | [diff] [blame] | 363 | * This method is called in the encoding pipeline. It will take the standard 8-bit JPEG image, |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 364 | * the compressed gain map and optionally the exif package as inputs, and generate the XMP |
| Dichen Zhang | 50ff129 | 2023-01-27 18:03:43 +0000 | [diff] [blame] | 365 | * metadata, and finally append everything in the order of: |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 366 | * SOI, APP2(EXIF) (if EXIF is from outside), APP2(XMP), primary image, gain map |
| Dichen Zhang | 50ff129 | 2023-01-27 18:03:43 +0000 | [diff] [blame] | 367 | * Note that EXIF package is only available for encoding API-0 and API-1. For encoding API-2 and |
| 368 | * API-3 this parameter is null, but the primary image in JPEG/R may still have EXIF as long as |
| 369 | * the input JPEG has EXIF. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 370 | * |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 371 | * @param compressed_jpeg_image compressed 8-bit JPEG image |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 372 | * @param compress_gain_map compressed recover map |
| Dichen Zhang | 50ff129 | 2023-01-27 18:03:43 +0000 | [diff] [blame] | 373 | * @param (nullable) exif EXIF package |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 374 | * @param (nullable) icc ICC package |
| 375 | * @param icc_size length in bytes of ICC package |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 376 | * @param metadata JPEG/R metadata to encode in XMP of the jpeg |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 377 | * @param dest compressed JPEGR image |
| 378 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 379 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 380 | status_t appendGainMap(jr_compressed_ptr compressed_jpeg_image, |
| 381 | jr_compressed_ptr compressed_gain_map, |
| 382 | jr_exif_ptr exif, |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 383 | void* icc, size_t icc_size, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 384 | ultrahdr_metadata_ptr metadata, |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 385 | jr_compressed_ptr dest); |
| Dichen Zhang | 72fd2b1 | 2022-11-01 06:11:50 +0000 | [diff] [blame] | 386 | |
| 387 | /* |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 388 | * This method will tone map a HDR image to an SDR image. |
| 389 | * |
| Dichen Zhang | c3437ca | 2023-01-04 14:00:08 -0800 | [diff] [blame] | 390 | * @param src (input) uncompressed P010 image |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 391 | * @param dest (output) tone mapping result as a YUV_420 image |
| 392 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| 393 | */ |
| Dichen Zhang | c3437ca | 2023-01-04 14:00:08 -0800 | [diff] [blame] | 394 | status_t toneMap(jr_uncompressed_ptr src, |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 395 | jr_uncompressed_ptr dest); |
| Dichen Zhang | 66ca6e3 | 2023-04-05 12:22:54 -0700 | [diff] [blame] | 396 | |
| 397 | /* |
| Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 398 | * This method will convert a YUV420 image from one YUV encoding to another in-place (eg. |
| 399 | * Bt.709 to Bt.601 YUV encoding). |
| 400 | * |
| 401 | * src_encoding and dest_encoding indicate the encoding via the YUV conversion defined for that |
| 402 | * gamut. P3 indicates Rec.601, since this is how DataSpace encodes Display-P3 YUV data. |
| 403 | * |
| 404 | * @param image the YUV420 image to convert |
| 405 | * @param src_encoding input YUV encoding |
| 406 | * @param dest_encoding output YUV encoding |
| 407 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| 408 | */ |
| 409 | status_t convertYuv(jr_uncompressed_ptr image, |
| 410 | ultrahdr_color_gamut src_encoding, |
| 411 | ultrahdr_color_gamut dest_encoding); |
| 412 | |
| 413 | /* |
| Ram Mohan | ac1cfec | 2023-05-18 14:41:15 +0530 | [diff] [blame] | 414 | * This method will check the validity of the input arguments. |
| Dichen Zhang | 66ca6e3 | 2023-04-05 12:22:54 -0700 | [diff] [blame] | 415 | * |
| 416 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| 417 | * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format |
| Ram Mohan | ac1cfec | 2023-05-18 14:41:15 +0530 | [diff] [blame] | 418 | * @param hdr_tf transfer function of the HDR image |
| 419 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 420 | * represents the maximum available size of the desitination buffer, and it must be |
| 421 | * set before calling this method. If the encoded JPEGR size exceeds |
| 422 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| 423 | * @return NO_ERROR if the input args are valid, error code is not valid. |
| Dichen Zhang | 66ca6e3 | 2023-04-05 12:22:54 -0700 | [diff] [blame] | 424 | */ |
| Ram Mohan | ac1cfec | 2023-05-18 14:41:15 +0530 | [diff] [blame] | 425 | status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image, |
| 426 | jr_uncompressed_ptr uncompressed_yuv_420_image, |
| 427 | ultrahdr_transfer_function hdr_tf, |
| 428 | jr_compressed_ptr dest); |
| 429 | |
| 430 | /* |
| 431 | * This method will check the validity of the input arguments. |
| 432 | * |
| 433 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| 434 | * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format |
| 435 | * @param hdr_tf transfer function of the HDR image |
| 436 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 437 | * represents the maximum available size of the desitination buffer, and it must be |
| 438 | * set before calling this method. If the encoded JPEGR size exceeds |
| 439 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| 440 | * @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is |
| 441 | * the highest quality |
| 442 | * @return NO_ERROR if the input args are valid, error code is not valid. |
| 443 | */ |
| 444 | status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image, |
| 445 | jr_uncompressed_ptr uncompressed_yuv_420_image, |
| 446 | ultrahdr_transfer_function hdr_tf, |
| 447 | jr_compressed_ptr dest, |
| 448 | int quality); |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 449 | }; |
| 450 | |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 451 | } // namespace android::ultrahdr |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 452 | |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 453 | #endif // ANDROID_ULTRAHDR_JPEGR_H |