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

License

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

本プログラムを使用する際は、以下の内容に合意した事になります。

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

Information

Japanese version is available in lower part of this page.

このページの後半に日本語版が用意されています.

What is this ?

JPEG driver sample program using GR-PEACH or GR-LYCHEE.
When you decode/encode file, input file set width is 320 pixel and height is 240 pixel.

FormatJPEG file".jpg"
Color formatYCbCr422
Pixel size320x240
FormatBitmap file".bin"
Color formatYCbCr422
Pixel size320x240

Composition

GR-PEACH or GR-LYCHEE, PC and USB media.

  • Use USB0 of GR-PEACH:

/media/uploads/tyama/jpeg_conv_usb0.png

If you use the USB0 as USB Host, please close GR-PEACH's JP3.

/media/uploads/tyama/gr-peach-c_usb2.png

Please select USB0 connector by the following configuration.

mbed_app.json

{
    "config": {
        "usb-host-ch":{
            "help": "(for GR-PEACH) 0:ch0 1:ch1",
            "value": "0"
        },
        "audio-camera-shield":{
            "help": "(for GR-PEACH) 0:not use 1:use",
            "value": "0"
        }
   }
}


  • Use USB1 of GR-PEACH:

/media/uploads/tyama/jpeg_conv_usb1.png

If you use the USB1 as USB Host, please close Audio/Camera Shield's JP1.

/media/uploads/tyama/audiocamerashield_jp1.jpg

Please select Audio/Camera Shield and USB1 connector by the following configuration.

mbed_app.json

{
    "config": {
        "usb-host-ch":{
            "help": "(for GR-PEACH) 0:ch0 1:ch1",
            "value": "1"
        },
        "audio-camera-shield":{
            "help": "(for GR-PEACH) 0:not use 1:use",
            "value": "1"
        }
    }
}



The size of the changed picture is made 320x240pixel fixing by a sample program. When using a file with the picture size besides that, please change the below.

JPEG_converter\main.cpp

#define SAMPLE_WIDTH        (320)
#define SAMPLE_HEIGHT       (240)

How to use

  1. Please turn on the power to GR-PEACH or GR-LYCHEE and start terminal software.
  2. Please connect a USB media and press the reset button on the board.
  3. Input request of the file name is output on terminal.
  4. When a file of extension ".jpg" is designated, a JPEG file is decoded. A decoded file will be data of 320x240pixel and YCbCr422.
  5. When a file of extension ".bin" is designated, it's encoded to a JPEG file. An encoded file will be a JPEG file of 320x240pixel and YCbCr422.

The default setting of serial communication (baud rate etc.) in mbed is shown the following link.
Please refer to the link and change the settings of your PC terminal software.
The default value of baud rate in mbed is 9600, and this application uses baud rate 9600.
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication


概要

GR-PEACH or GR-LYCHEEを使ったJPEG driverサンプルプログラムです。
デコード/エンコードする入力ファイルは、幅を320pixel、高さを240pixelに設定してください。

FormatJPEG file".jpg"
Color formatYCbCr422
Pixel size320x240
FormatBitmap file".bin"
Color formatYCbCr422
Pixel size320x240

構成

GR-PEACH or GR-LYCHEE、PC、USBメディア

  • GR-PEACHでUSB0を使用する場合:

/media/uploads/tyama/jpeg_conv_usb0.png

USBホストとしてUSB0を使用する場合は、GR-PEACHのJP3を閉じてください。

/media/uploads/tyama/gr-peach-c_usb2.png

以下の設定でUSB0コネクタを選択して下さい。

mbed_app.json

