• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include "codec_def.h"
3 #include "utils/HashFunctions.h"
4 #include "utils/BufferedData.h"
5 #include "utils/InputStream.h"
6 #include "BaseDecoderTest.h"
7 #include "BaseEncoderTest.h"
8 #include <string>
UpdateHashFromFrame(const SFrameBSInfo & info,SHA1Context * ctx)9 static void UpdateHashFromFrame (const SFrameBSInfo& info, SHA1Context* ctx) {
10   for (int i = 0; i < info.iLayerNum; ++i) {
11     const SLayerBSInfo& layerInfo = info.sLayerInfo[i];
12     int layerSize = 0;
13     for (int j = 0; j < layerInfo.iNalCount; ++j) {
14       layerSize += layerInfo.pNalLengthInByte[j];
15     }
16     SHA1Input (ctx, layerInfo.pBsBuf, layerSize);
17   }
18 }
19 
WritePlaneBuffer(BufferedData * buf,const uint8_t * plane,int width,int height,int stride)20 static void WritePlaneBuffer (BufferedData* buf, const uint8_t* plane,
21                               int width, int height, int stride) {
22   for (int i = 0; i < height; i++) {
23     if (!buf->PushBack (plane, width)) {
24       FAIL() << "unable to allocate memory";
25     }
26     plane += stride;
27   }
28 }
29 
30 struct DecodeEncodeFileParam {
31   const char* fileName;
32   const char* hashStr;
33   int width;
34   int height;
35   float frameRate;
36 };
37 
38 class DecodeEncodeTest : public ::testing::TestWithParam<DecodeEncodeFileParam>,
39   public BaseDecoderTest, public BaseDecoderTest::Callback,
40   public BaseEncoderTest , public BaseEncoderTest::Callback,
41   public InputStream {
42  public:
SetUp()43   virtual void SetUp() {
44     BaseDecoderTest::SetUp();
45     if (HasFatalFailure()) {
46       return;
47     }
48     BaseEncoderTest::SetUp();
49     if (HasFatalFailure()) {
50       return;
51     }
52     SHA1Reset (&ctx_);
53   }
54 
TearDown()55   virtual void TearDown() {
56     BaseDecoderTest::TearDown();
57     BaseEncoderTest::TearDown();
58   }
59 
onDecodeFrame(const Frame & frame)60   virtual void onDecodeFrame (const Frame& frame) {
61     const Plane& y = frame.y;
62     const Plane& u = frame.u;
63     const Plane& v = frame.v;
64     WritePlaneBuffer (&buf_, y.data, y.width, y.height, y.stride);
65     WritePlaneBuffer (&buf_, u.data, u.width, u.height, u.stride);
66     WritePlaneBuffer (&buf_, v.data, v.width, v.height, v.stride);
67   }
68 
onEncodeFrame(const SFrameBSInfo & frameInfo)69   virtual void onEncodeFrame (const SFrameBSInfo& frameInfo) {
70     UpdateHashFromFrame (frameInfo, &ctx_);
71   }
72 
read(void * ptr,size_t len)73   virtual int read (void* ptr, size_t len) {
74     while (buf_.Length() < len) {
75       bool hasNext = DecodeNextFrame (this);
76       if (HasFatalFailure()) {
77         return -1;
78       }
79       if (!hasNext) {
80         if (buf_.Length() == 0) {
81           return -1;
82         }
83         break;
84       }
85     }
86     return (int) buf_.PopFront (static_cast<uint8_t*> (ptr), len);
87   }
88 
89  protected:
90   SHA1Context ctx_;
91   BufferedData buf_;
92 };
93 
DecEncFileParamToParamExt(DecodeEncodeFileParam * pDecEncFileParam,SEncParamExt * pEnxParamExt)94 void DecEncFileParamToParamExt (DecodeEncodeFileParam* pDecEncFileParam, SEncParamExt* pEnxParamExt) {
95   ASSERT_TRUE (NULL != pDecEncFileParam && NULL != pEnxParamExt);
96 
97   pEnxParamExt->iPicWidth      = pDecEncFileParam->width;
98   pEnxParamExt->iPicHeight     = pDecEncFileParam->height;
99   pEnxParamExt->fMaxFrameRate  = pDecEncFileParam->frameRate;
100 
101   //default value
102   pEnxParamExt->iUsageType        = CAMERA_VIDEO_REAL_TIME;
103   pEnxParamExt->iSpatialLayerNum  = 1;
104   pEnxParamExt->bEnableDenoise    = false;
105   pEnxParamExt->bIsLosslessLink   = false;
106   pEnxParamExt->bEnableLongTermReference  = false;
107   pEnxParamExt->iEntropyCodingModeFlag    = 0;
108 
109   for (int i = 0; i < pEnxParamExt->iSpatialLayerNum; i++) {
110     pEnxParamExt->sSpatialLayers[i].sSliceArgument.uiSliceMode = SM_SINGLE_SLICE;
111   }
112 
113 }
TEST_P(DecodeEncodeTest,CompareOutput)114 TEST_P (DecodeEncodeTest, CompareOutput) {
115   DecodeEncodeFileParam p = GetParam();
116   SEncParamExt  EnxParamExt;
117   DecEncFileParamToParamExt (&p, &EnxParamExt);
118 
119 #if defined(ANDROID_NDK)
120   std::string filename = std::string ("/sdcard/") + p.fileName;
121   ASSERT_TRUE (Open (filename.c_str()));
122 #else
123   ASSERT_TRUE (Open (p.fileName));
124 #endif
125   EncodeStream (this, &EnxParamExt, this);
126   unsigned char digest[SHA_DIGEST_LENGTH];
127   SHA1Result (&ctx_, digest);
128   if (!HasFatalFailure()) {
129     CompareHash (digest, p.hashStr);
130   }
131 }
132 static const DecodeEncodeFileParam kFileParamArray[] = {
133   {"res/test_vd_1d.264", "47cdeeb156991a61af309f4145b23643556d35a2", 320, 192, 12.0f},
134   {"res/test_vd_rc.264", "37f9f80c7330ab35f611c6cb6d009c2f1e7815ab", 320, 192, 12.0f},
135 };
136 
137 
138 INSTANTIATE_TEST_CASE_P (DecodeEncodeFile, DecodeEncodeTest,
139                          ::testing::ValuesIn (kFileParamArray));
140