• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "CreateJavaOutputStreamAdaptor.h"
2 #include "SkJPEGWriteUtility.h"
3 #include "YuvToJpegEncoder.h"
4 #include <ui/PixelFormat.h>
5 #include <hardware/hardware.h>
6 
7 #include "graphics_jni_helpers.h"
8 
create(int format,int * strides)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 
YuvToJpegEncoder(int * strides)21 YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) {
22 }
23 
24 struct ErrorMgr {
25     struct jpeg_error_mgr pub;
26     jmp_buf jmp;
27 };
28 
error_exit(j_common_ptr cinfo)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 
encode(SkWStream * stream,void * inYuv,int width,int height,int * offsets,int jpegQuality)35 bool YuvToJpegEncoder::encode(SkWStream* stream, void* inYuv, int width,
36         int height, int* offsets, int jpegQuality) {
37     jpeg_compress_struct    cinfo;
38     ErrorMgr                err;
39     skjpeg_destination_mgr  sk_wstream(stream);
40 
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);
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 
60     jpeg_destroy_compress(&cinfo);
61 
62     return true;
63 }
64 
setJpegCompressStruct(jpeg_compress_struct * cinfo,int width,int height,int quality)65 void YuvToJpegEncoder::setJpegCompressStruct(jpeg_compress_struct* cinfo,
66         int width, int height, int quality) {
67     cinfo->image_width = width;
68     cinfo->image_height = height;
69     cinfo->input_components = 3;
70     cinfo->in_color_space = JCS_YCbCr;
71     jpeg_set_defaults(cinfo);
72 
73     jpeg_set_quality(cinfo, quality, TRUE);
74     jpeg_set_colorspace(cinfo, JCS_YCbCr);
75     cinfo->raw_data_in = TRUE;
76     cinfo->dct_method = JDCT_IFAST;
77     configSamplingFactors(cinfo);
78 }
79 
80 ///////////////////////////////////////////////////////////////////
Yuv420SpToJpegEncoder(int * strides)81 Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) :
82         YuvToJpegEncoder(strides) {
83     fNumPlanes = 2;
84 }
85 
compress(jpeg_compress_struct * cinfo,uint8_t * yuv,int * offsets)86 void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo,
87         uint8_t* yuv, int* offsets) {
88     ALOGD("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
108         deinterleave(vuPlanar, uRows, vRows, cinfo->next_scanline, width, height);
109 
110         // Jpeg library ignores the rows whose indices are greater than height.
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 
deinterleave(uint8_t * vuPlanar,uint8_t * uRows,uint8_t * vRows,int rowIndex,int width,int height)130 void Yuv420SpToJpegEncoder::deinterleave(uint8_t* vuPlanar, uint8_t* uRows,
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) {
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 
configSamplingFactors(jpeg_compress_struct * cinfo)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 ///////////////////////////////////////////////////////////////////////////////
Yuv422IToJpegEncoder(int * strides)157 Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) :
158         YuvToJpegEncoder(strides) {
159     fNumPlanes = 1;
160 }
161 
compress(jpeg_compress_struct * cinfo,uint8_t * yuv,int * offsets)162 void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo,
163         uint8_t* yuv, int* offsets) {
164     ALOGD("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 
185         // Jpeg library ignores the rows whose indices are greater than height.
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 
deinterleave(uint8_t * yuv,uint8_t * yRows,uint8_t * uRows,uint8_t * vRows,int rowIndex,int width,int height)205 void Yuv422IToJpegEncoder::deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows,
206         uint8_t* vRows, int rowIndex, int width, int height) {
207     int numRows = height - rowIndex;
208     if (numRows > 16) numRows = 16;
209     for (int row = 0; row < numRows; ++row) {
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 
configSamplingFactors(jpeg_compress_struct * cinfo)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 
YuvImage_compressToJpeg(JNIEnv * env,jobject,jbyteArray inYuv,jint format,jint width,jint height,jintArray offsets,jintArray strides,jint jpegQuality,jobject jstream,jbyteArray jstorage)234 static jboolean YuvImage_compressToJpeg(JNIEnv* env, jobject, jbyteArray inYuv,
235         jint format, jint width, jint height, jintArray offsets,
236         jintArray strides, jint jpegQuality, jobject jstream,
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);
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;
249     }
250 
251     env->ReleaseByteArrayElements(inYuv, yuv, 0);
252     env->ReleaseIntArrayElements(offsets, imgOffsets, 0);
253     env->ReleaseIntArrayElements(strides, imgStrides, 0);
254     delete strm;
255     return result;
256 }
257 ///////////////////////////////////////////////////////////////////////////////
258 
259 static const JNINativeMethod gYuvImageMethods[] = {
260     {   "nativeCompressToJpeg",  "([BIII[I[IILjava/io/OutputStream;[B)Z",
261         (void*)YuvImage_compressToJpeg }
262 };
263 
register_android_graphics_YuvImage(JNIEnv * env)264 int register_android_graphics_YuvImage(JNIEnv* env)
265 {
266     return android::RegisterMethodsOrDie(env, "android/graphics/YuvImage", gYuvImageMethods,
267                                          NELEM(gYuvImageMethods));
268 }
269