blob: 7ccf1f3697e6a905f56b39f9622ddc0173e5ad05 [file] [log] [blame]
Dichen Zhang0d12ba82022-10-19 20:44:51 +00001/*
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#include <ultrahdr/jpegdecoderhelper.h>
Dichen Zhang0d12ba82022-10-19 20:44:51 +000018
Dichen Zhangb27d06d2022-12-14 19:57:50 +000019#include <utils/Log.h>
Dichen Zhang0d12ba82022-10-19 20:44:51 +000020
21#include <errno.h>
22#include <setjmp.h>
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000023#include <string>
24
25using namespace std;
Dichen Zhang0d12ba82022-10-19 20:44:51 +000026
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000027namespace android::ultrahdr {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000028
Ram Mohan0202c122023-08-13 17:22:35 +053029#define ALIGNM(x, m) ((((x) + ((m)-1)) / (m)) * (m))
Ram Mohan2838bec2023-05-19 02:46:49 +053030
Ram Mohan0202c122023-08-13 17:22:35 +053031const uint32_t kAPP0Marker = JPEG_APP0; // JFIF
32const uint32_t kAPP1Marker = JPEG_APP0 + 1; // EXIF, XMP
33const uint32_t kAPP2Marker = JPEG_APP0 + 2; // ICC
Dichen Zhangd18bc302022-12-16 20:55:24 +000034
35const std::string kXmpNameSpace = "http://ns.adobe.com/xap/1.0/";
36const std::string kExifIdCode = "Exif";
Fyodor Kyslove00916d2023-01-16 22:12:02 +000037constexpr uint32_t kICCMarkerHeaderSize = 14;
38constexpr uint8_t kICCSig[] = {
39 'I', 'C', 'C', '_', 'P', 'R', 'O', 'F', 'I', 'L', 'E', '\0',
40};
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000041
Dichen Zhang0d12ba82022-10-19 20:44:51 +000042struct jpegr_source_mgr : jpeg_source_mgr {
43 jpegr_source_mgr(const uint8_t* ptr, int len);
44 ~jpegr_source_mgr();
45
46 const uint8_t* mBufferPtr;
47 size_t mBufferLength;
48};
49
50struct jpegrerror_mgr {
51 struct jpeg_error_mgr pub;
52 jmp_buf setjmp_buffer;
53};
54
55static void jpegr_init_source(j_decompress_ptr cinfo) {
56 jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
57 src->next_input_byte = static_cast<const JOCTET*>(src->mBufferPtr);
58 src->bytes_in_buffer = src->mBufferLength;
59}
60
61static boolean jpegr_fill_input_buffer(j_decompress_ptr /* cinfo */) {
62 ALOGE("%s : should not get here", __func__);
63 return FALSE;
64}
65
66static void jpegr_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
67 jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
68
69 if (num_bytes > static_cast<long>(src->bytes_in_buffer)) {
70 ALOGE("jpegr_skip_input_data - num_bytes > (long)src->bytes_in_buffer");
71 } else {
72 src->next_input_byte += num_bytes;
73 src->bytes_in_buffer -= num_bytes;
74 }
75}
76
77static void jpegr_term_source(j_decompress_ptr /*cinfo*/) {}
78
Ram Mohan0202c122023-08-13 17:22:35 +053079jpegr_source_mgr::jpegr_source_mgr(const uint8_t* ptr, int len)
80 : mBufferPtr(ptr), mBufferLength(len) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +000081 init_source = jpegr_init_source;
82 fill_input_buffer = jpegr_fill_input_buffer;
83 skip_input_data = jpegr_skip_input_data;
84 resync_to_restart = jpeg_resync_to_restart;
85 term_source = jpegr_term_source;
86}
87
88jpegr_source_mgr::~jpegr_source_mgr() {}
89
90static void jpegrerror_exit(j_common_ptr cinfo) {
91 jpegrerror_mgr* err = reinterpret_cast<jpegrerror_mgr*>(cinfo->err);
92 longjmp(err->setjmp_buffer, 1);
93}
94
Ram Mohan0202c122023-08-13 17:22:35 +053095JpegDecoderHelper::JpegDecoderHelper() {}
Dichen Zhang0d12ba82022-10-19 20:44:51 +000096
Ram Mohan0202c122023-08-13 17:22:35 +053097JpegDecoderHelper::~JpegDecoderHelper() {}
Dichen Zhang0d12ba82022-10-19 20:44:51 +000098
Dichen Zhang02dd0592023-02-10 20:16:57 +000099bool JpegDecoderHelper::decompressImage(const void* image, int length, bool decodeToRGBA) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000100 if (image == nullptr || length <= 0) {
101 ALOGE("Image size can not be handled: %d", length);
102 return false;
103 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000104 mResultBuffer.clear();
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000105 mXMPBuffer.clear();
Ram Mohan0202c122023-08-13 17:22:35 +0530106 return decode(image, length, decodeToRGBA);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000107}
108
Dichen Zhang02dd0592023-02-10 20:16:57 +0000109void* JpegDecoderHelper::getDecompressedImagePtr() {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000110 return mResultBuffer.data();
111}
112
Dichen Zhang02dd0592023-02-10 20:16:57 +0000113size_t JpegDecoderHelper::getDecompressedImageSize() {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000114 return mResultBuffer.size();
115}
116
Dichen Zhang02dd0592023-02-10 20:16:57 +0000117void* JpegDecoderHelper::getXMPPtr() {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000118 return mXMPBuffer.data();
119}
120
Dichen Zhang02dd0592023-02-10 20:16:57 +0000121size_t JpegDecoderHelper::getXMPSize() {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000122 return mXMPBuffer.size();
123}
124
Dichen Zhang02dd0592023-02-10 20:16:57 +0000125void* JpegDecoderHelper::getEXIFPtr() {
Dichen Zhangd18bc302022-12-16 20:55:24 +0000126 return mEXIFBuffer.data();
127}
128
Dichen Zhang02dd0592023-02-10 20:16:57 +0000129size_t JpegDecoderHelper::getEXIFSize() {
Dichen Zhangd18bc302022-12-16 20:55:24 +0000130 return mEXIFBuffer.size();
131}
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000132
Nick Deakin0db53ee2023-05-19 17:14:45 -0400133void* JpegDecoderHelper::getICCPtr() {
134 return mICCBuffer.data();
135}
136
137size_t JpegDecoderHelper::getICCSize() {
138 return mICCBuffer.size();
139}
140
Dichen Zhang02dd0592023-02-10 20:16:57 +0000141size_t JpegDecoderHelper::getDecompressedImageWidth() {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400142 return mWidth;
143}
144
Dichen Zhang02dd0592023-02-10 20:16:57 +0000145size_t JpegDecoderHelper::getDecompressedImageHeight() {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400146 return mHeight;
147}
148
Dichen Zhang02dd0592023-02-10 20:16:57 +0000149bool JpegDecoderHelper::decode(const void* image, int length, bool decodeToRGBA) {
Ram Mohane69d9d22023-06-02 17:44:45 +0530150 bool status = true;
Ram Mohan0202c122023-08-13 17:22:35 +0530151 jpeg_decompress_struct cinfo;
152 jpegrerror_mgr myerr;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000153 cinfo.err = jpeg_std_error(&myerr.pub);
154 myerr.pub.error_exit = jpegrerror_exit;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000155 if (setjmp(myerr.setjmp_buffer)) {
156 jpeg_destroy_decompress(&cinfo);
157 return false;
158 }
Ram Mohan0202c122023-08-13 17:22:35 +0530159
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000160 jpeg_create_decompress(&cinfo);
161
Dichen Zhangd18bc302022-12-16 20:55:24 +0000162 jpeg_save_markers(&cinfo, kAPP0Marker, 0xFFFF);
163 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
164 jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000165
Ram Mohan0202c122023-08-13 17:22:35 +0530166 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000167 cinfo.src = &mgr;
Ram Mohan0202c122023-08-13 17:22:35 +0530168 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
169 jpeg_destroy_decompress(&cinfo);
170 return false;
171 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000172
Nick Deakin0db53ee2023-05-19 17:14:45 -0400173 // Save XMP data, EXIF data, and ICC data.
174 // Here we only handle the first XMP / EXIF / ICC package.
Dichen Zhangd18bc302022-12-16 20:55:24 +0000175 // We assume that all packages are starting with two bytes marker (eg FF E1 for EXIF package),
176 // two bytes of package length which is stored in marker->original_length, and the real data
Nick Deakin0db53ee2023-05-19 17:14:45 -0400177 // which is stored in marker->data.
Dichen Zhangd18bc302022-12-16 20:55:24 +0000178 bool exifAppears = false;
179 bool xmpAppears = false;
Nick Deakin0db53ee2023-05-19 17:14:45 -0400180 bool iccAppears = false;
Dichen Zhangd18bc302022-12-16 20:55:24 +0000181 for (jpeg_marker_struct* marker = cinfo.marker_list;
Ram Mohan0202c122023-08-13 17:22:35 +0530182 marker && !(exifAppears && xmpAppears && iccAppears); marker = marker->next) {
Nick Deakin0db53ee2023-05-19 17:14:45 -0400183 if (marker->marker != kAPP1Marker && marker->marker != kAPP2Marker) {
Dichen Zhangd18bc302022-12-16 20:55:24 +0000184 continue;
185 }
Dichen Zhangd18bc302022-12-16 20:55:24 +0000186 const unsigned int len = marker->data_length;
Ram Mohan0202c122023-08-13 17:22:35 +0530187 if (!xmpAppears && len > kXmpNameSpace.size() &&
188 !strncmp(reinterpret_cast<const char*>(marker->data), kXmpNameSpace.c_str(),
Dichen Zhangd18bc302022-12-16 20:55:24 +0000189 kXmpNameSpace.size())) {
Ram Mohan0202c122023-08-13 17:22:35 +0530190 mXMPBuffer.resize(len + 1, 0);
Dichen Zhangd18bc302022-12-16 20:55:24 +0000191 memcpy(static_cast<void*>(mXMPBuffer.data()), marker->data, len);
192 xmpAppears = true;
Ram Mohan0202c122023-08-13 17:22:35 +0530193 } else if (!exifAppears && len > kExifIdCode.size() &&
194 !strncmp(reinterpret_cast<const char*>(marker->data), kExifIdCode.c_str(),
Dichen Zhangd18bc302022-12-16 20:55:24 +0000195 kExifIdCode.size())) {
196 mEXIFBuffer.resize(len, 0);
197 memcpy(static_cast<void*>(mEXIFBuffer.data()), marker->data, len);
198 exifAppears = true;
Ram Mohan0202c122023-08-13 17:22:35 +0530199 } else if (!iccAppears && len > sizeof(kICCSig) &&
Nick Deakin0db53ee2023-05-19 17:14:45 -0400200 !memcmp(marker->data, kICCSig, sizeof(kICCSig))) {
201 mICCBuffer.resize(len, 0);
202 memcpy(static_cast<void*>(mICCBuffer.data()), marker->data, len);
203 iccAppears = true;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000204 }
205 }
206
Ram Mohan0202c122023-08-13 17:22:35 +0530207 mWidth = cinfo.image_width;
208 mHeight = cinfo.image_height;
209 if (mWidth > kMaxWidth || mHeight > kMaxHeight) {
Ram Mohane69d9d22023-06-02 17:44:45 +0530210 status = false;
211 goto CleanUp;
Ram Mohand136b8a2023-06-02 09:06:40 +0530212 }
213
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000214 if (decodeToRGBA) {
Dichen Zhangf63125c2023-07-31 22:27:27 +0000215 // The primary image is expected to be yuv420 sampling
Ram Mohan0202c122023-08-13 17:22:35 +0530216 if (cinfo.jpeg_color_space != JCS_YCbCr) {
217 status = false;
218 ALOGE("%s: decodeToRGBA unexpected jpeg color space ", __func__);
219 goto CleanUp;
220 }
221 if (cinfo.comp_info[0].h_samp_factor != 2 || cinfo.comp_info[0].v_samp_factor != 2 ||
222 cinfo.comp_info[1].h_samp_factor != 1 || cinfo.comp_info[1].v_samp_factor != 1 ||
223 cinfo.comp_info[2].h_samp_factor != 1 || cinfo.comp_info[2].v_samp_factor != 1) {
224 status = false;
225 ALOGE("%s: decodeToRGBA unexpected primary image sub-sampling", __func__);
Ram Mohane69d9d22023-06-02 17:44:45 +0530226 goto CleanUp;
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000227 }
228 // 4 bytes per pixel
229 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 4);
230 cinfo.out_color_space = JCS_EXT_RGBA;
231 } else {
232 if (cinfo.jpeg_color_space == JCS_YCbCr) {
Ram Mohan0202c122023-08-13 17:22:35 +0530233 if (cinfo.comp_info[0].h_samp_factor != 2 || cinfo.comp_info[0].v_samp_factor != 2 ||
234 cinfo.comp_info[1].h_samp_factor != 1 || cinfo.comp_info[1].v_samp_factor != 1 ||
235 cinfo.comp_info[2].h_samp_factor != 1 || cinfo.comp_info[2].v_samp_factor != 1) {
Ram Mohane69d9d22023-06-02 17:44:45 +0530236 status = false;
Nick Deakin0db53ee2023-05-19 17:14:45 -0400237 ALOGE("%s: decoding to YUV only supports 4:2:0 subsampling", __func__);
Ram Mohane69d9d22023-06-02 17:44:45 +0530238 goto CleanUp;
Ram Mohan2838bec2023-05-19 02:46:49 +0530239 }
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000240 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 3 / 2, 0);
241 } else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
242 mResultBuffer.resize(cinfo.image_width * cinfo.image_height, 0);
Dichen Zhangf63125c2023-07-31 22:27:27 +0000243 } else {
244 status = false;
245 ALOGE("%s: decodeToYUV unexpected jpeg color space", __func__);
246 goto CleanUp;
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000247 }
248 cinfo.out_color_space = cinfo.jpeg_color_space;
249 cinfo.raw_data_out = TRUE;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000250 }
251
Ram Mohan1790bb52023-06-11 07:07:27 +0530252 cinfo.dct_method = JDCT_ISLOW;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000253 jpeg_start_decompress(&cinfo);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000254 if (!decompress(&cinfo, static_cast<const uint8_t*>(mResultBuffer.data()),
Ram Mohan0202c122023-08-13 17:22:35 +0530255 cinfo.jpeg_color_space == JCS_GRAYSCALE)) {
Ram Mohane69d9d22023-06-02 17:44:45 +0530256 status = false;
257 goto CleanUp;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000258 }
259
Ram Mohane69d9d22023-06-02 17:44:45 +0530260CleanUp:
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000261 jpeg_finish_decompress(&cinfo);
262 jpeg_destroy_decompress(&cinfo);
263
Ram Mohane69d9d22023-06-02 17:44:45 +0530264 return status;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000265}
266
Dichen Zhang02dd0592023-02-10 20:16:57 +0000267bool JpegDecoderHelper::decompress(jpeg_decompress_struct* cinfo, const uint8_t* dest,
Ram Mohan0202c122023-08-13 17:22:35 +0530268 bool isSingleChannel) {
269 return isSingleChannel
270 ? decompressSingleChannel(cinfo, dest)
271 : ((cinfo->out_color_space == JCS_EXT_RGBA) ? decompressRGBA(cinfo, dest)
272 : decompressYUV(cinfo, dest));
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000273}
274
Ram Mohan0202c122023-08-13 17:22:35 +0530275bool JpegDecoderHelper::getCompressedImageParameters(const void* image, int length, size_t* pWidth,
276 size_t* pHeight, std::vector<uint8_t>* iccData,
277 std::vector<uint8_t>* exifData) {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000278 jpeg_decompress_struct cinfo;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000279 jpegrerror_mgr myerr;
280 cinfo.err = jpeg_std_error(&myerr.pub);
281 myerr.pub.error_exit = jpegrerror_exit;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000282 if (setjmp(myerr.setjmp_buffer)) {
283 jpeg_destroy_decompress(&cinfo);
284 return false;
285 }
286 jpeg_create_decompress(&cinfo);
287
Dichen Zhangd18bc302022-12-16 20:55:24 +0000288 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
289 jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000290
Ram Mohan0202c122023-08-13 17:22:35 +0530291 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000292 cinfo.src = &mgr;
293 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
294 jpeg_destroy_decompress(&cinfo);
295 return false;
296 }
297
Nick Deakin0db53ee2023-05-19 17:14:45 -0400298 if (pWidth != nullptr) {
299 *pWidth = cinfo.image_width;
300 }
301 if (pHeight != nullptr) {
302 *pHeight = cinfo.image_height;
303 }
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000304
Fyodor Kyslove00916d2023-01-16 22:12:02 +0000305 if (iccData != nullptr) {
Ram Mohan0202c122023-08-13 17:22:35 +0530306 for (jpeg_marker_struct* marker = cinfo.marker_list; marker; marker = marker->next) {
Fyodor Kyslove00916d2023-01-16 22:12:02 +0000307 if (marker->marker != kAPP2Marker) {
308 continue;
309 }
310 if (marker->data_length <= kICCMarkerHeaderSize ||
311 memcmp(marker->data, kICCSig, sizeof(kICCSig)) != 0) {
312 continue;
313 }
314
Nick Deakin0db53ee2023-05-19 17:14:45 -0400315 iccData->insert(iccData->end(), marker->data, marker->data + marker->data_length);
Fyodor Kyslove00916d2023-01-16 22:12:02 +0000316 }
317 }
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000318
Fyodor Kyslova90ee002023-01-10 23:41:41 +0000319 if (exifData != nullptr) {
320 bool exifAppears = false;
321 for (jpeg_marker_struct* marker = cinfo.marker_list; marker && !exifAppears;
322 marker = marker->next) {
323 if (marker->marker != kAPP1Marker) {
324 continue;
325 }
326
327 const unsigned int len = marker->data_length;
328 if (len >= kExifIdCode.size() &&
329 !strncmp(reinterpret_cast<const char*>(marker->data), kExifIdCode.c_str(),
330 kExifIdCode.size())) {
331 exifData->resize(len, 0);
332 memcpy(static_cast<void*>(exifData->data()), marker->data, len);
333 exifAppears = true;
334 }
335 }
336 }
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000337
338 jpeg_destroy_decompress(&cinfo);
339 return true;
340}
341
Dichen Zhang02dd0592023-02-10 20:16:57 +0000342bool JpegDecoderHelper::decompressRGBA(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
Ram Mohan0202c122023-08-13 17:22:35 +0530343 JSAMPLE* out = (JSAMPLE*)dest;
344
345 while (cinfo->output_scanline < cinfo->image_height) {
346 if (1 != jpeg_read_scanlines(cinfo, &out, 1)) return false;
347 out += cinfo->image_width * 4;
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000348 }
Ram Mohan0202c122023-08-13 17:22:35 +0530349 return true;
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000350}
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000351
Dichen Zhang02dd0592023-02-10 20:16:57 +0000352bool JpegDecoderHelper::decompressYUV(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000353 JSAMPROW y[kCompressBatchSize];
354 JSAMPROW cb[kCompressBatchSize / 2];
355 JSAMPROW cr[kCompressBatchSize / 2];
Ram Mohan0202c122023-08-13 17:22:35 +0530356 JSAMPARRAY planes[3]{y, cb, cr};
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000357
358 size_t y_plane_size = cinfo->image_width * cinfo->image_height;
359 size_t uv_plane_size = y_plane_size / 4;
360 uint8_t* y_plane = const_cast<uint8_t*>(dest);
361 uint8_t* u_plane = const_cast<uint8_t*>(dest + y_plane_size);
362 uint8_t* v_plane = const_cast<uint8_t*>(dest + y_plane_size + uv_plane_size);
Ram Mohane69d9d22023-06-02 17:44:45 +0530363 std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000364 memset(empty.get(), 0, cinfo->image_width);
365
Ram Mohan2838bec2023-05-19 02:46:49 +0530366 const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
367 bool is_width_aligned = (aligned_width == cinfo->image_width);
368 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
369 uint8_t* y_plane_intrm = nullptr;
370 uint8_t* u_plane_intrm = nullptr;
371 uint8_t* v_plane_intrm = nullptr;
372 JSAMPROW y_intrm[kCompressBatchSize];
373 JSAMPROW cb_intrm[kCompressBatchSize / 2];
374 JSAMPROW cr_intrm[kCompressBatchSize / 2];
Ram Mohan0202c122023-08-13 17:22:35 +0530375 JSAMPARRAY planes_intrm[3]{y_intrm, cb_intrm, cr_intrm};
Ram Mohan2838bec2023-05-19 02:46:49 +0530376 if (!is_width_aligned) {
377 size_t mcu_row_size = aligned_width * kCompressBatchSize * 3 / 2;
378 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
379 y_plane_intrm = buffer_intrm.get();
380 u_plane_intrm = y_plane_intrm + (aligned_width * kCompressBatchSize);
381 v_plane_intrm = u_plane_intrm + (aligned_width * kCompressBatchSize) / 4;
382 for (int i = 0; i < kCompressBatchSize; ++i) {
383 y_intrm[i] = y_plane_intrm + i * aligned_width;
384 }
385 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
386 int offset_intrm = i * (aligned_width / 2);
387 cb_intrm[i] = u_plane_intrm + offset_intrm;
388 cr_intrm[i] = v_plane_intrm + offset_intrm;
389 }
390 }
391
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000392 while (cinfo->output_scanline < cinfo->image_height) {
393 for (int i = 0; i < kCompressBatchSize; ++i) {
394 size_t scanline = cinfo->output_scanline + i;
395 if (scanline < cinfo->image_height) {
396 y[i] = y_plane + scanline * cinfo->image_width;
397 } else {
398 y[i] = empty.get();
399 }
400 }
401 // cb, cr only have half scanlines
402 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
403 size_t scanline = cinfo->output_scanline / 2 + i;
404 if (scanline < cinfo->image_height / 2) {
405 int offset = scanline * (cinfo->image_width / 2);
406 cb[i] = u_plane + offset;
407 cr[i] = v_plane + offset;
408 } else {
409 cb[i] = cr[i] = empty.get();
410 }
411 }
412
Ram Mohan2838bec2023-05-19 02:46:49 +0530413 int processed = jpeg_read_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
414 kCompressBatchSize);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000415 if (processed != kCompressBatchSize) {
416 ALOGE("Number of processed lines does not equal input lines.");
417 return false;
418 }
Ram Mohan2838bec2023-05-19 02:46:49 +0530419 if (!is_width_aligned) {
420 for (int i = 0; i < kCompressBatchSize; ++i) {
421 memcpy(y[i], y_intrm[i], cinfo->image_width);
422 }
423 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
424 memcpy(cb[i], cb_intrm[i], cinfo->image_width / 2);
425 memcpy(cr[i], cr_intrm[i], cinfo->image_width / 2);
426 }
427 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000428 }
429 return true;
430}
431
Ram Mohan0202c122023-08-13 17:22:35 +0530432bool JpegDecoderHelper::decompressSingleChannel(jpeg_decompress_struct* cinfo,
433 const uint8_t* dest) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000434 JSAMPROW y[kCompressBatchSize];
Ram Mohan0202c122023-08-13 17:22:35 +0530435 JSAMPARRAY planes[1]{y};
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000436
437 uint8_t* y_plane = const_cast<uint8_t*>(dest);
Ram Mohane69d9d22023-06-02 17:44:45 +0530438 std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000439 memset(empty.get(), 0, cinfo->image_width);
440
Ram Mohan2838bec2023-05-19 02:46:49 +0530441 int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
442 bool is_width_aligned = (aligned_width == cinfo->image_width);
443 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
444 uint8_t* y_plane_intrm = nullptr;
445 JSAMPROW y_intrm[kCompressBatchSize];
Ram Mohan0202c122023-08-13 17:22:35 +0530446 JSAMPARRAY planes_intrm[1]{y_intrm};
Ram Mohan2838bec2023-05-19 02:46:49 +0530447 if (!is_width_aligned) {
448 size_t mcu_row_size = aligned_width * kCompressBatchSize;
449 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
450 y_plane_intrm = buffer_intrm.get();
451 for (int i = 0; i < kCompressBatchSize; ++i) {
452 y_intrm[i] = y_plane_intrm + i * aligned_width;
453 }
454 }
455
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000456 while (cinfo->output_scanline < cinfo->image_height) {
457 for (int i = 0; i < kCompressBatchSize; ++i) {
458 size_t scanline = cinfo->output_scanline + i;
459 if (scanline < cinfo->image_height) {
460 y[i] = y_plane + scanline * cinfo->image_width;
461 } else {
462 y[i] = empty.get();
463 }
464 }
465
Ram Mohan2838bec2023-05-19 02:46:49 +0530466 int processed = jpeg_read_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
467 kCompressBatchSize);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000468 if (processed != kCompressBatchSize / 2) {
469 ALOGE("Number of processed lines does not equal input lines.");
470 return false;
471 }
Ram Mohan2838bec2023-05-19 02:46:49 +0530472 if (!is_width_aligned) {
473 for (int i = 0; i < kCompressBatchSize; ++i) {
474 memcpy(y[i], y_intrm[i], cinfo->image_width);
475 }
476 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000477 }
478 return true;
479}
480
Ram Mohan0202c122023-08-13 17:22:35 +0530481} // namespace android::ultrahdr