shithub: openh264

Download patch

ref: e79769fb881f399e171a7d0121a8868576b02d77
parent: 0fcd066cde0e384ca71ef8677f88c7fcd8fc41dd
parent: 7d638f8a6bee932a7da0e5fd1256707c98d87996
author: ruil2 <[email protected]>
date: Wed Feb 19 04:12:58 EST 2014

Merge pull request #305 from mstorsjo/fix-encoder-param-interface

Don't duplicate the SEncParamExt/SEncParamBase structs for C compatibility

--- a/codec/api/svc/codec_api.h
+++ b/codec/api/svc/codec_api.h
@@ -56,8 +56,8 @@
   /*
    * return: CM_RETURN: 0 - success; otherwise - failed;
    */
-  virtual int EXTAPI Initialize (SEncParamBase* pParam, const INIT_TYPE kiInitType = INIT_TYPE_PARAMETER_BASED) = 0;
-  virtual int EXTAPI Initialize2 (void* pParam, const INIT_TYPE kiInitType = INIT_TYPE_PARAMETER_BASED) = 0;
+  virtual int EXTAPI Initialize (const SEncParamBase* pParam) = 0;
+  virtual int EXTAPI InitializeExt (const SEncParamExt* pParam) = 0;
 
   virtual int EXTAPI Uninitialize() = 0;
 
@@ -141,8 +141,8 @@
 typedef const ISVCEncoderVtbl* ISVCEncoder;
 struct ISVCEncoderVtbl {
 
-  int (*Initialize) (ISVCEncoder*, SEncParamBase* pParam, const INIT_TYPE kiInitType);
-  int (*Initialize2) (ISVCEncoder*, void* pParam, const INIT_TYPE kiInitType);
+  int (*Initialize) (ISVCEncoder*, const SEncParamBase* pParam);
+  int (*InitializeExt) (ISVCEncoder*, const SEncParamExt* pParam);
 
   int (*Uninitialize) (ISVCEncoder*);
 
--- a/codec/api/svc/codec_app_def.h
+++ b/codec/api/svc/codec_app_def.h
@@ -116,12 +116,6 @@
   VIDEO_CODING_LAYER = 1
 } LAYER_TYPE;
 
-/* SVC Encoder/Decoder Initializing Parameter Types */
-typedef enum {
-  INIT_TYPE_PARAMETER_BASED = 0,	// For SVC DEMO Application
-  INIT_TYPE_PARAMETER_EXT,			// For SVC CONSOLE Application
-} INIT_TYPE;
-
 //enumerate the type of video bitstream which is provided to decoder
 typedef enum {
   VIDEO_BITSTREAM_AVC               = 0,
@@ -185,7 +179,6 @@
   SSliceConfig sSliceCfg;
 } SSpatialLayerConfig;
 
-#ifdef __cplusplus
 /* SVC Encoding Parameters */
 typedef struct TagEncParamBase{
 
@@ -201,8 +194,17 @@
 } SEncParamBase, *PEncParamBase;
 
 
-typedef struct TagEncParamExt:SEncParamBase
+typedef struct TagEncParamExt
 {
+  int       iUsageType;	//enable_screen_content_signal;// 0: //camera video signal; 1: screen content signal;
+  int		iInputCsp;	// color space of input sequence
+
+  int		iPicWidth;			// width of picture in samples
+  int		iPicHeight;			// height of picture in samples
+  int		iTargetBitrate;		// target bitrate desired
+  int       iRCMode;                 // RC mode
+  float	    fMaxFrameRate;			// input maximal frame rate
+
   int		iTemporalLayerNum;	// layer number at temporal level
   int		iSpatialLayerNum;	// layer number at spatial level
 
@@ -247,79 +249,6 @@
   int iMinQp;
 
 }SEncParamExt;
