videoeditorengine/avcedit/src/parameterset.cpp
branchRCL_3
changeset 3 e0b5df5c0969
parent 0 951a5db380a0
child 7 4c409de21d23
equal deleted inserted replaced
0:951a5db380a0 3:e0b5df5c0969
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Ixonos Plc
       
    14 *
       
    15 * Description:
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <string.h>
       
    21 #include "globals.h"
       
    22 #include "vld.h"
       
    23 #include "parameterset.h"
       
    24 
       
    25 
       
    26 #define MIN_CHROMA_QP_INDEX           -12
       
    27 #define MAX_CHROMA_QP_INDEX            12
       
    28 #define NUM_LEVELS                     16     /* The number of AVC levels */
       
    29 #define MAX_PIC_SIZE_IN_MBS            36864
       
    30 #define MAX_PIC_WIDTH_IN_MBS           543    /* Sqrt( MAX_PIC_SIZE_IN_MBS * 8 ) */
       
    31 #define MAX_PIC_HEIGHT_IN_MBS          543    /* Sqrt( MAX_PIC_SIZE_IN_MBS * 8 ) */
       
    32 
       
    33 
       
    34 /* These fields are defined in Annex A of the standard */
       
    35 typedef struct _level_s 
       
    36 {
       
    37   int8 levelNumber;
       
    38   int8 constraintSet3flag;
       
    39   int32 maxMBPS;
       
    40   int32 maxFS;
       
    41   int32 maxDPB;
       
    42   int32 maxBR;
       
    43   int32 maxCPB;
       
    44   int16 maxVmvR;
       
    45   int8 minCR;
       
    46   int8 maxMvsPer2Mb;
       
    47 } level_s;
       
    48 
       
    49 /* Parameters for all levels */
       
    50 static const level_s levelArray[NUM_LEVELS] = {
       
    51   {10, 0,   1485,    99,   152064,     64,    175,  64, 2, 32},
       
    52   {11, 1,   1485,    99,   152064,    128,    350,  64, 2, 32}, /* level 1b */
       
    53   {11, 0,   3000,   396,   345600,    192,    500, 128, 2, 32},
       
    54   {12, 0,   6000,   396,   912384,    384,   1000, 128, 2, 32},
       
    55   {13, 0,  11880,   396,   912384,    768,   2000, 128, 2, 32},
       
    56   {20, 0,  11880,   396,   912384,   2000,   2000, 128, 2, 32},
       
    57   {21, 0,  19800,   792,  1824768,   4000,   4000, 256, 2, 32},
       
    58   {22, 0,  20250,  1620,  3110400,   4000,   4000, 256, 2, 32},
       
    59   {30, 0,  40500,  1620,  3110400,  10000,  10000, 256, 2, 32},
       
    60   {31, 0, 108000,  3600,  6912000,  14000,  14000, 512, 4, 16},
       
    61   {32, 0, 216000,  5120,  7864320,  20000,  20000, 512, 4, 16},
       
    62   {40, 0, 245760,  8192, 12582912,  20000,  25000, 512, 4, 16},
       
    63   {41, 0, 245760,  8192, 12582912,  50000,  62500, 512, 2, 16},
       
    64   {42, 0, 491520,  8192, 12582912,  50000,  62500, 512, 2, 16},
       
    65   {50, 0, 589824, 22080, 42393600, 135000, 135000, 512, 2, 16},
       
    66   {51, 0, 983040, 36864, 70778880, 240000, 240000, 512, 2, 16}
       
    67 };
       
    68 
       
    69 #ifdef VIDEOEDITORENGINE_AVC_EDITING
       
    70 
       
    71 struct aspectRatio_s 
       
    72 {
       
    73   int width;
       
    74   int height;
       
    75 };
       
    76 
       
    77 static const struct aspectRatio_s aspectRatioArr[13] = 
       
    78 {
       
    79   {  1,  1},
       
    80   { 12, 11},
       
    81   { 10, 11},
       
    82   { 16, 11},
       
    83   { 40, 33},
       
    84   { 24, 11},
       
    85   { 20, 11},
       
    86   { 32, 11},
       
    87   { 80, 33},
       
    88   { 18, 11},
       
    89   { 15, 11},
       
    90   { 64, 33},
       
    91   {160, 99}
       
    92 };
       
    93 #endif  // VIDEOEDITORENGINE_AVC_EDITING
       
    94 
       
    95 /*
       
    96  * AVC syntax functions as specified in specification
       
    97  */
       
    98 
       
    99 /* Return fixed length code */
       
   100 static int u_n(bitbuffer_s *bitbuf, int len, unsigned int *val)
       
   101 {
       
   102   *val = vldGetFLC(bitbuf, len);
       
   103 
       
   104   if (bibGetStatus(bitbuf) < 0)
       
   105     return PS_ERROR;
       
   106 
       
   107   return PS_OK;
       
   108 }
       
   109 
       
   110 /* Return unsigned UVLC code */
       
   111 static int ue_v(bitbuffer_s *bitbuf, unsigned int *val, unsigned int maxVal)
       
   112 {
       
   113   *val = vldGetUVLC(bitbuf);
       
   114 
       
   115   if (bibGetStatus(bitbuf) < 0)
       
   116     return PS_ERROR;
       
   117 
       
   118   if (*val > maxVal)
       
   119     return PS_ERR_ILLEGAL_VALUE;
       
   120 
       
   121   return PS_OK;
       
   122 }
       
   123 
       
   124 /* Return long signed UVLC code */
       
   125 static int se_v_long(bitbuffer_s *bitbuf, int32 *val)
       
   126 {
       
   127   *val = vldGetSignedUVLClong(bitbuf);
       
   128 
       
   129   if (bibGetStatus(bitbuf) < 0)
       
   130     return PS_ERROR;
       
   131 
       
   132   return PS_OK;
       
   133 }
       
   134 
       
   135 /* Return long unsigned UVLC code */
       
   136 static int ue_v_long(bitbuffer_s *bitbuf, u_int32 *val, u_int32 maxVal)
       
   137 {
       
   138   *val = vldGetUVLClong(bitbuf);
       
   139 
       
   140   if (bibGetStatus(bitbuf) < 0)
       
   141     return PS_ERROR;
       
   142 
       
   143   if (*val > maxVal)
       
   144     return PS_ERR_ILLEGAL_VALUE;
       
   145 
       
   146   return PS_OK;
       
   147 }
       
   148 
       
   149 #ifdef VIDEOEDITORENGINE_AVC_EDITING
       
   150 
       
   151 /* Return signed UVLC code */
       
   152 static int se_v(bitbuffer_s *bitbuf, int *val, int minVal, int maxVal)
       
   153 {
       
   154   *val = vldGetSignedUVLC(bitbuf);
       
   155 
       
   156   if (bibGetStatus(bitbuf) < 0)
       
   157     return PS_ERROR;
       
   158 
       
   159   if (*val < minVal || *val > maxVal)
       
   160     return PS_ERR_ILLEGAL_VALUE;
       
   161 
       
   162   return PS_OK;
       
   163 }
       
   164 
       
   165 #endif  // VIDEOEDITORENGINE_AVC_EDITING
       
   166 
       
   167 
       
   168 /*
       
   169  * getLevel:
       
   170  *
       
   171  * Parameters:
       
   172  *      levelNumber
       
   173  *      constraintSet3flag
       
   174  *
       
   175  * Function:
       
   176  *      Return parameters for level based on level number.
       
   177  *
       
   178  *  Return:
       
   179  *      Pointer to level or 0 if level does not exist
       
   180  */
       
   181 static const level_s *getLevel(int levelNumber, int constraintSet3flag)
       
   182 {
       
   183   int i;
       
   184 
       
   185   for (i = 0; i < NUM_LEVELS; i++) {
       
   186     if (levelArray[i].levelNumber == levelNumber &&
       
   187         levelArray[i].constraintSet3flag == constraintSet3flag)
       
   188       return &levelArray[i];
       
   189   }
       
   190 
       
   191   PRINT((_L("Unknown level: %i.\n"), levelNumber));            
       
   192   return 0;
       
   193 }
       
   194 
       
   195 /*
       
   196  *
       
   197  * getHrdParameters:
       
   198  *
       
   199  * Parameters:
       
   200  *      bitbuf                The bitbuffer object
       
   201  *      hrd                   the pointer for returning HRD parameters
       
   202  *
       
   203  * Function:
       
   204  *      decode the HRD Parameters
       
   205  *
       
   206  * Returns:
       
   207  *      PS_OK:                Hrd parameters decoded succesfully
       
   208  *      <0:                   Fail
       
   209  */
       
   210 static int getHrdParameters(bitbuffer_s *bitbuf, hrd_parameters_s *hrd)
       
   211 {
       
   212   unsigned int i;
       
   213   int retCode;
       
   214 
       
   215   if ((retCode = ue_v(bitbuf, &hrd->cpb_cnt_minus1, 31)) < 0)
       
   216     return retCode;
       
   217 
       
   218   if ((retCode = u_n(bitbuf, 4, &hrd->bit_rate_scale)) < 0)
       
   219     return retCode;
       
   220 
       
   221   if ((retCode = u_n(bitbuf, 4, &hrd->cpb_size_scale)) < 0)
       
   222     return retCode;
       
   223 
       
   224   for (i = 0; i <= hrd->cpb_cnt_minus1; i++) {
       
   225     /* bit_rate_value_minus1 must be in range of 0 to 2^32-2 */
       
   226     if ((retCode = ue_v_long(bitbuf, &hrd->bit_rate_value_minus1[i], (u_int32)4294967294U)) < 0)
       
   227       return retCode;
       
   228 
       
   229     /* cpb_size_value_minus1 must be in range of 0 to 2^32-2 */
       
   230     if ((retCode = ue_v_long(bitbuf, &hrd->cpb_size_value_minus1[i], (u_int32)4294967294U)) < 0)
       
   231       return retCode;
       
   232 
       
   233     if ((retCode = u_n(bitbuf, 1, &hrd->cbr_flag[i])) < 0)
       
   234       return retCode;
       
   235   }
       
   236 
       
   237   if ((retCode = u_n(bitbuf, 5, &hrd->initial_cpb_removal_delay_length_minus1)) < 0)
       
   238     return retCode;
       
   239 
       
   240   if ((retCode = u_n(bitbuf, 5, &hrd->cpb_removal_delay_length_minus1)) < 0)
       
   241     return retCode;
       
   242 
       
   243   if ((retCode = u_n(bitbuf, 5, &hrd->dpb_output_delay_length_minus1)) < 0)
       
   244     return retCode;
       
   245 
       
   246   if ((retCode = u_n(bitbuf, 5, &hrd->time_offset_length)) < 0)
       
   247     return retCode;
       
   248 
       
   249   return PS_OK;
       
   250 }
       
   251 
       
   252 
       
   253 
       
   254 /*
       
   255  *
       
   256  * getVUI:
       
   257  *
       
   258  * Parameters:
       
   259  *      bitbuf                The bitbuffer object
       
   260  *      vui                   the pointer for returning VUI parameters
       
   261  *
       
   262  * Function:
       
   263  *      decode the VUI Parameters
       
   264  *
       
   265  * Returns:
       
   266  *      PS_OK:                VUI parameters decoded succesfully
       
   267  *      <0:                   Fail
       
   268  */
       
   269 static int getVUI(bitbuffer_s *bitbuf, vui_parameters_s *vui)
       
   270 {
       
   271   unsigned tempWordHi, tempWordLo;
       
   272   int retCode;
       
   273 
       
   274   if ((retCode = u_n(bitbuf, 1, &vui->aspect_ratio_info_present_flag)) < 0)
       
   275     return retCode;
       
   276 
       
   277   if (vui->aspect_ratio_info_present_flag) {
       
   278     if ((retCode = u_n(bitbuf, 8, &vui->aspect_ratio_idc)) < 0)
       
   279       return retCode;
       
   280     if (vui->aspect_ratio_idc == PS_EXTENDED_SAR) {
       
   281       if ((retCode = u_n(bitbuf, 16, &vui->sar_width)) < 0)
       
   282         return retCode;
       
   283       if ((retCode = u_n(bitbuf, 16, &vui->sar_height)) < 0)
       
   284         return retCode;
       
   285     }
       
   286   }
       
   287 
       
   288   if ((retCode = u_n(bitbuf, 1, &vui->overscan_info_present_flag)) < 0)
       
   289     return retCode;
       
   290 
       
   291   if (vui->overscan_info_present_flag) {
       
   292     if ((retCode = u_n(bitbuf, 1, &vui->overscan_appropriate_flag)) < 0)
       
   293       return retCode;
       
   294   }
       
   295 
       
   296   if ((retCode = u_n(bitbuf, 1, &vui->video_signal_type_present_flag)) < 0)
       
   297     return retCode;
       
   298 
       
   299   if (vui->video_signal_type_present_flag) {
       
   300     if ((retCode = u_n(bitbuf, 3, &vui->video_format)) < 0)
       
   301       return retCode;
       
   302     if ((retCode = u_n(bitbuf, 1, &vui->video_full_range_flag)) < 0)
       
   303       return retCode;
       
   304     if ((retCode = u_n(bitbuf, 1, &vui->colour_description_present_flag)) < 0)
       
   305       return retCode;
       
   306     if (vui->colour_description_present_flag) {
       
   307       if ((retCode = u_n(bitbuf, 8, &vui->colour_primaries)) < 0)
       
   308         return retCode;
       
   309       if ((retCode = u_n(bitbuf, 8, &vui->transfer_characteristics)) < 0)
       
   310         return retCode;
       
   311       if ((retCode = u_n(bitbuf, 8, &vui->matrix_coefficients)) < 0)
       
   312         return retCode;
       
   313     }
       
   314   }
       
   315 
       
   316   if ((retCode = u_n(bitbuf, 1, &vui->chroma_loc_info_present_flag)) < 0)
       
   317     return retCode;
       
   318 
       
   319   if (vui->chroma_loc_info_present_flag) {
       
   320     if ((retCode = ue_v(bitbuf, &vui->chroma_sample_loc_type_top_field, 5)) < 0)
       
   321       return retCode;
       
   322     if ((retCode = ue_v(bitbuf, &vui->chroma_sample_loc_type_bottom_field, 5)) < 0)
       
   323       return retCode;
       
   324   }
       
   325 
       
   326   if ((retCode = u_n(bitbuf, 1, &vui->timing_info_present_flag)) < 0)
       
   327     return retCode;
       
   328 
       
   329   if (vui->timing_info_present_flag) {
       
   330     if ((retCode = u_n(bitbuf, 16, &tempWordHi)) < 0)
       
   331       return retCode;
       
   332     if ((retCode = u_n(bitbuf, 16, &tempWordLo)) < 0)
       
   333       return retCode;
       
   334     vui->num_units_in_tick = (((u_int32)tempWordHi) << 16) | ((u_int32)tempWordLo);
       
   335 
       
   336     if ((retCode = u_n(bitbuf, 16, &tempWordHi)) < 0)
       
   337       return retCode;
       
   338     if ((retCode = u_n(bitbuf, 16, &tempWordLo)) < 0)
       
   339       return retCode;
       
   340     vui->time_scale = (((u_int32)tempWordHi) << 16) | ((u_int32)tempWordLo);
       
   341 
       
   342     if ((retCode = u_n(bitbuf, 1, &vui->fixed_frame_rate_flag)) < 0)
       
   343       return retCode;
       
   344   }
       
   345 
       
   346   if ((retCode = u_n(bitbuf, 1, &vui->nal_hrd_parameters_present_flag)) < 0)
       
   347     return retCode;
       
   348 
       
   349   if (vui->nal_hrd_parameters_present_flag) {
       
   350     if ((retCode = getHrdParameters(bitbuf, &vui->nal_hrd_parameters)) < 0)
       
   351       return retCode;
       
   352   }
       
   353 
       
   354   if ((retCode = u_n(bitbuf, 1, &vui->vcl_hrd_parameters_present_flag)) < 0)
       
   355     return retCode;
       
   356 
       
   357   if (vui->vcl_hrd_parameters_present_flag) {
       
   358     if ((retCode = getHrdParameters(bitbuf, &vui->vcl_hrd_parameters)) < 0)
       
   359       return retCode;
       
   360   }
       
   361 
       
   362   if (vui->nal_hrd_parameters_present_flag || vui->vcl_hrd_parameters_present_flag) {
       
   363     if ((retCode = u_n(bitbuf, 1, &vui->low_delay_hrd_flag)) < 0)
       
   364       return retCode;
       
   365   }
       
   366 
       
   367   if ((retCode = u_n(bitbuf, 1, &vui->pic_struct_present_flag)) < 0)
       
   368     return retCode;
       
   369 
       
   370   if ((retCode = u_n(bitbuf, 1, &vui->bitstream_restriction_flag)) < 0)
       
   371     return retCode;
       
   372 
       
   373   if (vui->bitstream_restriction_flag) {
       
   374     if ((retCode = u_n(bitbuf, 1, &vui->motion_vectors_over_pic_boundaries_flag)) < 0)
       
   375       return retCode;
       
   376     if ((retCode = ue_v(bitbuf, &vui->max_bytes_per_pic_denom, 16)) < 0)
       
   377       return retCode;
       
   378     if ((retCode = ue_v(bitbuf, &vui->max_bits_per_mb_denom, 16)) < 0)
       
   379       return retCode;
       
   380     if ((retCode = ue_v(bitbuf, &vui->log2_max_mv_length_horizontal, 16)) < 0)
       
   381       return retCode;
       
   382     if ((retCode = ue_v(bitbuf, &vui->log2_max_mv_length_vertical, 16)) < 0)
       
   383       return retCode;
       
   384     if ((retCode = ue_v(bitbuf, &vui->num_reorder_frames, 16)) < 0)
       
   385       return retCode;
       
   386     if ((retCode = ue_v(bitbuf, &vui->max_dec_frame_buffering, 16)) < 0)
       
   387       return retCode;
       
   388   }
       
   389 
       
   390   return PS_OK;
       
   391 }
       
   392 
       
   393 
       
   394 /*
       
   395  *
       
   396  * setVUIdefaults:
       
   397  *
       
   398  * Parameters:
       
   399  *      vui                   Pointer to VUI parameters
       
   400  *
       
   401  * Function:
       
   402  *      Set VUI parameters to their default values when default value is non-zero.
       
   403  *
       
   404  * Returns:
       
   405  *      -
       
   406  */
       
   407 static void setVUIdefaults(seq_parameter_set_s *sps)
       
   408 {
       
   409   vui_parameters_s *vui;
       
   410   const level_s *level;
       
   411   int MaxDpbSize;
       
   412 
       
   413   vui = &sps->vui_parameters;
       
   414 
       
   415   vui->video_format                            = 5;
       
   416   vui->colour_primaries                        = 2;
       
   417   vui->transfer_characteristics                = 2;
       
   418   vui->matrix_coefficients                     = 2;
       
   419   vui->motion_vectors_over_pic_boundaries_flag = 1;
       
   420   vui->max_bytes_per_pic_denom                 = 2;
       
   421   vui->max_bits_per_mb_denom                   = 1;
       
   422   vui->log2_max_mv_length_horizontal           = 16;
       
   423   vui->log2_max_mv_length_vertical             = 16;
       
   424 
       
   425   level = getLevel(sps->level_idc, sps->constraint_set3_flag);
       
   426   MaxDpbSize = level->maxDPB /
       
   427     ((sps->pic_width_in_mbs_minus1+1) * (sps->pic_height_in_map_units_minus1+1) * 384);
       
   428   MaxDpbSize = clip(1, 16, MaxDpbSize);
       
   429 
       
   430   vui->max_dec_frame_buffering = MaxDpbSize;
       
   431   vui->num_reorder_frames      = vui->max_dec_frame_buffering;
       
   432 }
       
   433 
       
   434 /*
       
   435  *
       
   436  * psDecodeSPS:
       
   437  *      
       
   438  * Parameters:
       
   439  *      bitbuf                Bitbuffer object
       
   440  *      spsList               The list for SPS's, the newly decoded SPS will be stored into the list
       
   441  *
       
   442  * Function:
       
   443  *      Decode the SPS, and store it into the SPS list
       
   444  *
       
   445  * Returns:
       
   446  *      PS_OK:                SPS decoded succesfully
       
   447  *      <0:                   Fail
       
   448  */
       
   449 int psDecodeSPS( bitbuffer_s *bitbuf, seq_parameter_set_s **spsList, 
       
   450                  TInt& aWidth, TInt& aHeight )
       
   451 {
       
   452   seq_parameter_set_s *sps;
       
   453   unsigned int i;
       
   454   int retCode;
       
   455 
       
   456   unsigned  profile_idc;                                      // u(8)
       
   457   Boolean   constraint_set0_flag;                             // u(1)
       
   458   Boolean   constraint_set1_flag;                             // u(1)
       
   459   Boolean   constraint_set2_flag;                             // u(1)
       
   460   Boolean   constraint_set3_flag;                             // u(1)
       
   461   Boolean   reserved_zero_4bits;                              // u(4)
       
   462   unsigned  level_idc;                                        // u(8)
       
   463   unsigned  seq_parameter_set_id;                             // ue(v)
       
   464 
       
   465 
       
   466   /*
       
   467    * Parse sequence parameter set syntax until sps id
       
   468    */
       
   469 
       
   470   if ((retCode = u_n(bitbuf, 8, &profile_idc)) < 0)
       
   471     return retCode;
       
   472 
       
   473   /* If constraint_set0_flag == 1, stream is Baseline Profile compliant */
       
   474   if ((retCode = u_n(bitbuf, 1, &constraint_set0_flag)) < 0)
       
   475     return retCode;
       
   476 
       
   477   /* If constraint_set1_flag == 1, stream is Main Profile compliant */
       
   478   if ((retCode = u_n(bitbuf, 1, &constraint_set1_flag)) < 0)
       
   479     return retCode;
       
   480 
       
   481   /* If constraint_set2_flag == 1, stream is Extended Profile compliant */
       
   482   if ((retCode = u_n(bitbuf, 1, &constraint_set2_flag)) < 0)
       
   483     return retCode;
       
   484 
       
   485   if ((retCode = u_n(bitbuf, 1, &constraint_set3_flag)) < 0)
       
   486     return retCode;
       
   487 
       
   488   /* If CABAC is not defined we support only baseline compliant streams */
       
   489 #ifndef ENABLE_CABAC
       
   490   if (profile_idc != PS_BASELINE_PROFILE_IDC && constraint_set0_flag == 0)
       
   491     return PS_ERR_UNSUPPORTED_PROFILE;
       
   492 #else
       
   493   if (profile_idc != PS_BASELINE_PROFILE_IDC && constraint_set0_flag == 0 &&
       
   494       profile_idc != PS_MAIN_PROFILE_IDC && constraint_set1_flag == 0)
       
   495     return PS_ERR_UNSUPPORTED_PROFILE;
       
   496 #endif
       
   497 
       
   498   /* We don't care what is in these bits */
       
   499   if ((retCode = u_n(bitbuf, 4, &reserved_zero_4bits)) < 0)
       
   500     return retCode;
       
   501 
       
   502   /* Fetch level */
       
   503   if ((retCode = u_n(bitbuf, 8, &level_idc)) < 0)
       
   504     return retCode;
       
   505 
       
   506   /* Find level in the list of legal levels */
       
   507   for (i = 0; i < NUM_LEVELS; i++) {
       
   508     if ((int)level_idc == levelArray[i].levelNumber)
       
   509       break;
       
   510   }
       
   511 
       
   512   /* If level was not found in the list, return with error */
       
   513   if (i == NUM_LEVELS)
       
   514     return PS_ERR_ILLEGAL_VALUE;
       
   515 
       
   516   /* Get sequence parameter set id */
       
   517   if ((retCode = ue_v(bitbuf, &seq_parameter_set_id, PS_MAX_NUM_OF_SPS-1)) < 0)
       
   518     return retCode;
       
   519 
       
   520 
       
   521   /*
       
   522    * Allocate memory for SPS
       
   523    */
       
   524 
       
   525   /* Pointer to sequence parameter set structure */
       
   526   sps = spsList[seq_parameter_set_id];
       
   527 
       
   528   /* allocate mem for SPS, if it has not been allocated already */
       
   529   if (!sps) {
       
   530     sps = (seq_parameter_set_s *) User::Alloc(sizeof(seq_parameter_set_s));    
       
   531     if (sps == 0) {
       
   532       return PS_ERR_MEM_ALLOC;
       
   533     }
       
   534     memset( sps, 0, sizeof(seq_parameter_set_s));
       
   535     spsList[seq_parameter_set_id] = sps;
       
   536   }
       
   537 
       
   538 
       
   539   /* Copy temporary variables to sequence parameter set structure */
       
   540   sps->profile_idc          = profile_idc;
       
   541   sps->constraint_set0_flag = constraint_set0_flag;
       
   542   sps->constraint_set1_flag = constraint_set1_flag;
       
   543   sps->constraint_set2_flag = constraint_set2_flag;
       
   544   sps->constraint_set3_flag = constraint_set3_flag;
       
   545   sps->reserved_zero_4bits  = reserved_zero_4bits;
       
   546   sps->level_idc            = level_idc;
       
   547   sps->seq_parameter_set_id = seq_parameter_set_id;
       
   548 
       
   549 
       
   550   /*
       
   551    * Parse rest of the sequence parameter set syntax
       
   552    */
       
   553 
       
   554   /* This defines how many bits there are in frame_num syntax element */
       
   555   if ((retCode = ue_v(bitbuf, &sps->log2_max_frame_num_minus4, 12)) < 0)
       
   556     return retCode;
       
   557 
       
   558   /* Fetch POC type */
       
   559   if ((retCode = ue_v(bitbuf, &sps->pic_order_cnt_type, 2)) < 0)
       
   560     return retCode;
       
   561 
       
   562   if (sps->pic_order_cnt_type == 0) {
       
   563     if ((retCode = ue_v(bitbuf, &sps->log2_max_pic_order_cnt_lsb_minus4, 12)) < 0)
       
   564       return retCode;
       
   565   }
       
   566   else if (sps->pic_order_cnt_type == 1) {
       
   567     if ((retCode = u_n(bitbuf, 1, &sps->delta_pic_order_always_zero_flag)) < 0)
       
   568       return retCode;
       
   569 
       
   570     if ((retCode = se_v_long(bitbuf, &sps->offset_for_non_ref_pic)) < 0)
       
   571       return retCode;
       
   572 
       
   573     if ((retCode = se_v_long(bitbuf, &sps->offset_for_top_to_bottom_field)) < 0)
       
   574       return retCode;
       
   575 
       
   576     if ((retCode = ue_v(bitbuf, &sps->num_ref_frames_in_pic_order_cnt_cycle, 255)) < 0)
       
   577       return retCode;
       
   578 
       
   579     for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++) {
       
   580       if ((retCode = se_v_long(bitbuf, &sps->offset_for_ref_frame[i])) < 0)
       
   581         return retCode;
       
   582     }
       
   583   }
       
   584 
       
   585   if ((retCode = ue_v(bitbuf, &sps->num_ref_frames, 16)) < 0)
       
   586     return retCode;
       
   587 
       
   588   if ((retCode = u_n(bitbuf, 1, &sps->gaps_in_frame_num_value_allowed_flag)) < 0)
       
   589     return retCode;
       
   590 
       
   591   if ((retCode = ue_v(bitbuf, &sps->pic_width_in_mbs_minus1, MAX_PIC_WIDTH_IN_MBS-1)) < 0)
       
   592     return retCode;
       
   593   
       
   594   aWidth = (sps->pic_width_in_mbs_minus1 + 1) * 16;
       
   595 
       
   596   if ((retCode = ue_v(bitbuf, &sps->pic_height_in_map_units_minus1, MAX_PIC_WIDTH_IN_MBS-1)) < 0)
       
   597     return retCode;
       
   598   
       
   599   aHeight = (sps->pic_height_in_map_units_minus1 + 1) * 16;
       
   600 
       
   601   if ((retCode = u_n(bitbuf, 1, &sps->frame_mbs_only_flag)) < 0)
       
   602     return retCode;
       
   603 
       
   604   if (!sps->frame_mbs_only_flag) {
       
   605     // u_n(bitbuf, 1, &sps->mb_adaptive_frame_field_flag);
       
   606     return PS_ERR_UNSUPPORTED_FEATURE;
       
   607   }
       
   608 
       
   609   if ((retCode = u_n(bitbuf, 1, &sps->direct_8x8_inference_flag)) < 0)
       
   610     return retCode;
       
   611 
       
   612   if ((retCode = u_n(bitbuf, 1, &sps->frame_cropping_flag)) < 0)
       
   613     return retCode;
       
   614 
       
   615   /* Fetch cropping window */
       
   616   if (sps->frame_cropping_flag) {
       
   617     if ((retCode = ue_v(bitbuf, &sps->frame_crop_left_offset, 8*(sps->pic_width_in_mbs_minus1+1)-1)) < 0)
       
   618       return retCode;
       
   619 
       
   620     if ((retCode = ue_v(bitbuf, &sps->frame_crop_right_offset, 8*(sps->pic_width_in_mbs_minus1+1)-sps->frame_crop_left_offset-1)) < 0)
       
   621       return retCode;
       
   622 
       
   623     if ((retCode = ue_v(bitbuf, &sps->frame_crop_top_offset, 8*(sps->pic_height_in_map_units_minus1+1)-1)) < 0)
       
   624       return retCode;
       
   625 
       
   626     if ((retCode = ue_v(bitbuf, &sps->frame_crop_bottom_offset, 8*(sps->pic_height_in_map_units_minus1+1)-sps->frame_crop_top_offset-1)) < 0)
       
   627       return retCode;
       
   628     
       
   629     TInt cropUnitX = 2;
       
   630     TInt cropUnitY = 2 * ( 2 - sps->frame_mbs_only_flag );
       
   631     
       
   632     TInt leftBorder = cropUnitX * sps->frame_crop_left_offset;
       
   633     TInt rightBorder = aWidth - ( cropUnitX * sps->frame_crop_right_offset );
       
   634     
       
   635     aWidth = rightBorder - leftBorder;
       
   636     
       
   637     TInt topBorder = cropUnitY * sps->frame_crop_top_offset;
       
   638     TInt bottomBorder = ( 16 * (sps->pic_height_in_map_units_minus1 + 1) ) -
       
   639                         cropUnitY * sps->frame_crop_bottom_offset;
       
   640                         
       
   641     aHeight = bottomBorder - topBorder;
       
   642     
       
   643   }
       
   644 
       
   645   if ((retCode = u_n(bitbuf, 1, &sps->vui_parameters_present_flag)) < 0)
       
   646     return retCode;
       
   647 
       
   648   setVUIdefaults(sps);
       
   649 
       
   650   if (sps->vui_parameters_present_flag) {
       
   651     if ((retCode = getVUI(bitbuf, &sps->vui_parameters)) < 0)
       
   652       return retCode;
       
   653   }
       
   654 
       
   655   if (bibSkipTrailingBits(bitbuf) < 0)
       
   656     return PS_ERROR;
       
   657 
       
   658   return PS_OK;
       
   659 }
       
   660 
       
   661 /*
       
   662  *
       
   663  * psCloseParametersSets:
       
   664  *
       
   665  * Parameters:
       
   666  *      spsList               The sequence parameter set list
       
   667  *      ppsList               The picture parameter set list
       
   668  *
       
   669  * Fucntion:
       
   670  *      Free all parameter sets
       
   671  *
       
   672  * Returns:
       
   673  *      -
       
   674  */
       
   675 void psCloseParametersSets(seq_parameter_set_s **spsList,
       
   676                            pic_parameter_set_s **ppsList)
       
   677 {
       
   678   int i;
       
   679 
       
   680   for (i = 0; i < PS_MAX_NUM_OF_SPS; i++) {
       
   681     psCloseSPS(spsList[i]);
       
   682     spsList[i] = 0;
       
   683   }
       
   684 
       
   685   for (i = 0; i < PS_MAX_NUM_OF_PPS; i++) {
       
   686     psClosePPS(ppsList[i]);
       
   687     ppsList[i] = 0;
       
   688   }
       
   689 }
       
   690 
       
   691 /*
       
   692  *
       
   693  * psClosePPS:
       
   694  *
       
   695  * Parameters:
       
   696  *      pps                   the picture parameter set to be freed
       
   697  *
       
   698  * Function:
       
   699  *      free the picture parameter set
       
   700  *
       
   701  * Returns:
       
   702  *      -
       
   703  */
       
   704 void psClosePPS( pic_parameter_set_s *pps )
       
   705 {
       
   706   if (pps == 0)
       
   707     return;
       
   708   
       
   709   	// [KW]: Added
       
   710   	if (pps->codedPPSBuffer)
       
   711 	  	User::Free(pps->codedPPSBuffer);
       
   712 
       
   713 
       
   714   if (pps->slice_group_id)
       
   715     User::Free(pps->slice_group_id);
       
   716 //    nccFree(pps->slice_group_id);
       
   717 
       
   718 //  nccFree(pps);
       
   719 	User::Free(pps);
       
   720 }
       
   721 
       
   722 
       
   723 /*
       
   724  *
       
   725  * psCloseSPS:
       
   726  *
       
   727  * Parameters:
       
   728  *      sps                   the sequence parameter set to be freed
       
   729  *
       
   730  * Fucntion:
       
   731  *      free the sequence parameter set
       
   732  *
       
   733  * Returns:
       
   734  *      -
       
   735  */
       
   736 void psCloseSPS( seq_parameter_set_s *sps )
       
   737 {
       
   738   if (sps == 0)
       
   739     return;
       
   740   	
       
   741   	// [KW]: Added
       
   742   	if (sps->codedSPSBuffer)
       
   743 	  	User::Free(sps->codedSPSBuffer);
       
   744 
       
   745 //  nccFree(sps);
       
   746   User::Free(sps);
       
   747 }
       
   748 
       
   749 
       
   750 // psParseLevelFromSPS
       
   751 // Returns the baseline profile level from SPS
       
   752 TInt psParseLevelFromSPS( bitbuffer_s *bitbuf, TInt& aLevel )
       
   753 {
       
   754   	TInt retCode;
       
   755   	TUint profile_idc;                                      // u(8)
       
   756   	Boolean constraint_set0_flag;                          // u(1)
       
   757   	Boolean constraint_set1_flag;                          // u(1)
       
   758   	Boolean constraint_set2_flag;                          // u(1)
       
   759   	Boolean constraint_set3_flag;                          // u(1)
       
   760   	Boolean reserved_zero_4bits;                           // u(4)
       
   761   	TUint level_idc;                                        // u(8)      
       
   762   
       
   763   	// Parse sequence parameter set syntax until sps id
       
   764   	if ((retCode = u_n(bitbuf, 8, &profile_idc)) < 0)
       
   765     	return retCode;
       
   766 
       
   767   	// If constraint_set0_flag == 1, stream is Baseline Profile compliant 
       
   768   	if ((retCode = u_n(bitbuf, 1, &constraint_set0_flag)) < 0)
       
   769     	return retCode;
       
   770 
       
   771   	// If constraint_set1_flag == 1, stream is Main Profile compliant 
       
   772   	if ((retCode = u_n(bitbuf, 1, &constraint_set1_flag)) < 0)
       
   773     	return retCode;
       
   774 
       
   775   	// If constraint_set2_flag == 1, stream is Extended Profile compliant 
       
   776   	if ((retCode = u_n(bitbuf, 1, &constraint_set2_flag)) < 0)
       
   777     	return retCode;
       
   778 
       
   779   	if ((retCode = u_n(bitbuf, 1, &constraint_set3_flag)) < 0)
       
   780     	return retCode;
       
   781 
       
   782   	// If CABAC is not defined we support only baseline compliant streams 
       
   783 	if (profile_idc != PS_BASELINE_PROFILE_IDC && constraint_set0_flag == 0)
       
   784    		return PS_ERR_UNSUPPORTED_PROFILE;
       
   785 
       
   786   	// We don't care what is in these bits 
       
   787   	if ((retCode = u_n(bitbuf, 4, &reserved_zero_4bits)) < 0)
       
   788     	return retCode;
       
   789 
       
   790   	// Fetch level 
       
   791   	if ((retCode = u_n(bitbuf, 8, &level_idc)) < 0)
       
   792     	return retCode;
       
   793   
       
   794   	aLevel = level_idc;
       
   795   
       
   796   	if ( level_idc == 11 && constraint_set3_flag == 1 )
       
   797     	aLevel = 101;  // level 1b
       
   798   
       
   799   	return KErrNone;
       
   800 }
       
   801 
       
   802 
       
   803 // AddBytesToBuffer
       
   804 // Adds aNumBytes bytes to bit buffer aBitBuffer, reallocates the bit buffer data if necessary.
       
   805 TInt AddBytesToBuffer(bitbuffer_s *aBitBuffer, TUint aNumBytes)
       
   806 {
       
   807 	TInt i;
       
   808 	
       
   809 	// Reallocate the bitbuffer data
       
   810 	aBitBuffer->data = (TUint8 *) User::ReAlloc(aBitBuffer->data, (aBitBuffer->dataLen+aNumBytes));
       
   811 
       
   812 	if (aBitBuffer->data == 0)
       
   813         return KErrNoMemory;
       
   814 
       
   815 	// Set the new bytes as zeros
       
   816 	for (i=aBitBuffer->dataLen; i<aBitBuffer->dataLen+aNumBytes; i++)
       
   817 	{
       
   818 		aBitBuffer->data[i] = 0;
       
   819 	}
       
   820 
       
   821 	return KErrNone;
       
   822 }
       
   823 
       
   824 
       
   825 #ifdef VIDEOEDITORENGINE_AVC_EDITING
       
   826 
       
   827 
       
   828 /*
       
   829  *
       
   830  * psGetAspectRatio:
       
   831  *      
       
   832  * Parameters:
       
   833  *      sps                   Sequence parameter set
       
   834  *      width                 Horizontal size of the sample aspect ratio
       
   835  *      height                Vertical size of the sample aspect ratio
       
   836  *
       
   837  * Function:
       
   838  *      Return sample aspect ratio in width and height
       
   839  *
       
   840  * Returns:
       
   841  *      -
       
   842  */
       
   843 void psGetAspectRatio(seq_parameter_set_s *sps, int *width, int *height)
       
   844 {
       
   845   vui_parameters_s *vui;
       
   846 
       
   847   vui = &sps->vui_parameters;
       
   848 
       
   849   *width  = 0;
       
   850   *height = 0;
       
   851 
       
   852   if (sps->vui_parameters_present_flag &&
       
   853       vui->aspect_ratio_info_present_flag &&
       
   854       vui->aspect_ratio_idc != 0 &&
       
   855       (vui->aspect_ratio_idc <= 13 || vui->aspect_ratio_idc == 255))
       
   856   {
       
   857     if (vui->aspect_ratio_idc == 255) {
       
   858       /* Extended_SAR */
       
   859       if (vui->sar_width != 0 && vui->sar_height != 0) {
       
   860         *width  = vui->sar_width;
       
   861         *height = vui->sar_height;
       
   862       }
       
   863     }
       
   864     else {
       
   865       *width  = aspectRatioArr[vui->aspect_ratio_idc-1].width;
       
   866       *height = aspectRatioArr[vui->aspect_ratio_idc-1].height;
       
   867     }
       
   868   }
       
   869 
       
   870 }
       
   871 
       
   872 
       
   873 // CompareSPSSets
       
   874 // Compares two SPS input sets to see if we can use one for both clips, if exact match is not required
       
   875 // then some parameters maybe be different in the two sets. 
       
   876 TInt CompareSPSSets( seq_parameter_set_s *aSPSSet1, seq_parameter_set_s *aSPSSet2, TBool aExactMatch )
       
   877 {
       
   878 	TUint i;
       
   879 
       
   880 	// Different maxFrameNum & maxPOCLsb can be handled by modifying the slice header, thus do not return EFalse
       
   881 	if ( aExactMatch )
       
   882 	{
       
   883 		// If exact match is required, return false for different max frame number value
       
   884 		if ( aSPSSet1->log2_max_frame_num_minus4 != aSPSSet2->log2_max_frame_num_minus4 )
       
   885 			return EFalse;	
       
   886 	}
       
   887 	
       
   888 	if ( aSPSSet1->pic_order_cnt_type != aSPSSet2->pic_order_cnt_type )
       
   889 	{
       
   890 		return EFalse;
       
   891 	}
       
   892 	else
       
   893 	{
       
   894 	  	if (aSPSSet1->pic_order_cnt_type == 0) 
       
   895   		{
       
   896 			// Different maxFrameNum & maxPOCLsb can be handled by modifying the slice header, thus do not return EFalse
       
   897 			if ( aExactMatch )
       
   898 			{
       
   899 				// If exact match is required, return false for different max POCLSB number value
       
   900 				if ( aSPSSet1->log2_max_pic_order_cnt_lsb_minus4 != aSPSSet2->log2_max_pic_order_cnt_lsb_minus4 )
       
   901 					return EFalse;	
       
   902 			}
       
   903 	
       
   904   		}
       
   905 	  	else if (aSPSSet1->pic_order_cnt_type == 1) 
       
   906   		{
       
   907 			if ( aSPSSet1->delta_pic_order_always_zero_flag != aSPSSet2->delta_pic_order_always_zero_flag ||
       
   908 				 aSPSSet1->offset_for_non_ref_pic != aSPSSet2->offset_for_non_ref_pic ||
       
   909 				 aSPSSet1->num_ref_frames_in_pic_order_cnt_cycle != aSPSSet2->num_ref_frames_in_pic_order_cnt_cycle )
       
   910 			{
       
   911 				return EFalse;
       
   912 			}
       
   913 
       
   914 	    	for (i = 0; i < aSPSSet1->num_ref_frames_in_pic_order_cnt_cycle; i++) 
       
   915     		{
       
   916 				if ( aSPSSet1->offset_for_ref_frame[i] != aSPSSet2->offset_for_ref_frame[i] )
       
   917 				{
       
   918 					return EFalse;
       
   919 				}
       
   920     		}
       
   921   		}
       
   922 	}
       
   923 
       
   924 	if ( aSPSSet1->num_ref_frames != aSPSSet2->num_ref_frames )
       
   925 	{
       
   926 		return EFalse;
       
   927 	}
       
   928 	
       
   929 	// Direct 8x8 inference flag is not used in baseline, ignore
       
   930 
       
   931 	return ETrue;
       
   932 }
       
   933 
       
   934 
       
   935 // IsSPSSupported
       
   936 // Checks if the input SPS contains supported values. Returns KErrNotSupported if 
       
   937 // unsupported parameters are found.
       
   938 TInt IsSPSSupported( seq_parameter_set_s *aSPS )
       
   939 {
       
   940 
       
   941 	// Only Baseline profile supported at the moment
       
   942 	if ( aSPS->profile_idc != PS_BASELINE_PROFILE_IDC )
       
   943 	{
       
   944 		return KErrNotSupported;
       
   945 	}
       
   946 	
       
   947 	// Check if maximum supported level is exceeded
       
   948 	if ( aSPS->level_idc > PS_MAX_SUPPORTED_LEVEL )
       
   949 	{
       
   950 		return KErrNotSupported;
       
   951 	}
       
   952 	
       
   953 	// For now more than one reference frames are not supported
       
   954 	if ( aSPS->num_ref_frames > 1 )
       
   955 	{
       
   956 		return KErrNotSupported;
       
   957 	}
       
   958 	
       
   959 	// Coded fields are not supported
       
   960 	if ( !aSPS->frame_mbs_only_flag )
       
   961 	{
       
   962 		return KErrNotSupported;
       
   963 	}
       
   964 	
       
   965 	if ( aSPS->vui_parameters_present_flag )
       
   966 	{
       
   967 		if ( aSPS->vui_parameters.num_reorder_frames != 0 && aSPS->pic_order_cnt_type != 2)
       
   968 		{
       
   969 			// Since we can't be sure how many input frames we have to buffer before getting 
       
   970 			// an output picture, return KErrNotSupported
       
   971 			return KErrNotSupported;
       
   972 		}
       
   973 	}
       
   974 	else
       
   975 	{
       
   976 		if ( aSPS->pic_order_cnt_type != 2)
       
   977 		{
       
   978 			// Since we can't be sure how many input frames we have to buffer before getting 
       
   979 			// an output picture, return KErrNotSupported
       
   980 			return KErrNotSupported;
       
   981 		}
       
   982 	}
       
   983 	
       
   984 	return KErrNone;
       
   985 }
       
   986 
       
   987 
       
   988 // IsPPSSupported
       
   989 // Checks if the input PPS contains supported values. Returns KErrNotSupported if 
       
   990 // unsupported parameters are found.
       
   991 TInt IsPPSSupported( pic_parameter_set_s *aPPS )
       
   992 {
       
   993 
       
   994 	// For baseline, both prediction values shall be zero
       
   995 	if( aPPS->weighted_pred_flag != 0 || aPPS->weighted_bipred_idc != 0)
       
   996 	{
       
   997 		return KErrNotSupported;
       
   998 	}
       
   999 	
       
  1000 	// For baseline, entropy coding mode shall be zero
       
  1001 	if ( aPPS->entropy_coding_mode_flag != 0 )
       
  1002 	{
       
  1003 		return KErrNotSupported;
       
  1004 	}
       
  1005 	
       
  1006 	if ( aPPS->num_slice_groups_minus1 > PS_BASELINE_MAX_SLICE_GROUPS )
       
  1007 	{
       
  1008 		return KErrNotSupported;
       
  1009 	}
       
  1010 
       
  1011 	return KErrNone;
       
  1012 }
       
  1013 
       
  1014 
       
  1015 // psParseSPS
       
  1016 // Parses the input SPS set. Modifies the SPS id if a conflicting id is found 
       
  1017 // and stores the modified data to codedSPSBuffer for later use.
       
  1018 TInt psParseSPS( bitbuffer_s *bitbuf, seq_parameter_set_s **spsList, TUint aFrameFromEncoder, TBool *aEncodeUntilIDR, TUint *aNumSPS )
       
  1019 {
       
  1020   	seq_parameter_set_s *sps;
       
  1021   	TUint i;
       
  1022   	TInt retCode;
       
  1023   	TUint bitPosit = 0;
       
  1024   	TUint bytePosit = 0;
       
  1025   	TUint profile_idc;                                      // u(8)
       
  1026   	Boolean constraint_set0_flag;                          // u(1)
       
  1027   	Boolean constraint_set1_flag;                          // u(1)
       
  1028   	Boolean constraint_set2_flag;                          // u(1)
       
  1029   	Boolean constraint_set3_flag;                          // u(1)
       
  1030   	Boolean reserved_zero_4bits;                           // u(4)
       
  1031   	TUint level_idc;                                        // u(8)
       
  1032   	TUint seq_parameter_set_id;                             // ue(v)
       
  1033   	TUint newSPSId = 0;
       
  1034   	TUint possibleIdConflict = 0;
       
  1035   	TUint useOneSPS = 0;
       
  1036 
       
  1037 
       
  1038   	if (!aFrameFromEncoder)
       
  1039   	{	
       
  1040 	  	// Reset the encode until IDR flag if this SPS is not from the encoder.
       
  1041   		*aEncodeUntilIDR = EFalse;
       
  1042   	}
       
  1043 
       
  1044   	// Parse sequence parameter set syntax until sps id
       
  1045   	if ((retCode = u_n(bitbuf, 8, &profile_idc)) < 0)
       
  1046     	return retCode;
       
  1047 	
       
  1048   	// If constraint_set0_flag == 1, stream is Baseline Profile compliant 
       
  1049   	if ((retCode = u_n(bitbuf, 1, &constraint_set0_flag)) < 0)
       
  1050     	return retCode;
       
  1051 
       
  1052   	// If constraint_set1_flag == 1, stream is Main Profile compliant 
       
  1053   	if ((retCode = u_n(bitbuf, 1, &constraint_set1_flag)) < 0)
       
  1054     	return retCode;
       
  1055 
       
  1056   	// If constraint_set2_flag == 1, stream is Extended Profile compliant 
       
  1057   	if ((retCode = u_n(bitbuf, 1, &constraint_set2_flag)) < 0)
       
  1058     	return retCode;
       
  1059 
       
  1060   	if ((retCode = u_n(bitbuf, 1, &constraint_set3_flag)) < 0)
       
  1061     	return retCode;
       
  1062 
       
  1063   	// If CABAC is not defined we support only baseline compliant streams 
       
  1064 	if (profile_idc != PS_BASELINE_PROFILE_IDC && constraint_set0_flag == 0)
       
  1065    		return PS_ERR_UNSUPPORTED_PROFILE;
       
  1066 
       
  1067   	// We don't care what is in these bits 
       
  1068   	if ((retCode = u_n(bitbuf, 4, &reserved_zero_4bits)) < 0)
       
  1069     	return retCode;
       
  1070 
       
  1071   	// Fetch level 
       
  1072   	if ((retCode = u_n(bitbuf, 8, &level_idc)) < 0)
       
  1073     	return retCode;
       
  1074 
       
  1075   	// Find level in the list of legal levels 
       
  1076   	for (i = 0; i < NUM_LEVELS; i++) 
       
  1077   	{
       
  1078     	if ((int)level_idc == levelArray[i].levelNumber)
       
  1079       		break;
       
  1080   	}
       
  1081 
       
  1082   	// If level was not found in the list, return with error 
       
  1083   	if (i == NUM_LEVELS)
       
  1084     	return PS_ERR_ILLEGAL_VALUE;
       
  1085 
       
  1086   	// Get sequence parameter set id 
       
  1087   	if ((retCode = ue_v(bitbuf, &seq_parameter_set_id, PS_MAX_NUM_OF_SPS-1)) < 0)
       
  1088     	return retCode;
       
  1089 
       
  1090   	// Pointer to sequence parameter set structure 
       
  1091   	sps = spsList[seq_parameter_set_id];
       
  1092 
       
  1093   	// Allocate memory for SPS, if it has not been allocated already 
       
  1094   	if (!sps) 
       
  1095   	{
       
  1096     	sps = (seq_parameter_set_s *) User::Alloc(sizeof(seq_parameter_set_s));
       
  1097     	
       
  1098     	if (sps == 0) 
       
  1099     	{
       
  1100 			PRINT((_L("Error while allocating memory for SPS.\n")));
       
  1101       			return PS_ERR_MEM_ALLOC;
       
  1102     	}
       
  1103     	
       
  1104     	memset( sps, 0, sizeof(seq_parameter_set_s));
       
  1105     	spsList[seq_parameter_set_id] = sps;
       
  1106 
       
  1107   		sps->seq_parameter_set_id = seq_parameter_set_id;
       
  1108   		(*aNumSPS)++;
       
  1109   	}
       
  1110   	else
       
  1111   	{
       
  1112   		// There might be a conflicting Id with an existing SPS set
       
  1113   		// Give the new SPS set the next free SPS Id
       
  1114   		possibleIdConflict = 1;
       
  1115   		newSPSId = 0;
       
  1116   		useOneSPS = 1;
       
  1117   		
       
  1118   		// Search for the first free SPS id
       
  1119   		while (spsList[newSPSId])
       
  1120   		{
       
  1121   			newSPSId++;
       
  1122   		}
       
  1123   	
       
  1124   		// And allocate memory for the SPS
       
  1125     	sps = (seq_parameter_set_s *) User::Alloc(sizeof(seq_parameter_set_s));
       
  1126     	
       
  1127     	if (sps == 0) 
       
  1128     	{
       
  1129       		PRINT((_L("Error while allocating memory for SPS.\n")));
       
  1130       			return PS_ERR_MEM_ALLOC;
       
  1131     	}
       
  1132     
       
  1133     	memset( sps, 0, sizeof(seq_parameter_set_s));
       
  1134     
       
  1135     	sps->seq_parameter_set_id = newSPSId;
       
  1136 
       
  1137   		// Store the position of the bit buffer
       
  1138   		bitPosit = bitbuf->bitpos;
       
  1139   		bytePosit = bitbuf->bytePos;
       
  1140   	}
       
  1141 
       
  1142 
       
  1143   	// Copy temporary variables to sequence parameter set structure 
       
  1144   	sps->profile_idc          = profile_idc;
       
  1145   	sps->constraint_set0_flag = constraint_set0_flag;
       
  1146   	sps->constraint_set1_flag = constraint_set1_flag;
       
  1147   	sps->constraint_set2_flag = constraint_set2_flag;
       
  1148   	sps->constraint_set3_flag = constraint_set3_flag;
       
  1149   	sps->reserved_zero_4bits  = reserved_zero_4bits;
       
  1150   	sps->level_idc            = level_idc;
       
  1151 
       
  1152 	// Initialize
       
  1153 	sps->maxFrameNumChanged = 0;
       
  1154 	sps->maxPOCNumChanged = 0;
       
  1155 	
       
  1156   	// This defines how many bits there are in frame_num syntax element 
       
  1157   	if ((retCode = ue_v(bitbuf, &sps->log2_max_frame_num_minus4, 12)) < 0)
       
  1158     	return retCode;
       
  1159 
       
  1160   	// Fetch POC type 
       
  1161   	if ((retCode = ue_v(bitbuf, &sps->pic_order_cnt_type, 2)) < 0)
       
  1162     	return retCode;
       
  1163 
       
  1164   	if (sps->pic_order_cnt_type == 0) 
       
  1165   	{
       
  1166     	if ((retCode = ue_v(bitbuf, &sps->log2_max_pic_order_cnt_lsb_minus4, 12)) < 0)
       
  1167       		return retCode;
       
  1168   	}
       
  1169   	else if (sps->pic_order_cnt_type == 1) 
       
  1170   	{
       
  1171     	if ((retCode = u_n(bitbuf, 1, &sps->delta_pic_order_always_zero_flag)) < 0)
       
  1172       		return retCode;
       
  1173 
       
  1174     	if ((retCode = se_v_long(bitbuf, &sps->offset_for_non_ref_pic)) < 0)
       
  1175       		return retCode;
       
  1176 
       
  1177     	if ((retCode = se_v_long(bitbuf, &sps->offset_for_top_to_bottom_field)) < 0)
       
  1178       		return retCode;
       
  1179 
       
  1180     	if ((retCode = ue_v(bitbuf, &sps->num_ref_frames_in_pic_order_cnt_cycle, 255)) < 0)
       
  1181       		return retCode;
       
  1182 
       
  1183     	for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++) 
       
  1184     	{
       
  1185       		if ((retCode = se_v_long(bitbuf, &sps->offset_for_ref_frame[i])) < 0)
       
  1186         		return retCode;
       
  1187     	}
       
  1188   	}
       
  1189 
       
  1190   	if ((retCode = ue_v(bitbuf, &sps->num_ref_frames, 16)) < 0)
       
  1191     	return retCode;
       
  1192 
       
  1193   	if ((retCode = u_n(bitbuf, 1, &sps->gaps_in_frame_num_value_allowed_flag)) < 0)
       
  1194     	return retCode;
       
  1195 
       
  1196   	if ((retCode = ue_v(bitbuf, &sps->pic_width_in_mbs_minus1, MAX_PIC_WIDTH_IN_MBS-1)) < 0)
       
  1197     	return retCode;
       
  1198 
       
  1199   	if ((retCode = ue_v(bitbuf, &sps->pic_height_in_map_units_minus1, MAX_PIC_WIDTH_IN_MBS-1)) < 0)
       
  1200     	return retCode;
       
  1201 
       
  1202   	if ((retCode = u_n(bitbuf, 1, &sps->frame_mbs_only_flag)) < 0)
       
  1203     	return retCode;
       
  1204 
       
  1205   	if (!sps->frame_mbs_only_flag) 
       
  1206   	{
       
  1207     	return PS_ERR_UNSUPPORTED_FEATURE;
       
  1208   	}
       
  1209 
       
  1210   	if ((retCode = u_n(bitbuf, 1, &sps->direct_8x8_inference_flag)) < 0)
       
  1211     	return retCode;
       
  1212 
       
  1213   	if ((retCode = u_n(bitbuf, 1, &sps->frame_cropping_flag)) < 0)
       
  1214     	return retCode;
       
  1215 
       
  1216   	// Fetch cropping window 
       
  1217   	if (sps->frame_cropping_flag) 
       
  1218   	{
       
  1219     	if ((retCode = ue_v(bitbuf, &sps->frame_crop_left_offset, 8*(sps->pic_width_in_mbs_minus1+1)-1)) < 0)
       
  1220       		return retCode;
       
  1221 
       
  1222     	if ((retCode = ue_v(bitbuf, &sps->frame_crop_right_offset, 8*(sps->pic_width_in_mbs_minus1+1)-sps->frame_crop_left_offset-1)) < 0)
       
  1223       		return retCode;
       
  1224 
       
  1225     	if ((retCode = ue_v(bitbuf, &sps->frame_crop_top_offset, 8*(sps->pic_height_in_map_units_minus1+1)-1)) < 0)
       
  1226       		return retCode;
       
  1227 
       
  1228     	if ((retCode = ue_v(bitbuf, &sps->frame_crop_bottom_offset, 8*(sps->pic_height_in_map_units_minus1+1)-sps->frame_crop_top_offset-1)) < 0)
       
  1229       		return retCode;
       
  1230   	}
       
  1231 
       
  1232   	if ((retCode = u_n(bitbuf, 1, &sps->vui_parameters_present_flag)) < 0)
       
  1233     	return retCode;
       
  1234 
       
  1235   	setVUIdefaults(sps);
       
  1236 
       
  1237   	if (sps->vui_parameters_present_flag) 
       
  1238   	{
       
  1239     	if ((retCode = getVUI(bitbuf, &sps->vui_parameters)) < 0)
       
  1240       		return retCode;
       
  1241   	}
       
  1242 
       
  1243   	if (bibSkipTrailingBits(bitbuf) < 0)
       
  1244     	return PS_ERROR;
       
  1245   
       
  1246   	// Store the size of the SPS set
       
  1247   	sps->SPSlength = bitbuf->bytePos;
       
  1248   
       
  1249 	syncBitBufferBitpos(bitbuf);
       
  1250 	
       
  1251   	// If we had a possible conflict, compare the SPS sets with the same id to see if only one SPS set can be used.
       
  1252   	if (possibleIdConflict)
       
  1253   	{
       
  1254   		// Check if one SPS can be used instead of two separate ones
       
  1255 	  	useOneSPS = CompareSPSSets(spsList[seq_parameter_set_id],sps,EFalse);
       
  1256 	  	
       
  1257 	  	if (!useOneSPS)
       
  1258 	  	{
       
  1259   			TUint trailingBits = GetNumTrailingBits(bitbuf);
       
  1260   			TInt diff = 0;
       
  1261 			TUint oldSPSId = seq_parameter_set_id;
       
  1262 			TUint oldIdLength = ReturnUnsignedExpGolombCodeLength(oldSPSId);
       
  1263 			TUint newIdLength = ReturnUnsignedExpGolombCodeLength(newSPSId);
       
  1264 			TUint storeSPS = 1;
       
  1265 		
       
  1266   			for (i=0; i<PS_MAX_NUM_OF_SPS; i++)
       
  1267   			{
       
  1268 			  	// Check if an older SPS matches exactly this one, then use the old
       
  1269   				if (spsList[i])
       
  1270 	  				useOneSPS = CompareSPSSets(spsList[i],sps,ETrue);
       
  1271   			
       
  1272 	  			if (useOneSPS)
       
  1273 	  			{
       
  1274 	  				newSPSId = i;
       
  1275 	  				storeSPS  =0;
       
  1276   					break;		
       
  1277 	  			}
       
  1278   			}
       
  1279   		
       
  1280 	  		if ( newSPSId > PS_MAX_NUM_OF_SPS )
       
  1281 	  		{
       
  1282 	  			// We have reached maximum number of SPS, return an error
       
  1283 	  			return PS_ERROR;
       
  1284 	  		}
       
  1285 	  		
       
  1286 	  		(*aNumSPS)++;
       
  1287 	  	
       
  1288 	  		// Set indexChanged to true and give the new index to old SPS, so that (new) PPS can refer to the new SPS
       
  1289 	  		spsList[seq_parameter_set_id]->indexChanged = 1;
       
  1290 	  		spsList[seq_parameter_set_id]->newSPSId = newSPSId;	// The new Id
       
  1291 	  	
       
  1292 	  		if (aFrameFromEncoder)
       
  1293 	  		{
       
  1294 		  		spsList[seq_parameter_set_id]->encSPSId = newSPSId;	// The new Id
       
  1295 		  	
       
  1296 		  		// Store information that there are different SPS in use, we have to encode until an IDR NAL unit.
       
  1297 		  		*aEncodeUntilIDR = ETrue;
       
  1298 	  		}
       
  1299 			else
       
  1300 		  		spsList[seq_parameter_set_id]->origSPSId = newSPSId;	// The new Id
       
  1301 		  	
       
  1302 	  		
       
  1303 	  		// Store the new SPS at the new index and modify SPS id, 
       
  1304 	  		// unless we are using a previously stored SPS
       
  1305 	  		if(storeSPS)
       
  1306 	  		{
       
  1307 		  		spsList[newSPSId] = sps;
       
  1308 		  		
       
  1309 				// Restore the bit buffer position at the SPS Id
       
  1310 				bitbuf->bitpos = bitPosit;
       
  1311 				bitbuf->bytePos = bytePosit;
       
  1312 		
       
  1313 				if(trailingBits > 8)
       
  1314 				{
       
  1315 					trailingBits = 8;
       
  1316 				}
       
  1317 		
       
  1318 				if ( oldIdLength == newIdLength )
       
  1319 				{
       
  1320 					// Just encode the new Id on top of the old Id
       
  1321 					bitbuf->bitpos += oldIdLength;
       
  1322 					if (bitbuf->bitpos > 8)
       
  1323 					{	
       
  1324 						// Go to the right byte and bit position
       
  1325 						bitbuf->bytePos -= bitbuf->bitpos / 8;
       
  1326 						bitbuf->bitpos = bitbuf->bitpos % 8;
       
  1327 					}
       
  1328 				
       
  1329 					EncodeUnsignedExpGolombCode(bitbuf, newSPSId);
       
  1330 				}
       
  1331 				else if ( oldIdLength < newIdLength )
       
  1332 				{
       
  1333 					diff = newIdLength - oldIdLength;
       
  1334 			
       
  1335 					// Adjust the SPS length
       
  1336 					if (diff >= 8)
       
  1337 					{
       
  1338 						// Add as many extra bytes as is required
       
  1339 						sps->SPSlength += (diff / 8);
       
  1340 					}
       
  1341 				
       
  1342 					if ( trailingBits < (diff % 8) )
       
  1343 					{
       
  1344 						// Add one byte since there aren't enough trailing bits for the extra bits
       
  1345 						sps->SPSlength += 1;
       
  1346 					}
       
  1347 			
       
  1348 					ShiftBufferRight(bitbuf, diff, trailingBits, oldIdLength);
       
  1349 	
       
  1350 					// After shifting, encode the new value to the bit buffer
       
  1351 					EncodeUnsignedExpGolombCode(bitbuf, newSPSId);
       
  1352 				}
       
  1353 				else
       
  1354 				{
       
  1355 					// New id's length is smaller than old id's length
       
  1356 					diff = oldIdLength - newIdLength;
       
  1357 			
       
  1358 					if (diff >= 8)
       
  1359 					{
       
  1360 						// Adjust the SPS length
       
  1361 						sps->SPSlength -= (diff / 8);
       
  1362 					}
       
  1363 			
       
  1364 					ShiftBufferLeft(bitbuf, diff, oldIdLength);
       
  1365 			
       
  1366 					// After shifting, encode the new value to the bit buffer
       
  1367 					EncodeUnsignedExpGolombCode(bitbuf, newSPSId);
       
  1368 				}
       
  1369 	  		}
       
  1370 	  	}
       
  1371 	  	else	// Use one SPS for both
       
  1372 	  	{
       
  1373 	  		// Reset indexChanged to false 
       
  1374 	  		spsList[seq_parameter_set_id]->indexChanged = 0;
       
  1375 	  		
       
  1376 	  		// Check if the frame numbering or POC numbering has to be changed
       
  1377 	  		if (spsList[seq_parameter_set_id]->log2_max_frame_num_minus4 != sps->log2_max_frame_num_minus4)
       
  1378 	  		{
       
  1379 	  			spsList[seq_parameter_set_id]->maxFrameNumChanged = 1;
       
  1380 	  		
       
  1381 	  			if (aFrameFromEncoder)
       
  1382 			  		spsList[seq_parameter_set_id]->encMaxFrameNum = sps->log2_max_frame_num_minus4;
       
  1383 				else
       
  1384 		  			spsList[seq_parameter_set_id]->origMaxFrameNum = sps->log2_max_frame_num_minus4;
       
  1385 	  		}
       
  1386 	  		else
       
  1387 	  		{
       
  1388 	  			// Reset the value in case it was changed for another clip earlier
       
  1389 	  			spsList[seq_parameter_set_id]->maxFrameNumChanged = 0;
       
  1390 	  		}
       
  1391 
       
  1392 	  		if (spsList[seq_parameter_set_id]->log2_max_pic_order_cnt_lsb_minus4 != sps->log2_max_pic_order_cnt_lsb_minus4)
       
  1393 	  		{
       
  1394 	  			spsList[seq_parameter_set_id]->maxPOCNumChanged = 1;
       
  1395 	  		
       
  1396 	  			if (aFrameFromEncoder)
       
  1397 			  		spsList[seq_parameter_set_id]->encMaxPOCNum = sps->log2_max_pic_order_cnt_lsb_minus4;
       
  1398 				else
       
  1399 		  			spsList[seq_parameter_set_id]->origMaxPOCNum = sps->log2_max_pic_order_cnt_lsb_minus4;
       
  1400 	  		}
       
  1401 	  		else
       
  1402 	  		{
       
  1403 	  			// Reset the value in case it was changed for another clip earlier
       
  1404 	  			spsList[seq_parameter_set_id]->maxPOCNumChanged = 0;
       
  1405 	  		}
       
  1406 	  	}
       
  1407   	}
       
  1408 
       
  1409   	if ( IsSPSSupported(sps) == KErrNotSupported )
       
  1410   		return KErrNotSupported;
       
  1411   	
       
  1412 
       
  1413   	// Store the buffer containing the SPS set in order to later pass it to the 3gpmp4library
       
  1414   	// If we use the same sps for both, don't allocate, otherwise allocate
       
  1415   	if ( !useOneSPS )
       
  1416   	{
       
  1417   		
       
  1418 		// Store the buffer containing the SPS set in order to later pass it to the 3gpmp4library
       
  1419   		sps->codedSPSBuffer = (TUint8*) User::Alloc(sps->SPSlength);    
       
  1420 
       
  1421 		if (sps->codedSPSBuffer == 0)
       
  1422 			return PS_ERR_MEM_ALLOC;
       
  1423   
       
  1424   		for (i=0; i<sps->SPSlength; i++)
       
  1425   		{
       
  1426   			sps->codedSPSBuffer[i] = bitbuf->data[i];
       
  1427   		}
       
  1428   	}
       
  1429   	else if (possibleIdConflict)
       
  1430   	{
       
  1431   		// Free the SPS since we will use only one which has been already allocated earlier
       
  1432 	  	User::Free(sps);
       
  1433   	}
       
  1434 
       
  1435 
       
  1436   	return PS_OK;
       
  1437 }
       
  1438 
       
  1439 
       
  1440 // ComparePPSSets
       
  1441 // Compares two input PPS sets to see if a single PPS set could be used for both. 
       
  1442 // Returns ETrue if the sets are similar enough, EFalse otherwise.
       
  1443 TInt ComparePPSSets( pic_parameter_set_s *aPPSSet1, pic_parameter_set_s *aPPSSet2 )
       
  1444 {
       
  1445 	TUint i;
       
  1446 
       
  1447 	// This is the most likely parameter to differ, thus check it first
       
  1448 	if ( aPPSSet1->pic_init_qp_minus26 != aPPSSet2->pic_init_qp_minus26 ||
       
  1449 		 aPPSSet1->pic_init_qs_minus26 != aPPSSet2->pic_init_qs_minus26 )
       
  1450 	{
       
  1451 		return EFalse;
       
  1452 	}
       
  1453 
       
  1454 	if ( aPPSSet1->entropy_coding_mode_flag != aPPSSet2->entropy_coding_mode_flag )
       
  1455 	{
       
  1456 		return EFalse;
       
  1457 	}
       
  1458 
       
  1459 	if ( aPPSSet1->pic_order_present_flag != aPPSSet2->pic_order_present_flag )
       
  1460 	{
       
  1461 		return EFalse;
       
  1462 	}
       
  1463 
       
  1464 	if ( aPPSSet1->num_slice_groups_minus1 != aPPSSet2->num_slice_groups_minus1 )
       
  1465 	{
       
  1466 		return EFalse;
       
  1467 	}
       
  1468 	else
       
  1469 	{
       
  1470 	  	if ( aPPSSet1->num_slice_groups_minus1 > 0 ) 
       
  1471   		{
       
  1472 			if ( aPPSSet1->slice_group_map_type != aPPSSet2->slice_group_map_type ) 
       
  1473 			{
       
  1474 				return EFalse;
       
  1475 			}
       
  1476 			
       
  1477 	    	switch ( aPPSSet1->slice_group_map_type ) 
       
  1478    	 		{
       
  1479 
       
  1480 	      		case PS_SLICE_GROUP_MAP_TYPE_INTERLEAVED:
       
  1481     	    		for (i = 0; i <= aPPSSet1->num_slice_groups_minus1; i++) 
       
  1482         			{
       
  1483 						if ( aPPSSet1->run_length_minus1[i] != aPPSSet2->run_length_minus1[i] ) 
       
  1484 						{
       
  1485 							return EFalse;
       
  1486 						}
       
  1487 	        		}
       
  1488     	    		break;
       
  1489 
       
  1490       			case PS_SLICE_GROUP_MAP_TYPE_DISPERSED:
       
  1491         			break;
       
  1492 
       
  1493 	      		case PS_SLICE_GROUP_MAP_TYPE_FOREGROUND:
       
  1494     	    		for (i = 0; i < aPPSSet1->num_slice_groups_minus1; i++) 
       
  1495         			{
       
  1496 						if ( aPPSSet1->top_left[i] != aPPSSet2->top_left[i] ||
       
  1497 						     aPPSSet1->bottom_right[i] != aPPSSet2->bottom_right[i] ) 
       
  1498 						{
       
  1499 							return EFalse;
       
  1500 						}
       
  1501 	        		}
       
  1502     	    		break;
       
  1503 
       
  1504 	      		case PS_SLICE_GROUP_MAP_TYPE_CHANGING_3:
       
  1505     	  		case PS_SLICE_GROUP_MAP_TYPE_CHANGING_4:
       
  1506       			case PS_SLICE_GROUP_MAP_TYPE_CHANGING_5:
       
  1507 					if ( aPPSSet1->slice_group_change_direction_flag != aPPSSet2->slice_group_change_direction_flag ||
       
  1508 					     aPPSSet1->slice_group_change_rate_minus1 != aPPSSet2->slice_group_change_rate_minus1 ) 
       
  1509 					{
       
  1510 						return EFalse;
       
  1511 					}
       
  1512         			break;
       
  1513 
       
  1514 	      		case PS_SLICE_GROUP_MAP_TYPE_EXPLICIT:
       
  1515 					if ( aPPSSet1->pic_size_in_map_units_minus1 != aPPSSet2->pic_size_in_map_units_minus1 ) 
       
  1516 					{
       
  1517 						return EFalse;
       
  1518 					}
       
  1519 
       
  1520 	        		for( i = 0; i <= aPPSSet1->pic_size_in_map_units_minus1; i++ ) 
       
  1521     	    		{
       
  1522 						if ( aPPSSet1->slice_group_id[i] != aPPSSet2->slice_group_id[i] ) 
       
  1523 						{
       
  1524 							return EFalse;
       
  1525 						}
       
  1526         			}
       
  1527 
       
  1528 	        		break;
       
  1529 
       
  1530     	  		default:
       
  1531         			// Cannnot happen 
       
  1532         			break;
       
  1533   			}
       
  1534   		}
       
  1535 	}
       
  1536 
       
  1537 	if ( aPPSSet1->num_ref_idx_l0_active_minus1 != aPPSSet2->num_ref_idx_l0_active_minus1 ||
       
  1538 		 aPPSSet1->num_ref_idx_l1_active_minus1 != aPPSSet2->num_ref_idx_l1_active_minus1 )
       
  1539 	{
       
  1540 		return EFalse;
       
  1541 	}
       
  1542 
       
  1543 	if ( aPPSSet1->weighted_pred_flag != aPPSSet2->weighted_pred_flag ||
       
  1544 		 aPPSSet1->weighted_bipred_idc != aPPSSet2->weighted_bipred_idc )
       
  1545 	{
       
  1546 		return EFalse;
       
  1547 	}
       
  1548 
       
  1549 	if ( aPPSSet1->chroma_qp_index_offset != aPPSSet2->chroma_qp_index_offset )
       
  1550 	{
       
  1551 		return EFalse;
       
  1552 	}
       
  1553 
       
  1554 	if ( aPPSSet1->deblocking_filter_parameters_present_flag != aPPSSet2->deblocking_filter_parameters_present_flag )
       
  1555 	{
       
  1556 		return EFalse;
       
  1557 	}
       
  1558 
       
  1559 	if ( aPPSSet1->constrained_intra_pred_flag != aPPSSet2->constrained_intra_pred_flag )
       
  1560 	{
       
  1561 		return EFalse;
       
  1562 	}
       
  1563 
       
  1564 	if ( aPPSSet1->redundant_pic_cnt_present_flag != aPPSSet2->redundant_pic_cnt_present_flag )
       
  1565 	{
       
  1566 		return EFalse;
       
  1567 	}
       
  1568 
       
  1569 	return ETrue;
       
  1570 }
       
  1571 
       
  1572 
       
  1573 // GetNumTrailingBits
       
  1574 // Returns the number of trailing (zero) bits in the input bit buffer.
       
  1575 TInt GetNumTrailingBits(bitbuffer_s *aBitBuffer)
       
  1576 {
       
  1577 	TInt i;
       
  1578 	TUint bit = 0;
       
  1579 
       
  1580 	for (i=0; i<8; i++)
       
  1581 	{
       
  1582 		// Get the i'th bit from the end
       
  1583 		bit = (aBitBuffer->data[aBitBuffer->dataLen - 1] & (1 << i)) >> i;
       
  1584 		if (bit)
       
  1585 		{
       
  1586 			return (i);		// Return the number of trailing bits here
       
  1587 		}
       
  1588 	}
       
  1589 	
       
  1590 	// Return 9 for cases when there are one or more zero byte at the end
       
  1591 	return (9);
       
  1592 }
       
  1593 
       
  1594 
       
  1595 // ReturnUnsignedExpGolombCodeLength
       
  1596 // Returns the amount of bits required for encoding the input aValue with unsigned Exp-Golomb codes.
       
  1597 TInt ReturnUnsignedExpGolombCodeLength(TUint aValue)
       
  1598 {
       
  1599 	TUint codeNumLength;
       
  1600 	
       
  1601 	codeNumLength = 0;
       
  1602 	
       
  1603 	aValue++;
       
  1604 	
       
  1605 	while ( aValue > 1 )
       
  1606 	{
       
  1607 		aValue >>= 1;
       
  1608 		codeNumLength++;
       
  1609 	}
       
  1610 
       
  1611 	// The required code length is codeNumLength*2+1
       
  1612 	return ((codeNumLength << 1) + 1);
       
  1613 }
       
  1614 
       
  1615 
       
  1616 // EncodeUnsignedExpGolombCode
       
  1617 // Encodes the input aValue to the bit buffer with unsigned Exp-Golomb codes.
       
  1618 void EncodeUnsignedExpGolombCode(bitbuffer_s *aBitBuffer, TUint aValue)
       
  1619 {
       
  1620 	TUint codeLength;
       
  1621 	TUint tempValue = aValue;
       
  1622 	TInt i;
       
  1623 	TUint8 byteValue;
       
  1624 	 
       
  1625 	// First, compute the required code length
       
  1626 	codeLength = ReturnUnsignedExpGolombCodeLength(aValue);
       
  1627 
       
  1628 	// The Exp-Golomb coded value is the same as value+1 with the prefix zero bits,
       
  1629 	// thus it can be simply coded by coding value+1 with the number of bits computed 
       
  1630 	// by the above function.
       
  1631 	aValue++;	
       
  1632 
       
  1633 	// Then write the bits to the bit buffer one bit at a time
       
  1634 	for (i=codeLength-1; i>=0; i--)
       
  1635 	{
       
  1636 		tempValue = (aValue & (1 << i)) >> i;
       
  1637 		
       
  1638 		// Zero out the bitpos bit
       
  1639 		byteValue = aBitBuffer->data[aBitBuffer->bytePos-1] & ~(1<<(aBitBuffer->bitpos-1));
       
  1640 
       
  1641 		// Add the bit from the value to be coded and store the result back to bit buffer
       
  1642 		byteValue |= tempValue << (aBitBuffer->bitpos-1);
       
  1643 		aBitBuffer->data[aBitBuffer->bytePos-1] = byteValue;
       
  1644 		aBitBuffer->bitpos--;
       
  1645 		
       
  1646 		if(aBitBuffer->bitpos == 0)
       
  1647 		{
       
  1648 			aBitBuffer->bytePos++;
       
  1649 			aBitBuffer->bitpos = 8;
       
  1650 		}
       
  1651 	}
       
  1652 
       
  1653  	// Update the currentBits value 
       
  1654  	aBitBuffer->currentBits = aBitBuffer->data[aBitBuffer->bytePos-1];
       
  1655 }
       
  1656 
       
  1657 
       
  1658 // ShiftBitBufferBitsRight
       
  1659 // This function shifts bits right by aDiff in the aBitBuffer, note that if 
       
  1660 // the shift is more than 8 bits, full bytes should be shifted before calling this function.
       
  1661 // The gap between unmodified and shofted part of the buffer is filled with zero bits
       
  1662 void ShiftBitBufferBitsRight(bitbuffer_s *aBitBuffer, TInt aDiff)
       
  1663 {
       
  1664 	TUint8 byteValue;
       
  1665 	TUint8 tempValue;
       
  1666 	TUint8 bitMask;
       
  1667 	TInt i;
       
  1668 	
       
  1669  	// Start from the end, shift bits in each byte until the current byte
       
  1670  	for (i=aBitBuffer->dataLen-1; i>=aBitBuffer->bytePos; i--)
       
  1671  	{
       
  1672 		bitMask = (1 << aDiff) - 1;	// The aDiff lowest bits
       
  1673 		
       
  1674 		// Shift the bits in this byte right by aDiff
       
  1675  		byteValue = aBitBuffer->data[i];
       
  1676  		byteValue >>= aDiff;
       
  1677  		
       
  1678  		// The aDiff lowest bits from the next byte (to the left)
       
  1679  		tempValue = aBitBuffer->data[i-1] & bitMask;
       
  1680  		
       
  1681  		tempValue <<= (8 - aDiff);
       
  1682  		aBitBuffer->data[i] = byteValue | tempValue;
       
  1683  	}
       
  1684  	
       
  1685  	// Take care of the first byte separately
       
  1686 	bitMask = (1 << aBitBuffer->bitpos) - 1;	// The bitPos lowest bits
       
  1687 	byteValue = aBitBuffer->data[aBitBuffer->bytePos-1] & bitMask;
       
  1688 	byteValue >>= aDiff;		// Shift right by aDiff bits
       
  1689 		
       
  1690 	bitMask = 255 << (aBitBuffer->bitpos);	// Mask the 8-bitPos upper bits
       
  1691 		
       
  1692 	// Write the shifted value back to bit buffer
       
  1693 	aBitBuffer->data[aBitBuffer->bytePos-1] = (bitMask & aBitBuffer->data[aBitBuffer->bytePos-1]) | byteValue;
       
  1694  	
       
  1695  	// Update the currentBits value 
       
  1696  	aBitBuffer->currentBits = aBitBuffer->data[aBitBuffer->bytePos-1];
       
  1697 }
       
  1698 
       
  1699 
       
  1700 // ShiftBitBufferBitsLeft
       
  1701 // This function shifts bits left by aDiff in the aBitBuffer, note that if 
       
  1702 // the shift is more than 8 bits, full bytes should be shifted before calling this function.
       
  1703 void ShiftBitBufferBitsLeft(bitbuffer_s *aBitBuffer, TInt aDiff)
       
  1704 {
       
  1705 	TUint8 byteValue;
       
  1706 	TUint8 tempValue;
       
  1707 	TUint8 bitMask;
       
  1708 	TInt i;
       
  1709 	
       
  1710 	
       
  1711  	// Take care of the first byte separately
       
  1712  	if ( aBitBuffer->bitpos > aDiff )
       
  1713  	{
       
  1714 		bitMask = (1 << aBitBuffer->bitpos) - 1;	// The aBitBuf->bitpos lowest bits
       
  1715 		byteValue = aBitBuffer->currentBits & bitMask;
       
  1716 		
       
  1717 		// Shift the byteValue left by aDiff
       
  1718 		byteValue <<= aDiff;
       
  1719 		// Take only the bitpos lowest bits from this value
       
  1720 		byteValue &= bitMask;
       
  1721 		
       
  1722 		bitMask = 255 << (aBitBuffer->bitpos);	// Mask the 8-bitPos upper bits, i.e. the bits to the left from the start of the shift
       
  1723 		byteValue = byteValue | (aBitBuffer->currentBits & bitMask);
       
  1724 		aBitBuffer->data[aBitBuffer->bytePos-1] = byteValue;
       
  1725 		
       
  1726 		bitMask = 255 << (8 - aDiff);	// Mask the aDiff upper bits
       
  1727 		byteValue = aBitBuffer->data[aBitBuffer->bytePos] & bitMask;
       
  1728 		byteValue >>= (8 - aDiff);
       
  1729 		
       
  1730 		// "Add" the aDiff bits from the next byte (msb) to this byte (lsb)
       
  1731  		aBitBuffer->data[aBitBuffer->bytePos-1] |= byteValue;
       
  1732  	}
       
  1733  	else
       
  1734  	{
       
  1735 		bitMask = 255 << (aBitBuffer->bitpos);	// Mask the 8-bitPos upper bits, i.e. the bits to the left from the start of the shift
       
  1736 		aBitBuffer->data[aBitBuffer->bytePos-1] = aBitBuffer->currentBits & bitMask;
       
  1737 		
       
  1738 		bitMask = (1 << (8-aDiff+aBitBuffer->bitpos)) - 1;	// The 8-diff+aBitBuf->bitpos lowest bits
       
  1739 		tempValue = aBitBuffer->data[aBitBuffer->bytePos] & bitMask;
       
  1740 		
       
  1741 		// Shift tempValue right by 8 - diff bits, resulting in bitpos lowest bits 
       
  1742 		tempValue >>= (8 - aDiff);
       
  1743 
       
  1744  		aBitBuffer->data[aBitBuffer->bytePos-1] |= tempValue;
       
  1745  	}
       
  1746  	
       
  1747  	
       
  1748  	// Start from the current byte, shift bits in each byte until the end
       
  1749  	for (i=aBitBuffer->bytePos; i<(aBitBuffer->dataLen-1); i++)
       
  1750  	{
       
  1751  	
       
  1752 		bitMask = 255 << (8 - aDiff);	// Mask the 8-aDiff upper bits
       
  1753 		byteValue = aBitBuffer->data[i+1] & bitMask;
       
  1754 		byteValue >>= (8 - aDiff);
       
  1755 		
       
  1756  		tempValue = aBitBuffer->data[i];
       
  1757  		tempValue <<= aDiff;
       
  1758 
       
  1759  		aBitBuffer->data[i] = byteValue | tempValue;
       
  1760  	}
       
  1761  	
       
  1762  	// Take care of the last byte separately, just shift to the left
       
  1763 	aBitBuffer->data[aBitBuffer->dataLen-1] <<= aDiff;
       
  1764  	
       
  1765  	// Update the currentBits value 
       
  1766  	aBitBuffer->currentBits = aBitBuffer->data[aBitBuffer->bytePos-1];
       
  1767 }
       
  1768 
       
  1769 
       
  1770 // ShiftBufferLeftByOneByte
       
  1771 // Shifts the bytes left in the bit buffer by one position starting from the current byte position.
       
  1772 void ShiftBufferLeftByOneByte(bitbuffer_s *aBitBuffer)
       
  1773 {
       
  1774 	TInt i;
       
  1775 	TUint8 byteValue;
       
  1776 	TUint8 bitMask;
       
  1777 
       
  1778 	
       
  1779 	// For the current byte, take 8-bitpos upper bits from this byte and bitpos lowest bits from the next 
       
  1780 	// byte (this is ok since we are shift at least 8 bits when this function is called)
       
  1781 	bitMask = 255 << (aBitBuffer->bitpos);	// Mask the 8-bitPos upper bits
       
  1782 	aBitBuffer->data[aBitBuffer->bytePos] &= bitMask;
       
  1783 
       
  1784 	bitMask = (1 << aBitBuffer->bitpos) - 1;	// The aBitBuf->bitpos lowest bits
       
  1785 	byteValue = aBitBuffer->data[aBitBuffer->bytePos+1] & bitMask;
       
  1786 	aBitBuffer->data[aBitBuffer->bytePos] |= byteValue;
       
  1787 	
       
  1788 	// Start from the next byte position, and go through the whole buffer
       
  1789  	for (i=aBitBuffer->bytePos+1; i<(aBitBuffer->dataLen-1); i++)
       
  1790  	{
       
  1791  		// Copy the next byte to here
       
  1792 		aBitBuffer->data[i] =  	aBitBuffer->data[i+1];
       
  1793  	}
       
  1794  	
       
  1795  	// Adjust the bit buffer length
       
  1796  	aBitBuffer->dataLen--;
       
  1797 }
       
  1798 
       
  1799 
       
  1800 // ShiftBufferRightByOneByte
       
  1801 // Shifts the bytes right in the bit buffer by one position starting from the current byte position.
       
  1802 void ShiftBufferRightByOneByte(bitbuffer_s *aBitBuffer)
       
  1803 {
       
  1804 	TInt i;
       
  1805 	
       
  1806 	// Start from the last byte position, and go through the whole buffer until the current byte position
       
  1807 	// Note: also the current byte can be shifted, since the bits that should not be shifted from that byte
       
  1808 	// will be written over by the new value coded later, thus no error will be there.
       
  1809  	for (i=aBitBuffer->dataLen-1; i>=aBitBuffer->bytePos; i--)
       
  1810  	{
       
  1811  		// Copy the next byte to here
       
  1812 		aBitBuffer->data[i] =  	aBitBuffer->data[i-1];
       
  1813 	}
       
  1814 }
       
  1815 
       
  1816 
       
  1817 // ShiftBufferRight
       
  1818 // Shifts bits right in the input bit buffer by aDiff value, the bit buffer length is modified if required.
       
  1819 void ShiftBufferRight(bitbuffer_s *aBitBuffer, TInt aDiff, TUint aTrailingBits, TUint aOldIdLength)
       
  1820 {
       
  1821 	TInt i;
       
  1822 	
       
  1823 	if ( aDiff >= 8 )
       
  1824 	{
       
  1825 		TUint bytesToShift = aDiff / 8;
       
  1826 				
       
  1827 		// Add byte(s) to the bit buffer
       
  1828 		aBitBuffer->dataLen += bytesToShift;
       
  1829 				
       
  1830 		// Shift full bytes to right
       
  1831 		for (i=0; i<bytesToShift; i++)
       
  1832 		{
       
  1833 			ShiftBufferRightByOneByte(aBitBuffer);
       
  1834 			aDiff -= 8;
       
  1835 		}
       
  1836 				
       
  1837 		aDiff = aDiff % 8;
       
  1838 	}
       
  1839 	
       
  1840 	// If there are less trailing bits than we need to shift then we have to add one byte to the buffer		
       
  1841 	if ( aTrailingBits < aDiff )	
       
  1842 	{
       
  1843 		// Have to add byte to the SPS set
       
  1844 		aBitBuffer->dataLen += 1;
       
  1845 	}
       
  1846 
       
  1847 	if (aDiff != 0)
       
  1848 	{
       
  1849 		// Shift the bits in the bit buffer to the right
       
  1850 		ShiftBitBufferBitsRight(aBitBuffer, aDiff);
       
  1851 	}
       
  1852 				
       
  1853 	// Adjust the bitbuffer bitpos value
       
  1854 	aBitBuffer->bitpos += aOldIdLength;
       
  1855 	if ( aBitBuffer->bitpos > 8 )
       
  1856 	{
       
  1857 		aBitBuffer->bitpos -= 8;
       
  1858 		aBitBuffer->bytePos--;
       
  1859 	}
       
  1860 }
       
  1861 
       
  1862 
       
  1863 // ShiftBufferLeft
       
  1864 // Shifts bits left in the input bit buffer by aDiff value.
       
  1865 void ShiftBufferLeft(bitbuffer_s *aBitBuffer, TInt aDiff, TUint aOldIdLength)
       
  1866 {
       
  1867 	TInt i;
       
  1868 	
       
  1869 	if (aDiff >= 8)
       
  1870 	{
       
  1871 		// Shift full bytes to the left before shifting bits
       
  1872 		TUint bytesToShift = aDiff / 8;
       
  1873 
       
  1874 		// First, adjust the byte position to be correct
       
  1875 		aBitBuffer->bytePos -= bytesToShift;
       
  1876 				
       
  1877 		for (i=0; i<bytesToShift; i++)
       
  1878 		{
       
  1879 			ShiftBufferLeftByOneByte(aBitBuffer);
       
  1880 			aDiff -= 8;
       
  1881 		}
       
  1882 	}
       
  1883 			
       
  1884 	// Adjust the bit position of the bit buffer
       
  1885 	aBitBuffer->bitpos += aOldIdLength;
       
  1886 	if ( aBitBuffer->bitpos > 8 )
       
  1887 	{
       
  1888 		aBitBuffer->bitpos -= 8;
       
  1889 		aBitBuffer->bytePos--;
       
  1890 					
       
  1891 		aBitBuffer->currentBits = aBitBuffer->data[aBitBuffer->bytePos-1];
       
  1892 	}
       
  1893 			
       
  1894 	if ( aDiff != 0 )
       
  1895 	{
       
  1896 		// Shift the bits in the bit buffer to the left
       
  1897 		ShiftBitBufferBitsLeft(aBitBuffer, aDiff);
       
  1898 	}
       
  1899 	
       
  1900 }
       
  1901 
       
  1902 
       
  1903 // psParsePPS
       
  1904 // Parses the input PPS set, the PPS and SPS id's are modified if necessary 
       
  1905 // and the modified data is stored in codedPPSBuffer.
       
  1906 TInt psParsePPS( bitbuffer_s *bitbuf, pic_parameter_set_s **ppsList, seq_parameter_set_s **spsList, 
       
  1907 				 TUint aFrameFromEncoder, TUint *aNumPPS )
       
  1908 {
       
  1909   	pic_parameter_set_s *pps;
       
  1910   	TUint i, tmp;
       
  1911   	TInt len;
       
  1912   	TUint pic_parameter_set_id;
       
  1913   	TInt retCode;
       
  1914   	TUint pic_size_in_map_units_minus1;
       
  1915   	TUint newPPSId = 0;
       
  1916   	TUint possibleIdConflict = 0;
       
  1917   	TUint modifySPSId = 0;
       
  1918   	TUint bitPosit = 0;
       
  1919   	TUint bytePosit = 0;
       
  1920   	TInt bitSPSPosit = 0;
       
  1921   	TUint byteSPSPosit = 0;
       
  1922   	TUint useOnePPS = 0;
       
  1923 
       
  1924   	// Parse pps id 
       
  1925   	if ((retCode = ue_v(bitbuf, &pic_parameter_set_id, PS_MAX_NUM_OF_PPS-1)) < 0)
       
  1926     	return retCode;
       
  1927 
       
  1928   	// Allocate memory for pps if not already allocated
       
  1929   	pps = ppsList[pic_parameter_set_id];
       
  1930 
       
  1931   	if (!pps) 
       
  1932   	{
       
  1933     	pps = (pic_parameter_set_s *) User::Alloc(sizeof(pic_parameter_set_s));
       
  1934     	
       
  1935     	if (pps == 0) 
       
  1936     	{
       
  1937       		PRINT((_L("Error while allocating memory for PPS.\n")));
       
  1938       		return PS_ERR_MEM_ALLOC;
       
  1939     	}
       
  1940     
       
  1941     	memset( pps, 0, sizeof(pic_parameter_set_s));
       
  1942     	ppsList[pic_parameter_set_id] = pps;
       
  1943     	
       
  1944     	(*aNumPPS)++;
       
  1945   	}
       
  1946   	else
       
  1947   	{
       
  1948   		// There might be a conflicting Id with an existing PPS set
       
  1949   		// Give the new SPS set the next free PPS Id
       
  1950   		possibleIdConflict = 1;
       
  1951   		useOnePPS = 1;
       
  1952   		newPPSId = 0;
       
  1953   	
       
  1954   		while (ppsList[newPPSId])
       
  1955   		{
       
  1956   			newPPSId++;
       
  1957   		}
       
  1958   	
       
  1959   		// Allocate memory for the PPS
       
  1960     	pps = (pic_parameter_set_s *) User::Alloc(sizeof(pic_parameter_set_s));
       
  1961     	
       
  1962     	if (pps == 0) 
       
  1963     	{
       
  1964       		PRINT((_L("Error while allocating memory for PPS.\n")));
       
  1965       		return PS_ERR_MEM_ALLOC;
       
  1966     	}
       
  1967     
       
  1968     	memset( pps, 0, sizeof(pic_parameter_set_s));
       
  1969     	pps->pic_parameter_set_id = newPPSId;
       
  1970     
       
  1971   		// Store the position of the bit buffer
       
  1972   		bitPosit = bitbuf->bitpos;
       
  1973   		bytePosit = bitbuf->bytePos;
       
  1974   	}
       
  1975 
       
  1976 
       
  1977   	// Parse the rest of the picture parameter set syntax
       
  1978   	if ((retCode = ue_v( bitbuf, &pps->seq_parameter_set_id, PS_MAX_NUM_OF_SPS-1)) < 0)
       
  1979     	return retCode;
       
  1980   
       
  1981   	// Check if the Id of the SPS that this PPS refers to has changed (and that 
       
  1982   	// the frame originated form the encoder)
       
  1983   	if( spsList[pps->seq_parameter_set_id]->indexChanged)
       
  1984   	{
       
  1985   		if ( !aFrameFromEncoder )
       
  1986   		{
       
  1987   			if (pps->seq_parameter_set_id != spsList[pps->seq_parameter_set_id]->origSPSId)
       
  1988   			{
       
  1989 		  		// Indicate a changed SPS Id, perform the change at the end (when we know the size of the PPS set)
       
  1990   				modifySPSId = ETrue;
       
  1991   	
       
  1992   				// Store the position of the bit buffer
       
  1993   				bitSPSPosit = bitbuf->bitpos;
       
  1994   				byteSPSPosit = bitbuf->bytePos;
       
  1995   			}
       
  1996   		}
       
  1997   		else
       
  1998   		{
       
  1999 	  		// Indicate a changed SPS Id, perform the change at the end (when we know the size of the PPS set)
       
  2000   			modifySPSId = ETrue;
       
  2001 	  	
       
  2002   			// Store the position of the bit buffer
       
  2003   			bitSPSPosit = bitbuf->bitpos;
       
  2004   			byteSPSPosit = bitbuf->bytePos;
       
  2005   		}
       
  2006   	}
       
  2007 
       
  2008   	// Fetch entropy coding mode. Mode is 0 for CAVLC and 1 for CABAC 
       
  2009   	if ((retCode = u_n( bitbuf, 1, &pps->entropy_coding_mode_flag)) < 0)
       
  2010     	return retCode;
       
  2011 
       
  2012   	// If this flag is 1, POC related syntax elements are present in slice header 
       
  2013   	if ((retCode = u_n( bitbuf, 1, &pps->pic_order_present_flag)) < 0)
       
  2014     	return retCode;
       
  2015 
       
  2016   	// Fetch the number of slice groups minus 1 
       
  2017   	if ((retCode = ue_v( bitbuf, &pps->num_slice_groups_minus1, PS_MAX_NUM_SLICE_GROUPS-1)) < 0)
       
  2018     	return retCode;
       
  2019 
       
  2020   	if(pps->num_slice_groups_minus1 > 0 ) 
       
  2021   	{
       
  2022 
       
  2023     	if ((retCode = ue_v( bitbuf, &pps->slice_group_map_type, 6)) < 0)
       
  2024       		return retCode;
       
  2025 
       
  2026     	switch (pps->slice_group_map_type) 
       
  2027     	{
       
  2028 
       
  2029       		case PS_SLICE_GROUP_MAP_TYPE_INTERLEAVED:
       
  2030         		for (i = 0; i <= pps->num_slice_groups_minus1; i++) 
       
  2031         		{
       
  2032           			if ((retCode = ue_v( bitbuf, &pps->run_length_minus1[i], MAX_PIC_SIZE_IN_MBS-1 )) < 0)
       
  2033             			return retCode;
       
  2034         		}
       
  2035         		break;
       
  2036 
       
  2037       		case PS_SLICE_GROUP_MAP_TYPE_DISPERSED:
       
  2038         		break;
       
  2039 
       
  2040       		case PS_SLICE_GROUP_MAP_TYPE_FOREGROUND:
       
  2041         		for (i = 0; i < pps->num_slice_groups_minus1; i++) 
       
  2042         		{
       
  2043           			// Fetch MB address of the top-left corner 
       
  2044           			if ((retCode = ue_v( bitbuf, &pps->top_left[i], MAX_PIC_SIZE_IN_MBS-1)) < 0)
       
  2045             			return retCode;
       
  2046           			// Fetch MB address of the bottom-right corner (top-left address must 
       
  2047           			// be smaller than or equal to bottom-right address)                  
       
  2048           			if ((retCode = ue_v( bitbuf, &pps->bottom_right[i], MAX_PIC_SIZE_IN_MBS-1)) < 0)
       
  2049             			return retCode;
       
  2050 
       
  2051           			if (pps->top_left[i] > pps->bottom_right[i])
       
  2052             			return PS_ERR_ILLEGAL_VALUE;
       
  2053         		}
       
  2054         		break;
       
  2055 
       
  2056       		case PS_SLICE_GROUP_MAP_TYPE_CHANGING_3:
       
  2057       		case PS_SLICE_GROUP_MAP_TYPE_CHANGING_4:
       
  2058       		case PS_SLICE_GROUP_MAP_TYPE_CHANGING_5:
       
  2059         		if ((retCode = u_n( bitbuf, 1, &pps->slice_group_change_direction_flag)) < 0)
       
  2060           			return retCode;
       
  2061         		if ((retCode = ue_v( bitbuf, &pps->slice_group_change_rate_minus1, MAX_PIC_SIZE_IN_MBS-1)) < 0)
       
  2062           			return retCode;
       
  2063         		break;
       
  2064 
       
  2065       		case PS_SLICE_GROUP_MAP_TYPE_EXPLICIT:
       
  2066 
       
  2067         		if ((retCode = ue_v( bitbuf, &pic_size_in_map_units_minus1, MAX_PIC_SIZE_IN_MBS-1 )) < 0)
       
  2068           			return retCode;
       
  2069 
       
  2070         		// Allocate array for slice group ids if not already allocated 
       
  2071         		if (pic_size_in_map_units_minus1 != pps->pic_size_in_map_units_minus1) 
       
  2072         		{
       
  2073           			User::Free(pps->slice_group_id);
       
  2074 
       
  2075           			pps->slice_group_id = (unsigned int *)User::Alloc( (pic_size_in_map_units_minus1+1) * sizeof(int));
       
  2076 
       
  2077 					if (pps->slice_group_id == 0)
       
  2078                         return PS_ERR_MEM_ALLOC;
       
  2079 
       
  2080           			pps->pic_size_in_map_units_minus1 = pic_size_in_map_units_minus1;
       
  2081         		}
       
  2082 
       
  2083         		// Calculate len = ceil( Log2( num_slice_groups_minus1 + 1 ) )
       
  2084         		tmp = pps->num_slice_groups_minus1 + 1;
       
  2085         		tmp = tmp >> 1;
       
  2086         		for( len = 0; len < 16 && tmp != 0; len++ )
       
  2087           			tmp >>= 1;
       
  2088         		
       
  2089         		if ( (((unsigned)1)<<len) < (pps->num_slice_groups_minus1 + 1) )
       
  2090           			len++;
       
  2091 
       
  2092         		for( i = 0; i <= pps->pic_size_in_map_units_minus1; i++ ) 
       
  2093         		{
       
  2094           			if ((retCode = u_n( bitbuf, len, &pps->slice_group_id[i])) < 0)
       
  2095             			return retCode;
       
  2096         		}
       
  2097 
       
  2098         		break;
       
  2099 
       
  2100       		default:
       
  2101         		// Cannnot happen 
       
  2102         		break;
       
  2103     		}
       
  2104   	}
       
  2105 
       
  2106   	if ((retCode = ue_v( bitbuf, &pps->num_ref_idx_l0_active_minus1, 31 )) < 0)
       
  2107     	return retCode;
       
  2108 
       
  2109   	if ((retCode = ue_v( bitbuf, &pps->num_ref_idx_l1_active_minus1, 31 )) < 0)
       
  2110     	return retCode;
       
  2111 
       
  2112   	if ((retCode = u_n( bitbuf, 1, &pps->weighted_pred_flag)) < 0)
       
  2113     	return retCode;
       
  2114 
       
  2115   	if ((retCode = u_n( bitbuf, 2, &pps->weighted_bipred_idc)) < 0)
       
  2116     	return retCode;
       
  2117 
       
  2118   	if (pps->weighted_bipred_idc > 2)
       
  2119     	return PS_ERR_ILLEGAL_VALUE;
       
  2120 
       
  2121   	if ((retCode = se_v( bitbuf, &pps->pic_init_qp_minus26, -26, 25 )) < 0)
       
  2122     	return retCode;
       
  2123 
       
  2124   	if ((retCode = se_v( bitbuf, &pps->pic_init_qs_minus26, -26, 25  )) < 0)
       
  2125     	return retCode;
       
  2126 
       
  2127   	if ((retCode = se_v( bitbuf, &pps->chroma_qp_index_offset, -12, 12 )) < 0)
       
  2128     	return retCode;
       
  2129 
       
  2130   	pps->chroma_qp_index_offset = clip(MIN_CHROMA_QP_INDEX, MAX_CHROMA_QP_INDEX, pps->chroma_qp_index_offset);
       
  2131 
       
  2132   	if ((retCode = u_n( bitbuf, 1, &pps->deblocking_filter_parameters_present_flag )) < 0)
       
  2133     	return retCode;
       
  2134 
       
  2135   	if ((retCode = u_n( bitbuf, 1, &pps->constrained_intra_pred_flag )) < 0)
       
  2136     	return retCode;
       
  2137 
       
  2138   	if ((retCode = u_n( bitbuf, 1, &pps->redundant_pic_cnt_present_flag )) < 0)
       
  2139     	return retCode;
       
  2140 
       
  2141   	if (bibSkipTrailingBits(bitbuf) < 0)
       
  2142     	return PS_ERROR;
       
  2143 
       
  2144   	// Store the size of the PPS set
       
  2145   	pps->PPSlength = bitbuf->bytePos;
       
  2146   
       
  2147 	syncBitBufferBitpos(bitbuf);
       
  2148 	
       
  2149   	// If we had a possible conflict, compare the PPS sets with the same id to see if only one PPS set can be used.
       
  2150   	if (possibleIdConflict)
       
  2151   	{
       
  2152 		useOnePPS = ComparePPSSets(ppsList[pic_parameter_set_id],pps);
       
  2153 
       
  2154 	  	if (!useOnePPS)
       
  2155 	  	{
       
  2156   			TUint trailingBits = GetNumTrailingBits(bitbuf);
       
  2157   			TInt diff = 0;
       
  2158 			TUint oldPPSId = pic_parameter_set_id;
       
  2159 			TUint oldIdLength = ReturnUnsignedExpGolombCodeLength(oldPPSId);
       
  2160 			TUint newIdLength = ReturnUnsignedExpGolombCodeLength(newPPSId);
       
  2161 
       
  2162 	  		for (i=0; i<PS_MAX_NUM_OF_PPS; i++)
       
  2163   			{
       
  2164   				// Compared if a previously stored PPS might be used
       
  2165   				if (ppsList[i])
       
  2166 					useOnePPS = ComparePPSSets(ppsList[i],pps);
       
  2167   			
       
  2168 	  			if (useOnePPS)
       
  2169   				{
       
  2170   					// Check also that the SPS id's match
       
  2171 				  	if ( modifySPSId )
       
  2172 				  	{
       
  2173 						if (spsList[pps->seq_parameter_set_id]->newSPSId != ppsList[i]->seq_parameter_set_id)
       
  2174 							useOnePPS = 0;
       
  2175 						else
       
  2176 						{
       
  2177 	  						// We can use this previously generated PPSId here also
       
  2178   							newPPSId = i;
       
  2179   							break;	
       
  2180 						}
       
  2181 				  	}
       
  2182 				  	else
       
  2183 				  	{
       
  2184 						if (pps->seq_parameter_set_id != ppsList[i]->seq_parameter_set_id)
       
  2185 							useOnePPS = 0;
       
  2186 						else
       
  2187 						{
       
  2188 	  						// We can use this previously generated PPSId here also
       
  2189   							newPPSId = i;
       
  2190   							break;	
       
  2191 						}
       
  2192 				  	}
       
  2193   				}
       
  2194   			}
       
  2195 		
       
  2196 	  		if ( newPPSId > PS_MAX_NUM_OF_PPS )
       
  2197 	  		{
       
  2198 	  			// We have reached maximum number of PPS, return an error
       
  2199 	  			return PS_ERROR;
       
  2200 	  		}	
       
  2201 	  	
       
  2202 	    	(*aNumPPS)++;
       
  2203 
       
  2204 	  		// Set indexChanged to true and give the new index to old PPS, so that (new) slices can refer to the new PPS
       
  2205 	  		ppsList[pic_parameter_set_id]->indexChanged = 1;
       
  2206 	  		ppsList[pic_parameter_set_id]->newPPSId = newPPSId;	// The new Id
       
  2207 	  	
       
  2208 	  		if (aFrameFromEncoder)
       
  2209 		  		ppsList[pic_parameter_set_id]->encPPSId = newPPSId;		// The new Id
       
  2210 			else
       
  2211 		  		ppsList[pic_parameter_set_id]->origPPSId = newPPSId;	// The new Id
       
  2212 		  	
       
  2213 	  		// Store the new PPS at the new index, unless we are using a previously stored PPS
       
  2214 			if (!ppsList[newPPSId])
       
  2215 		  		ppsList[newPPSId] = pps;
       
  2216   		
       
  2217 			// Restore the bit buffer position at the PPS Id
       
  2218 			bitbuf->bitpos = bitPosit;
       
  2219 			bitbuf->bytePos = bytePosit;
       
  2220 		
       
  2221 			if(trailingBits > 8)
       
  2222 			{	
       
  2223 				trailingBits = 8;
       
  2224 			}
       
  2225 		
       
  2226 			if ( oldIdLength == newIdLength )
       
  2227 			{	
       
  2228 				// Just encode the new Id on top of the old Id
       
  2229 				bitbuf->bitpos += oldIdLength;
       
  2230 				if (bitbuf->bitpos > 8)
       
  2231 				{	
       
  2232 					// Go to the right byte and bit position
       
  2233 					bitbuf->bytePos -= bitbuf->bitpos / 8;
       
  2234 					bitbuf->bitpos = bitbuf->bitpos % 8;
       
  2235 				}
       
  2236 				
       
  2237 				EncodeUnsignedExpGolombCode(bitbuf, newPPSId);
       
  2238 			}
       
  2239 			else if ( oldIdLength < newIdLength )
       
  2240 			{
       
  2241 				diff = newIdLength - oldIdLength;
       
  2242 			
       
  2243 				// Adjust the PPS length
       
  2244 				if (diff >= 8)
       
  2245 				{
       
  2246 					// Add as many extra bytes as is required
       
  2247 					pps->PPSlength += (diff / 8);
       
  2248 				}
       
  2249 			
       
  2250 				if ( trailingBits < (diff % 8) )
       
  2251 				{
       
  2252 					// Add one byte since there aren't enough trailing bits for the extra bits
       
  2253 					pps->PPSlength += 1;
       
  2254 				}
       
  2255 			
       
  2256 				ShiftBufferRight(bitbuf, diff, trailingBits, oldIdLength);
       
  2257 
       
  2258 				// After shifting, encode the new value to the bit buffer
       
  2259 				EncodeUnsignedExpGolombCode(bitbuf, newPPSId);
       
  2260 			}
       
  2261 			else
       
  2262 			{
       
  2263 				// New id's length is smaller than old id's length
       
  2264 				diff = oldIdLength - newIdLength;
       
  2265 			
       
  2266 				if (diff >= 8)
       
  2267 				{
       
  2268 					// Adjust the PPS length
       
  2269 					pps->PPSlength -= (diff / 8);
       
  2270 				}
       
  2271 			
       
  2272 				ShiftBufferLeft(bitbuf, diff, oldIdLength);
       
  2273 			
       
  2274 				// After shifting, encode the new value to the bit buffer
       
  2275 				EncodeUnsignedExpGolombCode(bitbuf, newPPSId);
       
  2276 			}
       
  2277 
       
  2278 	  		// Store the position of the bit buffer for possible SPS id modification
       
  2279   			// The right bit position is the current minus the size of the SPS id length
       
  2280   			bitSPSPosit = bitbuf->bitpos - ReturnUnsignedExpGolombCodeLength(pps->seq_parameter_set_id);
       
  2281   			byteSPSPosit = bitbuf->bytePos;
       
  2282   		
       
  2283   			if(bitSPSPosit < 1)
       
  2284   			{
       
  2285 	  			byteSPSPosit++;
       
  2286   				bitSPSPosit += 8;
       
  2287   			}
       
  2288 		}
       
  2289 		else
       
  2290 		{
       
  2291 	  		// In case the index was changed for some earlier PPS, 
       
  2292 	  		// reset the index changed value back to zero
       
  2293 	  		ppsList[pic_parameter_set_id]->indexChanged = 0;
       
  2294 		}
       
  2295   	}
       
  2296   
       
  2297   	if ( modifySPSId )
       
  2298   	{
       
  2299   		TUint trailingBits = GetNumTrailingBits(bitbuf);
       
  2300   		TInt diff = 0;
       
  2301 
       
  2302 		TUint oldSPSId = pps->seq_parameter_set_id;
       
  2303 		TUint newSPSId = spsList[pps->seq_parameter_set_id]->newSPSId;
       
  2304 		
       
  2305 		TUint oldIdLength = ReturnUnsignedExpGolombCodeLength(oldSPSId);
       
  2306 		TUint newIdLength = ReturnUnsignedExpGolombCodeLength(newSPSId);
       
  2307 		
       
  2308 		
       
  2309 		// Restore the bit buffer position at the SPS Id
       
  2310 		bitbuf->bitpos = bitSPSPosit;
       
  2311 		bitbuf->bytePos = byteSPSPosit;
       
  2312 		
       
  2313 		if(trailingBits > 8)
       
  2314 		{
       
  2315 			trailingBits = 8;
       
  2316 		}
       
  2317 		
       
  2318 		if ( oldIdLength == newIdLength )
       
  2319 		{
       
  2320 			// Just encode the new Id on top of the old Id
       
  2321 			bitbuf->bitpos += oldIdLength;
       
  2322 			if(bitbuf->bitpos > 8)
       
  2323 			{	
       
  2324 				// Go to the right byte and bit position
       
  2325 				bitbuf->bytePos -= bitbuf->bitpos / 8;
       
  2326 				bitbuf->bitpos = bitbuf->bitpos % 8;
       
  2327 			}
       
  2328 		
       
  2329 			EncodeUnsignedExpGolombCode(bitbuf, newSPSId);
       
  2330 		}
       
  2331 		else if ( oldIdLength < newIdLength )
       
  2332 		{
       
  2333 			diff = newIdLength - oldIdLength;
       
  2334 			
       
  2335 			// Adjust the PPS length
       
  2336 			if (diff >= 8)
       
  2337 			{
       
  2338 				// Add as many extra bytes as is required
       
  2339 				pps->PPSlength += (diff / 8);
       
  2340 			}
       
  2341 		
       
  2342 			if ( trailingBits < (diff % 8) )
       
  2343 			{
       
  2344 				// Add one byte since there aren't enough trailing bits for the extra bits
       
  2345 				pps->PPSlength += 1;
       
  2346 			}
       
  2347 			
       
  2348 			ShiftBufferRight(bitbuf, diff, trailingBits, oldIdLength);
       
  2349 
       
  2350 			// After shifting, encode the new value to the bit buffer
       
  2351 			EncodeUnsignedExpGolombCode(bitbuf, newSPSId);
       
  2352 			
       
  2353 		}
       
  2354 		else
       
  2355 		{
       
  2356 			// New id's length is smaller than old id's length
       
  2357 			diff = oldIdLength - newIdLength;
       
  2358 			
       
  2359 			if (diff >= 8)
       
  2360 			{
       
  2361 				// Adjust the PPS length
       
  2362 				pps->PPSlength -= (diff / 8);
       
  2363 			}
       
  2364 			
       
  2365 			ShiftBufferLeft(bitbuf, diff, oldIdLength);
       
  2366 			
       
  2367 			// After shifting, encode the new value to the bit buffer
       
  2368 			EncodeUnsignedExpGolombCode(bitbuf, newSPSId);
       
  2369 		}
       
  2370   
       
  2371   		// Modify the SPS id in the pps
       
  2372   		pps->seq_parameter_set_id = newSPSId;	
       
  2373   	}
       
  2374   
       
  2375 	if ( IsPPSSupported(pps) == KErrNotSupported )
       
  2376   		return KErrNotSupported;
       
  2377   
       
  2378   	
       
  2379   	// Allocate memory for the encoded PPS data in case we have a new PPS (i.e. a new original PPS or a PPS with a new index)  
       
  2380   	if ( !useOnePPS )
       
  2381   	{
       
  2382 		// Store the buffer containing the PPS set in order to later pass it to the 3gpmp4library
       
  2383   		pps->codedPPSBuffer = (TUint8*) User::Alloc(pps->PPSlength);    
       
  2384 
       
  2385 		if (pps->codedPPSBuffer == 0)
       
  2386 		    return PS_ERR_MEM_ALLOC;
       
  2387   
       
  2388   		for (i=0; i<pps->PPSlength; i++)
       
  2389   		{
       
  2390   			pps->codedPPSBuffer[i] = bitbuf->data[i];
       
  2391   		}
       
  2392   	}
       
  2393   	else if (possibleIdConflict)
       
  2394   	{
       
  2395   		// In case of conflicting id and one PPS, free the PPS allocated here
       
  2396 	  	User::Free(pps);
       
  2397   	}
       
  2398 
       
  2399   	return PS_OK;
       
  2400 }
       
  2401 
       
  2402 #endif  // VIDEOEDITORENGINE_AVC_EDITING
       
  2403