blob: 22a5389648b9684081535cea1f4a874074a21bae [file] [log] [blame]
Dichen Zhang0d12ba82022-10-19 20:44:51 +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
17#include <jpegrecoverymap/jpegdecoder.h>
18
19#include <cutils/log.h>
20
21#include <errno.h>
22#include <setjmp.h>
23
24namespace android::recoverymap {
25struct jpegr_source_mgr : jpeg_source_mgr {
26 jpegr_source_mgr(const uint8_t* ptr, int len);
27 ~jpegr_source_mgr();
28
29 const uint8_t* mBufferPtr;
30 size_t mBufferLength;
31};
32
33struct jpegrerror_mgr {
34 struct jpeg_error_mgr pub;
35 jmp_buf setjmp_buffer;
36};
37
38static void jpegr_init_source(j_decompress_ptr cinfo) {
39 jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
40 src->next_input_byte = static_cast<const JOCTET*>(src->mBufferPtr);
41 src->bytes_in_buffer = src->mBufferLength;
42}
43
44static boolean jpegr_fill_input_buffer(j_decompress_ptr /* cinfo */) {
45 ALOGE("%s : should not get here", __func__);
46 return FALSE;
47}
48
49static void jpegr_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
50 jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
51
52 if (num_bytes > static_cast<long>(src->bytes_in_buffer)) {
53 ALOGE("jpegr_skip_input_data - num_bytes > (long)src->bytes_in_buffer");
54 } else {
55 src->next_input_byte += num_bytes;
56 src->bytes_in_buffer -= num_bytes;
57 }
58}
59
60static void jpegr_term_source(j_decompress_ptr /*cinfo*/) {}
61
62jpegr_source_mgr::jpegr_source_mgr(const uint8_t* ptr, int len) :
63 mBufferPtr(ptr), mBufferLength(len) {
64 init_source = jpegr_init_source;
65 fill_input_buffer = jpegr_fill_input_buffer;
66 skip_input_data = jpegr_skip_input_data;
67 resync_to_restart = jpeg_resync_to_restart;
68 term_source = jpegr_term_source;
69}
70
71jpegr_source_mgr::~jpegr_source_mgr() {}
72
73static void jpegrerror_exit(j_common_ptr cinfo) {
74 jpegrerror_mgr* err = reinterpret_cast<jpegrerror_mgr*>(cinfo->err);
75 longjmp(err->setjmp_buffer, 1);
76}
77
78JpegDecoder::JpegDecoder() {
79}
80
81JpegDecoder::~JpegDecoder() {
82}
83
84bool JpegDecoder::decompressImage(const void* image, int length) {
85 if (image == nullptr || length <= 0) {
86 ALOGE("Image size can not be handled: %d", length);
87 return false;
88 }
89
90 mResultBuffer.clear();
91 if (!decode(image, length)) {
92 return false;
93 }
94
95 return true;
96}
97
98const void* JpegDecoder::getDecompressedImagePtr() {
99 return mResultBuffer.data();
100}
101
102size_t JpegDecoder::getDecompressedImageSize() {
103 return mResultBuffer.size();
104}
105
106bool JpegDecoder::decode(const void* image, int length) {
107 jpeg_decompress_struct cinfo;
108 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
109 jpegrerror_mgr myerr;
110 cinfo.err = jpeg_std_error(&myerr.pub);
111 myerr.pub.error_exit = jpegrerror_exit;
112
113 if (setjmp(myerr.setjmp_buffer)) {
114 jpeg_destroy_decompress(&cinfo);
115 return false;
116 }
117 jpeg_create_decompress(&cinfo);
118
119 cinfo.src = &mgr;
120 jpeg_read_header(&cinfo, TRUE);
121
122 if (cinfo.jpeg_color_space == JCS_YCbCr) {
123 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 3 / 2, 0);
124 } else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
125 mResultBuffer.resize(cinfo.image_width * cinfo.image_height, 0);
126 }
127
128 cinfo.raw_data_out = TRUE;
129 cinfo.dct_method = JDCT_IFAST;
130 cinfo.out_color_space = cinfo.jpeg_color_space;
131
132 jpeg_start_decompress(&cinfo);
133
134 if (!decompress(&cinfo, static_cast<const uint8_t*>(mResultBuffer.data()),
135 cinfo.jpeg_color_space == JCS_GRAYSCALE)) {
136 return false;
137 }
138
139 jpeg_finish_decompress(&cinfo);
140 jpeg_destroy_decompress(&cinfo);
141
142 return true;
143}
144
145bool JpegDecoder::decompress(jpeg_decompress_struct* cinfo, const uint8_t* dest,
146 bool isSingleChannel) {
147 if (isSingleChannel) {
148 return decompressSingleChannel(cinfo, dest);
149 }
150 return decompressYUV(cinfo, dest);
151}
152
153bool JpegDecoder::decompressYUV(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
154
155 JSAMPROW y[kCompressBatchSize];
156 JSAMPROW cb[kCompressBatchSize / 2];
157 JSAMPROW cr[kCompressBatchSize / 2];
158 JSAMPARRAY planes[3] {y, cb, cr};
159
160 size_t y_plane_size = cinfo->image_width * cinfo->image_height;
161 size_t uv_plane_size = y_plane_size / 4;
162 uint8_t* y_plane = const_cast<uint8_t*>(dest);
163 uint8_t* u_plane = const_cast<uint8_t*>(dest + y_plane_size);
164 uint8_t* v_plane = const_cast<uint8_t*>(dest + y_plane_size + uv_plane_size);
165 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
166 memset(empty.get(), 0, cinfo->image_width);
167
168 while (cinfo->output_scanline < cinfo->image_height) {
169 for (int i = 0; i < kCompressBatchSize; ++i) {
170 size_t scanline = cinfo->output_scanline + i;
171 if (scanline < cinfo->image_height) {
172 y[i] = y_plane + scanline * cinfo->image_width;
173 } else {
174 y[i] = empty.get();
175 }
176 }
177 // cb, cr only have half scanlines
178 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
179 size_t scanline = cinfo->output_scanline / 2 + i;
180 if (scanline < cinfo->image_height / 2) {
181 int offset = scanline * (cinfo->image_width / 2);
182 cb[i] = u_plane + offset;
183 cr[i] = v_plane + offset;
184 } else {
185 cb[i] = cr[i] = empty.get();
186 }
187 }
188
189 int processed = jpeg_read_raw_data(cinfo, planes, kCompressBatchSize);
190 if (processed != kCompressBatchSize) {
191 ALOGE("Number of processed lines does not equal input lines.");
192 return false;
193 }
194 }
195 return true;
196}
197
198bool JpegDecoder::decompressSingleChannel(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
199 JSAMPROW y[kCompressBatchSize];
200 JSAMPARRAY planes[1] {y};
201
202 uint8_t* y_plane = const_cast<uint8_t*>(dest);
203 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
204 memset(empty.get(), 0, cinfo->image_width);
205
206 while (cinfo->output_scanline < cinfo->image_height) {
207 for (int i = 0; i < kCompressBatchSize; ++i) {
208 size_t scanline = cinfo->output_scanline + i;
209 if (scanline < cinfo->image_height) {
210 y[i] = y_plane + scanline * cinfo->image_width;
211 } else {
212 y[i] = empty.get();
213 }
214 }
215
216 int processed = jpeg_read_raw_data(cinfo, planes, kCompressBatchSize);
217 if (processed != kCompressBatchSize / 2) {
218 ALOGE("Number of processed lines does not equal input lines.");
219 return false;
220 }
221 }
222 return true;
223}
224
225} // namespace android