• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 /*****************************************************************************/
22 /* File Includes                                                             */
23 /*****************************************************************************/
24 /* System include files */
25 #include <stdio.h>
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <time.h>
30 
31 /* User include files */
32 #include "ihevc_typedefs.h"
33 #include "ihevc_macros.h"
34 #include "ihevc_debug.h"
35 #include "ihevc_platform_macros.h"
36 
37 #include "hme_datatype.h"
38 #include "hme_common_defs.h"
39 
40 /*****************************************************************************/
41 /* Function Definitions                                                      */
42 /*****************************************************************************/
43 
44 /**
45 ********************************************************************************
46 *  @fn     S16 median4_s16(S16 i2_n1, S16 i2_n2, S16 i2_n3, S16 i2_n4);
47 *
48 *  @brief  Returns median4 of 4 16 bits signed nubers
49 *
50 *  @param[in] i2_n1 : first number
51 *
52 *  @param[in] i2_n2 : 2nd number
53 *
54 *  @param[in] i2_n3 : 3rd number
55 *
56 *  @param[in] i2_n4 : 4th number (order does not matter)
57 *
58 *  @return range of the number
59 ********************************************************************************
60 */
median4_s16(S16 i2_n1,S16 i2_n2,S16 i2_n3,S16 i2_n4)61 S16 median4_s16(S16 i2_n1, S16 i2_n2, S16 i2_n3, S16 i2_n4)
62 {
63     S16 i2_max, i2_min;
64 
65     i2_max = MAX(i2_n1, i2_n2);
66     i2_max = MAX(i2_max, i2_n3);
67     i2_max = MAX(i2_max, i2_n4);
68 
69     i2_min = MIN(i2_n1, i2_n2);
70     i2_min = MIN(i2_min, i2_n3);
71     i2_min = MIN(i2_min, i2_n4);
72 
73     return ((S16)((i2_n1 + i2_n2 + i2_n3 + i2_n4 - i2_max - i2_min) >> 1));
74 }
75 
hme_compute_2d_sum_u08(U08 * pu1_inp,S32 i4_wd,S32 i4_ht,S32 i4_stride)76 U32 hme_compute_2d_sum_u08(U08 *pu1_inp, S32 i4_wd, S32 i4_ht, S32 i4_stride)
77 {
78     S32 i, j;
79     U32 u4_sum = 0;
80 
81     for(i = 0; i < i4_ht; i++)
82     {
83         for(j = 0; j < i4_wd; j++)
84             u4_sum += (U32)pu1_inp[j];
85 
86         pu1_inp += i4_stride;
87     }
88 
89     return (u4_sum);
90 }
hme_compute_2d_sum_u16(U16 * pu2_inp,S32 i4_wd,S32 i4_ht,S32 i4_stride)91 U32 hme_compute_2d_sum_u16(U16 *pu2_inp, S32 i4_wd, S32 i4_ht, S32 i4_stride)
92 {
93     S32 i, j;
94     U32 u4_sum = 0;
95 
96     for(i = 0; i < i4_ht; i++)
97     {
98         for(j = 0; j < i4_wd; j++)
99             u4_sum += (U32)pu2_inp[j];
100 
101         pu2_inp += i4_stride;
102     }
103 
104     return (u4_sum);
105 }
hme_compute_2d_sum_u32(U32 * pu4_inp,S32 i4_wd,S32 i4_ht,S32 i4_stride)106 U32 hme_compute_2d_sum_u32(U32 *pu4_inp, S32 i4_wd, S32 i4_ht, S32 i4_stride)
107 {
108     S32 i, j;
109     U32 u4_sum = 0;
110 
111     for(i = 0; i < i4_ht; i++)
112     {
113         for(j = 0; j < i4_wd; j++)
114             u4_sum += (U32)pu4_inp[j];
115 
116         pu4_inp += i4_stride;
117     }
118 
119     return (u4_sum);
120 }
121 /**
122 ********************************************************************************
123 *  @fn     S32 hme_compute_2d_sum_unsigned(void *pv_inp,
124 *                                       S32 i4_blk_wd,
125 *                                       S32 i4_blk_ht,
126 *                                   S32 i4_stride,
127 *                                   S32 i4_datatype)
128 *
129 *  @brief  Computes and returns 2D sum of a unsigned 2d buffer, with datatype
130 *          equal to 8/16/32 bit.
131 *
132 *  @param[in] pv_inp : input pointer
133 *
134 *  @param[in] i4_blk_wd : block width
135 *
136 *  @param[in] i4_blk_ht : block ht
137 *
138 *  @param[in] i4_stride : stride
139 *
140 *  @param[in] i4_datatype : datatype 1 - 8 bit, 2 - 16 bit, 4 - 32 bit
141 *
142 *  @return sum of i4_blk_wd * i4_blk_ht number of entries starting at pv_inp
143 ********************************************************************************
144 */
145 
hme_compute_2d_sum_unsigned(void * pv_inp,S32 i4_blk_wd,S32 i4_blk_ht,S32 i4_stride,S32 i4_datatype)146 U32 hme_compute_2d_sum_unsigned(
147     void *pv_inp, S32 i4_blk_wd, S32 i4_blk_ht, S32 i4_stride, S32 i4_datatype)
148 {
149     if(i4_datatype == sizeof(U08))
150         return (hme_compute_2d_sum_u08((U08 *)pv_inp, i4_blk_wd, i4_blk_ht, i4_stride));
151     else if(i4_datatype == sizeof(U16))
152         return (hme_compute_2d_sum_u16((U16 *)pv_inp, i4_blk_wd, i4_blk_ht, i4_stride));
153     else if(i4_datatype == sizeof(U32))
154         return (hme_compute_2d_sum_u32((U32 *)pv_inp, i4_blk_wd, i4_blk_ht, i4_stride));
155     else
156         ASSERT(0);
157 
158     return 0;
159 }
160 
161 /**
162 ********************************************************************************
163 *  @fn     S32 get_rand_num(S32 low, S32 high)
164 *
165 *  @brief  returns a radom integer in the closed interval [low, high - 1]
166 *
167 *  @param[in] low : lower limit
168 *
169 *  @param[in] high : higher limit
170 *
171 *  @return S32 result: the random number
172 ********************************************************************************
173 */
get_rand_num(S32 low,S32 high)174 S32 get_rand_num(S32 low, S32 high)
175 {
176     double num;
177     S32 result;
178     num = (double)rand() / (double)RAND_MAX;
179     num = num * (high - low) + low;
180 
181     result = (S32)floor((num + 0.5));
182     if(result < low)
183         result = low;
184     if(result >= high)
185         result = high - 1;
186 
187     return (result);
188 }
189