blob: 867ffbb4e9632d8e04682db09b5525e86a9a278b [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
Dichen Zhang02dd0592023-02-10 20:16:57 +000025// The destination manager that can access |mResultBuffer| in JpegEncoderHelper.
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000026struct destination_mgr {
27public:
28 struct jpeg_destination_mgr mgr;
Dichen Zhang02dd0592023-02-10 20:16:57 +000029 JpegEncoderHelper* encoder;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000030};
31
Dichen Zhang02dd0592023-02-10 20:16:57 +000032JpegEncoderHelper::JpegEncoderHelper() {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000033}
34
Dichen Zhang02dd0592023-02-10 20:16:57 +000035JpegEncoderHelper::~JpegEncoderHelper() {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +000036}
37
Dichen Zhang02dd0592023-02-10 20:16:57 +000038bool JpegEncoderHelper::compressImage(const void* image, int width, int height, int quality,
Dichen Zhanga4819142022-10-21 04:12:30 +000039 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 }
45 ALOGI("Compressed JPEG: %d[%dx%d] -> %zu bytes",
46 (width * height * 12) / 8, width, height, mResultBuffer.size());
47 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 */
86 (*cinfo->err->format_message) (cinfo, buffer);
87 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,
Dichen Zhanga4819142022-10-21 04:12:30 +000091 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
Dichen Zhanga4819142022-10-21 04:12:30 +0000108 if (!compress(&cinfo, static_cast<const uint8_t*>(image), isSingleChannel)) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000109 return false;
110 }
111 jpeg_finish_compress(&cinfo);
112 jpeg_destroy_compress(&cinfo);
113 return true;
114}
115
Dichen Zhang02dd0592023-02-10 20:16:57 +0000116void JpegEncoderHelper::setJpegDestination(jpeg_compress_struct* cinfo) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000117 destination_mgr* dest = static_cast<struct destination_mgr *>((*cinfo->mem->alloc_small) (
118 (j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(destination_mgr)));
119 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,
Dichen Zhanga4819142022-10-21 04:12:30 +0000127 jpeg_compress_struct* cinfo, bool isSingleChannel) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000128 cinfo->image_width = width;
129 cinfo->image_height = height;
Dichen Zhanga4819142022-10-21 04:12:30 +0000130 if (isSingleChannel) {
131 cinfo->input_components = 1;
132 cinfo->in_color_space = JCS_GRAYSCALE;
133 } else {
134 cinfo->input_components = 3;
135 cinfo->in_color_space = JCS_YCbCr;
136 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000137 jpeg_set_defaults(cinfo);
138
139 jpeg_set_quality(cinfo, quality, TRUE);
Dichen Zhanga4819142022-10-21 04:12:30 +0000140 jpeg_set_colorspace(cinfo, isSingleChannel ? JCS_GRAYSCALE : JCS_YCbCr);
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000141 cinfo->raw_data_in = TRUE;
Ram Mohan1790bb52023-06-11 07:07:27 +0530142 cinfo->dct_method = JDCT_ISLOW;
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000143
Dichen Zhanga4819142022-10-21 04:12:30 +0000144 if (!isSingleChannel) {
145 // Configure sampling factors. The sampling factor is JPEG subsampling 420 because the
146 // source format is YUV420.
147 cinfo->comp_info[0].h_samp_factor = 2;
148 cinfo->comp_info[0].v_samp_factor = 2;
149 cinfo->comp_info[1].h_samp_factor = 1;
150 cinfo->comp_info[1].v_samp_factor = 1;
151 cinfo->comp_info[2].h_samp_factor = 1;
152 cinfo->comp_info[2].v_samp_factor = 1;
153 }
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000154}
155
Dichen Zhang02dd0592023-02-10 20:16:57 +0000156bool JpegEncoderHelper::compress(
Dichen Zhanga4819142022-10-21 04:12:30 +0000157 jpeg_compress_struct* cinfo, const uint8_t* image, bool isSingleChannel) {
158 if (isSingleChannel) {
159 return compressSingleChannel(cinfo, image);
160 }
161 return compressYuv(cinfo, image);
162}
163
Dichen Zhang02dd0592023-02-10 20:16:57 +0000164bool JpegEncoderHelper::compressYuv(jpeg_compress_struct* cinfo, const uint8_t* yuv) {
Dichen Zhang4a66b3c2022-10-19 18:04:47 +0000165 JSAMPROW y[kCompressBatchSize];
166 JSAMPROW cb[kCompressBatchSize / 2];
167 JSAMPROW cr[kCompressBatchSize / 2];
168 JSAMPARRAY planes[3] {y, cb, cr};
169
170 size_t y_plane_size = cinfo->image_width * cinfo->image_height;
171 size_t uv_plane_size = y_plane_size / 4;
172 uint8_t* y_plane = const_cast<uint8_t*>(yuv);
173 uint8_t* u_plane = const_cast<uint8_t*>(yuv + y_plane_size);
174 uint8_t* v_plane = const_cast<uint8_t*>(yuv + y_plane_size + uv_plane_size);
175 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
176 memset(empty.get(), 0, cinfo->image_width);
177
178 while (cinfo->next_scanline < cinfo->image_height) {
179 for (int i = 0; i < kCompressBatchSize; ++i) {
180 size_t scanline = cinfo->next_scanline + i;
181 if (scanline < cinfo->image_height) {
182 y[i] = y_plane + scanline * cinfo->image_width;
183 } else {
184 y[i] = empty.get();
185 }
186 }
187 // cb, cr only have half scanlines
188 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
189 size_t scanline = cinfo->next_scanline / 2 + i;
190 if (scanline < cinfo->image_height / 2) {
191 int offset = scanline * (cinfo->image_width / 2);
192 cb[i] = u_plane + offset;
193 cr[i] = v_plane + offset;
194 } else {
195 cb[i] = cr[i] = empty.get();
196 }
197 }
198
199 int processed = jpeg_write_raw_data(cinfo, planes, kCompressBatchSize);
200 if (processed != kCompressBatchSize) {
201 ALOGE("Number of processed lines does not equal input lines.");
202 return false;
203 }
204 }
205 return true;
206}
207
Dichen Zhang02dd0592023-02-10 20:16:57 +0000208bool JpegEncoderHelper::compressSingleChannel(jpeg_compress_struct* cinfo, const uint8_t* image) {
Dichen Zhanga4819142022-10-21 04:12:30 +0000209 JSAMPROW y[kCompressBatchSize];
210 JSAMPARRAY planes[1] {y};
211
212 uint8_t* y_plane = const_cast<uint8_t*>(image);
213 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
214 memset(empty.get(), 0, cinfo->image_width);
215
216 while (cinfo->next_scanline < cinfo->image_height) {
217 for (int i = 0; i < kCompressBatchSize; ++i) {
218 size_t scanline = cinfo->next_scanline + i;
219 if (scanline < cinfo->image_height) {
220 y[i] = y_plane + scanline * cinfo->image_width;
221 } else {
222 y[i] = empty.get();
223 }
224 }
225 int processed = jpeg_write_raw_data(cinfo, planes, kCompressBatchSize);
226 if (processed != kCompressBatchSize / 2) {
227 ALOGE("Number of processed lines does not equal input lines.");
228 return false;
229 }
230 }
231 return true;
232}
233
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000234} // namespace ultrahdr