• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*!
2  * \copy
3  *     Copyright (c)  2009-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    encoder_context.h
33  *
34  * \brief   Main pData to be operated over Wels encoder all modules
35  *
36  * \date    2/4/2009 Created
37  *
38  *************************************************************************************
39  */
40 #ifndef WELS_ENCODER_CONTEXT_H__
41 #define WELS_ENCODER_CONTEXT_H__
42 
43 #include <stdio.h>
44 #include "typedefs.h"
45 #include "param_svc.h"
46 #include "nal_encap.h"
47 #include "picture.h"
48 #include "paraset_strategy.h"
49 #include "dq_map.h"
50 #include "stat.h"
51 #include "macros.h"
52 #include "rc.h"
53 #include "as264_common.h"
54 #include "wels_preprocess.h"
55 #include "wels_func_ptr_def.h"
56 #include "crt_util_safe_x.h"
57 #include "utils.h"
58 
59 #include "mt_defs.h" // for multiple threadin,
60 #include "WelsThreadLib.h"
61 #include "wels_task_management.h"
62 
63 namespace WelsEnc {
64 
65 class IWelsTaskManage;
66 class IWelsReferenceStrategy;
67 
68 /*
69  *  reference list for each quality layer in SVC
70  */
71 typedef struct TagRefList {
72   SPicture*     pShortRefList[1 + MAX_SHORT_REF_COUNT]; // reference list 0 - int16_t
73   SPicture*     pLongRefList[1 + MAX_REF_PIC_COUNT];    // reference list 1 - int32_t
74   SPicture*     pNextBuffer;
75   SPicture*     pRef[1 + MAX_REF_PIC_COUNT];    // plus 1 for swap intend
76   uint8_t       uiShortRefCount;
77   uint8_t       uiLongRefCount; // dependend on pRef pic module
78 } SRefList;
79 
80 typedef struct TagLTRState {
81 // LTR mark feedback
82   uint32_t      uiLtrMarkState; // LTR mark state, indicate whether there is a LTR mark feedback unsolved
83   int32_t       iLtrMarkFbFrameNum;// the unsolved LTR mark feedback, the marked iFrameNum feedback from decoder
84 
85 // LTR used as recovery reference
86   int32_t       iLastRecoverFrameNum; // reserve the last LTR or IDR recover iFrameNum
87   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
88   int32_t       iCurFrameNumInDec; // current iFrameNum in decoder side, use to select valid LTR to recover or to decide the LTR mark validation
89 
90 // LTR mark
91   int32_t       iLTRMarkMode; // direct mark or delay mark
92   int32_t       iLTRMarkSuccessNum; //successful marked num, for mark mode switch
93   int32_t       iCurLtrIdx;// current int32_t term reference index to mark
94   int32_t       iLastLtrIdx[MAX_TEMPORAL_LAYER_NUM];
95   int32_t       iSceneLtrIdx;// related to Scene LTR, used by screen content
96 
97   uint32_t      uiLtrMarkInterval;// the interval from the last int32_t term pRef mark
98 
99   bool          bLTRMarkingFlag;        //decide whether current frame marked as LTR
100   bool          bLTRMarkEnable; //when LTR is confirmed and the interval is no smaller than the marking period
101   bool          bReceivedT0LostFlag;    // indicate whether a t0 lost feedback is recieved, for LTR recovery
102 } SLTRState;
103 
104 typedef struct TagSpatialPicIndex {
105   SPicture*     pSrc;   // I420 based and after color space converted
106   int32_t       iDid;   // dependency id
107 } SSpatialPicIndex;
108 
109 typedef struct TagStrideTables {
110   int32_t*      pStrideDecBlockOffset[MAX_DEPENDENCY_LAYER][2]; // [iDid][tid==0][24 x 4]: luma+chroma= 24 x 4
111   int32_t*      pStrideEncBlockOffset[MAX_DEPENDENCY_LAYER];    // [iDid][24 x 4]: luma+chroma= 24 x 4
112   int16_t*      pMbIndexX[MAX_DEPENDENCY_LAYER];                // [iDid][iMbX]: map for iMbX in each spatial layer coding
113   int16_t*      pMbIndexY[MAX_DEPENDENCY_LAYER];                // [iDid][iMbY]: map for iMbY in each spatial layer coding
114 } SStrideTables;
115 
116 typedef struct TagWelsEncCtx {
117   SLogContext sLogCtx;
118 // Input
119   SWelsSvcCodingParam* pSvcParam;   // SVC parameter, WelsSVCParamConfig in svc_param_settings.h
120 
121   int32_t*          pSadCostMb;
122   /* MVD cost tables for Inter MB */
123   int32_t           iMvRange;
124   uint16_t*         pMvdCostTable; //[52];  // adaptive to spatial layers
125   int32_t           iMvdCostTableSize; //the size of above table
126   int32_t           iMvdCostTableStride; //the stride of above table
127   SMVUnitXY*
128   pMvUnitBlock4x4;      // (*pMvUnitBlock4x4[2])[MB_BLOCK4x4_NUM];          // for store each 4x4 blocks' mv unit, the two swap after different d layer
129   int8_t*
130   pRefIndexBlock4x4;    // (*pRefIndexBlock4x4[2])[MB_BLOCK8x8_NUM];        // for store each 4x4 blocks' pRef index, the two swap after different d layer
131   int8_t*           pNonZeroCountBlocks;     // (*pNonZeroCountBlocks)[MB_LUMA_CHROMA_BLOCK4x4_NUM];
132   int8_t*
133   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)
134 
135   SMB**             ppMbListD;     // [MAX_DEPENDENCY_LAYER];
136   SStrideTables*    pStrideTab;     // stride tables for internal coding used
137   SWelsFuncPtrList* pFuncList;
138 
139   SSliceThreading*  pSliceThreading;
140   IWelsTaskManage*  pTaskManage; //was planning to put it under CWelsH264SVCEncoder but it may be updated (lock/no lock) when param is changed
141   IWelsReferenceStrategy* pReferenceStrategy;
142 
143   // pointers
144   SPicture*         pEncPic;                // pointer to current picture to be encoded
145   SPicture*         pDecPic;                // pointer to current picture being reconstructed
146   SPicture*         pRefPic;                // pointer to current reference picture
147 
148   SDqLayer*         pCurDqLayer;            // DQ layer context used to being encoded currently, for reference base layer to refer: pCurDqLayer->pRefLayer if applicable
149   SDqLayer**        ppDqLayerList;          // overall DQ layers encoded for storage
150 
151   SRefList**        ppRefPicListExt;        // reference picture list for SVC
152   SPicture*         pRefList0[16];
153   SLTRState*        pLtr;//[MAX_DEPENDENCY_LAYER];
154   bool              bCurFrameMarkedAsSceneLtr;
155 // Derived
156 
157   EWelsSliceType    eSliceType;             // currently coding slice type
158   EWelsNalUnitType  eNalType;               // NAL type
159   EWelsNalRefIdc    eNalPriority;           // NAL_Reference_Idc currently
160   EWelsNalRefIdc    eLastNalPriority[MAX_DEPENDENCY_LAYER];       // NAL_Reference_Idc in last frame
161   uint8_t           iNumRef0;
162 
163   uint8_t           uiDependencyId;         // Idc of dependecy layer to be coded
164   uint8_t           uiTemporalId;           // Idc of temporal layer to be coded
165   bool              bNeedPrefixNalFlag;     // whether add prefix nal
166 
167 // Rate control routine
168   SWelsSvcRc*       pWelsSvcRc;
169   bool              bCheckWindowStatusRefreshFlag;
170   int64_t           iCheckWindowStartTs;
171   int64_t           iCheckWindowCurrentTs;
172   int32_t           iCheckWindowInterval;
173   int32_t           iCheckWindowIntervalShift;
174   bool              bCheckWindowShiftResetFlag;
175   int32_t           iGlobalQp;      // global qp
176 
177 // VAA
178   SVAAFrameInfo*    pVaa;           // VAA information of reference
179   CWelsPreProcess*  pVpp;
180 
181   SWelsSPS*         pSpsArray;      // MAX_SPS_COUNT by standard compatible
182   SWelsSPS*         pSps;
183   SWelsPPS*         pPPSArray;      // MAX_PPS_COUNT by standard compatible
184   SWelsPPS*         pPps;
185   /* SVC only */
186   SSubsetSps*       pSubsetArray;   // MAX_SPS_COUNT by standard compatible
187   SSubsetSps*       pSubsetSps;
188   int32_t           iSpsNum;        // number of pSps used
189   int32_t           iSubsetSpsNum;  // number of pSps used
190   int32_t           iPpsNum;        // number of pPps used
191 
192 // Output
193   SWelsEncoderOutput* pOut;         // for NAL raw pData (need allocating memory for sNalList internal)
194   uint8_t*          pFrameBs;       // restoring bitstream pBuffer of all NALs in a frame
195   int32_t           iFrameBsSize;   // count size of frame bs in bytes allocated
196   int32_t           iPosBsBuffer;   // current writing position of frame bs pBuffer
197 
198   SSpatialPicIndex  sSpatialIndexMap[MAX_DEPENDENCY_LAYER];
199   int32_t           iSliceBufferSize[MAX_DEPENDENCY_LAYER];
200 
201   bool              bRefOfCurTidIsLtr[MAX_DEPENDENCY_LAYER][MAX_TEMPORAL_LEVEL];
202  // uint16_t          uiIdrPicId;           // IDR picture id: [0, 65535], this one is used for LTR
203   int32_t           iMaxSliceCount;// maximal count number of slices for all layers observation
204   int16_t           iActiveThreadsNum;      // number of threads active so far
205 
206   /*
207    * DQ layer idc map for svc encoding, might be a better scheme than that of design before,
208    * can aware idc of referencing layer and that idc of successive layer to be coded
209    */
210   /* SVC only */
211   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
212 
213   SParaSetOffset    sPSOVector;
214   SParaSetOffset*   pPSOVector;
215   CMemoryAlign*     pMemAlign;
216 
217 #if defined(STAT_OUTPUT)
218 // overall stat pData, refer to SStatData in stat.h, in case avc to use stat[0][0]
219   SStatData         sStatData [ MAX_DEPENDENCY_LAYER ] [ MAX_QUALITY_LEVEL ];
220   SStatSliceInfo    sPerInfo;
221 #endif//STAT_OUTPUT
222 
223   //related to Statistics
224   int64_t            uiStartTimestamp;
225   SEncoderStatistics sEncoderStatistics[MAX_DEPENDENCY_LAYER];
226   int32_t            iStatisticsLogInterval;
227   int64_t            iLastStatisticsLogTs;
228 
229   int32_t iEncoderError;
230   WELS_MUTEX mutexEncoderError;
231   bool bDeliveryFlag;
232   SStateCtx sWelsCabacContexts[4][WELS_QP_MAX + 1][WELS_CONTEXT_COUNT];
233 #ifdef ENABLE_FRAME_DUMP
234   bool bDependencyRecFlag[MAX_DEPENDENCY_LAYER];
235 #endif
236   int64_t            uiLastTimestamp;
237   uint8_t*           pDynamicBsBuffer[MAX_THREADS_NUM];
238 } sWelsEncCtx/*, *PWelsEncCtx*/;
239 }
240 #endif//sWelsEncCtx_H__
241