Skip to content
This repository was archived by the owner on May 17, 2023. It is now read-only.

Commit 5bbddaf

Browse files
ogolikovOleg Nabiullin
authored and
Oleg Nabiullin
committed
[sample_encode] parameter for TCBRC test was added
Issue: MDP-66304 Test: -
1 parent f557633 commit 5bbddaf

File tree

6 files changed

+194
-3
lines changed

6 files changed

+194
-3
lines changed

doc/samples/readme-encode_linux.md

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ The following command-line switches are optional:
129129
| [-perf_opt n] | sets number of prefetched frames. In performance mode app preallocates buffer and loads first n frames |
130130
| [-fps]| limits overall fps of pipeline|
131131
| [-dump fileName] |dump MSDK components configuration to the file in text form|
132+
| [-tcbrctestfile fileName] |file with target frame sizes in bytes. It can be defined for each frame in format: one value per line|
132133
| [-usei]| insert user data unregistered SEI. eg: 7fc92488825d11e7bb31be2e44b06b34:0:MSDK (uuid:type<0-preifx/1-suffix>:message) <br>the suffix SEI for HEVCe can be inserted when CQP used or HRD disabled|
133134
| [-extbrc:<on,off,implicit>] | External BRC for AVC and HEVC encoders|
134135
| [-ExtBrcAdaptiveLTR:<on,off>] | Set AdaptiveLTR for implicit extbrcExample: `./sample_encode h265 -i InputYUVFile -o OutputEncodedFile -w width -h height -hw -p 2fca99749fdb49aeb121a5b63ef568f7`|

samples/sample_common/include/sample_utils.h

+85
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,91 @@ namespace QPFile {
212212
}
213213
}
214214

215+
namespace TCBRCTestFile {
216+
217+
enum ReaderStatus
218+
{
219+
READER_ERR_NONE,
220+
READER_ERR_NOT_INITIALIZED,
221+
READER_ERR_CODEC_UNSUPPORTED,
222+
READER_ERR_FILE_NOT_OPEN,
223+
READER_ERR_INCORRECT_FILE
224+
};
225+
226+
struct FrameInfo
227+
{
228+
mfxU32 displayOrder;
229+
mfxU32 targetFrameSize;
230+
};
231+
232+
// TCBRCTestFile reads target frame size in display order
233+
// from external text file (for encoding in Low delay BRC mode)
234+
235+
class Reader
236+
{
237+
public:
238+
mfxStatus Read(const msdk_string& strFileName, mfxU32 codecid);
239+
void ResetState();
240+
241+
mfxU32 GetTargetFrameSize(mfxU32 frameOrder) const;
242+
mfxU32 GetFramesNum() const;
243+
void NextFrame();
244+
std::string GetErrorMessage() const;
245+
246+
private:
247+
void ResetState(ReaderStatus set_sts);
248+
249+
ReaderStatus m_ReaderSts = READER_ERR_NOT_INITIALIZED;
250+
mfxU32 m_CurFrameNum = std::numeric_limits<mfxU32>::max();
251+
std::vector<FrameInfo> m_FrameVals{};
252+
};
253+
254+
inline bool get_line(std::ifstream& ifs, std::string& line)
255+
{
256+
std::getline(ifs, line, '\n');
257+
if (!line.empty() && line.back() == '\r')
258+
line.pop_back();
259+
return !ifs.fail();
260+
}
261+
inline size_t find_nth(const std::string& str, size_t pos, const std::string& needle, mfxU32 nth)
262+
{
263+
size_t found_pos = str.find(needle, pos);
264+
for (; nth != 0 && std::string::npos != found_pos; --nth)
265+
found_pos = str.find(needle, found_pos + 1);
266+
return found_pos;
267+
}
268+
269+
inline std::string ReaderStatusToString(ReaderStatus sts)
270+
{
271+
switch (sts)
272+
{
273+
case READER_ERR_NOT_INITIALIZED:
274+
return std::string("reader not initialized (TCBRCTestfile has not yet read the file)\n");
275+
case READER_ERR_FILE_NOT_OPEN:
276+
return std::string("failed to open file with TargetFrameSize parameters (check provided path in -tcbrcfile <path>)\n");
277+
case READER_ERR_INCORRECT_FILE:
278+
return std::string("incorrect file with frame parameters\n");
279+
case READER_ERR_CODEC_UNSUPPORTED:
280+
return std::string("h264 and h265 are supported now\n");
281+
default:
282+
return std::string();
283+
}
284+
}
285+
inline mfxU32 ReadDisplayOrder(const std::string& line)
286+
{
287+
size_t pos = find_nth(line, 0, ":", 0);
288+
if (pos != std::string::npos)
289+
return std::stoi(line.substr(0, pos));
290+
else
291+
return 0;
292+
}
293+
inline mfxU16 ReadTargetFrameSize(const std::string& line)
294+
{
295+
size_t pos = find_nth(line, 0, ":", 0);
296+
pos = (pos != std::string::npos) ? pos + 1 : 0;
297+
return static_cast<mfxU16>(std::stoi(line.substr(pos, line.size() - pos)));
298+
}
299+
}
215300
mfxStatus GetFrameLength(mfxU16 width, mfxU16 height, mfxU32 ColorFormat, mfxU32 &length);
216301

