PageRenderTime 29727ms CodeModel.GetById 171ms app.highlight 116ms RepoModel.GetById 2ms app.codeStats 1ms

/Avc/header.cpp

http://github.com/mbebenita/Broadway
C++ | 1391 lines | 1077 code | 174 blank | 140 comment | 241 complexity | 6aa41ed613ba6467df1e6e9ed4dff216 MD5 | raw file
   1/* ------------------------------------------------------------------
   2 * Copyright (C) 1998-2009 PacketVideo
   3 *
   4 * Licensed under the Apache License, Version 2.0 (the "License");
   5 * you may not use this file except in compliance with the License.
   6 * You may obtain a copy of the License at
   7 *
   8 *      http://www.apache.org/licenses/LICENSE-2.0
   9 *
  10 * Unless required by applicable law or agreed to in writing, software
  11 * distributed under the License is distributed on an "AS IS" BASIS,
  12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13 * express or implied.
  14 * See the License for the specific language governing permissions
  15 * and limitations under the License.
  16 * -------------------------------------------------------------------
  17 */
  18#include "avcdec_lib.h"
  19#include "avcdec_bitstream.h"
  20#include "avcdec_api.h"
  21
  22/** see subclause 7.4.2.1 */
  23AVCDec_Status DecodeSPS(AVCDecObject *decvid, AVCDecBitstream *stream)
  24{
  25    AVCDec_Status status = AVCDEC_SUCCESS;
  26    AVCSeqParamSet *seqParam;
  27    uint temp;
  28    int i;
  29    uint profile_idc, constrained_set0_flag, constrained_set1_flag, constrained_set2_flag;
  30    uint level_idc, seq_parameter_set_id;
  31    void *userData = decvid->avcHandle->userData;
  32    AVCHandle *avcHandle = decvid->avcHandle;
  33
  34    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "DecodeSPS", -1, -1);
  35
  36    BitstreamReadBits(stream, 8, &profile_idc);
  37    BitstreamRead1Bit(stream, &constrained_set0_flag);
  38//  if (profile_idc != 66 && constrained_set0_flag != 1)
  39//  {
  40//      return AVCDEC_FAIL;
  41//  }
  42    BitstreamRead1Bit(stream, &constrained_set1_flag);
  43    BitstreamRead1Bit(stream, &constrained_set2_flag);
  44    BitstreamReadBits(stream, 5, &temp);
  45    BitstreamReadBits(stream, 8, &level_idc);
  46    if (level_idc > 51)
  47    {
  48        return AVCDEC_FAIL;
  49    }
  50    if (mapLev2Idx[level_idc] == 255)
  51    {
  52        return AVCDEC_FAIL;
  53    }
  54    ue_v(stream, &seq_parameter_set_id);
  55
  56    if (seq_parameter_set_id > 31)
  57    {
  58        return AVCDEC_FAIL;
  59    }
  60
  61    /* Allocate sequence param set for seqParams[seq_parameter_set_id]. */
  62    if (decvid->seqParams[seq_parameter_set_id] == NULL)  /* allocate seqParams[id] */
  63    {
  64        decvid->seqParams[seq_parameter_set_id] =
  65            (AVCSeqParamSet*) avcHandle->CBAVC_Malloc(userData, sizeof(AVCSeqParamSet), DEFAULT_ATTR);
  66
  67        if (decvid->seqParams[seq_parameter_set_id] == NULL)
  68        {
  69            return AVCDEC_MEMORY_FAIL;
  70        }
  71    }
  72
  73    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "done alloc seqParams", -1, -1);
  74
  75    seqParam = decvid->seqParams[seq_parameter_set_id];
  76
  77    seqParam->profile_idc = profile_idc;
  78    seqParam->constrained_set0_flag = constrained_set0_flag;
  79    seqParam->constrained_set1_flag = constrained_set1_flag;
  80    seqParam->constrained_set2_flag = constrained_set2_flag;
  81    seqParam->level_idc = level_idc;
  82    seqParam->seq_parameter_set_id = seq_parameter_set_id;
  83
  84    /* continue decoding SPS */
  85    ue_v(stream, &(seqParam->log2_max_frame_num_minus4));
  86
  87    if (seqParam->log2_max_frame_num_minus4 > 12)
  88    {
  89        return AVCDEC_FAIL;
  90    }
  91
  92    ue_v(stream, &(seqParam->pic_order_cnt_type));
  93
  94    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "check point 1", seqParam->log2_max_frame_num_minus4, seqParam->pic_order_cnt_type);
  95
  96    if (seqParam->pic_order_cnt_type == 0)
  97    {
  98        ue_v(stream, &(seqParam->log2_max_pic_order_cnt_lsb_minus4));
  99    }
 100    else if (seqParam->pic_order_cnt_type == 1)
 101    {               // MC_CHECK
 102        BitstreamRead1Bit(stream, (uint*)&(seqParam->delta_pic_order_always_zero_flag));
 103        se_v32bit(stream, &(seqParam->offset_for_non_ref_pic));
 104        se_v32bit(stream, &(seqParam->offset_for_top_to_bottom_field));
 105        ue_v(stream, &(seqParam->num_ref_frames_in_pic_order_cnt_cycle));
 106
 107        for (i = 0; i < (int)(seqParam->num_ref_frames_in_pic_order_cnt_cycle); i++)
 108        {
 109            se_v32bit(stream, &(seqParam->offset_for_ref_frame[i]));
 110        }
 111    }
 112
 113    ue_v(stream, &(seqParam->num_ref_frames));
 114
 115    if (seqParam->num_ref_frames > 16)
 116    {
 117        return AVCDEC_FAIL;
 118    }
 119
 120    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "check point 2", seqParam->num_ref_frames, -1);
 121
 122    BitstreamRead1Bit(stream, (uint*)&(seqParam->gaps_in_frame_num_value_allowed_flag));
 123    ue_v(stream, &(seqParam->pic_width_in_mbs_minus1));
 124
 125    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "picwidth", seqParam->pic_width_in_mbs_minus1, -1);
 126
 127    ue_v(stream, &(seqParam->pic_height_in_map_units_minus1));
 128
 129    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "picwidth", seqParam->pic_height_in_map_units_minus1, -1);
 130
 131    BitstreamRead1Bit(stream, (uint*)&(seqParam->frame_mbs_only_flag));
 132
 133    seqParam->mb_adaptive_frame_field_flag = 0; /* default value */
 134    if (!seqParam->frame_mbs_only_flag)
 135    {
 136        BitstreamRead1Bit(stream, (uint*)&(seqParam->mb_adaptive_frame_field_flag));
 137    }
 138
 139    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "check point 3", seqParam->frame_mbs_only_flag, -1);
 140
 141    BitstreamRead1Bit(stream, (uint*)&(seqParam->direct_8x8_inference_flag));
 142
 143    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "check point 4", seqParam->direct_8x8_inference_flag, -1);
 144
 145    BitstreamRead1Bit(stream, (uint*)&(seqParam->frame_cropping_flag));
 146    seqParam->frame_crop_left_offset = 0;  /* default value */
 147    seqParam->frame_crop_right_offset = 0;/* default value */
 148    seqParam->frame_crop_top_offset = 0;/* default value */
 149    seqParam->frame_crop_bottom_offset = 0;/* default value */
 150    if (seqParam->frame_cropping_flag)
 151    {
 152        ue_v(stream, &(seqParam->frame_crop_left_offset));
 153        ue_v(stream, &(seqParam->frame_crop_right_offset));
 154        ue_v(stream, &(seqParam->frame_crop_top_offset));
 155        ue_v(stream, &(seqParam->frame_crop_bottom_offset));
 156    }
 157
 158    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "check point 5", seqParam->frame_cropping_flag, -1);
 159
 160    BitstreamRead1Bit(stream, (uint*)&(seqParam->vui_parameters_present_flag));
 161    if (seqParam->vui_parameters_present_flag)
 162    {
 163        status = vui_parameters(decvid, stream, seqParam);
 164        if (status != AVCDEC_SUCCESS)
 165        {
 166            return AVCDEC_FAIL;
 167        }
 168    }
 169
 170    return status;
 171}
 172
 173
 174AVCDec_Status vui_parameters(AVCDecObject *decvid, AVCDecBitstream *stream, AVCSeqParamSet *currSPS)
 175{
 176    uint temp;
 177    uint temp32;
 178    uint aspect_ratio_idc, overscan_appopriate_flag, video_format, video_full_range_flag;
 179    /* aspect_ratio_info_present_flag */
 180    BitstreamRead1Bit(stream, &temp);
 181    if (temp)
 182    {
 183        BitstreamReadBits(stream, 8, &aspect_ratio_idc);
 184        if (aspect_ratio_idc == 255)
 185        {
 186            /* sar_width */
 187            BitstreamReadBits(stream, 16, &temp);
 188            /* sar_height */
 189            BitstreamReadBits(stream, 16, &temp);
 190        }
 191    }
 192    /* overscan_info_present */
 193    BitstreamRead1Bit(stream, &temp);
 194    if (temp)
 195    {
 196        BitstreamRead1Bit(stream, &overscan_appopriate_flag);
 197    }
 198    /* video_signal_type_present_flag */
 199    BitstreamRead1Bit(stream, &temp);
 200    if (temp)
 201    {
 202        BitstreamReadBits(stream, 3, &video_format);
 203        BitstreamRead1Bit(stream, &video_full_range_flag);
 204        /* colour_description_present_flag */
 205        BitstreamRead1Bit(stream, &temp);
 206        if (temp)
 207        {
 208            /* colour_primaries */
 209            BitstreamReadBits(stream, 8, &temp);
 210            /* transfer_characteristics */
 211            BitstreamReadBits(stream, 8, &temp);
 212            /* matrix coefficients */
 213            BitstreamReadBits(stream, 8, &temp);
 214        }
 215    }
 216    /*  chroma_loc_info_present_flag */
 217    BitstreamRead1Bit(stream, &temp);
 218    if (temp)
 219    {
 220        /*  chroma_sample_loc_type_top_field */
 221        ue_v(stream, &temp);
 222        /*  chroma_sample_loc_type_bottom_field */
 223        ue_v(stream, &temp);
 224    }
 225
 226    /*  timing_info_present_flag*/
 227    BitstreamRead1Bit(stream, &temp);
 228    if (temp)
 229    {
 230        /*  num_unit_in_tick*/
 231        BitstreamReadBits(stream, 32, &temp32);
 232        /*  time_scale */
 233        BitstreamReadBits(stream, 32, &temp32);
 234        /*  fixed_frame_rate_flag */
 235        BitstreamRead1Bit(stream, &temp);
 236    }
 237
 238    /*  nal_hrd_parameters_present_flag */
 239    BitstreamRead1Bit(stream, &temp);
 240    currSPS->vui_parameters.nal_hrd_parameters_present_flag = temp;
 241    if (temp)
 242    {
 243        hrd_parameters(decvid, stream, &(currSPS->vui_parameters.nal_hrd_parameters));
 244    }
 245    /*  vcl_hrd_parameters_present_flag*/
 246    BitstreamRead1Bit(stream, &temp);
 247    currSPS->vui_parameters.vcl_hrd_parameters_present_flag = temp;
 248    if (temp)
 249    {
 250        hrd_parameters(decvid, stream, &(currSPS->vui_parameters.vcl_hrd_parameters));
 251    }
 252    if (currSPS->vui_parameters.nal_hrd_parameters_present_flag || currSPS->vui_parameters.vcl_hrd_parameters_present_flag)
 253    {
 254        /*  low_delay_hrd_flag */
 255        BitstreamRead1Bit(stream, &temp);
 256    }
 257    /*  pic_struct_present_flag */
 258    BitstreamRead1Bit(stream, &temp);
 259    currSPS->vui_parameters.pic_struct_present_flag = temp;
 260    /*  bitstream_restriction_flag */
 261    BitstreamRead1Bit(stream, &temp);
 262    if (temp)
 263    {
 264        /*  motion_vectors_over_pic_boundaries_flag */
 265        BitstreamRead1Bit(stream, &temp);
 266        /*  max_bytes_per_pic_denom */
 267        ue_v(stream, &temp);
 268        /*  max_bits_per_mb_denom */
 269        ue_v(stream, &temp);
 270        /*  log2_max_mv_length_horizontal */
 271        ue_v(stream, &temp);
 272        /*  log2_max_mv_length_vertical */
 273        ue_v(stream, &temp);
 274        /*  num_reorder_frames */
 275        ue_v(stream, &temp);
 276        /*  max_dec_frame_buffering */
 277        ue_v(stream, &temp);
 278    }
 279    return AVCDEC_SUCCESS;
 280}
 281AVCDec_Status hrd_parameters(AVCDecObject *decvid, AVCDecBitstream *stream, AVCHRDParams *HRDParam)
 282{
 283    OSCL_UNUSED_ARG(decvid);
 284    uint temp;
 285    uint cpb_cnt_minus1;
 286    uint i;
 287    ue_v(stream, &cpb_cnt_minus1);
 288    HRDParam->cpb_cnt_minus1 = cpb_cnt_minus1;
 289    /*  bit_rate_scale */
 290    BitstreamReadBits(stream, 4, &temp);
 291    /*  cpb_size_scale */
 292    BitstreamReadBits(stream, 4, &temp);
 293    for (i = 0; i <= cpb_cnt_minus1; i++)
 294    {
 295        /*  bit_rate_value_minus1[i] */
 296        ue_v(stream, &temp);
 297        /*  cpb_size_value_minus1[i] */
 298        ue_v(stream, &temp);
 299        /*  cbr_flag[i] */
 300        ue_v(stream, &temp);
 301    }
 302    /*  initial_cpb_removal_delay_length_minus1 */
 303    BitstreamReadBits(stream, 5, &temp);
 304    /*  cpb_removal_delay_length_minus1 */
 305    BitstreamReadBits(stream, 5, &temp);
 306    HRDParam->cpb_removal_delay_length_minus1 = temp;
 307    /*  dpb_output_delay_length_minus1 */
 308    BitstreamReadBits(stream, 5, &temp);
 309    HRDParam->dpb_output_delay_length_minus1 = temp;
 310    /*  time_offset_length  */
 311    BitstreamReadBits(stream, 5, &temp);
 312    HRDParam->time_offset_length = temp;
 313    return AVCDEC_SUCCESS;
 314}
 315
 316
 317/** see subclause 7.4.2.2 */
 318AVCDec_Status DecodePPS(AVCDecObject *decvid, AVCCommonObj *video, AVCDecBitstream *stream)
 319{
 320    AVCPicParamSet *picParam;
 321    AVCDec_Status status;
 322    int i, iGroup, numBits;
 323    int PicWidthInMbs, PicHeightInMapUnits, PicSizeInMapUnits;
 324    uint pic_parameter_set_id, seq_parameter_set_id;
 325    void *userData = decvid->avcHandle->userData;
 326    AVCHandle *avcHandle = decvid->avcHandle;
 327
 328    ue_v(stream, &pic_parameter_set_id);
 329    if (pic_parameter_set_id > 255)
 330    {
 331        return AVCDEC_FAIL;
 332    }
 333
 334    ue_v(stream, &seq_parameter_set_id);
 335
 336    if (seq_parameter_set_id > 31)
 337    {
 338        return AVCDEC_FAIL;
 339    }
 340
 341    /* 2.1 if picParams[pic_param_set_id] is NULL, allocate it. */
 342    if (decvid->picParams[pic_parameter_set_id] == NULL)
 343    {
 344        decvid->picParams[pic_parameter_set_id] =
 345            (AVCPicParamSet*)avcHandle->CBAVC_Malloc(userData, sizeof(AVCPicParamSet), DEFAULT_ATTR);
 346        if (decvid->picParams[pic_parameter_set_id] == NULL)
 347        {
 348            return AVCDEC_MEMORY_FAIL;
 349        }
 350
 351        decvid->picParams[pic_parameter_set_id]->slice_group_id = NULL;
 352    }
 353
 354    video->currPicParams = picParam = decvid->picParams[pic_parameter_set_id];
 355    picParam->seq_parameter_set_id = seq_parameter_set_id;
 356    picParam->pic_parameter_set_id = pic_parameter_set_id;
 357
 358    BitstreamRead1Bit(stream, (uint*)&(picParam->entropy_coding_mode_flag));
 359    if (picParam->entropy_coding_mode_flag)
 360    {
 361        status = AVCDEC_FAIL;
 362        goto clean_up;
 363    }
 364    BitstreamRead1Bit(stream, (uint*)&(picParam->pic_order_present_flag));
 365    ue_v(stream, &(picParam->num_slice_groups_minus1));
 366
 367    if (picParam->num_slice_groups_minus1 > MAX_NUM_SLICE_GROUP - 1)
 368    {
 369        status = AVCDEC_FAIL;
 370        goto clean_up;
 371    }
 372
 373    picParam->slice_group_change_rate_minus1 = 0; /* default value */
 374    if (picParam->num_slice_groups_minus1 > 0)
 375    {
 376        ue_v(stream, &(picParam->slice_group_map_type));
 377        if (picParam->slice_group_map_type == 0)
 378        {
 379            for (iGroup = 0; iGroup <= (int)picParam->num_slice_groups_minus1; iGroup++)
 380            {
 381                ue_v(stream, &(picParam->run_length_minus1[iGroup]));
 382            }
 383        }
 384        else if (picParam->slice_group_map_type == 2)
 385        {   // MC_CHECK  <= or <
 386            for (iGroup = 0; iGroup < (int)picParam->num_slice_groups_minus1; iGroup++)
 387            {
 388                ue_v(stream, &(picParam->top_left[iGroup]));
 389                ue_v(stream, &(picParam->bottom_right[iGroup]));
 390            }
 391        }
 392        else if (picParam->slice_group_map_type == 3 ||
 393                 picParam->slice_group_map_type == 4 ||
 394                 picParam->slice_group_map_type == 5)
 395        {
 396            BitstreamRead1Bit(stream, (uint*)&(picParam->slice_group_change_direction_flag));
 397            ue_v(stream, &(picParam->slice_group_change_rate_minus1));
 398        }
 399        else if (picParam->slice_group_map_type == 6)
 400        {
 401            ue_v(stream, &(picParam->pic_size_in_map_units_minus1));
 402
 403            numBits = 0;/* ceil(log2(num_slice_groups_minus1+1)) bits */
 404            i = picParam->num_slice_groups_minus1;
 405            while (i > 0)
 406            {
 407                numBits++;
 408                i >>= 1;
 409            }
 410
 411            i = picParam->seq_parameter_set_id;
 412            if (decvid->seqParams[i] == NULL)
 413            {
 414                status = AVCDEC_FAIL;
 415                goto clean_up;
 416            }
 417
 418
 419            PicWidthInMbs = decvid->seqParams[i]->pic_width_in_mbs_minus1 + 1;
 420            PicHeightInMapUnits = decvid->seqParams[i]->pic_height_in_map_units_minus1 + 1 ;
 421            PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits ;
 422
 423            /* information has to be consistent with the seq_param */
 424            if ((int)picParam->pic_size_in_map_units_minus1 != PicSizeInMapUnits - 1)
 425            {
 426                status = AVCDEC_FAIL;
 427                goto clean_up;
 428            }
 429
 430            if (picParam->slice_group_id)
 431            {
 432                avcHandle->CBAVC_Free(userData, (uintptr_t)picParam->slice_group_id);
 433            }
 434            picParam->slice_group_id = (uint*)avcHandle->CBAVC_Malloc(userData, sizeof(uint) * PicSizeInMapUnits, DEFAULT_ATTR);
 435            if (picParam->slice_group_id == NULL)
 436            {
 437                status =  AVCDEC_MEMORY_FAIL;
 438                goto clean_up;
 439            }
 440
 441            for (i = 0; i < PicSizeInMapUnits; i++)
 442            {
 443                BitstreamReadBits(stream, numBits, &(picParam->slice_group_id[i]));
 444            }
 445        }
 446
 447    }
 448
 449    ue_v(stream, &(picParam->num_ref_idx_l0_active_minus1));
 450    if (picParam->num_ref_idx_l0_active_minus1 > 31)
 451    {
 452        status = AVCDEC_FAIL; /* out of range */
 453        goto clean_up;
 454    }
 455
 456    ue_v(stream, &(picParam->num_ref_idx_l1_active_minus1));
 457    if (picParam->num_ref_idx_l1_active_minus1 > 31)
 458    {
 459        status = AVCDEC_FAIL; /* out of range */
 460        goto clean_up;
 461    }
 462
 463    BitstreamRead1Bit(stream, (uint*)&(picParam->weighted_pred_flag));
 464    BitstreamReadBits(stream, 2, &(picParam->weighted_bipred_idc));
 465    if (picParam->weighted_bipred_idc > 2)
 466    {
 467        status = AVCDEC_FAIL; /* out of range */
 468        goto clean_up;
 469    }
 470
 471    se_v(stream, &(picParam->pic_init_qp_minus26));
 472    if (picParam->pic_init_qp_minus26 < -26 || picParam->pic_init_qp_minus26 > 25)
 473    {
 474        status = AVCDEC_FAIL; /* out of range */
 475        goto clean_up;
 476    }
 477
 478    se_v(stream, &(picParam->pic_init_qs_minus26));
 479    if (picParam->pic_init_qs_minus26 < -26 || picParam->pic_init_qs_minus26 > 25)
 480    {
 481        status = AVCDEC_FAIL; /* out of range */
 482        goto clean_up;
 483    }
 484
 485    se_v(stream, &(picParam->chroma_qp_index_offset));
 486    if (picParam->chroma_qp_index_offset < -12 || picParam->chroma_qp_index_offset > 12)
 487    {
 488        status = AVCDEC_FAIL; /* out of range */
 489        status = AVCDEC_FAIL; /* out of range */
 490        goto clean_up;
 491    }
 492
 493    BitstreamReadBits(stream, 3, &pic_parameter_set_id);
 494    picParam->deblocking_filter_control_present_flag = pic_parameter_set_id >> 2;
 495    picParam->constrained_intra_pred_flag = (pic_parameter_set_id >> 1) & 1;
 496    picParam->redundant_pic_cnt_present_flag = pic_parameter_set_id & 1;
 497
 498    return AVCDEC_SUCCESS;
 499clean_up:
 500    if (decvid->picParams[pic_parameter_set_id])
 501    {
 502        if (picParam->slice_group_id)
 503        {
 504            avcHandle->CBAVC_Free(userData, (uintptr_t)picParam->slice_group_id);
 505        }
 506        decvid->picParams[pic_parameter_set_id]->slice_group_id = NULL;
 507        avcHandle->CBAVC_Free(userData, (uintptr_t)decvid->picParams[pic_parameter_set_id]);
 508        decvid->picParams[pic_parameter_set_id] = NULL;
 509        return status;
 510    }
 511    return AVCDEC_SUCCESS;
 512}
 513
 514
 515/* FirstPartOfSliceHeader();
 516    RestOfSliceHeader() */
 517/** see subclause 7.4.3 */
 518AVCDec_Status DecodeSliceHeader(AVCDecObject *decvid, AVCCommonObj *video, AVCDecBitstream *stream)
 519{
 520    AVCSliceHeader *sliceHdr = video->sliceHdr;
 521    AVCPicParamSet *currPPS;
 522    AVCSeqParamSet *currSPS;
 523    AVCDec_Status status;
 524    uint idr_pic_id;
 525    int slice_type, temp, i;
 526
 527    ue_v(stream, &(sliceHdr->first_mb_in_slice));
 528    ue_v(stream, (uint*)&slice_type);
 529
 530    if (sliceHdr->first_mb_in_slice != 0)
 531    {
 532        if ((int)sliceHdr->slice_type >= 5 && slice_type != (int)sliceHdr->slice_type - 5)
 533        {
 534            return AVCDEC_FAIL; /* slice type doesn't follow the first slice in the picture */
 535        }
 536    }
 537    sliceHdr->slice_type = (AVCSliceType) slice_type;
 538    if (slice_type > 4)
 539    {
 540        slice_type -= 5;
 541    }
 542
 543    if (slice_type == 1 || slice_type > 2)
 544    {
 545        return AVCDEC_FAIL;
 546    }
 547
 548    video->slice_type = (AVCSliceType) slice_type;
 549
 550    ue_v(stream, &(sliceHdr->pic_parameter_set_id));
 551    /* end FirstPartSliceHeader() */
 552    /* begin RestOfSliceHeader() */
 553    /* after getting pic_parameter_set_id, we have to load corresponding SPS and PPS */
 554    if (sliceHdr->pic_parameter_set_id > 255)
 555    {
 556        return AVCDEC_FAIL;
 557    }
 558
 559    if (decvid->picParams[sliceHdr->pic_parameter_set_id] == NULL)
 560        return AVCDEC_FAIL; /* PPS doesn't exist */
 561
 562    currPPS = video->currPicParams = decvid->picParams[sliceHdr->pic_parameter_set_id];
 563
 564    if (decvid->seqParams[currPPS->seq_parameter_set_id] == NULL)
 565        return AVCDEC_FAIL; /* SPS doesn't exist */
 566
 567    currSPS = video->currSeqParams = decvid->seqParams[currPPS->seq_parameter_set_id];
 568
 569    if (currPPS->seq_parameter_set_id != video->seq_parameter_set_id)
 570    {
 571        video->seq_parameter_set_id = currPPS->seq_parameter_set_id;
 572        status = (AVCDec_Status)AVCConfigureSequence(decvid->avcHandle, video, false);
 573        if (status != AVCDEC_SUCCESS)
 574            return status;
 575        video->level_idc = currSPS->level_idc;
 576    }
 577
 578    /* derived variables from SPS */
 579    video->MaxFrameNum = 1 << (currSPS->log2_max_frame_num_minus4 + 4);
 580    // MC_OPTIMIZE
 581    video->PicWidthInMbs = currSPS->pic_width_in_mbs_minus1 + 1;
 582    video->PicWidthInSamplesL = video->PicWidthInMbs * 16 ;
 583    video->PicWidthInSamplesC = video->PicWidthInMbs * 8 ;
 584    video->PicHeightInMapUnits = currSPS->pic_height_in_map_units_minus1 + 1 ;
 585    video->PicSizeInMapUnits = video->PicWidthInMbs * video->PicHeightInMapUnits ;
 586    video->FrameHeightInMbs = (2 - currSPS->frame_mbs_only_flag) * video->PicHeightInMapUnits ;
 587
 588    /* derived from PPS */
 589    video->SliceGroupChangeRate = currPPS->slice_group_change_rate_minus1 + 1;
 590
 591    /* then we can continue decoding slice header */
 592
 593    BitstreamReadBits(stream, currSPS->log2_max_frame_num_minus4 + 4, &(sliceHdr->frame_num));
 594
 595    if (video->currFS == NULL && sliceHdr->frame_num != 0)
 596    {
 597        video->prevFrameNum = video->PrevRefFrameNum = sliceHdr->frame_num - 1;
 598    }
 599
 600    if (!currSPS->frame_mbs_only_flag)
 601    {
 602        BitstreamRead1Bit(stream, &(sliceHdr->field_pic_flag));
 603        if (sliceHdr->field_pic_flag)
 604        {
 605            return AVCDEC_FAIL;
 606        }
 607    }
 608
 609    /* derived variables from slice header*/
 610    video->PicHeightInMbs = video->FrameHeightInMbs;
 611    video->PicHeightInSamplesL = video->PicHeightInMbs * 16;
 612    video->PicHeightInSamplesC = video->PicHeightInMbs * 8;
 613    video->PicSizeInMbs = video->PicWidthInMbs * video->PicHeightInMbs;
 614
 615    if (sliceHdr->first_mb_in_slice >= video->PicSizeInMbs)
 616    {
 617        return AVCDEC_FAIL;
 618    }
 619    video->MaxPicNum = video->MaxFrameNum;
 620    video->CurrPicNum = sliceHdr->frame_num;
 621
 622
 623    if (video->nal_unit_type == AVC_NALTYPE_IDR)
 624    {
 625        if (sliceHdr->frame_num != 0)
 626        {
 627            return AVCDEC_FAIL;
 628        }
 629        ue_v(stream, &idr_pic_id);
 630    }
 631
 632    sliceHdr->delta_pic_order_cnt_bottom = 0; /* default value */
 633    sliceHdr->delta_pic_order_cnt[0] = 0; /* default value */
 634    sliceHdr->delta_pic_order_cnt[1] = 0; /* default value */
 635    if (currSPS->pic_order_cnt_type == 0)
 636    {
 637        BitstreamReadBits(stream, currSPS->log2_max_pic_order_cnt_lsb_minus4 + 4,
 638                          &(sliceHdr->pic_order_cnt_lsb));
 639        video->MaxPicOrderCntLsb =  1 << (currSPS->log2_max_pic_order_cnt_lsb_minus4 + 4);
 640        if (sliceHdr->pic_order_cnt_lsb > video->MaxPicOrderCntLsb - 1)
 641            return AVCDEC_FAIL; /* out of range */
 642
 643        if (currPPS->pic_order_present_flag)
 644        {
 645            se_v32bit(stream, &(sliceHdr->delta_pic_order_cnt_bottom));
 646        }
 647    }
 648    if (currSPS->pic_order_cnt_type == 1 && !currSPS->delta_pic_order_always_zero_flag)
 649    {
 650        se_v32bit(stream, &(sliceHdr->delta_pic_order_cnt[0]));
 651        if (currPPS->pic_order_present_flag)
 652        {
 653            se_v32bit(stream, &(sliceHdr->delta_pic_order_cnt[1]));
 654        }
 655    }
 656
 657    sliceHdr->redundant_pic_cnt = 0; /* default value */
 658    if (currPPS->redundant_pic_cnt_present_flag)
 659    {
 660        // MC_CHECK
 661        ue_v(stream, &(sliceHdr->redundant_pic_cnt));
 662        if (sliceHdr->redundant_pic_cnt > 127) /* out of range */
 663            return AVCDEC_FAIL;
 664
 665        if (sliceHdr->redundant_pic_cnt > 0) /* redundant picture */
 666            return AVCDEC_FAIL; /* not supported */
 667    }
 668    sliceHdr->num_ref_idx_l0_active_minus1 = currPPS->num_ref_idx_l0_active_minus1;
 669    sliceHdr->num_ref_idx_l1_active_minus1 = currPPS->num_ref_idx_l1_active_minus1;
 670
 671    if (slice_type == AVC_P_SLICE)
 672    {
 673        BitstreamRead1Bit(stream, &(sliceHdr->num_ref_idx_active_override_flag));
 674        if (sliceHdr->num_ref_idx_active_override_flag)
 675        {
 676            ue_v(stream, &(sliceHdr->num_ref_idx_l0_active_minus1));
 677        }
 678        else  /* the following condition is not allowed if the flag is zero */
 679        {
 680            if ((slice_type == AVC_P_SLICE) && currPPS->num_ref_idx_l0_active_minus1 > 15)
 681            {
 682                return AVCDEC_FAIL; /* not allowed */
 683            }
 684        }
 685    }
 686
 687
 688    if (sliceHdr->num_ref_idx_l0_active_minus1 > 15 ||
 689            sliceHdr->num_ref_idx_l1_active_minus1 > 15)
 690    {
 691        return AVCDEC_FAIL; /* not allowed */
 692    }
 693    /* if MbaffFrameFlag =1,
 694    max value of index is num_ref_idx_l0_active_minus1 for frame MBs and
 695    2*sliceHdr->num_ref_idx_l0_active_minus1 + 1 for field MBs */
 696
 697    /* ref_pic_list_reordering() */
 698    status = ref_pic_list_reordering(video, stream, sliceHdr, slice_type);
 699    if (status != AVCDEC_SUCCESS)
 700    {
 701        return status;
 702    }
 703
 704
 705    if (video->nal_ref_idc != 0)
 706    {
 707        dec_ref_pic_marking(video, stream, sliceHdr);
 708    }
 709    se_v(stream, &(sliceHdr->slice_qp_delta));
 710
 711    video->QPy = 26 + currPPS->pic_init_qp_minus26 + sliceHdr->slice_qp_delta;
 712    if (video->QPy > 51 || video->QPy < 0)
 713    {
 714        video->QPy = AVC_CLIP3(0, 51, video->QPy);
 715//                  return AVCDEC_FAIL;
 716    }
 717    video->QPc = mapQPi2QPc[AVC_CLIP3(0, 51, video->QPy + video->currPicParams->chroma_qp_index_offset)];
 718
 719    video->QPy_div_6 = (video->QPy * 43) >> 8;
 720    video->QPy_mod_6 = video->QPy - 6 * video->QPy_div_6;
 721
 722    video->QPc_div_6 = (video->QPc * 43) >> 8;
 723    video->QPc_mod_6 = video->QPc - 6 * video->QPc_div_6;
 724
 725    sliceHdr->slice_alpha_c0_offset_div2 = 0;
 726    sliceHdr->slice_beta_offset_div_2 = 0;
 727    sliceHdr->disable_deblocking_filter_idc = 0;
 728    video->FilterOffsetA = video->FilterOffsetB = 0;
 729
 730    if (currPPS->deblocking_filter_control_present_flag)
 731    {
 732        ue_v(stream, &(sliceHdr->disable_deblocking_filter_idc));
 733        if (sliceHdr->disable_deblocking_filter_idc > 2)
 734        {
 735            return AVCDEC_FAIL; /* out of range */
 736        }
 737        if (sliceHdr->disable_deblocking_filter_idc != 1)
 738        {
 739            se_v(stream, &(sliceHdr->slice_alpha_c0_offset_div2));
 740            if (sliceHdr->slice_alpha_c0_offset_div2 < -6 ||
 741                    sliceHdr->slice_alpha_c0_offset_div2 > 6)
 742            {
 743                return AVCDEC_FAIL;
 744            }
 745            video->FilterOffsetA = sliceHdr->slice_alpha_c0_offset_div2 << 1;
 746
 747            se_v(stream, &(sliceHdr->slice_beta_offset_div_2));
 748            if (sliceHdr->slice_beta_offset_div_2 < -6 ||
 749                    sliceHdr->slice_beta_offset_div_2 > 6)
 750            {
 751                return AVCDEC_FAIL;
 752            }
 753            video->FilterOffsetB = sliceHdr->slice_beta_offset_div_2 << 1;
 754        }
 755    }
 756
 757    if (currPPS->num_slice_groups_minus1 > 0 && currPPS->slice_group_map_type >= 3
 758            && currPPS->slice_group_map_type <= 5)
 759    {
 760        /* Ceil(Log2(PicSizeInMapUnits/(float)SliceGroupChangeRate + 1)) */
 761        temp = video->PicSizeInMapUnits / video->SliceGroupChangeRate;
 762        if (video->PicSizeInMapUnits % video->SliceGroupChangeRate)
 763        {
 764            temp++;
 765        }
 766        i = 0;
 767        temp++;
 768        while (temp)
 769        {
 770            temp >>= 1;
 771            i++;
 772        }
 773
 774        BitstreamReadBits(stream, i, &(sliceHdr->slice_group_change_cycle));
 775        video->MapUnitsInSliceGroup0 =
 776            AVC_MIN(sliceHdr->slice_group_change_cycle * video->SliceGroupChangeRate, video->PicSizeInMapUnits);
 777    }
 778
 779    return AVCDEC_SUCCESS;
 780}
 781
 782
 783AVCDec_Status fill_frame_num_gap(AVCHandle *avcHandle, AVCCommonObj *video)
 784{
 785    AVCDec_Status status;
 786    int CurrFrameNum;
 787    int UnusedShortTermFrameNum;
 788    int tmp1 = video->sliceHdr->delta_pic_order_cnt[0];
 789    int tmp2 = video->sliceHdr->delta_pic_order_cnt[1];
 790    int tmp3 = video->CurrPicNum;
 791    int tmp4 = video->sliceHdr->adaptive_ref_pic_marking_mode_flag;
 792    UnusedShortTermFrameNum = (video->prevFrameNum + 1) % video->MaxFrameNum;
 793    CurrFrameNum = video->sliceHdr->frame_num;
 794
 795    video->sliceHdr->delta_pic_order_cnt[0] = 0;
 796    video->sliceHdr->delta_pic_order_cnt[1] = 0;
 797    while (CurrFrameNum != UnusedShortTermFrameNum)
 798    {
 799        video->CurrPicNum = UnusedShortTermFrameNum;
 800        video->sliceHdr->frame_num = UnusedShortTermFrameNum;
 801
 802        status = (AVCDec_Status)DPBInitBuffer(avcHandle, video);
 803        if (status != AVCDEC_SUCCESS)  /* no buffer available */
 804        {
 805            return status;
 806        }
 807        DecodePOC(video);
 808        DPBInitPic(video, UnusedShortTermFrameNum);
 809
 810
 811        video->currFS->PicOrderCnt = video->PicOrderCnt;
 812        video->currFS->FrameNum = video->sliceHdr->frame_num;
 813
 814        /* initialize everything to zero */
 815        video->currFS->IsOutputted = 0x01;
 816        video->currFS->IsReference = 3;
 817        video->currFS->IsLongTerm = 0;
 818        video->currFS->frame.isReference = TRUE;
 819        video->currFS->frame.isLongTerm = FALSE;
 820
 821        video->sliceHdr->adaptive_ref_pic_marking_mode_flag = 0;
 822
 823        status = (AVCDec_Status)StorePictureInDPB(avcHandle, video);  // MC_CHECK check the return status
 824        if (status != AVCDEC_SUCCESS)
 825        {
 826            return AVCDEC_FAIL;
 827        }
 828        video->prevFrameNum = UnusedShortTermFrameNum;
 829        UnusedShortTermFrameNum = (UnusedShortTermFrameNum + 1) % video->MaxFrameNum;
 830    }
 831    video->sliceHdr->frame_num = CurrFrameNum;
 832    video->CurrPicNum = tmp3;
 833    video->sliceHdr->delta_pic_order_cnt[0] = tmp1;
 834    video->sliceHdr->delta_pic_order_cnt[1] = tmp2;
 835    video->sliceHdr->adaptive_ref_pic_marking_mode_flag = tmp4;
 836    return AVCDEC_SUCCESS;
 837}
 838
 839/** see subclause 7.4.3.1 */
 840AVCDec_Status ref_pic_list_reordering(AVCCommonObj *video, AVCDecBitstream *stream, AVCSliceHeader *sliceHdr, int slice_type)
 841{
 842    int i;
 843
 844    if (slice_type != AVC_I_SLICE)
 845    {
 846        BitstreamRead1Bit(stream, &(sliceHdr->ref_pic_list_reordering_flag_l0));
 847        if (sliceHdr->ref_pic_list_reordering_flag_l0)
 848        {
 849            i = 0;
 850            do
 851            {
 852                ue_v(stream, &(sliceHdr->reordering_of_pic_nums_idc_l0[i]));
 853                if (sliceHdr->reordering_of_pic_nums_idc_l0[i] == 0 ||
 854                        sliceHdr->reordering_of_pic_nums_idc_l0[i] == 1)
 855                {
 856                    ue_v(stream, &(sliceHdr->abs_diff_pic_num_minus1_l0[i]));
 857                    if (sliceHdr->reordering_of_pic_nums_idc_l0[i] == 0 &&
 858                            sliceHdr->abs_diff_pic_num_minus1_l0[i] > video->MaxPicNum / 2 - 1)
 859                    {
 860                        return AVCDEC_FAIL; /* out of range */
 861                    }
 862                    if (sliceHdr->reordering_of_pic_nums_idc_l0[i] == 1 &&
 863                            sliceHdr->abs_diff_pic_num_minus1_l0[i] > video->MaxPicNum / 2 - 2)
 864                    {
 865                        return AVCDEC_FAIL; /* out of range */
 866                    }
 867                }
 868                else if (sliceHdr->reordering_of_pic_nums_idc_l0[i] == 2)
 869                {
 870                    ue_v(stream, &(sliceHdr->long_term_pic_num_l0[i]));
 871                }
 872                i++;
 873            }
 874            while (sliceHdr->reordering_of_pic_nums_idc_l0[i-1] != 3
 875                    && i <= (int)sliceHdr->num_ref_idx_l0_active_minus1 + 1) ;
 876        }
 877    }
 878    return AVCDEC_SUCCESS;
 879}
 880
 881/** see subclause 7.4.3.3 */
 882AVCDec_Status dec_ref_pic_marking(AVCCommonObj *video, AVCDecBitstream *stream, AVCSliceHeader *sliceHdr)
 883{
 884    int i;
 885    if (video->nal_unit_type == AVC_NALTYPE_IDR)
 886    {
 887        BitstreamRead1Bit(stream, &(sliceHdr->no_output_of_prior_pics_flag));
 888        BitstreamRead1Bit(stream, &(sliceHdr->long_term_reference_flag));
 889        if (sliceHdr->long_term_reference_flag == 0) /* used for short-term */
 890        {
 891            video->MaxLongTermFrameIdx = -1; /* no long-term frame indx */
 892        }
 893        else /* used for long-term */
 894        {
 895            video->MaxLongTermFrameIdx = 0;
 896            video->LongTermFrameIdx = 0;
 897        }
 898    }
 899    else
 900    {
 901        BitstreamRead1Bit(stream, &(sliceHdr->adaptive_ref_pic_marking_mode_flag));
 902        if (sliceHdr->adaptive_ref_pic_marking_mode_flag)
 903        {
 904            i = 0;
 905            do
 906            {
 907                ue_v(stream, &(sliceHdr->memory_management_control_operation[i]));
 908                if (sliceHdr->memory_management_control_operation[i] == 1 ||
 909                        sliceHdr->memory_management_control_operation[i] == 3)
 910                {
 911                    ue_v(stream, &(sliceHdr->difference_of_pic_nums_minus1[i]));
 912                }
 913                if (sliceHdr->memory_management_control_operation[i] == 2)
 914                {
 915                    ue_v(stream, &(sliceHdr->long_term_pic_num[i]));
 916                }
 917                if (sliceHdr->memory_management_control_operation[i] == 3 ||
 918                        sliceHdr->memory_management_control_operation[i] == 6)
 919                {
 920                    ue_v(stream, &(sliceHdr->long_term_frame_idx[i]));
 921                }
 922                if (sliceHdr->memory_management_control_operation[i] == 4)
 923                {
 924                    ue_v(stream, &(sliceHdr->max_long_term_frame_idx_plus1[i]));
 925                }
 926                i++;
 927            }
 928            while (sliceHdr->memory_management_control_operation[i-1] != 0 && i < MAX_DEC_REF_PIC_MARKING);
 929            if (i >= MAX_DEC_REF_PIC_MARKING)
 930            {
 931                return AVCDEC_FAIL; /* we're screwed!!, not enough memory */
 932            }
 933        }
 934    }
 935
 936    return AVCDEC_SUCCESS;
 937}
 938
 939/* see subclause 8.2.1 Decoding process for picture order count. */
 940AVCDec_Status DecodePOC(AVCCommonObj *video)
 941{
 942    AVCSeqParamSet *currSPS = video->currSeqParams;
 943    AVCSliceHeader *sliceHdr = video->sliceHdr;
 944    int i;
 945
 946    switch (currSPS->pic_order_cnt_type)
 947    {
 948        case 0: /* POC MODE 0 , subclause 8.2.1.1 */
 949            if (video->nal_unit_type == AVC_NALTYPE_IDR)
 950            {
 951                video->prevPicOrderCntMsb = 0;
 952                video->prevPicOrderCntLsb = 0;
 953            }
 954
 955            /* Calculate the MSBs of current picture */
 956            if (sliceHdr->pic_order_cnt_lsb  <  video->prevPicOrderCntLsb  &&
 957                    (video->prevPicOrderCntLsb - sliceHdr->pic_order_cnt_lsb)  >= (video->MaxPicOrderCntLsb / 2))
 958                video->PicOrderCntMsb = video->prevPicOrderCntMsb + video->MaxPicOrderCntLsb;
 959            else if (sliceHdr->pic_order_cnt_lsb  >  video->prevPicOrderCntLsb  &&
 960                     (sliceHdr->pic_order_cnt_lsb - video->prevPicOrderCntLsb)  > (video->MaxPicOrderCntLsb / 2))
 961                video->PicOrderCntMsb = video->prevPicOrderCntMsb - video->MaxPicOrderCntLsb;
 962            else
 963                video->PicOrderCntMsb = video->prevPicOrderCntMsb;
 964
 965            /* JVT-I010 page 81 is different from JM7.3 */
 966
 967
 968            video->PicOrderCnt = video->TopFieldOrderCnt = video->PicOrderCntMsb + sliceHdr->pic_order_cnt_lsb;
 969            video->BottomFieldOrderCnt = video->TopFieldOrderCnt + sliceHdr->delta_pic_order_cnt_bottom;
 970
 971            break;
 972
 973
 974        case 1: /* POC MODE 1, subclause 8.2.1.2 */
 975            /* calculate FrameNumOffset */
 976            if (video->nal_unit_type == AVC_NALTYPE_IDR)
 977            {
 978                video->prevFrameNumOffset = 0;
 979                video->FrameNumOffset = 0;
 980            }
 981            else if (video->prevFrameNum > sliceHdr->frame_num)
 982            {
 983                video->FrameNumOffset = video->prevFrameNumOffset + video->MaxFrameNum;
 984            }
 985            else
 986            {
 987                video->FrameNumOffset = video->prevFrameNumOffset;
 988            }
 989            /* calculate absFrameNum */
 990            if (currSPS->num_ref_frames_in_pic_order_cnt_cycle)
 991            {
 992                video->absFrameNum = video->FrameNumOffset + sliceHdr->frame_num;
 993            }
 994            else
 995            {
 996                video->absFrameNum = 0;
 997            }
 998
 999            if (video->absFrameNum > 0 && video->nal_ref_idc == 0)
1000            {
1001                video->absFrameNum--;
1002            }
1003
1004            /* derive picOrderCntCycleCnt and frameNumInPicOrderCntCycle */
1005            if (video->absFrameNum > 0)
1006            {
1007                video->picOrderCntCycleCnt = (video->absFrameNum - 1) / currSPS->num_ref_frames_in_pic_order_cnt_cycle;
1008                video->frameNumInPicOrderCntCycle = (video->absFrameNum - 1) % currSPS->num_ref_frames_in_pic_order_cnt_cycle;
1009            }
1010            /* derive expectedDeltaPerPicOrderCntCycle */
1011            video->expectedDeltaPerPicOrderCntCycle = 0;
1012            for (i = 0; i < (int)currSPS->num_ref_frames_in_pic_order_cnt_cycle; i++)
1013            {
1014                video->expectedDeltaPerPicOrderCntCycle += currSPS->offset_for_ref_frame[i];
1015            }
1016            /* derive expectedPicOrderCnt */
1017            if (video->absFrameNum)
1018            {
1019                video->expectedPicOrderCnt = video->picOrderCntCycleCnt * video->expectedDeltaPerPicOrderCntCycle;
1020                for (i = 0; i <= video->frameNumInPicOrderCntCycle; i++)
1021                {
1022                    video->expectedPicOrderCnt += currSPS->offset_for_ref_frame[i];
1023                }
1024            }
1025            else
1026            {
1027                video->expectedPicOrderCnt = 0;
1028            }
1029
1030            if (video->nal_ref_idc == 0)
1031            {
1032                video->expectedPicOrderCnt += currSPS->offset_for_non_ref_pic;
1033            }
1034            /* derive TopFieldOrderCnt and BottomFieldOrderCnt */
1035
1036            video->TopFieldOrderCnt = video->expectedPicOrderCnt + sliceHdr->delta_pic_order_cnt[0];
1037            video->BottomFieldOrderCnt = video->TopFieldOrderCnt + currSPS->offset_for_top_to_bottom_field + sliceHdr->delta_pic_order_cnt[1];
1038
1039            video->PicOrderCnt = AVC_MIN(video->TopFieldOrderCnt, video->BottomFieldOrderCnt);
1040
1041
1042            break;
1043
1044
1045        case 2: /* POC MODE 2, subclause 8.2.1.3 */
1046            if (video->nal_unit_type == AVC_NALTYPE_IDR)
1047            {
1048                video->FrameNumOffset = 0;
1049            }
1050            else if (video->prevFrameNum > sliceHdr->frame_num)
1051            {
1052                video->FrameNumOffset = video->prevFrameNumOffset + video->MaxFrameNum;
1053            }
1054            else
1055            {
1056                video->FrameNumOffset = video->prevFrameNumOffset;
1057            }
1058            /* derive tempPicOrderCnt, we just use PicOrderCnt */
1059            if (video->nal_unit_type == AVC_NALTYPE_IDR)
1060            {
1061                video->PicOrderCnt = 0;
1062            }
1063            else if (video->nal_ref_idc == 0)
1064            {
1065                video->PicOrderCnt = 2 * (video->FrameNumOffset + sliceHdr->frame_num) - 1;
1066            }
1067            else
1068            {
1069                video->PicOrderCnt = 2 * (video->FrameNumOffset + sliceHdr->frame_num);
1070            }
1071            video->TopFieldOrderCnt = video->BottomFieldOrderCnt = video->PicOrderCnt;
1072            break;
1073        default:
1074            return AVCDEC_FAIL;
1075    }
1076
1077    return AVCDEC_SUCCESS;
1078}
1079
1080
1081AVCDec_Status DecodeSEI(AVCDecObject *decvid, AVCDecBitstream *stream)
1082{
1083    OSCL_UNUSED_ARG(decvid);
1084    OSCL_UNUSED_ARG(stream);
1085    return AVCDEC_SUCCESS;
1086}
1087
1088AVCDec_Status sei_payload(AVCDecObject *decvid, AVCDecBitstream *stream, uint payloadType, uint payloadSize)
1089{
1090    AVCDec_Status status = AVCDEC_SUCCESS;
1091    uint i;
1092    switch (payloadType)
1093    {
1094        case 0:
1095            /*  buffering period SEI */
1096            status = buffering_period(decvid, stream);
1097            break;
1098        case 1:
1099            /*  picture timing SEI */
1100            status = pic_timing(decvid, stream);
1101            break;
1102        case 2:
1103
1104        case 3:
1105
1106        case 4:
1107
1108        case 5:
1109
1110        case 8:
1111
1112        case 9:
1113
1114        case 10:
1115
1116        case 11:
1117
1118        case 12:
1119
1120        case 13:
1121
1122        case 14:
1123
1124        case 15:
1125
1126        case 16:
1127
1128        case 17:
1129            for (i = 0; i < payloadSize; i++)
1130            {
1131                BitstreamFlushBits(stream, 8);
1132            }
1133            break;
1134        case 6:
1135            /*      recovery point SEI              */
1136            status = recovery_point(decvid, stream);
1137            break;
1138        case 7:
1139            /*      decoded reference picture marking repetition SEI */
1140            status = dec_ref_pic_marking_repetition(decvid, stream);
1141            break;
1142
1143        case 18:
1144            /*      motion-constrained slice group set SEI */
1145            status = motion_constrained_slice_group_set(decvid, stream);
1146            break;
1147        default:
1148            /*          reserved_sei_message */
1149            for (i = 0; i < payloadSize; i++)
1150            {
1151                BitstreamFlushBits(stream, 8);
1152            }
1153            break;
1154    }
1155    BitstreamByteAlign(stream);
1156    return status;
1157}
1158
1159AVCDec_Status buffering_period(AVCDecObject *decvid, AVCDecBitstream *stream)
1160{
1161    AVCSeqParamSet *currSPS;
1162    uint seq_parameter_set_id;
1163    uint temp;
1164    uint i;
1165    ue_v(stream, &seq_parameter_set_id);
1166    if (seq_parameter_set_id > 31)
1167    {
1168        return AVCDEC_FAIL;
1169    }
1170
1171//  decvid->common->seq_parameter_set_id = seq_parameter_set_id;
1172
1173    currSPS = decvid->seqParams[seq_parameter_set_id];
1174    if (currSPS->vui_parameters.nal_hrd_parameters_present_flag)
1175    {
1176        for (i = 0; i <= currSPS->vui_parameters.nal_hrd_parameters.cpb_cnt_minus1; i++)
1177        {
1178            /* initial_cpb_removal_delay[i] */
1179            BitstreamReadBits(stream, currSPS->vui_parameters.nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1180            /*initial _cpb_removal_delay_offset[i] */
1181            BitstreamReadBits(stream, currSPS->vui_parameters.nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1182        }
1183    }
1184
1185    if (currSPS->vui_parameters.vcl_hrd_parameters_present_flag)
1186    {
1187        for (i = 0; i <= currSPS->vui_parameters.vcl_hrd_parameters.cpb_cnt_minus1; i++)
1188        {
1189            /* initial_cpb_removal_delay[i] */
1190            BitstreamReadBits(stream, currSPS->vui_parameters.vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1191            /*initial _cpb_removal_delay_offset[i] */
1192            BitstreamReadBits(stream, currSPS->vui_parameters.vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1193        }
1194    }
1195
1196    return AVCDEC_SUCCESS;
1197}
1198AVCDec_Status pic_timing(AVCDecObject *decvid, AVCDecBitstream *stream)
1199{
1200    AVCSeqParamSet *currSPS;
1201    uint temp, NumClockTs = 0, time_offset_length = 24, full_timestamp_flag;
1202    uint i;
1203
1204    currSPS = decvid->seqParams[decvid->common->seq_parameter_set_id];
1205
1206    if (currSPS->vui_parameters.nal_hrd_parameters_present_flag)
1207    {
1208        BitstreamReadBits(stream, currSPS->vui_parameters.nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1209        BitstreamReadBits(stream, currSPS->vui_parameters.nal_hrd_parameters.dpb_output_delay_length_minus1 + 1, &temp);
1210        time_offset_length = currSPS->vui_parameters.nal_hrd_parameters.time_offset_length;
1211    }
1212    else if (currSPS->vui_parameters.vcl_hrd_parameters_present_flag)
1213    {
1214        BitstreamReadBits(stream, currSPS->vui_parameters.vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1215        BitstreamReadBits(stream, currSPS->vui_parameters.vcl_hrd_parameters.dpb_output_delay_length_minus1 + 1, &temp);
1216        time_offset_length = currSPS->vui_parameters.vcl_hrd_parameters.time_offset_length;
1217    }
1218
1219    if (currSPS->vui_parameters.pic_struct_present_flag)
1220    {
1221        /* pic_struct */
1222        BitstreamReadBits(stream, 4, &temp);
1223
1224        switch (temp)
1225        {
1226            case 0:
1227            case 1:
1228            case 2:
1229                NumClockTs = 1;
1230                break;
1231            case 3:
1232            case 4:
1233            case 7:
1234                NumClockTs = 2;
1235                break;
1236            case 5:
1237            case 6:
1238            case 8:
1239                NumClockTs = 3;
1240                break;
1241            default:
1242                NumClockTs = 0;
1243                break;
1244        }
1245
1246        for (i = 0; i < NumClockTs; i++)
1247        {
1248            /* clock_timestamp_flag[i] */
1249            BitstreamRead1Bit(stream, &temp);
1250            if (temp)
1251            {
1252                /* ct_type */
1253                BitstreamReadBits(stream, 2, &temp);
1254                /* nuit_field_based_flag */
1255                BitstreamRead1Bit(stream, &temp);
1256                /* counting_type        */
1257                BitstreamReadBits(stream, 5, &temp);
1258                /* full_timestamp_flag */
1259                BitstreamRead1Bit(stream, &temp);
1260                full_timestamp_flag = temp;
1261                /* discontinuity_flag */
1262                BitstreamRead1Bit(stream, &temp);
1263                /* cnt_dropped_flag */
1264                BitstreamRead1Bit(stream, &temp);
1265                /* n_frames           */
1266                BitstreamReadBits(stream, 8, &temp);
1267
1268
1269                if (full_timestamp_flag)
1270                {
1271                    /* seconds_value */
1272                    BitstreamReadBits(stream, 6, &temp);
1273                    /* minutes_value */
1274                    BitstreamReadBits(stream, 6, &temp);
1275                    /* hours_value */
1276                    BitstreamReadBits(stream, 5, &temp);
1277                }
1278                else
1279                {
1280                    /* seconds_flag  */
1281                    BitstreamRead1Bit(stream, &temp);
1282                    if (temp)
1283                    {
1284                        /* seconds_value */
1285                        BitstreamReadBits(stream, 6, &temp);
1286                        /* minutes_flag  */
1287                        BitstreamRead1Bit(stream, &temp);
1288                        if (temp)
1289                        {
1290                            /* minutes_value */
1291                            BitstreamReadBits(stream, 6, &temp);
1292
1293                            /* hourss_flag  */
1294                            BitstreamRead1Bit(stream, &temp);
1295
1296                            if (temp)
1297                            {
1298                                /* hours_value */
1299                                BitstreamReadBits(stream, 5, &temp);
1300                            }
1301
1302                        }
1303                    }
1304                }
1305
1306                if (time_offset_length)
1307                {
1308                    /* time_offset */
1309                    BitstreamReadBits(stream, time_offset_length, &temp);
1310                }
1311                else
1312                {
1313                    /* time_offset */
1314                    temp = 0;
1315                }
1316            }
1317        }
1318    }
1319    return AVCDEC_SUCCESS;
1320}
1321AVCDec_Status recovery_point(AVCDecObject *decvid, AVCDecBitstream *stream)
1322{
1323    OSCL_UNUSED_ARG(decvid);
1324    uint temp;
1325    /* recover_frame_cnt */
1326    ue_v(stream, &temp);
1327    /* exact_match_flag */
1328    BitstreamRead1Bit(stream, &temp);
1329    /* broken_link_flag */
1330    BitstreamRead1Bit(stream, &temp);
1331    /* changing slic_group_idc */
1332    BitstreamReadBits(stream, 2, &temp);
1333    return AVCDEC_SUCCESS;
1334}
1335AVCDec_Status dec_ref_pic_marking_repetition(AVCDecObject *decvid, AVCDecBitstream *stream)
1336{
1337    AVCSeqParamSet *currSPS;
1338    uint temp;
1339    currSPS = decvid->seqParams[decvid->common->seq_parameter_set_id];
1340    /* original_idr_flag */
1341    BitstreamRead1Bit(stream, &temp);
1342    /* original_frame_num */
1343    ue_v(stream, &temp);
1344    if (currSPS->frame_mbs_only_flag == 0)
1345    {
1346        /* original_field_pic_flag */
1347        BitstreamRead1Bit(stream, &temp);
1348        if (temp)
1349        {
1350            /* original_bottom_field_flag */
1351            BitstreamRead1Bit(stream, &temp);
1352        }
1353    }
1354
1355    /*  dec_ref_pic_marking(video,stream,sliceHdr); */
1356
1357
1358    return AVCDEC_SUCCESS;
1359}
1360AVCDec_Status motion_constrained_slice_group_set(AVCDecObject *decvid, AVCDecBitstream *stream)
1361{
1362    OSCL_UNUSED_ARG(decvid);
1363    uint temp, i, numBits;
1364    /* num_slice_groups_in_set_minus1 */
1365    ue_v(stream, &temp);
1366
1367    numBits = 0;/* ceil(log2(num_slice_groups_minus1+1)) bits */
1368    i = temp;
1369    while (i > 0)
1370    {
1371        numBits++;
1372        i >>= 1;
1373    }
1374    for (i = 0; i <= temp; i++)
1375    {
1376        /* slice_group_id */
1377        BitstreamReadBits(stream, numBits, &temp);
1378    }
1379    /* exact_sample_value_match_flag */
1380    BitstreamRead1Bit(stream, &temp);
1381    /* pan_scan_rect_flag */
1382    BitstreamRead1Bit(stream, &temp);
1383    if (temp)
1384    {
1385        /* pan_scan_rect_id */
1386        ue_v(stream, &temp);
1387    }
1388
1389    return AVCDEC_SUCCESS;
1390}
1391