1 /******************************************************************************
2 *
3 * Copyright (C) 2015 The Android Open Source Project
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 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 /*!
21 **************************************************************************
22 * \file ih264d_parse_headers.c
23 *
24 * \brief
25 * Contains High level syntax[above slice] parsing routines
26 *
27 * \date
28 * 19/12/2002
29 *
30 * \author AI
31 **************************************************************************
32 */
33 #include <string.h>
34
35 #include "ih264_typedefs.h"
36 #include "ih264_macros.h"
37 #include "ih264_platform_macros.h"
38 #include "ih264d_bitstrm.h"
39 #include "ih264d_structs.h"
40 #include "ih264d_parse_cavlc.h"
41 #include "ih264d_defs.h"
42 #include "ih264d_defs.h"
43 #include "ih264d_defs.h"
44 #include "ih264d_parse_slice.h"
45 #include "ih264d_tables.h"
46 #include "ih264d_utils.h"
47 #include "ih264d_nal.h"
48 #include "ih264d_deblocking.h"
49
50 #include "ih264d_mem_request.h"
51 #include "ih264d_debug.h"
52 #include "ih264d_error_handler.h"
53 #include "ih264d_mb_utils.h"
54 #include "ih264d_sei.h"
55 #include "ih264d_vui.h"
56 #include "ih264d_thread_parse_decode.h"
57 #include "ih264d_thread_compute_bs.h"
58 #include "ih264d_quant_scaling.h"
59 #include "ih264d_defs.h"
60 #include "ivd.h"
61 #include "ih264d.h"
62
63 /*****************************************************************************/
64 /* */
65 /* Function Name : ih264d_parse_slice_partition */
66 /* */
67 /* Description : This function is intended to parse and decode slice part */
68 /* itions. Currently it's not implemented. Decoder will */
69 /* print a message, skips this NAL and continues */
70 /* Inputs : ps_dec Decoder parameters */
71 /* ps_bitstrm Bitstream */
72 /* Globals : None */
73 /* Processing : This functionality needs to be implemented */
74 /* Outputs : None */
75 /* Returns : None */
76 /* */
77 /* Issues : Not implemented */
78 /* */
79 /* Revision History: */
80 /* */
81 /* DD MM YYYY Author(s) Changes (Describe the changes made) */
82 /* 06 05 2002 NS Draft */
83 /* */
84 /*****************************************************************************/
85
ih264d_parse_slice_partition(dec_struct_t * ps_dec,dec_bit_stream_t * ps_bitstrm)86 WORD32 ih264d_parse_slice_partition(dec_struct_t * ps_dec,
87 dec_bit_stream_t * ps_bitstrm)
88 {
89 H264_DEC_DEBUG_PRINT("\nSlice partition not supported");
90 UNUSED(ps_dec);
91 UNUSED(ps_bitstrm);
92 return (0);
93 }
94
95 /*****************************************************************************/
96 /* */
97 /* Function Name : ih264d_parse_sei */
98 /* */
99 /* Description : This function is intended to parse and decode SEI */
100 /* Currently it's not implemented. Decoder will print a */
101 /* message, skips this NAL and continues */
102 /* Inputs : ps_dec Decoder parameters */
103 /* ps_bitstrm Bitstream */
104 /* Globals : None */
105 /* Processing : This functionality needs to be implemented */
106 /* Outputs : None */
107 /* Returns : None */
108 /* */
109 /* Issues : Not implemented */
110 /* */
111 /* Revision History: */
112 /* */
113 /* DD MM YYYY Author(s) Changes (Describe the changes made) */
114 /* 06 05 2002 NS Draft */
115 /* */
116 /*****************************************************************************/
ih264d_parse_sei(dec_struct_t * ps_dec,dec_bit_stream_t * ps_bitstrm)117 WORD32 ih264d_parse_sei(dec_struct_t * ps_dec, dec_bit_stream_t * ps_bitstrm)
118 {
119 UNUSED(ps_dec);
120 UNUSED(ps_bitstrm);
121 return (0);
122 }
123
124 /*****************************************************************************/
125 /* */
126 /* Function Name : ih264d_parse_filler_data */
127 /* */
128 /* Description : This function is intended to parse and decode filler */
129 /* data NAL. Currently it's not implemented. Decoder will */
130 /* print a message, skips this NAL and continues */
131 /* Inputs : ps_dec Decoder parameters */
132 /* ps_bitstrm Bitstream */
133 /* Globals : None */
134 /* Processing : This functionality needs to be implemented */
135 /* Outputs : None */
136 /* Returns : None */
137 /* */
138 /* Issues : Not implemented */
139 /* */
140 /* Revision History: */
141 /* */
142 /* DD MM YYYY Author(s) Changes (Describe the changes made) */
143 /* 06 05 2002 NS Draft */
144 /* */
145 /*****************************************************************************/
ih264d_parse_filler_data(dec_struct_t * ps_dec,dec_bit_stream_t * ps_bitstrm)146 WORD32 ih264d_parse_filler_data(dec_struct_t * ps_dec,
147 dec_bit_stream_t * ps_bitstrm)
148 {
149 UNUSED(ps_dec);
150 UNUSED(ps_bitstrm);
151 return (0);
152 }
153
154 /*****************************************************************************/
155 /* */
156 /* Function Name : ih264d_parse_end_of_stream */
157 /* */
158 /* Description : This function is intended to parse and decode end of */
159 /* sequence. Currently it's not implemented. Decoder will */
160 /* print a message, skips this NAL and continues */
161 /* Inputs : ps_dec Decoder parameters */
162 /* Globals : None */
163 /* Processing : This functionality needs to be implemented */
164 /* Outputs : None */
165 /* Returns : None */
166 /* */
167 /* Issues : Not implemented */
168 /* */
169 /* Revision History: */
170 /* */
171 /* DD MM YYYY Author(s) Changes (Describe the changes made) */
172 /* 06 05 2002 NS Draft */
173 /* */
174 /*****************************************************************************/
ih264d_parse_end_of_stream(dec_struct_t * ps_dec)175 void ih264d_parse_end_of_stream(dec_struct_t * ps_dec)
176 {
177 UNUSED(ps_dec);
178 return;
179 }
180
181 /*!
182 **************************************************************************
183 * \if Function name : ih264d_parse_pps \endif
184 *
185 * \brief
186 * Decodes Picture Parameter set
187 *
188 * \return
189 * 0 on Success and Error code otherwise
190 **************************************************************************
191 */
ih264d_parse_pps(dec_struct_t * ps_dec,dec_bit_stream_t * ps_bitstrm)192 WORD32 ih264d_parse_pps(dec_struct_t * ps_dec, dec_bit_stream_t * ps_bitstrm)
193 {
194 UWORD8 uc_temp;
195 dec_seq_params_t * ps_sps = NULL;
196 dec_pic_params_t * ps_pps = NULL;
197 UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer;
198 UWORD32 *pu4_bitstrm_ofst = &ps_dec->ps_bitstrm->u4_ofst;
199
200 /* Variables used for error resilience checks */
201 UWORD32 u4_temp;
202 WORD32 i_temp;
203
204 /* For High profile related syntax elements */
205 UWORD8 u1_more_data_flag;
206 WORD32 i4_i;
207
208 if(!(ps_dec->i4_header_decoded & 1))
209 return ERROR_INV_SPS_PPS_T;
210
211 /*--------------------------------------------------------------------*/
212 /* Decode pic_parameter_set_id and find corresponding pic params */
213 /*--------------------------------------------------------------------*/
214 u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
215 if(u4_temp & MASK_ERR_PIC_SET_ID)
216 return ERROR_INV_SPS_PPS_T;
217 ps_pps = ps_dec->pv_scratch_sps_pps;
218 *ps_pps = ps_dec->ps_pps[u4_temp];
219 ps_pps->u1_pic_parameter_set_id = (WORD8)u4_temp;
220 COPYTHECONTEXT("PPS: pic_parameter_set_id",ps_pps->u1_pic_parameter_set_id);
221
222 /************************************************/
223 /* initilization of High profile syntax element */
224 /************************************************/
225 ps_pps->i4_transform_8x8_mode_flag = 0;
226 ps_pps->i4_pic_scaling_matrix_present_flag = 0;
227
228 /*--------------------------------------------------------------------*/
229 /* Decode seq_parameter_set_id and map it to a seq_parameter_set */
230 /*--------------------------------------------------------------------*/
231 u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
232 if(u4_temp & MASK_ERR_SEQ_SET_ID)
233 return ERROR_INV_SPS_PPS_T;
234 COPYTHECONTEXT("PPS: seq_parameter_set_id",u4_temp);
235 ps_sps = &ps_dec->ps_sps[u4_temp];
236
237 if(FALSE == ps_sps->u1_is_valid)
238 return ERROR_INV_SPS_PPS_T;
239 ps_pps->ps_sps = ps_sps;
240
241 /*--------------------------------------------------------------------*/
242 /* Decode entropy_coding_mode */
243 /*--------------------------------------------------------------------*/
244 ps_pps->u1_entropy_coding_mode = ih264d_get_bit_h264(ps_bitstrm);
245 COPYTHECONTEXT("PPS: entropy_coding_mode_flag",ps_pps->u1_entropy_coding_mode);
246
247 ps_pps->u1_pic_order_present_flag = ih264d_get_bit_h264(ps_bitstrm);
248 COPYTHECONTEXT("PPS: pic_order_present_flag",ps_pps->u1_pic_order_present_flag);
249
250 /*--------------------------------------------------------------------*/
251 /* Decode num_slice_groups_minus1 */
252 /*--------------------------------------------------------------------*/
253 u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf) + 1;
254 if(u4_temp != 1)
255 {
256 UWORD32 i4_error_code;
257 i4_error_code = ERROR_FEATURE_UNAVAIL;
258 return i4_error_code;
259 }
260 ps_pps->u1_num_slice_groups = u4_temp;
261 COPYTHECONTEXT("PPS: num_slice_groups_minus1",ps_pps->u1_num_slice_groups -1);
262
263 /*--------------------------------------------------------------------*/
264 /* Other parameter set values */
265 /*--------------------------------------------------------------------*/
266 u4_temp = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
267 if(u4_temp > H264_MAX_REF_IDX)
268 return ERROR_REF_IDX;
269 ps_pps->u1_num_ref_idx_lx_active[0] = u4_temp;
270 COPYTHECONTEXT("PPS: num_ref_idx_l0_active_minus1",
271 ps_pps->u1_num_ref_idx_lx_active[0] - 1);
272
273 u4_temp = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
274 if(u4_temp > H264_MAX_REF_IDX)
275 return ERROR_REF_IDX;
276 ps_pps->u1_num_ref_idx_lx_active[1] = u4_temp;
277 COPYTHECONTEXT("PPS: num_ref_idx_l1_active_minus1",
278 ps_pps->u1_num_ref_idx_lx_active[1] - 1);
279
280 ps_pps->u1_wted_pred_flag = ih264d_get_bit_h264(ps_bitstrm);
281 COPYTHECONTEXT("PPS: weighted prediction u4_flag",ps_pps->u1_wted_pred_flag);
282 uc_temp = ih264d_get_bits_h264(ps_bitstrm, 2);
283 COPYTHECONTEXT("PPS: weighted_bipred_idc",uc_temp);
284 ps_pps->u1_wted_bipred_idc = uc_temp;
285
286 if(ps_pps->u1_wted_bipred_idc > MAX_WEIGHT_BIPRED_IDC)
287 return ERROR_INV_SPS_PPS_T;
288
289 i_temp = 26 + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
290
291 if((i_temp < 0) || (i_temp > 51))
292 return ERROR_INV_RANGE_QP_T;
293
294 ps_pps->u1_pic_init_qp = i_temp;
295 COPYTHECONTEXT("PPS: pic_init_qp_minus26",ps_pps->u1_pic_init_qp - 26);
296
297 i_temp = 26 + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
298
299 if((i_temp < 0) || (i_temp > 51))
300 return ERROR_INV_RANGE_QP_T;
301
302 ps_pps->u1_pic_init_qs = i_temp;
303 COPYTHECONTEXT("PPS: pic_init_qs_minus26",ps_pps->u1_pic_init_qs - 26);
304
305 i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
306 if((i_temp < -12) || (i_temp > 12))
307 return ERROR_INV_RANGE_QP_T;
308 ps_pps->i1_chroma_qp_index_offset = i_temp;
309 COPYTHECONTEXT("PPS: chroma_qp_index_offset",ps_pps->i1_chroma_qp_index_offset);
310
311 /***************************************************************************/
312 /* initialize second_chroma_qp_index_offset to i1_chroma_qp_index_offset if */
313 /* second_chroma_qp_index_offset is not present in bit-ps_bitstrm */
314 /***************************************************************************/
315 ps_pps->i1_second_chroma_qp_index_offset =
316 ps_pps->i1_chroma_qp_index_offset;
317
318 ps_pps->u1_deblocking_filter_parameters_present_flag = ih264d_get_bit_h264(
319 ps_bitstrm);
320 COPYTHECONTEXT("PPS: deblocking_filter_control_present_flag",
321 ps_pps->u1_deblocking_filter_parameters_present_flag);
322 ps_pps->u1_constrained_intra_pred_flag = ih264d_get_bit_h264(ps_bitstrm);
323 COPYTHECONTEXT("PPS: constrained_intra_pred_flag",
324 ps_pps->u1_constrained_intra_pred_flag);
325 ps_pps->u1_redundant_pic_cnt_present_flag = ih264d_get_bit_h264(ps_bitstrm);
326 COPYTHECONTEXT("PPS: redundant_pic_cnt_present_flag",
327 ps_pps->u1_redundant_pic_cnt_present_flag);
328
329 /* High profile related syntax elements */
330 u1_more_data_flag = MORE_RBSP_DATA(ps_bitstrm);
331 if(u1_more_data_flag && (ps_pps->ps_sps->u1_profile_idc == HIGH_PROFILE_IDC))
332 {
333 /* read transform_8x8_mode_flag */
334 ps_pps->i4_transform_8x8_mode_flag = (WORD32)ih264d_get_bit_h264(
335 ps_bitstrm);
336
337 /* read pic_scaling_matrix_present_flag */
338 ps_pps->i4_pic_scaling_matrix_present_flag =
339 (WORD32)ih264d_get_bit_h264(ps_bitstrm);
340
341 if(ps_pps->i4_pic_scaling_matrix_present_flag)
342 {
343 /* read the scaling matrices */
344 for(i4_i = 0; i4_i < (6 + (ps_pps->i4_transform_8x8_mode_flag << 1)); i4_i++)
345 {
346 ps_pps->u1_pic_scaling_list_present_flag[i4_i] =
347 ih264d_get_bit_h264(ps_bitstrm);
348
349 if(ps_pps->u1_pic_scaling_list_present_flag[i4_i])
350 {
351 if(i4_i < 6)
352 {
353 ih264d_scaling_list(
354 ps_pps->i2_pic_scalinglist4x4[i4_i],
355 16,
356 &ps_pps->u1_pic_use_default_scaling_matrix_flag[i4_i],
357 ps_bitstrm);
358 }
359 else
360 {
361 ih264d_scaling_list(
362 ps_pps->i2_pic_scalinglist8x8[i4_i - 6],
363 64,
364 &ps_pps->u1_pic_use_default_scaling_matrix_flag[i4_i],
365 ps_bitstrm);
366 }
367 }
368 }
369 }
370
371 /* read second_chroma_qp_index_offset syntax element */
372 i_temp = ih264d_sev(
373 pu4_bitstrm_ofst, pu4_bitstrm_buf);
374
375 if((i_temp < -12) || (i_temp > 12))
376 return ERROR_INV_RANGE_QP_T;
377
378 ps_pps->i1_second_chroma_qp_index_offset = i_temp;
379 }
380
381 /* In case bitstream read has exceeded the filled size, then
382 return an error */
383 if(ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst + 8)
384 {
385 return ERROR_INV_SPS_PPS_T;
386 }
387 ps_pps->u1_is_valid = TRUE;
388 ps_dec->ps_pps[ps_pps->u1_pic_parameter_set_id] = *ps_pps;
389 return OK;
390 }
391
392 /*!
393 **************************************************************************
394 * \if Function name : ih264d_parse_sps \endif
395 *
396 * \brief
397 * Decodes Sequence parameter set from the bitstream
398 *
399 * \return
400 * 0 on Success and Error code otherwise
401 **************************************************************************
402 */
ih264d_correct_level_idc(UWORD32 u4_level_idc,UWORD32 u4_total_mbs)403 UWORD32 ih264d_correct_level_idc(UWORD32 u4_level_idc, UWORD32 u4_total_mbs)
404 {
405 UWORD32 u4_max_mbs_allowed;
406
407 switch(u4_level_idc)
408 {
409 case H264_LEVEL_1_0:
410 u4_max_mbs_allowed = MAX_MBS_LEVEL_10;
411 break;
412 case H264_LEVEL_1_1:
413 u4_max_mbs_allowed = MAX_MBS_LEVEL_11;
414 break;
415 case H264_LEVEL_1_2:
416 u4_max_mbs_allowed = MAX_MBS_LEVEL_12;
417 break;
418 case H264_LEVEL_1_3:
419 u4_max_mbs_allowed = MAX_MBS_LEVEL_13;
420 break;
421 case H264_LEVEL_2_0:
422 u4_max_mbs_allowed = MAX_MBS_LEVEL_20;
423 break;
424 case H264_LEVEL_2_1:
425 u4_max_mbs_allowed = MAX_MBS_LEVEL_21;
426 break;
427 case H264_LEVEL_2_2:
428 u4_max_mbs_allowed = MAX_MBS_LEVEL_22;
429 break;
430 case H264_LEVEL_3_0:
431 u4_max_mbs_allowed = MAX_MBS_LEVEL_30;
432 break;
433 case H264_LEVEL_3_1:
434 u4_max_mbs_allowed = MAX_MBS_LEVEL_31;
435 break;
436 case H264_LEVEL_3_2:
437 u4_max_mbs_allowed = MAX_MBS_LEVEL_32;
438 break;
439 case H264_LEVEL_4_0:
440 u4_max_mbs_allowed = MAX_MBS_LEVEL_40;
441 break;
442 case H264_LEVEL_4_1:
443 u4_max_mbs_allowed = MAX_MBS_LEVEL_41;
444 break;
445 case H264_LEVEL_4_2:
446 u4_max_mbs_allowed = MAX_MBS_LEVEL_42;
447 break;
448 case H264_LEVEL_5_0:
449 u4_max_mbs_allowed = MAX_MBS_LEVEL_50;
450 break;
451 case H264_LEVEL_5_1:
452 default:
453 u4_max_mbs_allowed = MAX_MBS_LEVEL_51;
454 break;
455
456 }
457
458 /*correct of the level is incorrect*/
459 if(u4_total_mbs > u4_max_mbs_allowed)
460 {
461 if(u4_total_mbs > MAX_MBS_LEVEL_50)
462 u4_level_idc = H264_LEVEL_5_1;
463 else if(u4_total_mbs > MAX_MBS_LEVEL_42)
464 u4_level_idc = H264_LEVEL_5_0;
465 else if(u4_total_mbs > MAX_MBS_LEVEL_41)
466 u4_level_idc = H264_LEVEL_4_2;
467 else if(u4_total_mbs > MAX_MBS_LEVEL_40)
468 u4_level_idc = H264_LEVEL_4_1;
469 else if(u4_total_mbs > MAX_MBS_LEVEL_32)
470 u4_level_idc = H264_LEVEL_4_0;
471 else if(u4_total_mbs > MAX_MBS_LEVEL_31)
472 u4_level_idc = H264_LEVEL_3_2;
473 else if(u4_total_mbs > MAX_MBS_LEVEL_30)
474 u4_level_idc = H264_LEVEL_3_1;
475 else if(u4_total_mbs > MAX_MBS_LEVEL_21)
476 u4_level_idc = H264_LEVEL_3_0;
477 else if(u4_total_mbs > MAX_MBS_LEVEL_20)
478 u4_level_idc = H264_LEVEL_2_1;
479 else if(u4_total_mbs > MAX_MBS_LEVEL_10)
480 u4_level_idc = H264_LEVEL_2_0;
481 }
482
483 return (u4_level_idc);
484
485 }
ih264d_parse_sps(dec_struct_t * ps_dec,dec_bit_stream_t * ps_bitstrm)486 WORD32 ih264d_parse_sps(dec_struct_t *ps_dec, dec_bit_stream_t *ps_bitstrm)
487 {
488 UWORD8 i;
489 dec_seq_params_t *ps_seq = NULL;
490 UWORD8 u1_profile_idc, u1_level_idc, u1_seq_parameter_set_id, u1_mb_aff_flag = 0;
491 UWORD16 i2_max_frm_num;
492 UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
493 UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
494 UWORD8 u1_frm, uc_constraint_set0_flag, uc_constraint_set1_flag;
495 WORD32 i4_cropped_ht, i4_cropped_wd;
496 UWORD32 u4_temp;
497 UWORD32 u4_pic_height_in_map_units, u4_pic_width_in_mbs;
498 UWORD32 u2_pic_wd = 0;
499 UWORD32 u2_pic_ht = 0;
500 UWORD32 u2_frm_wd_y = 0;
501 UWORD32 u2_frm_ht_y = 0;
502 UWORD32 u2_frm_wd_uv = 0;
503 UWORD32 u2_frm_ht_uv = 0;
504 UWORD32 u2_crop_offset_y = 0;
505 UWORD32 u2_crop_offset_uv = 0;
506 WORD32 ret;
507 WORD32 num_reorder_frames;
508 /* High profile related syntax element */
509 WORD32 i4_i;
510 /* G050 */
511 UWORD8 u1_frame_cropping_flag, u1_frame_cropping_rect_left_ofst,
512 u1_frame_cropping_rect_right_ofst,
513 u1_frame_cropping_rect_top_ofst,
514 u1_frame_cropping_rect_bottom_ofst;
515 /* G050 */
516 /*--------------------------------------------------------------------*/
517 /* Decode seq_parameter_set_id and profile and level values */
518 /*--------------------------------------------------------------------*/
519 SWITCHONTRACE;
520 u1_profile_idc = ih264d_get_bits_h264(ps_bitstrm, 8);
521 COPYTHECONTEXT("SPS: profile_idc",u1_profile_idc);
522
523 /* G050 */
524 uc_constraint_set0_flag = ih264d_get_bit_h264(ps_bitstrm);
525 uc_constraint_set1_flag = ih264d_get_bit_h264(ps_bitstrm);
526 ih264d_get_bit_h264(ps_bitstrm);
527
528 /*****************************************************/
529 /* Read 5 bits for uc_constraint_set3_flag (1 bit) */
530 /* and reserved_zero_4bits (4 bits) - Sushant */
531 /*****************************************************/
532 ih264d_get_bits_h264(ps_bitstrm, 5);
533 /* G050 */
534
535 /* Check whether particular profile is suported or not */
536 /* Check whether particular profile is suported or not */
537 if((u1_profile_idc != MAIN_PROFILE_IDC) &&
538
539 (u1_profile_idc != BASE_PROFILE_IDC) &&
540
541 (u1_profile_idc != HIGH_PROFILE_IDC)
542
543 )
544 {
545
546 /* Apart from Baseline, main and high profile,
547 * only extended profile is supported provided
548 * uc_constraint_set0_flag or uc_constraint_set1_flag are set to 1
549 */
550 if((u1_profile_idc != EXTENDED_PROFILE_IDC) ||
551 ((uc_constraint_set1_flag != 1) && (uc_constraint_set0_flag != 1)))
552 {
553 return (ERROR_FEATURE_UNAVAIL);
554 }
555 }
556
557 u1_level_idc = ih264d_get_bits_h264(ps_bitstrm, 8);
558
559
560
561 COPYTHECONTEXT("SPS: u4_level_idc",u1_level_idc);
562
563 u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
564 if(u4_temp & MASK_ERR_SEQ_SET_ID)
565 return ERROR_INV_SPS_PPS_T;
566 u1_seq_parameter_set_id = u4_temp;
567 COPYTHECONTEXT("SPS: seq_parameter_set_id",
568 u1_seq_parameter_set_id);
569
570 /*--------------------------------------------------------------------*/
571 /* Find an seq param entry in seqparam array of decStruct */
572 /*--------------------------------------------------------------------*/
573
574 ps_seq = ps_dec->pv_scratch_sps_pps;
575 memset(ps_seq, 0, sizeof(dec_seq_params_t));
576
577 if(ps_dec->i4_header_decoded & 1)
578 {
579 *ps_seq = *ps_dec->ps_cur_sps;
580 }
581
582
583 if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_profile_idc != u1_profile_idc))
584 {
585 ps_dec->u1_res_changed = 1;
586 return IVD_RES_CHANGED;
587 }
588
589 if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_level_idc != u1_level_idc))
590 {
591 ps_dec->u1_res_changed = 1;
592 return IVD_RES_CHANGED;
593 }
594
595 ps_seq->u1_profile_idc = u1_profile_idc;
596 ps_seq->u1_level_idc = u1_level_idc;
597 ps_seq->u1_seq_parameter_set_id = u1_seq_parameter_set_id;
598
599 /*******************************************************************/
600 /* Initializations for high profile - Sushant */
601 /*******************************************************************/
602 ps_seq->i4_chroma_format_idc = 1;
603 ps_seq->i4_bit_depth_luma_minus8 = 0;
604 ps_seq->i4_bit_depth_chroma_minus8 = 0;
605 ps_seq->i4_qpprime_y_zero_transform_bypass_flag = 0;
606 ps_seq->i4_seq_scaling_matrix_present_flag = 0;
607 if(u1_profile_idc == HIGH_PROFILE_IDC)
608 {
609
610 /* reading chroma_format_idc */
611 ps_seq->i4_chroma_format_idc = ih264d_uev(pu4_bitstrm_ofst,
612 pu4_bitstrm_buf);
613
614 /* Monochrome is not supported */
615 if(ps_seq->i4_chroma_format_idc != 1)
616 {
617 return ERROR_FEATURE_UNAVAIL;
618 }
619
620 /* reading bit_depth_luma_minus8 */
621 ps_seq->i4_bit_depth_luma_minus8 = ih264d_uev(pu4_bitstrm_ofst,
622 pu4_bitstrm_buf);
623
624 if(ps_seq->i4_bit_depth_luma_minus8 != 0)
625 {
626 return ERROR_FEATURE_UNAVAIL;
627 }
628
629 /* reading bit_depth_chroma_minus8 */
630 ps_seq->i4_bit_depth_chroma_minus8 = ih264d_uev(pu4_bitstrm_ofst,
631 pu4_bitstrm_buf);
632
633 if(ps_seq->i4_bit_depth_chroma_minus8 != 0)
634 {
635 return ERROR_FEATURE_UNAVAIL;
636 }
637
638 /* reading qpprime_y_zero_transform_bypass_flag */
639 ps_seq->i4_qpprime_y_zero_transform_bypass_flag =
640 (WORD32)ih264d_get_bit_h264(ps_bitstrm);
641
642 if(ps_seq->i4_qpprime_y_zero_transform_bypass_flag != 0)
643 {
644 return ERROR_INV_SPS_PPS_T;
645 }
646
647 /* reading seq_scaling_matrix_present_flag */
648 ps_seq->i4_seq_scaling_matrix_present_flag =
649 (WORD32)ih264d_get_bit_h264(ps_bitstrm);
650
651 if(ps_seq->i4_seq_scaling_matrix_present_flag)
652 {
653 for(i4_i = 0; i4_i < 8; i4_i++)
654 {
655 ps_seq->u1_seq_scaling_list_present_flag[i4_i] =
656 ih264d_get_bit_h264(ps_bitstrm);
657
658 /* initialize u1_use_default_scaling_matrix_flag[i4_i] to zero */
659 /* before calling scaling list */
660 ps_seq->u1_use_default_scaling_matrix_flag[i4_i] = 0;
661
662 if(ps_seq->u1_seq_scaling_list_present_flag[i4_i])
663 {
664 if(i4_i < 6)
665 {
666 ih264d_scaling_list(
667 ps_seq->i2_scalinglist4x4[i4_i],
668 16,
669 &ps_seq->u1_use_default_scaling_matrix_flag[i4_i],
670 ps_bitstrm);
671 }
672 else
673 {
674 ih264d_scaling_list(
675 ps_seq->i2_scalinglist8x8[i4_i - 6],
676 64,
677 &ps_seq->u1_use_default_scaling_matrix_flag[i4_i],
678 ps_bitstrm);
679 }
680 }
681 }
682 }
683 }
684 /*--------------------------------------------------------------------*/
685 /* Decode MaxFrameNum */
686 /*--------------------------------------------------------------------*/
687 u4_temp = 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
688 if(u4_temp > MAX_BITS_IN_FRAME_NUM)
689 {
690 return ERROR_INV_SPS_PPS_T;
691 }
692 ps_seq->u1_bits_in_frm_num = u4_temp;
693 COPYTHECONTEXT("SPS: log2_max_frame_num_minus4",
694 (ps_seq->u1_bits_in_frm_num - 4));
695
696 i2_max_frm_num = (1 << (ps_seq->u1_bits_in_frm_num));
697 ps_seq->u2_u4_max_pic_num_minus1 = i2_max_frm_num - 1;
698 /*--------------------------------------------------------------------*/
699 /* Decode picture order count and related values */
700 /*--------------------------------------------------------------------*/
701 u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
702
703 if(u4_temp > MAX_PIC_ORDER_CNT_TYPE)
704 {
705 return ERROR_INV_POC_TYPE_T;
706 }
707 ps_seq->u1_pic_order_cnt_type = u4_temp;
708 COPYTHECONTEXT("SPS: pic_order_cnt_type",ps_seq->u1_pic_order_cnt_type);
709
710 ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle = 1;
711 if(ps_seq->u1_pic_order_cnt_type == 0)
712 {
713 u4_temp = 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
714 if(u4_temp > MAX_BITS_IN_POC_LSB)
715 {
716 return ERROR_INV_SPS_PPS_T;
717 }
718 ps_seq->u1_log2_max_pic_order_cnt_lsb_minus = u4_temp;
719 ps_seq->i4_max_pic_order_cntLsb = (1 << u4_temp);
720 COPYTHECONTEXT("SPS: log2_max_pic_order_cnt_lsb_minus4",(u4_temp - 4));
721 }
722 else if(ps_seq->u1_pic_order_cnt_type == 1)
723 {
724 ps_seq->u1_delta_pic_order_always_zero_flag = ih264d_get_bit_h264(
725 ps_bitstrm);
726 COPYTHECONTEXT("SPS: delta_pic_order_always_zero_flag",
727 ps_seq->u1_delta_pic_order_always_zero_flag);
728
729 ps_seq->i4_ofst_for_non_ref_pic = ih264d_sev(pu4_bitstrm_ofst,
730 pu4_bitstrm_buf);
731 COPYTHECONTEXT("SPS: offset_for_non_ref_pic",
732 ps_seq->i4_ofst_for_non_ref_pic);
733
734 ps_seq->i4_ofst_for_top_to_bottom_field = ih264d_sev(
735 pu4_bitstrm_ofst, pu4_bitstrm_buf);
736 COPYTHECONTEXT("SPS: offset_for_top_to_bottom_field",
737 ps_seq->i4_ofst_for_top_to_bottom_field);
738
739 u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
740 if(u4_temp > 255)
741 return ERROR_INV_SPS_PPS_T;
742 ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle = u4_temp;
743 COPYTHECONTEXT("SPS: num_ref_frames_in_pic_order_cnt_cycle",
744 ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle);
745
746 for(i = 0; i < ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle; i++)
747 {
748 ps_seq->i4_ofst_for_ref_frame[i] = ih264d_sev(
749 pu4_bitstrm_ofst, pu4_bitstrm_buf);
750 COPYTHECONTEXT("SPS: offset_for_ref_frame",
751 ps_seq->i4_ofst_for_ref_frame[i]);
752 }
753 }
754
755 u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
756
757 if((u4_temp > H264_MAX_REF_PICS))
758 {
759 return ERROR_NUM_REF;
760 }
761
762 /* Compare with older num_ref_frames is header is already once */
763 if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_num_ref_frames != u4_temp))
764 {
765 ps_dec->u1_res_changed = 1;
766 return IVD_RES_CHANGED;
767 }
768
769 ps_seq->u1_num_ref_frames = u4_temp;
770 COPYTHECONTEXT("SPS: num_ref_frames",ps_seq->u1_num_ref_frames);
771
772 ps_seq->u1_gaps_in_frame_num_value_allowed_flag = ih264d_get_bit_h264(
773 ps_bitstrm);
774 COPYTHECONTEXT("SPS: gaps_in_frame_num_value_allowed_flag",
775 ps_seq->u1_gaps_in_frame_num_value_allowed_flag);
776
777 /*--------------------------------------------------------------------*/
778 /* Decode FrameWidth and FrameHeight and related values */
779 /*--------------------------------------------------------------------*/
780 u4_pic_width_in_mbs = 1
781 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
782 COPYTHECONTEXT("SPS: pic_width_in_mbs_minus1",
783 u4_pic_width_in_mbs - 1);
784
785 u4_pic_height_in_map_units = 1 + ih264d_uev(pu4_bitstrm_ofst,
786 pu4_bitstrm_buf);
787
788 /* Check for unsupported resolutions*/
789 if((u4_pic_width_in_mbs > (H264_MAX_FRAME_WIDTH >> 4)) ||
790 (u4_pic_height_in_map_units > (H264_MAX_FRAME_HEIGHT >> 4)))
791 {
792 return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
793 }
794 ps_seq->u2_frm_wd_in_mbs = u4_pic_width_in_mbs;
795 ps_seq->u2_frm_ht_in_mbs = u4_pic_height_in_map_units;
796
797 u2_pic_wd = (u4_pic_width_in_mbs << 4);
798 u2_pic_ht = (u4_pic_height_in_map_units << 4);
799 /*--------------------------------------------------------------------*/
800 /* Get the value of MaxMbAddress and Number of bits needed for it */
801 /*--------------------------------------------------------------------*/
802 ps_seq->u2_max_mb_addr = (ps_seq->u2_frm_wd_in_mbs
803 * ps_seq->u2_frm_ht_in_mbs) - 1;
804
805 ps_seq->u2_total_num_of_mbs = ps_seq->u2_max_mb_addr + 1;
806
807 ps_seq->u1_level_idc = ih264d_correct_level_idc(
808 u1_level_idc, ps_seq->u2_total_num_of_mbs);
809
810 u1_frm = ih264d_get_bit_h264(ps_bitstrm);
811 if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_frame_mbs_only_flag != u1_frm))
812 {
813 ps_dec->u1_res_changed = 1;
814 return IVD_RES_CHANGED;
815 }
816
817 ps_seq->u1_frame_mbs_only_flag = u1_frm;
818
819 COPYTHECONTEXT("SPS: frame_mbs_only_flag", u1_frm);
820
821 if(!u1_frm)
822 u1_mb_aff_flag = ih264d_get_bit_h264(ps_bitstrm);
823
824 if((ps_dec->i4_header_decoded & 1)
825 && (ps_seq->u1_mb_aff_flag != u1_mb_aff_flag))
826 {
827 ps_dec->u1_res_changed = 1;
828 return IVD_RES_CHANGED;
829 }
830
831 if(!u1_frm)
832 {
833 u2_pic_ht <<= 1;
834 ps_seq->u1_mb_aff_flag = u1_mb_aff_flag;
835 COPYTHECONTEXT("SPS: mb_adaptive_frame_field_flag",
836 ps_seq->u1_mb_aff_flag);
837
838 }
839 else
840 ps_seq->u1_mb_aff_flag = 0;
841
842 ps_seq->u1_direct_8x8_inference_flag = ih264d_get_bit_h264(ps_bitstrm);
843
844 COPYTHECONTEXT("SPS: direct_8x8_inference_flag",
845 ps_seq->u1_direct_8x8_inference_flag);
846
847 /* G050 */
848 u1_frame_cropping_flag = ih264d_get_bit_h264(ps_bitstrm);
849 COPYTHECONTEXT("SPS: frame_cropping_flag",u1_frame_cropping_flag);
850
851 if(u1_frame_cropping_flag)
852 {
853 u1_frame_cropping_rect_left_ofst = ih264d_uev(pu4_bitstrm_ofst,
854 pu4_bitstrm_buf);
855 COPYTHECONTEXT("SPS: frame_cropping_rect_left_offset",
856 u1_frame_cropping_rect_left_ofst);
857 u1_frame_cropping_rect_right_ofst = ih264d_uev(pu4_bitstrm_ofst,
858 pu4_bitstrm_buf);
859 COPYTHECONTEXT("SPS: frame_cropping_rect_right_offset",
860 u1_frame_cropping_rect_right_ofst);
861 u1_frame_cropping_rect_top_ofst = ih264d_uev(pu4_bitstrm_ofst,
862 pu4_bitstrm_buf);
863 COPYTHECONTEXT("SPS: frame_cropping_rect_top_offset",
864 u1_frame_cropping_rect_top_ofst);
865 u1_frame_cropping_rect_bottom_ofst = ih264d_uev(pu4_bitstrm_ofst,
866 pu4_bitstrm_buf);
867 COPYTHECONTEXT("SPS: frame_cropping_rect_bottom_offset",
868 u1_frame_cropping_rect_bottom_ofst);
869 }
870 /* G050 */
871
872 ps_seq->u1_vui_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm);
873 COPYTHECONTEXT("SPS: vui_parameters_present_flag",
874 ps_seq->u1_vui_parameters_present_flag);
875
876 u2_frm_wd_y = u2_pic_wd + (UWORD8)(PAD_LEN_Y_H << 1);
877 if(1 == ps_dec->u4_share_disp_buf)
878 {
879 if(ps_dec->u4_app_disp_width > u2_frm_wd_y)
880 u2_frm_wd_y = ps_dec->u4_app_disp_width;
881 }
882
883 u2_frm_ht_y = u2_pic_ht + (UWORD8)(PAD_LEN_Y_V << 2);
884 u2_frm_wd_uv = u2_pic_wd + (UWORD8)(PAD_LEN_UV_H << 2);
885 u2_frm_wd_uv = MAX(u2_frm_wd_uv, u2_frm_wd_y);
886
887 u2_frm_ht_uv = (u2_pic_ht >> 1) + (UWORD8)(PAD_LEN_UV_V << 2);
888 u2_frm_ht_uv = MAX(u2_frm_ht_uv, (u2_frm_ht_y >> 1));
889
890
891 /* Calculate display picture width, height and start u4_ofst from YUV420 */
892 /* pictute buffers as per cropping information parsed above */
893 {
894 UWORD16 u2_rgt_ofst = 0;
895 UWORD16 u2_lft_ofst = 0;
896 UWORD16 u2_top_ofst = 0;
897 UWORD16 u2_btm_ofst = 0;
898 UWORD8 u1_frm_mbs_flag;
899 UWORD8 u1_vert_mult_factor;
900
901 if(u1_frame_cropping_flag)
902 {
903 /* Calculate right and left u4_ofst for cropped picture */
904 u2_rgt_ofst = u1_frame_cropping_rect_right_ofst << 1;
905 u2_lft_ofst = u1_frame_cropping_rect_left_ofst << 1;
906
907 /* Know frame MBs only u4_flag */
908 u1_frm_mbs_flag = (1 == ps_seq->u1_frame_mbs_only_flag);
909
910 /* Simplify the vertical u4_ofst calculation from field/frame */
911 u1_vert_mult_factor = (2 - u1_frm_mbs_flag);
912
913 /* Calculate bottom and top u4_ofst for cropped picture */
914 u2_btm_ofst = (u1_frame_cropping_rect_bottom_ofst
915 << u1_vert_mult_factor);
916 u2_top_ofst = (u1_frame_cropping_rect_top_ofst
917 << u1_vert_mult_factor);
918 }
919
920 /* Calculate u4_ofst from start of YUV 420 picture buffer to start of*/
921 /* cropped picture buffer */
922 u2_crop_offset_y = (u2_frm_wd_y * u2_top_ofst) + (u2_lft_ofst);
923 u2_crop_offset_uv = (u2_frm_wd_uv * (u2_top_ofst >> 1))
924 + (u2_lft_ofst >> 1) * YUV420SP_FACTOR;
925 /* Calculate the display picture width and height based on crop */
926 /* information */
927 i4_cropped_ht = (WORD32)u2_pic_ht - (WORD32)(u2_btm_ofst + u2_top_ofst);
928 i4_cropped_wd = (WORD32)u2_pic_wd - (WORD32)(u2_rgt_ofst + u2_lft_ofst);
929
930 if((i4_cropped_ht < MB_SIZE) || (i4_cropped_wd < MB_SIZE))
931 {
932 return ERROR_INV_SPS_PPS_T;
933 }
934
935 if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_pic_wd != u2_pic_wd))
936 {
937 ps_dec->u1_res_changed = 1;
938 return IVD_RES_CHANGED;
939 }
940
941 if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_disp_width != i4_cropped_wd))
942 {
943 ps_dec->u1_res_changed = 1;
944 return IVD_RES_CHANGED;
945 }
946
947 if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_pic_ht != u2_pic_ht))
948 {
949 ps_dec->u1_res_changed = 1;
950 return IVD_RES_CHANGED;
951 }
952
953 if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_disp_height != i4_cropped_ht))
954 {
955 ps_dec->u1_res_changed = 1;
956 return IVD_RES_CHANGED;
957 }
958
959 /* Check again for unsupported resolutions with updated values*/
960 if((u2_pic_wd > H264_MAX_FRAME_WIDTH) || (u2_pic_ht > H264_MAX_FRAME_HEIGHT)
961 || (u2_pic_wd < H264_MIN_FRAME_WIDTH) || (u2_pic_ht < H264_MIN_FRAME_HEIGHT)
962 || (u2_pic_wd * (UWORD32)u2_pic_ht > H264_MAX_FRAME_SIZE))
963 {
964 return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
965 }
966
967 /* If MBAff is enabled, decoder support is limited to streams with
968 * width less than half of H264_MAX_FRAME_WIDTH.
969 * In case of MBAff decoder processes two rows at a time
970 */
971 if((u2_pic_wd << ps_seq->u1_mb_aff_flag) > H264_MAX_FRAME_WIDTH)
972 {
973 return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
974 }
975
976 }
977
978 /* Backup num_reorder_frames if header is already decoded */
979 if((ps_dec->i4_header_decoded & 1) &&
980 (1 == ps_seq->u1_vui_parameters_present_flag) &&
981 (1 == ps_seq->s_vui.u1_bitstream_restriction_flag))
982 {
983 num_reorder_frames = (WORD32)ps_seq->s_vui.u4_num_reorder_frames;
984 }
985 else
986 {
987 num_reorder_frames = -1;
988 }
989 if(1 == ps_seq->u1_vui_parameters_present_flag)
990 {
991 ret = ih264d_parse_vui_parametres(&ps_seq->s_vui, ps_bitstrm);
992 if(ret != OK)
993 return ret;
994 }
995
996 /* Compare older num_reorder_frames with the new one if header is already decoded */
997 if((ps_dec->i4_header_decoded & 1) &&
998 (-1 != num_reorder_frames) &&
999 (1 == ps_seq->u1_vui_parameters_present_flag) &&
1000 (1 == ps_seq->s_vui.u1_bitstream_restriction_flag) &&
1001 ((WORD32)ps_seq->s_vui.u4_num_reorder_frames != num_reorder_frames))
1002 {
1003 ps_dec->u1_res_changed = 1;
1004 return IVD_RES_CHANGED;
1005 }
1006
1007 /* In case bitstream read has exceeded the filled size, then
1008 return an error */
1009 if (ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst)
1010 {
1011 return ERROR_INV_SPS_PPS_T;
1012 }
1013
1014 /*--------------------------------------------------------------------*/
1015 /* All initializations to ps_dec are beyond this point */
1016 /*--------------------------------------------------------------------*/
1017 ps_dec->u2_disp_height = i4_cropped_ht;
1018 ps_dec->u2_disp_width = i4_cropped_wd;
1019
1020 ps_dec->u2_pic_wd = u2_pic_wd;
1021 ps_dec->u2_pic_ht = u2_pic_ht;
1022
1023 /* Determining the Width and Height of Frame from that of Picture */
1024 ps_dec->u2_frm_wd_y = u2_frm_wd_y;
1025 ps_dec->u2_frm_ht_y = u2_frm_ht_y;
1026
1027 ps_dec->u2_frm_wd_uv = u2_frm_wd_uv;
1028 ps_dec->u2_frm_ht_uv = u2_frm_ht_uv;
1029 ps_dec->s_pad_mgr.u1_pad_len_y_v = (UWORD8)(PAD_LEN_Y_V << (1 - u1_frm));
1030 ps_dec->s_pad_mgr.u1_pad_len_cr_v = (UWORD8)(PAD_LEN_UV_V << (1 - u1_frm));
1031
1032 ps_dec->u2_frm_wd_in_mbs = ps_seq->u2_frm_wd_in_mbs;
1033 ps_dec->u2_frm_ht_in_mbs = ps_seq->u2_frm_ht_in_mbs;
1034
1035 ps_dec->u2_crop_offset_y = u2_crop_offset_y;
1036 ps_dec->u2_crop_offset_uv = u2_crop_offset_uv;
1037
1038 ps_seq->u1_is_valid = TRUE;
1039 ps_dec->ps_sps[u1_seq_parameter_set_id] = *ps_seq;
1040 ps_dec->ps_cur_sps = &ps_dec->ps_sps[u1_seq_parameter_set_id];
1041
1042 return OK;
1043 }
1044
1045 /*!
1046 **************************************************************************
1047 * \if Function name : ih264d_parse_end_of_sequence \endif
1048 *
1049 * \brief
1050 * Decodes End of Sequence.
1051 *
1052 * \param ps_bitstrm : Pointer to bit ps_bitstrm containing the NAL unit
1053 *
1054 * \return
1055 * 0 on Success and error code otherwise
1056 **************************************************************************
1057 */
ih264d_parse_end_of_sequence(dec_struct_t * ps_dec)1058 WORD32 ih264d_parse_end_of_sequence(dec_struct_t * ps_dec)
1059 {
1060 WORD32 ret;
1061
1062 ret = ih264d_end_of_pic_processing(ps_dec);
1063 return ret;
1064 }
1065
1066 /*!
1067 **************************************************************************
1068 * \if Function name : AcessUnitDelimiterRbsp \endif
1069 *
1070 * \brief
1071 * Decodes AcessUnitDelimiterRbsp.
1072 *
1073 * \param ps_bitstrm : Pointer to bit ps_bitstrm containing the NAL unit
1074 *
1075 * \return
1076 * 0 on Success and error code otherwise
1077 **************************************************************************
1078 */
1079
ih264d_access_unit_delimiter_rbsp(dec_struct_t * ps_dec)1080 WORD32 ih264d_access_unit_delimiter_rbsp(dec_struct_t * ps_dec)
1081 {
1082 UWORD8 u1_primary_pic_type;
1083 u1_primary_pic_type = ih264d_get_bits_h264(ps_dec->ps_bitstrm, 3);
1084 switch(u1_primary_pic_type)
1085 {
1086 case I_PIC:
1087 case SI_PIC:
1088 case ISI_PIC:
1089 ps_dec->ps_dec_err_status->u1_pic_aud_i = PIC_TYPE_I;
1090 break;
1091 default:
1092 ps_dec->ps_dec_err_status->u1_pic_aud_i = PIC_TYPE_UNKNOWN;
1093 }
1094 return (0);
1095 }
1096 /*!
1097 **************************************************************************
1098 * \if Function name : ih264d_parse_nal_unit \endif
1099 *
1100 * \brief
1101 * Decodes NAL unit
1102 *
1103 * \return
1104 * 0 on Success and error code otherwise
1105 **************************************************************************
1106 */
1107
ih264d_parse_nal_unit(iv_obj_t * dec_hdl,ivd_video_decode_op_t * ps_dec_op,UWORD8 * pu1_buf,UWORD32 u4_length)1108 WORD32 ih264d_parse_nal_unit(iv_obj_t *dec_hdl,
1109 ivd_video_decode_op_t *ps_dec_op,
1110 UWORD8 *pu1_buf,
1111 UWORD32 u4_length)
1112 {
1113
1114 dec_bit_stream_t *ps_bitstrm;
1115
1116
1117 dec_struct_t *ps_dec = (dec_struct_t *)dec_hdl->pv_codec_handle;
1118 ivd_video_decode_ip_t *ps_dec_in =
1119 (ivd_video_decode_ip_t *)ps_dec->pv_dec_in;
1120 dec_slice_params_t * ps_cur_slice = ps_dec->ps_cur_slice;
1121 UWORD8 u1_first_byte, u1_nal_ref_idc;
1122 UWORD8 u1_nal_unit_type;
1123 WORD32 i_status = OK;
1124 ps_bitstrm = ps_dec->ps_bitstrm;
1125
1126 if(pu1_buf)
1127 {
1128 if(u4_length)
1129 {
1130 ps_dec_op->u4_frame_decoded_flag = 0;
1131 ih264d_process_nal_unit(ps_dec->ps_bitstrm, pu1_buf,
1132 u4_length);
1133
1134 SWITCHOFFTRACE;
1135 u1_first_byte = ih264d_get_bits_h264(ps_bitstrm, 8);
1136
1137 if(NAL_FORBIDDEN_BIT(u1_first_byte))
1138 {
1139 H264_DEC_DEBUG_PRINT("\nForbidden bit set in Nal Unit, Let's try\n");
1140 }
1141 u1_nal_unit_type = NAL_UNIT_TYPE(u1_first_byte);
1142 // if any other nal unit other than slice nal is encountered in between a
1143 // frame break out of loop without consuming header
1144 if ((ps_dec->u4_slice_start_code_found == 1)
1145 && (ps_dec->u1_pic_decode_done != 1)
1146 && (u1_nal_unit_type > IDR_SLICE_NAL))
1147 {
1148 return ERROR_INCOMPLETE_FRAME;
1149 }
1150 ps_dec->u1_nal_unit_type = u1_nal_unit_type;
1151 u1_nal_ref_idc = (UWORD8)(NAL_REF_IDC(u1_first_byte));
1152 //Skip all NALUs if SPS and PPS are not decoded
1153 switch(u1_nal_unit_type)
1154 {
1155 case SLICE_DATA_PARTITION_A_NAL:
1156 case SLICE_DATA_PARTITION_B_NAL:
1157 case SLICE_DATA_PARTITION_C_NAL:
1158 if(!ps_dec->i4_decode_header)
1159 ih264d_parse_slice_partition(ps_dec, ps_bitstrm);
1160
1161 break;
1162
1163 case IDR_SLICE_NAL:
1164 case SLICE_NAL:
1165
1166 /* ! */
1167 DEBUG_THREADS_PRINTF("Decoding a slice NAL\n");
1168 if(!ps_dec->i4_decode_header)
1169 {
1170 if(ps_dec->i4_header_decoded == 3)
1171 {
1172 /* ! */
1173 ps_dec->u4_slice_start_code_found = 1;
1174
1175 ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm);
1176
1177 i_status = ih264d_parse_decode_slice(
1178 (UWORD8)(u1_nal_unit_type
1179 == IDR_SLICE_NAL),
1180 u1_nal_ref_idc, ps_dec);
1181
1182 if(i_status != OK)
1183 {
1184 return i_status;
1185 }
1186 }
1187 else
1188 {
1189 H264_DEC_DEBUG_PRINT(
1190 "\nSlice NAL Supplied but no header has been supplied\n");
1191 }
1192 }
1193 break;
1194
1195 case SEI_NAL:
1196 if(!ps_dec->i4_decode_header)
1197 {
1198 ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm);
1199 i_status = ih264d_parse_sei_message(ps_dec, ps_bitstrm);
1200 if(i_status != OK)
1201 return i_status;
1202 ih264d_parse_sei(ps_dec, ps_bitstrm);
1203 }
1204 break;
1205 case SEQ_PARAM_NAL:
1206 /* ! */
1207 ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm);
1208 i_status = ih264d_parse_sps(ps_dec, ps_bitstrm);
1209 ps_dec->u4_sps_cnt_in_process++;
1210 /*If a resolution change happens within a process call, due to multiple sps
1211 * we will not support it.
1212 */
1213 if((ps_dec->u4_sps_cnt_in_process > 1 ) &&
1214 (i_status == IVD_RES_CHANGED))
1215 {
1216 i_status = ERROR_INV_SPS_PPS_T;
1217 ps_dec->u1_res_changed = 0;
1218 }
1219 if(i_status == ERROR_INV_SPS_PPS_T)
1220 return i_status;
1221 if(!i_status)
1222 ps_dec->i4_header_decoded |= 0x1;
1223 break;
1224
1225 case PIC_PARAM_NAL:
1226 /* ! */
1227 ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm);
1228 i_status = ih264d_parse_pps(ps_dec, ps_bitstrm);
1229 if(i_status == ERROR_INV_SPS_PPS_T)
1230 return i_status;
1231 if(!i_status)
1232 ps_dec->i4_header_decoded |= 0x2;
1233 break;
1234 case ACCESS_UNIT_DELIMITER_RBSP:
1235 if(!ps_dec->i4_decode_header)
1236 {
1237 ih264d_access_unit_delimiter_rbsp(ps_dec);
1238 }
1239 break;
1240 //Let us ignore the END_OF_SEQ_RBSP NAL and decode even after this NAL
1241 case END_OF_STREAM_RBSP:
1242 if(!ps_dec->i4_decode_header)
1243 {
1244 ih264d_parse_end_of_stream(ps_dec);
1245 }
1246 break;
1247 case FILLER_DATA_NAL:
1248 if(!ps_dec->i4_decode_header)
1249 {
1250 ih264d_parse_filler_data(ps_dec, ps_bitstrm);
1251 }
1252 break;
1253 default:
1254 H264_DEC_DEBUG_PRINT("\nUnknown NAL type %d\n", u1_nal_unit_type);
1255 break;
1256 }
1257
1258 }
1259
1260 }
1261
1262 return i_status;
1263
1264 }
1265
1266