how to input GPU device data directly to encode data?

I have a question in 01_video_encode of tegra_multimedia_api,the application YUV reads input buffers from a file, performs H.264 or H.265 video encoding.But now i want to input GPU device data directly to encode.
I use cudaMemcpy to copy device data to planes,but it fails.
char *buffer_ptr = (char *)buffer.planes[0].data;
cudaError_t cudaStatus = cudaMemcpy(buffer_ptr, imgbuffer, data_size, cudaMemcpyDeviceToDevice);

I try to modify this:
ctx->output_memory_type = V4L2_MEMORY_MMAP;//V4L2_MEMORY_DMABUF;
It also fails.Now i am confused.
Please tell me how to solve this problem.
Thank you!

The imgbuffer is allocated in GPU using cudaMalloc function as follows:

unsigned char tImg;
*)&tImg, data_size);

I know the NvBuffer is allocated in HW buffer,
what is the difference between the HW buffer and Gpu buffer?
How to copy Gpu device buffer to Nvbuffer?

You may refer to below lines in 03_video_cuda_enc:

ctx.eglimg = NvEGLImageFromFd(ctx.eglDisplay, buffer->planes[0].fd);
NvDestroyEGLImage(ctx.eglDisplay, ctx.eglimg);

It demonstrates how to get CUdeviceptr in HandleEGLImage(). You can perform cudaMemcpy() with the pointer.