PageRenderTime 183ms CodeModel.GetById 41ms app.highlight 111ms RepoModel.GetById 19ms app.codeStats 1ms

/Avc/avcdec_api.cpp

http://github.com/mbebenita/Broadway
C++ | 1037 lines | 689 code | 152 blank | 196 comment | 198 complexity | 6e905f515134a3c87cd13ed53fdca1fe 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/**
  19This file contains application function interfaces to the AVC decoder library.
  20@publishedAll
  21*/
  22
  23#include <string.h>
  24#include <stdint.h>
  25
  26#include "avcdec_api.h"
  27#include "avcdec_lib.h"
  28#include "avcdec_bitstream.h"
  29
  30/* ======================================================================== */
  31/*  Function : EBSPtoRBSP()                                                 */
  32/*  Date     : 11/4/2003                                                    */
  33/*  Purpose  : Convert EBSP to RBSP and overwrite it.                       */
  34/*             Assuming that forbidden_zero, nal_ref_idc and nal_unit_type  */
  35/*          (first byte), has been taken out of the nal_unit.               */
  36/*  In/out   :                                                              */
  37/*  Return   :                                                              */
  38/*  Modified :                                                              */
  39/* ======================================================================== */
  40/**
  41@pseudocode "
  42    NumBytesInRBSP = 0;
  43    for(i=0:i< *size; i++){
  44        if(i+2 < *size && next_bits(24)==0x000003){
  45            rbsp_byte[NumBytesInRBSP++];
  46            rbsp_byte[NumBytesInRBSP++];
  47            i+=2;
  48            emulation_prevention_three_byte (0x03)
  49        }
  50        else
  51            rbsp_byte[NumBytesInRBSP++];
  52    }"
  53*/
  54AVCDec_Status EBSPtoRBSP(uint8 *nal_unit, int *size)
  55{
  56    int i, j;
  57    int count = 0;
  58
  59    /* This code is based on EBSPtoRBSP of JM */
  60    j = 0;
  61
  62    for (i = 0; i < *size; i++)
  63    {
  64        if (count == 2 && nal_unit[i] == 0x03)
  65        {
  66            i++;
  67            count = 0;
  68        }
  69        nal_unit[j] = nal_unit[i];
  70        if (nal_unit[i] == 0x00)
  71            count++;
  72        else
  73            count = 0;
  74        j++;
  75    }
  76
  77    *size = j;
  78
  79    return AVCDEC_SUCCESS;
  80}
  81
  82/* ======================================================================== */
  83/*  Function : PVAVCAnnexBGetNALUnit()                                      */
  84/*  Date     : 11/3/2003                                                    */
  85/*  Purpose  : Parse a NAL from byte stream format.                         */
  86/*  In/out   :                                                              */
  87/*  Return   : AVCDEC_SUCCESS if succeed, AVC_FAIL if fail.                 */
  88/*  Modified :                                                              */
  89/* ======================================================================== */
  90/**
  91@pseudocode "
  92    byte_stream_nal_unit(NumBytesInNalunit){
  93    while(next_bits(24) != 0x000001)
  94        zero_byte
  95    if(more_data_in_byte_stream()){
  96        start_code_prefix_one_3bytes // equal 0x000001
  97        nal_unit(NumBytesInNALunit)
  98    }
  99   }"
 100*/
 101OSCL_EXPORT_REF AVCDec_Status PVAVCAnnexBGetNALUnit(uint8 *bitstream, uint8 **nal_unit,
 102        int *size)
 103{
 104    int i, j, FoundStartCode = 0;
 105    int end;
 106
 107    i = 0;
 108    while (bitstream[i] == 0 && i < *size)
 109    {
 110        i++;
 111    }
 112    if (i >= *size)
 113    {
 114        *nal_unit = bitstream;
 115        return AVCDEC_FAIL; /* cannot find any start_code_prefix. */
 116    }
 117    else if (bitstream[i] != 0x1)
 118    {
 119        i = -1;  /* start_code_prefix is not at the beginning, continue */
 120    }
 121
 122    i++;
 123    *nal_unit = bitstream + i; /* point to the beginning of the NAL unit */
 124
 125    j = end = i;
 126    while (!FoundStartCode)
 127    {
 128        while ((j + 1 < *size) && (bitstream[j] != 0 || bitstream[j+1] != 0))  /* see 2 consecutive zero bytes */
 129        {
 130            j++;
 131        }
 132        end = j;   /* stop and check for start code */
 133        while (j + 2 < *size && bitstream[j+2] == 0) /* keep reading for zero byte */
 134        {
 135            j++;
 136        }
 137        if (j + 2 >= *size)
 138        {
 139            *size -= i;
 140            return AVCDEC_NO_NEXT_SC;  /* cannot find the second start_code_prefix */
 141        }
 142        if (bitstream[j+2] == 0x1)
 143        {
 144            FoundStartCode = 1;
 145        }
 146        else
 147        {
 148            /* could be emulation code 0x3 */
 149            j += 2; /* continue the search */
 150        }
 151    }
 152
 153    *size = end - i;
 154
 155    return AVCDEC_SUCCESS;
 156}
 157
 158/* ======================================================================== */
 159/*  Function : PVAVCGetNALType()                                            */
 160/*  Date     : 11/4/2003                                                    */
 161/*  Purpose  : Sniff NAL type from the bitstream                            */
 162/*  In/out   :                                                              */
 163/*  Return   : AVCDEC_SUCCESS if succeed, AVC_FAIL if fail.                 */
 164/*  Modified :                                                              */
 165/* ======================================================================== */
 166OSCL_EXPORT_REF AVCDec_Status PVAVCDecGetNALType(uint8 *bitstream, int size,
 167        int *nal_type, int *nal_ref_idc)
 168{
 169    int forbidden_zero_bit;
 170    if (size > 0)
 171    {
 172        forbidden_zero_bit = bitstream[0] >> 7;
 173        if (forbidden_zero_bit != 0)
 174            return AVCDEC_FAIL;
 175        *nal_ref_idc = (bitstream[0] & 0x60) >> 5;
 176        *nal_type = bitstream[0] & 0x1F;
 177        return AVCDEC_SUCCESS;
 178    }
 179
 180    return AVCDEC_FAIL;
 181}
 182
 183/* ======================================================================== */
 184/*  Function : PVAVCDecSeqParamSet()                                        */
 185/*  Date     : 11/4/2003                                                    */
 186/*  Purpose  : Initialize sequence, memory allocation if necessary.         */
 187/*  In/out   :                                                              */
 188/*  Return   : AVCDEC_SUCCESS if succeed, AVC_FAIL if fail.                 */
 189/*  Modified :                                                              */
 190/* ======================================================================== */
 191
 192OSCL_EXPORT_REF AVCDec_Status   PVAVCDecSeqParamSet(AVCHandle *avcHandle, uint8 *nal_unit,
 193        int nal_size)
 194{
 195    AVCDec_Status status;
 196    AVCDecObject *decvid;
 197    AVCCommonObj *video;
 198    AVCDecBitstream *bitstream;
 199    void *userData = avcHandle->userData;
 200    bool  first_seq = FALSE;
 201    int i;
 202
 203
 204    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "PVAVCDecSeqParamSet", -1, -1);
 205
 206    if (avcHandle->AVCObject == NULL)
 207    {
 208        first_seq = TRUE;
 209
 210        //avcHandle->memory_usage = 0;
 211        /* allocate AVCDecObject */
 212        avcHandle->AVCObject = (void*)avcHandle->CBAVC_Malloc(userData, sizeof(AVCDecObject), 0/*DEFAULT_ATTR*/);
 213        if (avcHandle->AVCObject == NULL)
 214        {
 215            return AVCDEC_MEMORY_FAIL;
 216        }
 217
 218        decvid = (AVCDecObject*) avcHandle->AVCObject;
 219
 220        memset(decvid, 0, sizeof(AVCDecObject));
 221
 222        decvid->common = (AVCCommonObj*)avcHandle->CBAVC_Malloc(userData, sizeof(AVCCommonObj), 0);
 223        if (decvid->common == NULL)
 224        {
 225            return AVCDEC_MEMORY_FAIL;
 226        }
 227
 228        video = decvid->common;
 229        memset(video, 0, sizeof(AVCCommonObj));
 230
 231        video->seq_parameter_set_id = 9999; /* set it to some illegal value */
 232
 233        decvid->bitstream = (AVCDecBitstream *) avcHandle->CBAVC_Malloc(userData, sizeof(AVCDecBitstream), 1/*DEFAULT_ATTR*/);
 234        if (decvid->bitstream == NULL)
 235        {
 236            return AVCDEC_MEMORY_FAIL;
 237        }
 238
 239        decvid->bitstream->userData = avcHandle->userData; /* callback for more data */
 240        decvid->avcHandle = avcHandle;
 241        decvid->debugEnable = avcHandle->debugEnable;
 242    }
 243
 244    decvid = (AVCDecObject*) avcHandle->AVCObject;
 245    video = decvid->common;
 246    bitstream = decvid->bitstream;
 247
 248    /* check if we can reuse the memory without re-allocating it. */
 249    /* always check if(first_seq==TRUE) */
 250
 251    /* Conversion from EBSP to RBSP */
 252    video->forbidden_bit = nal_unit[0] >> 7;
 253    if (video->forbidden_bit) return AVCDEC_FAIL;
 254    video->nal_ref_idc = (nal_unit[0] & 0x60) >> 5;
 255    video->nal_unit_type = (AVCNalUnitType)(nal_unit[0] & 0x1F);
 256
 257    if (video->nal_unit_type != AVC_NALTYPE_SPS) /* not a SPS NAL */
 258    {
 259        return AVCDEC_FAIL;
 260    }
 261
 262    /* Initialize bitstream structure*/
 263    BitstreamInit(bitstream, nal_unit + 1, nal_size - 1);
 264
 265    /* if first_seq == TRUE, allocate the following memory  */
 266    if (first_seq == TRUE)
 267    {
 268        video->currSeqParams = NULL; /* initialize it to NULL */
 269        video->currPicParams = NULL;
 270
 271        /* There are 32 pointers to sequence param set, seqParams.
 272                There are 255 pointers to picture param set, picParams.*/
 273        for (i = 0; i < 32; i++)
 274            decvid->seqParams[i] = NULL;
 275
 276        for (i = 0; i < 256; i++)
 277            decvid->picParams[i] = NULL;
 278
 279        video->MbToSliceGroupMap = NULL;
 280
 281        video->mem_mgr_ctrl_eq_5 = FALSE;
 282        video->newPic = TRUE;
 283        video->newSlice = TRUE;
 284        video->currPic = NULL;
 285        video->currFS = NULL;
 286        video->prevRefPic = NULL;
 287
 288        video->mbNum = 0; // MC_Conceal
 289        /*  Allocate sliceHdr. */
 290
 291        video->sliceHdr = (AVCSliceHeader*) avcHandle->CBAVC_Malloc(userData, sizeof(AVCSliceHeader), 5/*DEFAULT_ATTR*/);
 292        if (video->sliceHdr == NULL)
 293        {
 294            return AVCDEC_MEMORY_FAIL;
 295        }
 296
 297        video->decPicBuf = (AVCDecPicBuffer*) avcHandle->CBAVC_Malloc(userData, sizeof(AVCDecPicBuffer), 3/*DEFAULT_ATTR*/);
 298        if (video->decPicBuf == NULL)
 299        {
 300            return AVCDEC_MEMORY_FAIL;
 301        }
 302        memset(video->decPicBuf, 0, sizeof(AVCDecPicBuffer));
 303    }
 304
 305    /* Decode SPS, allocate video->seqParams[i] and assign video->currSeqParams */
 306    status = DecodeSPS(decvid, bitstream);
 307
 308    if (status != AVCDEC_SUCCESS)
 309    {
 310        return status;
 311    }
 312    return AVCDEC_SUCCESS;
 313}
 314
 315/* ======================================================================== */
 316/*  Function : PVAVCDecGetSeqInfo()                                         */
 317/*  Date     : 11/4/2003                                                    */
 318/*  Purpose  : Get sequence parameter info. after SPS NAL is decoded.       */
 319/*  In/out   :                                                              */
 320/*  Return   : AVCDEC_SUCCESS if succeed, AVC_FAIL if fail.                 */
 321/*  Modified :                                                              */
 322/*  12/20/03:  change input argument, use structure instead.                */
 323/* ======================================================================== */
 324
 325OSCL_EXPORT_REF AVCDec_Status PVAVCDecGetSeqInfo(AVCHandle *avcHandle, AVCDecSPSInfo *seqInfo)
 326{
 327    AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
 328    AVCCommonObj *video;
 329    int PicWidthInMbs, PicHeightInMapUnits, FrameHeightInMbs;
 330
 331    if (decvid == NULL || decvid->seqParams[0] == NULL)
 332    {
 333        return AVCDEC_FAIL;
 334    }
 335
 336    video = decvid->common;
 337
 338    PicWidthInMbs = decvid->seqParams[0]->pic_width_in_mbs_minus1 + 1;
 339    PicHeightInMapUnits = decvid->seqParams[0]->pic_height_in_map_units_minus1 + 1 ;
 340    FrameHeightInMbs = (2 - decvid->seqParams[0]->frame_mbs_only_flag) * PicHeightInMapUnits ;
 341
 342    seqInfo->FrameWidth = PicWidthInMbs << 4;
 343    seqInfo->FrameHeight = FrameHeightInMbs << 4;
 344
 345    seqInfo->frame_only_flag = decvid->seqParams[0]->frame_mbs_only_flag;
 346
 347    if (decvid->seqParams[0]->frame_cropping_flag)
 348    {
 349        seqInfo->frame_crop_left = 2 * decvid->seqParams[0]->frame_crop_left_offset;
 350        seqInfo->frame_crop_right = seqInfo->FrameWidth - (2 * decvid->seqParams[0]->frame_crop_right_offset + 1);
 351
 352        if (seqInfo->frame_only_flag)
 353        {
 354            seqInfo->frame_crop_top = 2 * decvid->seqParams[0]->frame_crop_top_offset;
 355            seqInfo->frame_crop_bottom = seqInfo->FrameHeight - (2 * decvid->seqParams[0]->frame_crop_bottom_offset + 1);
 356            /* Note in 7.4.2.1, there is a contraint on the value of frame_crop_left and frame_crop_top
 357            such that they have to be less than or equal to frame_crop_right/2 and frame_crop_bottom/2, respectively. */
 358        }
 359        else
 360        {
 361            seqInfo->frame_crop_top = 4 * decvid->seqParams[0]->frame_crop_top_offset;
 362            seqInfo->frame_crop_bottom = seqInfo->FrameHeight - (4 * decvid->seqParams[0]->frame_crop_bottom_offset + 1);
 363            /* Note in 7.4.2.1, there is a contraint on the value of frame_crop_left and frame_crop_top
 364            such that they have to be less than or equal to frame_crop_right/2 and frame_crop_bottom/4, respectively. */
 365        }
 366    }
 367    else  /* no cropping flag, just give the first and last pixel */
 368    {
 369        seqInfo->frame_crop_bottom = seqInfo->FrameHeight - 1;
 370        seqInfo->frame_crop_right = seqInfo->FrameWidth - 1;
 371        seqInfo->frame_crop_top = seqInfo->frame_crop_left = 0;
 372    }
 373
 374    return AVCDEC_SUCCESS;
 375}
 376
 377/* ======================================================================== */
 378/*  Function : PVAVCDecPicParamSet()                                        */
 379/*  Date     : 11/4/2003                                                    */
 380/*  Purpose  : Initialize picture                                           */
 381/*             create reference picture list.                               */
 382/*  In/out   :                                                              */
 383/*  Return   : AVCDEC_SUCCESS if succeed, AVC_FAIL if fail.                 */
 384/*  Modified :                                                              */
 385/* ======================================================================== */
 386/**
 387Since PPS doesn't contain much data, most of the picture initialization will
 388be done after decoding the slice header in PVAVCDecodeSlice. */
 389OSCL_EXPORT_REF AVCDec_Status   PVAVCDecPicParamSet(AVCHandle *avcHandle, uint8 *nal_unit,
 390        int nal_size)
 391{
 392    AVCDec_Status status;
 393    AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
 394    AVCCommonObj *video;
 395    AVCDecBitstream *bitstream;
 396
 397    if (decvid == NULL)
 398    {
 399        return AVCDEC_FAIL;
 400    }
 401
 402    video = decvid->common;
 403    bitstream = decvid->bitstream;
 404    /* 1. Convert EBSP to RBSP. Create bitstream structure */
 405    video->forbidden_bit = nal_unit[0] >> 7;
 406    video->nal_ref_idc = (nal_unit[0] & 0x60) >> 5;
 407    video->nal_unit_type = (AVCNalUnitType)(nal_unit[0] & 0x1F);
 408
 409    if (video->nal_unit_type != AVC_NALTYPE_PPS) /* not a PPS NAL */
 410    {
 411        return AVCDEC_FAIL;
 412    }
 413
 414
 415    /* 2. Initialize bitstream structure*/
 416    BitstreamInit(bitstream, nal_unit + 1, nal_size - 1);
 417
 418    /* 2. Decode pic_parameter_set_rbsp syntax. Allocate video->picParams[i] and assign to currPicParams */
 419    status = DecodePPS(decvid, video, bitstream);
 420    if (status != AVCDEC_SUCCESS)
 421    {
 422        return status;
 423    }
 424
 425    video->SliceGroupChangeRate = video->currPicParams->slice_group_change_rate_minus1 + 1 ;
 426
 427    return AVCDEC_SUCCESS;
 428}
 429
 430OSCL_EXPORT_REF AVCDec_Status   PVAVCDecSEI(AVCHandle *avcHandle, uint8 *nal_unit,
 431        int nal_size)
 432{
 433    OSCL_UNUSED_ARG(avcHandle);
 434    OSCL_UNUSED_ARG(nal_unit);
 435    OSCL_UNUSED_ARG(nal_size);
 436
 437    return AVCDEC_SUCCESS;
 438}
 439/* ======================================================================== */
 440/*  Function : PVAVCDecodeSlice()                                           */
 441/*  Date     : 11/4/2003                                                    */
 442/*  Purpose  : Decode one NAL unit.                                         */
 443/*  In/out   :                                                              */
 444/*  Return   : See enum AVCDec_Status for return values.                    */
 445/*  Modified :                                                              */
 446/* ======================================================================== */
 447OSCL_EXPORT_REF AVCDec_Status PVAVCDecodeSlice(AVCHandle *avcHandle, uint8 *buffer,
 448        int buf_size)
 449{
 450    AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
 451    AVCCommonObj *video;
 452    AVCDecBitstream *bitstream;
 453    AVCDec_Status status;
 454
 455    if (decvid == NULL)
 456    {
 457        return AVCDEC_FAIL;
 458    }
 459
 460    video = decvid->common;
 461    bitstream = decvid->bitstream;
 462
 463    if (video->mem_mgr_ctrl_eq_5)
 464    {
 465        return AVCDEC_PICTURE_OUTPUT_READY;      // to flushout frame buffers
 466    }
 467
 468    if (video->newSlice)
 469    {
 470        /* 2. Check NAL type  */
 471        if (buffer == NULL)
 472        {
 473            return AVCDEC_FAIL;
 474        }
 475        video->prev_nal_unit_type = video->nal_unit_type;
 476        video->forbidden_bit = buffer[0] >> 7;
 477        video->nal_ref_idc = (buffer[0] & 0x60) >> 5;
 478        video->nal_unit_type = (AVCNalUnitType)(buffer[0] & 0x1F);
 479
 480
 481        if (video->nal_unit_type == AVC_NALTYPE_AUD)
 482        {
 483            return AVCDEC_SUCCESS;
 484        }
 485
 486        if (video->nal_unit_type != AVC_NALTYPE_SLICE &&
 487                video->nal_unit_type != AVC_NALTYPE_IDR)
 488        {
 489            return AVCDEC_FAIL; /* not supported */
 490        }
 491
 492
 493
 494        if (video->nal_unit_type >= 2 && video->nal_unit_type <= 4)
 495        {
 496            return AVCDEC_FAIL; /* not supported */
 497        }
 498        else
 499        {
 500            video->slice_data_partitioning = FALSE;
 501        }
 502
 503        video->newSlice = FALSE;
 504        /*  Initialize bitstream structure*/
 505        BitstreamInit(bitstream, buffer + 1, buf_size - 1);
 506
 507
 508        /* 2.1 Decode Slice Header (separate function)*/
 509        status = DecodeSliceHeader(decvid, video, bitstream);
 510        if (status != AVCDEC_SUCCESS)
 511        {
 512            video->newSlice = TRUE;
 513            return status;
 514        }
 515
 516        if (video->sliceHdr->frame_num != video->prevFrameNum || (video->sliceHdr->first_mb_in_slice < (uint)video->mbNum && video->currSeqParams->constrained_set1_flag == 1))
 517        {
 518            video->newPic = TRUE;
 519            if (video->numMBs > 0)
 520            {
 521                // Conceal missing MBs of previously decoded frame
 522                ConcealSlice(decvid, video->PicSizeInMbs - video->numMBs, video->PicSizeInMbs);  // Conceal
 523                video->numMBs = 0;
 524
 525                //              DeblockPicture(video);   // No need to deblock
 526
 527                /* 3.2 Decoded frame reference marking. */
 528                /* 3.3 Put the decoded picture in output buffers */
 529                /* set video->mem_mge_ctrl_eq_5 */
 530                AVCNalUnitType temp = video->nal_unit_type;
 531                video->nal_unit_type = video->prev_nal_unit_type;
 532                StorePictureInDPB(avcHandle, video);
 533                video->nal_unit_type = temp;
 534                video->mbNum = 0; // MC_Conceal
 535                return AVCDEC_PICTURE_OUTPUT_READY;
 536            }
 537        }
 538
 539        if (video->nal_unit_type == AVC_NALTYPE_IDR)
 540        {
 541            video->prevFrameNum = 0;
 542            video->PrevRefFrameNum = 0;
 543        }
 544
 545        if (!video->currSeqParams->gaps_in_frame_num_value_allowed_flag)
 546        {   /* no gaps allowed, frame_num has to increase by one only */
 547            /*          if(sliceHdr->frame_num != (video->PrevRefFrameNum + 1)%video->MaxFrameNum) */
 548            if (video->sliceHdr->frame_num != video->PrevRefFrameNum && video->sliceHdr->frame_num != (video->PrevRefFrameNum + 1) % video->MaxFrameNum)
 549            {
 550                // Conceal missing MBs of previously decoded frame
 551                video->numMBs = 0;
 552                video->newPic = TRUE;
 553                video->prevFrameNum++; // FIX
 554                video->PrevRefFrameNum++;
 555                AVCNalUnitType temp = video->nal_unit_type;
 556                video->nal_unit_type = AVC_NALTYPE_SLICE; //video->prev_nal_unit_type;
 557                status = (AVCDec_Status)DPBInitBuffer(avcHandle, video);
 558                if (status != AVCDEC_SUCCESS)
 559                {
 560                    return status;
 561                }
 562                video->currFS->IsOutputted = 0x01;
 563                video->currFS->IsReference = 3;
 564                video->currFS->IsLongTerm = 0;
 565
 566                DecodePOC(video);
 567                /* find an empty memory from DPB and assigned to currPic */
 568                DPBInitPic(video, video->PrevRefFrameNum % video->MaxFrameNum);
 569                RefListInit(video);
 570                ConcealSlice(decvid, 0, video->PicSizeInMbs);  // Conceal
 571                video->currFS->IsOutputted |= 0x02;
 572                //conceal frame
 573                /* 3.2 Decoded frame reference marking. */
 574                /* 3.3 Put the decoded picture in output buffers */
 575                /* set video->mem_mge_ctrl_eq_5 */
 576                video->mbNum = 0; // Conceal
 577                StorePictureInDPB(avcHandle, video);
 578                video->nal_unit_type = temp;
 579
 580                return AVCDEC_PICTURE_OUTPUT_READY;
 581            }
 582        }
 583    }
 584
 585    if (video->newPic == TRUE)
 586    {
 587        status = (AVCDec_Status)DPBInitBuffer(avcHandle, video);
 588        if (status != AVCDEC_SUCCESS)
 589        {
 590            return status;
 591        }
 592    }
 593
 594    video->newSlice = TRUE;
 595
 596    /* function pointer setting at slice-level */
 597    // OPTIMIZE
 598    decvid->residual_block = &residual_block_cavlc;
 599
 600    /* derive picture order count */
 601    if (video->newPic == TRUE)
 602    {
 603        video->numMBs = video->PicSizeInMbs;
 604
 605        if (video->nal_unit_type != AVC_NALTYPE_IDR && video->currSeqParams->gaps_in_frame_num_value_allowed_flag)
 606        {
 607            if (video->sliceHdr->frame_num != (video->PrevRefFrameNum + 1) % video->MaxFrameNum)
 608            {
 609                status = fill_frame_num_gap(avcHandle, video);
 610                if (status != AVCDEC_SUCCESS)
 611                {
 612                    video->numMBs = 0;
 613                    return status;
 614                }
 615
 616                status = (AVCDec_Status)DPBInitBuffer(avcHandle, video);
 617                if (status != AVCDEC_SUCCESS)
 618                {
 619                    video->numMBs = 0;
 620                    return status;
 621                }
 622
 623
 624            }
 625        }
 626        /* if there's gap in the frame_num, we have to fill in the gap with
 627            imaginary frames that won't get used for short-term ref. */
 628        /* see fill_frame_num_gap() in JM */
 629
 630
 631        DecodePOC(video);
 632        /* find an empty memory from DPB and assigned to currPic */
 633        DPBInitPic(video, video->CurrPicNum);
 634
 635        video->currPic->isReference = TRUE;  // FIX
 636
 637        if (video->nal_ref_idc == 0)
 638        {
 639            video->currPic->isReference = FALSE;
 640            video->currFS->IsOutputted |= 0x02;     /* The MASK 0x02 means not needed for reference, or returned */
 641            /* node need to check for freeing of this buffer */
 642        }
 643
 644        FMOInit(video);
 645
 646        if (video->currPic->isReference)
 647        {
 648            video->PrevRefFrameNum = video->sliceHdr->frame_num;
 649        }
 650
 651
 652        video->prevFrameNum = video->sliceHdr->frame_num;
 653    }
 654
 655    video->newPic = FALSE;
 656
 657
 658    /* Initialize refListIdx for this picture */
 659    RefListInit(video);
 660
 661    /* Re-order the reference list according to the ref_pic_list_reordering() */
 662    status = (AVCDec_Status)ReOrderList(video);
 663    if (status != AVCDEC_SUCCESS)
 664    {
 665        return AVCDEC_FAIL;
 666    }
 667
 668    /* 2.2 Decode Slice. */
 669    status = (AVCDec_Status)DecodeSlice(decvid);
 670
 671    video->slice_id++;  //  slice
 672
 673    if (status == AVCDEC_PICTURE_READY)
 674    {
 675        /* 3. Check complete picture */
 676#ifndef MB_BASED_DEBLOCK
 677        /* 3.1 Deblock */
 678        DeblockPicture(video);
 679#endif
 680        /* 3.2 Decoded frame reference marking. */
 681        /* 3.3 Put the decoded picture in output buffers */
 682        /* set video->mem_mge_ctrl_eq_5 */
 683        status = (AVCDec_Status)StorePictureInDPB(avcHandle, video);          // CHECK check the retunr status
 684        if (status != AVCDEC_SUCCESS)
 685        {
 686            return AVCDEC_FAIL;
 687        }
 688
 689        if (video->mem_mgr_ctrl_eq_5)
 690        {
 691            video->PrevRefFrameNum = 0;
 692            video->prevFrameNum = 0;
 693            video->prevPicOrderCntMsb = 0;
 694            video->prevPicOrderCntLsb = video->TopFieldOrderCnt;
 695            video->prevFrameNumOffset = 0;
 696        }
 697        else
 698        {
 699            video->prevPicOrderCntMsb = video->PicOrderCntMsb;
 700            video->prevPicOrderCntLsb = video->sliceHdr->pic_order_cnt_lsb;
 701            video->prevFrameNumOffset = video->FrameNumOffset;
 702        }
 703
 704        return AVCDEC_PICTURE_READY;
 705    }
 706    else if (status != AVCDEC_SUCCESS)
 707    {
 708        return AVCDEC_FAIL;
 709    }
 710
 711    return AVCDEC_SUCCESS;
 712}
 713
 714/* ======================================================================== */
 715/*  Function : PVAVCDecGetOutput()                                          */
 716/*  Date     : 11/3/2003                                                    */
 717/*  Purpose  : Get the next picture according to PicOrderCnt.               */
 718/*  In/out   :                                                              */
 719/*  Return   : AVCFrameIO structure                                         */
 720/*  Modified :                                                              */
 721/* ======================================================================== */
 722
 723OSCL_EXPORT_REF AVCDec_Status PVAVCDecGetOutput(AVCHandle *avcHandle, int *indx, int *release, AVCFrameIO *output)
 724{
 725    AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
 726    AVCCommonObj *video;
 727    AVCDecPicBuffer *dpb;
 728    AVCFrameStore *oldestFrame = NULL;
 729    int i, first = 1;
 730    int count_frame = 0;
 731    int index = 0;
 732    int min_poc = 0;
 733
 734    if (decvid == NULL)
 735    {
 736        return AVCDEC_FAIL;
 737    }
 738
 739    video = decvid->common;
 740    dpb = video->decPicBuf;
 741
 742    if (dpb->num_fs == 0)
 743    {
 744        return AVCDEC_FAIL;
 745    }
 746
 747    /* search for the oldest frame_num in dpb */
 748    /* extension to field decoding, we have to search for every top_field/bottom_field within
 749    each frame in the dpb. This code only works for frame based.*/
 750
 751    if (video->mem_mgr_ctrl_eq_5 == FALSE)
 752    {
 753        for (i = 0; i < dpb->num_fs; i++)
 754        {
 755            if ((dpb->fs[i]->IsOutputted & 0x01) == 0)
 756            {
 757                count_frame++;
 758                if (first)
 759                {
 760                    min_poc = dpb->fs[i]->PicOrderCnt;
 761                    first = 0;
 762                    oldestFrame = dpb->fs[i];
 763                    index = i;
 764                }
 765                if (dpb->fs[i]->PicOrderCnt < min_poc)
 766                {
 767                    min_poc = dpb->fs[i]->PicOrderCnt;
 768                    oldestFrame = dpb->fs[i];
 769                    index = i;
 770                }
 771            }
 772        }
 773    }
 774    else
 775    {
 776        for (i = 0; i < dpb->num_fs; i++)
 777        {
 778            if ((dpb->fs[i]->IsOutputted & 0x01) == 0 && dpb->fs[i] != video->currFS)
 779            {
 780                count_frame++;
 781                if (first)
 782                {
 783                    min_poc = dpb->fs[i]->PicOrderCnt;
 784                    first = 0;
 785                    oldestFrame = dpb->fs[i];
 786                    index = i;
 787                }
 788                if (dpb->fs[i]->PicOrderCnt < min_poc)
 789                {
 790                    min_poc = dpb->fs[i]->PicOrderCnt;
 791                    oldestFrame = dpb->fs[i];
 792                    index = i;
 793                }
 794            }
 795        }
 796
 797        if (count_frame < 2 && video->nal_unit_type != AVC_NALTYPE_IDR)
 798        {
 799            video->mem_mgr_ctrl_eq_5 = FALSE;  // FIX
 800        }
 801        else if (count_frame < 1 && video->nal_unit_type == AVC_NALTYPE_IDR)
 802        {
 803            for (i = 0; i < dpb->num_fs; i++)
 804            {
 805                if (dpb->fs[i] == video->currFS && (dpb->fs[i]->IsOutputted & 0x01) == 0)
 806                {
 807                    oldestFrame = dpb->fs[i];
 808                    index = i;
 809                    break;
 810                }
 811            }
 812            video->mem_mgr_ctrl_eq_5 = FALSE;
 813        }
 814    }
 815
 816    if (oldestFrame == NULL)
 817    {
 818
 819        /*      Check for Mem_mgmt_operation_5 based forced output */
 820        for (i = 0; i < dpb->num_fs; i++)
 821        {
 822            /* looking for the one not used or not reference and has been outputted */
 823            if (dpb->fs[i]->IsReference == 0 && dpb->fs[i]->IsOutputted == 3)
 824            {
 825                break;
 826            }
 827        }
 828        if (i < dpb->num_fs)
 829        {
 830            /* there are frames available for decoding */
 831            return AVCDEC_FAIL; /* no frame to be outputted */
 832        }
 833
 834
 835        /* no free frame available, we have to release one to continue decoding */
 836        int MinIdx = 0;
 837        int32 MinFrameNumWrap = 0x7FFFFFFF;
 838
 839        for (i = 0; i < dpb->num_fs; i++)
 840        {
 841            if (dpb->fs[i]->IsReference && !dpb->fs[i]->IsLongTerm)
 842            {
 843                if (dpb->fs[i]->FrameNumWrap < MinFrameNumWrap)
 844                {
 845                    MinFrameNumWrap = dpb->fs[i]->FrameNumWrap;
 846                    MinIdx = i;
 847                }
 848            }
 849        }
 850        /* mark the frame with smallest PicOrderCnt to be unused for reference */
 851        dpb->fs[MinIdx]->IsReference = 0;
 852        dpb->fs[MinIdx]->IsLongTerm = 0;
 853        dpb->fs[MinIdx]->frame.isReference = FALSE;
 854        dpb->fs[MinIdx]->frame.isLongTerm = FALSE;
 855        dpb->fs[MinIdx]->IsOutputted |= 0x02;
 856#ifdef PV_MEMORY_POOL
 857        if (dpb->fs[MinIdx]->IsOutputted == 3)
 858        {
 859            avcHandle->CBAVC_FrameUnbind(avcHandle->userData, MinIdx);
 860        }
 861#endif
 862        return AVCDEC_FAIL;
 863    }
 864    /* MASK 0x01 means the frame is outputted (for display). A frame gets freed when it is
 865    outputted (0x01) and not needed for reference (0x02)   */
 866    oldestFrame->IsOutputted |= 0x01;
 867
 868    if (oldestFrame->IsOutputted == 3)
 869    {
 870        *release = 1; /* flag to release the buffer */
 871    }
 872    else
 873    {
 874        *release = 0;
 875    }
 876    /* do not release buffer here, release it after it is sent to the sink node */
 877
 878    output->YCbCr[0] = oldestFrame->frame.Sl;
 879    output->YCbCr[1] = oldestFrame->frame.Scb;
 880    output->YCbCr[2] = oldestFrame->frame.Scr;
 881    output->height = oldestFrame->frame.height;
 882    output->pitch = oldestFrame->frame.width;
 883    output->disp_order = oldestFrame->PicOrderCnt;
 884    output->coding_order = oldestFrame->FrameNum;
 885    output->id = (uintptr_t) oldestFrame->base_dpb; /* use the pointer as the id */
 886    *indx = index;
 887
 888
 889
 890    return AVCDEC_SUCCESS;
 891}
 892
 893
 894/* ======================================================================== */
 895/*  Function : PVAVCDecReset()                                              */
 896/*  Date     : 03/04/2004                                                   */
 897/*  Purpose  : Reset decoder, prepare it for a new IDR frame.               */
 898/*  In/out   :                                                              */
 899/*  Return   :  void                                                        */
 900/*  Modified :                                                              */
 901/* ======================================================================== */
 902OSCL_EXPORT_REF void    PVAVCDecReset(AVCHandle *avcHandle)
 903{
 904    AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
 905    AVCCommonObj *video;
 906    AVCDecPicBuffer *dpb;
 907    int i;
 908
 909    if (decvid == NULL)
 910    {
 911        return;
 912    }
 913
 914    video = decvid->common;
 915    dpb = video->decPicBuf;
 916
 917    /* reset the DPB */
 918
 919
 920    for (i = 0; i < dpb->num_fs; i++)
 921    {
 922        dpb->fs[i]->IsLongTerm = 0;
 923        dpb->fs[i]->IsReference = 0;
 924        dpb->fs[i]->IsOutputted = 3;
 925        dpb->fs[i]->frame.isReference = 0;
 926        dpb->fs[i]->frame.isLongTerm = 0;
 927    }
 928
 929    video->mem_mgr_ctrl_eq_5 = FALSE;
 930    video->newPic = TRUE;
 931    video->newSlice = TRUE;
 932    video->currPic = NULL;
 933    video->currFS = NULL;
 934    video->prevRefPic = NULL;
 935    video->prevFrameNum = 0;
 936    video->PrevRefFrameNum = 0;
 937    video->prevFrameNumOffset = 0;
 938    video->FrameNumOffset = 0;
 939    video->mbNum = 0;
 940    video->numMBs = 0;
 941
 942    return ;
 943}
 944
 945
 946/* ======================================================================== */
 947/*  Function : PVAVCCleanUpDecoder()                                        */
 948/*  Date     : 11/4/2003                                                    */
 949/*  Purpose  : Clean up the decoder, free all memories allocated.           */
 950/*  In/out   :                                                              */
 951/*  Return   :  void                                                        */
 952/*  Modified :                                                              */
 953/* ======================================================================== */
 954
 955OSCL_EXPORT_REF void PVAVCCleanUpDecoder(AVCHandle *avcHandle)
 956{
 957    AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
 958    AVCCommonObj *video;
 959    void *userData = avcHandle->userData;
 960    int i;
 961
 962    DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "PVAVCCleanUpDecoder", -1, -1);
 963
 964    if (decvid != NULL)
 965    {
 966        video = decvid->common;
 967        if (video != NULL)
 968        {
 969            if (video->MbToSliceGroupMap != NULL)
 970            {
 971                avcHandle->CBAVC_Free(userData, (uintptr_t)video->MbToSliceGroupMap);
 972            }
 973
 974#ifdef MB_BASED_DEBLOCK
 975            if (video->intra_pred_top != NULL)
 976            {
 977                avcHandle->CBAVC_Free(userData, (int)video->intra_pred_top);
 978            }
 979            if (video->intra_pred_top_cb != NULL)
 980            {
 981                avcHandle->CBAVC_Free(userData, (int)video->intra_pred_top_cb);
 982            }
 983            if (video->intra_pred_top_cr != NULL)
 984            {
 985                avcHandle->CBAVC_Free(userData, (int)video->intra_pred_top_cr);
 986            }
 987#endif
 988            if (video->mblock != NULL)
 989            {
 990                avcHandle->CBAVC_Free(userData, (uintptr_t)video->mblock);
 991            }
 992
 993            if (video->decPicBuf != NULL)
 994            {
 995                CleanUpDPB(avcHandle, video);
 996                avcHandle->CBAVC_Free(userData, (uintptr_t)video->decPicBuf);
 997            }
 998
 999            if (video->sliceHdr != NULL)
1000            {
1001                avcHandle->CBAVC_Free(userData, (uintptr_t)video->sliceHdr);
1002            }
1003
1004            avcHandle->CBAVC_Free(userData, (uintptr_t)video); /* last thing to do */
1005
1006        }
1007
1008        for (i = 0; i < 256; i++)
1009        {
1010            if (decvid->picParams[i] != NULL)
1011            {
1012                if (decvid->picParams[i]->slice_group_id != NULL)
1013                {
1014                    avcHandle->CBAVC_Free(userData, (uintptr_t)decvid->picParams[i]->slice_group_id);
1015                }
1016                avcHandle->CBAVC_Free(userData, (uintptr_t)decvid->picParams[i]);
1017            }
1018        }
1019        for (i = 0; i < 32; i++)
1020        {
1021            if (decvid->seqParams[i] != NULL)
1022            {
1023                avcHandle->CBAVC_Free(userData, (uintptr_t)decvid->seqParams[i]);
1024            }
1025        }
1026        if (decvid->bitstream != NULL)
1027        {
1028            avcHandle->CBAVC_Free(userData, (uintptr_t)decvid->bitstream);
1029        }
1030
1031
1032        avcHandle->CBAVC_Free(userData, (uintptr_t)decvid);
1033    }
1034
1035
1036    return ;
1037}