| 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 | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 128 | * resolution. |
| 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 | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 155 | * compressed JPEG. HDR and SDR inputs must be the same resolution and color space. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 156 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| 157 | * @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] | 158 | * Note: the SDR image must be the decoded version of the JPEG |
| 159 | * input |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 160 | * @param compressed_jpeg_image compressed 8-bit JPEG image |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 161 | * @param hdr_tf transfer function of the HDR image |
| Dichen Zhang | 92e6c6b | 2023-04-14 20:20:14 +0000 | [diff] [blame] | 162 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 163 | * represents the maximum available size of the desitination buffer, and it must be |
| 164 | * set before calling this method. If the encoded JPEGR size exceeds |
| 165 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 166 | * @return NO_ERROR if encoding succeeds, error code if error occurs. |
| 167 | */ |
| 168 | status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image, |
| 169 | jr_uncompressed_ptr uncompressed_yuv_420_image, |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 170 | jr_compressed_ptr compressed_jpeg_image, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 171 | ultrahdr_transfer_function hdr_tf, |
| Dichen Zhang | 95cbb9f | 2022-11-07 18:32:05 +0000 | [diff] [blame] | 172 | jr_compressed_ptr dest); |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 173 | |
| 174 | /* |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 175 | * Encode API-3 |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 176 | * Compress JPEGR image from 10-bit HDR YUV and 8-bit SDR YUV. |
| 177 | * |
| Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 178 | * This method requires HAL Hardware JPEG encoder. |
| 179 | * |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 180 | * Decode the compressed 8-bit JPEG image to YUV SDR, generate gain map from the HDR input |
| 181 | * and the decoded SDR result, append the gain map to the end of the compressed JPEG. HDR |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 182 | * and SDR inputs must be the same resolution. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 183 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| 184 | * @param compressed_jpeg_image compressed 8-bit JPEG image |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 185 | * @param hdr_tf transfer function of the HDR image |
| Dichen Zhang | 92e6c6b | 2023-04-14 20:20:14 +0000 | [diff] [blame] | 186 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 187 | * represents the maximum available size of the desitination buffer, and it must be |
| 188 | * set before calling this method. If the encoded JPEGR size exceeds |
| 189 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 190 | * @return NO_ERROR if encoding succeeds, error code if error occurs. |
| 191 | */ |
| 192 | status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image, |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 193 | jr_compressed_ptr compressed_jpeg_image, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 194 | ultrahdr_transfer_function hdr_tf, |
| Dichen Zhang | 95cbb9f | 2022-11-07 18:32:05 +0000 | [diff] [blame] | 195 | jr_compressed_ptr dest); |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 196 | |
| 197 | /* |
| Dichen Zhang | 92e6c6b | 2023-04-14 20:20:14 +0000 | [diff] [blame] | 198 | * Encode API-4 |
| 199 | * Assemble JPEGR image from SDR JPEG and gainmap JPEG. |
| 200 | * |
| 201 | * Assemble the primary JPEG image, the gain map and the metadata to JPEG/R format. |
| 202 | * @param compressed_jpeg_image compressed 8-bit JPEG image |
| 203 | * @param compressed_gainmap compressed 8-bit JPEG single channel image |
| 204 | * @param metadata metadata to be written in XMP of the primary jpeg |
| 205 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 206 | * represents the maximum available size of the desitination buffer, and it must be |
| 207 | * set before calling this method. If the encoded JPEGR size exceeds |
| 208 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| 209 | * @return NO_ERROR if encoding succeeds, error code if error occurs. |
| 210 | */ |
| 211 | status_t encodeJPEGR(jr_compressed_ptr compressed_jpeg_image, |
| 212 | jr_compressed_ptr compressed_gainmap, |
| 213 | ultrahdr_metadata_ptr metadata, |
| 214 | jr_compressed_ptr dest); |
| 215 | |
| 216 | /* |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 217 | * Decode API |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 218 | * Decompress JPEGR image. |
| 219 | * |
| Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 220 | * @param compressed_jpegr_image compressed JPEGR image. |
| 221 | * @param dest destination of the uncompressed JPEGR image. |
| Dichen Zhang | b96ba8f | 2023-03-21 23:33:41 +0000 | [diff] [blame] | 222 | * @param max_display_boost (optional) the maximum available boost supported by a display, |
| 223 | * the value must be greater than or equal to 1.0. |
| Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 224 | * @param exif destination of the decoded EXIF metadata. The default value is NULL where the |
| 225 | decoder will do nothing about it. If configured not NULL the decoder will write |
| 226 | EXIF data into this structure. The format is defined in {@code jpegr_exif_struct} |
| 227 | * @param output_format flag for setting output color format. Its value configures the output |
| 228 | color format. The default value is {@code JPEGR_OUTPUT_HDR_LINEAR}. |
| 229 | ---------------------------------------------------------------------- |
| Dichen Zhang | c660570 | 2023-03-15 18:40:55 -0700 | [diff] [blame] | 230 | | output_format | decoded color format to be written | |
| Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 231 | ---------------------------------------------------------------------- |
| 232 | | JPEGR_OUTPUT_SDR | RGBA_8888 | |
| 233 | ---------------------------------------------------------------------- |
| 234 | | JPEGR_OUTPUT_HDR_LINEAR | (default)RGBA_F16 linear | |
| 235 | ---------------------------------------------------------------------- |
| 236 | | JPEGR_OUTPUT_HDR_PQ | RGBA_1010102 PQ | |
| 237 | ---------------------------------------------------------------------- |
| 238 | | JPEGR_OUTPUT_HDR_HLG | RGBA_1010102 HLG | |
| 239 | ---------------------------------------------------------------------- |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 240 | * @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] | 241 | 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] | 242 | will write the decoded gain_map data into this structure. The format |
| Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 243 | is defined in {@code jpegr_uncompressed_struct}. |
| 244 | * @param metadata destination of the decoded metadata. The default value is NULL where the |
| 245 | decoder will do nothing about it. If configured not NULL the decoder will |
| 246 | write metadata into this structure. the format of metadata is defined in |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 247 | {@code ultrahdr_metadata_struct}. |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 248 | * @return NO_ERROR if decoding succeeds, error code if error occurs. |
| 249 | */ |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 250 | status_t decodeJPEGR(jr_compressed_ptr compressed_jpegr_image, |
| Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 251 | jr_uncompressed_ptr dest, |
| Dichen Zhang | b96ba8f | 2023-03-21 23:33:41 +0000 | [diff] [blame] | 252 | float max_display_boost = FLT_MAX, |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 253 | jr_exif_ptr exif = nullptr, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 254 | ultrahdr_output_format output_format = ULTRAHDR_OUTPUT_HDR_LINEAR, |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 255 | jr_uncompressed_ptr gain_map = nullptr, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 256 | ultrahdr_metadata_ptr metadata = nullptr); |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 257 | |
| 258 | /* |
| 259 | * Gets Info from JPEGR file without decoding it. |
| 260 | * |
| 261 | * The output is filled jpegr_info structure |
| 262 | * @param compressed_jpegr_image compressed JPEGR image |
| Fyodor Kyslov | a90ee00 | 2023-01-10 23:41:41 +0000 | [diff] [blame] | 263 | * @param jpegr_info pointer to output JPEGR info. Members of jpegr_info |
| 264 | * are owned by the caller |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 265 | * @return NO_ERROR if JPEGR parsing succeeds, error code otherwise |
| 266 | */ |
| 267 | status_t getJPEGRInfo(jr_compressed_ptr compressed_jpegr_image, |
| 268 | jr_info_ptr jpegr_info); |
| Nick Deakin | d19e576 | 2023-02-10 15:39:08 -0500 | [diff] [blame] | 269 | protected: |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 270 | /* |
| 271 | * 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] | 272 | * 10-bit yuv images as input, and calculate the uncompressed gain map. The input images |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 273 | * must be the same resolution. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 274 | * |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 275 | * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format |
| 276 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 277 | * @param hdr_tf transfer function of the HDR image |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 278 | * @param dest gain map; caller responsible for memory of data |
| Dichen Zhang | 761b52d | 2023-02-10 22:38:38 +0000 | [diff] [blame] | 279 | * @param metadata max_content_boost is filled in |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 280 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 281 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 282 | status_t generateGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image, |
| 283 | jr_uncompressed_ptr uncompressed_p010_image, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 284 | ultrahdr_transfer_function hdr_tf, |
| 285 | ultrahdr_metadata_ptr metadata, |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 286 | jr_uncompressed_ptr dest); |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * 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] | 290 | * 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] | 291 | * 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] | 292 | * color gamut as the SDR image, with HLG transfer function, and is in RGBA1010102 data format. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 293 | * |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 294 | * @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] | 295 | * @param uncompressed_gain_map uncompressed gain map |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 296 | * @param metadata JPEG/R metadata extracted from XMP. |
| Dichen Zhang | 3e5798c | 2023-03-01 22:30:43 +0000 | [diff] [blame] | 297 | * @param output_format flag for setting output color format. if set to |
| 298 | * {@code JPEGR_OUTPUT_SDR}, decoder will only decode the primary image |
| 299 | * which is SDR. Default value is JPEGR_OUTPUT_HDR_LINEAR. |
| Dichen Zhang | c660570 | 2023-03-15 18:40:55 -0700 | [diff] [blame] | 300 | * @param max_display_boost the maximum available boost supported by a display |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 301 | * @param dest reconstructed HDR image |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 302 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 303 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 304 | status_t applyGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image, |
| 305 | jr_uncompressed_ptr uncompressed_gain_map, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 306 | ultrahdr_metadata_ptr metadata, |
| 307 | ultrahdr_output_format output_format, |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 308 | float max_display_boost, |
| 309 | jr_uncompressed_ptr dest); |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 310 | |
| Nick Deakin | d19e576 | 2023-02-10 15:39:08 -0500 | [diff] [blame] | 311 | private: |
| 312 | /* |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 313 | * 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] | 314 | * |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 315 | * @param uncompressed_gain_map uncompressed gain map |
| Ram Mohan | f954040 | 2023-05-18 20:08:55 +0530 | [diff] [blame^] | 316 | * @param resource to compress gain map |
| Nick Deakin | d19e576 | 2023-02-10 15:39:08 -0500 | [diff] [blame] | 317 | * @return NO_ERROR if encoding succeeds, error code if error occurs. |
| 318 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 319 | status_t compressGainMap(jr_uncompressed_ptr uncompressed_gain_map, |
| Ram Mohan | f954040 | 2023-05-18 20:08:55 +0530 | [diff] [blame^] | 320 | JpegEncoderHelper* jpeg_encoder); |
| Nick Deakin | d19e576 | 2023-02-10 15:39:08 -0500 | [diff] [blame] | 321 | |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 322 | /* |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 323 | * 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] | 324 | * |
| 325 | * @param compressed_jpegr_image compressed JPEGR image |
| 326 | * @param primary_image destination of primary image |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 327 | * @param gain_map destination of compressed gain map |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 328 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| 329 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 330 | status_t extractPrimaryImageAndGainMap(jr_compressed_ptr compressed_jpegr_image, |
| 331 | jr_compressed_ptr primary_image, |
| 332 | jr_compressed_ptr gain_map); |
| Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 333 | /* |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 334 | * 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] | 335 | * 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] | 336 | * |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 337 | * @param compressed_jpegr_image compressed JPEGR image |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 338 | * @param dest destination of compressed gain map |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 339 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| 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 | status_t extractGainMap(jr_compressed_ptr compressed_jpegr_image, |
| 342 | jr_compressed_ptr dest); |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 343 | |
| 344 | /* |
| Dichen Zhang | 50ff129 | 2023-01-27 18:03:43 +0000 | [diff] [blame] | 345 | * 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] | 346 | * 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] | 347 | * metadata, and finally append everything in the order of: |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 348 | * 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] | 349 | * Note that EXIF package is only available for encoding API-0 and API-1. For encoding API-2 and |
| 350 | * API-3 this parameter is null, but the primary image in JPEG/R may still have EXIF as long as |
| 351 | * the input JPEG has EXIF. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 352 | * |
| Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 353 | * @param compressed_jpeg_image compressed 8-bit JPEG image |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 354 | * @param compress_gain_map compressed recover map |
| Dichen Zhang | 50ff129 | 2023-01-27 18:03:43 +0000 | [diff] [blame] | 355 | * @param (nullable) exif EXIF package |
| Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 356 | * @param metadata JPEG/R metadata to encode in XMP of the jpeg |
| Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 357 | * @param dest compressed JPEGR image |
| 358 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 359 | */ |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 360 | status_t appendGainMap(jr_compressed_ptr compressed_jpeg_image, |
| 361 | jr_compressed_ptr compressed_gain_map, |
| 362 | jr_exif_ptr exif, |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 363 | ultrahdr_metadata_ptr metadata, |
| Dichen Zhang | 10959a4 | 2023-04-10 16:28:16 -0700 | [diff] [blame] | 364 | jr_compressed_ptr dest); |
| Dichen Zhang | 72fd2b1 | 2022-11-01 06:11:50 +0000 | [diff] [blame] | 365 | |
| 366 | /* |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 367 | * This method will tone map a HDR image to an SDR image. |
| 368 | * |
| Dichen Zhang | c3437ca | 2023-01-04 14:00:08 -0800 | [diff] [blame] | 369 | * @param src (input) uncompressed P010 image |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 370 | * @param dest (output) tone mapping result as a YUV_420 image |
| 371 | * @return NO_ERROR if calculation succeeds, error code if error occurs. |
| 372 | */ |
| Dichen Zhang | c3437ca | 2023-01-04 14:00:08 -0800 | [diff] [blame] | 373 | status_t toneMap(jr_uncompressed_ptr src, |
| Dichen Zhang | 636f524 | 2022-12-07 20:25:44 +0000 | [diff] [blame] | 374 | jr_uncompressed_ptr dest); |
| Dichen Zhang | 66ca6e3 | 2023-04-05 12:22:54 -0700 | [diff] [blame] | 375 | |
| 376 | /* |
| Ram Mohan | ac1cfec | 2023-05-18 14:41:15 +0530 | [diff] [blame] | 377 | * This method will check the validity of the input arguments. |
| Dichen Zhang | 66ca6e3 | 2023-04-05 12:22:54 -0700 | [diff] [blame] | 378 | * |
| 379 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| 380 | * @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] | 381 | * @param hdr_tf transfer function of the HDR image |
| 382 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 383 | * represents the maximum available size of the desitination buffer, and it must be |
| 384 | * set before calling this method. If the encoded JPEGR size exceeds |
| 385 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| 386 | * @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] | 387 | */ |
| Ram Mohan | ac1cfec | 2023-05-18 14:41:15 +0530 | [diff] [blame] | 388 | status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image, |
| 389 | jr_uncompressed_ptr uncompressed_yuv_420_image, |
| 390 | ultrahdr_transfer_function hdr_tf, |
| 391 | jr_compressed_ptr dest); |
| 392 | |
| 393 | /* |
| 394 | * This method will check the validity of the input arguments. |
| 395 | * |
| 396 | * @param uncompressed_p010_image uncompressed HDR image in P010 color format |
| 397 | * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format |
| 398 | * @param hdr_tf transfer function of the HDR image |
| 399 | * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength} |
| 400 | * represents the maximum available size of the desitination buffer, and it must be |
| 401 | * set before calling this method. If the encoded JPEGR size exceeds |
| 402 | * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}. |
| 403 | * @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is |
| 404 | * the highest quality |
| 405 | * @return NO_ERROR if the input args are valid, error code is not valid. |
| 406 | */ |
| 407 | status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image, |
| 408 | jr_uncompressed_ptr uncompressed_yuv_420_image, |
| 409 | ultrahdr_transfer_function hdr_tf, |
| 410 | jr_compressed_ptr dest, |
| 411 | int quality); |
| Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 412 | }; |
| 413 | |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 414 | } // namespace android::ultrahdr |
| Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 415 | |
| Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 416 | #endif // ANDROID_ULTRAHDR_JPEGR_H |