⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mctf.cpp

📁 JMVM MPEG MVC/3DAV 测试平台 国际通用标准
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
********************************************************************************

NOTE - One of the two copyright statements below may be chosen
that applies for the software.

********************************************************************************

This software module was originally developed by

Heiko Schwarz    (Fraunhofer HHI),
Tobias Hinz      (Fraunhofer HHI),
Karsten Suehring (Fraunhofer HHI)

in the course of development of the ISO/IEC 14496-10:2005 Amd.1 (Scalable Video
Coding) for reference purposes and its performance may not have been optimized.
This software module is an implementation of one or more tools as specified by
the ISO/IEC 14496-10:2005 Amd.1 (Scalable Video Coding).

Those intending to use this software module in products are advised that its
use may infringe existing patents. ISO/IEC have no liability for use of this
software module or modifications thereof.

Assurance that the originally developed software module can be used
(1) in the ISO/IEC 14496-10:2005 Amd.1 (Scalable Video Coding) once the
ISO/IEC 14496-10:2005 Amd.1 (Scalable Video Coding) has been adopted; and
(2) to develop the ISO/IEC 14496-10:2005 Amd.1 (Scalable Video Coding): 

To the extent that Fraunhofer HHI owns patent rights that would be required to
make, use, or sell the originally developed software module or portions thereof
included in the ISO/IEC 14496-10:2005 Amd.1 (Scalable Video Coding) in a
conforming product, Fraunhofer HHI will assure the ISO/IEC that it is willing
to negotiate licenses under reasonable and non-discriminatory terms and
conditions with applicants throughout the world.

Fraunhofer HHI retains full right to modify and use the code for its own
purpose, assign or donate the code to a third party and to inhibit third
parties from using the code for products that do not conform to MPEG-related
ITU Recommendations and/or ISO/IEC International Standards. 

This copyright notice must be included in all copies or derivative works.
Copyright (c) ISO/IEC 2005. 

********************************************************************************

COPYRIGHT AND WARRANTY INFORMATION

Copyright 2005, International Telecommunications Union, Geneva

The Fraunhofer HHI hereby donate this source code to the ITU, with the following
understanding:
1. Fraunhofer HHI retain the right to do whatever they wish with the
contributed source code, without limit.
2. Fraunhofer HHI retain full patent rights (if any exist) in the technical
content of techniques and algorithms herein.
3. The ITU shall make this code available to anyone, free of license or
royalty fees.

DISCLAIMER OF WARRANTY

These software programs are available to the user without any license fee or
royalty on an "as is" basis. The ITU disclaims any and all warranties, whether
express, implied, or statutory, including any implied warranties of
merchantability or of fitness for a particular purpose. In no event shall the
contributor or the ITU be liable for any incidental, punitive, or consequential
damages of any kind whatsoever arising from the use of these programs.

This disclaimer of warranty extends to the user of these programs and user's
customers, employees, agents, transferees, successors, and assigns.

The ITU does not represent or warrant that the programs furnished hereunder are
free of infringement of any third-party patents. Commercial implementations of
ITU-T Recommendations, including shareware, may be subject to royalty fees to
patent holders. Information regarding the ITU-T patent policy is available from 
the ITU Web site at http://www.itu.int.

THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE ITU-T PATENT POLICY.

********************************************************************************
*/

#include "H264AVCEncoderLib.h"
#include "MCTF.h"
#include "PreProcessorParameter.h"
#include "../../lib/H264AVCEncoderLib/SliceEncoder.h"

H264AVC_NAMESPACE_BEGIN

#define FACTOR_22_HP  0.70710678118654752440084436210485  //sqrt( 1.0/ 2.0)
#define FACTOR_22_LP  1.4142135623730950488016887242097   //sqrt( 2.0/ 1.0)
#define FACTOR_53_HP  0.84779124789065851738306954082825  //sqrt(23.0/32.0)
#define FACTOR_53_LP  1.2247448713915890490986420373529   //sqrt( 3.0/ 2.0)


MCTF::MCTF()
: m_pcSPS                   ( 0 )
, m_pcPPS                   ( 0 )
, m_pcYuvFullPelBufferCtrl  ( 0 )
, m_pcYuvHalfPelBufferCtrl  ( 0 )
, m_pcMbEncoder             ( 0 )
, m_pcMotionEstimation      ( 0 )
, m_bFirstGOPCoded          ( false )
, m_uiGOPSize               ( 0 )
, m_uiDecompositionStages   ( 0 )
, m_uiFrameWidthInMb        ( 0 )
, m_uiFrameHeightInMb       ( 0 )
, m_uiMbNumber              ( 0 )
, m_papcFrame               ( 0 )
, m_papcResidual            ( 0 )
, m_pcFrameTemp             ( 0 )
, m_pacControlData          ( 0 )
{
}


MCTF::~MCTF()
{
}


ErrVal
MCTF::create( MCTF*& rpcMCTF )
{
  rpcMCTF = new MCTF;
  ROF( rpcMCTF );
  return Err::m_nOK;
}


