blob: f80496a75815454ea29764b02164e292027def6f [file] [log] [blame]
Dichen Zhang85b37562022-10-11 11:08:28 -07001/*
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 Zhangdbceb0e2023-04-14 19:03:18 +000017#ifndef ANDROID_ULTRAHDR_JPEGR_H
18#define ANDROID_ULTRAHDR_JPEGR_H
Nick Deakinf6bca5a2022-11-04 10:43:43 -040019
Ram Mohanf9540402023-05-18 20:08:55 +053020#include "jpegencoderhelper.h"
Dichen Zhang80b72482022-11-02 01:55:35 +000021#include "jpegrerrorcode.h"
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000022#include "ultrahdr.h"
Dichen Zhang6947d532022-10-22 02:16:21 +000023
Dichen Zhangb96ba8f2023-03-21 23:33:41 +000024#ifndef FLT_MAX
25#define FLT_MAX 0x1.fffffep127f
26#endif
27
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000028namespace android::ultrahdr {
Dichen Zhang3e5798c2023-03-01 22:30:43 +000029
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000030struct 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 Zhang596a7562022-10-12 14:57:05 -070037/*
Dichen Zhang10959a42023-04-10 16:28:16 -070038 * Holds information for uncompressed image or gain map.
Dichen Zhang596a7562022-10-12 14:57:05 -070039 */
Dichen Zhang6947d532022-10-22 02:16:21 +000040struct jpegr_uncompressed_struct {
Dichen Zhang596a7562022-10-12 14:57:05 -070041 // Pointer to the data location.
42 void* data;
Dichen Zhang10959a42023-04-10 16:28:16 -070043 // Width of the gain map or the luma plane of the image in pixels.
Dichen Zhang596a7562022-10-12 14:57:05 -070044 int width;
Dichen Zhang10959a42023-04-10 16:28:16 -070045 // Height of the gain map or the luma plane of the image in pixels.
Dichen Zhang596a7562022-10-12 14:57:05 -070046 int height;
Nick Deakin6bd90432022-11-20 16:26:37 -050047 // Color gamut.
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000048 ultrahdr_color_gamut colorGamut;
Dichen Zhang66ca6e32023-04-05 12:22:54 -070049
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 Zhang596a7562022-10-12 14:57:05 -070063};
64
65/*
Dichen Zhang10959a42023-04-10 16:28:16 -070066 * Holds information for compressed image or gain map.
Dichen Zhang596a7562022-10-12 14:57:05 -070067 */
Dichen Zhang6947d532022-10-22 02:16:21 +000068struct jpegr_compressed_struct {
Dichen Zhang596a7562022-10-12 14:57:05 -070069 // Pointer to the data location.
70 void* data;
Dichen Zhang0b9f7de2022-11-18 06:52:46 +000071 // Used data length in bytes.
Dichen Zhangffa34012022-11-03 23:21:13 +000072 int length;
Dichen Zhang0b9f7de2022-11-18 06:52:46 +000073 // Maximum available data length in bytes.
74 int maxLength;
Nick Deakin6bd90432022-11-20 16:26:37 -050075 // Color gamut.
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000076 ultrahdr_color_gamut colorGamut;
Dichen Zhangffa34012022-11-03 23:21:13 +000077};
78
79/*
80 * Holds information for EXIF metadata.
81 */
82struct jpegr_exif_struct {
83 // Pointer to the data location.
84 void* data;
Dichen Zhang596a7562022-10-12 14:57:05 -070085 // Data length;
86 int length;
87};
88
Dichen Zhang6947d532022-10-22 02:16:21 +000089typedef struct jpegr_uncompressed_struct* jr_uncompressed_ptr;
90typedef struct jpegr_compressed_struct* jr_compressed_ptr;
Dichen Zhangffa34012022-11-03 23:21:13 +000091typedef struct jpegr_exif_struct* jr_exif_ptr;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000092typedef struct jpegr_info_struct* jr_info_ptr;
Dichen Zhang596a7562022-10-12 14:57:05 -070093
Dichen Zhang761b52d2023-02-10 22:38:38 +000094class JpegR {
Dichen Zhang85b37562022-10-11 11:08:28 -070095public:
96 /*
Dichen Zhange286f1c2023-03-14 00:22:00 +000097 * Experimental only
98 *
Dichen Zhang636f5242022-12-07 20:25:44 +000099 * Encode API-0
100 * Compress JPEGR image from 10-bit HDR YUV.
101 *
Dichen Zhang10959a42023-04-10 16:28:16 -0700102 * 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 Zhang636f5242022-12-07 20:25:44 +0000104 * JPEG.
105 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
106 * @param hdr_tf transfer function of the HDR image
Dichen Zhang92e6c6b2023-04-14 20:20:14 +0000107 * @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 Zhang636f5242022-12-07 20:25:44 +0000111 * @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 Zhangdbceb0e2023-04-14 19:03:18 +0000117 ultrahdr_transfer_function hdr_tf,
Dichen Zhang636f5242022-12-07 20:25:44 +0000118 jr_compressed_ptr dest,
119 int quality,
120 jr_exif_ptr exif);
121
122 /*
123 * Encode API-1
Dichen Zhang6947d532022-10-22 02:16:21 +0000124 * Compress JPEGR image from 10-bit HDR YUV and 8-bit SDR YUV.
125 *
Dichen Zhang10959a42023-04-10 16:28:16 -0700126 * 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 Deakin0db53ee2023-05-19 17:14:45 -0400128 * resolution. SDR input is assumed to use the sRGB transfer function.
Dichen Zhang6947d532022-10-22 02:16:21 +0000129 * @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 Deakin6bd90432022-11-20 16:26:37 -0500131 * @param hdr_tf transfer function of the HDR image
Dichen Zhang92e6c6b2023-04-14 20:20:14 +0000132 * @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 Zhangffa34012022-11-03 23:21:13 +0000136 * @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 Zhang6947d532022-10-22 02:16:21 +0000139 * @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 Zhangdbceb0e2023-04-14 19:03:18 +0000143 ultrahdr_transfer_function hdr_tf,
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400144 jr_compressed_ptr dest,
Dichen Zhangffa34012022-11-03 23:21:13 +0000145 int quality,
Dichen Zhang95cbb9f2022-11-07 18:32:05 +0000146 jr_exif_ptr exif);
Dichen Zhang6947d532022-10-22 02:16:21 +0000147
148 /*
Dichen Zhang636f5242022-12-07 20:25:44 +0000149 * Encode API-2
Dichen Zhangffa34012022-11-03 23:21:13 +0000150 * 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 Zhang6947d532022-10-22 02:16:21 +0000153 *
Dichen Zhang10959a42023-04-10 16:28:16 -0700154 * Generate gain map from the HDR and SDR inputs, append the gain map to the end of the
Nick Deakin0db53ee2023-05-19 17:14:45 -0400155 * 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 Zhang6947d532022-10-22 02:16:21 +0000158 * @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 Zhang636f5242022-12-07 20:25:44 +0000160 * Note: the SDR image must be the decoded version of the JPEG
161 * input
Dichen Zhang6947d532022-10-22 02:16:21 +0000162 * @param compressed_jpeg_image compressed 8-bit JPEG image
Nick Deakin6bd90432022-11-20 16:26:37 -0500163 * @param hdr_tf transfer function of the HDR image
Dichen Zhang92e6c6b2023-04-14 20:20:14 +0000164 * @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 Zhang6947d532022-10-22 02:16:21 +0000168 * @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 Deakinf6bca5a2022-11-04 10:43:43 -0400172 jr_compressed_ptr compressed_jpeg_image,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000173 ultrahdr_transfer_function hdr_tf,
Dichen Zhang95cbb9f2022-11-07 18:32:05 +0000174 jr_compressed_ptr dest);
Dichen Zhang6947d532022-10-22 02:16:21 +0000175
176 /*
Dichen Zhang636f5242022-12-07 20:25:44 +0000177 * Encode API-3
Dichen Zhang6947d532022-10-22 02:16:21 +0000178 * Compress JPEGR image from 10-bit HDR YUV and 8-bit SDR YUV.
179 *
Dichen Zhangffa34012022-11-03 23:21:13 +0000180 * This method requires HAL Hardware JPEG encoder.
181 *
Dichen Zhang10959a42023-04-10 16:28:16 -0700182 * Decode the compressed 8-bit JPEG image to YUV SDR, generate gain map from the HDR input
Nick Deakin0db53ee2023-05-19 17:14:45 -0400183 * 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 Zhang6947d532022-10-22 02:16:21 +0000186 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
187 * @param compressed_jpeg_image compressed 8-bit JPEG image
Nick Deakin6bd90432022-11-20 16:26:37 -0500188 * @param hdr_tf transfer function of the HDR image
Dichen Zhang92e6c6b2023-04-14 20:20:14 +0000189 * @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 Zhang6947d532022-10-22 02:16:21 +0000193 * @return NO_ERROR if encoding succeeds, error code if error occurs.
194 */
195 status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400196 jr_compressed_ptr compressed_jpeg_image,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000197 ultrahdr_transfer_function hdr_tf,
Dichen Zhang95cbb9f2022-11-07 18:32:05 +0000198 jr_compressed_ptr dest);
Dichen Zhang6947d532022-10-22 02:16:21 +0000199
200 /*
Dichen Zhang92e6c6b2023-04-14 20:20:14 +0000201 * Encode API-4
202 * Assemble JPEGR image from SDR JPEG and gainmap JPEG.
203 *
Nick Deakin0db53ee2023-05-19 17:14:45 -0400204 * 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 Zhang92e6c6b2023-04-14 20:20:14 +0000206 * @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 Zhang636f5242022-12-07 20:25:44 +0000221 * Decode API
Dichen Zhang6947d532022-10-22 02:16:21 +0000222 * Decompress JPEGR image.
223 *
Nick Deakin0db53ee2023-05-19 17:14:45 -0400224 * This method assumes that the JPEGR image contains an ICC profile with primaries that match
Nick Deakin094946b2023-06-09 11:58:41 -0400225 * 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 Deakin0db53ee2023-05-19 17:14:45 -0400230 *
Dichen Zhange286f1c2023-03-14 00:22:00 +0000231 * @param compressed_jpegr_image compressed JPEGR image.
232 * @param dest destination of the uncompressed JPEGR image.
Dichen Zhangb96ba8f2023-03-21 23:33:41 +0000233 * @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 Zhange286f1c2023-03-14 00:22:00 +0000235 * @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 Zhangc6605702023-03-15 18:40:55 -0700241 | output_format | decoded color format to be written |
Dichen Zhange286f1c2023-03-14 00:22:00 +0000242 ----------------------------------------------------------------------
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 Zhang10959a42023-04-10 16:28:16 -0700251 * @param gain_map destination of the decoded gain map. The default value is NULL where
Dichen Zhange286f1c2023-03-14 00:22:00 +0000252 the decoder will do nothing about it. If configured not NULL the decoder
Dichen Zhang10959a42023-04-10 16:28:16 -0700253 will write the decoded gain_map data into this structure. The format
Dichen Zhange286f1c2023-03-14 00:22:00 +0000254 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 Zhangdbceb0e2023-04-14 19:03:18 +0000258 {@code ultrahdr_metadata_struct}.
Dichen Zhang6947d532022-10-22 02:16:21 +0000259 * @return NO_ERROR if decoding succeeds, error code if error occurs.
260 */
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400261 status_t decodeJPEGR(jr_compressed_ptr compressed_jpegr_image,
Dichen Zhangffa34012022-11-03 23:21:13 +0000262 jr_uncompressed_ptr dest,
Dichen Zhangb96ba8f2023-03-21 23:33:41 +0000263 float max_display_boost = FLT_MAX,
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000264 jr_exif_ptr exif = nullptr,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000265 ultrahdr_output_format output_format = ULTRAHDR_OUTPUT_HDR_LINEAR,
Dichen Zhang10959a42023-04-10 16:28:16 -0700266 jr_uncompressed_ptr gain_map = nullptr,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000267 ultrahdr_metadata_ptr metadata = nullptr);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000268
269 /*
270 * Gets Info from JPEGR file without decoding it.
271 *
Nick Deakin094946b2023-06-09 11:58:41 -0400272 * This method only supports single gain map metadata values for fields that allow multi-channel
273 * metadata values.
274 *
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000275 * The output is filled jpegr_info structure
276 * @param compressed_jpegr_image compressed JPEGR image
Fyodor Kyslova90ee002023-01-10 23:41:41 +0000277 * @param jpegr_info pointer to output JPEGR info. Members of jpegr_info
278 * are owned by the caller
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000279 * @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 Deakind19e5762023-02-10 15:39:08 -0500283protected:
Dichen Zhang85b37562022-10-11 11:08:28 -0700284 /*
285 * This method is called in the encoding pipeline. It will take the uncompressed 8-bit and
Dichen Zhang10959a42023-04-10 16:28:16 -0700286 * 10-bit yuv images as input, and calculate the uncompressed gain map. The input images
Nick Deakin0db53ee2023-05-19 17:14:45 -0400287 * must be the same resolution. The SDR input is assumed to use the sRGB transfer function.
Dichen Zhang85b37562022-10-11 11:08:28 -0700288 *
Dichen Zhang596a7562022-10-12 14:57:05 -0700289 * @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 Deakin01759062023-02-02 18:21:43 -0500291 * @param hdr_tf transfer function of the HDR image
Dichen Zhang10959a42023-04-10 16:28:16 -0700292 * @param dest gain map; caller responsible for memory of data
Dichen Zhang761b52d2023-02-10 22:38:38 +0000293 * @param metadata max_content_boost is filled in
Nick Deakin0db53ee2023-05-19 17:14:45 -0400294 * @param sdr_is_601 if true, then use BT.601 decoding of YUV regardless of SDR image gamut
Dichen Zhang6947d532022-10-22 02:16:21 +0000295 * @return NO_ERROR if calculation succeeds, error code if error occurs.
Dichen Zhang85b37562022-10-11 11:08:28 -0700296 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700297 status_t generateGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
298 jr_uncompressed_ptr uncompressed_p010_image,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000299 ultrahdr_transfer_function hdr_tf,
300 ultrahdr_metadata_ptr metadata,
Nick Deakin0db53ee2023-05-19 17:14:45 -0400301 jr_uncompressed_ptr dest,
302 bool sdr_is_601 = false);
Dichen Zhang85b37562022-10-11 11:08:28 -0700303
304 /*
305 * This method is called in the decoding pipeline. It will take the uncompressed (decoded)
Dichen Zhang10959a42023-04-10 16:28:16 -0700306 * 8-bit yuv image, the uncompressed (decoded) gain map, and extracted JPEG/R metadata as
Nick Deakin6bd90432022-11-20 16:26:37 -0500307 * input, and calculate the 10-bit recovered image. The recovered output image is the same
Nick Deakin01759062023-02-02 18:21:43 -0500308 * color gamut as the SDR image, with HLG transfer function, and is in RGBA1010102 data format.
Nick Deakin0db53ee2023-05-19 17:14:45 -0400309 * 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 Zhang85b37562022-10-11 11:08:28 -0700311 *
Dichen Zhang596a7562022-10-12 14:57:05 -0700312 * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
Dichen Zhang10959a42023-04-10 16:28:16 -0700313 * @param uncompressed_gain_map uncompressed gain map
Nick Deakin6bd90432022-11-20 16:26:37 -0500314 * @param metadata JPEG/R metadata extracted from XMP.
Dichen Zhang3e5798c2023-03-01 22:30:43 +0000315 * @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 Zhangc6605702023-03-15 18:40:55 -0700318 * @param max_display_boost the maximum available boost supported by a display
Dichen Zhang596a7562022-10-12 14:57:05 -0700319 * @param dest reconstructed HDR image
Dichen Zhang6947d532022-10-22 02:16:21 +0000320 * @return NO_ERROR if calculation succeeds, error code if error occurs.
Dichen Zhang85b37562022-10-11 11:08:28 -0700321 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700322 status_t applyGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
323 jr_uncompressed_ptr uncompressed_gain_map,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000324 ultrahdr_metadata_ptr metadata,
325 ultrahdr_output_format output_format,
Dichen Zhang10959a42023-04-10 16:28:16 -0700326 float max_display_boost,
327 jr_uncompressed_ptr dest);
Dichen Zhang85b37562022-10-11 11:08:28 -0700328
Nick Deakind19e5762023-02-10 15:39:08 -0500329private:
330 /*
Dichen Zhang10959a42023-04-10 16:28:16 -0700331 * This method is called in the encoding pipeline. It will encode the gain map.
Nick Deakind19e5762023-02-10 15:39:08 -0500332 *
Dichen Zhang10959a42023-04-10 16:28:16 -0700333 * @param uncompressed_gain_map uncompressed gain map
Ram Mohanf9540402023-05-18 20:08:55 +0530334 * @param resource to compress gain map
Nick Deakind19e5762023-02-10 15:39:08 -0500335 * @return NO_ERROR if encoding succeeds, error code if error occurs.
336 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700337 status_t compressGainMap(jr_uncompressed_ptr uncompressed_gain_map,
Ram Mohanf9540402023-05-18 20:08:55 +0530338 JpegEncoderHelper* jpeg_encoder);
Nick Deakind19e5762023-02-10 15:39:08 -0500339
Dichen Zhang85b37562022-10-11 11:08:28 -0700340 /*
Dichen Zhang10959a42023-04-10 16:28:16 -0700341 * This methoud is called to separate primary image and gain map image from JPEGR
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000342 *
343 * @param compressed_jpegr_image compressed JPEGR image
344 * @param primary_image destination of primary image
Dichen Zhang10959a42023-04-10 16:28:16 -0700345 * @param gain_map destination of compressed gain map
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000346 * @return NO_ERROR if calculation succeeds, error code if error occurs.
347 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700348 status_t extractPrimaryImageAndGainMap(jr_compressed_ptr compressed_jpegr_image,
349 jr_compressed_ptr primary_image,
350 jr_compressed_ptr gain_map);
Dichen Zhang85b37562022-10-11 11:08:28 -0700351
352 /*
Dichen Zhang50ff1292023-01-27 18:03:43 +0000353 * This method is called in the encoding pipeline. It will take the standard 8-bit JPEG image,
Dichen Zhang10959a42023-04-10 16:28:16 -0700354 * the compressed gain map and optionally the exif package as inputs, and generate the XMP
Dichen Zhang50ff1292023-01-27 18:03:43 +0000355 * metadata, and finally append everything in the order of:
Dichen Zhang10959a42023-04-10 16:28:16 -0700356 * SOI, APP2(EXIF) (if EXIF is from outside), APP2(XMP), primary image, gain map
Dichen Zhang50ff1292023-01-27 18:03:43 +0000357 * Note that EXIF package is only available for encoding API-0 and API-1. For encoding API-2 and
358 * API-3 this parameter is null, but the primary image in JPEG/R may still have EXIF as long as
359 * the input JPEG has EXIF.
Dichen Zhang85b37562022-10-11 11:08:28 -0700360 *
Dichen Zhang596a7562022-10-12 14:57:05 -0700361 * @param compressed_jpeg_image compressed 8-bit JPEG image
Dichen Zhang10959a42023-04-10 16:28:16 -0700362 * @param compress_gain_map compressed recover map
Dichen Zhang50ff1292023-01-27 18:03:43 +0000363 * @param (nullable) exif EXIF package
Nick Deakin0db53ee2023-05-19 17:14:45 -0400364 * @param (nullable) icc ICC package
365 * @param icc_size length in bytes of ICC package
Nick Deakin6bd90432022-11-20 16:26:37 -0500366 * @param metadata JPEG/R metadata to encode in XMP of the jpeg
Dichen Zhang6947d532022-10-22 02:16:21 +0000367 * @param dest compressed JPEGR image
368 * @return NO_ERROR if calculation succeeds, error code if error occurs.
Dichen Zhang85b37562022-10-11 11:08:28 -0700369 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700370 status_t appendGainMap(jr_compressed_ptr compressed_jpeg_image,
371 jr_compressed_ptr compressed_gain_map,
372 jr_exif_ptr exif,
Nick Deakin0db53ee2023-05-19 17:14:45 -0400373 void* icc, size_t icc_size,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000374 ultrahdr_metadata_ptr metadata,
Dichen Zhang10959a42023-04-10 16:28:16 -0700375 jr_compressed_ptr dest);
Dichen Zhang72fd2b12022-11-01 06:11:50 +0000376
377 /*
Dichen Zhang636f5242022-12-07 20:25:44 +0000378 * This method will tone map a HDR image to an SDR image.
379 *
Dichen Zhangc3437ca2023-01-04 14:00:08 -0800380 * @param src (input) uncompressed P010 image
Dichen Zhang636f5242022-12-07 20:25:44 +0000381 * @param dest (output) tone mapping result as a YUV_420 image
382 * @return NO_ERROR if calculation succeeds, error code if error occurs.
383 */
Dichen Zhangc3437ca2023-01-04 14:00:08 -0800384 status_t toneMap(jr_uncompressed_ptr src,
Dichen Zhang636f5242022-12-07 20:25:44 +0000385 jr_uncompressed_ptr dest);
Dichen Zhang66ca6e32023-04-05 12:22:54 -0700386
387 /*
Nick Deakin0db53ee2023-05-19 17:14:45 -0400388 * This method will convert a YUV420 image from one YUV encoding to another in-place (eg.
389 * Bt.709 to Bt.601 YUV encoding).
390 *
391 * src_encoding and dest_encoding indicate the encoding via the YUV conversion defined for that
392 * gamut. P3 indicates Rec.601, since this is how DataSpace encodes Display-P3 YUV data.
393 *
394 * @param image the YUV420 image to convert
395 * @param src_encoding input YUV encoding
396 * @param dest_encoding output YUV encoding
397 * @return NO_ERROR if calculation succeeds, error code if error occurs.
398 */
399 status_t convertYuv(jr_uncompressed_ptr image,
400 ultrahdr_color_gamut src_encoding,
401 ultrahdr_color_gamut dest_encoding);
402
403 /*
Ram Mohanac1cfec2023-05-18 14:41:15 +0530404 * This method will check the validity of the input arguments.
Dichen Zhang66ca6e32023-04-05 12:22:54 -0700405 *
406 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
407 * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
Ram Mohanac1cfec2023-05-18 14:41:15 +0530408 * @param hdr_tf transfer function of the HDR image
409 * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
410 * represents the maximum available size of the desitination buffer, and it must be
411 * set before calling this method. If the encoded JPEGR size exceeds
412 * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
413 * @return NO_ERROR if the input args are valid, error code is not valid.
Dichen Zhang66ca6e32023-04-05 12:22:54 -0700414 */
Ram Mohanac1cfec2023-05-18 14:41:15 +0530415 status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
416 jr_uncompressed_ptr uncompressed_yuv_420_image,
417 ultrahdr_transfer_function hdr_tf,
418 jr_compressed_ptr dest);
419
420 /*
421 * This method will check the validity of the input arguments.
422 *
423 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
424 * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
425 * @param hdr_tf transfer function of the HDR image
426 * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
427 * represents the maximum available size of the desitination buffer, and it must be
428 * set before calling this method. If the encoded JPEGR size exceeds
429 * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
430 * @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is
431 * the highest quality
432 * @return NO_ERROR if the input args are valid, error code is not valid.
433 */
434 status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
435 jr_uncompressed_ptr uncompressed_yuv_420_image,
436 ultrahdr_transfer_function hdr_tf,
437 jr_compressed_ptr dest,
438 int quality);
Dichen Zhang85b37562022-10-11 11:08:28 -0700439};
440
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000441} // namespace android::ultrahdr
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400442
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000443#endif // ANDROID_ULTRAHDR_JPEGR_H