OSDN Git Service

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