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 /*
19 * @file h265d_parser2_syntax.c
20 * @brief
21 * @author csy(csy@rock-chips.com)
22
23 * @version 1.0.0
24 * @history
25 * 2015.7.15 : Create
26 */
27
28 #define MODULE_TAG "H265SYNATX"
29
30 #include "h265d_parser.h"
31 #include "h265d_syntax.h"
32
33
fill_picture_entry(DXVA_PicEntry_HEVC * pic,unsigned index,unsigned flag)34 static void fill_picture_entry(DXVA_PicEntry_HEVC *pic,
35 unsigned index, unsigned flag)
36 {
37 mpp_assert((index & 0x7f) == index && (flag & 0x01) == flag);
38 pic->bPicEntry = index | (flag << 7);
39 }
40
get_refpic_index(const DXVA_PicParams_HEVC * pp,int surface_index)41 static RK_S32 get_refpic_index(const DXVA_PicParams_HEVC *pp, int surface_index)
42 {
43 RK_U32 i;
44 for (i = 0; i < MPP_ARRAY_ELEMS(pp->RefPicList); i++) {
45 if ((pp->RefPicList[i].bPicEntry & 0x7f) == surface_index) {
46 //mpp_err("retun %d slot_index = %d",i,surface_index);
47 return i;
48 }
49 }
50 return 0xff;
51 }
52
fill_picture_parameters(const HEVCContext * h,DXVA_PicParams_HEVC * pp)53 static void fill_picture_parameters(const HEVCContext *h,
54 DXVA_PicParams_HEVC *pp)
55 {
56 const HEVCFrame *current_picture = h->ref;
57 const HEVCPPS *pps = (HEVCPPS *)h->pps_list[h->sh.pps_id];
58 const HEVCSPS *sps = (HEVCSPS *)h->sps_list[pps->sps_id];
59 const ShortTermRPS *src_rps = sps->st_rps;
60 Short_SPS_RPS_HEVC *dst_rps = pp->sps_st_rps;
61 const ShortTermRPS *cur_src_rps = h->sh.short_term_rps;
62 Short_SPS_RPS_HEVC *cur_dst_rps = &pp->cur_st_rps;
63
64 RK_U32 i, j;
65 RK_U32 rps_used[16];
66 RK_U32 nb_rps_used;
67
68 memset(pp, 0, sizeof(*pp));
69
70 pp->PicWidthInMinCbsY = sps->min_cb_width;
71 pp->PicHeightInMinCbsY = sps->min_cb_height;
72 pp->pps_id = h->sh.pps_id;
73 pp->sps_id = pps->sps_id;
74 pp->vps_id = sps->vps_id;
75
76 pp->wFormatAndSequenceInfoFlags = (sps->chroma_format_idc << 0) |
77 (sps->separate_colour_plane_flag << 2) |
78 ((sps->bit_depth - 8) << 3) |
79 ((sps->bit_depth - 8) << 6) |
80 ((sps->log2_max_poc_lsb - 4) << 9) |
81 (0 << 13) |
82 (0 << 14) |
83 (0 << 15);
84
85 fill_picture_entry(&pp->CurrPic, current_picture->slot_index, 0);
86
87 pp->sps_max_dec_pic_buffering_minus1 = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1;
88 pp->log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3;
89 pp->log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size;
90 pp->log2_min_transform_block_size_minus2 = sps->log2_min_tb_size - 2;
91 pp->log2_diff_max_min_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size;
92 pp->max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter;
93 pp->max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra;
94 pp->num_short_term_ref_pic_sets = sps->nb_st_rps;
95 pp->num_long_term_ref_pics_sps = sps->num_long_term_ref_pics_sps;
96
97 pp->num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1;
98 pp->num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1;
99 pp->init_qp_minus26 = pps->pic_init_qp_minus26;
100
101 if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) {
102 pp->ucNumDeltaPocsOfRefRpsIdx = h->sh.short_term_rps->rps_idx_num_delta_pocs;
103 pp->wNumBitsForShortTermRPSInSlice = h->sh.short_term_ref_pic_set_size;
104 }
105
106 pp->dwCodingParamToolFlags = (sps->scaling_list_enable_flag << 0) |
107 (sps->amp_enabled_flag << 1) |
108 (sps->sao_enabled << 2) |
109 (sps->pcm_enabled_flag << 3) |
110 ((sps->pcm_enabled_flag ? (sps->pcm.bit_depth - 1) : 0) << 4) |
111 ((sps->pcm_enabled_flag ? (sps->pcm.bit_depth_chroma - 1) : 0) << 8) |
112 ((sps->pcm_enabled_flag ? (sps->pcm.log2_min_pcm_cb_size - 3) : 0) << 12) |
113 ((sps->pcm_enabled_flag ? (sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size) : 0) << 14) |
114 (sps->pcm.loop_filter_disable_flag << 16) |
115 (sps->long_term_ref_pics_present_flag << 17) |
116 (sps->sps_temporal_mvp_enabled_flag << 18) |
117 (sps->sps_strong_intra_smoothing_enable_flag << 19) |
118 (pps->dependent_slice_segments_enabled_flag << 20) |
119 (pps->output_flag_present_flag << 21) |
120 (pps->num_extra_slice_header_bits << 22) |
121 (pps->sign_data_hiding_flag << 25) |
122 (pps->cabac_init_present_flag << 26) |
123 (0 << 27);
124
125 pp->dwCodingSettingPicturePropertyFlags = (pps->constrained_intra_pred_flag << 0) |
126 (pps->transform_skip_enabled_flag << 1) |
127 (pps->cu_qp_delta_enabled_flag << 2) |
128 (pps->pic_slice_level_chroma_qp_offsets_present_flag << 3) |
129 (pps->weighted_pred_flag << 4) |
130 (pps->weighted_bipred_flag << 5) |
131 (pps->transquant_bypass_enable_flag << 6) |
132 (pps->tiles_enabled_flag << 7) |
133 (pps->entropy_coding_sync_enabled_flag << 8) |
134 (pps->uniform_spacing_flag << 9) |
135 (pps->loop_filter_across_tiles_enabled_flag << 10) |
136 (pps->seq_loop_filter_across_slices_enabled_flag << 11) |
137 (pps->deblocking_filter_override_enabled_flag << 12) |
138 (pps->disable_dbf << 13) |
139 (pps->lists_modification_present_flag << 14) |
140 (pps->slice_header_extension_present_flag << 15) |
141 (0 << 19);
142
143 pp->IdrPicFlag = (h->first_nal_type == 19 || h->first_nal_type == 20);
144 pp->IrapPicFlag = (h->first_nal_type >= 16 && h->first_nal_type <= 23);
145 pp->IntraPicFlag = (h->first_nal_type >= 16 && h->first_nal_type <= 23) || h->sh.slice_type == I_SLICE;
146 pp->pps_cb_qp_offset = pps->cb_qp_offset;
147 pp->pps_cr_qp_offset = pps->cr_qp_offset;
148 if (pps->tiles_enabled_flag) {
149 pp->num_tile_columns_minus1 = pps->num_tile_columns - 1;
150 pp->num_tile_rows_minus1 = pps->num_tile_rows - 1;
151
152 if (!pps->uniform_spacing_flag) {
153 for (i = 0; i < (RK_U32)pps->num_tile_columns; i++)
154 pp->column_width_minus1[i] = pps->bufs.column_width[i] - 1;
155
156 for (i = 0; i < (RK_U32)pps->num_tile_rows; i++)
157 pp->row_height_minus1[i] = pps->bufs.row_height[i] - 1;
158 }
159 }
160
161 pp->diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth;
162 pp->pps_beta_offset_div2 = pps->beta_offset / 2;
163 pp->pps_tc_offset_div2 = pps->tc_offset / 2;
164 pp->log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2;
165 pp->slice_segment_header_extension_present_flag = pps->slice_header_extension_present_flag;
166 pp->CurrPicOrderCntVal = h->poc;
167 pp->ps_update_flag = h->ps_need_upate;
168 pp->rps_update_flag = h->rps_need_upate || h->ps_need_upate;
169
170 if (pp->rps_update_flag) {
171 for (i = 0; i < 32; i++) {
172 pp->sps_lt_rps[i].lt_ref_pic_poc_lsb = sps->lt_ref_pic_poc_lsb_sps[i];
173 pp->sps_lt_rps[i].used_by_curr_pic_lt_flag = sps->used_by_curr_pic_lt_sps_flag[i];
174 }
175
176 if (cur_src_rps) {
177 RK_U32 n_pics = h->sh.short_term_rps->num_negative_pics;
178 cur_dst_rps->num_negative_pics = n_pics;
179 cur_dst_rps->num_positive_pics = cur_src_rps->num_delta_pocs - n_pics;
180 for (i = 0; i < cur_dst_rps->num_negative_pics; i++) {
181 cur_dst_rps->delta_poc_s0[i] = cur_src_rps->delta_poc[i];
182 cur_dst_rps->s0_used_flag[i] = cur_src_rps->used[i];
183 }
184 for (i = 0; i < cur_dst_rps->num_positive_pics; i++) {
185 cur_dst_rps->delta_poc_s1[i] = cur_src_rps->delta_poc[i + n_pics];
186 cur_dst_rps->s1_used_flag[i] = cur_src_rps->used[i + n_pics];
187 }
188 }
189
190 for (i = 0; i < 64; i++) {
191 if (i < sps->nb_st_rps) {
192
193 RK_U32 n_pics = src_rps[i].num_negative_pics;
194 dst_rps[i].num_negative_pics = n_pics;
195 dst_rps[i].num_positive_pics = src_rps[i].num_delta_pocs - n_pics;
196 for (j = 0; j < dst_rps[i].num_negative_pics; j++) {
197 dst_rps[i].delta_poc_s0[j] = src_rps[i].delta_poc[j];
198 dst_rps[i].s0_used_flag[j] = src_rps[i].used[j];
199 }
200
201 for ( j = 0; j < dst_rps[i].num_positive_pics; j++) {
202 dst_rps[i].delta_poc_s1[j] = src_rps[i].delta_poc[j + n_pics];
203 dst_rps[i].s1_used_flag[j] = src_rps[i].used[j + n_pics];
204 }
205 }
206 }
207 }
208
209 nb_rps_used = 0;
210 for (i = 0; i < NB_RPS_TYPE; i++) {
211 for (j = 0; j < (RK_U32)h->rps[i].nb_refs; j++) {
212 if ((i == ST_FOLL) || (i == LT_FOLL)) {
213 ;
214 } else {
215 rps_used[nb_rps_used++] = h->rps[i].list[j];
216 }
217 }
218 }
219 // mpp_err("fill RefPicList from the DPB");
220 // fill RefPicList from the DPB
221 pp->current_poc = current_picture->poc;
222 for (i = 0, j = 0; i < MPP_ARRAY_ELEMS(pp->RefPicList); i++) {
223 const HEVCFrame *frame = NULL;
224 while (!frame && j < MPP_ARRAY_ELEMS(h->DPB)) {
225 if (&h->DPB[j] != current_picture &&
226 (h->DPB[j].flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF))) {
227 RK_U32 k = 0;
228 for (k = 0; k < nb_rps_used; k++) { /*skip fill RefPicList no used in rps*/
229 if (rps_used[k] == (RK_U32)h->DPB[j].poc) {
230 frame = &h->DPB[j];
231 }
232 }
233 }
234 j++;
235 }
236
237 if (frame && (frame->slot_index != 0xff)) {
238 fill_picture_entry(&pp->RefPicList[i], frame->slot_index, !!(frame->flags & HEVC_FRAME_FLAG_LONG_REF));
239 pp->PicOrderCntValList[i] = frame->poc;
240 mpp_buf_slot_set_flag(h->slots, frame->slot_index, SLOT_HAL_INPUT);
241 h->task->refer[i] = frame->slot_index;
242 //mpp_err("ref[%d] = %d",i,frame->slot_index);
243 } else {
244 pp->RefPicList[i].bPicEntry = 0xff;
245 pp->PicOrderCntValList[i] = 0;
246 h->task->refer[i] = -1;
247 }
248 }
249
250 #define DO_REF_LIST(ref_idx, ref_list) { \
251 const RefPicList *rpl = &h->rps[ref_idx]; \
252 for (i = 0, j = 0; i < MPP_ARRAY_ELEMS(pp->ref_list); i++) { \
253 const HEVCFrame *frame = NULL; \
254 while (!frame && j < (RK_U32)rpl->nb_refs) \
255 frame = rpl->ref[j++]; \
256 if (frame) \
257 pp->ref_list[i] = get_refpic_index(pp, frame->slot_index); \
258 else \
259 pp->ref_list[i] = 0xff; \
260 } \
261 }
262
263 // Fill short term and long term lists
264 DO_REF_LIST(ST_CURR_BEF, RefPicSetStCurrBefore);
265 DO_REF_LIST(ST_CURR_AFT, RefPicSetStCurrAfter);
266 DO_REF_LIST(LT_CURR, RefPicSetLtCurr);
267
268 // sps exension flags
269 pp->sps_extension_flag = sps->sps_extension_flag;;
270 pp->sps_range_extension_flag = sps->sps_range_extension_flag;;
271 pp->transform_skip_rotation_enabled_flag = sps->transform_skip_rotation_enabled_flag;;
272 pp->transform_skip_context_enabled_flag = sps->transform_skip_context_enabled_flag;;
273 pp->implicit_rdpcm_enabled_flag = sps->implicit_rdpcm_enabled_flag;;
274 pp->explicit_rdpcm_enabled_flag = sps->explicit_rdpcm_enabled_flag;;
275 pp->extended_precision_processing_flag = sps->extended_precision_processing_flag;;
276 pp->intra_smoothing_disabled_flag = sps->intra_smoothing_disabled_flag;;
277 pp->high_precision_offsets_enabled_flag = sps->high_precision_offsets_enabled_flag;;
278 pp->persistent_rice_adaptation_enabled_flag = sps->persistent_rice_adaptation_enabled_flag;;
279 pp->cabac_bypass_alignment_enabled_flag = sps->cabac_bypass_alignment_enabled_flag;;
280
281 // pps exension flags
282 pp->cross_component_prediction_enabled_flag = pps->cross_component_prediction_enabled_flag;
283 pp->chroma_qp_offset_list_enabled_flag = pps->chroma_qp_offset_list_enabled_flag;
284
285 // PPS extension
286 pp->log2_max_transform_skip_block_size = pps->log2_max_transform_skip_block_size;
287 pp->diff_cu_chroma_qp_offset_depth = pps->diff_cu_chroma_qp_offset_depth;
288 pp->chroma_qp_offset_list_len_minus1 = pps->chroma_qp_offset_list_len_minus1;
289 for (i = 0; i < 6; i++) {
290 pp->cb_qp_offset_list[i] = pps->cb_qp_offset_list[i];
291 pp->cr_qp_offset_list[i] = pps->cr_qp_offset_list[i];
292 }
293 }
294 extern RK_U8 mpp_hevc_diag_scan4x4_x[16];
295 extern RK_U8 mpp_hevc_diag_scan4x4_y[16];
296 extern RK_U8 mpp_hevc_diag_scan8x8_x[64];
297 extern RK_U8 mpp_hevc_diag_scan8x8_y[64];
298
fill_scaling_lists(const HEVCContext * h,DXVA_Qmatrix_HEVC * qm)299 static void fill_scaling_lists(const HEVCContext *h, DXVA_Qmatrix_HEVC *qm)
300 {
301 RK_U32 i, j, pos;
302 const HEVCPPS *pps = (HEVCPPS *)h->pps_list[h->sh.pps_id];
303 const HEVCSPS *sps = (HEVCSPS *)h->sps_list[pps->sps_id];
304 const ScalingList *sl = pps->scaling_list_data_present_flag ?
305 &pps->scaling_list : &sps->scaling_list;
306 if (!sps->scaling_list_enable_flag) {
307 return;
308 }
309 memset(qm, 0, sizeof(DXVA_Qmatrix_HEVC));
310 for (i = 0; i < 6; i++) {
311 for (j = 0; j < 16; j++) {
312 pos = 4 * mpp_hevc_diag_scan4x4_y[j] + mpp_hevc_diag_scan4x4_x[j];
313 qm->ucScalingLists0[i][j] = sl->sl[0][i][pos];
314 }
315
316 for (j = 0; j < 64; j++) {
317 pos = 8 * mpp_hevc_diag_scan8x8_y[j] + mpp_hevc_diag_scan8x8_x[j];
318 qm->ucScalingLists1[i][j] = sl->sl[1][i][pos];
319 qm->ucScalingLists2[i][j] = sl->sl[2][i][pos];
320
321 if (i < 2)
322 qm->ucScalingLists3[i][j] = sl->sl[3][i * 3][pos];
323 }
324
325 qm->ucScalingListDCCoefSizeID2[i] = sl->sl_dc[0][i];
326
327 if (i < 2)
328 qm->ucScalingListDCCoefSizeID3[i] = sl->sl_dc[1][i * 3];
329 }
330 }
331
fill_slice_short(DXVA_Slice_HEVC_Short * slice,unsigned position,unsigned size)332 static void fill_slice_short(DXVA_Slice_HEVC_Short *slice,
333 unsigned position, unsigned size)
334 {
335 memset(slice, 0, sizeof(*slice));
336 slice->BSNALunitDataLocation = position;
337 slice->SliceBytesInBuffer = size;
338 slice->wBadSliceChopping = 0;
339 }
340
init_slice_cut_param(DXVA_Slice_HEVC_Cut_Param * slice)341 static void init_slice_cut_param(DXVA_Slice_HEVC_Cut_Param *slice)
342 {
343 memset(slice, 0, sizeof(*slice));
344 }
345
h265d_parser2_syntax(void * ctx)346 RK_S32 h265d_parser2_syntax(void *ctx)
347 {
348
349 H265dContext_t *h265dctx = (H265dContext_t *)ctx;
350 const HEVCContext *h = (const HEVCContext *)h265dctx->priv_data;
351
352 h265d_dxva2_picture_context_t *ctx_pic = (h265d_dxva2_picture_context_t *)h->hal_pic_private;
353
354 /* Fill up DXVA_PicParams_HEVC */
355 fill_picture_parameters(h, &ctx_pic->pp);
356
357 /* Fill up DXVA_Qmatrix_HEVC */
358 fill_scaling_lists(h, &ctx_pic->qm);
359
360 return 0;
361 }
362
h265d_syntax_fill_slice(void * ctx,RK_S32 input_index)363 RK_S32 h265d_syntax_fill_slice(void *ctx, RK_S32 input_index)
364 {
365 H265dContext_t *h265dctx = (H265dContext_t *)ctx;
366 const HEVCContext *h = (const HEVCContext *)h265dctx->priv_data;
367 h265d_dxva2_picture_context_t *ctx_pic = (h265d_dxva2_picture_context_t *)h->hal_pic_private;
368 MppBuffer streambuf = NULL;
369 RK_S32 i, count = 0;
370 RK_U32 position = 0;
371 RK_U8 *ptr = NULL;
372 RK_U8 *current = NULL;
373 RK_U32 size = 0, length = 0;
374 // mpp_err("input_index = %d",input_index);
375 if (-1 != input_index) {
376 mpp_buf_slot_get_prop(h->packet_slots, input_index, SLOT_BUFFER, &streambuf);
377 current = ptr = (RK_U8 *)mpp_buffer_get_ptr(streambuf);
378 if (current == NULL) {
379 return MPP_ERR_NULL_PTR;
380 }
381 } else {
382 RK_S32 buff_size = 0;
383 current = (RK_U8 *)mpp_packet_get_data(h->input_packet);
384 size = (RK_U32)mpp_packet_get_size(h->input_packet);
385 for (i = 0; i < h->nb_nals; i++) {
386 length += h->nals[i].size + 4;
387 }
388 length = MPP_ALIGN(length, 16) + 64;
389 if (length > size) {
390 mpp_free(current);
391 buff_size = MPP_ALIGN(length + 10 * 1024, 1024);
392 current = mpp_malloc(RK_U8, buff_size);
393 mpp_packet_set_data(h->input_packet, (void*)current);
394 mpp_packet_set_size(h->input_packet, buff_size);
395 }
396 }
397 if (ctx_pic->max_slice_num < h->nb_nals) {
398
399 MPP_FREE(ctx_pic->slice_short);
400
401 ctx_pic->slice_short = (DXVA_Slice_HEVC_Short *)mpp_malloc(DXVA_Slice_HEVC_Short, h->nb_nals);
402 if (!ctx_pic->slice_short)
403 return MPP_ERR_NOMEM;
404
405 MPP_FREE(ctx_pic->slice_cut_param);
406
407 ctx_pic->slice_cut_param = (DXVA_Slice_HEVC_Cut_Param *)mpp_malloc(DXVA_Slice_HEVC_Cut_Param, h->nb_nals);
408 if (!ctx_pic->slice_cut_param)
409 return MPP_ERR_NOMEM;
410
411 ctx_pic->max_slice_num = h->nb_nals;
412 }
413 for (i = 0; i < h->nb_nals; i++) {
414 static const RK_U8 start_code[] = {0, 0, 1 };
415 static const RK_U32 start_code_size = sizeof(start_code);
416 BitReadCtx_t gb_cxt, *gb;
417 RK_S32 value;
418 RK_U32 nal_type;
419
420 mpp_set_bitread_ctx(&gb_cxt, (RK_U8 *)h->nals[i].data,
421 h->nals[i].size);
422 mpp_set_bitread_pseudo_code_type(&gb_cxt, PSEUDO_CODE_H264_H265);
423
424 gb = &gb_cxt;
425
426 READ_ONEBIT(gb, &value); /*this bit should be zero*/
427
428 READ_BITS(gb, 6, &nal_type);
429
430 if (nal_type >= 32) {
431 continue;
432 }
433 memcpy(current, start_code, start_code_size);
434 current += start_code_size;
435 position += start_code_size;
436 memcpy(current, h->nals[i].data, h->nals[i].size);
437 // mpp_log("h->nals[%d].size = %d", i, h->nals[i].size);
438 fill_slice_short(&ctx_pic->slice_short[count], position, h->nals[i].size);
439 init_slice_cut_param(&ctx_pic->slice_cut_param[count]);
440 current += h->nals[i].size;
441 position += h->nals[i].size;
442 count++;
443 }
444 ctx_pic->slice_count = count;
445 ctx_pic->bitstream_size = position;
446 if (-1 != input_index) {
447 ctx_pic->bitstream = (RK_U8*)ptr;
448
449 mpp_buf_slot_set_flag(h->packet_slots, input_index, SLOT_CODEC_READY);
450 mpp_buf_slot_set_flag(h->packet_slots, input_index, SLOT_HAL_INPUT);
451 } else {
452 ctx_pic->bitstream = NULL;
453 mpp_packet_set_length(h->input_packet, position);
454 }
455 return MPP_OK;
456 __BITREAD_ERR:
457 return MPP_ERR_STREAM;
458 }
459