Intel® Integrated Performance Primitives
Deliberate problems developing high-performance vision, signal, security, and storage applications.
6711 Discussions

Mpeg4Decoder: Some .avi file can't be Decoded.

antony0604
Beginner
244 Views

I download w_ipp-samples_p_5.3.095.zip, and try to implement the Mpeg4 Encoder/Decoder. All the .avi(produce from the Mpeg4 Encoder ) files can be played in Window Media Player, but some .avi file cann't be decode by Mpeg4 Decoder. For example two avi file: their format is all the same as follow : mpeg4 encode, 720x288 , 5fps, video length is 5 frames. The channel_0_5_1.avi can be decoded successly be Mpeg4 Decoder, but channel_0_5_bad_1.avi cann't not. Is anyone can tell me how to resolve this problem.

Here is the avi link : http://www.sunvision.cc/Temp/mpeg4encode_avi.rar

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Follow is the Mpeg4 Decoder source code:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include
#include
#include "ippdefs.h"
#include "ippcore.h"
#include "ipps.h"
#include "ippi.h"
#include "ippj.h"
#include "ippcc.h"
#include "vm_time.h"
#include "umc_defs.h"
#include "umc_structures.h"
#include "umc_data_reader.h"
#include "umc_splitter.h"
#include "umc_video_decoder.h"
#include "umc_video_encoder.h"
#include "umc_video_data.h"
#include "umc_video_render.h"
#include "umc_file_writer.h"

#include "umc_file_reader.h"
#include "umc_fio_reader.h"
#include "umc_avi_splitter.h"
#include "umc_mjpeg_video_decoder.h"
#include "umc_mpeg4_video_decoder.h"
#include "umc_mpeg4_video_encoder.h"
#include "fw_video_render.h"

using namespace UMC;

static void copyright(void)
{
vm_string_printf(VM_STRING(" Intel Media processing sample based on "));
vm_string_printf(VM_STRING(" UMC and Intel Integrated PerformancePrimitives "));
vm_string_printf(VM_STRING("Copyright(c) 2005-2007 Intel Corporation. AllRights Reserved. "));
return;
} // copyright()

static void ipp_version(void)
{
const IppLibraryVersion* version;
vm_string_printf(VM_STRING("Intel Integrated Performance Primitives "));

/* ippCore version info */
version = ippGetLibVersion();
vm_string_printf(VM_STRING(" %s, %s {%d.%d.%d.%d}, build date %s "),
version->Name,
version->Version,
version->major,version->minor,version->majorBuild,version->build,
version->BuildDate);

/* ippSP version info */
version = ippsGetLibVersion();
vm_string_printf(VM_STRING(" %s, %s {%d.%d.%d.%d}, build date %s "),
version->Name,
version->Version,
version->major,version->minor,version->majorBuild,version->build,
version->BuildDate);

/* ippIP version info */
version = ippiGetLibVersion();
vm_string_printf(VM_STRING(" %s, %s {%d.%d.%d.%d}, build date %s "),
version->Name,
version->Version,
version->major,version->minor,version->majorBuild,version->build,
version->BuildDate);

/* ippJP vers ion info */
version = ippjGetLibVersion();
vm_string_printf(VM_STRING(" %s, %s {%d.%d.%d.%d}, build date %s "),
version->Name,
version->Version,
version->major,version->minor,version->majorBuild,version->build,
version->BuildDate);

/* ippCC version info */
version = ippccGetLibVersion();
vm_string_printf(VM_STRING(" %s, %s {%d.%d.%d.%d}, build date %s "),
version->Name,
version->Version,
version->major,version->minor,version->majorBuild,version->build,
version->BuildDate);
vm_string_printf(VM_STRING(" "));

return;
} // ipp_version()

UMC::Status
InitDataReader(
DataReader* pDataReader,
Ipp8u* file_name)
{
FileReaderParams reader_params;
reader_params.m_portion_size = 0;
vm_string_strcpy(reader_params.m_file_name, (const char*)file_name);
return pDataReader->Init(&reader_params);
} // InitDataReader()

UMC::Status
InitAVISplitter(
Splitter* pAVISplitter,
DataReader* pDataReader)
{
SplitterParams spl_params;
spl_params.m_lFlags = UMC::VIDEO_SPLITTER; // want to exctract only video
spl_params.m_pDataReader = pDataReader;
return pAVISplitter->Init(spl_params);
} // InitAVISplitter()

