Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 17 | #include <cstring> |
| 18 | #include <memory> |
| 19 | #include <vector> |
| 20 | |
Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 21 | #include <ultrahdr/jpegencoderhelper.h> |
Dichen Zhang | b27d06d | 2022-12-14 19:57:50 +0000 | [diff] [blame] | 22 | #include <utils/Log.h> |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 23 | |
Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 24 | namespace android::ultrahdr { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 25 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 26 | // The destination manager that can access |mResultBuffer| in JpegEncoderHelper. |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 27 | struct destination_mgr { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 28 | struct jpeg_destination_mgr mgr; |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 29 | JpegEncoderHelper* encoder; |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 32 | JpegEncoderHelper::JpegEncoderHelper() {} |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 33 | |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 34 | JpegEncoderHelper::~JpegEncoderHelper() {} |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 35 | |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 36 | bool JpegEncoderHelper::compressImage(const uint8_t* yBuffer, const uint8_t* uvBuffer, int width, |
| 37 | int height, int lumaStride, int chromaStride, int quality, |
| 38 | const void* iccBuffer, unsigned int iccSize) { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 39 | mResultBuffer.clear(); |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 40 | if (!encode(yBuffer, uvBuffer, width, height, lumaStride, chromaStride, quality, iccBuffer, |
| 41 | iccSize)) { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 42 | return false; |
| 43 | } |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 44 | ALOGI("Compressed JPEG: %d[%dx%d] -> %zu bytes", (width * height * 12) / 8, width, height, |
| 45 | mResultBuffer.size()); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 46 | return true; |
| 47 | } |
| 48 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 49 | void* JpegEncoderHelper::getCompressedImagePtr() { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 50 | return mResultBuffer.data(); |
| 51 | } |
| 52 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 53 | size_t JpegEncoderHelper::getCompressedImageSize() { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 54 | return mResultBuffer.size(); |
| 55 | } |
| 56 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 57 | void JpegEncoderHelper::initDestination(j_compress_ptr cinfo) { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 58 | destination_mgr* dest = reinterpret_cast<destination_mgr*>(cinfo->dest); |
| 59 | std::vector<JOCTET>& buffer = dest->encoder->mResultBuffer; |
| 60 | buffer.resize(kBlockSize); |
| 61 | dest->mgr.next_output_byte = &buffer[0]; |
| 62 | dest->mgr.free_in_buffer = buffer.size(); |
| 63 | } |
| 64 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 65 | boolean JpegEncoderHelper::emptyOutputBuffer(j_compress_ptr cinfo) { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 66 | destination_mgr* dest = reinterpret_cast<destination_mgr*>(cinfo->dest); |
| 67 | std::vector<JOCTET>& buffer = dest->encoder->mResultBuffer; |
| 68 | size_t oldsize = buffer.size(); |
| 69 | buffer.resize(oldsize + kBlockSize); |
| 70 | dest->mgr.next_output_byte = &buffer[oldsize]; |
| 71 | dest->mgr.free_in_buffer = kBlockSize; |
| 72 | return true; |
| 73 | } |
| 74 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 75 | void JpegEncoderHelper::terminateDestination(j_compress_ptr cinfo) { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 76 | destination_mgr* dest = reinterpret_cast<destination_mgr*>(cinfo->dest); |
| 77 | std::vector<JOCTET>& buffer = dest->encoder->mResultBuffer; |
| 78 | buffer.resize(buffer.size() - dest->mgr.free_in_buffer); |
| 79 | } |
| 80 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 81 | void JpegEncoderHelper::outputErrorMessage(j_common_ptr cinfo) { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 82 | char buffer[JMSG_LENGTH_MAX]; |
| 83 | |
| 84 | /* Create the message */ |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 85 | (*cinfo->err->format_message)(cinfo, buffer); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 86 | ALOGE("%s\n", buffer); |
| 87 | } |
| 88 | |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 89 | bool JpegEncoderHelper::encode(const uint8_t* yBuffer, const uint8_t* uvBuffer, int width, |
| 90 | int height, int lumaStride, int chromaStride, int quality, |
| 91 | const void* iccBuffer, unsigned int iccSize) { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 92 | jpeg_compress_struct cinfo; |
| 93 | jpeg_error_mgr jerr; |
| 94 | |
| 95 | cinfo.err = jpeg_std_error(&jerr); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 96 | cinfo.err->output_message = &outputErrorMessage; |
| 97 | jpeg_create_compress(&cinfo); |
| 98 | setJpegDestination(&cinfo); |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 99 | setJpegCompressStruct(width, height, quality, &cinfo, uvBuffer == nullptr); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 100 | jpeg_start_compress(&cinfo, TRUE); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 101 | if (iccBuffer != nullptr && iccSize > 0) { |
| 102 | jpeg_write_marker(&cinfo, JPEG_APP0 + 2, static_cast<const JOCTET*>(iccBuffer), iccSize); |
| 103 | } |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 104 | bool status = cinfo.num_components == 1 |
| 105 | ? compressY(&cinfo, yBuffer, lumaStride) |
| 106 | : compressYuv(&cinfo, yBuffer, uvBuffer, lumaStride, chromaStride); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 107 | jpeg_finish_compress(&cinfo); |
| 108 | jpeg_destroy_compress(&cinfo); |
Ram Mohan | e69d9d2 | 2023-06-02 17:44:45 +0530 | [diff] [blame] | 109 | |
| 110 | return status; |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 113 | void JpegEncoderHelper::setJpegDestination(jpeg_compress_struct* cinfo) { |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 114 | destination_mgr* dest = static_cast<struct destination_mgr*>( |
| 115 | (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT, |
| 116 | sizeof(destination_mgr))); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 117 | dest->encoder = this; |
| 118 | dest->mgr.init_destination = &initDestination; |
| 119 | dest->mgr.empty_output_buffer = &emptyOutputBuffer; |
| 120 | dest->mgr.term_destination = &terminateDestination; |
| 121 | cinfo->dest = reinterpret_cast<struct jpeg_destination_mgr*>(dest); |
| 122 | } |
| 123 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 124 | void JpegEncoderHelper::setJpegCompressStruct(int width, int height, int quality, |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 125 | jpeg_compress_struct* cinfo, bool isSingleChannel) { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 126 | cinfo->image_width = width; |
| 127 | cinfo->image_height = height; |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 128 | cinfo->input_components = isSingleChannel ? 1 : 3; |
| 129 | cinfo->in_color_space = isSingleChannel ? JCS_GRAYSCALE : JCS_YCbCr; |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 130 | jpeg_set_defaults(cinfo); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 131 | jpeg_set_quality(cinfo, quality, TRUE); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 132 | cinfo->raw_data_in = TRUE; |
Ram Mohan | 1790bb5 | 2023-06-11 07:07:27 +0530 | [diff] [blame] | 133 | cinfo->dct_method = JDCT_ISLOW; |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 134 | cinfo->comp_info[0].h_samp_factor = cinfo->in_color_space == JCS_GRAYSCALE ? 1 : 2; |
| 135 | cinfo->comp_info[0].v_samp_factor = cinfo->in_color_space == JCS_GRAYSCALE ? 1 : 2; |
| 136 | for (int i = 1; i < cinfo->num_components; i++) { |
| 137 | cinfo->comp_info[i].h_samp_factor = 1; |
| 138 | cinfo->comp_info[i].v_samp_factor = 1; |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 139 | } |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 142 | bool JpegEncoderHelper::compressYuv(jpeg_compress_struct* cinfo, const uint8_t* yBuffer, |
| 143 | const uint8_t* uvBuffer, int lumaStride, int chromaStride) { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 144 | JSAMPROW y[kCompressBatchSize]; |
| 145 | JSAMPROW cb[kCompressBatchSize / 2]; |
| 146 | JSAMPROW cr[kCompressBatchSize / 2]; |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 147 | JSAMPARRAY planes[3]{y, cb, cr}; |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 148 | |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 149 | size_t y_plane_size = lumaStride * cinfo->image_height; |
| 150 | size_t u_plane_size = chromaStride * cinfo->image_height / 2; |
| 151 | uint8_t* y_plane = const_cast<uint8_t*>(yBuffer); |
| 152 | uint8_t* u_plane = const_cast<uint8_t*>(uvBuffer); |
| 153 | uint8_t* v_plane = const_cast<uint8_t*>(u_plane + u_plane_size); |
Ram Mohan | e69d9d2 | 2023-06-02 17:44:45 +0530 | [diff] [blame] | 154 | std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 155 | memset(empty.get(), 0, cinfo->image_width); |
| 156 | |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 157 | const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize); |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 158 | const bool need_padding = (lumaStride < aligned_width); |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 159 | std::unique_ptr<uint8_t[]> buffer_intrm = nullptr; |
| 160 | uint8_t* y_plane_intrm = nullptr; |
| 161 | uint8_t* u_plane_intrm = nullptr; |
| 162 | uint8_t* v_plane_intrm = nullptr; |
| 163 | JSAMPROW y_intrm[kCompressBatchSize]; |
| 164 | JSAMPROW cb_intrm[kCompressBatchSize / 2]; |
| 165 | JSAMPROW cr_intrm[kCompressBatchSize / 2]; |
| 166 | JSAMPARRAY planes_intrm[3]{y_intrm, cb_intrm, cr_intrm}; |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 167 | if (need_padding) { |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 168 | size_t mcu_row_size = aligned_width * kCompressBatchSize * 3 / 2; |
| 169 | buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size); |
| 170 | y_plane_intrm = buffer_intrm.get(); |
| 171 | u_plane_intrm = y_plane_intrm + (aligned_width * kCompressBatchSize); |
| 172 | v_plane_intrm = u_plane_intrm + (aligned_width * kCompressBatchSize) / 4; |
| 173 | for (int i = 0; i < kCompressBatchSize; ++i) { |
| 174 | y_intrm[i] = y_plane_intrm + i * aligned_width; |
| 175 | memset(y_intrm[i] + cinfo->image_width, 0, aligned_width - cinfo->image_width); |
| 176 | } |
| 177 | for (int i = 0; i < kCompressBatchSize / 2; ++i) { |
| 178 | int offset_intrm = i * (aligned_width / 2); |
| 179 | cb_intrm[i] = u_plane_intrm + offset_intrm; |
| 180 | cr_intrm[i] = v_plane_intrm + offset_intrm; |
| 181 | memset(cb_intrm[i] + cinfo->image_width / 2, 0, |
| 182 | (aligned_width - cinfo->image_width) / 2); |
| 183 | memset(cr_intrm[i] + cinfo->image_width / 2, 0, |
| 184 | (aligned_width - cinfo->image_width) / 2); |
| 185 | } |
| 186 | } |
| 187 | |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 188 | while (cinfo->next_scanline < cinfo->image_height) { |
| 189 | for (int i = 0; i < kCompressBatchSize; ++i) { |
| 190 | size_t scanline = cinfo->next_scanline + i; |
| 191 | if (scanline < cinfo->image_height) { |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 192 | y[i] = y_plane + scanline * lumaStride; |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 193 | } else { |
| 194 | y[i] = empty.get(); |
| 195 | } |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 196 | if (need_padding) { |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 197 | memcpy(y_intrm[i], y[i], cinfo->image_width); |
| 198 | } |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 199 | } |
| 200 | // cb, cr only have half scanlines |
| 201 | for (int i = 0; i < kCompressBatchSize / 2; ++i) { |
| 202 | size_t scanline = cinfo->next_scanline / 2 + i; |
| 203 | if (scanline < cinfo->image_height / 2) { |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 204 | int offset = scanline * chromaStride; |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 205 | cb[i] = u_plane + offset; |
| 206 | cr[i] = v_plane + offset; |
| 207 | } else { |
| 208 | cb[i] = cr[i] = empty.get(); |
| 209 | } |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 210 | if (need_padding) { |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 211 | memcpy(cb_intrm[i], cb[i], cinfo->image_width / 2); |
| 212 | memcpy(cr_intrm[i], cr[i], cinfo->image_width / 2); |
| 213 | } |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 214 | } |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 215 | int processed = jpeg_write_raw_data(cinfo, need_padding ? planes_intrm : planes, |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 216 | kCompressBatchSize); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 217 | if (processed != kCompressBatchSize) { |
| 218 | ALOGE("Number of processed lines does not equal input lines."); |
| 219 | return false; |
| 220 | } |
| 221 | } |
| 222 | return true; |
| 223 | } |
| 224 | |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 225 | bool JpegEncoderHelper::compressY(jpeg_compress_struct* cinfo, const uint8_t* yBuffer, |
| 226 | int lumaStride) { |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 227 | JSAMPROW y[kCompressBatchSize]; |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 228 | JSAMPARRAY planes[1]{y}; |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 229 | |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 230 | uint8_t* y_plane = const_cast<uint8_t*>(yBuffer); |
Ram Mohan | e69d9d2 | 2023-06-02 17:44:45 +0530 | [diff] [blame] | 231 | std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width); |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 232 | memset(empty.get(), 0, cinfo->image_width); |
| 233 | |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 234 | const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize); |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 235 | const bool need_padding = (lumaStride < aligned_width); |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 236 | std::unique_ptr<uint8_t[]> buffer_intrm = nullptr; |
| 237 | uint8_t* y_plane_intrm = nullptr; |
| 238 | uint8_t* u_plane_intrm = nullptr; |
| 239 | JSAMPROW y_intrm[kCompressBatchSize]; |
| 240 | JSAMPARRAY planes_intrm[]{y_intrm}; |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 241 | if (need_padding) { |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 242 | size_t mcu_row_size = aligned_width * kCompressBatchSize; |
| 243 | buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size); |
| 244 | y_plane_intrm = buffer_intrm.get(); |
| 245 | for (int i = 0; i < kCompressBatchSize; ++i) { |
| 246 | y_intrm[i] = y_plane_intrm + i * aligned_width; |
| 247 | memset(y_intrm[i] + cinfo->image_width, 0, aligned_width - cinfo->image_width); |
| 248 | } |
| 249 | } |
| 250 | |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 251 | while (cinfo->next_scanline < cinfo->image_height) { |
| 252 | for (int i = 0; i < kCompressBatchSize; ++i) { |
| 253 | size_t scanline = cinfo->next_scanline + i; |
| 254 | if (scanline < cinfo->image_height) { |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 255 | y[i] = y_plane + scanline * lumaStride; |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 256 | } else { |
| 257 | y[i] = empty.get(); |
| 258 | } |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 259 | if (need_padding) { |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 260 | memcpy(y_intrm[i], y[i], cinfo->image_width); |
| 261 | } |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 262 | } |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 263 | int processed = jpeg_write_raw_data(cinfo, need_padding ? planes_intrm : planes, |
Ram Mohan | 4a72c19 | 2023-05-22 19:05:06 +0530 | [diff] [blame] | 264 | kCompressBatchSize); |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 265 | if (processed != kCompressBatchSize / 2) { |
| 266 | ALOGE("Number of processed lines does not equal input lines."); |
| 267 | return false; |
| 268 | } |
| 269 | } |
| 270 | return true; |
| 271 | } |
| 272 | |
Ram Mohan | b2359cd | 2023-07-28 14:33:49 +0530 | [diff] [blame] | 273 | } // namespace android::ultrahdr |