/* The copyright in this software is being made available under the BSD
 * License, included below. This software may be subject to other third party
 * and contributor rights, including patent rights, and no such rights are
 * granted under this license.  
 *
 * Copyright (c) 2010-2012, ITU/ISO/IEC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

/** \file     TAppDecTop.cpp
    \brief    Decoder application class
*/

#include <list>
#include <vector>
#include <stdio.h>
#include <fcntl.h>
#include <assert.h>

#include "TAppDecTop.h"
#include "TLibDecoder/AnnexBread.h"
#include "TLibDecoder/NALread.h"

//! \ingroup TAppDecoder
//! \{

// ====================================================================================================================
// Constructor / destructor / initialization / destroy
// ====================================================================================================================

TAppDecTop::TAppDecTop()
{
  ::memset (m_abDecFlag, 0, sizeof (m_abDecFlag));
  m_iPOCLastDisplay  = -MAX_INT;
}

Void TAppDecTop::create()
{
}

Void TAppDecTop::destroy()
{
}

// ====================================================================================================================
// Public member functions
// ====================================================================================================================

/**
 - create internal class
 - initialize internal class
 - until the end of the bitstream, call decoding function in TDecTop class
 - delete allocated buffers
 - destroy internal class
 .
 */
Void TAppDecTop::decode()
{
  UInt                uiPOC;
  TComList<TComPic*>* pcListPic = NULL;

  ifstream bitstreamFile(m_pchBitstreamFile, ifstream::in | ifstream::binary);
  if (!bitstreamFile)
  {
    fprintf(stderr, "\nfailed to open bitstream file `%s' for reading\n", m_pchBitstreamFile);
    exit(EXIT_FAILURE);
  }

  InputByteStream bytestream(bitstreamFile);

  // create & initialize internal classes
  xCreateDecLib();
  xInitDecLib  ();
  m_iPOCLastDisplay += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.

#if AHG5_COUNT_XXX_BITS_BINS
  extern UInt64 g_nContextBinCounter;
  extern UInt64 g_nBypassBinCounter;
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_REF_IDX_BITS_BINS
  m_cTDecTop.getEntropyDecoder()->resetCountedRefIdxBits();
  m_cTDecTop.getEntropyDecoder()->resetCountedRefIdxContextBins();
  m_cTDecTop.getEntropyDecoder()->resetCountedRefIdxBypassBins();
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_DQP_BITS_BINS
  m_cTDecTop.getEntropyDecoder()->resetCountedDQPBits();
  m_cTDecTop.getEntropyDecoder()->resetCountedDQPContextBins();
  m_cTDecTop.getEntropyDecoder()->resetCountedDQPBypassBins();
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_ABS_BITS_BINS
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOABSBits();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOABSContextBins();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOABSBypassBins();
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_TYPE_BITS_BINS
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOTypeBits();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOTypeContextBins();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOTypeBypassBins();
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_MERGE_LEFT_BITS_BINS
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOMLBits();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOMLContextBins();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOMLBypassBins();
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_MERGE_UP_BITS_BINS
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOMUBits();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOMUContextBins();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOMUBypassBins();
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_LEFT_BAND_BITS_BINS
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOLBBits();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOLBContextBins();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOLBBypassBins();
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_SIGN_BITS_BINS
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOSIGNBits();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOSIGNContextBins();
  m_cTDecTop.getEntropyDecoder()->resetCountedSAOSIGNBypassBins();
#endif
  // main decoder loop
  bool recon_opened = false; // reconstruction file not yet opened. (must be performed after SPS is seen)

  while (!!bitstreamFile)
  {
    /* location serves to work around a design fault in the decoder, whereby
     * the process of reading a new slice that is the first slice of a new frame
     * requires the TDecTop::decode() method to be called again with the same
     * nal unit. */
    streampos location = bitstreamFile.tellg();
    AnnexBStats stats = AnnexBStats();
    bool bPreviousPictureDecoded = false;

    vector<uint8_t> nalUnit;
    InputNALUnit nalu;
    byteStreamNALUnit(bytestream, nalUnit, stats);

    // call actual decoding function
    bool bNewPicture = false;
    if (nalUnit.empty())
    {
      /* this can happen if the following occur:
       *  - empty input file
       *  - two back-to-back start_code_prefixes
       *  - start_code_prefix immediately followed by EOF
       */
      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
    }
    else
    {
      read(nalu, nalUnit);
      if(m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer)
      {
        if(bPreviousPictureDecoded)
        {
          bNewPicture = true;
          bPreviousPictureDecoded = false;
        }
        else
        {
          bNewPicture = false;
        }
      }
      else
      {
        bNewPicture = m_cTDecTop.decode(nalu, m_iSkipFrame, m_iPOCLastDisplay);
        if (bNewPicture)
        {
          bitstreamFile.clear();
          /* location points to the current nalunit payload[1] due to the
           * need for the annexB parser to read three extra bytes.
           * [1] except for the first NAL unit in the file
           *     (but bNewPicture doesn't happen then) */
          bitstreamFile.seekg(location-streamoff(3));
          bytestream.reset();
        }
        bPreviousPictureDecoded = true; 
      }
    }
    if (bNewPicture || !bitstreamFile)
    {
      m_cTDecTop.executeDeblockAndAlf(uiPOC, pcListPic, m_iSkipFrame, m_iPOCLastDisplay);
    }

    if( pcListPic )
    {
      if ( m_pchReconFile && !recon_opened )
      {
        if ( m_outputBitDepth == 0 )
        {
          m_outputBitDepth = g_uiBitDepth + g_uiBitIncrement;
        }

        m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepth, g_uiBitDepth + g_uiBitIncrement ); // write mode
        recon_opened = true;
      }
      if (bNewPicture && (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR))
      {
        xFlushOutput( pcListPic );
      }
      // write reconstruction to file
      if(bNewPicture)
      {
        xWriteOutput( pcListPic, nalu.m_temporalId );
      }
    }
  }
  
  xFlushOutput( pcListPic );

