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 #define MODULE_TAG "h264d_sps"
19
20 #include <string.h>
21
22 #include "mpp_mem.h"
23
24 #include "h264d_global.h"
25 #include "h264d_sps.h"
26 #include "h264d_scalist.h"
27 #include "h264d_dpb.h"
28
29 #define MAX_CPB 240000 /* for level 5.1 */
30 #define MAX_BR 240000 /* for level 5.1 */
31
reset_cur_sps_data(H264_SPS_t * cur_sps)32 static void reset_cur_sps_data(H264_SPS_t *cur_sps)
33 {
34 memset(cur_sps, 0, sizeof(H264_SPS_t));
35 cur_sps->seq_parameter_set_id = 0; // reset
36 }
37
reset_cur_subpps_data(H264_subSPS_t * cur_subpps)38 static void reset_cur_subpps_data(H264_subSPS_t *cur_subpps)
39 {
40 memset(cur_subpps, 0, sizeof(H264_subSPS_t));
41 cur_subpps->sps.seq_parameter_set_id = 0; // reset
42 cur_subpps->num_views_minus1 = -1;
43 cur_subpps->num_level_values_signalled_minus1 = -1;
44 }
45
read_hrd_parameters(BitReadCtx_t * p_bitctx,H264_HRD_t * hrd)46 static MPP_RET read_hrd_parameters(BitReadCtx_t *p_bitctx, H264_HRD_t *hrd)
47 {
48 RK_U32 SchedSelIdx = 0;
49 MPP_RET ret = MPP_ERR_UNKNOW;
50 READ_UE(p_bitctx, &hrd->cpb_cnt_minus1);
51 VAL_CHECK(ret, hrd->cpb_cnt_minus1 < MAXIMUMVALUEOFcpb_cnt);
52 hrd->cpb_cnt_minus1 += 1;
53 READ_BITS(p_bitctx, 4, &hrd->bit_rate_scale);
54 READ_BITS(p_bitctx, 4, &hrd->cpb_size_scale);
55 for (SchedSelIdx = 0; SchedSelIdx < hrd->cpb_cnt_minus1; SchedSelIdx++) {
56 READ_UE(p_bitctx, &hrd->bit_rate_value_minus1[SchedSelIdx]);
57 READ_UE(p_bitctx, &hrd->cpb_size_value_minus1[SchedSelIdx]);
58 READ_ONEBIT(p_bitctx, &hrd->cbr_flag[SchedSelIdx]);
59 }
60 READ_BITS(p_bitctx, 5, &hrd->initial_cpb_removal_delay_length_minus1);
61 hrd->initial_cpb_removal_delay_length_minus1 += 1;
62 READ_BITS(p_bitctx, 5, &hrd->cpb_removal_delay_length_minus1);
63 hrd->cpb_removal_delay_length_minus1 += 1;
64 READ_BITS(p_bitctx, 5, &hrd->dpb_output_delay_length_minus1);
65 hrd->dpb_output_delay_length_minus1 += 1;
66 READ_BITS(p_bitctx, 5, &hrd->time_offset_length);
67
68 return ret = MPP_OK;
69 __BITREAD_ERR:
70 return ret = p_bitctx->ret;
71 __FAILED:
72 return ret;
73 }
74
init_VUI(H264_VUI_t * vui)75 static void init_VUI(H264_VUI_t *vui)
76 {
77 vui->matrix_coefficients = 2;
78 }
79
read_VUI(BitReadCtx_t * p_bitctx,H264_VUI_t * vui)80 static MPP_RET read_VUI(BitReadCtx_t *p_bitctx, H264_VUI_t *vui)
81 {
82 MPP_RET ret = MPP_ERR_UNKNOW;
83
84 READ_ONEBIT(p_bitctx, &vui->aspect_ratio_info_present_flag);
85 if (vui->aspect_ratio_info_present_flag) {
86 READ_BITS(p_bitctx, 8, &vui->aspect_ratio_idc);
87 if (255 == vui->aspect_ratio_idc) {
88 READ_BITS(p_bitctx, 16, &vui->sar_width);
89 READ_BITS(p_bitctx, 16, &vui->sar_height);
90 }
91 }
92 READ_ONEBIT(p_bitctx, &vui->overscan_info_present_flag);
93 if (vui->overscan_info_present_flag) {
94 READ_ONEBIT(p_bitctx, &vui->overscan_appropriate_flag);
95 }
96 READ_ONEBIT(p_bitctx, &vui->video_signal_type_present_flag);
97 if (vui->video_signal_type_present_flag) {
98 READ_BITS(p_bitctx, 3, &vui->video_format);
99 READ_ONEBIT(p_bitctx, &vui->video_full_range_flag);
100 READ_ONEBIT(p_bitctx, &vui->colour_description_present_flag);
101 if (vui->colour_description_present_flag) {
102 READ_BITS(p_bitctx, 8, &vui->colour_primaries);
103 READ_BITS(p_bitctx, 8, &vui->transfer_characteristics);
104 READ_BITS(p_bitctx, 8, &vui->matrix_coefficients);
105 if (vui->matrix_coefficients >= MPP_FRAME_SPC_NB)
106 vui->matrix_coefficients = MPP_FRAME_SPC_UNSPECIFIED;
107 }
108 }
109 READ_ONEBIT(p_bitctx, &vui->chroma_location_info_present_flag);
110 if (vui->chroma_location_info_present_flag) {
111 READ_UE(p_bitctx, &vui->chroma_sample_loc_type_top_field);
112 READ_UE(p_bitctx, &vui->chroma_sample_loc_type_bottom_field);
113 }
114 READ_ONEBIT(p_bitctx, &vui->timing_info_present_flag);
115 if (vui->timing_info_present_flag) {
116 READ_BITS_LONG(p_bitctx, 32, &vui->num_units_in_tick);
117 READ_BITS_LONG(p_bitctx, 32, &vui->time_scale);
118 READ_ONEBIT(p_bitctx, &vui->fixed_frame_rate_flag);
119 }
120 READ_ONEBIT(p_bitctx, &vui->nal_hrd_parameters_present_flag);
121 if (vui->nal_hrd_parameters_present_flag) {
122 FUN_CHECK(ret = read_hrd_parameters(p_bitctx, &vui->nal_hrd_parameters));
123 } else {
124 vui->nal_hrd_parameters.cpb_cnt_minus1 = 1;
125 /* MaxBR and MaxCPB should be the values correspondig to the levelIdc
126 * in the SPS containing these VUI parameters. However, these values
127 * are not used anywhere and maximum for any level will be used here */
128 vui->nal_hrd_parameters.bit_rate_value_minus1[0] = 1000 * MAX_BR + 1;
129 vui->nal_hrd_parameters.cpb_size_value_minus1[0] = 1000 * MAX_CPB + 1;
130 vui->nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 = 24;
131 vui->nal_hrd_parameters.cpb_removal_delay_length_minus1 = 24;
132 vui->nal_hrd_parameters.dpb_output_delay_length_minus1 = 24;
133 vui->nal_hrd_parameters.time_offset_length = 24;
134 }
135
136 READ_ONEBIT(p_bitctx, &vui->vcl_hrd_parameters_present_flag);
137 if (vui->vcl_hrd_parameters_present_flag) {
138 FUN_CHECK(ret = read_hrd_parameters(p_bitctx, &vui->vcl_hrd_parameters));
139 } else {
140 vui->vcl_hrd_parameters.cpb_cnt_minus1 = 0;
141 /* MaxBR and MaxCPB should be the values correspondig to the levelIdc
142 * in the SPS containing these VUI parameters. However, these values
143 * are not used anywhere and maximum for any level will be used here */
144 vui->vcl_hrd_parameters.bit_rate_value_minus1[0] = 1000 * MAX_BR + 1;
145 vui->vcl_hrd_parameters.cpb_size_value_minus1[0] = 1000 * MAX_CPB + 1;
146 vui->vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 = 24;
147 vui->vcl_hrd_parameters.cpb_removal_delay_length_minus1 = 24;
148 vui->vcl_hrd_parameters.dpb_output_delay_length_minus1 = 24;
149 vui->vcl_hrd_parameters.time_offset_length = 24;
150 }
151 if (vui->nal_hrd_parameters_present_flag || vui->vcl_hrd_parameters_present_flag) {
152 READ_ONEBIT(p_bitctx, &vui->low_delay_hrd_flag);
153 }
154 READ_ONEBIT(p_bitctx, &vui->pic_struct_present_flag);
155 READ_ONEBIT(p_bitctx, &vui->bitstream_restriction_flag);
156 if (vui->bitstream_restriction_flag) {
157 READ_ONEBIT(p_bitctx, &vui->motion_vectors_over_pic_boundaries_flag);
158 READ_UE(p_bitctx, &vui->max_bytes_per_pic_denom);
159 READ_UE(p_bitctx, &vui->max_bits_per_mb_denom);
160 READ_UE(p_bitctx, &vui->log2_max_mv_length_horizontal);
161 READ_UE(p_bitctx, &vui->log2_max_mv_length_vertical);
162 READ_UE(p_bitctx, &vui->num_reorder_frames);
163 READ_UE(p_bitctx, &vui->max_dec_frame_buffering);
164 }
165
166 return ret = MPP_OK;
167 __BITREAD_ERR:
168 ret = p_bitctx->ret;
169 __FAILED:
170 return ret;
171 }
172
parser_sps(BitReadCtx_t * p_bitctx,H264_SPS_t * cur_sps,H264_DecCtx_t * p_Dec)173 static MPP_RET parser_sps(BitReadCtx_t *p_bitctx, H264_SPS_t *cur_sps, H264_DecCtx_t *p_Dec)
174 {
175 RK_S32 i = 0, temp = 0;
176 MPP_RET ret = MPP_ERR_UNKNOW;
177 //!< init Fidelity Range Extensions stuff
178 cur_sps->chroma_format_idc = 1;
179 cur_sps->bit_depth_luma_minus8 = 0;
180 cur_sps->bit_depth_chroma_minus8 = 0;
181 cur_sps->qpprime_y_zero_transform_bypass_flag = 0;
182 cur_sps->separate_colour_plane_flag = 0;
183 cur_sps->log2_max_pic_order_cnt_lsb_minus4 = 0;
184 cur_sps->delta_pic_order_always_zero_flag = 0;
185 p_Dec->errctx.un_spt_flag = 0; // init unspport flag first
186
187 READ_BITS(p_bitctx, 8, &cur_sps->profile_idc);
188 VAL_CHECK (ret, (cur_sps->profile_idc == H264_PROFILE_BASELINE)
189 || (cur_sps->profile_idc == H264_PROFILE_MAIN)
190 || (cur_sps->profile_idc == H264_PROFILE_EXTENDED)
191 || (cur_sps->profile_idc == H264_PROFILE_HIGH)
192 || (cur_sps->profile_idc == H264_PROFILE_HIGH10)
193 || (cur_sps->profile_idc == H264_PROFILE_HIGH422)
194 || (cur_sps->profile_idc == H264_PROFILE_HIGH444)
195 || (cur_sps->profile_idc == H264_PROFILE_FREXT_CAVLC444)
196 || (cur_sps->profile_idc == H264_PROFILE_MVC_HIGH)
197 || (cur_sps->profile_idc == H264_PROFILE_STEREO_HIGH)
198 );
199 READ_ONEBIT(p_bitctx, &cur_sps->constrained_set0_flag);
200 READ_ONEBIT(p_bitctx, &cur_sps->constrained_set1_flag);
201 READ_ONEBIT(p_bitctx, &cur_sps->constrained_set2_flag);
202 READ_ONEBIT(p_bitctx, &cur_sps->constrained_set3_flag);
203 READ_ONEBIT(p_bitctx, &cur_sps->constrained_set4_flag);
204 READ_ONEBIT(p_bitctx, &cur_sps->constrained_set5_flag);
205 READ_BITS(p_bitctx, 2, &temp); //!< reserved_zero_2bits
206 ASSERT(temp == 0);
207 READ_BITS(p_bitctx, 8, &cur_sps->level_idc);
208 READ_UE(p_bitctx, &cur_sps->seq_parameter_set_id);
209 if (cur_sps->seq_parameter_set_id >= MAXSPS) {
210 cur_sps->seq_parameter_set_id = 0;
211 }
212 if (cur_sps->profile_idc == 100 || cur_sps->profile_idc == 110
213 || cur_sps->profile_idc == 122 || cur_sps->profile_idc == 244
214 || cur_sps->profile_idc == 44 || cur_sps->profile_idc == 83
215 || cur_sps->profile_idc == 86 || cur_sps->profile_idc == 118
216 || cur_sps->profile_idc == 128 || cur_sps->profile_idc == 138) {
217 READ_UE(p_bitctx, &cur_sps->chroma_format_idc);
218 /* check invalid chroma format idc */
219 VAL_CHECK (ret, (cur_sps->chroma_format_idc <= H264_CHROMA_444));
220 /* TODO: check unsupport chroma format by hardware capacity */
221 if (cur_sps->chroma_format_idc > H264_CHROMA_422) {
222 H264D_ERR("ERROR: Not support chroma_format_idc %d", cur_sps->chroma_format_idc);
223 p_Dec->errctx.un_spt_flag = MPP_FRAME_ERR_UNSUPPORT;
224 goto __FAILED;
225 }
226 READ_UE(p_bitctx, &cur_sps->bit_depth_luma_minus8);
227 ASSERT(cur_sps->bit_depth_luma_minus8 < 7);
228 READ_UE(p_bitctx, &cur_sps->bit_depth_chroma_minus8);
229 ASSERT(cur_sps->bit_depth_chroma_minus8 < 7);
230 READ_ONEBIT(p_bitctx, &cur_sps->qpprime_y_zero_transform_bypass_flag);
231 if (cur_sps->qpprime_y_zero_transform_bypass_flag == 1) {
232 H264D_ERR("ERROR: Not support high 4:4:4 lossless mode");
233 p_Dec->errctx.un_spt_flag = MPP_FRAME_ERR_UNSUPPORT;
234 goto __FAILED;
235 }
236 READ_ONEBIT(p_bitctx, &cur_sps->seq_scaling_matrix_present_flag);
237 if (cur_sps->seq_scaling_matrix_present_flag) {
238 H264D_WARNNING("Scaling matrix present.");
239 if (parse_sps_scalinglists(p_bitctx, cur_sps)) {
240 mpp_log_f(" parse_sps_scaling_list error.");
241 }
242 }
243 }
244 READ_UE(p_bitctx, &cur_sps->log2_max_frame_num_minus4);
245 ASSERT(cur_sps->log2_max_frame_num_minus4 < 13);
246 READ_UE(p_bitctx, &cur_sps->pic_order_cnt_type);
247 ASSERT(cur_sps->pic_order_cnt_type < 3);
248
249 cur_sps->log2_max_pic_order_cnt_lsb_minus4 = 0;
250 cur_sps->delta_pic_order_always_zero_flag = 0;
251 if (0 == cur_sps->pic_order_cnt_type) {
252 READ_UE(p_bitctx, &cur_sps->log2_max_pic_order_cnt_lsb_minus4);
253 ASSERT(cur_sps->log2_max_pic_order_cnt_lsb_minus4 < 13);
254 } else if (1 == cur_sps->pic_order_cnt_type) {
255 READ_ONEBIT(p_bitctx, &cur_sps->delta_pic_order_always_zero_flag);
256 READ_SE(p_bitctx, &cur_sps->offset_for_non_ref_pic);
257 READ_SE(p_bitctx, &cur_sps->offset_for_top_to_bottom_field);
258 READ_UE(p_bitctx, &cur_sps->num_ref_frames_in_pic_order_cnt_cycle);
259 VAL_CHECK(ret, cur_sps->num_ref_frames_in_pic_order_cnt_cycle < 256);
260 for (i = 0; i < cur_sps->num_ref_frames_in_pic_order_cnt_cycle; ++i) {
261 READ_SE(p_bitctx, &cur_sps->offset_for_ref_frame[i]);
262 cur_sps->expected_delta_per_pic_order_cnt_cycle += cur_sps->offset_for_ref_frame[i];
263 }
264 }
265 READ_UE(p_bitctx, &cur_sps->max_num_ref_frames);
266 READ_ONEBIT(p_bitctx, &cur_sps->gaps_in_frame_num_value_allowed_flag);
267 READ_UE(p_bitctx, &cur_sps->pic_width_in_mbs_minus1);
268 READ_UE(p_bitctx, &cur_sps->pic_height_in_map_units_minus1);
269 READ_ONEBIT(p_bitctx, &cur_sps->frame_mbs_only_flag);
270 if (!cur_sps->frame_mbs_only_flag) {
271 READ_ONEBIT(p_bitctx, &cur_sps->mb_adaptive_frame_field_flag);
272 }
273 READ_ONEBIT(p_bitctx, &cur_sps->direct_8x8_inference_flag);
274
275 READ_ONEBIT(p_bitctx, &cur_sps->frame_cropping_flag);
276 if (cur_sps->frame_cropping_flag) {
277 READ_UE(p_bitctx, &cur_sps->frame_crop_left_offset);
278 READ_UE(p_bitctx, &cur_sps->frame_crop_right_offset);
279 READ_UE(p_bitctx, &cur_sps->frame_crop_top_offset);
280 READ_UE(p_bitctx, &cur_sps->frame_crop_bottom_offset);
281 }
282 READ_ONEBIT(p_bitctx, &cur_sps->vui_parameters_present_flag);
283
284 if (cur_sps->vui_parameters_present_flag) {
285 init_VUI(&cur_sps->vui_seq_parameters);
286 ret = read_VUI(p_bitctx, &cur_sps->vui_seq_parameters);
287 }
288 cur_sps->Valid = 1;
289 (void)p_Dec;
290
291 return ret = MPP_OK;
292 __BITREAD_ERR:
293 ret = p_bitctx->ret;
294 __FAILED:
295 return ret;
296 }
297
sps_mvc_extension(BitReadCtx_t * p_bitctx,H264_subSPS_t * subset_sps)298 static MPP_RET sps_mvc_extension(BitReadCtx_t *p_bitctx, H264_subSPS_t *subset_sps)
299 {
300 MPP_RET ret = MPP_ERR_UNKNOW;
301 RK_S32 i = 0, j = 0, num_views = 0;
302
303 READ_UE(p_bitctx, &subset_sps->num_views_minus1);
304 VAL_CHECK(ret, subset_sps->num_views_minus1 < 16);
305 num_views = 1 + subset_sps->num_views_minus1;
306 //========================
307 if (num_views > 0) {
308 subset_sps->view_id = mpp_calloc(RK_S32, num_views);
309 subset_sps->num_anchor_refs_l0 = mpp_calloc(RK_S32, num_views);
310 subset_sps->num_anchor_refs_l1 = mpp_calloc(RK_S32, num_views);
311 subset_sps->anchor_ref_l0 = mpp_calloc(RK_S32*, num_views);
312 subset_sps->anchor_ref_l1 = mpp_calloc(RK_S32*, num_views);
313 subset_sps->num_non_anchor_refs_l0 = mpp_calloc(RK_S32, num_views);
314 subset_sps->num_non_anchor_refs_l1 = mpp_calloc(RK_S32, num_views);
315 subset_sps->non_anchor_ref_l0 = mpp_calloc(RK_S32*, num_views);
316 subset_sps->non_anchor_ref_l1 = mpp_calloc(RK_S32*, num_views);
317 MEM_CHECK(ret, subset_sps->view_id && subset_sps->num_anchor_refs_l0
318 && subset_sps->num_anchor_refs_l1 && subset_sps->anchor_ref_l0
319 && subset_sps->anchor_ref_l1 && subset_sps->num_non_anchor_refs_l0
320 && subset_sps->num_non_anchor_refs_l1 && subset_sps->non_anchor_ref_l0
321 && subset_sps->non_anchor_ref_l1);
322 }
323 for (i = 0; i < num_views; i++) {
324 READ_UE(p_bitctx, &subset_sps->view_id[i]);
325 }
326 for (i = 1; i < num_views; i++) {
327 READ_UE(p_bitctx, &subset_sps->num_anchor_refs_l0[i]);
328 if (subset_sps->num_anchor_refs_l0[i]) {
329 subset_sps->anchor_ref_l0[i] = mpp_calloc(RK_S32, subset_sps->num_anchor_refs_l0[i]);
330 MEM_CHECK(ret, subset_sps->anchor_ref_l0[i]);
331 for (j = 0; j < subset_sps->num_anchor_refs_l0[i]; j++) {
332 READ_UE(p_bitctx, &subset_sps->anchor_ref_l0[i][j]);
333 }
334 }
335 READ_UE(p_bitctx, &subset_sps->num_anchor_refs_l1[i]);
336 if (subset_sps->num_anchor_refs_l1[i]) {
337 subset_sps->anchor_ref_l1[i] = mpp_calloc(RK_S32, subset_sps->num_anchor_refs_l1[i]);
338 MEM_CHECK(ret, subset_sps->anchor_ref_l1[i]);
339 for (j = 0; j < subset_sps->num_anchor_refs_l1[i]; j++) {
340 READ_UE(p_bitctx, &subset_sps->anchor_ref_l1[i][j]);
341 }
342 }
343 }
344 for (i = 1; i < num_views; i++) {
345 READ_UE(p_bitctx, &subset_sps->num_non_anchor_refs_l0[i]);
346 if (subset_sps->num_non_anchor_refs_l0[i]) {
347 subset_sps->non_anchor_ref_l0[i] = mpp_calloc(RK_S32, subset_sps->num_non_anchor_refs_l0[i]);
348 MEM_CHECK(ret, subset_sps->non_anchor_ref_l0[i]);
349 for (j = 0; j < subset_sps->num_non_anchor_refs_l0[i]; j++) {
350 READ_UE(p_bitctx, &subset_sps->non_anchor_ref_l0[i][j]);
351 }
352 }
353 READ_UE(p_bitctx, &subset_sps->num_non_anchor_refs_l1[i]);
354 if (subset_sps->num_non_anchor_refs_l1[i]) {
355 subset_sps->non_anchor_ref_l1[i] = mpp_calloc(RK_S32, subset_sps->num_non_anchor_refs_l1[i]);
356 MEM_CHECK(ret, subset_sps->non_anchor_ref_l1[i]);
357
358 for (j = 0; j < subset_sps->num_non_anchor_refs_l1[i]; j++) {
359 READ_UE(p_bitctx, &subset_sps->non_anchor_ref_l1[i][j]);
360 }
361 }
362 }
363 return ret = MPP_OK;
364 __BITREAD_ERR:
365 ret = p_bitctx->ret;
366 __FAILED:
367 return ret;
368 }
369
parser_subsps_ext(BitReadCtx_t * p_bitctx,H264_subSPS_t * cur_subsps)370 static MPP_RET parser_subsps_ext(BitReadCtx_t *p_bitctx, H264_subSPS_t *cur_subsps)
371 {
372 MPP_RET ret = MPP_ERR_UNKNOW;
373
374 if ((cur_subsps->sps.profile_idc == H264_PROFILE_MVC_HIGH)
375 || (cur_subsps->sps.profile_idc == H264_PROFILE_STEREO_HIGH)) {
376 READ_ONEBIT(p_bitctx, &cur_subsps->bit_equal_to_one);
377 ASSERT(cur_subsps->bit_equal_to_one == 1);
378 FUN_CHECK(ret = sps_mvc_extension(p_bitctx, cur_subsps));
379
380 READ_ONEBIT(p_bitctx, &cur_subsps->mvc_vui_parameters_present_flag);
381 }
382
383 return ret = MPP_OK;
384 __BITREAD_ERR:
385 ret = p_bitctx->ret;
386 __FAILED:
387 return ret;
388 }
389
update_video_pars(H264dVideoCtx_t * p_Vid,H264_SPS_t * sps)390 static void update_video_pars(H264dVideoCtx_t *p_Vid, H264_SPS_t *sps)
391 {
392 RK_U32 crop_left = 0, crop_right = 0;
393 RK_U32 crop_top = 0, crop_bottom = 0;
394 static const RK_U32 SubWidthC [4] = { 1, 2, 2, 1};
395 static const RK_U32 SubHeightC [4] = { 1, 2, 1, 1};
396
397
398 p_Vid->max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
399 p_Vid->PicWidthInMbs = (sps->pic_width_in_mbs_minus1 + 1);
400 p_Vid->FrameHeightInMbs = (2 - sps->frame_mbs_only_flag) * (sps->pic_height_in_map_units_minus1 + 1);
401 p_Vid->yuv_format = sps->chroma_format_idc;
402 p_Vid->frame_mbs_only_flag = sps->frame_mbs_only_flag;
403
404 p_Vid->width = p_Vid->PicWidthInMbs * 16;
405 p_Vid->height = p_Vid->FrameHeightInMbs * 16;
406 p_Vid->bit_depth_luma = sps->bit_depth_luma_minus8 + 8;
407 p_Vid->bit_depth_chroma = sps->bit_depth_chroma_minus8 + 8;
408
409 if (p_Vid->yuv_format == H264_CHROMA_420) {
410 p_Vid->width_cr = (p_Vid->width >> 1);
411 p_Vid->height_cr = (p_Vid->height >> 1);
412 } else if (p_Vid->yuv_format == H264_CHROMA_422) {
413 p_Vid->width_cr = (p_Vid->width >> 1);
414 p_Vid->height_cr = p_Vid->height;
415 }
416 //!< calculate frame_width_after_crop && frame_height_after_crop
417 if (sps->frame_cropping_flag) {
418 crop_left = SubWidthC [sps->chroma_format_idc] * sps->frame_crop_left_offset;
419 crop_right = SubWidthC [sps->chroma_format_idc] * sps->frame_crop_right_offset;
420 crop_top = SubHeightC[sps->chroma_format_idc] * ( 2 - sps->frame_mbs_only_flag ) * sps->frame_crop_top_offset;
421 crop_bottom = SubHeightC[sps->chroma_format_idc] * ( 2 - sps->frame_mbs_only_flag ) * sps->frame_crop_bottom_offset;
422 } else {
423 crop_left = crop_right = crop_top = crop_bottom = 0;
424 }
425 p_Vid->width_after_crop = p_Vid->width - crop_left - crop_right;
426 p_Vid->height_after_crop = p_Vid->height - crop_top - crop_bottom;
427 }
428
video_pars_changed(H264dVideoCtx_t * p_Vid,H264_SPS_t * sps,RK_U8 layer_id)429 static RK_U32 video_pars_changed(H264dVideoCtx_t *p_Vid, H264_SPS_t *sps, RK_U8 layer_id)
430 {
431 RK_U32 ret = 0;
432
433 ret |= p_Vid->p_Dpb_layer[layer_id]->num_ref_frames != sps->max_num_ref_frames;
434 ret |= p_Vid->last_pic_width_in_mbs_minus1[layer_id] != sps->pic_width_in_mbs_minus1;
435 ret |= p_Vid->last_pic_height_in_map_units_minus1[layer_id] != sps->pic_height_in_map_units_minus1;
436 ret |= p_Vid->last_profile_idc[layer_id] != sps->profile_idc;
437 ret |= p_Vid->last_level_idc[layer_id] != sps->level_idc;
438 ret |= !p_Vid->p_Dpb_layer[layer_id]->init_done;
439
440 return ret;
441 }
442
update_last_video_pars(H264dVideoCtx_t * p_Vid,H264_SPS_t * sps,RK_U8 layer_id)443 static void update_last_video_pars(H264dVideoCtx_t *p_Vid, H264_SPS_t *sps, RK_U8 layer_id)
444 {
445 p_Vid->last_pic_width_in_mbs_minus1[layer_id] = sps->pic_width_in_mbs_minus1;
446 p_Vid->last_pic_height_in_map_units_minus1[layer_id] = sps->pic_height_in_map_units_minus1;
447 p_Vid->last_profile_idc[layer_id] = sps->profile_idc;
448 p_Vid->last_level_idc[layer_id] = sps->level_idc;
449 }
450
451 /*!
452 ***********************************************************************
453 * \brief
454 * prase sps and process sps
455 ***********************************************************************
456 */
457 //extern "C"
process_sps(H264_SLICE_t * currSlice)458 MPP_RET process_sps(H264_SLICE_t *currSlice)
459 {
460 MPP_RET ret = MPP_ERR_UNKNOW;
461
462 H264dCurCtx_t *p_Cur = currSlice->p_Cur;
463 BitReadCtx_t *p_bitctx = &p_Cur->bitctx;
464 H264_SPS_t *cur_sps = &p_Cur->sps;
465
466 reset_cur_sps_data(cur_sps); // reset
467 //!< parse sps
468 FUN_CHECK(ret = parser_sps(p_bitctx, cur_sps, currSlice->p_Dec));
469 //!< decide "max_dec_frame_buffering" for DPB
470 FUN_CHECK(ret = get_max_dec_frame_buf_size(cur_sps));
471 //!< make SPS available, copy
472 if (cur_sps->Valid) {
473 if (!currSlice->p_Vid->spsSet[cur_sps->seq_parameter_set_id]) {
474 currSlice->p_Vid->spsSet[cur_sps->seq_parameter_set_id] = mpp_calloc(H264_SPS_t, 1);
475 }
476 memcpy(currSlice->p_Vid->spsSet[cur_sps->seq_parameter_set_id],
477 cur_sps, sizeof(H264_SPS_t));
478 }
479 p_Cur->p_Vid->spspps_update = 1;
480
481 return ret = MPP_OK;
482 __FAILED:
483 return ret;
484 }
485
486
487 /*!
488 ***********************************************************************
489 * \brief
490 * prase sps and process sps
491 ***********************************************************************
492 */
493 //extern "C"
recycle_subsps(H264_subSPS_t * subset_sps)494 void recycle_subsps(H264_subSPS_t *subset_sps)
495 {
496 RK_S32 i = 0, num_views = 0;
497
498 num_views = 1 + subset_sps->num_views_minus1;
499 for (i = 1; i < num_views; i++) {
500 if (subset_sps->num_anchor_refs_l0[i] > 0) {
501 MPP_FREE(subset_sps->anchor_ref_l0[i]);
502 }
503 if (subset_sps->num_anchor_refs_l1[i] > 0) {
504 MPP_FREE(subset_sps->anchor_ref_l1[i]);
505 }
506 if (subset_sps->num_non_anchor_refs_l0[i] > 0) {
507 MPP_FREE(subset_sps->non_anchor_ref_l0[i]);
508 }
509 if (subset_sps->num_non_anchor_refs_l1[i] > 0) {
510 MPP_FREE(subset_sps->non_anchor_ref_l1[i]);
511 }
512 }
513 if (num_views > 0) {
514 MPP_FREE(subset_sps->view_id);
515 MPP_FREE(subset_sps->num_anchor_refs_l0);
516 MPP_FREE(subset_sps->num_anchor_refs_l1);
517 MPP_FREE(subset_sps->anchor_ref_l0);
518 MPP_FREE(subset_sps->anchor_ref_l1);
519 MPP_FREE(subset_sps->num_non_anchor_refs_l0);
520 MPP_FREE(subset_sps->num_non_anchor_refs_l1);
521 MPP_FREE(subset_sps->non_anchor_ref_l0);
522 MPP_FREE(subset_sps->non_anchor_ref_l1);
523 }
524 subset_sps->Valid = 0;
525 }
526
527 /*!
528 ***********************************************************************
529 * \brief
530 * prase sps and process sps
531 ***********************************************************************
532 */
533 //extern "C"
process_subsps(H264_SLICE_t * currSlice)534 MPP_RET process_subsps(H264_SLICE_t *currSlice)
535 {
536 MPP_RET ret = MPP_ERR_UNKNOW;
537
538 BitReadCtx_t *p_bitctx = &currSlice->p_Cur->bitctx;
539 H264_subSPS_t *cur_subsps = NULL;
540 H264_subSPS_t *p_subset = NULL;
541
542 if (!currSlice->p_Cur->subsps)
543 currSlice->p_Cur->subsps = mpp_calloc(H264_subSPS_t, 1);
544
545 cur_subsps = currSlice->p_Cur->subsps;
546 reset_cur_subpps_data(cur_subsps); //reset
547
548 FUN_CHECK(ret = parser_sps(p_bitctx, &cur_subsps->sps, currSlice->p_Dec));
549 FUN_CHECK(ret = parser_subsps_ext(p_bitctx, cur_subsps));
550 if (cur_subsps->sps.Valid) {
551 cur_subsps->Valid = 1;
552 currSlice->p_Vid->profile_idc = cur_subsps->sps.profile_idc;
553 }
554 get_max_dec_frame_buf_size(&cur_subsps->sps);
555 //!< make subSPS available
556 if (!currSlice->p_Vid->subspsSet[cur_subsps->sps.seq_parameter_set_id])
557 currSlice->p_Vid->subspsSet[cur_subsps->sps.seq_parameter_set_id] = mpp_calloc(H264_subSPS_t, 1);
558 p_subset = currSlice->p_Vid->subspsSet[cur_subsps->sps.seq_parameter_set_id];
559 if (p_subset->Valid) {
560 recycle_subsps(p_subset);
561 }
562 memcpy(p_subset, cur_subsps, sizeof(H264_subSPS_t));
563
564 return ret = MPP_OK;
565 __FAILED:
566 recycle_subsps(currSlice->p_Cur->subsps);
567
568 return ret;
569 }
570
571 /*!
572 ***********************************************************************
573 * \brief
574 * prase sps and process sps
575 ***********************************************************************
576 */
577 //extern "C"
activate_sps(H264dVideoCtx_t * p_Vid,H264_SPS_t * sps,H264_subSPS_t * subset_sps)578 MPP_RET activate_sps(H264dVideoCtx_t *p_Vid, H264_SPS_t *sps, H264_subSPS_t *subset_sps)
579 {
580 MPP_RET ret = MPP_ERR_UNKNOW;
581 INP_CHECK(ret, !p_Vid && !sps && !subset_sps);
582 if (p_Vid->dec_pic) {
583 FUN_CHECK(ret = exit_picture(p_Vid, &p_Vid->dec_pic));
584 }
585 if (p_Vid->active_mvc_sps_flag) { // layer_id == 1
586 p_Vid->active_sps = &subset_sps->sps;
587 p_Vid->active_subsps = subset_sps;
588 p_Vid->active_sps_id[0] = 0;
589 p_Vid->active_sps_id[1] = subset_sps->sps.seq_parameter_set_id;
590 if (video_pars_changed(p_Vid, p_Vid->active_sps, 1)) {
591 FUN_CHECK(ret = flush_dpb(p_Vid->p_Dpb_layer[1], 2));
592 FUN_CHECK(ret = init_dpb(p_Vid, p_Vid->p_Dpb_layer[1], 2));
593 FUN_CHECK(ret = check_mvc_dpb(p_Vid, p_Vid->p_Dpb_layer[0], p_Vid->p_Dpb_layer[1]));
594 update_last_video_pars(p_Vid, p_Vid->active_sps, 1);
595 //!< init frame slots, store frame buffer size
596 p_Vid->dpb_size[1] = p_Vid->p_Dpb_layer[1]->size;
597 p_Vid->spspps_update = 1;
598 if (p_Vid->p_Dec->mvc_valid && p_Vid->p_Dpb_layer[1]->size > 0) {
599 p_Vid->p_Dpb_layer[0]->size = MPP_MIN(p_Vid->p_Dpb_layer[1]->size, MAX_DPB_SIZE / 2);
600 p_Vid->dpb_size[0] = p_Vid->p_Dpb_layer[0]->size;
601 }
602 }
603 VAL_CHECK(ret, p_Vid->dpb_size[1] > 0);
604 } else { //!< layer_id == 0
605 p_Vid->active_sps = sps;
606 p_Vid->active_subsps = NULL;
607 p_Vid->active_sps_id[0] = sps->seq_parameter_set_id;
608 p_Vid->active_sps_id[1] = 0;
609 if (video_pars_changed(p_Vid, p_Vid->active_sps, 0)) {
610 if (!p_Vid->no_output_of_prior_pics_flag) {
611 FUN_CHECK(ret = flush_dpb(p_Vid->p_Dpb_layer[0], 1));
612 }
613 FUN_CHECK(ret = init_dpb(p_Vid, p_Vid->p_Dpb_layer[0], 1));
614 update_last_video_pars(p_Vid, p_Vid->active_sps, 0);
615 //!< init frame slots, store frame buffer size
616 p_Vid->dpb_size[0] = p_Vid->p_Dpb_layer[0]->size;
617 p_Vid->spspps_update = 1;
618 }
619 VAL_CHECK(ret, p_Vid->dpb_size[0] > 0);
620 }
621 H264D_DBG(H264D_DBG_DPB_INFO, "[DPB_size] dpb_size[0]=%d, mvc_flag=%d, dpb_size[1]=%d",
622 p_Vid->dpb_size[0], p_Vid->active_mvc_sps_flag, p_Vid->dpb_size[1]);
623 update_video_pars(p_Vid, p_Vid->active_sps);
624 __RETURN:
625 return ret = MPP_OK;
626 __FAILED:
627 return ret;
628 }
629