NvBufferMemMap fails with successful return value.

I’m using the following code to test converting color spaces:

#include "NvUtils.h"
#include "nvbuf_utils.h"

#include <iostream>
#include <chrono>
#include <string.h>

#define TEST_ERROR(COND, FMT, ...)                                      \
  do {                                                                  \
    if (COND) {                                                         \
      printf("ERROR: %s(): (line:%d) " FMT "\n",                        \
             __FUNCTION__, __LINE__, ##__VA_ARGS__);                    \
      exit(1);                                                          \
    }                                                                   \
  } while(0)

const int W = 2;
const int H = 2;

int
main(int argc, char *argv[])
{
  int src_fd;
  int dst_fd;
  NvBufferCreateParams input_params = {0};
  int ret;
  NvBufferTransformParams transform_params;
  NvBufferRect src_rect, dest_rect;
  char *src_data;
  char *dst_data;
  NvBufferParams params;

  input_params.payloadType = NvBufferPayload_SurfArray;
  input_params.width = W;
  input_params.height = H;
  input_params.layout = NvBufferLayout_Pitch;
  input_params.colorFormat = NvBufferColorFormat_ABGR32;
  input_params.nvbuf_tag = NvBufferTag_VIDEO_CONVERT;

  ret = NvBufferCreateEx (&src_fd, &input_params);
  TEST_ERROR(ret == -1, "create dmabuf failed.");

  input_params.payloadType = NvBufferPayload_SurfArray;
  input_params.width = W;
  input_params.height = H;
  input_params.layout = NvBufferLayout_Pitch;
  input_params.colorFormat = NvBufferColorFormat_YUV420;
  input_params.nvbuf_tag = NvBufferTag_VIDEO_CONVERT;

  ret = NvBufferCreateEx (&dst_fd, &input_params);
  TEST_ERROR(ret == -1, "create dmabuf failed.");

  // set transform parameters
  src_rect.top = 0;
  src_rect.left = 0;
  src_rect.width = W;
  src_rect.height = H;
  dest_rect.top = 0;
  dest_rect.left = 0;
  dest_rect.width = W;
  dest_rect.height = H;

  transform_params.transform_flag = NVBUFFER_TRANSFORM_FILTER;
  transform_params.transform_flip = NvBufferTransform_None;
  transform_params.transform_filter = NvBufferTransform_Filter_Smart;
  transform_params.src_rect = src_rect;
  transform_params.dst_rect = dest_rect;

  ret = NvBufferMemMap(src_fd, 0, NvBufferMem_Read_Write, (void **) &src_data);
  TEST_ERROR(ret == -1, "Could not map memory.");
  src_data[0] = 255;//
  src_data[1] = 255;
  src_data[2] = 0;
  src_data[3] = 0;
  src_data[4] = 255;//
  src_data[5] = 255;
  src_data[6] = 0;
  src_data[7] = 0;
  src_data[8] = 255;//
  src_data[9] = 255;
  src_data[10] = 0;
  src_data[11] = 0;
  src_data[12] = 255;//
  src_data[13] = 255;
  src_data[14] = 0;
  src_data[15] = 0;
  ret = NvBufferMemUnMap(src_fd, 0, (void **) src_data);
  TEST_ERROR(ret == -1, "Could not unmap memory.");

  ret = NvBufferTransform(src_fd, dst_fd, &transform_params);
  TEST_ERROR(ret == -1, "Transform failed.");

  ret = NvBufferGetParams(dst_fd, &params);
  TEST_ERROR(ret == -1, "Could not get params.");

  for (int i = 0; i < params.num_planes; i++) {
    printf("plane %d:\n", i);

    NvBufferMemMap(dst_fd, i, NvBufferMem_Read, (void **) dst_data);
    printf("mmap ret=%d\n", ret);
    TEST_ERROR(ret == -1, "Could not map memory.");

    for (int j = 0; j < params.height[i]; j++) {
      printf("%d: ", j);
      for (int k = 0; k < params.width[i]; k++) {
        printf("%d ", (int)(unsigned char) dst_data[j*params.pitch[i] + k]);
      }
      printf("\n");
    }

    NvBufferMemUnMap(dst_fd, i, (void **) dst_data);
    TEST_ERROR(ret == -1, "Could not unmap memory.");
  }

  return 0;
}

It fails with the following output:

plane 0:
nvbuf_utils: NvBufferMemMap function failed... Exiting...
mmap ret=0
Segmentation fault (core dumped)

As you can see the second NvBufferMemMap function prints an error, but then returns 0, which means success. I have two questions: 1) Why return 0, when the function has clearly failed. 2) Why does it fail in the first place?

Okay. I made two mistakes here. The first that on the second call to NvBufferMemMap I wasn’t assigning the return value to “ret”. The second was that the last argument should have been “&dst_data” not “dst_data”. Embarrassing, I know!