blob: 9394a83e4626de57b88b06a020cbe24cabe147b4 [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
Ram Mohanb2359cd2023-07-28 14:33:49 +053026#define ALIGNM(x, m) ((((x) + ((m)-1)) / (m)) * (m))
Ram Mohan4a72c192023-05-22 19:05:06 +053027
Dichen Zhang02dd0592023-02-10 20:16:57 +000028// The destination manager that can access |mResultBuffer| in JpegEncoderHelper.
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000029struct destination_mgr {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000030 struct jpeg_destination_mgr mgr;
Dichen Zhang02dd0592023-02-10 20:16:57 +000031 JpegEncoderHelper* encoder;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000032};
33
Ram Mohanb2359cd2023-07-28 14:33:49 +053034JpegEncoderHelper::JpegEncoderHelper() {}
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000035
Ram Mohanb2359cd2023-07-28 14:33:49 +053036JpegEncoderHelper::~JpegEncoderHelper() {}
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000037
Dichen Zhang02dd0592023-02-10 20:16:57 +000038bool JpegEncoderHelper::compressImage(const void* image, int width, int height, int quality,
Ram Mohanb2359cd2023-07-28 14:33:49 +053039 const void* iccBuffer, unsigned int iccSize,
40 bool isSingleChannel) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000041 mResultBuffer.clear();
Dichen Zhanga4819142022-10-21 04:12:30 +000042 if (!encode(image, width, height, quality, iccBuffer, iccSize, isSingleChannel)) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000043 return false;
44 }
Ram Mohanb2359cd2023-07-28 14:33:49 +053045 ALOGI("Compressed JPEG: %d[%dx%d] -> %zu bytes", (width * height * 12) / 8, width, height,
46 mResultBuffer.size());
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000047 return true;
48}
49
Dichen Zhang02dd0592023-02-10 20:16:57 +000050void* JpegEncoderHelper::getCompressedImagePtr() {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000051 return mResultBuffer.data();
52}
53
Dichen Zhang02dd0592023-02-10 20:16:57 +000054size_t JpegEncoderHelper::getCompressedImageSize() {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000055 return mResultBuffer.size();
56}
57
Dichen Zhang02dd0592023-02-10 20:16:57 +000058void JpegEncoderHelper::initDestination(j_compress_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000059 destination_mgr* dest = reinterpret_cast<destination_mgr*>(cinfo->dest);
60 std::vector<JOCTET>& buffer = dest->encoder->mResultBuffer;
61 buffer.resize(kBlockSize);
62 dest->mgr.next_output_byte = &buffer[0];
63 dest->mgr.free_in_buffer = buffer.size();
64}
65
Dichen Zhang02dd0592023-02-10 20:16:57 +000066boolean JpegEncoderHelper::emptyOutputBuffer(j_compress_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000067 destination_mgr* dest = reinterpret_cast<destination_mgr*>(cinfo->dest);
68 std::vector<JOCTET>& buffer = dest->encoder->mResultBuffer;
69 size_t oldsize = buffer.size();
70 buffer.resize(oldsize + kBlockSize);
71 dest->mgr.next_output_byte = &buffer[oldsize];
72 dest->mgr.free_in_buffer = kBlockSize;
73 return true;
74}
75
Dichen Zhang02dd0592023-02-10 20:16:57 +000076void JpegEncoderHelper::terminateDestination(j_compress_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000077 destination_mgr* dest = reinterpret_cast<destination_mgr*>(cinfo->dest);
78 std::vector<JOCTET>& buffer = dest->encoder->mResultBuffer;
79 buffer.resize(buffer.size() - dest->mgr.free_in_buffer);
80}
81
Dichen Zhang02dd0592023-02-10 20:16:57 +000082void JpegEncoderHelper::outputErrorMessage(j_common_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000083 char buffer[JMSG_LENGTH_MAX];
84
85 /* Create the message */
Ram Mohanb2359cd2023-07-28 14:33:49 +053086 (*cinfo->err->format_message)(cinfo, buffer);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000087 ALOGE("%s\n", buffer);
88}
89
Dichen Zhang02dd0592023-02-10 20:16:57 +000090bool JpegEncoderHelper::encode(const void* image, int width, int height, int jpegQuality,
Ram Mohanb2359cd2023-07-28 14:33:49 +053091 const void* iccBuffer, unsigned int iccSize, bool isSingleChannel) {
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);
96 // Override output_message() to print error log with ALOGE().
97 cinfo.err->output_message = &outputErrorMessage;
98 jpeg_create_compress(&cinfo);
99 setJpegDestination(&cinfo);
100
Dichen Zhanga4819142022-10-21 04:12:30 +0000101 setJpegCompressStruct(width, height, jpegQuality, &cinfo, isSingleChannel);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000102 jpeg_start_compress(&cinfo, TRUE);
103
104 if (iccBuffer != nullptr && iccSize > 0) {
105 jpeg_write_marker(&cinfo, JPEG_APP0 + 2, static_cast<const JOCTET*>(iccBuffer), iccSize);
106 }
107
Ram Mohane69d9d22023-06-02 17:44:45 +0530108 bool status = compress(&cinfo, static_cast<const uint8_t*>(image), isSingleChannel);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000109 jpeg_finish_compress(&cinfo);
110 jpeg_destroy_compress(&cinfo);
Ram Mohane69d9d22023-06-02 17:44:45 +0530111
112 return status;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000113}
114
Dichen Zhang02dd0592023-02-10 20:16:57 +0000115void JpegEncoderHelper::setJpegDestination(jpeg_compress_struct* cinfo) {
Ram Mohanb2359cd2023-07-28 14:33:49 +0530116 destination_mgr* dest = static_cast<struct destination_mgr*>(
117 (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT,
118 sizeof(destination_mgr)));
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000119 dest->encoder = this;
120 dest->mgr.init_destination = &initDestination;
121 dest->mgr.empty_output_buffer = &emptyOutputBuffer;
122 dest->mgr.term_destination = &terminateDestination;
123 cinfo->dest = reinterpret_cast<struct jpeg_destination_mgr*>(dest);
124}
125
Dichen Zhang02dd0592023-02-10 20:16:57 +0000126void JpegEncoderHelper::setJpegCompressStruct(int width, int height, int quality,
Ram Mohanb2359cd2023-07-28 14:33:49 +0530127 jpeg_compress_struct* cinfo, bool isSingleChannel) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000128 cinfo->image_width = width;
129 cinfo->image_height = height;
Ram Mohanb2359cd2023-07-28 14:33:49 +0530130 cinfo->input_components = isSingleChannel ? 1 : 3;
131 cinfo->in_color_space = isSingleChannel ? JCS_GRAYSCALE : JCS_YCbCr;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000132 jpeg_set_defaults(cinfo);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000133 jpeg_set_quality(cinfo, quality, TRUE);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000134 cinfo->raw_data_in = TRUE;
Ram Mohan1790bb52023-06-11 07:07:27 +0530135 cinfo->dct_method = JDCT_ISLOW;
Ram Mohanb2359cd2023-07-28 14:33:49 +0530136 cinfo->comp_info[0].h_samp_factor = cinfo->in_color_space == JCS_GRAYSCALE ? 1 : 2;
137 cinfo->comp_info[0].v_samp_factor = cinfo->in_color_space == JCS_GRAYSCALE ? 1 : 2;
138 for (int i = 1; i < cinfo->num_components; i++) {
139 cinfo->comp_info[i].h_samp_factor = 1;
140 cinfo->comp_info[i].v_samp_factor = 1;
Dichen Zhanga4819142022-10-21 04:12:30 +0000141 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000142}
143
Ram Mohanb2359cd2023-07-28 14:33:49 +0530144bool JpegEncoderHelper::compress(jpeg_compress_struct* cinfo, const uint8_t* image,
145 bool isSingleChannel) {
146 return isSingleChannel ? compressSingleChannel(cinfo, image) : compressYuv(cinfo, image);
Dichen Zhanga4819142022-10-21 04:12:30 +0000147}
148
Dichen Zhang02dd0592023-02-10 20:16:57 +0000149bool JpegEncoderHelper::compressYuv(jpeg_compress_struct* cinfo, const uint8_t* yuv) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000150 JSAMPROW y[kCompressBatchSize];
151 JSAMPROW cb[kCompressBatchSize / 2];
152 JSAMPROW cr[kCompressBatchSize / 2];
Ram Mohanb2359cd2023-07-28 14:33:49 +0530153 JSAMPARRAY planes[3]{y, cb, cr};
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000154
155 size_t y_plane_size = cinfo->image_width * cinfo->image_height;
156 size_t uv_plane_size = y_plane_size / 4;
157 uint8_t* y_plane = const_cast<uint8_t*>(yuv);
158 uint8_t* u_plane = const_cast<uint8_t*>(yuv + y_plane_size);
159 uint8_t* v_plane = const_cast<uint8_t*>(yuv + y_plane_size + uv_plane_size);
Ram Mohane69d9d22023-06-02 17:44:45 +0530160 std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000161 memset(empty.get(), 0, cinfo->image_width);
162
Ram Mohan4a72c192023-05-22 19:05:06 +0530163 const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
164 const bool is_width_aligned = (aligned_width == cinfo->image_width);
165 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
166 uint8_t* y_plane_intrm = nullptr;
167 uint8_t* u_plane_intrm = nullptr;
168 uint8_t* v_plane_intrm = nullptr;
169 JSAMPROW y_intrm[kCompressBatchSize];
170 JSAMPROW cb_intrm[kCompressBatchSize / 2];
171 JSAMPROW cr_intrm[kCompressBatchSize / 2];
172 JSAMPARRAY planes_intrm[3]{y_intrm, cb_intrm, cr_intrm};
173 if (!is_width_aligned) {
174 size_t mcu_row_size = aligned_width * kCompressBatchSize * 3 / 2;
175 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
176 y_plane_intrm = buffer_intrm.get();
177 u_plane_intrm = y_plane_intrm + (aligned_width * kCompressBatchSize);
178 v_plane_intrm = u_plane_intrm + (aligned_width * kCompressBatchSize) / 4;
179 for (int i = 0; i < kCompressBatchSize; ++i) {
180 y_intrm[i] = y_plane_intrm + i * aligned_width;
181 memset(y_intrm[i] + cinfo->image_width, 0, aligned_width - cinfo->image_width);
182 }
183 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
184 int offset_intrm = i * (aligned_width / 2);
185 cb_intrm[i] = u_plane_intrm + offset_intrm;
186 cr_intrm[i] = v_plane_intrm + offset_intrm;
187 memset(cb_intrm[i] + cinfo->image_width / 2, 0,
188 (aligned_width - cinfo->image_width) / 2);
189 memset(cr_intrm[i] + cinfo->image_width / 2, 0,
190 (aligned_width - cinfo->image_width) / 2);
191 }
192 }
193
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000194 while (cinfo->next_scanline < cinfo->image_height) {
195 for (int i = 0; i < kCompressBatchSize; ++i) {
196 size_t scanline = cinfo->next_scanline + i;
197 if (scanline < cinfo->image_height) {
198 y[i] = y_plane + scanline * cinfo->image_width;
199 } else {
200 y[i] = empty.get();
201 }
Ram Mohan4a72c192023-05-22 19:05:06 +0530202 if (!is_width_aligned) {
203 memcpy(y_intrm[i], y[i], cinfo->image_width);
204 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000205 }
206 // cb, cr only have half scanlines
207 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
208 size_t scanline = cinfo->next_scanline / 2 + i;
209 if (scanline < cinfo->image_height / 2) {
210 int offset = scanline * (cinfo->image_width / 2);
211 cb[i] = u_plane + offset;
212 cr[i] = v_plane + offset;
213 } else {
214 cb[i] = cr[i] = empty.get();
215 }
Ram Mohan4a72c192023-05-22 19:05:06 +0530216 if (!is_width_aligned) {
217 memcpy(cb_intrm[i], cb[i], cinfo->image_width / 2);
218 memcpy(cr_intrm[i], cr[i], cinfo->image_width / 2);
219 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000220 }
Ram Mohan4a72c192023-05-22 19:05:06 +0530221 int processed = jpeg_write_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
222 kCompressBatchSize);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000223 if (processed != kCompressBatchSize) {
224 ALOGE("Number of processed lines does not equal input lines.");
225 return false;
226 }
227 }
228 return true;
229}
230
Dichen Zhang02dd0592023-02-10 20:16:57 +0000231bool JpegEncoderHelper::compressSingleChannel(jpeg_compress_struct* cinfo, const uint8_t* image) {
Dichen Zhanga4819142022-10-21 04:12:30 +0000232 JSAMPROW y[kCompressBatchSize];
Ram Mohanb2359cd2023-07-28 14:33:49 +0530233 JSAMPARRAY planes[1]{y};
Dichen Zhanga4819142022-10-21 04:12:30 +0000234
235 uint8_t* y_plane = const_cast<uint8_t*>(image);
Ram Mohane69d9d22023-06-02 17:44:45 +0530236 std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
Dichen Zhanga4819142022-10-21 04:12:30 +0000237 memset(empty.get(), 0, cinfo->image_width);
238
Ram Mohan4a72c192023-05-22 19:05:06 +0530239 const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
240 bool is_width_aligned = (aligned_width == cinfo->image_width);
241 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
242 uint8_t* y_plane_intrm = nullptr;
243 uint8_t* u_plane_intrm = nullptr;
244 JSAMPROW y_intrm[kCompressBatchSize];
245 JSAMPARRAY planes_intrm[]{y_intrm};
246 if (!is_width_aligned) {
247 size_t mcu_row_size = aligned_width * kCompressBatchSize;
248 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
249 y_plane_intrm = buffer_intrm.get();
250 for (int i = 0; i < kCompressBatchSize; ++i) {
251 y_intrm[i] = y_plane_intrm + i * aligned_width;
252 memset(y_intrm[i] + cinfo->image_width, 0, aligned_width - cinfo->image_width);
253 }
254 }
255
Dichen Zhanga4819142022-10-21 04:12:30 +0000256 while (cinfo->next_scanline < cinfo->image_height) {
257 for (int i = 0; i < kCompressBatchSize; ++i) {
258 size_t scanline = cinfo->next_scanline + i;
259 if (scanline < cinfo->image_height) {
260 y[i] = y_plane + scanline * cinfo->image_width;
261 } else {
262 y[i] = empty.get();
263 }
Ram Mohan4a72c192023-05-22 19:05:06 +0530264 if (!is_width_aligned) {
265 memcpy(y_intrm[i], y[i], cinfo->image_width);
266 }
Dichen Zhanga4819142022-10-21 04:12:30 +0000267 }
Ram Mohan4a72c192023-05-22 19:05:06 +0530268 int processed = jpeg_write_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
269 kCompressBatchSize);
Dichen Zhanga4819142022-10-21 04:12:30 +0000270 if (processed != kCompressBatchSize / 2) {
271 ALOGE("Number of processed lines does not equal input lines.");
272 return false;
273 }
274 }
275 return true;
276}
277
Ram Mohanb2359cd2023-07-28 14:33:49 +0530278} // namespace android::ultrahdr