# JPEG encoding is not continuous

**URL:** <https://forums.developer.nvidia.com/t/jpeg-encoding-is-not-continuous/231197>\
**Category:** Jetson Nano\
**Tags:** board-design\
**Created:** [October 19, 2022, 12:58am UTC](https://forums.developer.nvidia.com/t/jpeg-encoding-is-not-continuous/231197 "2022-10-19T00:58:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TJCUT](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@TJCUT](https://forums.developer.nvidia.com/u/TJCUT)\
**Post date:** [October 19, 2022, 12:58am UTC](https://forums.developer.nvidia.com/t/jpeg-encoding-is-not-continuous/231197/1 "2022-10-19T00:58:27Z")

</div>

Hello

Our development environment is as follows:

1. NANO custmize board
2. Jetpack 4.5.1
3. Carmera Input : Pixel format - YUV422, resolution -1920x1080, frame - 30

I am trying to do JPEG encoding using the 12\_camera\_v4l2\_cuda source of Jetson Multimedia api.  
Jpeg encoding binding source refer to the following.

> [@How to convert YUV to JPG using jpeg encoder(hardware)](https://forums.developer.nvidia.com/t/how-to-convert-yuv-to-jpg-using-jpeg-encoder-hardware/47038/9):
>
> Hi Allen, In the pre-release sample, you can see the camera frame is sent to renderer via Fd: // Render the frame into display if (v4l2\_buf-\>m.planes[0].bytesused) ctx-\>renderer-\>render(buffer-\>planes[0].fd); And you should replace it with the code in 09\_camera\_jpeg\_capture { unsigned long size = JPEG\_BUFFER\_SIZE; unsigned char \*buffer = m\_OutputBuffer; m\_JpegEncoder-\>encodeFromFd(fd, JCS\_YCbCr, &buffer, size); outputFile-\>write((char \*)buffer, siz…

The 12\_camera\_v4l2\_cuda sources for combining jpeg encoding refer to the following.

> [@How to convert YUV to JPG using jpeg encoder(hardware)](https://forums.developer.nvidia.com/t/how-to-convert-yuv-to-jpg-using-jpeg-encoder-hardware/47038/6):
>
> Hi Allen, Please follow the steps to complete the JPEG encoding function. 1 Run the command $ gst-launch-1.0 v4l2src device=/dev/video0 num-buffers=300 ! ‘video/x-raw,width=1920,height=1080,format=(string)UYVY’ ! nvvidconv ! ‘video/x-raw(memory:NVMM),format=(string)NV12’ ! nvoverlaysink With output format being set, it should work fine. 2 Try the pre-release sample attached. You should see preview by running $ ./camera\_v4l2\_cuda -d /dev/video0 -s 1920x1080 -f UYVY -n 30 3 Refer to 09\_came…

However, JPEG encoding is not continuous as shown below.

```auto
-rw-rw-r-- 1 nvidia nvidia 134364 Oct 18 07:34 output001.jpg
-rw-rw-r-- 1 nvidia nvidia 0 Oct 18 07:34 output002.jpg
-rw-rw-r-- 1 nvidia nvidia 0 Oct 18 07:34 output003.jpg
-rw-rw-r-- 1 nvidia nvidia 0 Oct 18 07:34 output004.jpg

```

[camera\_v4l2\_cuda.cpp](https://forums.developer.nvidia.com/uploads/short-url/pvkiq96jGU0izeP9GE3GcHlPHCa.cpp) (24.5 KB)  
[camera\_v4l2\_cuda.h](https://forums.developer.nvidia.com/uploads/short-url/jI1OxlXQDfaKZtTIdViYdWyKzQM.h) (3.5 KB)

Should encoding take care of the synchronization?  
Is there a function about that?

---

<div class="post-metadata">

**Author:** ![DaneLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/danelll/32/14043_2.png) [@DaneLLL](https://forums.developer.nvidia.com/u/DaneLLL)\
**Post date:** [October 19, 2022, 1:58am UTC](https://forums.developer.nvidia.com/t/jpeg-encoding-is-not-continuous/231197/3 "2022-10-19T01:58:58Z")

</div>

Hi,  
The patch is for r28 releases. For r32(Jetpack 4) releases, please call _NvBufferTransform()_ to convert YUV422 to I420 and then encode to JPEG.

---

<div class="post-metadata">

**Author:** ![TJCUT](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@TJCUT](https://forums.developer.nvidia.com/u/TJCUT)\
**Post date:** [October 19, 2022, 8:18am UTC](https://forums.developer.nvidia.com/t/jpeg-encoding-is-not-continuous/231197/4 "2022-10-19T08:18:15Z")

</div>

Thank you for your reply.

The attached source(r32(Jetpack 4)) does Jpeg encoding after NvBufferTransform(YUV422 to YUV420P).  
[camera\_v4l2\_cuda.cpp](https://forums.developer.nvidia.com/uploads/short-url/247WrwaaP2GaU7AQWpwIXidFbc4.cpp) (27.0 KB)  
[camera\_v4l2\_cuda.h](https://forums.developer.nvidia.com/uploads/short-url/eAsFcbfoZT9GIS1zINV7jYMcnds.h) (3.5 KB)

```auto
                /* Convert the camera buffer from YUV422 to YUV420P */
                if (-1 == NvBufferTransform(ctx->g_buff[v4l2_buf.index].dmabuff_fd, ctx->render_dmabuf_fd,
                            &transParams))
                    ERROR_RETURN("Failed to convert the buffer");
...
            /* Preview */
            // ctx->renderer->render(ctx->render_dmabuf_fd);
            write_jpeg(ctx->render_dmabuf_fd, ctx->frame);

```

It’s still not continuous.

```auto
-rw-r--r-- 1 nvidia nvidia 123275 Oct 19 08:09 output001.jpg
-rw-r--r-- 1 nvidia nvidia 123212 Oct 19 08:09 output002.jpg
-rw-r--r-- 1 nvidia nvidia 0 Oct 19 08:09 output003.jpg
-rw-r--r-- 1 nvidia nvidia 0 Oct 19 08:09 output004.jpg
-rw-r--r-- 1 nvidia nvidia 0 Oct 19 08:09 output005.jpg
-rw-r--r-- 1 nvidia nvidia 0 Oct 19 08:09 output006.jpg

```

Is there a way to synchronize it?

---

<div class="post-metadata">

**Author:** ![DaneLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/danelll/32/14043_2.png) [@DaneLLL](https://forums.developer.nvidia.com/u/DaneLLL)\
**Post date:** [October 20, 2022, 1:56am UTC](https://forums.developer.nvidia.com/t/jpeg-encoding-is-not-continuous/231197/5 "2022-10-20T01:56:01Z")

</div>

Hi,  
Please reset _m\_Size_ to width \* height \* 1.5, after saving the JPEG file, and call next _encodeFromFd()_

---

<div class="post-metadata">

**Author:** ![TJCUT](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@TJCUT](https://forums.developer.nvidia.com/u/TJCUT)\
**Post date:** [October 20, 2022, 2:16am UTC](https://forums.developer.nvidia.com/t/jpeg-encoding-is-not-continuous/231197/6 "2022-10-20T02:16:41Z")

</div>

Result is the same

```auto
    // m_Size = (ctx->cam_w * ctx->cam_h) * 3 / 2;
    m_Size = (ctx->cam_w * ctx->cam_h) * 1.5;

```

```auto
-rw-rw-r-- 1 nvidia nvidia 122665 Oct 20 02:09 output001.jpg
-rw-rw-r-- 1 nvidia nvidia 122488 Oct 20 02:09 output002.jpg
-rw-rw-r-- 1 nvidia nvidia 0 Oct 20 02:09 output003.jpg
-rw-rw-r-- 1 nvidia nvidia 0 Oct 20 02:09 output004.jpg

```

Is there no API waiting for JPEG encoding to finish?

---

<div class="post-metadata">

**Author:** ![DaneLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/danelll/32/14043_2.png) [@DaneLLL](https://forums.developer.nvidia.com/u/DaneLLL)\
**Post date:** [October 20, 2022, 2:36am UTC](https://forums.developer.nvidia.com/t/jpeg-encoding-is-not-continuous/231197/7 "2022-10-20T02:36:25Z")

</div>

Hi,  
Please new/delete `NvJpegEncoder` in each JPEG encode. The class is demonstrated in 05\_jpeg\_encode sample and as you have tried, it supports single JPEG encoding.

---

<div class="post-metadata">

**Author:** ![TJCUT](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@TJCUT](https://forums.developer.nvidia.com/u/TJCUT)\
**Post date:** [October 20, 2022, 5:19am UTC](https://forums.developer.nvidia.com/t/jpeg-encoding-is-not-continuous/231197/8 "2022-10-20T05:19:03Z")

</div>

Hello,  
Jpeg encoding was checked normally with the following source.

```auto
    char filename[256];
    sprintf(filename, "output%03u.jpg", (unsigned) frame);

    std::ofstream *outputFile = new std::ofstream(filename);
    if (outputFile)
    {
        NvJPEGEncoder *jpegEncoder;
        unsigned char *outputBuffer;
        unsigned long size = (1920 * 1080) * 1.5;

        outputBuffer = new unsigned char[size];
        unsigned char *buffer = outputBuffer;

        jpegEncoder = NvJPEGEncoder::createJPEGEncoder("jpenenc");
        jpegEncoder->encodeFromFd(dest_dma_fd, JCS_YCbCr, &buffer, size);
        outputFile->write((char *)buffer, size);
        delete jpegEncoder;
        delete outputFile;
        delete outputBuffer;
    }
    else ERROR_RETURN("Failed to initialize camera device");

```

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [November 9, 2022, 5:18am UTC](https://forums.developer.nvidia.com/t/jpeg-encoding-is-not-continuous/231197/10 "2022-11-09T05:18:13Z")

</div>

Closed on November 9, 2022, 5:18am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
