summaryrefslogtreecommitdiffstats
path: root/decoder/ih264d_parse_headers.c
blob: ee383158b6f43ca172f922a8ec9dc176a15c5d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
/******************************************************************************
 *
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/*!
 **************************************************************************
 * \file ih264d_parse_headers.c
 *
 * \brief
 *    Contains High level syntax[above slice] parsing routines
 *
 * \date
 *    19/12/2002
 *
 * \author  AI
 **************************************************************************
 */
#include <string.h>

#include "ih264_typedefs.h"
#include "ih264_macros.h"
#include "ih264_platform_macros.h"
#include "ih264d_bitstrm.h"
#include "ih264d_structs.h"
#include "ih264d_parse_cavlc.h"
#include "ih264d_defs.h"
#include "ih264d_defs.h"
#include "ih264d_defs.h"
#include "ih264d_parse_slice.h"
#include "ih264d_tables.h"
#include "ih264d_utils.h"
#include "ih264d_nal.h"
#include "ih264d_deblocking.h"

#include "ih264d_mem_request.h"
#include "ih264d_debug.h"
#include "ih264d_error_handler.h"
#include "ih264d_mb_utils.h"
#include "ih264d_sei.h"
#include "ih264d_vui.h"
#include "ih264d_thread_parse_decode.h"
#include "ih264d_thread_compute_bs.h"
#include "ih264d_quant_scaling.h"
#include "ih264d_defs.h"
#include "ivd.h"
#include "ih264d.h"

