1 /*! 2 * \copy 3 * Copyright (c) 2011-2013, Cisco Systems 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 * 31 * 32 * \file wels_preprocess.h 33 * 34 * \brief interface of video pre-process plugins 35 * 36 * \date 03/15/2011 37 * 38 * \description : this class is designed as an interface to unify video pre-processing 39 * class implement sets such as denoise,colorspace conversion etc... 40 * 41 ************************************************************************************* 42 */ 43 44 #ifndef WELS_PREPROCESS_H 45 #define WELS_PREPROCESS_H 46 47 #include "typedefs.h" 48 #include "picture.h" 49 #include "wels_const.h" 50 #include "IWelsVP.h" 51 #include "param_svc.h" 52 53 namespace WelsEnc { 54 55 typedef struct TagWelsEncCtx sWelsEncCtx; 56 57 typedef struct { 58 SPicture* pScaledInputPicture; 59 int32_t iScaledWidth[MAX_DEPENDENCY_LAYER]; 60 int32_t iScaledHeight[MAX_DEPENDENCY_LAYER]; 61 } Scaled_Picture; 62 63 64 typedef struct { 65 int64_t iMinFrameComplexity; 66 int64_t iMinFrameComplexity08; 67 int64_t iMinFrameComplexity11; 68 69 int32_t iMinFrameNumGap; 70 int32_t iMinFrameQp; 71 } SRefJudgement; 72 73 typedef struct { 74 SPicture* pRefPicture; 75 int32_t iSrcListIdx; //idx in h->spatial_pic[base_did]; 76 bool bSceneLtrFlag; 77 unsigned char* pBestBlockStaticIdc; 78 } SRefInfoParam; 79 80 typedef struct TagVAAFrameInfo { 81 SVAACalcResult sVaaCalcInfo; 82 SAdaptiveQuantizationParam sAdaptiveQuantParam; 83 SComplexityAnalysisParam sComplexityAnalysisParam; 84 85 int32_t iPicWidth; // maximal iWidth of picture in samples for svc coding 86 int32_t iPicHeight; // maximal iHeight of picture in samples for svc coding 87 int32_t iPicStride; //luma 88 int32_t iPicStrideUV; 89 90 uint8_t* pRefY; //pRef 91 uint8_t* pCurY; //cur 92 uint8_t* pRefU; //pRef 93 uint8_t* pCurU; //cur 94 uint8_t* pRefV; //pRef 95 uint8_t* pCurV; //cur 96 97 int8_t* pVaaBackgroundMbFlag; 98 uint8_t uiValidLongTermPicIdx; 99 uint8_t uiMarkLongTermPicIdx; 100 101 ESceneChangeIdc eSceneChangeIdc; 102 bool bSceneChangeFlag; 103 bool bIdrPeriodFlag; 104 } SVAAFrameInfo; 105 106 typedef struct SVAAFrameInfoExt_t: public SVAAFrameInfo { 107 SComplexityAnalysisScreenParam sComplexityScreenParam; 108 SScrollDetectionParam sScrollDetectInfo; 109 SRefInfoParam sVaaStrBestRefCandidate[MAX_REF_PIC_COUNT]; 110 SRefInfoParam sVaaLtrBestRefCandidate[MAX_REF_PIC_COUNT]; 111 int32_t iNumOfAvailableRef; 112 113 int32_t iVaaBestRefFrameNum; 114 uint8_t* pVaaBestBlockStaticIdc;//pointer 115 uint8_t* pVaaBlockStaticIdc[16];//real memory, 116 } SVAAFrameInfoExt; 117 118 class CWelsPreProcess { 119 public: 120 CWelsPreProcess (sWelsEncCtx* pEncCtx); 121 virtual ~CWelsPreProcess(); 122 123 static CWelsPreProcess* CreatePreProcess (sWelsEncCtx* pEncCtx); 124 125 virtual SPicture* GetCurrentOrigFrame (int32_t iDIdx) = 0; 126 public: 127 int32_t WelsPreprocessReset (sWelsEncCtx* pEncCtx, int32_t iWidth, int32_t iHeight); 128 int32_t AllocSpatialPictures (sWelsEncCtx* pCtx, SWelsSvcCodingParam* pParam); 129 void FreeSpatialPictures (sWelsEncCtx* pCtx); 130 int32_t BuildSpatialPicList (sWelsEncCtx* pEncCtx, const SSourcePicture* kpSrcPic); 131 int32_t AnalyzeSpatialPic (sWelsEncCtx* pEncCtx, const int32_t kiDIdx); 132 int32_t UpdateSpatialPictures (sWelsEncCtx* pEncCtx, SWelsSvcCodingParam* pParam, const int8_t iCurTid, 133 const int32_t d_idx); 134 int32_t GetRefFrameInfo (int32_t iRefIdx, bool bCurrentFrameIsSceneLtr, SPicture*& pRefOri); 135 void AnalyzePictureComplexity (sWelsEncCtx* pCtx, SPicture* pCurPicture, SPicture* pRefPicture, 136 const int32_t kiDependencyId, const bool kbCalculateBGD); 137 int32_t UpdateBlockIdcForScreen (uint8_t* pCurBlockStaticPointer, const SPicture* kpRefPic, const SPicture* kpSrcPic); 138 139 140 void UpdateSrcList (SPicture* pCurPicture, const int32_t kiCurDid, SPicture** pShortRefList, 141 const uint32_t kuiShortRefCount); 142 void UpdateSrcListLosslessScreenRefSelectionWithLtr (SPicture* pCurPicture, const int32_t kiCurDid, 143 const int32_t kuiMarkLongTermPicIdx, SPicture** pLongRefList); 144 145 146 protected: 147 bool GetSceneChangeFlag (ESceneChangeIdc eSceneChangeIdc); 148 virtual ESceneChangeIdc DetectSceneChange (SPicture* pCurPicture, SPicture* pRefPicture = NULL) = 0; 149 150 void InitPixMap (const SPicture* pPicture, SPixMap* pPixMap); 151 152 int32_t GetCurPicPosition (const int32_t kiDidx); 153 154 private: 155 int32_t WelsPreprocessCreate(); 156 int32_t WelsPreprocessDestroy(); 157 int32_t InitLastSpatialPictures (sWelsEncCtx* pEncCtx); 158 159 private: 160 int32_t SingleLayerPreprocess (sWelsEncCtx* pEncCtx, const SSourcePicture* kpSrc, Scaled_Picture* m_sScaledPicture); 161 162 void BilateralDenoising (SPicture* pSrc, const int32_t iWidth, const int32_t iHeight); 163 164 int32_t DownsamplePadding (SPicture* pSrc, SPicture* pDstPic, int32_t iSrcWidth, int32_t iSrcHeight, 165 int32_t iShrinkWidth, int32_t iShrinkHeight, int32_t iTargetWidth, int32_t iTargetHeight, 166 bool bForceCopy); 167 168 void VaaCalculation (SVAAFrameInfo* pVaaInfo, SPicture* pCurPicture, SPicture* pRefPicture, bool bCalculateSQDiff, 169 bool bCalculateVar, bool bCalculateBGD); 170 void BackgroundDetection (SVAAFrameInfo* pVaaInfo, SPicture* pCurPicture, SPicture* pRefPicture, bool bDetectFlag); 171 void AdaptiveQuantCalculation (SVAAFrameInfo* pVaaInfo, SPicture* pCurPicture, SPicture* pRefPicture); 172 void Padding (uint8_t* pSrcY, uint8_t* pSrcU, uint8_t* pSrcV, int32_t iStrideY, int32_t iStrideUV, 173 int32_t iActualWidth, int32_t iPaddingWidth, int32_t iActualHeight, int32_t iPaddingHeight); 174 void SetRefMbType (sWelsEncCtx* pCtx, uint32_t** pRefMbTypeArray, int32_t iRefPicType); 175 176 int32_t ColorspaceConvert (SWelsSvcCodingParam* pSvcParam, SPicture* pDstPic, const SSourcePicture* kpSrc, 177 const int32_t kiWidth, const int32_t kiHeight); 178 void WelsMoveMemoryWrapper (SWelsSvcCodingParam* pSvcParam, SPicture* pDstPic, const SSourcePicture* kpSrc, 179 const int32_t kiWidth, const int32_t kiHeight); 180 181 /*! 182 * \brief exchange two picture pData planes 183 * \param ppPic1 picture pointer to picture 1 184 * \param ppPic2 picture pointer to picture 2 185 * \return none 186 */ 187 void WelsExchangeSpatialPictures (SPicture** ppPic1, SPicture** ppPic2); 188 189 SPicture* GetBestRefPic (EUsageType iUsageType, bool bSceneLtr, EWelsSliceType eSliceType, int32_t kiDidx, 190 int32_t iRefTemporalIdx); 191 SPicture* GetBestRefPic (const int32_t kiDidx, const int32_t iRefTemporalIdx); 192 protected: 193 IWelsVP* m_pInterfaceVp; 194 sWelsEncCtx* m_pEncCtx; 195 uint8_t m_uiSpatialLayersInTemporal[MAX_DEPENDENCY_LAYER]; 196 197 private: 198 Scaled_Picture m_sScaledPicture; 199 SPicture* m_pLastSpatialPicture[MAX_DEPENDENCY_LAYER][2]; 200 bool m_bInitDone; 201 uint8_t m_uiSpatialPicNum[MAX_DEPENDENCY_LAYER]; 202 protected: 203 /* For Downsampling & VAA I420 based source pictures */ 204 SPicture* m_pSpatialPic[MAX_DEPENDENCY_LAYER][MAX_REF_PIC_COUNT + 1]; 205 // need memory requirement with total number of num_of_ref + 1, "+1" is for current frame 206 int32_t m_iAvaliableRefInSpatialPicList; 207 208 }; 209 210 class CWelsPreProcessVideo : public CWelsPreProcess { 211 public: CWelsPreProcessVideo(sWelsEncCtx * pEncCtx)212 CWelsPreProcessVideo (sWelsEncCtx* pEncCtx) : CWelsPreProcess (pEncCtx) {}; 213 214 virtual SPicture* GetCurrentOrigFrame (int32_t iDIdx); 215 216 virtual ESceneChangeIdc DetectSceneChange (SPicture* pCurPicture, SPicture* pRefPicture = NULL); 217 }; 218 219 220 221 class CWelsPreProcessScreen : public CWelsPreProcess { 222 public: CWelsPreProcessScreen(sWelsEncCtx * pEncCtx)223 CWelsPreProcessScreen (sWelsEncCtx* pEncCtx) : CWelsPreProcess (pEncCtx) {}; 224 225 virtual SPicture* GetCurrentOrigFrame (int32_t iDIdx); 226 227 virtual ESceneChangeIdc DetectSceneChange (SPicture* pCurPicture, SPicture* pRefPicture = NULL); 228 229 private: 230 SPicture** GetReferenceSrcPicList(int32_t iTargetDid); 231 232 void GetAvailableRefListLosslessScreenRefSelection (SPicture** pSrcPicList, uint8_t iCurTid, 233 const int32_t iClosestLtrFrameNum, 234 SRefInfoParam* pAvailableRefList, int32_t& iAvailableRefNum, int32_t& iAvailableSceneRefNum); 235 236 void GetAvailableRefList (SPicture** pSrcPicList, uint8_t iCurTid, const int32_t iClosestLtrFrameNum, 237 SRefInfoParam* pAvailableRefList, int32_t& iAvailableRefNum, int32_t& iAvailableSceneRefNum); 238 void InitRefJudgement (SRefJudgement* pRefJudgement); 239 240 bool JudgeBestRef (SPicture* pRefPic, const SRefJudgement& sRefJudgement, const int64_t iFrameComplexity, 241 const bool bIsClosestLtrFrame); 242 void SaveBestRefToJudgement (const int32_t iRefPictureAvQP, const int64_t iComplexity, SRefJudgement* pRefJudgement); 243 void SaveBestRefToLocal (SRefInfoParam* pRefPicInfo, const SSceneChangeResult& sSceneChangeResult, 244 SRefInfoParam* pRefSaved); 245 void SaveBestRefToVaa (SRefInfoParam& sRefSaved, SRefInfoParam* pVaaBestRef); 246 }; 247 248 249 } 250 251 #endif 252