UMC::Status
InitVideoDecoder(
VideoDecoder* pVideoDecoder,
VideoStreamInfo* video_info, int iVideoType)
{
if (video_info->stream_type != iVideoType) //UMC::MJPEG_VIDEO
return UMC_ERR_FAILED;
VideoDecoderParams dec_params;
dec_params.info = *video_info;
dec_params.lFlags = 0;
dec_params.numThreads = 1;
return pVideoDecoder->Init(&dec_params);
} // InitMJPEGVideoDecoder()

UMC::Status
InitVideoRender(
VideoRender* pVideoRender,
VideoStreamInfo* video_info, char* sOutputName)
{
UMC::Status status;
//use FWVideoRenderParams to replace VideoRenderParams, because it can assign the output filename
//VideoRenderParams : default output name: output.yuv
//VideoRenderParams render_params;
FWVideoRenderParams render_params;
render_params.pOutFile = sOutputName;
status = render_params.out_data_template.Init(
video_info->clip_info.width,
video_info->clip_info.height,
video_info->color_format);
if(UMC_OK != status)
return status;
return pVideoRender->Init(&render_params);
} // InitVideoRender()

UMC::Status
DecodeVideo(vm_char* input_file, char* sOutputName)
{
Ipp32u track;
Ipp32s nframes;
vm_tick t0;
vm_tick t1;
vm_tick total;
vm_tick freq;
Ipp64f fps;
FIOReader src;
AVISplitter avi_spl;
SplitterInfo* spl_info;
VideoStreamInfo* video_info;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// if your file format change, you must change the Decoder
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MPEG4VideoDecoder VedioDecoder;//MJPEGVideo Decoder,MPEG4VideoDecoder

FWVideoRender dst;
MediaData in;
VideoData out;
UMC::Status umcRes;

// Initialize data reader component
umcRes = InitDataReader(&src, (Ipp8u *)input_file);
if (umcRes != UMC_OK)
return umcRes;

// Initialize splitter component
umcRes = InitAVISplitter(&avi_spl, &src);
if (umcRes != UMC_OK)
return umcRes;

// get stream info
umcRes = avi_spl.GetInfo(&spl_info);
if (umcRes != UMC_OK)
return umcRes;

// find required track
int iVideoStreamType = MPEG4_VIDEO;
for (track = 0; track < spl_info->m_nOfTracks; track++){
if (TRACK_MPEG4V == spl_info->m_ppTrackInfo[track]->m_Type) {
iVideoStreamType = MPEG4_VIDEO;
break;
} else
if (TRACK_MJPEG == spl_info->m_ppTrackInfo[track]->m_Type) {
iVideoStreamType = MJPEG_VIDEO;
break;
}
}

// track of MJPEG type is not found
if (track >= spl_info->m_nOfTracks)
return UMC_ERR_INVALID_STREAM;

// get VideoStreamInfo structure
video_info = (VideoStreamInfo*)spl_info->m_ppTrackInfo[track]->m_pStreamInfo;
video_info->color_format = RGB24;//YUY2,RGB24,YUV420,YUV422
vm_string_printf(VM_STRING("file: %s "),input_file);
vm_string_printf(VM_STRING(" video stream "));
vm_string_printf(VM_STRING(" type :%s "),GetVideoTypeString(spl_info->m_video_info.stream_type));
vm_string_printf(VM_STRING(" dims : %dx%d "),spl_info->m_video_info.clip_info.width,spl_info->m_video_info.clip_info.height);
vm_string_printf(VM_STRING(" color : %s "),GetFormatTypeString(spl_info->m_video_info.color_format));

// Initialize video decoder component
umcRes = InitVideoDecoder(&VedioDecoder, video_info,iVideoStreamType); // MJPEG_VIDEO,MPEG4_VIDEO
if (umcRes != UMC_OK)
return umcRes;

// Initialize video render component
umcRes = InitVideoRender(&dst, video_info,sOutputName);
if (umcRes != UMC_OK)
return umcRes;

freq = vm_time_get_frequency();
total = 0;
nframes = 0;
// decoding & rendering loop (frame by frame)
while(umcRes == UMC_OK || umcRes == UMC_ERR_NOT_ENOUGH_DATA)
{
while (UMC_ERR_NOT_ENOUGH_DATA == (umcRes = avi_spl.GetNextData(&in,track)))
vm_time_sleep(5);
if (umcRes != UMC_OK)
break;
umcRes = dst.LockInputBuffer(&out); // Lock VideoData
if (umcRes != UMC_OK)
break;
t0 = vm_time_get_tick();
umcRes = VedioDecoder.GetFrame(&in, &out); //MediaData ==> VideoData
if (umcRes == UMC::UMC_ERR_END_OF_STREAM || (umcRes != UMC::UMC_OK && umcRes !=UMC::UMC_ERR_NOT_ENOUGH_DATA))
break;
t1 = vm_time_get_tick();
total += (t1 - t0);
if (dst.UnLockInputBuffer(&out) != UMC_OK)
break;

//if (umcRes != UMC_OK)
//continue;
vm_string_printf(VM_STRING("frame# %d, FPS - %4.1f "),nframes,
(Ipp64f)1.0 / ((t1 - t0) / (Ipp64f)freq));
if(umcRes == UMC_OK)
{
Ipp64f time = -1;
while (UMC_ERR_TIMEOUT == dst.GetRenderFrame(&time));
umcRes = dst.RenderFrame();
if (umcRes != UMC::UMC_OK)
break;
}
nframes++;
} // while()
fps = (Ipp64f)(freq / ((Ipp64f)total / nframes));
vm_string_printf(VM_STRING("processed %d frames, with avg performance %4.1f FPS "),nframes, fps);

return umcRes;
} // DecodeVideo()

