Showing the changes to test/decoder_test.cpp (1 of 6 files). Show all
Unified · Split
| @@ -0,0 +1,199 @@ | ||
| 1 | +#include <gtest/gtest.h> | |
| 2 | +#include <stdlib.h> | |
| 3 | +#include <stdarg.h> | |
| 4 | +#include <stdint.h> | |
| 5 | +#include <limits.h> | |
| 6 | +#include <fstream> | |
| 7 | +#include <openssl/sha.h> | |
| 8 | + | |
| 9 | +#include "codec_def.h" | |
| 10 | +#include "codec_app_def.h" | |
| 11 | +#include "codec_api.h" | |
| 12 | + | |
| 13 | +#include "utils/BufferedData.h" | |
| 14 | + | |
| 15 | +static bool CompareHash(unsigned char(&digest)[SHA_DIGEST_LENGTH], | |
| 16 | + const char* hashStr) { | |
| 17 | + | |
| 18 | + char hashStrCmp[SHA_DIGEST_LENGTH * 2 + 1]; | |
| 19 | + for (int i = 0; i < SHA_DIGEST_LENGTH; ++i) { | |
| 20 | + sprintf(&hashStrCmp[i*2], "%.2x", digest[i]); | |
| 21 | + } | |
| 22 | + hashStrCmp[SHA_DIGEST_LENGTH * 2] = '\0'; | |
| 23 | + return strncmp(hashStr, hashStrCmp, SHA_DIGEST_LENGTH * 2) == 0; | |
| 24 | +} | |
| 25 | + | |
| 26 | +static void UpdateHashFromPlane(SHA_CTX* ctx, const uint8_t* plane, | |
| 27 | + int width, int height, int stride) { | |
| 28 | + | |
| 29 | + for (int i = 0; i < height; i++) { | |
| 30 | + SHA1_Update(ctx, plane, width); | |
| 31 | + plane += stride; | |
| 32 | + } | |
| 33 | +} | |
| 34 | + | |
| 35 | +/** | |
| 36 | + * @return frame size (>= 0), or -1 for memory allocation error. | |
| 37 | + */ | |
| 38 | +static int ReadFrame(std::ifstream* file, BufferedData* buf) { | |
| 39 | + // start code of a frame is {0, 0, 0, 1} | |
| 40 | + int zeroCount = 0; | |
| 41 | + char b; | |
| 42 | + | |
| 43 | + while (file->read(&b, 1), file->gcount() == 1) { | |
| 44 | + if (!buf->Push(b)) { | |
| 45 | + return -1; | |
| 46 | + } | |
| 47 | + | |
| 48 | + if (buf->Length() <= 4) { | |
| 49 | + continue; | |
| 50 | + } | |
| 51 | + | |
| 52 | + if (zeroCount < 3) { | |
| 53 | + zeroCount = b != 0 ? 0 : zeroCount + 1; | |
| 54 | + } else { | |
| 55 | + if (b == 1) { | |
| 56 | + file->seekg(-4, file->cur); | |
| 57 | + return buf->Length() - 4; | |
| 58 | + } else if (b == 0) { | |
| 59 | + zeroCount = 3; | |
| 60 | + } else { | |
| 61 | + zeroCount = 0; | |
| 62 | + } | |
| 63 | + } | |
| 64 | + } | |
| 65 | + return buf->Length(); | |
| 66 | +} | |
| 67 | + | |
| 68 | +/** | |
| 69 | + * @return true if a frame is decoded successfully, otherwise false. | |
| 70 | + */ | |
| 71 | +static bool DecodeAndProcess(ISVCDecoder* decoder, const uint8_t* src, | |
| 72 | + int sliceSize, SHA_CTX* ctx) { | |
| 73 | + | |
| 74 | + void* data[3]; | |
| 75 | + SBufferInfo bufInfo; | |
| 76 | + memset(data, 0, sizeof(data)); | |
| 77 | + memset(&bufInfo, 0, sizeof(SBufferInfo)); | |
| 78 | + | |
| 79 | + DECODING_STATE rv = decoder->DecodeFrame(src, sliceSize, data, &bufInfo); | |
| 80 | + if (rv == dsErrorFree) { | |
| 81 | + if (bufInfo.iBufferStatus == 1) { | |
| 82 | + // y plane | |
| 83 | + UpdateHashFromPlane(ctx, static_cast<uint8_t*>(data[0]), | |
| 84 | + bufInfo.UsrData.sSystemBuffer.iWidth, | |
| 85 | + bufInfo.UsrData.sSystemBuffer.iHeight, | |
| 86 | + bufInfo.UsrData.sSystemBuffer.iStride[0]); | |
| 87 | + // u plane | |
| 88 | + UpdateHashFromPlane(ctx, static_cast<uint8_t*>(data[1]), | |
| 89 | + bufInfo.UsrData.sSystemBuffer.iWidth / 2, | |
| 90 | + bufInfo.UsrData.sSystemBuffer.iHeight / 2, | |
| 91 | + bufInfo.UsrData.sSystemBuffer.iStride[1]); | |
| 92 | + // v plane | |
| 93 | + UpdateHashFromPlane(ctx, static_cast<uint8_t*>(data[2]), | |
| 94 | + bufInfo.UsrData.sSystemBuffer.iWidth / 2, | |
| 95 | + bufInfo.UsrData.sSystemBuffer.iHeight / 2, | |
| 96 | + bufInfo.UsrData.sSystemBuffer.iStride[1]); | |
| 97 | + } | |
| 98 | + return true; | |
| 99 | + } else { | |
| 100 | + return false; | |
| 101 | + } | |
| 102 | +} | |
| 103 | + | |
| 104 | +static void CompareFileToHash(ISVCDecoder* decoder, | |
| 105 | + const char* fileName, const char* hashStr) { | |
| 106 | + | |
| 107 | + std::ifstream file(fileName, std::ios::in | std::ios::binary); | |
| 108 | + ASSERT_TRUE(file.is_open()); | |
| 109 | + | |
| 110 | + unsigned char digest[SHA_DIGEST_LENGTH]; | |
| 111 | + SHA_CTX ctx; | |
| 112 | + SHA1_Init(&ctx); | |
| 113 | + | |
| 114 | + BufferedData buf; | |
| 115 | + int sliceSize; | |
| 116 | + | |
| 117 | + while ((sliceSize = ReadFrame(&file, &buf)) > 0) { | |
| 118 | + if (DecodeAndProcess(decoder, buf.data(), sliceSize, &ctx)) { | |
| 119 | + buf.Clear(); | |
| 120 | + } else { | |
| 121 | + SHA1_Final(digest, &ctx); | |
| 122 | + FAIL() << "unable to decode frame"; | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + if (sliceSize < 0) { | |
| 127 | + SHA1_Final(digest, &ctx); | |
| 128 | + FAIL() << "unable to allocate memory"; | |
| 129 | + } | |
| 130 | + | |
| 131 | + int32_t iEndOfStreamFlag = true; | |
| 132 | + decoder->SetOption(DECODER_OPTION_END_OF_STREAM, &iEndOfStreamFlag); | |
| 133 | + | |
| 134 | + // Get pending last frame | |
| 135 | + if (!DecodeAndProcess(decoder, NULL, 0, &ctx)) { | |
| 136 | + SHA1_Final(digest, &ctx); | |
| 137 | + FAIL() << "unable to decode last frame"; | |
| 138 | + } | |
| 139 | + | |
| 140 | + SHA1_Final(digest, &ctx); | |
| 141 | + ASSERT_TRUE(CompareHash(digest, hashStr)); | |
| 142 | +} | |
| 143 | + | |
| 144 | +class DecoderInitTest : public ::testing::Test { | |
| 145 | +public: | |
| 146 | + DecoderInitTest() : decoder_(NULL) {} | |
| 147 | + | |
| 148 | + virtual void SetUp() { | |
| 149 | + long rv = CreateDecoder(&decoder_); | |
| 150 | + ASSERT_EQ(0, rv); | |
| 151 | + ASSERT_TRUE(decoder_ != NULL); | |
| 152 | + | |
| 153 | + SDecodingParam decParam; | |
| 154 | + memset(&decParam, 0, sizeof(SDecodingParam)); | |
| 155 | + decParam.iOutputColorFormat = videoFormatI420; | |
| 156 | + decParam.uiTargetDqLayer = UCHAR_MAX; | |
| 157 | + decParam.uiEcActiveFlag = 1; | |
| 158 | + decParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT; | |
| 159 | + | |
| 160 | + rv = decoder_->Initialize(&decParam, INIT_TYPE_PARAMETER_BASED); | |
| 161 | + ASSERT_EQ(0, rv); | |
| 162 | + } | |
| 163 | + | |
| 164 | + virtual void TearDown() { | |
| 165 | + if (decoder_ != NULL) { | |
| 166 | + decoder_->Uninitialize(); | |
| 167 | + DestroyDecoder(decoder_); | |
| 168 | + } | |
| 169 | + } | |
| 170 | + | |
| 171 | +protected: | |
| 172 | + ISVCDecoder* decoder_; | |
| 173 | +}; | |
| 174 | + | |
| 175 | + | |
| 176 | +TEST_F(DecoderInitTest, JustInit) { | |
| 177 | +} | |
| 178 | + | |
| 179 | +struct FileParam { | |
| 180 | + const char* fileName; | |
| 181 | + const char* hashStr; | |
| 182 | +}; | |
| 183 | + | |
| 184 | +class DecoderOutputTest : public DecoderInitTest, | |
| 185 | + public ::testing::WithParamInterface<FileParam> { | |
| 186 | +}; | |
| 187 | + | |
| 188 | +TEST_P(DecoderOutputTest, CompareOutput) { | |
| 189 | + FileParam p = GetParam(); | |
| 190 | + CompareFileToHash(decoder_, p.fileName, p.hashStr); | |
| 191 | +} | |
| 192 | + | |
| 193 | +static const FileParam kFileParamArray[] = { | |
| 194 | + {"res/test_vd_1d.264", "5827d2338b79ff82cd091c707823e466197281d3"}, | |
| 195 | + {"res/test_vd_rc.264", "eea02e97bfec89d0418593a8abaaf55d02eaa1ca"} | |
| 196 | +}; | |
| 197 | + | |
| 198 | +INSTANTIATE_TEST_CASE_P(DecodeFile, DecoderOutputTest, | |
| 199 | + ::testing::ValuesIn(kFileParamArray)); | |