11bd4fe43Sopenharmony_ci/*
21bd4fe43Sopenharmony_ci * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
31bd4fe43Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41bd4fe43Sopenharmony_ci * you may not use this file except in compliance with the License.
51bd4fe43Sopenharmony_ci * You may obtain a copy of the License at
61bd4fe43Sopenharmony_ci *
71bd4fe43Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81bd4fe43Sopenharmony_ci *
91bd4fe43Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101bd4fe43Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111bd4fe43Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121bd4fe43Sopenharmony_ci * See the License for the specific language governing permissions and
131bd4fe43Sopenharmony_ci * limitations under the License.
141bd4fe43Sopenharmony_ci */
151bd4fe43Sopenharmony_ci
161bd4fe43Sopenharmony_ci#ifndef __HI_MATH_H__
171bd4fe43Sopenharmony_ci#define __HI_MATH_H__
181bd4fe43Sopenharmony_ci
191bd4fe43Sopenharmony_ci#include "hi_type.h"
201bd4fe43Sopenharmony_ci
211bd4fe43Sopenharmony_ci#ifdef __cplusplus
221bd4fe43Sopenharmony_ci#if __cplusplus
231bd4fe43Sopenharmony_ciextern "C" {
241bd4fe43Sopenharmony_ci#endif
251bd4fe43Sopenharmony_ci#endif /* __cplusplus */
261bd4fe43Sopenharmony_ci
271bd4fe43Sopenharmony_ci/*
281bd4fe43Sopenharmony_ci * ABS(x)                 absolute value of x
291bd4fe43Sopenharmony_ci * SIGN(x)                sign of x
301bd4fe43Sopenharmony_ci * CMP(x,y)               0 if x == y; 1 if x > y; -1 if x < y
311bd4fe43Sopenharmony_ci */
321bd4fe43Sopenharmony_ci#define ABS(x)          ((x) >= 0 ? (x) : (-(x)))
331bd4fe43Sopenharmony_ci#define _SIGN(x)         ((x) >= 0 ? 1 : (-1))
341bd4fe43Sopenharmony_ci#define CMP(x, y)        (((x) == (y)) ? 0 : (((x) > (y)) ? 1 : (-1)))
351bd4fe43Sopenharmony_ci
361bd4fe43Sopenharmony_ci/*
371bd4fe43Sopenharmony_ci * MAX2(x,y)              maximum of x and y
381bd4fe43Sopenharmony_ci * MIN2(x,y)              minimum of x and y
391bd4fe43Sopenharmony_ci * MAX3(x,y,z)            maximum of x, y and z
401bd4fe43Sopenharmony_ci * MIN3(x,y,z)            minimum of x, y and z
411bd4fe43Sopenharmony_ci * MEDIAN(x,y,z)          median of x,y,z
421bd4fe43Sopenharmony_ci * MEAN2(x,y)             mean of x,y
431bd4fe43Sopenharmony_ci */
441bd4fe43Sopenharmony_ci#define MAX2(x, y)        ((x) > (y) ? (x) : (y))
451bd4fe43Sopenharmony_ci#ifndef MIN2
461bd4fe43Sopenharmony_ci#define MIN2(x, y)        ((x) < (y) ? (x) : (y))
471bd4fe43Sopenharmony_ci#endif
481bd4fe43Sopenharmony_ci#define MAX3(x, y, z)     ((x) > (y) ? MAX2(x, z) : MAX2(y, z))
491bd4fe43Sopenharmony_ci#define MIN3(x, y, z)     ((x) < (y) ? MIN2(x, z) : MIN2(y, z))
501bd4fe43Sopenharmony_ci#define MEDIAN(x, y, z)   (((x) + (y) + (z) - MAX3(x, y, z)) - MIN3(x, y, z))
511bd4fe43Sopenharmony_ci#define MEAN2(x, y)       (((x) + (y)) >> 1)
521bd4fe43Sopenharmony_ci
531bd4fe43Sopenharmony_ci/*
541bd4fe43Sopenharmony_ci * CLIP3(x,min,max)       clip x within [min,max]
551bd4fe43Sopenharmony_ci * WRAP_MAX(x,max,min)    wrap to min if x equal max
561bd4fe43Sopenharmony_ci * WRAP_MIN(x,min,max)    wrap to max if x equal min
571bd4fe43Sopenharmony_ci * VALUE_BETWEEN(x,min.max)   True if x is between [min,max] inclusively.
581bd4fe43Sopenharmony_ci */
591bd4fe43Sopenharmony_ci#define CLIP_MIN(x, min)          (((x) >= (min)) ? (x) : (min))
601bd4fe43Sopenharmony_ci#define CLIP3(x, min, max)         ((x) < (min) ? (min) : ((x) > (max) ? (max) :(x)))
611bd4fe43Sopenharmony_ci#define CLIP_MAX(x, max)          ((x) > (max) ? (max) : (x))
621bd4fe43Sopenharmony_ci#define WRAP_MAX(x, max, min)      ((x) >= (max) ? (min) : (x))
631bd4fe43Sopenharmony_ci#define WRAP_MIN(x, min, max)      ((x) <= (min) ? (max) : (x))
641bd4fe43Sopenharmony_ci#define VALUE_BETWEEN(x, min, max) (((x) >= (min)) && ((x) <= (max)))
651bd4fe43Sopenharmony_ci
661bd4fe43Sopenharmony_ci/*
671bd4fe43Sopenharmony_ci * MULTI_OF_2_POWER(x,a)  whether x is multiple of a(a must be power of 2)
681bd4fe43Sopenharmony_ci * HI_ALIGN_DOWN(x,a)     floor x to multiple of a(a must be power of 2)
691bd4fe43Sopenharmony_ci * HI_ALIGN_UP(x, a)            align x to multiple of a
701bd4fe43Sopenharmony_ci *
711bd4fe43Sopenharmony_ci * Example:
721bd4fe43Sopenharmony_ci * HI_ALIGN_UP(5,4) = 8
731bd4fe43Sopenharmony_ci * HI_ALIGN_DOWN(5,4)   = 4
741bd4fe43Sopenharmony_ci */
751bd4fe43Sopenharmony_ci#define MULTI_OF_2_POWER(x, a)    (!((x) & ((a) - 1)))
761bd4fe43Sopenharmony_ci#define HICEILING(x, a)           (((x) + (a) - 1) / (a))
771bd4fe43Sopenharmony_ci
781bd4fe43Sopenharmony_ci#define HI_ALIGN_UP(x, a)           ((((x) + ((a) - 1)) / (a)) * (a))
791bd4fe43Sopenharmony_ci#define HI_ALIGN_DOWN(x, a)         (((x) / (a)) * (a))
801bd4fe43Sopenharmony_ci#define ALIGN_UP(x, a)              ((((x) + ((a) - 1)) / (a)) * (a))
811bd4fe43Sopenharmony_ci#define ALIGN_DOWN(x, a)            (((x) / (a)) * (a))
821bd4fe43Sopenharmony_ci
831bd4fe43Sopenharmony_ci#define DIV_UP(x, a)             (((x) + ((a) - 1)) / (a))
841bd4fe43Sopenharmony_ci
851bd4fe43Sopenharmony_ci/*
861bd4fe43Sopenharmony_ci * Get the span between two unsigned number, such as
871bd4fe43Sopenharmony_ci * SPAN(HI_U32, 200, 100) is 200 - 100 = 100
881bd4fe43Sopenharmony_ci * SPAN(HI_U32, 100, 200) is 0xFFFFFFFF - 200 + 100
891bd4fe43Sopenharmony_ci * SPAN(HI_U64, 100, 200) is 0xFFFFFFFFFFFFFFFF - 200 + 100
901bd4fe43Sopenharmony_ci */
911bd4fe43Sopenharmony_ci#define SPAN(type, begin, end) \
921bd4fe43Sopenharmony_ci({                             \
931bd4fe43Sopenharmony_ci    type b = (begin);          \
941bd4fe43Sopenharmony_ci    type e = (end);            \
951bd4fe43Sopenharmony_ci    (type)((b >= e) ? (b - e) : (b + ((~((type)0)) - e))); \
961bd4fe43Sopenharmony_ci})
971bd4fe43Sopenharmony_ci
981bd4fe43Sopenharmony_ci/*
991bd4fe43Sopenharmony_ci * ENDIAN32(x,y)              little endian <---> big endian
1001bd4fe43Sopenharmony_ci * IS_LITTLE_END()            whether the system is little end mode
1011bd4fe43Sopenharmony_ci */
1021bd4fe43Sopenharmony_ci#define  ENDIAN32(x)                   \
1031bd4fe43Sopenharmony_ci    (((x) << 24) |                     \
1041bd4fe43Sopenharmony_ci    (((x) & 0x0000ff00) << 8) |        \
1051bd4fe43Sopenharmony_ci    (((x) & 0x00ff0000) >> 8) |        \
1061bd4fe43Sopenharmony_ci    (((x) >> 24) & 0x000000ff))
1071bd4fe43Sopenharmony_ci
1081bd4fe43Sopenharmony_ci/*
1091bd4fe43Sopenharmony_ci * ENDIAN16(x,y)              little endian <---> big endian
1101bd4fe43Sopenharmony_ci * IS_LITTLE_END()            whether the system is little end mode
1111bd4fe43Sopenharmony_ci */
1121bd4fe43Sopenharmony_ci#define  ENDIAN16(x)    ((((x) << 8) & 0xff00) | (((x) >> 8) & 255))
1131bd4fe43Sopenharmony_ci
1141bd4fe43Sopenharmony_ci__inline static HI_BOOL IS_LITTLE_END(void)
1151bd4fe43Sopenharmony_ci{
1161bd4fe43Sopenharmony_ci    union unEND_TEST_U {
1171bd4fe43Sopenharmony_ci        HI_CHAR cTest[4];
1181bd4fe43Sopenharmony_ci        HI_U32 u32Test;
1191bd4fe43Sopenharmony_ci    } unEndTest;
1201bd4fe43Sopenharmony_ci
1211bd4fe43Sopenharmony_ci    unEndTest.cTest[0] = 0x01;
1221bd4fe43Sopenharmony_ci    unEndTest.cTest[1] = 0x02;
1231bd4fe43Sopenharmony_ci    unEndTest.cTest[2] = 0x03;
1241bd4fe43Sopenharmony_ci    unEndTest.cTest[3] = 0x04;
1251bd4fe43Sopenharmony_ci
1261bd4fe43Sopenharmony_ci    return (unEndTest.u32Test > 0x01020304) ? (HI_TRUE) : (HI_FALSE);
1271bd4fe43Sopenharmony_ci}
1281bd4fe43Sopenharmony_ci
1291bd4fe43Sopenharmony_ci/*
1301bd4fe43Sopenharmony_ci * FRACTION32(de,nu)          fraction: nu(minator) / de(nominator).
1311bd4fe43Sopenharmony_ci * NUMERATOR32(x)              of x(x is fraction)
1321bd4fe43Sopenharmony_ci * DENOMINATOR32(x)           Denominator of x(x is fraction)
1331bd4fe43Sopenharmony_ci
1341bd4fe43Sopenharmony_ci * represent fraction in 32 bit. LSB 16 is numerator, MSB 16 is denominator
1351bd4fe43Sopenharmony_ci * It is integer if denominator is 0.
1361bd4fe43Sopenharmony_ci */
1371bd4fe43Sopenharmony_ci#define FRACTION32(de, nu)       (((de) << 16) | (nu))
1381bd4fe43Sopenharmony_ci#define NUMERATOR32(x)          ((x) & 0xffff)
1391bd4fe43Sopenharmony_ci#define DENOMINATOR32(x)        ((x) >> 16)
1401bd4fe43Sopenharmony_ci
1411bd4fe43Sopenharmony_ci/*
1421bd4fe43Sopenharmony_ci * RGB(r,g,b)    assemble the r,g,b to 24bit color
1431bd4fe43Sopenharmony_ci * RGB_R(c)      get RED   from 24bit color
1441bd4fe43Sopenharmony_ci * RGB_G(c)      get GREEN from 24bit color
1451bd4fe43Sopenharmony_ci * RGB_B(c)      get BLUE  from 24bit color
1461bd4fe43Sopenharmony_ci */
1471bd4fe43Sopenharmony_ci#define RGB(r, g, b) ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
1481bd4fe43Sopenharmony_ci#define RGB_R(c)   (((c) & 0xff0000) >> 16)
1491bd4fe43Sopenharmony_ci#define RGB_G(c)   (((c) & 0xff00) >> 8)
1501bd4fe43Sopenharmony_ci#define RGB_B(c)   ((c) & 0xff)
1511bd4fe43Sopenharmony_ci
1521bd4fe43Sopenharmony_ci/*
1531bd4fe43Sopenharmony_ci * YUV(y,u,v)    assemble the y,u,v to 30bit color
1541bd4fe43Sopenharmony_ci * YUV_Y(c)      get Y from 30bit color
1551bd4fe43Sopenharmony_ci * YUV_U(c)      get U from 30bit color
1561bd4fe43Sopenharmony_ci * YUV_V(c)      get V from 30bit color
1571bd4fe43Sopenharmony_ci */
1581bd4fe43Sopenharmony_ci#define YUV(y, u, v) ((((y) & 0x03ff) << 20) | (((u) & 0x03ff) << 10) | ((v) & 0x03ff))
1591bd4fe43Sopenharmony_ci#define YUV_Y(c)   (((c) & 0x3ff00000) >> 20)
1601bd4fe43Sopenharmony_ci#define YUV_U(c)   (((c) & 0x000ffc00) >> 10)
1611bd4fe43Sopenharmony_ci#define YUV_V(c)   ((c) & 0x000003ff)
1621bd4fe43Sopenharmony_ci
1631bd4fe43Sopenharmony_ci/*
1641bd4fe43Sopenharmony_ci * YUV_8BIT(y,u,v)    assemble the y,u,v to 24bit color
1651bd4fe43Sopenharmony_ci * YUV_8BIT_Y(c)      get Y from 24bit color
1661bd4fe43Sopenharmony_ci * YUV_8BIT_U(c)      get U from 24bit color
1671bd4fe43Sopenharmony_ci * YUV_8BIT_V(c)      get V from 24bit color
1681bd4fe43Sopenharmony_ci */
1691bd4fe43Sopenharmony_ci#define YUV_8BIT(y, u, v) ((((y) & 0xff) << 16) | (((u) & 0xff) << 8) | ((v) & 0xff))
1701bd4fe43Sopenharmony_ci#define YUV_8BIT_Y(c)   (((c) & 0xff0000) >> 16)
1711bd4fe43Sopenharmony_ci#define YUV_8BIT_U(c)   (((c) & 0xff00) >> 8)
1721bd4fe43Sopenharmony_ci#define YUV_8BIT_V(c)   ((c) & 0xff)
1731bd4fe43Sopenharmony_ci
1741bd4fe43Sopenharmony_ci/*
1751bd4fe43Sopenharmony_ci * Rgb2Yc(r, g, b, *y, *u, *u)    convert r,g,b to y,u,v
1761bd4fe43Sopenharmony_ci * Rgb2Yuv(rgb)             convert rgb to yuv
1771bd4fe43Sopenharmony_ci */
1781bd4fe43Sopenharmony_ci__inline static HI_VOID Rgb2Yc(HI_U16 r, HI_U16 g, HI_U16 b, HI_U16 *py, HI_U16 *pcb, HI_U16 *pcr)
1791bd4fe43Sopenharmony_ci{
1801bd4fe43Sopenharmony_ci    /* Y */
1811bd4fe43Sopenharmony_ci    *py = (HI_U16)((((r * 66 + g * 129 + b * 25) >> 8) + 16) << 2);
1821bd4fe43Sopenharmony_ci
1831bd4fe43Sopenharmony_ci    /* Cb */
1841bd4fe43Sopenharmony_ci    *pcb = (HI_U16)(((((b * 112 - r * 38) - g * 74) >> 8) + 128) << 2);
1851bd4fe43Sopenharmony_ci
1861bd4fe43Sopenharmony_ci    /* Cr */
1871bd4fe43Sopenharmony_ci    *pcr = (HI_U16)(((((r * 112 - g * 94) - b * 18) >> 8) + 128) << 2);
1881bd4fe43Sopenharmony_ci}
1891bd4fe43Sopenharmony_ci
1901bd4fe43Sopenharmony_ci__inline static HI_U32 Rgb2Yuv(HI_U32 u32Rgb)
1911bd4fe43Sopenharmony_ci{
1921bd4fe43Sopenharmony_ci    HI_U16 y, u, v;
1931bd4fe43Sopenharmony_ci
1941bd4fe43Sopenharmony_ci    Rgb2Yc(RGB_R(u32Rgb), RGB_G(u32Rgb), RGB_B(u32Rgb), &y, &u, &v);
1951bd4fe43Sopenharmony_ci
1961bd4fe43Sopenharmony_ci    return YUV(y, u, v);
1971bd4fe43Sopenharmony_ci}
1981bd4fe43Sopenharmony_ci
1991bd4fe43Sopenharmony_ci__inline static HI_VOID Rgb2Yc_full(HI_U16 r, HI_U16 g, HI_U16 b, HI_U16 *py, HI_U16 *pcb, HI_U16 *pcr)
2001bd4fe43Sopenharmony_ci{
2011bd4fe43Sopenharmony_ci    HI_U16 py_temp, pcb_temp, pcr_temp;
2021bd4fe43Sopenharmony_ci
2031bd4fe43Sopenharmony_ci    py_temp = (HI_U16)(((r * 76 + g * 150 + b * 29) >> 8) * 4);
2041bd4fe43Sopenharmony_ci    pcb_temp = (HI_U16)(CLIP_MIN(((((b * 130 - r * 44) - g * 86) >> 8) + 128), 0) * 4);
2051bd4fe43Sopenharmony_ci    pcr_temp = (HI_U16)(CLIP_MIN(((((r * 130 - g * 109) - b * 21) >> 8) + 128), 0) * 4);
2061bd4fe43Sopenharmony_ci
2071bd4fe43Sopenharmony_ci    *py = MAX2(MIN2(py_temp, 1023), 0);
2081bd4fe43Sopenharmony_ci    *pcb = MAX2(MIN2(pcb_temp, 1023), 0);
2091bd4fe43Sopenharmony_ci    *pcr = MAX2(MIN2(pcr_temp, 1023), 0);
2101bd4fe43Sopenharmony_ci}
2111bd4fe43Sopenharmony_ci
2121bd4fe43Sopenharmony_ci__inline static HI_U32 Rgb2Yuv_full(HI_U32 u32Rgb)
2131bd4fe43Sopenharmony_ci{
2141bd4fe43Sopenharmony_ci    HI_U16 y, u, v;
2151bd4fe43Sopenharmony_ci
2161bd4fe43Sopenharmony_ci    Rgb2Yc_full(RGB_R(u32Rgb), RGB_G(u32Rgb), RGB_B(u32Rgb), &y, &u, &v);
2171bd4fe43Sopenharmony_ci
2181bd4fe43Sopenharmony_ci    return YUV(y, u, v);
2191bd4fe43Sopenharmony_ci}
2201bd4fe43Sopenharmony_ci
2211bd4fe43Sopenharmony_ci/*
2221bd4fe43Sopenharmony_ci * Rgb2Yc_8BIT(r, g, b, *y, *u, *u)    convert r,g,b to y,u,v
2231bd4fe43Sopenharmony_ci * Rgb2Yuv_8BIT(rgb)                   convert rgb to yuv
2241bd4fe43Sopenharmony_ci */
2251bd4fe43Sopenharmony_ci__inline static HI_VOID Rgb2Yc_8BIT(HI_U8 r, HI_U8 g, HI_U8 b, HI_U8 *py, HI_U8 *pcb, HI_U8 *pcr)
2261bd4fe43Sopenharmony_ci{
2271bd4fe43Sopenharmony_ci    /* Y */
2281bd4fe43Sopenharmony_ci    *py = (HI_U8)(((r * 66 + g * 129 + b * 25) >> 8) + 16);
2291bd4fe43Sopenharmony_ci
2301bd4fe43Sopenharmony_ci    /* Cb */
2311bd4fe43Sopenharmony_ci    *pcb = (HI_U8)((((b * 112 - r * 38) - g * 74) >> 8) + 128);
2321bd4fe43Sopenharmony_ci
2331bd4fe43Sopenharmony_ci    /* Cr */
2341bd4fe43Sopenharmony_ci    *pcr = (HI_U8)((((r * 112 - g * 94) - b * 18) >> 8) + 128);
2351bd4fe43Sopenharmony_ci}
2361bd4fe43Sopenharmony_ci
2371bd4fe43Sopenharmony_ci__inline static HI_U32 Rgb2Yuv_8BIT(HI_U32 u32Rgb)
2381bd4fe43Sopenharmony_ci{
2391bd4fe43Sopenharmony_ci    HI_U8 y, u, v;
2401bd4fe43Sopenharmony_ci
2411bd4fe43Sopenharmony_ci    Rgb2Yc_8BIT(RGB_R(u32Rgb), RGB_G(u32Rgb), RGB_B(u32Rgb), &y, &u, &v);
2421bd4fe43Sopenharmony_ci
2431bd4fe43Sopenharmony_ci    return YUV_8BIT(y, u, v);
2441bd4fe43Sopenharmony_ci}
2451bd4fe43Sopenharmony_ci
2461bd4fe43Sopenharmony_ci__inline static HI_VOID Rgb2Yc_full_8BIT(HI_U8 r, HI_U8 g, HI_U8 b, HI_U8 *py, HI_U8 *pcb, HI_U8 *pcr)
2471bd4fe43Sopenharmony_ci{
2481bd4fe43Sopenharmony_ci    HI_S16 py_temp, pcb_temp, pcr_temp;
2491bd4fe43Sopenharmony_ci
2501bd4fe43Sopenharmony_ci    py_temp = (r * 76 + g * 150 + b * 29) >> 8;
2511bd4fe43Sopenharmony_ci    pcb_temp = (((b * 130 - r * 44) - g * 86) >> 8) + 128;
2521bd4fe43Sopenharmony_ci    pcr_temp = (((r * 130 - g * 109) - b * 21) >> 8) + 128;
2531bd4fe43Sopenharmony_ci
2541bd4fe43Sopenharmony_ci    *py = MAX2(MIN2(py_temp, 255), 0);
2551bd4fe43Sopenharmony_ci    *pcb = MAX2(MIN2(pcb_temp, 255), 0);
2561bd4fe43Sopenharmony_ci    *pcr = MAX2(MIN2(pcr_temp, 255), 0);
2571bd4fe43Sopenharmony_ci}
2581bd4fe43Sopenharmony_ci
2591bd4fe43Sopenharmony_ci__inline static HI_U32 Rgb2Yuv_full_8BIT(HI_U32 u32Rgb)
2601bd4fe43Sopenharmony_ci{
2611bd4fe43Sopenharmony_ci    HI_U8 y, u, v;
2621bd4fe43Sopenharmony_ci
2631bd4fe43Sopenharmony_ci    Rgb2Yc_full_8BIT(RGB_R(u32Rgb), RGB_G(u32Rgb), RGB_B(u32Rgb), &y, &u, &v);
2641bd4fe43Sopenharmony_ci
2651bd4fe43Sopenharmony_ci    return YUV_8BIT(y, u, v);
2661bd4fe43Sopenharmony_ci}
2671bd4fe43Sopenharmony_ci
2681bd4fe43Sopenharmony_ci/*
2691bd4fe43Sopenharmony_ci * FpsControl Using Sample:
2701bd4fe43Sopenharmony_ci *  FPS_CTRL_S g_stFpsCtrl;
2711bd4fe43Sopenharmony_ci *
2721bd4fe43Sopenharmony_ci *  Take 12 frame uniform in 25.
2731bd4fe43Sopenharmony_ci *  InitFps(&g_stFpsCtrl, 25, 12);
2741bd4fe43Sopenharmony_ci *
2751bd4fe43Sopenharmony_ci *  {
2761bd4fe43Sopenharmony_ci *       if(FpsControl(&g_stFpsCtrl)) printf("Yes, this frame should be token");
2771bd4fe43Sopenharmony_ci *  }
2781bd4fe43Sopenharmony_ci *
2791bd4fe43Sopenharmony_ci */
2801bd4fe43Sopenharmony_citypedef struct hiFPS_CTRL_S {
2811bd4fe43Sopenharmony_ci    HI_U32 u32Ffps; /* Full frame rate    */
2821bd4fe43Sopenharmony_ci    HI_U32 u32Tfps; /* Target frame rate  */
2831bd4fe43Sopenharmony_ci    HI_U32 u32FrmKey; /* update key frame   */
2841bd4fe43Sopenharmony_ci} FPS_CTRL_S;
2851bd4fe43Sopenharmony_ci
2861bd4fe43Sopenharmony_ci__inline static HI_VOID InitFps(FPS_CTRL_S *pFrmCtrl, HI_U32 u32FullFps, HI_U32 u32TagFps)
2871bd4fe43Sopenharmony_ci{
2881bd4fe43Sopenharmony_ci    pFrmCtrl->u32Ffps   = u32FullFps;
2891bd4fe43Sopenharmony_ci    pFrmCtrl->u32Tfps   = u32TagFps;
2901bd4fe43Sopenharmony_ci    pFrmCtrl->u32FrmKey = 0;
2911bd4fe43Sopenharmony_ci}
2921bd4fe43Sopenharmony_ci
2931bd4fe43Sopenharmony_ci__inline static HI_BOOL FpsControl(FPS_CTRL_S *pFrmCtrl)
2941bd4fe43Sopenharmony_ci{
2951bd4fe43Sopenharmony_ci    HI_BOOL bReturn = HI_FALSE;
2961bd4fe43Sopenharmony_ci
2971bd4fe43Sopenharmony_ci    pFrmCtrl->u32FrmKey += pFrmCtrl->u32Tfps;
2981bd4fe43Sopenharmony_ci    if (pFrmCtrl->u32FrmKey >= pFrmCtrl->u32Ffps) {
2991bd4fe43Sopenharmony_ci        pFrmCtrl->u32FrmKey -= pFrmCtrl->u32Ffps;
3001bd4fe43Sopenharmony_ci        bReturn = HI_TRUE;
3011bd4fe43Sopenharmony_ci    }
3021bd4fe43Sopenharmony_ci
3031bd4fe43Sopenharmony_ci    return bReturn;
3041bd4fe43Sopenharmony_ci}
3051bd4fe43Sopenharmony_ci
3061bd4fe43Sopenharmony_ci__inline static HI_U32 GetLowAddr(HI_U64 u64Phyaddr)
3071bd4fe43Sopenharmony_ci{
3081bd4fe43Sopenharmony_ci    return (HI_U32)u64Phyaddr;
3091bd4fe43Sopenharmony_ci}
3101bd4fe43Sopenharmony_ci
3111bd4fe43Sopenharmony_ci__inline static HI_U32 GetHighAddr(HI_U64 u64Phyaddr)
3121bd4fe43Sopenharmony_ci{
3131bd4fe43Sopenharmony_ci    return (HI_U32)(u64Phyaddr >> 32);
3141bd4fe43Sopenharmony_ci}
3151bd4fe43Sopenharmony_ci
3161bd4fe43Sopenharmony_ci#define hi_usleep(usec) \
3171bd4fe43Sopenharmony_ci    do { \
3181bd4fe43Sopenharmony_ci        usleep(usec); \
3191bd4fe43Sopenharmony_ci    } while (0)
3201bd4fe43Sopenharmony_ci
3211bd4fe43Sopenharmony_ci#ifdef __cplusplus
3221bd4fe43Sopenharmony_ci#if __cplusplus
3231bd4fe43Sopenharmony_ci}
3241bd4fe43Sopenharmony_ci#endif
3251bd4fe43Sopenharmony_ci#endif /* __cplusplus */
3261bd4fe43Sopenharmony_ci
3271bd4fe43Sopenharmony_ci#endif /* __HI_MATH_H__ */
3281bd4fe43Sopenharmony_ci
329