Optimization to write raw images

I capture images from MIPI camera.
I have access to the .raw pointer. I want to save images in .raw as fast as possible.

The resolution of one image is 4224*3008.

I cope with two difficulties. Below an example to explain my problem :

#include <stdio.h>

int main ()
{
  FILE * pFile;
  pFile = fopen ("file.raw", "wb");
  unsigned short* buffer = new unsigned short[3008*4224];

  for (int i=0 ; i<600 ; i++)
  {
      for(int j=0 ; j<3008*4224 ; j++ ){buffer[j] = i ;}

      fwrite (buffer , sizeof(unsigned short), 3008*4224, pFile);
  }

  fclose (pFile);
  return 0;
}

This sample I wrote illustrates perfectly my issues.

I expect .raw with a size of 15.2GB, and a time to write in .raw more or less constant.

  1. I can write only 4.3GB, then the code “skip” the function fwrite(…). Anyone has already encounter this problem ?

  2. The function is really, really slow. At the beginning, the average time is around 250ms to write one image, and after some iterations, this time is around 2200ms. Does someone has an idea ?

It is probably not the most efficient solution to write images in .raw, but I didn’t other solutions. I tested to write with openCV in .tif, it is faster than .raw but I am pretty sure that it is because I have a problem in my code.
If someone has tips, it would be great !

I am working on jetson TX2 board, which runs L4T (linux for tegra).

The file system of my sd card is ext3/Ext4:

nvidia@nvidia-desktop:~$ df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/mmcblk0p1 ext4       28G   12G   15G  44% /
none           devtmpfs  3,8G     0  3,8G   0% /dev
tmpfs          tmpfs     3,9G  4,0K  3,9G   1% /dev/shm
tmpfs          tmpfs     3,9G   21M  3,9G   1% /run
tmpfs          tmpfs     5,0M  4,0K  5,0M   1% /run/lock
tmpfs          tmpfs     3,9G     0  3,9G   0% /sys/fs/cgroup
tmpfs          tmpfs     785M  124K  785M   1% /run/user/1000
/dev/sda1      fuseblk    58G   18G   40G  32% /media/nvidia/Ubuntu
/dev/mmcblk2p1 ext4       15G  5,2G  8,7G  38% /media/nvidia/sdCard
tmpfs          tmpfs     785M     0  785M   0% /run/user/0

I use nvcc compiler. I use -03 as well as -use_fast_math.

Thank you !

hello nolann.laine,

please refer to Applications Using V4L2 IOCTL Directly,
you may use V4L2 IOCTL to verify basic functionality during sensor bringup, it could also dump the raw file into memory.
thanks