/**
 * @file hv_cal_OsdScaler.c
 * @brief Osd Cal Scaler interface file.
 * @details This file provides the following functions: \n
 *          (1) Osd CAL layer initialization functions \n
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-08-31
 * @copyright Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author         <th>Date        <th>Change Description
 * <tr><td>OSD Group      <td>2022-08-31   <td>create
 * </table>
*/

#include "hv_drv_Osd.h"
#include "hv_cal_Osd.h"

/*bEnable=1:enable; bEnable=0:bypass*/
VOID Hv_Cal_OsdScaler_SetEnable(BOOL bEnable)
{
    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_00, reg_sc_throu_sw, !bEnable);
}

BOOL Hv_Cal_OsdScaler_GetEnable(VOID)
{
    BOOL bEnable = HV_FALSE;
    HV_R32_DECL_VAR();

    bEnable = HV_R32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_00, reg_sc_throu_sw);
    return !bEnable;
}

VOID Hv_Cal_OsdScaler_SetMode(OsdScalerMode enMode)
{
    UCHAR8 ucModeValue = 0;

    switch (enMode)
    {
        case OSD_SCALER_MODE_FILTER:
        {
            ucModeValue = 0;
            break;
        }
        case OSD_SCALER_MODE_BILINEAR:
        {
            ucModeValue = 1;
            break;
        }
        case OSD_SCALER_MODE_REPEAT:
        {
            ucModeValue = 2;
            break;
        }
        case OSD_SCALER_MODE_KEEP:
        {
            ucModeValue = 3;
            break;
        }
        default:
        {
            break;
        }
    }

    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_00, reg_sc_cvmode, ucModeValue);
    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_00, reg_sc_chmode, ucModeValue);

    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_00, reg_sc_yvmode, ucModeValue);
    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_00, reg_sc_yhmode, ucModeValue);

    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_00, reg_sc_avmode, ucModeValue);
    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_00, reg_sc_ahmode, ucModeValue);
}

VOID Hv_Cal_OsdScaler_SetHflipEnable(BOOL bEnable)
{
    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_00, reg_sc_h_flip, bEnable);
}

BOOL Hv_Cal_OsdScaler_GetHflipEnable(VOID)
{
    HV_R32_DECL_VAR();

    return HV_R32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_00, reg_sc_h_flip);
}

VOID Hv_Cal_OsdScaler_SetScalerInOut(USHORT16 usHszin, USHORT16 usVszin,
                                    USHORT16 usHszout, USHORT16 usVszout)
{
    FLOAT32 fHRatio = usHszout / usHszin;
    FLOAT32 fVRatio = usVszout / usVszin;
    USHORT16 usHfilbase = 8192 / fHRatio;
    USHORT16 usVfilbase = 8192 / fVRatio;

    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_04, reg_sc_hszin, usHszin);
    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_08, reg_sc_vszin, usVszin);

    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_10, reg_sc_hszout, usHszout);
    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_14, reg_sc_vszout, usVszout);

    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_28, reg_sc_hfilbase, usHfilbase);
    HV_W32_FIELD(DISP_TOP_REG_OSD_CUBIC_SCALER_V2_00_2C, reg_sc_vfilbase, usVfilbase);
}

VOID Hv_Cal_OsdScaler_SetScalerRatio()
{
    USHORT16 usWidth = 0;
    USHORT16 usHeight = 0;
    USHORT16 usScWidth = 0;
    USHORT16 usScHeight = 0;
    BOOL bScalerup = HV_FALSE;
    //OsdArea stArea;

    bScalerup = Hv_Cal_OsdDP_GetOsdScalerup();

    if (bScalerup)
    {
        Hv_Cal_OsdScaler_SetMode(OSD_SCALER_MODE_REPEAT);
    }
    else
    {
        Hv_Cal_OsdScaler_SetMode(OSD_SCALER_MODE_KEEP);
    }


    usWidth = Hv_Cal_OsdDP_GetRealHActive();
    usHeight = Hv_Cal_OsdDP_GetRealVActive();
    Hv_Cal_OsdDP_SetPreScalerSize(usWidth, usHeight);

    usScWidth = Hv_Cal_OsdDP_GetScreenHActive();
    usScHeight = Hv_Cal_OsdDP_GetScreenVActive();
    Hv_Cal_OsdScaler_SetScalerInOut(usWidth, usHeight, usScWidth, usScHeight);

#if 0
    Hv_Cal_OsdDP_SetMtxCanvas(usScWidth, usScHeight);

    stArea.usX = 0;
    stArea.usY = 0;
    stArea.usWidth = usScWidth;
    stArea.usHeight = usScHeight;
    Hv_Cal_OsdDP_SetVideoBlendPosition(&stArea);
#endif
}

VOID Hv_Cal_OsdScaler_Init(VOID)
{
    /* H flip */
    Hv_Cal_OsdScaler_SetHflipEnable(HV_FALSE);

    Hv_Cal_OsdScaler_SetScalerRatio();

    /* enable scaler */
    Hv_Cal_OsdScaler_SetEnable(1);
}
