Graphics framework for GR-PEACH. When you use this program, we judge you have agreed to the following contents. https://developer.mbed.org/teams/Renesas/wiki/About-LICENSE

Dependents:   ImageZoomInout_Sample ImageRotaion_Sample ImageScroll_Sample GR-PEACH_LCD_4_3inch_Save_to_USB ... more

License

When you use this library, we judge you have agreed to the following contents.

https://developer.mbed.org/teams/Renesas/wiki/About-LICENSE

Note

If you import the GraphicsFramework library, please import GR-PEACH_video library and R_BSP library together.



JPEG Converter

The JPEG Converter driver implements encode and decode functionality which uses the JCU of the RZ/A Series.

Hello World!

Import programJCU_HelloWorld

Hello World for JCU(JPEG Codec Unit). JCU is JPEG codec unit of RZ/A1. When you use this program, we judge you have agreed to the following contents. https://developer.mbed.org/teams/Renesas/wiki/About-LICENSE

API

Import library

Data Structures

struct bitmap_buff_info_t
Bitmap data setting struct. More...
struct encode_options_t
Encode option setting. More...

Public Types

enum jpeg_conv_error_t {
JPEG_CONV_OK = 0, JPEG_CONV_JCU_ERR = -1, JPEG_CONV_FORMA_ERR = -2, JPEG_CONV_PARAM_ERR = -3,
JPEG_CONV_BUSY = -4, JPEG_CONV_PARAM_RANGE_ERR = -7
}

Error codes.

More...
enum wr_rd_swa_t {
WR_RD_WRSWA_NON = 0, WR_RD_WRSWA_8BIT = 1, WR_RD_WRSWA_16BIT = 2, WR_RD_WRSWA_16_8BIT = 3,
WR_RD_WRSWA_32BIT = 4, WR_RD_WRSWA_32_8BIT = 5, WR_RD_WRSWA_32_16BIT = 6, WR_RD_WRSWA_32_16_8BIT = 7
}

Write/Read image pixcel frame buffer swap setting.

More...
enum wr_rd_format_t { WR_RD_YCbCr422 = 0x00, WR_RD_ARGB8888 = 0x01, WR_RD_RGB565 = 0x02 }

Write/Read image pixcel format selects.

More...
enum sub_sampling_t { SUB_SAMPLING_1_1 = 0x00, SUB_SAMPLING_1_2 = 0x01, SUB_SAMPLING_1_4 = 0x02, SUB_SAMPLING_1_8 = 0x03 }

Thinning output image selects.

More...
enum cbcr_offset_t { CBCR_OFFSET_0 = 0x00, CBCR_OFFSET_128 = 0x01 }

Cb/Cr range selects for decode.

More...

Public Member Functions

JPEG_Converter ()
Constructor method of JPEG converter(encode/decode)
virtual ~JPEG_Converter ()
Destructor method of JPEG converter(encode/decode)
JPEG_Converter::jpeg_conv_error_t decode (void *pJpegBuff, bitmap_buff_info_t *psOutputBuff)
Decode JPEG to rinear data.
JPEG_Converter::jpeg_conv_error_t decode (void *pJpegBuff, bitmap_buff_info_t *psOutputBuff, decode_options_t *pOptions)
JPEG data decode to bitmap.
JPEG_Converter::jpeg_conv_error_t encode ( bitmap_buff_info_t *psInputBuff, void *pJpegBuff, size_t *pEncodeSize)
Encode rinear data to JPEG.
JPEG_Converter::jpeg_conv_error_t encode ( bitmap_buff_info_t *psInputBuff, void *pJpegBuff, size_t *pEncodeSize, encode_options_t *pOptions)
Bitmap data encode to JPEG.
JPEG_Converter::jpeg_conv_error_t SetQuality (const uint8_t qual)
Set encode quality.

Correspondence file

A correspondence file of JPEG Converter is as the following table.

JPEGCorrespondence
Width>0(greater than 0)
Height>0(greater than 0)
Color formatYCbCr444, YCbCr422, YCbCr420, YCbCr411
BitmapCorrespondence
Width>0(greater than 0)
Height>0(greater than 0)
Color formatYCbCr422

Notice

You run JPEG converter once destruction each time.

You set whether these JPEG files aren't input, or it check error setting decode(set in "flag" = true). The JPEG file which becomes correspondence outside will be the following condition.

  • File besides the above-mentioned correspondence file.
  • As information in the JPEG file, WIDTH or HEIGHT is larger than output buffer setting.

Buffer area is used encode/decode, set 8 bytes align and non-cash memory area. The output buffer when decoding, is made beyond the size decided in the size of the JPEG file, the format, setting of thinning out. You make output buffer for decode/encode to enough big size in order to stock this result. JPEG Converter, if you do not particularly perform specified, does not check size against the output data at the time of encoding and decoding. You set the output buffer so that there is no effect of corruption by the output data.

Color format

