/*******************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only
* intended for use with Renesas products. No other uses are authorized. This
* software is owned by Renesas Electronics Corporation and is protected under
* all applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software
* and to discontinue the availability of this software. By using this software,
* you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer *
* Copyright (C) 2014 Renesas Electronics Corporation
* and Renesas Solutions Corp. All rights reserved.
*******************************************************************************/
/*******************************************************************************
* File Name    : r_jpeg_enc_apl.c
* Version      : 1.00
* Device(s)    : Renesas
* Tool-Chain   : Renesas
* OS           : None
* H/W Platform : Independent
* Description  : This is the jpeg encoder application program code.
******************************************************************************/
/*****************************************************************************
* History : DD.MM.YYYY Version Description
*         : 01.09.2014 1.00    First Release
******************************************************************************/

/******************************************************************************
Includes <System Includes> , "Project Includes"
******************************************************************************/
#include "platform.h"

#include "r_compress_jpege.h"
#define JPEG_CMP_INITIAL
#include "r_jpeg_enc_apl.h"

#include "ov7740.h"
#include "r_pdc_apl.h"

#pragma section _JPEG_OUTPUT_BUFFER
uint8_t buf_jpeg[JPEG_OUTPUT_AREA_SIZE];
#pragma section
#pragma section _IMAGE_BUFFER
uint8_t buf_image[JPEG_OUTPUT_AREA_SIZE];
#pragma section

/******************************************************************************
Macro definitions
******************************************************************************/

/******************************************************************************
Private global variables and functions
******************************************************************************/
#ifdef JPEG_AREA_MULTI
#define AREA_NUM   2
const static uint8_t *jpeg_output_area[AREA_NUM] = { buf_jpeg, buf_image };
#endif // JPEG_AREA_MULTI

static int32_t jpeg_output_size     = 0; // jpeg size afrer compress processing
static uint8_t jpeg_output_area_num = 0; // jpeg output area [0/1]

/******************************************************************************
Exported global variables and functions (to be accessed by other files)
******************************************************************************/

/******************************************************************************
Private global variables and functions
******************************************************************************/
static struct r_jpeg_encode_t cmp_jpeg_info;

/******************************************************************************
* Function Name : R_jpeg_compress_init
* Description   : initialize setting.
* Argument      : none
* Return Value  : none
******************************************************************************/
void R_jpeg_compress_init( void )
{
    /* JPEG Setting */
    cmp_jpeg_info.width             = IMG_SENSOR_OUTPUT_WIDTH;    /* WIDTH(PDC input)*/
    cmp_jpeg_info.height            = IMG_SENSOR_OUTPUT_HEIGHT;   /* HEIGHT(PDC input) */
    cmp_jpeg_info.quality           = JPEG_QUALITY;               /* 1-128(128:high quality) */
    cmp_jpeg_info.restart_interval  = 0;                          /* temp */

    /* input */
    cmp_jpeg_info.input_addr        = R_get_pdc_capture_buf_adr();    /* input address(original picture) */
    cmp_jpeg_info.input_line_byte   = IMG_SENSOR_OUTPUT_WIDTH * 2;    /* line number(input size(WIDTH * 16bit(4:2:2))) */
    cmp_jpeg_info.input_format      = JPEGE_IN_YCC422;                /* input format */

#ifdef JPEG_AREA_MULTI
    cmp_jpeg_info.outbuff           = R_get_jpeg_output_adr(R_get_jpeg_output_area_num()); /* output buffer address(internal RAM) */
#else
    cmp_jpeg_info.outbuff           = (uint8_t *)buf_jpeg;                                 /* output buffer address(internal RAM) */
#endif // JPEG_AREA_MULTI
    cmp_jpeg_info.outbuff_size      = JPEG_OUTPUT_AREA_SIZE;                                /* output buffer size(limite size) */
    cmp_jpeg_info.output_format     = JPEG_OUTPUT_FMT;                                      /* output format                   */
}

/******************************************************************************
* Function Name : R_jpeg_compress
* Description   : compression execute.
* Argument      : none
* Return Value  : none
******************************************************************************/
int32_t R_exec_jpeg_compress( struct r_jpeg_encode_t *jpeg_info )
{
    int32_t ret;

    ret = R_compress_jpeg( jpeg_info );    // compress exec.

    return ret;
}

/******************************************************************************
* Function Name : R_get_jpeg_compress_param
* Description   : get parameter
* Argument      : none
* Return Value  : none
******************************************************************************/
struct r_jpeg_encode_t *R_get_jpeg_compress_info( void )
{
#ifdef JPEG_AREA_MULTI
    cmp_jpeg_info.outbuff    = R_get_jpeg_output_adr(R_get_jpeg_output_area_num()); /* output buffer address(internal RAM) */
#else
    // fixed "cmp_jpeg_info.outbuff".
#endif // JPEG_AREA_MULTI

    return (struct r_jpeg_encode_t *)&cmp_jpeg_info;
}

#ifdef JPEG_AREA_MULTI
/******************************************************************************
* Function Name : R_get_jpeg_output_adr
* Description   : get buffer address for JPEG output area.
* Argument      : none
* Return Value  : none
******************************************************************************/
uint8_t *R_get_jpeg_output_adr( uint8_t area_num )
{
    uint8_t num = ( (area_num >= AREA_NUM) ? 0 : area_num );

    return (uint8_t *)jpeg_output_area[num];
}

/******************************************************************************
* Function Name : R_get_jpeg_output_area_num
* Description   : get jpeg output area number.
* Argument      : none
* Return Value  : none
******************************************************************************/
uint8_t R_get_jpeg_output_area_num( void )
{
    uint8_t num;

    R_BSP_InterruptsDisable();
    num = jpeg_output_area_num;
    R_BSP_InterruptsEnable();

    return num;
}
#endif // JPEG_AREA_MULTI

/******************************************************************************
* Function Name : R_get_jpeg_output_size
* Description   : get jpeg output size.
* Argument      : none
* Return Value  : none
******************************************************************************/
int32_t R_get_jpeg_output_size( void )
{
    int32_t size;

    R_BSP_InterruptsDisable();
    size = jpeg_output_size;
    R_BSP_InterruptsEnable();

    return size;
}


/******************************************************************************
* Function Name : R_jpeg_write_out_buffer
* Description   : This function is a callback function when the compressor finishes.
* Argument      : none
* Return Value  : none
******************************************************************************/
uint8_t *R_jpeg_write_out_buffer( int32_t *rest )
{
#if 0 // move to main.c
#ifndef JPEG_AREA_MULTI
    dmaca_stat_t                 stat_dmac;

    R_DMACA_Control(DMACA_CH1, DMACA_CMD_SOFT_REQ_NOT_CLR_REQ, &stat_dmac);
#endif
#endif

    R_BSP_InterruptsDisable();

    /* size = jpeg area size - output size(now) */
    jpeg_output_size = (int32_t)JPEG_OUTPUT_AREA_SIZE - *rest;    // jpeg size update

#ifdef JPEG_AREA_MULTI
    jpeg_output_area_num = ~jpeg_output_area_num & 0x01;
#endif

    R_BSP_InterruptsEnable();

    return buf_jpeg;
}

/******************************************************************************
End of file
******************************************************************************/
