1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ultrahdr/jpegdecoderhelper.h>
#include <utils/Log.h>
#include <errno.h>
#include <setjmp.h>
#include <string>
using namespace std;
namespace android::ultrahdr {
#define ALIGNM(x, m) ((((x) + ((m)-1)) / (m)) * (m))
const uint32_t kAPP0Marker = JPEG_APP0; // JFIF
const uint32_t kAPP1Marker = JPEG_APP0 + 1; // EXIF, XMP
const uint32_t kAPP2Marker = JPEG_APP0 + 2; // ICC
constexpr uint32_t kICCMarkerHeaderSize = 14;
constexpr uint8_t kICCSig[] = {
'I', 'C', 'C', '_', 'P', 'R', 'O', 'F', 'I', 'L', 'E', '\0',
};
constexpr uint8_t kXmpNameSpace[] = {
'h', 't', 't', 'p', ':', '/', '/', 'n', 's', '.', 'a', 'd', 'o', 'b', 'e',
'.', 'c', 'o', 'm', '/', 'x', 'a', 'p', '/', '1', '.', '0', '/', '\0',
};
constexpr uint8_t kExifIdCode[] = {
'E', 'x', 'i', 'f', '\0', '\0',
};
struct jpegr_source_mgr : jpeg_source_mgr {
jpegr_source_mgr(const uint8_t* ptr, int len);
~jpegr_source_mgr();
const uint8_t* mBufferPtr;
size_t mBufferLength;
};
struct jpegrerror_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
static void jpegr_init_source(j_decompress_ptr cinfo) {
jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
src->next_input_byte = static_cast<const JOCTET*>(src->mBufferPtr);
src->bytes_in_buffer = src->mBufferLength;
}
static boolean jpegr_fill_input_buffer(j_decompress_ptr /* cinfo */) {
ALOGE("%s : should not get here", __func__);
return FALSE;
}
static void jpegr_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
if (num_bytes > static_cast<long>(src->bytes_in_buffer)) {
ALOGE("jpegr_skip_input_data - num_bytes > (long)src->bytes_in_buffer");
} else {
src->next_input_byte += num_bytes;
src->bytes_in_buffer -= num_bytes;
}
}
static void jpegr_term_source(j_decompress_ptr /*cinfo*/) {}
jpegr_source_mgr::jpegr_source_mgr(const uint8_t* ptr, int len)
: mBufferPtr(ptr), mBufferLength(len) {
init_source = jpegr_init_source;
fill_input_buffer = jpegr_fill_input_buffer;
skip_input_data = jpegr_skip_input_data;
resync_to_restart = jpeg_resync_to_restart;
term_source = jpegr_term_source;
}
jpegr_source_mgr::~jpegr_source_mgr() {}
static void jpegrerror_exit(j_common_ptr cinfo) {
jpegrerror_mgr* err = reinterpret_cast<jpegrerror_mgr*>(cinfo->err);
longjmp(err->setjmp_buffer, 1);
}
JpegDecoderHelper::JpegDecoderHelper() {}
JpegDecoderHelper::~JpegDecoderHelper() {}
bool JpegDecoderHelper::decompressImage(const void* image, int length, bool decodeToRGBA) {
if (image == nullptr || length <= 0) {
ALOGE("Image size can not be handled: %d", length);
return false;
}
mResultBuffer.clear();
mXMPBuffer.clear();
return decode(image, length, decodeToRGBA);
}
void* JpegDecoderHelper::getDecompressedImagePtr() {
return mResultBuffer.data();
}
size_t JpegDecoderHelper::getDecompressedImageSize() {
return mResultBuffer.size();
}
void* JpegDecoderHelper::getXMPPtr() {
return mXMPBuffer.data();
}
size_t JpegDecoderHelper::getXMPSize() {
return mXMPBuffer.size();
}
void* JpegDecoderHelper::getEXIFPtr() {
return mEXIFBuffer.data();
}
size_t JpegDecoderHelper::getEXIFSize() {
return mEXIFBuffer.size();
}
void* JpegDecoderHelper::getICCPtr() {
return mICCBuffer.data();
}
size_t JpegDecoderHelper::getICCSize() {
return mICCBuffer.size();
}
size_t JpegDecoderHelper::getDecompressedImageWidth() {
return mWidth;
}
size_t JpegDecoderHelper::getDecompressedImageHeight() {
return mHeight;
}
// Here we only handle the first EXIF package, and in theary EXIF (or JFIF) must be the first
// in the image file.
// We assume that all packages are starting with two bytes marker (eg FF E1 for EXIF package),
// two bytes of package length which is stored in marker->original_length, and the real data
// which is stored in marker->data.
bool JpegDecoderHelper::extractEXIF(const void* image, int length) {
jpeg_decompress_struct cinfo;
jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
jpegrerror_mgr myerr;
cinfo.err = jpeg_std_error(&myerr.pub);
myerr.pub.error_exit = jpegrerror_exit;
if (setjmp(myerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
return false;
}
jpeg_create_decompress(&cinfo);
jpeg_save_markers(&cinfo, kAPP0Marker, 0xFFFF);
jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
cinfo.src = &mgr;
jpeg_read_header(&cinfo, TRUE);
size_t pos = 2; // position after SOI
for (jpeg_marker_struct* marker = cinfo.marker_list;
marker;
marker = marker->next) {
pos += 4;
pos += marker->original_length;
if (marker->marker != kAPP1Marker) {
continue;
}
const unsigned int len = marker->data_length;
if (len > sizeof(kExifIdCode) &&
!memcmp(marker->data, kExifIdCode, sizeof(kExifIdCode))) {
mEXIFBuffer.resize(len, 0);
memcpy(static_cast<void*>(mEXIFBuffer.data()), marker->data, len);
mExifPos = pos - marker->original_length;
break;
}
}
jpeg_destroy_decompress(&cinfo);
return true;
}
bool JpegDecoderHelper::decode(const void* image, int length, bool decodeToRGBA) {
bool status = true;
jpeg_decompress_struct cinfo;
jpegrerror_mgr myerr;
cinfo.err = jpeg_std_error(&myerr.pub);
myerr.pub.error_exit = jpegrerror_exit;
if (setjmp(myerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
return false;
}
jpeg_create_decompress(&cinfo);
jpeg_save_markers(&cinfo, kAPP0Marker, 0xFFFF);
jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
cinfo.src = &mgr;
if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
jpeg_destroy_decompress(&cinfo);
return false;
}
// Save XMP data, EXIF data, and ICC data.
// Here we only handle the first XMP / EXIF / ICC package.
// We assume that all packages are starting with two bytes marker (eg FF E1 for EXIF package),
// two bytes of package length which is stored in marker->original_length, and the real data
// which is stored in marker->data.
bool exifAppears = false;
bool xmpAppears = false;
bool iccAppears = false;
size_t pos = 2; // position after SOI
for (jpeg_marker_struct* marker = cinfo.marker_list;
marker && !(exifAppears && xmpAppears && iccAppears);
marker = marker->next) {
pos += 4;
pos += marker->original_length;
if (marker->marker != kAPP1Marker && marker->marker != kAPP2Marker) {
continue;
}
const unsigned int len = marker->data_length;
if (!xmpAppears &&
len > sizeof(kXmpNameSpace) &&
!memcmp(marker->data, kXmpNameSpace, sizeof(kXmpNameSpace))) {
mXMPBuffer.resize(len+1, 0);
memcpy(static_cast<void*>(mXMPBuffer.data()), marker->data, len);
xmpAppears = true;
} else if (!exifAppears &&
len > sizeof(kExifIdCode) &&
!memcmp(marker->data, kExifIdCode, sizeof(kExifIdCode))) {
mEXIFBuffer.resize(len, 0);
memcpy(static_cast<void*>(mEXIFBuffer.data()), marker->data, len);
exifAppears = true;
mExifPos = pos - marker->original_length;
} else if (!iccAppears &&
len > sizeof(kICCSig) &&
!memcmp(marker->data, kICCSig, sizeof(kICCSig))) {
mICCBuffer.resize(len, 0);
memcpy(static_cast<void*>(mICCBuffer.data()), marker->data, len);
iccAppears = true;
}
}
mWidth = cinfo.image_width;
mHeight = cinfo.image_height;
if (mWidth > kMaxWidth || mHeight > kMaxHeight) {
status = false;
goto CleanUp;
}
if (decodeToRGBA) {
// The primary image is expected to be yuv420 sampling
if (cinfo.jpeg_color_space != JCS_YCbCr) {
status = false;
ALOGE("%s: decodeToRGBA unexpected jpeg color space ", __func__);
goto CleanUp;
}
if (cinfo.comp_info[0].h_samp_factor != 2 || cinfo.comp_info[0].v_samp_factor != 2 ||
cinfo.comp_info[1].h_samp_factor != 1 || cinfo.comp_info[1].v_samp_factor != 1 ||
cinfo.comp_info[2].h_samp_factor != 1 || cinfo.comp_info[2].v_samp_factor != 1) {
status = false;
ALOGE("%s: decodeToRGBA unexpected primary image sub-sampling", __func__);
goto CleanUp;
}
// 4 bytes per pixel
mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 4);
cinfo.out_color_space = JCS_EXT_RGBA;
} else {
if (cinfo.jpeg_color_space == JCS_YCbCr) {
if (cinfo.comp_info[0].h_samp_factor != 2 || cinfo.comp_info[0].v_samp_factor != 2 ||
cinfo.comp_info[1].h_samp_factor != 1 || cinfo.comp_info[1].v_samp_factor != 1 ||
cinfo.comp_info[2].h_samp_factor != 1 || cinfo.comp_info[2].v_samp_factor != 1) {
status = false;
ALOGE("%s: decoding to YUV only supports 4:2:0 subsampling", __func__);
goto CleanUp;
}
mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 3 / 2, 0);
} else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
mResultBuffer.resize(cinfo.image_width * cinfo.image_height, 0);
} else {
status = false;
ALOGE("%s: decodeToYUV unexpected jpeg color space", __func__);
goto CleanUp;
}
cinfo.out_color_space = cinfo.jpeg_color_space;
cinfo.raw_data_out = TRUE;
}
cinfo.dct_method = JDCT_ISLOW;
jpeg_start_decompress(&cinfo);
if (!decompress(&cinfo, static_cast<const uint8_t*>(mResultBuffer.data()),
cinfo.jpeg_color_space == JCS_GRAYSCALE)) {
status = false;
goto CleanUp;
}
CleanUp:
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return status;
}
bool JpegDecoderHelper::decompress(jpeg_decompress_struct* cinfo, const uint8_t* dest,
bool isSingleChannel) {
return isSingleChannel
? decompressSingleChannel(cinfo, dest)
: ((cinfo->out_color_space == JCS_EXT_RGBA) ? decompressRGBA(cinfo, dest)
: decompressYUV(cinfo, dest));
}
bool JpegDecoderHelper::getCompressedImageParameters(const void* image, int length, size_t* pWidth,
size_t* pHeight, std::vector<uint8_t>* iccData,
std::vector<uint8_t>* exifData) {
jpeg_decompress_struct cinfo;
jpegrerror_mgr myerr;
cinfo.err = jpeg_std_error(&myerr.pub);
myerr.pub.error_exit = jpegrerror_exit;
if (setjmp(myerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
return false;
}
jpeg_create_decompress(&cinfo);
jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
cinfo.src = &mgr;
if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
jpeg_destroy_decompress(&cinfo);
return false;
}
if (pWidth != nullptr) {
*pWidth = cinfo.image_width;
}
if (pHeight != nullptr) {
*pHeight = cinfo.image_height;
}
if (iccData != nullptr) {
for (jpeg_marker_struct* marker = cinfo.marker_list; marker; marker = marker->next) {
if (marker->marker != kAPP2Marker) {
continue;
}
if (marker->data_length <= kICCMarkerHeaderSize ||
memcmp(marker->data, kICCSig, sizeof(kICCSig)) != 0) {
continue;
}
iccData->insert(iccData->end(), marker->data, marker->data + marker->data_length);
}
}
if (exifData != nullptr) {
bool exifAppears = false;
for (jpeg_marker_struct* marker = cinfo.marker_list; marker && !exifAppears;
marker = marker->next) {
if (marker->marker != kAPP1Marker) {
continue;
}
const unsigned int len = marker->data_length;
if (len >= sizeof(kExifIdCode) &&
!memcmp(marker->data, kExifIdCode, sizeof(kExifIdCode))) {
exifData->resize(len, 0);
memcpy(static_cast<void*>(exifData->data()), marker->data, len);
exifAppears = true;
}
}
}
jpeg_destroy_decompress(&cinfo);
return true;
}
bool JpegDecoderHelper::decompressRGBA(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
JSAMPLE* out = (JSAMPLE*)dest;
while (cinfo->output_scanline < cinfo->image_height) {
if (1 != jpeg_read_scanlines(cinfo, &out, 1)) return false;
out += cinfo->image_width * 4;
}
return true;
}
bool JpegDecoderHelper::decompressYUV(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
JSAMPROW y[kCompressBatchSize];
JSAMPROW cb[kCompressBatchSize / 2];
JSAMPROW cr[kCompressBatchSize / 2];
JSAMPARRAY planes[3]{y, cb, cr};
size_t y_plane_size = cinfo->image_width * cinfo->image_height;
size_t uv_plane_size = y_plane_size / 4;
uint8_t* y_plane = const_cast<uint8_t*>(dest);
uint8_t* u_plane = const_cast<uint8_t*>(dest + y_plane_size);
uint8_t* v_plane = const_cast<uint8_t*>(dest + y_plane_size + uv_plane_size);
std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
memset(empty.get(), 0, cinfo->image_width);
const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
bool is_width_aligned = (aligned_width == cinfo->image_width);
std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
uint8_t* y_plane_intrm = nullptr;
uint8_t* u_plane_intrm = nullptr;
uint8_t* v_plane_intrm = nullptr;
JSAMPROW y_intrm[kCompressBatchSize];
JSAMPROW cb_intrm[kCompressBatchSize / 2];
JSAMPROW cr_intrm[kCompressBatchSize / 2];
JSAMPARRAY planes_intrm[3]{y_intrm, cb_intrm, cr_intrm};
if (!is_width_aligned) {
size_t mcu_row_size = aligned_width * kCompressBatchSize * 3 / 2;
buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
y_plane_intrm = buffer_intrm.get();
u_plane_intrm = y_plane_intrm + (aligned_width * kCompressBatchSize);
v_plane_intrm = u_plane_intrm + (aligned_width * kCompressBatchSize) / 4;
for (int i = 0; i < kCompressBatchSize; ++i) {
y_intrm[i] = y_plane_intrm + i * aligned_width;
}
for (int i = 0; i < kCompressBatchSize / 2; ++i) {
int offset_intrm = i * (aligned_width / 2);
cb_intrm[i] = u_plane_intrm + offset_intrm;
cr_intrm[i] = v_plane_intrm + offset_intrm;
}
}
while (cinfo->output_scanline < cinfo->image_height) {
for (int i = 0; i < kCompressBatchSize; ++i) {
size_t scanline = cinfo->output_scanline + i;
if (scanline < cinfo->image_height) {
y[i] = y_plane + scanline * cinfo->image_width;
} else {
y[i] = empty.get();
}
}
// cb, cr only have half scanlines
for (int i = 0; i < kCompressBatchSize / 2; ++i) {
size_t scanline = cinfo->output_scanline / 2 + i;
if (scanline < cinfo->image_height / 2) {
int offset = scanline * (cinfo->image_width / 2);
cb[i] = u_plane + offset;
cr[i] = v_plane + offset;
} else {
cb[i] = cr[i] = empty.get();
}
}
int processed = jpeg_read_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
kCompressBatchSize);
if (processed != kCompressBatchSize) {
ALOGE("Number of processed lines does not equal input lines.");
return false;
}
if (!is_width_aligned) {
for (int i = 0; i < kCompressBatchSize; ++i) {
memcpy(y[i], y_intrm[i], cinfo->image_width);
}
for (int i = 0; i < kCompressBatchSize / 2; ++i) {
memcpy(cb[i], cb_intrm[i], cinfo->image_width / 2);
memcpy(cr[i], cr_intrm[i], cinfo->image_width / 2);
}
}
}
return true;
}
bool JpegDecoderHelper::decompressSingleChannel(jpeg_decompress_struct* cinfo,
const uint8_t* dest) {
JSAMPROW y[kCompressBatchSize];
JSAMPARRAY planes[1]{y};
uint8_t* y_plane = const_cast<uint8_t*>(dest);
std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
memset(empty.get(), 0, cinfo->image_width);
int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
bool is_width_aligned = (aligned_width == cinfo->image_width);
std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
uint8_t* y_plane_intrm = nullptr;
JSAMPROW y_intrm[kCompressBatchSize];
JSAMPARRAY planes_intrm[1]{y_intrm};
if (!is_width_aligned) {
size_t mcu_row_size = aligned_width * kCompressBatchSize;
buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
y_plane_intrm = buffer_intrm.get();
for (int i = 0; i < kCompressBatchSize; ++i) {
y_intrm[i] = y_plane_intrm + i * aligned_width;
}
}
while (cinfo->output_scanline < cinfo->image_height) {
for (int i = 0; i < kCompressBatchSize; ++i) {
size_t scanline = cinfo->output_scanline + i;
if (scanline < cinfo->image_height) {
y[i] = y_plane + scanline * cinfo->image_width;
} else {
y[i] = empty.get();
}
}
int processed = jpeg_read_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
kCompressBatchSize);
if (processed != kCompressBatchSize / 2) {
ALOGE("Number of processed lines does not equal input lines.");
return false;
}
if (!is_width_aligned) {
for (int i = 0; i < kCompressBatchSize; ++i) {
memcpy(y[i], y_intrm[i], cinfo->image_width);
}
}
}
return true;
}
} // namespace android::ultrahdr
|