-#else
-/* SVC Encoding Parameters */
-typedef struct TagEncParamBase{
-
-    int       iUsageType;	//enable_screen_content_signal;// 0: //camera video signal; 1: screen content signal;
-    int		iInputCsp;	// color space of input sequence
-
-    int		iPicWidth;			// width of picture in samples
-    int		iPicHeight;			// height of picture in samples
-    int		iTargetBitrate;		// target bitrate desired
-    int       iRCMode;                 // RC mode
-    float	    fMaxFrameRate;			// input maximal frame rate
-
-} SEncParamBase, *PEncParamBase;
-
-typedef struct TagEncParamExt
-{
-    int       iUsageType;	//enable_screen_content_signal;// 0: //camera video signal; 1: screen content signal;
-    int		iInputCsp;	// color space of input sequence
-
-    int		iPicWidth;			// width of picture in samples
-    int		iPicHeight;			// height of picture in samples
-    int		iTargetBitrate;		// target bitrate desired
-    int       iRCMode;                 // RC mode
-    float	    fMaxFrameRate;			// input maximal frame rate
-
-    int		iTemporalLayerNum;	// layer number at temporal level
-    int		iSpatialLayerNum;	// layer number at spatial level
-
-    unsigned int		uiIntraPeriod;		// period of Intra frame
-    bool    bEnableSpsPpsIdAddition;
-    bool    bPrefixNalAddingCtrl;
-    bool    bEnableDenoise;	    // denoise control
-    bool    bEnableBackgroundDetection;// background detection control //VAA_BACKGROUND_DETECTION //BGD cmd
-    bool    bEnableAdaptiveQuant; // adaptive quantization control
-    bool    bEnableFrameSkip; // allow skipping frames to keep the bitrate within limits
-    bool	bEnableCropPic;	// enable cropping source picture.  8/25/2010
-    // false: Streaming Video Sharing; true: Video Conferencing Meeting;
-
-    bool     bEnableLongTermReference; // 0: on, 1: off
-    int      iLtrMarkPeriod;
-    int   iPaddingFlag;            // 0:disable padding;1:padding
-    int   iEtropyCodingModeFlag;
-
-    SSpatialLayerConfig sSpatialLayers[MAX_SPATIAL_LAYER_NUM];
-    int		    iNumRefFrame;		// number of reference frame used
-    unsigned int	uiFrameToBeCoded;	// frame to be encoded (at input frame rate)
-    unsigned int  uiGopSize;
-    bool   bEnableRc;
-    short		iMultipleThreadIdc;		// 1	# 0: auto(dynamic imp. internal encoder); 1: multiple threads imp. disabled; > 1: count number of threads;
-    short		iCountThreadsNum;			//		# derived from disable_multiple_slice_idc (=0 or >1) means;
-
-    int		iLTRRefNum;
-    bool		bEnableSSEI;
-    bool		bEnableFrameCroppingFlag;// enable frame cropping flag: TRUE always in application
-
-    /* Deblocking loop filter */
-    int		iLoopFilterDisableIdc;	// 0: on, 1: off, 2: on except for slice boundaries
-    int		iLoopFilterAlphaC0Offset;// AlphaOffset: valid range [-6, 6], default 0
-
-    int		iLoopFilterBetaOffset;	// BetaOffset:	valid range [-6, 6], default 0
-    int		iInterLayerLoopFilterDisableIdc; // Employed based upon inter-layer, same comment as above
-    int		iInterLayerLoopFilterAlphaC0Offset;	// InterLayerLoopFilterAlphaC0Offset
-    int		iInterLayerLoopFilterBetaOffset;	// InterLayerLoopFilterBetaOffset
-    bool      bEnableSceneChangeDetect;
-
-    //added
-    int iMaxQp;
-    int iMinQp;
-
-}SEncParamExt;
-
-#endif
 
 //Define a new struct to show the property of video bitstream.
 typedef struct {
--- a/codec/console/enc/src/welsenc.cpp
+++ b/codec/console/enc/src/welsenc.cpp
@@ -752,7 +752,7 @@
   }
 
   memset (&sFbi, 0, sizeof (SFrameBSInfo));
-  memset (&sSvcParam, 0, sizeof (SEncParamBase));
+  memset (&sSvcParam, 0, sizeof (SEncParamExt));
 
   FillSpecificParameters (sSvcParam);
 
