📄 video_enc_con.cpp
字号:
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright (c) 2005-2007 Intel Corporation. All Rights Reserved.
//
#include "utils.h"
#include "vm_time.h"
#include "vm_strings.h"
#include "umc_sys_info.h"
#include "vm_sys_info.h"
#include "ippvc.h"
//#define ALPHA_BLENDING_H264
using namespace UMC;
#define MAX_FILELEN 1024
class VideoEncodingTest : public VideoEncodingSample
{
public:
virtual Status PutOutputData(MediaData *out);
virtual Ipp32s Main(Ipp32s argc, vm_char *argv[]);
virtual Ipp64f GetTick();
Status ChangeParams();
void DumpPerf0();
void DumpPerf1();
VideoEncodingTest()
{
mVideoStreamType = UNDEF_VIDEO;
mWidth = 0;
mHeight = 0;
maxNumFrames = 0;
BitRate = -1; // (BitRate < 0) means constant quality!
FrameRate = 0;
chroma_format = 1; // 4:2:0
SrcFileName[0] = 0;
ParFileName = NULL;
DstFileName = NULL;
PerfFileName = NULL;
refEncodedFile = NULL;
inpLogFile = NULL;
outLogFile = NULL;
resLogFile = NULL;
perf_file = NULL;
numThreads = 1;
AlphaFileName = NULL;
ReconFileName = NULL;
fullTime = 0;
tickDuration = 1.0/(Ipp64f)vm_time_get_frequency();
*codecName = 0;
FrameSize = NULL;
lastFrameNum = 0;
lastPTS = -1.0;
}
~VideoEncodingTest();
// Params
VideoStreamType mVideoStreamType;
Ipp32s mWidth;
Ipp32s mHeight;
Ipp32s maxNumFrames;
Ipp32s BitRate;
Ipp64f FrameRate;
Ipp32s chroma_format;
vm_char SrcFileName[MAX_FILELEN];
vm_char *ParFileName;
vm_char *DstFileName;
vm_char *AlphaFileName;
vm_char *ReconFileName; // Name of a file to save reconstructed frame
vm_char *PerfFileName;
vm_char *refEncodedFile;
vm_char *inpLogFile;
vm_char *outLogFile;
vm_char *resLogFile;
vm_file *perf_file;
Ipp32s numThreads;
// Info
SysInfo m_csysinfo;
Ipp64f fullTime;
Ipp64f tickDuration;
vm_char codecName[15];
Ipp32s *FrameSize;
Ipp32s lastFrameNum;
Ipp64f lastPTS;
};
Ipp64f VideoEncodingTest::GetTick()
{
return (Ipp64f)vm_time_get_tick() * tickDuration;
}
VideoEncodingTest::~VideoEncodingTest()
{
if (pCodec) {
delete pCodec;
pCodec = NULL;
}
if (pEncoderParams) {
delete pEncoderParams;
pEncoderParams = NULL;
}
if (FrameSize) {
delete [] FrameSize;
FrameSize = NULL;
}
if (dstFile) {
vm_file_fclose(dstFile);
dstFile = NULL;
}
}
Status VideoEncodingTest::PutOutputData(MediaData *out)
{
if (!out) { // at EOF
return UMC_ERR_NULL_PTR;
}
Ipp32s len = (Ipp32s)out->GetDataSize();
#if 1
Ipp64f PTS = out->GetTime();
if (!FrameSize && numFramesToEncode < (1 << 16)) {
FrameSize = new Ipp32s[numFramesToEncode];
}
if (FrameSize && len && lastFrameNum < numFramesToEncode) {
if (lastFrameNum == 0 || PTS > lastPTS) {
FrameSize[lastFrameNum] = len;
lastPTS = PTS;
} else {
FrameSize[lastFrameNum] = FrameSize[lastFrameNum-1];
FrameSize[lastFrameNum-1] = len;
}
lastFrameNum++;
}
#endif
m_csysinfo.GetCpuUsage();
if (!(mFramesEncoded % 10)) {
vm_string_printf(VM_STRING("%d."), mFramesEncoded);
}
#ifdef CHANGE_PARAMS
ChangeParams();
#endif
return VideoEncodingSample::PutOutputData(out);
}
Status VideoEncodingTest::ChangeParams() // check set bitrate/set frame rate
{
Status st;
VideoEncoderParams newpar;
st = pCodec->GetInfo(&newpar);
vm_string_printf(VM_STRING("Q=%d%% "), newpar.qualityMeasure);
if(st != UMC_OK)
return st;
if(mFramesEncoded%9==1) {
//newpar.info.framerate *= 0.9; // random
newpar.info.bitrate = newpar.info.bitrate * 3 / 4; // random
if(newpar.qualityMeasure<40)
newpar.info.bitrate *= 5; // random
vm_string_printf(VM_STRING("SetParams Called for bitrate = %d\n"),newpar.info.bitrate);
st = pCodec->SetParams(&newpar);
//if(st != UMC_OK) // not supported everywhere
// return st;
}
if(mFramesEncoded%3==1) {
newpar.numThreads = (newpar.numThreads & 3) + 1;
vm_string_printf(VM_STRING("SetParams Called for threads = %d\n"),newpar.numThreads);
st = pCodec->SetParams(&newpar);
}
if (mFramesEncoded%25==1) {
pCodec->Reset();
}
{ // close caption
MediaData cc;
vm_char text[30];
vm_string_sprintf(text, VM_STRING("CCaption %d"), mFramesEncoded);
cc.SetBufferPointer((Ipp8u*)text, sizeof(text));
cc.SetDataSize(vm_string_strlen(text)+1);
pCodec->GetFrame(&cc, 0);
}
return UMC_OK;
}
int PrintHelp(vm_char *prog_name, vm_char *err_message)
{
vm_string_printf(VM_STRING("Error: %s\n"), err_message);
vm_string_printf(VM_STRING("Usage1: %s [m2|m4|h264|h263|h261|dv_sd|dv_50|dv_hd|vc1] [Options] InputParFile OutputEncodedFile\n"), prog_name);
vm_string_printf(VM_STRING("Usage2: %s [m2|m4|h264|h263|h261|dv_sd|dv_50|dv_hd|vc1] [Options] -i InputYUVFile -o OutputEncodedFile\n"), prog_name);
vm_string_printf(VM_STRING("Options: \n"));
#if defined ALPHA_BLENDING_H264
vm_string_printf(VM_STRING(" [-a file] - file for auxiliary pictures for alpha blending for H.264 encoder\n"));
#endif // ALPHA_BLENDING_H264
#if defined UMC_ENABLE_H264_VIDEO_ENCODER && defined SAVE_RECON
vm_string_printf(VM_STRING(" [-s file] - file for reconstructed frames (H.264 only!!!).\n"));
#endif // UMC_ENABLE_H264_VIDEO_ENCODER && SAVE_RECON
vm_string_printf(VM_STRING(" [-i inputFile] - input YUV video file\n"));
vm_string_printf(VM_STRING(" [-w width] - video width\n"));
vm_string_printf(VM_STRING(" [-h height] - video height\n"));
vm_string_printf(VM_STRING(" [-f frameRate] - video frame rate (frames per second)\n"));
vm_string_printf(VM_STRING(" [-b bitRate] - encoded bit rate (bits per second)\n"));
vm_string_printf(VM_STRING(" [-n numFrames] - max frames to encode\n"));
vm_string_printf(VM_STRING(" [-t num] - number of threads for encoding\n"));
vm_string_printf(VM_STRING(" [-r] - enables threading of read/encode/write\n"));
vm_string_printf(VM_STRING(" [-y dir] - directory of YUV files\n"));
vm_string_printf(VM_STRING(" [-u file] - file for dumping perfomance info\n"));
vm_string_printf(VM_STRING(" [-p file.csv] - file for dumping perfomance info into csv format\n"));
vm_string_printf(VM_STRING("Note: Options -i,-w,-h,-f,-b,-n override corresponding values inside InputParFile.\n"));
vm_string_printf(VM_STRING(" If InputParFile not specified, at least options -w,-h should be specified and -o should be added before OutputEncodedFile.\n"));
vm_string_printf(VM_STRING("\n"));
return 1;
}
static int read_line(vm_char *file_name, int line_num, vm_char *line, int /*max_len*/)
{
vm_file *f;
int i;
if (NULL == (f = vm_file_open(file_name, VM_STRING("rt")))) {
vm_file_fprintf(vm_stderr, __VM_STRING("Couldn't open file %s\n"), file_name);
return 1;
}
for (i = 0; i < line_num; i++) {
if (NULL == vm_file_fgets(line, MAX_FILELEN-1, f)) {
vm_file_fclose(f);
return 1;
}
}
vm_file_fclose(f);
return 0;
}
#define GET_OPTION_POINTER(PTR) \
if (2 == vm_string_strlen(argv[i])) { \
i++; \
if (argv[i][0]=='-') { \
return PrintHelp(argv[0], VM_STRING("in options")); \
} else { \
PTR = argv[i]; \
} \
} else { \
PTR = argv[i] + 2; \
}
#define DVEncoderParams VideoEncoderParams
#define DV50EncoderParams VideoEncoderParams
#define DV100EncoderParams VideoEncoderParams
#define CREATE_ENCODER(CODEC) \
vm_string_strcpy(codecName, VM_STRING(#CODEC)); \
pCodec = new CODEC##VideoEncoder(); \
if (ParFileName) { \
pEncoderParams = new CODEC##EncoderParams; \
}
Ipp32s VideoEncodingTest::Main(Ipp32s argc, vm_char *argv[])
{
vm_char *pVal;
vm_char *optFile = NULL;
Ipp64f t_start;
Ipp32s i, j;
Ipp64f size_ratio = 0;
vm_char line[MAX_FILELEN] = {0};
t_start = GetTick();
if (ippStaticInit() < ippStsNoErr) {
vm_file_fprintf(vm_stderr, VM_STRING("Error: Can't initialize ipp libs!\n"));
return 5;
}
for (j = 0; j < 2; j++) {
vm_char optLine[256], *p, *p_end;
vm_char *argv_[256];
if (j) {
if (!optFile) {
break;
} else {
vm_file *f = vm_file_open(optFile, VM_STRING("r"));
if (!f) {
vm_file_fprintf(vm_stderr, VM_STRING("Cann't open opt file %s\n"), optFile);
return PrintHelp(argv[0], VM_STRING("Cann't open opt file"));
}
*optLine = 0;
vm_file_fgets(optLine, 255, f);
vm_file_fclose(f);
p = optLine;
p_end = optLine + vm_string_strlen(optLine);
argc = 1;
argv = argv_;
argv_[0] = argv[0];
for (;;) {
argv[argc] = p;
argc++;
while ((p < p_end) && (*p != ' ')) p++;
*p++ = 0;
while ((p < p_end) && (*p == ' ')) p++;
if (p >= p_end) break;
}
}
}
/*
for (i = 1; i < argc; i++) {
printf("Option '%s'\n", argv[i]);
}
*/
for (i = 1; i < argc; i++)
{
if ('-' != argv[i][0]) {
if (UMC_OK == GetVideoType(argv[i], &mVideoStreamType)) continue;
if (NULL == ParFileName) {
ParFileName = argv[i];
} else {
DstFileName = argv[i];
}
continue;
}
if (UMC_OK == GetVideoType(argv[i] + 1, &mVideoStreamType)) continue;
if (UMC_OK == GetFormatType(argv[i] + 1, &mSrcFileColorFormat)) continue;
switch (argv[i][1])
{
case 'y':
i++;
vm_string_strcpy(SrcFileName, argv[i]);
p = SrcFileName + vm_string_strlen(SrcFileName) - 1;
if (*p != '\\' && *p != '/') {
vm_string_strcat(SrcFileName, VM_STRING("\\"));
}
break;
case 'p':
case 'P':
GET_OPTION_POINTER(PerfFileName);
break;
case 'w':
GET_OPTION_POINTER(pVal);
mWidth = (Ipp32s)vm_string_atol(pVal);
break;
case 'h':
GET_OPTION_POINTER(pVal);
mHeight = (Ipp32s)vm_string_atol(pVal);
break;
case 'f':
GET_OPTION_POINTER(pVal);
vm_string_sscanf(pVal, VM_STRING("%lf"), &FrameRate);
break;
case 'b':
GET_OPTION_POINTER(pVal);
BitRate = (Ipp32s)vm_string_atol(pVal);
break;
case 'n':
GET_OPTION_POINTER(pVal);
maxNumFrames = (Ipp32s)vm_string_atol(pVal);
break;
case 'i':
GET_OPTION_POINTER(pVal);
vm_string_strcpy(SrcFileName, pVal);
break;
case 'o':
GET_OPTION_POINTER(pVal);
DstFileName = pVal;
break;
case 't':
GET_OPTION_POINTER(pVal);
numThreads = (Ipp32s)vm_string_atol(pVal);
break;
#if defined ALPHA_BLENDING_H264
case 'a':
GET_OPTION_POINTER(AlphaFileName);
break;
#endif // ALPHA_BLENDING_H264
case 's':
GET_OPTION_POINTER(ReconFileName);
break;
case 'u':
i++;
perf_file = vm_file_open(argv[i], VM_STRING("w"));
break;
case 'z':
i++;
optFile = argv[i];
break;
/////////////////
case 'c':
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -