Showing the changes to test/api/BaseDecoderTest.cpp (1 of 55 files). Show all
Unified · Split
| @@ -1,177 +1,219 @@ | ||
| 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 | −int32_t BaseDecoderTest::SetUp() { | |
| 50 | − long rv = WelsCreateDecoder (&decoder_); | |
| 51 | − EXPECT_EQ (0, rv); | |
| 52 | − EXPECT_TRUE (decoder_ != NULL); | |
| 53 | − if (decoder_ == NULL) { | |
| 54 | − return rv; | |
| 55 | − } | |
| 56 | − | |
| 57 | − SDecodingParam decParam; | |
| 58 | − memset (&decParam, 0, sizeof (SDecodingParam)); | |
| 59 | − decParam.uiTargetDqLayer = UCHAR_MAX; | |
| 60 | − decParam.eEcActiveIdc = ERROR_CON_SLICE_COPY; | |
| 61 | − decParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT; | |
| 62 | − | |
| 63 | − rv = decoder_->Initialize (&decParam); | |
| 64 | − EXPECT_EQ (0, rv); | |
| 65 | − return (int32_t)rv; | |
| 66 | −} | |
| 67 | − | |
| 68 | −void BaseDecoderTest::TearDown() { | |
| 69 | − if (decoder_ != NULL) { | |
| 70 | − decoder_->Uninitialize(); | |
| 71 | − WelsDestroyDecoder (decoder_); | |
| 72 | − } | |
| 73 | −} | |
| 74 | − | |
| 75 | − | |
| 76 | −void BaseDecoderTest::DecodeFrame (const uint8_t* src, size_t sliceSize, Callback* cbk) { | |
| 77 | − uint8_t* data[3]; | |
| 78 | − SBufferInfo bufInfo; | |
| 79 | − memset (data, 0, sizeof (data)); | |
| 80 | − memset (&bufInfo, 0, sizeof (SBufferInfo)); | |
| 81 | − | |
| 82 | − DECODING_STATE rv = decoder_->DecodeFrame2 (src, (int) sliceSize, data, &bufInfo); | |
| 83 | − ASSERT_TRUE (rv == dsErrorFree); | |
| 84 | − | |
| 85 | − if (bufInfo.iBufferStatus == 1 && cbk != NULL) { | |
| 86 | − const Frame frame = { | |
| 87 | − { | |
| 88 | − // y plane | |
| 89 | − data[0], | |
| 90 | − bufInfo.UsrData.sSystemBuffer.iWidth, | |
| 91 | − bufInfo.UsrData.sSystemBuffer.iHeight, | |
| 92 | − bufInfo.UsrData.sSystemBuffer.iStride[0] | |
| 93 | − }, | |
| 94 | − { | |
| 95 | − // u plane | |
| 96 | − data[1], | |
| 97 | − bufInfo.UsrData.sSystemBuffer.iWidth / 2, | |
| 98 | − bufInfo.UsrData.sSystemBuffer.iHeight / 2, | |
| 99 | − bufInfo.UsrData.sSystemBuffer.iStride[1] | |
| 100 | − }, | |
| 101 | − { | |
| 102 | − // v plane | |
| 103 | − data[2], | |
| 104 | − bufInfo.UsrData.sSystemBuffer.iWidth / 2, | |
| 105 | − bufInfo.UsrData.sSystemBuffer.iHeight / 2, | |
| 106 | − bufInfo.UsrData.sSystemBuffer.iStride[1] | |
| 107 | − }, | |
| 108 | − }; | |
| 109 | − cbk->onDecodeFrame (frame); | |
| 110 | − } | |
| 111 | −} | |
| 112 | −void BaseDecoderTest::DecodeFile (const char* fileName, Callback* cbk) { | |
| 113 | − std::ifstream file (fileName, std::ios::in | std::ios::binary); | |
| 114 | − ASSERT_TRUE (file.is_open()); | |
| 115 | − | |
| 116 | − BufferedData buf; | |
| 117 | − while (true) { | |
| 118 | − ReadFrame (&file, &buf); | |
| 119 | − if (::testing::Test::HasFatalFailure()) { | |
| 120 | − return; | |
| 121 | − } | |
| 122 | − if (buf.Length() == 0) { | |
| 123 | − break; | |
| 124 | − } | |
| 125 | − DecodeFrame (buf.data(), buf.Length(), cbk); | |
| 126 | − if (::testing::Test::HasFatalFailure()) { | |
| 127 | − return; | |
| 128 | − } | |
| 129 | − } | |
| 130 | − | |
| 131 | − int32_t iEndOfStreamFlag = 1; | |
| 132 | − decoder_->SetOption (DECODER_OPTION_END_OF_STREAM, &iEndOfStreamFlag); | |
| 133 | − | |
| 134 | − // Get pending last frame | |
| 135 | − DecodeFrame (NULL, 0, cbk); | |
| 136 | −} | |
| 137 | − | |
| 138 | −bool BaseDecoderTest::Open (const char* fileName) { | |
| 139 | − if (decodeStatus_ == OpenFile) { | |
| 140 | − file_.open (fileName, std::ios_base::out | std::ios_base::binary); | |
| 141 | − if (file_.is_open()) { | |
| 142 | − decodeStatus_ = Decoding; | |
| 143 | − return true; | |
| 144 | − } | |
| 145 | − } | |
| 146 | − return false; | |
| 147 | −} | |
| 148 | − | |
| 149 | −bool BaseDecoderTest::DecodeNextFrame (Callback* cbk) { | |
| 150 | − switch (decodeStatus_) { | |
| 151 | − case Decoding: | |
| 152 | − ReadFrame (&file_, &buf_); | |
| 153 | − if (::testing::Test::HasFatalFailure()) { | |
| 154 | − return false; | |
| 155 | − } | |
| 156 | − if (buf_.Length() == 0) { | |
| 157 | − decodeStatus_ = EndOfStream; | |
| 158 | − return true; | |
| 159 | − } | |
| 160 | − DecodeFrame (buf_.data(), buf_.Length(), cbk); | |
| 161 | − if (::testing::Test::HasFatalFailure()) { | |
| 162 | − return false; | |
| 163 | − } | |
| 164 | − return true; | |
| 165 | − case EndOfStream: { | |
| 166 | − int32_t iEndOfStreamFlag = 1; | |
| 167 | − decoder_->SetOption (DECODER_OPTION_END_OF_STREAM, &iEndOfStreamFlag); | |
| 168 | − DecodeFrame (NULL, 0, cbk); | |
| 169 | − decodeStatus_ = End; | |
| 170 | − break; | |
| 171 | − } | |
| 172 | − case OpenFile: | |
| 173 | − case End: | |
| 174 | − break; | |
| 175 | − } | |
| 176 | − return false; | |
| 177 | −} | |
| 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 | +int32_t BaseDecoderTest::SetUp() { | |
| 50 | + long rv = WelsCreateDecoder (&decoder_); | |
| 51 | + EXPECT_EQ (0, rv); | |
| 52 | + EXPECT_TRUE (decoder_ != NULL); | |
| 53 | + if (decoder_ == NULL) { | |
| 54 | + return rv; | |
| 55 | + } | |
| 56 | + | |
| 57 | + SDecodingParam decParam; | |
| 58 | + memset (&decParam, 0, sizeof (SDecodingParam)); | |
| 59 | + decParam.uiTargetDqLayer = UCHAR_MAX; | |
| 60 | + decParam.eEcActiveIdc = ERROR_CON_SLICE_COPY; | |
| 61 | + decParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT; | |
| 62 | + | |
| 63 | + rv = decoder_->Initialize (&decParam); | |
| 64 | + EXPECT_EQ (0, rv); | |
| 65 | + return (int32_t)rv; | |
| 66 | +} | |
| 67 | + | |
| 68 | +void BaseDecoderTest::TearDown() { | |
| 69 | + if (decoder_ != NULL) { | |
| 70 | + decoder_->Uninitialize(); | |
| 71 | + WelsDestroyDecoder (decoder_); | |
| 72 | + } | |
| 73 | +} | |
| 74 | + | |
| 75 | + | |
| 76 | +void BaseDecoderTest::DecodeFrame (const uint8_t* src, size_t sliceSize, Callback* cbk) { | |
| 77 | + uint8_t* data[3]; | |
| 78 | + SBufferInfo bufInfo; | |
| 79 | + memset (data, 0, sizeof (data)); | |
| 80 | + memset (&bufInfo, 0, sizeof (SBufferInfo)); | |
| 81 | + | |
| 82 | + DECODING_STATE rv = decoder_->DecodeFrame2 (src, (int) sliceSize, data, &bufInfo); | |
| 83 | + ASSERT_TRUE (rv == dsErrorFree); | |
| 84 | + | |
| 85 | + if (bufInfo.iBufferStatus == 1 && cbk != NULL) { | |
| 86 | + const Frame frame = { | |
| 87 | + { | |
| 88 | + // y plane | |
| 89 | + data[0], | |
| 90 | + bufInfo.UsrData.sSystemBuffer.iWidth, | |
| 91 | + bufInfo.UsrData.sSystemBuffer.iHeight, | |
| 92 | + bufInfo.UsrData.sSystemBuffer.iStride[0] | |
| 93 | + }, | |
| 94 | + { | |
| 95 | + // u plane | |
| 96 | + data[1], | |
| 97 | + bufInfo.UsrData.sSystemBuffer.iWidth / 2, | |
| 98 | + bufInfo.UsrData.sSystemBuffer.iHeight / 2, | |
| 99 | + bufInfo.UsrData.sSystemBuffer.iStride[1] | |
| 100 | + }, | |
| 101 | + { | |
| 102 | + // v plane | |
| 103 | + data[2], | |
| 104 | + bufInfo.UsrData.sSystemBuffer.iWidth / 2, | |
| 105 | + bufInfo.UsrData.sSystemBuffer.iHeight / 2, | |
| 106 | + bufInfo.UsrData.sSystemBuffer.iStride[1] | |
| 107 | + }, | |
| 108 | + }; | |
| 109 | + cbk->onDecodeFrame (frame); | |
| 110 | + } | |
| 111 | +} | |
| 112 | +void BaseDecoderTest::FlushFrame (Callback* cbk) { | |
| 113 | + uint8_t* data[3]; | |
| 114 | + SBufferInfo bufInfo; | |
| 115 | + memset (data, 0, sizeof (data)); | |
| 116 | + memset (&bufInfo, 0, sizeof (SBufferInfo)); | |
| 117 | + | |
| 118 | + DECODING_STATE rv = decoder_->FlushFrame (data, &bufInfo); | |
| 119 | + ASSERT_TRUE (rv == dsErrorFree); | |
| 120 | + | |
| 121 | + if (bufInfo.iBufferStatus == 1 && cbk != NULL) { | |
| 122 | + const Frame frame = { | |
| 123 | + { | |
| 124 | + // y plane | |
| 125 | + data[0], | |
| 126 | + bufInfo.UsrData.sSystemBuffer.iWidth, | |
| 127 | + bufInfo.UsrData.sSystemBuffer.iHeight, | |
| 128 | + bufInfo.UsrData.sSystemBuffer.iStride[0] | |
| 129 | + }, | |
| 130 | + { | |
| 131 | + // u plane | |
| 132 | + data[1], | |
| 133 | + bufInfo.UsrData.sSystemBuffer.iWidth / 2, | |
| 134 | + bufInfo.UsrData.sSystemBuffer.iHeight / 2, | |
| 135 | + bufInfo.UsrData.sSystemBuffer.iStride[1] | |
| 136 | + }, | |
| 137 | + { | |
| 138 | + // v plane | |
| 139 | + data[2], | |
| 140 | + bufInfo.UsrData.sSystemBuffer.iWidth / 2, | |
| 141 | + bufInfo.UsrData.sSystemBuffer.iHeight / 2, | |
| 142 | + bufInfo.UsrData.sSystemBuffer.iStride[1] | |
| 143 | + }, | |
| 144 | + }; | |
| 145 | + cbk->onDecodeFrame (frame); | |
| 146 | + } | |
| 147 | +} | |
| 148 | +void BaseDecoderTest::DecodeFile (const char* fileName, Callback* cbk) { | |
| 149 | + std::ifstream file (fileName, std::ios::in | std::ios::binary); | |
| 150 | + ASSERT_TRUE (file.is_open()); | |
| 151 | + | |
| 152 | + BufferedData buf; | |
| 153 | + while (true) { | |
| 154 | + ReadFrame (&file, &buf); | |
| 155 | + if (::testing::Test::HasFatalFailure()) { | |
| 156 | + return; | |
| 157 | + } | |
| 158 | + if (buf.Length() == 0) { | |
| 159 | + break; | |
| 160 | + } | |
| 161 | + DecodeFrame (buf.data(), buf.Length(), cbk); | |
| 162 | + if (::testing::Test::HasFatalFailure()) { | |
| 163 | + return; | |
| 164 | + } | |
| 165 | + } | |
| 166 | + | |
| 167 | + int32_t iEndOfStreamFlag = 1; | |
| 168 | + decoder_->SetOption (DECODER_OPTION_END_OF_STREAM, &iEndOfStreamFlag); | |
| 169 | + | |
| 170 | + // Get pending last frame | |
| 171 | + DecodeFrame (NULL, 0, cbk); | |
| 172 | + // Flush out last frames in decoder buffer | |
| 173 | + int32_t num_of_frames_in_buffer = 0; | |
| 174 | + decoder_->GetOption (DECODER_OPTION_NUM_OF_FRAMES_REMAINING_IN_BUFFER, &num_of_frames_in_buffer); | |
| 175 | + for (int32_t i = 0; i < num_of_frames_in_buffer; ++i) { | |
| 176 | + FlushFrame (cbk); | |
| 177 | + } | |
| 178 | +} | |
| 179 | + | |
| 180 | +bool BaseDecoderTest::Open (const char* fileName) { | |
| 181 | + if (decodeStatus_ == OpenFile) { | |
| 182 | + file_.open (fileName, std::ios_base::out | std::ios_base::binary); | |
| 183 | + if (file_.is_open()) { | |
| 184 | + decodeStatus_ = Decoding; | |
| 185 | + return true; | |
| 186 | + } | |
| 187 | + } | |
| 188 | + return false; | |
| 189 | +} | |
| 190 | + | |
| 191 | +bool BaseDecoderTest::DecodeNextFrame (Callback* cbk) { | |
| 192 | + switch (decodeStatus_) { | |
| 193 | + case Decoding: | |
| 194 | + ReadFrame (&file_, &buf_); | |
| 195 | + if (::testing::Test::HasFatalFailure()) { | |
| 196 | + return false; | |
| 197 | + } | |
| 198 | + if (buf_.Length() == 0) { | |
| 199 | + decodeStatus_ = EndOfStream; | |
| 200 | + return true; | |
| 201 | + } | |
| 202 | + DecodeFrame (buf_.data(), buf_.Length(), cbk); | |
| 203 | + if (::testing::Test::HasFatalFailure()) { | |
| 204 | + return false; | |
| 205 | + } | |
| 206 | + return true; | |
| 207 | + case EndOfStream: { | |
| 208 | + int32_t iEndOfStreamFlag = 1; | |
| 209 | + decoder_->SetOption (DECODER_OPTION_END_OF_STREAM, &iEndOfStreamFlag); | |
| 210 | + DecodeFrame (NULL, 0, cbk); | |
| 211 | + decodeStatus_ = End; | |
| 212 | + break; | |
| 213 | + } | |
| 214 | + case OpenFile: | |
| 215 | + case End: | |
| 216 | + break; | |
| 217 | + } | |
| 218 | + return false; | |
| 219 | +} | |