I'm using libjpeg (C/C++ programming on Windows Mobile 6.5), in order to decode images from an IP camera (sent in a MJPEG stream), before pushing them into a DirectShow graph.
Until now, I've been using a single function for : receiving the stream, parsing it manually (in order to find JPEG data starting- and end-point), decoding the data (i.e. initializing the libjpeg structures, reading the JPEG header, doing the actual decoding...) and finally pushing it into the graph. This works properly, but in order to make things smoother and tidier, i'd like to use a function for receiving, that calls another function (later, a thread) for decoding/pushing.
So, as a first step, instead of doing all the JPEG work right after finding the data in the stream, I simply call another function which is in charge of the JPEG structures init / header reading / decoding / pushing.
And this is where I get an error I can't decipher : "improper call to jpeg library in state 205".
// EDITED FOR CLARITY
For now, my code looks like :
void receive1() { while(1) { if(recvfrom(/* ... */) > 0) { /* parse the received data for JPEG start and end */ /* decode the JPEG */ //Declarations struct jpeg_decompress_struct cinfo; struct my_error_mgr jerr; // Step 1: allocation / initialization cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = my_error_exit; if(setjmp(jerr.setjmp_buffer)) { printf("--ERROR LIBJPEG\n"); /* quit with error code */ } // Next steps : proceed with the decoding and displaying... jpeg_create_decompress(&cinfo); /* ... */ } } }
I'd like to have my code look like :
void receive2() { while(1) { if(recvfrom(/* ... */) > 0) { /* parse the received data for JPEG start and end */ decode(data); } } } int decode(char* data) { /* decode the JPEG */ //Declarations struct jpeg_decompress_struct cinfo; struct my_error_mgr jerr; // Step 1: allocation / initialization cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = my_error_exit; if(setjmp(jerr.setjmp_buffer)) // This is where the "state 205" error occurs { printf("--ERROR LIBJPEG\n"); /* quit with error code */ } // Next steps : proceed with the decoding and displaying... jpeg_create_decompress(&cinfo); /* ... */ return 0; }
Any help would be appreciated here. Thanks a lot !
// EDIT
I'm realizing I've omitted quite a lot of information in my original post. Here are some (maybe) useful details :
- I'm using VS2008, but for many reasons I don't use the built-in debugger or emulators. Instead, the exe file is directly deployed and tested on the Windows Mobile device, using a custom dos-like command prompt.
- libjpeg originally reads from and writes to files, but I'm using a patch (and custom error handler) enabling to read data directly from a buffer, without opening a file. Code can be found here, and it uses an "extended error handler" defined this way :
typedef struct my_error_mgr * my_error_ptr; struct my_error_mgr { struct jpeg_error_mgr pub; jmp_buf setjmp_buffer; }; METHODDEF(void) my_error_exit (j_common_ptr cinfo) { my_error_ptr myerr = (my_error_ptr) cinfo->err; /* Always display the message. */ (*cinfo->err->output_message) (cinfo); /* Return control to the setjmp point */ longjmp(myerr->setjmp_buffer, 1); }
printf("ERROR\n"); abort(1);
, and the caller don't expect a return from it. So returning from it normally will completely f*#$ up the rest of the program flow. – ruslik