#if AHG5_COUNT_XXX_BITS_BINS
  printf("\n\n");
  printf("Total number of context bins: %I64d\n", g_nContextBinCounter);
  printf("Total number of bypass bins: %I64d\n", g_nBypassBinCounter);
#endif
#if  AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_REF_IDX_BITS_BINS
  printf("\n\n");
  printf("Total number of RefIdx bits: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedRefIdxBits());
  printf("Total number of RefIdx context bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedRefIdxContextBins());
  printf("Total number of RefIdx bypass bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedRefIdxBypassBins());
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_DQP_BITS_BINS
  printf("\n\n");
  printf("Total number of dQP bits: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedDQPBits());
  printf("Total number of dQP context bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedDQPContextBins());
  printf("Total number of dQP bypass bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedDQPBypassBins());
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_ABS_BITS_BINS
  printf("\n\n");
  printf("Total number of SAO ABS bits: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOABSBits());
  printf("Total number of SAO ABS context bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOABSContextBins());
  printf("Total number of SAO ABS bypass bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOABSBypassBins());
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_TYPE_BITS_BINS
  printf("\n\n");
  printf("Total number of SAO TYPE bits: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOTypeBits());
  printf("Total number of SAO TYPE context bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOTypeContextBins());
  printf("Total number of SAO TYPE bypass bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOTypeBypassBins());
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_MERGE_LEFT_BITS_BINS
  printf("\n\n");
  printf("Total number of SAO MERGE_LEFT bits: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOMLBits());
  printf("Total number of SAO MERGE_LEFT context bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOMLContextBins());
  printf("Total number of SAO MERGE_LEFT bypass bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOMLBypassBins());
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_MERGE_UP_BITS_BINS
  printf("\n\n");
  printf("Total number of SAO MERGE_UP bits: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOMUBits());
  printf("Total number of SAO MERGE_UP context bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOMUContextBins());
  printf("Total number of SAO MERGE_UP bypass bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOMUBypassBins());
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_LEFT_BAND_BITS_BINS
  printf("\n\n");
  printf("Total number of SAO LEFT_BAND bits: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOLBBits());
  printf("Total number of SAO LEFT_BAND context bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOLBContextBins());
  printf("Total number of SAO LEFT_BAND bypass bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOLBBypassBins());
#endif
#if AHG5_COUNT_XXX_BITS_BINS && AHG5_COUNT_SAO_SIGN_BITS_BINS
  printf("\n\n");
  printf("Total number of SAO SIGN bits: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOSIGNBits());
  printf("Total number of SAO SIGN context bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOSIGNContextBins());
  printf("Total number of SAO SIGN bypass bins: %d\n", m_cTDecTop.getEntropyDecoder()->getCountedSAOSIGNBypassBins());
#endif

  // delete buffers
  m_cTDecTop.deletePicBuffer();
  
  // destroy internal classes
  xDestroyDecLib();
}

// ====================================================================================================================
// Protected member functions
// ====================================================================================================================

Void TAppDecTop::xCreateDecLib()
{
  // create decoder class
  m_cTDecTop.create();
}

Void TAppDecTop::xDestroyDecLib()
{
  if ( m_pchReconFile )
  {
    m_cTVideoIOYuvReconFile. close();
  }
  
  // destroy decoder class
  m_cTDecTop.destroy();
}

Void TAppDecTop::xInitDecLib()
{
  // initialize decoder class
  m_cTDecTop.init();
  m_cTDecTop.setPictureDigestEnabled(m_pictureDigestEnabled);
}

/** \param pcListPic list of pictures to be written to file
    \todo            DYN_REF_FREE should be revised
 */
Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, UInt tId )
{
  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
  Int not_displayed = 0;

  while (iterPic != pcListPic->end())
  {
    TComPic* pcPic = *(iterPic);
    if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay)
    {
       not_displayed++;
    }
    iterPic++;
  }
  iterPic   = pcListPic->begin();
  
  while (iterPic != pcListPic->end())
  {
    TComPic* pcPic = *(iterPic);
    TComSPS *sps = pcPic->getSlice(0)->getSPS();
    
    if ( pcPic->getOutputMark() && (not_displayed >  pcPic->getSlice(0)->getSPS()->getNumReorderPics(tId) && pcPic->getPOC() > m_iPOCLastDisplay))
    {
      // write to file
       not_displayed--;
      if ( m_pchReconFile )
      {
        m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(), sps->getPicCropLeftOffset(), sps->getPicCropRightOffset(), sps->getPicCropTopOffset(), sps->getPicCropBottomOffset() );
      }
      
      // update POC of display order
      m_iPOCLastDisplay = pcPic->getPOC();
      
      // erase non-referenced picture in the reference picture list after display
      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
      {
#if !DYN_REF_FREE
        pcPic->setReconMark(false);
        
        // mark it should be extended later
        pcPic->getPicYuvRec()->setBorderExtension( false );
        
#else
        pcPic->destroy();
        pcListPic->erase( iterPic );
        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
        continue;
#endif
      }
      pcPic->setOutputMark(false);
    }
    
    iterPic++;
  }
}

/** \param pcListPic list of pictures to be written to file
    \todo            DYN_REF_FREE should be revised
 */
Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic )
{
  if(!pcListPic)
  {
    return;
  } 
  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();

  iterPic   = pcListPic->begin();
  
  while (iterPic != pcListPic->end())
  {
    TComPic* pcPic = *(iterPic);
    TComSPS *sps = pcPic->getSlice(0)->getSPS();

    if ( pcPic->getOutputMark() )
    {
      // write to file
      if ( m_pchReconFile )
      {
        m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(), sps->getPicCropLeftOffset(), sps->getPicCropRightOffset(), sps->getPicCropTopOffset(), sps->getPicCropBottomOffset() );
      }
      
      // update POC of display order
      m_iPOCLastDisplay = pcPic->getPOC();
      
      // erase non-referenced picture in the reference picture list after display
      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
      {
#if !DYN_REF_FREE
        pcPic->setReconMark(false);
        
        // mark it should be extended later
        pcPic->getPicYuvRec()->setBorderExtension( false );
        
#else
        pcPic->destroy();
        pcListPic->erase( iterPic );
        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
        continue;
#endif
      }
      pcPic->setOutputMark(false);
    }
    
    iterPic++;
  }
  pcListPic->clear();
  m_iPOCLastDisplay = -MAX_INT;
}

//! \}
