1 #include "CreateJavaOutputStreamAdaptor.h"
2 #include "SkJpegUtility.h"
3 #include "YuvToJpegEncoder.h"
4 #include <ui/PixelFormat.h>
5 #include <hardware/hardware.h>
9 YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) {
10 // Only ImageFormat.NV21 and ImageFormat.YUY2 are supported
12 if (format == HAL_PIXEL_FORMAT_YCrCb_420_SP) {
13 return new Yuv420SpToJpegEncoder(strides);
14 } else if (format == HAL_PIXEL_FORMAT_YCbCr_422_I) {
15 return new Yuv422IToJpegEncoder(strides);
21 YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) {
24 bool YuvToJpegEncoder::encode(SkWStream* stream, void* inYuv, int width,
25 int height, int* offsets, int jpegQuality) {
26 jpeg_compress_struct cinfo;
27 skjpeg_error_mgr sk_err;
28 skjpeg_destination_mgr sk_wstream(stream);
30 cinfo.err = jpeg_std_error(&sk_err);
31 sk_err.error_exit = skjpeg_error_exit;
32 if (setjmp(sk_err.fJmpBuf)) {
35 jpeg_create_compress(&cinfo);
37 cinfo.dest = &sk_wstream;
39 setJpegCompressStruct(&cinfo, width, height, jpegQuality);
41 jpeg_start_compress(&cinfo, TRUE);
43 compress(&cinfo, (uint8_t*) inYuv, offsets);
45 jpeg_finish_compress(&cinfo);
50 void YuvToJpegEncoder::setJpegCompressStruct(jpeg_compress_struct* cinfo,
51 int width, int height, int quality) {
52 cinfo->image_width = width;
53 cinfo->image_height = height;
54 cinfo->input_components = 3;
55 cinfo->in_color_space = JCS_YCbCr;
56 jpeg_set_defaults(cinfo);
58 jpeg_set_quality(cinfo, quality, TRUE);
59 jpeg_set_colorspace(cinfo, JCS_YCbCr);
60 cinfo->raw_data_in = TRUE;
61 cinfo->dct_method = JDCT_IFAST;
62 configSamplingFactors(cinfo);
65 ///////////////////////////////////////////////////////////////////
66 Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) :
67 YuvToJpegEncoder(strides) {
71 void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo,
72 uint8_t* yuv, int* offsets) {
73 SkDebugf("onFlyCompress");
82 int width = cinfo->image_width;
83 int height = cinfo->image_height;
84 uint8_t* yPlanar = yuv + offsets[0];
85 uint8_t* vuPlanar = yuv + offsets[1]; //width * height;
86 uint8_t* uRows = new uint8_t [8 * (width >> 1)];
87 uint8_t* vRows = new uint8_t [8 * (width >> 1)];
90 // process 16 lines of Y and 8 lines of U/V each time.
91 while (cinfo->next_scanline < cinfo->image_height) {
92 //deitnerleave u and v
93 deinterleave(vuPlanar, uRows, vRows, cinfo->next_scanline, width, height);
95 // Jpeg library ignores the rows whose indices are greater than height.
96 for (int i = 0; i < 16; i++) {
98 y[i] = yPlanar + (cinfo->next_scanline + i) * fStrides[0];
100 // construct u row and v row
102 // height and width are both halved because of downsampling
103 int offset = (i >> 1) * (width >> 1);
104 cb[i/2] = uRows + offset;
105 cr[i/2] = vRows + offset;
108 jpeg_write_raw_data(cinfo, planes, 16);
115 void Yuv420SpToJpegEncoder::deinterleave(uint8_t* vuPlanar, uint8_t* uRows,
116 uint8_t* vRows, int rowIndex, int width, int height) {
117 int numRows = (height - rowIndex) / 2;
118 if (numRows > 8) numRows = 8;
119 for (int row = 0; row < numRows; ++row) {
120 int offset = ((rowIndex >> 1) + row) * fStrides[1];
121 uint8_t* vu = vuPlanar + offset;
122 for (int i = 0; i < (width >> 1); ++i) {
123 int index = row * (width >> 1) + i;
124 uRows[index] = vu[1];
125 vRows[index] = vu[0];
131 void Yuv420SpToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) {
132 // cb and cr are horizontally downsampled and vertically downsampled as well.
133 cinfo->comp_info[0].h_samp_factor = 2;
134 cinfo->comp_info[0].v_samp_factor = 2;
135 cinfo->comp_info[1].h_samp_factor = 1;
136 cinfo->comp_info[1].v_samp_factor = 1;
137 cinfo->comp_info[2].h_samp_factor = 1;
138 cinfo->comp_info[2].v_samp_factor = 1;
141 ///////////////////////////////////////////////////////////////////////////////
142 Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) :
143 YuvToJpegEncoder(strides) {
147 void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo,
148 uint8_t* yuv, int* offsets) {
149 SkDebugf("onFlyCompress_422");
153 JSAMPARRAY planes[3];
158 int width = cinfo->image_width;
159 int height = cinfo->image_height;
160 uint8_t* yRows = new uint8_t [16 * width];
161 uint8_t* uRows = new uint8_t [16 * (width >> 1)];
162 uint8_t* vRows = new uint8_t [16 * (width >> 1)];
164 uint8_t* yuvOffset = yuv + offsets[0];
166 // process 16 lines of Y and 16 lines of U/V each time.
167 while (cinfo->next_scanline < cinfo->image_height) {
168 deinterleave(yuvOffset, yRows, uRows, vRows, cinfo->next_scanline, width, height);
170 // Jpeg library ignores the rows whose indices are greater than height.
171 for (int i = 0; i < 16; i++) {
173 y[i] = yRows + i * width;
175 // construct u row and v row
176 // width is halved because of downsampling
177 int offset = i * (width >> 1);
178 cb[i] = uRows + offset;
179 cr[i] = vRows + offset;
182 jpeg_write_raw_data(cinfo, planes, 16);
190 void Yuv422IToJpegEncoder::deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows,
191 uint8_t* vRows, int rowIndex, int width, int height) {
192 int numRows = height - rowIndex;
193 if (numRows > 16) numRows = 16;
194 for (int row = 0; row < numRows; ++row) {
195 uint8_t* yuvSeg = yuv + (rowIndex + row) * fStrides[0];
196 for (int i = 0; i < (width >> 1); ++i) {
197 int indexY = row * width + (i << 1);
198 int indexU = row * (width >> 1) + i;
199 yRows[indexY] = yuvSeg[0];
200 yRows[indexY + 1] = yuvSeg[2];
201 uRows[indexU] = yuvSeg[1];
202 vRows[indexU] = yuvSeg[3];
208 void Yuv422IToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) {
209 // cb and cr are horizontally downsampled and vertically downsampled as well.
210 cinfo->comp_info[0].h_samp_factor = 2;
211 cinfo->comp_info[0].v_samp_factor = 2;
212 cinfo->comp_info[1].h_samp_factor = 1;
213 cinfo->comp_info[1].v_samp_factor = 2;
214 cinfo->comp_info[2].h_samp_factor = 1;
215 cinfo->comp_info[2].v_samp_factor = 2;
217 ///////////////////////////////////////////////////////////////////////////////
219 static jboolean YuvImage_compressToJpeg(JNIEnv* env, jobject, jbyteArray inYuv,
220 jint format, jint width, jint height, jintArray offsets,
221 jintArray strides, jint jpegQuality, jobject jstream,
222 jbyteArray jstorage) {
223 jbyte* yuv = env->GetByteArrayElements(inYuv, NULL);
224 SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
226 jint* imgOffsets = env->GetIntArrayElements(offsets, NULL);
227 jint* imgStrides = env->GetIntArrayElements(strides, NULL);
228 YuvToJpegEncoder* encoder = YuvToJpegEncoder::create(format, imgStrides);
229 jboolean result = JNI_FALSE;
230 if (encoder != NULL) {
231 encoder->encode(strm, yuv, width, height, imgOffsets, jpegQuality);
236 env->ReleaseByteArrayElements(inYuv, yuv, 0);
237 env->ReleaseIntArrayElements(offsets, imgOffsets, 0);
238 env->ReleaseIntArrayElements(strides, imgStrides, 0);
241 ///////////////////////////////////////////////////////////////////////////////
243 #include <android_runtime/AndroidRuntime.h>
245 static JNINativeMethod gYuvImageMethods[] = {
246 { "nativeCompressToJpeg", "([BIII[I[IILjava/io/OutputStream;[B)Z",
247 (void*)YuvImage_compressToJpeg }
250 #define kClassPathName "android/graphics/YuvImage"
252 int register_android_graphics_YuvImage(JNIEnv* env)
254 return android::AndroidRuntime::registerNativeMethods(env, kClassPathName,
255 gYuvImageMethods, SK_ARRAY_COUNT(gYuvImageMethods));