217302
bool IsDecodeCodecSupported(mfxU32 codecFormat);

samples/sample_common/src/sample_utils.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,62 @@ mfxU16 QPFile::Reader::GetCurrentFrameType() const { return m_FrameVals.at(m_
14971497
mfxU32 QPFile::Reader::GetFramesNum() const { return m_nFrames; }
14981498
void QPFile::Reader::NextFrame() { ++m_CurFrameNum; }
14991499

1500+
1501+
void TCBRCTestFile::Reader::ResetState(ReaderStatus set_sts)
1502+
{
1503+
m_CurFrameNum = std::numeric_limits<mfxU32>::max();
1504+
m_ReaderSts = set_sts;
1505+
m_FrameVals.clear();
1506+
}
1507+
1508+
mfxStatus TCBRCTestFile::Reader::Read(const msdk_string& strFileName, mfxU32 codecid)
1509+
{
1510+
m_ReaderSts = READER_ERR_NONE;
1511+
m_CurFrameNum = 0;
1512+
1513+
if (codecid != MFX_CODEC_AVC && codecid != MFX_CODEC_HEVC)
1514+
{
1515+
ResetState(READER_ERR_CODEC_UNSUPPORTED);
1516+
return MFX_ERR_NOT_INITIALIZED;
1517+
}
1518+
1519+
std::ifstream ifs(strFileName, msdk_fstream::in);
1520+
if (!ifs.is_open())
1521+
{
1522+
ResetState(READER_ERR_FILE_NOT_OPEN);
1523+
return MFX_ERR_NOT_INITIALIZED;
1524+
}
1525+
1526+
FrameInfo frameInfo{};
1527+
std::string line;
1528+
1529+
1530+
mfxU32 n = 0;
1531+
while (TCBRCTestFile::get_line(ifs, line))
1532+
{
1533+
frameInfo.displayOrder = TCBRCTestFile::ReadDisplayOrder(line);
1534+
frameInfo.targetFrameSize = TCBRCTestFile::ReadTargetFrameSize(line);
1535+
if (frameInfo.displayOrder==0)
1536+
frameInfo.displayOrder = n;
1537+
m_FrameVals.push_back(frameInfo);
1538+
n++;
1539+
}
1540+
return MFX_ERR_NONE;
1541+
}
1542+
std::string TCBRCTestFile::Reader::GetErrorMessage() const { return ReaderStatusToString(m_ReaderSts); }
1543+
mfxU32 TCBRCTestFile::Reader::GetTargetFrameSize(mfxU32 displayOrder) const
1544+
{
1545+
mfxU32 num = (mfxU32)m_FrameVals.size();
1546+
if (num == 0) return 0;
1547+
for (mfxU32 i = 0; i < num - 1; i++)
1548+
{
1549+
if (m_FrameVals.at(i + 1).displayOrder > displayOrder)
1550+
return m_FrameVals.at(i).targetFrameSize;
1551+
1552+
}
1553+
return m_FrameVals.at(num - 1).targetFrameSize;
1554+
}
1555+
15001556
mfxStatus ConvertFrameRate(mfxF64 dFrameRate, mfxU32* pnFrameRateExtN, mfxU32* pnFrameRateExtD)
15011557
{
15021558
MSDK_CHECK_POINTER(pnFrameRateExtN, MFX_ERR_NULL_PTR);

samples/sample_encode/include/pipeline_encode.h

+5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct sInputParams
111111
mfxU16 nEncTileCols; // number of columns for encoding tiling
112112

113113
msdk_string strQPFilePath;
114+
msdk_string strTCBRCFilePath;
114115

115116
MemType memType;
116117
bool bUseHWLib; // true if application wants to use HW MSDK library
@@ -167,6 +168,7 @@ struct sInputParams
167168
bool bSoftRobustFlag;
168169

169170
bool QPFileMode;
171+
bool TCBRCFileMode;
170172

171173
mfxU32 nTimeout;
172174
mfxU16 nPerfOpt; // size of pre-load buffer which used for loop encode
@@ -332,6 +334,7 @@ class CEncodingPipeline
332334
CSmplYUVReader m_FileReader;
333335
CEncTaskPool m_TaskPool;
334336
QPFile::Reader m_QPFileReader;
337+
TCBRCTestFile::Reader m_TCBRCFileReader;
335338

336339
MFXVideoSession m_mfxSession;
337340
MFXVideoENCODE* m_pmfxENC;
@@ -378,6 +381,7 @@ class CEncodingPipeline
378381
CHWDevice *m_hwdev;
379382

380383
bool m_bQPFileMode;
384+
bool m_bTCBRCFileMode;
381385

382386
bool isV4L2InputEnabled;
383387
#if (MFX_VERSION >= 1027)
@@ -446,6 +450,7 @@ class CEncodingPipeline
446450
virtual mfxU32 FileFourCC2EncFourCC(mfxU32 fcc);
447451

448452
void InitExtMVCBuffers(mfxExtMVCSeqDesc *mvcBuffer) const;
453+
mfxStatus ConfigTCBRCTest(mfxFrameSurface1* pSurf);
449454
};
450455

451456
#endif // __PIPELINE_ENCODE_H__

samples/sample_encode/src/pipeline_encode.cpp

+37-2
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,17 @@ mfxStatus CEncodingPipeline::InitMfxEncParams(sInputParams *pInParams)
528528
mfxStatus sts = m_QPFileReader.Read(pInParams->strQPFilePath, pInParams->CodecId);
529529
MSDK_CHECK_STATUS(sts, m_QPFileReader.GetErrorMessage().c_str());
530530
}
531+
m_bTCBRCFileMode = pInParams->TCBRCFileMode;
532+
if (m_bTCBRCFileMode)
533+
{
534+
mfxStatus sts = m_TCBRCFileReader.Read(pInParams->strTCBRCFilePath, pInParams->CodecId);
535+
MSDK_CHECK_STATUS(sts, m_TCBRCFileReader.GetErrorMessage().c_str());
536+
m_mfxEncParams.AsyncDepth = 1;
537+
m_mfxEncParams.mfx.RateControlMethod = MFX_RATECONTROL_VBR;
538+
m_mfxEncParams.mfx.GopRefDist = 1;
539+
pInParams->nNalHrdConformance = MFX_CODINGOPTION_OFF;
540+
pInParams->LowDelayBRC = MFX_CODINGOPTION_ON;
541+
}
531542

532543
// specify memory type
533544
if (D3D9_MEMORY == pInParams->memType || D3D11_MEMORY == pInParams->memType)
@@ -1345,7 +1356,7 @@ CEncodingPipeline::CEncodingPipeline()
13451356
m_bIsFieldSplitting = false;
13461357

13471358
m_bQPFileMode = false;
1348-
1359+
m_bTCBRCFileMode = false;
13491360
m_nEncSurfIdx = 0;
13501361
m_nVppSurfIdx = 0;
13511362
}
@@ -2295,6 +2306,13 @@ mfxStatus CEncodingPipeline::EncodeOneFrame(const ExtendedSurface& In, sTask*& p
22952306
InsertIDR(pTask->encCtrl, m_bInsertIDR);
22962307
m_bInsertIDR = false;
22972308

2309+
if (m_bTCBRCFileMode && In.pSurface)
2310+
{
2311+
sts = ConfigTCBRCTest(In.pSurface);
2312+
MSDK_CHECK_STATUS(sts, "TCBRC reset failed");
2313+
}
2314+
2315+
22982316
sts = InitEncFrameParams(pTask);
22992317
MSDK_CHECK_STATUS(sts, "ENCODE: InitEncFrameParams failed");
23002318

@@ -2328,7 +2346,24 @@ mfxStatus CEncodingPipeline::EncodeOneFrame(const ExtendedSurface& In, sTask*& p
23282346

23292347
return sts;
23302348
}
2331-
2349+
mfxStatus CEncodingPipeline::ConfigTCBRCTest(mfxFrameSurface1* pSurf)
2350+
{
2351+
mfxStatus sts = MFX_ERR_NONE;
2352+
if (m_bTCBRCFileMode && pSurf)
2353+
{
2354+
mfxU32 targetFrameSize = m_TCBRCFileReader.GetTargetFrameSize(pSurf->Data.FrameOrder);
2355+
if (targetFrameSize)
2356+
{
2357+
mfxU32 newBitrate = targetFrameSize * 8 * m_mfxEncParams.mfx.FrameInfo.FrameRateExtN / (m_mfxEncParams.mfx.FrameInfo.FrameRateExtD * 1000);
2358+
if (m_mfxEncParams.mfx.TargetKbps != newBitrate)
2359+
{
2360+
m_mfxEncParams.mfx.TargetKbps = (mfxU16)newBitrate;
2361+
sts = m_pmfxENC->Reset(&m_mfxEncParams);
2362+
}
2363+
}
2364+
}
2365+
return sts;
2366+
}
23322367
mfxStatus CEncodingPipeline::Run()
23332368
{
23342369
m_statOverall.StartTimeMeasurement();

samples/sample_encode/src/sample_encode.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ void PrintHelp(msdk_char *strAppName, const msdk_char *strErrorMessage, ...)
178178
msdk_printf(MSDK_STRING(" [-uncut] - do not cut output file in looped mode (in case of -timeout option)\n"));
179179
msdk_printf(MSDK_STRING(" [-dump fileName] - dump MSDK components configuration to the file in text form\n"));
180180
msdk_printf(MSDK_STRING(" [-qpfile <filepath>] - if specified, the encoder will take frame parameters (frame number, QP, frame type) from text file\n"));
181+
msdk_printf(MSDK_STRING(" [-tcbrctestfile <filepath>] - if specified, the encoder will take targetFrameSize parameters for TCBRC test from text file\n"));
181182
msdk_printf(MSDK_STRING(" [-usei] - insert user data unregistered SEI. eg: 7fc92488825d11e7bb31be2e44b06b34:0:MSDK (uuid:type<0-preifx/1-suffix>:message)\n"));
182183
msdk_printf(MSDK_STRING(" the suffix SEI for HEVCe can be inserted when CQP used or HRD disabled\n"));
183184
#if (MFX_VERSION >= MFX_VERSION_NEXT)
@@ -242,6 +243,13 @@ mfxStatus ParseAdditionalParams(msdk_char *strInput[], mfxU8 nArgNum, mfxU8& i,
242243
}
243244
i += 1;
244245
}
246+
else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-tcbrctestfile")))
247+
{
248+
VAL_CHECK(i + 1 >= nArgNum, i, strInput[i]);
249+
pParams->TCBRCFileMode = true;
250+
pParams->strTCBRCFilePath = strInput[++i];
251+
i += 1;
252+
}
245253

246254
else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-BaseLayerPID")))
247255
{
@@ -426,6 +434,7 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p
426434
pParams->EncodeFourCC = 0;
427435
pParams->nPRefType = MFX_P_REF_DEFAULT;
428436
pParams->QPFileMode = false;
437+
pParams->TCBRCFileMode = false;
429438
pParams->BitrateLimit = MFX_CODINGOPTION_OFF;
430439
#if (MFX_VERSION >= 1027)
431440
pParams->RoundingOffsetFile = NULL;
@@ -743,7 +752,7 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p
743752
pParams->QPFileMode = true;
744753
pParams->strQPFilePath = strInput[++i];
745754
}
746-
755+
747756
#if D3D_SURFACES_SUPPORT
748757
else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-d3d")))
749758
{

0 commit comments

Comments
 (0)