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 "ih264_typedefs.h"
21 #include "ih264_macros.h"
22 #include "ih264_platform_macros.h"
23 #include "ih264_defs.h"
24 #include "ih264d_bitstrm.h"
25 #include "ih264d_structs.h"
26 #include "ih264d_parse_cavlc.h"
27 #include "ih264d_defs.h"
28 #include "ih264d_defs.h"
29 #include "ih264d_defs.h"
30 
31 #include "ih264d_parse_slice.h"
32 #include "ih264d_tables.h"
33 #include "ih264d_utils.h"
34 #include "ih264d_nal.h"
35 #include "ih264d_deblocking.h"
36 
37 #include "ih264d_mem_request.h"
38 #include "ih264d_debug.h"
39 
40 #include "ih264d_error_handler.h"
41 #include "ih264d_mb_utils.h"
42 #include "ih264d_sei.h"
43 #include "ih264d_vui.h"
44 #include "ih264d_tables.h"
45 
46 #define IDCT_BLOCK_WIDTH8X8  8
47 
ih264d_scaling_list(WORD16 * pi2_scaling_list,WORD32 i4_size_of_scalinglist,UWORD8 * pu1_use_default_scaling_matrix_flag,dec_bit_stream_t * ps_bitstrm)48 WORD32 ih264d_scaling_list(WORD16 *pi2_scaling_list,
49                          WORD32 i4_size_of_scalinglist,
50                          UWORD8 *pu1_use_default_scaling_matrix_flag,
51                          dec_bit_stream_t *ps_bitstrm)
52 {
53     WORD32 i4_j, i4_delta_scale, i4_lastScale = 8, i4_nextScale = 8;
54     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
55     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
56 
57     *pu1_use_default_scaling_matrix_flag = 0;
58 
59     for(i4_j = 0; i4_j < i4_size_of_scalinglist; i4_j++)
60     {
61         if(i4_nextScale != 0)
62         {
63             i4_delta_scale = ih264d_sev(pu4_bitstrm_ofst,
64                                         pu4_bitstrm_buf);
65 
66             if(i4_delta_scale < MIN_H264_DELTA_SCALE ||
67                         i4_delta_scale > MAX_H264_DELTA_SCALE)
68             {
69                 return ERROR_INV_RANGE_QP_T;
70             }
71             i4_nextScale = ((i4_lastScale + i4_delta_scale + 256) & 0xff);
72 
73             *pu1_use_default_scaling_matrix_flag = ((i4_j == 0)
74                             && (i4_nextScale == 0));
75 
76         }
77         pi2_scaling_list[i4_j] =
78                         (i4_nextScale == 0) ? (i4_lastScale) : (i4_nextScale);
79         i4_lastScale = pi2_scaling_list[i4_j];
80     }
81     return OK;
82 }
83 
ih264d_form_default_scaling_matrix(dec_struct_t * ps_dec)84 WORD32 ih264d_form_default_scaling_matrix(dec_struct_t *ps_dec)
85 {
86 
87     /*************************************************************************/
88     /* perform the inverse scanning for the frame and field scaling matrices */
89     /*************************************************************************/
90     {
91         UWORD8 *pu1_inv_scan;
92         WORD32 i4_i, i4_j;
93 
94         pu1_inv_scan = (UWORD8 *)gau1_ih264d_inv_scan;
95 
96         /* for all 4x4 matrices */
97         for(i4_i = 0; i4_i < 6; i4_i++)
98         {
99             for(i4_j = 0; i4_j < 16; i4_j++)
100             {
101                 ps_dec->s_high_profile.i2_scalinglist4x4[i4_i][pu1_inv_scan[i4_j]] =
102                                 16;
103 
104             }
105         }
106 
107         /* for all 8x8 matrices */
108         for(i4_i = 0; i4_i < 2; i4_i++)
109         {
110             for(i4_j = 0; i4_j < 64; i4_j++)
111             {
112                 ps_dec->s_high_profile.i2_scalinglist8x8[i4_i][gau1_ih264d_inv_scan_prog8x8_cabac[i4_j]] =
113                                 16;
114 
115             }
116         }
117     }
118     return OK;
119 }
120 
ih264d_form_scaling_matrix_picture(dec_seq_params_t * ps_seq,dec_pic_params_t * ps_pic,dec_struct_t * ps_dec)121 WORD32 ih264d_form_scaling_matrix_picture(dec_seq_params_t *ps_seq,
122                                           dec_pic_params_t *ps_pic,
123                                           dec_struct_t *ps_dec)
124 {
125     /* default scaling matrices */
126     WORD32 i4_i;
127 
128     /* check the SPS first */
129     if(ps_seq->i4_seq_scaling_matrix_present_flag)
130     {
131         for(i4_i = 0; i4_i < 8; i4_i++)
132         {
133             if(i4_i < 6)
134             {
135                 /* fall-back rule A */
136                 if(!ps_seq->u1_seq_scaling_list_present_flag[i4_i])
137                 {
138                     if((i4_i == 0) || (i4_i == 3))
139                     {
140                         ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
141                                         (i4_i == 0) ? (WORD16 *)(gai2_ih264d_default_intra4x4) : (WORD16 *)(gai2_ih264d_default_inter4x4);
142                     }
143                     else
144                     {
145                         ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
146                                         ps_dec->s_high_profile.pi2_scale_mat[i4_i
147                                                         - 1];
148                     }
149                 }
150                 else
151                 {
152                     if(ps_seq->u1_use_default_scaling_matrix_flag[i4_i])
153                     {
154                         ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
155                                         (i4_i < 3) ? (WORD16 *)(gai2_ih264d_default_intra4x4) : (WORD16 *)(gai2_ih264d_default_inter4x4);
156                     }
157                     else
158                     {
159                         ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
160                                         ps_seq->i2_scalinglist4x4[i4_i];
161                     }
162                 }
163 
164             }
165             else
166             {
167                 /* fall-back rule A */
168                 if((!ps_seq->u1_seq_scaling_list_present_flag[i4_i])
169                                 || (ps_seq->u1_use_default_scaling_matrix_flag[i4_i]))
170                 {
171                     ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
172                                     (i4_i == 6) ? ((WORD16*)gai2_ih264d_default_intra8x8) : ((WORD16*)gai2_ih264d_default_inter8x8);
173                 }
174                 else
175                 {
176                     ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
177                                     ps_seq->i2_scalinglist8x8[i4_i - 6];
178                 }
179             }
180         }
181     }
182 
183     /* checking for the PPS */
184 
185     if(ps_pic->i4_pic_scaling_matrix_present_flag)
186     {
187         for(i4_i = 0; i4_i < 8; i4_i++)
188         {
189             if(i4_i < 6)
190             {
191                 /* fall back rule B */
192                 if(!ps_pic->u1_pic_scaling_list_present_flag[i4_i])
193                 {
194                     if((i4_i == 0) || (i4_i == 3))
195                     {
196                         if(!ps_seq->i4_seq_scaling_matrix_present_flag)
197                         {
198                             ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
199                                             (i4_i == 0) ? (WORD16 *)(gai2_ih264d_default_intra4x4) : (WORD16 *)(gai2_ih264d_default_inter4x4);
200                         }
201                     }
202                     else
203                     {
204                         ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
205                                         ps_dec->s_high_profile.pi2_scale_mat[i4_i
206                                                         - 1];
207                     }
208                 }
209                 else
210                 {
211                     if(ps_pic->u1_pic_use_default_scaling_matrix_flag[i4_i])
212                     {
213                         ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
214                                         (i4_i < 3) ? (WORD16 *)(gai2_ih264d_default_intra4x4) : (WORD16 *)(gai2_ih264d_default_inter4x4);
215                     }
216                     else
217                     {
218                         ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
219                                         ps_pic->i2_pic_scalinglist4x4[i4_i];
220                     }
221                 }
222             }
223             else
224             {
225                 if(!ps_pic->u1_pic_scaling_list_present_flag[i4_i])
226                 {
227                     if(!ps_seq->i4_seq_scaling_matrix_present_flag)
228                     {
229                         ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
230                                         (i4_i == 6) ? ((WORD16*)gai2_ih264d_default_intra8x8) : ((WORD16*)gai2_ih264d_default_inter8x8);
231                     }
232                 }
233                 else
234                 {
235                     if(ps_pic->u1_pic_use_default_scaling_matrix_flag[i4_i])
236                     {
237                         ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
238                                         (i4_i == 6) ? (WORD16 *)(gai2_ih264d_default_intra8x8) : (WORD16 *)(gai2_ih264d_default_inter8x8);
239                     }
240                     else
241                     {
242                         ps_dec->s_high_profile.pi2_scale_mat[i4_i] =
243                                         ps_pic->i2_pic_scalinglist8x8[i4_i - 6];
244                     }
245                 }
246             }
247         }
248     }
249 
250     /*************************************************************************/
251     /* perform the inverse scanning for the frame and field scaling matrices */
252     /*************************************************************************/
253     {
254         UWORD8 *pu1_inv_scan_4x4;
255         WORD32 i4_i, i4_j;
256 
257         pu1_inv_scan_4x4 = (UWORD8 *)gau1_ih264d_inv_scan;
258 
259         /* for all 4x4 matrices */
260         for(i4_i = 0; i4_i < 6; i4_i++)
261         {
262             if(ps_dec->s_high_profile.pi2_scale_mat[i4_i] == NULL)
263                 return ERROR_CORRUPTED_SLICE;
264 
265             for(i4_j = 0; i4_j < 16; i4_j++)
266             {
267                 ps_dec->s_high_profile.i2_scalinglist4x4[i4_i][pu1_inv_scan_4x4[i4_j]] =
268                                 ps_dec->s_high_profile.pi2_scale_mat[i4_i][i4_j];
269 
270             }
271         }
272 
273         /* for all 8x8 matrices */
274         for(i4_i = 0; i4_i < 2; i4_i++)
275         {
276             if(ps_dec->s_high_profile.pi2_scale_mat[i4_i + 6] == NULL)
277                 return ERROR_CORRUPTED_SLICE;
278 
279             for(i4_j = 0; i4_j < 64; i4_j++)
280             {
281                 ps_dec->s_high_profile.i2_scalinglist8x8[i4_i][gau1_ih264d_inv_scan_prog8x8_cabac[i4_j]] =
282                                 ps_dec->s_high_profile.pi2_scale_mat[i4_i + 6][i4_j];
283 
284             }
285         }
286     }
287     return OK;
288 }
289 
290