Color format in case to be converted from Bitmap to JPEG is either ARGB8888 or RGB555, YCbCr422. Color format of the If you want to convert from JPEG file to Bitmap file is YCbCr422. You correct "alpha(member of decode_options_t)" of setting and "output_cb_cr_offset(member of decode_options_t)" according to color format when decoding.

  • example
    decode to ARGB8888(WR_RD_ARGB8888 set in format member of bitmap_buff_info_t)
    alpha = 0x01-0xFF
    output_cb_cr_offset = CBCR_OFFSET_0

    decode to YCbCr422(WR_RD_YCbCr422 set in format member of bitmap_buff_info_t)
    alpha = 0
    output_cb_cr_offset = CBCR_OFFSET_0 or CBCR_OFFSET_128

    decode to RGB565(WR_RD_RGB565 set in format member of bitmap_buff_info_t)
    alpha = 0
    output_cb_cr_offset = CBCR_OFFSET_0

Decode/encode settings are optional

If omitted encode/decode settings, it will work with the following settings.
[Decode option setting (member of decode_options_t)]

  • Vertical sub sampling is thinning output image to 1/1.
  • Horizontal sub sampling is thinning output image to 1/1.
  • Output data of Cb/Cr range is -128 to 127.
  • Output data of swap in 8-bit units: 2-1-4-3-6-5-8-7.
  • Alpha value of 0.
  • JPEG format correspondence outside error check.
  • It decode in a synchronous function.

[Encode option setting (member of encode_options_t)]

  • DRI value is 0.
  • Encoding JPEG file start width offset is 0.
  • Encoding JPEG file start height offset is 0.
  • Input data of Cb/Cr range of input data is -128 to 127.
  • Input data swap in 8-bit units: 2-1-4-3-6-5-8-7.
  • It don't check encode size.
  • Quantization Y use default table(Quality75).
  • Quantization C use default table(Quality75).
  • Huffman Y DC use default table.
  • Huffman C DC use default table.
  • Huffman Y AC use default table.
  • Huffman C AC use default table.
  • It encode in a synchronous function.

Synchronous/asynchronous switching

Decoding and encoding setting to operate asynchronously by setting a callback function(decode_options_t and encode_options_t).

Quality

Quality changes are possible. If you want to change the Quality, please specify the table made of Quality you want to change the address of the setting. If you do not want to change the Quality, it will operate at Quality75.

RGA

The RGA library implements fast drawing functionality which uses the RGA of the RZ/A Series.
Supporting compiler is ARMCC, GCC ARM and IAR.

Hello World!

Import programRGA_HelloWorld

Hello World for RGA(Renesas Graphics Architecture). RGA is the Graphics Library of RZ/A1. When you use this program, we judge you have agreed to the following contents. https://developer.mbed.org/teams/Renesas/wiki/About-LICENSE

Revision:
1:7b90e31adc5b
Parent:
0:37e1e6a45ced
Child:
7:df2bc72f7fb7
--- a/jcu/jcu_driver/JPEG_Coverter.cpp	Wed Aug 19 08:58:26 2015 +0000
+++ b/jcu/jcu_driver/JPEG_Coverter.cpp	Fri Oct 09 08:01:56 2015 +0000
@@ -38,6 +38,9 @@
 #include  "r_jcu_api.h"
 #include  "JPEG_Converter.h"
 #include  "converter_wrapper.h"
+#include  "mbed_error.h"
+#include  "rtos.h"
+#include  "Semaphore.h"
 
 
 /******************************************************************************
@@ -132,20 +135,45 @@
         0xF9, 0xFA
     };
 
-static  r_ospl_async_t   async;
+typedef void (JPEG_CallbackFunc_t)(JPEG_Converter::jpeg_conv_error_t err_code);
+
+static uint32_t             driver_ac_count = 0;
+static bool                 jcu_error_flag;
+static JPEG_CallbackFunc_t* pJPEG_ConverterCallback;
+Semaphore                   jpeg_converter_semaphore(1);
+
+
+/**************************************************************************//**
+ * @brief       Callback function from JCU async mode
+ * @param[in]   mbed_jcu_err_t          err_code       : JCU result
+ * @retval      None
+******************************************************************************/
+void JPEG_CallbackFunction(mbed_jcu_err_t err_code) {
+    if (pJPEG_ConverterCallback != NULL) {
+        pJPEG_ConverterCallback((JPEG_Converter::jpeg_conv_error_t)err_code);
+    }
+    if (err_code != MBED_JCU_E_OK) {
+        jcu_error_flag = true;
+    }
+    jpeg_converter_semaphore.release(); // RELEASE
+} /* End of callback function method () */
 
 /**************************************************************************//**
  * @brief       Constructor of the JPEG_Converter class
  * @param[in]   None
  * @retval      None
 ******************************************************************************/
