blob: 689cf0bea74175b4b6a24f8e363efb1776cd2269 [file] [log] [blame]
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001#include "CreateJavaOutputStreamAdaptor.h"
Matt Sarett79ad7be2016-03-25 14:28:40 -04002#include "SkJPEGWriteUtility.h"
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08003#include "YuvToJpegEncoder.h"
Mathias Agopian8f2423e2010-02-16 17:33:37 -08004#include <ui/PixelFormat.h>
5#include <hardware/hardware.h>
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08006
Derek Sollenbergerc5882c42019-10-25 11:11:32 -04007#include "graphics_jni_helpers.h"
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08008
9YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) {
Mathias Agopiana696f5d2010-02-17 17:53:09 -080010 // Only ImageFormat.NV21 and ImageFormat.YUY2 are supported
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080011 // for now.
Mathias Agopiana696f5d2010-02-17 17:53:09 -080012 if (format == HAL_PIXEL_FORMAT_YCrCb_420_SP) {
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080013 return new Yuv420SpToJpegEncoder(strides);
Mathias Agopian8f2423e2010-02-16 17:33:37 -080014 } else if (format == HAL_PIXEL_FORMAT_YCbCr_422_I) {
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080015 return new Yuv422IToJpegEncoder(strides);
16 } else {
17 return NULL;
18 }
19}
20
21YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) {
22}
23
Leon Scroggins III1c3ded392018-02-28 13:23:32 -050024struct ErrorMgr {
25 struct jpeg_error_mgr pub;
26 jmp_buf jmp;
27};
28
29void error_exit(j_common_ptr cinfo) {
30 ErrorMgr* err = (ErrorMgr*) cinfo->err;
31 (*cinfo->err->output_message) (cinfo);
32 longjmp(err->jmp, 1);
33}
34
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080035bool YuvToJpegEncoder::encode(SkWStream* stream, void* inYuv, int width,
36 int height, int* offsets, int jpegQuality) {
37 jpeg_compress_struct cinfo;
Leon Scroggins III1c3ded392018-02-28 13:23:32 -050038 ErrorMgr err;
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080039 skjpeg_destination_mgr sk_wstream(stream);
40
Leon Scroggins III1c3ded392018-02-28 13:23:32 -050041 cinfo.err = jpeg_std_error(&err.pub);
42 err.pub.error_exit = error_exit;
43
44 if (setjmp(err.jmp)) {
45 jpeg_destroy_compress(&cinfo);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080046 return false;
47 }
48 jpeg_create_compress(&cinfo);
49
50 cinfo.dest = &sk_wstream;
51
52 setJpegCompressStruct(&cinfo, width, height, jpegQuality);
53
54 jpeg_start_compress(&cinfo, TRUE);
55
56 compress(&cinfo, (uint8_t*) inYuv, offsets);
57
58 jpeg_finish_compress(&cinfo);
59
Leon Scroggins III1c3ded392018-02-28 13:23:32 -050060 jpeg_destroy_compress(&cinfo);
61
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080062 return true;
63}
64
65void YuvToJpegEncoder::setJpegCompressStruct(jpeg_compress_struct* cinfo,
66 int width, int height, int quality) {
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080067 cinfo->image_width = width;
68 cinfo->image_height = height;
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080069 cinfo->input_components = 3;
70 cinfo->in_color_space = JCS_YCbCr;
71 jpeg_set_defaults(cinfo);
Chia-chi Yehaa868592010-03-10 17:08:58 +080072
73 jpeg_set_quality(cinfo, quality, TRUE);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080074 jpeg_set_colorspace(cinfo, JCS_YCbCr);
75 cinfo->raw_data_in = TRUE;
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080076 cinfo->dct_method = JDCT_IFAST;
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080077 configSamplingFactors(cinfo);
78}
79
80///////////////////////////////////////////////////////////////////
81Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) :
82 YuvToJpegEncoder(strides) {
83 fNumPlanes = 2;
84}
85
86void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo,
87 uint8_t* yuv, int* offsets) {
88 SkDebugf("onFlyCompress");
89 JSAMPROW y[16];
90 JSAMPROW cb[8];
91 JSAMPROW cr[8];
92 JSAMPARRAY planes[3];
93 planes[0] = y;
94 planes[1] = cb;
95 planes[2] = cr;
96
97 int width = cinfo->image_width;
98 int height = cinfo->image_height;
99 uint8_t* yPlanar = yuv + offsets[0];
100 uint8_t* vuPlanar = yuv + offsets[1]; //width * height;
101 uint8_t* uRows = new uint8_t [8 * (width >> 1)];
102 uint8_t* vRows = new uint8_t [8 * (width >> 1)];
103
104
105 // process 16 lines of Y and 8 lines of U/V each time.
106 while (cinfo->next_scanline < cinfo->image_height) {
107 //deitnerleave u and v
Wu-cheng Li4b63f142012-10-16 23:40:03 +0800108 deinterleave(vuPlanar, uRows, vRows, cinfo->next_scanline, width, height);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800109
Wu-cheng Li4b63f142012-10-16 23:40:03 +0800110 // Jpeg library ignores the rows whose indices are greater than height.
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800111 for (int i = 0; i < 16; i++) {
112 // y row
113 y[i] = yPlanar + (cinfo->next_scanline + i) * fStrides[0];
114
115 // construct u row and v row
116 if ((i & 1) == 0) {
117 // height and width are both halved because of downsampling
118 int offset = (i >> 1) * (width >> 1);
119 cb[i/2] = uRows + offset;
120 cr[i/2] = vRows + offset;
121 }
122 }
123 jpeg_write_raw_data(cinfo, planes, 16);
124 }
125 delete [] uRows;
126 delete [] vRows;
127
128}
129
130void Yuv420SpToJpegEncoder::deinterleave(uint8_t* vuPlanar, uint8_t* uRows,
Wu-cheng Li4b63f142012-10-16 23:40:03 +0800131 uint8_t* vRows, int rowIndex, int width, int height) {
132 int numRows = (height - rowIndex) / 2;
133 if (numRows > 8) numRows = 8;
134 for (int row = 0; row < numRows; ++row) {
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800135 int offset = ((rowIndex >> 1) + row) * fStrides[1];
136 uint8_t* vu = vuPlanar + offset;
137 for (int i = 0; i < (width >> 1); ++i) {
138 int index = row * (width >> 1) + i;
139 uRows[index] = vu[1];
140 vRows[index] = vu[0];
141 vu += 2;
142 }
143 }
144}
145
146void Yuv420SpToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) {
147 // cb and cr are horizontally downsampled and vertically downsampled as well.
148 cinfo->comp_info[0].h_samp_factor = 2;
149 cinfo->comp_info[0].v_samp_factor = 2;
150 cinfo->comp_info[1].h_samp_factor = 1;
151 cinfo->comp_info[1].v_samp_factor = 1;
152 cinfo->comp_info[2].h_samp_factor = 1;
153 cinfo->comp_info[2].v_samp_factor = 1;
154}
155
156///////////////////////////////////////////////////////////////////////////////
157Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) :
158 YuvToJpegEncoder(strides) {
159 fNumPlanes = 1;
160}
161
162void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo,
163 uint8_t* yuv, int* offsets) {
164 SkDebugf("onFlyCompress_422");
165 JSAMPROW y[16];
166 JSAMPROW cb[16];
167 JSAMPROW cr[16];
168 JSAMPARRAY planes[3];
169 planes[0] = y;
170 planes[1] = cb;
171 planes[2] = cr;
172
173 int width = cinfo->image_width;
174 int height = cinfo->image_height;
175 uint8_t* yRows = new uint8_t [16 * width];
176 uint8_t* uRows = new uint8_t [16 * (width >> 1)];
177 uint8_t* vRows = new uint8_t [16 * (width >> 1)];
178
179 uint8_t* yuvOffset = yuv + offsets[0];
180
181 // process 16 lines of Y and 16 lines of U/V each time.
182 while (cinfo->next_scanline < cinfo->image_height) {
183 deinterleave(yuvOffset, yRows, uRows, vRows, cinfo->next_scanline, width, height);
184
Wu-cheng Li4b63f142012-10-16 23:40:03 +0800185 // Jpeg library ignores the rows whose indices are greater than height.
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800186 for (int i = 0; i < 16; i++) {
187 // y row
188 y[i] = yRows + i * width;
189
190 // construct u row and v row
191 // width is halved because of downsampling
192 int offset = i * (width >> 1);
193 cb[i] = uRows + offset;
194 cr[i] = vRows + offset;
195 }
196
197 jpeg_write_raw_data(cinfo, planes, 16);
198 }
199 delete [] yRows;
200 delete [] uRows;
201 delete [] vRows;
202}
203
204
205void Yuv422IToJpegEncoder::deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows,
206 uint8_t* vRows, int rowIndex, int width, int height) {
Wu-cheng Li4b63f142012-10-16 23:40:03 +0800207 int numRows = height - rowIndex;
208 if (numRows > 16) numRows = 16;
209 for (int row = 0; row < numRows; ++row) {
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800210 uint8_t* yuvSeg = yuv + (rowIndex + row) * fStrides[0];
211 for (int i = 0; i < (width >> 1); ++i) {
212 int indexY = row * width + (i << 1);
213 int indexU = row * (width >> 1) + i;
214 yRows[indexY] = yuvSeg[0];
215 yRows[indexY + 1] = yuvSeg[2];
216 uRows[indexU] = yuvSeg[1];
217 vRows[indexU] = yuvSeg[3];
218 yuvSeg += 4;
219 }
220 }
221}
222
223void Yuv422IToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) {
224 // cb and cr are horizontally downsampled and vertically downsampled as well.
225 cinfo->comp_info[0].h_samp_factor = 2;
226 cinfo->comp_info[0].v_samp_factor = 2;
227 cinfo->comp_info[1].h_samp_factor = 1;
228 cinfo->comp_info[1].v_samp_factor = 2;
229 cinfo->comp_info[2].h_samp_factor = 1;
230 cinfo->comp_info[2].v_samp_factor = 2;
231}
232///////////////////////////////////////////////////////////////////////////////
233
234static jboolean YuvImage_compressToJpeg(JNIEnv* env, jobject, jbyteArray inYuv,
Ashok Bhat39029b22014-01-10 16:24:38 +0000235 jint format, jint width, jint height, jintArray offsets,
236 jintArray strides, jint jpegQuality, jobject jstream,
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800237 jbyteArray jstorage) {
238 jbyte* yuv = env->GetByteArrayElements(inYuv, NULL);
239 SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
240
241 jint* imgOffsets = env->GetIntArrayElements(offsets, NULL);
242 jint* imgStrides = env->GetIntArrayElements(strides, NULL);
243 YuvToJpegEncoder* encoder = YuvToJpegEncoder::create(format, imgStrides);
Pawel Augustyn7c68a4072012-07-16 11:23:54 +0200244 jboolean result = JNI_FALSE;
245 if (encoder != NULL) {
246 encoder->encode(strm, yuv, width, height, imgOffsets, jpegQuality);
247 delete encoder;
248 result = JNI_TRUE;
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800249 }
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800250
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800251 env->ReleaseByteArrayElements(inYuv, yuv, 0);
252 env->ReleaseIntArrayElements(offsets, imgOffsets, 0);
253 env->ReleaseIntArrayElements(strides, imgStrides, 0);
Martin Wallgrend8659002014-08-20 14:58:58 +0200254 delete strm;
Pawel Augustyn7c68a4072012-07-16 11:23:54 +0200255 return result;
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800256}
257///////////////////////////////////////////////////////////////////////////////
258
Daniel Micay76f6a862015-09-19 17:31:01 -0400259static const JNINativeMethod gYuvImageMethods[] = {
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800260 { "nativeCompressToJpeg", "([BIII[I[IILjava/io/OutputStream;[B)Z",
261 (void*)YuvImage_compressToJpeg }
262};
263
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800264int register_android_graphics_YuvImage(JNIEnv* env)
265{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800266 return android::RegisterMethodsOrDie(env, "android/graphics/YuvImage", gYuvImageMethods,
267 NELEM(gYuvImageMethods));
Wei-Ta Chenbca2d612009-11-30 17:52:05 +0800268}