先介绍几个基本信息:
操作系统:Win7 64Bit | 显卡:GTX970
我有一个暂存 ID3D11Texture2D ,我想对其进行编码。
我想直接通过nvEncRegisterResource使用纹理,但似乎我只能通过 D3D9 而没有 D3D11 纹理。否则我会得到一个NV_ENC_ERR_UNIMPLEMENTED。
因此,我创建了一个输入缓冲区并手动填充它。
纹理的格式为DXGI_FORMAT_R8G8B8A8_UNORM。DXGI_FORMAT_NV12格式可能仅从 Windows 8 开始。
输入缓冲区格式为NV_ENC_BUFFER_FORMAT_ARGB。这也应该是每个颜色通道 8 位。由于 alpha 值是互换的,我预计会出现错误的图片,但仍应对其进行编码。
到目前为止我的过程:
- 创建 ID3D11Texture2D 渲染纹理
- 创建 ID3D11Texture2D 暂存纹理
- 使用 nvEncCreateInputBuffer 创建 NVENC 输入缓冲区
- 使用 nvEncCreateBitstreamBuffer 创建 NVENC 输出缓冲区
:: 更新例程 ::
- 渲染成渲染纹理
- CopyResource() 到暂存纹理
- 从暂存纹理填充 NVENC 输入缓冲区
- 编码帧
- 从 NVENC 输出缓冲区获取数据
如您所见,我每次更新只编码一帧。
到目前为止,一切正常,但如果我查看框架,图片大小是错误的。
那应该是 1280 x 720。如果我还尝试传递超过一帧,那么我会得到一个损坏的文件。
我知道在 H.264 中我的第一帧是 I 帧,接下来应该是 P 帧。
现在我的代码:
:: 创建输入缓冲区
NVENCSTATUS RenderManager::CreateInputBuffer()
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NV_ENC_CREATE_INPUT_BUFFER createInputBufferParams = {};
createInputBufferParams.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
createInputBufferParams.width = m_width;
createInputBufferParams.height = m_height;
createInputBufferParams.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
createInputBufferParams.bufferFmt = NV_ENC_BUFFER_FORMAT_ARGB;
nvStatus = m_pEncodeAPI->nvEncCreateInputBuffer(m_pEncoder, &createInputBufferParams);
if (nvStatus != NV_ENC_SUCCESS) return nvStatus;
m_pEncoderInputBuffer = createInputBufferParams.inputBuffer;
return nvStatus;
}
:: 创建输出缓冲区
NVENCSTATUS RenderManager::CreateOutputBuffer()
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NV_ENC_CREATE_BITSTREAM_BUFFER createBitstreamBufferParams = {};
createBitstreamBufferParams.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
createBitstreamBufferParams.size = 2 * 1024 * 1024;
createBitstreamBufferParams.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
nvStatus = m_pEncodeAPI->nvEncCreateBitstreamBuffer(m_pEncoder, &createBitstreamBufferParams);
if (nvStatus != NV_ENC_SUCCESS) return nvStatus;
m_pEncoderOutputBuffer = createBitstreamBufferParams.bitstreamBuffer;
return nvStatus;
}
:: 填充输入缓冲区
NVENCSTATUS RenderManager::WriteInputBuffer(ID3D11Texture2D* pTexture)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
HRESULT result = S_OK;
// get data from staging texture
D3D11_MAPPED_SUBRESOURCE mappedResource;
result = m_pContext->Map(pTexture, 0, D3D11_MAP_READ, 0, &mappedResource);
if (FAILED(result)) return NV_ENC_ERR_GENERIC;
m_pContext->Unmap(pTexture, 0);
// lock input buffer
NV_ENC_LOCK_INPUT_BUFFER lockInputBufferParams = {};
lockInputBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
lockInputBufferParams.inputBuffer = m_pEncoderInputBuffer;
nvStatus = m_pEncodeAPI->nvEncLockInputBuffer(m_pEncoder, &lockInputBufferParams);
if (nvStatus != NV_ENC_SUCCESS) return nvStatus;
unsigned int pitch = lockInputBufferParams.pitch;
//ToDo: Convert R8G8B8A8 to A8R8G8B8
// write into buffer
memcpy(lockInputBufferParams.bufferDataPtr, mappedResource.pData, m_height * mappedResource.RowPitch);
// unlock input buffer
nvStatus = m_pEncodeAPI->nvEncUnlockInputBuffer(m_pEncoder, m_pEncoderInputBuffer);
return nvStatus;
}
:: 编码帧
NVENCSTATUS RenderManager::EncodeFrame()
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
int8_t* qpDeltaMapArray = NULL;
unsigned int qpDeltaMapArraySize = 0;
NV_ENC_PIC_PARAMS encPicParams = {};
encPicParams.version = NV_ENC_PIC_PARAMS_VER;
encPicParams.inputWidth = m_width;
encPicParams.inputHeight = m_height;
encPicParams.inputBuffer = m_pEncoderInputBuffer;
encPicParams.outputBitstream = m_pEncoderOutputBuffer;
encPicParams.bufferFmt = NV_ENC_BUFFER_FORMAT_ARGB;
encPicParams.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
encPicParams.qpDeltaMap = qpDeltaMapArray;
encPicParams.qpDeltaMapSize = qpDeltaMapArraySize;
nvStatus = m_pEncodeAPI->nvEncEncodePicture(m_pEncoder, &encPicParams);
return nvStatus;
}
:: 从输出缓冲区读取
NVENCSTATUS RenderManager::ReadOutputBuffer()
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
// lock output buffer
NV_ENC_LOCK_BITSTREAM lockBitstreamBufferParams = {};
lockBitstreamBufferParams.version = NV_ENC_LOCK_BITSTREAM_VER;
lockBitstreamBufferParams.doNotWait = 0;
lockBitstreamBufferParams.outputBitstream = m_pEncoderOutputBuffer;
nvStatus = m_pEncodeAPI->nvEncLockBitstream(m_pEncoder, &lockBitstreamBufferParams);
if (nvStatus != NV_ENC_SUCCESS) return nvStatus;
void* pData = lockBitstreamBufferParams.bitstreamBufferPtr;
unsigned int size = lockBitstreamBufferParams.bitstreamSizeInBytes;
// read from buffer
PlatformManager::SaveToFile("TEST", static_cast<char*>(pData), size);
// unlock output buffer
nvStatus = m_pEncodeAPI->nvEncUnlockBitstream(m_pEncoder, m_pEncoderOutputBuffer);
return nvStatus;
}
m_width 和 m_height 始终为 1280 和 720。
为什么图像大小错误以及我如何用超过一帧填充输入缓冲区?
非常感谢您的帮助,
亚历山大