/*****************************************************************************/
/*                                                                           */
/*  Function Name : ih264d_parse_slice_partition                                     */
/*                                                                           */
/*  Description   : This function is intended to parse and decode slice part */
/*                  itions. Currently it's not implemented. Decoder will     */
/*                  print a message, skips this NAL and continues            */
/*  Inputs        : ps_dec    Decoder parameters                             */
/*                  ps_bitstrm    Bitstream                                */
/*  Globals       : None                                                     */
/*  Processing    : This functionality needs to be implemented               */
/*  Outputs       : None                                                     */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : Not implemented                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         06 05 2002   NS              Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 ih264d_parse_slice_partition(dec_struct_t * ps_dec,
                                    dec_bit_stream_t * ps_bitstrm)
{
    H264_DEC_DEBUG_PRINT("\nSlice partition not supported");
    UNUSED(ps_dec);
    UNUSED(ps_bitstrm);
    return (0);
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : ih264d_parse_sei                                                */
/*                                                                           */
/*  Description   : This function is intended to parse and decode SEI        */
/*                  Currently it's not implemented. Decoder will print a     */
/*                  message, skips this NAL and continues                    */
/*  Inputs        : ps_dec    Decoder parameters                       */
/*                  ps_bitstrm    Bitstream                                */
/*  Globals       : None                                                     */
/*  Processing    : This functionality needs to be implemented               */
/*  Outputs       : None                                                     */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : Not implemented                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         06 05 2002   NS              Draft                                */
/*                                                                           */
/*****************************************************************************/
WORD32 ih264d_parse_sei(dec_struct_t * ps_dec, dec_bit_stream_t * ps_bitstrm)
{
    UNUSED(ps_dec);
    UNUSED(ps_bitstrm);
    return (0);
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : ih264d_parse_filler_data                                          */
/*                                                                           */
/*  Description   : This function is intended to parse and decode filler     */
/*                  data NAL. Currently it's not implemented. Decoder will   */
/*                  print a message, skips this NAL and continues            */
/*  Inputs        : ps_dec    Decoder parameters                       */
/*                  ps_bitstrm    Bitstream                                */
/*  Globals       : None                                                     */
/*  Processing    : This functionality needs to be implemented               */
/*  Outputs       : None                                                     */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : Not implemented                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         06 05 2002   NS              Draft                                */
/*                                                                           */
/*****************************************************************************/
WORD32 ih264d_parse_filler_data(dec_struct_t * ps_dec,
                                dec_bit_stream_t * ps_bitstrm)
{
    UNUSED(ps_dec);
    UNUSED(ps_bitstrm);
    return (0);
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : ih264d_parse_end_of_stream                                        */
/*                                                                           */
/*  Description   : This function is intended to parse and decode end of     */
/*                  sequence. Currently it's not implemented. Decoder will   */
/*                  print a message, skips this NAL and continues            */
/*  Inputs        : ps_dec    Decoder parameters                       */
/*  Globals       : None                                                     */
/*  Processing    : This functionality needs to be implemented               */
/*  Outputs       : None                                                     */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : Not implemented                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         06 05 2002   NS              Draft                                */
/*                                                                           */
/*****************************************************************************/
void ih264d_parse_end_of_stream(dec_struct_t * ps_dec)
{
    UNUSED(ps_dec);
    return;
}

/*!
 **************************************************************************
 * \if Function name : ih264d_parse_pps \endif
 *
 * \brief
 *    Decodes Picture Parameter set
 *
 * \return
 *    0 on Success and Error code otherwise
 **************************************************************************
 */
WORD32 ih264d_parse_pps(dec_struct_t * ps_dec, dec_bit_stream_t * ps_bitstrm)
{
    UWORD8 uc_temp;
    dec_seq_params_t * ps_sps = NULL;
    dec_pic_params_t * ps_pps = NULL;
    UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer;
    UWORD32 *pu4_bitstrm_ofst = &ps_dec->ps_bitstrm->u4_ofst;

    /* Variables used for error resilience checks */
    UWORD32 u4_temp;
    WORD32 i_temp;

    /* For High profile related syntax elements */
    UWORD8 u1_more_data_flag;
    WORD32 i4_i;

    /*--------------------------------------------------------------------*/
    /* Decode pic_parameter_set_id and find corresponding pic params      */
    /*--------------------------------------------------------------------*/
    u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
    if(u4_temp & MASK_ERR_PIC_SET_ID)
        return ERROR_INV_SPS_PPS_T;
    ps_pps = ps_dec->pv_scratch_sps_pps;
    *ps_pps = ps_dec->ps_pps[u4_temp];
    ps_pps->u1_pic_parameter_set_id = (WORD8)u4_temp;
    COPYTHECONTEXT("PPS: pic_parameter_set_id",ps_pps->u1_pic_parameter_set_id);

    /************************************************/
    /* initilization of High profile syntax element */
    /************************************************/
    ps_pps->i4_transform_8x8_mode_flag = 0;
    ps_pps->i4_pic_scaling_matrix_present_flag = 0;

    /*--------------------------------------------------------------------*/
    /* Decode seq_parameter_set_id and map it to a seq_parameter_set      */
    /*--------------------------------------------------------------------*/
    u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
    if(u4_temp & MASK_ERR_SEQ_SET_ID)
        return ERROR_INV_SPS_PPS_T;
    COPYTHECONTEXT("PPS: seq_parameter_set_id",u4_temp);
    ps_sps = &ps_dec->ps_sps[u4_temp];
    ps_pps->ps_sps = ps_sps;

    /*--------------------------------------------------------------------*/
    /* Decode entropy_coding_mode                                         */
    /*--------------------------------------------------------------------*/
    ps_pps->u1_entropy_coding_mode = ih264d_get_bit_h264(ps_bitstrm);
    COPYTHECONTEXT("PPS: entropy_coding_mode_flag",ps_pps->u1_entropy_coding_mode);

    ps_pps->u1_pic_order_present_flag = ih264d_get_bit_h264(ps_bitstrm);
    COPYTHECONTEXT("PPS: pic_order_present_flag",ps_pps->u1_pic_order_present_flag);

    /*--------------------------------------------------------------------*/
    /* Decode num_slice_groups_minus1                                     */
    /*--------------------------------------------------------------------*/
    u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf) + 1;
    if(u4_temp != 1)
    {
        UWORD32 i4_error_code;
        i4_error_code = ERROR_FEATURE_UNAVAIL;
        return i4_error_code;
    }
    ps_pps->u1_num_slice_groups = u4_temp;
    COPYTHECONTEXT("PPS: num_slice_groups_minus1",ps_pps->u1_num_slice_groups -1);

    /*--------------------------------------------------------------------*/
    /* Other parameter set values                                         */
    /*--------------------------------------------------------------------*/
    u4_temp = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
    if(u4_temp > H264_MAX_REF_IDX)
        return ERROR_REF_IDX;
    ps_pps->u1_num_ref_idx_lx_active[0] = u4_temp;
    COPYTHECONTEXT("PPS: num_ref_idx_l0_active_minus1",
                    ps_pps->u1_num_ref_idx_lx_active[0] - 1);

    u4_temp = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
    if(u4_temp > H264_MAX_REF_IDX)
        return ERROR_REF_IDX;
    ps_pps->u1_num_ref_idx_lx_active[1] = u4_temp;
    COPYTHECONTEXT("PPS: num_ref_idx_l1_active_minus1",
                    ps_pps->u1_num_ref_idx_lx_active[1] - 1);

    ps_pps->u1_wted_pred_flag = ih264d_get_bit_h264(ps_bitstrm);
    COPYTHECONTEXT("PPS: weighted prediction u4_flag",ps_pps->u1_wted_pred_flag);
    uc_temp = ih264d_get_bits_h264(ps_bitstrm, 2);
    COPYTHECONTEXT("PPS: weighted_bipred_idc",uc_temp);
    ps_pps->u1_wted_bipred_idc = uc_temp;

    if(ps_pps->u1_wted_bipred_idc > MAX_WEIGHT_BIPRED_IDC)
        return ERROR_INV_SPS_PPS_T;

    i_temp = 26 + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);

    if((i_temp < 0) || (i_temp > 51))
        return ERROR_INV_RANGE_QP_T;

    ps_pps->u1_pic_init_qp = i_temp;
    COPYTHECONTEXT("PPS: pic_init_qp_minus26",ps_pps->u1_pic_init_qp - 26);

    i_temp = 26 + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);

    if((i_temp < 0) || (i_temp > 51))
        return ERROR_INV_RANGE_QP_T;

    ps_pps->u1_pic_init_qs = i_temp;
    COPYTHECONTEXT("PPS: pic_init_qs_minus26",ps_pps->u1_pic_init_qs - 26);

    i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
    if((i_temp < -12) || (i_temp > 12))
        return ERROR_INV_RANGE_QP_T;
    ps_pps->i1_chroma_qp_index_offset = i_temp;
    COPYTHECONTEXT("PPS: chroma_qp_index_offset",ps_pps->i1_chroma_qp_index_offset);

    /***************************************************************************/
    /* initialize second_chroma_qp_index_offset to i1_chroma_qp_index_offset if */
    /* second_chroma_qp_index_offset is not present in bit-ps_bitstrm              */
    /***************************************************************************/
    ps_pps->i1_second_chroma_qp_index_offset =
                    ps_pps->i1_chroma_qp_index_offset;

    ps_pps->u1_deblocking_filter_parameters_present_flag = ih264d_get_bit_h264(
                    ps_bitstrm);
    COPYTHECONTEXT("PPS: deblocking_filter_control_present_flag",
                    ps_pps->u1_deblocking_filter_parameters_present_flag);
    ps_pps->u1_constrained_intra_pred_flag = ih264d_get_bit_h264(ps_bitstrm);
    COPYTHECONTEXT("PPS: constrained_intra_pred_flag",
                    ps_pps->u1_constrained_intra_pred_flag);
    ps_pps->u1_redundant_pic_cnt_present_flag = ih264d_get_bit_h264(ps_bitstrm);
    COPYTHECONTEXT("PPS: redundant_pic_cnt_present_flag",
                    ps_pps->u1_redundant_pic_cnt_present_flag);

    /* High profile related syntax elements */
    u1_more_data_flag = MORE_RBSP_DATA(ps_bitstrm);
    if(u1_more_data_flag && (ps_pps->ps_sps->u1_profile_idc == HIGH_PROFILE_IDC))
    {
        /* read transform_8x8_mode_flag  */
        ps_pps->i4_transform_8x8_mode_flag = (WORD32)ih264d_get_bit_h264(
                        ps_bitstrm);

        /* read pic_scaling_matrix_present_flag */
        ps_pps->i4_pic_scaling_matrix_present_flag =
                        (WORD32)ih264d_get_bit_h264(ps_bitstrm);

        if(ps_pps->i4_pic_scaling_matrix_present_flag)
        {
            /* read the scaling matrices */
            for(i4_i = 0; i4_i < (6 + (ps_pps->i4_transform_8x8_mode_flag << 1)); i4_i++)
            {
                ps_pps->u1_pic_scaling_list_present_flag[i4_i] =
                                ih264d_get_bit_h264(ps_bitstrm);

                if(ps_pps->u1_pic_scaling_list_present_flag[i4_i])
                {
                    if(i4_i < 6)
                    {
                        ih264d_scaling_list(
                                        ps_pps->i2_pic_scalinglist4x4[i4_i],
                                        16,
                                        &ps_pps->u1_pic_use_default_scaling_matrix_flag[i4_i],
                                        ps_bitstrm);
                    }
                    else
                    {
                        ih264d_scaling_list(
                                        ps_pps->i2_pic_scalinglist8x8[i4_i - 6],
                                        64,
                                        &ps_pps->u1_pic_use_default_scaling_matrix_flag[i4_i],
                                        ps_bitstrm);
                    }
                }
            }
        }

        /* read second_chroma_qp_index_offset syntax element */
        i_temp = ih264d_sev(
                        pu4_bitstrm_ofst, pu4_bitstrm_buf);

        if((i_temp < -12) || (i_temp > 12))
            return ERROR_INV_RANGE_QP_T;

        ps_pps->i1_second_chroma_qp_index_offset = i_temp;
    }

    /* In case bitstream read has exceeded the filled size, then
       return an error */
    if(ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst + 8)
    {
        return ERROR_INV_SPS_PPS_T;
    }
    ps_pps->u1_is_valid = TRUE;
    ps_dec->ps_pps[ps_pps->u1_pic_parameter_set_id] = *ps_pps;
    return OK;
}

