YuvToJpegEncoder.cpp revision 8f2423e8f394ae0666f1b61f83df4c0c7a4782d9
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 9YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) { 10 // Only PIXEL_FORMAT_YCbCr_420_SP and PIXEl_FOMAT_YCbCr_422_I are supported 11 // for now. 12 if (format == HAL_PIXEL_FORMAT_YCbCr_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 21YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) { 22} 23 24bool 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 50void YuvToJpegEncoder::setJpegCompressStruct(jpeg_compress_struct* cinfo, 51 int width, int height, int quality) { 52 jpeg_set_quality(cinfo, quality, TRUE); 53 54 cinfo->image_width = width; 55 cinfo->image_height = height; 56 57 cinfo->input_components = 3; 58 cinfo->in_color_space = JCS_YCbCr; 59 jpeg_set_defaults(cinfo); 60 jpeg_set_colorspace(cinfo, JCS_YCbCr); 61 cinfo->raw_data_in = TRUE; 62 63 cinfo->dct_method = JDCT_IFAST; 64 65 configSamplingFactors(cinfo); 66} 67 68/////////////////////////////////////////////////////////////////// 69Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) : 70 YuvToJpegEncoder(strides) { 71 fNumPlanes = 2; 72} 73 74void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo, 75 uint8_t* yuv, int* offsets) { 76 SkDebugf("onFlyCompress"); 77 JSAMPROW y[16]; 78 JSAMPROW cb[8]; 79 JSAMPROW cr[8]; 80 JSAMPARRAY planes[3]; 81 planes[0] = y; 82 planes[1] = cb; 83 planes[2] = cr; 84 85 int width = cinfo->image_width; 86 int height = cinfo->image_height; 87 uint8_t* yPlanar = yuv + offsets[0]; 88 uint8_t* vuPlanar = yuv + offsets[1]; //width * height; 89 uint8_t* uRows = new uint8_t [8 * (width >> 1)]; 90 uint8_t* vRows = new uint8_t [8 * (width >> 1)]; 91 92 93 // process 16 lines of Y and 8 lines of U/V each time. 94 while (cinfo->next_scanline < cinfo->image_height) { 95 //deitnerleave u and v 96 deinterleave(vuPlanar, uRows, vRows, cinfo->next_scanline, width); 97 98 for (int i = 0; i < 16; i++) { 99 // y row 100 y[i] = yPlanar + (cinfo->next_scanline + i) * fStrides[0]; 101 102 // construct u row and v row 103 if ((i & 1) == 0) { 104 // height and width are both halved because of downsampling 105 int offset = (i >> 1) * (width >> 1); 106 cb[i/2] = uRows + offset; 107 cr[i/2] = vRows + offset; 108 } 109 } 110 jpeg_write_raw_data(cinfo, planes, 16); 111 } 112 delete [] uRows; 113 delete [] vRows; 114 115} 116 117void Yuv420SpToJpegEncoder::deinterleave(uint8_t* vuPlanar, uint8_t* uRows, 118 uint8_t* vRows, int rowIndex, int width) { 119 for (int row = 0; row < 8; ++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 131void 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/////////////////////////////////////////////////////////////////////////////// 142Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) : 143 YuvToJpegEncoder(strides) { 144 fNumPlanes = 1; 145} 146 147void 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 for (int i = 0; i < 16; i++) { 171 // y row 172 y[i] = yRows + i * width; 173 174 // construct u row and v row 175 // width is halved because of downsampling 176 int offset = i * (width >> 1); 177 cb[i] = uRows + offset; 178 cr[i] = vRows + offset; 179 } 180 181 jpeg_write_raw_data(cinfo, planes, 16); 182 } 183 delete [] yRows; 184 delete [] uRows; 185 delete [] vRows; 186} 187 188 189void Yuv422IToJpegEncoder::deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows, 190 uint8_t* vRows, int rowIndex, int width, int height) { 191 for (int row = 0; row < 16; ++row) { 192 uint8_t* yuvSeg = yuv + (rowIndex + row) * fStrides[0]; 193 for (int i = 0; i < (width >> 1); ++i) { 194 int indexY = row * width + (i << 1); 195 int indexU = row * (width >> 1) + i; 196 yRows[indexY] = yuvSeg[0]; 197 yRows[indexY + 1] = yuvSeg[2]; 198 uRows[indexU] = yuvSeg[1]; 199 vRows[indexU] = yuvSeg[3]; 200 yuvSeg += 4; 201 } 202 } 203} 204 205void Yuv422IToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) { 206 // cb and cr are horizontally downsampled and vertically downsampled as well. 207 cinfo->comp_info[0].h_samp_factor = 2; 208 cinfo->comp_info[0].v_samp_factor = 2; 209 cinfo->comp_info[1].h_samp_factor = 1; 210 cinfo->comp_info[1].v_samp_factor = 2; 211 cinfo->comp_info[2].h_samp_factor = 1; 212 cinfo->comp_info[2].v_samp_factor = 2; 213} 214/////////////////////////////////////////////////////////////////////////////// 215 216static jboolean YuvImage_compressToJpeg(JNIEnv* env, jobject, jbyteArray inYuv, 217 int format, int width, int height, jintArray offsets, 218 jintArray strides, int jpegQuality, jobject jstream, 219 jbyteArray jstorage) { 220 jbyte* yuv = env->GetByteArrayElements(inYuv, NULL); 221 SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); 222 223 jint* imgOffsets = env->GetIntArrayElements(offsets, NULL); 224 jint* imgStrides = env->GetIntArrayElements(strides, NULL); 225 YuvToJpegEncoder* encoder = YuvToJpegEncoder::create(format, imgStrides); 226 if (encoder == NULL) { 227 return false; 228 } 229 encoder->encode(strm, yuv, width, height, imgOffsets, jpegQuality); 230 231 delete encoder; 232 env->ReleaseByteArrayElements(inYuv, yuv, 0); 233 env->ReleaseIntArrayElements(offsets, imgOffsets, 0); 234 env->ReleaseIntArrayElements(strides, imgStrides, 0); 235 return true; 236} 237/////////////////////////////////////////////////////////////////////////////// 238 239#include <android_runtime/AndroidRuntime.h> 240 241static JNINativeMethod gYuvImageMethods[] = { 242 { "nativeCompressToJpeg", "([BIII[I[IILjava/io/OutputStream;[B)Z", 243 (void*)YuvImage_compressToJpeg } 244}; 245 246#define kClassPathName "android/graphics/YuvImage" 247 248int register_android_graphics_YuvImage(JNIEnv* env); 249int register_android_graphics_YuvImage(JNIEnv* env) 250{ 251 return android::AndroidRuntime::registerNativeMethods(env, kClassPathName, 252 gYuvImageMethods, SK_ARRAY_COUNT(gYuvImageMethods)); 253} 254