nvEncMapInputResource with NV_ENC_ERR_INVALID_PARAM

  •   errorLog	0x000001f1f51daed0 "NvEncoder::MapResources : m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResource) returned error 8 at D:\\windows\\Win32CaptureSample\\Samples\\NvCodec\\NvEncoder\\NvEncoder.cpp:370\n"	std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>
    

I’m capture screen with Windows.Graphics.Capture,but failed in m_nvenc.nvEncMapInputResource. the origin sample capture with dxgi is ok.what’s the problem???

void NvEncoder::MapResources(uint32_t bfrIdx)
{
    NV_ENC_MAP_INPUT_RESOURCE mapInputResource = { NV_ENC_MAP_INPUT_RESOURCE_VER };

    mapInputResource.registeredResource = m_vRegisteredResources[bfrIdx];
    NVENC_API_CALL(m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResource));
    m_vMappedInputBuffers[bfrIdx] = mapInputResource.mappedResource;

    if (m_bMotionEstimationOnly)
    {
        mapInputResource.registeredResource = m_vRegisteredResourcesForReference[bfrIdx];
        NVENC_API_CALL(m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResource));
        m_vMappedRefBuffers[bfrIdx] = mapInputResource.mappedResource;
    }
}

void SimpleCapture::OnFrameArrived(winrt::Direct3D11CaptureFramePool const& sender, winrt::IInspectable const&)
{
	HRESULT hr = S_OK;
	auto frame = sender.TryGetNextFrame();
	auto surfaceTexture = GetDXGIInterfaceFromObject<ID3D11Texture2D>(frame.Surface());
	...
	Preproc();//convert surfaceTexture, ConvertRGBToNV12
	Encode();//error in MapResources
}

I’m having the same issue. I have even verified that I am un-mapping the resource correctly (though from another thread) I also made my queue 16 deep. I am wondering how big is your queue? (I.e. size of your m_vRegisteredResourcesForReference?

EDIT: Same issue even if mapping and un-mapping from the same thread.

At least in my case it turns out that I was just encoding the frames in too quick of a succession. I’m not exactly sure how to make sure I don’t bulldoze the buffer, but when I reduced the incoming frame rate, it stopped crashing.