1 #include "CreateJavaOutputStreamAdaptor.h" 2 #include "SkJpegUtility.h" 3 #include "YuvToJpegEncoder.h" 4 #include <ui/PixelFormat.h> 5 #include <hardware/hardware.h> 6 7 #include <jni.h> 8 9 YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) { 10 // Only ImageFormat.NV21 and ImageFormat.YUY2 are supported 11 // for now. 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); 16 } else { 17 return NULL; 18 } 19 } 20 21 YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) { 22 } 23 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); 29 30 cinfo.err = jpeg_std_error(&sk_err); 31 sk_err.error_exit = skjpeg_error_exit; 32 if (setjmp(sk_err.fJmpBuf)) { 33 return false; 34 } 35 jpeg_create_compress(&cinfo); 36 37 cinfo.dest = &sk_wstream; 38 39 setJpegCompressStruct(&cinfo, width, height, jpegQuality); 40 41 jpeg_start_compress(&cinfo, TRUE); 42 43 compress(&cinfo, (uint8_t*) inYuv, offsets); 44 45 jpeg_finish_compress(&cinfo); 46 47 return true; 48 } 49 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); 57 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); 63 } 64 65 /////////////////////////////////////////////////////////////////// 66 Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) : 67 YuvToJpegEncoder(strides) { 68 fNumPlanes = 2; 69 } 70 71 void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo, 72 uint8_t* yuv, int* offsets) { 73 SkDebugf("onFlyCompress"); 74 JSAMPROW y[16]; 75 JSAMPROW cb[8]; 76 JSAMPROW cr[8]; 77 JSAMPARRAY planes[3]; 78 planes[0] = y; 79 planes[1] = cb; 80 planes[2] = cr; 81 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)]; 88 89 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); 94 95 // Jpeg library ignores the rows whose indices are greater than height. 96 for (int i = 0; i < 16; i++) { 97 // y row 98 y[i] = yPlanar + (cinfo->next_scanline + i) * fStrides[0]; 99 100 // construct u row and v row 101 if ((i & 1) == 0) { 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; 106 } 107 } 108 jpeg_write_raw_data(cinfo, planes, 16); 109 } 110 delete [] uRows; 111 delete [] vRows; 112 113 } 114 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]; 126 vu += 2; 127 } 128 } 129 } 130 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; 139 } 140 141 /////////////////////////////////////////////////////////////////////////////// 142 Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) : 143 YuvToJpegEncoder(strides) { 144 fNumPlanes = 1; 145 } 146 147 void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo, 148 uint8_t* yuv, int* offsets) { 149 SkDebugf("onFlyCompress_422"); 150 JSAMPROW y[16]; 151 JSAMPROW cb[16]; 152 JSAMPROW cr[16]; 153 JSAMPARRAY planes[3]; 154 planes[0] = y; 155 planes[1] = cb; 156 planes[2] = cr; 157 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)]; 163 164 uint8_t* yuvOffset = yuv + offsets[0]; 165 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); 169 170 // Jpeg library ignores the rows whose indices are greater than height. 171 for (int i = 0; i < 16; i++) { 172 // y row 173 y[i] = yRows + i * width; 174 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; 180 } 181 182 jpeg_write_raw_data(cinfo, planes, 16); 183 } 184 delete [] yRows; 185 delete [] uRows; 186 delete [] vRows; 187 } 188 189 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]; 203 yuvSeg += 4; 204 } 205 } 206 } 207 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; 216 } 217 /////////////////////////////////////////////////////////////////////////////// 218 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); 225 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); 232 delete encoder; 233 result = JNI_TRUE; 234 } 235 236 env->ReleaseByteArrayElements(inYuv, yuv, 0); 237 env->ReleaseIntArrayElements(offsets, imgOffsets, 0); 238 env->ReleaseIntArrayElements(strides, imgStrides, 0); 239 return result; 240 } 241 /////////////////////////////////////////////////////////////////////////////// 242 243 #include <android_runtime/AndroidRuntime.h> 244 245 static JNINativeMethod gYuvImageMethods[] = { 246 { "nativeCompressToJpeg", "([BIII[I[IILjava/io/OutputStream;[B)Z", 247 (void*)YuvImage_compressToJpeg } 248 }; 249 250 #define kClassPathName "android/graphics/YuvImage" 251 252 int register_android_graphics_YuvImage(JNIEnv* env) 253 { 254 return android::AndroidRuntime::registerNativeMethods(env, kClassPathName, 255 gYuvImageMethods, SK_ARRAY_COUNT(gYuvImageMethods)); 256 } 257