X265的编码的线程流程

转载至:https://blog.csdn.net/dragon_dy/article/details/52469678

非常感谢原作者的详细解析,再次感谢一下哈

一、X265的编码主函数

int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)
过程为:
1. 先将输入图像x265_picture* pic_in中的数据复制到内部帧结构Frame *inFrame,计算好其它帧信息
2. 调用m_lookahead->addPicture(*inFrame, sliceType)将当前帧送入lookahead队列,唤醒一个工作线程来处理,在lookahead满后,会将处理完的miniGOP放入output队列,等待getDecidedPicture取出。如果输入帧为NULL,结果编码结束了,则通过调用m_lookahead->flush()来让lookahead处理完剩下的帧,输出miniGOP到output队列。
3. 选择当前的并行帧编码单元FrameEncoder,并调用curEncoder->getEncodedPicture(m_nalList)取它已经在编码的帧数据。如果有已经在编码的帧,会等待其编码结束得到数据,再输出编码数据;如果没有在编码的帧,返回NULL
4. 调用frameEnc = m_lookahead->getDecidedPicture()取出下一帧待编码帧。
5. 调用curEncoder->startCompressFrame(frameEnc)让并行编码器进行编码,此函数将帧数据送入FrameEncoder 后就返回到调用者流程,FrameEncoder 的后台线程会负责编码。FrameEncoder 在编码过程中会根据是否使用了WPP来决定是否唤醒工作线程来调用processRow处理一行


二、线程数目

1. FrameEncoder 个数

        if (!p->bEnableWavefront)
            p->frameNumThreads = X265_MIN3(cpuCount, (rows + 1) / 2, X265_MAX_FRAME_THREADS);
        else if (cpuCount >= 32)
            p->frameNumThreads = (p->sourceHeight > 2000) ? 8 : 6; // dual-socket 10-core IvyBridge or higher
        else if (cpuCount >= 16)
            p->frameNumThreads = 5; // 8 HT cores, or dual socket
        else if (cpuCount >= 8)
            p->frameNumThreads = 3; // 4 HT cores
        else if (cpuCount >= 4)
            p->frameNumThreads = 2; // Dual or Quad core
        else
            p->frameNumThreads = 1;

2. ThreadPool个数

    for (int i = 0; i < numNumaNodes + 1; i++)
    {
        if (threadsPerPool[i])
            numPools += (threadsPerPool[i] + MAX_POOL_THREADS - 1) / MAX_POOL_THREADS;
    }//即线程池个数为CPU个数

    if (numPools > p->frameNumThreads)
    { 
        numPools = X265_MAX(p->frameNumThreads / 2, 1);
    }//且最大为FrameEncoder 个数的一半


3. 线程池中的JobProvider个数

  int maxProviders = (p->frameNumThreads + numPools - 1) / numPools + 1; /* +1 is Lookahead, always assigned to threadpool 0 */

因为JobProvider只有两种FrameEncoder(派生自WaveFront )和Lookahead。Lookahead只在Encoder中有一个。

(p->frameNumThreads + numPools - 1) / numPools的意思是将FrameEncoder 平均分到numPools中,平分后的个数(可能某些pool会少1个)

因此,JobProvider个数就是平分的FrameEncoder 个数+Lookahead


4. 线程池中的线程数

int numThreads = X265_MIN(MAX_POOL_THREADS, threadsPerPool[node]);

就是CPU核心数



三、总结:
1. 编码器中有FrameEncoder个数的后台线程在等待并行编码,这些后台线程每次编码一帧。在编码一帧过程中,会根据WPP设置决定是否调用工作线程来一起处理一行。在编码每行过程中,再在不同的环节通过联合任务的方式来使用其它线程一起对帧数据分成并行单元编码。
2. lookahead没有后台线程,encode会随时唤醒工作线程来进行lookahead处理

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Open Source (GPL) H.265/HEVC video encoder 下载网址:https://bitbucket.org/multicoreware/x265/src x265 developer wiki To compile x265 you must first install Mercurial (or TortoiseHg on Windows) and CMake. Then follow these easy steps: (for the most definitive instructions, consult our build README) Linux Instructions # ubuntu packages: $ sudo apt-get install mercurial cmake cmake-curses-gui build-essential yasm # Note: if the packaged yasm is older than 1.2, you must download yasm-1.2 and build it $ hg clone https://bitbucket.org/multicoreware/x265 $ cd x265/build/linux $ ./make-Makefiles.bash $ make Windows (Visual Studio) Instructions $ hg clone https://bitbucket.org/multicoreware/x265 Then run make-solutions.bat in the build\ folder that corresponds to your favorite compiler, configure your build options, click 'configure', click 'generate', then close cmake-gui. You will be rewarded with an x265.sln file. Also see cmake documentation. Intel Compiler Instructions On Windows, you should open an Intel Compiler command prompt and within it run one of the make-makefiles.bat scripts in build/icl32 or build/icl64, then run nmake. On Linux, you can tell cmake to build Makefiles for icpc directly. This requires you to have configured Intel's compiler environment (by sourcing the appropriate shell script). For example: $ source /opt/intel/composer_xe_2013/bin/compilervars.sh intel64 $ cd repos/x265/build/linux $ export CXX=icpc $ export CC=icc $ ./make-Makefiles $ make Command line interface The Makefile/solution builds a static encoder.lib library and a standalone x265 executable that aims to be similar to x264 in its command line interface. Running without arguments shows you the command line help. Info Mission Statement Road Map TODO HOWTO add a new encoder performance primitive HOWTO Contribute patches to x265 HOWTO cross compile from Linux to Windows Coding Style Helpful links

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值