-JPEG_Converter::JPEG_Converter( void )
-{
+JPEG_Converter::JPEG_Converter(void) {
     jcu_errorcode_t           jcu_error;
     
-    jcu_error = R_JCU_Initialize( NULL );
-    if ( jcu_error == JCU_ERROR_OK ) {
-        is_JCU_initialized = true;
+    if (driver_ac_count == 0) {
+        jcu_error = R_JCU_Initialize(NULL);
+        if (jcu_error == JCU_ERROR_OK) {
+            driver_ac_count++;
+            jcu_error_flag = false;
+        }
+    } else {
+        driver_ac_count++;
     }
 } /* End of constructor method () */
 
@@ -154,11 +182,12 @@
  * @param[in]   None
  * @retval      None
 ******************************************************************************/
-JPEG_Converter::~JPEG_Converter( void )
-{
-    if ( is_JCU_initialized != false ) {
-        is_JCU_initialized = false;
-        (void)R_JCU_Terminate();
+JPEG_Converter::~JPEG_Converter(void) {
+    if (driver_ac_count > 0) {
+        driver_ac_count--;
+        if (driver_ac_count == 0) {
+            (void)R_JCU_Terminate();
+        }
     }
 } /* End of destructor method () */
 
@@ -169,63 +198,10 @@
  * @retval        error code
 ******************************************************************************/
 JPEG_Converter::jpeg_conv_error_t
-JPEG_Converter::decode(void* pJpegBuff, bitmap_buff_info_t* psOutputBuff ) {
-    jpeg_conv_error_t                  e;
-    jcu_errorcode_t           jcu_error;
-    jcu_decode_param_t        decode;
-    jcu_buffer_param_t        buffer;
-    std::uint8_t*           pBuff = (std::uint8_t *)pJpegBuff;
+JPEG_Converter::decode(void* pJpegBuff, bitmap_buff_info_t* psOutputBuff) {
+    decode_options_t Options;
     
-    // Check JCU initialized
-    if ( is_JCU_initialized != false ) {
-        // Check input address
-        if ((pJpegBuff == NULL) || (psOutputBuff == NULL)) {
-            e = JPEG_CONV_PARAM_ERR;  // Input address error
-            goto  fin;
-        }
-        // Check JPEG header
-        if ( ((std::uint32_t)(pBuff[0]) != JPEG_HEADER_LETTER_1 ) ||
-             ((std::uint32_t)(pBuff[1]) != JPEG_HEADER_LETTER_2) ) {
-            e = JPEG_CONV_FORMA_ERR;  // JPEG data is not in ROM
-            goto  fin;
-        }
-
-        jcu_error = R_JCU_SelectCodec( JCU_DECODE );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_FORMA_ERR;
-            goto fin;
-        }
-
-        buffer.source.swapSetting       = JCU_SWAP_LONG_WORD_AND_WORD_AND_BYTE;
-        buffer.source.address           = (uint32_t *)pBuff;
-        buffer.lineOffset               = (int16_t)psOutputBuff->width;
-        buffer.destination.address      = (uint32_t *)psOutputBuff->buffer_address;
-        decode.decodeFormat             = (jcu_decode_format_t)psOutputBuff->format;    // JCU_OUTPUT_YCbCr422;
-        buffer.destination.swapSetting  = (jcu_swap_t)WR_RD_WRSWA_8BIT;
-        decode.outputCbCrOffset         = (jcu_cbcr_offset_t)1;
-        decode.alpha                    = 0;
-        decode.horizontalSubSampling    = (jcu_sub_sampling_t)SUB_SAMPLING_1_1;
-        decode.verticalSubSampling      = (jcu_sub_sampling_t)SUB_SAMPLING_1_1;
-        
-        jcu_error = R_JCU_SetDecodeParam( &decode, &buffer );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_PARAM_RANGE_ERR;
-            goto fin;
-        }
-
-        jcu_error = R_JCU_Start();
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_JCU_ERR;
-            goto fin;
-        }
-        e = JPEG_CONV_OK;
-    }
-    else
-    {
-        e = JPEG_CONV_PARAM_RANGE_ERR;
-    }
-fin:
-    return  e;
+    return (decode(pJpegBuff, psOutputBuff, &Options));
 } /* End of method decode() */
 
 /**************************************************************************//**
@@ -236,19 +212,20 @@
  * @retval      error code
 ******************************************************************************/
 JPEG_Converter::jpeg_conv_error_t
-JPEG_Converter::decode(void* pJpegBuff, bitmap_buff_info_t* psOutputBuff, decode_options_t* pOptions ) {
+JPEG_Converter::decode(void* pJpegBuff, bitmap_buff_info_t* psOutputBuff, decode_options_t* pOptions) {
     jpeg_conv_error_t           e;
     jcu_errorcode_t             jcu_error;
     jcu_decode_param_t          decode;
     jcu_buffer_param_t          buffer;
-    std::uint8_t*               pBuff = (std::uint8_t *)pJpegBuff;
+    uint8_t*            pBuff = (uint8_t *)pJpegBuff;
     const jcu_async_status_t*   status;
     jcu_image_info_t            image_info;
+    bool                        mutex_release = false;
     
     // Check JCU initialized
-    if ( is_JCU_initialized != false ) {
-        std::size_t calc_height;
-        std::size_t calc_width;
+    if (driver_ac_count > 0) {
+        size_t calc_height;
+        size_t calc_width;
         
         calc_height = psOutputBuff->height * (2 ^ pOptions->vertical_sub_sampling);
         calc_width  = psOutputBuff->width * (2 ^ pOptions->horizontal_sub_sampling);
@@ -259,15 +236,31 @@
             goto  fin;
         }
         // Check JPEG header
-        if ( ((std::uint32_t)(pBuff[0]) != JPEG_HEADER_LETTER_1 ) ||
-             ((std::uint32_t)(pBuff[1]) != JPEG_HEADER_LETTER_2) ) {
+        if (((uint32_t)(pBuff[0]) != JPEG_HEADER_LETTER_1) ||
+            ((uint32_t)(pBuff[1]) != JPEG_HEADER_LETTER_2)) {
             e = JPEG_CONV_FORMA_ERR;  // JPEG data is not in ROM
             goto  fin;
         }
-
+        // JCU Error reset
+        if (jcu_error_flag == true) {
+            (void)R_JCU_Terminate();
+            (void)R_JCU_Initialize(NULL);
+            jcu_error_flag = false;
+        }
+        // Get mutex
+        if (pOptions->p_DecodeCallBackFunc == NULL) {
+            jpeg_converter_semaphore.wait(0xFFFFFFFFuL); // WAIT
+        } else {
+            if (!jpeg_converter_semaphore.wait(0)) {
+                e = JPEG_CONV_BUSY;  // Busy
+                goto  fin;
+            }
+        }
+        // Select decode
         jcu_error = R_JCU_SelectCodec( JCU_DECODE );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_FORMA_ERR;
+        if (jcu_error != JCU_ERROR_OK) {
+            e = JPEG_CONV_JCU_ERR;
+            mutex_release = true;
             goto fin;
         }
         
@@ -281,60 +274,78 @@
         decode.alpha                    = pOptions->alpha;
         decode.horizontalSubSampling    = (jcu_sub_sampling_t)pOptions->horizontal_sub_sampling;
         decode.verticalSubSampling      = (jcu_sub_sampling_t)pOptions->vertical_sub_sampling;
-        
-        jcu_error = R_JCU_SetDecodeParam( &decode, &buffer );
-        if ( jcu_error != JCU_ERROR_OK ) {
+
+        jcu_error = R_JCU_SetDecodeParam(&decode, &buffer);
+        if (jcu_error != JCU_ERROR_OK) {
             e = JPEG_CONV_FORMA_ERR;
+            mutex_release = true;
+            jcu_error_flag = false;
             goto fin;
         }
 
         if (pOptions->check_jpeg_format != false) {
-            jcu_error = R_JCU_SetPauseForImageInfo( true );
-            if ( jcu_error != JCU_ERROR_OK ) {
+            jcu_error = R_JCU_SetPauseForImageInfo(true);
+            if (jcu_error != JCU_ERROR_OK) {
                 e = JPEG_CONV_JCU_ERR;
+                mutex_release = true;
+                jcu_error_flag = false;
                 goto fin;
             }
         }
-        if ( pOptions->p_DecodeCallBackFunc == NULL ) {
+        if (pOptions->p_DecodeCallBackFunc == NULL) {
             jcu_error = R_JCU_Start();
-            if ( jcu_error != JCU_ERROR_OK ) {
+            mutex_release = true;
+            if (jcu_error != JCU_ERROR_OK) {
                 e = JPEG_CONV_JCU_ERR;
-                goto fin;
-            }
-            R_JCU_GetAsyncStatus( &status );
-            if (status -> IsPaused == false) {
-                e = JPEG_CONV_JCU_ERR;
-                goto fin;
-            }
-            if ((status->SubStatusFlags & JCU_SUB_INFOMATION_READY) == 0) {
-                e = JPEG_CONV_JCU_ERR;
+                jcu_error_flag = false;
                 goto fin;
             }
-            jcu_error = R_JCU_GetImageInfo( &image_info );
-            if (jcu_error != JCU_ERROR_OK) goto fin;
-            
-            if ((image_info.width == 0u) || (image_info.height == 0u) || 
-                (image_info.width > calc_width) || 
-                (image_info.height > calc_height)) {
-                e = JPEG_CONV_FORMA_ERR;
-                goto fin;
+            if (pOptions->check_jpeg_format != false) {
+                R_JCU_GetAsyncStatus( &status );
+                if (status -> IsPaused == false) {
+                    e = JPEG_CONV_JCU_ERR;
+                    jcu_error_flag = false;
+                    goto fin;
+                }
+                if ((status->SubStatusFlags & JCU_SUB_INFOMATION_READY) == 0) {
+                    e = JPEG_CONV_JCU_ERR;
+                    jcu_error_flag = false;
+                    goto fin;
+                }
+                jcu_error = R_JCU_GetImageInfo( &image_info );
+                if (jcu_error != JCU_ERROR_OK) {
+                    e = JPEG_CONV_JCU_ERR;
+                    jcu_error_flag = false;
+                    goto fin;
+                }
+                if ((image_info.width == 0u) || (image_info.height == 0u) || 
+                    (image_info.width > calc_width) || 
+                    (image_info.height > calc_height)) {
+                    e = JPEG_CONV_FORMA_ERR;
+                    jcu_error_flag = false;
+                    goto fin;
+                }
+                if ((image_info.encodedFormat != JCU_JPEG_YCbCr444) &&
+                    (image_info.encodedFormat != JCU_JPEG_YCbCr422) &&
+                    (image_info.encodedFormat != JCU_JPEG_YCbCr420) &&
+                    (image_info.encodedFormat != JCU_JPEG_YCbCr411)) {
+                    e = JPEG_CONV_FORMA_ERR;
+                    jcu_error_flag = false;
+                    goto fin;
+                }
+                jcu_error = R_JCU_Continue(JCU_IMAGE_INFO);
+                if (jcu_error != JCU_ERROR_OK) {
+                    e = JPEG_CONV_JCU_ERR;
+                    jcu_error_flag = false;
+                    goto fin;
+                }
             }
-            if ((image_info.encodedFormat != JCU_JPEG_YCbCr444) &&
-                (image_info.encodedFormat != JCU_JPEG_YCbCr422) &&
-                (image_info.encodedFormat != JCU_JPEG_YCbCr420) &&
-                (image_info.encodedFormat != JCU_JPEG_YCbCr411)) {
-                e = JPEG_CONV_FORMA_ERR;
-                goto fin;
-            }
-            jcu_error = R_JCU_Continue(JCU_IMAGE_INFO);
-            if (jcu_error != JCU_ERROR_OK) goto fin;
         } else {
-            async.Flags = R_F_OSPL_InterruptCallback;
-            async.A_Thread = R_OSPL_THREAD_GetCurrentId();
-            (void)R_wrpper_set_decode_callback((mbed_CallbackFunc_t*)pOptions->p_DecodeCallBackFunc, &async, (size_t)calc_width, calc_height);
-            jcu_error = R_JCU_StartAsync( &async );
-            if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_JCU_ERR;
+            pJPEG_ConverterCallback = pOptions->p_DecodeCallBackFunc;
+            jcu_error = R_wrpper_set_decode_callback((mbed_CallbackFunc_t*)JPEG_CallbackFunction, (size_t)calc_width, calc_height);
+            if (jcu_error != JCU_ERROR_OK) {
+                e = JPEG_CONV_JCU_ERR;
+                mutex_release = true;
                 goto fin;
             }
         }
@@ -345,6 +356,10 @@
         e = JPEG_CONV_JCU_ERR;
     }
 fin:
+    if (mutex_release == true) {
+        jpeg_converter_semaphore.release(); // RELEASE
+    }
+
     return  e;
 } /* End of method decode() */
 
@@ -358,109 +373,9 @@
 ******************************************************************************/
 JPEG_Converter::jpeg_conv_error_t
 JPEG_Converter::encode(bitmap_buff_info_t* psInputBuff, void* pJpegBuff, size_t* pEncodeSize ) {
-
-    jpeg_conv_error_t   e;
-    jcu_errorcode_t     jcu_error;
-    jcu_buffer_param_t  buffer;
-    uint8_t*            TableAddress;
-    jcu_encode_param_t  encode;
-
-    // Check JCU initialized
-    if ( is_JCU_initialized != false ) {
-        // Check input address
-        if ((pJpegBuff == NULL) || (psInputBuff == NULL) || (pEncodeSize == NULL)) {
-            e = JPEG_CONV_PARAM_ERR;  // Input address error
-            goto  fin;
-        }
-        // Select Encode
-        jcu_error = R_JCU_SelectCodec( JCU_ENCODE );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_JCU_ERR;
-            goto fin;
-        }
-
-        // Set tables
-        TableAddress = (uint8_t*)csaDefaultQuantizationTable_Y;
-        jcu_error = R_JCU_SetQuantizationTable( JCU_TABLE_NO_0, (uint8_t*)TableAddress );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_PARAM_RANGE_ERR;
-            goto fin;
-        }
-        TableAddress = (uint8_t*)csaDefaultQuantizationTable_C;
-        jcu_error = R_JCU_SetQuantizationTable( JCU_TABLE_NO_1, (uint8_t*)TableAddress );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_PARAM_RANGE_ERR;
-            goto fin;
-        }
-        TableAddress = (uint8_t*)csaDefaultHuffmanTable_Y_DC;
-        jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_0, JCU_HUFFMAN_DC, (uint8_t*)TableAddress );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_PARAM_RANGE_ERR;
-            goto fin;
-        }
-        TableAddress = (uint8_t*)csaDefaultHuffmanTable_C_DC;
-        jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_1, JCU_HUFFMAN_DC, (uint8_t*)TableAddress );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_PARAM_RANGE_ERR;
-            goto fin;
-        }
-        TableAddress = (uint8_t*)csaDefaultHuffmanTable_Y_AC;
-        jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_0, JCU_HUFFMAN_AC, (uint8_t*)TableAddress );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_PARAM_RANGE_ERR;
-            goto fin;
-        }
-        TableAddress = (uint8_t*)csaDefaultHuffmanTable_C_AC;
-        jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_1, JCU_HUFFMAN_AC, (uint8_t*)TableAddress );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_PARAM_RANGE_ERR;
-            goto fin;
-        }
-
-        // JPEG encode
-        buffer.source.swapSetting  = (jcu_swap_t)WR_RD_WRSWA_8BIT;
-        buffer.source.address           = (uint32_t *)psInputBuff->buffer_address;
-        buffer.destination.swapSetting  = JCU_SWAP_LONG_WORD_AND_WORD_AND_BYTE;
-        buffer.destination.address      = (uint32_t *)pJpegBuff;
-        buffer.lineOffset               = psInputBuff->width;
-        encode.encodeFormat = (jcu_jpeg_format_t)JCU_JPEG_YCbCr422;
-        encode.QuantizationTable[ JCU_ELEMENT_Y  ] = JCU_TABLE_NO_0;
-        encode.QuantizationTable[ JCU_ELEMENT_Cb ] = JCU_TABLE_NO_1;
-        encode.QuantizationTable[ JCU_ELEMENT_Cr ] = JCU_TABLE_NO_1;
-        encode.HuffmanTable[ JCU_ELEMENT_Y  ] = JCU_TABLE_NO_0;
-        encode.HuffmanTable[ JCU_ELEMENT_Cb ] = JCU_TABLE_NO_1;
-        encode.HuffmanTable[ JCU_ELEMENT_Cr ] = JCU_TABLE_NO_1;
-        encode.DRI_value = 0;
-        encode.width  = psInputBuff->width;
-        encode.height = psInputBuff->height;
-        encode.inputCbCrOffset = (jcu_cbcr_offset_t)1;
-        jcu_error = R_JCU_SetEncodeParam( &encode, &buffer );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_PARAM_RANGE_ERR;
-            goto fin;
-        }
-
-        jcu_error = R_JCU_Start();
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_JCU_ERR;
-            goto fin;
-        }
-
-        jcu_error = R_JCU_GetEncodedSize( pEncodeSize );
-        if ( jcu_error != JCU_ERROR_OK ) {
-            e = JPEG_CONV_JCU_ERR;
-            goto fin;
-        }
-
-        e = JPEG_CONV_OK;
-    }
-    else
-    {
-        e = JPEG_CONV_PARAM_RANGE_ERR;
-    }
-fin:
-
-    return  e;
+    encode_options_t Options;
+    
+    return (encode(psInputBuff, pJpegBuff, pEncodeSize, &Options));
 } /* End of method encode() */
 
 
