Home | History | Annotate | Download | only in camera
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "Decoder_libjpeg.h"
     18 
     19 extern "C" {
     20     #include "jpeglib.h"
     21     #include "jerror.h"
     22 }
     23 
     24 #define NUM_COMPONENTS_IN_YUV 3
     25 
     26 namespace Ti {
     27 namespace Camera {
     28 
     29 /* JPEG DHT Segment omitted from MJPEG data */
     30 static unsigned char jpeg_odml_dht[0x1a6] = {
     31     0xff, 0xd8, /* Start of Image */
     32     0xff, 0xc4, 0x01, 0xa2, /* Define Huffman Table */
     33 
     34     0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     35     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
     36 
     37     0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
     38     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
     39 
     40     0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
     41     0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
     42     0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
     43     0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
     44     0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
     45     0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
     46     0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
     47     0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
     48     0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
     49     0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
     50     0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
     51     0xf9, 0xfa,
     52 
     53     0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
     54     0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
     55     0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
     56     0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
     57     0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
     58     0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
     59     0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
     60     0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
     61     0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
     62     0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
     63     0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
     64     0xf9, 0xfa
     65 };
     66 
     67 struct libjpeg_source_mgr : jpeg_source_mgr {
     68     libjpeg_source_mgr(unsigned char *buffer_ptr, int len);
     69     ~libjpeg_source_mgr();
     70 
     71     unsigned char *mBufferPtr;
     72     int mFilledLen;
     73 };
     74 
     75 static void libjpeg_init_source(j_decompress_ptr cinfo) {
     76     libjpeg_source_mgr*  src = (libjpeg_source_mgr*)cinfo->src;
     77     src->next_input_byte = (const JOCTET*)src->mBufferPtr;
     78     src->bytes_in_buffer = 0;
     79     src->current_offset = 0;
     80 }
     81 
     82 static boolean libjpeg_seek_input_data(j_decompress_ptr cinfo, long byte_offset) {
     83     libjpeg_source_mgr* src = (libjpeg_source_mgr*)cinfo->src;
     84     src->current_offset = byte_offset;
     85     src->next_input_byte = (const JOCTET*)src->mBufferPtr + byte_offset;
     86     src->bytes_in_buffer = 0;
     87     return TRUE;
     88 }
     89 
     90 static boolean libjpeg_fill_input_buffer(j_decompress_ptr cinfo) {
     91     libjpeg_source_mgr* src = (libjpeg_source_mgr*)cinfo->src;
     92     src->current_offset += src->mFilledLen;
     93     src->next_input_byte = src->mBufferPtr;
     94     src->bytes_in_buffer = src->mFilledLen;
     95     return TRUE;
     96 }
     97 
     98 static void libjpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
     99     libjpeg_source_mgr*  src = (libjpeg_source_mgr*)cinfo->src;
    100 
    101     if (num_bytes > (long)src->bytes_in_buffer) {
    102         CAMHAL_LOGEA("\n\n\n libjpeg_skip_input_data - num_bytes > (long)src->bytes_in_buffer \n\n\n");
    103     } else {
    104         src->next_input_byte += num_bytes;
    105         src->bytes_in_buffer -= num_bytes;
    106     }
    107 }
    108 
    109 static boolean libjpeg_resync_to_restart(j_decompress_ptr cinfo, int desired) {
    110     libjpeg_source_mgr*  src = (libjpeg_source_mgr*)cinfo->src;
    111     src->next_input_byte = (const JOCTET*)src->mBufferPtr;
    112     src->bytes_in_buffer = 0;
    113     return TRUE;
    114 }
    115 
    116 static void libjpeg_term_source(j_decompress_ptr /*cinfo*/) {}
    117 
    118 libjpeg_source_mgr::libjpeg_source_mgr(unsigned char *buffer_ptr, int len) : mBufferPtr(buffer_ptr), mFilledLen(len) {
    119     init_source = libjpeg_init_source;
    120     fill_input_buffer = libjpeg_fill_input_buffer;
    121     skip_input_data = libjpeg_skip_input_data;
    122     resync_to_restart = libjpeg_resync_to_restart;
    123     term_source = libjpeg_term_source;
    124     seek_input_data = libjpeg_seek_input_data;
    125 }
    126 
    127 libjpeg_source_mgr::~libjpeg_source_mgr() {}
    128 
    129 Decoder_libjpeg::Decoder_libjpeg()
    130 {
    131     mWidth = 0;
    132     mHeight = 0;
    133     Y_Plane = NULL;
    134     U_Plane = NULL;
    135     V_Plane = NULL;
    136     UV_Plane = NULL;
    137 }
    138 
    139 Decoder_libjpeg::~Decoder_libjpeg()
    140 {
    141     release();
    142 }
    143 
    144 void Decoder_libjpeg::release()
    145 {
    146     if (Y_Plane) {
    147         free(Y_Plane);
    148         Y_Plane = NULL;
    149     }
    150     if (U_Plane) {
    151         free(U_Plane);
    152         U_Plane = NULL;
    153     }
    154     if (V_Plane) {
    155         free(V_Plane);
    156         V_Plane = NULL;
    157     }
    158     if (UV_Plane) {
    159         free(UV_Plane);
    160         UV_Plane = NULL;
    161     }
    162 }
    163 
    164 int Decoder_libjpeg::readDHTSize()
    165 {
    166     return sizeof(jpeg_odml_dht);
    167 }
    168 
    169 int Decoder_libjpeg::appendDHT(unsigned char *jpeg_src, int filled_len, unsigned char *jpeg_with_dht_buffer, int buff_size)
    170 {
    171     /* Appending DHT to JPEG */
    172 
    173     int len = filled_len + sizeof(jpeg_odml_dht) - 2; // final length of jpeg data
    174     if (len > buff_size)  {
    175         CAMHAL_LOGEA("\n\n\n Buffer size too small. filled_len=%d, buff_size=%d, sizeof(jpeg_odml_dht)=%d\n\n\n", filled_len, buff_size, sizeof(jpeg_odml_dht));
    176         return 0;
    177     }
    178 
    179     memcpy(jpeg_with_dht_buffer, jpeg_odml_dht, sizeof(jpeg_odml_dht));
    180     memcpy((jpeg_with_dht_buffer + sizeof(jpeg_odml_dht)), jpeg_src + 2, (filled_len - 2));
    181     return len;
    182 }
    183 
    184 
    185 bool Decoder_libjpeg::decode(unsigned char *jpeg_src, int filled_len, unsigned char *nv12_buffer, int stride)
    186 {
    187     struct jpeg_decompress_struct cinfo;
    188     struct jpeg_error_mgr jerr;
    189     struct libjpeg_source_mgr s_mgr(jpeg_src, filled_len);
    190 
    191     if (filled_len == 0)
    192         return false;
    193 
    194     cinfo.err = jpeg_std_error(&jerr);
    195     jpeg_create_decompress(&cinfo);
    196 
    197     cinfo.src = &s_mgr;
    198     int status = jpeg_read_header(&cinfo, true);
    199     if (status != JPEG_HEADER_OK) {
    200         CAMHAL_LOGEA("jpeg header corrupted");
    201         return false;
    202     }
    203 
    204     cinfo.out_color_space = JCS_YCbCr;
    205     cinfo.raw_data_out = true;
    206     status = jpeg_start_decompress(&cinfo);
    207     if (!status){
    208         CAMHAL_LOGEA("jpeg_start_decompress failed");
    209         return false;
    210     }
    211 
    212     if (mWidth == 0){
    213         mWidth = cinfo.output_width;
    214         mHeight = cinfo.output_height;
    215         CAMHAL_LOGEA("w x h = %d x %d. stride=%d", cinfo.output_width, cinfo.output_height, stride);
    216     }
    217     else if ((cinfo.output_width > mWidth) || (cinfo.output_height > mHeight)) {
    218         CAMHAL_LOGEA(" Free the existing buffers so that they are reallocated for new w x h. Old WxH = %dx%d. New WxH = %dx%d",
    219         mWidth, mHeight, cinfo.output_width, cinfo.output_height);
    220         release();
    221     }
    222 
    223     unsigned int decoded_uv_buffer_size = cinfo.output_width * cinfo.output_height / 2;
    224     if (Y_Plane == NULL)Y_Plane = (unsigned char **)malloc(cinfo.output_height * sizeof(unsigned char *));
    225     if (U_Plane == NULL)U_Plane = (unsigned char **)malloc(cinfo.output_height * sizeof(unsigned char *));
    226     if (V_Plane == NULL)V_Plane = (unsigned char **)malloc(cinfo.output_height * sizeof(unsigned char *));
    227     if (UV_Plane == NULL) UV_Plane = (unsigned char *)malloc(decoded_uv_buffer_size);
    228 
    229     unsigned char **YUV_Planes[NUM_COMPONENTS_IN_YUV];
    230     YUV_Planes[0] = Y_Plane;
    231     YUV_Planes[1] = U_Plane;
    232     YUV_Planes[2] = V_Plane;
    233 
    234     unsigned char *row = &nv12_buffer[0];
    235 
    236     // Y Component
    237     for (unsigned int j = 0; j < cinfo.output_height; j++, row += stride)
    238         YUV_Planes[0][j] = row;
    239 
    240     row = &UV_Plane[0];
    241 
    242     // U Component
    243     for (unsigned int j = 0; j < cinfo.output_height; j+=2, row += cinfo.output_width / 2){
    244         YUV_Planes[1][j+0] = row;
    245         YUV_Planes[1][j+1] = row;
    246     }
    247 
    248     // V Component
    249     for (unsigned int j = 0; j < cinfo.output_height; j+=2, row += cinfo.output_width / 2){
    250         YUV_Planes[2][j+0] = row;
    251         YUV_Planes[2][j+1] = row;
    252     }
    253 
    254     // Interleaving U and V
    255     for (unsigned int i = 0; i < cinfo.output_height; i += 8) {
    256         jpeg_read_raw_data(&cinfo, YUV_Planes, 8);
    257         YUV_Planes[0] += 8;
    258         YUV_Planes[1] += 8;
    259         YUV_Planes[2] += 8;
    260     }
    261 
    262     unsigned char *uv_ptr = nv12_buffer + (stride * cinfo.output_height);
    263     unsigned char *u_ptr = UV_Plane;
    264     unsigned char *v_ptr = UV_Plane + (decoded_uv_buffer_size / 2);
    265     for(unsigned int i = 0; i < cinfo.output_height / 2; i++){
    266         for(unsigned int j = 0; j < cinfo.output_width; j+=2){
    267             *(uv_ptr + j) = *u_ptr; u_ptr++;
    268             *(uv_ptr + j + 1) = *v_ptr; v_ptr++;
    269         }
    270         uv_ptr = uv_ptr + stride;
    271     }
    272 
    273     jpeg_finish_decompress(&cinfo);
    274     jpeg_destroy_decompress(&cinfo);
    275 
    276     return true;
    277 }
    278 
    279 } // namespace Camera
    280 } // namespace Ti
    281 
    282