Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 1 | #include "CreateJavaOutputStreamAdaptor.h" |
Matt Sarett | 79ad7be | 2016-03-25 14:28:40 -0400 | [diff] [blame] | 2 | #include "SkJPEGWriteUtility.h" |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 3 | #include "YuvToJpegEncoder.h" |
Mathias Agopian | 8f2423e | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 4 | #include <ui/PixelFormat.h> |
| 5 | #include <hardware/hardware.h> |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 6 | |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 7 | #include "graphics_jni_helpers.h" |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 8 | |
| 9 | YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) { |
Mathias Agopian | a696f5d | 2010-02-17 17:53:09 -0800 | [diff] [blame] | 10 | // Only ImageFormat.NV21 and ImageFormat.YUY2 are supported |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 11 | // for now. |
Mathias Agopian | a696f5d | 2010-02-17 17:53:09 -0800 | [diff] [blame] | 12 | if (format == HAL_PIXEL_FORMAT_YCrCb_420_SP) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 13 | return new Yuv420SpToJpegEncoder(strides); |
Mathias Agopian | 8f2423e | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 14 | } else if (format == HAL_PIXEL_FORMAT_YCbCr_422_I) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 15 | return new Yuv422IToJpegEncoder(strides); |
| 16 | } else { |
| 17 | return NULL; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) { |
| 22 | } |
| 23 | |
Leon Scroggins III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 24 | struct ErrorMgr { |
| 25 | struct jpeg_error_mgr pub; |
| 26 | jmp_buf jmp; |
| 27 | }; |
| 28 | |
| 29 | void 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 Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 35 | bool YuvToJpegEncoder::encode(SkWStream* stream, void* inYuv, int width, |
| 36 | int height, int* offsets, int jpegQuality) { |
| 37 | jpeg_compress_struct cinfo; |
Leon Scroggins III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 38 | ErrorMgr err; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 39 | skjpeg_destination_mgr sk_wstream(stream); |
| 40 | |
Leon Scroggins III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 41 | 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 Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 46 | 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 III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 60 | jpeg_destroy_compress(&cinfo); |
| 61 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | |
| 65 | void YuvToJpegEncoder::setJpegCompressStruct(jpeg_compress_struct* cinfo, |
| 66 | int width, int height, int quality) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 67 | cinfo->image_width = width; |
| 68 | cinfo->image_height = height; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 69 | cinfo->input_components = 3; |
| 70 | cinfo->in_color_space = JCS_YCbCr; |
| 71 | jpeg_set_defaults(cinfo); |
Chia-chi Yeh | aa86859 | 2010-03-10 17:08:58 +0800 | [diff] [blame] | 72 | |
| 73 | jpeg_set_quality(cinfo, quality, TRUE); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 74 | jpeg_set_colorspace(cinfo, JCS_YCbCr); |
| 75 | cinfo->raw_data_in = TRUE; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 76 | cinfo->dct_method = JDCT_IFAST; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 77 | configSamplingFactors(cinfo); |
| 78 | } |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////// |
| 81 | Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) : |
| 82 | YuvToJpegEncoder(strides) { |
| 83 | fNumPlanes = 2; |
| 84 | } |
| 85 | |
| 86 | void 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 Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 108 | deinterleave(vuPlanar, uRows, vRows, cinfo->next_scanline, width, height); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 109 | |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 110 | // Jpeg library ignores the rows whose indices are greater than height. |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 111 | 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 | |
| 130 | void Yuv420SpToJpegEncoder::deinterleave(uint8_t* vuPlanar, uint8_t* uRows, |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 131 | 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 Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 135 | 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 | |
| 146 | void 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 | /////////////////////////////////////////////////////////////////////////////// |
| 157 | Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) : |
| 158 | YuvToJpegEncoder(strides) { |
| 159 | fNumPlanes = 1; |
| 160 | } |
| 161 | |
| 162 | void 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 Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 185 | // Jpeg library ignores the rows whose indices are greater than height. |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 186 | 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 | |
| 205 | void Yuv422IToJpegEncoder::deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows, |
| 206 | uint8_t* vRows, int rowIndex, int width, int height) { |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 207 | int numRows = height - rowIndex; |
| 208 | if (numRows > 16) numRows = 16; |
| 209 | for (int row = 0; row < numRows; ++row) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 210 | 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 | |
| 223 | void 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 | |
| 234 | static jboolean YuvImage_compressToJpeg(JNIEnv* env, jobject, jbyteArray inYuv, |
Ashok Bhat | 39029b2 | 2014-01-10 16:24:38 +0000 | [diff] [blame] | 235 | jint format, jint width, jint height, jintArray offsets, |
| 236 | jintArray strides, jint jpegQuality, jobject jstream, |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 237 | 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 Augustyn | 7c68a407 | 2012-07-16 11:23:54 +0200 | [diff] [blame] | 244 | 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 Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 249 | } |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 250 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 251 | env->ReleaseByteArrayElements(inYuv, yuv, 0); |
| 252 | env->ReleaseIntArrayElements(offsets, imgOffsets, 0); |
| 253 | env->ReleaseIntArrayElements(strides, imgStrides, 0); |
Martin Wallgren | d865900 | 2014-08-20 14:58:58 +0200 | [diff] [blame] | 254 | delete strm; |
Pawel Augustyn | 7c68a407 | 2012-07-16 11:23:54 +0200 | [diff] [blame] | 255 | return result; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 256 | } |
| 257 | /////////////////////////////////////////////////////////////////////////////// |
| 258 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 259 | static const JNINativeMethod gYuvImageMethods[] = { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 260 | { "nativeCompressToJpeg", "([BIII[I[IILjava/io/OutputStream;[B)Z", |
| 261 | (void*)YuvImage_compressToJpeg } |
| 262 | }; |
| 263 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 264 | int register_android_graphics_YuvImage(JNIEnv* env) |
| 265 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 266 | return android::RegisterMethodsOrDie(env, "android/graphics/YuvImage", gYuvImageMethods, |
| 267 | NELEM(gYuvImageMethods)); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 268 | } |