Home | History | Annotate | Download | only in graphics
      1 #include "CreateJavaOutputStreamAdaptor.h"
      2 #include "SkJpegUtility.h"
      3 #include "YuvToJpegEncoder.h"
      4 #include <ui/PixelFormat.h>
      5 #include <hardware/hardware.h>
      6 
      7 #include <jni.h>
      8 
      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 
     21 YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) {
     22 }
     23 
     24 bool 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 
     50 void YuvToJpegEncoder::setJpegCompressStruct(jpeg_compress_struct* cinfo,
     51         int width, int height, int quality) {
     52     cinfo->image_width = width;
     53     cinfo->image_height = height;
     54     cinfo->input_components = 3;
     55     cinfo->in_color_space = JCS_YCbCr;
     56     jpeg_set_defaults(cinfo);
     57 
     58     jpeg_set_quality(cinfo, quality, TRUE);
     59     jpeg_set_colorspace(cinfo, JCS_YCbCr);
     60     cinfo->raw_data_in = TRUE;
     61     cinfo->dct_method = JDCT_IFAST;
     62     configSamplingFactors(cinfo);
     63 }
     64 
     65 ///////////////////////////////////////////////////////////////////
     66 Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) :
     67         YuvToJpegEncoder(strides) {
     68     fNumPlanes = 2;
     69 }
     70 
     71 void 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 
    114 void 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 
    128 void 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 ///////////////////////////////////////////////////////////////////////////////
    139 Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) :
    140         YuvToJpegEncoder(strides) {
    141     fNumPlanes = 1;
    142 }
    143 
    144 void 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 
    186 void 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 
    202 void 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 
    213 static 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 
    238 static JNINativeMethod gYuvImageMethods[] = {
    239     {   "nativeCompressToJpeg",  "([BIII[I[IILjava/io/OutputStream;[B)Z",
    240         (void*)YuvImage_compressToJpeg }
    241 };
    242 
    243 #define kClassPathName  "android/graphics/YuvImage"
    244 
    245 int register_android_graphics_YuvImage(JNIEnv* env)
    246 {
    247     return android::AndroidRuntime::registerNativeMethods(env, kClassPathName,
    248             gYuvImageMethods, SK_ARRAY_COUNT(gYuvImageMethods));
    249 }
    250