blob: 13ae7424d5cc722520d1bbdc9b32d4f2b8f614e2 [file] [log] [blame]
Dichen Zhang4a66b3c2022-10-19 18:04:47 +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
Ram Mohanb2359cd2023-07-28 14:33:49 +053017#include <cstring>
18#include <memory>
19#include <vector>
20
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000021#include <ultrahdr/jpegencoderhelper.h>
Dichen Zhangb27d06d2022-12-14 19:57:50 +000022#include <utils/Log.h>
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000023
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000024namespace android::ultrahdr {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000025
Dichen Zhang02dd0592023-02-10 20:16:57 +000026// The destination manager that can access |mResultBuffer| in JpegEncoderHelper.
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000027struct destination_mgr {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000028 struct jpeg_destination_mgr mgr;
Dichen Zhang02dd0592023-02-10 20:16:57 +000029 JpegEncoderHelper* encoder;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000030};
31
Ram Mohanb2359cd2023-07-28 14:33:49 +053032JpegEncoderHelper::JpegEncoderHelper() {}
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000033
Ram Mohanb2359cd2023-07-28 14:33:49 +053034JpegEncoderHelper::~JpegEncoderHelper() {}
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000035
Ram Mohaneca81942023-07-29 14:41:48 +053036bool 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 Zhang4a66b3c2022-10-19 18:04:47 +000039 mResultBuffer.clear();
Ram Mohaneca81942023-07-29 14:41:48 +053040 if (!encode(yBuffer, uvBuffer, width, height, lumaStride, chromaStride, quality, iccBuffer,
41 iccSize)) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000042 return false;
43 }
Ram Mohanb2359cd2023-07-28 14:33:49 +053044 ALOGI("Compressed JPEG: %d[%dx%d] -> %zu bytes", (width * height * 12) / 8, width, height,
45 mResultBuffer.size());
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000046 return true;
47}
48
Dichen Zhang02dd0592023-02-10 20:16:57 +000049void* JpegEncoderHelper::getCompressedImagePtr() {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000050 return mResultBuffer.data();
51}
52
Dichen Zhang02dd0592023-02-10 20:16:57 +000053size_t JpegEncoderHelper::getCompressedImageSize() {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000054 return mResultBuffer.size();
55}
56
Dichen Zhang02dd0592023-02-10 20:16:57 +000057void JpegEncoderHelper::initDestination(j_compress_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000058 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 Zhang02dd0592023-02-10 20:16:57 +000065boolean JpegEncoderHelper::emptyOutputBuffer(j_compress_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000066 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 Zhang02dd0592023-02-10 20:16:57 +000075void JpegEncoderHelper::terminateDestination(j_compress_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000076 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 Zhang02dd0592023-02-10 20:16:57 +000081void JpegEncoderHelper::outputErrorMessage(j_common_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000082 char buffer[JMSG_LENGTH_MAX];
83
84 /* Create the message */
Ram Mohanb2359cd2023-07-28 14:33:49 +053085 (*cinfo->err->format_message)(cinfo, buffer);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000086 ALOGE("%s\n", buffer);
87}
88
Ram Mohaneca81942023-07-29 14:41:48 +053089bool 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 Zhang4a66b3c2022-10-19 18:04:47 +000092 jpeg_compress_struct cinfo;
93 jpeg_error_mgr jerr;
94
95 cinfo.err = jpeg_std_error(&jerr);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000096 cinfo.err->output_message = &outputErrorMessage;
97 jpeg_create_compress(&cinfo);
98 setJpegDestination(&cinfo);
Ram Mohaneca81942023-07-29 14:41:48 +053099 setJpegCompressStruct(width, height, quality, &cinfo, uvBuffer == nullptr);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000100 jpeg_start_compress(&cinfo, TRUE);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000101 if (iccBuffer != nullptr && iccSize > 0) {
102 jpeg_write_marker(&cinfo, JPEG_APP0 + 2, static_cast<const JOCTET*>(iccBuffer), iccSize);
103 }
Ram Mohaneca81942023-07-29 14:41:48 +0530104 bool status = cinfo.num_components == 1
105 ? compressY(&cinfo, yBuffer, lumaStride)
106 : compressYuv(&cinfo, yBuffer, uvBuffer, lumaStride, chromaStride);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000107 jpeg_finish_compress(&cinfo);
108 jpeg_destroy_compress(&cinfo);
Ram Mohane69d9d22023-06-02 17:44:45 +0530109
110 return status;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000111}
112
Dichen Zhang02dd0592023-02-10 20:16:57 +0000113void JpegEncoderHelper::setJpegDestination(jpeg_compress_struct* cinfo) {
Ram Mohanb2359cd2023-07-28 14:33:49 +0530114 destination_mgr* dest = static_cast<struct destination_mgr*>(
115 (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT,
116 sizeof(destination_mgr)));
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000117 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 Zhang02dd0592023-02-10 20:16:57 +0000124void JpegEncoderHelper::setJpegCompressStruct(int width, int height, int quality,
Ram Mohanb2359cd2023-07-28 14:33:49 +0530125 jpeg_compress_struct* cinfo, bool isSingleChannel) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000126 cinfo->image_width = width;
127 cinfo->image_height = height;
Ram Mohanb2359cd2023-07-28 14:33:49 +0530128 cinfo->input_components = isSingleChannel ? 1 : 3;
129 cinfo->in_color_space = isSingleChannel ? JCS_GRAYSCALE : JCS_YCbCr;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000130 jpeg_set_defaults(cinfo);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000131 jpeg_set_quality(cinfo, quality, TRUE);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000132 cinfo->raw_data_in = TRUE;
Ram Mohan1790bb52023-06-11 07:07:27 +0530133 cinfo->dct_method = JDCT_ISLOW;
Ram Mohanb2359cd2023-07-28 14:33:49 +0530134 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 Zhanga4819142022-10-21 04:12:30 +0000139 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000140}
141
Ram Mohaneca81942023-07-29 14:41:48 +0530142bool JpegEncoderHelper::compressYuv(jpeg_compress_struct* cinfo, const uint8_t* yBuffer,
143 const uint8_t* uvBuffer, int lumaStride, int chromaStride) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000144 JSAMPROW y[kCompressBatchSize];
145 JSAMPROW cb[kCompressBatchSize / 2];
146 JSAMPROW cr[kCompressBatchSize / 2];
Ram Mohanb2359cd2023-07-28 14:33:49 +0530147 JSAMPARRAY planes[3]{y, cb, cr};
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000148
Ram Mohaneca81942023-07-29 14:41:48 +0530149 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 Mohane69d9d22023-06-02 17:44:45 +0530154 std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000155 memset(empty.get(), 0, cinfo->image_width);
156
Ram Mohan4a72c192023-05-22 19:05:06 +0530157 const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
Ram Mohaneca81942023-07-29 14:41:48 +0530158 const bool need_padding = (lumaStride < aligned_width);
Ram Mohan4a72c192023-05-22 19:05:06 +0530159 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 Mohaneca81942023-07-29 14:41:48 +0530167 if (need_padding) {
Ram Mohan4a72c192023-05-22 19:05:06 +0530168 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 Zhang4a66b3c2022-10-19 18:04:47 +0000188 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 Mohaneca81942023-07-29 14:41:48 +0530192 y[i] = y_plane + scanline * lumaStride;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000193 } else {
194 y[i] = empty.get();
195 }
Ram Mohaneca81942023-07-29 14:41:48 +0530196 if (need_padding) {
Ram Mohan4a72c192023-05-22 19:05:06 +0530197 memcpy(y_intrm[i], y[i], cinfo->image_width);
198 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000199 }
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 Mohaneca81942023-07-29 14:41:48 +0530204 int offset = scanline * chromaStride;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000205 cb[i] = u_plane + offset;
206 cr[i] = v_plane + offset;
207 } else {
208 cb[i] = cr[i] = empty.get();
209 }
Ram Mohaneca81942023-07-29 14:41:48 +0530210 if (need_padding) {
Ram Mohan4a72c192023-05-22 19:05:06 +0530211 memcpy(cb_intrm[i], cb[i], cinfo->image_width / 2);
212 memcpy(cr_intrm[i], cr[i], cinfo->image_width / 2);
213 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000214 }
Ram Mohaneca81942023-07-29 14:41:48 +0530215 int processed = jpeg_write_raw_data(cinfo, need_padding ? planes_intrm : planes,
Ram Mohan4a72c192023-05-22 19:05:06 +0530216 kCompressBatchSize);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000217 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 Mohaneca81942023-07-29 14:41:48 +0530225bool JpegEncoderHelper::compressY(jpeg_compress_struct* cinfo, const uint8_t* yBuffer,
226 int lumaStride) {
Dichen Zhanga4819142022-10-21 04:12:30 +0000227 JSAMPROW y[kCompressBatchSize];
Ram Mohanb2359cd2023-07-28 14:33:49 +0530228 JSAMPARRAY planes[1]{y};
Dichen Zhanga4819142022-10-21 04:12:30 +0000229
Ram Mohaneca81942023-07-29 14:41:48 +0530230 uint8_t* y_plane = const_cast<uint8_t*>(yBuffer);
Ram Mohane69d9d22023-06-02 17:44:45 +0530231 std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
Dichen Zhanga4819142022-10-21 04:12:30 +0000232 memset(empty.get(), 0, cinfo->image_width);
233
Ram Mohan4a72c192023-05-22 19:05:06 +0530234 const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
Ram Mohaneca81942023-07-29 14:41:48 +0530235 const bool need_padding = (lumaStride < aligned_width);
Ram Mohan4a72c192023-05-22 19:05:06 +0530236 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 Mohaneca81942023-07-29 14:41:48 +0530241 if (need_padding) {
Ram Mohan4a72c192023-05-22 19:05:06 +0530242 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 Zhanga4819142022-10-21 04:12:30 +0000251 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 Mohaneca81942023-07-29 14:41:48 +0530255 y[i] = y_plane + scanline * lumaStride;
Dichen Zhanga4819142022-10-21 04:12:30 +0000256 } else {
257 y[i] = empty.get();
258 }
Ram Mohaneca81942023-07-29 14:41:48 +0530259 if (need_padding) {
Ram Mohan4a72c192023-05-22 19:05:06 +0530260 memcpy(y_intrm[i], y[i], cinfo->image_width);
261 }
Dichen Zhanga4819142022-10-21 04:12:30 +0000262 }
Ram Mohaneca81942023-07-29 14:41:48 +0530263 int processed = jpeg_write_raw_data(cinfo, need_padding ? planes_intrm : planes,
Ram Mohan4a72c192023-05-22 19:05:06 +0530264 kCompressBatchSize);
Dichen Zhanga4819142022-10-21 04:12:30 +0000265 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 Mohanb2359cd2023-07-28 14:33:49 +0530273} // namespace android::ultrahdr