0

I'm trying to write out the memory that I get from a video 4 linux 2 example program. However, it's not working. I'm getting a segmentation fault when trying to access the pointer. I hope this isn't a stupid mistake because I've spent a couple of days on it. Here is the code: (It's not formatted because there were too many conflicts with html.) My computer is using the mmap branch of execution. It seg faults in writeFile() at this line: mRGB = mScreen->pixels[pixel];

I'm using the v4l2 example code found here http://linuxtv.org/downloads/v4l-dvb-apis/capture-example.html

Here are the changes I made:

at line 497 I changed

fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB332;
fmt.fmt.pix.field = V4L2_FIELD_NONE;

I also added a line to main(...) at around line 704 somewhere.

close_device();
writeFile();
fprintf(stderr, "\n");

and I've inserted a writeFile() method specified below:

typedef struct Screen {
  unsigned char pixels[640*480];
} Screen;


static void writeFile() {
  const int dimx = 640, dimy = 480;
  int mNumPixels = dimx * dimy;

  Screen *mScreen;

  int i, pixel;
  FILE *file = fopen("output","w");
  if (file == NULL) return;
  /* shift the bits around */
  (void)fprintf(file, "P6\n%d %d\n255\n", dimx, dimy);

  for (i = 1; i < n_buffers; i++) {
    mScreen = buffers[i].start;
    printf("\npointer to mScreen is: %p\n", mScreen);

    for (pixel = 0; pixel < 640*480; pixel++) {      
      static unsigned char color[3];
      unsigned char mRGB = 0;
      printf("%d:%x\n", pixel, mRGB);
      fflush(stdout);
      mRGB = mScreen->pixels[pixel];
      color[0] = (mRGB & 0xE0) >> 5;
      color[1] = (mRGB & 0x1D) >> 2;
      color[2] = mRGB & 0x03;

      fwrite(color, sizeof(unsigned char)*3, 1, file);
    }
  }
  fclose(file);
}
  • Where did you get that a pointer is the size of 2 chars??? Try `malloc(sizeof(char *))`. And you never deallocate this memory. – Paul Ogilvie May 09 '15 at 17:14
  • @PaulOgilvie that pointer is clearly never used, so it's a memory leak, but it's probably also debugging stuff the poster left in. – Jay Kominek May 09 '15 at 17:18
  • The pixel loop should not go to 10 but to`buffers[i].length`. I can't find a definition of `Screen`. Make sure it is defined as a `struct` with one member, `pixels`, which is an array of `unsigned char`. – Paul Ogilvie May 09 '15 at 17:26
  • @JohnByrne You should show us where you changed the example program to call your writeFile() function. And have you run it in a debugger, to figure out which pointer access is causing the error? – Jay Kominek May 09 '15 at 17:27
  • Sorry, that is debugging code, and I never deallocate it. So it would be a memory leak. Also, I've included the struct definition of Screen. – John Byrne May 10 '15 at 02:33
  • I changed the program in a few ways: I added in main(...) writeFile(); after the close_device(); call. I also changed the fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB332 and fmt.fmt.pix.field = V4L@_FIELD_NONE; – John Byrne May 10 '15 at 02:35
  • I think I figured it out. The uninit_device function call deallocates the memory so it's not available any more. I moved the writeFile function call up to after stop_capturing() and it seems to at least recognize the memory address as valid. – John Byrne May 10 '15 at 02:51
  • Now I'm getting all 0's though for my data... I'll keep working on it. – John Byrne May 10 '15 at 02:51

1 Answers1

0

uninit_device() of the sample code modifies the available memory such that the pointer isn't available any more. The writeFile() method must be called before this method/function.