YuvToJpegEncoder.cpp revision bca2d613e0d6d2630fedd302c0d779b7610adbcf
1#include "CreateJavaOutputStreamAdaptor.h" 2#include "SkJpegUtility.h" 3#include "YuvToJpegEncoder.h" 4#include "ui/PixelFormat.h" 5 6#include <jni.h> 7 8YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) { 9 // Only PIXEL_FORMAT_YCbCr_420_SP and PIXEl_FOMAT_YCbCr_422_I are supported 10 // for now. 11 if (format == android::PIXEL_FORMAT_YCbCr_420_SP) { 12 return new Yuv420SpToJpegEncoder(strides); 13 } else if (format == android::PIXEL_FORMAT_YCbCr_422_I) { 14 return new Yuv422IToJpegEncoder(strides); 15 } else { 16 return NULL; 17 } 18} 19 20YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) { 21} 22 23bool YuvToJpegEncoder::encode(SkWStream* stream, void* inYuv, int width, 24 int height, int* offsets, int jpegQuality) { 25 jpeg_compress_struct cinfo; 26 skjpeg_error_mgr sk_err; 27 skjpeg_destination_mgr sk_wstream(stream); 28 29 cinfo.err = jpeg_std_error(&sk_err); 30 sk_err.error_exit = skjpeg_error_exit; 31 if (setjmp(sk_err.fJmpBuf)) { 32 return false; 33 } 34 jpeg_create_compress(&cinfo); 35 36 cinfo.dest = &sk_wstream; 37 38 setJpegCompressStruct(&cinfo, width, height, jpegQuality); 39 40 jpeg_start_compress(&cinfo, TRUE); 41 42 compress(&cinfo, (uint8_t*) inYuv, offsets); 43 44 jpeg_finish_compress(&cinfo); 45 46 return true; 47} 48 49void YuvToJpegEncoder::setJpegCompressStruct(jpeg_compress_struct* cinfo, 50 int width, int height, int quality) { 51 jpeg_set_quality(cinfo, quality, TRUE); 52 53 cinfo->image_width = width; 54 cinfo->image_height = height; 55 56 cinfo->input_components = 3; 57 cinfo->in_color_space = JCS_YCbCr; 58 jpeg_set_defaults(cinfo); 59 jpeg_set_colorspace(cinfo, JCS_YCbCr); 60 cinfo->raw_data_in = TRUE; 61 62 cinfo->dct_method = JDCT_IFAST; 63 64 configSamplingFactors(cinfo); 65} 66 67/////////////////////////////////////////////////////////////////// 68Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) : 69 YuvToJpegEncoder(strides) { 70 fNumPlanes = 2; 71} 72 73void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo, 74 uint8_t* yuv, int* offsets) { 75 SkDebugf("onFlyCompress"); 76 JSAMPROW y[16]; 77 JSAMPROW cb[8]; 78 JSAMPROW cr[8]; 79 JSAMPARRAY planes[3]; 80 planes[0] = y; 81 planes[1] = cb; 82 planes[2] = cr; 83 84 int width = cinfo->image_width; 85 int height = cinfo->image_height; 86 uint8_t* yPlanar = yuv + offsets[0]; 87 uint8_t* vuPlanar = yuv + offsets[1]; //width * height; 88 uint8_t* uRows = new uint8_t [8 * (width >> 1)]; 89 uint8_t* vRows = new uint8_t [8 * (width >> 1)]; 90 91 92 // process 16 lines of Y and 8 lines of U/V each time. 93 while (cinfo->next_scanline < cinfo->image_height) { 94 //deitnerleave u and v 95 deinterleave(vuPlanar, uRows, vRows, cinfo->next_scanline, width); 96 97 for (int i = 0; i < 16; i++) { 98 // y row 99 y[i] = yPlanar + (cinfo->next_scanline + i) * fStrides[0]; 100 101 // construct u row and v row 102 if ((i & 1) == 0) { 103 // height and width are both halved because of downsampling 104 int offset = (i >> 1) * (width >> 1); 105 cb[i/2] = uRows + offset; 106 cr[i/2] = vRows + offset; 107 } 108 } 109 jpeg_write_raw_data(cinfo, planes, 16); 110 } 111 delete [] uRows; 112 delete [] vRows; 113 114} 115 116void Yuv420SpToJpegEncoder::deinterleave(uint8_t* vuPlanar, uint8_t* uRows, 117 uint8_t* vRows, int rowIndex, int width) { 118 for (int row = 0; row < 8; ++row) { 119 int offset = ((rowIndex >> 1) + row) * fStrides[1]; 120 uint8_t* vu = vuPlanar + offset; 121 for (int i = 0; i < (width >> 1); ++i) { 122 int index = row * (width >> 1) + i; 123 uRows[index] = vu[1]; 124 vRows[index] = vu[0]; 125 vu += 2; 126 } 127 } 128} 129 130void Yuv420SpToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) { 131 // cb and cr are horizontally downsampled and vertically downsampled as well. 132 cinfo->comp_info[0].h_samp_factor = 2; 133 cinfo->comp_info[0].v_samp_factor = 2; 134 cinfo->comp_info[1].h_samp_factor = 1; 135 cinfo->comp_info[1].v_samp_factor = 1; 136 cinfo->comp_info[2].h_samp_factor = 1; 137 cinfo->comp_info[2].v_samp_factor = 1; 138} 139 140/////////////////////////////////////////////////////////////////////////////// 141Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) : 142 YuvToJpegEncoder(strides) { 143 fNumPlanes = 1; 144} 145 146void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo, 147 uint8_t* yuv, int* offsets) { 148 SkDebugf("onFlyCompress_422"); 149 JSAMPROW y[16]; 150 JSAMPROW cb[16]; 151 JSAMPROW cr[16]; 152 JSAMPARRAY planes[3]; 153 planes[0] = y; 154 planes[1] = cb; 155 planes[2] = cr; 156 157 int width = cinfo->image_width; 158 int height = cinfo->image_height; 159 uint8_t* yRows = new uint8_t [16 * width]; 160 uint8_t* uRows = new uint8_t [16 * (width >> 1)]; 161 uint8_t* vRows = new uint8_t [16 * (width >> 1)]; 162 163 uint8_t* yuvOffset = yuv + offsets[0]; 164 165 // process 16 lines of Y and 16 lines of U/V each time. 166 while (cinfo->next_scanline < cinfo->image_height) { 167 deinterleave(yuvOffset, yRows, uRows, vRows, cinfo->next_scanline, width, height); 168 169 for (int i = 0; i < 16; i++) { 170 // y row 171 y[i] = yRows + i * width; 172 173 // construct u row and v row 174 // width is halved because of downsampling 175 int offset = i * (width >> 1); 176 cb[i] = uRows + offset; 177 cr[i] = vRows + offset; 178 } 179 180 jpeg_write_raw_data(cinfo, planes, 16); 181 } 182 delete [] yRows; 183 delete [] uRows; 184 delete [] vRows; 185} 186 187 188void Yuv422IToJpegEncoder::deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows, 189 uint8_t* vRows, int rowIndex, int width, int height) { 190 for (int row = 0; row < 16; ++row) { 191 uint8_t* yuvSeg = yuv + (rowIndex + row) * fStrides[0]; 192 for (int i = 0; i < (width >> 1); ++i) { 193 int indexY = row * width + (i << 1); 194 int indexU = row * (width >> 1) + i; 195 yRows[indexY] = yuvSeg[0]; 196 yRows[indexY + 1] = yuvSeg[2]; 197 uRows[indexU] = yuvSeg[1]; 198 vRows[indexU] = yuvSeg[3]; 199 yuvSeg += 4; 200 } 201 } 202} 203 204void Yuv422IToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) { 205 // cb and cr are horizontally downsampled and vertically downsampled as well. 206 cinfo->comp_info[0].h_samp_factor = 2; 207 cinfo->comp_info[0].v_samp_factor = 2; 208 cinfo->comp_info[1].h_samp_factor = 1; 209 cinfo->comp_info[1].v_samp_factor = 2; 210 cinfo->comp_info[2].h_samp_factor = 1; 211 cinfo->comp_info[2].v_samp_factor = 2; 212} 213/////////////////////////////////////////////////////////////////////////////// 214 215static jboolean YuvImage_compressToJpeg(JNIEnv* env, jobject, jbyteArray inYuv, 216 int format, int width, int height, jintArray offsets, 217 jintArray strides, int jpegQuality, jobject jstream, 218 jbyteArray jstorage) { 219 jbyte* yuv = env->GetByteArrayElements(inYuv, NULL); 220 SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); 221 222 jint* imgOffsets = env->GetIntArrayElements(offsets, NULL); 223 jint* imgStrides = env->GetIntArrayElements(strides, NULL); 224 YuvToJpegEncoder* encoder = YuvToJpegEncoder::create(format, imgStrides); 225 if (encoder == NULL) { 226 return false; 227 } 228 encoder->encode(strm, yuv, width, height, imgOffsets, jpegQuality); 229 230 delete encoder; 231 env->ReleaseByteArrayElements(inYuv, yuv, 0); 232 env->ReleaseIntArrayElements(offsets, imgOffsets, 0); 233 env->ReleaseIntArrayElements(strides, imgStrides, 0); 234 return true; 235} 236/////////////////////////////////////////////////////////////////////////////// 237 238#include <android_runtime/AndroidRuntime.h> 239 240static JNINativeMethod gYuvImageMethods[] = { 241 { "nativeCompressToJpeg", "([BIII[I[IILjava/io/OutputStream;[B)Z", 242 (void*)YuvImage_compressToJpeg } 243}; 244 245#define kClassPathName "android/graphics/YuvImage" 246 247int register_android_graphics_YuvImage(JNIEnv* env); 248int register_android_graphics_YuvImage(JNIEnv* env) 249{ 250 return android::AndroidRuntime::registerNativeMethods(env, kClassPathName, 251 gYuvImageMethods, SK_ARRAY_COUNT(gYuvImageMethods)); 252} 253