How to use NVENC and NVDEC for on device transcoding? (no cpu copies)

When I’m using NVDEC to decode a H264 stream (from CPU, upload into CUDA buffer) how do I provide the decoded frame (using cuda device memory) to an instance of the NVENC?

In contrast to the encoder exampes I don’t know up front how many different input buffers I will receive. Therefore I can’t generate and register the input buffers at once.

Is the correct approach to create a function which e.g. accepts a CUdeviceptr which was retrieved by calling cuvidMapVideoFrame() in the pfnDisplayPicture callback of the decoder. Then, in that function I check if the received CUdeviceptr has been registered or not. When the CUdeviceptr hasn’t been registered yet I call nvEncRegisterResource() to register it, then use the mapped pointer to encode. Is this the correct approach to make sure a decode frame stays in device memory after decoding?