• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
104 
EncDecOneFrame(const int iWidth,const int iHeight,const int iFrame,FILE * pfEnc)105 bool EncodeDecodeTestAPIBase::EncDecOneFrame (const int iWidth, const int iHeight, const int iFrame, FILE* pfEnc) {
106   int iLen = 0, rv;
107   if (!InitialEncDec (iWidth, iHeight))
108     return false;
109   EncodeOneFrame (iFrame);
110 
111   //extract target layer data
112   encToDecData (info, iLen);
113   //call decoder
114   unsigned char* pData[3] = { NULL };
115   memset (&dstBufInfo_, 0, sizeof (SBufferInfo));
116   rv = decoder_->DecodeFrameNoDelay (info.sLayerInfo[0].pBsBuf, iLen, pData, &dstBufInfo_);
117   EXPECT_TRUE (rv == cmResultSuccess) << " rv = " << rv << " iFrameIdx = " << iFrame;
118   if (NULL != pfEnc) {
119     fwrite (info.sLayerInfo[0].pBsBuf, iLen, 1, pfEnc);
120   }
121   return true;
122 }
123 
TestOneSimulcastAVC(SEncParamExt * pParam,ISVCDecoder ** decoder,unsigned char ** pBsBuf,int iSpatialLayerNum,int iEncFrameNum,int iSaveFileIdx)124 bool EncodeDecodeTestAPIBase::TestOneSimulcastAVC (SEncParamExt* pParam, ISVCDecoder** decoder, unsigned char** pBsBuf,
125     int iSpatialLayerNum,
126     int iEncFrameNum,
127     int iSaveFileIdx) {
128   int aLen[MAX_SPATIAL_LAYER_NUM] = {0, 0, 0, 0};
129 
130   FILE* fEnc[MAX_SPATIAL_LAYER_NUM];
131   if (iSaveFileIdx == 1) {
132     fEnc[0] = fopen ("enc00.264", "wb");
133     fEnc[1] = fopen ("enc01.264", "wb");
134     fEnc[2] = fopen ("enc02.264", "wb");
135     fEnc[3] = fopen ("enc03.264", "wb");
136   } else if (iSaveFileIdx > 1) {
137     fEnc[0] = fopen ("enc10.264", "wb");
138     fEnc[1] = fopen ("enc11.264", "wb");
139     fEnc[2] = fopen ("enc12.264", "wb");
140     fEnc[3] = fopen ("enc13.264", "wb");
141   }
142 
143   int rv = encoder_->SetOption (ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, pParam);
144   if (rv != cmResultSuccess) {
145     printf ("SetOption Failed, rv = %d\n", rv);
146     return false;
147   }
148 
149   int iIdx;
150   //begin testing
151   for (int iFrame = 0; iFrame < iEncFrameNum; iFrame++) {
152     int iResult;
153     int iLayerLen = 0;
154     unsigned char* pData[3] = { NULL };
155 
156     if (!InitialEncDec (pParam->iPicWidth, pParam->iPicHeight))
157       return false;
158     EncodeOneFrame (0);
159 
160     // init
161     for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
162       aLen[iIdx] = 0;
163     }
164     for (int iLayer = 0; iLayer < info.iLayerNum; ++iLayer) {
165       iLayerLen = 0;
166       const SLayerBSInfo& layerInfo = info.sLayerInfo[iLayer];
167       for (int iNal = 0; iNal < layerInfo.iNalCount; ++iNal) {
168         iLayerLen += layerInfo.pNalLengthInByte[iNal];
169       }
170 
171       iIdx = layerInfo.uiSpatialId;
172       EXPECT_TRUE (iIdx < iSpatialLayerNum) << "iIdx = " << iIdx << ", iSpatialLayerNum = " << iSpatialLayerNum;
173       memcpy ((pBsBuf[iIdx] + aLen[iIdx]), layerInfo.pBsBuf, iLayerLen * sizeof (unsigned char));
174       aLen[iIdx] += iLayerLen;
175     }
176 
177     for (iIdx = 0; iIdx < iSpatialLayerNum; iIdx++) {
178       pData[0] = pData[1] = pData[2] = 0;
179       memset (&dstBufInfo_, 0, sizeof (SBufferInfo));
180 
181       if (iSaveFileIdx > 0) {
182         fwrite (pBsBuf[iIdx], aLen[iIdx], 1, fEnc[iIdx]);
183       }
184 
185       iResult = decoder[iIdx]->DecodeFrame2 (pBsBuf[iIdx], aLen[iIdx], pData, &dstBufInfo_);
186       EXPECT_TRUE (iResult == cmResultSuccess) << "iResult=" << iResult << ", LayerIdx=" << iIdx;
187 
188       iResult = decoder[iIdx]->DecodeFrame2 (NULL, 0, pData, &dstBufInfo_);
189       EXPECT_TRUE (iResult == cmResultSuccess) << "iResult=" << iResult << ", LayerIdx=" << iIdx;
190       EXPECT_EQ (dstBufInfo_.iBufferStatus, 1) << "LayerIdx=" << iIdx;
191     }
192   }
193 
194   if (iSaveFileIdx > 0) {
195     fclose (fEnc[0]);
196     fclose (fEnc[1]);
197     fclose (fEnc[2]);
198     fclose (fEnc[3]);
199   }
200   return true;
201 }
202 
IsKeyFrameLost(ISVCDecoder * pDecoder,SLTRRecoverRequest * p_LTR_Recover_Request,long hr)203 long IsKeyFrameLost (ISVCDecoder* pDecoder, SLTRRecoverRequest* p_LTR_Recover_Request, long hr) {
204   long bLost = NO_RECOVERY_REQUSET;
205   int tempInt = -1;
206   int temple_id = -1;
207   bool m_P2PmodeFlag = true;
208   pDecoder->GetOption (DECODER_OPTION_TEMPORAL_ID, &temple_id);
209   if (hr == dsErrorFree) {
210     if (m_P2PmodeFlag && temple_id == 0) {
211       pDecoder->GetOption (DECODER_OPTION_IDR_PIC_ID, &tempInt);
212       // idr_pic_id change ,reset last correct position
213       if (p_LTR_Recover_Request->uiIDRPicId != (unsigned int) tempInt) {
214         p_LTR_Recover_Request->uiIDRPicId = tempInt;
215         p_LTR_Recover_Request->iLastCorrectFrameNum = -1;
216       }
217       pDecoder->GetOption (DECODER_OPTION_FRAME_NUM, &tempInt);
218       if (tempInt >= 0) {
219         p_LTR_Recover_Request->iLastCorrectFrameNum = tempInt;
220       }
221     }
222     bLost = NO_RECOVERY_REQUSET;
223   } else if (hr & dsNoParamSets) {
224     bLost = IDR_RECOVERY_REQUEST;
225   } else if (((hr & dsRefLost) && (1 == temple_id)) || ((dsErrorFree != hr) && (0 == temple_id))) {
226     bLost = LTR_RECOVERY_REQUEST;
227   } else {
228     bLost = NO_RECOVERY_REQUSET;
229   }
230   return bLost;
231 }
232 
IsLTRMarking(ISVCDecoder * pDecoder)233 bool IsLTRMarking (ISVCDecoder* pDecoder) {
234   int bLTR_marking_flag = 0;
235   pDecoder->GetOption (DECODER_OPTION_LTR_MARKING_FLAG, &bLTR_marking_flag);
236   return (bLTR_marking_flag) ? (true) : (false);
237 }
238 
LTRRecoveryRequest(ISVCDecoder * pDecoder,ISVCEncoder * pEncoder,SLTRRecoverRequest * p_LTR_Recover_Request,long hr,bool m_P2PmodeFlag)239 void LTRRecoveryRequest (ISVCDecoder* pDecoder, ISVCEncoder* pEncoder, SLTRRecoverRequest* p_LTR_Recover_Request,
240                          long hr, bool m_P2PmodeFlag) {
241   long bKLost = IsKeyFrameLost (pDecoder, p_LTR_Recover_Request, hr);
242   if (m_P2PmodeFlag) {
243     if (bKLost == IDR_RECOVERY_REQUEST) {
244       pEncoder->ForceIntraFrame (true);
245     } else if (bKLost == LTR_RECOVERY_REQUEST) {
246       p_LTR_Recover_Request->uiFeedbackType = LTR_RECOVERY_REQUEST;
247       pDecoder->GetOption (DECODER_OPTION_FRAME_NUM, &p_LTR_Recover_Request->iCurrentFrameNum);
248       pDecoder->GetOption (DECODER_OPTION_IDR_PIC_ID, &p_LTR_Recover_Request->uiIDRPicId);
249       pEncoder->SetOption (ENCODER_LTR_RECOVERY_REQUEST, p_LTR_Recover_Request);
250     }
251   } else {
252     if (bKLost == IDR_RECOVERY_REQUEST || bKLost == LTR_RECOVERY_REQUEST) {
253       p_LTR_Recover_Request->uiFeedbackType = IDR_RECOVERY_REQUEST;
254       pEncoder->ForceIntraFrame (true);
255     }
256   }
257 }
258 
LTRMarkFeedback(ISVCDecoder * pDecoder,ISVCEncoder * pEncoder,SLTRMarkingFeedback * p_LTR_Marking_Feedback,long hr)259 void LTRMarkFeedback (ISVCDecoder* pDecoder, ISVCEncoder* pEncoder, SLTRMarkingFeedback* p_LTR_Marking_Feedback,
260                       long hr) {
261   if (IsLTRMarking (pDecoder) == true) {
262     p_LTR_Marking_Feedback->uiFeedbackType = (hr == dsErrorFree) ? (LTR_MARKING_SUCCESS) : (LTR_MARKING_FAILED);
263     pDecoder->GetOption (DECODER_OPTION_IDR_PIC_ID, &p_LTR_Marking_Feedback->uiIDRPicId);
264     pDecoder->GetOption (DECODER_OPTION_LTR_MARKED_FRAME_NUM, &p_LTR_Marking_Feedback->iLTRFrameNum);
265     pEncoder->SetOption (ENCODER_LTR_MARKING_FEEDBACK, p_LTR_Marking_Feedback);
266   }
267 }
268 
ToRemainDidNal(const unsigned char * pSrc,EWelsNalUnitType eNalType,int iTarDid)269 bool ToRemainDidNal (const unsigned char* pSrc, EWelsNalUnitType eNalType, int iTarDid) {
270   uint8_t uiCurByte = *pSrc;
271   if (IS_NEW_INTRODUCED_SVC_NAL (eNalType)) {
272     int iDid = (uiCurByte & 0x70) >> 4;
273     return iDid == iTarDid;
274   } else if ((IS_VCL_NAL_AVC_BASE (eNalType)) && iTarDid != 0) {
275     return false;
276   } else {
277     return true;
278   }
279 }
280 
ExtractDidNal(SFrameBSInfo * pBsInfo,int & iSrcLen,std::vector<SLostSim> * p_SLostSim,int iTarDid)281 void ExtractDidNal (SFrameBSInfo* pBsInfo, int& iSrcLen, std::vector<SLostSim>* p_SLostSim, int iTarDid) {
282   unsigned char* pDst = new unsigned char[iSrcLen];
283   const unsigned char* pSrc = pBsInfo->sLayerInfo[0].pBsBuf;
284   int iDstLen = 0;
285   bool bLost;
286   SLostSim tmpSLostSim;
287   p_SLostSim->clear();
288   int iPrefix;
289   unsigned char* pSrcPtr = pBsInfo->sLayerInfo[0].pBsBuf;
290   for (int j = 0; j < pBsInfo->iLayerNum; j++) {
291     for (int k = 0; k < pBsInfo->sLayerInfo[j].iNalCount; k++) {
292       if (pSrcPtr[0] == 0 && pSrcPtr[1] == 0 && pSrcPtr[2] == 0 && pSrcPtr[3] == 1) {
293         iPrefix = 4;
294       } else if (pSrcPtr[0] == 0 && pSrcPtr[1] == 0 && pSrcPtr[2] == 1) {
295         iPrefix = 3;
296       } else {
297         iPrefix = 0;
298       }
299       tmpSLostSim.eNalType = (EWelsNalUnitType) ((* (pSrcPtr + iPrefix)) & 0x1f); // eNalUnitType
300       bLost = (ToRemainDidNal ((pSrcPtr + iPrefix + 2), tmpSLostSim.eNalType, iTarDid)) ? false : true;
301       tmpSLostSim.isLost = bLost;
302       p_SLostSim->push_back (tmpSLostSim);
303       if (!bLost) {
304         memcpy (pDst + iDstLen, pSrcPtr, pBsInfo->sLayerInfo[j].pNalLengthInByte[k]);
305         iDstLen += (pBsInfo->sLayerInfo[j].pNalLengthInByte[k]);
306       }
307       pSrcPtr += pBsInfo->sLayerInfo[j].pNalLengthInByte[k];
308     }
309   }
310   memset ((void*)pSrc, 0, iSrcLen);
311   memcpy ((void*)pSrc, pDst, iDstLen);
312   iSrcLen = iDstLen;
313   delete [] pDst;
314 }
315 
SimulateNALLoss(const unsigned char * pSrc,int & iSrcLen,std::vector<SLostSim> * p_SLostSim,const char * pLossChars,bool bLossPara,int & iLossIdx,bool & bVCLLoss)316 int SimulateNALLoss (const unsigned char* pSrc,  int& iSrcLen, std::vector<SLostSim>* p_SLostSim,
317                      const char* pLossChars, bool bLossPara, int& iLossIdx, bool& bVCLLoss) {
318   unsigned char* pDst = new unsigned char[iSrcLen];
319   int iLossCharLen = (int) strlen (pLossChars);
320   int iSkipedBytes = 0;
321   int iDstLen = 0;
322   int iBufPos = 0;
323   int ilastprefixlen = 0;
324   int i = 0;
325   bool bLost;
326   bVCLLoss = false;
327   SLostSim tmpSLostSim;
328   p_SLostSim->clear();
329   for (i = 0; i < iSrcLen;) {
330     if (pSrc[i] == 0 && pSrc[i + 1] == 0 && pSrc[i + 2] == 0 && pSrc[i + 3] == 1) {
331       if (i - iBufPos) {
332         tmpSLostSim.eNalType = (EWelsNalUnitType) ((* (pSrc + iBufPos + ilastprefixlen)) & 0x1f); // eNalUnitType
333         bLost = iLossIdx < iLossCharLen ? (pLossChars[iLossIdx] == '1') : (rand() % 2 == 1);
334         bLost = (!bLossPara) && (IS_PARAM_SETS_NALS (tmpSLostSim.eNalType)) ? false : bLost;
335         iLossIdx++;
336         tmpSLostSim.isLost = bLost;
337         p_SLostSim->push_back (tmpSLostSim);
338         if (!bLost) {
339           memcpy (pDst + iDstLen, pSrc + iBufPos, i - iBufPos);
340           iDstLen += (i - iBufPos);
341         } else {
342           bVCLLoss = (IS_VCL_NAL (tmpSLostSim.eNalType, 1)) ? true : bVCLLoss;
343           iSkipedBytes += (i - iBufPos);
344         }
345       }
346       ilastprefixlen = 4;
347       iBufPos = i;
348       i = i + 4;
349     } else if (pSrc[i] == 0 && pSrc[i + 1] == 0 && pSrc[i + 2] == 1) {
350       if (i - iBufPos) {
351         tmpSLostSim.eNalType = (EWelsNalUnitType) ((* (pSrc + iBufPos + ilastprefixlen)) & 0x1f); // eNalUnitType
352         bLost = iLossIdx < iLossCharLen ? (pLossChars[iLossIdx] == '1') : (rand() % 2 == 1);
353         bLost = (!bLossPara) && (IS_PARAM_SETS_NALS (tmpSLostSim.eNalType)) ? false : bLost;
354         iLossIdx++;
355         tmpSLostSim.isLost = bLost;
356         p_SLostSim->push_back (tmpSLostSim);
357         if (!bLost) {
358           memcpy (pDst + iDstLen, pSrc + iBufPos, i - iBufPos);
359           iDstLen += (i - iBufPos);
360         } else {
361           bVCLLoss = (IS_VCL_NAL (tmpSLostSim.eNalType, 1)) ? true : bVCLLoss;
362           iSkipedBytes += (i - iBufPos);
363         }
364       }
365       ilastprefixlen = 3;
366       iBufPos = i;
367       i = i + 3;
368     } else {
369       i++;
370     }
371   }
372   if (i - iBufPos) {
373     tmpSLostSim.eNalType = (EWelsNalUnitType) ((* (pSrc + iBufPos + ilastprefixlen)) & 0x1f); // eNalUnitType
374     bLost = iLossIdx < iLossCharLen ? (pLossChars[iLossIdx] == '1') : (rand() % 2 == 1);
375     bLost = (!bLossPara) && (IS_PARAM_SETS_NALS (tmpSLostSim.eNalType)) ? false : bLost;
376     iLossIdx++;
377     tmpSLostSim.isLost = bLost;
378     p_SLostSim->push_back (tmpSLostSim);
379     if (!bLost) {
380       memcpy (pDst + iDstLen, pSrc + iBufPos, i - iBufPos);
381       iDstLen += (i - iBufPos);
382     } else {
383       bVCLLoss = (IS_VCL_NAL (tmpSLostSim.eNalType, 1)) ? true : bVCLLoss;
384       iSkipedBytes += (i - iBufPos);
385     }
386   }
387   memset ((void*)pSrc, 0, iSrcLen);
388   memcpy ((void*)pSrc, pDst, iDstLen);
389   iSrcLen = iDstLen;
390   delete [] pDst;
391   return iSkipedBytes;
392 }
393 
394 
395