blob: ab2f8c7b5a4647e1cae554d16df88485808d87ff [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
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000017#include <ultrahdr/jpegencoderhelper.h>
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000018
Dichen Zhangb27d06d2022-12-14 19:57:50 +000019#include <utils/Log.h>
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000020
21#include <errno.h>
22
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000023namespace android::ultrahdr {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000024
Ram Mohan4a72c192023-05-22 19:05:06 +053025#define ALIGNM(x, m) ((((x) + ((m) - 1)) / (m)) * (m))
26
Dichen Zhang02dd0592023-02-10 20:16:57 +000027// The destination manager that can access |mResultBuffer| in JpegEncoderHelper.
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000028struct destination_mgr {
29public:
30 struct jpeg_destination_mgr mgr;
Dichen Zhang02dd0592023-02-10 20:16:57 +000031 JpegEncoderHelper* encoder;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000032};
33
Dichen Zhang02dd0592023-02-10 20:16:57 +000034JpegEncoderHelper::JpegEncoderHelper() {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000035}
36
Dichen Zhang02dd0592023-02-10 20:16:57 +000037JpegEncoderHelper::~JpegEncoderHelper() {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000038}
39
Dichen Zhang02dd0592023-02-10 20:16:57 +000040bool JpegEncoderHelper::compressImage(const void* image, int width, int height, int quality,
Dichen Zhanga4819142022-10-21 04:12:30 +000041 const void* iccBuffer, unsigned int iccSize,
42 bool isSingleChannel) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000043 mResultBuffer.clear();
Dichen Zhanga4819142022-10-21 04:12:30 +000044 if (!encode(image, width, height, quality, iccBuffer, iccSize, isSingleChannel)) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000045 return false;
46 }
47 ALOGI("Compressed JPEG: %d[%dx%d] -> %zu bytes",
48 (width * height * 12) / 8, width, height, mResultBuffer.size());
49 return true;
50}
51
Dichen Zhang02dd0592023-02-10 20:16:57 +000052void* JpegEncoderHelper::getCompressedImagePtr() {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000053 return mResultBuffer.data();
54}
55
Dichen Zhang02dd0592023-02-10 20:16:57 +000056size_t JpegEncoderHelper::getCompressedImageSize() {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000057 return mResultBuffer.size();
58}
59
Dichen Zhang02dd0592023-02-10 20:16:57 +000060void JpegEncoderHelper::initDestination(j_compress_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000061 destination_mgr* dest = reinterpret_cast<destination_mgr*>(cinfo->dest);
62 std::vector<JOCTET>& buffer = dest->encoder->mResultBuffer;
63 buffer.resize(kBlockSize);
64 dest->mgr.next_output_byte = &buffer[0];
65 dest->mgr.free_in_buffer = buffer.size();
66}
67
Dichen Zhang02dd0592023-02-10 20:16:57 +000068boolean JpegEncoderHelper::emptyOutputBuffer(j_compress_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000069 destination_mgr* dest = reinterpret_cast<destination_mgr*>(cinfo->dest);
70 std::vector<JOCTET>& buffer = dest->encoder->mResultBuffer;
71 size_t oldsize = buffer.size();
72 buffer.resize(oldsize + kBlockSize);
73 dest->mgr.next_output_byte = &buffer[oldsize];
74 dest->mgr.free_in_buffer = kBlockSize;
75 return true;
76}
77
Dichen Zhang02dd0592023-02-10 20:16:57 +000078void JpegEncoderHelper::terminateDestination(j_compress_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000079 destination_mgr* dest = reinterpret_cast<destination_mgr*>(cinfo->dest);
80 std::vector<JOCTET>& buffer = dest->encoder->mResultBuffer;
81 buffer.resize(buffer.size() - dest->mgr.free_in_buffer);
82}
83
Dichen Zhang02dd0592023-02-10 20:16:57 +000084void JpegEncoderHelper::outputErrorMessage(j_common_ptr cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000085 char buffer[JMSG_LENGTH_MAX];
86
87 /* Create the message */
88 (*cinfo->err->format_message) (cinfo, buffer);
89 ALOGE("%s\n", buffer);
90}
91
Dichen Zhang02dd0592023-02-10 20:16:57 +000092bool JpegEncoderHelper::encode(const void* image, int width, int height, int jpegQuality,
Dichen Zhanga4819142022-10-21 04:12:30 +000093 const void* iccBuffer, unsigned int iccSize, bool isSingleChannel) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000094 jpeg_compress_struct cinfo;
95 jpeg_error_mgr jerr;
96
97 cinfo.err = jpeg_std_error(&jerr);
98 // Override output_message() to print error log with ALOGE().
99 cinfo.err->output_message = &outputErrorMessage;
100 jpeg_create_compress(&cinfo);
101 setJpegDestination(&cinfo);
102
Dichen Zhanga4819142022-10-21 04:12:30 +0000103 setJpegCompressStruct(width, height, jpegQuality, &cinfo, isSingleChannel);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000104 jpeg_start_compress(&cinfo, TRUE);
105
106 if (iccBuffer != nullptr && iccSize > 0) {
107 jpeg_write_marker(&cinfo, JPEG_APP0 + 2, static_cast<const JOCTET*>(iccBuffer), iccSize);
108 }
109
Dichen Zhanga4819142022-10-21 04:12:30 +0000110 if (!compress(&cinfo, static_cast<const uint8_t*>(image), isSingleChannel)) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000111 return false;
112 }
113 jpeg_finish_compress(&cinfo);
114 jpeg_destroy_compress(&cinfo);
115 return true;
116}
117
Dichen Zhang02dd0592023-02-10 20:16:57 +0000118void JpegEncoderHelper::setJpegDestination(jpeg_compress_struct* cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000119 destination_mgr* dest = static_cast<struct destination_mgr *>((*cinfo->mem->alloc_small) (
120 (j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(destination_mgr)));
121 dest->encoder = this;
122 dest->mgr.init_destination = &initDestination;
123 dest->mgr.empty_output_buffer = &emptyOutputBuffer;
124 dest->mgr.term_destination = &terminateDestination;
125 cinfo->dest = reinterpret_cast<struct jpeg_destination_mgr*>(dest);
126}
127
Dichen Zhang02dd0592023-02-10 20:16:57 +0000128void JpegEncoderHelper::setJpegCompressStruct(int width, int height, int quality,
Dichen Zhanga4819142022-10-21 04:12:30 +0000129 jpeg_compress_struct* cinfo, bool isSingleChannel) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000130 cinfo->image_width = width;
131 cinfo->image_height = height;
Dichen Zhanga4819142022-10-21 04:12:30 +0000132 if (isSingleChannel) {
133 cinfo->input_components = 1;
134 cinfo->in_color_space = JCS_GRAYSCALE;
135 } else {
136 cinfo->input_components = 3;
137 cinfo->in_color_space = JCS_YCbCr;
138 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000139 jpeg_set_defaults(cinfo);
140
141 jpeg_set_quality(cinfo, quality, TRUE);
Dichen Zhanga4819142022-10-21 04:12:30 +0000142 jpeg_set_colorspace(cinfo, isSingleChannel ? JCS_GRAYSCALE : JCS_YCbCr);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000143 cinfo->raw_data_in = TRUE;
144 cinfo->dct_method = JDCT_IFAST;
145
Dichen Zhanga4819142022-10-21 04:12:30 +0000146 if (!isSingleChannel) {
147 // Configure sampling factors. The sampling factor is JPEG subsampling 420 because the
148 // source format is YUV420.
149 cinfo->comp_info[0].h_samp_factor = 2;
150 cinfo->comp_info[0].v_samp_factor = 2;
151 cinfo->comp_info[1].h_samp_factor = 1;
152 cinfo->comp_info[1].v_samp_factor = 1;
153 cinfo->comp_info[2].h_samp_factor = 1;
154 cinfo->comp_info[2].v_samp_factor = 1;
155 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000156}
157
Dichen Zhang02dd0592023-02-10 20:16:57 +0000158bool JpegEncoderHelper::compress(
Dichen Zhanga4819142022-10-21 04:12:30 +0000159 jpeg_compress_struct* cinfo, const uint8_t* image, bool isSingleChannel) {
160 if (isSingleChannel) {
161 return compressSingleChannel(cinfo, image);
162 }
163 return compressYuv(cinfo, image);
164}
165
Dichen Zhang02dd0592023-02-10 20:16:57 +0000166bool JpegEncoderHelper::compressYuv(jpeg_compress_struct* cinfo, const uint8_t* yuv) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000167 JSAMPROW y[kCompressBatchSize];
168 JSAMPROW cb[kCompressBatchSize / 2];
169 JSAMPROW cr[kCompressBatchSize / 2];
170 JSAMPARRAY planes[3] {y, cb, cr};
171
172 size_t y_plane_size = cinfo->image_width * cinfo->image_height;
173 size_t uv_plane_size = y_plane_size / 4;
174 uint8_t* y_plane = const_cast<uint8_t*>(yuv);
175 uint8_t* u_plane = const_cast<uint8_t*>(yuv + y_plane_size);
176 uint8_t* v_plane = const_cast<uint8_t*>(yuv + y_plane_size + uv_plane_size);
177 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
178 memset(empty.get(), 0, cinfo->image_width);
179
Ram Mohan4a72c192023-05-22 19:05:06 +0530180 const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
181 const bool is_width_aligned = (aligned_width == cinfo->image_width);
182 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
183 uint8_t* y_plane_intrm = nullptr;
184 uint8_t* u_plane_intrm = nullptr;
185 uint8_t* v_plane_intrm = nullptr;
186 JSAMPROW y_intrm[kCompressBatchSize];
187 JSAMPROW cb_intrm[kCompressBatchSize / 2];
188 JSAMPROW cr_intrm[kCompressBatchSize / 2];
189 JSAMPARRAY planes_intrm[3]{y_intrm, cb_intrm, cr_intrm};
190 if (!is_width_aligned) {
191 size_t mcu_row_size = aligned_width * kCompressBatchSize * 3 / 2;
192 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
193 y_plane_intrm = buffer_intrm.get();
194 u_plane_intrm = y_plane_intrm + (aligned_width * kCompressBatchSize);
195 v_plane_intrm = u_plane_intrm + (aligned_width * kCompressBatchSize) / 4;
196 for (int i = 0; i < kCompressBatchSize; ++i) {
197 y_intrm[i] = y_plane_intrm + i * aligned_width;
198 memset(y_intrm[i] + cinfo->image_width, 0, aligned_width - cinfo->image_width);
199 }
200 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
201 int offset_intrm = i * (aligned_width / 2);
202 cb_intrm[i] = u_plane_intrm + offset_intrm;
203 cr_intrm[i] = v_plane_intrm + offset_intrm;
204 memset(cb_intrm[i] + cinfo->image_width / 2, 0,
205 (aligned_width - cinfo->image_width) / 2);
206 memset(cr_intrm[i] + cinfo->image_width / 2, 0,
207 (aligned_width - cinfo->image_width) / 2);
208 }
209 }
210
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000211 while (cinfo->next_scanline < cinfo->image_height) {
212 for (int i = 0; i < kCompressBatchSize; ++i) {
213 size_t scanline = cinfo->next_scanline + i;
214 if (scanline < cinfo->image_height) {
215 y[i] = y_plane + scanline * cinfo->image_width;
216 } else {
217 y[i] = empty.get();
218 }
Ram Mohan4a72c192023-05-22 19:05:06 +0530219 if (!is_width_aligned) {
220 memcpy(y_intrm[i], y[i], cinfo->image_width);
221 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000222 }
223 // cb, cr only have half scanlines
224 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
225 size_t scanline = cinfo->next_scanline / 2 + i;
226 if (scanline < cinfo->image_height / 2) {
227 int offset = scanline * (cinfo->image_width / 2);
228 cb[i] = u_plane + offset;
229 cr[i] = v_plane + offset;
230 } else {
231 cb[i] = cr[i] = empty.get();
232 }
Ram Mohan4a72c192023-05-22 19:05:06 +0530233 if (!is_width_aligned) {
234 memcpy(cb_intrm[i], cb[i], cinfo->image_width / 2);
235 memcpy(cr_intrm[i], cr[i], cinfo->image_width / 2);
236 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000237 }
Ram Mohan4a72c192023-05-22 19:05:06 +0530238 int processed = jpeg_write_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
239 kCompressBatchSize);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000240 if (processed != kCompressBatchSize) {
241 ALOGE("Number of processed lines does not equal input lines.");
242 return false;
243 }
244 }
245 return true;
246}
247
Dichen Zhang02dd0592023-02-10 20:16:57 +0000248bool JpegEncoderHelper::compressSingleChannel(jpeg_compress_struct* cinfo, const uint8_t* image) {
Dichen Zhanga4819142022-10-21 04:12:30 +0000249 JSAMPROW y[kCompressBatchSize];
250 JSAMPARRAY planes[1] {y};
251
252 uint8_t* y_plane = const_cast<uint8_t*>(image);
253 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
254 memset(empty.get(), 0, cinfo->image_width);
255
Ram Mohan4a72c192023-05-22 19:05:06 +0530256 const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
257 bool is_width_aligned = (aligned_width == cinfo->image_width);
258 std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
259 uint8_t* y_plane_intrm = nullptr;
260 uint8_t* u_plane_intrm = nullptr;
261 JSAMPROW y_intrm[kCompressBatchSize];
262 JSAMPARRAY planes_intrm[]{y_intrm};
263 if (!is_width_aligned) {
264 size_t mcu_row_size = aligned_width * kCompressBatchSize;
265 buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
266 y_plane_intrm = buffer_intrm.get();
267 for (int i = 0; i < kCompressBatchSize; ++i) {
268 y_intrm[i] = y_plane_intrm + i * aligned_width;
269 memset(y_intrm[i] + cinfo->image_width, 0, aligned_width - cinfo->image_width);
270 }
271 }
272
Dichen Zhanga4819142022-10-21 04:12:30 +0000273 while (cinfo->next_scanline < cinfo->image_height) {
274 for (int i = 0; i < kCompressBatchSize; ++i) {
275 size_t scanline = cinfo->next_scanline + i;
276 if (scanline < cinfo->image_height) {
277 y[i] = y_plane + scanline * cinfo->image_width;
278 } else {
279 y[i] = empty.get();
280 }
Ram Mohan4a72c192023-05-22 19:05:06 +0530281 if (!is_width_aligned) {
282 memcpy(y_intrm[i], y[i], cinfo->image_width);
283 }
Dichen Zhanga4819142022-10-21 04:12:30 +0000284 }
Ram Mohan4a72c192023-05-22 19:05:06 +0530285 int processed = jpeg_write_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
286 kCompressBatchSize);
Dichen Zhanga4819142022-10-21 04:12:30 +0000287 if (processed != kCompressBatchSize / 2) {
288 ALOGE("Number of processed lines does not equal input lines.");
289 return false;
290 }
291 }
292 return true;
293}
294
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000295} // namespace ultrahdr