--- trunk/xvidcore/examples/xvid_decraw.c 2004/04/12 14:05:08 1423 +++ trunk/xvidcore/examples/xvid_decraw.c 2004/09/04 14:16:24 1547 @@ -20,7 +20,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: xvid_decraw.c,v 1.12 2004-04-12 14:05:08 edgomez Exp $ + * $Id: xvid_decraw.c,v 1.19 2004-09-04 14:16:24 edgomez Exp $ * ****************************************************************************/ @@ -54,9 +54,6 @@ * Global vars in module and constants ****************************************************************************/ -/* max number of frames */ -#define ABS_MAXFRAMENR 9999 - #define USE_PNM 0 #define USE_TGA 1 @@ -74,13 +71,13 @@ #define BUFFER_SIZE (2*1024*1024) +static const int display_buffer_bytes = 0; + /***************************************************************************** * Local prototypes ****************************************************************************/ static double msecond(); -static int write_pgm(char *filename, - unsigned char *image); static int dec_init(int use_assembler, int debug_level); static int dec_main(unsigned char *istream, unsigned char *ostream, @@ -88,7 +85,9 @@ xvid_dec_stats_t *xvid_dec_stats); static int dec_stop(); static void usage(); - +static int write_image(char *prefix, unsigned char *image); +static int write_pnm(char *filename, unsigned char *image); +static int write_tga(char *filename, unsigned char *image); const char * type2str(int type) { @@ -111,6 +110,7 @@ unsigned char *mp4_ptr = NULL; unsigned char *out_buffer = NULL; int useful_bytes; + int chunk; xvid_dec_stats_t xvid_dec_stats; double totaldectime; @@ -184,6 +184,12 @@ } } +#if defined(_MSC_VER) + if (ARG_INPUTFILE==NULL) { + fprintf(stderr, "Warning: MSVC build does not read EOF correctly from stdin. Use the -i switch.\n\n"); + } +#endif + /***************************************************************************** * Values checking ****************************************************************************/ @@ -238,7 +244,8 @@ totalsize = 0; filenr = 0; mp4_ptr = mp4_buffer; - + chunk = 0; + do { int used_bytes = 0; double dectime; @@ -296,6 +303,22 @@ fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM); } + + /* Save individual mpeg4 stream if required */ + if(ARG_SAVEMPEGSTREAM) { + FILE *filehandle = NULL; + + sprintf(filename, "%svolhdr.m4v", filepath); + filehandle = fopen(filename, "wb"); + if(!filehandle) { + fprintf(stderr, + "Error writing vol header mpeg4 stream to file %s\n", + filename); + } else { + fwrite(mp4_ptr, 1, used_bytes, filehandle); + fclose(filehandle); + } + } } /* Update buffer pointers */ @@ -307,7 +330,10 @@ totalsize += used_bytes; } - }while(xvid_dec_stats.type <= 0 && useful_bytes > 0); + if (display_buffer_bytes) { + printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes); + } + } while (xvid_dec_stats.type <= 0 && useful_bytes > 0); /* Check if there is a negative number of useful bytes left in buffer * This means we went too far */ @@ -318,9 +344,11 @@ totaldectime += dectime; - printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", - filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); - + if (!display_buffer_bytes) { + printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", + filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); + } + /* Save individual mpeg4 stream if required */ if(ARG_SAVEMPEGSTREAM) { FILE *filehandle = NULL; @@ -343,14 +371,16 @@ sprintf(filename, "%sdec%05d", filepath, filenr); if(write_image(filename, out_buffer)) { fprintf(stderr, - "Error writing decoded PGM frame %s\n", + "Error writing decoded frame %s\n", filename); } } filenr++; - } while ( (status>=0) && (filenr0 || !feof(in_file)); + + useful_bytes = 0; /* Empty buffer */ /***************************************************************************** * Flush decoder buffers @@ -366,7 +396,10 @@ dectime = msecond(); used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); dectime = msecond() - dectime; - }while(used_bytes>=0 && xvid_dec_stats.type <= 0); + if (display_buffer_bytes) { + printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes); + } + } while(used_bytes>=0 && xvid_dec_stats.type <= 0); if (used_bytes < 0) { /* XVID_ERR_END */ break; @@ -376,15 +409,17 @@ totaldectime += dectime; /* Prints some decoding stats */ - printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", - filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); - + if (!display_buffer_bytes) { + printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", + filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); + } + /* Save output frame if required */ if (ARG_SAVEDECOUTPUT) { sprintf(filename, "%sdec%05d", filepath, filenr); if(write_image(filename, out_buffer)) { fprintf(stderr, - "Error writing decoded PGM frame %s\n", + "Error writing decoded frame %s\n", filename); } } @@ -397,11 +432,14 @@ * Calculate totals and averages for output, print results ****************************************************************************/ - totalsize /= filenr; - totaldectime /= filenr; - - printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n", - totaldectime, 1000/totaldectime, (int)totalsize); + if (filenr>0) { + totalsize /= filenr; + totaldectime /= filenr; + printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n", + totaldectime, 1000/totaldectime, (int)totalsize); + }else{ + printf("Nothing was decoded!\n"); + } /***************************************************************************** * XviD PART Stop @@ -465,7 +503,7 @@ * output functions ****************************************************************************/ -int write_image(char *prefix, unsigned char *image) +static int write_image(char *prefix, unsigned char *image) { char filename[1024]; char *ext; @@ -493,7 +531,7 @@ return(ret); } -int write_tga(char *filename, unsigned char *image) +static int write_tga(char *filename, unsigned char *image) { FILE * f; char hdr[18]; @@ -574,7 +612,7 @@ return(0); } -int write_pnm(char *filename, unsigned char *image) +static int write_pnm(char *filename, unsigned char *image) { FILE * f; @@ -613,6 +651,7 @@ return 0; } + /***************************************************************************** * Routines for decoding: init decoder, use, and stop decoder ****************************************************************************/ @@ -626,6 +665,10 @@ xvid_gbl_init_t xvid_gbl_init; xvid_dec_create_t xvid_dec_create; + /* Reset the structure with zeros */ + memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init_t)); + memset(&xvid_dec_create, 0, sizeof(xvid_dec_create_t)); + /*------------------------------------------------------------------------ * XviD core initialization *----------------------------------------------------------------------*/ @@ -680,6 +723,10 @@ xvid_dec_frame_t xvid_dec_frame; + /* Reset all structures */ + memset(&xvid_dec_frame, 0, sizeof(xvid_dec_frame_t)); + memset(xvid_dec_stats, 0, sizeof(xvid_dec_stats_t)); + /* Set version */ xvid_dec_frame.version = XVID_VERSION; xvid_dec_stats->version = XVID_VERSION;