Showing the changes to test/api/BaseDecoderTest.cpp (1 of 23 files). Show all
Unified · Split
| @@ -0,0 +1,171 @@ | ||
| 1 | +#include <fstream> | |
| 2 | +#include <gtest/gtest.h> | |
| 3 | +#include "codec_def.h" | |
| 4 | +#include "codec_app_def.h" | |
| 5 | +#include "utils/BufferedData.h" | |
| 6 | +#include "BaseDecoderTest.h" | |
| 7 | + | |
| 8 | +static void ReadFrame(std::ifstream* file, BufferedData* buf) { | |
| 9 | + // start code of a frame is {0, 0, 0, 1} | |
| 10 | + int zeroCount = 0; | |
| 11 | + char b; | |
| 12 | + | |
| 13 | + buf->Clear(); | |
| 14 | + for (;;) { | |
| 15 | + file->read(&b, 1); | |
| 16 | + if (file->gcount() != 1) { // end of file | |
| 17 | + return; | |
| 18 | + } | |
| 19 | + if (!buf->PushBack(b)) { | |
| 20 | + FAIL() << "unable to allocate memory"; | |
| 21 | + } | |
| 22 | + | |
| 23 | + if (buf->Length() <= 4) { | |
| 24 | + continue; | |
| 25 | + } | |
| 26 | + | |
| 27 | + if (zeroCount < 3) { | |
| 28 | + zeroCount = b != 0 ? 0 : zeroCount + 1; | |
| 29 | + } else { | |
| 30 | + if (b == 1) { | |
| 31 | + if (file->seekg(-4, file->cur).good()) { | |
| 32 | + buf->SetLength(buf->Length() - 4); | |
| 33 | + return; | |
| 34 | + } else { | |
| 35 | + FAIL() << "unable to seek file"; | |
| 36 | + } | |
| 37 | + } else if (b == 0) { | |
| 38 | + zeroCount = 3; | |
| 39 | + } else { | |
| 40 | + zeroCount = 0; | |
| 41 | + } | |
| 42 | + } | |
| 43 | + } | |
| 44 | +} | |
| 45 | + | |
| 46 | +BaseDecoderTest::BaseDecoderTest() | |
| 47 | + : decoder_(NULL), decodeStatus_(OpenFile) {} | |
| 48 | + | |
| 49 | +void BaseDecoderTest::SetUp() { | |
| 50 | + long rv = CreateDecoder(&decoder_); | |
| 51 | + ASSERT_EQ(0, rv); | |
| 52 | + ASSERT_TRUE(decoder_ != NULL); | |
| 53 | + | |
| 54 | + SDecodingParam decParam; | |
| 55 | + memset(&decParam, 0, sizeof(SDecodingParam)); | |
| 56 | + decParam.iOutputColorFormat = videoFormatI420; | |
| 57 | + decParam.uiTargetDqLayer = UCHAR_MAX; | |
| 58 | + decParam.uiEcActiveFlag = 1; | |
| 59 | + decParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT; | |
| 60 | + | |
| 61 | + rv = decoder_->Initialize(&decParam); | |
| 62 | + ASSERT_EQ(0, rv); | |
| 63 | +} | |
| 64 | + | |
| 65 | +void BaseDecoderTest::TearDown() { | |
| 66 | + if (decoder_ != NULL) { | |
| 67 | + decoder_->Uninitialize(); | |
| 68 | + DestroyDecoder(decoder_); | |
| 69 | + } | |
| 70 | +} | |
| 71 | + | |
| 72 | + | |
| 73 | +void BaseDecoderTest::DecodeFrame(const uint8_t* src, int sliceSize, Callback* cbk) { | |
| 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_->DecodeFrame2(src, sliceSize, data, &bufInfo); | |
| 80 | + ASSERT_TRUE(rv == dsErrorFree); | |
| 81 | + | |
| 82 | + if (bufInfo.iBufferStatus == 1 && cbk != NULL) { | |
| 83 | + const Frame frame = { | |
| 84 | + { // y plane | |
| 85 | + static_cast<uint8_t*>(data[0]), | |
| 86 | + bufInfo.UsrData.sSystemBuffer.iWidth, | |
| 87 | + bufInfo.UsrData.sSystemBuffer.iHeight, | |
| 88 | + bufInfo.UsrData.sSystemBuffer.iStride[0] | |
| 89 | + }, | |
| 90 | + { // u plane | |
| 91 | + static_cast<uint8_t*>(data[1]), | |
| 92 | + bufInfo.UsrData.sSystemBuffer.iWidth / 2, | |
| 93 | + bufInfo.UsrData.sSystemBuffer.iHeight / 2, | |
| 94 | + bufInfo.UsrData.sSystemBuffer.iStride[1] | |
| 95 | + }, | |
| 96 | + { // v plane | |
| 97 | + static_cast<uint8_t*>(data[2]), | |
| 98 | + bufInfo.UsrData.sSystemBuffer.iWidth / 2, | |
| 99 | + bufInfo.UsrData.sSystemBuffer.iHeight / 2, | |
| 100 | + bufInfo.UsrData.sSystemBuffer.iStride[1] | |
| 101 | + }, | |
| 102 | + }; | |
| 103 | + cbk->onDecodeFrame(frame); | |
| 104 | + } | |
| 105 | +} | |
| 106 | +void BaseDecoderTest::DecodeFile(const char* fileName, Callback* cbk) { | |
| 107 | + std::ifstream file(fileName, std::ios::in | std::ios::binary); | |
| 108 | + ASSERT_TRUE(file.is_open()); | |
| 109 | + | |
| 110 | + BufferedData buf; | |
| 111 | + while (true) { | |
| 112 | + ReadFrame(&file, &buf); | |
| 113 | + if (::testing::Test::HasFatalFailure()) { | |
| 114 | + return; | |
| 115 | + } | |
| 116 | + if (buf.Length() == 0) { | |
| 117 | + break; | |
| 118 | + } | |
| 119 | + DecodeFrame(buf.data(), buf.Length(), cbk); | |
| 120 | + if (::testing::Test::HasFatalFailure()) { | |
| 121 | + return; | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + int32_t iEndOfStreamFlag = 1; | |
| 126 | + decoder_->SetOption(DECODER_OPTION_END_OF_STREAM, &iEndOfStreamFlag); | |
| 127 | + | |
| 128 | + // Get pending last frame | |
| 129 | + DecodeFrame(NULL, 0, cbk); | |
| 130 | +} | |
| 131 | + | |
| 132 | +bool BaseDecoderTest::Open(const char* fileName) { | |
| 133 | + if (decodeStatus_ == OpenFile) { | |
| 134 | + file_.open(fileName, std::ios_base::out | std::ios_base::binary); | |
| 135 | + if (file_.is_open()) { | |
| 136 | + decodeStatus_ = Decoding; | |
| 137 | + return true; | |
| 138 | + } | |
| 139 | + } | |
| 140 | + return false; | |
| 141 | +} | |
| 142 | + | |
| 143 | +bool BaseDecoderTest::DecodeNextFrame(Callback* cbk) { | |
| 144 | + switch (decodeStatus_) { | |
| 145 | + case Decoding: | |
| 146 | + ReadFrame(&file_, &buf_); | |
| 147 | + if (::testing::Test::HasFatalFailure()) { | |
| 148 | + return false; | |
| 149 | + } | |
| 150 | + if (buf_.Length() == 0) { | |
| 151 | + decodeStatus_ = EndOfStream; | |
| 152 | + return true; | |
| 153 | + } | |
| 154 | + DecodeFrame(buf_.data(), buf_.Length(), cbk); | |
| 155 | + if (::testing::Test::HasFatalFailure()) { | |
| 156 | + return false; | |
| 157 | + } | |
| 158 | + return true; | |
| 159 | + case EndOfStream: { | |
| 160 | + int32_t iEndOfStreamFlag = 1; | |
| 161 | + decoder_->SetOption(DECODER_OPTION_END_OF_STREAM, &iEndOfStreamFlag); | |
| 162 | + DecodeFrame(NULL, 0, cbk); | |
| 163 | + decodeStatus_ = End; | |
| 164 | + break; | |
| 165 | + } | |
| 166 | + case OpenFile: | |
| 167 | + case End: | |
| 168 | + break; | |
| 169 | + } | |
| 170 | + return false; | |
| 171 | +} | |