@@ -469,7 +384,7 @@
  * @param[in]   bitmap_buff_info_t*     psInputBuff     : Input bitmap data address
  * @param[out]  void*                   pJpegBuff       : Output JPEG data address
  * @param[out]  size_t*                 pEncodeSize     : Encode size address
- * @param[in]   encode_options_t*       pOptions[IN]    : Encode option(Optional)
+ * @param[in]   encode_options_t*       pOptions        : Encode option(Optional)
  * @retval      error code
 ******************************************************************************/
 JPEG_Converter::jpeg_conv_error_t
@@ -480,21 +395,42 @@
     jcu_buffer_param_t  buffer;
     uint8_t*            TableAddress;
     jcu_encode_param_t  encode;
+    jcu_count_mode_param_t      count_para;
+    int32_t     encode_count;
+    int32_t    size_max_count = 1;
+    size_t      BufferSize = pOptions->encode_buff_size;
+    const jcu_async_status_t*  status;
+    bool                        mutex_release = false;
 
     // Check JCU initialized
-    if ( is_JCU_initialized != false ) {
+    if (driver_ac_count > 0) {
         // Check input address
         if ((pJpegBuff == NULL) || (psInputBuff == NULL) || (pEncodeSize == NULL)) {
             e = JPEG_CONV_PARAM_ERR;  // Input address error
             goto  fin;
         }
-        // Select Encode
-        jcu_error = R_JCU_SelectCodec( JCU_ENCODE );
-        if ( jcu_error != JCU_ERROR_OK ) {
+        // JCU Error reset
+        if (jcu_error_flag == true) {
+            (void)R_JCU_Terminate();
+            (void)R_JCU_Initialize(NULL);
+            jcu_error_flag = false;
+        }
+        // Get mutex
+        if ( pOptions->p_EncodeCallBackFunc == NULL ) {
+            jpeg_converter_semaphore.wait(0xFFFFFFFFuL); // WAIT
+        } else {
+            if (!jpeg_converter_semaphore.wait(0)) {
+                e = JPEG_CONV_BUSY;  // Busy
+                goto  fin;
+            }
+        }
+        // Select encode
+        jcu_error = R_JCU_SelectCodec(JCU_ENCODE);
+        if (jcu_error != JCU_ERROR_OK) {
             e = JPEG_CONV_JCU_ERR;
+            mutex_release = true;
             goto fin;
         }
-
         /* Set tables */
         if ( pOptions->quantization_table_Y != NULL ) {
             TableAddress = (uint8_t*)pOptions->quantization_table_Y;
@@ -504,6 +440,8 @@
         jcu_error = R_JCU_SetQuantizationTable( JCU_TABLE_NO_0, (uint8_t*)TableAddress );
         if ( jcu_error != JCU_ERROR_OK ) {
             e = JPEG_CONV_PARAM_RANGE_ERR;
+            mutex_release = true;
+            jcu_error_flag = true;
             goto fin;
         }
         if ( pOptions->quantization_table_C != NULL ) {
@@ -514,6 +452,8 @@
         jcu_error = R_JCU_SetQuantizationTable( JCU_TABLE_NO_1, (uint8_t*)TableAddress );
         if ( jcu_error != JCU_ERROR_OK ) {
             e = JPEG_CONV_PARAM_RANGE_ERR;
+            mutex_release = true;
+            jcu_error_flag = true;
             goto fin;
         }
         if ( pOptions->huffman_table_Y_DC != NULL ) {
@@ -524,6 +464,8 @@
         jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_0, JCU_HUFFMAN_DC, (uint8_t*)TableAddress );
         if ( jcu_error != JCU_ERROR_OK ) {
             e = JPEG_CONV_PARAM_RANGE_ERR;
+            mutex_release = true;
+            jcu_error_flag = true;
             goto fin;
         }
         if ( pOptions->huffman_table_C_DC != NULL ) {
@@ -534,6 +476,8 @@
         jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_1, JCU_HUFFMAN_DC, (uint8_t*)TableAddress );
         if ( jcu_error != JCU_ERROR_OK ) {
             e = JPEG_CONV_PARAM_RANGE_ERR;
+            mutex_release = true;
+            jcu_error_flag = true;
             goto fin;
         }
         if ( pOptions->huffman_table_Y_AC != NULL ) {
@@ -544,6 +488,8 @@
         jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_0, JCU_HUFFMAN_AC, (uint8_t*)TableAddress );
         if ( jcu_error != JCU_ERROR_OK ) {
             e = JPEG_CONV_PARAM_RANGE_ERR;
+            mutex_release = true;
+            jcu_error_flag = true;
             goto fin;
         }
         if ( pOptions->huffman_table_C_AC != NULL ) {
@@ -554,6 +500,8 @@
         jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_1, JCU_HUFFMAN_AC, (uint8_t*)TableAddress );
         if ( jcu_error != JCU_ERROR_OK ) {
             e = JPEG_CONV_PARAM_RANGE_ERR;
+            mutex_release = true;
+            jcu_error_flag = true;
             goto fin;
         }
 
@@ -585,120 +533,81 @@
         jcu_error = R_JCU_SetEncodeParam( &encode, &buffer );
         if ( jcu_error != JCU_ERROR_OK ) {
             e = JPEG_CONV_PARAM_RANGE_ERR;
+            mutex_release = true;
+            jcu_error_flag = true;
             goto fin;
         }
-
-        if ( pOptions->p_EncodeCallBackFunc == NULL ) {
-            if (pOptions->encode_buff_size == 0) {
-                jcu_error = R_JCU_Start();
-                if ( jcu_error != JCU_ERROR_OK ) {
-                    e = JPEG_CONV_JCU_ERR;
-                    goto fin;
-                }
-            } else {
-                jcu_count_mode_param_t      count_para;
-                int32_t     encode_count;
-                int32_t    size_max_count = 1;
-                size_t      BufferSize = pOptions->encode_buff_size;
-                const jcu_async_status_t*  status;
+        if (pOptions->encode_buff_size > 0) {
 
-                while(BufferSize > ENC_SIZE_MAX) {
-                    size_max_count *= 2;
-                    BufferSize /= 2;
-                }
-                BufferSize = BufferSize & MASK_8BYTE;
-                
-                count_para.inputBuffer.isEnable       = false;
-                count_para.inputBuffer.isInitAddress  = false;
-                count_para.inputBuffer.restartAddress = NULL;
-                count_para.inputBuffer.dataCount      = 0;
-                count_para.outputBuffer.isEnable       = true;
-                count_para.outputBuffer.isInitAddress  = false;
-                count_para.outputBuffer.restartAddress = NULL;
-                count_para.outputBuffer.dataCount      = BufferSize;
-                
-                R_JCU_SetCountMode(&count_para);
-                
-                jcu_error = R_JCU_Start();
-                if ( jcu_error != JCU_ERROR_OK ) {
-                    e = JPEG_CONV_JCU_ERR;
-                    goto fin;
-                }
+            while(BufferSize > ENC_SIZE_MAX) {
+                size_max_count *= 2;
+                BufferSize /= 2;
+            }
+            BufferSize = BufferSize & MASK_8BYTE;
+            
+            count_para.inputBuffer.isEnable       = false;
+            count_para.inputBuffer.isInitAddress  = false;
+            count_para.inputBuffer.restartAddress = NULL;
+            count_para.inputBuffer.dataCount      = 0;
+            count_para.outputBuffer.isEnable       = true;
+            count_para.outputBuffer.isInitAddress  = false;
+            count_para.outputBuffer.restartAddress = NULL;
+            count_para.outputBuffer.dataCount      = BufferSize;
+            
+            R_JCU_SetCountMode(&count_para);
+        } else {
+            size_max_count = 0;
+        }
+        // Check async
+        if ( pOptions->p_EncodeCallBackFunc == NULL ) {
+            jcu_error = R_JCU_Start();
+            mutex_release = true;
+            if ( jcu_error != JCU_ERROR_OK ) {
+                e = JPEG_CONV_JCU_ERR;
+                jcu_error_flag = true;
+                goto fin;
+            }
+            if (pOptions->encode_buff_size > 0) {
                 // Check Pause flag
                 R_JCU_GetAsyncStatus( &status );
                 for ( encode_count = 1; (encode_count < size_max_count) && (status->IsPaused != false); encode_count++) {
-                    if (status->SubStatusFlags & JCU_SUB_DECODE_INPUT_PAUSE == 0) {
+                    if ((status->SubStatusFlags & JCU_SUB_ENCODE_OUTPUT_PAUSE) == 0) {
                         e = JPEG_CONV_JCU_ERR;
+                        jcu_error_flag = true;
                         goto fin;
                     }
                     jcu_error = R_JCU_Continue( JCU_OUTPUT_BUFFER );
                     if (jcu_error != JCU_ERROR_OK) {
                         e = JPEG_CONV_JCU_ERR;
+                        jcu_error_flag = true;
                         goto fin;
                     }
                     R_JCU_GetAsyncStatus( &status );
                 }
                 if (status->IsPaused != false) {
                     e = JPEG_CONV_PARAM_RANGE_ERR;
+                    jcu_error_flag = true;
                     goto fin;
                 }
             }
-            jcu_error = R_JCU_GetEncodedSize( pEncodeSize );
+            (void)R_JCU_GetEncodedSize(pEncodeSize);
+        } else {
+            pJPEG_ConverterCallback = pOptions->p_EncodeCallBackFunc;
+            jcu_error = R_wrpper_set_encode_callback((mbed_CallbackFunc_t*)JPEG_CallbackFunction, pEncodeSize, size_max_count);
             if ( jcu_error != JCU_ERROR_OK ) {
                 e = JPEG_CONV_JCU_ERR;
                 goto fin;
             }
-        } else {
-            async.Flags = R_F_OSPL_InterruptCallback;
-            async.A_Thread = R_OSPL_THREAD_GetCurrentId();
-            if (pOptions->encode_buff_size == 0) {
-                (void)R_wrpper_set_encode_callback((mbed_CallbackFunc_t*)pOptions->p_EncodeCallBackFunc, &async, pEncodeSize, 0);
-                jcu_error = R_JCU_StartAsync( &async );
-                if ( jcu_error != JCU_ERROR_OK ) {
-                    e = JPEG_CONV_JCU_ERR;
-                    goto fin;
-                }
-            } else {
-                jcu_count_mode_param_t      count_para;
-                int32_t                     size_max_count = 1;
-                size_t                      BufferSize = pOptions->encode_buff_size;
-
-                while(BufferSize > ENC_SIZE_MAX) {
-                    size_max_count *= 2;
-                    BufferSize /= 2;
-                }
-                BufferSize = BufferSize & MASK_8BYTE;
-                
-                (void)R_wrpper_set_encode_callback((mbed_CallbackFunc_t*)pOptions->p_EncodeCallBackFunc, &async, pEncodeSize, size_max_count);
-                
-                count_para.inputBuffer.isEnable       = false;
-                count_para.inputBuffer.isInitAddress  = false;
-                count_para.inputBuffer.restartAddress = NULL;
-                count_para.inputBuffer.dataCount      = 0;
-                count_para.outputBuffer.isEnable       = true;
-                count_para.outputBuffer.isInitAddress  = false;
-                count_para.outputBuffer.restartAddress = NULL;
-                count_para.outputBuffer.dataCount      = BufferSize;
-                
-                R_JCU_SetCountMode(&count_para);
-                
-                jcu_error = R_JCU_StartAsync( &async );
-                if ( jcu_error != JCU_ERROR_OK ) {
-                    e = JPEG_CONV_JCU_ERR;
-                    goto fin;
-                }
-            }
         }
-
         e = JPEG_CONV_OK;
-    }
-    else
-    {
+    } else {
         e = JPEG_CONV_PARAM_RANGE_ERR;
     }
 fin:
+    if (mutex_release == true) {
+        jpeg_converter_semaphore.release(); // RELEASE
+    }
 
     return  e;
 } /* End of method encode() */
 
-