int main(int argc, char* argv[])
{
ippStaticInit();
copyright();
ipp_version();

if(argc > 1)
DecodeVideo("channel_0.avi","channel_01.rgb");
else
vm_string_printf(VM_STRING("expected AVI file name"));

return 0;
} // main()

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//End of the Mpeg4 Decoder source code:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Follow is the Mpeg4 Encoder source code:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#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
extern void AVI_Muxer(int MPEG4_W,int MPEG4_H,int MPEG4_FR,char *MPEG4filename,char *AVIfilename);

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; // 1==>4:2:0 , 2==>4:2:2
SrcFileName[0] = 0;
ParFileName = NULL;
DstFileName = NULL;
PerfFileName = NULL;
refEncodedFile = NULL;
inpLogFile = NULL;
outLogFile = NULL;
&n bsp; 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 "),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 "),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 "), err_message);
vm_string_printf(VM_STRING("Usage1: %s [m2|m4|h264|h263|h261|dv_sd|dv_50|dv_hd|vc1] [Options] InputParFile OutputEncodedFile "), 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 "), prog_name);
vm_string_printf(VM_STRING("Options: "));
#if defined ALPHA_BLENDING_H264
vm_string_printf(VM_STRING(" [-a file] - file for auxiliary pictures for alpha blending for H.264 encoder "));
#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!!!). "));
#endif // UMC_ENABLE_H264_VIDEO_ENCODER && SAVE_RECON
vm_string_printf(VM_STRING(" [-i inputFile] - input YUV video file "));
vm_string_printf(VM_STRING(" [-w width] - video width "));
vm_string_printf(VM_STRING(" [-h height] - video height "));
vm_string_printf(VM_STRING(" [-f frameRate] - video frame rate (frames per second) "));
vm_string_printf(VM_STRING(" [-b bitRate] - encoded bit rate (bits per second) "));
vm_string_printf(VM_STRING(" [-n numFrames] - max frames to encode "));
vm_string_printf(VM_STRING(" [-t num] - number of threads for encoding "));
vm_string_printf(VM_STRING(" [-r] - enable s threading of read/encode/write "));
vm_string_printf(VM_STRING(" [-y dir] - directory of YUV files "));
vm_string_printf(VM_STRING(" [-u file] - file for dumping perfomance info "));
vm_string_printf(VM_STRING(" [-p file.csv] - file for dumping perfomance info into csv format "));
vm_string_printf(VM_STRING("Note: Options -i,-w,-h,-f,-b,-n override corresponding values inside InputParFile. "));
vm_string_printf(VM_STRING(" If InputParFile not specified, at least options -w,-h should be specified and -o should be added before OutputEncodedFile. "));
vm_string_printf(VM_STRING(" "));
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 "), 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++;
if (argv[0]=='-') {
return PrintHelp(argv[0], VM_STRING("in options"));
} else {
PTR = argv;
}
} else {
PTR = argv + 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();&n bsp;
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! "));
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 "), 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' ", argv);
}
*/
for (i = 1; i < argc; i++)
{
if ('-' != argv[0]) {
if (UMC_OK == GetVideoType(argv, &mVideoStreamType))
continue;
if (NULL == ParFileName) {
ParFileName = argv;
} else {
&n bsp; DstFileName = argv;
}
continue;
}
if (UMC_OK == GetVideoType(argv + 1, &mVideoStreamType)) continue;
if (UMC_OK == GetFormatType(argv + 1, &mSrcFileColorFormat)) continue;
switch (argv[1])
{
case 'y':
i++;
vm_string_strcpy(SrcFileName, argv);
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);
Ds tFileName = 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, VM_STRING("w"));
break;
case 'z':
i++;
optFile = argv;
break;
/////////////////
case 'c':
i++;
refEncodedFile = argv;
break;
case 'l':
i++;
inpLogFile = argv;
break;
case 'v':
i++;
outLogFile = argv;
break;
case 'g':
i++;
resLogFile = argv;
break;
/////////////////
//case 'h':
case '?':
default:
return PrintHelp(argv[0], VM_STRING("Unknown options"));
}
}
}

