Lcd companion boards support (VKLCD50RTA & VKLCD70RT)

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers JPEG_Coverter.cpp Source File

JPEG_Coverter.cpp

00001 /*******************************************************************************
00002 * DISCLAIMER
00003 * This software is supplied by Renesas Electronics Corporation and is only
00004 * intended for use with Renesas products. No other uses are authorized. This
00005 * software is owned by Renesas Electronics Corporation and is protected under
00006 * all applicable laws, including copyright laws.
00007 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
00008 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
00009 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00010 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
00011 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
00012 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
00013 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
00014 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
00015 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
00016 * Renesas reserves the right, without notice, to make changes to this software
00017 * and to discontinue the availability of this software. By using this software,
00018 * you agree to the additional terms and conditions found by accessing the
00019 * following link:
00020 * http://www.renesas.com/disclaimer*
00021 * Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.
00022 *******************************************************************************/
00023 
00024 /**************************************************************************//**
00025 * @file         jpeg_coverter.cpp
00026 * @version      1.00
00027 * $Rev: 1 $
00028 * $Date:: 2015-08-06 16:33:52 +0900#$
00029 * @brief        Decodes JPEG data and encodes to JPEG data
00030 ******************************************************************************/
00031 
00032 /******************************************************************************
00033 Includes   <System Includes> , "Project Includes"
00034 ******************************************************************************/
00035 #include  <string.h>
00036 #include  <stdio.h>
00037 #include  "r_typedefs.h"
00038 #include  "r_jcu_api.h"
00039 #include  "JPEG_Converter.h"
00040 #include  "converter_wrapper.h"
00041 #include  "mbed_error.h"
00042 #include  "rtos.h"
00043 #include  "Semaphore.h"
00044 
00045 
00046 /******************************************************************************
00047 Typedef definitions
00048 ******************************************************************************/
00049 
00050 /******************************************************************************
00051 Macro definitions
00052 ******************************************************************************/
00053 #define  QUANTIZATION_TABLE_SIZE    (64u)
00054 #define  HUFFMAN_TABLE_DC_SIZE      (28u)
00055 #define  HUFFMAN_TABLE_AC_SIZE      (178u)
00056 #define  JPEG_HEADER_LETTER_1       (0xFFu)
00057 #define  JPEG_HEADER_LETTER_2       (0xD8u)
00058 #define  ALPHA_VAL_MAX              (0xFF)
00059 #define  LOC_KIND_COLOR_FORMAT      (3u)
00060 
00061 #define  ENC_SIZE_MAX               (1024 * 30)
00062 #define  MASK_8BYTE                 (0xFFFFFFF8)
00063 
00064     /*[QuantizationTable_Y]*/
00065     /* Quality is IJG75 */
00066     static const uint8_t  csaDefaultQuantizationTable_Y[QUANTIZATION_TABLE_SIZE] = {
00067         8, 6, 5, 8, 12, 20, 26, 31,
00068         6, 6, 7, 10, 13, 29, 30, 28,
00069         7, 7, 8, 12, 20, 29, 35, 28,
00070         7, 9, 11, 15, 26, 44, 40, 31,
00071         9, 11, 19, 28, 34, 55, 52, 39,
00072         12, 18, 28, 32, 41, 52, 57, 46,
00073         25, 32, 39, 44, 52, 61, 60, 51,
00074         36, 46, 48, 49, 56, 50, 52, 50
00075     };
00076 
00077     /*[QuantizationTable_C]*/
00078     /* Quality is IJG75 */
00079     static const uint8_t  csaDefaultQuantizationTable_C[QUANTIZATION_TABLE_SIZE] = {
00080         9, 9, 12, 24, 50, 50, 50, 50,
00081         9, 11, 13, 33, 50, 50, 50, 50,
00082         12, 13, 28, 50, 50, 50, 50, 50,
00083         24, 33, 50, 50, 50, 50, 50, 50,
00084         50, 50, 50, 50, 50, 50, 50, 50,
00085         50, 50, 50, 50, 50, 50, 50, 50,
00086         50, 50, 50, 50, 50, 50, 50, 50,
00087         50, 50, 50, 50, 50, 50, 50, 50
00088     };
00089 
00090     /*[HuffmanTable_Y_DC]*/
00091     /* Example written in ITU-T T81 specification */
00092     static const uint8_t  csaDefaultHuffmanTable_Y_DC[HUFFMAN_TABLE_DC_SIZE] = {
00093         0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00094         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
00095     };
00096 
00097     /*[HuffmanTable_C_DC]*/
00098     /* Example written in ITU-T T81 specification */
00099     static const uint8_t  csaDefaultHuffmanTable_C_DC[HUFFMAN_TABLE_DC_SIZE] = {
00100         0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
00101         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
00102     };
00103 
00104     /*[HuffmanTable_Y_AC]*/
00105     /* Example written in ITU-T T81 specification */
00106     static const uint8_t  csaDefaultHuffmanTable_Y_AC[HUFFMAN_TABLE_AC_SIZE] = {
00107         0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D,
00108         0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
00109         0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0,
00110         0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
00111         0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
00112         0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
00113         0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
00114         0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
00115         0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5,
00116         0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
00117         0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
00118         0xF9, 0xFA
00119     };
00120 
00121     /*[HuffmanTable_C_AC]*/
00122     /* Example written in ITU-T T81 specification */
00123     static const uint8_t  csaDefaultHuffmanTable_C_AC[HUFFMAN_TABLE_AC_SIZE] = {
00124         0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
00125         0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
00126         0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0,
00127         0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
00128         0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
00129         0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
00130         0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
00131         0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
00132         0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
00133         0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
00134         0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
00135         0xF9, 0xFA
00136     };
00137 
00138 typedef void (JPEG_CallbackFunc_t)(JPEG_Converter::jpeg_conv_error_t err_code);
00139 
00140 static uint32_t             driver_ac_count = 0;
00141 static bool                 jcu_error_flag;
00142 static JPEG_CallbackFunc_t* pJPEG_ConverterCallback;
00143 Semaphore                   jpeg_converter_semaphore(1);
00144 
00145 
00146 /**************************************************************************//**
00147  * @brief       Callback function from JCU async mode
00148  * @param[in]   mbed_jcu_err_t          err_code       : JCU result
00149  * @retval      None
00150 ******************************************************************************/
00151 void JPEG_CallbackFunction(mbed_jcu_err_t err_code) {
00152     if (pJPEG_ConverterCallback != NULL) {
00153         pJPEG_ConverterCallback((JPEG_Converter::jpeg_conv_error_t)err_code);
00154     }
00155     if (err_code != MBED_JCU_E_OK ) {
00156         jcu_error_flag = true;
00157     }
00158     jpeg_converter_semaphore.release(); // RELEASE
00159 } /* End of callback function method () */
00160 
00161 /**************************************************************************//**
00162  * @brief       Constructor of the JPEG_Converter class
00163  * @param[in]   None
00164  * @retval      None
00165 ******************************************************************************/
00166 JPEG_Converter::JPEG_Converter(void) {
00167     jcu_errorcode_t           jcu_error;
00168     
00169     if (driver_ac_count == 0) {
00170         jcu_error = R_JCU_Initialize(NULL);
00171         if (jcu_error == JCU_ERROR_OK) {
00172             driver_ac_count++;
00173             jcu_error_flag = false;
00174         }
00175     } else {
00176         driver_ac_count++;
00177     }
00178 } /* End of constructor method () */
00179 
00180 /**************************************************************************//**
00181  * @brief       Destructor of the JPEG_Converter class
00182  * @param[in]   None
00183  * @retval      None
00184 ******************************************************************************/
00185 JPEG_Converter::~JPEG_Converter(void) {
00186     if (driver_ac_count > 0) {
00187         driver_ac_count--;
00188         if (driver_ac_count == 0) {
00189             (void)R_JCU_Terminate();
00190         }
00191     }
00192 } /* End of destructor method () */
00193 
00194 /**************************************************************************//**
00195  * @brief         JPEG data decode to bitmap
00196  * @param[in]     void*                 pJpegBuff       : Input JPEG data address
00197  * @param[in/out] bitmap_buff_info_t*   psOutputBuff    : Output bitmap data address
00198  * @retval        error code
00199 ******************************************************************************/
00200 JPEG_Converter::jpeg_conv_error_t
00201 JPEG_Converter::decode(void* pJpegBuff, bitmap_buff_info_t* psOutputBuff) {
00202     decode_options_t Options;
00203     
00204     return (decode(pJpegBuff, psOutputBuff, &Options));
00205 } /* End of method decode() */
00206 
00207 /**************************************************************************//**
00208  * @brief       JPEG data decode to bitmap
00209  * @param[in]     void*                 pJpegBuff       : Input JPEG data address
00210  * @param[in/out] bitmap_buff_info_t*   psOutputBuff    : Output bitmap data address
00211  * @param[in]     decode_options_t*     pOptions        : Decode option(Optional)
00212  * @retval      error code
00213 ******************************************************************************/
00214 JPEG_Converter::jpeg_conv_error_t
00215 JPEG_Converter::decode(void* pJpegBuff, bitmap_buff_info_t* psOutputBuff, decode_options_t* pOptions) {
00216     jpeg_conv_error_t           e;
00217     jcu_errorcode_t             jcu_error;
00218     jcu_decode_param_t          decode;
00219     jcu_buffer_param_t          buffer;
00220     uint8_t*            pBuff = (uint8_t *)pJpegBuff;
00221     const jcu_async_status_t*   status;
00222     jcu_image_info_t            image_info;
00223     bool                        mutex_release = false;
00224     
00225     // Check JCU initialized
00226     if (driver_ac_count > 0) {
00227         size_t calc_height;
00228         size_t calc_width;
00229         
00230         calc_height = psOutputBuff->height  * (2 ^ pOptions->vertical_sub_sampling);
00231         calc_width  = psOutputBuff->width  * (2 ^ pOptions->horizontal_sub_sampling);
00232         
00233         // Check input address
00234         if ((pJpegBuff == NULL) || (psOutputBuff == NULL) || (pOptions == NULL)) {
00235             e = JPEG_CONV_PARAM_ERR ;  // Input address error
00236             goto  fin;
00237         }
00238         // Check JPEG header
00239         if (((uint32_t)(pBuff[0]) != JPEG_HEADER_LETTER_1) ||
00240             ((uint32_t)(pBuff[1]) != JPEG_HEADER_LETTER_2)) {
00241             e = JPEG_CONV_FORMA_ERR ;  // JPEG data is not in ROM
00242             goto  fin;
00243         }
00244         // JCU Error reset
00245         if (jcu_error_flag == true) {
00246             (void)R_JCU_Terminate();
00247             (void)R_JCU_Initialize(NULL);
00248             jcu_error_flag = false;
00249         }
00250         // Get mutex
00251         if (pOptions->p_DecodeCallBackFunc == NULL) {
00252             jpeg_converter_semaphore.wait(0xFFFFFFFFuL); // WAIT
00253         } else {
00254             if (!jpeg_converter_semaphore.wait(0)) {
00255                 e = JPEG_CONV_BUSY ;  // Busy
00256                 goto  fin;
00257             }
00258         }
00259         // Select decode
00260         jcu_error = R_JCU_SelectCodec( JCU_DECODE );
00261         if (jcu_error != JCU_ERROR_OK) {
00262             e = JPEG_CONV_JCU_ERR ;
00263             mutex_release = true;
00264             goto fin;
00265         }
00266         
00267         buffer.source.swapSetting       = JCU_SWAP_LONG_WORD_AND_WORD_AND_BYTE;
00268         buffer.source.address           = (uint32_t *)pBuff;
00269         buffer.lineOffset               = (int16_t)psOutputBuff->width ;
00270         buffer.destination.address      = (uint32_t *)psOutputBuff->buffer_address ;
00271         decode.decodeFormat             = (jcu_decode_format_t)psOutputBuff->format ;
00272         buffer.destination.swapSetting  = (jcu_swap_t)pOptions->output_swapsetting;
00273         decode.outputCbCrOffset         = (jcu_cbcr_offset_t)pOptions->output_cb_cr_offset;
00274         decode.alpha                    = pOptions->alpha;
00275         decode.horizontalSubSampling    = (jcu_sub_sampling_t)pOptions->horizontal_sub_sampling;
00276         decode.verticalSubSampling      = (jcu_sub_sampling_t)pOptions->vertical_sub_sampling;
00277 
00278         jcu_error = R_JCU_SetDecodeParam(&decode, &buffer);
00279         if (jcu_error != JCU_ERROR_OK) {
00280             e = JPEG_CONV_FORMA_ERR ;
00281             mutex_release = true;
00282             jcu_error_flag = false;
00283             goto fin;
00284         }
00285 
00286         if (pOptions->check_jpeg_format != false) {
00287             jcu_error = R_JCU_SetPauseForImageInfo(true);
00288             if (jcu_error != JCU_ERROR_OK) {
00289                 e = JPEG_CONV_JCU_ERR ;
00290                 mutex_release = true;
00291                 jcu_error_flag = false;
00292                 goto fin;
00293             }
00294         }
00295         if (pOptions->p_DecodeCallBackFunc == NULL) {
00296             jcu_error = R_JCU_Start();
00297             mutex_release = true;
00298             if (jcu_error != JCU_ERROR_OK) {
00299                 e = JPEG_CONV_JCU_ERR ;
00300                 jcu_error_flag = false;
00301                 goto fin;
00302             }
00303             if (pOptions->check_jpeg_format != false) {
00304                 R_JCU_GetAsyncStatus( &status );
00305                 if (status -> IsPaused == false) {
00306                     e = JPEG_CONV_JCU_ERR ;
00307                     jcu_error_flag = false;
00308                     goto fin;
00309                 }
00310                 if ((status->SubStatusFlags & JCU_SUB_INFOMATION_READY) == 0) {
00311                     e = JPEG_CONV_JCU_ERR ;
00312                     jcu_error_flag = false;
00313                     goto fin;
00314                 }
00315                 jcu_error = R_JCU_GetImageInfo( &image_info );
00316                 if (jcu_error != JCU_ERROR_OK) {
00317                     e = JPEG_CONV_JCU_ERR ;
00318                     jcu_error_flag = false;
00319                     goto fin;
00320                 }
00321                 if ((image_info.width == 0u) || (image_info.height == 0u) || 
00322                     (image_info.width > calc_width) || 
00323                     (image_info.height > calc_height)) {
00324                     e = JPEG_CONV_FORMA_ERR ;
00325                     jcu_error_flag = false;
00326                     goto fin;
00327                 }
00328                 if ((image_info.encodedFormat != JCU_JPEG_YCbCr444) &&
00329                     (image_info.encodedFormat != JCU_JPEG_YCbCr422) &&
00330                     (image_info.encodedFormat != JCU_JPEG_YCbCr420) &&
00331                     (image_info.encodedFormat != JCU_JPEG_YCbCr411)) {
00332                     e = JPEG_CONV_FORMA_ERR ;
00333                     jcu_error_flag = false;
00334                     goto fin;
00335                 }
00336                 jcu_error = R_JCU_Continue(JCU_IMAGE_INFO);
00337                 if (jcu_error != JCU_ERROR_OK) {
00338                     e = JPEG_CONV_JCU_ERR ;
00339                     jcu_error_flag = false;
00340                     goto fin;
00341                 }
00342             }
00343         } else {
00344             pJPEG_ConverterCallback = pOptions->p_DecodeCallBackFunc;
00345             jcu_error = R_wrpper_set_decode_callback((mbed_CallbackFunc_t*)JPEG_CallbackFunction, (size_t)calc_width, calc_height);
00346             if (jcu_error != JCU_ERROR_OK) {
00347                 e = JPEG_CONV_JCU_ERR ;
00348                 mutex_release = true;
00349                 goto fin;
00350             }
00351         }
00352         e = JPEG_CONV_OK ;
00353     }
00354     else
00355     {
00356         e = JPEG_CONV_JCU_ERR ;
00357     }
00358 fin:
00359     if (mutex_release == true) {
00360         jpeg_converter_semaphore.release(); // RELEASE
00361     }
00362 
00363     return  e;
00364 } /* End of method decode() */
00365 
00366 
00367 /**************************************************************************//**
00368  * @brief       Bitmap data encode to JPEG
00369  * @param[in]   bitmap_buff_info_t*     psInputBuff     : Input bitmap data address
00370  * @param[out]  void*                   pJpegBuff       : Output JPEG data address
00371  * @param[out]  size_t*                 pEncodeSize     : Encode size address
00372  * @retval      error code
00373 ******************************************************************************/
00374 JPEG_Converter::jpeg_conv_error_t
00375 JPEG_Converter::encode(bitmap_buff_info_t* psInputBuff, void* pJpegBuff, size_t* pEncodeSize ) {
00376     encode_options_t Options;
00377     
00378     return (encode(psInputBuff, pJpegBuff, pEncodeSize, &Options));
00379 } /* End of method encode() */
00380 
00381 
00382 /**************************************************************************//**
00383  * @brief       Bitmap data encode to JPEG
00384  * @param[in]   bitmap_buff_info_t*     psInputBuff     : Input bitmap data address
00385  * @param[out]  void*                   pJpegBuff       : Output JPEG data address
00386  * @param[out]  size_t*                 pEncodeSize     : Encode size address
00387  * @param[in]   encode_options_t*       pOptions        : Encode option(Optional)
00388  * @retval      error code
00389 ******************************************************************************/
00390 JPEG_Converter::jpeg_conv_error_t
00391 JPEG_Converter::encode(bitmap_buff_info_t* psInputBuff, void* pJpegBuff, size_t* pEncodeSize, encode_options_t* pOptions ) {
00392 
00393     jpeg_conv_error_t   e;
00394     jcu_errorcode_t     jcu_error;
00395     jcu_buffer_param_t  buffer;
00396     uint8_t*            TableAddress;
00397     jcu_encode_param_t  encode;
00398     jcu_count_mode_param_t      count_para;
00399     int32_t     encode_count;
00400     int32_t    size_max_count = 1;
00401     size_t      BufferSize = pOptions->encode_buff_size ;
00402     const jcu_async_status_t*  status;
00403     bool                        mutex_release = false;
00404 
00405     // Check JCU initialized
00406     if (driver_ac_count > 0) {
00407         // Check input address
00408         if ((pJpegBuff == NULL) || (psInputBuff == NULL) || (pEncodeSize == NULL)) {
00409             e = JPEG_CONV_PARAM_ERR ;  // Input address error
00410             goto  fin;
00411         }
00412         // JCU Error reset
00413         if (jcu_error_flag == true) {
00414             (void)R_JCU_Terminate();
00415             (void)R_JCU_Initialize(NULL);
00416             jcu_error_flag = false;
00417         }
00418         // Get mutex
00419         if ( pOptions->p_EncodeCallBackFunc  == NULL ) {
00420             jpeg_converter_semaphore.wait(0xFFFFFFFFuL); // WAIT
00421         } else {
00422             if (!jpeg_converter_semaphore.wait(0)) {
00423                 e = JPEG_CONV_BUSY ;  // Busy
00424                 goto  fin;
00425             }
00426         }
00427         // Select encode
00428         jcu_error = R_JCU_SelectCodec(JCU_ENCODE);
00429         if (jcu_error != JCU_ERROR_OK) {
00430             e = JPEG_CONV_JCU_ERR ;
00431             mutex_release = true;
00432             goto fin;
00433         }
00434         /* Set tables */
00435         if ( pOptions->quantization_table_Y  != NULL ) {
00436             TableAddress = (uint8_t*)pOptions->quantization_table_Y ;
00437         } else {
00438             TableAddress = (uint8_t*)csaDefaultQuantizationTable_Y;
00439         }
00440         jcu_error = R_JCU_SetQuantizationTable( JCU_TABLE_NO_0, (uint8_t*)TableAddress );
00441         if ( jcu_error != JCU_ERROR_OK ) {
00442             e = JPEG_CONV_PARAM_RANGE_ERR ;
00443             mutex_release = true;
00444             jcu_error_flag = true;
00445             goto fin;
00446         }
00447         if ( pOptions->quantization_table_C  != NULL ) {
00448             TableAddress = (uint8_t*)pOptions->quantization_table_C ;
00449         } else {
00450             TableAddress = (uint8_t*)csaDefaultQuantizationTable_C;
00451         }
00452         jcu_error = R_JCU_SetQuantizationTable( JCU_TABLE_NO_1, (uint8_t*)TableAddress );
00453         if ( jcu_error != JCU_ERROR_OK ) {
00454             e = JPEG_CONV_PARAM_RANGE_ERR ;
00455             mutex_release = true;
00456             jcu_error_flag = true;
00457             goto fin;
00458         }
00459         if ( pOptions->huffman_table_Y_DC  != NULL ) {
00460             TableAddress = (uint8_t*)pOptions->huffman_table_Y_DC ;
00461         } else {
00462             TableAddress = (uint8_t*)csaDefaultHuffmanTable_Y_DC;
00463         }
00464         jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_0, JCU_HUFFMAN_DC, (uint8_t*)TableAddress );
00465         if ( jcu_error != JCU_ERROR_OK ) {
00466             e = JPEG_CONV_PARAM_RANGE_ERR ;
00467             mutex_release = true;
00468             jcu_error_flag = true;
00469             goto fin;
00470         }
00471         if ( pOptions->huffman_table_C_DC  != NULL ) {
00472             TableAddress = (uint8_t*)pOptions->huffman_table_C_DC ;
00473         } else {
00474             TableAddress = (uint8_t*)csaDefaultHuffmanTable_C_DC;
00475         }
00476         jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_1, JCU_HUFFMAN_DC, (uint8_t*)TableAddress );
00477         if ( jcu_error != JCU_ERROR_OK ) {
00478             e = JPEG_CONV_PARAM_RANGE_ERR ;
00479             mutex_release = true;
00480             jcu_error_flag = true;
00481             goto fin;
00482         }
00483         if ( pOptions->huffman_table_Y_AC  != NULL ) {
00484             TableAddress = (uint8_t*)pOptions->huffman_table_Y_AC ;
00485         } else {
00486             TableAddress = (uint8_t*)csaDefaultHuffmanTable_Y_AC;
00487         }
00488         jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_0, JCU_HUFFMAN_AC, (uint8_t*)TableAddress );
00489         if ( jcu_error != JCU_ERROR_OK ) {
00490             e = JPEG_CONV_PARAM_RANGE_ERR ;
00491             mutex_release = true;
00492             jcu_error_flag = true;
00493             goto fin;
00494         }
00495         if ( pOptions->huffman_table_C_AC  != NULL ) {
00496             TableAddress = (uint8_t*)pOptions->huffman_table_C_AC ;
00497         } else {
00498             TableAddress = (uint8_t*)csaDefaultHuffmanTable_C_AC;
00499         }
00500         jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_1, JCU_HUFFMAN_AC, (uint8_t*)TableAddress );
00501         if ( jcu_error != JCU_ERROR_OK ) {
00502             e = JPEG_CONV_PARAM_RANGE_ERR ;
00503             mutex_release = true;
00504             jcu_error_flag = true;
00505             goto fin;
00506         }
00507 
00508         // JPEG encode
00509         buffer.source.swapSetting       = (jcu_swap_t)pOptions->input_swapsetting ;
00510         buffer.source.address           = (uint32_t *)psInputBuff->buffer_address ;
00511         buffer.destination.swapSetting  = JCU_SWAP_LONG_WORD_AND_WORD_AND_BYTE;
00512         buffer.destination.address      = (uint32_t *)pJpegBuff;
00513         buffer.lineOffset               = psInputBuff->width ;
00514         encode.encodeFormat = (jcu_jpeg_format_t)JCU_JPEG_YCbCr422;
00515         encode.QuantizationTable[ JCU_ELEMENT_Y  ] = JCU_TABLE_NO_0;
00516         encode.QuantizationTable[ JCU_ELEMENT_Cb ] = JCU_TABLE_NO_1;
00517         encode.QuantizationTable[ JCU_ELEMENT_Cr ] = JCU_TABLE_NO_1;
00518         encode.HuffmanTable[ JCU_ELEMENT_Y  ] = JCU_TABLE_NO_0;
00519         encode.HuffmanTable[ JCU_ELEMENT_Cb ] = JCU_TABLE_NO_1;
00520         encode.HuffmanTable[ JCU_ELEMENT_Cr ] = JCU_TABLE_NO_1;
00521         encode.DRI_value = pOptions->DRI_value ;
00522         if ( pOptions->width  != 0 ) {
00523             encode.width  = pOptions->width ;
00524         } else {
00525             encode.width  = psInputBuff->width ;
00526         }
00527         if ( pOptions->height  != 0 ) {
00528             encode.height = pOptions->height ;
00529         } else {
00530             encode.height = psInputBuff->height ;
00531         }
00532         encode.inputCbCrOffset = (jcu_cbcr_offset_t)pOptions->input_cb_cr_offset ;
00533         jcu_error = R_JCU_SetEncodeParam( &encode, &buffer );
00534         if ( jcu_error != JCU_ERROR_OK ) {
00535             e = JPEG_CONV_PARAM_RANGE_ERR ;
00536             mutex_release = true;
00537             jcu_error_flag = true;
00538             goto fin;
00539         }
00540         if (pOptions->encode_buff_size  > 0) {
00541 
00542             while(BufferSize > ENC_SIZE_MAX) {
00543                 size_max_count *= 2;
00544                 BufferSize /= 2;
00545             }
00546             BufferSize = BufferSize & MASK_8BYTE;
00547             
00548             count_para.inputBuffer.isEnable       = false;
00549             count_para.inputBuffer.isInitAddress  = false;
00550             count_para.inputBuffer.restartAddress = NULL;
00551             count_para.inputBuffer.dataCount      = 0;
00552             count_para.outputBuffer.isEnable       = true;
00553             count_para.outputBuffer.isInitAddress  = false;
00554             count_para.outputBuffer.restartAddress = NULL;
00555             count_para.outputBuffer.dataCount      = BufferSize;
00556             
00557             R_JCU_SetCountMode(&count_para);
00558         } else {
00559             size_max_count = 0;
00560         }
00561         // Check async
00562         if ( pOptions->p_EncodeCallBackFunc  == NULL ) {
00563             jcu_error = R_JCU_Start();
00564             mutex_release = true;
00565             if ( jcu_error != JCU_ERROR_OK ) {
00566                 e = JPEG_CONV_JCU_ERR ;
00567                 jcu_error_flag = true;
00568                 goto fin;
00569             }
00570             if (pOptions->encode_buff_size  > 0) {
00571                 // Check Pause flag
00572                 R_JCU_GetAsyncStatus( &status );
00573                 for ( encode_count = 1; (encode_count < size_max_count) && (status->IsPaused != false); encode_count++) {
00574                     if ((status->SubStatusFlags & JCU_SUB_ENCODE_OUTPUT_PAUSE) == 0) {
00575                         e = JPEG_CONV_JCU_ERR ;
00576                         jcu_error_flag = true;
00577                         goto fin;
00578                     }
00579                     jcu_error = R_JCU_Continue( JCU_OUTPUT_BUFFER );
00580                     if (jcu_error != JCU_ERROR_OK) {
00581                         e = JPEG_CONV_JCU_ERR ;
00582                         jcu_error_flag = true;
00583                         goto fin;
00584                     }
00585                     R_JCU_GetAsyncStatus( &status );
00586                 }
00587                 if (status->IsPaused != false) {
00588                     e = JPEG_CONV_PARAM_RANGE_ERR ;
00589                     jcu_error_flag = true;
00590                     goto fin;
00591                 }
00592             }
00593             (void)R_JCU_GetEncodedSize(pEncodeSize);
00594         } else {
00595             pJPEG_ConverterCallback = pOptions->p_EncodeCallBackFunc ;
00596             jcu_error = R_wrpper_set_encode_callback((mbed_CallbackFunc_t*)JPEG_CallbackFunction, pEncodeSize, size_max_count);
00597             if ( jcu_error != JCU_ERROR_OK ) {
00598                 e = JPEG_CONV_JCU_ERR ;
00599                 goto fin;
00600             }
00601         }
00602         e = JPEG_CONV_OK ;
00603     } else {
00604         e = JPEG_CONV_PARAM_RANGE_ERR ;
00605     }
00606 fin:
00607     if (mutex_release == true) {
00608         jpeg_converter_semaphore.release(); // RELEASE
00609     }
00610 
00611     return  e;
00612 } /* End of method encode() */
00613 
00614