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_ihadamard_scaling.c
24 *
25 * @brief
26 *  Contains definition of functions for h264 inverse hadamard 4x4 transform and scaling
27 *
28 * @author
29 *  Mohit
30 *
31 *  @par List of Functions:
32 *  - ih264_ihadamard_scaling_4x4()
33 *
34 * @remarks
35 *
36 *******************************************************************************
37 */
38
39/*****************************************************************************/
40/* File Includes                                                             */
41/*****************************************************************************/
42
43/* User include files */
44#include "ih264_typedefs.h"
45#include "ih264_defs.h"
46#include "ih264_trans_macros.h"
47#include "ih264_macros.h"
48#include "ih264_trans_data.h"
49#include "ih264_size_defs.h"
50#include "ih264_structs.h"
51#include "ih264_trans_quant_itrans_iquant.h"
52
53/*
54 ********************************************************************************
55 *
56 * @brief This function performs a 4x4 inverse hadamard transform on the 4x4 DC coefficients
57 * of a 16x16 intra prediction macroblock, and then performs scaling.
58 * prediction buffer
59 *
60 * @par Description:
61 *  The DC coefficients pass through a 2-stage inverse hadamard transform.
62 *  This inverse transformed content is scaled to based on Qp value.
63 *
64 * @param[in] pi2_src
65 *  input 4x4 block of DC coefficients
66 *
67 * @param[out] pi2_out
68 *  output 4x4 block
69 *
70 * @param[in] pu2_iscal_mat
71 *  pointer to scaling list
72 *
73 * @param[in] pu2_weigh_mat
74 *  pointer to weight matrix
75 *
76 * @param[in] u4_qp_div_6
77 *  Floor (qp/6)
78 *
79 * @param[in] pi4_tmp
80 * temporary buffer of size 1*16
81 *
82 * @returns none
83 *
84 * @remarks none
85 *
86 *******************************************************************************
87 */
88void ih264_ihadamard_scaling_4x4(WORD16* pi2_src,
89                                 WORD16* pi2_out,
90                                 const UWORD16 *pu2_iscal_mat,
91                                 const UWORD16 *pu2_weigh_mat,
92                                 UWORD32 u4_qp_div_6,
93                                 WORD32* pi4_tmp)
94{
95    WORD32 i;
96    WORD32 x0, x1, x2, x3, x4, x5, x6, x7;
97    WORD16* pi2_src_ptr, *pi2_out_ptr;
98    WORD32* pi4_tmp_ptr;
99    WORD32 rnd_fact = (u4_qp_div_6 < 6) ? (1 << (5 - u4_qp_div_6)) : 0;
100    pi4_tmp_ptr = pi4_tmp;
101    pi2_src_ptr = pi2_src;
102    pi2_out_ptr = pi2_out;
103    // Horizontal transform
104    for(i = 0; i < SUB_BLK_WIDTH_4x4; i++)
105    {
106        x4 = pi2_src_ptr[0];
107        x5 = pi2_src_ptr[1];
108        x6 = pi2_src_ptr[2];
109        x7 = pi2_src_ptr[3];
110
111        x0 = x4 + x7;
112        x1 = x5 + x6;
113        x2 = x5 - x6;
114        x3 = x4 - x7;
115
116        pi4_tmp_ptr[0] = x0 + x1;
117        pi4_tmp_ptr[1] = x2 + x3;
118        pi4_tmp_ptr[2] = x0 - x1;
119        pi4_tmp_ptr[3] = x3 - x2;
120
121        pi4_tmp_ptr += SUB_BLK_WIDTH_4x4;
122        pi2_src_ptr += SUB_BLK_WIDTH_4x4;
123    }
124    pi4_tmp_ptr = pi4_tmp;
125    // Vertical Transform
126    for(i = 0; i < SUB_BLK_WIDTH_4x4; i++)
127    {
128        x4 = pi4_tmp_ptr[0];
129        x5 = pi4_tmp_ptr[4];
130        x6 = pi4_tmp_ptr[8];
131        x7 = pi4_tmp_ptr[12];
132
133        x0 = x4 + x7;
134        x1 = x5 + x6;
135        x2 = x5 - x6;
136        x3 = x4 - x7;
137
138        pi4_tmp_ptr[0] = x0 + x1;
139        pi4_tmp_ptr[4] = x2 + x3;
140        pi4_tmp_ptr[8] = x0 - x1;
141        pi4_tmp_ptr[12] = x3 - x2;
142
143        pi4_tmp_ptr++;
144    }
145    pi4_tmp_ptr = pi4_tmp;
146    //Scaling
147    for(i = 0; i < (SUB_BLK_WIDTH_4x4 * SUB_BLK_WIDTH_4x4); i++)
148    {
149      INV_QUANT(pi4_tmp_ptr[i], pu2_iscal_mat[0], pu2_weigh_mat[0], u4_qp_div_6,
150                rnd_fact, 6);
151      pi2_out_ptr[i] = pi4_tmp_ptr[i];
152    }
153}
154
155void ih264_ihadamard_scaling_2x2_uv(WORD16* pi2_src,
156                                    WORD16* pi2_out,
157                                    const UWORD16 *pu2_iscal_mat,
158                                    const UWORD16 *pu2_weigh_mat,
159                                    UWORD32 u4_qp_div_6,
160                                    WORD32* pi4_tmp)
161{
162  WORD32 i4_x0,i4_x1,i4_x2,i4_x3,i4_x4,i4_x5,i4_x6,i4_x7;
163  WORD32 i4_y0,i4_y1,i4_y2,i4_y3,i4_y4,i4_y5,i4_y6,i4_y7;
164
165  UNUSED(pi4_tmp);
166
167  i4_x4 = pi2_src[0];
168  i4_x5 = pi2_src[1];
169  i4_x6 = pi2_src[2];
170  i4_x7 = pi2_src[3];
171
172  i4_x0 = i4_x4 + i4_x5;
173  i4_x1 = i4_x4 - i4_x5;
174  i4_x2 = i4_x6 + i4_x7;
175  i4_x3 = i4_x6 - i4_x7;
176
177  i4_x4 = i4_x0+i4_x2;
178  i4_x5 = i4_x1+i4_x3;
179  i4_x6 = i4_x0-i4_x2;
180  i4_x7 = i4_x1-i4_x3;
181
182  INV_QUANT(i4_x4,pu2_iscal_mat[0],pu2_weigh_mat[0],u4_qp_div_6,0,5);
183  INV_QUANT(i4_x5,pu2_iscal_mat[0],pu2_weigh_mat[0],u4_qp_div_6,0,5);
184  INV_QUANT(i4_x6,pu2_iscal_mat[0],pu2_weigh_mat[0],u4_qp_div_6,0,5);
185  INV_QUANT(i4_x7,pu2_iscal_mat[0],pu2_weigh_mat[0],u4_qp_div_6,0,5);
186
187  pi2_out[0] = i4_x4;
188  pi2_out[1] = i4_x5;
189  pi2_out[2] = i4_x6;
190  pi2_out[3] = i4_x7;
191
192  i4_y4 = pi2_src[4];
193  i4_y5 = pi2_src[5];
194  i4_y6 = pi2_src[6];
195  i4_y7 = pi2_src[7];
196
197  i4_y0 = i4_y4 + i4_y5;
198  i4_y1 = i4_y4 - i4_y5;
199  i4_y2 = i4_y6 + i4_y7;
200  i4_y3 = i4_y6 - i4_y7;
201
202  i4_y4 = i4_y0+i4_y2;
203  i4_y5 = i4_y1+i4_y3;
204  i4_y6 = i4_y0-i4_y2;
205  i4_y7 = i4_y1-i4_y3;
206
207  INV_QUANT(i4_y4,pu2_iscal_mat[0],pu2_weigh_mat[0],u4_qp_div_6,0,5);
208  INV_QUANT(i4_y5,pu2_iscal_mat[0],pu2_weigh_mat[0],u4_qp_div_6,0,5);
209  INV_QUANT(i4_y6,pu2_iscal_mat[0],pu2_weigh_mat[0],u4_qp_div_6,0,5);
210  INV_QUANT(i4_y7,pu2_iscal_mat[0],pu2_weigh_mat[0],u4_qp_div_6,0,5);
211
212  pi2_out[4] = i4_y4;
213  pi2_out[5] = i4_y5;
214  pi2_out[6] = i4_y6;
215  pi2_out[7] = i4_y7;
216}
217