if (!DstFileName) {
return PrintHelp(argv[0], VM_STRING("Destination file name not found"));
}

if (!ParFileName && (!mWidth || !mHeight)) {
return PrintHelp(argv[0], VM_STRING("at least ParFile or options -w,-h should be specified"));
}

if (ParFileName) { // read first line of ParFile
if (read_line(ParFileName, 1, line, MAX_FILELEN)) {
return 3;
}
}

if (mVideoStreamType == UNDEF_VIDEO) {
if (!ParFileName) {
mVideoStreamType = MPEG2_VIDEO;
} else {
if (vm_string_strstr(line, VM_STRING("MPEG-2"))) mVideoStreamType = MPEG2_VIDEO; else
if (vm_string_strstr(line, VM_STRING("MPEG-4"))) mVideoStreamType = MPEG4_VIDEO; else
if (vm_string_strstr(line, VM_STRING("H.261"))) mVideoStreamType = H261_VIDEO; else
if (vm_string_strstr(line, VM_STRING("H.263"))) mVideoStreamType = H263_VIDEO; else
if (vm_string_strstr(line, VM_STRING("H.264"))) mVideoStreamType = H264_VIDEO; else {
return PrintHelp(argv[0], VM_STRING("Unknown codec"));
}
}
}

if (ParFileName && vm_string_strstr(line, VM_STRING("UseDefaultParameters"))) {
ParFileName = NULL; // use default parameters
}

switch (mVideoStreamType) {
#ifdef UMC_ENABLE_MPEG2_VIDEO_ENCODER
case MPEG2_VIDEO:
CREATE_ENCODER(MPEG2)
break;
#endif
#ifdef UMC_ENABLE_MPEG4_VIDEO_ENCODER
case MPEG4_VIDEO:
CREATE_ENCODER(MPEG4)
break;
#endif
#ifdef UMC_ENABLE_H261_VIDEO_ENCODER
case H261_VIDEO:
CREATE_ENCODER(H261)
break;
#endif
#ifdef UMC_ENABLE_H263_VIDEO_ENCODER
case H263_VIDEO:
CREATE_ENCODER(H263)
break;
#endif
#ifdef UMC_ENABLE_H264_VIDEO_ENCODER
case H264_VIDEO:
CREATE_ENCODER(H264)
break;
#endif
#ifdef UMC_ENABLE_DV_VIDEO_ENCODER
case DIGITAL_VIDEO_SD:
CREATE_ENCODER(DV)
break;
#endif
#ifdef UMC_ENABLE_DV50_VIDEO_ENCODER
case DIGITAL_VIDEO_50:
CREATE_ENCODER(DV50)
mColorFormat = YUV422;
break;
#endif
#ifdef UMC_ENABLE_DVHD_VIDEO_ENCODER
case DIGITAL_VIDEO_HD:
CREATE_ENCODER(DV100)
mColorFormat = YUV422;
break;
#endif
#ifdef UMC_ENABLE_VC1_VIDEO_ENCODER
case VC1_VIDEO:
CREATE_ENCODER(VC1)
break;
#endif
default:
vm_file_fprintf(vm_stderr, VM_STRING("Codec (%d) not supported "), (int)mVideoStreamType);
return PrintHelp(argv[0], VM_STRING("Codec not supported"));
}