@@ -763,7 +763,7 @@
     goto ERROR_RET;
   }
 
-  if (cmResultSuccess != pPtrEnc->Initialize (&sSvcParam, INIT_TYPE_PARAMETER_BASED)) {
+  if (cmResultSuccess != pPtrEnc->InitializeExt (&sSvcParam)) {
     fprintf (stderr, "Encoder Initialization failed!\n");
 	ret = 1;
     goto ERROR_RET;
@@ -932,7 +932,7 @@
   sSvcParam.iPicHeight = sSvcParam.sSpatialLayers[sSvcParam.iSpatialLayerNum - 1].iVideoHeight;
 
 
-  if (cmResultSuccess != pPtrEnc->Initialize (&sSvcParam, INIT_TYPE_PARAMETER_EXT)) {	// SVC encoder initialization
+  if (cmResultSuccess != pPtrEnc->InitializeExt (&sSvcParam)) {	// SVC encoder initialization
     fprintf (stderr, "SVC encoder Initialize failed\n");
     iRet = 1;
     goto INSIDE_MEM_FREE;
--- a/codec/encoder/core/inc/param_svc.h
+++ b/codec/encoder/core/inc/param_svc.h
@@ -129,8 +129,6 @@
 // FALSE: Streaming Video Sharing; TRUE: Video Conferencing Meeting;
 
 int8_t		iDecompStages;		// GOP size dependency
-/* Rate Control */
-bool		bEnableRc;
 
 
  public:
@@ -222,9 +220,8 @@
 
 }
 
-int32_t ParamBaseTranscode (SEncParamBase& pCodingParam, const bool kbEnableRc = true) {
+int32_t ParamBaseTranscode (const SEncParamBase& pCodingParam, const bool kbEnableRc = true) {
 
-  pCodingParam.fMaxFrameRate		= WELS_CLIP3 (pCodingParam.fMaxFrameRate, MIN_FRAME_RATE, MAX_FRAME_RATE);
   iInputCsp		= pCodingParam.iInputCsp;		// color space of input sequence
 
   iPicWidth   = pCodingParam.iPicWidth;
@@ -278,8 +275,17 @@
 
    return 0;
 }
-int32_t ParamTranscode (SEncParamExt& pCodingParam, const bool kbEnableRc = true) {
-  pCodingParam.fMaxFrameRate		= WELS_CLIP3 (pCodingParam.fMaxFrameRate, MIN_FRAME_RATE, MAX_FRAME_RATE);
+void GetBaseParams (SEncParamBase* pCodingParam) {
+  pCodingParam->iUsageType     = iUsageType;
+  pCodingParam->iInputCsp      = iInputCsp;
+  pCodingParam->iPicWidth      = iPicWidth;
+  pCodingParam->iPicHeight     = iPicHeight;
+  pCodingParam->iTargetBitrate = iTargetBitrate;
+  pCodingParam->iRCMode        = iRCMode;
+  pCodingParam->fMaxFrameRate  = fMaxFrameRate;
+}
+int32_t ParamTranscode (const SEncParamExt& pCodingParam) {
+  float fParamMaxFrameRate		= WELS_CLIP3 (pCodingParam.fMaxFrameRate, MIN_FRAME_RATE, MAX_FRAME_RATE);
 
   iInputCsp		= pCodingParam.iInputCsp;		// color space of input sequence
   uiFrameToBeCoded	= (uint32_t) -
@@ -308,7 +314,7 @@
   bEnableFrameCroppingFlag	= true;
 
   /* Rate Control */
-  bEnableRc			= kbEnableRc;
+  bEnableRc			= pCodingParam.bEnableRc;
   if (pCodingParam.iRCMode != RC_MODE0 && pCodingParam.iRCMode != RC_MODE1)
     iRCMode = RC_MODE1;
   else
@@ -345,10 +351,7 @@
   /* Layer definition */
   iSpatialLayerNum	= (int8_t)WELS_CLIP3 (pCodingParam.iSpatialLayerNum, 1,
                         MAX_DEPENDENCY_LAYER); // number of dependency(Spatial/CGS) layers used to be encoded
-  pCodingParam.iTemporalLayerNum = (int8_t)WELS_CLIP3 (pCodingParam.iTemporalLayerNum, 1,
-                                   MAX_TEMPORAL_LEVEL);	// safe valid iTemporalLayerNum
-  iTemporalLayerNum		= (int8_t)
-                        pCodingParam.iTemporalLayerNum;//(int8_t)WELS_CLIP3(pCodingParam.iTemporalLayerNum, 1, MAX_TEMPORAL_LEVEL);// number of temporal layer specified
+  iTemporalLayerNum		= (int8_t)WELS_CLIP3(pCodingParam.iTemporalLayerNum, 1, MAX_TEMPORAL_LEVEL);// number of temporal layer specified
 
   uiGopSize			= 1 << (iTemporalLayerNum - 1);	// Override GOP size based temporal layer
   iDecompStages		= iTemporalLayerNum - 1;	// WELS_LOG2( uiGopSize );// GOP size dependency
@@ -377,11 +380,10 @@
   while (iIdxSpatial < iSpatialLayerNum) {
     pDlp->uiProfileIdc		= uiProfileIdc;
 
-    pCodingParam.sSpatialLayers[iIdxSpatial].fFrameRate	= WELS_CLIP3 (pCodingParam.sSpatialLayers[iIdxSpatial].fFrameRate,
-        MIN_FRAME_RATE, pCodingParam.fMaxFrameRate);
+    float fLayerFrameRate	= WELS_CLIP3 (pCodingParam.sSpatialLayers[iIdxSpatial].fFrameRate,
+        MIN_FRAME_RATE, fParamMaxFrameRate);
     pDlp->fInputFrameRate	=
-      pDlp->fOutputFrameRate	= WELS_CLIP3 (pCodingParam.sSpatialLayers[iIdxSpatial].fFrameRate, MIN_FRAME_RATE,
-                                            MAX_FRAME_RATE);
+      pDlp->fOutputFrameRate	= WELS_CLIP3 (fLayerFrameRate, MIN_FRAME_RATE, MAX_FRAME_RATE);
     if (pDlp->fInputFrameRate > fMaxFr + EPSN)
       fMaxFr = pDlp->fInputFrameRate;
 
--- a/codec/encoder/plus/inc/welsEncoderExt.h
+++ b/codec/encoder/plus/inc/welsEncoderExt.h
@@ -66,8 +66,8 @@
   /*
    * return: CM_RETURN: 0 - success; otherwise - failed;
    */
-  virtual int EXTAPI Initialize (SEncParamBase* argv, const INIT_TYPE init_type);
-  virtual int EXTAPI Initialize2 (void* argv, const INIT_TYPE init_type);
+  virtual int EXTAPI Initialize (const SEncParamBase* argv);
+  virtual int EXTAPI InitializeExt (const SEncParamExt* argv);
 
   virtual int EXTAPI Uninitialize();
 
@@ -102,6 +102,8 @@
   virtual int EXTAPI GetOption (ENCODER_OPTION opt_id, void* option);
 
  private:
+  int Initialize2 (SWelsSvcCodingParam* argv);
+
   sWelsEncCtx*	m_pEncContext;
 
   welsCodecTrace*			m_pWelsTrace;
--- a/codec/encoder/plus/src/welsEncoderExt.cpp
+++ b/codec/encoder/plus/src/welsEncoderExt.cpp
@@ -200,45 +200,59 @@
 /*
  *	SVC Encoder Initialization
  */
-int CWelsH264SVCEncoder::Initialize (SEncParamBase* argv, const INIT_TYPE iInitType) {
+int CWelsH264SVCEncoder::Initialize (const SEncParamBase* argv) {
 
-
-  if ((INIT_TYPE_PARAMETER_BASED != iInitType && INIT_TYPE_PARAMETER_EXT != iInitType)|| NULL == argv) {
-    WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), invalid iInitType= %d, argv= 0x%p\n",
-             iInitType, (void*)argv);
+  if (NULL == argv) {
+    WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), invalid argv= 0x%p\n",
+             argv);
     return cmInitParaError;
   }
- if (m_bInitialFlag) {
-    WelsLog (m_pEncContext, WELS_LOG_WARNING, "CWelsH264SVCEncoder::Initialize(), reinitialize, m_bInitialFlag= %d\n",
-             m_bInitialFlag);
+
+  SWelsSvcCodingParam	sConfig (true);
+  // Convert SEncParamBase into WelsSVCParamConfig here..
+  if (sConfig.ParamBaseTranscode (*argv, true)) {
+    WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), parameter_translation failed.\n");
     Uninitialize();
+    return cmInitParaError;
   }
 
+  return Initialize2 (&sConfig);
+}
+
+int CWelsH264SVCEncoder::InitializeExt (const SEncParamExt* argv) {
+
+  if (NULL == argv) {
+    WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), invalid argv= 0x%p\n",
+             argv);
+    return cmInitParaError;
+  }
+
   SWelsSvcCodingParam	sConfig (true);
-  if(iInitType ==  INIT_TYPE_PARAMETER_BASED)
-  {
-	 SEncParamBase		sEncodingParam;
-	 memcpy (&sEncodingParam, argv, sizeof (SEncParamBase));	// confirmed_safe_unsafe_usage
+  // Convert SEncParamExt into WelsSVCParamConfig here..
+  if (sConfig.ParamTranscode (*argv)) {
+    WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), parameter_translation failed.\n");
+    Uninitialize();
+    return cmInitParaError;
+  }
 
-	 // Convert SEncParamBase into WelsSVCParamConfig here..
-	 if (sConfig.ParamBaseTranscode (sEncodingParam, true)) {
-		 WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), parameter_translation failed.\n");
-		 Uninitialize();
-		 return cmInitParaError;
-	 }
+  return Initialize2 (&sConfig);
+}
+
+int CWelsH264SVCEncoder::Initialize2 (SWelsSvcCodingParam* pCfg) {
+  if (NULL == pCfg) {
+    WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), invalid argv= 0x%p.\n",
+             pCfg);
+    return cmInitParaError;
   }
-  else if(iInitType ==  INIT_TYPE_PARAMETER_EXT)
-  {
-	SEncParamExt		sEncodingParam;
-	memcpy (&sEncodingParam, argv, sizeof (SEncParamExt));	// confirmed_safe_unsafe_usage
-	// Convert SEncParamBase into WelsSVCParamConfig here..
-	if (sConfig.ParamTranscode (sEncodingParam, true)) {
-		WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), parameter_translation failed.\n");
-		Uninitialize();
-		return cmInitParaError;
-	}
+
+  if (m_bInitialFlag) {
+    WelsLog (m_pEncContext, WELS_LOG_WARNING, "CWelsH264SVCEncoder::Initialize(), reinitialize, m_bInitialFlag= %d.\n",
+             m_bInitialFlag);
+    Uninitialize();
   }
+
 #ifdef REC_FRAME_COUNT
+  SWelsSvcCodingParam &sEncodingParam = *pCfg;
   WelsLog (m_pEncContext, WELS_LOG_INFO, "CWelsH264SVCEncoder::Initialize, m_uiCountFrameNum= %d, m_iCspInternal= 0x%x\n",
            m_uiCountFrameNum, m_iCspInternal);
   WelsLog (m_pEncContext, WELS_LOG_INFO,
@@ -249,9 +263,9 @@
            sEncodingParam.iRCMode,
            sEncodingParam.iTemporalLayerNum,
            sEncodingParam.iSpatialLayerNum,
-           sEncodingParam.fFrameRate,
+           sEncodingParam.fMaxFrameRate,
            sEncodingParam.iInputCsp,
-           sEncodingParam.iIntraPeriod,
+           sEncodingParam.uiIntraPeriod,
            sEncodingParam.bEnableSpsPpsIdAddition,
            sEncodingParam.bPrefixNalAddingCtrl,
            sEncodingParam.bEnableDenoise,
@@ -280,26 +294,7 @@
     ++ i;
   }
 #endif//REC_FRAME_COUNT
-  m_iSrcListSize  = 1;
 
-  return Initialize2 ((void*)&sConfig, INIT_TYPE_PARAMETER_BASED);
-}
-
-int CWelsH264SVCEncoder::Initialize2 (void* argv, const INIT_TYPE iInitType) {
-  if (INIT_TYPE_PARAMETER_BASED != iInitType || NULL == argv) {
-    WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), invalid iInitType= %d, argv= 0x%p.\n",
-             iInitType, (void*)argv);
-    return cmInitParaError;
-  }
-
-  if (m_bInitialFlag) {
-    WelsLog (m_pEncContext, WELS_LOG_WARNING, "CWelsH264SVCEncoder::Initialize(), reinitialize, m_bInitialFlag= %d.\n",
-             m_bInitialFlag);
-    Uninitialize();
-  }
-
-  SWelsSvcCodingParam*  pCfg = static_cast<SWelsSvcCodingParam*> (argv);
-
   const int32_t iColorspace = pCfg->iInputCsp;
   if (0 == iColorspace) {
     WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), invalid iInputCsp= %d.\n", iColorspace);
