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