ErrVal
MCTF::destroy()
{
  delete this;
  return Err::m_nOK;
}


ErrVal
MCTF::init( PreProcessorParameter*  pcParameter,
            MbEncoder*              pcMbEncoder,
            YuvBufferCtrl*          pcYuvFullPelBufferCtrl,
            YuvBufferCtrl*          pcYuvHalfPelBufferCtrl,
            QuarterPelFilter*       pcQuarterPelFilter,
            MotionEstimation*       pcMotionEstimation )
{
  ROF( pcParameter );
  ROF( pcMbEncoder );
  ROF( pcYuvFullPelBufferCtrl );
  ROF( pcYuvHalfPelBufferCtrl );
  ROF( pcQuarterPelFilter );
  ROF( pcMotionEstimation );

  //----- references -----
  m_pcSPS                   = 0;
  m_pcPPS                   = 0;
  m_pcYuvFullPelBufferCtrl  = pcYuvFullPelBufferCtrl;
  m_pcYuvHalfPelBufferCtrl  = pcYuvHalfPelBufferCtrl;
  m_pcMbEncoder             = pcMbEncoder;
  m_pcQuarterPelFilter      = pcQuarterPelFilter;
  m_pcMotionEstimation      = pcMotionEstimation;

  //----- control parameters -----
  m_uiDecompositionStages   = (UInt)floor( 0.5 + log10( (Double)pcParameter->getGOPSize() ) / log10( 2.0 ) );

  for( UInt uiStage = 0; uiStage < MAX_DSTAGES; uiStage++ )
  {
    m_adBaseQpLambdaMotion[uiStage] = pcParameter->getQP();
  }

  //----- init parameter sets -----
  m_uiFrameWidthInMb      = pcParameter->getFrameWidth  () >> 4;
  m_uiFrameHeightInMb     = pcParameter->getFrameHeight () >> 4;
  m_uiMbNumber            = m_uiFrameWidthInMb * m_uiFrameHeightInMb;
  m_bFirstGOPCoded        = false;
  RNOK( SequenceParameterSet::create( m_pcSPS ) );
  RNOK( PictureParameterSet ::create( m_pcPPS ) );
  m_pcSPS->setFrameWidthInMbs           ( m_uiFrameWidthInMb  );
  m_pcSPS->setFrameHeightInMbs          ( m_uiFrameHeightInMb );
  m_pcSPS->setDirect8x8InferenceFlag    ( true  );
  m_pcPPS->setNumRefIdxActive           ( LIST_0, 1 );
  m_pcPPS->setNumRefIdxActive           ( LIST_1, 1 );
  m_pcPPS->setConstrainedIntraPredFlag  ( true );

  //----- init frame buffer controls -----
  RNOK( m_pcYuvFullPelBufferCtrl->initSPS( m_uiFrameHeightInMb<<4, m_uiFrameWidthInMb<<4, YUV_Y_MARGIN, YUV_X_MARGIN    ) );
  RNOK( m_pcYuvHalfPelBufferCtrl->initSPS( m_uiFrameHeightInMb<<4, m_uiFrameWidthInMb<<4, YUV_Y_MARGIN, YUV_X_MARGIN, 1 ) );

  //------ re-allocate dynamic memory -----
  RNOK( xDeleteData() );
  RNOK( xCreateData( *m_pcSPS ) );

  return Err::m_nOK;
}


ErrVal
MCTF::xCreateData( const SequenceParameterSet& rcSPS )
{
  UInt uiIndex;

  ROFS   ( ( m_papcFrame       = new IntFrame*   [ ( 1 << m_uiDecompositionStages ) + 1 ] ) );
  ROFS   ( ( m_papcResidual    = new IntFrame*   [ ( 1 << m_uiDecompositionStages ) + 1 ] ) );
  ROFS   ( ( m_pacControlData  = new ControlData [ ( 1 << m_uiDecompositionStages ) + 1 ] ) );

  for( uiIndex = 0; uiIndex <= ( 1U << m_uiDecompositionStages ); uiIndex++ )
  {
    ROFS ( ( m_papcFrame         [ uiIndex ] = new IntFrame( *m_pcYuvFullPelBufferCtrl,
                                                              *m_pcYuvHalfPelBufferCtrl ) ) );
    ROFS ( ( m_papcResidual      [ uiIndex ] = new IntFrame( *m_pcYuvFullPelBufferCtrl,
                                                              *m_pcYuvHalfPelBufferCtrl ) ) );
    RNOK  (   m_papcFrame         [ uiIndex ] ->init        () );
    RNOK  (   m_papcResidual      [ uiIndex ] ->init        () );

    MbDataCtrl*   pcMbDataCtrl                = 0;
    ROFS ( (     pcMbDataCtrl                = new MbDataCtrl  () ) );
    RNOK  (       pcMbDataCtrl                ->init            ( rcSPS ) );
    RNOK  (       m_pacControlData[ uiIndex ] . setMbDataCtrl   ( pcMbDataCtrl ) );

    SliceHeader*  pcSliceHeader               = 0;
    ROFS ( (     pcSliceHeader               = new SliceHeader ( *m_pcSPS, *m_pcPPS ) ) );
    RNOK  (       m_pacControlData[ uiIndex ] . setSliceHeader  (  pcSliceHeader ) );
  }

  ROFS   ( ( m_pcFrameTemp                   = new IntFrame( *m_pcYuvFullPelBufferCtrl,
                                                              *m_pcYuvHalfPelBufferCtrl ) ) );
  RNOK    (   m_pcFrameTemp                   ->init        () );

  return Err::m_nOK;
}


