/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 * $RCSfile: x_img_dec.h,v $
 * $Revision: #1 $
 * $Date: 2015/05/29 $ 2007/08/27
 * $Author: brianpc.huang $
 * $CCRevision: $
 * $SWAuthor: Vincent Hsu $
 * $MD5HEX:  $
 *
 * Description: 
 *         This file exports all set & get types commands and related structures 
 *         for the image decoder driver.
 *---------------------------------------------------------------------------*/

#ifndef _X_IMG_DEC_H_
#define _X_IMG_DEC_H_


/*-----------------------------------------------------------------------------
                    include files
 ----------------------------------------------------------------------------*/
#include "x_gfx.h"

#ifdef __cplusplus
extern "C" {
#endif

/*-----------------------------------------------------------------------------
                    macros, defines, typedefs, enums
 ----------------------------------------------------------------------------*/

/* get types */
#define IMG_GET_TYPE_PROGRESS                       \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 1))

#define IMG_GET_TYPE_LIMITATION                     \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 2))
    
/* set types */
#define IMG_SET_TYPE_CONFIG							\
	(RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 1))

#define IMG_SET_TYPE_FRM_START                      \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 2))
    
#define IMG_SET_TYPE_DECODE                         \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 3))
    
#define IMG_SET_TYPE_STOP                           \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 4))
    
#define IMG_SET_TYPE_BUF_FILLED                     \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 5))
    
#define IMG_SET_TYPE_FRM_END                        \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 6))

#define IMG_SET_TYPE_DIRECT_DECODE                  \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 7))

/* Driver error codes */
#define IMG_DRV_HW_ERROR        (-1)
#define IMG_DRV_IO_ERROR        (-2)
#define IMG_DRV_NOT_SUPPORT     (-3)
#define IMG_DRV_BUSY            (-4)

/* MW buffer-filling return codes */
#define IMG_MW_FILL_OK          ( 0)
#define IMG_MW_FILL_EOF         (-1)
#define IMG_MW_FILL_ERROR       (-2)

/* ID for limitation qeury */
#define IMG_LIMIT_PROGRESSIVE       ((UINT32) 0)
#define IMG_LIMIT_RESOLUTION        ((UINT32) 1)
#define IMG_LIMIT_PGSV_RESOLUTION   ((UINT32) 2)
#define IMG_LIMIT_FULL_MODE         ((UINT32) 3)

/* notification states */
typedef enum
{
    IMG_NFY_FILL_BUF,
    IMG_NFY_FINISHED,
    IMG_NFY_ERROR,
    IMG_NFY_STOP_DONE,
    IMG_NFY_ALREADY_STOPPED,
    IMG_NFY_DRV_STOPED
} IMG_NFY_STATE_T;

/* callback function */
typedef VOID (*IMG_NFY_FCT_T)
(
    UINT32          ui4_img_id,         /* the image id which causes this notification */
    VOID            *pv_tag,            /* tag passed to the callback function */
    VOID            *pv_data,           /* data passed with this notification */
    IMG_NFY_STATE_T e_state);           /* notification state */

/* rotation information */
typedef enum
{
#if 0
    IMG_ROTATE_NONE             = 0,    /* no rotation */
    IMG_ROTATE_CW_90            = 1,    /* clockwise  90 degrees  */
    IMG_ROTATE_CW_180           = 2,    /* clockwise 180 degrees  */
    IMG_ROTATE_CW_270           = 3,    /* clockwise 270 degrees  */
    IMG_ROTATE_NONE_WITH_FLIP   = 4,    /* no rotation, with flip */
    IMG_ROTATE_CW_90_WITH_FLIP  = 5,    /* clockwise  90 degrees, with flip */
    IMG_ROTATE_CW_180_WITH_FLIP = 6,    /* clockwise 180 degrees, with flip */
    IMG_ROTATE_CW_270_WITH_FLIP = 7     /* clockwise 270 degrees, with flip */
#endif
	IMG_ROTATE_NONE			 		= 0,
	IMG_ROTATE_NONE_WITH_FLIP	 	= 1,
	IMG_ROTATE_CW_180			 	= 2,
	IMG_ROTATE_CW_180_WITH_FLIP 	= 3,
	IMG_ROTATE_CW_270_WITH_FLIP 	= 4,
	IMG_ROTATE_CW_90			 	= 5,
	IMG_ROTATE_CW_90_WITH_FLIP  	= 6,
	IMG_ROTATE_CW_270			 	= 7
} IMG_ROTATE_T;

/* data passed with IMG_SET_TYPE_FRM_START */
typedef struct
{
    UINT32          ui4_img_id;         /* the image id decoded */
    VOID            *pv_img_buf;        /* image data */
    UINT32          ui4_img_size;       /* image size */
    VOID            *pv_aux_cache;      /* auxiliary cache data */
    IMG_NFY_FCT_T   pf_func;            /* callback function */
    VOID            *pv_tag;            /* tag passed to the callback function */
} IMG_FRM_START_T;

