| 1 | #ifndef WEBRTC_NVIDIA_I420_BUFFER_CUDA_H_ |
| 2 | #define WEBRTC_NVIDIA_I420_BUFFER_CUDA_H_ |
| 3 | |
| 4 | #include <cuda.h> |
| 5 | |
| 6 | #include <cstdint> |
| 7 | |
| 8 | #include "NvEncoder/NvEncoderCuda.h" |
| 9 | #include "api/video/i420_buffer.h" |
| 10 | |
| 11 | namespace webrtc { |
| 12 | |
| 13 | inline void CopyHostPlaneToDevice(CUdeviceptr dst, |
| 14 | uint32_t dst_pitch, |
| 15 | const uint8_t* src, |
| 16 | uint32_t src_pitch, |
| 17 | uint32_t width_bytes, |
| 18 | uint32_t height) { |
| 19 | CUDA_MEMCPY2D copy = {0}; |
| 20 | copy.srcMemoryType = CU_MEMORYTYPE_HOST; |
| 21 | copy.srcHost = src; |
| 22 | copy.srcPitch = src_pitch; |
| 23 | copy.dstMemoryType = CU_MEMORYTYPE_DEVICE; |
| 24 | copy.dstDevice = dst; |
| 25 | copy.dstPitch = dst_pitch; |
| 26 | copy.WidthInBytes = width_bytes; |
| 27 | copy.Height = height; |
| 28 | CUDA_DRVAPI_CALL(cuMemcpy2D(©)); |
| 29 | } |
| 30 | |
| 31 | inline void CopyI420BufferToDeviceFrame(CUcontext context, |
| 32 | const I420BufferInterface& buffer, |
| 33 | const NvEncInputFrame& input_frame) { |
| 34 | if (input_frame.bufferFormat != NV_ENC_BUFFER_FORMAT_IYUV || |
| 35 | input_frame.numChromaPlanes != 2) { |
| 36 | NVENC_THROW_ERROR("NVENC I420 upload requires an IYUV input frame", |
| 37 | NV_ENC_ERR_INVALID_PARAM); |
| 38 | } |
| 39 | |
| 40 | CUDA_DRVAPI_CALL(cuCtxPushCurrent(context)); |
| 41 | |
| 42 | const CUdeviceptr dst_y = reinterpret_cast<CUdeviceptr>(input_frame.inputPtr); |
| 43 | const CUdeviceptr dst_u = dst_y + input_frame.chromaOffsets[0]; |
| 44 | const CUdeviceptr dst_v = dst_y + input_frame.chromaOffsets[1]; |
| 45 | |
| 46 | CopyHostPlaneToDevice(dst_y, input_frame.pitch, buffer.DataY(), |
| 47 | buffer.StrideY(), buffer.width(), buffer.height()); |
| 48 | CopyHostPlaneToDevice(dst_u, input_frame.chromaPitch, buffer.DataU(), |
| 49 | buffer.StrideU(), buffer.ChromaWidth(), |
| 50 | buffer.ChromaHeight()); |
| 51 | CopyHostPlaneToDevice(dst_v, input_frame.chromaPitch, buffer.DataV(), |
| 52 | buffer.StrideV(), buffer.ChromaWidth(), |
| 53 | buffer.ChromaHeight()); |
| 54 | |
| 55 | CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); |
| 56 | } |
| 57 | |
| 58 | } // namespace webrtc |
| 59 | |
| 60 | #endif // WEBRTC_NVIDIA_I420_BUFFER_CUDA_H_ |