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 #include <stdio.h>
21 #include "iv_datatypedef.h"
22 #include "iv.h"
23 #include "impeg2_buf_mgr.h"
24 #include "impeg2_disp_mgr.h"
25 #include "impeg2_defs.h"
26 #include "impeg2_platform_macros.h"
27 #include "impeg2_globals.h"
28 
29 /* Table for converting the quantizer_scale_code to quantizer_scale */
30 const UWORD8 gau1_impeg2_non_linear_quant_scale[] =
31 {
32     0, 1, 2, 3, 4, 5, 6, 7,
33     8,10,12,14,16,18,20,22,
34     24,28,32,36,40,44,48,52,
35     56,64,72,80,88,96,104,112
36 };
37 
38 
39 /* Default quantizer matrix to be used for intra blocks */
40 const UWORD8 gau1_impeg2_intra_quant_matrix_default[] =
41 {
42     8, 16, 19, 22, 26, 27, 29, 34,
43     16, 16, 22, 24, 27, 29, 34, 37,
44     19, 22, 26, 27, 29, 34, 34, 38,
45     22, 22, 26, 27, 29, 34, 37, 40,
46     22, 26, 27, 29, 32, 35, 40, 48,
47     26, 27, 29, 32, 35, 40, 48, 58,
48     26, 27, 29, 34, 38, 46, 56, 69,
49     27, 29, 35, 38, 46, 56, 69, 83
50 };
51 
52 /* Default quantizer matrix to be used for inter blocks */
53 const UWORD8 gau1_impeg2_inter_quant_matrix_default[] =
54 {
55     16,16,16,16,16,16,16,16,
56     16,16,16,16,16,16,16,16,
57     16,16,16,16,16,16,16,16,
58     16,16,16,16,16,16,16,16,
59     16,16,16,16,16,16,16,16,
60     16,16,16,16,16,16,16,16,
61     16,16,16,16,16,16,16,16,
62     16,16,16,16,16,16,16,16
63 };
64 
65 /* Table to perform inverse scan when the scan direction is zigzag */
66 const UWORD8 gau1_impeg2_inv_scan_zig_zag[] =
67 {
68      0,  1,  8, 16,  9,  2,  3, 10,
69     17, 24, 32, 25, 18, 11,  4,  5,
70     12, 19, 26, 33, 40, 48, 41, 34,
71     27, 20, 13,  6,  7, 14, 21, 28,
72     35, 42, 49, 56, 57, 50, 43, 36,
73     29, 22, 15, 23, 30, 37, 44, 51,
74     58, 59, 52, 45, 38, 31, 39, 46,
75     53, 60, 61, 54, 47, 55, 62, 63
76 };
77 
78 /*  Table to perform inverse scan when the direction of scanning is vertical */
79 const UWORD8 gau1_impeg2_inv_scan_vertical[] =
80 {
81     0, 8, 16, 24, 1, 9, 2, 10,
82     17, 25, 32, 40, 48, 56, 57, 49,
83     41, 33, 26, 18, 3, 11, 4, 12,
84     19, 27, 34, 42, 50, 58, 35, 43,
85     51, 59, 20, 28, 5, 13, 6, 14,
86     21, 29, 36, 44, 52, 60, 37, 45,
87     53, 61, 22, 30, 7, 15, 23, 31,
88     38, 46, 54, 62, 39, 47, 55, 63
89 };
90 
91 /*****************************************************************************/
92 /* Table that indicate which interpolation  type is to used                  */
93 /*****************************************************************************/
94 /* Chroma when motion vector is positive */
95 const UWORD16 gau2_impeg2_chroma_interp_mv[][16] =
96 {
97     /* Pos X Pos Y */
98     {
99         0,  0,  1,  1,
100         0,  0,  1,  1,
101         2,  2,  3,  3,
102         2,  2,  3,  3
103     },
104     /* Neg X Pos Y */
105     {
106         0,  1,  1,  0,
107         0,  1,  1,  0,
108         2,  3,  3,  2,
109         2,  3,  3,  2
110     },
111     /* Pos X Neg Y */
112     {
113         0,  0,  1,  1,
114         2,  2,  3,  3,
115         2,  2,  3,  3,
116         0,  0,  1,  1
117     },
118     /* Neg X Neg Y */
119     {
120         0,  1,  1, 0,
121         2,  3,  3, 2,
122         2,  3,  3, 2,
123         0,  1,  1, 0
124     }
125 };
126 /*****************************************************************************/
127 /* Input #1 Offset in bytes                                                  */
128 /*****************************************************************************/
129 /* Chroma */
130 const UWORD16 gau2_impeg2_chroma_interp_inp1[][16] =
131 {
132     /* Pos X Pos Y */
133     {
134         0,  0,  0,  0,
135         0,  0,  0,  0,
136         0,  0,  0,  0,
137         0,  0,  0,  0
138     },
139     /* Neg X Pos Y */
140     {
141         0,  0,  0,  4,
142         0,  0,  0,  4,
143         0,  0,  0,  4,
144         0,  0,  0,  4
145     },
146     /* Pos X Neg Y */
147     {
148         0,  0,  0,  0,
149         0,  0,  0,  0,
150         0,  0,  0,  0,
151         72, 72, 72, 72
152     },
153     /* Neg X Neg Y */
154     {
155         0,  0,  0,  4,
156         0,  0,  0,  4,
157         0,  0,  0,  4,
158         72, 72, 72, 76
159     }
160 };
161 /* Luma */
162 const UWORD16 gau2_impeg2_luma_interp_inp1[] =
163 {
164     1,  1,  3,  3,
165     1,  1,  3,  3,
166     37, 37, 39, 39,
167     37, 37, 39, 39
168 };
169 /*****************************************************************************/
170 /* Input #2 Offset from Input #1 in bytes                                    */
171 /*****************************************************************************/
172 /*
173     FXFY  0,
174     HXFY  2,
175     FXHY 36,
176     HXHY 36
177 */
178 const UWORD16 gau2_impeg2_luma_interp_inp2[] =
179 {
180       0,  2,  0,  2,
181      36, 36, 36, 36,
182       0,  2,  0,  2,
183      36, 36, 36, 36
184 };
185 const UWORD16 gau2_impeg2_chroma_interp_inp2[] =
186 {
187     /* FXFY */
188     0,
189     /* HXFY */
190     4,
191     /* FXHY */
192     72,
193     /* HXHY */
194     72
195 };
196 
197 /*****************************************************************************/
198 /* Corresponds to Table 6-4 frame_rate_value  of the standard                */
199 /*****************************************************************************/
200 /*
201     frame_rate_code frame_rate_value
202 
203     0000            Forbidden
204     0001            24 000 � 1001
205     0010            24
206     0011            25
207     0100            30 000 � 1001
208     0101            30
209     0110            50
210     0111            60 000 � 1001
211     1000            60
212     1001            Reserved
213     ....
214     1111            Reserved
215 */
216 const UWORD16 gau2_impeg2_frm_rate_code[][2] =
217 {
218     {1    ,    1}, /* Forbidden */
219     {24000, 1001},
220     {24000, 1000},
221     {25000, 1000},
222     {30000, 1001},
223     {30000, 1000},
224     {50000, 1000},
225     {60000, 1001},
226     {60000, 1000}
227     /* Rest reserved */
228 };
229 
230 const WORD16 gai2_impeg2_idct_q15[] =
231 {
232     23170,    23170,    23170,    23170,    23170,    23170,    23170,    23170,
233     32138,    27246,    18205,     6393,    -6393,   -18205,   -27246,   -32138,
234     30274,    12540,   -12540,   -30274,   -30274,   -12540,    12540,    30274,
235     27246,    -6393,   -32138,   -18205,    18205,    32138,     6393,   -27246,
236     23170,   -23170,   -23170,    23170,    23170,   -23170,   -23170,    23170,
237     18205,   -32138,     6393,    27246,   -27246,    -6393,    32138,   -18205,
238     12540,   -30274,    30274,   -12540,   -12540,    30274,   -30274,    12540,
239      6393,   -18205,    27246,   -32138,    32138,   -27246,    18205,    -6393,
240 };
241 
242 const WORD16 gai2_impeg2_idct_q11[] =
243 {
244     1448,     1448,     1448,     1448,     1448,     1448,     1448,     1448,
245     2009,     1703,     1138,      400,     -400,    -1138,    -1703,    -2009,
246     1892,      784,     -784,    -1892,    -1892,     -784,      784,     1892,
247     1703,     -400,    -2009,    -1138,     1138,     2009,      400,    -1703,
248     1448,    -1448,    -1448,     1448,     1448,    -1448,    -1448,     1448,
249     1138,    -2009,      400,     1703,    -1703,     -400,     2009,    -1138,
250      784,    -1892,     1892,     -784,     -784,     1892,    -1892,      784,
251      400,    -1138,     1703,    -2009,     2009,    -1703,     1138,     -400,
252 };
253 
254 const WORD16 gai2_impeg2_idct_even_8_q15[][8] =
255 {
256     {  23170,  23170,  23170,  23170,  23170,  23170,  23170,  23170  },
257     {  12540, -30274,  12540, -30274,  12540, -30274,  12540, -30274  },
258     {  30274,  12540,  30274,  12540,  30274,  12540,  30274,  12540  },
259     {  23170, -23170,  23170, -23170,  23170, -23170,  23170, -23170  }
260 };
261 const WORD16 gai2_impeg2_idct_odd_8_q15[][8] =
262 {
263     {  32138,  27246,  32138,  27246,  32138,  27246,  32138,  27246 },
264     {  18205,   6393,  18205,   6393,  18205,   6393,  18205,   6393 },
265     {  27246,  -6393,  27246,  -6393,  27246,  -6393,  27246,  -6393 },
266     {  32138,  18205,  32138,  18205,  32138,  18205,  32138,  18205 },
267     {  18205, -32138,  18205, -32138,  18205, -32138,  18205, -32138 },
268     {  6393,   27246,   6393,  27246,   6393,  27246,   6393,  27246 },
269     {  6393,  -18205,   6393, -18205,   6393, -18205,   6393, -18205 },
270     {  27246, -32138,  27246, -32138,  27246, -32138,  27246, -32138 },
271 };
272 
273 const WORD16 gai2_impeg2_idct_even_8_q11[][8] =
274 {
275     {   1448,   1448,   1448,   1448,   1448,   1448,   1448,   1448 },
276     {    784,  -1892,    784,  -1892,    784,  -1892,    784,  -1892 },
277     {   1892,    784,   1892,    784,   1892,    784,   1892,    784 },
278     {   1448,  -1448,   1448,  -1448,   1448,  -1448,   1448,  -1448 }
279 };
280 const WORD16 gai2_impeg2_idct_odd_8_q11[][8] =
281 {
282     {   2009,   1703,   2009,   1703,   2009,   1703,   2009,   1703 },
283     {   1138,    400,   1138,    400,   1138,    400,   1138,    400 },
284     {   1703,   -400,   1703,   -400,   1703,   -400,   1703,   -400 },
285     {   2009,   1138,   2009,   1138,   2009,   1138,   2009,   1138 },
286     {   1138,  -2009,   1138,  -2009,   1138,  -2009,   1138,  -2009 },
287     {    400,   1703,    400,   1703,    400,   1703,    400,   1703 },
288     {    400,  -1138,    400,  -1138,    400,  -1138,    400,  -1138 },
289     {   1703,  -2009,   1703,  -2009,   1703,  -2009,   1703,  -2009 },
290 };
291 
292 
293 
294 /*****************************************************************************/
295 /* Last row IDCT Coefficients in Q11 format                                           */
296 /*****************************************************************************/
297 const WORD16 gai2_impeg2_idct_last_row_q11[] =
298 {
299      400,    -1138,     1703,    -2009,     2009,    -1703,     1138,     -400,
300 };
301 
302 const WORD16 gai2_impeg2_idct_first_col_q15[] =
303 {
304    23170,    32138,    30274,    27246,    23170,    18205,    12540,     6393,
305 };
306 
307 const WORD16 gai2_impeg2_idct_first_col_q11[] =
308 {
309      1448,    2009,     1892,     1703,     1448,     1138,      784,      400,
310 };
311 
312 /*****************************************************************************/
313 /* Output of first stage dct (using gai2_impeg2_idct_q15 as coeffs)          */
314 /* for a 1D data (0, 0, 0, 0, 0, 0, 0, 1)                                    */
315 /*****************************************************************************/
316 
317 const WORD16 gai2_impeg2_mismatch_stg1_outp[] =
318 {
319     2, -4, 7, -8, 8, -7, 4, -2
320 };
321 
322 const WORD16 gai2_impeg2_mismatch_stg2_additive[] =
323 {
324      800,   -2276,  3406,   -4018,  4018,   -3406,  2276,   -800,
325      -1600, 4552,   -6812,  8036,   -8036,  6812,   -4552,  1600,
326      2800,  -7966,  11921,  -14063, 14063,  -11921, 7966,   -2800,
327      -3200, 9104,   -13624, 16072,  -16072, 13624,  -9104,  3200,
328      3200,  -9104,  13624,  -16072, 16072,  -13624, 9104,   -3200,
329      -2800, 7966,   -11921, 14063,  -14063, 11921,  -7966,  2800,
330      1600,  -4552,  6812,   -8036,  8036,   -6812,  4552,   -1600,
331      -800,  2276,   -3406,  4018,   -4018,  3406,   -2276,  800,
332 };
333 
334 
335 const UWORD8 gau1_impeg2_zerobuf[] =
336 {
337     0, 0, 0, 0, 0, 0, 0, 0,
338     0, 0, 0, 0, 0, 0, 0, 0,
339     0, 0, 0, 0, 0, 0, 0, 0,
340     0, 0, 0, 0, 0, 0, 0, 0,
341     0, 0, 0, 0, 0, 0, 0, 0,
342     0, 0, 0, 0, 0, 0, 0, 0,
343     0, 0, 0, 0, 0, 0, 0, 0,
344     0, 0, 0, 0, 0, 0, 0, 0,
345 };
346 /*****************************************************************************/
347 /* Tables of offset needed to address block in an MB                         */
348 /*****************************************************************************/
349 const WORD16  gai2_impeg2_blk_y_off_fld[]  = {0,0,1,1};
350 const WORD16  gai2_impeg2_blk_y_off_frm[]  = {0,0,8,8};
351 const WORD16  gai2_impeg2_blk_x_off[]      = {0,8,0,8};
352