if (ParFileName) {
Status res;
res = pEncoderParams->ReadParamFile(ParFileName);
if (res < UMC_OK) { // &l t; means error
if (res == UMC_ERR_OPEN_FAILED) {
vm_file_fprintf(vm_stderr, VM_STRING("Error: Can't open par file %s "), ParFileName);
} else {
vm_file_fprintf(vm_stderr, VM_STRING("Error: Can't parse par file %s "), ParFileName);
}
return 3;
}
#ifdef UMC_ENABLE_MPEG2_VIDEO_ENCODER
if (mVideoStreamType == MPEG2_VIDEO) {
//chroma_format = ((MPEG2EncoderParams*)pEncoderParams)->chroma_format;
mColorFormat = pEncoderParams->info.color_format;
}
#endif
#ifdef UMC_ENABLE_VC1_VIDEO_ENCODER
if (mVideoStreamType == VC1_VIDEO) {
if(!maxNumFrames)
{
if (((VC1EncoderParams*)pEncoderParams)->m_uiNumFrames)
maxNumFrames = ((VC1EncoderParams*)pEncoderParams)->m_uiNumFrames;
}
}
#endif
#ifdef UMC_ENABLE_H264_VIDEO_ENCODER
if (mVideoStreamType == H264_VIDEO) {
H264EncoderParams *params = (H264EncoderParams*)pEncoderParams;
chroma_format = params->chroma_format_idc;
mBitDepth = (IPP_MAX(params->bit_depth_luma, params->bit_depth_chroma) > 8)? 16 : 8;
mBitDepthAlpha = (params->bit_depth_aux > 8)? 16: 8;
numFramesToEncode = params->numFramesToEncode;
#if 0
if (ReconFileName) {
((H264EncoderParams*)pEncoderParams)->m_SaveReconFlag = 1;
vm_string_strncpy(((H264EncoderParams*)pEncoderParams)->m_ReconFileName, ReconFileName, MAXIMUM_PATH);
printf("ReconFileName = "%s" ", ((H264EncoderParams*)pEncoderParams)->m_ReconFileName);
}
#endif
mColorFormat = (chroma_format == 0)? GRAY
: (chroma_format == 2)? YUV422
: (chroma_format == 3)? YUV444
: YUV420; // Accept monochromes with UV components filled with dummy values.
}
#endif
}

if ((!SrcFileName[0] || SrcFileName[vm_string_strlen(SrcFileName) - 1] == '') && ParFileName) {
// read stream filename from second line of ParFile
vm_char SrcName[MAX_FILELEN];

if (read_line(ParFileName, 2, line, MAX_FILELEN)) {
return 3;
}
  ; vm_string_sscanf(line, VM_STRING("%s"), SrcName);
vm_string_strcat(SrcFileName, SrcName); // concatenate
}
#ifdef UMC_ENABLE_VC1_VIDEO_ENCODER
if (ParFileName) {
if (mVideoStreamType == VC1_VIDEO) {
((VC1EncoderParams*)pEncoderParams)->m_pStreamName = SrcFileName;
}
}
#endif
if (!ParFileName) {
pEncoderParams = new VideoEncoderParams;
pEncoderParams->info.color_format = (chroma_format == 0)? GRAY
: (chroma_format == 2)? YUV422
: (chroma_format == 3)? YUV444
: YUV420; // Accept monochromes with UV components filled with dummy values.

if (!maxNumFrames) {
maxNumFrames = 0x7fffffff;
}
if (!FrameRate) {
FrameRate = 30;
}
if (BitRate < 0) {
BitRate = 5000000;
}
}