@@ -796,7 +791,7 @@
     }
 
     iInputColorspace	= sEncodingParam.iInputCsp;
-    if (sConfig.ParamTranscode (sEncodingParam, true)) {
+    if (sConfig.ParamTranscode (sEncodingParam)) {
       return cmInitParaError;
     }
     if (sConfig.iSpatialLayerNum < 1) {
@@ -973,7 +968,7 @@
              "CWelsH264SVCEncoder::GetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_BASE, m_uiCountFrameNum= %d, m_iCspInternal= 0x%x\n",
              m_uiCountFrameNum, m_iCspInternal);
 #endif//REC_FRAME_COUNT
-    memcpy (pOption, m_pEncContext->pSvcParam, sizeof (SEncParamBase));	// confirmed_safe_unsafe_usage
+    m_pEncContext->pSvcParam->GetBaseParams((SEncParamBase*) pOption);
   }
   break;
 
--- a/test/BaseEncoderTest.cpp
+++ b/test/BaseEncoderTest.cpp
@@ -15,7 +15,7 @@
   param.iPicHeight = height;
   param.iTargetBitrate = 5000000;
   param.iInputCsp = videoFormatI420;
-  return encoder->Initialize(&param, INIT_TYPE_PARAMETER_BASED);
+  return encoder->Initialize(&param);
 }
 
 BaseEncoderTest::BaseEncoderTest() : encoder_(NULL) {}
--- a/test/c_interface_test.c
+++ b/test/c_interface_test.c
@@ -11,7 +11,7 @@
 
 void CheckEncoderInterface(ISVCEncoder* p, CheckFunc check) {
   CHECK(1, p, Initialize);
-  CHECK(2, p, Initialize2);
+  CHECK(2, p, InitializeExt);
   CHECK(3, p, Uninitialize);
   CHECK(4, p, EncodeFrame);
   CHECK(5, p, EncodeFrame2);
--- a/test/cpp_interface_test.cpp
+++ b/test/cpp_interface_test.cpp
@@ -22,12 +22,11 @@
  */
 struct SVCEncoderImpl : public ISVCEncoder {
   virtual ~SVCEncoderImpl() {}
-  virtual int EXTAPI Initialize(SEncParamBase* pParam,
-      const INIT_TYPE kiInitType) {
+  virtual int EXTAPI Initialize(const SEncParamBase* pParam) {
     EXPECT_TRUE(gThis == this);
     return 1;
   }
-  virtual int EXTAPI Initialize2(void* pParam, const INIT_TYPE kiInitType) {
+  virtual int EXTAPI InitializeExt(const SEncParamExt* pParam) {
     EXPECT_TRUE(gThis == this);
     return 2;
   }