shithub: openh264

Download patch

ref: 542fd232cc05f86b4de26516d71a5c9ec7d0e55a
parent: f9f2bbf805ebb82d0cc46dd79aade2dfb264f046
author: Sijia Chen <sijchen@cisco.com>
date: Tue Oct 6 07:10:06 EDT 2015

add a .template file for codec UT

--- /dev/null
+++ b/test/api/encode_decode_api_test.h
@@ -1,0 +1,163 @@
+#include <gtest/gtest.h>
+#include "codec_def.h"
+#include "utils/BufferedData.h"
+#include "utils/FileInputStream.h"
+#include "BaseDecoderTest.h"
+#include "BaseEncoderTest.h"
+#include "wels_common_defs.h"
+#include "utils/HashFunctions.h"
+#include <string>
+#include <vector>
+using namespace WelsCommon;
+
+//TODO: some content here in this file is the same with encode_decode_api_test.cpp
+//plan to combine them after some on-going code reviews in that file to avoid merging conflict
+
+#define TRY_TIME_RANGE           (10)
+#define ENCODE_FRAME_NUM         (30)
+#define LEVEL_ID_RANGE           (18)
+#define MAX_WIDTH                (4096)
+#define MAX_HEIGHT               (2304)
+#define MAX_FRAME_RATE           (30)
+#define MIN_FRAME_RATE           (1)
+#define FRAME_RATE_RANGE         (2*MAX_FRAME_RATE)
+#define RC_MODE_RANGE            (4)
+#define BIT_RATE_RANGE           (10000)
+#define MAX_QP                   (51)
+#define MIN_QP                   (0)
+#define QP_RANGE                 (2*MAX_QP)
+#define SPATIAL_LAYER_NUM_RANGE  (2*MAX_SPATIAL_LAYER_NUM)
+#define TEMPORAL_LAYER_NUM_RANGE (2*MAX_TEMPORAL_LAYER_NUM)
+#define SAVED_NALUNIT_NUM        ( (MAX_SPATIAL_LAYER_NUM*MAX_QUALITY_LAYER_NUM) + 1 + MAX_SPATIAL_LAYER_NUM )
+#define MAX_SLICES_NUM           ( ( MAX_NAL_UNITS_IN_LAYER - SAVED_NALUNIT_NUM ) / 3 )
+#define SLICE_MODE_NUM           (6)
+#define LOOP_FILTER_IDC_NUM      (3)
+#define LOOF_FILTER_OFFSET_RANGE (6)
+#define MAX_REF_PIC_COUNT        (16)
+#define MIN_REF_PIC_COUNT        (1)
+#define LONG_TERM_REF_NUM        (2)
+#define LONG_TERM_REF_NUM_SCREEN (4)
+#define MAX_REFERENCE_PICTURE_COUNT_NUM_CAMERA (6)
+#define MAX_REFERENCE_PICTURE_COUNT_NUM_SCREEN (8)
+#define VALID_SIZE(iSize) (((iSize)>16)?(iSize):16)
+#define GET_MB_WIDTH(x) (((x) + 15)/16)
+
+typedef struct SLost_Sim {
+  WelsCommon::EWelsNalUnitType eNalType;
+  bool isLost;
+} SLostSim;
+
+
+struct EncodeDecodeFileParamBase {
+  int numframes;
+  int width;
+  int height;
+  float frameRate;
+  int slicenum;
+  bool bLostPara;
+  const char* pLossSequence;
+};
+
+static void welsStderrTraceOrigin (void* ctx, int level, const char* string) {
+  fprintf (stderr, "%s\n", string);
+}
+
+typedef struct STrace_Unit {
+  int iTarLevel;
+} STraceUnit;
+
+static void TestOutPutTrace (void* ctx, int level, const char* string) {
+  STraceUnit* pTraceUnit = (STraceUnit*) ctx;
+  EXPECT_LE (level, pTraceUnit->iTarLevel);
+}
+
+class EncodeDecodeTestBase : public BaseEncoderTest, public BaseDecoderTest {
+ public:
+  uint8_t iRandValue;
+ public:
+  virtual void SetUp() {
+    BaseEncoderTest::SetUp();
+    BaseDecoderTest::SetUp();
+    pFunc = welsStderrTraceOrigin;
+    pTraceInfo = NULL;
+    encoder_->SetOption (ENCODER_OPTION_TRACE_CALLBACK, &pFunc);
+    encoder_->SetOption (ENCODER_OPTION_TRACE_CALLBACK_CONTEXT, &pTraceInfo);
+    decoder_->SetOption (DECODER_OPTION_TRACE_CALLBACK, &pFunc);
+    decoder_->SetOption (DECODER_OPTION_TRACE_CALLBACK_CONTEXT, &pTraceInfo);
+  }
+
+  virtual void TearDown() {
+    BaseEncoderTest::TearDown();
+    BaseDecoderTest::TearDown();
+  }
+
+  virtual void prepareParam (int iLayers, int iSlices, int width, int height, float framerate, SEncParamExt* pParam);
+
+  virtual void prepareEncDecParam (const EncodeDecodeFileParamBase EncDecFileParam);
+
+  virtual void encToDecData (const SFrameBSInfo& info, int& len);
+
+  virtual void encToDecSliceData (const int iLayerNum, const int iSliceNum, const SFrameBSInfo& info, int& len);
+
+  virtual int GetRandWidth() {
+    return WelsClip3 ((((rand() % MAX_WIDTH) >> 1) + 1) << 1, 16, MAX_WIDTH);
+  }
+
+  virtual int GetRandHeight() {
+    return WelsClip3 ((((rand() % MAX_HEIGHT) >> 1) + 1) << 1, 16, MAX_HEIGHT);
+  }
+
+ protected:
+  SEncParamExt   param_;
+  BufferedData   buf_;
+  SSourcePicture EncPic;
+  SFrameBSInfo   info;
+  SBufferInfo    dstBufInfo_;
+  std::vector<SLostSim> m_SLostSim;
+  WelsTraceCallback pFunc;
+  STraceUnit sTrace;
+  STraceUnit* pTraceInfo;
+};
+
+class EncodeDecodeTestAPIBase : public EncodeDecodeTestBase {
+ public:
+  uint8_t iRandValue;
+ public:
+  void SetUp() {
+    EncodeDecodeTestBase::SetUp();
+  }
+
+  void TearDown() {
+    EncodeDecodeTestBase::TearDown();
+  }
+
+  void prepareParam0 (int iLayers, int iSlices, int width, int height, float framerate, SEncParamExt* pParam);
+
+  void prepareParamDefault (int iLayers, int iSlices, int width, int height, float framerate, SEncParamExt* pParam);
+
+  void InitialEncDec (int iWidth, int iHeight);
+  void RandomParamExtCombination();
+  void ValidateParamExtCombination();
+  void SliceParamValidationForMode2 (int iSpatialIdx);
+  void SliceParamValidationForMode3 (int iSpatialIdx);
+  void SliceParamValidationForMode4();
+
+  void EncodeOneFrame (int iCheckTypeIndex);
+  void EncDecOneFrame (const int iWidth, const int iHeight, const int iFrame, FILE* pfEnc);
+  void TestOneSimulcastAVC (SEncParamExt* pParam, ISVCDecoder** decoder, unsigned char** pBsBuf, int iSpatialLayerNum,
+                            int iEncFrameNum,
+                            int iCallTimes);
+};
+
+class EncodeDecodeTestAPI : public ::testing::TestWithParam<EncodeDecodeFileParamBase>, public EncodeDecodeTestAPIBase {
+  void SetUp() {
+    EncodeDecodeTestAPIBase::SetUp();
+  }
+
+  void TearDown() {
+    EncodeDecodeTestAPIBase::TearDown();
+  }
+};
+
+
+
--- /dev/null
+++ b/test/api/encode_decode_api_test.template
@@ -1,0 +1,142 @@
+#include <gtest/gtest.h>
+#include "codec_def.h"
+#include "utils/BufferedData.h"
+#include "utils/FileInputStream.h"
+#include "BaseDecoderTest.h"
+#include "BaseEncoderTest.h"
+#include "wels_common_defs.h"
+#include "utils/HashFunctions.h"
+#include <string>
+#include <vector>
+#include "encode_decode_api_test.h"
+using namespace WelsCommon;
+
+
+TEST_F (EncodeDecodeTestAPI, LogEncoding) {
+#define DEBUG_FILE_SAVE
+  //FillInit
+  //int iSpatialLayerNum = $SPATIAL_NUM;
+  //int iWidth       = $SOURCE_WIDTH;
+  //int iHeight      = $SOURCE_HEIGHT;
+  //int iEncFrameNum = $TOTAL_FRMS;
+
+  //FillFirstParam
+  int rv = encoder_->InitializeExt (&param_);
+  ASSERT_TRUE (rv == cmResultSuccess);
+
+  unsigned char*  pBsBuf[MAX_SPATIAL_LAYER_NUM];
+  int aLen[MAX_SPATIAL_LAYER_NUM] = {0};
+  ISVCDecoder* decoder[MAX_SPATIAL_LAYER_NUM];
+
+#ifdef DEBUG_FILE_SAVE
+  FILE* fEnc[MAX_SPATIAL_LAYER_NUM];
+  fEnc[0] = fopen ("enc0.264", "wb");
+  fEnc[1] = fopen ("enc1.264", "wb");
+  fEnc[2] = fopen ("enc2.264", "wb");
+  fEnc[3] = fopen ("enc3.264", "wb");
+#endif
+
+  int iIdx = 0;
+
+  //create decoder
+  for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
+    pBsBuf[iIdx] = static_cast<unsigned char*> (malloc (iWidth * iHeight * 3 * sizeof (unsigned char) / 2));
+    EXPECT_TRUE (pBsBuf[iIdx] != NULL);
+    aLen[iIdx] = 0;
+
+    long rv = WelsCreateDecoder (&decoder[iIdx]);
+    ASSERT_EQ (0, rv);
+    EXPECT_TRUE (decoder[iIdx] != NULL);
+
+    SDecodingParam decParam;
+    memset (&decParam, 0, sizeof (SDecodingParam));
+    decParam.eOutputColorFormat  = videoFormatI420;
+    decParam.uiTargetDqLayer = UCHAR_MAX;
+    decParam.eEcActiveIdc = ERROR_CON_SLICE_COPY;
+    decParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
+
+    rv = decoder[iIdx]->Initialize (&decParam);
+    ASSERT_EQ (0, rv);
+  }
+
+
+  for (int iFrame = 0; iFrame < iEncFrameNum; iFrame++) {
+    int iResult;
+    int iLayerLen = 0;
+    unsigned char* pData[3] = { NULL };
+
+    //FillParam
+
+    InitialEncDec (param_.iPicWidth, param_.iPicHeight);
+    EncodeOneFrame (0);
+
+    // init
+    for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
+      aLen[iIdx] = 0;
+    }
+    for (int iLayer = 0; iLayer < info.iLayerNum; ++iLayer) {
+      iLayerLen = 0;
+      const SLayerBSInfo& layerInfo = info.sLayerInfo[iLayer];
+      const int kiFirstNalType = ((* (layerInfo.pBsBuf + 4)) & 0x1f);
+      if (param_.bSimulcastAVC) {
+        ASSERT_TRUE ((kiFirstNalType == NAL_SPS) || (kiFirstNalType == NAL_PPS) || (kiFirstNalType == NAL_SLICE)
+                     || (kiFirstNalType == NAL_SLICE_IDR) || (kiFirstNalType == NAL_SEI));
+      }
+      for (int iNal = 0; iNal < layerInfo.iNalCount; ++iNal) {
+        iLayerLen += layerInfo.pNalLengthInByte[iNal];
+      }
+
+      if (param_.bSimulcastAVC) {
+        iIdx = layerInfo.uiSpatialId;
+        EXPECT_TRUE (iIdx < iSpatialLayerNum);
+        memcpy ((pBsBuf[iIdx] + aLen[iIdx]), layerInfo.pBsBuf, iLayerLen * sizeof (unsigned char));
+        aLen[iIdx] += iLayerLen;
+      } else {
+        if (layerInfo.uiLayerType == NON_VIDEO_CODING_LAYER) {
+          // under SimulcastSVC, need to copy non-VCL to all layers
+          for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
+            memcpy ((pBsBuf[iIdx] + aLen[iIdx]), layerInfo.pBsBuf, iLayerLen * sizeof (unsigned char));
+            aLen[iIdx] += iLayerLen;
+          }
+        } else {
+          iIdx = layerInfo.uiSpatialId;
+          EXPECT_TRUE (iIdx < iSpatialLayerNum);
+          memcpy ((pBsBuf[iIdx] + aLen[iIdx]), layerInfo.pBsBuf, iLayerLen * sizeof (unsigned char));
+          aLen[iIdx] += iLayerLen;
+        }
+      }
+    }
+
+    for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
+      pData[0] = pData[1] = pData[2] = 0;
+      memset (&dstBufInfo_, 0, sizeof (SBufferInfo));
+
+      if (aLen[iIdx] > 0) {
+#ifdef DEBUG_FILE_SAVE
+        fwrite (pBsBuf[iIdx], aLen[iIdx], 1, fEnc[iIdx]);
+#endif
+        iResult = decoder[iIdx]->DecodeFrame2 (pBsBuf[iIdx], aLen[iIdx], pData, &dstBufInfo_);
+        EXPECT_TRUE (iResult == cmResultSuccess) << "iResult=" << iResult << "LayerIdx=" << iIdx;
+
+        iResult = decoder[iIdx]->DecodeFrame2 (NULL, 0, pData, &dstBufInfo_);
+        EXPECT_TRUE (iResult == cmResultSuccess) << "iResult=" << iResult << "LayerIdx=" << iIdx;
+        EXPECT_EQ (dstBufInfo_.iBufferStatus, 1) << "LayerIdx=" << iIdx;
+      }
+    }
+  }
+
+
+  for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
+    free (pBsBuf[iIdx]);
+
+    if (decoder[iIdx] != NULL) {
+      decoder[iIdx]->Uninitialize();
+      WelsDestroyDecoder (decoder[iIdx]);
+    }
+  }
+#ifdef DEBUG_FILE_SAVE
+  for (int i = 0; i < MAX_SPATIAL_LAYER_NUM; i++) {
+    fclose (fEnc[i]);
+  }
+#endif
+}
\ No newline at end of file