Hardware Accelerated JPEG encode/decode on Jetson Xavier JP 5.1.3

Hi,
Thanks for the test sample. One query to this step:

Custom build of OpenCV (use attached deb packages)

Should we run the commands for installation:

$ sudo apt -y purge *libopencv*
$ sudo apt install ./libalpropencvgpu4_4.5.5-200420240324133246_arm64.deb
$ sudo apt install ./libalpropencvgpu-dev_4.5.5-200420240324133246_arm64.deb
dpkg -i ./libalpropencvgpu4_4.5.5-200420240324133246_arm64.deb
dpkg -i ./libalpropencvgpu-dev_4.5.5-200420240324133246_arm64.deb

If there is a problem you need to find other instances of OpenCV using:

dpkg --list | grep opencv

And remove them using:

dpkg -r <name of installed package>

And then try againā€¦

Hi,
Thanks for the information. Please also share how to install the so files:

test_jpeg: /usr/lib/aarch64-linux-gnu/libturbojpeg.so.0
test_jpeg: /usr/lib/aarch64-linux-gnu/libcurl.so
test_jpeg: /usr/lib/aarch64-linux-gnu/libcryptopp.so
test_jpeg: /usr/lib/aarch64-linux-gnu/liblog4cplus.so

Those are actually not required. Iā€™ve attached an updated project with these removed.
jpeg-hwaccel.zip (86.1 KB)

Hi,
I am able to run the default application and get the JPEG file:

$ ./test_jpeg image_00000.jpg /tmp/b.jpg
NvMMLiteBlockCreate : Block : BlockType = 1
Generated output jpeg file: /tmp/b.jpg, size: 25952
Test encode succeeded using quality: 84
Test encode SUCCEEDED

However, the content is not synchronized. And would need to add the two function calls:

  cv::cuda::GpuMat nvGpuMatRGBA(in_height, in_width, CV_8UC4, eglFrame.frame.pPitch[0]);
  gpuMatRGBA.copyTo(nvGpuMatRGBA);
+  cuCtxSynchronize();
+  cuGraphicsUnregisterResource(pResource);

Please give it a try.

Furthermore, the function calls are to map NvBufSurface for accessing through CPU. We are accessing through GPU so the calls can be removed.

NvBufSurfaceMap();
NvBufSurfaceSyncForDevice();
NvBufSurfaceUnMap();

Iā€™ve made the changes and get the following:

nvbufsurface: Failed to create EGLImage.
NvBufSurfaceMapEglImage() FAILED
Test encode FAILED

jpeg-hwaccel.zip (86.1 KB)

Please attach your modified project in a zip file

Just to be clear this is on an Jetson Xavier NX running Jetpack 5.1.3

Hi,
We have tried the defualt test_jpeg on AGX Orin developer kit and Xavier NX developer kit with Jetpack 5.1.3. Both can run successfully and do not hit NvBufSurfaceMapEglImage() FAILED.

The issue may be specific to you renvironemnt. Please try Xavier NX developer kit with clean Jetpack 5.1.3. We donā€™t hit the issue in the setup and would expect you can get identical result.

After testing on a newly Jetpack 5.1.3 flashed Jetson Xavier the same error is occurring.

nvbufsurface: Could not get EGL display connection
nvbufsurface: Can't get EGL display
NvBufSurfaceMapEglImage() FAILED, ret: -1
Test encode FAILED

This is being tested on a headless system.

Hi,
Are you able to try developer kit? Would like to make sure you donā€™t hit the error on developer kit. Looks like you only test it on your custom board.

Will attempt to acquire a developer kit. In the meantime, Iā€™m providing an updated sample test project with an initial implementation of hwaccel JPEG decoding.

Usage:
Test Decode Usage: ./test_jpeg </path/to/input.jpg>

Can you please verify implementation with emphasis on the dump_dmabuf_to_buffer method used to populate a cv::cuda::GpuMat with BGR colorspace.

jpeg-hwaccel.zip (88.6 KB)

Hi,
There is a known issue in hardware JPEG decoding. Please apply this prebuilt lib to 5.1.3:

Jetson/L4T/r35.5.x patches - eLinux.org
[MMAPI/gstramer] NvJpegDecoder/nvjpegdec does not work properly

Remove the line and try again:

  jpegdec->disableMjpegDecode();

It appears the decode functionality is working correctly.

Still in the process of bringing up a developer kit board. In the meantime, I have updated the sample project to first decode a JPEG and then immediately encode the decoded image. Can you please verify that you get a new JPEG transcoded from the original passed to the sample app.

