Hello - Having trouble using the jpeg encoder on the xavier agx. This code works fine on the NX. Each frame is taking over 10 seconds to encode on the AGX even though the code is identical.
Total units processed = 9
Average latency(usec) = 10222106
Minimum latency(usec) = 10210707
Maximum latency(usec) = 10239063
int read_video_frame(const char* inpBuf, unsigned inpBufLen, NvBuffer& buffer){
uint32_t i, j;
char *data;
for (i = 0; i < buffer.n_planes; i++)
{
NvBuffer::NvBufferPlane &plane = buffer.planes[i];
std::streamsize bytes_to_read = plane.fmt.bytesperpixel * plane.fmt.width;
data = (char *) plane.data;
plane.bytesused = 0;
for (j = 0; j < plane.fmt.height; j++)
{
unsigned numRead = std::min((unsigned)bytes_to_read, (unsigned)inpBufLen);
memcpy(data, inpBuf, numRead);
if (numRead < bytes_to_read) {
return -1;
}
inpBuf += numRead;
inpBufLen -= numRead;
data += plane.fmt.stride;
}
plane.bytesused = plane.fmt.stride * plane.fmt.height;
}
return 0;
}
std::vector<uchar> Video::compressJpeg(const cv::Mat &image){
cv::Mat yuv;
cv::cvtColor(image, yuv, cv::COLOR_BGR2YUV_I420);
unsigned long out_buf_size = image.rows * image.cols * 3 / 2;
std::vector<uchar> out_buf(out_buf_size);
NvBuffer buffer(V4L2_PIX_FMT_YUV420M, image.cols, image.rows, 0);
buffer.allocateMemory();
auto ret = read_video_frame((const char*)yuv.data, yuv.total()*yuv.elemSize(), buffer);
if(ret < 0) {
LOG(ERROR) << "read_video_frame error";
}
//set in buffer
uchar *obuf = out_buf.data();
LOG(INFO) << "Start encode";
ret = jpegenc_->encodeFromBuffer(buffer, JCS_YCbCr, &obuf , out_buf_size, jpeg_compression_);
if(ret < 0) {
LOG(ERROR) << "encodeFromBuffer error";
}
LOG(INFO) << "End encode";
out_buf.resize(out_buf_size);
jpegenc_->printProfilingStats(std::cout);
return out_buf;
}
There is a question here with a similar issue: NvJPEGDecoder ultra slow (Xavier & Nano)
The response is unhelpful as I don’t know how to use the decodeToFd()
with my data. I tried to set it up like this and I get an error for the memcpy to the image plane after the call to buffer.map()
fails:
int fd;
NvBuffer::NvBufferPlaneFormat buf_format[image.channels()];
for (int i = 0; i < image.channels(); i++) {
buf_format[i].height = image.rows;
buf_format[i].width = image.cols;
buf_format[i].bytesperpixel = 1;
buf_format[i].stride = image.cols;
buf_format[i].sizeimage = image.rows * image.cols;
}
NvBuffer buffer(V4L2_BUF_TYPE_VIDEO_CAPTURE, v4l2_memory::V4L2_MEMORY_MMAP, image.channels(), buf_format, fd);
auto ret = buffer.map();
.
.
.
ret = jpegenc_->encodeFromFd(fd, JCS_YCbCr, &obuf , out_buf_size, jpeg_compression_);
Any help appreciated.
Thanks in advance.