blob: 2a9bc9ac1e3a69047950eeb7c2890a6747d544de [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 Mohan2838bec2023-05-19 02:46:49 +053029#define ALIGNM(x, m) ((((x) + ((m) - 1)) / (m)) * (m))
30
Dichen Zhangd18bc302022-12-16 20:55:24 +000031const uint32_t kAPP0Marker = JPEG_APP0; // JFIF
32const uint32_t kAPP1Marker = JPEG_APP0 + 1; // EXIF, XMP
33const uint32_t kAPP2Marker = JPEG_APP0 + 2; // ICC
34
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
79jpegr_source_mgr::jpegr_source_mgr(const uint8_t* ptr, int len) :
80 mBufferPtr(ptr), mBufferLength(len) {
81 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
Dichen Zhang02dd0592023-02-10 20:16:57 +000095JpegDecoderHelper::JpegDecoderHelper() {
Dichen Zhangd18bc302022-12-16 20:55:24 +000096 mExifPos = 0;
Dichen Zhang0d12ba82022-10-19 20:44:51 +000097}
98
Dichen Zhang02dd0592023-02-10 20:16:57 +000099JpegDecoderHelper::~JpegDecoderHelper() {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000100}
101
Dichen Zhang02dd0592023-02-10 20:16:57 +0000102bool JpegDecoderHelper::decompressImage(const void* image, int length, bool decodeToRGBA) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000103 if (image == nullptr || length <= 0) {
104 ALOGE("Image size can not be handled: %d", length);
105 return false;
106 }
107
108 mResultBuffer.clear();
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000109 mXMPBuffer.clear();
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000110 if (!decode(image, length, decodeToRGBA)) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000111 return false;
112 }
113
114 return true;
115}
116
Dichen Zhang02dd0592023-02-10 20:16:57 +0000117void* JpegDecoderHelper::getDecompressedImagePtr() {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000118 return mResultBuffer.data();
119}
120
Dichen Zhang02dd0592023-02-10 20:16:57 +0000121size_t JpegDecoderHelper::getDecompressedImageSize() {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000122 return mResultBuffer.size();
123}
124
Dichen Zhang02dd0592023-02-10 20:16:57 +0000125void* JpegDecoderHelper::getXMPPtr() {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000126 return mXMPBuffer.data();
127}
128
Dichen Zhang02dd0592023-02-10 20:16:57 +0000129size_t JpegDecoderHelper::getXMPSize() {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000130 return mXMPBuffer.size();
131}
132
Dichen Zhang02dd0592023-02-10 20:16:57 +0000133void* JpegDecoderHelper::getEXIFPtr() {
Dichen Zhangd18bc302022-12-16 20:55:24 +0000134 return mEXIFBuffer.data();
135}
136
Dichen Zhang02dd0592023-02-10 20:16:57 +0000137size_t JpegDecoderHelper::getEXIFSize() {
Dichen Zhangd18bc302022-12-16 20:55:24 +0000138 return mEXIFBuffer.size();
139}
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000140
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) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000150 jpeg_decompress_struct cinfo;
151 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
152 jpegrerror_mgr myerr;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000153
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000154 cinfo.err = jpeg_std_error(&myerr.pub);
155 myerr.pub.error_exit = jpegrerror_exit;
156
157 if (setjmp(myerr.setjmp_buffer)) {
158 jpeg_destroy_decompress(&cinfo);
159 return false;
160 }
161 jpeg_create_decompress(&cinfo);
162
Dichen Zhangd18bc302022-12-16 20:55:24 +0000163 jpeg_save_markers(&cinfo, kAPP0Marker, 0xFFFF);
164 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
165 jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000166
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000167 cinfo.src = &mgr;
168 jpeg_read_header(&cinfo, TRUE);
169
Dichen Zhangd18bc302022-12-16 20:55:24 +0000170 // Save XMP data and EXIF data.
171 // Here we only handle the first XMP / EXIF package.
172 // The parameter pos is used for capturing start offset of EXIF, which is hacky, but working...
173 // We assume that all packages are starting with two bytes marker (eg FF E1 for EXIF package),
174 // two bytes of package length which is stored in marker->original_length, and the real data
175 // which is stored in marker->data. The pos is adding up all previous package lengths (
176 // 4 bytes marker and length, marker->original_length) before EXIF appears. Note that here we
177 // we are using marker->original_length instead of marker->data_length because in case the real
178 // package length is larger than the limitation, jpeg-turbo will only copy the data within the
179 // limitation (represented by data_length) and this may vary from original_length / real offset.
180 // A better solution is making jpeg_marker_struct holding the offset, but currently it doesn't.
181 bool exifAppears = false;
182 bool xmpAppears = false;
183 size_t pos = 2; // position after SOI
184 for (jpeg_marker_struct* marker = cinfo.marker_list;
185 marker && !(exifAppears && xmpAppears);
186 marker = marker->next) {
187
188 pos += 4;
189 pos += marker->original_length;
190
191 if (marker->marker != kAPP1Marker) {
192 continue;
193 }
194
195 const unsigned int len = marker->data_length;
196 if (!xmpAppears &&
197 len > kXmpNameSpace.size() &&
198 !strncmp(reinterpret_cast<const char*>(marker->data),
199 kXmpNameSpace.c_str(),
200 kXmpNameSpace.size())) {
201 mXMPBuffer.resize(len+1, 0);
202 memcpy(static_cast<void*>(mXMPBuffer.data()), marker->data, len);
203 xmpAppears = true;
204 } else if (!exifAppears &&
205 len > kExifIdCode.size() &&
206 !strncmp(reinterpret_cast<const char*>(marker->data),
207 kExifIdCode.c_str(),
208 kExifIdCode.size())) {
209 mEXIFBuffer.resize(len, 0);
210 memcpy(static_cast<void*>(mEXIFBuffer.data()), marker->data, len);
211 exifAppears = true;
212 mExifPos = pos - marker->original_length;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000213 }
214 }
215
Ram Mohand136b8a2023-06-02 09:06:40 +0530216 if (cinfo.image_width > kMaxWidth || cinfo.image_height > kMaxHeight) {
217 // constraint on max width and max height is only due to alloc constraints
218 // tune these values basing on the target device
219 return false;
220 }
221
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400222 mWidth = cinfo.image_width;
223 mHeight = cinfo.image_height;
224
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000225 if (decodeToRGBA) {
226 if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
227 // We don't intend to support decoding grayscale to RGBA
228 return false;
229 }
230 // 4 bytes per pixel
231 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 4);
232 cinfo.out_color_space = JCS_EXT_RGBA;
233 } else {
234 if (cinfo.jpeg_color_space == JCS_YCbCr) {
Ram Mohan2838bec2023-05-19 02:46:49 +0530235 if (cinfo.comp_info[0].h_samp_factor != 2 ||
236 cinfo.comp_info[1].h_samp_factor != 1 ||
237 cinfo.comp_info[2].h_samp_factor != 1 ||
238 cinfo.comp_info[0].v_samp_factor != 2 ||
239 cinfo.comp_info[1].v_samp_factor != 1 ||
240 cinfo.comp_info[2].v_samp_factor != 1) {
241 return false;
242 }
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000243 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 3 / 2, 0);
244 } else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
245 mResultBuffer.resize(cinfo.image_width * cinfo.image_height, 0);
246 }
247 cinfo.out_color_space = cinfo.jpeg_color_space;
248 cinfo.raw_data_out = TRUE;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000249 }
250
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000251 cinfo.dct_method = JDCT_IFAST;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000252
253 jpeg_start_decompress(&cinfo);
254
255 if (!decompress(&cinfo, static_cast<const uint8_t*>(mResultBuffer.data()),
256 cinfo.jpeg_color_space == JCS_GRAYSCALE)) {
257 return false;
258 }
259
260 jpeg_finish_decompress(&cinfo);
261 jpeg_destroy_decompress(&cinfo);
262
263 return true;
264}
265
Dichen Zhang02dd0592023-02-10 20:16:57 +0000266bool JpegDecoderHelper::decompress(jpeg_decompress_struct* cinfo, const uint8_t* dest,
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000267 bool isSingleChannel) {
268 if (isSingleChannel) {
269 return decompressSingleChannel(cinfo, dest);
270 }
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000271 if (cinfo->out_color_space == JCS_EXT_RGBA)
272 return decompressRGBA(cinfo, dest);
273 else
274 return decompressYUV(cinfo, dest);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000275}
276
Dichen Zhang02dd0592023-02-10 20:16:57 +0000277bool JpegDecoderHelper::getCompressedImageParameters(const void* image, int length,
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000278 size_t *pWidth, size_t *pHeight,
Fyodor Kyslova90ee002023-01-10 23:41:41 +0000279 std::vector<uint8_t> *iccData , std::vector<uint8_t> *exifData) {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000280 jpeg_decompress_struct cinfo;
281 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
282 jpegrerror_mgr myerr;
283 cinfo.err = jpeg_std_error(&myerr.pub);
284 myerr.pub.error_exit = jpegrerror_exit;
285
286 if (setjmp(myerr.setjmp_buffer)) {
287 jpeg_destroy_decompress(&cinfo);
288 return false;
289 }
290 jpeg_create_decompress(&cinfo);
291
Dichen Zhangd18bc302022-12-16 20:55:24 +0000292 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
293 jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000294
295 cinfo.src = &mgr;
296 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
297 jpeg_destroy_decompress(&cinfo);
298 return false;
299 }
300
301 *pWidth = cinfo.image_width;
302 *pHeight = cinfo.image_height;
303
Fyodor Kyslove00916d2023-01-16 22:12:02 +0000304 if (iccData != nullptr) {
305 for (jpeg_marker_struct* marker = cinfo.marker_list; marker;
306 marker = marker->next) {
307 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
315 const unsigned int len = marker->data_length - kICCMarkerHeaderSize;
316 const uint8_t *src = marker->data + kICCMarkerHeaderSize;
317 iccData->insert(iccData->end(), src, src+len);
318 }
319 }
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000320
Fyodor Kyslova90ee002023-01-10 23:41:41 +0000321 if (exifData != nullptr) {
322 bool exifAppears = false;
323 for (jpeg_marker_struct* marker = cinfo.marker_list; marker && !exifAppears;
324 marker = marker->next) {
325 if (marker->marker != kAPP1Marker) {
326 continue;
327 }
328
329 const unsigned int len = marker->data_length;
330 if (len >= kExifIdCode.size() &&
331 !strncmp(reinterpret_cast<const char*>(marker->data), kExifIdCode.c_str(),
332 kExifIdCode.size())) {
333 exifData->resize(len, 0);
334 memcpy(static_cast<void*>(exifData->data()), marker->data, len);
335 exifAppears = true;
336 }
337 }
338 }
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000339
340 jpeg_destroy_decompress(&cinfo);
341 return true;
342}
343
Dichen Zhang02dd0592023-02-10 20:16:57 +0000344bool JpegDecoderHelper::decompressRGBA(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000345 JSAMPLE* decodeDst = (JSAMPLE*) dest;
346 uint32_t lines = 0;
347 // TODO: use batches for more effectiveness
348 while (lines < cinfo->image_height) {
349 uint32_t ret = jpeg_read_scanlines(cinfo, &decodeDst, 1);
350 if (ret == 0) {
351 break;
352 }
353 decodeDst += cinfo->image_width * 4;
354 lines++;
355 }
356 return lines == cinfo->image_height;
357}
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000358
Dichen Zhang02dd0592023-02-10 20:16:57 +0000359bool JpegDecoderHelper::decompressYUV(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000360 JSAMPROW y[kCompressBatchSize];
361 JSAMPROW cb[kCompressBatchSize / 2];
362 JSAMPROW cr[kCompressBatchSize / 2];
363 JSAMPARRAY planes[3] {y, cb, cr};
364
365 size_t y_plane_size = cinfo->image_width * cinfo->image_height;
366 size_t uv_plane_size = y_plane_size / 4;
367 uint8_t* y_plane = const_cast<uint8_t*>(dest);
368 uint8_t* u_plane = const_cast<uint8_t*>(dest + y_plane_size);
369 uint8_t* v_plane = const_cast<uint8_t*>(dest + y_plane_size + uv_plane_size);
370 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
371 memset(empty.get(), 0, cinfo->image_width);
372
Ram Mohan2838bec2023-05-19 02:46:49 +0530373 const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
374 bool is_width_aligned = (aligned_width == cinfo->image_width);
375 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
376 uint8_t* y_plane_intrm = nullptr;
377 uint8_t* u_plane_intrm = nullptr;
378 uint8_t* v_plane_intrm = nullptr;
379 JSAMPROW y_intrm[kCompressBatchSize];
380 JSAMPROW cb_intrm[kCompressBatchSize / 2];
381 JSAMPROW cr_intrm[kCompressBatchSize / 2];
382 JSAMPARRAY planes_intrm[3] {y_intrm, cb_intrm, cr_intrm};
383 if (!is_width_aligned) {
384 size_t mcu_row_size = aligned_width * kCompressBatchSize * 3 / 2;
385 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
386 y_plane_intrm = buffer_intrm.get();
387 u_plane_intrm = y_plane_intrm + (aligned_width * kCompressBatchSize);
388 v_plane_intrm = u_plane_intrm + (aligned_width * kCompressBatchSize) / 4;
389 for (int i = 0; i < kCompressBatchSize; ++i) {
390 y_intrm[i] = y_plane_intrm + i * aligned_width;
391 }
392 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
393 int offset_intrm = i * (aligned_width / 2);
394 cb_intrm[i] = u_plane_intrm + offset_intrm;
395 cr_intrm[i] = v_plane_intrm + offset_intrm;
396 }
397 }
398
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000399 while (cinfo->output_scanline < cinfo->image_height) {
400 for (int i = 0; i < kCompressBatchSize; ++i) {
401 size_t scanline = cinfo->output_scanline + i;
402 if (scanline < cinfo->image_height) {
403 y[i] = y_plane + scanline * cinfo->image_width;
404 } else {
405 y[i] = empty.get();
406 }
407 }
408 // cb, cr only have half scanlines
409 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
410 size_t scanline = cinfo->output_scanline / 2 + i;
411 if (scanline < cinfo->image_height / 2) {
412 int offset = scanline * (cinfo->image_width / 2);
413 cb[i] = u_plane + offset;
414 cr[i] = v_plane + offset;
415 } else {
416 cb[i] = cr[i] = empty.get();
417 }
418 }
419
Ram Mohan2838bec2023-05-19 02:46:49 +0530420 int processed = jpeg_read_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
421 kCompressBatchSize);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000422 if (processed != kCompressBatchSize) {
423 ALOGE("Number of processed lines does not equal input lines.");
424 return false;
425 }
Ram Mohan2838bec2023-05-19 02:46:49 +0530426 if (!is_width_aligned) {
427 for (int i = 0; i < kCompressBatchSize; ++i) {
428 memcpy(y[i], y_intrm[i], cinfo->image_width);
429 }
430 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
431 memcpy(cb[i], cb_intrm[i], cinfo->image_width / 2);
432 memcpy(cr[i], cr_intrm[i], cinfo->image_width / 2);
433 }
434 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000435 }
436 return true;
437}
438
Dichen Zhang02dd0592023-02-10 20:16:57 +0000439bool JpegDecoderHelper::decompressSingleChannel(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000440 JSAMPROW y[kCompressBatchSize];
441 JSAMPARRAY planes[1] {y};
442
443 uint8_t* y_plane = const_cast<uint8_t*>(dest);
444 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
445 memset(empty.get(), 0, cinfo->image_width);
446
Ram Mohan2838bec2023-05-19 02:46:49 +0530447 int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
448 bool is_width_aligned = (aligned_width == cinfo->image_width);
449 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
450 uint8_t* y_plane_intrm = nullptr;
451 JSAMPROW y_intrm[kCompressBatchSize];
452 JSAMPARRAY planes_intrm[1] {y_intrm};
453 if (!is_width_aligned) {
454 size_t mcu_row_size = aligned_width * kCompressBatchSize;
455 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
456 y_plane_intrm = buffer_intrm.get();
457 for (int i = 0; i < kCompressBatchSize; ++i) {
458 y_intrm[i] = y_plane_intrm + i * aligned_width;
459 }
460 }
461
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000462 while (cinfo->output_scanline < cinfo->image_height) {
463 for (int i = 0; i < kCompressBatchSize; ++i) {
464 size_t scanline = cinfo->output_scanline + i;
465 if (scanline < cinfo->image_height) {
466 y[i] = y_plane + scanline * cinfo->image_width;
467 } else {
468 y[i] = empty.get();
469 }
470 }
471
Ram Mohan2838bec2023-05-19 02:46:49 +0530472 int processed = jpeg_read_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
473 kCompressBatchSize);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000474 if (processed != kCompressBatchSize / 2) {
475 ALOGE("Number of processed lines does not equal input lines.");
476 return false;
477 }
Ram Mohan2838bec2023-05-19 02:46:49 +0530478 if (!is_width_aligned) {
479 for (int i = 0; i < kCompressBatchSize; ++i) {
480 memcpy(y[i], y_intrm[i], cinfo->image_width);
481 }
482 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000483 }
484 return true;
485}
486
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000487} // namespace ultrahdr