aboutsummaryrefslogtreecommitdiff
path: root/encoder/hme_common_utils.c
blob: 8509bb77d3e58f273c2e39fa3648cebe6150ade0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/******************************************************************************
 *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/

/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/
/* System include files */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

/* User include files */
#include "ihevc_typedefs.h"
#include "ihevc_macros.h"
#include "ihevc_debug.h"
#include "ihevc_platform_macros.h"

#include "hme_datatype.h"
#include "hme_common_defs.h"

/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/

/**
********************************************************************************
*  @fn     S16 median4_s16(S16 i2_n1, S16 i2_n2, S16 i2_n3, S16 i2_n4);
*
*  @brief  Returns median4 of 4 16 bits signed nubers
*
*  @param[in] i2_n1 : first number
*
*  @param[in] i2_n2 : 2nd number
*
*  @param[in] i2_n3 : 3rd number
*
*  @param[in] i2_n4 : 4th number (order does not matter)
*
*  @return range of the number
********************************************************************************
*/
S16 median4_s16(S16 i2_n1, S16 i2_n2, S16 i2_n3, S16 i2_n4)
{
    S16 i2_max, i2_min;

    i2_max = MAX(i2_n1, i2_n2);
    i2_max = MAX(i2_max, i2_n3);
    i2_max = MAX(i2_max, i2_n4);

    i2_min = MIN(i2_n1, i2_n2);
    i2_min = MIN(i2_min, i2_n3);
    i2_min = MIN(i2_min, i2_n4);

    return ((S16)((i2_n1 + i2_n2 + i2_n3 + i2_n4 - i2_max - i2_min) >> 1));
}

U32 hme_compute_2d_sum_u08(U08 *pu1_inp, S32 i4_wd, S32 i4_ht, S32 i4_stride)
{
    S32 i, j;
    U32 u4_sum = 0;

    for(i = 0; i < i4_ht; i++)
    {
        for(j = 0; j < i4_wd; j++)
            u4_sum += (U32)pu1_inp[j];

        pu1_inp += i4_stride;
    }

    return (u4_sum);
}
U32 hme_compute_2d_sum_u16(U16 *pu2_inp, S32 i4_wd, S32 i4_ht, S32 i4_stride)
{
    S32 i, j;
    U32 u4_sum = 0;

    for(i = 0; i < i4_ht; i++)
    {
        for(j = 0; j < i4_wd; j++)
            u4_sum += (U32)pu2_inp[j];

        pu2_inp += i4_stride;
    }

    return (u4_sum);
}
U32 hme_compute_2d_sum_u32(U32 *pu4_inp, S32 i4_wd, S32 i4_ht, S32 i4_stride)
{
    S32 i, j;
    U32 u4_sum = 0;

    for(i = 0; i < i4_ht; i++)
    {
        for(j = 0; j < i4_wd; j++)
            u4_sum += (U32)pu4_inp[j];

        pu4_inp += i4_stride;
    }

    return (u4_sum);
}
/**
********************************************************************************
*  @fn     S32 hme_compute_2d_sum_unsigned(void *pv_inp,
*                                       S32 i4_blk_wd,
*                                       S32 i4_blk_ht,
*                                   S32 i4_stride,
*                                   S32 i4_datatype)
*
*  @brief  Computes and returns 2D sum of a unsigned 2d buffer, with datatype
*          equal to 8/16/32 bit.
*
*  @param[in] pv_inp : input pointer
*
*  @param[in] i4_blk_wd : block width
*
*  @param[in] i4_blk_ht : block ht
*
*  @param[in] i4_stride : stride
*
*  @param[in] i4_datatype : datatype 1 - 8 bit, 2 - 16 bit, 4 - 32 bit
*
*  @return sum of i4_blk_wd * i4_blk_ht number of entries starting at pv_inp
********************************************************************************
*/

U32 hme_compute_2d_sum_unsigned(
    void *pv_inp, S32 i4_blk_wd, S32 i4_blk_ht, S32 i4_stride, S32 i4_datatype)
{
    if(i4_datatype == sizeof(U08))
        return (hme_compute_2d_sum_u08((U08 *)pv_inp, i4_blk_wd, i4_blk_ht, i4_stride));
    else if(i4_datatype == sizeof(U16))
        return (hme_compute_2d_sum_u16((U16 *)pv_inp, i4_blk_wd, i4_blk_ht, i4_stride));
    else if(i4_datatype == sizeof(U32))
        return (hme_compute_2d_sum_u32((U32 *)pv_inp, i4_blk_wd, i4_blk_ht, i4_stride));
    else
        ASSERT(0);

    return 0;
}

/**
********************************************************************************
*  @fn     S32 get_rand_num(S32 low, S32 high)
*
*  @brief  returns a radom integer in the closed interval [low, high - 1]
*
*  @param[in] low : lower limit
*
*  @param[in] high : higher limit
*
*  @return S32 result: the random number
********************************************************************************
*/
S32 get_rand_num(S32 low, S32 high)
{
    double num;
    S32 result;
    num = (double)rand() / (double)RAND_MAX;
    num = num * (high - low) + low;

    result = (S32)floor((num + 0.5));
    if(result < low)
        result = low;
    if(result >= high)
        result = high - 1;

    return (result);
}