blob: fac90c503d24836ac5cb5abccb659337c0ddf2df [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
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400216 mWidth = cinfo.image_width;
217 mHeight = cinfo.image_height;
218
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000219 if (decodeToRGBA) {
220 if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
221 // We don't intend to support decoding grayscale to RGBA
222 return false;
223 }
224 // 4 bytes per pixel
225 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 4);
226 cinfo.out_color_space = JCS_EXT_RGBA;
227 } else {
228 if (cinfo.jpeg_color_space == JCS_YCbCr) {
Ram Mohan2838bec2023-05-19 02:46:49 +0530229 if (cinfo.comp_info[0].h_samp_factor != 2 ||
230 cinfo.comp_info[1].h_samp_factor != 1 ||
231 cinfo.comp_info[2].h_samp_factor != 1 ||
232 cinfo.comp_info[0].v_samp_factor != 2 ||
233 cinfo.comp_info[1].v_samp_factor != 1 ||
234 cinfo.comp_info[2].v_samp_factor != 1) {
235 return false;
236 }
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000237 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 3 / 2, 0);
238 } else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
239 mResultBuffer.resize(cinfo.image_width * cinfo.image_height, 0);
240 }
241 cinfo.out_color_space = cinfo.jpeg_color_space;
242 cinfo.raw_data_out = TRUE;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000243 }
244
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000245 cinfo.dct_method = JDCT_IFAST;
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000246
247 jpeg_start_decompress(&cinfo);
248
249 if (!decompress(&cinfo, static_cast<const uint8_t*>(mResultBuffer.data()),
250 cinfo.jpeg_color_space == JCS_GRAYSCALE)) {
251 return false;
252 }
253
254 jpeg_finish_decompress(&cinfo);
255 jpeg_destroy_decompress(&cinfo);
256
257 return true;
258}
259
Dichen Zhang02dd0592023-02-10 20:16:57 +0000260bool JpegDecoderHelper::decompress(jpeg_decompress_struct* cinfo, const uint8_t* dest,
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000261 bool isSingleChannel) {
262 if (isSingleChannel) {
263 return decompressSingleChannel(cinfo, dest);
264 }
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000265 if (cinfo->out_color_space == JCS_EXT_RGBA)
266 return decompressRGBA(cinfo, dest);
267 else
268 return decompressYUV(cinfo, dest);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000269}
270
Dichen Zhang02dd0592023-02-10 20:16:57 +0000271bool JpegDecoderHelper::getCompressedImageParameters(const void* image, int length,
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000272 size_t *pWidth, size_t *pHeight,
Fyodor Kyslova90ee002023-01-10 23:41:41 +0000273 std::vector<uint8_t> *iccData , std::vector<uint8_t> *exifData) {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000274 jpeg_decompress_struct cinfo;
275 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
276 jpegrerror_mgr myerr;
277 cinfo.err = jpeg_std_error(&myerr.pub);
278 myerr.pub.error_exit = jpegrerror_exit;
279
280 if (setjmp(myerr.setjmp_buffer)) {
281 jpeg_destroy_decompress(&cinfo);
282 return false;
283 }
284 jpeg_create_decompress(&cinfo);
285
Dichen Zhangd18bc302022-12-16 20:55:24 +0000286 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
287 jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000288
289 cinfo.src = &mgr;
290 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
291 jpeg_destroy_decompress(&cinfo);
292 return false;
293 }
294
295 *pWidth = cinfo.image_width;
296 *pHeight = cinfo.image_height;
297
Fyodor Kyslove00916d2023-01-16 22:12:02 +0000298 if (iccData != nullptr) {
299 for (jpeg_marker_struct* marker = cinfo.marker_list; marker;
300 marker = marker->next) {
301 if (marker->marker != kAPP2Marker) {
302 continue;
303 }
304 if (marker->data_length <= kICCMarkerHeaderSize ||
305 memcmp(marker->data, kICCSig, sizeof(kICCSig)) != 0) {
306 continue;
307 }
308
309 const unsigned int len = marker->data_length - kICCMarkerHeaderSize;
310 const uint8_t *src = marker->data + kICCMarkerHeaderSize;
311 iccData->insert(iccData->end(), src, src+len);
312 }
313 }
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000314
Fyodor Kyslova90ee002023-01-10 23:41:41 +0000315 if (exifData != nullptr) {
316 bool exifAppears = false;
317 for (jpeg_marker_struct* marker = cinfo.marker_list; marker && !exifAppears;
318 marker = marker->next) {
319 if (marker->marker != kAPP1Marker) {
320 continue;
321 }
322
323 const unsigned int len = marker->data_length;
324 if (len >= kExifIdCode.size() &&
325 !strncmp(reinterpret_cast<const char*>(marker->data), kExifIdCode.c_str(),
326 kExifIdCode.size())) {
327 exifData->resize(len, 0);
328 memcpy(static_cast<void*>(exifData->data()), marker->data, len);
329 exifAppears = true;
330 }
331 }
332 }
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000333
334 jpeg_destroy_decompress(&cinfo);
335 return true;
336}
337
Dichen Zhang02dd0592023-02-10 20:16:57 +0000338bool JpegDecoderHelper::decompressRGBA(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
Fyodor Kyslovea9180f2023-01-06 01:11:43 +0000339 JSAMPLE* decodeDst = (JSAMPLE*) dest;
340 uint32_t lines = 0;
341 // TODO: use batches for more effectiveness
342 while (lines < cinfo->image_height) {
343 uint32_t ret = jpeg_read_scanlines(cinfo, &decodeDst, 1);
344 if (ret == 0) {
345 break;
346 }
347 decodeDst += cinfo->image_width * 4;
348 lines++;
349 }
350 return lines == cinfo->image_height;
351}
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000352
Dichen Zhang02dd0592023-02-10 20:16:57 +0000353bool JpegDecoderHelper::decompressYUV(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000354 JSAMPROW y[kCompressBatchSize];
355 JSAMPROW cb[kCompressBatchSize / 2];
356 JSAMPROW cr[kCompressBatchSize / 2];
357 JSAMPARRAY planes[3] {y, cb, cr};
358
359 size_t y_plane_size = cinfo->image_width * cinfo->image_height;
360 size_t uv_plane_size = y_plane_size / 4;
361 uint8_t* y_plane = const_cast<uint8_t*>(dest);
362 uint8_t* u_plane = const_cast<uint8_t*>(dest + y_plane_size);
363 uint8_t* v_plane = const_cast<uint8_t*>(dest + y_plane_size + uv_plane_size);
364 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
365 memset(empty.get(), 0, cinfo->image_width);
366
Ram Mohan2838bec2023-05-19 02:46:49 +0530367 const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
368 bool is_width_aligned = (aligned_width == cinfo->image_width);
369 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
370 uint8_t* y_plane_intrm = nullptr;
371 uint8_t* u_plane_intrm = nullptr;
372 uint8_t* v_plane_intrm = nullptr;
373 JSAMPROW y_intrm[kCompressBatchSize];
374 JSAMPROW cb_intrm[kCompressBatchSize / 2];
375 JSAMPROW cr_intrm[kCompressBatchSize / 2];
376 JSAMPARRAY planes_intrm[3] {y_intrm, cb_intrm, cr_intrm};
377 if (!is_width_aligned) {
378 size_t mcu_row_size = aligned_width * kCompressBatchSize * 3 / 2;
379 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
380 y_plane_intrm = buffer_intrm.get();
381 u_plane_intrm = y_plane_intrm + (aligned_width * kCompressBatchSize);
382 v_plane_intrm = u_plane_intrm + (aligned_width * kCompressBatchSize) / 4;
383 for (int i = 0; i < kCompressBatchSize; ++i) {
384 y_intrm[i] = y_plane_intrm + i * aligned_width;
385 }
386 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
387 int offset_intrm = i * (aligned_width / 2);
388 cb_intrm[i] = u_plane_intrm + offset_intrm;
389 cr_intrm[i] = v_plane_intrm + offset_intrm;
390 }
391 }
392
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000393 while (cinfo->output_scanline < cinfo->image_height) {
394 for (int i = 0; i < kCompressBatchSize; ++i) {
395 size_t scanline = cinfo->output_scanline + i;
396 if (scanline < cinfo->image_height) {
397 y[i] = y_plane + scanline * cinfo->image_width;
398 } else {
399 y[i] = empty.get();
400 }
401 }
402 // cb, cr only have half scanlines
403 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
404 size_t scanline = cinfo->output_scanline / 2 + i;
405 if (scanline < cinfo->image_height / 2) {
406 int offset = scanline * (cinfo->image_width / 2);
407 cb[i] = u_plane + offset;
408 cr[i] = v_plane + offset;
409 } else {
410 cb[i] = cr[i] = empty.get();
411 }
412 }
413
Ram Mohan2838bec2023-05-19 02:46:49 +0530414 int processed = jpeg_read_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
415 kCompressBatchSize);
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000416 if (processed != kCompressBatchSize) {
417 ALOGE("Number of processed lines does not equal input lines.");
418 return false;
419 }
Ram Mohan2838bec2023-05-19 02:46:49 +0530420 if (!is_width_aligned) {
421 for (int i = 0; i < kCompressBatchSize; ++i) {
422 memcpy(y[i], y_intrm[i], cinfo->image_width);
423 }
424 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
425 memcpy(cb[i], cb_intrm[i], cinfo->image_width / 2);
426 memcpy(cr[i], cr_intrm[i], cinfo->image_width / 2);
427 }
428 }
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000429 }
430 return true;
431}
432
Dichen Zhang02dd0592023-02-10 20:16:57 +0000433bool JpegDecoderHelper::decompressSingleChannel(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000434 JSAMPROW y[kCompressBatchSize];
435 JSAMPARRAY planes[1] {y};
436
437 uint8_t* y_plane = const_cast<uint8_t*>(dest);
438 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
439 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];
446 JSAMPARRAY planes_intrm[1] {y_intrm};
447 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
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000481} // namespace ultrahdr