1 #include <gtest/gtest.h>
2 #include "codec_def.h"
3 #include "utils/BufferedData.h"
4 #include "utils/FileInputStream.h"
5 #include "BaseDecoderTest.h"
6 #include "BaseEncoderTest.h"
7 #include "wels_common_defs.h"
8 #include <string>
9 #include <vector>
10 #include "encode_decode_api_test.h"
11 using namespace WelsCommon;
12
13
prepareParam(int iLayers,int iSlices,int width,int height,float framerate,SEncParamExt * pParam)14 void EncodeDecodeTestBase::prepareParam (int iLayers, int iSlices, int width, int height, float framerate,
15 SEncParamExt* pParam) {
16 pParam->iUsageType = CAMERA_VIDEO_REAL_TIME;
17 pParam->iPicWidth = width;
18 pParam->iPicHeight = height;
19 pParam->fMaxFrameRate = framerate;
20 pParam->iRCMode = RC_OFF_MODE; //rc off
21 pParam->iMultipleThreadIdc = 1; //single thread
22 pParam->iSpatialLayerNum = iLayers;
23 pParam->iNumRefFrame = AUTO_REF_PIC_COUNT;
24 for (int i = 0; i < iLayers; i++) {
25 pParam->sSpatialLayers[i].iVideoWidth = width >> (iLayers - i - 1);
26 pParam->sSpatialLayers[i].iVideoHeight = height >> (iLayers - i - 1);
27 pParam->sSpatialLayers[i].fFrameRate = framerate;
28 pParam->sSpatialLayers[i].sSliceArgument.uiSliceMode = SM_FIXEDSLCNUM_SLICE;
29 pParam->sSpatialLayers[i].sSliceArgument.uiSliceNum = iSlices;
30 }
31 }
32
prepareEncDecParam(const EncodeDecodeFileParamBase EncDecFileParam)33 bool EncodeDecodeTestBase::prepareEncDecParam (const EncodeDecodeFileParamBase EncDecFileParam) {
34 //for encoder
35 //I420: 1(Y) + 1/4(U) + 1/4(V)
36 int frameSize = EncDecFileParam.width * EncDecFileParam.height * 3 / 2;
37 if (-1 == buf_.SetLength(frameSize))
38 return false;
39 if (buf_.Length() != (size_t)frameSize) {
40 printf ("buf_.Length() failed! frameSize = %d\n", frameSize);
41 return false;
42 }
43 memset (&EncPic, 0, sizeof (SSourcePicture));
44 EncPic.iPicWidth = EncDecFileParam.width;
45 EncPic.iPicHeight = EncDecFileParam.height;
46 EncPic.iColorFormat = videoFormatI420;
47 EncPic.iStride[0] = EncPic.iPicWidth;
48 EncPic.iStride[1] = EncPic.iStride[2] = EncPic.iPicWidth >> 1;
49 EncPic.pData[0] = buf_.data();
50 EncPic.pData[1] = EncPic.pData[0] + EncDecFileParam.width * EncDecFileParam.height;
51 EncPic.pData[2] = EncPic.pData[1] + (EncDecFileParam.width * EncDecFileParam.height >> 2);
52 //for decoder
53 memset (&info, 0, sizeof (SFrameBSInfo));
54 //set a fixed random value
55 iRandValue = rand() % 256;
56 return true;
57 }
58
encToDecData(const SFrameBSInfo & info,int & len)59 void EncodeDecodeTestBase::encToDecData (const SFrameBSInfo& info, int& len) {
60 len = 0;
61 for (int i = 0; i < info.iLayerNum; ++i) {
62 const SLayerBSInfo& layerInfo = info.sLayerInfo[i];
63 for (int j = 0; j < layerInfo.iNalCount; ++j) {
64 len += layerInfo.pNalLengthInByte[j];
65 }
66 }
67 }
68
encToDecSliceData(const int iLayerNum,const int iSliceNum,const SFrameBSInfo & info,int & len)69 void EncodeDecodeTestBase::encToDecSliceData (const int iLayerNum, const int iSliceNum, const SFrameBSInfo& info,
70 int& len) {
71 ASSERT_TRUE (iLayerNum < MAX_LAYER_NUM_OF_FRAME);
72 len = 0;
73 const SLayerBSInfo& layerInfo = info.sLayerInfo[iLayerNum];
74 if (iSliceNum < layerInfo.iNalCount)
75 len = layerInfo.pNalLengthInByte[iSliceNum];
76 }
77
prepareParam0(int iLayers,int iSlices,int width,int height,float framerate,SEncParamExt * pParam)78 void EncodeDecodeTestAPIBase::prepareParam0 (int iLayers, int iSlices, int width, int height, float framerate,
79 SEncParamExt* pParam) {
80 memset (pParam, 0, sizeof (SEncParamExt));
81 EncodeDecodeTestBase::prepareParam (iLayers, iSlices, width, height, framerate, pParam);
82 }
83
prepareParamDefault(int iLayers,int iSlices,int width,int height,float framerate,SEncParamExt * pParam)84 void EncodeDecodeTestAPIBase::prepareParamDefault (int iLayers, int iSlices, int width, int height, float framerate,
85 SEncParamExt* pParam) {
86 memset (pParam, 0, sizeof (SEncParamExt));
87 encoder_->GetDefaultParams (pParam);
88 EncodeDecodeTestBase::prepareParam (iLayers, iSlices, VALID_SIZE(width), VALID_SIZE(height), framerate, pParam);
89 }
90
91
EncodeOneFrame(int iCheckTypeIndex)92 void EncodeDecodeTestAPIBase::EncodeOneFrame (int iCheckTypeIndex) {
93 int frameSize = EncPic.iPicWidth * EncPic.iPicHeight * 3 / 2;
94 int lumaSize = EncPic.iPicWidth * EncPic.iPicHeight;
95 memset (buf_.data(), iRandValue, lumaSize);
96 memset (buf_.data() + lumaSize, rand() % 256, (frameSize - lumaSize));
97 int rv = encoder_->EncodeFrame (&EncPic, &info);
98 if (0 == iCheckTypeIndex)
99 ASSERT_TRUE (rv == cmResultSuccess) << rv;
100 else if (1 == iCheckTypeIndex)
101 ASSERT_TRUE (rv == cmResultSuccess || rv == cmUnknownReason);
102 }
103
EncDecOneFrame(const int iWidth,const int iHeight,const int iFrame,FILE * pfEnc)104 bool EncodeDecodeTestAPIBase::EncDecOneFrame (const int iWidth, const int iHeight, const int iFrame, FILE* pfEnc) {
105 int iLen = 0, rv;
106 if (!InitialEncDec (iWidth, iHeight))
107 return false;
108 EncodeOneFrame (iFrame);
109
110 //extract target layer data
111 encToDecData (info, iLen);
112 //call decoder
113 unsigned char* pData[3] = { NULL };
114 memset (&dstBufInfo_, 0, sizeof (SBufferInfo));
115 rv = decoder_->DecodeFrameNoDelay (info.sLayerInfo[0].pBsBuf, iLen, pData, &dstBufInfo_);
116 EXPECT_TRUE (rv == cmResultSuccess) << " rv = " << rv << " iFrameIdx = " << iFrame;
117 if (NULL != pfEnc) {
118 fwrite (info.sLayerInfo[0].pBsBuf, iLen, 1, pfEnc);
119 }
120 return true;
121 }
122
TestOneSimulcastAVC(SEncParamExt * pParam,ISVCDecoder ** decoder,unsigned char ** pBsBuf,int iSpatialLayerNum,int iEncFrameNum,int iSaveFileIdx)123 bool EncodeDecodeTestAPIBase::TestOneSimulcastAVC (SEncParamExt* pParam, ISVCDecoder** decoder, unsigned char** pBsBuf,
124 int iSpatialLayerNum,
125 int iEncFrameNum,
126 int iSaveFileIdx) {
127 int aLen[MAX_SPATIAL_LAYER_NUM] = {0, 0, 0, 0};
128
129 FILE* fEnc[MAX_SPATIAL_LAYER_NUM];
130 if (iSaveFileIdx == 1) {
131 fEnc[0] = fopen ("enc00.264", "wb");
132 fEnc[1] = fopen ("enc01.264", "wb");
133 fEnc[2] = fopen ("enc02.264", "wb");
134 fEnc[3] = fopen ("enc03.264", "wb");
135 } else if (iSaveFileIdx > 1) {
136 fEnc[0] = fopen ("enc10.264", "wb");
137 fEnc[1] = fopen ("enc11.264", "wb");
138 fEnc[2] = fopen ("enc12.264", "wb");
139 fEnc[3] = fopen ("enc13.264", "wb");
140 }
141
142 int rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, pParam);
143 if (rv != cmResultSuccess) {
144 printf ("SetOption Failed, rv = %d\n", rv);
145 return false;
146 }
147
148 int iIdx;
149 //begin testing
150 for (int iFrame = 0; iFrame < iEncFrameNum; iFrame++) {
151 int iResult;
152 int iLayerLen = 0;
153 unsigned char* pData[3] = { NULL };
154
155 if (!InitialEncDec (pParam->iPicWidth, pParam->iPicHeight))
156 return false;
157 EncodeOneFrame (0);
158
159 // init
160 for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
161 aLen[iIdx] = 0;
162 }
163 for (int iLayer = 0; iLayer < info.iLayerNum; ++iLayer) {
164 iLayerLen = 0;
165 const SLayerBSInfo& layerInfo = info.sLayerInfo[iLayer];
166 for (int iNal = 0; iNal < layerInfo.iNalCount; ++iNal) {
167 iLayerLen += layerInfo.pNalLengthInByte[iNal];
168 }
169
170 iIdx = layerInfo.uiSpatialId;
171 EXPECT_TRUE (iIdx < iSpatialLayerNum) << "iIdx = " << iIdx << ", iSpatialLayerNum = " << iSpatialLayerNum;
172 memcpy ((pBsBuf[iIdx] + aLen[iIdx]), layerInfo.pBsBuf, iLayerLen * sizeof (unsigned char));
173 aLen[iIdx] += iLayerLen;
174 }
175
176 for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
177 pData[0] = pData[1] = pData[2] = 0;
178 memset (&dstBufInfo_, 0, sizeof (SBufferInfo));
179
180 if (iSaveFileIdx > 0) {
181 fwrite (pBsBuf[iIdx], aLen[iIdx], 1, fEnc[iIdx]);
182 }
183
184 iResult = decoder[iIdx]->DecodeFrame2 (pBsBuf[iIdx], aLen[iIdx], pData, &dstBufInfo_);
185 EXPECT_TRUE (iResult == cmResultSuccess) << "iResult=" << iResult << ", LayerIdx=" << iIdx;
186
187 iResult = decoder[iIdx]->DecodeFrame2 (NULL, 0, pData, &dstBufInfo_);
188 EXPECT_TRUE (iResult == cmResultSuccess) << "iResult=" << iResult << ", LayerIdx=" << iIdx;
189 EXPECT_EQ (dstBufInfo_.iBufferStatus, 1) << "LayerIdx=" << iIdx;
190 }
191 }
192
193 if (iSaveFileIdx > 0) {
194 fclose (fEnc[0]);
195 fclose (fEnc[1]);
196 fclose (fEnc[2]);
197 fclose (fEnc[3]);
198 }
199 return true;
200 }
201
IsKeyFrameLost(ISVCDecoder * pDecoder,SLTRRecoverRequest * p_LTR_Recover_Request,long hr)202 long IsKeyFrameLost (ISVCDecoder* pDecoder, SLTRRecoverRequest* p_LTR_Recover_Request, long hr) {
203 long bLost = NO_RECOVERY_REQUSET;
204 int tempInt = -1;
205 int temple_id = -1;
206 bool m_P2PmodeFlag = true;
207 pDecoder->GetOption (DECODER_OPTION_TEMPORAL_ID, &temple_id);
208 if (hr == dsErrorFree) {
209 if (m_P2PmodeFlag && temple_id == 0) {
210 pDecoder->GetOption (DECODER_OPTION_IDR_PIC_ID, &tempInt);
211 // idr_pic_id change ,reset last correct position
212 if (p_LTR_Recover_Request->uiIDRPicId != (unsigned int) tempInt) {
213 p_LTR_Recover_Request->uiIDRPicId = tempInt;
214 p_LTR_Recover_Request->iLastCorrectFrameNum = -1;
215 }
216 pDecoder->GetOption (DECODER_OPTION_FRAME_NUM, &tempInt);
217 if (tempInt >= 0) {
218 p_LTR_Recover_Request->iLastCorrectFrameNum = tempInt;
219 }
220 }
221 bLost = NO_RECOVERY_REQUSET;
222 } else if (hr & dsNoParamSets) {
223 bLost = IDR_RECOVERY_REQUEST;
224 } else if (((hr & dsRefLost) && (1 == temple_id)) || ((dsErrorFree != hr) && (0 == temple_id))) {
225 bLost = LTR_RECOVERY_REQUEST;
226 } else {
227 bLost = NO_RECOVERY_REQUSET;
228 }
229 return bLost;
230 }
231
IsLTRMarking(ISVCDecoder * pDecoder)232 bool IsLTRMarking (ISVCDecoder* pDecoder) {
233 int bLTR_marking_flag = 0;
234 pDecoder->GetOption (DECODER_OPTION_LTR_MARKING_FLAG, &bLTR_marking_flag);
235 return (bLTR_marking_flag) ? (true) : (false);
236 }
237
LTRRecoveryRequest(ISVCDecoder * pDecoder,ISVCEncoder * pEncoder,SLTRRecoverRequest * p_LTR_Recover_Request,long hr,bool m_P2PmodeFlag)238 void LTRRecoveryRequest (ISVCDecoder* pDecoder, ISVCEncoder* pEncoder, SLTRRecoverRequest* p_LTR_Recover_Request,
239 long hr, bool m_P2PmodeFlag) {
240 long bKLost = IsKeyFrameLost (pDecoder, p_LTR_Recover_Request, hr);
241 if (m_P2PmodeFlag) {
242 if (bKLost == IDR_RECOVERY_REQUEST) {
243 pEncoder->ForceIntraFrame (true);
244 } else if (bKLost == LTR_RECOVERY_REQUEST) {
245 p_LTR_Recover_Request->uiFeedbackType = LTR_RECOVERY_REQUEST;
246 pDecoder->GetOption (DECODER_OPTION_FRAME_NUM, &p_LTR_Recover_Request->iCurrentFrameNum);
247 pDecoder->GetOption (DECODER_OPTION_IDR_PIC_ID, &p_LTR_Recover_Request->uiIDRPicId);
248 pEncoder->SetOption (ENCODER_LTR_RECOVERY_REQUEST, p_LTR_Recover_Request);
249 }
250 } else {
251 if (bKLost == IDR_RECOVERY_REQUEST || bKLost == LTR_RECOVERY_REQUEST) {
252 p_LTR_Recover_Request->uiFeedbackType = IDR_RECOVERY_REQUEST;
253 pEncoder->ForceIntraFrame (true);
254 }
255 }
256 }
257
LTRMarkFeedback(ISVCDecoder * pDecoder,ISVCEncoder * pEncoder,SLTRMarkingFeedback * p_LTR_Marking_Feedback,long hr)258 void LTRMarkFeedback (ISVCDecoder* pDecoder, ISVCEncoder* pEncoder, SLTRMarkingFeedback* p_LTR_Marking_Feedback,
259 long hr) {
260 if (IsLTRMarking (pDecoder) == true) {
261 p_LTR_Marking_Feedback->uiFeedbackType = (hr == dsErrorFree) ? (LTR_MARKING_SUCCESS) : (LTR_MARKING_FAILED);
262 pDecoder->GetOption (DECODER_OPTION_IDR_PIC_ID, &p_LTR_Marking_Feedback->uiIDRPicId);
263 pDecoder->GetOption (DECODER_OPTION_LTR_MARKED_FRAME_NUM, &p_LTR_Marking_Feedback->iLTRFrameNum);
264 pEncoder->SetOption (ENCODER_LTR_MARKING_FEEDBACK, p_LTR_Marking_Feedback);
265 }
266 }
267
ToRemainDidNal(const unsigned char * pSrc,EWelsNalUnitType eNalType,int iTarDid)268 bool ToRemainDidNal (const unsigned char* pSrc, EWelsNalUnitType eNalType, int iTarDid) {
269 uint8_t uiCurByte = *pSrc;
270 if (IS_NEW_INTRODUCED_SVC_NAL (eNalType)) {
271 int iDid = (uiCurByte & 0x70) >> 4;
272 return iDid == iTarDid;
273 } else if ((IS_VCL_NAL_AVC_BASE (eNalType)) && iTarDid != 0) {
274 return false;
275 } else {
276 return true;
277 }
278 }
279
ExtractDidNal(SFrameBSInfo * pBsInfo,int & iSrcLen,std::vector<SLostSim> * p_SLostSim,int iTarDid)280 void ExtractDidNal (SFrameBSInfo* pBsInfo, int& iSrcLen, std::vector<SLostSim>* p_SLostSim, int iTarDid) {
281 unsigned char* pDst = new unsigned char[iSrcLen];
282 const unsigned char* pSrc = pBsInfo->sLayerInfo[0].pBsBuf;
283 int iDstLen = 0;
284 bool bLost;
285 SLostSim tmpSLostSim;
286 p_SLostSim->clear();
287 int iPrefix;
288 unsigned char* pSrcPtr = pBsInfo->sLayerInfo[0].pBsBuf;
289 for (int j = 0; j < pBsInfo->iLayerNum; j++) {
290 for (int k = 0; k < pBsInfo->sLayerInfo[j].iNalCount; k++) {
291 if (pSrcPtr[0] == 0 && pSrcPtr[1] == 0 && pSrcPtr[2] == 0 && pSrcPtr[3] == 1) {
292 iPrefix = 4;
293 } else if (pSrcPtr[0] == 0 && pSrcPtr[1] == 0 && pSrcPtr[2] == 1) {
294 iPrefix = 3;
295 } else {
296 iPrefix = 0;
297 }
298 tmpSLostSim.eNalType = (EWelsNalUnitType) ((* (pSrcPtr + iPrefix)) & 0x1f); // eNalUnitType
299 bLost = (ToRemainDidNal ((pSrcPtr + iPrefix + 2), tmpSLostSim.eNalType, iTarDid)) ? false : true;
300 tmpSLostSim.isLost = bLost;
301 p_SLostSim->push_back (tmpSLostSim);
302 if (!bLost) {
303 memcpy (pDst + iDstLen, pSrcPtr, pBsInfo->sLayerInfo[j].pNalLengthInByte[k]);
304 iDstLen += (pBsInfo->sLayerInfo[j].pNalLengthInByte[k]);
305 }
306 pSrcPtr += pBsInfo->sLayerInfo[j].pNalLengthInByte[k];
307 }
308 }
309 memset ((void*)pSrc, 0, iSrcLen);
310 memcpy ((void*)pSrc, pDst, iDstLen);
311 iSrcLen = iDstLen;
312 delete [] pDst;
313 }
314
SimulateNALLoss(const unsigned char * pSrc,int & iSrcLen,std::vector<SLostSim> * p_SLostSim,const char * pLossChars,bool bLossPara,int & iLossIdx,bool & bVCLLoss)315 int SimulateNALLoss (const unsigned char* pSrc, int& iSrcLen, std::vector<SLostSim>* p_SLostSim,
316 const char* pLossChars, bool bLossPara, int& iLossIdx, bool& bVCLLoss) {
317 unsigned char* pDst = new unsigned char[iSrcLen];
318 int iLossCharLen = (int) strlen (pLossChars);
319 int iSkipedBytes = 0;
320 int iDstLen = 0;
321 int iBufPos = 0;
322 int ilastprefixlen = 0;
323 int i = 0;
324 bool bLost;
325 bVCLLoss = false;
326 SLostSim tmpSLostSim;
327 p_SLostSim->clear();
328 for (i = 0; i < iSrcLen;) {
329 if (pSrc[i] == 0 && pSrc[i + 1] == 0 && pSrc[i + 2] == 0 && pSrc[i + 3] == 1) {
330 if (i - iBufPos) {
331 tmpSLostSim.eNalType = (EWelsNalUnitType) ((* (pSrc + iBufPos + ilastprefixlen)) & 0x1f); // eNalUnitType
332 bLost = iLossIdx < iLossCharLen ? (pLossChars[iLossIdx] == '1') : (rand() % 2 == 1);
333 bLost = (!bLossPara) && (IS_PARAM_SETS_NALS (tmpSLostSim.eNalType)) ? false : bLost;
334 iLossIdx++;
335 tmpSLostSim.isLost = bLost;
336 p_SLostSim->push_back (tmpSLostSim);
337 if (!bLost) {
338 memcpy (pDst + iDstLen, pSrc + iBufPos, i - iBufPos);
339 iDstLen += (i - iBufPos);
340 } else {
341 bVCLLoss = (IS_VCL_NAL (tmpSLostSim.eNalType, 1)) ? true : bVCLLoss;
342 iSkipedBytes += (i - iBufPos);
343 }
344 }
345 ilastprefixlen = 4;
346 iBufPos = i;
347 i = i + 4;
348 } else if (pSrc[i] == 0 && pSrc[i + 1] == 0 && pSrc[i + 2] == 1) {
349 if (i - iBufPos) {
350 tmpSLostSim.eNalType = (EWelsNalUnitType) ((* (pSrc + iBufPos + ilastprefixlen)) & 0x1f); // eNalUnitType
351 bLost = iLossIdx < iLossCharLen ? (pLossChars[iLossIdx] == '1') : (rand() % 2 == 1);
352 bLost = (!bLossPara) && (IS_PARAM_SETS_NALS (tmpSLostSim.eNalType)) ? false : bLost;
353 iLossIdx++;
354 tmpSLostSim.isLost = bLost;
355 p_SLostSim->push_back (tmpSLostSim);
356 if (!bLost) {
357 memcpy (pDst + iDstLen, pSrc + iBufPos, i - iBufPos);
358 iDstLen += (i - iBufPos);
359 } else {
360 bVCLLoss = (IS_VCL_NAL (tmpSLostSim.eNalType, 1)) ? true : bVCLLoss;
361 iSkipedBytes += (i - iBufPos);
362 }
363 }
364 ilastprefixlen = 3;
365 iBufPos = i;
366 i = i + 3;
367 } else {
368 i++;
369 }
370 }
371 if (i - iBufPos) {
372 tmpSLostSim.eNalType = (EWelsNalUnitType) ((* (pSrc + iBufPos + ilastprefixlen)) & 0x1f); // eNalUnitType
373 bLost = iLossIdx < iLossCharLen ? (pLossChars[iLossIdx] == '1') : (rand() % 2 == 1);
374 bLost = (!bLossPara) && (IS_PARAM_SETS_NALS (tmpSLostSim.eNalType)) ? false : bLost;
375 iLossIdx++;
376 tmpSLostSim.isLost = bLost;
377 p_SLostSim->push_back (tmpSLostSim);
378 if (!bLost) {
379 memcpy (pDst + iDstLen, pSrc + iBufPos, i - iBufPos);
380 iDstLen += (i - iBufPos);
381 } else {
382 bVCLLoss = (IS_VCL_NAL (tmpSLostSim.eNalType, 1)) ? true : bVCLLoss;
383 iSkipedBytes += (i - iBufPos);
384 }
385 }
386 memset ((void*)pSrc, 0, iSrcLen);
387 memcpy ((void*)pSrc, pDst, iDstLen);
388 iSrcLen = iDstLen;
389 delete [] pDst;
390 return iSkipedBytes;
391 }
392
393
394