/*!
 **************************************************************************
 * \if Function name : ih264d_parse_sps \endif
 *
 * \brief
 *    Decodes Sequence parameter set from the bitstream
 *
 * \return
 *    0 on Success and Error code otherwise
 **************************************************************************
 */
UWORD32 ih264d_correct_level_idc(UWORD32 u4_level_idc, UWORD32 u4_total_mbs)
{
    UWORD32 u4_max_mbs_allowed;

    switch(u4_level_idc)
    {
        case H264_LEVEL_1_0:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_10;
            break;
        case H264_LEVEL_1_1:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_11;
            break;
        case H264_LEVEL_1_2:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_12;
            break;
        case H264_LEVEL_1_3:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_13;
            break;
        case H264_LEVEL_2_0:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_20;
            break;
        case H264_LEVEL_2_1:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_21;
            break;
        case H264_LEVEL_2_2:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_22;
            break;
        case H264_LEVEL_3_0:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_30;
            break;
        case H264_LEVEL_3_1:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_31;
            break;
        case H264_LEVEL_3_2:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_32;
            break;
        case H264_LEVEL_4_0:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_40;
            break;
        case H264_LEVEL_4_1:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_41;
            break;
        case H264_LEVEL_4_2:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_42;
            break;
        case H264_LEVEL_5_0:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_50;
            break;
        case H264_LEVEL_5_1:
        default:
            u4_max_mbs_allowed = MAX_MBS_LEVEL_51;
            break;

    }

    /*correct of the level is incorrect*/
    if(u4_total_mbs > u4_max_mbs_allowed)
    {
        if(u4_total_mbs > MAX_MBS_LEVEL_50)
            u4_level_idc = H264_LEVEL_5_1;
        else if(u4_total_mbs > MAX_MBS_LEVEL_42)
            u4_level_idc = H264_LEVEL_5_0;
        else if(u4_total_mbs > MAX_MBS_LEVEL_41)
            u4_level_idc = H264_LEVEL_4_2;
        else if(u4_total_mbs > MAX_MBS_LEVEL_40)
            u4_level_idc = H264_LEVEL_4_1;
        else if(u4_total_mbs > MAX_MBS_LEVEL_32)
            u4_level_idc = H264_LEVEL_4_0;
        else if(u4_total_mbs > MAX_MBS_LEVEL_31)
            u4_level_idc = H264_LEVEL_3_2;
        else if(u4_total_mbs > MAX_MBS_LEVEL_30)
            u4_level_idc = H264_LEVEL_3_1;
        else if(u4_total_mbs > MAX_MBS_LEVEL_21)
            u4_level_idc = H264_LEVEL_3_0;
        else if(u4_total_mbs > MAX_MBS_LEVEL_20)
            u4_level_idc = H264_LEVEL_2_1;
        else if(u4_total_mbs > MAX_MBS_LEVEL_10)
            u4_level_idc = H264_LEVEL_2_0;
    }

    return (u4_level_idc);

}
WORD32 ih264d_parse_sps(dec_struct_t *ps_dec, dec_bit_stream_t *ps_bitstrm)
{
    UWORD8 i;
    dec_seq_params_t *ps_seq = NULL;
    UWORD8 u1_profile_idc, u1_level_idc, u1_seq_parameter_set_id, u1_mb_aff_flag = 0;
    UWORD16 i2_max_frm_num;
    UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
    UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
    UWORD8 u1_frm, uc_constraint_set0_flag, uc_constraint_set1_flag;
    WORD32 i4_cropped_ht, i4_cropped_wd;
    UWORD32 u4_temp;
    WORD32 pic_height_in_map_units_minus1 = 0;
    UWORD32 u2_pic_wd = 0;
    UWORD32 u2_pic_ht = 0;
    UWORD32 u2_frm_wd_y = 0;
    UWORD32 u2_frm_ht_y = 0;
    UWORD32 u2_frm_wd_uv = 0;
    UWORD32 u2_frm_ht_uv = 0;
    UWORD32 u2_crop_offset_y = 0;
    UWORD32 u2_crop_offset_uv = 0;
    WORD32 ret;
    UWORD32 u4_num_reorder_frames;
    /* High profile related syntax element */
    WORD32 i4_i;
    /* G050 */
    UWORD8 u1_frame_cropping_flag, u1_frame_cropping_rect_left_ofst,
                    u1_frame_cropping_rect_right_ofst,
                    u1_frame_cropping_rect_top_ofst,
                    u1_frame_cropping_rect_bottom_ofst;
    /* G050 */
    /*--------------------------------------------------------------------*/
    /* Decode seq_parameter_set_id and profile and level values           */
    /*--------------------------------------------------------------------*/
    SWITCHONTRACE;
    u1_profile_idc = ih264d_get_bits_h264(ps_bitstrm, 8);
    COPYTHECONTEXT("SPS: profile_idc",u1_profile_idc);

    /* G050 */
    uc_constraint_set0_flag = ih264d_get_bit_h264(ps_bitstrm);
    uc_constraint_set1_flag = ih264d_get_bit_h264(ps_bitstrm);
    ih264d_get_bit_h264(ps_bitstrm);

    /*****************************************************/
    /* Read 5 bits for uc_constraint_set3_flag (1 bit)   */
    /* and reserved_zero_4bits (4 bits) - Sushant        */
    /*****************************************************/
    ih264d_get_bits_h264(ps_bitstrm, 5);
    /* G050 */

    /* Check whether particular profile is suported or not */
    /* Check whether particular profile is suported or not */
    if((u1_profile_idc != MAIN_PROFILE_IDC) &&

    (u1_profile_idc != BASE_PROFILE_IDC) &&

    (u1_profile_idc != HIGH_PROFILE_IDC)

    )
    {

        /* Apart from Baseline, main and high profile,
         * only extended profile is supported provided
         * uc_constraint_set0_flag or uc_constraint_set1_flag are set to 1
         */
        if((u1_profile_idc != EXTENDED_PROFILE_IDC) ||
           ((uc_constraint_set1_flag != 1) && (uc_constraint_set0_flag != 1)))
        {
            return (ERROR_FEATURE_UNAVAIL);
        }
    }

    u1_level_idc = ih264d_get_bits_h264(ps_bitstrm, 8);



    COPYTHECONTEXT("SPS: u4_level_idc",u1_level_idc);

    u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
    if(u4_temp & MASK_ERR_SEQ_SET_ID)
        return ERROR_INV_SPS_PPS_T;
    u1_seq_parameter_set_id = u4_temp;
    COPYTHECONTEXT("SPS: seq_parameter_set_id",
                    u1_seq_parameter_set_id);

    /*--------------------------------------------------------------------*/
    /* Find an seq param entry in seqparam array of decStruct             */
    /*--------------------------------------------------------------------*/

    ps_seq = ps_dec->pv_scratch_sps_pps;
    memset(ps_seq, 0, sizeof(dec_seq_params_t));

    if(ps_dec->i4_header_decoded & 1)
    {
        *ps_seq = *ps_dec->ps_cur_sps;
    }


    if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_profile_idc != u1_profile_idc))
    {
        ps_dec->u1_res_changed = 1;
        return IVD_RES_CHANGED;
    }

    if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_level_idc != u1_level_idc))
    {
        ps_dec->u1_res_changed = 1;
        return IVD_RES_CHANGED;
    }

    ps_seq->u1_profile_idc = u1_profile_idc;
    ps_seq->u1_level_idc = u1_level_idc;
    ps_seq->u1_seq_parameter_set_id = u1_seq_parameter_set_id;

    /*******************************************************************/
    /* Initializations for high profile - Sushant                      */
    /*******************************************************************/
    ps_seq->i4_chroma_format_idc = 1;
    ps_seq->i4_bit_depth_luma_minus8 = 0;
    ps_seq->i4_bit_depth_chroma_minus8 = 0;
    ps_seq->i4_qpprime_y_zero_transform_bypass_flag = 0;
    ps_seq->i4_seq_scaling_matrix_present_flag = 0;
    if(u1_profile_idc == HIGH_PROFILE_IDC)
    {

        /* reading chroma_format_idc   */
        ps_seq->i4_chroma_format_idc = ih264d_uev(pu4_bitstrm_ofst,
                                                  pu4_bitstrm_buf);

        /* Monochrome is not supported */
        if(ps_seq->i4_chroma_format_idc != 1)
        {
            return ERROR_INV_SPS_PPS_T;
        }

        /* reading bit_depth_luma_minus8   */
        ps_seq->i4_bit_depth_luma_minus8 = ih264d_uev(pu4_bitstrm_ofst,
                                                      pu4_bitstrm_buf);

        if(ps_seq->i4_bit_depth_luma_minus8 != 0)
        {
            return ERROR_INV_SPS_PPS_T;
        }

        /* reading bit_depth_chroma_minus8   */
        ps_seq->i4_bit_depth_chroma_minus8 = ih264d_uev(pu4_bitstrm_ofst,
                                                        pu4_bitstrm_buf);

        if(ps_seq->i4_bit_depth_chroma_minus8 != 0)
        {
            return ERROR_INV_SPS_PPS_T;
        }

        /* reading qpprime_y_zero_transform_bypass_flag   */
        ps_seq->i4_qpprime_y_zero_transform_bypass_flag =
                        (WORD32)ih264d_get_bit_h264(ps_bitstrm);

        if(ps_seq->i4_qpprime_y_zero_transform_bypass_flag != 0)
        {
            return ERROR_INV_SPS_PPS_T;
        }

        /* reading seq_scaling_matrix_present_flag   */
        ps_seq->i4_seq_scaling_matrix_present_flag =
                        (WORD32)ih264d_get_bit_h264(ps_bitstrm);

        if(ps_seq->i4_seq_scaling_matrix_present_flag)
        {
            for(i4_i = 0; i4_i < 8; i4_i++)
            {
                ps_seq->u1_seq_scaling_list_present_flag[i4_i] =
                                ih264d_get_bit_h264(ps_bitstrm);

                /* initialize u1_use_default_scaling_matrix_flag[i4_i] to zero */
                /* before calling scaling list                             */
                ps_seq->u1_use_default_scaling_matrix_flag[i4_i] = 0;

                if(ps_seq->u1_seq_scaling_list_present_flag[i4_i])
                {
                    if(i4_i < 6)
                    {
                        ih264d_scaling_list(
                                        ps_seq->i2_scalinglist4x4[i4_i],
                                        16,
                                        &ps_seq->u1_use_default_scaling_matrix_flag[i4_i],
                                        ps_bitstrm);
                    }
                    else
                    {
                        ih264d_scaling_list(
                                        ps_seq->i2_scalinglist8x8[i4_i - 6],
                                        64,
                                        &ps_seq->u1_use_default_scaling_matrix_flag[i4_i],
                                        ps_bitstrm);
                    }
                }
            }
        }
    }
    /*--------------------------------------------------------------------*/
    /* Decode MaxFrameNum                                                 */
    /*--------------------------------------------------------------------*/
    u4_temp = 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
    if(u4_temp > MAX_BITS_IN_FRAME_NUM)
    {
        return ERROR_INV_SPS_PPS_T;
    }
    ps_seq->u1_bits_in_frm_num = u4_temp;
    COPYTHECONTEXT("SPS: log2_max_frame_num_minus4",
                    (ps_seq->u1_bits_in_frm_num - 4));

    i2_max_frm_num = (1 << (ps_seq->u1_bits_in_frm_num));
    ps_seq->u2_u4_max_pic_num_minus1 = i2_max_frm_num - 1;
    /*--------------------------------------------------------------------*/
    /* Decode picture order count and related values                      */
    /*--------------------------------------------------------------------*/
    u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);

    if(u4_temp > MAX_PIC_ORDER_CNT_TYPE)
    {
        return ERROR_INV_POC_TYPE_T;
    }
    ps_seq->u1_pic_order_cnt_type = u4_temp;
    COPYTHECONTEXT("SPS: pic_order_cnt_type",ps_seq->u1_pic_order_cnt_type);

    ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle = 1;
    if(ps_seq->u1_pic_order_cnt_type == 0)
    {
        u4_temp = 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
        if(u4_temp > MAX_BITS_IN_POC_LSB)
        {
            return ERROR_INV_SPS_PPS_T;
        }
        ps_seq->u1_log2_max_pic_order_cnt_lsb_minus = u4_temp;
        ps_seq->i4_max_pic_order_cntLsb = (1 << u4_temp);
        COPYTHECONTEXT("SPS: log2_max_pic_order_cnt_lsb_minus4",(u4_temp - 4));
    }
    else if(ps_seq->u1_pic_order_cnt_type == 1)
    {
        ps_seq->u1_delta_pic_order_always_zero_flag = ih264d_get_bit_h264(
                        ps_bitstrm);
        COPYTHECONTEXT("SPS: delta_pic_order_always_zero_flag",
                        ps_seq->u1_delta_pic_order_always_zero_flag);

        ps_seq->i4_ofst_for_non_ref_pic = ih264d_sev(pu4_bitstrm_ofst,
                                                     pu4_bitstrm_buf);
        COPYTHECONTEXT("SPS: offset_for_non_ref_pic",
                        ps_seq->i4_ofst_for_non_ref_pic);

        ps_seq->i4_ofst_for_top_to_bottom_field = ih264d_sev(
                        pu4_bitstrm_ofst, pu4_bitstrm_buf);
        COPYTHECONTEXT("SPS: offset_for_top_to_bottom_field",
                        ps_seq->i4_ofst_for_top_to_bottom_field);

        u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
        if(u4_temp > 255)
            return ERROR_INV_SPS_PPS_T;
        ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle = u4_temp;
        COPYTHECONTEXT("SPS: num_ref_frames_in_pic_order_cnt_cycle",
                        ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle);

        for(i = 0; i < ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle; i++)
        {
            ps_seq->i4_ofst_for_ref_frame[i] = ih264d_sev(
                            pu4_bitstrm_ofst, pu4_bitstrm_buf);
            COPYTHECONTEXT("SPS: offset_for_ref_frame",
                            ps_seq->i4_ofst_for_ref_frame[i]);
        }
    }

    u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);

    if((u4_temp > H264_MAX_REF_PICS))
    {
        return ERROR_NUM_REF;
    }

    /* Compare with older num_ref_frames is header is already once */
    if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_num_ref_frames != u4_temp))
    {
        ps_dec->u1_res_changed = 1;
        return IVD_RES_CHANGED;
    }

    ps_seq->u1_num_ref_frames = u4_temp;
    COPYTHECONTEXT("SPS: num_ref_frames",ps_seq->u1_num_ref_frames);

    ps_seq->u1_gaps_in_frame_num_value_allowed_flag = ih264d_get_bit_h264(
                    ps_bitstrm);
    COPYTHECONTEXT("SPS: gaps_in_frame_num_value_allowed_flag",
                    ps_seq->u1_gaps_in_frame_num_value_allowed_flag);

    /*--------------------------------------------------------------------*/
    /* Decode FrameWidth and FrameHeight and related values               */
    /*--------------------------------------------------------------------*/
    ps_seq->u2_frm_wd_in_mbs = 1
                    + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
    COPYTHECONTEXT("SPS: pic_width_in_mbs_minus1",
                    ps_seq->u2_frm_wd_in_mbs - 1);
    u2_pic_wd = (ps_seq->u2_frm_wd_in_mbs << 4);

    pic_height_in_map_units_minus1 = ih264d_uev(pu4_bitstrm_ofst,
                                                pu4_bitstrm_buf);
    ps_seq->u2_frm_ht_in_mbs = 1 + pic_height_in_map_units_minus1;

    u2_pic_ht = (ps_seq->u2_frm_ht_in_mbs << 4);

    /*--------------------------------------------------------------------*/
    /* Get the value of MaxMbAddress and Number of bits needed for it     */
    /*--------------------------------------------------------------------*/
    ps_seq->u2_max_mb_addr = (ps_seq->u2_frm_wd_in_mbs
                    * ps_seq->u2_frm_ht_in_mbs) - 1;

    ps_seq->u2_total_num_of_mbs = ps_seq->u2_max_mb_addr + 1;

    ps_seq->u1_level_idc = ih264d_correct_level_idc(
                    u1_level_idc, ps_seq->u2_total_num_of_mbs);

    u1_frm = ih264d_get_bit_h264(ps_bitstrm);
    ps_seq->u1_frame_mbs_only_flag = u1_frm;

    COPYTHECONTEXT("SPS: frame_mbs_only_flag", u1_frm);

    if(!u1_frm)
        u1_mb_aff_flag = ih264d_get_bit_h264(ps_bitstrm);

    if((ps_dec->i4_header_decoded & 1)
                    && (ps_seq->u1_mb_aff_flag != u1_mb_aff_flag))
    {
        ps_dec->u1_res_changed = 1;
        return IVD_RES_CHANGED;
    }

    if(!u1_frm)
    {
        u2_pic_ht <<= 1;
        ps_seq->u1_mb_aff_flag = u1_mb_aff_flag;
        COPYTHECONTEXT("SPS: mb_adaptive_frame_field_flag",
                        ps_seq->u1_mb_aff_flag);

    }
    else
        ps_seq->u1_mb_aff_flag = 0;

    ps_seq->u1_direct_8x8_inference_flag = ih264d_get_bit_h264(ps_bitstrm);

    COPYTHECONTEXT("SPS: direct_8x8_inference_flag",
                    ps_seq->u1_direct_8x8_inference_flag);

    /* G050 */
    u1_frame_cropping_flag = ih264d_get_bit_h264(ps_bitstrm);
    COPYTHECONTEXT("SPS: frame_cropping_flag",u1_frame_cropping_flag);

    if(u1_frame_cropping_flag)
    {
        u1_frame_cropping_rect_left_ofst = ih264d_uev(pu4_bitstrm_ofst,
                                                      pu4_bitstrm_buf);
        COPYTHECONTEXT("SPS: frame_cropping_rect_left_offset",
                        u1_frame_cropping_rect_left_ofst);
        u1_frame_cropping_rect_right_ofst = ih264d_uev(pu4_bitstrm_ofst,
                                                       pu4_bitstrm_buf);
        COPYTHECONTEXT("SPS: frame_cropping_rect_right_offset",
                        u1_frame_cropping_rect_right_ofst);
        u1_frame_cropping_rect_top_ofst = ih264d_uev(pu4_bitstrm_ofst,
                                                     pu4_bitstrm_buf);
        COPYTHECONTEXT("SPS: frame_cropping_rect_top_offset",
                        u1_frame_cropping_rect_top_ofst);
        u1_frame_cropping_rect_bottom_ofst = ih264d_uev(pu4_bitstrm_ofst,
                                                        pu4_bitstrm_buf);
        COPYTHECONTEXT("SPS: frame_cropping_rect_bottom_offset",
                        u1_frame_cropping_rect_bottom_ofst);
    }
    /* G050 */

    ps_seq->u1_vui_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm);
    COPYTHECONTEXT("SPS: vui_parameters_present_flag",
                    ps_seq->u1_vui_parameters_present_flag);

    u2_frm_wd_y = u2_pic_wd + (UWORD8)(PAD_LEN_Y_H << 1);
    if(1 == ps_dec->u4_share_disp_buf)
    {
        if(ps_dec->u4_app_disp_width > u2_frm_wd_y)
            u2_frm_wd_y = ps_dec->u4_app_disp_width;
    }

    u2_frm_ht_y = u2_pic_ht + (UWORD8)(PAD_LEN_Y_V << 2);
    u2_frm_wd_uv = u2_pic_wd + (UWORD8)(PAD_LEN_UV_H << 2);
    u2_frm_wd_uv = MAX(u2_frm_wd_uv, u2_frm_wd_y);

    u2_frm_ht_uv = (u2_pic_ht >> 1) + (UWORD8)(PAD_LEN_UV_V << 2);
    u2_frm_ht_uv = MAX(u2_frm_ht_uv, (u2_frm_ht_y >> 1));


    /* Calculate display picture width, height and start u4_ofst from YUV420 */
    /* pictute buffers as per cropping information parsed above             */
    {
        UWORD16 u2_rgt_ofst = 0;
        UWORD16 u2_lft_ofst = 0;
        UWORD16 u2_top_ofst = 0;
        UWORD16 u2_btm_ofst = 0;
        UWORD8 u1_frm_mbs_flag;
        UWORD8 u1_vert_mult_factor;

        if(u1_frame_cropping_flag)
        {
            /* Calculate right and left u4_ofst for cropped picture           */
            u2_rgt_ofst = u1_frame_cropping_rect_right_ofst << 1;
            u2_lft_ofst = u1_frame_cropping_rect_left_ofst << 1;

            /* Know frame MBs only u4_flag                                      */
            u1_frm_mbs_flag = (1 == ps_seq->u1_frame_mbs_only_flag);

            /* Simplify the vertical u4_ofst calculation from field/frame     */
            u1_vert_mult_factor = (2 - u1_frm_mbs_flag);

            /* Calculate bottom and top u4_ofst for cropped  picture          */
            u2_btm_ofst = (u1_frame_cropping_rect_bottom_ofst
                            << u1_vert_mult_factor);
            u2_top_ofst = (u1_frame_cropping_rect_top_ofst
                            << u1_vert_mult_factor);
        }

        /* Calculate u4_ofst from start of YUV 420 picture buffer to start of*/
        /* cropped picture buffer                                           */
        u2_crop_offset_y = (u2_frm_wd_y * u2_top_ofst) + (u2_lft_ofst);
        u2_crop_offset_uv = (u2_frm_wd_uv * (u2_top_ofst >> 1))
                        + (u2_lft_ofst >> 1) * YUV420SP_FACTOR;
        /* Calculate the display picture width and height based on crop      */
        /* information                                                       */
        i4_cropped_ht = u2_pic_ht - (u2_btm_ofst + u2_top_ofst);
        i4_cropped_wd = u2_pic_wd - (u2_rgt_ofst + u2_lft_ofst);

        if((i4_cropped_ht < MB_SIZE) || (i4_cropped_wd < MB_SIZE))
        {
            return ERROR_INV_SPS_PPS_T;
        }

        if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_pic_wd != u2_pic_wd))
        {
            ps_dec->u1_res_changed = 1;
            return IVD_RES_CHANGED;
        }

        if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_disp_width != i4_cropped_wd))
        {
            ps_dec->u1_res_changed = 1;
            return IVD_RES_CHANGED;
        }

        if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_pic_ht != u2_pic_ht))
        {
            ps_dec->u1_res_changed = 1;
            return IVD_RES_CHANGED;
        }

        if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_disp_height != i4_cropped_ht))
        {
            ps_dec->u1_res_changed = 1;
            return IVD_RES_CHANGED;
        }

        /* Check for unsupported resolutions */
        if((u2_pic_wd > H264_MAX_FRAME_WIDTH) || (u2_pic_ht > H264_MAX_FRAME_HEIGHT))
        {
            return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
        }

        /* If MBAff is enabled, decoder support is limited to streams with
         * width less than half of H264_MAX_FRAME_WIDTH.
         * In case of MBAff decoder processes two rows at a time
         */
        if((u2_pic_wd << ps_seq->u1_mb_aff_flag) > H264_MAX_FRAME_WIDTH)
        {
            return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
        }

    }

    /* Backup u4_num_reorder_frames if header is already decoded */
    if((ps_dec->i4_header_decoded & 1) &&
                    (1 == ps_seq->u1_vui_parameters_present_flag) &&
                    (1 == ps_seq->s_vui.u1_bitstream_restriction_flag))
    {
        u4_num_reorder_frames =  ps_seq->s_vui.u4_num_reorder_frames;
    }
    else
    {
        u4_num_reorder_frames = -1;
    }
    if(1 == ps_seq->u1_vui_parameters_present_flag)
    {
        ret = ih264d_parse_vui_parametres(&ps_seq->s_vui, ps_bitstrm);
        if(ret != OK)
            return ret;
    }

    /* Compare older u4_num_reorder_frames with the new one if header is already decoded */
    if((ps_dec->i4_header_decoded & 1) &&
                    (-1 != (WORD32)u4_num_reorder_frames) &&
                    (1 == ps_seq->u1_vui_parameters_present_flag) &&
                    (1 == ps_seq->s_vui.u1_bitstream_restriction_flag) &&
                    (ps_seq->s_vui.u4_num_reorder_frames != u4_num_reorder_frames))
    {
        ps_dec->u1_res_changed = 1;
        return IVD_RES_CHANGED;
    }

    /* In case bitstream read has exceeded the filled size, then
     return an error */
    if (ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst)
    {
        return ERROR_INV_SPS_PPS_T;
    }

    /*--------------------------------------------------------------------*/
    /* All initializations to ps_dec are beyond this point                */
    /*--------------------------------------------------------------------*/
    ps_dec->u2_disp_height = i4_cropped_ht;
    ps_dec->u2_disp_width = i4_cropped_wd;

    ps_dec->u2_pic_wd = u2_pic_wd;
    ps_dec->u2_pic_ht = u2_pic_ht;

    /* Determining the Width and Height of Frame from that of Picture */
    ps_dec->u2_frm_wd_y = u2_frm_wd_y;
    ps_dec->u2_frm_ht_y = u2_frm_ht_y;

    ps_dec->u2_frm_wd_uv = u2_frm_wd_uv;
    ps_dec->u2_frm_ht_uv = u2_frm_ht_uv;
    ps_dec->s_pad_mgr.u1_pad_len_y_v = (UWORD8)(PAD_LEN_Y_V << (1 - u1_frm));
    ps_dec->s_pad_mgr.u1_pad_len_cr_v = (UWORD8)(PAD_LEN_UV_V << (1 - u1_frm));

    ps_dec->u2_frm_wd_in_mbs = ps_seq->u2_frm_wd_in_mbs;
    ps_dec->u2_frm_ht_in_mbs = ps_seq->u2_frm_ht_in_mbs;

    ps_dec->u2_crop_offset_y = u2_crop_offset_y;
    ps_dec->u2_crop_offset_uv = u2_crop_offset_uv;

    ps_seq->u1_is_valid = TRUE;
    ps_dec->ps_sps[u1_seq_parameter_set_id] = *ps_seq;
    ps_dec->ps_cur_sps = &ps_dec->ps_sps[u1_seq_parameter_set_id];

    return OK;
}

