NvBufferTransform not copying data

I am trying to use the NvBufferTransform function to copy data from a source buffer to a destination buffer. Below is a small example of how I’m using it. At the end of the program I print the value of the first byte of memory in each buffer. I expect them to both be 10, but only the source buffer is 10 while the destination buffer is 0, so it seems like the data wasn’t copied. Am I using the NvBufferTransform function incorrectly?

Also, I am using a Xavier NX running R32.6.1.

#include <Argus/Argus.h>
#include <EGLStream/EGLStream.h>
#include <EGLStream/NV/ImageNativeBuffer.h>
#include <nvbuf_utils.h>
#include <NvEglRenderer.h>

#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <iostream>

int main(){
  int ret = 0;
  void* pdata1 = NULL;
  void* pdata2 = NULL;
  int width = 1000;
  int height = 800;
  // create two buffers
  NvBufferCreateParams input_params = {0};
  input_params.payloadType = NvBufferPayload_SurfArray;
  input_params.width = width;
  input_params.height = height;
  input_params.layout = NvBufferLayout_Pitch;
  input_params.colorFormat = NvBufferColorFormat_ABGR32;
  input_params.nvbuf_tag = NvBufferTag_NONE;

  int dmabuf_fd1, dmabuf_fd2;
  ret = NvBufferCreateEx(&dmabuf_fd1, &input_params);
    std::cout << "Failed to create NvBuffer" << std::endl;;
  ret = NvBufferCreateEx(&dmabuf_fd2, &input_params);
    std::cout << "Failed to create NvBuffer" << std::endl;
  // get buffer params
  NvBufferParams params1;
  ret = NvBufferGetParams(dmabuf_fd1, &params1);
    std::cout << "NvBufferGetParams failed " << std::endl;
  NvBufferParams params2;
  ret = NvBufferGetParams(dmabuf_fd2, &params2);
    std::cout << "NvBufferGetParams failed " << ret << std::endl;

  // initialize transform params
  NvBufferRect rect;
  rect.top = 0;
  rect.left = 0;
  rect.width = width;
  rect.height = height;
  NvBufferRect rect2;
  rect2.top = 0;
  rect2.left = 0;
  rect2.width = width;
  rect2.height = height;
  NvBufferTransformParams transform_params{0};
  transform_params.transform_flag = NVBUFFER_TRANSFORM_FLIP;
  transform_params.transform_flip = NvBufferTransform_None;
  transform_params.transform_filter = NvBufferTransform_Filter_Nearest;
  transform_params.src_rect = rect;
  transform_params.dst_rect = rect2;

  // memmap and sync the buffers
  ret = NvBufferMemMap(dmabuf_fd1, 0, NvBufferMem_Read_Write, &pdata1);
    std::cout << "NvBufferMemMap failed" << std::endl;
  ret = NvBufferMemSyncForCpu(dmabuf_fd1, 0, &pdata1);
    std::cout << "NvBufferMemSyncForCpu failed" << std::endl;
  ret = NvBufferMemMap(dmabuf_fd2, 0, NvBufferMem_Read_Write, &pdata2);
    std::cout << "NvBufferMemMap failed" << std::endl;
  ret = NvBufferMemSyncForCpu(dmabuf_fd2, 0, &pdata2);
    std::cout << "NvBufferMemSyncForCpu failed" << std::endl;

  // set the value of the first byte of buffer 1
  std::memset(pdata1, 10, 1);
  // transform
  ret = NvBufferTransform(dmabuf_fd1, dmabuf_fd2, &transform_params);
    std::cout << "NvBufferTransform failed" << std::endl;

  // print the original value and the copied value
  std::cout << "pdata1[0] = " << (int)(((uint8_t*)pdata1)[0]) << std::endl;
  std::cout << "pdata2[0] = " << (int)(((uint8_t*)pdata2)[0]) << std::endl;

Please try to add NvBufferMemSyncForDevice() after memset() and check if it helps.

Thanks for the fast reply. I added the following immediately after the memset() line

ret = NvBufferMemSyncForDevice(dmabuf_fd1, 0, &pdata1);
  std::cout << "NvBufferMemSyncForDevice failed" << std::endl;

The output of the program is the same as before, the first value printed is 10 and the second is 0. It doesn’t seem like it was copied.

I also tried adding the following after NvBufferTransform(), in case I needed to sync with the cpu after the transform, but it didn’t change the result.

  ret = NvBufferMemSyncForCpu(dmabuf_fd2, 0, &pdata2);
    std::cout << "NvBufferMemSyncForCpu failed" << std::endl;


It looks like you only write one byte. Please try
std::memset(pdata1, 10, 1000800);*

[UPDATE]This does not help.

Thanks. I tried it and the result was the same. The first value printed was 10, the second was 0.

Please try this:

memset(pdata1, 10, 1);
memset(&((uint8_t*)pdata1)[3], 0xFF, 1);

Alpha channel will be applied in NvBufferTransform() so please set it to 255.

Thanks a lot. This worked. 10 was printed by both buffers.

1 Like

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