xref: /rockchip-linux_mpp/mpp/codec/dec/h264/h264d_pps.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_pps"
19 
20 #include <string.h>
21 
22 #include "mpp_err.h"
23 
24 #include "h264d_pps.h"
25 #include "h264d_scalist.h"
26 #include "h264d_dpb.h"
27 
reset_curpps_data(H264_PPS_t * cur_pps)28 static void reset_curpps_data(H264_PPS_t *cur_pps)
29 {
30     memset(cur_pps, 0, sizeof(H264_PPS_t));
31     cur_pps->seq_parameter_set_id = 0;  // reset
32     cur_pps->pic_parameter_set_id = 0;
33 }
34 
parse_pps_calingLists(BitReadCtx_t * p_bitctx,H264_SPS_t * sps,H264_PPS_t * pps)35 static MPP_RET parse_pps_calingLists(BitReadCtx_t *p_bitctx, H264_SPS_t *sps, H264_PPS_t *pps)
36 {
37     RK_S32 i = 0;
38     MPP_RET ret = MPP_ERR_UNKNOW;
39 
40     for (i = 0; i < 6; ++i) {
41         READ_ONEBIT(p_bitctx, &pps->pic_scaling_list_present_flag[i]);
42 
43         if (pps->pic_scaling_list_present_flag[i]) {
44             FUN_CHECK (ret = parse_scalingList(p_bitctx, H264ScalingList4x4Length,
45                                                pps->ScalingList4x4[i], &pps->UseDefaultScalingMatrix4x4Flag[i]));
46         }
47     }
48     if (pps->transform_8x8_mode_flag) {
49         for (i = 0; i < ((sps->chroma_format_idc != 3) ? 2 : 6); ++i) {
50             READ_ONEBIT(p_bitctx, &pps->pic_scaling_list_present_flag[i + 6]);
51             if (pps->pic_scaling_list_present_flag[i + 6]) {
52                 FUN_CHECK(ret = parse_scalingList(p_bitctx, H264ScalingList8x8Length,
53                                                   pps->ScalingList8x8[i], &pps->UseDefaultScalingMatrix8x8Flag[i]));
54             }
55         }
56     }
57 
58     return ret = MPP_OK;
59 __BITREAD_ERR:
60     ret = p_bitctx->ret;
61 __FAILED:
62     return ret;
63 }
64 
parser_pps(BitReadCtx_t * p_bitctx,H264_SPS_t * cur_sps,H264_PPS_t * cur_pps)65 static MPP_RET parser_pps(BitReadCtx_t *p_bitctx, H264_SPS_t *cur_sps, H264_PPS_t *cur_pps)
66 {
67     MPP_RET ret = MPP_ERR_UNKNOW;
68 
69     READ_UE(p_bitctx, &cur_pps->pic_parameter_set_id);
70     READ_UE(p_bitctx, &cur_pps->seq_parameter_set_id);
71     //VAL_CHECK(ret, cur_pps->seq_parameter_set_id < 32);
72     if (cur_pps->seq_parameter_set_id >= MAXSPS) {
73         cur_pps->seq_parameter_set_id = 0;
74     }
75     if (cur_pps->pic_parameter_set_id >= MAXPPS)    {
76         cur_pps->pic_parameter_set_id = 0;
77     }
78     READ_ONEBIT(p_bitctx, &cur_pps->entropy_coding_mode_flag);
79     READ_ONEBIT(p_bitctx, &cur_pps->bottom_field_pic_order_in_frame_present_flag);
80 
81     READ_UE(p_bitctx, &cur_pps->num_slice_groups_minus1);
82     VAL_CHECK(ret, cur_pps->num_slice_groups_minus1 <= 1);
83     READ_UE(p_bitctx, &cur_pps->num_ref_idx_l0_default_active_minus1);
84     VAL_CHECK(ret, cur_pps->num_ref_idx_l0_default_active_minus1 < 32);
85     READ_UE(p_bitctx, &cur_pps->num_ref_idx_l1_default_active_minus1);
86     VAL_CHECK(ret, cur_pps->num_ref_idx_l1_default_active_minus1 < 32);
87     READ_ONEBIT(p_bitctx, &cur_pps->weighted_pred_flag);
88     READ_BITS(p_bitctx, 2, &cur_pps->weighted_bipred_idc);
89     VAL_CHECK(ret, cur_pps->weighted_bipred_idc < 3);
90     READ_SE(p_bitctx, &cur_pps->pic_init_qp_minus26);
91     READ_SE(p_bitctx, &cur_pps->pic_init_qs_minus26);
92     READ_SE(p_bitctx, &cur_pps->chroma_qp_index_offset);
93     cur_pps->second_chroma_qp_index_offset = cur_pps->chroma_qp_index_offset;
94     READ_ONEBIT(p_bitctx, &cur_pps->deblocking_filter_control_present_flag);
95     READ_ONEBIT(p_bitctx, &cur_pps->constrained_intra_pred_flag);
96     READ_ONEBIT(p_bitctx, &cur_pps->redundant_pic_cnt_present_flag);
97     VAL_CHECK(ret , cur_pps->redundant_pic_cnt_present_flag == 0);
98 
99     if (mpp_has_more_rbsp_data(p_bitctx)) {
100         READ_ONEBIT(p_bitctx, &cur_pps->transform_8x8_mode_flag);
101         READ_ONEBIT(p_bitctx, &cur_pps->pic_scaling_matrix_present_flag);
102         if (cur_pps->pic_scaling_matrix_present_flag) {
103             H264D_WARNNING("Picture scaling matrix present.");
104             FUN_CHECK(ret = parse_pps_calingLists(p_bitctx, cur_sps, cur_pps));
105         }
106         READ_SE(p_bitctx, &cur_pps->second_chroma_qp_index_offset);
107     } else {
108         cur_pps->transform_8x8_mode_flag = 0;
109         cur_pps->second_chroma_qp_index_offset = cur_pps->chroma_qp_index_offset;
110     }
111     cur_pps->Valid = 1;
112 
113     return ret = MPP_OK;
114 
115 __BITREAD_ERR:
116     ret = p_bitctx->ret;
117 __FAILED:
118 
119     return ret;
120 }
121 
122 
123 /*!
124 ***********************************************************************
125 * \brief
126 *    parser pps and process pps
127 ***********************************************************************
128 */
129 //extern "C"
process_pps(H264_SLICE_t * currSlice)130 MPP_RET process_pps(H264_SLICE_t *currSlice)
131 {
132     MPP_RET ret = MPP_ERR_UNKNOW;
133 
134     H264dCurCtx_t *p_Cur = currSlice->p_Cur;
135     BitReadCtx_t *p_bitctx = &p_Cur->bitctx;
136     H264_PPS_t *cur_pps = &p_Cur->pps;
137 
138     reset_curpps_data(cur_pps);// reset
139 
140     FUN_CHECK(ret = parser_pps(p_bitctx, &p_Cur->sps, cur_pps));
141     //!< MakePPSavailable
142     ASSERT(cur_pps->Valid == 1);
143     if (!currSlice->p_Vid->ppsSet[cur_pps->pic_parameter_set_id]) {
144         currSlice->p_Vid->ppsSet[cur_pps->pic_parameter_set_id] = mpp_calloc(H264_PPS_t, 1);
145     }
146 
147     memcpy(currSlice->p_Vid->ppsSet[cur_pps->pic_parameter_set_id], cur_pps, sizeof(H264_PPS_t));
148     p_Cur->p_Vid->spspps_update = 1;
149 
150     return ret = MPP_OK;
151 __FAILED:
152     return ret;
153 }
154 
155 /*!
156 ***********************************************************************
157 * \brief
158 *    prase sps and process sps
159 ***********************************************************************
160 */
161 //extern "C"
activate_pps(H264dVideoCtx_t * p_Vid,H264_PPS_t * pps)162 MPP_RET activate_pps(H264dVideoCtx_t *p_Vid, H264_PPS_t *pps)
163 {
164     MPP_RET ret = MPP_ERR_UNKNOW;
165 
166     INP_CHECK(ret, !p_Vid && !pps);
167     if (p_Vid->active_pps != pps) {
168         if (p_Vid->dec_pic) {
169             //!< return if the last picture has already been finished
170             FUN_CHECK(ret = exit_picture(p_Vid, &p_Vid->dec_pic));
171         }
172         p_Vid->active_pps = pps;
173     }
174 __RETURN:
175     return ret = MPP_OK;
176 __FAILED:
177     return ret;
178 }
179 
180 /*!
181 ***********************************************************************
182 * \brief
183 *    parser nal prefix
184 ***********************************************************************
185 */
186 //extern "C"
process_prefix(H264_SLICE_t * currSlice)187 MPP_RET process_prefix(H264_SLICE_t *currSlice)
188 {
189     MPP_RET ret = MPP_ERR_UNKNOW;
190 
191     H264dCurCtx_t *p_Cur = currSlice->p_Cur;
192     BitReadCtx_t *p_bitctx = &p_Cur->bitctx;
193     H264_PREFIX_t *cur_prefix = &p_Cur->prefix;
194 
195     if (currSlice->nal_reference_idc) {
196         READ_ONEBIT(p_bitctx, &cur_prefix->store_ref_base_pic_flag);
197 
198         if ((currSlice->svcExt.use_ref_base_pic_flag ||
199              cur_prefix->store_ref_base_pic_flag) &&
200             !currSlice->svcExt.idr_flag) {
201             H264D_LOG("read dec_ref_base_pic_marking\n");
202         }
203     }
204 
205     return ret = MPP_OK;
206 __BITREAD_ERR:
207     return p_bitctx->ret;
208 }
209