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