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

Committer:
dkato
Date:
Mon Apr 24 08:16:23 2017 +0000
Revision:
13:1ee2176ef13f
Parent:
7:df2bc72f7fb7
Add "SetQuality()" to JCU.
; Bug fixes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:37e1e6a45ced 1 /*******************************************************************************
dkato 0:37e1e6a45ced 2 * DISCLAIMER
dkato 0:37e1e6a45ced 3 * This software is supplied by Renesas Electronics Corporation and is only
dkato 0:37e1e6a45ced 4 * intended for use with Renesas products. No other uses are authorized. This
dkato 0:37e1e6a45ced 5 * software is owned by Renesas Electronics Corporation and is protected under
dkato 0:37e1e6a45ced 6 * all applicable laws, including copyright laws.
dkato 0:37e1e6a45ced 7 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
dkato 0:37e1e6a45ced 8 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
dkato 0:37e1e6a45ced 9 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
dkato 0:37e1e6a45ced 10 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
dkato 0:37e1e6a45ced 11 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
dkato 0:37e1e6a45ced 12 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
dkato 0:37e1e6a45ced 13 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
dkato 0:37e1e6a45ced 14 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
dkato 0:37e1e6a45ced 15 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
dkato 0:37e1e6a45ced 16 * Renesas reserves the right, without notice, to make changes to this software
dkato 0:37e1e6a45ced 17 * and to discontinue the availability of this software. By using this software,
dkato 0:37e1e6a45ced 18 * you agree to the additional terms and conditions found by accessing the
dkato 0:37e1e6a45ced 19 * following link:
dkato 0:37e1e6a45ced 20 * http://www.renesas.com/disclaimer*
dkato 0:37e1e6a45ced 21 * Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.
dkato 0:37e1e6a45ced 22 *******************************************************************************/
dkato 0:37e1e6a45ced 23
dkato 0:37e1e6a45ced 24 /**************************************************************************//**
dkato 0:37e1e6a45ced 25 * @file jpeg_coverter.cpp
dkato 0:37e1e6a45ced 26 * @version 1.00
dkato 0:37e1e6a45ced 27 * $Rev: 1 $
dkato 0:37e1e6a45ced 28 * $Date:: 2015-08-06 16:33:52 +0900#$
dkato 0:37e1e6a45ced 29 * @brief Decodes JPEG data and encodes to JPEG data
dkato 0:37e1e6a45ced 30 ******************************************************************************/
dkato 0:37e1e6a45ced 31
dkato 0:37e1e6a45ced 32 /******************************************************************************
dkato 0:37e1e6a45ced 33 Includes <System Includes> , "Project Includes"
dkato 0:37e1e6a45ced 34 ******************************************************************************/
dkato 0:37e1e6a45ced 35 #include <string.h>
dkato 0:37e1e6a45ced 36 #include <stdio.h>
dkato 7:df2bc72f7fb7 37 #include "rtos.h"
dkato 0:37e1e6a45ced 38 #include "r_typedefs.h"
dkato 0:37e1e6a45ced 39 #include "r_jcu_api.h"
dkato 0:37e1e6a45ced 40 #include "JPEG_Converter.h"
dkato 0:37e1e6a45ced 41 #include "converter_wrapper.h"
dkato 0:37e1e6a45ced 42
dkato 0:37e1e6a45ced 43 /******************************************************************************
dkato 0:37e1e6a45ced 44 Typedef definitions
dkato 0:37e1e6a45ced 45 ******************************************************************************/
dkato 0:37e1e6a45ced 46
dkato 0:37e1e6a45ced 47 /******************************************************************************
dkato 0:37e1e6a45ced 48 Macro definitions
dkato 0:37e1e6a45ced 49 ******************************************************************************/
dkato 0:37e1e6a45ced 50 #define QUANTIZATION_TABLE_SIZE (64u)
dkato 0:37e1e6a45ced 51 #define HUFFMAN_TABLE_DC_SIZE (28u)
dkato 0:37e1e6a45ced 52 #define HUFFMAN_TABLE_AC_SIZE (178u)
dkato 0:37e1e6a45ced 53 #define JPEG_HEADER_LETTER_1 (0xFFu)
dkato 0:37e1e6a45ced 54 #define JPEG_HEADER_LETTER_2 (0xD8u)
dkato 0:37e1e6a45ced 55 #define ALPHA_VAL_MAX (0xFF)
dkato 0:37e1e6a45ced 56 #define LOC_KIND_COLOR_FORMAT (3u)
dkato 13:1ee2176ef13f 57 #define QUANTIZATION_TABLE_NUM (2)
dkato 0:37e1e6a45ced 58
dkato 0:37e1e6a45ced 59 #define ENC_SIZE_MAX (1024 * 30)
dkato 0:37e1e6a45ced 60 #define MASK_8BYTE (0xFFFFFFF8)
dkato 0:37e1e6a45ced 61
dkato 0:37e1e6a45ced 62 /*[HuffmanTable_Y_DC]*/
dkato 0:37e1e6a45ced 63 /* Example written in ITU-T T81 specification */
dkato 0:37e1e6a45ced 64 static const uint8_t csaDefaultHuffmanTable_Y_DC[HUFFMAN_TABLE_DC_SIZE] = {
dkato 0:37e1e6a45ced 65 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
dkato 0:37e1e6a45ced 66 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
dkato 0:37e1e6a45ced 67 };
dkato 0:37e1e6a45ced 68
dkato 0:37e1e6a45ced 69 /*[HuffmanTable_C_DC]*/
dkato 0:37e1e6a45ced 70 /* Example written in ITU-T T81 specification */
dkato 0:37e1e6a45ced 71 static const uint8_t csaDefaultHuffmanTable_C_DC[HUFFMAN_TABLE_DC_SIZE] = {
dkato 0:37e1e6a45ced 72 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
dkato 0:37e1e6a45ced 73 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
dkato 0:37e1e6a45ced 74 };
dkato 0:37e1e6a45ced 75
dkato 0:37e1e6a45ced 76 /*[HuffmanTable_Y_AC]*/
dkato 0:37e1e6a45ced 77 /* Example written in ITU-T T81 specification */
dkato 0:37e1e6a45ced 78 static const uint8_t csaDefaultHuffmanTable_Y_AC[HUFFMAN_TABLE_AC_SIZE] = {
dkato 0:37e1e6a45ced 79 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D,
dkato 0:37e1e6a45ced 80 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
dkato 0:37e1e6a45ced 81 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0,
dkato 0:37e1e6a45ced 82 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
dkato 0:37e1e6a45ced 83 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
dkato 0:37e1e6a45ced 84 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
dkato 0:37e1e6a45ced 85 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
dkato 0:37e1e6a45ced 86 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
dkato 0:37e1e6a45ced 87 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5,
dkato 0:37e1e6a45ced 88 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
dkato 0:37e1e6a45ced 89 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
dkato 0:37e1e6a45ced 90 0xF9, 0xFA
dkato 0:37e1e6a45ced 91 };
dkato 0:37e1e6a45ced 92
dkato 0:37e1e6a45ced 93 /*[HuffmanTable_C_AC]*/
dkato 0:37e1e6a45ced 94 /* Example written in ITU-T T81 specification */
dkato 0:37e1e6a45ced 95 static const uint8_t csaDefaultHuffmanTable_C_AC[HUFFMAN_TABLE_AC_SIZE] = {
dkato 0:37e1e6a45ced 96 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
dkato 0:37e1e6a45ced 97 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
dkato 0:37e1e6a45ced 98 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0,
dkato 0:37e1e6a45ced 99 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
dkato 0:37e1e6a45ced 100 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
dkato 0:37e1e6a45ced 101 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
dkato 0:37e1e6a45ced 102 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
dkato 0:37e1e6a45ced 103 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
dkato 0:37e1e6a45ced 104 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
dkato 0:37e1e6a45ced 105 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
dkato 0:37e1e6a45ced 106 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
dkato 0:37e1e6a45ced 107 0xF9, 0xFA
dkato 0:37e1e6a45ced 108 };
dkato 0:37e1e6a45ced 109
dkato 1:7b90e31adc5b 110 typedef void (JPEG_CallbackFunc_t)(JPEG_Converter::jpeg_conv_error_t err_code);
dkato 1:7b90e31adc5b 111
dkato 1:7b90e31adc5b 112 static uint32_t driver_ac_count = 0;
dkato 1:7b90e31adc5b 113 static bool jcu_error_flag;
dkato 1:7b90e31adc5b 114 static JPEG_CallbackFunc_t* pJPEG_ConverterCallback;
dkato 1:7b90e31adc5b 115 Semaphore jpeg_converter_semaphore(1);
dkato 13:1ee2176ef13f 116 #if defined(__ICCARM__)
dkato 13:1ee2176ef13f 117 #pragma data_alignment=32
dkato 13:1ee2176ef13f 118 static uint8_t QuantizationTable_Y[QUANTIZATION_TABLE_SIZE]@ ".mirrorram";
dkato 13:1ee2176ef13f 119 #pragma data_alignment=32
dkato 13:1ee2176ef13f 120 static uint8_t QuantizationTable_C[QUANTIZATION_TABLE_SIZE]@ ".mirrorram";
dkato 13:1ee2176ef13f 121 #else
dkato 13:1ee2176ef13f 122 static uint8_t QuantizationTable_Y[QUANTIZATION_TABLE_SIZE]__attribute((section("NC_BSS"),aligned(32)));
dkato 13:1ee2176ef13f 123 static uint8_t QuantizationTable_C[QUANTIZATION_TABLE_SIZE]__attribute((section("NC_BSS"),aligned(32)));
dkato 13:1ee2176ef13f 124 #endif
dkato 13:1ee2176ef13f 125
dkato 13:1ee2176ef13f 126 /**************************************************************************//**
dkato 13:1ee2176ef13f 127 * @brief Set encode quality
dkato 13:1ee2176ef13f 128 * @param[in] uint8_t qual : Encode quality (1 <= qual <= 100)
dkato 13:1ee2176ef13f 129 * @retval error code
dkato 13:1ee2176ef13f 130 ******************************************************************************/
dkato 13:1ee2176ef13f 131 JPEG_Converter::jpeg_conv_error_t
dkato 13:1ee2176ef13f 132 JPEG_Converter::SetQuality(const uint8_t qual) {
dkato 13:1ee2176ef13f 133 uint8_t* pqs[QUANTIZATION_TABLE_NUM];
dkato 13:1ee2176ef13f 134 const uint8_t* pqb[QUANTIZATION_TABLE_NUM];
dkato 13:1ee2176ef13f 135 uint8_t* ptqs;
dkato 13:1ee2176ef13f 136 const uint8_t* ptqb;
dkato 13:1ee2176ef13f 137 int_t temp;
dkato 13:1ee2176ef13f 138 int_t i;
dkato 13:1ee2176ef13f 139 int_t j;
dkato 13:1ee2176ef13f 140
dkato 13:1ee2176ef13f 141 /* ITU-T Recommendation T.81 "K.1 Quantization tables for luminance and chrominance components" */
dkato 13:1ee2176ef13f 142 /* Table K.1 - Luminance quantization table */
dkato 13:1ee2176ef13f 143 const uint8_t quantization_table_y_50[QUANTIZATION_TABLE_SIZE] = {
dkato 13:1ee2176ef13f 144 16, 11, 10, 16, 24, 40, 51, 61,
dkato 13:1ee2176ef13f 145 12, 12, 14, 19, 26, 58, 60, 55,
dkato 13:1ee2176ef13f 146 14, 13, 16, 24, 40, 57, 69, 56,
dkato 13:1ee2176ef13f 147 14, 17, 22, 29, 51, 87, 80, 62,
dkato 13:1ee2176ef13f 148 18, 22, 37, 56, 68, 109, 103, 77,
dkato 13:1ee2176ef13f 149 24, 35, 55, 64, 81, 104, 113, 92,
dkato 13:1ee2176ef13f 150 49, 64, 78, 87, 103, 121, 120, 101,
dkato 13:1ee2176ef13f 151 72, 92, 95, 98, 112, 100, 103, 99
dkato 13:1ee2176ef13f 152 };
dkato 13:1ee2176ef13f 153
dkato 13:1ee2176ef13f 154 /* Table K.2 - Chrominance quantization table */
dkato 13:1ee2176ef13f 155 const uint8_t quantization_table_c_50[QUANTIZATION_TABLE_SIZE] = {
dkato 13:1ee2176ef13f 156 17, 18, 24, 47, 99, 99, 99, 99,
dkato 13:1ee2176ef13f 157 18, 21, 26, 66, 99, 99, 99, 99,
dkato 13:1ee2176ef13f 158 24, 26, 56, 99, 99, 99, 99, 99,
dkato 13:1ee2176ef13f 159 47, 66, 99, 99, 99, 99, 99, 99,
dkato 13:1ee2176ef13f 160 99, 99, 99, 99, 99, 99, 99, 99,
dkato 13:1ee2176ef13f 161 99, 99, 99, 99, 99, 99, 99, 99,
dkato 13:1ee2176ef13f 162 99, 99, 99, 99, 99, 99, 99, 99,
dkato 13:1ee2176ef13f 163 99, 99, 99, 99, 99, 99, 99, 99
dkato 13:1ee2176ef13f 164 };
dkato 13:1ee2176ef13f 165
dkato 13:1ee2176ef13f 166 if (((int_t)qual < 1) || ((int_t)qual > 100)) {
dkato 13:1ee2176ef13f 167 return JPEG_CONV_PARAM_RANGE_ERR;
dkato 13:1ee2176ef13f 168 }
dkato 13:1ee2176ef13f 169
dkato 13:1ee2176ef13f 170 pqs[0] = QuantizationTable_Y;
dkato 13:1ee2176ef13f 171 pqb[0] = quantization_table_y_50;
dkato 13:1ee2176ef13f 172 pqs[1] = QuantizationTable_C;
dkato 13:1ee2176ef13f 173 pqb[1] = quantization_table_c_50;
dkato 13:1ee2176ef13f 174
dkato 13:1ee2176ef13f 175 for (j = 0; j < QUANTIZATION_TABLE_NUM; j++) {
dkato 13:1ee2176ef13f 176 ptqs = pqs[j];
dkato 13:1ee2176ef13f 177 ptqb = pqb[j];
dkato 13:1ee2176ef13f 178 if ((int_t)qual < 50) {
dkato 13:1ee2176ef13f 179 for (i = 0; i < QUANTIZATION_TABLE_SIZE; i++) {
dkato 13:1ee2176ef13f 180 temp = (((int_t)ptqb[i] * 100) + (int_t)qual) / (2 * (int_t)qual);
dkato 13:1ee2176ef13f 181 if (temp == 0) {
dkato 13:1ee2176ef13f 182 temp = 1;
dkato 13:1ee2176ef13f 183 }
dkato 13:1ee2176ef13f 184 if (temp > 255) {
dkato 13:1ee2176ef13f 185 temp = 255;
dkato 13:1ee2176ef13f 186 }
dkato 13:1ee2176ef13f 187 ptqs[i] = (uint8_t)temp;
dkato 13:1ee2176ef13f 188 }
dkato 13:1ee2176ef13f 189 } else {
dkato 13:1ee2176ef13f 190 for (i = 0; i < QUANTIZATION_TABLE_SIZE; i++) {
dkato 13:1ee2176ef13f 191 temp = (((200 - (2 * (int_t)qual)) * (int_t)ptqb[i]) + 50) / 100;
dkato 13:1ee2176ef13f 192 if (temp == 0) {
dkato 13:1ee2176ef13f 193 temp = 1;
dkato 13:1ee2176ef13f 194 }
dkato 13:1ee2176ef13f 195 if (temp > 255) {
dkato 13:1ee2176ef13f 196 temp = 255;
dkato 13:1ee2176ef13f 197 }
dkato 13:1ee2176ef13f 198 ptqs[i] = (uint8_t)temp;
dkato 13:1ee2176ef13f 199 }
dkato 13:1ee2176ef13f 200 }
dkato 13:1ee2176ef13f 201 }
dkato 13:1ee2176ef13f 202
dkato 13:1ee2176ef13f 203 return JPEG_CONV_OK;
dkato 13:1ee2176ef13f 204 }
dkato 1:7b90e31adc5b 205
dkato 1:7b90e31adc5b 206
dkato 1:7b90e31adc5b 207 /**************************************************************************//**
dkato 1:7b90e31adc5b 208 * @brief Callback function from JCU async mode
dkato 1:7b90e31adc5b 209 * @param[in] mbed_jcu_err_t err_code : JCU result
dkato 1:7b90e31adc5b 210 * @retval None
dkato 1:7b90e31adc5b 211 ******************************************************************************/
dkato 1:7b90e31adc5b 212 void JPEG_CallbackFunction(mbed_jcu_err_t err_code) {
dkato 1:7b90e31adc5b 213 if (pJPEG_ConverterCallback != NULL) {
dkato 1:7b90e31adc5b 214 pJPEG_ConverterCallback((JPEG_Converter::jpeg_conv_error_t)err_code);
dkato 1:7b90e31adc5b 215 }
dkato 1:7b90e31adc5b 216 if (err_code != MBED_JCU_E_OK) {
dkato 1:7b90e31adc5b 217 jcu_error_flag = true;
dkato 1:7b90e31adc5b 218 }
dkato 1:7b90e31adc5b 219 jpeg_converter_semaphore.release(); // RELEASE
dkato 1:7b90e31adc5b 220 } /* End of callback function method () */
dkato 0:37e1e6a45ced 221
dkato 0:37e1e6a45ced 222 /**************************************************************************//**
dkato 0:37e1e6a45ced 223 * @brief Constructor of the JPEG_Converter class
dkato 0:37e1e6a45ced 224 * @param[in] None
dkato 0:37e1e6a45ced 225 * @retval None
dkato 0:37e1e6a45ced 226 ******************************************************************************/
dkato 1:7b90e31adc5b 227 JPEG_Converter::JPEG_Converter(void) {
dkato 0:37e1e6a45ced 228 jcu_errorcode_t jcu_error;
dkato 0:37e1e6a45ced 229
dkato 1:7b90e31adc5b 230 if (driver_ac_count == 0) {
dkato 13:1ee2176ef13f 231 SetQuality(75);
dkato 1:7b90e31adc5b 232 jcu_error = R_JCU_Initialize(NULL);
dkato 1:7b90e31adc5b 233 if (jcu_error == JCU_ERROR_OK) {
dkato 1:7b90e31adc5b 234 driver_ac_count++;
dkato 1:7b90e31adc5b 235 jcu_error_flag = false;
dkato 1:7b90e31adc5b 236 }
dkato 1:7b90e31adc5b 237 } else {
dkato 1:7b90e31adc5b 238 driver_ac_count++;
dkato 0:37e1e6a45ced 239 }
dkato 0:37e1e6a45ced 240 } /* End of constructor method () */
dkato 0:37e1e6a45ced 241
dkato 0:37e1e6a45ced 242 /**************************************************************************//**
dkato 0:37e1e6a45ced 243 * @brief Destructor of the JPEG_Converter class
dkato 0:37e1e6a45ced 244 * @param[in] None
dkato 0:37e1e6a45ced 245 * @retval None
dkato 0:37e1e6a45ced 246 ******************************************************************************/
dkato 1:7b90e31adc5b 247 JPEG_Converter::~JPEG_Converter(void) {
dkato 1:7b90e31adc5b 248 if (driver_ac_count > 0) {
dkato 1:7b90e31adc5b 249 driver_ac_count--;
dkato 1:7b90e31adc5b 250 if (driver_ac_count == 0) {
dkato 1:7b90e31adc5b 251 (void)R_JCU_Terminate();
dkato 1:7b90e31adc5b 252 }
dkato 0:37e1e6a45ced 253 }
dkato 0:37e1e6a45ced 254 } /* End of destructor method () */
dkato 0:37e1e6a45ced 255
dkato 0:37e1e6a45ced 256 /**************************************************************************//**
dkato 0:37e1e6a45ced 257 * @brief JPEG data decode to bitmap
dkato 0:37e1e6a45ced 258 * @param[in] void* pJpegBuff : Input JPEG data address
dkato 0:37e1e6a45ced 259 * @param[in/out] bitmap_buff_info_t* psOutputBuff : Output bitmap data address
dkato 0:37e1e6a45ced 260 * @retval error code
dkato 0:37e1e6a45ced 261 ******************************************************************************/
dkato 0:37e1e6a45ced 262 JPEG_Converter::jpeg_conv_error_t
dkato 1:7b90e31adc5b 263 JPEG_Converter::decode(void* pJpegBuff, bitmap_buff_info_t* psOutputBuff) {
dkato 1:7b90e31adc5b 264 decode_options_t Options;
dkato 0:37e1e6a45ced 265
dkato 1:7b90e31adc5b 266 return (decode(pJpegBuff, psOutputBuff, &Options));
dkato 0:37e1e6a45ced 267 } /* End of method decode() */
dkato 0:37e1e6a45ced 268
dkato 0:37e1e6a45ced 269 /**************************************************************************//**
dkato 0:37e1e6a45ced 270 * @brief JPEG data decode to bitmap
dkato 0:37e1e6a45ced 271 * @param[in] void* pJpegBuff : Input JPEG data address
dkato 0:37e1e6a45ced 272 * @param[in/out] bitmap_buff_info_t* psOutputBuff : Output bitmap data address
dkato 0:37e1e6a45ced 273 * @param[in] decode_options_t* pOptions : Decode option(Optional)
dkato 0:37e1e6a45ced 274 * @retval error code
dkato 0:37e1e6a45ced 275 ******************************************************************************/
dkato 0:37e1e6a45ced 276 JPEG_Converter::jpeg_conv_error_t
dkato 1:7b90e31adc5b 277 JPEG_Converter::decode(void* pJpegBuff, bitmap_buff_info_t* psOutputBuff, decode_options_t* pOptions) {
dkato 0:37e1e6a45ced 278 jpeg_conv_error_t e;
dkato 0:37e1e6a45ced 279 jcu_errorcode_t jcu_error;
dkato 0:37e1e6a45ced 280 jcu_decode_param_t decode;
dkato 0:37e1e6a45ced 281 jcu_buffer_param_t buffer;
dkato 1:7b90e31adc5b 282 uint8_t* pBuff = (uint8_t *)pJpegBuff;
dkato 0:37e1e6a45ced 283 const jcu_async_status_t* status;
dkato 0:37e1e6a45ced 284 jcu_image_info_t image_info;
dkato 1:7b90e31adc5b 285 bool mutex_release = false;
dkato 0:37e1e6a45ced 286
dkato 0:37e1e6a45ced 287 // Check JCU initialized
dkato 1:7b90e31adc5b 288 if (driver_ac_count > 0) {
dkato 1:7b90e31adc5b 289 size_t calc_height;
dkato 1:7b90e31adc5b 290 size_t calc_width;
dkato 0:37e1e6a45ced 291
dkato 0:37e1e6a45ced 292 calc_height = psOutputBuff->height * (2 ^ pOptions->vertical_sub_sampling);
dkato 0:37e1e6a45ced 293 calc_width = psOutputBuff->width * (2 ^ pOptions->horizontal_sub_sampling);
dkato 0:37e1e6a45ced 294
dkato 0:37e1e6a45ced 295 // Check input address
dkato 0:37e1e6a45ced 296 if ((pJpegBuff == NULL) || (psOutputBuff == NULL) || (pOptions == NULL)) {
dkato 0:37e1e6a45ced 297 e = JPEG_CONV_PARAM_ERR; // Input address error
dkato 0:37e1e6a45ced 298 goto fin;
dkato 0:37e1e6a45ced 299 }
dkato 0:37e1e6a45ced 300 // Check JPEG header
dkato 1:7b90e31adc5b 301 if (((uint32_t)(pBuff[0]) != JPEG_HEADER_LETTER_1) ||
dkato 1:7b90e31adc5b 302 ((uint32_t)(pBuff[1]) != JPEG_HEADER_LETTER_2)) {
dkato 0:37e1e6a45ced 303 e = JPEG_CONV_FORMA_ERR; // JPEG data is not in ROM
dkato 0:37e1e6a45ced 304 goto fin;
dkato 0:37e1e6a45ced 305 }
dkato 1:7b90e31adc5b 306 // JCU Error reset
dkato 1:7b90e31adc5b 307 if (jcu_error_flag == true) {
dkato 1:7b90e31adc5b 308 (void)R_JCU_Terminate();
dkato 1:7b90e31adc5b 309 (void)R_JCU_Initialize(NULL);
dkato 1:7b90e31adc5b 310 jcu_error_flag = false;
dkato 1:7b90e31adc5b 311 }
dkato 1:7b90e31adc5b 312 // Get mutex
dkato 1:7b90e31adc5b 313 if (pOptions->p_DecodeCallBackFunc == NULL) {
dkato 1:7b90e31adc5b 314 jpeg_converter_semaphore.wait(0xFFFFFFFFuL); // WAIT
dkato 1:7b90e31adc5b 315 } else {
dkato 1:7b90e31adc5b 316 if (!jpeg_converter_semaphore.wait(0)) {
dkato 1:7b90e31adc5b 317 e = JPEG_CONV_BUSY; // Busy
dkato 1:7b90e31adc5b 318 goto fin;
dkato 1:7b90e31adc5b 319 }
dkato 1:7b90e31adc5b 320 }
dkato 1:7b90e31adc5b 321 // Select decode
dkato 0:37e1e6a45ced 322 jcu_error = R_JCU_SelectCodec( JCU_DECODE );
dkato 1:7b90e31adc5b 323 if (jcu_error != JCU_ERROR_OK) {
dkato 1:7b90e31adc5b 324 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 325 mutex_release = true;
dkato 0:37e1e6a45ced 326 goto fin;
dkato 0:37e1e6a45ced 327 }
dkato 0:37e1e6a45ced 328
dkato 0:37e1e6a45ced 329 buffer.source.swapSetting = JCU_SWAP_LONG_WORD_AND_WORD_AND_BYTE;
dkato 0:37e1e6a45ced 330 buffer.source.address = (uint32_t *)pBuff;
dkato 0:37e1e6a45ced 331 buffer.lineOffset = (int16_t)psOutputBuff->width;
dkato 0:37e1e6a45ced 332 buffer.destination.address = (uint32_t *)psOutputBuff->buffer_address;
dkato 0:37e1e6a45ced 333 decode.decodeFormat = (jcu_decode_format_t)psOutputBuff->format;
dkato 0:37e1e6a45ced 334 buffer.destination.swapSetting = (jcu_swap_t)pOptions->output_swapsetting;
dkato 0:37e1e6a45ced 335 decode.outputCbCrOffset = (jcu_cbcr_offset_t)pOptions->output_cb_cr_offset;
dkato 0:37e1e6a45ced 336 decode.alpha = pOptions->alpha;
dkato 0:37e1e6a45ced 337 decode.horizontalSubSampling = (jcu_sub_sampling_t)pOptions->horizontal_sub_sampling;
dkato 0:37e1e6a45ced 338 decode.verticalSubSampling = (jcu_sub_sampling_t)pOptions->vertical_sub_sampling;
dkato 1:7b90e31adc5b 339
dkato 1:7b90e31adc5b 340 jcu_error = R_JCU_SetDecodeParam(&decode, &buffer);
dkato 1:7b90e31adc5b 341 if (jcu_error != JCU_ERROR_OK) {
dkato 0:37e1e6a45ced 342 e = JPEG_CONV_FORMA_ERR;
dkato 1:7b90e31adc5b 343 mutex_release = true;
dkato 1:7b90e31adc5b 344 jcu_error_flag = false;
dkato 0:37e1e6a45ced 345 goto fin;
dkato 0:37e1e6a45ced 346 }
dkato 0:37e1e6a45ced 347
dkato 0:37e1e6a45ced 348 if (pOptions->check_jpeg_format != false) {
dkato 1:7b90e31adc5b 349 jcu_error = R_JCU_SetPauseForImageInfo(true);
dkato 1:7b90e31adc5b 350 if (jcu_error != JCU_ERROR_OK) {
dkato 0:37e1e6a45ced 351 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 352 mutex_release = true;
dkato 1:7b90e31adc5b 353 jcu_error_flag = false;
dkato 0:37e1e6a45ced 354 goto fin;
dkato 0:37e1e6a45ced 355 }
dkato 0:37e1e6a45ced 356 }
dkato 1:7b90e31adc5b 357 if (pOptions->p_DecodeCallBackFunc == NULL) {
dkato 0:37e1e6a45ced 358 jcu_error = R_JCU_Start();
dkato 1:7b90e31adc5b 359 mutex_release = true;
dkato 1:7b90e31adc5b 360 if (jcu_error != JCU_ERROR_OK) {
dkato 0:37e1e6a45ced 361 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 362 jcu_error_flag = false;
dkato 0:37e1e6a45ced 363 goto fin;
dkato 0:37e1e6a45ced 364 }
dkato 1:7b90e31adc5b 365 if (pOptions->check_jpeg_format != false) {
dkato 1:7b90e31adc5b 366 R_JCU_GetAsyncStatus( &status );
dkato 1:7b90e31adc5b 367 if (status -> IsPaused == false) {
dkato 1:7b90e31adc5b 368 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 369 jcu_error_flag = false;
dkato 1:7b90e31adc5b 370 goto fin;
dkato 1:7b90e31adc5b 371 }
dkato 1:7b90e31adc5b 372 if ((status->SubStatusFlags & JCU_SUB_INFOMATION_READY) == 0) {
dkato 1:7b90e31adc5b 373 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 374 jcu_error_flag = false;
dkato 1:7b90e31adc5b 375 goto fin;
dkato 1:7b90e31adc5b 376 }
dkato 1:7b90e31adc5b 377 jcu_error = R_JCU_GetImageInfo( &image_info );
dkato 1:7b90e31adc5b 378 if (jcu_error != JCU_ERROR_OK) {
dkato 1:7b90e31adc5b 379 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 380 jcu_error_flag = false;
dkato 1:7b90e31adc5b 381 goto fin;
dkato 1:7b90e31adc5b 382 }
dkato 1:7b90e31adc5b 383 if ((image_info.width == 0u) || (image_info.height == 0u) ||
dkato 1:7b90e31adc5b 384 (image_info.width > calc_width) ||
dkato 1:7b90e31adc5b 385 (image_info.height > calc_height)) {
dkato 1:7b90e31adc5b 386 e = JPEG_CONV_FORMA_ERR;
dkato 1:7b90e31adc5b 387 jcu_error_flag = false;
dkato 1:7b90e31adc5b 388 goto fin;
dkato 1:7b90e31adc5b 389 }
dkato 1:7b90e31adc5b 390 if ((image_info.encodedFormat != JCU_JPEG_YCbCr444) &&
dkato 1:7b90e31adc5b 391 (image_info.encodedFormat != JCU_JPEG_YCbCr422) &&
dkato 1:7b90e31adc5b 392 (image_info.encodedFormat != JCU_JPEG_YCbCr420) &&
dkato 1:7b90e31adc5b 393 (image_info.encodedFormat != JCU_JPEG_YCbCr411)) {
dkato 1:7b90e31adc5b 394 e = JPEG_CONV_FORMA_ERR;
dkato 1:7b90e31adc5b 395 jcu_error_flag = false;
dkato 1:7b90e31adc5b 396 goto fin;
dkato 1:7b90e31adc5b 397 }
dkato 1:7b90e31adc5b 398 jcu_error = R_JCU_Continue(JCU_IMAGE_INFO);
dkato 1:7b90e31adc5b 399 if (jcu_error != JCU_ERROR_OK) {
dkato 1:7b90e31adc5b 400 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 401 jcu_error_flag = false;
dkato 1:7b90e31adc5b 402 goto fin;
dkato 1:7b90e31adc5b 403 }
dkato 0:37e1e6a45ced 404 }
dkato 0:37e1e6a45ced 405 } else {
dkato 1:7b90e31adc5b 406 pJPEG_ConverterCallback = pOptions->p_DecodeCallBackFunc;
dkato 1:7b90e31adc5b 407 jcu_error = R_wrpper_set_decode_callback((mbed_CallbackFunc_t*)JPEG_CallbackFunction, (size_t)calc_width, calc_height);
dkato 1:7b90e31adc5b 408 if (jcu_error != JCU_ERROR_OK) {
dkato 1:7b90e31adc5b 409 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 410 mutex_release = true;
dkato 0:37e1e6a45ced 411 goto fin;
dkato 0:37e1e6a45ced 412 }
dkato 0:37e1e6a45ced 413 }
dkato 0:37e1e6a45ced 414 e = JPEG_CONV_OK;
dkato 0:37e1e6a45ced 415 }
dkato 0:37e1e6a45ced 416 else
dkato 0:37e1e6a45ced 417 {
dkato 0:37e1e6a45ced 418 e = JPEG_CONV_JCU_ERR;
dkato 0:37e1e6a45ced 419 }
dkato 0:37e1e6a45ced 420 fin:
dkato 1:7b90e31adc5b 421 if (mutex_release == true) {
dkato 1:7b90e31adc5b 422 jpeg_converter_semaphore.release(); // RELEASE
dkato 1:7b90e31adc5b 423 }
dkato 1:7b90e31adc5b 424
dkato 0:37e1e6a45ced 425 return e;
dkato 0:37e1e6a45ced 426 } /* End of method decode() */
dkato 0:37e1e6a45ced 427
dkato 0:37e1e6a45ced 428
dkato 0:37e1e6a45ced 429 /**************************************************************************//**
dkato 0:37e1e6a45ced 430 * @brief Bitmap data encode to JPEG
dkato 0:37e1e6a45ced 431 * @param[in] bitmap_buff_info_t* psInputBuff : Input bitmap data address
dkato 0:37e1e6a45ced 432 * @param[out] void* pJpegBuff : Output JPEG data address
dkato 0:37e1e6a45ced 433 * @param[out] size_t* pEncodeSize : Encode size address
dkato 0:37e1e6a45ced 434 * @retval error code
dkato 0:37e1e6a45ced 435 ******************************************************************************/
dkato 0:37e1e6a45ced 436 JPEG_Converter::jpeg_conv_error_t
dkato 0:37e1e6a45ced 437 JPEG_Converter::encode(bitmap_buff_info_t* psInputBuff, void* pJpegBuff, size_t* pEncodeSize ) {
dkato 1:7b90e31adc5b 438 encode_options_t Options;
dkato 1:7b90e31adc5b 439
dkato 1:7b90e31adc5b 440 return (encode(psInputBuff, pJpegBuff, pEncodeSize, &Options));
dkato 0:37e1e6a45ced 441 } /* End of method encode() */
dkato 0:37e1e6a45ced 442
dkato 0:37e1e6a45ced 443
dkato 0:37e1e6a45ced 444 /**************************************************************************//**
dkato 0:37e1e6a45ced 445 * @brief Bitmap data encode to JPEG
dkato 0:37e1e6a45ced 446 * @param[in] bitmap_buff_info_t* psInputBuff : Input bitmap data address
dkato 0:37e1e6a45ced 447 * @param[out] void* pJpegBuff : Output JPEG data address
dkato 0:37e1e6a45ced 448 * @param[out] size_t* pEncodeSize : Encode size address
dkato 1:7b90e31adc5b 449 * @param[in] encode_options_t* pOptions : Encode option(Optional)
dkato 0:37e1e6a45ced 450 * @retval error code
dkato 0:37e1e6a45ced 451 ******************************************************************************/
dkato 0:37e1e6a45ced 452 JPEG_Converter::jpeg_conv_error_t
dkato 0:37e1e6a45ced 453 JPEG_Converter::encode(bitmap_buff_info_t* psInputBuff, void* pJpegBuff, size_t* pEncodeSize, encode_options_t* pOptions ) {
dkato 0:37e1e6a45ced 454
dkato 0:37e1e6a45ced 455 jpeg_conv_error_t e;
dkato 0:37e1e6a45ced 456 jcu_errorcode_t jcu_error;
dkato 0:37e1e6a45ced 457 jcu_buffer_param_t buffer;
dkato 0:37e1e6a45ced 458 uint8_t* TableAddress;
dkato 0:37e1e6a45ced 459 jcu_encode_param_t encode;
dkato 1:7b90e31adc5b 460 jcu_count_mode_param_t count_para;
dkato 1:7b90e31adc5b 461 int32_t encode_count;
dkato 1:7b90e31adc5b 462 int32_t size_max_count = 1;
dkato 1:7b90e31adc5b 463 size_t BufferSize = pOptions->encode_buff_size;
dkato 1:7b90e31adc5b 464 const jcu_async_status_t* status;
dkato 1:7b90e31adc5b 465 bool mutex_release = false;
dkato 0:37e1e6a45ced 466
dkato 0:37e1e6a45ced 467 // Check JCU initialized
dkato 1:7b90e31adc5b 468 if (driver_ac_count > 0) {
dkato 0:37e1e6a45ced 469 // Check input address
dkato 0:37e1e6a45ced 470 if ((pJpegBuff == NULL) || (psInputBuff == NULL) || (pEncodeSize == NULL)) {
dkato 0:37e1e6a45ced 471 e = JPEG_CONV_PARAM_ERR; // Input address error
dkato 0:37e1e6a45ced 472 goto fin;
dkato 0:37e1e6a45ced 473 }
dkato 1:7b90e31adc5b 474 // JCU Error reset
dkato 1:7b90e31adc5b 475 if (jcu_error_flag == true) {
dkato 1:7b90e31adc5b 476 (void)R_JCU_Terminate();
dkato 1:7b90e31adc5b 477 (void)R_JCU_Initialize(NULL);
dkato 1:7b90e31adc5b 478 jcu_error_flag = false;
dkato 1:7b90e31adc5b 479 }
dkato 1:7b90e31adc5b 480 // Get mutex
dkato 1:7b90e31adc5b 481 if ( pOptions->p_EncodeCallBackFunc == NULL ) {
dkato 1:7b90e31adc5b 482 jpeg_converter_semaphore.wait(0xFFFFFFFFuL); // WAIT
dkato 1:7b90e31adc5b 483 } else {
dkato 1:7b90e31adc5b 484 if (!jpeg_converter_semaphore.wait(0)) {
dkato 1:7b90e31adc5b 485 e = JPEG_CONV_BUSY; // Busy
dkato 1:7b90e31adc5b 486 goto fin;
dkato 1:7b90e31adc5b 487 }
dkato 1:7b90e31adc5b 488 }
dkato 1:7b90e31adc5b 489 // Select encode
dkato 1:7b90e31adc5b 490 jcu_error = R_JCU_SelectCodec(JCU_ENCODE);
dkato 1:7b90e31adc5b 491 if (jcu_error != JCU_ERROR_OK) {
dkato 0:37e1e6a45ced 492 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 493 mutex_release = true;
dkato 0:37e1e6a45ced 494 goto fin;
dkato 0:37e1e6a45ced 495 }
dkato 0:37e1e6a45ced 496 /* Set tables */
dkato 0:37e1e6a45ced 497 if ( pOptions->quantization_table_Y != NULL ) {
dkato 0:37e1e6a45ced 498 TableAddress = (uint8_t*)pOptions->quantization_table_Y;
dkato 0:37e1e6a45ced 499 } else {
dkato 13:1ee2176ef13f 500 TableAddress = (uint8_t*)QuantizationTable_Y;
dkato 0:37e1e6a45ced 501 }
dkato 0:37e1e6a45ced 502 jcu_error = R_JCU_SetQuantizationTable( JCU_TABLE_NO_0, (uint8_t*)TableAddress );
dkato 0:37e1e6a45ced 503 if ( jcu_error != JCU_ERROR_OK ) {
dkato 0:37e1e6a45ced 504 e = JPEG_CONV_PARAM_RANGE_ERR;
dkato 1:7b90e31adc5b 505 mutex_release = true;
dkato 1:7b90e31adc5b 506 jcu_error_flag = true;
dkato 0:37e1e6a45ced 507 goto fin;
dkato 0:37e1e6a45ced 508 }
dkato 0:37e1e6a45ced 509 if ( pOptions->quantization_table_C != NULL ) {
dkato 0:37e1e6a45ced 510 TableAddress = (uint8_t*)pOptions->quantization_table_C;
dkato 0:37e1e6a45ced 511 } else {
dkato 13:1ee2176ef13f 512 TableAddress = (uint8_t*)QuantizationTable_C;
dkato 0:37e1e6a45ced 513 }
dkato 0:37e1e6a45ced 514 jcu_error = R_JCU_SetQuantizationTable( JCU_TABLE_NO_1, (uint8_t*)TableAddress );
dkato 0:37e1e6a45ced 515 if ( jcu_error != JCU_ERROR_OK ) {
dkato 0:37e1e6a45ced 516 e = JPEG_CONV_PARAM_RANGE_ERR;
dkato 1:7b90e31adc5b 517 mutex_release = true;
dkato 1:7b90e31adc5b 518 jcu_error_flag = true;
dkato 0:37e1e6a45ced 519 goto fin;
dkato 0:37e1e6a45ced 520 }
dkato 0:37e1e6a45ced 521 if ( pOptions->huffman_table_Y_DC != NULL ) {
dkato 0:37e1e6a45ced 522 TableAddress = (uint8_t*)pOptions->huffman_table_Y_DC;
dkato 0:37e1e6a45ced 523 } else {
dkato 0:37e1e6a45ced 524 TableAddress = (uint8_t*)csaDefaultHuffmanTable_Y_DC;
dkato 0:37e1e6a45ced 525 }
dkato 0:37e1e6a45ced 526 jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_0, JCU_HUFFMAN_DC, (uint8_t*)TableAddress );
dkato 0:37e1e6a45ced 527 if ( jcu_error != JCU_ERROR_OK ) {
dkato 0:37e1e6a45ced 528 e = JPEG_CONV_PARAM_RANGE_ERR;
dkato 1:7b90e31adc5b 529 mutex_release = true;
dkato 1:7b90e31adc5b 530 jcu_error_flag = true;
dkato 0:37e1e6a45ced 531 goto fin;
dkato 0:37e1e6a45ced 532 }
dkato 0:37e1e6a45ced 533 if ( pOptions->huffman_table_C_DC != NULL ) {
dkato 0:37e1e6a45ced 534 TableAddress = (uint8_t*)pOptions->huffman_table_C_DC;
dkato 0:37e1e6a45ced 535 } else {
dkato 0:37e1e6a45ced 536 TableAddress = (uint8_t*)csaDefaultHuffmanTable_C_DC;
dkato 0:37e1e6a45ced 537 }
dkato 0:37e1e6a45ced 538 jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_1, JCU_HUFFMAN_DC, (uint8_t*)TableAddress );
dkato 0:37e1e6a45ced 539 if ( jcu_error != JCU_ERROR_OK ) {
dkato 0:37e1e6a45ced 540 e = JPEG_CONV_PARAM_RANGE_ERR;
dkato 1:7b90e31adc5b 541 mutex_release = true;
dkato 1:7b90e31adc5b 542 jcu_error_flag = true;
dkato 0:37e1e6a45ced 543 goto fin;
dkato 0:37e1e6a45ced 544 }
dkato 0:37e1e6a45ced 545 if ( pOptions->huffman_table_Y_AC != NULL ) {
dkato 0:37e1e6a45ced 546 TableAddress = (uint8_t*)pOptions->huffman_table_Y_AC;
dkato 0:37e1e6a45ced 547 } else {
dkato 0:37e1e6a45ced 548 TableAddress = (uint8_t*)csaDefaultHuffmanTable_Y_AC;
dkato 0:37e1e6a45ced 549 }
dkato 0:37e1e6a45ced 550 jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_0, JCU_HUFFMAN_AC, (uint8_t*)TableAddress );
dkato 0:37e1e6a45ced 551 if ( jcu_error != JCU_ERROR_OK ) {
dkato 0:37e1e6a45ced 552 e = JPEG_CONV_PARAM_RANGE_ERR;
dkato 1:7b90e31adc5b 553 mutex_release = true;
dkato 1:7b90e31adc5b 554 jcu_error_flag = true;
dkato 0:37e1e6a45ced 555 goto fin;
dkato 0:37e1e6a45ced 556 }
dkato 0:37e1e6a45ced 557 if ( pOptions->huffman_table_C_AC != NULL ) {
dkato 0:37e1e6a45ced 558 TableAddress = (uint8_t*)pOptions->huffman_table_C_AC;
dkato 0:37e1e6a45ced 559 } else {
dkato 0:37e1e6a45ced 560 TableAddress = (uint8_t*)csaDefaultHuffmanTable_C_AC;
dkato 0:37e1e6a45ced 561 }
dkato 0:37e1e6a45ced 562 jcu_error = R_JCU_SetHuffmanTable( JCU_TABLE_NO_1, JCU_HUFFMAN_AC, (uint8_t*)TableAddress );
dkato 0:37e1e6a45ced 563 if ( jcu_error != JCU_ERROR_OK ) {
dkato 0:37e1e6a45ced 564 e = JPEG_CONV_PARAM_RANGE_ERR;
dkato 1:7b90e31adc5b 565 mutex_release = true;
dkato 1:7b90e31adc5b 566 jcu_error_flag = true;
dkato 0:37e1e6a45ced 567 goto fin;
dkato 0:37e1e6a45ced 568 }
dkato 0:37e1e6a45ced 569
dkato 0:37e1e6a45ced 570 // JPEG encode
dkato 0:37e1e6a45ced 571 buffer.source.swapSetting = (jcu_swap_t)pOptions->input_swapsetting;
dkato 0:37e1e6a45ced 572 buffer.source.address = (uint32_t *)psInputBuff->buffer_address;
dkato 0:37e1e6a45ced 573 buffer.destination.swapSetting = JCU_SWAP_LONG_WORD_AND_WORD_AND_BYTE;
dkato 0:37e1e6a45ced 574 buffer.destination.address = (uint32_t *)pJpegBuff;
dkato 0:37e1e6a45ced 575 buffer.lineOffset = psInputBuff->width;
dkato 0:37e1e6a45ced 576 encode.encodeFormat = (jcu_jpeg_format_t)JCU_JPEG_YCbCr422;
dkato 0:37e1e6a45ced 577 encode.QuantizationTable[ JCU_ELEMENT_Y ] = JCU_TABLE_NO_0;
dkato 0:37e1e6a45ced 578 encode.QuantizationTable[ JCU_ELEMENT_Cb ] = JCU_TABLE_NO_1;
dkato 0:37e1e6a45ced 579 encode.QuantizationTable[ JCU_ELEMENT_Cr ] = JCU_TABLE_NO_1;
dkato 0:37e1e6a45ced 580 encode.HuffmanTable[ JCU_ELEMENT_Y ] = JCU_TABLE_NO_0;
dkato 0:37e1e6a45ced 581 encode.HuffmanTable[ JCU_ELEMENT_Cb ] = JCU_TABLE_NO_1;
dkato 0:37e1e6a45ced 582 encode.HuffmanTable[ JCU_ELEMENT_Cr ] = JCU_TABLE_NO_1;
dkato 0:37e1e6a45ced 583 encode.DRI_value = pOptions->DRI_value;
dkato 0:37e1e6a45ced 584 if ( pOptions->width != 0 ) {
dkato 0:37e1e6a45ced 585 encode.width = pOptions->width;
dkato 0:37e1e6a45ced 586 } else {
dkato 0:37e1e6a45ced 587 encode.width = psInputBuff->width;
dkato 0:37e1e6a45ced 588 }
dkato 0:37e1e6a45ced 589 if ( pOptions->height != 0 ) {
dkato 0:37e1e6a45ced 590 encode.height = pOptions->height;
dkato 0:37e1e6a45ced 591 } else {
dkato 0:37e1e6a45ced 592 encode.height = psInputBuff->height;
dkato 0:37e1e6a45ced 593 }
dkato 0:37e1e6a45ced 594 encode.inputCbCrOffset = (jcu_cbcr_offset_t)pOptions->input_cb_cr_offset;
dkato 0:37e1e6a45ced 595 jcu_error = R_JCU_SetEncodeParam( &encode, &buffer );
dkato 0:37e1e6a45ced 596 if ( jcu_error != JCU_ERROR_OK ) {
dkato 0:37e1e6a45ced 597 e = JPEG_CONV_PARAM_RANGE_ERR;
dkato 1:7b90e31adc5b 598 mutex_release = true;
dkato 1:7b90e31adc5b 599 jcu_error_flag = true;
dkato 0:37e1e6a45ced 600 goto fin;
dkato 0:37e1e6a45ced 601 }
dkato 1:7b90e31adc5b 602 if (pOptions->encode_buff_size > 0) {
dkato 0:37e1e6a45ced 603
dkato 1:7b90e31adc5b 604 while(BufferSize > ENC_SIZE_MAX) {
dkato 1:7b90e31adc5b 605 size_max_count *= 2;
dkato 1:7b90e31adc5b 606 BufferSize /= 2;
dkato 1:7b90e31adc5b 607 }
dkato 1:7b90e31adc5b 608 BufferSize = BufferSize & MASK_8BYTE;
dkato 1:7b90e31adc5b 609
dkato 1:7b90e31adc5b 610 count_para.inputBuffer.isEnable = false;
dkato 1:7b90e31adc5b 611 count_para.inputBuffer.isInitAddress = false;
dkato 1:7b90e31adc5b 612 count_para.inputBuffer.restartAddress = NULL;
dkato 1:7b90e31adc5b 613 count_para.inputBuffer.dataCount = 0;
dkato 1:7b90e31adc5b 614 count_para.outputBuffer.isEnable = true;
dkato 1:7b90e31adc5b 615 count_para.outputBuffer.isInitAddress = false;
dkato 1:7b90e31adc5b 616 count_para.outputBuffer.restartAddress = NULL;
dkato 1:7b90e31adc5b 617 count_para.outputBuffer.dataCount = BufferSize;
dkato 1:7b90e31adc5b 618
dkato 1:7b90e31adc5b 619 R_JCU_SetCountMode(&count_para);
dkato 1:7b90e31adc5b 620 } else {
dkato 1:7b90e31adc5b 621 size_max_count = 0;
dkato 1:7b90e31adc5b 622 }
dkato 1:7b90e31adc5b 623 // Check async
dkato 1:7b90e31adc5b 624 if ( pOptions->p_EncodeCallBackFunc == NULL ) {
dkato 1:7b90e31adc5b 625 jcu_error = R_JCU_Start();
dkato 1:7b90e31adc5b 626 mutex_release = true;
dkato 1:7b90e31adc5b 627 if ( jcu_error != JCU_ERROR_OK ) {
dkato 1:7b90e31adc5b 628 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 629 jcu_error_flag = true;
dkato 1:7b90e31adc5b 630 goto fin;
dkato 1:7b90e31adc5b 631 }
dkato 1:7b90e31adc5b 632 if (pOptions->encode_buff_size > 0) {
dkato 0:37e1e6a45ced 633 // Check Pause flag
dkato 0:37e1e6a45ced 634 R_JCU_GetAsyncStatus( &status );
dkato 0:37e1e6a45ced 635 for ( encode_count = 1; (encode_count < size_max_count) && (status->IsPaused != false); encode_count++) {
dkato 1:7b90e31adc5b 636 if ((status->SubStatusFlags & JCU_SUB_ENCODE_OUTPUT_PAUSE) == 0) {
dkato 0:37e1e6a45ced 637 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 638 jcu_error_flag = true;
dkato 0:37e1e6a45ced 639 goto fin;
dkato 0:37e1e6a45ced 640 }
dkato 0:37e1e6a45ced 641 jcu_error = R_JCU_Continue( JCU_OUTPUT_BUFFER );
dkato 0:37e1e6a45ced 642 if (jcu_error != JCU_ERROR_OK) {
dkato 0:37e1e6a45ced 643 e = JPEG_CONV_JCU_ERR;
dkato 1:7b90e31adc5b 644 jcu_error_flag = true;
dkato 0:37e1e6a45ced 645 goto fin;
dkato 0:37e1e6a45ced 646 }
dkato 0:37e1e6a45ced 647 R_JCU_GetAsyncStatus( &status );
dkato 0:37e1e6a45ced 648 }
dkato 0:37e1e6a45ced 649 if (status->IsPaused != false) {
dkato 0:37e1e6a45ced 650 e = JPEG_CONV_PARAM_RANGE_ERR;
dkato 1:7b90e31adc5b 651 jcu_error_flag = true;
dkato 0:37e1e6a45ced 652 goto fin;
dkato 0:37e1e6a45ced 653 }
dkato 0:37e1e6a45ced 654 }
dkato 1:7b90e31adc5b 655 (void)R_JCU_GetEncodedSize(pEncodeSize);
dkato 1:7b90e31adc5b 656 } else {
dkato 1:7b90e31adc5b 657 pJPEG_ConverterCallback = pOptions->p_EncodeCallBackFunc;
dkato 1:7b90e31adc5b 658 jcu_error = R_wrpper_set_encode_callback((mbed_CallbackFunc_t*)JPEG_CallbackFunction, pEncodeSize, size_max_count);
dkato 0:37e1e6a45ced 659 if ( jcu_error != JCU_ERROR_OK ) {
dkato 0:37e1e6a45ced 660 e = JPEG_CONV_JCU_ERR;
dkato 0:37e1e6a45ced 661 goto fin;
dkato 0:37e1e6a45ced 662 }
dkato 0:37e1e6a45ced 663 }
dkato 0:37e1e6a45ced 664 e = JPEG_CONV_OK;
dkato 1:7b90e31adc5b 665 } else {
dkato 0:37e1e6a45ced 666 e = JPEG_CONV_PARAM_RANGE_ERR;
dkato 0:37e1e6a45ced 667 }
dkato 0:37e1e6a45ced 668 fin:
dkato 1:7b90e31adc5b 669 if (mutex_release == true) {
dkato 1:7b90e31adc5b 670 jpeg_converter_semaphore.release(); // RELEASE
dkato 1:7b90e31adc5b 671 }
dkato 0:37e1e6a45ced 672
dkato 0:37e1e6a45ced 673 return e;
dkato 0:37e1e6a45ced 674 } /* End of method encode() */
dkato 0:37e1e6a45ced 675