// override param's if non-zero
if (mWidth && mHeight) {
pEncoderParams->info.clip_info.width = mWidth;
pEncoderParams->info.clip_info.height = mHeight;
} else {
mWidth = pEncoderParams->info.clip_info.width;
mHeight = pEncoderParams->info.clip_info.height;
}
if (maxNumFrames) {
numFramesToEncode = maxNumFrames;
} else {
maxNumFrames = numFramesToEncode;
}
if (FrameRate) {
pEncoderParams->info.framerate = FrameRate;
} else {
FrameRate = pEncoderParams->info.framerate;
}
if (BitRate >= 0) {
pEncoderParams->info.bitrate = BitRate;
} else {
BitRate = pEncoderParams->info.bitrate;
}
pEncoderParams->numThreads = numThreads;

srcFile = vm_file_open(SrcFileName, VM_STRING("rb"));
if (NULL == srcFile) {
vm_file_fprintf(vm_stderr, VM_STRING("Error opening file %s"), SrcFileName);
return 8;
}

dstFile = vm_file_open(DstFileName, VM_STRING("wb"));
if (NULL == dstFile) {
vm_file_fprintf(vm_stderr, VM_STRING("Error: Can't open output file '%s' "), DstFileName);
return 9;
}

if (AlphaFileName) {
srcAlphaFile = vm_file_open(AlphaFileName, VM_STRING("rb"));
if (NULL == srcAlphaFile) {
vm_file_fprintf(vm_stderr, VM_STRING("Error opening file %s"), AlphaFileName);
return 8;
}
if (mColorFormat == YUV420) mColorFormat = YUV420A; else
if (mColorFormat == YUV422) mColorFormat = YUV422A; els e
if (mColorFormat == YUV444) mColorFormat = YUV444A;
}

if (pCodec->Init(pEncoderParams) != UMC_OK) {
vm_file_fprintf(vm_stderr, VM_STRING("Error: Video encoder initialization failed "));
return 6;
}

if (pCodec->GetInfo(pEncoderParams) != UMC_OK) {
vm_file_fprintf(vm_stderr, VM_STRING("Error: Video encoder GetInfo failed "));
return 7;
}

vm_string_printf(VM_STRING(" "));
vm_string_printf(VM_STRING("Starting %s encoding %s to file %s "), codecName, SrcFileName, DstFileName);
vm_string_printf(VM_STRING("Source video width = %d, height = %d, frameRate = %.2lf "),
mWidth, mHeight, FrameRate);
vm_string_printf(VM_STRING("Max frames to encode = %d "), maxNumFrames);
if (pEncoderParams->info.bitrate > 0)
vm_string_printf(VM_STRING("Encoding bit rate = %d bits per second "), pEncoderParams->info.bitrate);
else
vm_string_printf(VM_STRING("Encoding without rate control "));

m_csysinfo.GetCpuUsage();
DumpPerf0();

Run();

fullTime = GetTick() - t_start;

m_csysinfo.GetCpuUsage();
DumpPerf1();

vm_string_printf(VM_STRING(" Summary: "));
vm_string_printf(VM_STRING(" Num frames encoded = %d "), mFramesEncoded);
vm_string_printf(VM_STRING(" Encoding Time = %.2f sec, %.2f fps "), encodingTime, mFramesEncoded/encodingTime);
vm_string_printf(VM_STRING(" Overall Time = %.2f sec, %.2f fps "), fullTime, mFramesEncoded/fullTime);
vm_string_printf(VM_STRING(" Average CPU usage = %.2lf%% "), m_csysinfo.GetAvgCpuUsage());
vm_string_printf(VM_STRING(" Encoded Size = %d bytes "), encodedSize);
vm_string_printf(VM_STRING(" Compression Ratio = %.2f "), ((Ipp64f)mFramesEncoded*mFrameSize)/encodedSize);

if (pEncoderParams->info.framerate > 0 && pEncoderParams->info.bitrate > 0) {
size_ratio = encodedSize/(((Ipp64f)mFramesEncoded/pEncoderParams->info.framerate)*(pEncoderParams->info.bitrate/8));
vm_string_printf(VM_STRING(" EncodedSize/ExpectedSize = %.2f "), size_ratio);
}

if (perf_file != NULL) {
vm_string_fprintf(perf_file, VM_STRING("Performance = %.2f fps"), mFramesEncoded/encodingTime);
vm_string_fprintf(perf_file, VM_STRING(", EncodedSize/ExpectedSize = %.2f "), size_ratio);
vm_string_fprintf(perf_file, VM_STRING("num = %d"), mFramesEncoded);
vm_string_fprintf(perf_file, VM_STRING("performance = %.5f"), mFramesEncoded/encodingTime);
vm_string_fprintf(perf_file, VM_STRING("perf_overall = %.5f"), fullTime);
vm_string_fprintf(perf_file, VM_STRING("size_ratio = %.5f"), ((Ipp64f)mFramesEncoded*mFrameSize)/encodedSize);
vm_file_fclose(perf_file);
}

