1 /******************************************************************************
2 *
3 * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
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 *******************************************************************************
20 * @file
21 * ihevcd_parse_headers.c
22 *
23 * @brief
24 * Contains functions for parsing headers
25 *
26 * @author
27 * Harish
28 *
29 * @par List of Functions:
30 *
31 * @remarks
32 * None
33 *
34 *******************************************************************************
35 */
36
37 /*****************************************************************************/
38 /* File Includes */
39 /*****************************************************************************/
40 #include <stdio.h>
41 #include <stddef.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <assert.h>
45
46 #include "ihevc_typedefs.h"
47 #include "iv.h"
48 #include "ivd.h"
49 #include "ihevcd_cxa.h"
50
51 #include "ihevc_defs.h"
52 #include "ihevc_debug.h"
53 #include "ihevc_defs.h"
54 #include "ihevc_structs.h"
55 #include "ihevc_buf_mgr.h"
56 #include "ihevc_dpb_mgr.h"
57 #include "ihevc_macros.h"
58 #include "ihevc_platform_macros.h"
59 #include "ihevc_cabac_tables.h"
60 #include "ihevc_common_tables.h"
61 #include "ihevc_quant_tables.h"
62
63 #include "ihevcd_trace.h"
64 #include "ihevcd_defs.h"
65 #include "ihevcd_function_selector.h"
66 #include "ihevcd_structs.h"
67 #include "ihevcd_error.h"
68 #include "ihevcd_debug.h"
69 #include "ihevcd_nal.h"
70 #include "ihevcd_bitstream.h"
71 #include "ihevcd_parse_headers.h"
72 #include "ihevcd_parse_slice_header.h"
73 #include "ihevcd_ref_list.h"
74
75 mv_buf_t* ihevcd_mv_mgr_get_poc(buf_mgr_t *ps_mv_buf_mgr, UWORD32 abs_poc);
76
77 /**
78 *******************************************************************************
79 *
80 * @brief
81 * Parses VPS operation point
82 *
83 * @par Description
84 * Parses VPS operation point as per section 7.3.5
85 *
86 * @param[out] ps_vps
87 * Pointer to VPS structure
88 *
89 * @param[in] ps_bitstrm
90 * Pointer to bitstream structure
91 *
92 * @param[in] ops_idx
93 * Operating point index
94 *
95 * @returns Error code from IHEVCD_ERROR_T
96 *
97 * @remarks
98 *
99 *******************************************************************************
100 */
ihevcd_operation_point_set(vps_t * ps_vps,bitstrm_t * ps_bitstrm,WORD32 ops_idx)101 IHEVCD_ERROR_T ihevcd_operation_point_set(vps_t *ps_vps, bitstrm_t *ps_bitstrm, WORD32 ops_idx)
102 {
103 WORD32 i;
104 WORD32 value;
105
106 IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
107 UNUSED(ops_idx);
108 for(i = 0; i <= ps_vps->i1_vps_max_nuh_reserved_zero_layer_id; i++)
109 {
110 BITS_PARSE("list_entry_l0[ i ]", value, ps_bitstrm, 1);
111 //ps_vps->ai1_layer_id_included_flag[ops_idx][i] = value;
112
113 }
114 UNUSED(value);
115
116 return ret;
117 }
118
119 /**
120 *******************************************************************************
121 *
122 * @brief
123 * Parses pic_lismod_t (picture list mod syntax) Section:7.3.8.3 Reference
124 * picture list mod syntax
125 *
126 * @par Description:
127 * Parse pict list mod synt and update pic_lismod_t struct
128 *
129 * @param[in] ps_codec
130 * Pointer to codec context
131 *
132 * @returns Error code from IHEVCD_ERROR_T
133 *
134 * @remarks
135 *
136 *
137 *******************************************************************************
138 */
139
ihevcd_ref_pic_list_modification(bitstrm_t * ps_bitstrm,slice_header_t * ps_slice_hdr,WORD32 num_poc_total_curr)140 WORD32 ihevcd_ref_pic_list_modification(bitstrm_t *ps_bitstrm,
141 slice_header_t *ps_slice_hdr,
142 WORD32 num_poc_total_curr)
143 {
144 WORD32 ret = IHEVCD_SUCCESS;
145 WORD32 value;
146 WORD32 i;
147 rplm_t *ps_rplm;
148 WORD32 num_bits_list_entry;
149
150 ps_rplm = &(ps_slice_hdr->s_rplm);
151
152 /* Calculate Ceil(Log2(num_poc_total_curr)) */
153 {
154 num_bits_list_entry = 32 - CLZ(num_poc_total_curr);
155 /* Check if num_poc_total_curr is power of 2 */
156 if(0 == (num_poc_total_curr & (num_poc_total_curr - 1)))
157 {
158 num_bits_list_entry--;
159 }
160 }
161
162 if(ps_slice_hdr->i1_slice_type == PSLICE || ps_slice_hdr->i1_slice_type == BSLICE)
163 {
164 BITS_PARSE("ref_pic_list_modification_flag_l0", value, ps_bitstrm, 1);
165 ps_rplm->i1_ref_pic_list_modification_flag_l0 = value;
166
167 if(ps_rplm->i1_ref_pic_list_modification_flag_l0)
168 for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
169 {
170 BITS_PARSE("list_entry_l0", value, ps_bitstrm, num_bits_list_entry);
171 ps_rplm->i1_list_entry_l0[i] = value;
172
173 ps_rplm->i1_list_entry_l0[i] = CLIP3(ps_rplm->i1_list_entry_l0[i], 0, num_poc_total_curr - 1);
174 }
175 }
176
177 if(ps_slice_hdr->i1_slice_type == BSLICE)
178 {
179 BITS_PARSE("ref_pic_list_modification_flag_l1", value, ps_bitstrm, 1);
180 ps_rplm->i1_ref_pic_list_modification_flag_l1 = value;
181
182 if(ps_rplm->i1_ref_pic_list_modification_flag_l1)
183 for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
184 {
185 BITS_PARSE("list_entry_l1", value, ps_bitstrm, num_bits_list_entry);
186 ps_rplm->i1_list_entry_l1[i] = value;
187
188 ps_rplm->i1_list_entry_l1[i] = CLIP3(ps_rplm->i1_list_entry_l1[i], 0, num_poc_total_curr - 1);
189 }
190
191 }
192
193 return ret;
194 }
195
196 /**
197 *******************************************************************************
198 *
199 * @brief
200 * Parse Slice Header
201 * slice_header_syntax()
202 *
203 * @par Description:
204 * Parse Slice Header as per Section: 7.3.8
205 *
206 * @param[in] ps_codec
207 * Pointer to codec context
208 *
209 * @returns Error code from IHEVCD_ERROR_T
210 *
211 * @remarks
212 *
213 *
214 *******************************************************************************
215 */
216
ihevcd_parse_slice_header(codec_t * ps_codec,nal_header_t * ps_nal)217 IHEVCD_ERROR_T ihevcd_parse_slice_header(codec_t *ps_codec,
218 nal_header_t *ps_nal)
219 {
220 IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
221 WORD32 value;
222 WORD32 i, j;
223 WORD32 sps_id;
224
225 pps_t *ps_pps;
226 sps_t *ps_sps;
227 slice_header_t *ps_slice_hdr;
228 WORD32 disable_deblocking_filter_flag;
229 bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
230 WORD32 idr_pic_flag;
231 WORD32 pps_id;
232 WORD32 first_slice_in_pic_flag;
233 WORD32 no_output_of_prior_pics_flag = 0;
234 WORD8 i1_nal_unit_type = ps_nal->i1_nal_unit_type;
235 WORD32 num_poc_total_curr = 0;
236 WORD32 slice_address;
237
238 if(ps_codec->i4_slice_error == 1)
239 return ret;
240
241 idr_pic_flag = (NAL_IDR_W_LP == i1_nal_unit_type) ||
242 (NAL_IDR_N_LP == i1_nal_unit_type);
243
244
245 BITS_PARSE("first_slice_in_pic_flag", first_slice_in_pic_flag, ps_bitstrm, 1);
246 if((NAL_BLA_W_LP <= i1_nal_unit_type) &&
247 (NAL_RSV_RAP_VCL23 >= i1_nal_unit_type))
248 {
249 BITS_PARSE("no_output_of_prior_pics_flag", no_output_of_prior_pics_flag, ps_bitstrm, 1);
250 }
251 UEV_PARSE("pic_parameter_set_id", pps_id, ps_bitstrm);
252 pps_id = CLIP3(pps_id, 0, MAX_PPS_CNT - 2);
253
254 /* Get the current PPS structure */
255 ps_pps = ps_codec->s_parse.ps_pps_base + pps_id;
256 if(0 == ps_pps->i1_pps_valid)
257 {
258 pps_t *ps_pps_ref = ps_codec->ps_pps_base;
259 while(0 == ps_pps_ref->i1_pps_valid)
260 {
261 ps_pps_ref++;
262 if((ps_pps_ref - ps_codec->ps_pps_base >= MAX_PPS_CNT - 1))
263 return IHEVCD_INVALID_HEADER;
264 }
265
266 ihevcd_copy_pps(ps_codec, pps_id, ps_pps_ref->i1_pps_id);
267 }
268
269 /* Get SPS id for the current PPS */
270 sps_id = ps_pps->i1_sps_id;
271
272 /* Get the current SPS structure */
273 ps_sps = ps_codec->s_parse.ps_sps_base + sps_id;
274
275 /* When the current slice is the first in a pic,
276 * check whether the previous frame is complete
277 * If the previous frame is incomplete -
278 * treat the remaining CTBs as skip */
279 if((0 != ps_codec->u4_pic_cnt || ps_codec->i4_pic_present) &&
280 first_slice_in_pic_flag)
281 {
282 if(ps_codec->i4_pic_present)
283 {
284 slice_header_t *ps_slice_hdr_next;
285 ps_codec->i4_slice_error = 1;
286 ps_codec->s_parse.i4_cur_slice_idx--;
287 if(ps_codec->s_parse.i4_cur_slice_idx < 0)
288 ps_codec->s_parse.i4_cur_slice_idx = 0;
289
290 ps_slice_hdr_next = ps_codec->s_parse.ps_slice_hdr_base + ((ps_codec->s_parse.i4_cur_slice_idx + 1) & (MAX_SLICE_HDR_CNT - 1));
291 ps_slice_hdr_next->i2_ctb_x = 0;
292 ps_slice_hdr_next->i2_ctb_y = ps_codec->s_parse.ps_sps->i2_pic_ht_in_ctb;
293 return ret;
294 }
295 else
296 {
297 ps_codec->i4_slice_error = 0;
298 }
299 }
300
301 if(first_slice_in_pic_flag)
302 {
303 ps_codec->s_parse.i4_cur_slice_idx = 0;
304 }
305 else
306 {
307 /* If the current slice is not the first slice in the pic,
308 * but the first one to be parsed, set the current slice indx to 1
309 * Treat the first slice to be missing and copy the current slice header
310 * to the first one */
311 if(0 == ps_codec->i4_pic_present)
312 ps_codec->s_parse.i4_cur_slice_idx = 1;
313 }
314
315 ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base + (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1));
316
317
318 if((ps_pps->i1_dependent_slice_enabled_flag) &&
319 (!first_slice_in_pic_flag))
320 {
321 BITS_PARSE("dependent_slice_flag", value, ps_bitstrm, 1);
322
323 /* If dependendent slice, copy slice header from previous slice */
324 if(value && (ps_codec->s_parse.i4_cur_slice_idx > 0))
325 {
326 ihevcd_copy_slice_hdr(ps_codec,
327 (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1)),
328 ((ps_codec->s_parse.i4_cur_slice_idx - 1) & (MAX_SLICE_HDR_CNT - 1)));
329 }
330 ps_slice_hdr->i1_dependent_slice_flag = value;
331 }
332 else
333 {
334 ps_slice_hdr->i1_dependent_slice_flag = 0;
335 }
336 ps_slice_hdr->i1_nal_unit_type = i1_nal_unit_type;
337 ps_slice_hdr->i1_pps_id = pps_id;
338 ps_slice_hdr->i1_first_slice_in_pic_flag = first_slice_in_pic_flag;
339
340 ps_slice_hdr->i1_no_output_of_prior_pics_flag = 1;
341 if((NAL_BLA_W_LP <= i1_nal_unit_type) &&
342 (NAL_RSV_RAP_VCL23 >= i1_nal_unit_type))
343 {
344 ps_slice_hdr->i1_no_output_of_prior_pics_flag = no_output_of_prior_pics_flag;
345 }
346 ps_slice_hdr->i1_pps_id = pps_id;
347
348 if(!ps_slice_hdr->i1_first_slice_in_pic_flag)
349 {
350 WORD32 num_bits;
351
352 /* Use CLZ to compute Ceil( Log2( PicSizeInCtbsY ) ) */
353 num_bits = 32 - CLZ(ps_sps->i4_pic_size_in_ctb - 1);
354 BITS_PARSE("slice_address", value, ps_bitstrm, num_bits);
355
356 slice_address = value;
357 /* If slice address is greater than the number of CTBs in a picture,
358 * ignore the slice */
359 if(value >= ps_sps->i4_pic_size_in_ctb)
360 return IHEVCD_IGNORE_SLICE;
361 }
362 else
363 {
364 slice_address = 0;
365 }
366
367 if(!ps_slice_hdr->i1_dependent_slice_flag)
368 {
369 ps_slice_hdr->i1_pic_output_flag = 1;
370 ps_slice_hdr->i4_pic_order_cnt_lsb = 0;
371 ps_slice_hdr->i1_num_long_term_sps = 0;
372 ps_slice_hdr->i1_num_long_term_pics = 0;
373
374 for(i = 0; i < ps_pps->i1_num_extra_slice_header_bits; i++)
375 {
376 BITS_PARSE("slice_reserved_undetermined_flag[ i ]", value, ps_bitstrm, 1);
377 //slice_reserved_undetermined_flag[ i ]
378 }
379 UEV_PARSE("slice_type", value, ps_bitstrm);
380 ps_slice_hdr->i1_slice_type = value;
381
382 /* If the picture is IRAP, slice type must be equal to ISLICE */
383 if((ps_slice_hdr->i1_nal_unit_type >= NAL_BLA_W_LP) &&
384 (ps_slice_hdr->i1_nal_unit_type <= NAL_RSV_RAP_VCL23))
385 ps_slice_hdr->i1_slice_type = ISLICE;
386
387 if((ps_slice_hdr->i1_slice_type < 0) ||
388 (ps_slice_hdr->i1_slice_type > 2))
389 return IHEVCD_IGNORE_SLICE;
390
391 if(ps_pps->i1_output_flag_present_flag)
392 {
393 BITS_PARSE("pic_output_flag", value, ps_bitstrm, 1);
394 ps_slice_hdr->i1_pic_output_flag = value;
395 }
396 ps_slice_hdr->i1_colour_plane_id = 0;
397 if(1 == ps_sps->i1_separate_colour_plane_flag)
398 {
399 BITS_PARSE("colour_plane_id", value, ps_bitstrm, 2);
400 ps_slice_hdr->i1_colour_plane_id = value;
401 }
402 ps_slice_hdr->i1_slice_temporal_mvp_enable_flag = 0;
403
404 if(!idr_pic_flag)
405 {
406
407 WORD32 st_rps_idx;
408 WORD32 num_neg_pics;
409 WORD32 num_pos_pics;
410 WORD8 *pi1_used;
411
412 BITS_PARSE("pic_order_cnt_lsb", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
413 //value = ihevcd_extend_sign_bit(value, ps_sps->i1_log2_max_pic_order_cnt_lsb);
414 ps_slice_hdr->i4_pic_order_cnt_lsb = value;
415
416 BITS_PARSE("short_term_ref_pic_set_sps_flag", value, ps_bitstrm, 1);
417 ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag = value;
418
419 if(1 == ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag)
420 {
421 WORD32 numbits;
422
423 ps_slice_hdr->i1_short_term_ref_pic_set_idx = 0;
424 if(ps_sps->i1_num_short_term_ref_pic_sets > 1)
425 {
426 numbits = 32 - CLZ(ps_sps->i1_num_short_term_ref_pic_sets - 1);
427 BITS_PARSE("short_term_ref_pic_set_idx", value, ps_bitstrm, numbits);
428 ps_slice_hdr->i1_short_term_ref_pic_set_idx = value;
429 }
430
431 st_rps_idx = ps_slice_hdr->i1_short_term_ref_pic_set_idx;
432 num_neg_pics = ps_sps->as_stref_picset[st_rps_idx].i1_num_neg_pics;
433 num_pos_pics = ps_sps->as_stref_picset[st_rps_idx].i1_num_pos_pics;
434 pi1_used = ps_sps->as_stref_picset[st_rps_idx].ai1_used;
435 }
436 else
437 {
438 ihevcd_short_term_ref_pic_set(ps_bitstrm,
439 &ps_sps->as_stref_picset[0],
440 ps_sps->i1_num_short_term_ref_pic_sets,
441 ps_sps->i1_num_short_term_ref_pic_sets,
442 &ps_slice_hdr->s_stref_picset);
443
444 st_rps_idx = ps_sps->i1_num_short_term_ref_pic_sets;
445 num_neg_pics = ps_slice_hdr->s_stref_picset.i1_num_neg_pics;
446 num_pos_pics = ps_slice_hdr->s_stref_picset.i1_num_pos_pics;
447 pi1_used = ps_slice_hdr->s_stref_picset.ai1_used;
448 }
449
450 if(ps_sps->i1_long_term_ref_pics_present_flag)
451 {
452 if(ps_sps->i1_num_long_term_ref_pics_sps > 0)
453 {
454 UEV_PARSE("num_long_term_sps", value, ps_bitstrm);
455 ps_slice_hdr->i1_num_long_term_sps = value;
456
457 ps_slice_hdr->i1_num_long_term_sps = CLIP3(ps_slice_hdr->i1_num_long_term_sps,
458 0, MAX_DPB_SIZE - num_neg_pics - num_pos_pics);
459 }
460 UEV_PARSE("num_long_term_pics", value, ps_bitstrm);
461 ps_slice_hdr->i1_num_long_term_pics = value;
462 ps_slice_hdr->i1_num_long_term_pics = CLIP3(ps_slice_hdr->i1_num_long_term_pics,
463 0, MAX_DPB_SIZE - num_neg_pics - num_pos_pics -
464 ps_slice_hdr->i1_num_long_term_sps);
465
466 for(i = 0; i < (ps_slice_hdr->i1_num_long_term_sps +
467 ps_slice_hdr->i1_num_long_term_pics); i++)
468 {
469 if(i < ps_slice_hdr->i1_num_long_term_sps)
470 {
471 /* Use CLZ to compute Ceil( Log2( num_long_term_ref_pics_sps ) ) */
472 if (ps_sps->i1_num_long_term_ref_pics_sps > 1)
473 {
474 WORD32 num_bits = 32 - CLZ(ps_sps->i1_num_long_term_ref_pics_sps - 1);
475 BITS_PARSE("lt_idx_sps[ i ]", value, ps_bitstrm, num_bits);
476 }
477 else
478 {
479 value = 0;
480 }
481 ps_slice_hdr->ai4_poc_lsb_lt[i] = ps_sps->au2_lt_ref_pic_poc_lsb_sps[value];
482 ps_slice_hdr->ai1_used_by_curr_pic_lt_flag[i] = ps_sps->ai1_used_by_curr_pic_lt_sps_flag[value];
483
484 }
485 else
486 {
487 BITS_PARSE("poc_lsb_lt[ i ]", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
488 ps_slice_hdr->ai4_poc_lsb_lt[i] = value;
489
490 BITS_PARSE("used_by_curr_pic_lt_flag[ i ]", value, ps_bitstrm, 1);
491 ps_slice_hdr->ai1_used_by_curr_pic_lt_flag[i] = value;
492
493 }
494 BITS_PARSE("delta_poc_msb_present_flag[ i ]", value, ps_bitstrm, 1);
495 ps_slice_hdr->ai1_delta_poc_msb_present_flag[i] = value;
496
497
498 ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i] = 0;
499 if(ps_slice_hdr->ai1_delta_poc_msb_present_flag[i])
500 {
501
502 UEV_PARSE("delata_poc_msb_cycle_lt[ i ]", value, ps_bitstrm);
503 ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i] = value;
504 }
505
506 if((i != 0) && (i != ps_slice_hdr->i1_num_long_term_sps))
507 {
508 ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i] += ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i - 1];
509 }
510
511 }
512 }
513
514 for(i = 0; i < num_neg_pics + num_pos_pics; i++)
515 {
516 if(pi1_used[i])
517 {
518 num_poc_total_curr++;
519 }
520 }
521 for(i = 0; i < ps_slice_hdr->i1_num_long_term_sps + ps_slice_hdr->i1_num_long_term_pics; i++)
522 {
523 if(ps_slice_hdr->ai1_used_by_curr_pic_lt_flag[i])
524 {
525 num_poc_total_curr++;
526 }
527 }
528
529
530 if(ps_sps->i1_sps_temporal_mvp_enable_flag)
531 {
532 BITS_PARSE("enable_temporal_mvp_flag", value, ps_bitstrm, 1);
533 ps_slice_hdr->i1_slice_temporal_mvp_enable_flag = value;
534 }
535
536 }
537 ps_slice_hdr->i1_slice_sao_luma_flag = 0;
538 ps_slice_hdr->i1_slice_sao_chroma_flag = 0;
539 if(ps_sps->i1_sample_adaptive_offset_enabled_flag)
540 {
541 BITS_PARSE("slice_sao_luma_flag", value, ps_bitstrm, 1);
542 ps_slice_hdr->i1_slice_sao_luma_flag = value;
543
544 BITS_PARSE("slice_sao_chroma_flag", value, ps_bitstrm, 1);
545 ps_slice_hdr->i1_slice_sao_chroma_flag = value;
546
547 }
548
549 ps_slice_hdr->i1_max_num_merge_cand = 1;
550 ps_slice_hdr->i1_cabac_init_flag = 0;
551
552 ps_slice_hdr->i1_num_ref_idx_l0_active = 0;
553 ps_slice_hdr->i1_num_ref_idx_l1_active = 0;
554 ps_slice_hdr->i1_slice_cb_qp_offset = 0;
555 ps_slice_hdr->i1_slice_cr_qp_offset = 0;
556 if((PSLICE == ps_slice_hdr->i1_slice_type) ||
557 (BSLICE == ps_slice_hdr->i1_slice_type))
558 {
559 BITS_PARSE("num_ref_idx_active_override_flag", value, ps_bitstrm, 1);
560 ps_slice_hdr->i1_num_ref_idx_active_override_flag = value;
561
562 if(ps_slice_hdr->i1_num_ref_idx_active_override_flag)
563 {
564 UEV_PARSE("num_ref_idx_l0_active_minus1", value, ps_bitstrm);
565 ps_slice_hdr->i1_num_ref_idx_l0_active = value + 1;
566
567 if(BSLICE == ps_slice_hdr->i1_slice_type)
568 {
569 UEV_PARSE("num_ref_idx_l1_active_minus1", value, ps_bitstrm);
570 ps_slice_hdr->i1_num_ref_idx_l1_active = value + 1;
571 }
572
573 }
574 else
575 {
576 ps_slice_hdr->i1_num_ref_idx_l0_active = ps_pps->i1_num_ref_idx_l0_default_active;
577
578 if(BSLICE == ps_slice_hdr->i1_slice_type)
579 {
580 ps_slice_hdr->i1_num_ref_idx_l1_active = ps_pps->i1_num_ref_idx_l1_default_active;
581 }
582 }
583
584 ps_slice_hdr->i1_num_ref_idx_l0_active = CLIP3(ps_slice_hdr->i1_num_ref_idx_l0_active, 0, MAX_DPB_SIZE - 1);
585 ps_slice_hdr->i1_num_ref_idx_l1_active = CLIP3(ps_slice_hdr->i1_num_ref_idx_l1_active, 0, MAX_DPB_SIZE - 1);
586
587 if(0 == num_poc_total_curr)
588 return IHEVCD_IGNORE_SLICE;
589 if((ps_pps->i1_lists_modification_present_flag) && (num_poc_total_curr > 1))
590 {
591 ihevcd_ref_pic_list_modification(ps_bitstrm,
592 ps_slice_hdr, num_poc_total_curr);
593 }
594 else
595 {
596 ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l0 = 0;
597 ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l1 = 0;
598 }
599
600 if(BSLICE == ps_slice_hdr->i1_slice_type)
601 {
602 BITS_PARSE("mvd_l1_zero_flag", value, ps_bitstrm, 1);
603 ps_slice_hdr->i1_mvd_l1_zero_flag = value;
604 }
605
606 ps_slice_hdr->i1_cabac_init_flag = 0;
607 if(ps_pps->i1_cabac_init_present_flag)
608 {
609 BITS_PARSE("cabac_init_flag", value, ps_bitstrm, 1);
610 ps_slice_hdr->i1_cabac_init_flag = value;
611
612 }
613 ps_slice_hdr->i1_collocated_from_l0_flag = 1;
614 ps_slice_hdr->i1_collocated_ref_idx = 0;
615 if(ps_slice_hdr->i1_slice_temporal_mvp_enable_flag)
616 {
617 if(BSLICE == ps_slice_hdr->i1_slice_type)
618 {
619 BITS_PARSE("collocated_from_l0_flag", value, ps_bitstrm, 1);
620 ps_slice_hdr->i1_collocated_from_l0_flag = value;
621 }
622
623 if((ps_slice_hdr->i1_collocated_from_l0_flag && (ps_slice_hdr->i1_num_ref_idx_l0_active > 1)) ||
624 (!ps_slice_hdr->i1_collocated_from_l0_flag && (ps_slice_hdr->i1_num_ref_idx_l1_active > 1)))
625 {
626 UEV_PARSE("collocated_ref_idx", value, ps_bitstrm);
627 ps_slice_hdr->i1_collocated_ref_idx = value;
628 }
629
630 }
631 ps_slice_hdr->i1_collocated_ref_idx = CLIP3(ps_slice_hdr->i1_collocated_ref_idx, 0, MAX_DPB_SIZE - 1);
632
633 if((ps_pps->i1_weighted_pred_flag && (PSLICE == ps_slice_hdr->i1_slice_type)) ||
634 (ps_pps->i1_weighted_bipred_flag && (BSLICE == ps_slice_hdr->i1_slice_type)))
635 {
636 ihevcd_parse_pred_wt_ofst(ps_bitstrm, ps_sps, ps_pps, ps_slice_hdr);
637 }
638 UEV_PARSE("five_minus_max_num_merge_cand", value, ps_bitstrm);
639 ps_slice_hdr->i1_max_num_merge_cand = 5 - value;
640
641 }
642 ps_slice_hdr->i1_max_num_merge_cand = CLIP3(ps_slice_hdr->i1_max_num_merge_cand, 1, 5);
643 SEV_PARSE("slice_qp_delta", value, ps_bitstrm);
644 ps_slice_hdr->i1_slice_qp_delta = value;
645
646 if(ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag)
647 {
648 SEV_PARSE("slice_cb_qp_offset", value, ps_bitstrm);
649 ps_slice_hdr->i1_slice_cb_qp_offset = value;
650
651 SEV_PARSE("slice_cr_qp_offset", value, ps_bitstrm);
652 ps_slice_hdr->i1_slice_cr_qp_offset = value;
653
654 }
655 ps_slice_hdr->i1_deblocking_filter_override_flag = 0;
656 ps_slice_hdr->i1_slice_disable_deblocking_filter_flag = ps_pps->i1_pic_disable_deblocking_filter_flag;
657 ps_slice_hdr->i1_beta_offset_div2 = ps_pps->i1_beta_offset_div2;
658 ps_slice_hdr->i1_tc_offset_div2 = ps_pps->i1_tc_offset_div2;
659
660 disable_deblocking_filter_flag = ps_pps->i1_pic_disable_deblocking_filter_flag;
661
662 if(ps_pps->i1_deblocking_filter_control_present_flag)
663 {
664
665 if(ps_pps->i1_deblocking_filter_override_enabled_flag)
666 {
667 BITS_PARSE("deblocking_filter_override_flag", value, ps_bitstrm, 1);
668 ps_slice_hdr->i1_deblocking_filter_override_flag = value;
669 }
670
671 if(ps_slice_hdr->i1_deblocking_filter_override_flag)
672 {
673 BITS_PARSE("slice_disable_deblocking_filter_flag", value, ps_bitstrm, 1);
674 ps_slice_hdr->i1_slice_disable_deblocking_filter_flag = value;
675 disable_deblocking_filter_flag = ps_slice_hdr->i1_slice_disable_deblocking_filter_flag;
676
677 if(!ps_slice_hdr->i1_slice_disable_deblocking_filter_flag)
678 {
679 SEV_PARSE("beta_offset_div2", value, ps_bitstrm);
680 ps_slice_hdr->i1_beta_offset_div2 = value;
681
682 SEV_PARSE("tc_offset_div2", value, ps_bitstrm);
683 ps_slice_hdr->i1_tc_offset_div2 = value;
684
685 }
686 }
687 }
688
689 ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag = ps_pps->i1_loop_filter_across_slices_enabled_flag;
690 if(ps_pps->i1_loop_filter_across_slices_enabled_flag &&
691 (ps_slice_hdr->i1_slice_sao_luma_flag || ps_slice_hdr->i1_slice_sao_chroma_flag || !disable_deblocking_filter_flag))
692 {
693 BITS_PARSE("slice_loop_filter_across_slices_enabled_flag", value, ps_bitstrm, 1);
694 ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag = value;
695 }
696
697 }
698
699 /* Check sanity of slice */
700 if((!first_slice_in_pic_flag) &&
701 (ps_codec->i4_pic_present))
702 {
703 slice_header_t *ps_slice_hdr_base = ps_codec->ps_slice_hdr_base;
704
705
706 /* According to the standard, the above conditions must be satisfied - But for error resilience,
707 * only the following conditions are checked */
708 if((ps_slice_hdr_base->i1_pps_id != ps_slice_hdr->i1_pps_id) ||
709 (ps_slice_hdr_base->i4_pic_order_cnt_lsb != ps_slice_hdr->i4_pic_order_cnt_lsb))
710 {
711 return IHEVCD_IGNORE_SLICE;
712 }
713
714 }
715
716
717 if(0 == ps_codec->i4_pic_present)
718 {
719 ps_slice_hdr->i4_abs_pic_order_cnt = ihevcd_calc_poc(ps_codec, ps_nal, ps_sps->i1_log2_max_pic_order_cnt_lsb, ps_slice_hdr->i4_pic_order_cnt_lsb);
720 }
721 else
722 {
723 ps_slice_hdr->i4_abs_pic_order_cnt = ps_codec->s_parse.i4_abs_pic_order_cnt;
724 }
725
726
727 if(!first_slice_in_pic_flag)
728 {
729 /* Check if the current slice belongs to the same pic (Pic being parsed) */
730 if(ps_codec->s_parse.i4_abs_pic_order_cnt == ps_slice_hdr->i4_abs_pic_order_cnt)
731 {
732
733 /* If the Next CTB's index is less than the slice address,
734 * the previous slice is incomplete.
735 * Indicate slice error, and treat the remaining CTBs as skip */
736 if(slice_address > ps_codec->s_parse.i4_next_ctb_indx)
737 {
738 if(ps_codec->i4_pic_present)
739 {
740 slice_header_t *ps_slice_hdr_next;
741 ps_codec->i4_slice_error = 1;
742 ps_codec->s_parse.i4_cur_slice_idx--;
743 if(ps_codec->s_parse.i4_cur_slice_idx < 0)
744 ps_codec->s_parse.i4_cur_slice_idx = 0;
745
746 ps_slice_hdr_next = ps_codec->s_parse.ps_slice_hdr_base + ((ps_codec->s_parse.i4_cur_slice_idx + 1) & (MAX_SLICE_HDR_CNT - 1));
747 ps_slice_hdr_next->i2_ctb_x = slice_address % ps_sps->i2_pic_wd_in_ctb;
748 ps_slice_hdr_next->i2_ctb_y = slice_address / ps_sps->i2_pic_wd_in_ctb;
749 return ret;
750 }
751 else
752 {
753 return IHEVCD_IGNORE_SLICE;
754 }
755 }
756 /* If the slice address is less than the next CTB's index,
757 * extra CTBs have been decoded in the previous slice.
758 * Ignore the current slice. Treat it as incomplete */
759 else if(slice_address < ps_codec->s_parse.i4_next_ctb_indx)
760 {
761 return IHEVCD_IGNORE_SLICE;
762 }
763 else
764 {
765 ps_codec->i4_slice_error = 0;
766 }
767 }
768
769 /* The current slice does not belong to the pic that is being parsed */
770 else
771 {
772 /* The previous pic is incomplete.
773 * Treat the remaining CTBs as skip */
774 if(ps_codec->i4_pic_present)
775 {
776 slice_header_t *ps_slice_hdr_next;
777 ps_codec->i4_slice_error = 1;
778 ps_codec->s_parse.i4_cur_slice_idx--;
779 if(ps_codec->s_parse.i4_cur_slice_idx < 0)
780 ps_codec->s_parse.i4_cur_slice_idx = 0;
781
782 ps_slice_hdr_next = ps_codec->s_parse.ps_slice_hdr_base + ((ps_codec->s_parse.i4_cur_slice_idx + 1) & (MAX_SLICE_HDR_CNT - 1));
783 ps_slice_hdr_next->i2_ctb_x = 0;
784 ps_slice_hdr_next->i2_ctb_y = ps_codec->s_parse.ps_sps->i2_pic_ht_in_ctb;
785 return ret;
786 }
787
788 /* If the previous pic is complete,
789 * return if the current slice is dependant
790 * otherwise, update the parse context's POC */
791 else
792 {
793 if(ps_slice_hdr->i1_dependent_slice_flag)
794 return IHEVCD_IGNORE_SLICE;
795
796 ps_codec->s_parse.i4_abs_pic_order_cnt = ps_slice_hdr->i4_abs_pic_order_cnt;
797 }
798 }
799 }
800
801 /* If the slice is the first slice in the pic, update the parse context's POC */
802 else
803 {
804 /* If the first slice is repeated, ignore the second occurrence
805 * If any other slice is repeated, the CTB addr will be greater than the slice addr,
806 * and hence the second occurrence is ignored */
807 if(ps_codec->s_parse.i4_abs_pic_order_cnt == ps_slice_hdr->i4_abs_pic_order_cnt)
808 return IHEVCD_IGNORE_SLICE;
809
810 ps_codec->s_parse.i4_abs_pic_order_cnt = ps_slice_hdr->i4_abs_pic_order_cnt;
811 }
812
813 // printf("POC: %d\n", ps_slice_hdr->i4_abs_pic_order_cnt);
814 // AEV_TRACE("POC", ps_slice_hdr->i4_abs_pic_order_cnt, 0);
815 ps_slice_hdr->i4_num_entry_point_offsets = 0;
816 if((ps_pps->i1_tiles_enabled_flag) ||
817 (ps_pps->i1_entropy_coding_sync_enabled_flag))
818 {
819 UEV_PARSE("num_entry_point_offsets", value, ps_bitstrm);
820 ps_slice_hdr->i4_num_entry_point_offsets = value;
821
822 {
823 WORD32 max_num_entry_point_offsets;
824 if((ps_pps->i1_tiles_enabled_flag) &&
825 (ps_pps->i1_entropy_coding_sync_enabled_flag))
826 {
827 max_num_entry_point_offsets = ps_pps->i1_num_tile_columns * (ps_sps->i2_pic_ht_in_ctb - 1);
828 }
829 else if(ps_pps->i1_tiles_enabled_flag)
830 {
831 max_num_entry_point_offsets = ps_pps->i1_num_tile_columns * ps_pps->i1_num_tile_rows;
832 }
833 else
834 {
835 max_num_entry_point_offsets = (ps_sps->i2_pic_ht_in_ctb - 1);
836 }
837
838 ps_slice_hdr->i4_num_entry_point_offsets = CLIP3(ps_slice_hdr->i4_num_entry_point_offsets,
839 0, max_num_entry_point_offsets);
840 }
841
842 if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
843 {
844 UEV_PARSE("offset_len_minus1", value, ps_bitstrm);
845 ps_slice_hdr->i1_offset_len = value + 1;
846
847 for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
848 {
849 BITS_PARSE("entry_point_offset", value, ps_bitstrm, ps_slice_hdr->i1_offset_len);
850
851 /* TODO: pu4_entry_point_offset needs to be initialized */
852 //ps_slice_hdr->pu4_entry_point_offset[i] = value;
853 }
854
855 }
856 }
857
858 if(ps_pps->i1_slice_header_extension_present_flag)
859 {
860 UEV_PARSE("slice_header_extension_length", value, ps_bitstrm);
861 ps_slice_hdr->i2_slice_header_extension_length = value;
862
863
864 for(i = 0; i < ps_slice_hdr->i2_slice_header_extension_length; i++)
865 {
866 BITS_PARSE("slice_header_extension_data_byte", value, ps_bitstrm, 8);
867 }
868
869 }
870
871 ihevcd_bits_flush_to_byte_boundary(ps_bitstrm);
872
873 if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
874 return IHEVCD_INVALID_PARAMETER;
875
876 {
877 dpb_mgr_t *ps_dpb_mgr = (dpb_mgr_t *)ps_codec->pv_dpb_mgr;
878 WORD32 r_idx;
879
880 if((NAL_IDR_W_LP == ps_slice_hdr->i1_nal_unit_type) ||
881 (NAL_IDR_N_LP == ps_slice_hdr->i1_nal_unit_type) ||
882 (NAL_BLA_N_LP == ps_slice_hdr->i1_nal_unit_type) ||
883 (NAL_BLA_W_DLP == ps_slice_hdr->i1_nal_unit_type) ||
884 (NAL_BLA_W_LP == ps_slice_hdr->i1_nal_unit_type) ||
885 (0 == ps_codec->u4_pic_cnt))
886 {
887 for(i = 0; i < MAX_DPB_BUFS; i++)
888 {
889 if(ps_dpb_mgr->as_dpb_info[i].ps_pic_buf)
890 {
891 pic_buf_t *ps_pic_buf = ps_dpb_mgr->as_dpb_info[i].ps_pic_buf;
892 mv_buf_t *ps_mv_buf;
893
894 /* Long term index is set to MAX_DPB_BUFS to ensure it is not added as LT */
895 ihevc_dpb_mgr_del_ref((dpb_mgr_t *)ps_codec->pv_dpb_mgr, (buf_mgr_t *)ps_codec->pv_pic_buf_mgr, ps_pic_buf->i4_abs_poc);
896 /* Find buffer id of the MV bank corresponding to the buffer being freed (Buffer with POC of u4_abs_poc) */
897 ps_mv_buf = (mv_buf_t *)ps_codec->ps_mv_buf;
898 for(j = 0; j < ps_codec->i4_max_dpb_size; j++)
899 {
900 if(ps_mv_buf && ps_mv_buf->i4_abs_poc == ps_pic_buf->i4_abs_poc)
901 {
902 ihevc_buf_mgr_release((buf_mgr_t *)ps_codec->pv_mv_buf_mgr, j, BUF_MGR_REF);
903 break;
904 }
905 ps_mv_buf++;
906 }
907
908 }
909
910 }
911
912 /* Initialize the reference lists to NULL
913 * This is done to take care of the cases where the first pic is not IDR
914 * but the reference list is not created for the first pic because
915 * pic count is zero leaving the reference list uninitialised */
916 for(r_idx = 0; r_idx < MAX_DPB_SIZE; r_idx++)
917 {
918 ps_slice_hdr->as_ref_pic_list0[r_idx].pv_pic_buf = NULL;
919 ps_slice_hdr->as_ref_pic_list0[r_idx].pv_mv_buf = NULL;
920
921 ps_slice_hdr->as_ref_pic_list1[r_idx].pv_pic_buf = NULL;
922 ps_slice_hdr->as_ref_pic_list1[r_idx].pv_mv_buf = NULL;
923 }
924
925 }
926 else
927 {
928 ret = ihevcd_ref_list(ps_codec, ps_pps, ps_sps, ps_slice_hdr);
929 if ((WORD32)IHEVCD_SUCCESS != ret)
930 {
931 return ret;
932 }
933
934 }
935
936 }
937
938 /* Fill the remaining entries of the reference lists with the nearest POC
939 * This is done to handle cases where there is a corruption in the reference index */
940 if(ps_codec->i4_pic_present)
941 {
942 pic_buf_t *ps_pic_buf_ref;
943 mv_buf_t *ps_mv_buf_ref;
944 WORD32 r_idx;
945 dpb_mgr_t *ps_dpb_mgr = (dpb_mgr_t *)ps_codec->pv_dpb_mgr;
946 buf_mgr_t *ps_mv_buf_mgr = (buf_mgr_t *)ps_codec->pv_mv_buf_mgr;
947
948 ps_pic_buf_ref = ihevc_dpb_mgr_get_ref_by_nearest_poc(ps_dpb_mgr, ps_slice_hdr->i4_abs_pic_order_cnt);
949 if(NULL == ps_pic_buf_ref)
950 {
951 ps_pic_buf_ref = ps_codec->as_process[0].ps_cur_pic;
952 ps_mv_buf_ref = ps_codec->s_parse.ps_cur_mv_buf;
953 }
954 else
955 {
956 ps_mv_buf_ref = ihevcd_mv_mgr_get_poc(ps_mv_buf_mgr, ps_pic_buf_ref->i4_abs_poc);
957 }
958
959 for(r_idx = 0; r_idx < ps_slice_hdr->i1_num_ref_idx_l0_active; r_idx++)
960 {
961 if(NULL == ps_slice_hdr->as_ref_pic_list0[r_idx].pv_pic_buf)
962 {
963 ps_slice_hdr->as_ref_pic_list0[r_idx].pv_pic_buf = (void *)ps_pic_buf_ref;
964 ps_slice_hdr->as_ref_pic_list0[r_idx].pv_mv_buf = (void *)ps_mv_buf_ref;
965 }
966 }
967
968 for(r_idx = ps_slice_hdr->i1_num_ref_idx_l0_active; r_idx < MAX_DPB_SIZE; r_idx++)
969 {
970 ps_slice_hdr->as_ref_pic_list0[r_idx].pv_pic_buf = (void *)ps_pic_buf_ref;
971 ps_slice_hdr->as_ref_pic_list0[r_idx].pv_mv_buf = (void *)ps_mv_buf_ref;
972 }
973
974 for(r_idx = 0; r_idx < ps_slice_hdr->i1_num_ref_idx_l1_active; r_idx++)
975 {
976 if(NULL == ps_slice_hdr->as_ref_pic_list1[r_idx].pv_pic_buf)
977 {
978 ps_slice_hdr->as_ref_pic_list1[r_idx].pv_pic_buf = (void *)ps_pic_buf_ref;
979 ps_slice_hdr->as_ref_pic_list1[r_idx].pv_mv_buf = (void *)ps_mv_buf_ref;
980 }
981 }
982
983 for(r_idx = ps_slice_hdr->i1_num_ref_idx_l1_active; r_idx < MAX_DPB_SIZE; r_idx++)
984 {
985 ps_slice_hdr->as_ref_pic_list1[r_idx].pv_pic_buf = (void *)ps_pic_buf_ref;
986 ps_slice_hdr->as_ref_pic_list1[r_idx].pv_mv_buf = (void *)ps_mv_buf_ref;
987 }
988 }
989
990 /* Update slice address in the header */
991 if(!ps_slice_hdr->i1_first_slice_in_pic_flag)
992 {
993 ps_slice_hdr->i2_ctb_x = slice_address % ps_sps->i2_pic_wd_in_ctb;
994 ps_slice_hdr->i2_ctb_y = slice_address / ps_sps->i2_pic_wd_in_ctb;
995
996 if(!ps_slice_hdr->i1_dependent_slice_flag)
997 {
998 ps_slice_hdr->i2_independent_ctb_x = ps_slice_hdr->i2_ctb_x;
999 ps_slice_hdr->i2_independent_ctb_y = ps_slice_hdr->i2_ctb_y;
1000 }
1001 }
1002 else
1003 {
1004 ps_slice_hdr->i2_ctb_x = 0;
1005 ps_slice_hdr->i2_ctb_y = 0;
1006
1007 ps_slice_hdr->i2_independent_ctb_x = 0;
1008 ps_slice_hdr->i2_independent_ctb_y = 0;
1009 }
1010
1011 /* If the first slice in the pic is missing, copy the current slice header to
1012 * the first slice's header */
1013 if((!first_slice_in_pic_flag) &&
1014 (0 == ps_codec->i4_pic_present))
1015 {
1016 slice_header_t *ps_slice_hdr_prev = ps_codec->s_parse.ps_slice_hdr_base;
1017 ihevcd_copy_slice_hdr(ps_codec, 0, (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1)));
1018
1019 ps_codec->i4_slice_error = 1;
1020
1021 ps_slice_hdr_prev->i2_ctb_x = 0;
1022 ps_slice_hdr_prev->i2_ctb_y = 0;
1023
1024 ps_codec->s_parse.i4_ctb_x = 0;
1025 ps_codec->s_parse.i4_ctb_y = 0;
1026
1027 ps_codec->s_parse.i4_cur_slice_idx = 0;
1028
1029 if((ps_slice_hdr->i2_ctb_x == 0) &&
1030 (ps_slice_hdr->i2_ctb_y == 0))
1031 {
1032 ps_slice_hdr->i2_ctb_x++;
1033 }
1034 }
1035
1036 {
1037 /* If skip B is enabled,
1038 * ignore pictures that are non-reference
1039 * TODO: (i1_nal_unit_type < NAL_BLA_W_LP) && (i1_nal_unit_type % 2 == 0) only says it is
1040 * sub-layer non-reference slice. May need to find a way to detect actual non-reference pictures*/
1041
1042 if((i1_nal_unit_type < NAL_BLA_W_LP) &&
1043 (i1_nal_unit_type % 2 == 0))
1044 {
1045 if(IVD_SKIP_B == ps_codec->e_pic_skip_mode)
1046 return IHEVCD_IGNORE_SLICE;
1047 }
1048
1049 /* If skip PB is enabled,
1050 * decode only I slices */
1051 if((IVD_SKIP_PB == ps_codec->e_pic_skip_mode) &&
1052 (ISLICE != ps_slice_hdr->i1_slice_type))
1053 {
1054 return IHEVCD_IGNORE_SLICE;
1055 }
1056 }
1057
1058 return ret;
1059 }
1060