#ifndef WELS_DECODER_FRAMEWORK_H__
#define WELS_DECODER_FRAMEWORK_H__
#include "typedefs.h"
#include "utils.h"
#include "wels_const.h"
#include "wels_common_basis.h"
#include "wels_common_defs.h"
#include "codec_app_def.h"
#include "parameter_sets.h"
#include "nalu.h"
#include "dec_frame.h"
#include "pic_queue.h"
#include "vlc_decoder.h"
#include "fmo.h"
#include "crt_util_safe_x.h"
#include "mb_cache.h"
#include "expand_pic.h"
#include "mc.h"
#include "memory_align.h"
#include "wels_decoder_thread.h"
namespace WelsDec {
#define MAX_PRED_MODE_ID_I16x16 3
#define MAX_PRED_MODE_ID_CHROMA 3
#define MAX_PRED_MODE_ID_I4x4 8
#define WELS_QP_MAX 51
#define LONG_TERM_REF
#define IMinInt32 -0x7FFFFFFF
typedef struct SWels_Cabac_Element {
uint8_t uiState;
uint8_t uiMPS;
} SWelsCabacCtx, *PWelsCabacCtx;
typedef struct {
uint64_t uiRange;
uint64_t uiOffset;
int32_t iBitsLeft;
uint8_t* pBuffStart;
uint8_t* pBuffCurr;
uint8_t* pBuffEnd;
} SWelsCabacDecEngine, *PWelsCabacDecEngine;
#define NEW_CTX_OFFSET_MB_TYPE_I 3
#define NEW_CTX_OFFSET_SKIP 11
#define NEW_CTX_OFFSET_SUBMB_TYPE 21
#define NEW_CTX_OFFSET_B_SUBMB_TYPE 36
#define NEW_CTX_OFFSET_MVD 40
#define NEW_CTX_OFFSET_REF_NO 54
#define NEW_CTX_OFFSET_DELTA_QP 60
#define NEW_CTX_OFFSET_IPR 68
#define NEW_CTX_OFFSET_CIPR 64
#define NEW_CTX_OFFSET_CBP 73
#define NEW_CTX_OFFSET_CBF 85
#define NEW_CTX_OFFSET_MAP 105
#define NEW_CTX_OFFSET_LAST 166
#define NEW_CTX_OFFSET_ONE 227
#define NEW_CTX_OFFSET_ABS 232
#define NEW_CTX_OFFSET_TS_8x8_FLAG 399
#define CTX_NUM_MVD 7
#define CTX_NUM_CBP 4
#define NEW_CTX_OFFSET_TRANSFORM_SIZE_8X8_FLAG 399
#define NEW_CTX_OFFSET_MAP_8x8 402
#define NEW_CTX_OFFSET_LAST_8x8 417
#define NEW_CTX_OFFSET_ONE_8x8 426
#define NEW_CTX_OFFSET_ABS_8x8 431
typedef struct TagDataBuffer {
uint8_t* pHead;
uint8_t* pEnd;
uint8_t* pStartPos;
uint8_t* pCurPos;
} SDataBuffer;
#define SPS_PPS_BS_SIZE 128
typedef struct TagSpsBsInfo {
uint8_t pSpsBsBuf [SPS_PPS_BS_SIZE];
int32_t iSpsId;
uint16_t uiSpsBsLen;
} SSpsBsInfo;
typedef struct TagPpsBsInfo {
uint8_t pPpsBsBuf [SPS_PPS_BS_SIZE];
int32_t iPpsId;
uint16_t uiPpsBsLen;
} SPpsBsInfo;
typedef void (*PGetIntraPredFunc) (uint8_t* pPred, const int32_t kiLumaStride);
typedef void (*PIdctResAddPredFunc) (uint8_t* pPred, const int32_t kiStride, int16_t* pRs);
typedef void (*PIdctFourResAddPredFunc) (uint8_t* pPred, int32_t iStride, int16_t* pRs, const int8_t* pNzc);
typedef void (*PExpandPictureFunc) (uint8_t* pDst, const int32_t kiStride, const int32_t kiPicWidth,
const int32_t kiPicHeight);
typedef void (*PGetIntraPred8x8Func) (uint8_t* pPred, const int32_t kiLumaStride, bool bTLAvail, bool bTRAvail);
typedef struct TagRefPic {
PPicture pRefList[LIST_A][MAX_DPB_COUNT]; PPicture pShortRefList[LIST_A][MAX_DPB_COUNT];
PPicture pLongRefList[LIST_A][MAX_DPB_COUNT];
uint8_t uiRefCount[LIST_A];
uint8_t uiShortRefCount[LIST_A];
uint8_t uiLongRefCount[LIST_A]; int32_t iMaxLongTermFrameIdx;
} SRefPic, *PRefPic;
typedef void (*PCopyFunc) (uint8_t* pDst, int32_t iStrideD, uint8_t* pSrc, int32_t iStrideS);
typedef struct TagCopyFunc {
PCopyFunc pCopyLumaFunc;
PCopyFunc pCopyChromaFunc;
} SCopyFunc;
struct TagDeblockingFunc;
typedef struct tagDeblockingFilter {
uint8_t* pCsData[3]; int32_t iCsStride[2]; EWelsSliceType eSliceType;
int8_t iSliceAlphaC0Offset;
int8_t iSliceBetaOffset;
int8_t iChromaQP[2];
int8_t iLumaQP;
struct TagDeblockingFunc* pLoopf;
PPicture* pRefPics[LIST_A];
} SDeblockingFilter, *PDeblockingFilter;
typedef void (*PDeblockingFilterMbFunc) (PDqLayer pCurDqLayer, PDeblockingFilter filter, int32_t boundry_flag);
typedef void (*PLumaDeblockingLT4Func) (uint8_t* iSampleY, int32_t iStride, int32_t iAlpha, int32_t iBeta,
int8_t* iTc);
typedef void (*PLumaDeblockingEQ4Func) (uint8_t* iSampleY, int32_t iStride, int32_t iAlpha, int32_t iBeta);
typedef void (*PChromaDeblockingLT4Func) (uint8_t* iSampleCb, uint8_t* iSampleCr, int32_t iStride, int32_t iAlpha,
int32_t iBeta, int8_t* iTc);
typedef void (*PChromaDeblockingEQ4Func) (uint8_t* iSampleCb, uint8_t* iSampleCr, int32_t iStride, int32_t iAlpha,
int32_t iBeta);
typedef void (*PChromaDeblockingLT4Func2) (uint8_t* iSampleCbr, int32_t iStride, int32_t iAlpha,
int32_t iBeta, int8_t* iTc);
typedef void (*PChromaDeblockingEQ4Func2) (uint8_t* iSampleCbr, int32_t iStride, int32_t iAlpha,
int32_t iBeta);
typedef struct TagDeblockingFunc {
PLumaDeblockingLT4Func pfLumaDeblockingLT4Ver;
PLumaDeblockingEQ4Func pfLumaDeblockingEQ4Ver;
PLumaDeblockingLT4Func pfLumaDeblockingLT4Hor;
PLumaDeblockingEQ4Func pfLumaDeblockingEQ4Hor;
PChromaDeblockingLT4Func pfChromaDeblockingLT4Ver;
PChromaDeblockingEQ4Func pfChromaDeblockingEQ4Ver;
PChromaDeblockingLT4Func pfChromaDeblockingLT4Hor;
PChromaDeblockingEQ4Func pfChromaDeblockingEQ4Hor;
PChromaDeblockingLT4Func2 pfChromaDeblockingLT4Ver2;
PChromaDeblockingEQ4Func2 pfChromaDeblockingEQ4Ver2;
PChromaDeblockingLT4Func2 pfChromaDeblockingLT4Hor2;
PChromaDeblockingEQ4Func2 pfChromaDeblockingEQ4Hor2;
} SDeblockingFunc, *PDeblockingFunc;
typedef void (*PWelsNonZeroCountFunc) (int8_t* pNonZeroCount);
typedef void (*PWelsBlockZeroFunc) (int16_t* block, int32_t stride);
typedef struct TagBlockFunc {
PWelsNonZeroCountFunc pWelsSetNonZeroCountFunc;
PWelsBlockZeroFunc pWelsBlockZero16x16Func;
PWelsBlockZeroFunc pWelsBlockZero8x8Func;
} SBlockFunc;
typedef void (*PWelsFillNeighborMbInfoIntra4x4Func) (PWelsNeighAvail pNeighAvail, uint8_t* pNonZeroCount,
int8_t* pIntraPredMode, PDqLayer pCurDqLayer);
typedef void (*PWelsMapNeighToSample) (PWelsNeighAvail pNeighAvail, int32_t* pSampleAvail);
typedef void (*PWelsMap16NeighToSample) (PWelsNeighAvail pNeighAvail, uint8_t* pSampleAvail);
typedef int32_t (*PWelsParseIntra4x4ModeFunc) (PWelsNeighAvail pNeighAvail, int8_t* pIntraPredMode, PBitStringAux pBs,
PDqLayer pCurDqLayer);
typedef int32_t (*PWelsParseIntra16x16ModeFunc) (PWelsNeighAvail pNeighAvail, PBitStringAux pBs, PDqLayer pCurDqLayer);
enum {
OVERWRITE_NONE = 0,
OVERWRITE_PPS = 1,
OVERWRITE_SPS = 1 << 1,
OVERWRITE_SUBSETSPS = 1 << 2
};
typedef struct tagWelsWelsDecoderSpsPpsCTX {
SPosOffset sFrameCrop;
SSps sSpsBuffer[MAX_SPS_COUNT + 1];
SPps sPpsBuffer[MAX_PPS_COUNT + 1];
SSubsetSps sSubsetSpsBuffer[MAX_SPS_COUNT + 1];
SNalUnit sPrefixNal;
PSps pActiveLayerSps[MAX_LAYER_NUM];
bool bAvcBasedFlag;
bool bSpsExistAheadFlag; bool bSubspsExistAheadFlag; bool bPpsExistAheadFlag;
int32_t iSpsErrorIgnored;
int32_t iSubSpsErrorIgnored;
int32_t iPpsErrorIgnored;
bool bSpsAvailFlags[MAX_SPS_COUNT];
bool bSubspsAvailFlags[MAX_SPS_COUNT];
bool bPpsAvailFlags[MAX_PPS_COUNT];
int32_t iPPSLastInvalidId;
int32_t iPPSInvalidNum;
int32_t iSPSLastInvalidId;
int32_t iSPSInvalidNum;
int32_t iSubSPSLastInvalidId;
int32_t iSubSPSInvalidNum;
int32_t iSeqId; int iOverwriteFlags;
} SWelsDecoderSpsPpsCTX, *PWelsDecoderSpsPpsCTX;
typedef struct tagSWelsLastDecPicInfo {
SNalUnitHeaderExt sLastNalHdrExt;
SSliceHeader sLastSliceHeader;
int32_t iPrevPicOrderCntMsb;
int32_t iPrevPicOrderCntLsb;
PPicture pPreviousDecodedPictureInDpb; int32_t iPrevFrameNum; bool bLastHasMmco5;
uint32_t uiDecodingTimeStamp; } SWelsLastDecPicInfo, *PWelsLastDecPicInfo;
typedef struct tagPictInfo {
SBufferInfo sBufferInfo;
int32_t iPOC;
int32_t iPicBuffIdx;
uint32_t uiDecodingTimeStamp;
int32_t iSeqNum;
} SPictInfo, *PPictInfo;
typedef struct tagPictReoderingStatus {
int32_t iPictInfoIndex;
int32_t iMinSeqNum;
int32_t iMinPOC;
int32_t iNumOfPicts;
int32_t iLastWrittenSeqNum;
int32_t iLastWrittenPOC;
int32_t iLargestBufferedPicIndex;
bool bHasBSlice;
} SPictReoderingStatus, *PPictReoderingStatus;
typedef struct TagWelsDecoderContext {
SLogContext sLogCtx;
void*
pArgDec;
SDataBuffer sRawData;
SDataBuffer sSavedData;
SDecodingParam* pParam;
uint32_t uiCpuFlag;
VIDEO_BITSTREAM_TYPE eVideoType; bool bHaveGotMemory;
int32_t iImgWidthInPixel; int32_t iImgHeightInPixel; int32_t
iLastImgWidthInPixel; int32_t
iLastImgHeightInPixel; bool bFreezeOutput;
SNalUnitHeader sCurNalHead;
EWelsSliceType eSliceType; bool bUsedAsRef; int32_t iFrameNum;
int32_t iErrorCode; SFmo sFmoList[MAX_PPS_COUNT]; PFmo pFmo; int32_t iActiveFmoNum;
int32_t
iDecBlockOffsetArray[24];
struct {
uint32_t* pMbType[LAYER_NUM_EXCHANGEABLE];
int16_t (*pMv[LAYER_NUM_EXCHANGEABLE][LIST_A])[MB_BLOCK4x4_NUM][MV_A]; int8_t (*pRefIndex[LAYER_NUM_EXCHANGEABLE][LIST_A])[MB_BLOCK4x4_NUM];
int8_t (*pDirect[LAYER_NUM_EXCHANGEABLE])[MB_BLOCK4x4_NUM];
bool* pNoSubMbPartSizeLessThan8x8Flag[LAYER_NUM_EXCHANGEABLE];
bool* pTransformSize8x8Flag[LAYER_NUM_EXCHANGEABLE];
int8_t* pLumaQp[LAYER_NUM_EXCHANGEABLE];
int8_t (*pChromaQp[LAYER_NUM_EXCHANGEABLE])[2];
int16_t (*pMvd[LAYER_NUM_EXCHANGEABLE][LIST_A])[MB_BLOCK4x4_NUM][MV_A]; uint16_t* pCbfDc[LAYER_NUM_EXCHANGEABLE];
int8_t (*pNzc[LAYER_NUM_EXCHANGEABLE])[24];
int8_t (*pNzcRs[LAYER_NUM_EXCHANGEABLE])[24];
int16_t (*pScaledTCoeff[LAYER_NUM_EXCHANGEABLE])[MB_COEFF_LIST_SIZE];
int8_t (*pIntraPredMode[LAYER_NUM_EXCHANGEABLE])[8]; int8_t (*pIntra4x4FinalMode[LAYER_NUM_EXCHANGEABLE])[MB_BLOCK4x4_NUM];
uint8_t* pIntraNxNAvailFlag[LAYER_NUM_EXCHANGEABLE];
int8_t* pChromaPredMode[LAYER_NUM_EXCHANGEABLE];
int8_t* pCbp[LAYER_NUM_EXCHANGEABLE];
uint8_t (*pMotionPredFlag[LAYER_NUM_EXCHANGEABLE][LIST_A])[MB_PARTITION_SIZE]; uint32_t (*pSubMbType[LAYER_NUM_EXCHANGEABLE])[MB_SUB_PARTITION_SIZE];
int32_t* pSliceIdc[LAYER_NUM_EXCHANGEABLE]; int8_t* pResidualPredFlag[LAYER_NUM_EXCHANGEABLE];
int8_t* pInterPredictionDoneFlag[LAYER_NUM_EXCHANGEABLE];
bool* pMbCorrectlyDecodedFlag[LAYER_NUM_EXCHANGEABLE];
bool* pMbRefConcealedFlag[LAYER_NUM_EXCHANGEABLE];
uint32_t iMbWidth;
uint32_t iMbHeight;
} sMb;
PPicture pDec;
PPicture
pTempDec;
SRefPic sRefPic;
SRefPic sTmpRefPic; SVlcTable* pVlcTable;
SBitStringAux sBs;
int32_t iMaxBsBufferSizeInByte;
SWelsDecoderSpsPpsCTX sSpsPpsCtx;
bool bHasNewSps;
SPosOffset sFrameCrop;
PSliceHeader pSliceHeader;
PPicBuff pPicBuff; int32_t iPicQueueNumber;
PAccessUnit pAccessUnitList; PSps pSps; PPps pPps; PDqLayer
pCurDqLayer; PDqLayer pDqLayersList[LAYER_NUM_EXCHANGEABLE]; PNalUnit pNalCur; uint8_t uiNalRefIdc; int32_t iPicWidthReq; int32_t iPicHeightReq;
uint8_t uiTargetDqId; bool bEndOfStreamFlag; bool bInstantDecFlag; bool bInitialDqLayersMem;
bool bOnlyOneLayerInCurAuFlag;
bool bReferenceLostAtT0Flag;
int32_t iTotalNumMbRec; #ifdef LONG_TERM_REF
bool bParamSetsLostFlag;
bool
bCurAuContainLtrMarkSeFlag; int32_t iFrameNumOfAuMarkedLtr;
uint16_t uiCurIdrPicId;
#endif
bool bNewSeqBegin;
bool bNextNewSeqBegin;
int32_t *pStreamSeqNum;
int32_t iSeqNum;
bool bFramePending;
bool bFrameFinish;
int32_t iNalNum;
int32_t iMaxNalNum; SSpsBsInfo sSpsBsInfo [MAX_SPS_COUNT];
SSpsBsInfo sSubsetSpsBsInfo [MAX_PPS_COUNT];
SPpsBsInfo sPpsBsInfo [MAX_PPS_COUNT];
SParserBsInfo* pParserBsInfo;
PGetIntraPredFunc pGetI16x16LumaPredFunc[7]; PGetIntraPredFunc pGetI4x4LumaPredFunc[14]; PGetIntraPredFunc pGetIChromaPredFunc[7]; PIdctResAddPredFunc pIdctResAddPredFunc;
PIdctFourResAddPredFunc pIdctFourResAddPredFunc;
SMcFunc sMcFunc;
PGetIntraPred8x8Func pGetI8x8LumaPredFunc[14];
PIdctResAddPredFunc pIdctResAddPredFunc8x8;
SCopyFunc sCopyFunc;
SDeblockingFunc sDeblockingFunc;
SExpandPicFunc sExpandPicFunc;
SBlockFunc sBlockFunc;
int32_t iCurSeqIntervalTargetDependId;
int32_t iCurSeqIntervalMaxPicWidth;
int32_t iCurSeqIntervalMaxPicHeight;
PWelsFillNeighborMbInfoIntra4x4Func pFillInfoCacheIntraNxNFunc;
PWelsMapNeighToSample pMapNxNNeighToSampleFunc;
PWelsMap16NeighToSample pMap16x16NeighToSampleFunc;
int32_t iFeedbackVclNalInAu;
int32_t iFeedbackTidInAu;
int32_t iFeedbackNalRefIdc;
bool bAuReadyFlag;
bool bPrintFrameErrorTraceFlag; int32_t iIgnoredErrorInfoPacketCount; void* pTraceHandle;
PWelsLastDecPicInfo pLastDecPicInfo;
SWelsCabacCtx sWelsCabacContexts[4][WELS_QP_MAX + 1][WELS_CONTEXT_COUNT];
bool bCabacInited;
SWelsCabacCtx pCabacCtx[WELS_CONTEXT_COUNT];
PWelsCabacDecEngine pCabacDecEngine;
double dDecTime;
SDecoderStatistics* pDecoderStatistics; int32_t iMbEcedNum;
int32_t iMbEcedPropNum;
int32_t iMbNum;
bool bMbRefConcealed;
bool bRPLRError;
int32_t iECMVs[16][2];
PPicture pECRefPic[16];
unsigned long long uiTimeStamp;
uint32_t uiDecodingTimeStamp; uint16_t pDequant_coeff_buffer4x4[6][52][16];
uint16_t pDequant_coeff_buffer8x8[6][52][64];
uint16_t (*pDequant_coeff4x4[6])[16]; uint16_t (*pDequant_coeff8x8[6])[64]; int iDequantCoeffPpsid; bool bDequantCoeff4x4Init;
bool bUseScalingList;
CMemoryAlign* pMemAlign;
void* pThreadCtx;
void* pLastThreadCtx;
WELS_MUTEX* pCsDecoder;
int16_t lastReadyHeightOffset[LIST_A][MAX_REF_PIC_COUNT]; PPictInfo pPictInfoList;
PPictReoderingStatus pPictReoderingStatus;
} SWelsDecoderContext, *PWelsDecoderContext;
typedef struct tagSWelsDecThread {
SWelsDecSemphore* sIsBusy;
SWelsDecSemphore sIsActivated;
SWelsDecSemphore sIsIdle;
SWelsDecThread sThrHandle;
uint32_t uiCommand;
uint32_t uiThrNum;
uint32_t uiThrMaxNum;
uint32_t uiThrStackSize;
DECLARE_PROCTHREAD_PTR (pThrProcMain);
} SWelsDecThreadInfo, *PWelsDecThreadInfo;
typedef struct tagSWelsDecThreadCtx {
SWelsDecThreadInfo sThreadInfo;
PWelsDecoderContext pCtx;
void* threadCtxOwner;
uint8_t* kpSrc;
int32_t kiSrcLen;
uint8_t** ppDst;
SBufferInfo sDstInfo;
PPicture pDec;
SWelsDecEvent sImageReady;
SWelsDecEvent sSliceDecodeStart;
SWelsDecEvent sSliceDecodeFinish;
int32_t iPicBuffIdx; } SWelsDecoderThreadCTX, *PWelsDecoderThreadCTX;
static inline void ResetActiveSPSForEachLayer (PWelsDecoderContext pCtx) {
if (pCtx->iTotalNumMbRec == 0) {
for (int i = 0; i < MAX_LAYER_NUM; i++) {
pCtx->sSpsPpsCtx.pActiveLayerSps[i] = NULL;
}
}
}
static inline int32_t GetThreadCount (PWelsDecoderContext pCtx) {
int32_t iThreadCount = 0;
if (pCtx->pThreadCtx != NULL) {
PWelsDecoderThreadCTX pThreadCtx = (PWelsDecoderThreadCTX)pCtx->pThreadCtx;
iThreadCount = pThreadCtx->sThreadInfo.uiThrMaxNum;
}
return iThreadCount;
}
static inline int32_t GetPrevFrameNum (PWelsDecoderContext pCtx) {
if (pCtx->uiDecodingTimeStamp > 0) {
PWelsDecoderThreadCTX pThreadCtx = (PWelsDecoderThreadCTX)pCtx->pThreadCtx;
int32_t iThreadCount = int32_t (pThreadCtx->sThreadInfo.uiThrMaxNum);
int32_t uiThrNum = int32_t (pThreadCtx->sThreadInfo.uiThrNum);
for (int32_t i = 0; i < iThreadCount; ++i) {
int32_t id = i - uiThrNum;
if (id != 0 && pThreadCtx[id].pCtx->uiDecodingTimeStamp == pCtx->uiDecodingTimeStamp - 1) {
if (pThreadCtx[id].pCtx->pDec != NULL) {
int32_t iFrameNum = pThreadCtx[id].pCtx->pDec->iFrameNum;
if (iFrameNum >= 0) return iFrameNum;
}
return pThreadCtx[id].pCtx->iFrameNum;
}
}
}
return pCtx->pLastDecPicInfo->iPrevFrameNum;
}
}
#endif