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);
}