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 "H265E_SYNTAX"
19
20 #include <string.h>
21
22 #include "h265e_codec.h"
23 #include "h265e_syntax_new.h"
24
fill_picture_parameters(const H265eCtx * h,H265ePicParams * pp)25 static void fill_picture_parameters(const H265eCtx *h,
26 H265ePicParams *pp)
27 {
28 const H265ePps *pps = (H265ePps *)&h->pps;
29 const H265eSps *sps = (H265eSps *)&h->sps;
30 MppEncCfgSet *cfg = h->cfg;
31 memset(pp, 0, sizeof(H265ePicParams));
32
33 pp->pic_width = h->cfg->prep.width;
34 pp->pic_height = h->cfg->prep.height;
35 pp->hor_stride = h->cfg->prep.hor_stride;
36 pp->ver_stride = h->cfg->prep.ver_stride;
37 pp->pps_id = h->slice->m_ppsId;
38 pp->sps_id = pps->m_SPSId;
39 pp->vps_id = sps->m_VPSId;
40 pp->mpp_format = cfg->prep.format;
41
42 pp->wFormatAndSequenceInfoFlags = (sps->m_chromaFormatIdc << 0) |
43 (sps->m_colorPlaneFlag << 2) |
44 ((sps->m_bitDepthY - 8) << 3) |
45 ((sps->m_bitDepthC - 8) << 6) |
46 ((sps->m_bitsForPOC - 4) << 9) |
47 (0 << 13) |
48 (0 << 14) |
49 (0 << 15);
50
51
52 pp->sps_max_dec_pic_buffering_minus1 = sps->m_maxDecPicBuffering[sps->m_maxTLayers - 1] - 1;
53 pp->log2_min_luma_coding_block_size_minus3 = sps->m_log2MinCodingBlockSize - 3;
54 pp->log2_diff_max_min_luma_coding_block_size = sps->m_log2DiffMaxMinCodingBlockSize;
55
56 pp->log2_min_transform_block_size_minus2 = sps->m_quadtreeTULog2MinSize - 2;
57 pp->log2_diff_max_min_transform_block_size = sps->m_quadtreeTULog2MaxSize - sps->m_quadtreeTULog2MinSize;
58
59 pp->max_transform_hierarchy_depth_inter = sps->m_quadtreeTUMaxDepthInter;
60 pp->max_transform_hierarchy_depth_intra = sps->m_quadtreeTUMaxDepthIntra;
61
62 pp->num_short_term_ref_pic_sets = sps->m_RPSList.m_numberOfReferencePictureSets;
63 pp->num_long_term_ref_pics_sps = sps->m_numLongTermRefPicSPS;
64
65 pp->sample_adaptive_offset_enabled_flag = sps->m_bUseSAO;
66
67 pp->num_ref_idx_l0_default_active_minus1 = pps->m_numRefIdxL0DefaultActive - 1;
68 pp->num_ref_idx_l1_default_active_minus1 = pps->m_numRefIdxL1DefaultActive - 1;
69 pp->init_qp_minus26 = pps->m_picInitQPMinus26;
70
71
72 pp->CodingParamToolFlags = (sps->m_scalingListEnabledFlag << 0) |
73 (sps->m_useAMP << 1) |
74 (sps->m_bUseSAO << 2) |
75 (sps->m_usePCM << 3) |
76 ((sps->m_usePCM ? (sps->m_pcmBitDepthLuma - 1) : 0) << 4) |
77 ((sps->m_usePCM ? (sps->m_pcmBitDepthChroma - 1) : 0) << 8) |
78 ((sps->m_usePCM ? (sps->m_pcmLog2MinSize - 3) : 0) << 12) |
79 ((sps->m_usePCM ? (sps->m_pcmLog2MaxSize - sps->m_pcmLog2MinSize) : 0) << 14) |
80 (sps->m_bPCMFilterDisableFlag << 16) |
81 (sps->m_bLongTermRefsPresent << 17) |
82 (sps->m_TMVPFlagsPresent << 18) |
83 (sps->m_useStrongIntraSmoothing << 19) |
84 (0 << 20) |
85 (pps->m_outputFlagPresentFlag << 21) |
86 (pps->m_numExtraSliceHeaderBits << 22) |
87 (pps->m_signHideFlag << 25) |
88 (pps->m_cabacInitPresentFlag << 26) |
89 (0 << 27);
90
91 pp->CodingSettingPicturePropertyFlags = (pps->m_bConstrainedIntraPred << 0) |
92 (pps->m_useTransformSkip << 1) |
93 (pps->m_useDQP << 2) |
94 (pps->m_bSliceChromaQpFlag << 3) |
95 (pps->m_bUseWeightPred << 4) |
96 (pps->m_useWeightedBiPred << 5) |
97 (pps->m_transquantBypassEnableFlag << 6) |
98 (pps->m_tiles_enabled_flag << 7) |
99 (pps->m_entropyCodingSyncEnabledFlag << 8) |
100 (pps->m_bTileUniformSpacing << 9) |
101 (pps->m_loopFilterAcrossTilesEnabledFlag << 10) |
102 (pps->m_LFCrossSliceBoundaryFlag << 11) |
103 (pps->m_deblockingFilterOverrideEnabledFlag << 12) |
104 (pps->m_picDisableDeblockingFilterFlag << 13) |
105 (pps->m_listsModificationPresentFlag << 14) |
106 (pps->m_sliceHeaderExtensionPresentFlag << 15);
107
108 pp->pps_cb_qp_offset = pps->m_chromaCbQpOffset;
109 pp->pps_cr_qp_offset = pps->m_chromaCrQpOffset;
110 pp->diff_cu_qp_delta_depth = pps->m_maxCuDQPDepth;
111 pp->pps_beta_offset_div2 = pps->m_deblockingFilterBetaOffsetDiv2;
112 pp->pps_tc_offset_div2 = pps->m_deblockingFilterTcOffsetDiv2;
113 pp->log2_parallel_merge_level_minus2 = pps->m_log2ParallelMergeLevelMinus2 - 2;
114 if (pps->m_tiles_enabled_flag) {
115 RK_U8 i = 0;
116
117 mpp_assert(pps->m_nNumTileColumnsMinus1 <= 19);
118 mpp_assert(pps->m_nNumTileRowsMinus1 <= 21);
119
120 pp->num_tile_columns_minus1 = pps->m_nNumTileColumnsMinus1;
121 pp->num_tile_rows_minus1 = pps->m_nNumTileRowsMinus1;
122
123 for (i = 0; i < pp->num_tile_columns_minus1; i++)
124 pp->column_width_minus1[i] = pps->m_nTileColumnWidthArray[i];
125
126 for (i = 0; i < pp->num_tile_rows_minus1; i++)
127 pp->row_height_minus1[i] = pps->m_nTileRowHeightArray[i];
128 }
129 }
130
fill_slice_parameters(const H265eCtx * h,H265eSlicParams * sp)131 static void fill_slice_parameters( const H265eCtx *h,
132 H265eSlicParams *sp)
133 {
134 MppEncH265Cfg *codec = &h->cfg->codec.h265;
135 H265eSlice *slice = h->slice;
136 memset(sp, 0, sizeof(H265eSlicParams));
137 if (codec->slice_cfg.split_enable) {
138 sp->sli_splt_cpst = 1;
139 sp->sli_splt = 1;
140 sp->sli_splt_mode = codec->slice_cfg.split_mode;
141 if (codec->slice_cfg.split_mode) {
142 sp->sli_splt_cnum_m1 = codec->slice_cfg.slice_size - 1;
143 } else {
144 sp->sli_splt_byte = codec->slice_cfg.slice_size;
145 }
146 sp->sli_max_num_m1 = 50;
147 sp->sli_flsh = 1;
148 }
149
150
151
152 sp->cbc_init_flg = slice->m_cabacInitFlag;
153 sp->mvd_l1_zero_flg = slice->m_bLMvdL1Zero;
154 sp->merge_up_flag = codec->merge_cfg.merge_up_flag;
155 sp->merge_left_flag = codec->merge_cfg.merge_left_flag;
156 sp->ref_pic_lst_mdf_l0 = slice->ref_pic_list_modification_flag_l0;
157
158 sp->num_refidx_l1_act = 0;
159 sp->num_refidx_l0_act = 1;
160
161 sp->num_refidx_act_ovrd = (((RK_U32)slice->m_numRefIdx[0] != slice->m_pps->m_numRefIdxL0DefaultActive)
162 || (slice->m_sliceType == B_SLICE &&
163 (RK_U32)slice->m_numRefIdx[1] != slice->m_pps->m_numRefIdxL1DefaultActive));
164
165 sp->sli_sao_chrm_flg = slice->m_sps->m_bUseSAO && slice->m_saoEnabledFlagChroma;
166 sp->sli_sao_luma_flg = slice->m_sps->m_bUseSAO && slice->m_saoEnabledFlag;
167 sp->sli_tmprl_mvp_en = slice->m_enableTMVPFlag;
168
169 sp->pic_out_flg = slice->m_picOutputFlag;
170 sp->slice_type = slice->m_sliceType;
171 sp->slice_rsrv_flg = 0;
172 sp->dpdnt_sli_seg_flg = 0;
173 sp->sli_pps_id = slice->m_pps->m_PPSId;
174 sp->no_out_pri_pic = slice->no_output_of_prior_pics_flag;
175
176 sp->sli_tc_ofst_div2 = slice->m_deblockingFilterTcOffsetDiv2;
177 sp->sli_beta_ofst_div2 = slice->m_deblockingFilterBetaOffsetDiv2;
178 sp->sli_lp_fltr_acrs_sli = slice->m_LFCrossSliceBoundaryFlag;
179 sp->sli_dblk_fltr_dis = slice->m_deblockingFilterDisable;
180 sp->dblk_fltr_ovrd_flg = slice->m_deblockingFilterOverrideFlag;
181 sp->sli_cb_qp_ofst = slice->m_sliceQpDeltaCb;
182 sp->sli_qp = slice->m_sliceQp;
183 sp->max_mrg_cnd = slice->m_maxNumMergeCand;
184 sp->non_reference_flag = slice->m_temporalLayerNonReferenceFlag;
185 sp->col_ref_idx = 0;
186 sp->col_frm_l0_flg = slice->m_colFromL0Flag;
187 sp->sli_poc_lsb = (slice->poc - slice->last_idr + (1 << slice->m_sps->m_bitsForPOC)) %
188 (1 << slice->m_sps->m_bitsForPOC);
189 sp->sli_hdr_ext_len = slice->slice_header_extension_length;
190 }
191
fill_ref_parameters(const H265eCtx * h,H265eSlicParams * sp)192 RK_S32 fill_ref_parameters(const H265eCtx *h, H265eSlicParams *sp)
193 {
194 H265eSlice *slice = h->slice;
195 H265eReferencePictureSet* rps = slice->m_rps;
196 RK_U32 numRpsCurrTempList = 0;
197 RK_S32 ref_num = 0;
198 H265eDpbFrm *ref_frame;
199 RK_S32 i, j, k;
200 sp->dlt_poc_msb_prsnt0 = 0;
201 sp->dlt_poc_msb_cycl0 = 0;
202 sp->tot_poc_num = 0;
203 if (slice->m_bdIdx < 0) {
204 RK_S32 prev = 0;
205 sp->st_ref_pic_flg = 0;
206 sp->num_neg_pic = rps->num_negative_pic;
207 sp->num_pos_pic = rps->num_positive_pic;
208 for (j = 0; j < rps->num_negative_pic; j++) {
209 if (0 == j) {
210 sp->dlt_poc_s0_m10 = prev - rps->delta_poc[j] - 1;
211 sp->used_by_s0_flg = rps->m_ref[j];
212 } else if (1 == j) {
213 sp->dlt_poc_s0_m11 = prev - rps->delta_poc[j] - 1;
214 sp->used_by_s0_flg |= rps->m_ref[j] << 1;
215 } else if (2 == j) {
216 sp->dlt_poc_s0_m12 = prev - rps->delta_poc[j] - 1;
217 sp->used_by_s0_flg |= rps->m_ref[j] << 2;
218 } else if (3 == j) {
219 sp->dlt_poc_s0_m13 = prev - rps->delta_poc[j] - 1;
220 sp->used_by_s0_flg |= rps->m_ref[j] << 3;
221 }
222 prev = rps->delta_poc[j];
223 }
224 }
225
226 for (i = 0; i < rps->m_numberOfPictures; i++) {
227 if (rps->m_ref[i]) {
228 sp->tot_poc_num++;
229 }
230 }
231
232 if (slice->m_sps->m_bLongTermRefsPresent) {
233 RK_S32 numLtrpInSH = rps->num_long_term_pic;
234 RK_S32 numLtrpInSPS = 0;
235 RK_S32 counter = 0;
236 RK_U32 poc_lsb_lt[3] = { 0, 0, 0 };
237 RK_U32 used_by_lt_flg[3] = { 0, 0, 0 };
238 RK_U32 dlt_poc_msb_prsnt[3] = { 0, 0, 0 };
239 RK_U32 dlt_poc_msb_cycl[3] = { 0, 0, 0 };
240 RK_S32 prevDeltaMSB = 0;
241 RK_S32 offset = rps->num_negative_pic + rps->num_positive_pic;
242 RK_S32 numLongTerm = rps->m_numberOfPictures - offset;
243 RK_S32 bitsForLtrpInSPS = 0;
244 for (k = rps->m_numberOfPictures - 1; k > rps->m_numberOfPictures - rps->num_long_term_pic - 1; k--) {
245 RK_U32 lsb = rps->poc[k] % (1 << slice->m_sps->m_bitsForPOC);
246 RK_U32 find_flag = 0;
247 for (i = 0; i < (RK_S32)slice->m_sps->m_numLongTermRefPicSPS; i++) {
248 if ((lsb == slice->m_sps->m_ltRefPicPocLsbSps[i]) && (rps->m_used[k] == slice->m_sps->m_usedByCurrPicLtSPSFlag[i])) {
249 find_flag = 1;
250 break;
251 }
252 }
253
254 if (find_flag) {
255 numLtrpInSPS++;
256 } else {
257 counter++;
258 }
259 }
260
261 numLtrpInSH -= numLtrpInSPS;
262
263 while (slice->m_sps->m_numLongTermRefPicSPS > (RK_U32)(1 << bitsForLtrpInSPS)) {
264 bitsForLtrpInSPS++;
265 }
266
267 if (slice->m_sps->m_numLongTermRefPicSPS > 0) {
268 sp->num_lt_sps = numLtrpInSPS;
269 }
270 sp->num_lt_pic = numLtrpInSH;
271 // Note that the LSBs of the LT ref. pic. POCs must be sorted before.
272 // Not sorted here because LT ref indices will be used in setRefPicList()
273 sp->poc_lsb_lt0 = 0;
274 sp->used_by_lt_flg0 = 0;
275 sp->dlt_poc_msb_prsnt0 = 0;
276 sp->dlt_poc_msb_cycl0 = 0;
277 sp->poc_lsb_lt1 = 0;
278 sp->used_by_lt_flg1 = 0;
279 sp->dlt_poc_msb_prsnt1 = 0;
280 sp->dlt_poc_msb_cycl1 = 0;
281 sp->poc_lsb_lt2 = 0;
282 sp->used_by_lt_flg2 = 0;
283 sp->dlt_poc_msb_prsnt2 = 0;
284 sp->dlt_poc_msb_cycl2 = 0;
285
286 for ( i = rps->m_numberOfPictures - 1; i > offset - 1; i--) {
287 RK_U32 deltaFlag = 0;
288 if ((i == rps->m_numberOfPictures - 1) || (i == rps->m_numberOfPictures - 1 - numLtrpInSPS)) {
289 deltaFlag = 1;
290 }
291 poc_lsb_lt[numLongTerm - 1 - (i - offset)] = rps->m_pocLSBLT[i];
292 used_by_lt_flg[numLongTerm - 1 - (i - offset)] = rps->m_ref[i];
293 dlt_poc_msb_prsnt[numLongTerm - 1 - (i - offset)] = rps->m_deltaPocMSBPresentFlag[i];
294
295 if (rps->m_deltaPocMSBPresentFlag[i]) {
296 if (deltaFlag) {
297 dlt_poc_msb_cycl[numLongTerm - 1 - (i - offset)] = rps->m_deltaPOCMSBCycleLT[i];
298 } else {
299 RK_S32 differenceInDeltaMSB = rps->m_deltaPOCMSBCycleLT[i] - prevDeltaMSB;
300 mpp_assert(differenceInDeltaMSB >= 0);
301 dlt_poc_msb_cycl[numLongTerm - 1 - (i - offset)] = differenceInDeltaMSB;
302 }
303 }
304 prevDeltaMSB = rps->m_deltaPOCMSBCycleLT[i];
305 }
306
307 sp->poc_lsb_lt0 = poc_lsb_lt[0];
308 sp->used_by_lt_flg0 = used_by_lt_flg[0];
309 sp->dlt_poc_msb_prsnt0 = dlt_poc_msb_prsnt[0];
310 sp->dlt_poc_msb_cycl0 = dlt_poc_msb_cycl[0];
311 sp->poc_lsb_lt1 = poc_lsb_lt[1];
312 sp->used_by_lt_flg1 = used_by_lt_flg[1];
313 sp->dlt_poc_msb_prsnt1 = dlt_poc_msb_prsnt[1];
314 sp->dlt_poc_msb_cycl1 = dlt_poc_msb_cycl[1];
315 sp->poc_lsb_lt2 = poc_lsb_lt[2];
316 sp->used_by_lt_flg2 = used_by_lt_flg[2];
317 sp->dlt_poc_msb_prsnt2 = dlt_poc_msb_prsnt[2];
318 sp->dlt_poc_msb_cycl2 = dlt_poc_msb_cycl[2];
319 }
320
321 sp->lst_entry_l0 = 0;
322 sp->ref_pic_lst_mdf_l0 = 0;
323
324 if (slice->m_sliceType == I_SLICE) {
325 numRpsCurrTempList = 0;
326 } else {
327 ref_num = rps->num_negative_pic + rps->num_positive_pic + rps->num_long_term_pic;
328 for (i = 0; i < ref_num; i++) {
329 if (rps->m_used[i]) {
330 numRpsCurrTempList++;
331 }
332 }
333 }
334
335 if (slice->m_pps->m_listsModificationPresentFlag && numRpsCurrTempList > 1) {
336 H265eRefPicListModification* refPicListModification = &slice->m_RefPicListModification;
337 if (slice->m_sliceType != I_SLICE) {
338 sp->ref_pic_lst_mdf_l0 = refPicListModification->m_refPicListModificationFlagL0 ? 1 : 0;
339 if (sp->ref_pic_lst_mdf_l0) {
340 sp->lst_entry_l0 = refPicListModification->m_RefPicSetIdxL0[0];
341 }
342 }
343 }
344
345 sp->recon_pic.slot_idx = h->dpb->curr->slot_idx;
346 ref_frame = slice->m_refPicList[0][0];
347 if (ref_frame != NULL) {
348 sp->ref_pic.slot_idx = ref_frame->slot_idx;
349 } else {
350 sp->ref_pic.slot_idx = h->dpb->curr->slot_idx;
351 }
352 return 0;
353 }
354
h265e_syntax_fill(void * ctx)355 RK_S32 h265e_syntax_fill(void *ctx)
356 {
357 H265eCtx *h = (H265eCtx *)ctx;
358 H265eSyntax_new *syn = (H265eSyntax_new*)&h->syntax;
359 fill_picture_parameters(h, &syn->pp);
360 fill_slice_parameters(h, &syn->sp);
361 fill_ref_parameters(h, &syn->sp);
362 return 0;
363 }
364