实现FFMPEG 使用CUDA解码播放

播放器使用ffmpeg读取视频文件,packet发给cuda解码,解码后OpenGL直接映射给pbo,pbo绑定texture,然后显示出来

流程读取文件,判断packet,丢到CUDA那

复制代码
if (av_read_frame(m_pFormatContext, &packet) >= 0)
        {
            if (packet.stream_index == m_VideoStreamIndex)
            {

                if (m_UseGPUDecode)
                {
                    //MiniConsole::getInstance().Output("ThreadDecodeVideoGPU begin  \n");
                    ThreadDecodeVideoGPU(packet);

                    //MiniConsole::getInstance().Output("ThreadDecodeVideoGPU  end \n");
                }
                else
                {
                    ThreadDecodeVideoCPU(packet);
                }
                
            }
            else if (packet.stream_index == m_AudioStreamIndex)
            {

                ThreadDecodeAudio(packet);
            }
            else
            {
                av_free_packet(&packet);
            }
        }
复制代码

 

 数据丢给cuvidParseVideoData

复制代码
CUVIDSOURCEDATAPACKET cudaPkt;
    CUresult oResult;
    if (pData == NULL)
    {
        cudaPkt.flags = CUVID_PKT_ENDOFSTREAM; //end of stream
    }
    else
    {
        cudaPkt.flags = CUVID_PKT_TIMESTAMP;
    }
    cudaPkt.payload_size = (unsigned long)nSize;
    cudaPkt.payload = (const unsigned char*)pData;

    cudaPkt.timestamp = packpts;

    cuCtxPushCurrent(m_cudaResPtr->m_CuContext);

    
    oResult = cuvidParseVideoData(m_CuVideoParser, &cudaPkt);
    

    if ((cudaPkt.flags & CUVID_PKT_ENDOFSTREAM) || (oResult != CUDA_SUCCESS))
    {
        checkCudaErrors(cuCtxPopCurrent(NULL));
        return false;
    }
    
    //printf("Succeed to read avpkt %d !\n", iPkt);
    checkCudaErrors(cuCtxPopCurrent(NULL));
复制代码

然后数据到

HandlePictureDecode(CUVIDPICPARAMS *pPicParams)  发送解码

HandlePictureDisplay(CUVIDPARSERDISPINFO *pDispInfo)  GPU解码结束发送到这里,存起来

然后映射pbo ,map texture就到纹理上了,就可以绘制了

 

这个4k x60fps的,如果CPU解码cpu使用率100%都会卡顿

 

这个8k的视频,CPU解码无法播放

 

posted on   c_dragon  阅读(4809)  评论(5编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
历史上的今天:
2014-02-27 深入new/delete:Operator new的全局重载

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示