• 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    param_svc.h
33  *
34  * \brief   Configurable parameters in H.264/SVC Encoder
35  *
36  * \date    4/20/2009 Created
37  *
38  *************************************************************************************
39  */
40 #if !defined(WELS_ENCODER_PARAMETER_SVC_H__)
41 #define WELS_ENCODER_PARAMETER_SVC_H__
42 
43 #include <string.h>
44 #include <math.h>
45 #include "typedefs.h"
46 #include "codec_def.h"
47 #include "macros.h"
48 #include "wels_const.h"
49 #include "rc.h"
50 #include "svc_enc_slice_segment.h"
51 #include "as264_common.h"
52 
53 namespace WelsEnc {
54 
55 #define   INVALID_TEMPORAL_ID   ((uint8_t)0xff)
56 
57 extern const uint8_t   g_kuiTemporalIdListTable[MAX_TEMPORAL_LEVEL][MAX_GOP_SIZE + 1];
58 
59 /*!
60 * \brief    get Logarithms base 2 of (upper/base)
61 * \param    base    based scaler
62 * \param    upper   input upper value
63 * \return   2 based scaling factor
64 */
GetLogFactor(float base,float upper)65 static inline uint32_t GetLogFactor (float base, float upper) {
66 #if defined(_M_X64) && _MSC_VER == 1800
67   _set_FMA3_enable(0);
68 #endif
69   const double dLog2factor      = log10 (1.0 * upper / base) / log10 (2.0);
70   const double dEpsilon         = 0.0001;
71   const double dRound           = floor (dLog2factor + 0.5);
72 
73   if (dLog2factor < dRound + dEpsilon && dRound < dLog2factor + dEpsilon) {
74     return (uint32_t) (dRound);
75   }
76   return UINT_MAX;
77 }
78 
79 /*
80  *  Dependency Layer Parameter
81  */
82 typedef struct TagDLayerParam {
83   int32_t       iActualWidth;                   // input source picture actual width
84   int32_t       iActualHeight;                  // input source picture actual height
85   int32_t       iTemporalResolution;
86   int32_t       iDecompositionStages;
87   uint8_t       uiCodingIdx2TemporalId[ (1 << MAX_TEMPORAL_LEVEL) + 1];
88 
89   int8_t        iHighestTemporalId;
90   float         fInputFrameRate;                // input frame rate
91   float         fOutputFrameRate;               // output frame rate
92   uint16_t          uiIdrPicId;           // IDR picture id: [0, 65535], this one is used for LTR
93   int32_t           iCodingIndex;
94   int32_t           iFrameIndex;            // count how many frames elapsed during coding context currently
95   bool              bEncCurFrmAsIdrFlag;
96   int32_t           iFrameNum;              // current frame number coding
97   int32_t           iPOC;                   // frame iPOC
98 #ifdef ENABLE_FRAME_DUMP
99   char          sRecFileName[MAX_FNAME_LEN];    // file to be constructed
100 #endif//ENABLE_FRAME_DUMP
101 } SSpatialLayerInternal;
102 
103 /*
104  *  Cisco OpenH264 Encoder Parameter Configuration
105  */
106 typedef struct TagWelsSvcCodingParam: SEncParamExt {
107   SSpatialLayerInternal sDependencyLayers[MAX_DEPENDENCY_LAYER];
108 
109   /* General */
110   uint32_t uiGopSize;                      // GOP size (at maximal frame rate: 16)
111   struct {
112     int32_t iLeft;
113     int32_t iTop;
114     int32_t iWidth;
115     int32_t iHeight;
116   } SUsedPicRect;       // the rect in input picture that encoder actually used
117 
118   char*       pCurPath; // record current lib path such as:/pData/pData/com.wels.enc/lib/
119 
120   bool      bDeblockingParallelFlag;        // deblocking filter parallelization control flag
121   int32_t   iBitsVaryPercentage;
122 
123   int8_t   iDecompStages;          // GOP size dependency
124   int32_t  iMaxNumRefFrame;
125 
126  public:
TagWelsSvcCodingParamTagWelsSvcCodingParam127   TagWelsSvcCodingParam() {
128     FillDefault();
129   }
~TagWelsSvcCodingParamTagWelsSvcCodingParam130   ~TagWelsSvcCodingParam() {}
131 
FillDefaultTagWelsSvcCodingParam132   static void FillDefault (SEncParamExt& param) {
133     memset (&param, 0, sizeof (param));
134     param.uiIntraPeriod         = 0;                    // intra period (multiple of GOP size as desired)
135     param.iNumRefFrame          = AUTO_REF_PIC_COUNT;// number of reference frame used
136 
137     param.iPicWidth             = 0;    //   actual input picture width
138     param.iPicHeight            = 0;    //   actual input picture height
139 
140     param.fMaxFrameRate         = MAX_FRAME_RATE;       // maximal frame rate [Hz / fps]
141 
142     param.iComplexityMode       = LOW_COMPLEXITY;
143     param.iTargetBitrate        = UNSPECIFIED_BIT_RATE; // overall target bitrate introduced in RC module
144     param.iMaxBitrate           = UNSPECIFIED_BIT_RATE;
145     param.iMultipleThreadIdc    = 1;
146     param.bUseLoadBalancing = true;
147 
148     param.iLTRRefNum            = 0;
149     param.iLtrMarkPeriod        = 30;   //the min distance of two int32_t references
150 
151     param.bEnableSSEI           = false;
152     param.bSimulcastAVC         = false;
153     param.bEnableFrameCroppingFlag = true; // enable frame cropping flag: true alwayse in application
154     // false: Streaming Video Sharing; true: Video Conferencing Meeting;
155 
156     /* Deblocking loop filter */
157     param.iLoopFilterDisableIdc         = 0;    // 0: on, 1: off, 2: on except for slice boundaries
158     param.iLoopFilterAlphaC0Offset      = 0;    // AlphaOffset: valid range [-6, 6], default 0
159     param.iLoopFilterBetaOffset         = 0;    // BetaOffset:  valid range [-6, 6], default 0
160 
161     /* Rate Control */
162     param.iRCMode                       = RC_QUALITY_MODE;
163     param.iPaddingFlag                  = 0;
164     param.iEntropyCodingModeFlag        = 0;
165     param.bEnableDenoise                = false;        // denoise control
166     param.bEnableSceneChangeDetect      = true;         // scene change detection control
167     param.bEnableBackgroundDetection    = true;         // background detection control
168     param.bEnableAdaptiveQuant          = true;         // adaptive quantization control
169     param.bEnableFrameSkip              = true;         // frame skipping
170     param.bEnableLongTermReference      = false;        // long term reference control
171     param.eSpsPpsIdStrategy             = INCREASING_ID;// pSps pPps id addition control
172     param.bPrefixNalAddingCtrl          = false;        // prefix NAL adding control
173     param.iSpatialLayerNum              = 1;            // number of dependency(Spatial/CGS) layers used to be encoded
174     param.iTemporalLayerNum             = 1;            // number of temporal layer specified
175 
176     param.iMaxQp = QP_MAX_VALUE;
177     param.iMinQp = QP_MIN_VALUE;
178     param.iUsageType = CAMERA_VIDEO_REAL_TIME;
179     param.uiMaxNalSize = 0;
180     param.bIsLosslessLink = false;
181     for (int32_t iLayer = 0; iLayer < MAX_SPATIAL_LAYER_NUM; iLayer++) {
182       param.sSpatialLayers[iLayer].uiProfileIdc = PRO_UNKNOWN;
183       param.sSpatialLayers[iLayer].uiLevelIdc = LEVEL_UNKNOWN;
184       param.sSpatialLayers[iLayer].iDLayerQp = SVC_QUALITY_BASE_QP;
185       param.sSpatialLayers[iLayer].fFrameRate = param.fMaxFrameRate;
186 
187       param.sSpatialLayers[iLayer].iMaxSpatialBitrate = UNSPECIFIED_BIT_RATE;
188 
189       param.sSpatialLayers[iLayer].sSliceArgument.uiSliceMode = SM_SINGLE_SLICE;
190       param.sSpatialLayers[iLayer].sSliceArgument.uiSliceNum = 0; //AUTO, using number of CPU cores
191       param.sSpatialLayers[iLayer].sSliceArgument.uiSliceSizeConstraint = 1500;
192 
193       param.sSpatialLayers[iLayer].bAspectRatioPresent = false; // do not write any of the following information to the header
194       param.sSpatialLayers[iLayer].eAspectRatio = ASP_UNSPECIFIED;
195       param.sSpatialLayers[iLayer].sAspectRatioExtWidth = 0;
196       param.sSpatialLayers[iLayer].sAspectRatioExtHeight = 0;
197 
198       const int32_t kiLesserSliceNum = ((MAX_SLICES_NUM < MAX_SLICES_NUM_TMP) ? MAX_SLICES_NUM : MAX_SLICES_NUM_TMP);
199       for (int32_t idx = 0; idx < kiLesserSliceNum; idx++)
200         param.sSpatialLayers[iLayer].sSliceArgument.uiSliceMbNum[idx] = 0; //default, using one row a slice if uiSliceMode is SM_RASTER_MODE
201 
202       // See codec_app_def.h for more info about members bVideoSignalTypePresent through uiColorMatrix.  The default values
203       // used below preserve the previous behavior; i.e., no additional information will be written to the output file.
204       param.sSpatialLayers[iLayer].bVideoSignalTypePresent = false;			// do not write any of the following information to the header
205       param.sSpatialLayers[iLayer].uiVideoFormat = VF_UNDEF;				// undefined
206       param.sSpatialLayers[iLayer].bFullRange = false;						// analog video data range [16, 235]
207       param.sSpatialLayers[iLayer].bColorDescriptionPresent = false;		// do not write any of the following three items to the header
208       param.sSpatialLayers[iLayer].uiColorPrimaries = CP_UNDEF;				// undefined
209       param.sSpatialLayers[iLayer].uiTransferCharacteristics = TRC_UNDEF;	// undefined
210       param.sSpatialLayers[iLayer].uiColorMatrix = CM_UNDEF;				// undefined
211     }
212   }
213 
FillDefaultTagWelsSvcCodingParam214   void FillDefault() {
215     FillDefault (*this);
216     uiGopSize = 1;                      // GOP size (at maximal frame rate: 16)
217     iMaxNumRefFrame = AUTO_REF_PIC_COUNT;
218     SUsedPicRect.iLeft =
219       SUsedPicRect.iTop =
220         SUsedPicRect.iWidth =
221           SUsedPicRect.iHeight = 0;     // the rect in input picture that encoder actually used
222 
223     pCurPath                    = NULL; // record current lib path such as:/pData/pData/com.wels.enc/lib/
224 
225     bDeblockingParallelFlag     = false;// deblocking filter parallelization control flag
226 
227     iDecompStages               = 0;    // GOP size dependency, unknown here and be revised later
228     iBitsVaryPercentage = 10;
229   }
230 
ParamBaseTranscodeTagWelsSvcCodingParam231   int32_t ParamBaseTranscode (const SEncParamBase& pCodingParam) {
232 
233     fMaxFrameRate  = WELS_CLIP3 (pCodingParam.fMaxFrameRate, MIN_FRAME_RATE, MAX_FRAME_RATE);
234     iTargetBitrate = pCodingParam.iTargetBitrate;
235     iUsageType  = pCodingParam.iUsageType;
236     iPicWidth   = pCodingParam.iPicWidth;
237     iPicHeight  = pCodingParam.iPicHeight;
238 
239     SUsedPicRect.iLeft = 0;
240     SUsedPicRect.iTop  = 0;
241     SUsedPicRect.iWidth = ((iPicWidth >> 1) * (1 << 1));
242     SUsedPicRect.iHeight = ((iPicHeight >> 1) * (1 << 1));
243 
244     iRCMode = pCodingParam.iRCMode;    // rc mode
245 
246     int8_t iIdxSpatial = 0;
247     EProfileIdc uiProfileIdc = PRO_UNKNOWN;
248     if (iEntropyCodingModeFlag)
249       uiProfileIdc = PRO_MAIN;
250     SSpatialLayerInternal* pDlp = &sDependencyLayers[0];
251 
252     while (iIdxSpatial < iSpatialLayerNum) {
253 
254       sSpatialLayers->uiProfileIdc              = uiProfileIdc;
255       sSpatialLayers->uiLevelIdc                = LEVEL_UNKNOWN;
256       sSpatialLayers[iIdxSpatial].fFrameRate    = WELS_CLIP3 (pCodingParam.fMaxFrameRate,
257           MIN_FRAME_RATE, MAX_FRAME_RATE);
258       pDlp->fInputFrameRate =
259         pDlp->fOutputFrameRate = WELS_CLIP3 (sSpatialLayers[iIdxSpatial].fFrameRate, MIN_FRAME_RATE,
260                                              MAX_FRAME_RATE);
261 #ifdef ENABLE_FRAME_DUMP
262       pDlp->sRecFileName[0] = '\0'; // file to be constructed
263 #endif//ENABLE_FRAME_DUMP
264       pDlp->iActualWidth = sSpatialLayers[iIdxSpatial].iVideoWidth = iPicWidth;
265       pDlp->iActualHeight = sSpatialLayers[iIdxSpatial].iVideoHeight = iPicHeight;
266 
267       sSpatialLayers->iSpatialBitrate =
268         sSpatialLayers[iIdxSpatial].iSpatialBitrate = pCodingParam.iTargetBitrate; // target bitrate for current spatial layer
269 
270       sSpatialLayers->iMaxSpatialBitrate = UNSPECIFIED_BIT_RATE;
271       sSpatialLayers->iDLayerQp = SVC_QUALITY_BASE_QP;
272 
273       uiProfileIdc = (!bSimulcastAVC) ? PRO_SCALABLE_BASELINE : uiProfileIdc;
274       ++ pDlp;
275       ++ iIdxSpatial;
276     }
277     SetActualPicResolution();
278 
279     return 0;
280   }
GetBaseParamsTagWelsSvcCodingParam281   void GetBaseParams (SEncParamBase* pCodingParam) {
282     pCodingParam->iUsageType     = iUsageType;
283     pCodingParam->iPicWidth      = iPicWidth;
284     pCodingParam->iPicHeight     = iPicHeight;
285     pCodingParam->iTargetBitrate = iTargetBitrate;
286     pCodingParam->iRCMode        = iRCMode;
287     pCodingParam->fMaxFrameRate  = fMaxFrameRate;
288   }
ParamTranscodeTagWelsSvcCodingParam289   int32_t ParamTranscode (const SEncParamExt& pCodingParam) {
290     float fParamMaxFrameRate = WELS_CLIP3 (pCodingParam.fMaxFrameRate, MIN_FRAME_RATE, MAX_FRAME_RATE);
291 
292     iUsageType = pCodingParam.iUsageType;
293     iPicWidth   = pCodingParam.iPicWidth;
294     iPicHeight  = pCodingParam.iPicHeight;
295     fMaxFrameRate = fParamMaxFrameRate;
296     iComplexityMode = pCodingParam.iComplexityMode;
297 
298     SUsedPicRect.iLeft = 0;
299     SUsedPicRect.iTop  = 0;
300     SUsedPicRect.iWidth = ((iPicWidth >> 1) << 1);
301     SUsedPicRect.iHeight = ((iPicHeight >> 1) << 1);
302 
303     iMultipleThreadIdc = pCodingParam.iMultipleThreadIdc;
304     bUseLoadBalancing = pCodingParam.bUseLoadBalancing;
305 
306     /* Deblocking loop filter */
307     iLoopFilterDisableIdc       = pCodingParam.iLoopFilterDisableIdc;      // 0: on, 1: off, 2: on except for slice boundaries,
308     iLoopFilterAlphaC0Offset    = pCodingParam.iLoopFilterAlphaC0Offset;   // AlphaOffset: valid range [-6, 6], default 0
309     iLoopFilterBetaOffset       = pCodingParam.iLoopFilterBetaOffset;      // BetaOffset:  valid range [-6, 6], default 0
310     iEntropyCodingModeFlag      = pCodingParam.iEntropyCodingModeFlag;
311     bEnableFrameCroppingFlag    = pCodingParam.bEnableFrameCroppingFlag;
312 
313     /* Rate Control */
314     iRCMode = pCodingParam.iRCMode;    // rc mode
315     bSimulcastAVC = pCodingParam.bSimulcastAVC;
316     iPaddingFlag = pCodingParam.iPaddingFlag;
317 
318     iTargetBitrate      = pCodingParam.iTargetBitrate;  // target bitrate
319     iMaxBitrate         = pCodingParam.iMaxBitrate;
320     if ((iMaxBitrate != UNSPECIFIED_BIT_RATE) && (iMaxBitrate < iTargetBitrate)) {
321       iMaxBitrate  = iTargetBitrate;
322     }
323     iMaxQp = pCodingParam.iMaxQp;
324     iMinQp = pCodingParam.iMinQp;
325     uiMaxNalSize          = pCodingParam.uiMaxNalSize;
326     /* Denoise Control */
327     bEnableDenoise = pCodingParam.bEnableDenoise ? true : false;    // Denoise Control  // only support 0 or 1 now
328 
329     /* Scene change detection control */
330     bEnableSceneChangeDetect   = pCodingParam.bEnableSceneChangeDetect;
331 
332     /* Background detection Control */
333     bEnableBackgroundDetection = pCodingParam.bEnableBackgroundDetection ? true : false;
334 
335     /* Adaptive quantization control */
336     bEnableAdaptiveQuant       = pCodingParam.bEnableAdaptiveQuant ? true : false;
337 
338     /* Frame skipping */
339     bEnableFrameSkip           = pCodingParam.bEnableFrameSkip ? true : false;
340 
341     /* Enable int32_t term reference */
342     bEnableLongTermReference   = pCodingParam.bEnableLongTermReference ? true : false;
343     iLtrMarkPeriod = pCodingParam.iLtrMarkPeriod;
344     bIsLosslessLink = pCodingParam.bIsLosslessLink;
345     if (iUsageType == SCREEN_CONTENT_REAL_TIME && !bIsLosslessLink && bEnableLongTermReference) {
346       bEnableLongTermReference = false;
347     }
348 
349     /* For ssei information */
350     bEnableSSEI         = pCodingParam.bEnableSSEI;
351     bSimulcastAVC       = pCodingParam.bSimulcastAVC;
352 
353     /* Layer definition */
354     iSpatialLayerNum = (int8_t)WELS_CLIP3 (pCodingParam.iSpatialLayerNum, 1,
355                                             MAX_DEPENDENCY_LAYER); // number of dependency(Spatial/CGS) layers used to be encoded
356     iTemporalLayerNum = (int8_t)WELS_CLIP3 (pCodingParam.iTemporalLayerNum, 1,
357                           MAX_TEMPORAL_LEVEL); // number of temporal layer specified
358 
359     uiGopSize           = 1 << (iTemporalLayerNum - 1); // Override GOP size based temporal layer
360     iDecompStages       = iTemporalLayerNum - 1;        // WELS_LOG2( uiGopSize );// GOP size dependency
361     uiIntraPeriod       = pCodingParam.uiIntraPeriod;// intra period (multiple of GOP size as desired)
362     if (uiIntraPeriod == (uint32_t) (-1))
363       uiIntraPeriod = 0;
364     else if (uiIntraPeriod & (uiGopSize - 1)) // none multiple of GOP size
365       uiIntraPeriod = ((uiIntraPeriod + uiGopSize - 1) / uiGopSize) * uiGopSize;
366 
367     if (((pCodingParam.iNumRefFrame != AUTO_REF_PIC_COUNT)
368          && !((pCodingParam.iNumRefFrame > MAX_REF_PIC_COUNT) || (pCodingParam.iNumRefFrame < MIN_REF_PIC_COUNT)))
369         || ((iNumRefFrame != AUTO_REF_PIC_COUNT) && (pCodingParam.iNumRefFrame == AUTO_REF_PIC_COUNT))) {
370       iNumRefFrame  = pCodingParam.iNumRefFrame;
371     }
372     if ((iNumRefFrame != AUTO_REF_PIC_COUNT) && (iNumRefFrame > iMaxNumRefFrame)) {
373       iMaxNumRefFrame = iNumRefFrame;
374     }
375     iLTRRefNum  = (pCodingParam.bEnableLongTermReference ? pCodingParam.iLTRRefNum : 0);
376     iLtrMarkPeriod  = pCodingParam.iLtrMarkPeriod;
377 
378     bPrefixNalAddingCtrl = pCodingParam.bPrefixNalAddingCtrl;
379 
380     if ( (CONSTANT_ID == pCodingParam.eSpsPpsIdStrategy)
381         || (INCREASING_ID == pCodingParam.eSpsPpsIdStrategy)
382         || (SPS_LISTING == pCodingParam.eSpsPpsIdStrategy)
383         || (SPS_LISTING_AND_PPS_INCREASING == pCodingParam.eSpsPpsIdStrategy)
384         || (SPS_PPS_LISTING == pCodingParam.eSpsPpsIdStrategy)) {
385     eSpsPpsIdStrategy =
386       pCodingParam.eSpsPpsIdStrategy;//For SVC meeting application, to avoid mosaic issue caused by cross-IDR reference.
387     //SHOULD enable this feature.
388     } else {
389       // keep the default value
390     }
391 
392     SSpatialLayerInternal* pDlp        = &sDependencyLayers[0];
393     SSpatialLayerConfig* pSpatialLayer = &sSpatialLayers[0];
394     EProfileIdc uiProfileIdc           = iEntropyCodingModeFlag ? PRO_HIGH : PRO_BASELINE;
395     int8_t iIdxSpatial  = 0;
396     while (iIdxSpatial < iSpatialLayerNum) {
397       pSpatialLayer->uiProfileIdc      = (pCodingParam.sSpatialLayers[iIdxSpatial].uiProfileIdc == PRO_UNKNOWN) ? uiProfileIdc :
398                                       pCodingParam.sSpatialLayers[iIdxSpatial].uiProfileIdc;
399       pSpatialLayer->uiLevelIdc        = pCodingParam.sSpatialLayers[iIdxSpatial].uiLevelIdc;
400 
401       float fLayerFrameRate     = WELS_CLIP3 (pCodingParam.sSpatialLayers[iIdxSpatial].fFrameRate,
402                                           MIN_FRAME_RATE, fParamMaxFrameRate);
403       pDlp->fInputFrameRate     = fParamMaxFrameRate;
404       pSpatialLayer->fFrameRate =
405         pDlp->fOutputFrameRate  = WELS_CLIP3 (fLayerFrameRate, MIN_FRAME_RATE, fParamMaxFrameRate);
406 
407 #ifdef ENABLE_FRAME_DUMP
408       pDlp->sRecFileName[0]     = '\0'; // file to be constructed
409 #endif//ENABLE_FRAME_DUMP
410       pSpatialLayer->iVideoWidth = WELS_CLIP3 (pCodingParam.sSpatialLayers[iIdxSpatial].iVideoWidth, 0,
411                                    iPicWidth);  // frame width
412       pSpatialLayer->iVideoHeight = WELS_CLIP3 (pCodingParam.sSpatialLayers[iIdxSpatial].iVideoHeight, 0,
413                                     iPicHeight);// frame height
414 
415       pSpatialLayer->iSpatialBitrate    =
416         pCodingParam.sSpatialLayers[iIdxSpatial].iSpatialBitrate;       // target bitrate for current spatial layer
417       pSpatialLayer->iMaxSpatialBitrate =
418         pCodingParam.sSpatialLayers[iIdxSpatial].iMaxSpatialBitrate;
419 
420       if ((iSpatialLayerNum==1) && (iIdxSpatial==0)) {
421         if (pSpatialLayer->iVideoWidth == 0) {
422           pSpatialLayer->iVideoWidth = iPicWidth;
423         }
424         if (pSpatialLayer->iVideoHeight == 0) {
425           pSpatialLayer->iVideoHeight = iPicHeight;
426         }
427         if (pSpatialLayer->iSpatialBitrate == 0) {
428           pSpatialLayer->iSpatialBitrate = iTargetBitrate;
429         }
430         if (pSpatialLayer->iMaxSpatialBitrate == 0) {
431           pSpatialLayer->iMaxSpatialBitrate = iMaxBitrate;
432         }
433       }
434 
435       //multi slice
436       pSpatialLayer->sSliceArgument = pCodingParam.sSpatialLayers[iIdxSpatial].sSliceArgument;
437 
438       memcpy (&(pSpatialLayer->sSliceArgument),
439               &(pCodingParam.sSpatialLayers[iIdxSpatial].sSliceArgument), // confirmed_safe_unsafe_usage
440               sizeof (SSliceArgument)) ;
441 
442       pSpatialLayer->iDLayerQp = pCodingParam.sSpatialLayers[iIdxSpatial].iDLayerQp;
443 
444       // See codec_app_def.h and parameter_sets.h for more info about members bVideoSignalTypePresent through uiColorMatrix.
445       pSpatialLayer->bVideoSignalTypePresent =   pCodingParam.sSpatialLayers[iIdxSpatial].bVideoSignalTypePresent;
446       pSpatialLayer->uiVideoFormat =             pCodingParam.sSpatialLayers[iIdxSpatial].uiVideoFormat;
447       pSpatialLayer->bFullRange =                pCodingParam.sSpatialLayers[iIdxSpatial].bFullRange;
448       pSpatialLayer->bColorDescriptionPresent =  pCodingParam.sSpatialLayers[iIdxSpatial].bColorDescriptionPresent;
449       pSpatialLayer->uiColorPrimaries =          pCodingParam.sSpatialLayers[iIdxSpatial].uiColorPrimaries;
450       pSpatialLayer->uiTransferCharacteristics = pCodingParam.sSpatialLayers[iIdxSpatial].uiTransferCharacteristics;
451       pSpatialLayer->uiColorMatrix =             pCodingParam.sSpatialLayers[iIdxSpatial].uiColorMatrix;
452 
453       pSpatialLayer->bAspectRatioPresent = pCodingParam.sSpatialLayers[iIdxSpatial].bAspectRatioPresent;
454       pSpatialLayer->eAspectRatio = pCodingParam.sSpatialLayers[iIdxSpatial].eAspectRatio;
455       pSpatialLayer->sAspectRatioExtWidth = pCodingParam.sSpatialLayers[iIdxSpatial].sAspectRatioExtWidth;
456       pSpatialLayer->sAspectRatioExtHeight = pCodingParam.sSpatialLayers[iIdxSpatial].sAspectRatioExtHeight;
457 
458       uiProfileIdc = (!bSimulcastAVC) ? PRO_SCALABLE_BASELINE : uiProfileIdc; //it is used in the D>0 layer if SVC is applied, so set to PRO_SCALABLE_BASELINE
459       ++ pDlp;
460       ++ pSpatialLayer;
461       ++ iIdxSpatial;
462     }
463 
464     SetActualPicResolution();
465 
466     return 0;
467   }
468 
469 // assuming that the width/height ratio of all spatial layers are the same
470 
SetActualPicResolutionTagWelsSvcCodingParam471   void SetActualPicResolution() {
472     int32_t iSpatialIdx = iSpatialLayerNum - 1;
473     for (; iSpatialIdx >= 0; iSpatialIdx --) {
474       SSpatialLayerInternal* pDlayerInternal = &sDependencyLayers[iSpatialIdx];
475       SSpatialLayerConfig* pDlayer =  &sSpatialLayers[iSpatialIdx];
476 
477       pDlayerInternal->iActualWidth = pDlayer->iVideoWidth;
478       pDlayerInternal->iActualHeight = pDlayer->iVideoHeight;
479       pDlayer->iVideoWidth = WELS_ALIGN (pDlayerInternal->iActualWidth, MB_WIDTH_LUMA);
480       pDlayer->iVideoHeight = WELS_ALIGN (pDlayerInternal->iActualHeight, MB_HEIGHT_LUMA);
481     }
482   }
483 
484   /*!
485   * \brief  determined key coding tables for temporal scalability, uiProfileIdc etc for each spatial layer settings
486   * \param  SWelsSvcCodingParam, and carried with known GOP size, max, input and output frame rate of each spatial
487   * \return NONE (should ensure valid parameter before this procedure)
488   */
DetermineTemporalSettingsTagWelsSvcCodingParam489   int32_t DetermineTemporalSettings() {
490     const int32_t iDecStages = WELS_LOG2 (uiGopSize); // (int8_t)GetLogFactor(1.0f, 1.0f * pcfg->uiGopSize);  //log2(uiGopSize)
491     const uint8_t* pTemporalIdList = &g_kuiTemporalIdListTable[iDecStages][0];
492     SSpatialLayerInternal* pDlp    = &sDependencyLayers[0];
493     SSpatialLayerConfig* pSpatialLayer = &sSpatialLayers[0];
494     int8_t i = 0;
495 
496     while (i < iSpatialLayerNum) {
497       const uint32_t kuiLogFactorInOutRate = GetLogFactor (pDlp->fOutputFrameRate, pDlp->fInputFrameRate);
498       const uint32_t kuiLogFactorMaxInRate = GetLogFactor (pDlp->fInputFrameRate, fMaxFrameRate);
499       if (UINT_MAX == kuiLogFactorInOutRate || UINT_MAX == kuiLogFactorMaxInRate) {
500         return ENC_RETURN_INVALIDINPUT;
501       }
502       int32_t iNotCodedMask = 0;
503       int8_t iMaxTemporalId = 0;
504 
505       memset (pDlp->uiCodingIdx2TemporalId, INVALID_TEMPORAL_ID, sizeof (pDlp->uiCodingIdx2TemporalId));
506       iNotCodedMask = (1 << (kuiLogFactorInOutRate + kuiLogFactorMaxInRate)) - 1;
507       for (uint32_t uiFrameIdx = 0; uiFrameIdx <= uiGopSize; ++ uiFrameIdx) {
508         if (0 == (uiFrameIdx & iNotCodedMask)) {
509           const int8_t kiTemporalId = pTemporalIdList[uiFrameIdx];
510           pDlp->uiCodingIdx2TemporalId[uiFrameIdx] = kiTemporalId;
511           if (kiTemporalId > iMaxTemporalId) {
512             iMaxTemporalId = kiTemporalId;
513           }
514         }
515       }
516 
517       pDlp->iHighestTemporalId   = iMaxTemporalId;
518       pDlp->iTemporalResolution  = kuiLogFactorMaxInRate + kuiLogFactorInOutRate;
519       pDlp->iDecompositionStages = iDecStages - kuiLogFactorMaxInRate - kuiLogFactorInOutRate;
520       if (pDlp->iDecompositionStages < 0) {
521         return ENC_RETURN_INVALIDINPUT;
522       }
523       ++ pDlp;
524       ++ pSpatialLayer;
525       ++ i;
526     }
527     iDecompStages = (int8_t)iDecStages;
528     return ENC_RETURN_SUCCESS;
529   }
530 
531 } SWelsSvcCodingParam;
532 
533 
534 typedef struct TagExistingParasetList {
535   SWelsSPS            sSps[MAX_SPS_COUNT];
536   SSubsetSps          sSubsetSps[MAX_SPS_COUNT];
537   SWelsPPS            sPps[MAX_PPS_COUNT];
538 
539   uint32_t            uiInUseSpsNum;
540   uint32_t            uiInUseSubsetSpsNum;
541   uint32_t            uiInUsePpsNum;
542 } SExistingParasetList;
543 
544 
FreeCodingParam(SWelsSvcCodingParam ** pParam,CMemoryAlign * pMa)545 static inline int32_t FreeCodingParam (SWelsSvcCodingParam** pParam, CMemoryAlign* pMa) {
546   if (pParam == NULL || *pParam == NULL || pMa == NULL)
547     return 1;
548   pMa->WelsFree (*pParam, "SWelsSvcCodingParam");
549   *pParam = NULL;
550   return 0;
551 }
552 
AllocCodingParam(SWelsSvcCodingParam ** pParam,CMemoryAlign * pMa)553 static inline int32_t AllocCodingParam (SWelsSvcCodingParam** pParam, CMemoryAlign* pMa) {
554   if (pParam == NULL || pMa == NULL)
555     return 1;
556   if (*pParam != NULL) {
557     FreeCodingParam (pParam, pMa);
558   }
559   SWelsSvcCodingParam* pCodingParam = (SWelsSvcCodingParam*)pMa->WelsMallocz (sizeof (SWelsSvcCodingParam),
560                                       "SWelsSvcCodingParam");
561   if (NULL == pCodingParam)
562     return 1;
563   *pParam = pCodingParam;
564   return 0;
565 }
566 
567 }//end of namespace WelsEnc
568 
569 #endif//WELS_ENCODER_PARAMETER_SVC_H__
570