blob: 2e7940cc2cf38ec0dbb0fb928a6b883dfc0db4f0 [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
Fyodor Kyslove00916d2023-01-16 22:12:02 +000035constexpr uint32_t kICCMarkerHeaderSize = 14;
36constexpr uint8_t kICCSig[] = {
37 'I', 'C', 'C', '_', 'P', 'R', 'O', 'F', 'I', 'L', 'E', '\0',
38};
Dichen Zhang6b3cd9a2023-08-18 01:48:10 +000039constexpr uint8_t kXmpNameSpace[] = {
40 'h', 't', 't', 'p', ':', '/', '/', 'n', 's', '.', 'a', 'd', 'o', 'b', 'e',
41 '.', 'c', 'o', 'm', '/', 'x', 'a', 'p', '/', '1', '.', '0', '/', '\0',
42};
43constexpr uint8_t kExifIdCode[] = {
44 'E', 'x', 'i', 'f', '\0', '\0',
45};
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000046
Dichen Zhang0d12ba82022-10-19 20:44:51 +000047struct jpegr_source_mgr : jpeg_source_mgr {
48 jpegr_source_mgr(const uint8_t* ptr, int len);
49 ~jpegr_source_mgr();
50
51 const uint8_t* mBufferPtr;
52 size_t mBufferLength;
53};
54
55struct jpegrerror_mgr {
56 struct jpeg_error_mgr pub;
57 jmp_buf setjmp_buffer;
58};
59
60static void jpegr_init_source(j_decompress_ptr cinfo) {
61 jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
62 src->next_input_byte = static_cast<const JOCTET*>(src->mBufferPtr);
63 src->bytes_in_buffer = src->mBufferLength;
64}
65
66static boolean jpegr_fill_input_buffer(j_decompress_ptr /* cinfo */) {
67 ALOGE("%s : should not get here", __func__);
68 return FALSE;
69}
70
71static void jpegr_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
72 jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
73
74 if (num_bytes > static_cast<long>(src->bytes_in_buffer)) {
75 ALOGE("jpegr_skip_input_data - num_bytes > (long)src->bytes_in_buffer");
76 } else {
77 src->next_input_byte += num_bytes;
78 src->bytes_in_buffer -= num_bytes;
79 }
80}
81
82static void jpegr_term_source(j_decompress_ptr /*cinfo*/) {}
83
Ram Mohan0202c122023-08-13 17:22:35 +053084jpegr_source_mgr::jpegr_source_mgr(const uint8_t* ptr, int len)
85 : mBufferPtr(ptr), mBufferLength(len) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +000086 init_source = jpegr_init_source;
87 fill_input_buffer = jpegr_fill_input_buffer;
88 skip_input_data = jpegr_skip_input_data;
89 resync_to_restart = jpeg_resync_to_restart;
90 term_source = jpegr_term_source;
91}
92
93jpegr_source_mgr::~jpegr_source_mgr() {}
94
95static void jpegrerror_exit(j_common_ptr cinfo) {
96 jpegrerror_mgr* err = reinterpret_cast<jpegrerror_mgr*>(cinfo->err);
97 longjmp(err->setjmp_buffer, 1);
98}
99
Ram Mohan0202c122023-08-13 17:22:35 +0530100JpegDecoderHelper::JpegDecoderHelper() {}
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000101
Ram Mohan0202c122023-08-13 17:22:35 +0530102JpegDecoderHelper::~JpegDecoderHelper() {}
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000103
Dichen Zhang02dd0592023-02-10 20:16:57 +0000104bool JpegDecoderHelper::decompressImage(const void* image, int length, bool decodeToRGBA) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000105 if (image == nullptr || length <= 0) {
106 ALOGE("Image size can not be handled: %d", length);
107 return false;
108 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000109 mResultBuffer.clear();
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000110 mXMPBuffer.clear();
Ram Mohan0202c122023-08-13 17:22:35 +0530111 return decode(image, length, decodeToRGBA);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000112}
113
Dichen Zhang02dd0592023-02-10 20:16:57 +0000114void* JpegDecoderHelper::getDecompressedImagePtr() {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000115 return mResultBuffer.data();
116}
117
Dichen Zhang02dd0592023-02-10 20:16:57 +0000118size_t JpegDecoderHelper::getDecompressedImageSize() {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000119 return mResultBuffer.size();
120}
121
Dichen Zhang02dd0592023-02-10 20:16:57 +0000122void* JpegDecoderHelper::getXMPPtr() {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000123 return mXMPBuffer.data();
124}
125
Dichen Zhang02dd0592023-02-10 20:16:57 +0000126size_t JpegDecoderHelper::getXMPSize() {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000127 return mXMPBuffer.size();
128}
129
Dichen Zhang02dd0592023-02-10 20:16:57 +0000130void* JpegDecoderHelper::getEXIFPtr() {
Dichen Zhangd18bc302022-12-16 20:55:24 +0000131 return mEXIFBuffer.data();
132}
133
Dichen Zhang02dd0592023-02-10 20:16:57 +0000134size_t JpegDecoderHelper::getEXIFSize() {
Dichen Zhangd18bc302022-12-16 20:55:24 +0000135 return mEXIFBuffer.size();
136}
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000137
Nick Deakin0db53ee2023-05-19 17:14:45 -0400138void* JpegDecoderHelper::getICCPtr() {
139 return mICCBuffer.data();
140}
141
142size_t JpegDecoderHelper::getICCSize() {
143 return mICCBuffer.size();
144}
145
Dichen Zhang02dd0592023-02-10 20:16:57 +0000146size_t JpegDecoderHelper::getDecompressedImageWidth() {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400147 return mWidth;
148}
149
Dichen Zhang02dd0592023-02-10 20:16:57 +0000150size_t JpegDecoderHelper::getDecompressedImageHeight() {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400151 return mHeight;
152}
153
Dichen Zhang6b3cd9a2023-08-18 01:48:10 +0000154// Here we only handle the first EXIF package, and in theary EXIF (or JFIF) must be the first
155// in the image file.
156// We assume that all packages are starting with two bytes marker (eg FF E1 for EXIF package),
157// two bytes of package length which is stored in marker->original_length, and the real data
158// which is stored in marker->data.
159bool JpegDecoderHelper::extractEXIF(const void* image, int length) {
160 jpeg_decompress_struct cinfo;
161 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
162 jpegrerror_mgr myerr;
163
164 cinfo.err = jpeg_std_error(&myerr.pub);
165 myerr.pub.error_exit = jpegrerror_exit;
166
167 if (setjmp(myerr.setjmp_buffer)) {
168 jpeg_destroy_decompress(&cinfo);
169 return false;
170 }
171 jpeg_create_decompress(&cinfo);
172
173 jpeg_save_markers(&cinfo, kAPP0Marker, 0xFFFF);
174 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
175
176 cinfo.src = &mgr;
177 jpeg_read_header(&cinfo, TRUE);
178
179 size_t pos = 2; // position after SOI
180 for (jpeg_marker_struct* marker = cinfo.marker_list;
181 marker;
182 marker = marker->next) {
183
184 pos += 4;
185 pos += marker->original_length;
186
187 if (marker->marker != kAPP1Marker) {
188 continue;
189 }
190
191 const unsigned int len = marker->data_length;
192
193 if (len > sizeof(kExifIdCode) &&
194 !memcmp(marker->data, kExifIdCode, sizeof(kExifIdCode))) {
195 mEXIFBuffer.resize(len, 0);
196 memcpy(static_cast<void*>(mEXIFBuffer.data()), marker->data, len);
197 mExifPos = pos - marker->original_length;
198 break;
199 }
200 }
201
202 jpeg_destroy_decompress(&cinfo);
203 return true;
204}
205
Dichen Zhang02dd0592023-02-10 20:16:57 +0000206bool JpegDecoderHelper::decode(const void* image, int length, bool decodeToRGBA) {
Ram Mohane69d9d22023-06-02 17:44:45 +0530207 bool status = true;
Ram Mohan0202c122023-08-13 17:22:35 +0530208 jpeg_decompress_struct cinfo;
209 jpegrerror_mgr myerr;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000210 cinfo.err = jpeg_std_error(&myerr.pub);
211 myerr.pub.error_exit = jpegrerror_exit;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000212 if (setjmp(myerr.setjmp_buffer)) {
213 jpeg_destroy_decompress(&cinfo);
214 return false;
215 }
Ram Mohan0202c122023-08-13 17:22:35 +0530216
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000217 jpeg_create_decompress(&cinfo);
218
Dichen Zhangd18bc302022-12-16 20:55:24 +0000219 jpeg_save_markers(&cinfo, kAPP0Marker, 0xFFFF);
220 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
221 jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000222
Ram Mohan0202c122023-08-13 17:22:35 +0530223 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000224 cinfo.src = &mgr;
Ram Mohan0202c122023-08-13 17:22:35 +0530225 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
226 jpeg_destroy_decompress(&cinfo);
227 return false;
228 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000229
Nick Deakin0db53ee2023-05-19 17:14:45 -0400230 // Save XMP data, EXIF data, and ICC data.
231 // Here we only handle the first XMP / EXIF / ICC package.
Dichen Zhangd18bc302022-12-16 20:55:24 +0000232 // We assume that all packages are starting with two bytes marker (eg FF E1 for EXIF package),
233 // two bytes of package length which is stored in marker->original_length, and the real data
Nick Deakin0db53ee2023-05-19 17:14:45 -0400234 // which is stored in marker->data.
Dichen Zhangd18bc302022-12-16 20:55:24 +0000235 bool exifAppears = false;
236 bool xmpAppears = false;
Nick Deakin0db53ee2023-05-19 17:14:45 -0400237 bool iccAppears = false;
Dichen Zhang6b3cd9a2023-08-18 01:48:10 +0000238 size_t pos = 2; // position after SOI
Dichen Zhangd18bc302022-12-16 20:55:24 +0000239 for (jpeg_marker_struct* marker = cinfo.marker_list;
Dichen Zhang6b3cd9a2023-08-18 01:48:10 +0000240 marker && !(exifAppears && xmpAppears && iccAppears);
241 marker = marker->next) {
242 pos += 4;
243 pos += marker->original_length;
Nick Deakin0db53ee2023-05-19 17:14:45 -0400244 if (marker->marker != kAPP1Marker && marker->marker != kAPP2Marker) {
Dichen Zhangd18bc302022-12-16 20:55:24 +0000245 continue;
246 }
Dichen Zhangd18bc302022-12-16 20:55:24 +0000247 const unsigned int len = marker->data_length;
Dichen Zhang6b3cd9a2023-08-18 01:48:10 +0000248 if (!xmpAppears &&
249 len > sizeof(kXmpNameSpace) &&
250 !memcmp(marker->data, kXmpNameSpace, sizeof(kXmpNameSpace))) {
251 mXMPBuffer.resize(len+1, 0);
Dichen Zhangd18bc302022-12-16 20:55:24 +0000252 memcpy(static_cast<void*>(mXMPBuffer.data()), marker->data, len);
253 xmpAppears = true;
Dichen Zhang6b3cd9a2023-08-18 01:48:10 +0000254 } else if (!exifAppears &&
255 len > sizeof(kExifIdCode) &&
256 !memcmp(marker->data, kExifIdCode, sizeof(kExifIdCode))) {
Dichen Zhangd18bc302022-12-16 20:55:24 +0000257 mEXIFBuffer.resize(len, 0);
258 memcpy(static_cast<void*>(mEXIFBuffer.data()), marker->data, len);
259 exifAppears = true;
Dichen Zhang6b3cd9a2023-08-18 01:48:10 +0000260 mExifPos = pos - marker->original_length;
261 } else if (!iccAppears &&
262 len > sizeof(kICCSig) &&
Nick Deakin0db53ee2023-05-19 17:14:45 -0400263 !memcmp(marker->data, kICCSig, sizeof(kICCSig))) {
264 mICCBuffer.resize(len, 0);
265 memcpy(static_cast<void*>(mICCBuffer.data()), marker->data, len);
266 iccAppears = true;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000267 }
268 }
269
Ram Mohan0202c122023-08-13 17:22:35 +0530270 mWidth = cinfo.image_width;
271 mHeight = cinfo.image_height;
272 if (mWidth > kMaxWidth || mHeight > kMaxHeight) {
Ram Mohane69d9d22023-06-02 17:44:45 +0530273 status = false;
274 goto CleanUp;
Ram Mohand136b8a2023-06-02 09:06:40 +0530275 }
276
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000277 if (decodeToRGBA) {
Dichen Zhangf63125c2023-07-31 22:27:27 +0000278 // The primary image is expected to be yuv420 sampling
Ram Mohan0202c122023-08-13 17:22:35 +0530279 if (cinfo.jpeg_color_space != JCS_YCbCr) {
280 status = false;
281 ALOGE("%s: decodeToRGBA unexpected jpeg color space ", __func__);
282 goto CleanUp;
283 }
284 if (cinfo.comp_info[0].h_samp_factor != 2 || cinfo.comp_info[0].v_samp_factor != 2 ||
285 cinfo.comp_info[1].h_samp_factor != 1 || cinfo.comp_info[1].v_samp_factor != 1 ||
286 cinfo.comp_info[2].h_samp_factor != 1 || cinfo.comp_info[2].v_samp_factor != 1) {
287 status = false;
288 ALOGE("%s: decodeToRGBA unexpected primary image sub-sampling", __func__);
Ram Mohane69d9d22023-06-02 17:44:45 +0530289 goto CleanUp;
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000290 }
291 // 4 bytes per pixel
292 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 4);
293 cinfo.out_color_space = JCS_EXT_RGBA;
294 } else {
295 if (cinfo.jpeg_color_space == JCS_YCbCr) {
Ram Mohan0202c122023-08-13 17:22:35 +0530296 if (cinfo.comp_info[0].h_samp_factor != 2 || cinfo.comp_info[0].v_samp_factor != 2 ||
297 cinfo.comp_info[1].h_samp_factor != 1 || cinfo.comp_info[1].v_samp_factor != 1 ||
298 cinfo.comp_info[2].h_samp_factor != 1 || cinfo.comp_info[2].v_samp_factor != 1) {
Ram Mohane69d9d22023-06-02 17:44:45 +0530299 status = false;
Nick Deakin0db53ee2023-05-19 17:14:45 -0400300 ALOGE("%s: decoding to YUV only supports 4:2:0 subsampling", __func__);
Ram Mohane69d9d22023-06-02 17:44:45 +0530301 goto CleanUp;
Ram Mohan2838bec2023-05-19 02:46:49 +0530302 }
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000303 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 3 / 2, 0);
304 } else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
305 mResultBuffer.resize(cinfo.image_width * cinfo.image_height, 0);
Dichen Zhangf63125c2023-07-31 22:27:27 +0000306 } else {
307 status = false;
308 ALOGE("%s: decodeToYUV unexpected jpeg color space", __func__);
309 goto CleanUp;
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000310 }
311 cinfo.out_color_space = cinfo.jpeg_color_space;
312 cinfo.raw_data_out = TRUE;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000313 }
314
Ram Mohan1790bb52023-06-11 07:07:27 +0530315 cinfo.dct_method = JDCT_ISLOW;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000316 jpeg_start_decompress(&cinfo);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000317 if (!decompress(&cinfo, static_cast<const uint8_t*>(mResultBuffer.data()),
Ram Mohan0202c122023-08-13 17:22:35 +0530318 cinfo.jpeg_color_space == JCS_GRAYSCALE)) {
Ram Mohane69d9d22023-06-02 17:44:45 +0530319 status = false;
320 goto CleanUp;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000321 }
322
Ram Mohane69d9d22023-06-02 17:44:45 +0530323CleanUp:
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000324 jpeg_finish_decompress(&cinfo);
325 jpeg_destroy_decompress(&cinfo);
326
Ram Mohane69d9d22023-06-02 17:44:45 +0530327 return status;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000328}
329
Dichen Zhang02dd0592023-02-10 20:16:57 +0000330bool JpegDecoderHelper::decompress(jpeg_decompress_struct* cinfo, const uint8_t* dest,
Ram Mohan0202c122023-08-13 17:22:35 +0530331 bool isSingleChannel) {
332 return isSingleChannel
333 ? decompressSingleChannel(cinfo, dest)
334 : ((cinfo->out_color_space == JCS_EXT_RGBA) ? decompressRGBA(cinfo, dest)
335 : decompressYUV(cinfo, dest));
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000336}
337
Ram Mohan0202c122023-08-13 17:22:35 +0530338bool JpegDecoderHelper::getCompressedImageParameters(const void* image, int length, size_t* pWidth,
339 size_t* pHeight, std::vector<uint8_t>* iccData,
340 std::vector<uint8_t>* exifData) {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000341 jpeg_decompress_struct cinfo;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000342 jpegrerror_mgr myerr;
343 cinfo.err = jpeg_std_error(&myerr.pub);
344 myerr.pub.error_exit = jpegrerror_exit;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000345 if (setjmp(myerr.setjmp_buffer)) {
346 jpeg_destroy_decompress(&cinfo);
347 return false;
348 }
349 jpeg_create_decompress(&cinfo);
350
Dichen Zhangd18bc302022-12-16 20:55:24 +0000351 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
352 jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000353
Ram Mohan0202c122023-08-13 17:22:35 +0530354 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000355 cinfo.src = &mgr;
356 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
357 jpeg_destroy_decompress(&cinfo);
358 return false;
359 }
360
Nick Deakin0db53ee2023-05-19 17:14:45 -0400361 if (pWidth != nullptr) {
362 *pWidth = cinfo.image_width;
363 }
364 if (pHeight != nullptr) {
365 *pHeight = cinfo.image_height;
366 }
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000367
Fyodor Kyslove00916d2023-01-16 22:12:02 +0000368 if (iccData != nullptr) {
Ram Mohan0202c122023-08-13 17:22:35 +0530369 for (jpeg_marker_struct* marker = cinfo.marker_list; marker; marker = marker->next) {
Fyodor Kyslove00916d2023-01-16 22:12:02 +0000370 if (marker->marker != kAPP2Marker) {
371 continue;
372 }
373 if (marker->data_length <= kICCMarkerHeaderSize ||
374 memcmp(marker->data, kICCSig, sizeof(kICCSig)) != 0) {
375 continue;
376 }
377
Nick Deakin0db53ee2023-05-19 17:14:45 -0400378 iccData->insert(iccData->end(), marker->data, marker->data + marker->data_length);
Fyodor Kyslove00916d2023-01-16 22:12:02 +0000379 }
380 }
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000381
Fyodor Kyslova90ee002023-01-10 23:41:41 +0000382 if (exifData != nullptr) {
383 bool exifAppears = false;
384 for (jpeg_marker_struct* marker = cinfo.marker_list; marker && !exifAppears;
385 marker = marker->next) {
386 if (marker->marker != kAPP1Marker) {
387 continue;
388 }
389
390 const unsigned int len = marker->data_length;
Dichen Zhang6b3cd9a2023-08-18 01:48:10 +0000391 if (len >= sizeof(kExifIdCode) &&
392 !memcmp(marker->data, kExifIdCode, sizeof(kExifIdCode))) {
Fyodor Kyslova90ee002023-01-10 23:41:41 +0000393 exifData->resize(len, 0);
394 memcpy(static_cast<void*>(exifData->data()), marker->data, len);
395 exifAppears = true;
396 }
397 }
398 }
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000399
400 jpeg_destroy_decompress(&cinfo);
401 return true;
402}
403
Dichen Zhang02dd0592023-02-10 20:16:57 +0000404bool JpegDecoderHelper::decompressRGBA(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
Ram Mohan0202c122023-08-13 17:22:35 +0530405 JSAMPLE* out = (JSAMPLE*)dest;
406
407 while (cinfo->output_scanline < cinfo->image_height) {
408 if (1 != jpeg_read_scanlines(cinfo, &out, 1)) return false;
409 out += cinfo->image_width * 4;
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000410 }
Ram Mohan0202c122023-08-13 17:22:35 +0530411 return true;
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000412}
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000413
Dichen Zhang02dd0592023-02-10 20:16:57 +0000414bool JpegDecoderHelper::decompressYUV(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000415 JSAMPROW y[kCompressBatchSize];
416 JSAMPROW cb[kCompressBatchSize / 2];
417 JSAMPROW cr[kCompressBatchSize / 2];
Ram Mohan0202c122023-08-13 17:22:35 +0530418 JSAMPARRAY planes[3]{y, cb, cr};
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000419
420 size_t y_plane_size = cinfo->image_width * cinfo->image_height;
421 size_t uv_plane_size = y_plane_size / 4;
422 uint8_t* y_plane = const_cast<uint8_t*>(dest);
423 uint8_t* u_plane = const_cast<uint8_t*>(dest + y_plane_size);
424 uint8_t* v_plane = const_cast<uint8_t*>(dest + y_plane_size + uv_plane_size);
Ram Mohane69d9d22023-06-02 17:44:45 +0530425 std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000426 memset(empty.get(), 0, cinfo->image_width);
427
Ram Mohan2838bec2023-05-19 02:46:49 +0530428 const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
429 bool is_width_aligned = (aligned_width == cinfo->image_width);
430 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
431 uint8_t* y_plane_intrm = nullptr;
432 uint8_t* u_plane_intrm = nullptr;
433 uint8_t* v_plane_intrm = nullptr;
434 JSAMPROW y_intrm[kCompressBatchSize];
435 JSAMPROW cb_intrm[kCompressBatchSize / 2];
436 JSAMPROW cr_intrm[kCompressBatchSize / 2];
Ram Mohan0202c122023-08-13 17:22:35 +0530437 JSAMPARRAY planes_intrm[3]{y_intrm, cb_intrm, cr_intrm};
Ram Mohan2838bec2023-05-19 02:46:49 +0530438 if (!is_width_aligned) {
439 size_t mcu_row_size = aligned_width * kCompressBatchSize * 3 / 2;
440 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
441 y_plane_intrm = buffer_intrm.get();
442 u_plane_intrm = y_plane_intrm + (aligned_width * kCompressBatchSize);
443 v_plane_intrm = u_plane_intrm + (aligned_width * kCompressBatchSize) / 4;
444 for (int i = 0; i < kCompressBatchSize; ++i) {
445 y_intrm[i] = y_plane_intrm + i * aligned_width;
446 }
447 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
448 int offset_intrm = i * (aligned_width / 2);
449 cb_intrm[i] = u_plane_intrm + offset_intrm;
450 cr_intrm[i] = v_plane_intrm + offset_intrm;
451 }
452 }
453
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000454 while (cinfo->output_scanline < cinfo->image_height) {
455 for (int i = 0; i < kCompressBatchSize; ++i) {
456 size_t scanline = cinfo->output_scanline + i;
457 if (scanline < cinfo->image_height) {
458 y[i] = y_plane + scanline * cinfo->image_width;
459 } else {
460 y[i] = empty.get();
461 }
462 }
463 // cb, cr only have half scanlines
464 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
465 size_t scanline = cinfo->output_scanline / 2 + i;
466 if (scanline < cinfo->image_height / 2) {
467 int offset = scanline * (cinfo->image_width / 2);
468 cb[i] = u_plane + offset;
469 cr[i] = v_plane + offset;
470 } else {
471 cb[i] = cr[i] = empty.get();
472 }
473 }
474
Ram Mohan2838bec2023-05-19 02:46:49 +0530475 int processed = jpeg_read_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
476 kCompressBatchSize);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000477 if (processed != kCompressBatchSize) {
478 ALOGE("Number of processed lines does not equal input lines.");
479 return false;
480 }
Ram Mohan2838bec2023-05-19 02:46:49 +0530481 if (!is_width_aligned) {
482 for (int i = 0; i < kCompressBatchSize; ++i) {
483 memcpy(y[i], y_intrm[i], cinfo->image_width);
484 }
485 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
486 memcpy(cb[i], cb_intrm[i], cinfo->image_width / 2);
487 memcpy(cr[i], cr_intrm[i], cinfo->image_width / 2);
488 }
489 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000490 }
491 return true;
492}
493
Ram Mohan0202c122023-08-13 17:22:35 +0530494bool JpegDecoderHelper::decompressSingleChannel(jpeg_decompress_struct* cinfo,
495 const uint8_t* dest) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000496 JSAMPROW y[kCompressBatchSize];
Ram Mohan0202c122023-08-13 17:22:35 +0530497 JSAMPARRAY planes[1]{y};
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000498
499 uint8_t* y_plane = const_cast<uint8_t*>(dest);
Ram Mohane69d9d22023-06-02 17:44:45 +0530500 std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000501 memset(empty.get(), 0, cinfo->image_width);
502
Ram Mohan2838bec2023-05-19 02:46:49 +0530503 int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
504 bool is_width_aligned = (aligned_width == cinfo->image_width);
505 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
506 uint8_t* y_plane_intrm = nullptr;
507 JSAMPROW y_intrm[kCompressBatchSize];
Ram Mohan0202c122023-08-13 17:22:35 +0530508 JSAMPARRAY planes_intrm[1]{y_intrm};
Ram Mohan2838bec2023-05-19 02:46:49 +0530509 if (!is_width_aligned) {
510 size_t mcu_row_size = aligned_width * kCompressBatchSize;
511 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
512 y_plane_intrm = buffer_intrm.get();
513 for (int i = 0; i < kCompressBatchSize; ++i) {
514 y_intrm[i] = y_plane_intrm + i * aligned_width;
515 }
516 }
517
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000518 while (cinfo->output_scanline < cinfo->image_height) {
519 for (int i = 0; i < kCompressBatchSize; ++i) {
520 size_t scanline = cinfo->output_scanline + i;
521 if (scanline < cinfo->image_height) {
522 y[i] = y_plane + scanline * cinfo->image_width;
523 } else {
524 y[i] = empty.get();
525 }
526 }
527
Ram Mohan2838bec2023-05-19 02:46:49 +0530528 int processed = jpeg_read_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
529 kCompressBatchSize);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000530 if (processed != kCompressBatchSize / 2) {
531 ALOGE("Number of processed lines does not equal input lines.");
532 return false;
533 }
Ram Mohan2838bec2023-05-19 02:46:49 +0530534 if (!is_width_aligned) {
535 for (int i = 0; i < kCompressBatchSize; ++i) {
536 memcpy(y[i], y_intrm[i], cinfo->image_width);
537 }
538 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000539 }
540 return true;
541}
542
Ram Mohan0202c122023-08-13 17:22:35 +0530543} // namespace android::ultrahdr