if (dstFile) {
vm_file_fclose(dstFile);
dstFile = NU LL;
}

if (srcFile) {
vm_file_fclose(srcFile);
srcFile = NULL;
}

return 0;
}

void VideoEncodingTest::DumpPerf0()
{
sSystemInfo* m_ssysteminfo = (sSystemInfo *)m_csysinfo.GetSysInfo();
vm_char* stream_name = NULL;
vm_file *perf_file_csv = NULL;
vm_char tmpbuf[128]={0,};

if(PerfFileName)
perf_file_csv = vm_file_open(PerfFileName, VM_STRING("a"));

if(perf_file_csv){
IppLibraryVersion *ipplibversion;
stream_name = vm_string_strrchr(SrcFileName, (Ipp32s)(''));
if(!stream_name)stream_name = SrcFileName;
else stream_name++;
vm_file_fseek( perf_file_csv, 0, VM_FILE_SEEK_END);
if(!vm_file_ftell( perf_file_csv))
vm_string_fprintf(perf_file_csv, VM_STRING("Date,Time,Comp.name,OS,CPU,Num.CPU,CPU freq.,MEM,VGA,App,App description,Ver.,Ipp Type,Ipp ver.,Ipp Desc.,Streams(S) name,Output S. name,Status,Output S. size,S.Type,S.Res.,S.FR,S.BR,Video(V)Type,V.Format,V.Num,R.S.BR,V.E/D(FpS),V.E/D(MHz),Compr.(%%),CPU Usage (%%),Max Cpu Usage (%%),Work.Time,1e,2e,3e,4e,5e, "));
vm_sys_info_get_date(tmpbuf,MMDDYY);
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"), tmpbuf);
vm_sys_info_get_time(tmpbuf,HHMMSS);
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"), tmpbuf);
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"), m_ssysteminfo->computer_name);
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"), m_ssysteminfo->os_name);
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"), m_ssysteminfo->proc_name);
vm_string_fprintf(perf_file_csv,VM_STRING("%d,"), m_ssysteminfo->num_proc);
vm_string_fprintf(perf_file_csv,VM_STRING("%.2f,"), (Ipp64f)m_ssysteminfo->cpu_freq);
vm_string_fprintf(perf_file_csv,VM_STRING("%d,"), m_ssysteminfo->phys_mem);
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"), m_ssysteminfo->video_card);
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"),m_ssysteminfo->program_name);
vm_string_fprintf(perf_file_csv,VM_STRING("UMC Video Encoder Con.,"));//,m_ssysteminfo->description);
vm_string_fprintf(perf_file_csv,VM_STRING(",")); // ver. of aplications
ipplibversion = (IppLibraryVersion*)ippvcGetLibVersion();
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"), ipplibversion->targetCpu);
vm_string_fprintf(perf_file_csv,VM_STRING("%d.%d.%d.%d,"), ipplibversion->major,ipplibversion->minor,ipplibversion->majorBuild,ipplibversion->build);
vm_string_fprintf(perf_file_csv,VM_STRING("%s (%s),"), ipplibversion->Version,ipplibversion->BuildDate);
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"), stream_name);
stream_name = vm _string_strrchr(DstFileName, (Ipp32s)(''));
if(!stream_name)stream_name = DstFileName;
else stream_name++;
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"), stream_name);
vm_string_fprintf(perf_file_csv,VM_STRING("FAIL, "));
vm_file_fclose(perf_file_csv);
}
}