/*!
 **************************************************************************
 * \if Function name : ih264d_parse_end_of_sequence \endif
 *
 * \brief
 *    Decodes End of Sequence.
 *
 * \param ps_bitstrm   : Pointer to bit ps_bitstrm containing the NAL unit
 *
 * \return
 *    0 on Success and error code otherwise
 **************************************************************************
 */
WORD32 ih264d_parse_end_of_sequence(dec_struct_t * ps_dec)
{
    WORD32 ret;

    ret = ih264d_end_of_pic_processing(ps_dec);
    return ret;
}

/*!
 **************************************************************************
 * \if Function name : AcessUnitDelimiterRbsp \endif
 *
 * \brief
 *    Decodes AcessUnitDelimiterRbsp.
 *
 * \param ps_bitstrm   : Pointer to bit ps_bitstrm containing the NAL unit
 *
 * \return
 *    0 on Success and error code otherwise
 **************************************************************************
 */

WORD32 ih264d_access_unit_delimiter_rbsp(dec_struct_t * ps_dec)
{
    UWORD8 u1_primary_pic_type;
    u1_primary_pic_type = ih264d_get_bits_h264(ps_dec->ps_bitstrm, 3);
    switch(u1_primary_pic_type)
    {
        case I_PIC:
        case SI_PIC:
        case ISI_PIC:
            ps_dec->ps_dec_err_status->u1_pic_aud_i = PIC_TYPE_I;
            break;
        default:
            ps_dec->ps_dec_err_status->u1_pic_aud_i = PIC_TYPE_UNKNOWN;
    }
    return (0);
}
/*!
 **************************************************************************
 * \if Function name : ih264d_parse_nal_unit \endif
 *
 * \brief
 *    Decodes NAL unit
 *
 * \return
 *    0 on Success and error code otherwise
 **************************************************************************
 */

