/* 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.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 *
 * $Author: brianpc.huang $
 * $Date: 2015/05/29 $
 * $RCSfile: drv_nr.h,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

/**
 * @file Noise meter header file
 *
 * @author skywalker.yin@mediatek.com
 */

#ifndef _DRV_NOISEMETER_H_
#define _DRV_NOISEMETER_H_

#include "typedef.h"

/** Data structure
 */
typedef struct
{
	UINT32 u4CurSum;                //current block sum
	UINT32 u4PreSum;                //previoud block sum
	UINT32 u4AbsDiffSum;            //Sum of absolute diff
	UINT32 u4SquareDiffSum;	        //sun of square diff
	UINT32 u4BlockAPL;              //APL of current block
}NR_NOISE_METER_LOCALAREA_INFO;

typedef struct
{
    UINT16 u2NoiseLevel;            //final noise level
    UINT16 u2FinalConfidence;       //final noise level confidence
}NR_NOISE_METER_RESULT;

typedef struct
{
	UINT8  fgPixelCountStable;      //pixel count of diff. histogram is stable or not
	UINT8  bMotionPixelRatio;       //motion pixel count ratio
    UINT16 u2MoPxlConfidence;       //confidence1: motion pixel ratio confidence	
}NR_NOISE_METER_MPR_INFO;

typedef struct
{
	UINT32 u4MinDS;                 //Minimum DS
	UINT32 u4MaxDS;                 //Maximum DS
	UINT32 u4AvgDS;                 //Average DS
	UINT16 u2DSRatio;               //original DSR
	UINT16 u2NDSRatio;              //normalized DSR
    UINT16 u2DSRatioConfidence;     //confidence2: DS ratio confidence	
	UINT8 fgDSStable;               //flag to show if DS info. is stable or not
}NR_NOISE_METER_DSR_INFO;

//u2GlobalHistogram[0]  -128 ~ -84
//u2GlobalHistogram[1]  -83 ~ -72
//u2GlobalHistogram[2]  -71 ~ -60
//u2GlobalHistogram[3]  -59 ~ -48
//u2GlobalHistogram[4]  -47 ~ -36
//u2GlobalHistogram[5]  -35 ~ -24
//u2GlobalHistogram[6]  -23 ~ -12
//u2GlobalHistogram[7]  -11 ~   0
//u2GlobalHistogram[8]    1 ~  12
//u2GlobalHistogram[9]   13 ~  24
//u2GlobalHistogram[10]  25 ~  36
//u2GlobalHistogram[11]  37 ~  48
//u2GlobalHistogram[12]  49 ~  60
//u2GlobalHistogram[13]  61 ~  72
//u2GlobalHistogram[14]  73 ~  84
//u2GlobalHistogram[15]  85 ~ 127		
typedef struct
{
	UINT16 u2NoiseValue;                                //Noise Value
	UINT16 u2GlobalHistogram[16];                       //16 bin of diff. histogram
	NR_NOISE_METER_LOCALAREA_INFO NMLocalAreaInfo[16];	//info. of 16 local area
	UINT8  bAPLW;                                       //APL weighting
	NR_NOISE_METER_MPR_INFO nmMPR;                      //MPR info.	
	NR_NOISE_METER_DSR_INFO nmDSR;                      //DSR info.
	NR_NOISE_METER_RESULT nmResult;                     //NM result
	UINT8 fgTriggerStatus;                              //flag to trigger all table update
}NR_NOISE_METER_INFO;

typedef struct
{
    UINT32 xTurn1;          //Table x turn point1
    UINT32 xTurn2;          //Table x turn point2
    UINT32 yStart;          //Table y start value
    UINT32 yEnd;            //Table y end value
}NM_LINEAR_LUT;

/** Public function    
 */

//noise meter
void vDrvNMInit(UINT8 fgNM,UINT32 loopNumber,UINT32 intervalNumber);
void vDrvNMSetBlockSize(UINT16 width, UINT16 height);
void vDrvNMProc(void);
NR_NOISE_METER_RESULT* vDrvNMGetResult(void);
void vDrvNMLUTMappingUT(void);      //Function for CLI unit test

#endif
