openh264/test/api/BaseEncoderTest.cpp

112 lines
4.0 KiB
C++
Raw Normal View History

#include <fstream>
#include <gtest/gtest.h>
#include "codec_def.h"
#include "utils/BufferedData.h"
#include "utils/FileInputStream.h"
#include "BaseEncoderTest.h"
2014-06-26 03:50:41 +02:00
static int InitWithParam (ISVCEncoder* encoder, EUsageType usageType, int width,
2014-09-19 08:46:02 +02:00
int height, float frameRate, SliceModeEnum sliceMode, bool denoise, int layers, bool losslessLink, bool enableLtr) {
if (SM_SINGLE_SLICE == sliceMode && !denoise && layers == 1 && !losslessLink && !enableLtr) {
SEncParamBase param;
2014-06-26 03:50:41 +02:00
memset (&param, 0, sizeof (SEncParamBase));
param.iUsageType = usageType;
param.fMaxFrameRate = frameRate;
param.iPicWidth = width;
param.iPicHeight = height;
param.iTargetBitrate = 5000000;
2014-06-26 03:50:41 +02:00
return encoder->Initialize (&param);
} else {
SEncParamExt param;
2014-06-26 03:50:41 +02:00
encoder->GetDefaultParams (&param);
param.iUsageType = usageType;
param.fMaxFrameRate = frameRate;
param.iPicWidth = width;
param.iPicHeight = height;
param.iTargetBitrate = 5000000;
param.bEnableDenoise = denoise;
param.iSpatialLayerNum = layers;
2014-09-19 08:46:02 +02:00
param.bIsLosslessLink = losslessLink;
param.bEnableLongTermReference = enableLtr;
2014-05-22 13:39:43 +02:00
if (sliceMode != SM_SINGLE_SLICE && sliceMode != SM_DYN_SLICE) //SM_DYN_SLICE don't support multi-thread now
param.iMultipleThreadIdc = 2;
for (int i = 0; i < param.iSpatialLayerNum; i++) {
param.sSpatialLayers[i].iVideoWidth = width >> (param.iSpatialLayerNum - 1 - i);
param.sSpatialLayers[i].iVideoHeight = height >> (param.iSpatialLayerNum - 1 - i);
param.sSpatialLayers[i].fFrameRate = frameRate;
param.sSpatialLayers[i].iSpatialBitrate = param.iTargetBitrate;
param.sSpatialLayers[i].sSliceCfg.uiSliceMode = sliceMode;
2014-05-22 13:39:43 +02:00
if (sliceMode == SM_DYN_SLICE) {
param.sSpatialLayers[i].sSliceCfg.sSliceArgument.uiSliceSizeConstraint = 600;
param.uiMaxNalSize = 1500;
}
}
param.iTargetBitrate *= param.iSpatialLayerNum;
2014-06-26 03:50:41 +02:00
return encoder->InitializeExt (&param);
}
}
2014-06-26 03:50:41 +02:00
BaseEncoderTest::BaseEncoderTest() : encoder_ (NULL) {}
void BaseEncoderTest::SetUp() {
2014-06-26 03:50:41 +02:00
int rv = WelsCreateSVCEncoder (&encoder_);
ASSERT_EQ (0, rv);
ASSERT_TRUE (encoder_ != NULL);
}
void BaseEncoderTest::TearDown() {
if (encoder_) {
encoder_->Uninitialize();
2014-06-26 03:50:41 +02:00
WelsDestroySVCEncoder (encoder_);
}
}
2014-06-26 03:50:41 +02:00
void BaseEncoderTest::EncodeStream (InputStream* in, EUsageType usageType, int width, int height,
2014-09-19 08:46:02 +02:00
float frameRate, SliceModeEnum slices, bool denoise, int layers, bool losslessLink, bool enableLtr, Callback* cbk) {
2014-10-23 11:54:33 +02:00
int rv = InitWithParam (encoder_, usageType, width, height, frameRate, slices, denoise, layers, losslessLink,
enableLtr);
2014-06-26 03:50:41 +02:00
ASSERT_TRUE (rv == cmResultSuccess);
// I420: 1(Y) + 1/4(U) + 1/4(V)
int frameSize = width * height * 3 / 2;
BufferedData buf;
2014-06-26 03:50:41 +02:00
buf.SetLength (frameSize);
ASSERT_TRUE (buf.Length() == (size_t)frameSize);
SFrameBSInfo info;
2014-06-26 03:50:41 +02:00
memset (&info, 0, sizeof (SFrameBSInfo));
SSourcePicture pic;
2014-06-26 03:50:41 +02:00
memset (&pic, 0, sizeof (SSourcePicture));
pic.iPicWidth = width;
pic.iPicHeight = height;
pic.iColorFormat = videoFormatI420;
pic.iStride[0] = pic.iPicWidth;
2014-06-26 03:50:41 +02:00
pic.iStride[1] = pic.iStride[2] = pic.iPicWidth >> 1;
pic.pData[0] = buf.data();
2014-06-26 03:50:41 +02:00
pic.pData[1] = pic.pData[0] + width * height;
pic.pData[2] = pic.pData[1] + (width * height >> 2);
while (in->read (buf.data(), frameSize) == frameSize) {
rv = encoder_->EncodeFrame (&pic, &info);
ASSERT_TRUE (rv == cmResultSuccess);
if (info.eFrameType != videoFrameTypeSkip && cbk != NULL) {
2014-06-26 03:50:41 +02:00
cbk->onEncodeFrame (info);
}
}
}
2014-06-26 03:50:41 +02:00
void BaseEncoderTest::EncodeFile (const char* fileName, EUsageType usageType, int width, int height,
2014-09-19 08:46:02 +02:00
float frameRate, SliceModeEnum slices, bool denoise, int layers, bool losslessLink, bool enableLtr, Callback* cbk) {
FileInputStream fileStream;
2014-06-26 03:50:41 +02:00
ASSERT_TRUE (fileStream.Open (fileName));
2014-09-19 08:46:02 +02:00
EncodeStream (&fileStream, usageType, width, height, frameRate, slices, denoise, layers, losslessLink, enableLtr, cbk);
}