办理 FFmpeg 使用 C/C++ 接口时,解码没有 shell 快的问题(使用多线程)
一、问题硬件设备为香橙派 5Plus,近来必要使用硬件视频解码来加快 YOLO 的检测,shell 窗口的FFmpeg已经调通,详见文章:
编译支持 RKmpp 和 RGA 的 ffmpeg 源码_rk3588 ffmpeg mpp-CSDN博客https://csdnimg.cn/release/blog_editor_html/release2.3.8/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=P1C7https://blog.csdn.net/plmm__/article/details/146188927?spm=1001.2014.3001.5501 在现实测试时,发现c++接口的解码占用情况较低,只有三个工作线程:
https://i-blog.csdnimg.cn/direct/e148473f87d24939a5c514099b1b0efb.png
而使用 shell 窗口进行解码时,可以达到13个线程:
https://i-blog.csdnimg.cn/direct/a82661ee885d491f88dae86c84e62a61.png
二、使用多线程
询问 AI 后,原来是解码器的上下文 AVCodecContext 在初始化后默认是使用单线程,可以通过打印变量得到:
cout << "thread_count: " << codecContext->thread_count << endl; 我的输出是 1。这里可以手动指定线程数,也可以改为 0 ,FFmpeg 会根据 CPU 核心数和编解码器特性自动选择线程数。
以下是我的代码片段:
/* 初始化编解码器上下文 */
codecContext = avcodec_alloc_context3(codec);
if (!codecContext)
throw std::runtime_error("Couldn't allocate decoder context");
/* 获取视频流,它包含了视频流的元数据和参数 */
video_stream = formatContext->streams;
/* 复制视频参数到解码器上下文 */
if (avcodec_parameters_to_context(codecContext, video_stream->codecpar) < 0)
throw std::runtime_error("Couldn't copy decoder context");
/* 自动选择线程数 */
codecContext->thread_count = 0; 三、检查
使用自动选择线程数后,工作线程会在 3 到 10 和线程之间浮动:
https://i-blog.csdnimg.cn/direct/42a738a91c7b40179badf0efbb6121ee.png
https://i-blog.csdnimg.cn/direct/f24d7824f8964ec3b8ad409e6a180d17.png
这个只是综合测试解码的情况,如果只解码并且不必要输出,应该就可以达到 shell 的 13 个线程。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]