--- branches/release-1_0-branch/xvidcore/examples/xvid_decraw.c 2004/04/07 22:02:56 1414 +++ branches/release-1_0-branch/xvidcore/examples/xvid_decraw.c 2004/04/15 22:39:30 1433 @@ -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.10.2.1 2004-04-07 22:02:56 edgomez Exp $ + * $Id: xvid_decraw.c,v 1.10.2.4 2004-04-15 22:39:30 edgomez Exp $ * ****************************************************************************/ @@ -57,6 +57,9 @@ /* max number of frames */ #define ABS_MAXFRAMENR 9999 +#define USE_PNM 0 +#define USE_TGA 1 + static int XDIM = 0; static int YDIM = 0; static int ARG_SAVEDECOUTPUT = 0; @@ -64,6 +67,7 @@ static char *ARG_INPUTFILE = NULL; static int CSP = XVID_CSP_I420; static int BPP = 1; +static int FORMAT = USE_PNM; static char filepath[256] = "./"; static void *dec_handle = NULL; @@ -75,8 +79,6 @@ ****************************************************************************/ static double msecond(); -static int write_pgm(char *filename, - unsigned char *image); static int dec_init(int use_assembler); static int dec_main(unsigned char *istream, unsigned char *ostream, @@ -84,7 +86,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) { @@ -158,6 +162,13 @@ CSP = XVID_CSP_I420; BPP = 1; } + } else if (strcmp("-f", argv[i]) == 0 && i < argc -1) { + i++; + if (strcmp(argv[i], "tga") == 0) { + FORMAT = USE_TGA; + } else { + FORMAT = USE_PNM; + } } else if (strcmp("-help", argv[i]) == 0) { usage(); return(0); @@ -167,6 +178,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 ****************************************************************************/ @@ -183,6 +200,11 @@ } } + /* PNM/PGM format can't handle 16/32 bit data */ + if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) { + FORMAT = USE_TGA; + } + /***************************************************************************** * Memory allocation ****************************************************************************/ @@ -318,10 +340,10 @@ /* Save output frame if required */ if (ARG_SAVEDECOUTPUT) { - sprintf(filename, "%sdec%05d.tga", filepath, filenr); - if(write_tga(filename,out_buffer)) { + 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); } } @@ -359,10 +381,10 @@ /* Save output frame if required */ if (ARG_SAVEDECOUTPUT) { - sprintf(filename, "%sdec%05d.tga", filepath, filenr); - if(write_tga(filename, out_buffer)) { + 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); } } @@ -375,11 +397,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 @@ -411,7 +436,8 @@ fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); fprintf(stderr, " -i string : input filename (default=stdin)\n"); fprintf(stderr, " -d : save decoder output\n"); - fprintf(stderr, " -c csp : choose colorspace output (csp can be one of rgb16, rgb24, rgb32, yv12, i420)\n"); + fprintf(stderr, " -c csp : choose colorspace output (rgb16, rgb24, rgb32, yv12, i420)\n"); + fprintf(stderr, " -f format : choose output file format (tga, pnm, pgm)\n"); fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); fprintf(stderr, " -help : This help message\n"); fprintf(stderr, " (* means default)\n"); @@ -441,7 +467,35 @@ * output functions ****************************************************************************/ -int write_tga(char *filename, unsigned char *image) +static int write_image(char *prefix, unsigned char *image) +{ + char filename[1024]; + char *ext; + int ret; + + if (FORMAT == USE_PNM && BPP == 1) { + ext = "pgm"; + } else if (FORMAT == USE_PNM && BPP == 3) { + ext = "pnm"; + } else if (FORMAT == USE_TGA) { + ext = "tga"; + } else { + fprintf(stderr, "Bug: should not reach this path code -- please report to xvid-devel@xvid.org with command line options used"); + exit(-1); + } + + sprintf(filename, "%s.%s", prefix, ext); + + if (FORMAT == USE_PNM) { + ret = write_pnm(filename, image); + } else { + ret = write_tga(filename, image); + } + + return(ret); +} + +static int write_tga(char *filename, unsigned char *image) { FILE * f; char hdr[18]; @@ -510,8 +564,8 @@ /* Write the two chrominance planes */ for (i=0; i