Usage:
./test_jpeg </path/to/input.jpg> </path/to/output.jpg>

jpeg-hwaccel.zip (22.2 KB)

Hi,
The encoded JPEG is not correct. Please check the attachment.
b

Seems like there may be something wrong in reading the buffer in decode(). Do you have any other developer kit? If you have either developer kit which can flash Jetpack 5, you can do further debugging/implementation on your own.

I am suspicious of the dump_dmabuf_to_buffer method.

What is the correct code for getting a file buffer from a dma file descriptor?

Should have a developer kit board sometime today, but it appears the code is failing for you.

Receiving the same error on a fresh Jetson Developer Kit board:

NvBufSurfaceMapEglImage() FAILED, ret: -1

Fresh flash of Jetpack 5.1.3:

# R35 (release), REVISION: 5.0, GCID: 35550185, BOARD: t186ref, EABI: aarch64, DATE: Tue Feb 20 04:46:31 UTC 2024

The following command were used to set up the Jetson Developer Kit board:

# cmake update
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates gnupg software-properties-common wget
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc | gpg --dearmor - | sudo tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null
echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ bionic main' | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null
sudo apt-get update && sudo apt-get install -y cmake
sudo rm /etc/apt/sources.list.d/kitware.list && sudo rm /usr/share/keyrings/kitware-archive-keyring.gpg
sudo apt-get update

# g++ update
sudo apt-get install -y software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update && sudo apt-get install -y g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-9

# dependencies update
sudo apt --fix-broken install
sudo apt install -y cuda-toolkit-11-4
apt install -y -f ./cuda-repo-l4t-11-4-local_11.4.19-1_arm64.deb
sudo apt-get remove --auto-remove libgstreamer-opencv1.0-0 libopencv-*
apt install -y -f ./libalpropencvgpu4_4.5.5-200420240324133246_arm64.deb
apt install -y -f ./libalpropencvgpu-dev_4.5.5-200420240324133246_arm64.deb
apt install -y -f ./nvidia-l4t-jetson-multimedia-api_35.5.0-20240219203809_arm64.deb
apt install -y -f ./deepstream-6.3_6.3.0-1_arm64.deb
apt autoremove -y

# libnvjpeg patch
wget -O r35_5_TEST_libnvjpeg_1.zip https://forums.developer.nvidia.com/uploads/short-url/nz3uWYNzgYxH82gXw2b2IQM4K5d.zip
unzip r35_5_TEST_libnvjpeg_1.zip
sudo mv libnvjpeg.so /usr/lib/aarch64-linux-gnu/tegra/

Hi,
Please try clean system with only SDK Components being installed. It looks like some other installed packages trigger the failure. We donā€™t hit this on developer kit.

This was done on a clean system. Only cmake, g++, CUDA, Deepstream, Jetson Multimedia API and the libnvjpeg.so patch were applied.

I provided the entire list of updates. Which should be omitted for testing?

Hi,
We donā€™t manually upgrade cmake and g++. Please only install SDK Components and try again.

Testing on a fresh flash on a Jetson Developer Kit board. Only with the SDK Components listed above and cmake from apt-get install.

root@rekor-desktop:/test/nvidia/build# ./test_jpeg /test/nvidia/image_00000.jpg /test/nvidia/image_00000_out.jpg
MoTTY X11 proxy: Unsupported authorisation protocol
MoTTY X11 proxy: Unsupported authorisation protocol
MoTTY X11 proxy: Unsupported authorisation protocol
MoTTY X11 proxy: Unsupported authorisation protocol
nvbufsurftransform: Could not get EGL display connection
NvMMLiteOpen : Block : BlockType = 277
NvMMLiteBlockCreate : Block : BlockType = 277
Decoded input jpeg file: /test/nvidia/image_00000.jpg, size: 368640
MoTTY X11 proxy: Unsupported authorisation protocol
MoTTY X11 proxy: Unsupported authorisation protocol
MoTTY X11 proxy: Unsupported authorisation protocol
MoTTY X11 proxy: Unsupported authorisation protocol
nvbufsurface: Could not get EGL display connection
nvbufsurface: Can't get EGL display
NvBufSurfaceMapEglImage() FAILED, ret: -1
Test encode FAILED

In your testing, why is the output JPEG not correct? Is appears to be an issue with a colorspace copy.

And why the warnings when we test about not being able to get an EGL display connection? I remind you that this is on a headless system.