/src/libavc/decoder/ih264d_quant_scaling.c
Line | Count | Source |
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 | | |
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 | 24.7k | { |
53 | 24.7k | WORD32 i4_j, i4_delta_scale, i4_lastScale = 8, i4_nextScale = 8; |
54 | 24.7k | UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer; |
55 | 24.7k | UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst; |
56 | | |
57 | 24.7k | *pu1_use_default_scaling_matrix_flag = 0; |
58 | | |
59 | 695k | for(i4_j = 0; i4_j < i4_size_of_scalinglist; i4_j++) |
60 | 671k | { |
61 | 671k | if(i4_nextScale != 0) |
62 | 309k | { |
63 | 309k | i4_delta_scale = ih264d_sev(pu4_bitstrm_ofst, |
64 | 309k | pu4_bitstrm_buf); |
65 | | |
66 | 309k | if(i4_delta_scale < MIN_H264_DELTA_SCALE || |
67 | 309k | i4_delta_scale > MAX_H264_DELTA_SCALE) |
68 | 1.11k | { |
69 | 1.11k | return ERROR_INV_RANGE_QP_T; |
70 | 1.11k | } |
71 | 308k | i4_nextScale = ((i4_lastScale + i4_delta_scale + 256) & 0xff); |
72 | | |
73 | 308k | *pu1_use_default_scaling_matrix_flag = ((i4_j == 0) |
74 | 24.5k | && (i4_nextScale == 0)); |
75 | | |
76 | 308k | } |
77 | 670k | pi2_scaling_list[i4_j] = |
78 | 670k | (i4_nextScale == 0) ? (i4_lastScale) : (i4_nextScale); |
79 | 670k | i4_lastScale = pi2_scaling_list[i4_j]; |
80 | 670k | } |
81 | 23.6k | return OK; |
82 | 24.7k | } |
83 | | |
84 | | WORD32 ih264d_form_default_scaling_matrix(dec_struct_t *ps_dec) |
85 | 217k | { |
86 | | |
87 | | /*************************************************************************/ |
88 | | /* perform the inverse scanning for the frame and field scaling matrices */ |
89 | | /*************************************************************************/ |
90 | 217k | { |
91 | 217k | UWORD8 *pu1_inv_scan; |
92 | 217k | WORD32 i4_i, i4_j; |
93 | | |
94 | 217k | pu1_inv_scan = (UWORD8 *)gau1_ih264d_inv_scan; |
95 | | |
96 | | /* for all 4x4 matrices */ |
97 | 1.52M | for(i4_i = 0; i4_i < 6; i4_i++) |
98 | 1.30M | { |
99 | 22.1M | for(i4_j = 0; i4_j < 16; i4_j++) |
100 | 20.8M | { |
101 | 20.8M | ps_dec->s_high_profile.i2_scalinglist4x4[i4_i][pu1_inv_scan[i4_j]] = |
102 | 20.8M | 16; |
103 | | |
104 | 20.8M | } |
105 | 1.30M | } |
106 | | |
107 | | /* for all 8x8 matrices */ |
108 | 651k | for(i4_i = 0; i4_i < 2; i4_i++) |
109 | 434k | { |
110 | 28.2M | for(i4_j = 0; i4_j < 64; i4_j++) |
111 | 27.7M | { |
112 | 27.7M | ps_dec->s_high_profile.i2_scalinglist8x8[i4_i][gau1_ih264d_inv_scan_prog8x8_cabac[i4_j]] = |
113 | 27.7M | 16; |
114 | | |
115 | 27.7M | } |
116 | 434k | } |
117 | 217k | } |
118 | 217k | return OK; |
119 | 217k | } |
120 | | |
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 | 17.1k | { |
125 | | /* default scaling matrices */ |
126 | 17.1k | WORD32 i4_i; |
127 | | |
128 | | /* check the SPS first */ |
129 | 17.1k | if(ps_seq->i4_seq_scaling_matrix_present_flag) |
130 | 13.9k | { |
131 | 125k | for(i4_i = 0; i4_i < 8; i4_i++) |
132 | 111k | { |
133 | 111k | if(i4_i < 6) |
134 | 83.6k | { |
135 | | /* fall-back rule A */ |
136 | 83.6k | if(!ps_seq->u1_seq_scaling_list_present_flag[i4_i]) |
137 | 49.3k | { |
138 | 49.3k | if((i4_i == 0) || (i4_i == 3)) |
139 | 18.1k | { |
140 | 18.1k | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
141 | 18.1k | (i4_i == 0) ? (WORD16 *)(gai2_ih264d_default_intra4x4) : (WORD16 *)(gai2_ih264d_default_inter4x4); |
142 | 18.1k | } |
143 | 31.1k | else |
144 | 31.1k | { |
145 | 31.1k | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
146 | 31.1k | ps_dec->s_high_profile.pi2_scale_mat[i4_i |
147 | 31.1k | - 1]; |
148 | 31.1k | } |
149 | 49.3k | } |
150 | 34.3k | else |
151 | 34.3k | { |
152 | 34.3k | if(ps_seq->u1_use_default_scaling_matrix_flag[i4_i]) |
153 | 734 | { |
154 | 734 | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
155 | 734 | (i4_i < 3) ? (WORD16 *)(gai2_ih264d_default_intra4x4) : (WORD16 *)(gai2_ih264d_default_inter4x4); |
156 | 734 | } |
157 | 33.5k | else |
158 | 33.5k | { |
159 | 33.5k | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
160 | 33.5k | ps_seq->i2_scalinglist4x4[i4_i]; |
161 | 33.5k | } |
162 | 34.3k | } |
163 | | |
164 | 83.6k | } |
165 | 27.8k | else |
166 | 27.8k | { |
167 | | /* fall-back rule A */ |
168 | 27.8k | if((!ps_seq->u1_seq_scaling_list_present_flag[i4_i]) |
169 | 12.8k | || (ps_seq->u1_use_default_scaling_matrix_flag[i4_i])) |
170 | 15.8k | { |
171 | 15.8k | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
172 | 15.8k | (i4_i == 6) ? ((WORD16*)gai2_ih264d_default_intra8x8) : ((WORD16*)gai2_ih264d_default_inter8x8); |
173 | 15.8k | } |
174 | 12.0k | else |
175 | 12.0k | { |
176 | 12.0k | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
177 | 12.0k | ps_seq->i2_scalinglist8x8[i4_i - 6]; |
178 | 12.0k | } |
179 | 27.8k | } |
180 | 111k | } |
181 | 13.9k | } |
182 | | |
183 | | /* checking for the PPS */ |
184 | | |
185 | 17.1k | if(ps_pic->i4_pic_scaling_matrix_present_flag) |
186 | 7.28k | { |
187 | 65.5k | for(i4_i = 0; i4_i < 8; i4_i++) |
188 | 58.2k | { |
189 | 58.2k | if(i4_i < 6) |
190 | 43.7k | { |
191 | | /* fall back rule B */ |
192 | 43.7k | if(!ps_pic->u1_pic_scaling_list_present_flag[i4_i]) |
193 | 33.8k | { |
194 | 33.8k | if((i4_i == 0) || (i4_i == 3)) |
195 | 11.1k | { |
196 | 11.1k | if(!ps_seq->i4_seq_scaling_matrix_present_flag) |
197 | 3.54k | { |
198 | 3.54k | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
199 | 3.54k | (i4_i == 0) ? (WORD16 *)(gai2_ih264d_default_intra4x4) : (WORD16 *)(gai2_ih264d_default_inter4x4); |
200 | 3.54k | } |
201 | 11.1k | } |
202 | 22.6k | else |
203 | 22.6k | { |
204 | 22.6k | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
205 | 22.6k | ps_dec->s_high_profile.pi2_scale_mat[i4_i |
206 | 22.6k | - 1]; |
207 | 22.6k | } |
208 | 33.8k | } |
209 | 9.87k | else |
210 | 9.87k | { |
211 | 9.87k | if(ps_pic->u1_pic_use_default_scaling_matrix_flag[i4_i]) |
212 | 523 | { |
213 | 523 | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
214 | 523 | (i4_i < 3) ? (WORD16 *)(gai2_ih264d_default_intra4x4) : (WORD16 *)(gai2_ih264d_default_inter4x4); |
215 | 523 | } |
216 | 9.35k | else |
217 | 9.35k | { |
218 | 9.35k | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
219 | 9.35k | ps_pic->i2_pic_scalinglist4x4[i4_i]; |
220 | 9.35k | } |
221 | 9.87k | } |
222 | 43.7k | } |
223 | 14.5k | else |
224 | 14.5k | { |
225 | 14.5k | if(!ps_pic->u1_pic_scaling_list_present_flag[i4_i]) |
226 | 11.2k | { |
227 | 11.2k | if(!ps_seq->i4_seq_scaling_matrix_present_flag) |
228 | 3.86k | { |
229 | 3.86k | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
230 | 3.86k | (i4_i == 6) ? ((WORD16*)gai2_ih264d_default_intra8x8) : ((WORD16*)gai2_ih264d_default_inter8x8); |
231 | 3.86k | } |
232 | 11.2k | } |
233 | 3.28k | else |
234 | 3.28k | { |
235 | 3.28k | if(ps_pic->u1_pic_use_default_scaling_matrix_flag[i4_i]) |
236 | 406 | { |
237 | 406 | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
238 | 406 | (i4_i == 6) ? (WORD16 *)(gai2_ih264d_default_intra8x8) : (WORD16 *)(gai2_ih264d_default_inter8x8); |
239 | 406 | } |
240 | 2.87k | else |
241 | 2.87k | { |
242 | 2.87k | ps_dec->s_high_profile.pi2_scale_mat[i4_i] = |
243 | 2.87k | ps_pic->i2_pic_scalinglist8x8[i4_i - 6]; |
244 | 2.87k | } |
245 | 3.28k | } |
246 | 14.5k | } |
247 | 58.2k | } |
248 | 7.28k | } |
249 | | |
250 | | /*************************************************************************/ |
251 | | /* perform the inverse scanning for the frame and field scaling matrices */ |
252 | | /*************************************************************************/ |
253 | 17.1k | { |
254 | 17.1k | UWORD8 *pu1_inv_scan_4x4; |
255 | 17.1k | WORD32 i4_i, i4_j; |
256 | | |
257 | 17.1k | pu1_inv_scan_4x4 = (UWORD8 *)gau1_ih264d_inv_scan; |
258 | | |
259 | | /* for all 4x4 matrices */ |
260 | 119k | for(i4_i = 0; i4_i < 6; i4_i++) |
261 | 102k | { |
262 | 102k | if(ps_dec->s_high_profile.pi2_scale_mat[i4_i] == NULL) |
263 | 0 | return ERROR_CORRUPTED_SLICE; |
264 | | |
265 | 1.74M | for(i4_j = 0; i4_j < 16; i4_j++) |
266 | 1.64M | { |
267 | 1.64M | ps_dec->s_high_profile.i2_scalinglist4x4[i4_i][pu1_inv_scan_4x4[i4_j]] = |
268 | 1.64M | ps_dec->s_high_profile.pi2_scale_mat[i4_i][i4_j]; |
269 | | |
270 | 1.64M | } |
271 | 102k | } |
272 | | |
273 | | /* for all 8x8 matrices */ |
274 | 51.3k | for(i4_i = 0; i4_i < 2; i4_i++) |
275 | 34.2k | { |
276 | 34.2k | if(ps_dec->s_high_profile.pi2_scale_mat[i4_i + 6] == NULL) |
277 | 0 | return ERROR_CORRUPTED_SLICE; |
278 | | |
279 | 2.22M | for(i4_j = 0; i4_j < 64; i4_j++) |
280 | 2.19M | { |
281 | 2.19M | ps_dec->s_high_profile.i2_scalinglist8x8[i4_i][gau1_ih264d_inv_scan_prog8x8_cabac[i4_j]] = |
282 | 2.19M | ps_dec->s_high_profile.pi2_scale_mat[i4_i + 6][i4_j]; |
283 | | |
284 | 2.19M | } |
285 | 34.2k | } |
286 | 17.1k | } |
287 | 17.1k | return OK; |
288 | 17.1k | } |
289 | | |