/* data passed with IMG_NFY_FILL_BUF */
typedef struct
{
    UINT32          ui4_trsn_id;        /* transaction id */
    VOID            *pv_start_add;      /* starting address */
    UINT32          ui4_required_len;   /* required length */
    BOOL            b_reset_pos;        /* position-resetting flag */
    UINT32          ui4_position;       /* the position to be resettd */
} IMG_FILL_BUF_T;

/* data passed with IMG_SET_TYPE_BUF_FILLED */
typedef struct
{
    UINT32          ui4_trsn_id;        /* transaction id */
    INT32           i4_ret;             /* MW buffer-filling return codes */
    UINT32          ui4_filled_len;     /* filled length */
} IMG_BUF_FILLED_T;

/* data passed with IMG_SET_TYPE_DECODE */
typedef struct
{	  
    VOID            *pv_img_buf;        /* image data */
    VOID            *pv_img_buf2;       /* image data2 */
    UINT32          ui4_img_size;       /* image size */
    VOID            *pv_type_data;      /* type data */

    UINT32          ui4_src_x;          /* x offset in the source image in pixels */
    UINT32          ui4_src_y;          /* y offset in the source image in pixels */
    UINT32          ui4_src_width;      /* width to be decoded in pixels */
    UINT32          ui4_src_height;     /* height to be decoded in pixels */

    VOID            *pv_dst;            /* destination starting address */
    VOID            *pv_dst2;           /* additional dst starting address */
    UINT32          ui4_dst_x;          /* x offset in the destination in pixels */
    UINT32          ui4_dst_y;          /* y offset in the destination in pixels */
    UINT32          ui4_dst_width;      /* expected output width in pixels */
    UINT32          ui4_dst_height;     /* expected output height in pixels */
    UINT32          ui4_dst_pitch;      /* pitch of the destination image */
    UINT32          ui4_dst2_pitch;     /* pitch of the additional destination image */
    GFX_COLORMODE_T e_dst_cm;           /* destination color mode */
    
    IMG_ROTATE_T    e_rotate;           /* rotation option */
} IMG_DIRECT_DECODE_T;

typedef struct
{
    UINT32          ui4_img_id;         /* the image id decoded */
    VOID            *pv_type_data;      /* frame index */

    UINT32          ui4_src_x;          /* x offset in the source image in pixels */
    UINT32          ui4_src_y;          /* y offset in the source image in pixels */
    UINT32          ui4_src_width;      /* width to be decoded in pixels */
    UINT32          ui4_src_height;     /* height to be decoded in pixels */

    VOID            *pv_dst;            /* destination starting address */
    VOID            *pv_dst2;           /* additional dst starting address */
    UINT32          ui4_dst_x;          /* x offset in the destination in pixels */
    UINT32          ui4_dst_y;          /* y offset in the destination in pixels */
    UINT32          ui4_dst_width;      /* expected output width in pixels */
    UINT32          ui4_dst_height;     /* expected output height in pixels */
    UINT32          ui4_dst_pitch;      /* pitch of the destination image */
    UINT32          ui4_dst2_pitch;     /* pitch of the additional destination image */
    GFX_COLORMODE_T e_dst_cm;           /* destination color mode */
    BOOL            b_is_virtual_addr;
    
    IMG_ROTATE_T    e_rotate;           /* rotation option */
    BOOL b_compressed;
	UINT32			ui4_CbCrOffset;
} IMG_DECODE_T;

/* passed with IMG_GET_TYPE_LIMITATION */
typedef struct
{
    /* INPUT */
    UINT32          ui4_type;

    /* OUTPUT */
    union {
        /* used for IMG_LIMIT_PROGRESSIVE */
        BOOL        b_progressive_support;
        
        /* used for IMG_LIMIT_RESOLUTION */
        struct {
            UINT32  ui4_max_width;
            UINT32  ui4_min_width;
            UINT32  ui4_max_height;
            UINT32  ui4_min_height;
            UINT32  ui4_max_area;
        } t_resolution;

        /* used for IMG_LIMIT_PGSV_RESOLUTION */
        struct {
            UINT32  ui4_max_area;
            UINT32  ui4_min_width;
            UINT32  ui4_min_height;
        } t_pgsv_resolution;

        /* used for IMG_LIMIT_FULL_MODE */
        BOOL        b_full_mode_support;
    } u;
} IMG_LIMITATION_T;

typedef enum
{
    IMAGE_TYPE_JPEG,
    IMAGE_TYPE_PNG,
    IMAGE_TYPE_WEBP,
    IMAGE_TYPE_MAX
}ENUM_IMG_TYPE;

//component flag
#define		JPG_COMP_FLAG_JPG_SUPPORT	0x4
#define		JPG_COMP_FLAG_PNG_SUPPORT	0x8
#define  	JPG_COMP_FLAG_WEBP_SUPPORT	0x10

/*-----------------------------------------------------------------------------
                    functions declarations
 ----------------------------------------------------------------------------*/

#ifdef __cplusplus
}
#endif

#endif /* _X_IMG_DEC_H_ */

