Hi,
in the construct function .it just execute once to create a nvbuffer for image.the fd is the member value.
jpegenc = NvJPEGEncoder::createJPEGEncoder("jpenenc");
jpegenc->setCropRect(0, 0, FaceDetectionParams::cameraWidth, FaceDetectionParams::cameraHeight);
if (-1 == NvBufferCreate(&fd, FaceDetectionParams::cameraWidth, FaceDetectionParams::cameraHeight,
NvBufferLayout_BlockLinear, NvBufferColorFormat_YUV420)) {
printf("Create nvbuffer failed.\n");
}
display = EGLDisplayAccessor::getInstance();
eglImage = NvEGLImageFromFd (display, fd);
if(eglImage == NULL) {
printf("Create eglImage failed.\n");
}
if (cudaMalloc(&YUVI420, FaceDetectionParams::cameraHeight * FaceDetectionParams::cameraWidth * 3 / 2)
!= cudaSuccess) {
printf("Malloc YUVI420's cuda buffer failed.\n");
throw;
}
outBufSize = FaceDetectionParams::cameraHeight * FaceDetectionParams::cameraWidth * 3 / 2;
outBuf = new unsigned char[outBufSize];
And every frame past to this function:
//if the fd value not change, the recreate eglImage will the same value.
NvDestroyEGLImage(display, eglImage);
eglImage = NULL;
eglImage = NvEGLImageFromFd (display, fd);
if(eglImage == NULL) {
printf("Create eglImage failed.\n");
}
time.reset();
CUgraphicsResource resource;
CUresult status;
//cudaFree(0);
status = cuGraphicsEGLRegisterImage(&resource, eglImage, CU_GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD);
if (status != CUDA_SUCCESS) {
printf("cuGraphicsEGLRegisterImage failed: %d.\n", status);
}
memset(&frame, 0, sizeof(frame));
status = cuGraphicsResourceGetMappedEglFrame(&frame, resource, 0, 0);
if (status != CUDA_SUCCESS) {
printf("cuGraphicsResourceGetMappedEglFrame failed: %d.\n", status);
}
// convert the RGBA to YUV420 use nvx
convertRGBA2YUVI420(fcp.imgData, YUVI420, FaceDetectionParams::cameraWidth, FaceDetectionParams::cameraHeight);
// copy the YUV420 to frame from cuGraphicsResourceGetMappedEglFrame.
cudaMemcpyToArray((cudaArray_t)frame.frame.pArray[0], 0, 0, YUVI420,
FaceDetectionParams::cameraWidth * FaceDetectionParams::cameraHeight, cudaMemcpyDeviceToDevice);
cudaMemcpyToArray((cudaArray_t)frame.frame.pArray[1], 0, 0,
YUVI420 + FaceDetectionParams::cameraWidth * FaceDetectionParams::cameraHeight,
FaceDetectionParams::cameraWidth * FaceDetectionParams::cameraHeight / 4, cudaMemcpyDeviceToDevice);
cudaMemcpyToArray((cudaArray_t)frame.frame.pArray[2], 0, 0,
YUVI420 + FaceDetectionParams::cameraWidth * FaceDetectionParams::cameraHeight * 5 / 4,
FaceDetectionParams::cameraWidth * FaceDetectionParams::cameraHeight / 4, cudaMemcpyDeviceToDevice);
cudaDeviceSynchronize();
//memset(outBuf, 0, sizeof(outBufSize));
int ret = jpegenc->encodeFromFd( fd, JCS_YCbCr, &outBuf, outBufSize);
if(ret == -1) {
printf("jpeg encode failed.\n");
}
std::string outFilePath = "test/" + std::to_string(fcp.frameID) + "qq.jpg";
std::ofstream * outFile = new ofstream(outFilePath);
outFile->write((char *) outBuf, outBufSize);
delete outFile;
printf("test time info: %d ms.\n", time.elapsedMilliSeconds());
this is the code