本文主要是介绍【HEVC学习与研究】31、HM编码器的基本结构,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
通过解码器代码的研究,已经对HEVC的编解码技术有了一个初步的认识。现在我们就对照着编码器的代码进一步理解HEVC视频编码算法的各个技术细节。
编码器在整个HM解决方案中的工程名为TAppEncoder,入口点函数位于encmain.cpp文件中:
int main(int argc, char* argv[])
{TAppEncTop cTAppEncTop;// print informationfprintf( stdout, "\n" );fprintf( stdout, "HM software: Encoder Version [%s]", NV_VERSION );fprintf( stdout, NVM_ONOS );fprintf( stdout, NVM_COMPILEDBY );fprintf( stdout, NVM_BITS );fprintf( stdout, "\n" );// create application encoder classcTAppEncTop.create();// parse configurationtry{if(!cTAppEncTop.parseCfg( argc, argv )){cTAppEncTop.destroy();return 1;}}catch (po::ParseFailure& e){cerr << "Error parsing option \""<< e.arg <<"\" with argument \""<< e.val <<"\"." << endl;return 1;}// starting timedouble dResult;long lBefore = clock();// call encoding functioncTAppEncTop.encode();// ending timedResult = (double)(clock()-lBefore) / CLOCKS_PER_SEC;printf("\n Total Time: %12.3f sec.\n", dResult);// destroy application encoder classcTAppEncTop.destroy();return 0;
}
可以很清楚地看到,整个main函数非常简洁清晰,主要可以分为几大部分,分别是输入软件信息、创建编码器类的实例、解析配置文件、获取开始时间、编码数据、计算耗费时间和销毁编码器类的实例几大部分。我们主要关心的编码过程仅通过调用编码器实例的一个方法实现:
// call encoding function
cTAppEncTop.encode();
该函数的实现如下:
Void TAppEncTop::encode()
{fstream bitstreamFile(m_pchBitstreamFile, fstream::binary | fstream::out);if (!bitstreamFile){fprintf(stderr, "\nfailed to open bitstream file `%s' for writing\n", m_pchBitstreamFile);exit(EXIT_FAILURE);}TComPicYuv* pcPicYuvOrg = new TComPicYuv;TComPicYuv* pcPicYuvRec = NULL;// initialize internal class & member variablesxInitLibCfg();xCreateLib();xInitLib();// main encoder loopInt iNumEncoded = 0;Bool bEos = false;list<AccessUnit> outputAccessUnits; ///< list of access units to write out. is populated by the encoding process// allocate original YUV bufferpcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );while ( !bEos ){// get buffersxGetBuffer(pcPicYuvRec);// read input YUV filem_cTVideoIOYuvInputFile.read( pcPicYuvOrg, m_aiPad );// increase number of received framesm_iFrameRcvd++;bEos = (m_iFrameRcvd == m_framesToBeEncoded);Bool flush = 0;// if end of file (which is only detected on a read failure) flush the encoder of any queued picturesif (m_cTVideoIOYuvInputFile.isEof()){flush = true;bEos = true;m_iFrameRcvd--;m_cTEncTop.setFramesToBeEncoded(m_iFrameRcvd);}// call encoding function for one framem_cTEncTop.encode( bEos, flush ? 0 : pcPicYuvOrg, m_cListPicYuvRec, outputAccessUnits, iNumEncoded );// write bistream to file if necessaryif ( iNumEncoded > 0 ){xWriteOutput(bitstreamFile, iNumEncoded, outputAccessUnits);outputAccessUnits.clear();}}m_cTEncTop.printSummary();// delete original YUV bufferpcPicYuvOrg->destroy();delete pcPicYuvOrg;pcPicYuvOrg = NULL;// delete used buffers in encoder classm_cTEncTop.deletePicBuffer();// delete buffers & classesxDeleteBuffer();xDestroyLib();printRateSummary();return;
}
该函数中首先调用pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth )分配YUV数据缓存,然后再while循环中逐帧读取YUV数据、设置当前以编码的帧数、编码当前帧、写出码流,随后做其他清理工作。核心功能实现在m_cTEncTop.encode( bEos, flush ? 0 : pcPicYuvOrg, m_cListPicYuvRec, outputAccessUnits, iNumEncoded )函数中。在该函数中调用m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut)进行编码一个GOP的操作。这个函数奇长无比,用了接近1500行代码,看来实现了很多很多很多的功能。这个碉堡了的函数究竟做了些啥事儿呢?这个函数中大部分内容就是在为了编码当前slice做准备,以及编码完成之后一些辅助操作。实际编码过程的操作由以下函数m_pcSliceEncoder->compressSlice( pcPic )实现。
这又是一个碉堡了的函数,占了将近400行……代码就不贴了,会死人的……简单看下好了。
首先还是各种编码的配置,包括配置熵编码器、初始化CU编码器等。在完成了一长串的设置之后,在compressCU函数中实现对一个CU的编码:
m_pcCuEncoder->compressCU( pcCU );
具体的细节,且待下文。
这篇关于【HEVC学习与研究】31、HM编码器的基本结构的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!