基于D3D11VA硬解码的帧队列回退问题解析与解决方案
在实现基于FFmpeg与D3D11VA的视频实时预览系统时,采用异步解码与渲染架构,通过一个可配置长度的渲染队列管理解码后的帧数据(默认大小为6)。该队列负责在解码线程与渲染线程之间传递待显示的视频帧。
核心接口设计如下:
// 入队操作
int CDxvaDrawDataQueue::InputData(AVFrame *data) {
m_mutex.lock();
if (m_dxvaDrawDataQue.size() >= m_nMaxSize) {
if (m_bThrowPacket) {
av_frame_free(&data);
} else {
m_mutex.unlock();
while (!m_bExit) {
if (m_dxvaDrawDataQue.size() < m_nMaxSize) {
m_mutex.lock();
m_dxvaDrawDataQue.push_back(data);
break;
}
Sleep(10);
}
}
} else {
m_dxvaDrawDataQue.push_back(data);
}
m_mutex.unlock();
return 0;
}
// 出队操作
AVFrame* CDxvaDrawDataQueue::OutputData() {
AVFrame *data = nullptr;
if (!m_dxvaDrawDataQue.empty()) {
m_mutex.lock();
data = m_dxvaDrawDataQue.front();
m_dxvaDrawDataQue.pop_front();
m_mutex.unlock();
}
return data;
}
解码流程中,每帧解码结果经由 avcodec_decode_video2 输出后,通过浅拷贝方式转移至新分配的 AVFrame 实例,并入队:
AVFrame *frame = av_frame_alloc();
av_frame_move_ref(frame, outFrame);
m_dxvaDrawQueue->InputData(frame);
测试发现:当渲染队列长度设置较大(如6)时,虽然解码速率高,但播放过程中出现画面倒退现象——已播放的帧突然被更早的帧覆盖。相反,当队列长度缩小至1时,虽无回退,但因解码线程频繁阻塞等待,导致整体延迟显著增加。
根本原因在于:在使用D3D11VA硬件解码时,FFmpeg内部维护一个有限数量的纹理池(Texture Array)作为解码缓冲区。调用 av_frame_move_ref 并未复制实际像素数据,仅转移了对原始纹理资源的引用。若队列过长或渲染速度滞后,这些纹理将长期被占用,导致解码器无法获取新的缓冲区,进而引发阻塞或丢帧,最终表现为画面跳帧或回退。
解决方案是将原本的"引用转移"升级为"硬件级数据克隆",即在解码后主动将纹理内容从解码器池中复制到独立的新纹理中,从而释放原始缓冲区,避免资源争用。
为此引入以下深度拷贝函数:
AVFrame* FFmpegD3D11VA::clone_frame(AVFrame *frame) {
if (!frame || frame->format != AV_PIX_FMT_D3D11 || !m_pD3D11Device || !m_pD3D11DeviceContext) {
return nullptr;
}
ID3D11Texture2D* srcTexture = reinterpret_cast(frame->data[0]);
int arrayIndex = static_cast<int>(reinterpret_cast(frame->data[1]));
if (!srcTexture || arrayIndex < 0) {
return nullptr;
}
D3D11_TEXTURE2D_DESC srcDesc;
srcTexture->GetDesc(&srcDesc);
if (arrayIndex >= static_cast<int>(srcDesc.ArraySize)) {
return nullptr;
}
D3D11_TEXTURE2D_DESC dstDesc = srcDesc;
dstDesc.ArraySize = 1;
dstDesc.MipLevels = 1;
dstDesc.MiscFlags = 0;
ID3D11Texture2D* dstTexture = nullptr;
HRESULT hr = m_pD3D11Device->CreateTexture2D(&dstDesc, nullptr, &dstTexture);
if (FAILED(hr) || !dstTexture) {
return nullptr;
}
UINT srcSubresource = D3D11CalcSubresource(0, arrayIndex, srcDesc.MipLevels);
m_pD3D11DeviceContext->CopySubresourceRegion(dstTexture, 0, 0, 0, 0, srcTexture, srcSubresource, nullptr);
m_pD3D11DeviceContext->Flush();
AVFrame* clonedFrame = av_frame_alloc();
if (!clonedFrame) {
dstTexture->Release();
return nullptr;
}
clonedFrame->format = frame->format;
clonedFrame->width = frame->width;
clonedFrame->height = frame->height;
clonedFrame->pts = frame->pts;
clonedFrame->pkt_dts = frame->pkt_dts;
clonedFrame->sample_aspect_ratio = frame->sample_aspect_ratio;
clonedFrame->data[0] = reinterpret_cast(dstTexture);
clonedFrame->data[1] = reinterpret_cast(static_cast(0));
clonedFrame->buf[0] = av_buffer_create(reinterpret_cast(dstTexture), 0, ReleaseTextureBuffer, dstTexture, 0);
if (!clonedFrame->buf[0]) {
av_frame_free(&clonedFrame);
dstTexture->Release();
return nullptr;
}
return clonedFrame;
}
修改解码逻辑,在入队前执行深拷贝:
AVFrame *frame = nullptr;
frame = m_ffd3d11va->clone_frame(outFrame);
av_frame_unref(outFrame);
m_dxvaDrawQueue->InputData(frame);
此方案有效分离了解码与渲染阶段对显存资源的依赖,确保解码器始终能获得可用缓冲区,从根本上消除因纹理池耗尽引起的卡顿与画面回退问题,同时支持较高的队列长度以降低延迟,实现流畅播放与低延迟的兼顾。