ErrVal
MCTF::xDeleteData()
{
  UInt uiIndex;

  if( m_papcFrame )
  {
    for( uiIndex = 0; uiIndex <= ( 1U << m_uiDecompositionStages ); uiIndex++ )
    {
      if( m_papcFrame[ uiIndex ] )
      {
        RNOK(   m_papcFrame[ uiIndex ]->uninit() );
        delete  m_papcFrame[ uiIndex ];
        m_papcFrame[ uiIndex ] = 0;
      }
    }
    delete [] m_papcFrame;
    m_papcFrame = 0;
  }

  if( m_papcResidual )
  {
    for( uiIndex = 0; uiIndex <= ( 1U << m_uiDecompositionStages ); uiIndex++ )
    {
      if( m_papcResidual[ uiIndex ] )
      {
        RNOK(   m_papcResidual[ uiIndex ]->uninit() );
        delete  m_papcResidual[ uiIndex ];
        m_papcResidual[ uiIndex ] = 0;
      }
    }
    delete [] m_papcResidual;
    m_papcResidual = 0;
  }

  if( m_pcFrameTemp )
  {
    RNOK(   m_pcFrameTemp->uninit() );
    delete  m_pcFrameTemp;
    m_pcFrameTemp = 0;
  }

  if( m_pacControlData )
  {
    for( uiIndex = 0; uiIndex <= ( 1U << m_uiDecompositionStages ); uiIndex++ )
    {
      MbDataCtrl*   pcMbDataCtrl  = m_pacControlData[ uiIndex ].getMbDataCtrl  ();
      SliceHeader*  pcSliceHeader = m_pacControlData[ uiIndex ].getSliceHeader ();
      if( pcMbDataCtrl )
      {
        RNOK( pcMbDataCtrl->uninit() );
      }
      delete pcMbDataCtrl;
      delete pcSliceHeader;
    }
    delete [] m_pacControlData;
    m_pacControlData = 0;
  }

  return Err::m_nOK;
}


ErrVal
MCTF::uninit()
{
  xDeleteData();

  return Err::m_nOK;
}


ErrVal
MCTF::xFillAndUpsampleFrame( IntFrame* rcFrame )
{
  RNOK( m_pcYuvFullPelBufferCtrl->initMb() );
  RNOK( m_pcYuvHalfPelBufferCtrl->initMb() );

  if( ! rcFrame->isHalfPel() )
  {
    RNOK( rcFrame->initHalfPel() );
  }
  RNOK( rcFrame->extendFrame( m_pcQuarterPelFilter ) );

  return Err::m_nOK;
}


ErrVal
MCTF::xFillAndExtendFrame( IntFrame* rcFrame )
{
  RNOK( m_pcYuvFullPelBufferCtrl->initMb() );
  RNOK( rcFrame->extendFrame( NULL ) );
  return Err::m_nOK;
}


ErrVal
MCTF::xMotionEstimation( RefFrameList*    pcRefFrameList0,
                         RefFrameList*    pcRefFrameList1,
                         const IntFrame*  pcOrigFrame,
                         ControlData&     rcControlData )
{
  SliceHeader&  rcSliceHeader = *rcControlData.getSliceHeader();
  MbDataCtrl*   pcMbDataCtrl  =  rcControlData.getMbDataCtrl ();

  //===== initialization =====
  RNOK( m_pcMotionEstimation->initSlice( rcSliceHeader ) );
  RNOK( m_pcMbEncoder       ->initSlice( rcSliceHeader ) );

  //===== loop over macroblocks =====
  for( UInt uiMbIndex = 0; uiMbIndex < m_uiMbNumber; uiMbIndex++ )
  {
    UInt          uiMbY               = uiMbIndex / m_uiFrameWidthInMb;
    UInt          uiMbX               = uiMbIndex % m_uiFrameWidthInMb;
    MbDataAccess* pcMbDataAccess      = 0;

    RNOK( pcMbDataCtrl            ->initMb  ( pcMbDataAccess, uiMbY, uiMbX ) );
    RNOK( m_pcYuvFullPelBufferCtrl->initMb  ( uiMbY, uiMbX ) );
    RNOK( m_pcYuvHalfPelBufferCtrl->initMb  ( uiMbY, uiMbX ) );
    RNOK( m_pcMotionEstimation    ->initMb  ( uiMbY, uiMbX, *pcMbDataAccess ) );

    RNOK( m_pcMbEncoder->estimatePrediction ( *pcMbDataAccess, NULL, 0, 
                                              *pcRefFrameList0, *pcRefFrameList1, NULL, NULL,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -