xref: /aosp_15_r20/external/libavc/common/ih264_trans_macros.h (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1 /******************************************************************************
2  *
3  * Copyright (C) 2015 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 *******************************************************************************
23 * @file
24 *  ih264_trans_macros.h
25 *
26 * @brief
27 *  The file contains definitions of macros that perform forward and inverse
28 *  quantization
29 *
30 * @author
31 *  ittiam
32 *
33 * @remark
34 *  none
35 *
36 *******************************************************************************
37 */
38 
39 #ifndef _IH264_TRANS_MACROS_H_
40 #define _IH264_TRANS_MACROS_H_
41 
42 /*****************************************************************************/
43 /* Function Macros                                                           */
44 /*****************************************************************************/
45 
46 /**
47 ******************************************************************************
48  *  @brief   Macro to perform forward quantization.
49  *  @description The value to be quantized is first compared with a threshold.
50  *  If the value is less than the threshold, the quantization value is returned
51  *  as zero else the value is quantized traditionally as per the rules of
52  *  h264 specification
53 ******************************************************************************
54  */
55 #define FWD_QUANT(i4_value, threshold, scale, rndfactor, qbits, u4_nnz)      \
56                 {\
57                         WORD32 i4_sign;\
58                         UWORD32 u4_abs_value;\
59                         if (i4_value < 0)\
60                         {\
61                             u4_abs_value = -i4_value;\
62                             i4_sign = -1;\
63                         }\
64                         else\
65                         {\
66                             u4_abs_value = i4_value;\
67                             i4_sign = 1;\
68                         }\
69                         if (u4_abs_value < threshold)\
70                         {\
71                             i4_value = 0;\
72                         }\
73                         else\
74                         {\
75                             u4_abs_value *= scale;\
76                             u4_abs_value += rndfactor;\
77                             u4_abs_value >>= qbits;\
78                             i4_value = u4_abs_value;\
79                             if (i4_sign == -1) i4_value = -i4_value;\
80                             if (i4_value)\
81                             {\
82                                 u4_nnz++;\
83                             }\
84                         }\
85                 }
86 
87 /**
88 ******************************************************************************
89  *  @brief   Macro to perform inverse quantization.
90  *  @remarks The value can also be de-quantized as
91  *  if (u4_qp_div_6 < 4)
92  *  {
93  *      i4_value = (quant_scale * weight_scale * i4_value + (1 << (3-u4_qp_div_6)))
94  *      i4_value >>= (4 - u4_qp_div_6)
95  *  }
96  *  else
97  *  {
98  *      i4_value = (quant_scale * weight_scale * i4_value) << (u4_qp_div_6 -4)
99  *  }
100 ******************************************************************************
101  */
102 #define INV_QUANT(i4_value, quant_scale, weight_scale, u4_qp_div_6, rndfactor, qbits)\
103                 {\
104                     i4_value *= quant_scale;\
105                     i4_value *= weight_scale;\
106                     i4_value += rndfactor;\
107                     i4_value <<= u4_qp_div_6;\
108                     i4_value >>= qbits;\
109                 }
110 
111 #define QUANT_H264(x,y,w,z,shft) (shft = ABS(x),\
112                 shft *= y,\
113                 shft += z,\
114                 shft = shft>>w,\
115                 shft = SIGNXY(shft,x))
116 
117 #define IQUANT_H264(x,y,wscal,w,shft) (shft = x, \
118                 shft *=y, \
119                 shft *=wscal, \
120                 shft = shft<<w)
121 
122 #define IQUANT_lev_H264(x,y,wscal,add_f,w,shft) (shft = x, \
123                 shft *=y, \
124                 shft *=wscal, \
125                 shft+= add_f, \
126                 shft = shft>>w)
127 
128 #endif /* _IH264_TRANS_MACROS_H_ */
129