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