WORD32 ih264d_parse_nal_unit(iv_obj_t *dec_hdl,
                          ivd_video_decode_op_t *ps_dec_op,
                          UWORD8 *pu1_buf,
                          UWORD32 u4_length)
{

    dec_bit_stream_t *ps_bitstrm;


    dec_struct_t *ps_dec = (dec_struct_t *)dec_hdl->pv_codec_handle;
    ivd_video_decode_ip_t *ps_dec_in =
                    (ivd_video_decode_ip_t *)ps_dec->pv_dec_in;
    dec_slice_params_t * ps_cur_slice = ps_dec->ps_cur_slice;
    UWORD8 u1_first_byte, u1_nal_ref_idc;
    UWORD8 u1_nal_unit_type;
    WORD32 i_status = OK;
    ps_bitstrm = ps_dec->ps_bitstrm;

    if(pu1_buf)
    {
        if(u4_length)
        {
            ps_dec_op->u4_frame_decoded_flag = 0;
            ih264d_process_nal_unit(ps_dec->ps_bitstrm, pu1_buf,
                                    u4_length);

            SWITCHOFFTRACE;
            u1_first_byte = ih264d_get_bits_h264(ps_bitstrm, 8);

            if(NAL_FORBIDDEN_BIT(u1_first_byte))
            {
                H264_DEC_DEBUG_PRINT("\nForbidden bit set in Nal Unit, Let's try\n");
            }
            u1_nal_unit_type = NAL_UNIT_TYPE(u1_first_byte);
            // if any other nal unit other than slice nal is encountered in between a
            // frame break out of loop without consuming header
            if ((ps_dec->u4_slice_start_code_found == 1)
                    && (ps_dec->u1_pic_decode_done != 1)
                    && (u1_nal_unit_type > IDR_SLICE_NAL))
            {
                return ERROR_INCOMPLETE_FRAME;
            }
            ps_dec->u1_nal_unit_type = u1_nal_unit_type;
            u1_nal_ref_idc = (UWORD8)(NAL_REF_IDC(u1_first_byte));
            //Skip all NALUs if SPS and PPS are not decoded
            switch(u1_nal_unit_type)
            {
                case SLICE_DATA_PARTITION_A_NAL:
                case SLICE_DATA_PARTITION_B_NAL:
                case SLICE_DATA_PARTITION_C_NAL:
                    if(!ps_dec->i4_decode_header)
                        ih264d_parse_slice_partition(ps_dec, ps_bitstrm);

                    break;

                case IDR_SLICE_NAL:
                case SLICE_NAL:

                    /* ! */
                    DEBUG_THREADS_PRINTF("Decoding  a slice NAL\n");
                    if(!ps_dec->i4_decode_header)
                    {
                        if(ps_dec->i4_header_decoded == 3)
                        {
                            /* ! */
                            ps_dec->u4_slice_start_code_found = 1;

                            ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm);

                            i_status = ih264d_parse_decode_slice(
                                            (UWORD8)(u1_nal_unit_type
                                                            == IDR_SLICE_NAL),
                                            u1_nal_ref_idc, ps_dec);

                            if(i_status != OK)
                            {
                                return i_status;
                            }
                        }
                        else
                        {
                            H264_DEC_DEBUG_PRINT(
                                            "\nSlice NAL Supplied but no header has been supplied\n");
                        }
                    }
                    break;

                case SEI_NAL:
                    if(!ps_dec->i4_decode_header)
                    {
                        ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm);
                        i_status = ih264d_parse_sei_message(ps_dec, ps_bitstrm);
                        if(i_status != OK)
                            return i_status;
                        ih264d_parse_sei(ps_dec, ps_bitstrm);
                    }
                    break;
                case SEQ_PARAM_NAL:
                    /* ! */
                    ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm);
                    i_status = ih264d_parse_sps(ps_dec, ps_bitstrm);
                    ps_dec->u4_sps_cnt_in_process++;
                    /*If a resolution change happens within a process call, due to multiple sps
                     * we will not support it.
                     */
                    if((ps_dec->u4_sps_cnt_in_process > 1 ) &&
                                    (i_status == IVD_RES_CHANGED))
                    {
                        i_status = ERROR_INV_SPS_PPS_T;
                        ps_dec->u1_res_changed = 0;
                    }
                    if(i_status == ERROR_INV_SPS_PPS_T)
                        return i_status;
                    if(!i_status)
                        ps_dec->i4_header_decoded |= 0x1;
                    break;

                case PIC_PARAM_NAL:
                    /* ! */
                    ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm);
                    i_status = ih264d_parse_pps(ps_dec, ps_bitstrm);
                    if(i_status == ERROR_INV_SPS_PPS_T)
                        return i_status;
                    if(!i_status)
                        ps_dec->i4_header_decoded |= 0x2;
                    break;
                case ACCESS_UNIT_DELIMITER_RBSP:
                    if(!ps_dec->i4_decode_header)
                    {
                        ih264d_access_unit_delimiter_rbsp(ps_dec);
                    }
                    break;
                    //Let us ignore the END_OF_SEQ_RBSP NAL and decode even after this NAL
                case END_OF_STREAM_RBSP:
                    if(!ps_dec->i4_decode_header)
                    {
                        ih264d_parse_end_of_stream(ps_dec);
                    }
                    break;
                case FILLER_DATA_NAL:
                    if(!ps_dec->i4_decode_header)
                    {
                        ih264d_parse_filler_data(ps_dec, ps_bitstrm);
                    }
                    break;
                default:
                    H264_DEC_DEBUG_PRINT("\nUnknown NAL type %d\n", u1_nal_unit_type);
                    break;
            }

        }

    }

    return i_status;

}