{
    "config": {
        "usb-host-ch":{
            "help": "(for GR-PEACH) 0:ch0 1:ch1",
            "value": "0"
        },
        "audio-camera-shield":{
            "help": "(for GR-PEACH) 0:not use 1:use",
            "value": "0"
        }
}


  • GR-PEACHでUSB1を使用する場合:

/media/uploads/tyama/jpeg_conv_usb1.png

USBホストとしてUSB1を使用する場合は、Audio/Camera ShieldのJP1を閉じてください。

/media/uploads/tyama/audiocamerashield_jp1.jpg

以下の設定でAudio/Camera ShieldとUSB1コネクタを選択して下さい。

mbed_app.json

{
    "config": {
        "usb-host-ch":{
            "help": "(for GR-PEACH) 0:ch0 1:ch1",
            "value": "1"
        },
        "audio-camera-shield":{
            "help": "(for GR-PEACH) 0:not use 1:use",
            "value": "1"
        }
    }
}



サンプルプログラムでは、変換する画像のサイズを320x240pixel固定としております。 それ以外の画像サイズのファイルを使用する場合は以下を変更してください。

JPEG_converter\main.cpp

#define SAMPLE_WIDTH        (320)
#define SAMPLE_HEIGHT       (240)

使い方

  1. GR-PEACH or GR-LYCHEEに電源を入れ、Terminalソフトを立ち上げます。
  2. USBメディアを接続し、ボード上にあるリセットします。
  3. Terminal上にファイル名の入力要求が出力されます。
  4. 拡張子".jpg"のファイルを指定するとJPEGファイルのデコードを実行します。デコードしたファイルは、320x240pixel、YCbCr422のデータとなります。
  5. 拡張子".bin"のファイルを指定するとJPEGファイルへのエンコードを実行します。エンコードしたファイルは、320x240pixel、YCbCr422のJPEGファイルとなります。

mbedのシリアル通信(ボーレート等)のデフォルト設定は以下のリンクに示しています。
リンクを参考に、お使いのPCターミナルソフトの設定を変更して下さい。
mbedでのボーレートのデフォルト値は9600で、このサンプルではボーレート9600を使います。
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication

main.cpp

Committer:
dkato
Date:
2016-04-18
Revision:
3:19e5a993ec7d
Parent:
0:94bcab936677
Child:
4:6e390b05b558

File content as of revision 3:19e5a993ec7d:

#include "mbed.h"
#include "JPEG_Converter.h"
#include "USBHostMSD.h"
#if defined(TARGET_RZ_A1H)
#include "usb_host_setting.h"
#else
#define USB_HOST_CH     0
#endif

#define SAMPLE_WIDTH        (320)
#define SAMPLE_HEIGHT       (240)
#define SAMPLE_BUFF_SIZE    (SAMPLE_WIDTH * SAMPLE_HEIGHT * 4)

#define MAX_FILE_NAME_LENGTH    (64)
#define FOLD_COUNT              (5)

#if (USB_HOST_CH == 1) //Audio Camera Shield USB1
DigitalOut usb1en(P3_8);
#endif
DigitalOut led1(LED1);
Serial pc(USBTX, USBRX);

static uint8_t JCUBuffer_INPUT[SAMPLE_BUFF_SIZE]__attribute((section("NC_BSS"),aligned(8)));  //8 bytes aligned!;
static uint8_t JCUBuffer_OUTPUT[SAMPLE_BUFF_SIZE]__attribute((section("NC_BSS"),aligned(8)));  //8 bytes aligned!;

bool get_file_name(char * p_buf, int size) {
    bool ret;
    char ch;
    int i = 0;

    while (1) {
        ch = (char)pc.getc();
        if (ch == '\r'){
            p_buf[i] = '\0';
            pc.putc('\n');
            ret = true;
            break;
        } else if (ch == '\n') {
            // Do Nothing
        } else if (ch == '\b') {
            if (i > 0) {
                pc.puts("\b \b");
                i--;
                p_buf[i] = '\0';
            }
        } else if (i < size) {
            p_buf[i] = ch;
            i++;
            pc.putc(ch);
        } else {
            ret = false;
            break;
        }
    }

    return ret;
}

int main() {
    char file_name[MAX_FILE_NAME_LENGTH + FOLD_COUNT];
    JPEG_Converter  decoder;
    JPEG_Converter::bitmap_buff_info_t  aBitmapData;
    FILE * rd_fp = NULL;
    FILE * wr_fp = NULL;
    int filesize;
    size_t EncodeSize;

    file_name[0] = '/';
    file_name[1] = 'u';
    file_name[2] = 's';
    file_name[3] = 'b';
    file_name[4] = '/';

#if (USB_HOST_CH == 1) //Audio Shield USB1
    //Audio Shield USB1 enable
    usb1en = 1;        //Outputs high level
    Thread::wait(5);
    usb1en = 0;        //Outputs low level
#endif
    USBHostMSD msd("usb");

    //try to connect a MSD device
    while(!msd.connect()) {
        Thread::wait(500);
    }

    pc.printf("%dpix x %dpix\n", SAMPLE_WIDTH, SAMPLE_HEIGHT);

    while (1) {
        pc.printf("\nInput file name (.jpg or .bin)>");
        if (get_file_name(&file_name[FOLD_COUNT], MAX_FILE_NAME_LENGTH) == false) {
            pc.printf("Error:Max file name length is %d\n", MAX_FILE_NAME_LENGTH);
        } else {
            size_t len = strlen(file_name);
            if ((len > 4) && (file_name[len - 4] == (char)'.')
                && ((file_name[len - 3] | 0x20u) == (char)'j')
                && ((file_name[len - 2] | 0x20u) == (char)'p')
                && ((file_name[len - 1] | 0x20u) == (char)'g')) {
                // input ".jpg"
                // decode JPG file to bitmap file
                rd_fp = fopen(file_name, "r");
                if (rd_fp == NULL) {
                    pc.printf("Error:File is not exist \n");
                } else {
                    fseek(rd_fp, 0, SEEK_END);
                    filesize = ftell(rd_fp);
                    if (filesize > SAMPLE_BUFF_SIZE) {
                        pc.printf("Error:File size over \n");
                    } else {
                        led1 = 1;
                        fseek(rd_fp, 0, SEEK_SET);
                        fread(&JCUBuffer_INPUT[0], sizeof(char), filesize, rd_fp);
                        pc.printf("[Decode Mode]\n");
                        pc.printf("\nOutput file name (.bin)>");
                        if (get_file_name(&file_name[FOLD_COUNT], MAX_FILE_NAME_LENGTH) == false) {
                            pc.printf("Error:Max file name length is %d\n", MAX_FILE_NAME_LENGTH);
                        } else {
                            //YCbCr setting
                            aBitmapData.width           = SAMPLE_WIDTH;
                            aBitmapData.height          = SAMPLE_HEIGHT;
                            aBitmapData.format          = JPEG_Converter::WR_RD_YCbCr422;   //YCbCr[0] & ARGB8888[1] is 4byte, not RGB565[2] is 2byte
                            aBitmapData.buffer_address  = (void *)JCUBuffer_OUTPUT;
                            pc.printf("File decode start\n");
                            // JPEG_Converter
                            if (decoder.decode((void *)JCUBuffer_INPUT, &aBitmapData) == JPEG_Converter::JPEG_CONV_OK) {
                                pc.printf("File decode done %dbyte\n", (SAMPLE_WIDTH * SAMPLE_HEIGHT * 4));
                                pc.printf("File write start\n");
                                wr_fp = fopen(file_name, "w");
                                fwrite(JCUBuffer_OUTPUT, sizeof(char), (SAMPLE_WIDTH * SAMPLE_HEIGHT * 4), wr_fp);

                                fclose(wr_fp);
                                pc.printf("File write done\n");
                                led1 = 0;
                            } else {
                                pc.printf("Error:JCU decode error\n");
                                led1 = 0;
                            }
                        }
                    }
                    fclose(rd_fp);
                }
            } else if ((file_name[len - 4] == (char)'.')
                && ((file_name[len - 3] | 0x20u) == (char)'b')
                && ((file_name[len - 2] | 0x20u) == (char)'i')
                && ((file_name[len - 1] | 0x20u) == (char)'n')) {
                // input ".bin"
                // encode bitmap file to JPEG file
                rd_fp = fopen(file_name, "r");
                if (rd_fp == NULL) {
                    pc.printf("Error:File is not exist\n");
                } else {
                    fseek(rd_fp, 0, SEEK_END);
                    filesize = ftell(rd_fp);
                    if (filesize > SAMPLE_BUFF_SIZE) {
                        fclose(wr_fp);
                        pc.printf("Error:File size over\n");
                    } else {
                        led1 = 1;
                        fseek(rd_fp, 0, SEEK_SET);
                        fread(&JCUBuffer_INPUT[0], sizeof(char), filesize, rd_fp);
                        pc.printf("[Encode Mode]\n");
                        pc.printf("\nOutput file name (.jpg)>");
                        if (get_file_name(&file_name[FOLD_COUNT], MAX_FILE_NAME_LENGTH) == false) {
                            pc.printf("Error:Max file name length is %d\n", MAX_FILE_NAME_LENGTH);
                            fclose(rd_fp);
                        } else {
                            //YCbCr setting
                            aBitmapData.width           = SAMPLE_WIDTH;
                            aBitmapData.height          = SAMPLE_HEIGHT;
                            aBitmapData.format          = JPEG_Converter::WR_RD_YCbCr422;   //YCbCr[0] & ARGB8888[1] is 4byte, not RGB565[2] is 2byte
                            aBitmapData.buffer_address  = (void *)JCUBuffer_INPUT;
                            pc.printf("File encode start\n");
                            // JPEG_Converter
                            if (decoder.encode(&aBitmapData, JCUBuffer_OUTPUT, &EncodeSize) == JPEG_Converter::JPEG_CONV_OK) {
                                pc.printf("File encode done %dbyte\n", EncodeSize);
                                pc.printf("File write start\n");
                                wr_fp = fopen(file_name, "w");
                                fwrite(JCUBuffer_OUTPUT, sizeof(char), EncodeSize, wr_fp);

                                fclose(wr_fp);
                                pc.printf("File write done\n");
                                led1 = 0;
                            } else {
                                pc.printf("Error:JCU encode error\n");
                                led1 = 0;
                            }
                        }
                    }
                    fclose(rd_fp);
                }
            } else {
                pc.printf("Error:Not supported extension (.jpg or .bin)\n");
            }
        }
    }
}