PageRenderTime 116ms CodeModel.GetById 3ms app.highlight 103ms RepoModel.GetById 1ms app.codeStats 0ms

/Show/avc/header.cpp

http://github.com/mbebenita/Broadway
C++ | 1483 lines | 1157 code | 186 blank | 140 comment | 241 complexity | ae7709b5b7611f55ef27cf5f2edadfd4 MD5 | raw file

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

Large files files are truncated, but you can click here to view the full file