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
11YuvToJpegEncoder* 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
23YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) {
24}
25
26bool 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
49    return true;
50}
51
52void YuvToJpegEncoder::setJpegCompressStruct(jpeg_compress_struct* cinfo,
53        int width, int height, int quality) {
54    cinfo->image_width = width;
55    cinfo->image_height = height;
56    cinfo->input_components = 3;
57    cinfo->in_color_space = JCS_YCbCr;
58    jpeg_set_defaults(cinfo);
59
60    jpeg_set_quality(cinfo, quality, TRUE);
61    jpeg_set_colorspace(cinfo, JCS_YCbCr);
62    cinfo->raw_data_in = TRUE;
63    cinfo->dct_method = JDCT_IFAST;
64    configSamplingFactors(cinfo);
65}
66
67///////////////////////////////////////////////////////////////////
68Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) :
69        YuvToJpegEncoder(strides) {
70    fNumPlanes = 2;
71}
72
73void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo,
74        uint8_t* yuv, int* offsets) {
75    SkDebugf("onFlyCompress");
76    JSAMPROW y[16];
77    JSAMPROW cb[8];
78    JSAMPROW cr[8];
79    JSAMPARRAY planes[3];
80    planes[0] = y;
81    planes[1] = cb;
82    planes[2] = cr;
83
84    int width = cinfo->image_width;
85    int height = cinfo->image_height;
86    uint8_t* yPlanar = yuv + offsets[0];
87    uint8_t* vuPlanar = yuv + offsets[1]; //width * height;
88    uint8_t* uRows = new uint8_t [8 * (width >> 1)];
89    uint8_t* vRows = new uint8_t [8 * (width >> 1)];
90
91
92    // process 16 lines of Y and 8 lines of U/V each time.
93    while (cinfo->next_scanline < cinfo->image_height) {
94        //deitnerleave u and v
95        deinterleave(vuPlanar, uRows, vRows, cinfo->next_scanline, width, height);
96
97        // Jpeg library ignores the rows whose indices are greater than height.
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, int height) {
119    int numRows = (height - rowIndex) / 2;
120    if (numRows > 8) numRows = 8;
121    for (int row = 0; row < numRows; ++row) {
122        int offset = ((rowIndex >> 1) + row) * fStrides[1];
123        uint8_t* vu = vuPlanar + offset;
124        for (int i = 0; i < (width >> 1); ++i) {
125            int index = row * (width >> 1) + i;
126            uRows[index] = vu[1];
127            vRows[index] = vu[0];
128            vu += 2;
129        }
130    }
131}
132
133void Yuv420SpToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) {
134    // cb and cr are horizontally downsampled and vertically downsampled as well.
135    cinfo->comp_info[0].h_samp_factor = 2;
136    cinfo->comp_info[0].v_samp_factor = 2;
137    cinfo->comp_info[1].h_samp_factor = 1;
138    cinfo->comp_info[1].v_samp_factor = 1;
139    cinfo->comp_info[2].h_samp_factor = 1;
140    cinfo->comp_info[2].v_samp_factor = 1;
141}
142
143///////////////////////////////////////////////////////////////////////////////
144Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) :
145        YuvToJpegEncoder(strides) {
146    fNumPlanes = 1;
147}
148
149void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo,
150        uint8_t* yuv, int* offsets) {
151    SkDebugf("onFlyCompress_422");
152    JSAMPROW y[16];
153    JSAMPROW cb[16];
154    JSAMPROW cr[16];
155    JSAMPARRAY planes[3];
156    planes[0] = y;
157    planes[1] = cb;
158    planes[2] = cr;
159
160    int width = cinfo->image_width;
161    int height = cinfo->image_height;
162    uint8_t* yRows = new uint8_t [16 * width];
163    uint8_t* uRows = new uint8_t [16 * (width >> 1)];
164    uint8_t* vRows = new uint8_t [16 * (width >> 1)];
165
166    uint8_t* yuvOffset = yuv + offsets[0];
167
168    // process 16 lines of Y and 16 lines of U/V each time.
169    while (cinfo->next_scanline < cinfo->image_height) {
170        deinterleave(yuvOffset, yRows, uRows, vRows, cinfo->next_scanline, width, height);
171
172        // Jpeg library ignores the rows whose indices are greater than height.
173        for (int i = 0; i < 16; i++) {
174            // y row
175            y[i] = yRows + i * width;
176
177            // construct u row and v row
178            // width is halved because of downsampling
179            int offset = i * (width >> 1);
180            cb[i] = uRows + offset;
181            cr[i] = vRows + offset;
182        }
183
184        jpeg_write_raw_data(cinfo, planes, 16);
185    }
186    delete [] yRows;
187    delete [] uRows;
188    delete [] vRows;
189}
190
191
192void Yuv422IToJpegEncoder::deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows,
193        uint8_t* vRows, int rowIndex, int width, int height) {
194    int numRows = height - rowIndex;
195    if (numRows > 16) numRows = 16;
196    for (int row = 0; row < numRows; ++row) {
197        uint8_t* yuvSeg = yuv + (rowIndex + row) * fStrides[0];
198        for (int i = 0; i < (width >> 1); ++i) {
199            int indexY = row * width + (i << 1);
200            int indexU = row * (width >> 1) + i;
201            yRows[indexY] = yuvSeg[0];
202            yRows[indexY + 1] = yuvSeg[2];
203            uRows[indexU] = yuvSeg[1];
204            vRows[indexU] = yuvSeg[3];
205            yuvSeg += 4;
206        }
207    }
208}
209
210void Yuv422IToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) {
211    // cb and cr are horizontally downsampled and vertically downsampled as well.
212    cinfo->comp_info[0].h_samp_factor = 2;
213    cinfo->comp_info[0].v_samp_factor = 2;
214    cinfo->comp_info[1].h_samp_factor = 1;
215    cinfo->comp_info[1].v_samp_factor = 2;
216    cinfo->comp_info[2].h_samp_factor = 1;
217    cinfo->comp_info[2].v_samp_factor = 2;
218}
219///////////////////////////////////////////////////////////////////////////////
220
221static jboolean YuvImage_compressToJpeg(JNIEnv* env, jobject, jbyteArray inYuv,
222        jint format, jint width, jint height, jintArray offsets,
223        jintArray strides, jint jpegQuality, jobject jstream,
224        jbyteArray jstorage) {
225    jbyte* yuv = env->GetByteArrayElements(inYuv, NULL);
226    SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
227
228    jint* imgOffsets = env->GetIntArrayElements(offsets, NULL);
229    jint* imgStrides = env->GetIntArrayElements(strides, NULL);
230    YuvToJpegEncoder* encoder = YuvToJpegEncoder::create(format, imgStrides);
231    jboolean result = JNI_FALSE;
232    if (encoder != NULL) {
233        encoder->encode(strm, yuv, width, height, imgOffsets, jpegQuality);
234        delete encoder;
235        result = JNI_TRUE;
236    }
237
238    env->ReleaseByteArrayElements(inYuv, yuv, 0);
239    env->ReleaseIntArrayElements(offsets, imgOffsets, 0);
240    env->ReleaseIntArrayElements(strides, imgStrides, 0);
241    delete strm;
242    return result;
243}
244///////////////////////////////////////////////////////////////////////////////
245
246static const JNINativeMethod gYuvImageMethods[] = {
247    {   "nativeCompressToJpeg",  "([BIII[I[IILjava/io/OutputStream;[B)Z",
248        (void*)YuvImage_compressToJpeg }
249};
250
251int register_android_graphics_YuvImage(JNIEnv* env)
252{
253    return android::RegisterMethodsOrDie(env, "android/graphics/YuvImage", gYuvImageMethods,
254                                         NELEM(gYuvImageMethods));
255}
256