shithub: openh264

Download patch

ref: 59fefbe7c712a77b88f09d3844becc193f374e69
parent: a9c2e1b431ed113da603855c7baf419fbe9c44e6
author: Martin Storsjö <martin@martin.st>
date: Tue Dec 16 06:08:19 EST 2014

Use an inline function instead of a macro for clipping which contains rand()

When using a macro, the macro parameters get evaluated
multiple times, which means that the rand() value compared
actually isn't the same that is used as return value.

This makes sure that clipping works as intended for the
random tests.

--- a/codec/common/inc/macros.h
+++ b/codec/common/inc/macros.h
@@ -197,6 +197,14 @@
 #define WELS_CLIP3(iX, iY, iZ) ((iX) < (iY) ? (iY) : ((iX) > (iZ) ? (iZ) : (iX)))
 #endif //WELS_CLIP3
 
+template<typename T> T WelsClip3(T iX, T iY, T iZ) {
+  if (iX < iY)
+    return iY;
+  if (iX > iZ)
+    return iZ;
+  return iX;
+}
+
 /*
  * Description: to check variable validation and return the specified result
  *	iResult:	value to be checked
--- a/test/api/encode_decode_api_test.cpp
+++ b/test/api/encode_decode_api_test.cpp
@@ -221,8 +221,8 @@
 
 void EncodeDecodeTestAPI::RandomParamExtCombination() {
 
-  param_.iPicWidth  = WELS_CLIP3 ((((rand() % MAX_WIDTH) >> 1)  + 1) << 1, 2, MAX_WIDTH);
-  param_.iPicHeight = WELS_CLIP3 ((((rand() % MAX_HEIGHT) >> 1) + 1) << 1, 2, MAX_HEIGHT);
+  param_.iPicWidth  = WelsClip3 ((((rand() % MAX_WIDTH) >> 1)  + 1) << 1, 2, MAX_WIDTH);
+  param_.iPicHeight = WelsClip3 ((((rand() % MAX_HEIGHT) >> 1) + 1) << 1, 2, MAX_HEIGHT);
 
   param_.fMaxFrameRate      = rand() % FRAME_RATE_RANGE + 0.5f;
   param_.iUsageType         = static_cast<EUsageType> (rand() % 2);
@@ -271,8 +271,8 @@
       //to do: profile and level id
       //pSpatialLayer->uiProfileIdc        = 0;
       //pSpatialLayer->uiLevelIdc          = 0;
-      pSpatialLayer->iVideoWidth         = WELS_CLIP3 ((((rand() % MAX_WIDTH) >> 1)  + 1) << 1, 2, MAX_WIDTH);
-      pSpatialLayer->iVideoHeight        = WELS_CLIP3 ((((rand() % MAX_HEIGHT) >> 1) + 1) << 1, 2, MAX_HEIGHT);
+      pSpatialLayer->iVideoWidth         = WelsClip3 ((((rand() % MAX_WIDTH) >> 1)  + 1) << 1, 2, MAX_WIDTH);
+      pSpatialLayer->iVideoHeight        = WelsClip3 ((((rand() % MAX_HEIGHT) >> 1) + 1) << 1, 2, MAX_HEIGHT);
       pSpatialLayer->fFrameRate          = rand() % FRAME_RATE_RANGE + 0.5f;
       pSpatialLayer->iMaxSpatialBitrate  = rand() % BIT_RATE_RANGE;
       pSpatialLayer->iSpatialBitrate     = rand() % BIT_RATE_RANGE;
@@ -2057,10 +2057,10 @@
 }
 
 TEST_F (EncodeDecodeTestAPI, SetOptionEncParamExt) {
-  int iWidth       = WELS_CLIP3 ((((rand() % MAX_WIDTH) >> 1)  + 1) << 1, 2, MAX_WIDTH);
-  int iHeight      = WELS_CLIP3 ((((rand() % MAX_HEIGHT) >> 1)  + 1) << 1, 2, MAX_HEIGHT);
+  int iWidth       = WelsClip3 ((((rand() % MAX_WIDTH) >> 1)  + 1) << 1, 2, MAX_WIDTH);
+  int iHeight      = WelsClip3 ((((rand() % MAX_HEIGHT) >> 1)  + 1) << 1, 2, MAX_HEIGHT);
   float fFrameRate = rand() + 0.5f;
-  int iEncFrameNum = WELS_CLIP3 ((rand() % ENCODE_FRAME_NUM) + 1, 1, ENCODE_FRAME_NUM);
+  int iEncFrameNum = WelsClip3 ((rand() % ENCODE_FRAME_NUM) + 1, 1, ENCODE_FRAME_NUM);
   int iSpatialLayerNum = 4;
   int iSliceNum        = 1;
   encoder_->GetDefaultParams (&param_);
--- a/test/decoder/DecUT_DeblockCommon.cpp
+++ b/test/decoder/DecUT_DeblockCommon.cpp
@@ -26,7 +26,7 @@
   iTc[0] = iTc[1] = iTc[2] = iTc[3] = 25; \
   pBase[0] = pRef[0] = 128; \
   for (int i = 1; i < iWidth*iWidth; i++) { \
-  pBase[i] = pRef[i] = WELS_CLIP3( pBase[i-1] -16 + rand()%32, 0, 255 ); \
+  pBase[i] = pRef[i] = WelsClip3( pBase[i-1] -16 + rand()%32, 0, 255 ); \
   } \
 } else if (iNum==1) { \
   iAlpha = 4; \
@@ -34,7 +34,7 @@
   iTc[0] = iTc[1] = iTc[2] = iTc[3] = 9; \
   pBase[0] = pRef[0] = 128; \
   for (int i = 1; i < iWidth*iWidth; i++) { \
-  pBase[i] = pRef[i] = WELS_CLIP3( pBase[i-1] -4 + rand()%8, 0, 255 ); \
+  pBase[i] = pRef[i] = WelsClip3( pBase[i-1] -4 + rand()%8, 0, 255 ); \
   } \
 } else { \
   iAlpha = rand() % 256; \