blob: 0a0c5b3d3d41957802fc204e5da6470b04d91f76 [file] [log] [blame]
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001#include "CreateJavaOutputStreamAdaptor.h"
2#include "SkJpegUtility.h"
3#include "YuvToJpegEncoder.h"
Mathias Agopian8f2423e2010-02-16 17:33:37 -08004#include <ui/PixelFormat.h>
5#include <hardware/hardware.h>
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08006
7#include <jni.h>
8
9YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) {
Mathias Agopiana696f5d2010-02-17 17:53:09 -080010 // Only ImageFormat.NV21 and ImageFormat.YUY2 are supported
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080011 // for now.
Mathias Agopiana696f5d2010-02-17 17:53:09 -080012 if (format == HAL_PIXEL_FORMAT_YCrCb_420_SP) {
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080013 return new Yuv420SpToJpegEncoder(strides);
Mathias Agopian8f2423e2010-02-16 17:33:37 -080014 } else if (format == HAL_PIXEL_FORMAT_YCbCr_422_I) {
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080015 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) {
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080052 cinfo->image_width = width;
53 cinfo->image_height = height;
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080054 cinfo->input_components = 3;
55 cinfo->in_color_space = JCS_YCbCr;
56 jpeg_set_defaults(cinfo);
Chia-chi Yehaa868592010-03-10 17:08:58 +080057
58 jpeg_set_quality(cinfo, quality, TRUE);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080059 jpeg_set_colorspace(cinfo, JCS_YCbCr);
60 cinfo->raw_data_in = TRUE;
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080061 cinfo->dct_method = JDCT_IFAST;
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080062 configSamplingFactors(cinfo);
63}
64
65///////////////////////////////////////////////////////////////////
66Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) :
67 YuvToJpegEncoder(strides) {
68 fNumPlanes = 2;
69}
70
71void 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);
94
95 for (int i = 0; i < 16; i++) {
96 // y row
97 y[i] = yPlanar + (cinfo->next_scanline + i) * fStrides[0];
98
99 // construct u row and v row
100 if ((i & 1) == 0) {
101 // height and width are both halved because of downsampling
102 int offset = (i >> 1) * (width >> 1);
103 cb[i/2] = uRows + offset;
104 cr[i/2] = vRows + offset;
105 }
106 }
107 jpeg_write_raw_data(cinfo, planes, 16);
108 }
109 delete [] uRows;
110 delete [] vRows;
111
112}
113
114void Yuv420SpToJpegEncoder::deinterleave(uint8_t* vuPlanar, uint8_t* uRows,
115 uint8_t* vRows, int rowIndex, int width) {
116 for (int row = 0; row < 8; ++row) {
117 int offset = ((rowIndex >> 1) + row) * fStrides[1];
118 uint8_t* vu = vuPlanar + offset;
119 for (int i = 0; i < (width >> 1); ++i) {
120 int index = row * (width >> 1) + i;
121 uRows[index] = vu[1];
122 vRows[index] = vu[0];
123 vu += 2;
124 }
125 }
126}
127
128void Yuv420SpToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) {
129 // cb and cr are horizontally downsampled and vertically downsampled as well.
130 cinfo->comp_info[0].h_samp_factor = 2;
131 cinfo->comp_info[0].v_samp_factor = 2;
132 cinfo->comp_info[1].h_samp_factor = 1;
133 cinfo->comp_info[1].v_samp_factor = 1;
134 cinfo->comp_info[2].h_samp_factor = 1;
135 cinfo->comp_info[2].v_samp_factor = 1;
136}
137
138///////////////////////////////////////////////////////////////////////////////
139Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) :
140 YuvToJpegEncoder(strides) {
141 fNumPlanes = 1;
142}
143
144void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo,
145 uint8_t* yuv, int* offsets) {
146 SkDebugf("onFlyCompress_422");
147 JSAMPROW y[16];
148 JSAMPROW cb[16];
149 JSAMPROW cr[16];
150 JSAMPARRAY planes[3];
151 planes[0] = y;
152 planes[1] = cb;
153 planes[2] = cr;
154
155 int width = cinfo->image_width;
156 int height = cinfo->image_height;
157 uint8_t* yRows = new uint8_t [16 * width];
158 uint8_t* uRows = new uint8_t [16 * (width >> 1)];
159 uint8_t* vRows = new uint8_t [16 * (width >> 1)];
160
161 uint8_t* yuvOffset = yuv + offsets[0];
162
163 // process 16 lines of Y and 16 lines of U/V each time.
164 while (cinfo->next_scanline < cinfo->image_height) {
165 deinterleave(yuvOffset, yRows, uRows, vRows, cinfo->next_scanline, width, height);
166
167 for (int i = 0; i < 16; i++) {
168 // y row
169 y[i] = yRows + i * width;
170
171 // construct u row and v row
172 // width is halved because of downsampling
173 int offset = i * (width >> 1);
174 cb[i] = uRows + offset;
175 cr[i] = vRows + offset;
176 }
177
178 jpeg_write_raw_data(cinfo, planes, 16);
179 }
180 delete [] yRows;
181 delete [] uRows;
182 delete [] vRows;
183}
184
185
186void Yuv422IToJpegEncoder::deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows,
187 uint8_t* vRows, int rowIndex, int width, int height) {
188 for (int row = 0; row < 16; ++row) {
189 uint8_t* yuvSeg = yuv + (rowIndex + row) * fStrides[0];
190 for (int i = 0; i < (width >> 1); ++i) {
191 int indexY = row * width + (i << 1);
192 int indexU = row * (width >> 1) + i;
193 yRows[indexY] = yuvSeg[0];
194 yRows[indexY + 1] = yuvSeg[2];
195 uRows[indexU] = yuvSeg[1];
196 vRows[indexU] = yuvSeg[3];
197 yuvSeg += 4;
198 }
199 }
200}
201
202void Yuv422IToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) {
203 // cb and cr are horizontally downsampled and vertically downsampled as well.
204 cinfo->comp_info[0].h_samp_factor = 2;
205 cinfo->comp_info[0].v_samp_factor = 2;
206 cinfo->comp_info[1].h_samp_factor = 1;
207 cinfo->comp_info[1].v_samp_factor = 2;
208 cinfo->comp_info[2].h_samp_factor = 1;
209 cinfo->comp_info[2].v_samp_factor = 2;
210}
211///////////////////////////////////////////////////////////////////////////////
212
213static jboolean YuvImage_compressToJpeg(JNIEnv* env, jobject, jbyteArray inYuv,
214 int format, int width, int height, jintArray offsets,
215 jintArray strides, int jpegQuality, jobject jstream,
216 jbyteArray jstorage) {
217 jbyte* yuv = env->GetByteArrayElements(inYuv, NULL);
218 SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
219
220 jint* imgOffsets = env->GetIntArrayElements(offsets, NULL);
221 jint* imgStrides = env->GetIntArrayElements(strides, NULL);
222 YuvToJpegEncoder* encoder = YuvToJpegEncoder::create(format, imgStrides);
223 if (encoder == NULL) {
224 return false;
225 }
226 encoder->encode(strm, yuv, width, height, imgOffsets, jpegQuality);
227
228 delete encoder;
229 env->ReleaseByteArrayElements(inYuv, yuv, 0);
230 env->ReleaseIntArrayElements(offsets, imgOffsets, 0);
231 env->ReleaseIntArrayElements(strides, imgStrides, 0);
232 return true;
233}
234///////////////////////////////////////////////////////////////////////////////
235
236#include <android_runtime/AndroidRuntime.h>
237
238static JNINativeMethod gYuvImageMethods[] = {
239 { "nativeCompressToJpeg", "([BIII[I[IILjava/io/OutputStream;[B)Z",
240 (void*)YuvImage_compressToJpeg }
241};
242
243#define kClassPathName "android/graphics/YuvImage"
244
245int register_android_graphics_YuvImage(JNIEnv* env);
246int register_android_graphics_YuvImage(JNIEnv* env)
247{
248 return android::AndroidRuntime::registerNativeMethods(env, kClassPathName,
249 gYuvImageMethods, SK_ARRAY_COUNT(gYuvImageMethods));
250}