1 #include <fstream>
2 #include <gtest/gtest.h>
3 #include "codec_def.h"
4 #include "utils/BufferedData.h"
5 #include "utils/FileInputStream.h"
6 #include "BaseEncoderTest.h"
7
InitWithParam(ISVCEncoder * encoder,SEncParamExt * pEncParamExt)8 static int InitWithParam (ISVCEncoder* encoder, SEncParamExt* pEncParamExt) {
9
10 SliceModeEnum eSliceMode = pEncParamExt->sSpatialLayers[0].sSliceArgument.uiSliceMode;
11 bool bBaseParamFlag = (SM_SINGLE_SLICE == eSliceMode && !pEncParamExt->bEnableDenoise
12 && pEncParamExt->iSpatialLayerNum == 1 && !pEncParamExt->bIsLosslessLink
13 && !pEncParamExt->bEnableLongTermReference && !pEncParamExt->iEntropyCodingModeFlag) ? true : false;
14 if (bBaseParamFlag) {
15 SEncParamBase param;
16 memset (¶m, 0, sizeof (SEncParamBase));
17
18 param.iUsageType = pEncParamExt->iUsageType;
19 param.fMaxFrameRate = pEncParamExt->fMaxFrameRate;
20 param.iPicWidth = pEncParamExt->iPicWidth;
21 param.iPicHeight = pEncParamExt->iPicHeight;
22 param.iTargetBitrate = 5000000;
23 return encoder->Initialize (¶m);
24 } else {
25 SEncParamExt param;
26 encoder->GetDefaultParams (¶m);
27
28 param.iUsageType = pEncParamExt->iUsageType;
29 param.fMaxFrameRate = pEncParamExt->fMaxFrameRate;
30 param.iPicWidth = pEncParamExt->iPicWidth;
31 param.iPicHeight = pEncParamExt->iPicHeight;
32 param.iTargetBitrate = 5000000;
33 param.bEnableDenoise = pEncParamExt->bEnableDenoise;
34 param.iSpatialLayerNum = pEncParamExt->iSpatialLayerNum;
35 param.bIsLosslessLink = pEncParamExt->bIsLosslessLink;
36 param.bEnableLongTermReference = pEncParamExt->bEnableLongTermReference;
37 param.iEntropyCodingModeFlag = pEncParamExt->iEntropyCodingModeFlag ? 1 : 0;
38 if (eSliceMode != SM_SINGLE_SLICE
39 && eSliceMode != SM_SIZELIMITED_SLICE) //SM_SIZELIMITED_SLICE don't support multi-thread now
40 param.iMultipleThreadIdc = 2;
41
42 for (int i = 0; i < param.iSpatialLayerNum; i++) {
43 param.sSpatialLayers[i].iVideoWidth = pEncParamExt->iPicWidth >> (param.iSpatialLayerNum - 1 - i);
44 param.sSpatialLayers[i].iVideoHeight = pEncParamExt->iPicHeight >> (param.iSpatialLayerNum - 1 - i);
45 param.sSpatialLayers[i].fFrameRate = pEncParamExt->fMaxFrameRate;
46 param.sSpatialLayers[i].iSpatialBitrate = param.iTargetBitrate;
47
48 param.sSpatialLayers[i].sSliceArgument.uiSliceMode = eSliceMode;
49 if (eSliceMode == SM_SIZELIMITED_SLICE) {
50 param.sSpatialLayers[i].sSliceArgument.uiSliceSizeConstraint = 600;
51 param.uiMaxNalSize = 1500;
52 param.iMultipleThreadIdc = 4;
53 param.bUseLoadBalancing = false;
54 }
55 if (eSliceMode == SM_FIXEDSLCNUM_SLICE) {
56 param.sSpatialLayers[i].sSliceArgument.uiSliceNum = 4;
57 param.iMultipleThreadIdc = 4;
58 param.bUseLoadBalancing = false;
59 }
60 if (param.iEntropyCodingModeFlag) {
61 param.sSpatialLayers[i].uiProfileIdc = PRO_MAIN;
62 }
63 }
64 param.iTargetBitrate *= param.iSpatialLayerNum;
65 return encoder->InitializeExt (¶m);
66 }
67 }
68
BaseEncoderTest()69 BaseEncoderTest::BaseEncoderTest() : encoder_ (NULL) {}
70
SetUp()71 void BaseEncoderTest::SetUp() {
72 int rv = WelsCreateSVCEncoder (&encoder_);
73 ASSERT_EQ (0, rv);
74 ASSERT_TRUE (encoder_ != NULL);
75
76 unsigned int uiTraceLevel = WELS_LOG_ERROR;
77 encoder_->SetOption (ENCODER_OPTION_TRACE_LEVEL, &uiTraceLevel);
78 }
79
TearDown()80 void BaseEncoderTest::TearDown() {
81 if (encoder_) {
82 encoder_->Uninitialize();
83 WelsDestroySVCEncoder (encoder_);
84 }
85 }
86
EncodeStream(InputStream * in,SEncParamExt * pEncParamExt,Callback * cbk)87 void BaseEncoderTest::EncodeStream (InputStream* in, SEncParamExt* pEncParamExt, Callback* cbk) {
88
89 ASSERT_TRUE (NULL != pEncParamExt);
90
91 int rv = InitWithParam (encoder_, pEncParamExt);
92 ASSERT_TRUE (rv == cmResultSuccess);
93
94 // I420: 1(Y) + 1/4(U) + 1/4(V)
95 int frameSize = pEncParamExt->iPicWidth * pEncParamExt->iPicHeight * 3 / 2;
96
97 BufferedData buf;
98 buf.SetLength (frameSize);
99 ASSERT_TRUE (buf.Length() == (size_t)frameSize); //include memory fail (-1) case
100
101 SFrameBSInfo info;
102 memset (&info, 0, sizeof (SFrameBSInfo));
103
104 SSourcePicture pic;
105 memset (&pic, 0, sizeof (SSourcePicture));
106 pic.iPicWidth = pEncParamExt->iPicWidth;
107 pic.iPicHeight = pEncParamExt->iPicHeight;
108 pic.iColorFormat = videoFormatI420;
109 pic.iStride[0] = pic.iPicWidth;
110 pic.iStride[1] = pic.iStride[2] = pic.iPicWidth >> 1;
111 pic.pData[0] = buf.data();
112 pic.pData[1] = pic.pData[0] + pEncParamExt->iPicWidth * pEncParamExt->iPicHeight;
113 pic.pData[2] = pic.pData[1] + (pEncParamExt->iPicWidth * pEncParamExt->iPicHeight >> 2);
114 while (in->read (buf.data(), frameSize) == frameSize) {
115 rv = encoder_->EncodeFrame (&pic, &info);
116 ASSERT_TRUE (rv == cmResultSuccess);
117 if (info.eFrameType != videoFrameTypeSkip && cbk != NULL) {
118 cbk->onEncodeFrame (info);
119 }
120 }
121 }
122
EncodeFile(const char * fileName,SEncParamExt * pEncParamExt,Callback * cbk)123 void BaseEncoderTest::EncodeFile (const char* fileName, SEncParamExt* pEncParamExt, Callback* cbk) {
124 FileInputStream fileStream;
125 ASSERT_TRUE (fileStream.Open (fileName));
126 ASSERT_TRUE (NULL != pEncParamExt);
127 EncodeStream (&fileStream, pEncParamExt, cbk);
128 }
129