xref: /rockchip-linux_mpp/mpp/codec/dec/h264/h264d_slice.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
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->qpprime_y_zero_transform_bypass_flag == 1);
287 
288     if (ret) {
289         H264D_ERR("sps has error, sps_id=%d", sps->seq_parameter_set_id);
290         goto __FAILED;
291     }
292 
293     if (hw_info && hw_info->cap_8k)
294         max_mb_width  = MAX_MBW_8Kx4K * hw_info->cap_core_num;
295     else if (hw_info && hw_info->cap_4k)
296         max_mb_width  = MAX_MBW_4Kx2K;
297 
298     ret |= (sps->pic_width_in_mbs_minus1 < 3);
299     if (ret) {
300         H264D_ERR("sps %d too small width %d\n", sps->seq_parameter_set_id,
301                   (sps->pic_width_in_mbs_minus1 + 1) * 16);
302         goto __FAILED;
303     }
304     ret |= (sps->pic_width_in_mbs_minus1 > max_mb_width);
305     if (ret) {
306         H264D_ERR("width %d is larger than soc max support %d\n",
307                   (sps->pic_width_in_mbs_minus1 + 1) * 16, max_mb_width * 16);
308         goto __FAILED;
309     }
310 
311     if (subset_sps) { //!< MVC
312         ret |= (subset_sps->num_views_minus1 != 1);
313         if (subset_sps->num_anchor_refs_l0[0] > 0)
314             ret |= (subset_sps->anchor_ref_l0[0][0] != subset_sps->view_id[0]);
315         if (subset_sps->num_anchor_refs_l1[0] > 0)
316             ret |= (subset_sps->anchor_ref_l1[0][0] != subset_sps->view_id[1]);
317         if (subset_sps->num_non_anchor_refs_l0[0] > 0)
318             ret |= (subset_sps->non_anchor_ref_l0[0][0] != subset_sps->view_id[0]);
319         if (subset_sps->num_non_anchor_refs_l1[0] > 0)
320             ret |= (subset_sps->non_anchor_ref_l1[0][0] != subset_sps->view_id[1]);
321         if (ret) {
322             H264D_ERR("subsps has error, sps_id=%d", sps->seq_parameter_set_id);
323             goto __FAILED;
324         }
325     }
326     //!< check PPS
327     ret |= (pps->pic_parameter_set_id > 255);
328     ret |= (pps->seq_parameter_set_id > 31);
329     ret |= (pps->num_slice_groups_minus1 > 0);
330     ret |= (pps->num_ref_idx_l0_default_active_minus1 > 31);
331     ret |= (pps->num_ref_idx_l1_default_active_minus1 > 31);
332     ret |= (pps->pic_init_qp_minus26 > 25 || pps->pic_init_qp_minus26 < -(26 + 6 * (RK_S32)sps->bit_depth_luma_minus8));
333     ret |= (pps->pic_init_qs_minus26 > 25 || pps->pic_init_qs_minus26 < -26);
334     ret |= (pps->chroma_qp_index_offset > 12 || pps->chroma_qp_index_offset < -12);
335     ret |= (pps->redundant_pic_cnt_present_flag == 1);
336     if (ret) {
337         H264D_ERR("pps has error, sps_id=%d, pps_id", sps->seq_parameter_set_id, pps->pic_parameter_set_id);
338         goto __FAILED;
339     }
340     return MPP_OK;
341 __FAILED:
342     return MPP_NOK;
343 }
344 
345 
set_slice_user_parmeters(H264_SLICE_t * currSlice)346 static MPP_RET set_slice_user_parmeters(H264_SLICE_t *currSlice)
347 {
348     MPP_RET ret = MPP_ERR_UNKNOW;
349     H264_PPS_t *cur_pps = NULL;
350     H264_SPS_t *cur_sps = NULL;
351     H264_subSPS_t *cur_subsps = NULL;
352     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
353 
354     //!< use parameter set
355     if (currSlice->pic_parameter_set_id < MAXPPS) {
356         cur_pps = p_Vid->ppsSet[currSlice->pic_parameter_set_id];
357         cur_pps = (cur_pps && cur_pps->Valid) ? cur_pps : NULL;
358     }
359     VAL_CHECK(ret, cur_pps != NULL);
360 
361     if (currSlice->mvcExt.valid) {
362         cur_subsps = p_Vid->subspsSet[cur_pps->seq_parameter_set_id];
363         // only num view(except base view) > 0, need to combine view id
364         if (cur_subsps && cur_subsps->Valid && cur_subsps->num_views_minus1 > 0) {
365             cur_sps = &cur_subsps->sps;
366             if ((RK_S32)currSlice->mvcExt.view_id == cur_subsps->view_id[0]) { // combine subsps to sps
367                 p_Vid->active_mvc_sps_flag = 0;
368                 cur_subsps = NULL;
369                 cur_sps = p_Vid->spsSet[cur_pps->seq_parameter_set_id];
370             } else if ((RK_S32)currSlice->mvcExt.view_id == cur_subsps->view_id[1]) {
371                 p_Vid->active_mvc_sps_flag = 1;
372             }
373         } else {
374             p_Vid->active_mvc_sps_flag = 0;
375             cur_sps = p_Vid->spsSet[cur_pps->seq_parameter_set_id];
376             cur_subsps = NULL;
377         }
378     } else {
379         p_Vid->active_mvc_sps_flag = 0;
380         cur_sps = p_Vid->spsSet[cur_pps->seq_parameter_set_id];
381         cur_subsps = NULL;
382     }
383 
384     if (p_Vid->active_mvc_sps_flag) { // layer_id == 1
385         cur_subsps = (cur_subsps && cur_subsps->Valid) ? cur_subsps : NULL;
386         VAL_CHECK(ret, cur_subsps != NULL);
387         cur_sps = &cur_subsps->sps;
388     } else { //!< layer_id == 0
389         cur_subsps = NULL;
390         cur_sps = (cur_sps && cur_sps->Valid) ? cur_sps : NULL;
391     }
392     VAL_CHECK(ret, cur_sps);
393     VAL_CHECK(ret, check_sps_pps(cur_sps, cur_subsps, cur_pps, p_Vid->p_Dec->hw_info) != MPP_NOK);
394 
395     FUN_CHECK(ret = activate_sps(p_Vid, cur_sps, cur_subsps));
396     FUN_CHECK(ret = activate_pps(p_Vid, cur_pps));
397 
398     if (p_Vid->last_sps_id != cur_sps->seq_parameter_set_id ||
399         p_Vid->last_pps_id != cur_pps->pic_parameter_set_id) {
400         p_Vid->last_sps_id = cur_sps->seq_parameter_set_id;
401         p_Vid->last_pps_id = cur_pps->pic_parameter_set_id;
402         p_Vid->spspps_update = 1;
403     }
404 
405     /* NOTE: the SVC is not supported by hardware, so it is dropped.
406      * Or svc_extension_flag doesn't equal to -1, it should apply the
407      * subset_seq_parameter_set_rbsp().
408      */
409     if (p_Vid->p_Dec->mvc_valid) {
410         struct h264_subsps_t *active_subsps = NULL;
411 
412         active_subsps = p_Vid->subspsSet[cur_pps->seq_parameter_set_id];
413         if (active_subsps && active_subsps->Valid)
414             p_Vid->active_subsps = active_subsps;
415         else
416             p_Vid->active_subsps = NULL;
417     }
418 
419     currSlice->active_sps = p_Vid->active_sps;
420     currSlice->active_pps = p_Vid->active_pps;
421 
422     p_Vid->type = currSlice->slice_type;
423     return MPP_OK;
424 __FAILED:
425     return ret;
426 }
427 
428 
429 /*!
430 ***********************************************************************
431 * \brief
432 *    reset current slice buffer
433 ***********************************************************************
434 */
435 //extern "C"
reset_cur_slice(H264dCurCtx_t * p_Cur,H264_SLICE_t * p)436 MPP_RET reset_cur_slice(H264dCurCtx_t *p_Cur, H264_SLICE_t *p)
437 {
438     if (p) {
439         p->modification_of_pic_nums_idc[LIST_0] = p_Cur->modification_of_pic_nums_idc[LIST_0];
440         p->abs_diff_pic_num_minus1[LIST_0]      = p_Cur->abs_diff_pic_num_minus1[LIST_0];
441         p->long_term_pic_idx[LIST_0]            = p_Cur->long_term_pic_idx[LIST_0];
442         p->abs_diff_view_idx_minus1[LIST_0]     = p_Cur->abs_diff_view_idx_minus1[LIST_0];
443 
444         p->modification_of_pic_nums_idc[LIST_1] = p_Cur->modification_of_pic_nums_idc[LIST_1];
445         p->abs_diff_pic_num_minus1[LIST_1]      = p_Cur->abs_diff_pic_num_minus1[LIST_1];
446         p->long_term_pic_idx[LIST_1]            = p_Cur->long_term_pic_idx[LIST_1];
447         p->abs_diff_view_idx_minus1[LIST_1]     = p_Cur->abs_diff_view_idx_minus1[LIST_1];
448 
449         p->dec_ref_pic_marking_buffer = NULL;
450     }
451 
452     return MPP_OK;
453 }
454 
455 /*!
456 ***********************************************************************
457 * \brief
458 *    parse SEI information
459 ***********************************************************************
460 */
461 //extern "C"
process_slice(H264_SLICE_t * currSlice)462 MPP_RET process_slice(H264_SLICE_t *currSlice)
463 {
464     RK_U32 temp = 0;
465     RK_U32 poc_used_bits = 0;
466     RK_U32 emulation_prevention = 0;
467     MPP_RET ret = MPP_ERR_UNKNOW;
468     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
469     H264dCurCtx_t *p_Cur = currSlice->p_Cur;
470     BitReadCtx_t *p_bitctx = &p_Cur->bitctx;
471     RecoveryPoint *recovery = &p_Vid->recovery;
472 
473     //!< initial value
474     currSlice->p_Dpb_layer[0] = p_Vid->p_Dpb_layer[0];
475     currSlice->p_Dpb_layer[1] = p_Vid->p_Dpb_layer[1];
476 
477     //!< read slice head syntax
478     READ_UE(p_bitctx, &currSlice->start_mb_nr); //!< first_mb_in_slice
479     READ_UE(p_bitctx, &temp); //!< slice_type
480     p_Vid->slice_type = currSlice->slice_type = temp % 5;
481     if (p_Vid->slice_type == H264_SP_SLICE || p_Vid->slice_type == H264_SI_SLICE) {
482         H264D_WARNNING("sp or si slice not support\n");
483         goto __FAILED;
484     }
485     READ_UE(p_bitctx, &currSlice->pic_parameter_set_id);
486     init_slice_parmeters(currSlice);
487     FUN_CHECK(ret = set_slice_user_parmeters(currSlice));
488     //!< read rest slice header syntax
489     {
490         READ_BITS(p_bitctx, currSlice->active_sps->log2_max_frame_num_minus4 + 4, &currSlice->frame_num);
491 
492         if (recovery->valid_flag) {
493             if (!recovery->first_frm_valid) {
494                 recovery->first_frm_id = currSlice->frame_num;
495                 recovery->first_frm_valid = 1;
496                 recovery->recovery_pic_id = recovery->first_frm_id + recovery->recovery_frame_cnt;
497                 H264D_DBG(H264D_DBG_SEI, "First recovery frame found, frame_num %d", currSlice->frame_num);
498             } else {
499                 // It may be too early to reset recovery point info when frame_num is wrapped
500                 if (recovery->recovery_pic_id % p_Vid->max_frame_num < currSlice->frame_num)
501                     memset(&p_Vid->recovery, 0, sizeof(RecoveryPoint));
502             }
503         }
504 
505         if (currSlice->active_sps->frame_mbs_only_flag) { //!< user in_slice info
506             p_Vid->structure = FRAME;
507             currSlice->field_pic_flag = 0;
508             currSlice->bottom_field_flag = 0;
509         } else {
510             READ_ONEBIT(p_bitctx, &currSlice->field_pic_flag);
511             if (currSlice->field_pic_flag) {
512                 READ_ONEBIT(p_bitctx, &currSlice->bottom_field_flag);
513                 p_Vid->structure = currSlice->bottom_field_flag ? BOTTOM_FIELD : TOP_FIELD;
514             } else {
515                 p_Vid->structure = FRAME;
516                 currSlice->bottom_field_flag = 0;
517             }
518         }
519         currSlice->structure = p_Vid->structure;
520         currSlice->mb_aff_frame_flag = currSlice->active_sps->mb_adaptive_frame_field_flag &&
521                                        !currSlice->field_pic_flag;
522         if (currSlice->idr_flag) {
523             READ_UE(p_bitctx, &currSlice->idr_pic_id);
524         } else if (currSlice->svc_extension_flag == 0 && currSlice->mvcExt.non_idr_flag == 0) {
525             READ_UE(p_bitctx, &currSlice->idr_pic_id);
526         }
527         poc_used_bits = p_bitctx->used_bits; //!< init poc used bits
528         emulation_prevention = p_bitctx->emulation_prevention_bytes_;
529         if (currSlice->active_sps->pic_order_cnt_type == 0) {
530             READ_BITS(p_bitctx, currSlice->active_sps->log2_max_pic_order_cnt_lsb_minus4 + 4, &currSlice->pic_order_cnt_lsb);
531             if (currSlice->p_Vid->active_pps->bottom_field_pic_order_in_frame_present_flag == 1
532                 && !currSlice->field_pic_flag) {
533                 READ_SE(p_bitctx, &currSlice->delta_pic_order_cnt_bottom);
534             } else {
535                 currSlice->delta_pic_order_cnt_bottom = 0;
536             }
537         }
538         if (currSlice->active_sps->pic_order_cnt_type == 1) {
539             if (!currSlice->active_sps->delta_pic_order_always_zero_flag) {
540                 READ_SE(p_bitctx, &currSlice->delta_pic_order_cnt[0]);
541 
542                 if (currSlice->p_Vid->active_pps->bottom_field_pic_order_in_frame_present_flag == 1 && !currSlice->field_pic_flag) {
543                     READ_SE(p_bitctx, &currSlice->delta_pic_order_cnt[1]);
544                 } else {
545                     currSlice->delta_pic_order_cnt[1] = 0;  //!< set to zero if not in stream
546                 }
547             } else {
548                 currSlice->delta_pic_order_cnt[0] = 0;
549                 currSlice->delta_pic_order_cnt[1] = 0;
550             }
551         }
552 
553         // need to minus emulation prevention bytes(0x000003) we met
554         emulation_prevention = p_bitctx->emulation_prevention_bytes_ - emulation_prevention;
555         currSlice->poc_used_bitlen = p_bitctx->used_bits - poc_used_bits - (emulation_prevention * 8); //!< calculate poc used bit length
556         //!< redundant_pic_cnt is missing here
557         ASSERT(currSlice->p_Vid->active_pps->redundant_pic_cnt_present_flag == 0); // add by dw, high 4:2:2 profile not support
558         if (currSlice->p_Vid->active_pps->redundant_pic_cnt_present_flag) {
559             READ_UE(p_bitctx, &currSlice->redundant_pic_cnt);
560         }
561 
562         if (currSlice->slice_type == H264_B_SLICE) {
563             READ_ONEBIT(p_bitctx, &currSlice->direct_spatial_mv_pred_flag);
564         } else {
565             currSlice->direct_spatial_mv_pred_flag = 0;
566         }
567         currSlice->num_ref_idx_active[LIST_0] = currSlice->p_Vid->active_pps->num_ref_idx_l0_default_active_minus1 + 1;
568         currSlice->num_ref_idx_active[LIST_1] = currSlice->p_Vid->active_pps->num_ref_idx_l1_default_active_minus1 + 1;
569 
570         if (currSlice->slice_type == H264_P_SLICE
571             || currSlice->slice_type == H264_SP_SLICE || currSlice->slice_type == H264_B_SLICE) {
572             READ_ONEBIT(p_bitctx, &currSlice->num_ref_idx_override_flag);
573             if (currSlice->num_ref_idx_override_flag) {
574                 READ_UE(p_bitctx, &currSlice->num_ref_idx_active[LIST_0]);
575                 currSlice->num_ref_idx_active[LIST_0] += 1;
576                 if (currSlice->slice_type == H264_B_SLICE) {
577                     READ_UE(p_bitctx, &currSlice->num_ref_idx_active[LIST_1]);
578                     currSlice->num_ref_idx_active[LIST_1] += 1;
579                 }
580             }
581         }
582         if (currSlice->slice_type != H264_B_SLICE) {
583             currSlice->num_ref_idx_active[LIST_1] = 0;
584         }
585         FUN_CHECK(ret = ref_pic_list_mvc_modification(currSlice));
586         if ((currSlice->p_Vid->active_pps->weighted_pred_flag
587              && (currSlice->slice_type == H264_P_SLICE || currSlice->slice_type == H264_SP_SLICE))
588             || (currSlice->p_Vid->active_pps->weighted_bipred_idc == 1 && (currSlice->slice_type == H264_B_SLICE))) {
589             FUN_CHECK(ret = pred_weight_table(currSlice));
590         }
591         currSlice->drpm_used_bitlen = 0;
592         if (currSlice->nal_reference_idc) {
593             FUN_CHECK(ret = dec_ref_pic_marking(currSlice));
594         }
595         H264D_DBG(H264D_DBG_PPS_SPS, "[SLICE_HEAD] type=%d, layer_id=%d,sps_id=%d, pps_id=%d, struct=%d, frame_num=%d",
596                   currSlice->slice_type, currSlice->layer_id, currSlice->active_sps->seq_parameter_set_id,
597                   currSlice->active_pps->pic_parameter_set_id, currSlice->structure, currSlice->frame_num);
598     }
599 
600     return ret = MPP_OK;
601 __BITREAD_ERR:
602     ret = p_bitctx->ret;
603 __FAILED:
604     return ret;
605 }
606