2013-12-09 04:51:09 -08:00
/*!
* \ copy
* Copyright ( c ) 2009 - 2013 , Cisco Systems
* 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 .
*
* 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 .
*
*
2015-04-27 15:29:29 +03:00
* \ file encoder_context . h
2013-12-09 04:51:09 -08:00
*
2015-04-27 15:29:29 +03:00
* \ brief Main pData to be operated over Wels encoder all modules
2013-12-09 04:51:09 -08:00
*
2015-04-27 15:29:29 +03:00
* \ date 2 / 4 / 2009 Created
2013-12-09 04:51:09 -08:00
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
# ifndef WELS_ENCODER_CONTEXT_H__
# define WELS_ENCODER_CONTEXT_H__
# include <stdio.h>
# include "typedefs.h"
# include "param_svc.h"
# include "nal_encap.h"
# include "picture.h"
# include "dq_map.h"
# include "stat.h"
# include "macros.h"
# include "rc.h"
# include "as264_common.h"
# include "wels_preprocess.h"
# include "wels_func_ptr_def.h"
2014-01-26 16:04:52 +02:00
# include "crt_util_safe_x.h"
2014-06-10 12:29:14 +03:00
# include "utils.h"
2013-12-09 04:51:09 -08:00
2015-05-15 11:09:11 +03:00
# include "mt_defs.h" // for multiple threadin,
2013-12-09 04:51:09 -08:00
# include "WelsThreadLib.h"
2015-10-28 09:39:26 -07:00
# include "wels_task_management.h"
2013-12-09 04:51:09 -08:00
2014-08-11 16:08:49 +08:00
namespace WelsEnc {
2013-12-09 04:51:09 -08:00
2015-10-19 22:48:28 -07:00
class IWelsTaskManage ;
2013-12-09 04:51:09 -08:00
/*
2015-05-15 10:34:02 +03:00
* reference list for each quality layer in SVC
2013-12-09 04:51:09 -08:00
*/
typedef struct TagRefList {
2015-06-03 09:56:54 +03:00
SPicture * pShortRefList [ 1 + MAX_SHORT_REF_COUNT ] ; // reference list 0 - int16_t
SPicture * pLongRefList [ 1 + MAX_REF_PIC_COUNT ] ; // reference list 1 - int32_t
SPicture * pNextBuffer ;
SPicture * pRef [ 1 + MAX_REF_PIC_COUNT ] ; // plus 1 for swap intend
uint8_t uiShortRefCount ;
uint8_t uiLongRefCount ; // dependend on pRef pic module
2013-12-09 04:51:09 -08:00
} SRefList ;
2013-12-12 14:21:12 -08:00
typedef struct TagLTRState {
2014-06-25 18:50:41 -07:00
// LTR mark feedback
2015-06-03 09:56:54 +03:00
uint32_t uiLtrMarkState ; // LTR mark state, indicate whether there is a LTR mark feedback unsolved
int32_t iLtrMarkFbFrameNum ; // the unsolved LTR mark feedback, the marked iFrameNum feedback from decoder
2014-06-25 18:50:41 -07:00
// LTR used as recovery reference
2015-06-03 09:56:54 +03:00
int32_t iLastRecoverFrameNum ; // reserve the last LTR or IDR recover iFrameNum
int32_t iLastCorFrameNumDec ; // reserved the last correct position in decoder side, use to select valid LTR to recover or to decide the LTR mark validation
int32_t iCurFrameNumInDec ; // current iFrameNum in decoder side, use to select valid LTR to recover or to decide the LTR mark validation
2014-06-25 18:50:41 -07:00
// LTR mark
2015-06-03 09:56:54 +03:00
int32_t iLTRMarkMode ; // direct mark or delay mark
int32_t iLTRMarkSuccessNum ; //successful marked num, for mark mode switch
int32_t iCurLtrIdx ; // current int32_t term reference index to mark
int32_t iLastLtrIdx [ MAX_TEMPORAL_LAYER_NUM ] ;
int32_t iSceneLtrIdx ; // related to Scene LTR, used by screen content
2014-06-25 18:50:41 -07:00
2015-06-03 09:56:54 +03:00
uint32_t uiLtrMarkInterval ; // the interval from the last int32_t term pRef mark
2014-06-25 18:50:41 -07:00
2015-06-03 09:56:54 +03:00
bool bLTRMarkingFlag ; //decide whether current frame marked as LTR
bool bLTRMarkEnable ; //when LTR is confirmed and the interval is no smaller than the marking period
bool bReceivedT0LostFlag ; // indicate whether a t0 lost feedback is recieved, for LTR recovery
2013-12-12 14:21:12 -08:00
} SLTRState ;
typedef struct TagSpatialPicIndex {
2015-06-03 09:56:54 +03:00
SPicture * pSrc ; // I420 based and after color space converted
int32_t iDid ; // dependency id
2013-12-09 04:51:09 -08:00
} SSpatialPicIndex ;
typedef struct TagStrideTables {
2015-06-03 09:56:54 +03:00
int32_t * pStrideDecBlockOffset [ MAX_DEPENDENCY_LAYER ] [ 2 ] ; // [iDid][tid==0][24 x 4]: luma+chroma= 24 x 4
int32_t * pStrideEncBlockOffset [ MAX_DEPENDENCY_LAYER ] ; // [iDid][24 x 4]: luma+chroma= 24 x 4
int16_t * pMbIndexX [ MAX_DEPENDENCY_LAYER ] ; // [iDid][iMbX]: map for iMbX in each spatial layer coding
int16_t * pMbIndexY [ MAX_DEPENDENCY_LAYER ] ; // [iDid][iMbY]: map for iMbY in each spatial layer coding
2013-12-09 04:51:09 -08:00
} SStrideTables ;
2013-12-12 14:21:12 -08:00
typedef struct TagWelsEncCtx {
2014-09-05 10:57:51 +08:00
SLogContext sLogCtx ;
2014-06-25 18:50:41 -07:00
// Input
2015-06-03 09:56:54 +03:00
SWelsSvcCodingParam * pSvcParam ; // SVC parameter, WelsSVCParamConfig in svc_param_settings.h
2014-09-05 10:57:51 +08:00
2015-06-03 09:56:54 +03:00
int32_t * pSadCostMb ;
2014-09-05 10:57:51 +08:00
/* MVD cost tables for Inter MB */
2015-06-03 09:56:54 +03:00
int32_t iMvRange ;
uint16_t * pMvdCostTable ; //[52]; // adaptive to spatial layers
int32_t iMvdCostTableSize ; //the size of above table
int32_t iMvdCostTableStride ; //the stride of above table
2014-09-05 10:57:51 +08:00
SMVUnitXY *
2015-06-03 09:56:54 +03:00
pMvUnitBlock4x4 ; // (*pMvUnitBlock4x4[2])[MB_BLOCK4x4_NUM]; // for store each 4x4 blocks' mv unit, the two swap after different d layer
2014-09-05 10:57:51 +08:00
int8_t *
2015-06-03 09:56:54 +03:00
pRefIndexBlock4x4 ; // (*pRefIndexBlock4x4[2])[MB_BLOCK8x8_NUM]; // for store each 4x4 blocks' pRef index, the two swap after different d layer
int8_t * pNonZeroCountBlocks ; // (*pNonZeroCountBlocks)[MB_LUMA_CHROMA_BLOCK4x4_NUM];
2014-09-05 10:57:51 +08:00
int8_t *
2015-06-03 09:56:54 +03:00
pIntra4x4PredModeBlocks ; // (*pIntra4x4PredModeBlocks)[INTRA_4x4_MODE_NUM]; //last byte is not used; the first 4 byte is for the bottom 12,13,14,15 4x4 block intra mode, and 3 byte for (3,7,11)
2014-09-05 10:57:51 +08:00
2015-06-03 09:56:54 +03:00
SMB * * ppMbListD ; // [MAX_DEPENDENCY_LAYER];
SStrideTables * pStrideTab ; // stride tables for internal coding used
SWelsFuncPtrList * pFuncList ;
2014-09-05 10:57:51 +08:00
2015-06-03 09:56:54 +03:00
SSliceThreading * pSliceThreading ;
2015-10-28 09:39:26 -07:00
IWelsTaskManage * pTaskManage ; //was planning to put it under CWelsH264SVCEncoder but it may be updated (lock/no lock) when param is changed
2014-06-25 18:50:41 -07:00
2015-11-19 11:03:50 +08:00
// pointers
2015-06-03 09:56:54 +03:00
SPicture * pEncPic ; // pointer to current picture to be encoded
SPicture * pDecPic ; // pointer to current picture being reconstructed
SPicture * pRefPic ; // pointer to current reference picture
SDqLayer * pCurDqLayer ; // DQ layer context used to being encoded currently, for reference base layer to refer: pCurDqLayer->pRefLayer if applicable
SDqLayer * * ppDqLayerList ; // overall DQ layers encoded for storage
SRefList * * ppRefPicListExt ; // reference picture list for SVC
SPicture * pRefList0 [ 16 ] ;
SLTRState * pLtr ; //[MAX_DEPENDENCY_LAYER];
bool bCurFrameMarkedAsSceneLtr ;
2014-06-25 18:50:41 -07:00
// Derived
2016-01-14 09:16:12 +08:00
2015-06-03 09:56:54 +03:00
EWelsSliceType eSliceType ; // currently coding slice type
EWelsNalUnitType eNalType ; // NAL type
EWelsNalRefIdc eNalPriority ; // NAL_Reference_Idc currently
2015-09-21 15:42:17 -07:00
EWelsNalRefIdc eLastNalPriority [ MAX_DEPENDENCY_LAYER ] ; // NAL_Reference_Idc in last frame
2015-06-03 09:56:54 +03:00
uint8_t iNumRef0 ;
uint8_t uiDependencyId ; // Idc of dependecy layer to be coded
uint8_t uiTemporalId ; // Idc of temporal layer to be coded
bool bNeedPrefixNalFlag ; // whether add prefix nal
2014-06-25 18:50:41 -07:00
// Rate control routine
2015-06-03 09:56:54 +03:00
SWelsSvcRc * pWelsSvcRc ;
2014-12-05 12:09:08 +08:00
bool bCheckWindowStatusRefreshFlag ;
int64_t iCheckWindowStartTs ;
int64_t iCheckWindowCurrentTs ;
int32_t iCheckWindowInterval ;
int32_t iCheckWindowIntervalShift ;
bool bCheckWindowShiftResetFlag ;
2015-06-03 09:56:54 +03:00
int32_t iSkipFrameFlag ; //_GOM_RC_
2014-12-05 12:09:08 +08:00
int32_t iContinualSkipFrames ;
2015-06-03 09:56:54 +03:00
int32_t iGlobalQp ; // global qp
2014-06-25 18:50:41 -07:00
// VAA
2015-06-03 09:56:54 +03:00
SVAAFrameInfo * pVaa ; // VAA information of reference
CWelsPreProcess * pVpp ;
2014-09-05 10:57:51 +08:00
2015-06-03 09:56:54 +03:00
SWelsSPS * pSpsArray ; // MAX_SPS_COUNT by standard compatible
SWelsSPS * pSps ;
SWelsPPS * pPPSArray ; // MAX_PPS_COUNT by standard compatible
SWelsPPS * pPps ;
2014-09-05 10:57:51 +08:00
/* SVC only */
2015-06-03 09:56:54 +03:00
SSubsetSps * pSubsetArray ; // MAX_SPS_COUNT by standard compatible
SSubsetSps * pSubsetSps ;
int32_t iSpsNum ; // number of pSps used
int32_t iSubsetSpsNum ; // number of pSps used
int32_t iPpsNum ; // number of pPps used
2014-06-25 18:50:41 -07:00
// Output
2015-06-03 09:56:54 +03:00
SWelsEncoderOutput * pOut ; // for NAL raw pData (need allocating memory for sNalList internal)
uint8_t * pFrameBs ; // restoring bitstream pBuffer of all NALs in a frame
int32_t iFrameBsSize ; // count size of frame bs in bytes allocated
int32_t iPosBsBuffer ; // current writing position of frame bs pBuffer
2014-06-25 18:50:41 -07:00
2015-06-03 09:56:54 +03:00
SSpatialPicIndex sSpatialIndexMap [ MAX_DEPENDENCY_LAYER ] ;
2015-12-15 17:10:52 +08:00
int32_t iSliceBufferSize [ MAX_DEPENDENCY_LAYER ] ;
2014-06-25 18:50:41 -07:00
2015-06-03 09:56:54 +03:00
bool bRefOfCurTidIsLtr [ MAX_DEPENDENCY_LAYER ] [ MAX_TEMPORAL_LEVEL ] ;
uint16_t uiIdrPicId ; // IDR picture id: [0, 65535], this one is used for LTR
int32_t iMaxSliceCount ; // maximal count number of slices for all layers observation
int16_t iActiveThreadsNum ; // number of threads active so far
2014-06-25 18:50:41 -07:00
2014-09-05 10:57:51 +08:00
/*
* DQ layer idc map for svc encoding , might be a better scheme than that of design before ,
* can aware idc of referencing layer and that idc of successive layer to be coded
*/
/* SVC only */
2015-06-03 09:56:54 +03:00
SDqIdc * pDqIdcMap ; // overall DQ map of full scalability in specific frame (All full D/T/Q layers involved) // pDqIdcMap[dq_index] for each SDqIdc pData
2014-06-25 18:50:41 -07:00
2015-06-03 09:56:54 +03:00
SParaSetOffset sPSOVector ;
SParaSetOffset * pPSOVector ;
CMemoryAlign * pMemAlign ;
2013-12-09 04:51:09 -08:00
2013-12-12 14:21:12 -08:00
# if defined(STAT_OUTPUT)
2014-06-25 18:50:41 -07:00
// overall stat pData, refer to SStatData in stat.h, in case avc to use stat[0][0]
2015-06-03 09:56:54 +03:00
SStatData sStatData [ MAX_DEPENDENCY_LAYER ] [ MAX_QUALITY_LEVEL ] ;
SStatSliceInfo sPerInfo ;
2014-01-05 14:35:37 +02:00
# endif //STAT_OUTPUT
2013-12-09 04:51:09 -08:00
2014-10-22 11:35:17 +08:00
//related to Statistics
int64_t uiStartTimestamp ;
2016-01-05 14:02:58 +08:00
SEncoderStatistics sEncoderStatistics [ MAX_DEPENDENCY_LAYER ] ;
2014-10-22 11:35:17 +08:00
int32_t iStatisticsLogInterval ;
int64_t iLastStatisticsLogTs ;
2016-01-05 14:02:58 +08:00
int64_t iTotalEncodedBytes [ MAX_DEPENDENCY_LAYER ] ;
int64_t iLastStatisticsBytes [ MAX_DEPENDENCY_LAYER ] ;
int64_t iLastStatisticsFrameCount [ MAX_DEPENDENCY_LAYER ] ;
2014-10-22 11:35:17 +08:00
2014-09-05 10:57:51 +08:00
int32_t iEncoderError ;
2015-06-03 09:56:54 +03:00
WELS_MUTEX mutexEncoderError ;
2014-09-05 10:57:51 +08:00
bool bDeliveryFlag ;
2014-10-27 13:06:33 +08:00
SStateCtx sWelsCabacContexts [ 4 ] [ WELS_QP_MAX + 1 ] [ WELS_CONTEXT_COUNT ] ;
2014-06-26 15:04:30 +03:00
# ifdef ENABLE_FRAME_DUMP
2014-09-05 10:57:51 +08:00
bool bDependencyRecFlag [ MAX_DEPENDENCY_LAYER ] ;
bool bRecFlag ;
2014-06-26 15:04:30 +03:00
# endif
2015-02-05 16:34:28 +08:00
uint32_t GetNeededSpsNum ( ) {
if ( 0 = = sPSOVector . uiNeededSpsNum ) {
2015-02-11 17:31:17 +08:00
sPSOVector . uiNeededSpsNum = ( ( SPS_LISTING & pSvcParam - > eSpsPpsIdStrategy ) ? ( MAX_SPS_COUNT ) : ( 1 ) ) ;
sPSOVector . uiNeededSpsNum * = ( ( pSvcParam - > bSimulcastAVC ) ? ( pSvcParam - > iSpatialLayerNum ) : ( 1 ) ) ;
2015-02-05 16:34:28 +08:00
}
return sPSOVector . uiNeededSpsNum ;
}
uint32_t GetNeededSubsetSpsNum ( ) {
if ( 0 = = sPSOVector . uiNeededSubsetSpsNum ) {
sPSOVector . uiNeededSubsetSpsNum = ( ( pSvcParam - > bSimulcastAVC ) ? ( 0 ) :
( ( SPS_LISTING & pSvcParam - > eSpsPpsIdStrategy ) ? ( MAX_SPS_COUNT ) : ( pSvcParam - > iSpatialLayerNum - 1 ) ) ) ;
}
return sPSOVector . uiNeededSubsetSpsNum ;
}
uint32_t GetNeededPpsNum ( ) {
if ( 0 = = sPSOVector . uiNeededPpsNum ) {
sPSOVector . uiNeededPpsNum = ( ( pSvcParam - > eSpsPpsIdStrategy & SPS_PPS_LISTING ) ? ( MAX_PPS_COUNT ) :
( 1 + pSvcParam - > iSpatialLayerNum ) ) ;
2015-02-11 17:31:17 +08:00
sPSOVector . uiNeededPpsNum * = ( ( pSvcParam - > bSimulcastAVC ) ? ( pSvcParam - > iSpatialLayerNum ) : ( 1 ) ) ;
2015-02-05 16:34:28 +08:00
}
return sPSOVector . uiNeededPpsNum ;
}
2013-12-12 14:21:12 -08:00
} sWelsEncCtx /*, *PWelsEncCtx*/ ;
2013-12-09 04:51:09 -08:00
}
# endif //sWelsEncCtx_H__