xref: /OK3568_Linux_fs/external/mpp/mpp/codec/dec/h264/h264d_slice.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_slice"
19 
20 #include <string.h>
21 
22 #include "mpp_mem.h"
23 
24 #include "h264d_global.h"
25 #include "h264d_slice.h"
26 #include "h264d_sps.h"
27 #include "h264d_pps.h"
28 #include "h2645d_sei.h"
29 
30 #define PIXW_1080P      (1920)
31 #define PIXH_1080P      (1088)
32 #define PIXW_4Kx2K      (4096)
33 #define PIXH_4Kx2K      (2304)
34 #define PIXW_8Kx4K      (8192)
35 #define PIXH_8Kx4K      (4320)
36 
37 #define MAX_MBW_1080P   (((PIXW_1080P) / 16) - 1)   /* 119 */
38 #define MAX_MBH_1080P   (((PIXH_1080P) / 16) - 1)   /* 67  */
39 #define MAX_MBW_4Kx2K   (((PIXW_4Kx2K) / 16) - 1)   /* 255 */
40 #define MAX_MBH_4Kx2K   (((PIXH_4Kx2K) / 16) - 1)   /* 143 */
41 #define MAX_MBW_8Kx4K   (((PIXW_8Kx4K) / 16) - 1)   /* 511 */
42 #define MAX_MBH_8Kx4K   (((PIXH_8Kx4K) / 16) - 1)   /* 269 */
43 
ref_pic_list_mvc_modification(H264_SLICE_t * currSlice)44 static MPP_RET ref_pic_list_mvc_modification(H264_SLICE_t *currSlice)
45 {
46     RK_U32 i = 0;
47     MPP_RET ret = MPP_ERR_UNKNOW;
48     RK_S32 modification_of_pic_nums_idc = 0;
49     BitReadCtx_t *p_bitctx = &currSlice->p_Cur->bitctx;
50 
51     if ((currSlice->slice_type % 5) != H264_I_SLICE && (currSlice->slice_type % 5) != H264_SI_SLICE) {
52         READ_ONEBIT(p_bitctx, &currSlice->ref_pic_list_reordering_flag[LIST_0]);
53         if (currSlice->ref_pic_list_reordering_flag[LIST_0]) {
54             RK_U32 size = currSlice->num_ref_idx_active[LIST_0] + 1;
55             i = 0;
56             do {
57                 if (i >= size || i >= MAX_REORDER_TIMES) {
58                     ret = MPP_NOK;
59                     H264D_WARNNING("ref_pic_list_reordering error, i >= num_ref_idx_active[LIST_0](%d)", size);
60                     goto __BITREAD_ERR;
61                 }
62                 READ_UE(p_bitctx, &modification_of_pic_nums_idc);
63                 currSlice->modification_of_pic_nums_idc[LIST_0][i] = modification_of_pic_nums_idc;
64                 if (modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1) {
65                     READ_UE(p_bitctx, &currSlice->abs_diff_pic_num_minus1[LIST_0][i]);
66                 } else {
67                     if (modification_of_pic_nums_idc == 2) {
68                         READ_UE(p_bitctx, &currSlice->long_term_pic_idx[LIST_0][i]);
69                     } else if (modification_of_pic_nums_idc == 4 || modification_of_pic_nums_idc == 5) {
70                         READ_UE(p_bitctx, &currSlice->abs_diff_view_idx_minus1[LIST_0][i]);
71                     }
72                 }
73                 i++;
74             } while (modification_of_pic_nums_idc != 3);
75         }
76     }
77     if (currSlice->slice_type % 5 == H264_B_SLICE) {
78         READ_ONEBIT(p_bitctx, &currSlice->ref_pic_list_reordering_flag[LIST_1]);
79         if (currSlice->ref_pic_list_reordering_flag[LIST_1]) {
80             RK_U32 size = currSlice->num_ref_idx_active[LIST_1] + 1;
81             i = 0;
82             do {
83                 if (i >= size || i >= MAX_REORDER_TIMES) {
84                     ret = MPP_NOK;
85                     H264D_WARNNING("ref_pic_list_reordering error, i >= num_ref_idx_active[LIST_1](%d)", size);
86                     goto __BITREAD_ERR;
87                 }
88                 READ_UE(p_bitctx, &modification_of_pic_nums_idc);
89                 currSlice->modification_of_pic_nums_idc[LIST_1][i] = modification_of_pic_nums_idc;
90                 if (modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1) {
91                     READ_UE(p_bitctx, &currSlice->abs_diff_pic_num_minus1[LIST_1][i]);
92                 } else {
93                     if (modification_of_pic_nums_idc == 2) {
94                         READ_UE(p_bitctx, &currSlice->long_term_pic_idx[LIST_1][i]);
95                     } else if (modification_of_pic_nums_idc == 4 || modification_of_pic_nums_idc == 5) {
96                         READ_UE(p_bitctx, &currSlice->abs_diff_view_idx_minus1[LIST_1][i]);
97                     }
98                 }
99                 i++;
100             } while (modification_of_pic_nums_idc != 3);
101         }
102     }
103     ASSERT(currSlice->redundant_pic_cnt == 0);  //!< not support reference index of redundant slices
104 
105     return ret = MPP_OK;
106 __BITREAD_ERR:
107     ret = p_bitctx->ret;
108 
109     return ret;
110 }
111 
pred_weight_table(H264_SLICE_t * currSlice)112 static MPP_RET pred_weight_table(H264_SLICE_t *currSlice)
113 {
114     RK_S32 se_tmp = 0;
115     MPP_RET ret = MPP_ERR_UNKNOW;
116     RK_S32 i = 0, j = 0, temp = 0;
117     BitReadCtx_t *p_bitctx = &currSlice->p_Cur->bitctx;
118 
119     READ_UE(p_bitctx, &temp); //!< log2_weight_denom
120     if (currSlice->active_sps->chroma_format_idc) {
121         READ_UE(p_bitctx, &temp); //!< log2_weight_denom
122     }
123     for (i = 0; i < currSlice->num_ref_idx_active[LIST_0]; i++) {
124         READ_ONEBIT(p_bitctx, &temp); //!< luma_weight_flag_l0
125         if (temp) {
126             READ_SE(p_bitctx, &se_tmp); //!< slice->wp_weight[LIST_0][i][0]
127             READ_SE(p_bitctx, &se_tmp); //!< slice->wp_offset[LIST_0][i][0]
128         }
129         if (currSlice->active_sps->chroma_format_idc) {
130             READ_ONEBIT(p_bitctx, &temp); //!< chroma_weight_flag_l0
131             for (j = 1; j < 3; j++) {
132                 if (temp) { //!< chroma_weight_flag_l0
133                     READ_SE(p_bitctx, &se_tmp); //!< slice->wp_weight[LIST_0][i][j]
134                     READ_SE(p_bitctx, &se_tmp); //!< slice->wp_offset[LIST_0][i][j]
135                 }
136             }
137         }
138     }
139 
140     if ((currSlice->slice_type == H264_B_SLICE) && currSlice->p_Vid->active_pps->weighted_bipred_idc == 1) {
141         for (i = 0; i < currSlice->num_ref_idx_active[LIST_1]; i++) {
142             READ_ONEBIT(p_bitctx, &temp); //!< luma_weight_flag_l1
143             if (temp) {
144                 READ_SE(p_bitctx, &se_tmp); //!< slice->wp_weight[LIST_1][i][0]
145                 READ_SE(p_bitctx, &se_tmp); //!< slice->wp_offset[LIST_1][i][0]
146             }
147             if (currSlice->active_sps->chroma_format_idc) {
148                 READ_ONEBIT(p_bitctx, &temp); //!< chroma_weight_flag_l1
149                 for (j = 1; j < 3; j++) {
150                     if (temp) { // chroma_weight_flag_l1
151                         READ_SE(p_bitctx, &se_tmp); //!< slice->wp_weight[LIST_1][i][j]
152                         READ_SE(p_bitctx, &se_tmp); //!< slice->wp_offset[LIST_1][i][j]
153                     }
154                 }
155             }
156         }
157     }
158 
159     return ret = MPP_OK;
160 __BITREAD_ERR:
161     return ret = p_bitctx->ret;
162 }
163 
dec_ref_pic_marking(H264_SLICE_t * pSlice)164 static MPP_RET dec_ref_pic_marking(H264_SLICE_t *pSlice)
165 {
166     RK_U32 val = 0;
167     MPP_RET ret = MPP_ERR_UNKNOW;
168     RK_U32 drpm_used_bits = 0;
169     RK_U32 emulation_prevention = 0;
170     H264_DRPM_t *tmp_drpm = NULL, *tmp_drpm2 = NULL;
171     H264dVideoCtx_t *p_Vid = pSlice->p_Vid;
172     BitReadCtx_t *p_bitctx = &pSlice->p_Cur->bitctx;
173 
174     drpm_used_bits = p_bitctx->used_bits;
175     emulation_prevention = p_bitctx->emulation_prevention_bytes_;
176     pSlice->drpm_used_bitlen = 0;
177 
178     if (pSlice->idr_flag ||
179         (pSlice->svc_extension_flag == 0 && pSlice->mvcExt.non_idr_flag == 0)) {
180         READ_ONEBIT(p_bitctx, &pSlice->no_output_of_prior_pics_flag);
181         p_Vid->no_output_of_prior_pics_flag = pSlice->no_output_of_prior_pics_flag;
182         READ_ONEBIT(p_bitctx, &pSlice->long_term_reference_flag);
183     } else {
184         READ_ONEBIT(p_bitctx, &pSlice->adaptive_ref_pic_buffering_flag);
185 
186         if (pSlice->adaptive_ref_pic_buffering_flag) {
187             RK_U32 i = 0;
188 
189             for (i = 0; i < MAX_MARKING_TIMES; i++) {
190                 if (!pSlice->p_Cur->dec_ref_pic_marking_buffer[i])
191                     pSlice->p_Cur->dec_ref_pic_marking_buffer[i] = mpp_calloc(H264_DRPM_t, 1);
192                 tmp_drpm = pSlice->p_Cur->dec_ref_pic_marking_buffer[i];
193                 tmp_drpm->Next = NULL;
194                 READ_UE(p_bitctx, &val); //!< mmco
195                 tmp_drpm->memory_management_control_operation = val;
196                 if (val == 0)
197                     break;
198 
199                 if ((val == 1) || (val == 3)) {
200                     READ_UE(p_bitctx, &tmp_drpm->difference_of_pic_nums_minus1);
201                 }
202                 if (val == 2) {
203                     READ_UE(p_bitctx, &tmp_drpm->long_term_pic_num);
204                 }
205                 if ((val == 3) || (val == 6)) {
206                     READ_UE(p_bitctx, &tmp_drpm->long_term_frame_idx);
207                 }
208                 if (val == 4) {
209                     READ_UE(p_bitctx, &tmp_drpm->max_long_term_frame_idx_plus1);
210                 }
211                 // add command
212                 if (pSlice->dec_ref_pic_marking_buffer == NULL) {
213                     pSlice->dec_ref_pic_marking_buffer = tmp_drpm;
214                 } else {
215                     tmp_drpm2 = pSlice->dec_ref_pic_marking_buffer;
216                     while (tmp_drpm2->Next != NULL) {
217                         tmp_drpm2 = tmp_drpm2->Next;
218                     }
219                     tmp_drpm2->Next = tmp_drpm;
220                 }
221             }
222             if (i >= MAX_MARKING_TIMES) {
223                 H264D_ERR("Too many memory management control operations.");
224                 goto __BITREAD_ERR;
225             }
226         }
227     }
228 
229     // need to minus emulation prevention bytes(0x000003) we met
230     emulation_prevention = p_bitctx->emulation_prevention_bytes_ - emulation_prevention;
231     pSlice->drpm_used_bitlen = p_bitctx->used_bits - drpm_used_bits - (emulation_prevention * 8);
232 
233     return ret = MPP_OK;
234 __BITREAD_ERR:
235     ret = p_bitctx->ret;
236 
237     return ret;
238 }
239 
init_slice_parmeters(H264_SLICE_t * currSlice)240 static void init_slice_parmeters(H264_SLICE_t *currSlice)
241 {
242     H264dVideoCtx_t *p_Vid   = currSlice->p_Vid;
243     H264_Nalu_t    *cur_nalu = &currSlice->p_Cur->nalu;
244 
245     //--- init slice syntax
246     currSlice->idr_flag = cur_nalu->nalu_type == H264_NALU_TYPE_IDR;
247     currSlice->nal_reference_idc = cur_nalu->nal_reference_idc;
248     //!< set ref flag and dpb error flag
249     {
250         p_Vid->p_Dec->errctx.used_ref_flag = currSlice->nal_reference_idc ? 1 : 0;
251         if (currSlice->slice_type == H264_I_SLICE) {
252             p_Vid->p_Dec->errctx.dpb_err_flag = 0;
253         }
254     }
255     if ((!currSlice->svc_extension_flag) || currSlice->mvcExt.iPrefixNALU) { // MVC or have prefixNALU
256         currSlice->view_id = currSlice->mvcExt.view_id;
257         currSlice->inter_view_flag = currSlice->mvcExt.inter_view_flag;
258         currSlice->anchor_pic_flag = currSlice->mvcExt.anchor_pic_flag;
259     } else if (currSlice->svc_extension_flag == -1) { // normal AVC
260         currSlice->view_id = currSlice->mvcExt.valid ? p_Vid->active_subsps->view_id[0] : 0;
261         currSlice->inter_view_flag = 1;
262         currSlice->anchor_pic_flag = currSlice->idr_flag;
263     }
264     currSlice->layer_id = currSlice->view_id;
265     if (currSlice->layer_id >= 0) { // if not found, layer_id == -1
266         currSlice->p_Dpb = p_Vid->p_Dpb_layer[currSlice->layer_id];
267     }
268 }
269 
check_sps_pps(H264_SPS_t * sps,H264_subSPS_t * subset_sps,H264_PPS_t * pps,const MppDecHwCap * hw_info)270 static MPP_RET check_sps_pps(H264_SPS_t *sps, H264_subSPS_t *subset_sps,
271                              H264_PPS_t *pps, const MppDecHwCap *hw_info)
272 {
273     RK_U32 ret = 0;
274     RK_S32 max_mb_width  = MAX_MBW_1080P;
275 
276     ret |= (sps->seq_parameter_set_id > 31);
277     ret |= (sps->separate_colour_plane_flag == 1);
278     ret |= (sps->chroma_format_idc == 3);
279     ret |= (sps->bit_depth_luma_minus8 > 2);
280     ret |= (sps->bit_depth_chroma_minus8 > 2);
281     ret |= (sps->log2_max_frame_num_minus4 > 12);
282     ret |= (sps->pic_order_cnt_type > 2);
283     ret |= (sps->log2_max_pic_order_cnt_lsb_minus4 > 12);
284     ret |= (sps->num_ref_frames_in_pic_order_cnt_cycle > 255);
285     ret |= (sps->max_num_ref_frames > 16);
286     ret |= (sps->profile_idc == 244);
287 
288     if (hw_info && hw_info->cap_8k)
289         max_mb_width  = MAX_MBW_8Kx4K * hw_info->cap_core_num;
290     else if (hw_info && hw_info->cap_4k)
291         max_mb_width  = MAX_MBW_4Kx2K;
292 
293     ret |= (sps->pic_width_in_mbs_minus1 < 3 || sps->pic_width_in_mbs_minus1 > max_mb_width);
294 
295     if (ret) {
296         H264D_ERR("sps has error, sps_id=%d", sps->seq_parameter_set_id);
297         goto __FAILED;
298     }
299     if (subset_sps) { //!< MVC
300         ret |= (subset_sps->num_views_minus1 != 1);
301         if (subset_sps->num_anchor_refs_l0[0] > 0)
302             ret |= (subset_sps->anchor_ref_l0[0][0] != subset_sps->view_id[0]);
303         if (subset_sps->num_anchor_refs_l1[0] > 0)
304             ret |= (subset_sps->anchor_ref_l1[0][0] != subset_sps->view_id[1]);
305         if (subset_sps->num_non_anchor_refs_l0[0] > 0)
306             ret |= (subset_sps->non_anchor_ref_l0[0][0] != subset_sps->view_id[0]);
307         if (subset_sps->num_non_anchor_refs_l1[0] > 0)
308             ret |= (subset_sps->non_anchor_ref_l1[0][0] != subset_sps->view_id[1]);
309         if (ret) {
310             H264D_ERR("subsps has error, sps_id=%d", sps->seq_parameter_set_id);
311             goto __FAILED;
312         }
313     }
314     //!< check PPS
315     ret |= (pps->pic_parameter_set_id > 255);
316     ret |= (pps->seq_parameter_set_id > 31);
317     ret |= (pps->num_slice_groups_minus1 > 0);
318     ret |= (pps->num_ref_idx_l0_default_active_minus1 > 31);
319     ret |= (pps->num_ref_idx_l1_default_active_minus1 > 31);
320     ret |= (pps->pic_init_qp_minus26 > 25 || pps->pic_init_qp_minus26 < -(26 + 6 * (RK_S32)sps->bit_depth_luma_minus8));
321     ret |= (pps->pic_init_qs_minus26 > 25 || pps->pic_init_qs_minus26 < -26);
322     ret |= (pps->chroma_qp_index_offset > 12 || pps->chroma_qp_index_offset < -12);
323     ret |= (pps->redundant_pic_cnt_present_flag == 1);
324     if (ret) {
325         H264D_ERR("pps has error, sps_id=%d, pps_id", sps->seq_parameter_set_id, pps->pic_parameter_set_id);
326         goto __FAILED;
327     }
328     return MPP_OK;
329 __FAILED:
330     return MPP_NOK;
331 }
332 
333 
set_slice_user_parmeters(H264_SLICE_t * currSlice)334 static MPP_RET set_slice_user_parmeters(H264_SLICE_t *currSlice)
335 {
336     MPP_RET ret = MPP_ERR_UNKNOW;
337     H264_PPS_t *cur_pps = NULL;
338     H264_SPS_t *cur_sps = NULL;
339     H264_subSPS_t *cur_subsps = NULL;
340     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
341 
342     //!< use parameter set
343     if (currSlice->pic_parameter_set_id < MAXPPS) {
344         cur_pps = p_Vid->ppsSet[currSlice->pic_parameter_set_id];
345         cur_pps = (cur_pps && cur_pps->Valid) ? cur_pps : NULL;
346     }
347     VAL_CHECK(ret, cur_pps != NULL);
348 
349     if (currSlice->mvcExt.valid) {
350         cur_subsps = p_Vid->subspsSet[cur_pps->seq_parameter_set_id];
351         // only num view(except base view) > 0, need to combine view id
352         if (cur_subsps && cur_subsps->Valid && cur_subsps->num_views_minus1 > 0) {
353             cur_sps = &cur_subsps->sps;
354             if ((RK_S32)currSlice->mvcExt.view_id == cur_subsps->view_id[0]) { // combine subsps to sps
355                 p_Vid->active_mvc_sps_flag = 0;
356                 cur_subsps = NULL;
357                 cur_sps = p_Vid->spsSet[cur_pps->seq_parameter_set_id];
358             } else if ((RK_S32)currSlice->mvcExt.view_id == cur_subsps->view_id[1]) {
359                 p_Vid->active_mvc_sps_flag = 1;
360             }
361         } else {
362             p_Vid->active_mvc_sps_flag = 0;
363             cur_sps = p_Vid->spsSet[cur_pps->seq_parameter_set_id];
364             cur_subsps = NULL;
365         }
366     } else {
367         p_Vid->active_mvc_sps_flag = 0;
368         cur_sps = p_Vid->spsSet[cur_pps->seq_parameter_set_id];
369         cur_subsps = NULL;
370     }
371 
372     if (p_Vid->active_mvc_sps_flag) { // layer_id == 1
373         cur_subsps = (cur_subsps && cur_subsps->Valid) ? cur_subsps : NULL;
374         VAL_CHECK(ret, cur_subsps != NULL);
375         cur_sps = &cur_subsps->sps;
376     } else { //!< layer_id == 0
377         cur_subsps = NULL;
378         cur_sps = (cur_sps && cur_sps->Valid) ? cur_sps : NULL;
379     }
380     VAL_CHECK(ret, cur_sps);
381     VAL_CHECK(ret, check_sps_pps(cur_sps, cur_subsps, cur_pps, p_Vid->p_Dec->hw_info) != MPP_NOK);
382 
383     FUN_CHECK(ret = activate_sps(p_Vid, cur_sps, cur_subsps));
384     FUN_CHECK(ret = activate_pps(p_Vid, cur_pps));
385 
386     if (p_Vid->last_sps_id != cur_sps->seq_parameter_set_id ||
387         p_Vid->last_pps_id != cur_pps->pic_parameter_set_id) {
388         p_Vid->last_sps_id = cur_sps->seq_parameter_set_id;
389         p_Vid->last_pps_id = cur_pps->pic_parameter_set_id;
390         p_Vid->spspps_update = 1;
391     }
392 
393     /* NOTE: the SVC is not supported by hardware, so it is dropped.
394      * Or svc_extension_flag doesn't equal to -1, it should apply the
395      * subset_seq_parameter_set_rbsp().
396      */
397     if (p_Vid->p_Dec->mvc_valid) {
398         struct h264_subsps_t *active_subsps = NULL;
399 
400         active_subsps = p_Vid->subspsSet[cur_pps->seq_parameter_set_id];
401         if (active_subsps && active_subsps->Valid)
402             p_Vid->active_subsps = active_subsps;
403         else
404             p_Vid->active_subsps = NULL;
405     }
406 
407     currSlice->active_sps = p_Vid->active_sps;
408     currSlice->active_pps = p_Vid->active_pps;
409 
410     p_Vid->type = currSlice->slice_type;
411     return MPP_OK;
412 __FAILED:
413     return ret;
414 }
415 
416 
417 /*!
418 ***********************************************************************
419 * \brief
420 *    reset current slice buffer
421 ***********************************************************************
422 */
423 //extern "C"
reset_cur_slice(H264dCurCtx_t * p_Cur,H264_SLICE_t * p)424 MPP_RET reset_cur_slice(H264dCurCtx_t *p_Cur, H264_SLICE_t *p)
425 {
426     if (p) {
427         p->modification_of_pic_nums_idc[LIST_0] = p_Cur->modification_of_pic_nums_idc[LIST_0];
428         p->abs_diff_pic_num_minus1[LIST_0]      = p_Cur->abs_diff_pic_num_minus1[LIST_0];
429         p->long_term_pic_idx[LIST_0]            = p_Cur->long_term_pic_idx[LIST_0];
430         p->abs_diff_view_idx_minus1[LIST_0]     = p_Cur->abs_diff_view_idx_minus1[LIST_0];
431 
432         p->modification_of_pic_nums_idc[LIST_1] = p_Cur->modification_of_pic_nums_idc[LIST_1];
433         p->abs_diff_pic_num_minus1[LIST_1]      = p_Cur->abs_diff_pic_num_minus1[LIST_1];
434         p->long_term_pic_idx[LIST_1]            = p_Cur->long_term_pic_idx[LIST_1];
435         p->abs_diff_view_idx_minus1[LIST_1]     = p_Cur->abs_diff_view_idx_minus1[LIST_1];
436 
437         p->dec_ref_pic_marking_buffer = NULL;
438     }
439 
440     return MPP_OK;
441 }
442 
443 /*!
444 ***********************************************************************
445 * \brief
446 *    parse SEI information
447 ***********************************************************************
448 */
449 //extern "C"
process_slice(H264_SLICE_t * currSlice)450 MPP_RET process_slice(H264_SLICE_t *currSlice)
451 {
452     RK_U32 temp = 0;
453     RK_U32 poc_used_bits = 0;
454     RK_U32 emulation_prevention = 0;
455     MPP_RET ret = MPP_ERR_UNKNOW;
456     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
457     H264dCurCtx_t *p_Cur = currSlice->p_Cur;
458     BitReadCtx_t *p_bitctx = &p_Cur->bitctx;
459     RecoveryPoint *recovery = &p_Vid->recovery;
460 
461     //!< initial value
462     currSlice->p_Dpb_layer[0] = p_Vid->p_Dpb_layer[0];
463     currSlice->p_Dpb_layer[1] = p_Vid->p_Dpb_layer[1];
464 
465     //!< read slice head syntax
466     READ_UE(p_bitctx, &currSlice->start_mb_nr); //!< first_mb_in_slice
467     READ_UE(p_bitctx, &temp); //!< slice_type
468     p_Vid->slice_type = currSlice->slice_type = temp % 5;
469     if (p_Vid->slice_type == H264_SP_SLICE || p_Vid->slice_type == H264_SI_SLICE) {
470         H264D_WARNNING("sp or si slice not support\n");
471         goto __FAILED;
472     }
473     READ_UE(p_bitctx, &currSlice->pic_parameter_set_id);
474     init_slice_parmeters(currSlice);
475     FUN_CHECK(ret = set_slice_user_parmeters(currSlice));
476     //!< read rest slice header syntax
477     {
478         READ_BITS(p_bitctx, currSlice->active_sps->log2_max_frame_num_minus4 + 4, &currSlice->frame_num);
479 
480         if (recovery->valid_flag) {
481             if (!recovery->first_frm_valid) {
482                 recovery->first_frm_id = currSlice->frame_num;
483                 recovery->first_frm_valid = 1;
484                 recovery->recovery_pic_id = recovery->first_frm_id + recovery->recovery_frame_cnt;
485                 H264D_DBG(H264D_DBG_SEI, "First recovery frame found, frame_num %d", currSlice->frame_num);
486             } else {
487                 // It may be too early to reset recovery point info when frame_num is wrapped
488                 if (recovery->recovery_pic_id % p_Vid->max_frame_num <= currSlice->frame_num)
489                     memset(&p_Vid->recovery, 0, sizeof(RecoveryPoint));
490             }
491         }
492 
493         if (currSlice->active_sps->frame_mbs_only_flag) { //!< user in_slice info
494             p_Vid->structure = FRAME;
495             currSlice->field_pic_flag = 0;
496             currSlice->bottom_field_flag = 0;
497         } else {
498             READ_ONEBIT(p_bitctx, &currSlice->field_pic_flag);
499             if (currSlice->field_pic_flag) {
500                 READ_ONEBIT(p_bitctx, &currSlice->bottom_field_flag);
501                 p_Vid->structure = currSlice->bottom_field_flag ? BOTTOM_FIELD : TOP_FIELD;
502             } else {
503                 p_Vid->structure = FRAME;
504                 currSlice->bottom_field_flag = 0;
505             }
506         }
507         currSlice->structure = p_Vid->structure;
508         currSlice->mb_aff_frame_flag = currSlice->active_sps->mb_adaptive_frame_field_flag &&
509                                        !currSlice->field_pic_flag;
510         if (currSlice->idr_flag) {
511             READ_UE(p_bitctx, &currSlice->idr_pic_id);
512         } else if (currSlice->svc_extension_flag == 0 && currSlice->mvcExt.non_idr_flag == 0) {
513             READ_UE(p_bitctx, &currSlice->idr_pic_id);
514         }
515         poc_used_bits = p_bitctx->used_bits; //!< init poc used bits
516         emulation_prevention = p_bitctx->emulation_prevention_bytes_;
517         if (currSlice->active_sps->pic_order_cnt_type == 0) {
518             READ_BITS(p_bitctx, currSlice->active_sps->log2_max_pic_order_cnt_lsb_minus4 + 4, &currSlice->pic_order_cnt_lsb);
519             if (currSlice->p_Vid->active_pps->bottom_field_pic_order_in_frame_present_flag == 1
520                 && !currSlice->field_pic_flag) {
521                 READ_SE(p_bitctx, &currSlice->delta_pic_order_cnt_bottom);
522             } else {
523                 currSlice->delta_pic_order_cnt_bottom = 0;
524             }
525         }
526         if (currSlice->active_sps->pic_order_cnt_type == 1) {
527             if (!currSlice->active_sps->delta_pic_order_always_zero_flag) {
528                 READ_SE(p_bitctx, &currSlice->delta_pic_order_cnt[0]);
529 
530                 if (currSlice->p_Vid->active_pps->bottom_field_pic_order_in_frame_present_flag == 1 && !currSlice->field_pic_flag) {
531                     READ_SE(p_bitctx, &currSlice->delta_pic_order_cnt[1]);
532                 } else {
533                     currSlice->delta_pic_order_cnt[1] = 0;  //!< set to zero if not in stream
534                 }
535             } else {
536                 currSlice->delta_pic_order_cnt[0] = 0;
537                 currSlice->delta_pic_order_cnt[1] = 0;
538             }
539         }
540 
541         // need to minus emulation prevention bytes(0x000003) we met
542         emulation_prevention = p_bitctx->emulation_prevention_bytes_ - emulation_prevention;
543         currSlice->poc_used_bitlen = p_bitctx->used_bits - poc_used_bits - (emulation_prevention * 8); //!< calculate poc used bit length
544         //!< redundant_pic_cnt is missing here
545         ASSERT(currSlice->p_Vid->active_pps->redundant_pic_cnt_present_flag == 0); // add by dw, high 4:2:2 profile not support
546         if (currSlice->p_Vid->active_pps->redundant_pic_cnt_present_flag) {
547             READ_UE(p_bitctx, &currSlice->redundant_pic_cnt);
548         }
549 
550         if (currSlice->slice_type == H264_B_SLICE) {
551             READ_ONEBIT(p_bitctx, &currSlice->direct_spatial_mv_pred_flag);
552         } else {
553             currSlice->direct_spatial_mv_pred_flag = 0;
554         }
555         currSlice->num_ref_idx_active[LIST_0] = currSlice->p_Vid->active_pps->num_ref_idx_l0_default_active_minus1 + 1;
556         currSlice->num_ref_idx_active[LIST_1] = currSlice->p_Vid->active_pps->num_ref_idx_l1_default_active_minus1 + 1;
557 
558         if (currSlice->slice_type == H264_P_SLICE
559             || currSlice->slice_type == H264_SP_SLICE || currSlice->slice_type == H264_B_SLICE) {
560             READ_ONEBIT(p_bitctx, &currSlice->num_ref_idx_override_flag);
561             if (currSlice->num_ref_idx_override_flag) {
562                 READ_UE(p_bitctx, &currSlice->num_ref_idx_active[LIST_0]);
563                 currSlice->num_ref_idx_active[LIST_0] += 1;
564                 if (currSlice->slice_type == H264_B_SLICE) {
565                     READ_UE(p_bitctx, &currSlice->num_ref_idx_active[LIST_1]);
566                     currSlice->num_ref_idx_active[LIST_1] += 1;
567                 }
568             }
569         }
570         if (currSlice->slice_type != H264_B_SLICE) {
571             currSlice->num_ref_idx_active[LIST_1] = 0;
572         }
573         FUN_CHECK(ret = ref_pic_list_mvc_modification(currSlice));
574         if ((currSlice->p_Vid->active_pps->weighted_pred_flag
575              && (currSlice->slice_type == H264_P_SLICE || currSlice->slice_type == H264_SP_SLICE))
576             || (currSlice->p_Vid->active_pps->weighted_bipred_idc == 1 && (currSlice->slice_type == H264_B_SLICE))) {
577             FUN_CHECK(ret = pred_weight_table(currSlice));
578         }
579         currSlice->drpm_used_bitlen = 0;
580         if (currSlice->nal_reference_idc) {
581             FUN_CHECK(ret = dec_ref_pic_marking(currSlice));
582         }
583         H264D_DBG(H264D_DBG_PPS_SPS, "[SLICE_HEAD] type=%d, layer_id=%d,sps_id=%d, pps_id=%d, struct=%d, frame_num=%d",
584                   currSlice->slice_type, currSlice->layer_id, currSlice->active_sps->seq_parameter_set_id,
585                   currSlice->active_pps->pic_parameter_set_id, currSlice->structure, currSlice->frame_num);
586     }
587 
588     return ret = MPP_OK;
589 __BITREAD_ERR:
590     ret = p_bitctx->ret;
591 __FAILED:
592     return ret;
593 }
594