void VideoEncodingTest::DumpPerf1()
{
sSystemInfo* m_ssysteminfo = (sSystemInfo *)m_csysinfo.GetSysInfo();
vm_file *perf_file_csv = NULL;

if(PerfFileName)
perf_file_csv = vm_file_open(PerfFileName, VM_STRING("r+"));

if(perf_file_csv) {
vm_file_fseek( perf_file_csv, -6, VM_FILE_SEEK_END);
vm_string_fprintf(perf_file_csv,VM_STRING("PASSED,"));
vm_string_fprintf(perf_file_csv,VM_STRING("%d,"), encodedSize);
vm_string_fprintf(perf_file_csv,VM_STRING("PV,"));
vm_string_fprintf(perf_file_csv,VM_STRING("%dx%d,"),pEncoderParams->info.clip_info.width,pEncoderParams->info.clip_info.height);
vm_string_fprintf(perf_file_csv,VM_STRING("%.2lf,"),pEncoderParams->info.framerate);
vm_string_fprintf(perf_file_csv,VM_STRING("%d,"),pEncoderParams->info.bitrate/1000);
vm_string_fprintf(perf_file_csv,VM_STRING("%s,"), codecName);
vm_string_fprintf(perf_file_csv,VM_STRING(","));
vm_string_fprintf(perf_file_csv,VM_STRING("%d,"), mFramesEncoded);
vm_string_fprintf(perf_file_csv,VM_STRING("%d,"), (Ipp32s)(encodedSize/mFramesEncoded*pEncoderParams->info.framerate*8/1000));
vm_string_fprintf(perf_file_csv,VM_STRING("%.2lf,"), mFramesEncoded/encodingTime);
vm_string_fprintf(perf_file_csv,VM_STRING("%.2lf,"), (Ipp64f)(pEncoderParams->info.framerate/(mFramesEncoded/encodingTime)*(m_ssysteminfo->cpu_freq)));
vm_string_fprintf(perf_file_csv,VM_STRING("%.2lf,"), ((Ipp64f)encodedSize*100.0/(mFramesEncoded*mFrameSize)));
vm_string_fprintf(perf_file_csv,VM_STRING("%.2lf,"), m_csysinfo.GetAvgCpuUsage());
vm_string_fprintf(perf_file_csv,VM_STRING("%.2lf,"), m_csysinfo.GetMaxCpuUsage());
vm_string_fprintf(perf_file_csv,VM_STRING("%02d:%02d:%02d.%02d,"),(Ipp32s)fullTime/3600,(Ipp32s)(fullTime)%3600/60,(Ipp32s)fullTime%3600%60,(Ipp32s)((fullTime-(Ipp32s)fullTime)*100));
vm_string_fprintf(perf_file_csv,VM_STRING(" " ));
#if 0
if (FrameSize) {
vm_string_fprintf(perf_file_csv,VM_STRING("Frame Info Num, Size "));
for (i = 0; i < mFramesEncoded; i ++)
vm_string_fprintf(perf_file_csv,VM_STRING("%d, %d "), i, FrameSize);
}
#endif
vm_file_fclose(perf_file_csv);
}
}


#ifndef NO_MAIN

int main(Ipp32s argc, vm_char *argv[])
{
//Command line: video_enc_con m4 -i output_mpeg4_420.yuv -w 720 -h 288 -f 25 -b 800000 -n 577 -o out.m4v
char lpCmdLine[MAX_PATH];
char sYUV_File[MAX_PATH];
strcpy(sYUV_File,"channel0.yuv");
in t iWidth = 720;
int iHeight = 288;
int iFrameRate = 5; // 25
int iTotalFrame = 5; // 577
sprintf(lpCmdLine,"video_enc_con m4 -i %s -w %i -h %i -f %i -b 100000 -n %i -o out.m4v",sYUV_File,iWidth,iHeight,iFrameRate,iTotalFrame);

int _argc = 0;
char* _argv[MAX_PATH];
char seps[]=" ";
char *token;
token = strtok( lpCmdLine, seps );
while( token != NULL ) {
_argv[_argc] = new char [MAX_PATH];
strcpy(_argv[_argc],token);
_argc++;
token = strtok( NULL, seps );
}

VideoEncodingTest Encode;
Encode.Main(_argc, _argv);
AVI_Muxer(iWidth,iHeight,iFrameRate,"out.m4v","channel_0_5_1.avi");
for (int i=0; i<_argc;i++)
delete []_argv;
}

#endif

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//End of the Mpeg4 Encoder source code:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 Kudos
1 Reply
antony0604
Beginner
244 Views
Thank you for your answer. It work fine now when running on the Dos command line. But if the code is running on the ActiveX (IE), still some video cann't being decoded. I will trying to find why cause this problem.
0 Kudos
Reply