1 /*
2 *
3 * Copyright 2015 Rockchip Electronics Co. LTD
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 #include <stdlib.h>
19 #include <string.h>
20
21 #include "h264d_scalist.h"
22
23
24
25 static RK_U8 ZZ_SCAN[16] =
26 { 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 };
27
28 static RK_U8 ZZ_SCAN8[64] = {
29 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
30 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
31 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
32 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
33 };
34
35 static RK_S32 Default4x4Intra[H264ScalingList4x4Length] =
36 { 6, 13, 20, 28, 13, 20, 28, 32, 20, 28, 32, 37, 28, 32, 37, 42 };
37
38 static RK_S32 Default4x4Inter[H264ScalingList4x4Length] =
39 { 10, 14, 20, 24, 14, 20, 24, 27, 20, 24, 27, 30, 24, 27, 30, 34 };
40
41 static RK_S32 Default8x8Intra[H264ScalingList8x8Length] = {
42 6, 10, 13, 16, 18, 23, 25, 27, 10, 11, 16, 18, 23, 25, 27, 29,
43 13, 16, 18, 23, 25, 27, 29, 31, 16, 18, 23, 25, 27, 29, 31, 33,
44 18, 23, 25, 27, 29, 31, 33, 36, 23, 25, 27, 29, 31, 33, 36, 38,
45 25, 27, 29, 31, 33, 36, 38, 40, 27, 29, 31, 33, 36, 38, 40, 42
46 };
47
48 static RK_S32 Default8x8Inter[H264ScalingList8x8Length] = {
49 9, 13, 15, 17, 19, 21, 22, 24, 13, 13, 17, 19, 21, 22, 24, 25,
50 15, 17, 19, 21, 22, 24, 25, 27, 17, 19, 21, 22, 24, 25, 27, 28,
51 19, 21, 22, 24, 25, 27, 28, 30, 21, 22, 24, 25, 27, 28, 30, 32,
52 22, 24, 25, 27, 28, 30, 32, 33, 24, 25, 27, 28, 30, 32, 33, 35
53 };
54
55 static RK_S32 Default4x4[16] =
56 { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 };
57
58 static RK_S32 Default8x8[64] = {
59 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
60 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
61 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
62 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16
63 };
64
65
set_sps_scanlist_matrix(H264_SPS_t * sps,H264dVideoCtx_t * p_Vid)66 static void set_sps_scanlist_matrix(H264_SPS_t *sps, H264dVideoCtx_t *p_Vid)
67 {
68 RK_S32 i = 0;
69
70 for (i = 0; i < 6; ++i) {
71 if (!sps->seq_scaling_list_present_flag[i]) { // fall-back rule A
72 if (i == 0) {
73 p_Vid->qmatrix[i] = Default4x4Intra;
74 } else if (i == 3) {
75 p_Vid->qmatrix[i] = Default4x4Inter;
76 } else {
77 p_Vid->qmatrix[i] = p_Vid->qmatrix[i - 1];
78 }
79 } else {
80 if (sps->UseDefaultScalingMatrix4x4Flag[i]) {
81
82 p_Vid->qmatrix[i] = (i < 3) ? Default4x4Intra : Default4x4Inter;
83 } else {
84 p_Vid->qmatrix[i] = sps->ScalingList4x4[i];
85 }
86 }
87 }
88
89 for (i = 6; i < ((sps->chroma_format_idc != H264_CHROMA_444) ? 8 : 12); ++i) {
90 if (!sps->seq_scaling_list_present_flag[i]) { // fall-back rule A
91 if (i == 6) {
92 p_Vid->qmatrix[i] = Default8x8Intra;
93 } else if (i == 7) {
94 p_Vid->qmatrix[i] = Default8x8Inter;
95 } else {
96 p_Vid->qmatrix[i] = p_Vid->qmatrix[i - 2];
97 }
98 } else {
99 if (sps->UseDefaultScalingMatrix8x8Flag[i - 6]) {
100
101 p_Vid->qmatrix[i] = (i == 6 || i == 8 || i == 10) ? Default8x8Intra : Default8x8Inter;
102 } else {
103 p_Vid->qmatrix[i] = sps->ScalingList8x8[i - 6];
104 }
105 }
106 }
107 }
108
set_pps_scanlist_matrix(H264_SPS_t * sps,H264_PPS_t * pps,H264dVideoCtx_t * p_Vid)109 static void set_pps_scanlist_matrix(H264_SPS_t *sps, H264_PPS_t *pps, H264dVideoCtx_t *p_Vid)
110 {
111 RK_S32 i = 0;
112
113 for (i = 0; i < 6; ++i) {
114 if (!pps->pic_scaling_list_present_flag[i]) { // fall-back rule B
115 if (i == 0) {
116 if (!sps->seq_scaling_matrix_present_flag) {
117 p_Vid->qmatrix[i] = Default4x4Intra;
118 }
119 } else if (i == 3) {
120 if (!sps->seq_scaling_matrix_present_flag) {
121 p_Vid->qmatrix[i] = Default4x4Inter;
122 }
123 } else {
124 p_Vid->qmatrix[i] = p_Vid->qmatrix[i - 1];
125 }
126 } else {
127 if (pps->UseDefaultScalingMatrix4x4Flag[i]) {
128 p_Vid->qmatrix[i] = (i < 3) ? Default4x4Intra : Default4x4Inter;
129 } else {
130 p_Vid->qmatrix[i] = pps->ScalingList4x4[i];
131 }
132 }
133 }
134 for (i = 6; i < ((sps->chroma_format_idc != H264_CHROMA_444) ? 8 : 12); ++i) {
135 if (!pps->pic_scaling_list_present_flag[i]) { // fall-back rule B
136 if (i == 6) {
137 if (!sps->seq_scaling_matrix_present_flag) {
138 p_Vid->qmatrix[i] = Default8x8Intra;
139 }
140 } else if (i == 7) {
141 if (!sps->seq_scaling_matrix_present_flag)
142 p_Vid->qmatrix[i] = Default8x8Inter;
143 } else
144 p_Vid->qmatrix[i] = p_Vid->qmatrix[i - 2];
145 } else {
146 if (pps->UseDefaultScalingMatrix8x8Flag[i - 6]) {
147 p_Vid->qmatrix[i] = (i == 6 || i == 8 || i == 10) ? Default8x8Intra : Default8x8Inter;
148 } else {
149 p_Vid->qmatrix[i] = pps->ScalingList8x8[i - 6];
150 }
151 }
152 }
153 }
154
155 /*!
156 ***********************************************************************
157 * \brief
158 * check profile
159 ***********************************************************************
160 */
161 //extern "C"
is_prext_profile(RK_U32 profile_idc)162 RK_U32 is_prext_profile(RK_U32 profile_idc)
163 {
164 return (profile_idc >= H264_PROFILE_HIGH || profile_idc == H264_PROFILE_FREXT_CAVLC444) ? 1 : 0;
165 }
166
167 /*!
168 ***********************************************************************
169 * \brief
170 * parse sps and process sps
171 ***********************************************************************
172 */
173 //extern "C"
parse_scalingList(BitReadCtx_t * p_bitctx,RK_S32 size,RK_S32 * scaling_list,RK_S32 * use_default)174 MPP_RET parse_scalingList(BitReadCtx_t *p_bitctx, RK_S32 size, RK_S32 *scaling_list, RK_S32 *use_default)
175 {
176 MPP_RET ret = MPP_ERR_UNKNOW;
177 RK_S32 last_scale = 8;
178 RK_S32 next_scale = 8;
179 RK_S32 delta_scale = 0;
180 RK_S32 j = 0, scanj = 0;
181
182 RK_U8 *zz_scan = (size > 16) ? ZZ_SCAN8 : ZZ_SCAN;
183
184 *use_default = 0;
185 for (j = 0; j < size; ++j) {
186 scanj = zz_scan[j];
187 if (next_scale != 0) {
188 READ_SE(p_bitctx, &delta_scale);
189 next_scale = (last_scale + delta_scale + 256) & 0xff;
190 *use_default = (scanj == 0 && next_scale == 0) ? 1 : 0;
191 }
192 scaling_list[scanj] = (next_scale == 0) ? last_scale : next_scale;
193 last_scale = scaling_list[scanj];
194 }
195
196 return ret = MPP_OK;
197 __BITREAD_ERR:
198 ret = p_bitctx->ret;
199 return ret;
200 }
201
202
203 /*!
204 ***********************************************************************
205 * \brief
206 * parse sps and process sps
207 ***********************************************************************
208 */
209 //extern "C"
get_max_dec_frame_buf_size(H264_SPS_t * sps)210 MPP_RET get_max_dec_frame_buf_size(H264_SPS_t *sps)
211 {
212 RK_S32 size = 0;
213 RK_S32 pic_size = 0;
214 MPP_RET ret = MPP_ERR_UNKNOW;
215
216
217 switch (sps->level_idc) {
218 case 9:
219 size = 152064;
220 break;
221 case 10:
222 size = 152064;
223 break;
224 case 11:
225 if (sps->constrained_set3_flag && !is_prext_profile(sps->profile_idc)) {
226 size = 152064;
227 } else {
228 size = 345600;
229 }
230 break;
231 case 12:
232 size = 912384;
233 break;
234 case 13:
235 size = 912384;
236 break;
237 case 20:
238 size = 912384;
239 break;
240 case 21:
241 size = 1824768;
242 break;
243 case 22:
244 size = 3110400;
245 break;
246 case 30:
247 size = 3110400;
248 break;
249 case 31:
250 size = 6912000;
251 break;
252 case 32:
253 size = 7864320;
254 break;
255 case 40:
256 size = 12582912;
257 break;
258 case 41:
259 size = 12582912;
260 break;
261 case 42:
262 size = 13369344;
263 break;
264 case 50:
265 size = 42393600;
266 break;
267 case 51:
268 size = 70778880;
269 break;
270 case 52:
271 size = 70778880;
272 break;
273 case 60:
274 size = 267386880;
275 break;
276 case 61:
277 size = 267386880;
278 break;
279 case 62:
280 size = 267386880;
281 break;
282 default:
283 ASSERT(0); // undefined level
284 return ret = MPP_NOK;
285 }
286 pic_size = (sps->pic_width_in_mbs_minus1 + 1)
287 * (sps->pic_height_in_map_units_minus1 + 1)
288 * (sps->frame_mbs_only_flag ? 1 : 2) * 384;
289 size /= pic_size;
290 size = MPP_MIN(size, 16);
291 sps->max_dec_frame_buffering = size;
292
293 return ret = MPP_OK;
294 }
295
296 /*!
297 ***********************************************************************
298 * \brief
299 * parse sps and process sps
300 ***********************************************************************
301 */
302 //extern "C"
parse_sps_scalinglists(BitReadCtx_t * p_bitctx,H264_SPS_t * sps)303 MPP_RET parse_sps_scalinglists(BitReadCtx_t *p_bitctx, H264_SPS_t *sps)
304 {
305 RK_S32 i = 0;
306 MPP_RET ret = MPP_ERR_UNKNOW;
307 // Parse scaling_list4x4.
308 for (i = 0; i < 6; ++i) {
309 READ_ONEBIT(p_bitctx, &sps->seq_scaling_list_present_flag[i]);
310
311 if (sps->seq_scaling_list_present_flag[i]) {
312 FUN_CHECK(ret = parse_scalingList(p_bitctx, H264ScalingList4x4Length,
313 sps->ScalingList4x4[i], &sps->UseDefaultScalingMatrix4x4Flag[i]));
314 }
315 }
316 // Parse scaling_list8x8.
317 for (i = 0; i < ((sps->chroma_format_idc != H264_CHROMA_444) ? 2 : 6); ++i) {
318 READ_ONEBIT(p_bitctx, &sps->seq_scaling_list_present_flag[6 + i]);
319 if (sps->seq_scaling_list_present_flag[6 + i]) {
320 FUN_CHECK(ret = parse_scalingList(p_bitctx, H264ScalingList8x8Length,
321 sps->ScalingList8x8[i], &sps->UseDefaultScalingMatrix8x8Flag[i]));
322 }
323 }
324
325 return ret = MPP_OK;
326 __BITREAD_ERR:
327 ret = p_bitctx->ret;
328 __FAILED:
329 return ret;
330 }
331 /*!
332 ***********************************************************************
333 * \brief
334 * prepare scanlist info to register syntax
335 ***********************************************************************
336 */
337 //extern "C"
prepare_init_scanlist(H264_SLICE_t * currSlice)338 MPP_RET prepare_init_scanlist(H264_SLICE_t *currSlice)
339 {
340 RK_S32 i = 0;
341 H264_SPS_t *sps = currSlice->p_Vid->active_sps;
342 H264_PPS_t *pps = currSlice->p_Vid->active_pps;
343
344 if (!pps->pic_scaling_matrix_present_flag && !sps->seq_scaling_matrix_present_flag) {
345 for (i = 0; i < 12; i++) {
346 currSlice->p_Vid->qmatrix[i] = (i < 6) ? Default4x4 : Default8x8;
347 }
348 } else {
349 if (sps->seq_scaling_matrix_present_flag) { // check sps first
350 set_sps_scanlist_matrix(sps, currSlice->p_Vid);
351 }
352 if (pps->pic_scaling_matrix_present_flag) { // then check pps
353 set_pps_scanlist_matrix(sps, pps, currSlice->p_Vid);
354 }
355 }
356 return MPP_OK;
357 }
358