hi, Dane
we use jetson_multimedia_api’s sample12 to fulfil zero-copy function.
if the camera’s output is 2592x1944 uyvy422, the nvbuffer’s pitch is 5376, each line’s valid data is 2592x2, we can use below method to save picture. the picture is normal.
but if the camera’s output is 1280x720 uyvy422, the nvbuffer’s pitch is 2560, eash line’s valid data also 1280x2=2560. we use the same method to save picture, but the picture is abnormal, it shows a green screen.
if the pitch is 2560, i think the valid data can not be 2560, so how do we know each line’s valid data?
if (eglFrame.frameType == CU_EGL_FRAME_TYPE_PITCH)
{
int bytesPerPix = 2; // UYVY
size_t srcPitch = eglFrame.pitch; // 5376
size_t dstPitch = eglFrame.width \* bytesPerPix; // 5184
printf("liuting new Handle_EGLImage srcPitch:%ld, width:%d, height:%d\\n",srcPitch, eglFrame.width, eglFrame.height);
// 分配 host buffer (紧凑行)
std::vector<uint8_t> hostBuffer(dstPitch \* eglFrame.height);
// CUDA 2D 拷贝:GPU带pitch -> host紧凑
CUDA_MEMCPY2D copyParams = {0};
copyParams.srcMemoryType = CU_MEMORYTYPE_DEVICE;
copyParams.srcDevice = (CUdeviceptr)eglFrame.frame.pPitch\[0\];
copyParams.srcPitch = eglFrame.width\*bytesPerPix;
// copyParams.srcPitch = srcPitch;
copyParams.srcXInBytes = 0;
copyParams.srcY = 0;
copyParams.dstMemoryType = CU_MEMORYTYPE_HOST;
copyParams.dstHost = hostBuffer.data();
copyParams.dstPitch = dstPitch;
copyParams.dstXInBytes = 0;
copyParams.dstY = 0;
copyParams.WidthInBytes = dstPitch;
copyParams.Height = eglFrame.height;
status = cuMemcpy2D(©Params);
if (status != CUDA_SUCCESS)
{
printf("cuMemcpy2D failed: %d\\n", status);
cuGraphicsUnregisterResource(pResource);
return;
}
// OpenCV 转 BGR
cv::Mat uyvy(eglFrame.height, eglFrame.width, CV_8UC2, hostBuffer.data());
cv::Mat bgr;
cv::cvtColor(uyvy, bgr, cv::COLOR_YUV2BGR_UYVY);
// 保存图片
cv::imwrite("frame_uyvy.png", bgr);


