Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 1 | #include "CreateJavaOutputStreamAdaptor.h" |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 2 | #include "SkStream.h" |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 3 | #include "YuvToJpegEncoder.h" |
Mathias Agopian | 8f2423e | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 4 | #include <ui/PixelFormat.h> |
| 5 | #include <hardware/hardware.h> |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 6 | |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 7 | #include "graphics_jni_helpers.h" |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 8 | |
Kevin Lubick | ba25360 | 2022-08-09 21:13:39 +0000 | [diff] [blame] | 9 | #include <csetjmp> |
| 10 | |
Kevin Lubick | 4c39197 | 2023-02-16 12:54:46 +0000 | [diff] [blame] | 11 | extern "C" { |
| 12 | // We need to include stdio.h before jpeg because jpeg does not include it, but uses FILE |
| 13 | // See https://github.com/libjpeg-turbo/libjpeg-turbo/issues/17 |
| 14 | #include <stdio.h> |
| 15 | #include "jpeglib.h" |
| 16 | #include "jerror.h" |
| 17 | #include "jmorecfg.h" |
| 18 | } |
| 19 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 20 | YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) { |
Mathias Agopian | a696f5d | 2010-02-17 17:53:09 -0800 | [diff] [blame] | 21 | // Only ImageFormat.NV21 and ImageFormat.YUY2 are supported |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 22 | // for now. |
Mathias Agopian | a696f5d | 2010-02-17 17:53:09 -0800 | [diff] [blame] | 23 | if (format == HAL_PIXEL_FORMAT_YCrCb_420_SP) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 24 | return new Yuv420SpToJpegEncoder(strides); |
Mathias Agopian | 8f2423e | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 25 | } else if (format == HAL_PIXEL_FORMAT_YCbCr_422_I) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 26 | return new Yuv422IToJpegEncoder(strides); |
| 27 | } else { |
| 28 | return NULL; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) { |
| 33 | } |
| 34 | |
Leon Scroggins III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 35 | struct ErrorMgr { |
| 36 | struct jpeg_error_mgr pub; |
| 37 | jmp_buf jmp; |
| 38 | }; |
| 39 | |
| 40 | void error_exit(j_common_ptr cinfo) { |
| 41 | ErrorMgr* err = (ErrorMgr*) cinfo->err; |
| 42 | (*cinfo->err->output_message) (cinfo); |
| 43 | longjmp(err->jmp, 1); |
| 44 | } |
| 45 | |
Kevin Lubick | 4c39197 | 2023-02-16 12:54:46 +0000 | [diff] [blame] | 46 | /* |
| 47 | * Destination struct for directing decompressed pixels to a SkStream. |
| 48 | */ |
| 49 | static constexpr size_t kMgrBufferSize = 1024; |
| 50 | struct skstream_destination_mgr : jpeg_destination_mgr { |
| 51 | skstream_destination_mgr(SkWStream* stream); |
| 52 | |
| 53 | SkWStream* const fStream; |
| 54 | |
| 55 | uint8_t fBuffer[kMgrBufferSize]; |
| 56 | }; |
| 57 | |
| 58 | static void sk_init_destination(j_compress_ptr cinfo) { |
| 59 | skstream_destination_mgr* dest = (skstream_destination_mgr*)cinfo->dest; |
| 60 | |
| 61 | dest->next_output_byte = dest->fBuffer; |
| 62 | dest->free_in_buffer = kMgrBufferSize; |
| 63 | } |
| 64 | |
| 65 | static boolean sk_empty_output_buffer(j_compress_ptr cinfo) { |
| 66 | skstream_destination_mgr* dest = (skstream_destination_mgr*)cinfo->dest; |
| 67 | |
| 68 | if (!dest->fStream->write(dest->fBuffer, kMgrBufferSize)) { |
| 69 | ERREXIT(cinfo, JERR_FILE_WRITE); |
| 70 | return FALSE; |
| 71 | } |
| 72 | |
| 73 | dest->next_output_byte = dest->fBuffer; |
| 74 | dest->free_in_buffer = kMgrBufferSize; |
| 75 | return TRUE; |
| 76 | } |
| 77 | |
| 78 | static void sk_term_destination(j_compress_ptr cinfo) { |
| 79 | skstream_destination_mgr* dest = (skstream_destination_mgr*)cinfo->dest; |
| 80 | |
| 81 | size_t size = kMgrBufferSize - dest->free_in_buffer; |
| 82 | if (size > 0) { |
| 83 | if (!dest->fStream->write(dest->fBuffer, size)) { |
| 84 | ERREXIT(cinfo, JERR_FILE_WRITE); |
| 85 | return; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | dest->fStream->flush(); |
| 90 | } |
| 91 | |
| 92 | skstream_destination_mgr::skstream_destination_mgr(SkWStream* stream) |
| 93 | : fStream(stream) { |
| 94 | this->init_destination = sk_init_destination; |
| 95 | this->empty_output_buffer = sk_empty_output_buffer; |
| 96 | this->term_destination = sk_term_destination; |
| 97 | } |
| 98 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 99 | bool YuvToJpegEncoder::encode(SkWStream* stream, void* inYuv, int width, |
| 100 | int height, int* offsets, int jpegQuality) { |
Kevin Lubick | 4c39197 | 2023-02-16 12:54:46 +0000 | [diff] [blame] | 101 | jpeg_compress_struct cinfo; |
| 102 | ErrorMgr err; |
| 103 | skstream_destination_mgr sk_wstream(stream); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 104 | |
Leon Scroggins III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 105 | cinfo.err = jpeg_std_error(&err.pub); |
| 106 | err.pub.error_exit = error_exit; |
| 107 | |
| 108 | if (setjmp(err.jmp)) { |
| 109 | jpeg_destroy_compress(&cinfo); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | jpeg_create_compress(&cinfo); |
| 113 | |
| 114 | cinfo.dest = &sk_wstream; |
| 115 | |
| 116 | setJpegCompressStruct(&cinfo, width, height, jpegQuality); |
| 117 | |
| 118 | jpeg_start_compress(&cinfo, TRUE); |
| 119 | |
| 120 | compress(&cinfo, (uint8_t*) inYuv, offsets); |
| 121 | |
| 122 | jpeg_finish_compress(&cinfo); |
| 123 | |
Leon Scroggins III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 124 | jpeg_destroy_compress(&cinfo); |
| 125 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
| 129 | void YuvToJpegEncoder::setJpegCompressStruct(jpeg_compress_struct* cinfo, |
| 130 | int width, int height, int quality) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 131 | cinfo->image_width = width; |
| 132 | cinfo->image_height = height; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 133 | cinfo->input_components = 3; |
| 134 | cinfo->in_color_space = JCS_YCbCr; |
| 135 | jpeg_set_defaults(cinfo); |
Chia-chi Yeh | aa86859 | 2010-03-10 17:08:58 +0800 | [diff] [blame] | 136 | |
| 137 | jpeg_set_quality(cinfo, quality, TRUE); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 138 | jpeg_set_colorspace(cinfo, JCS_YCbCr); |
| 139 | cinfo->raw_data_in = TRUE; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 140 | cinfo->dct_method = JDCT_IFAST; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 141 | configSamplingFactors(cinfo); |
| 142 | } |
| 143 | |
| 144 | /////////////////////////////////////////////////////////////////// |
| 145 | Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) : |
| 146 | YuvToJpegEncoder(strides) { |
| 147 | fNumPlanes = 2; |
| 148 | } |
| 149 | |
| 150 | void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo, |
| 151 | uint8_t* yuv, int* offsets) { |
Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 152 | ALOGD("onFlyCompress"); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 153 | JSAMPROW y[16]; |
| 154 | JSAMPROW cb[8]; |
| 155 | JSAMPROW cr[8]; |
| 156 | JSAMPARRAY planes[3]; |
| 157 | planes[0] = y; |
| 158 | planes[1] = cb; |
| 159 | planes[2] = cr; |
| 160 | |
| 161 | int width = cinfo->image_width; |
| 162 | int height = cinfo->image_height; |
| 163 | uint8_t* yPlanar = yuv + offsets[0]; |
| 164 | uint8_t* vuPlanar = yuv + offsets[1]; //width * height; |
| 165 | uint8_t* uRows = new uint8_t [8 * (width >> 1)]; |
| 166 | uint8_t* vRows = new uint8_t [8 * (width >> 1)]; |
| 167 | |
| 168 | |
| 169 | // process 16 lines of Y and 8 lines of U/V each time. |
| 170 | while (cinfo->next_scanline < cinfo->image_height) { |
| 171 | //deitnerleave u and v |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 172 | deinterleave(vuPlanar, uRows, vRows, cinfo->next_scanline, width, height); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 173 | |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 174 | // Jpeg library ignores the rows whose indices are greater than height. |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 175 | for (int i = 0; i < 16; i++) { |
| 176 | // y row |
| 177 | y[i] = yPlanar + (cinfo->next_scanline + i) * fStrides[0]; |
| 178 | |
| 179 | // construct u row and v row |
| 180 | if ((i & 1) == 0) { |
| 181 | // height and width are both halved because of downsampling |
| 182 | int offset = (i >> 1) * (width >> 1); |
| 183 | cb[i/2] = uRows + offset; |
| 184 | cr[i/2] = vRows + offset; |
| 185 | } |
| 186 | } |
| 187 | jpeg_write_raw_data(cinfo, planes, 16); |
| 188 | } |
| 189 | delete [] uRows; |
| 190 | delete [] vRows; |
| 191 | |
| 192 | } |
| 193 | |
| 194 | void Yuv420SpToJpegEncoder::deinterleave(uint8_t* vuPlanar, uint8_t* uRows, |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 195 | uint8_t* vRows, int rowIndex, int width, int height) { |
| 196 | int numRows = (height - rowIndex) / 2; |
| 197 | if (numRows > 8) numRows = 8; |
| 198 | for (int row = 0; row < numRows; ++row) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 199 | int offset = ((rowIndex >> 1) + row) * fStrides[1]; |
| 200 | uint8_t* vu = vuPlanar + offset; |
| 201 | for (int i = 0; i < (width >> 1); ++i) { |
| 202 | int index = row * (width >> 1) + i; |
| 203 | uRows[index] = vu[1]; |
| 204 | vRows[index] = vu[0]; |
| 205 | vu += 2; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void Yuv420SpToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) { |
| 211 | // cb and cr are horizontally downsampled and vertically downsampled as well. |
| 212 | cinfo->comp_info[0].h_samp_factor = 2; |
| 213 | cinfo->comp_info[0].v_samp_factor = 2; |
| 214 | cinfo->comp_info[1].h_samp_factor = 1; |
| 215 | cinfo->comp_info[1].v_samp_factor = 1; |
| 216 | cinfo->comp_info[2].h_samp_factor = 1; |
| 217 | cinfo->comp_info[2].v_samp_factor = 1; |
| 218 | } |
| 219 | |
| 220 | /////////////////////////////////////////////////////////////////////////////// |
| 221 | Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) : |
| 222 | YuvToJpegEncoder(strides) { |
| 223 | fNumPlanes = 1; |
| 224 | } |
| 225 | |
| 226 | void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo, |
| 227 | uint8_t* yuv, int* offsets) { |
Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 228 | ALOGD("onFlyCompress_422"); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 229 | JSAMPROW y[16]; |
| 230 | JSAMPROW cb[16]; |
| 231 | JSAMPROW cr[16]; |
| 232 | JSAMPARRAY planes[3]; |
| 233 | planes[0] = y; |
| 234 | planes[1] = cb; |
| 235 | planes[2] = cr; |
| 236 | |
| 237 | int width = cinfo->image_width; |
| 238 | int height = cinfo->image_height; |
| 239 | uint8_t* yRows = new uint8_t [16 * width]; |
| 240 | uint8_t* uRows = new uint8_t [16 * (width >> 1)]; |
| 241 | uint8_t* vRows = new uint8_t [16 * (width >> 1)]; |
| 242 | |
| 243 | uint8_t* yuvOffset = yuv + offsets[0]; |
| 244 | |
| 245 | // process 16 lines of Y and 16 lines of U/V each time. |
| 246 | while (cinfo->next_scanline < cinfo->image_height) { |
| 247 | deinterleave(yuvOffset, yRows, uRows, vRows, cinfo->next_scanline, width, height); |
| 248 | |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 249 | // Jpeg library ignores the rows whose indices are greater than height. |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 250 | for (int i = 0; i < 16; i++) { |
| 251 | // y row |
| 252 | y[i] = yRows + i * width; |
| 253 | |
| 254 | // construct u row and v row |
| 255 | // width is halved because of downsampling |
| 256 | int offset = i * (width >> 1); |
| 257 | cb[i] = uRows + offset; |
| 258 | cr[i] = vRows + offset; |
| 259 | } |
| 260 | |
| 261 | jpeg_write_raw_data(cinfo, planes, 16); |
| 262 | } |
| 263 | delete [] yRows; |
| 264 | delete [] uRows; |
| 265 | delete [] vRows; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | void Yuv422IToJpegEncoder::deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows, |
| 270 | uint8_t* vRows, int rowIndex, int width, int height) { |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 271 | int numRows = height - rowIndex; |
| 272 | if (numRows > 16) numRows = 16; |
| 273 | for (int row = 0; row < numRows; ++row) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 274 | uint8_t* yuvSeg = yuv + (rowIndex + row) * fStrides[0]; |
| 275 | for (int i = 0; i < (width >> 1); ++i) { |
| 276 | int indexY = row * width + (i << 1); |
| 277 | int indexU = row * (width >> 1) + i; |
| 278 | yRows[indexY] = yuvSeg[0]; |
| 279 | yRows[indexY + 1] = yuvSeg[2]; |
| 280 | uRows[indexU] = yuvSeg[1]; |
| 281 | vRows[indexU] = yuvSeg[3]; |
| 282 | yuvSeg += 4; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void Yuv422IToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) { |
| 288 | // cb and cr are horizontally downsampled and vertically downsampled as well. |
| 289 | cinfo->comp_info[0].h_samp_factor = 2; |
| 290 | cinfo->comp_info[0].v_samp_factor = 2; |
| 291 | cinfo->comp_info[1].h_samp_factor = 1; |
| 292 | cinfo->comp_info[1].v_samp_factor = 2; |
| 293 | cinfo->comp_info[2].h_samp_factor = 1; |
| 294 | cinfo->comp_info[2].v_samp_factor = 2; |
| 295 | } |
| 296 | /////////////////////////////////////////////////////////////////////////////// |
| 297 | |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 298 | using namespace android::ultrahdr; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 299 | |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 300 | ultrahdr_color_gamut P010Yuv420ToJpegREncoder::findColorGamut(JNIEnv* env, int aDataSpace) { |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 301 | switch (aDataSpace & ADataSpace::STANDARD_MASK) { |
| 302 | case ADataSpace::STANDARD_BT709: |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 303 | return ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT709; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 304 | case ADataSpace::STANDARD_DCI_P3: |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 305 | return ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_P3; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 306 | case ADataSpace::STANDARD_BT2020: |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 307 | return ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 308 | default: |
| 309 | jclass IllegalArgumentException = env->FindClass("java/lang/IllegalArgumentException"); |
| 310 | env->ThrowNew(IllegalArgumentException, |
| 311 | "The requested color gamut is not supported by JPEG/R."); |
| 312 | } |
| 313 | |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 314 | return ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_UNSPECIFIED; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 317 | ultrahdr_transfer_function P010Yuv420ToJpegREncoder::findHdrTransferFunction(JNIEnv* env, |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 318 | int aDataSpace) { |
| 319 | switch (aDataSpace & ADataSpace::TRANSFER_MASK) { |
| 320 | case ADataSpace::TRANSFER_ST2084: |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 321 | return ultrahdr_transfer_function::ULTRAHDR_TF_PQ; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 322 | case ADataSpace::TRANSFER_HLG: |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 323 | return ultrahdr_transfer_function::ULTRAHDR_TF_HLG; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 324 | default: |
| 325 | jclass IllegalArgumentException = env->FindClass("java/lang/IllegalArgumentException"); |
| 326 | env->ThrowNew(IllegalArgumentException, |
| 327 | "The requested HDR transfer function is not supported by JPEG/R."); |
| 328 | } |
| 329 | |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 330 | return ultrahdr_transfer_function::ULTRAHDR_TF_UNSPECIFIED; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | bool P010Yuv420ToJpegREncoder::encode(JNIEnv* env, |
| 334 | SkWStream* stream, void* hdr, int hdrColorSpace, void* sdr, int sdrColorSpace, |
Dichen Zhang | 843f1bd | 2023-10-26 02:38:21 +0000 | [diff] [blame^] | 335 | int width, int height, int jpegQuality, ScopedByteArrayRO* jExif, |
| 336 | ScopedIntArrayRO* jHdrStrides, ScopedIntArrayRO* jSdrStrides) { |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 337 | // Check SDR color space. Now we only support SRGB transfer function |
| 338 | if ((sdrColorSpace & ADataSpace::TRANSFER_MASK) != ADataSpace::TRANSFER_SRGB) { |
| 339 | jclass IllegalArgumentException = env->FindClass("java/lang/IllegalArgumentException"); |
| 340 | env->ThrowNew(IllegalArgumentException, |
| 341 | "The requested SDR color space is not supported. Transfer function must be SRGB"); |
| 342 | return false; |
| 343 | } |
Dichen Zhang | 843f1bd | 2023-10-26 02:38:21 +0000 | [diff] [blame^] | 344 | // Check HDR and SDR strides length. |
| 345 | // HDR is YCBCR_P010 color format, and its strides length must be 2 (Y, chroma (Cb, Cr)). |
| 346 | // SDR is YUV_420_888 color format, and its strides length must be 3 (Y, Cb, Cr). |
| 347 | if (jHdrStrides->size() != 2) { |
| 348 | jclass IllegalArgumentException = env->FindClass("java/lang/IllegalArgumentException"); |
| 349 | env->ThrowNew(IllegalArgumentException, "HDR stride length must be 2."); |
| 350 | return false; |
| 351 | } |
| 352 | if (jSdrStrides->size() != 3) { |
| 353 | jclass IllegalArgumentException = env->FindClass("java/lang/IllegalArgumentException"); |
| 354 | env->ThrowNew(IllegalArgumentException, "SDR stride length must be 3."); |
| 355 | return false; |
| 356 | } |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 357 | |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 358 | ultrahdr_color_gamut hdrColorGamut = findColorGamut(env, hdrColorSpace); |
| 359 | ultrahdr_color_gamut sdrColorGamut = findColorGamut(env, sdrColorSpace); |
| 360 | ultrahdr_transfer_function hdrTransferFunction = findHdrTransferFunction(env, hdrColorSpace); |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 361 | |
Dichen Zhang | 7087aaf | 2023-04-14 19:01:05 +0000 | [diff] [blame] | 362 | if (hdrColorGamut == ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_UNSPECIFIED |
| 363 | || sdrColorGamut == ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_UNSPECIFIED |
| 364 | || hdrTransferFunction == ultrahdr_transfer_function::ULTRAHDR_TF_UNSPECIFIED) { |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 365 | return false; |
| 366 | } |
| 367 | |
Dichen Zhang | 843f1bd | 2023-10-26 02:38:21 +0000 | [diff] [blame^] | 368 | const int* hdrStrides = reinterpret_cast<const int*>(jHdrStrides->get()); |
| 369 | const int* sdrStrides = reinterpret_cast<const int*>(jSdrStrides->get()); |
| 370 | |
Dichen Zhang | f84ed40 | 2023-02-10 22:41:46 +0000 | [diff] [blame] | 371 | JpegR jpegREncoder; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 372 | |
| 373 | jpegr_uncompressed_struct p010; |
| 374 | p010.data = hdr; |
| 375 | p010.width = width; |
| 376 | p010.height = height; |
Dichen Zhang | 843f1bd | 2023-10-26 02:38:21 +0000 | [diff] [blame^] | 377 | // Divided by 2 because unit in libultrader is pixel and in YuvImage it is byte. |
| 378 | p010.luma_stride = (hdrStrides[0] + 1) / 2; |
| 379 | p010.chroma_stride = (hdrStrides[1] + 1) / 2; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 380 | p010.colorGamut = hdrColorGamut; |
| 381 | |
| 382 | jpegr_uncompressed_struct yuv420; |
| 383 | yuv420.data = sdr; |
| 384 | yuv420.width = width; |
| 385 | yuv420.height = height; |
Dichen Zhang | 843f1bd | 2023-10-26 02:38:21 +0000 | [diff] [blame^] | 386 | yuv420.luma_stride = sdrStrides[0]; |
| 387 | yuv420.chroma_stride = sdrStrides[1]; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 388 | yuv420.colorGamut = sdrColorGamut; |
| 389 | |
Dichen Zhang | bfd2380 | 2023-10-26 01:29:19 +0000 | [diff] [blame] | 390 | jpegr_exif_struct exif; |
| 391 | exif.data = const_cast<void*>(reinterpret_cast<const void*>(jExif->get())); |
| 392 | exif.length = jExif->size(); |
| 393 | |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 394 | jpegr_compressed_struct jpegR; |
| 395 | jpegR.maxLength = width * height * sizeof(uint8_t); |
| 396 | |
| 397 | std::unique_ptr<uint8_t[]> jpegr_data = std::make_unique<uint8_t[]>(jpegR.maxLength); |
| 398 | jpegR.data = jpegr_data.get(); |
| 399 | |
Dichen Zhang | f84ed40 | 2023-02-10 22:41:46 +0000 | [diff] [blame] | 400 | if (int success = jpegREncoder.encodeJPEGR(&p010, &yuv420, |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 401 | hdrTransferFunction, |
Dichen Zhang | bfd2380 | 2023-10-26 01:29:19 +0000 | [diff] [blame] | 402 | &jpegR, jpegQuality, |
| 403 | exif.length > 0 ? &exif : NULL); success != android::OK) { |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 404 | ALOGW("Encode JPEG/R failed, error code: %d.", success); |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | if (!stream->write(jpegR.data, jpegR.length)) { |
| 409 | ALOGW("Writing JPEG/R to stream failed."); |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | /////////////////////////////////////////////////////////////////////////////// |
| 417 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 418 | static jboolean YuvImage_compressToJpeg(JNIEnv* env, jobject, jbyteArray inYuv, |
Ashok Bhat | 39029b2 | 2014-01-10 16:24:38 +0000 | [diff] [blame] | 419 | jint format, jint width, jint height, jintArray offsets, |
| 420 | jintArray strides, jint jpegQuality, jobject jstream, |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 421 | jbyteArray jstorage) { |
| 422 | jbyte* yuv = env->GetByteArrayElements(inYuv, NULL); |
| 423 | SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); |
| 424 | |
| 425 | jint* imgOffsets = env->GetIntArrayElements(offsets, NULL); |
| 426 | jint* imgStrides = env->GetIntArrayElements(strides, NULL); |
| 427 | YuvToJpegEncoder* encoder = YuvToJpegEncoder::create(format, imgStrides); |
Pawel Augustyn | 7c68a407 | 2012-07-16 11:23:54 +0200 | [diff] [blame] | 428 | jboolean result = JNI_FALSE; |
| 429 | if (encoder != NULL) { |
| 430 | encoder->encode(strm, yuv, width, height, imgOffsets, jpegQuality); |
| 431 | delete encoder; |
| 432 | result = JNI_TRUE; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 433 | } |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 434 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 435 | env->ReleaseByteArrayElements(inYuv, yuv, 0); |
| 436 | env->ReleaseIntArrayElements(offsets, imgOffsets, 0); |
| 437 | env->ReleaseIntArrayElements(strides, imgStrides, 0); |
Martin Wallgren | d865900 | 2014-08-20 14:58:58 +0200 | [diff] [blame] | 438 | delete strm; |
Pawel Augustyn | 7c68a407 | 2012-07-16 11:23:54 +0200 | [diff] [blame] | 439 | return result; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 440 | } |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 441 | |
| 442 | static jboolean YuvImage_compressToJpegR(JNIEnv* env, jobject, jbyteArray inHdr, |
| 443 | jint hdrColorSpace, jbyteArray inSdr, jint sdrColorSpace, |
| 444 | jint width, jint height, jint quality, jobject jstream, |
Dichen Zhang | 843f1bd | 2023-10-26 02:38:21 +0000 | [diff] [blame^] | 445 | jbyteArray jstorage, jbyteArray jExif, |
| 446 | jintArray jHdrStrides, jintArray jSdrStrides) { |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 447 | jbyte* hdr = env->GetByteArrayElements(inHdr, NULL); |
| 448 | jbyte* sdr = env->GetByteArrayElements(inSdr, NULL); |
Dichen Zhang | bfd2380 | 2023-10-26 01:29:19 +0000 | [diff] [blame] | 449 | ScopedByteArrayRO exif(env, jExif); |
Dichen Zhang | 843f1bd | 2023-10-26 02:38:21 +0000 | [diff] [blame^] | 450 | ScopedIntArrayRO hdrStrides(env, jHdrStrides); |
| 451 | ScopedIntArrayRO sdrStrides(env, jSdrStrides); |
Dichen Zhang | bfd2380 | 2023-10-26 01:29:19 +0000 | [diff] [blame] | 452 | |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 453 | SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); |
| 454 | P010Yuv420ToJpegREncoder encoder; |
| 455 | |
| 456 | jboolean result = JNI_FALSE; |
| 457 | if (encoder.encode(env, strm, hdr, hdrColorSpace, sdr, sdrColorSpace, |
Dichen Zhang | 843f1bd | 2023-10-26 02:38:21 +0000 | [diff] [blame^] | 458 | width, height, quality, &exif, |
| 459 | &hdrStrides, &sdrStrides)) { |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 460 | result = JNI_TRUE; |
| 461 | } |
| 462 | |
| 463 | env->ReleaseByteArrayElements(inHdr, hdr, 0); |
| 464 | env->ReleaseByteArrayElements(inSdr, sdr, 0); |
Dichen Zhang | 843f1bd | 2023-10-26 02:38:21 +0000 | [diff] [blame^] | 465 | |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 466 | delete strm; |
| 467 | return result; |
| 468 | } |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 469 | /////////////////////////////////////////////////////////////////////////////// |
| 470 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 471 | static const JNINativeMethod gYuvImageMethods[] = { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 472 | { "nativeCompressToJpeg", "([BIII[I[IILjava/io/OutputStream;[B)Z", |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 473 | (void*)YuvImage_compressToJpeg }, |
Dichen Zhang | 843f1bd | 2023-10-26 02:38:21 +0000 | [diff] [blame^] | 474 | { "nativeCompressToJpegR", "([BI[BIIIILjava/io/OutputStream;[B[B[I[I)Z", |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 475 | (void*)YuvImage_compressToJpegR } |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 476 | }; |
| 477 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 478 | int register_android_graphics_YuvImage(JNIEnv* env) |
| 479 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 480 | return android::RegisterMethodsOrDie(env, "android/graphics/YuvImage", gYuvImageMethods, |
| 481 | NELEM(gYuvImageMethods)); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 482 | } |