--- xvid_decraw.c 2004/04/15 19:44:05 1.14 +++ xvid_decraw.c 2010/01/05 09:25:19 1.25 @@ -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.14 2004/04/15 19:44:05 edgomez Exp $ + * $Id: xvid_decraw.c,v 1.25 2010/01/05 09:25:19 Isibaar Exp $ * ****************************************************************************/ @@ -54,11 +54,9 @@ * Global vars in module and constants ****************************************************************************/ -/* max number of frames */ -#define ABS_MAXFRAMENR 9999 - #define USE_PNM 0 #define USE_TGA 1 +#define USE_YUV 2 static int XDIM = 0; static int YDIM = 0; @@ -74,13 +72,15 @@ #define BUFFER_SIZE (2*1024*1024) +static const int display_buffer_bytes = 0; + +#define MIN_USEFUL_BYTES 1 + /***************************************************************************** * 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,9 +88,10 @@ xvid_dec_stats_t *xvid_dec_stats); static int dec_stop(); static void usage(); -int write_image(char *prefix, unsigned char *image); -int write_pnm(char *filename, unsigned char *image); -int write_tga(char *filename, unsigned char *image); +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); +static int write_yuv(char *filename, unsigned char *image); const char * type2str(int type) { @@ -113,6 +114,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; @@ -174,6 +176,8 @@ i++; if (strcmp(argv[i], "tga") == 0) { FORMAT = USE_TGA; + } else if (strcmp(argv[i], "yuv") == 0) { + FORMAT = USE_YUV; } else { FORMAT = USE_PNM; } @@ -212,6 +216,9 @@ if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) { FORMAT = USE_TGA; } + if (BPP != 1 && FORMAT == USE_YUV) { + FORMAT = USE_TGA; + } /***************************************************************************** * Memory allocation @@ -246,7 +253,8 @@ totalsize = 0; filenr = 0; mp4_ptr = mp4_buffer; - + chunk = 0; + do { int used_bytes = 0; double dectime; @@ -266,13 +274,11 @@ mp4_ptr = mp4_buffer; /* read new data */ - if(feof(in_file)) - break; - - useful_bytes += fread(mp4_buffer + already_in_buffer, - 1, BUFFER_SIZE - already_in_buffer, - in_file); - + if(!feof(in_file)) { + useful_bytes += fread(mp4_buffer + already_in_buffer, + 1, BUFFER_SIZE - already_in_buffer, + in_file); + } } @@ -304,6 +310,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 */ @@ -315,7 +337,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 > MIN_USEFUL_BYTES); /* Check if there is a negative number of useful bytes left in buffer * This means we went too far */ @@ -326,9 +351,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; @@ -348,17 +375,24 @@ /* Save output frame if required */ if (ARG_SAVEDECOUTPUT) { - sprintf(filename, "%sdec%05d", filepath, filenr); + if (FORMAT == USE_YUV) { + sprintf(filename, "%sdec", filepath); + } + else { + 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) && (filenrMIN_USEFUL_BYTES || !feof(in_file)); + + useful_bytes = 0; /* Empty buffer */ /***************************************************************************** * Flush decoder buffers @@ -374,7 +408,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; @@ -384,12 +421,19 @@ 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 (FORMAT == USE_YUV) { + sprintf(filename, "%sdec", filepath); + } + else { + sprintf(filename, "%sdec%05d", filepath, filenr); + } if(write_image(filename, out_buffer)) { fprintf(stderr, "Error writing decoded frame %s\n", @@ -446,7 +490,7 @@ fprintf(stderr, " -i string : input filename (default=stdin)\n"); fprintf(stderr, " -d : save decoder output\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, " -f format : choose output file format (tga, pnm, pgm, yuv)\n"); fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); fprintf(stderr, " -help : This help message\n"); fprintf(stderr, " (* means default)\n"); @@ -468,7 +512,7 @@ #else clock_t clk; clk = clock(); - return(clk * 1000 / CLOCKS_PER_SEC); + return(clk * 1000.0 / CLOCKS_PER_SEC); #endif } @@ -476,7 +520,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; @@ -486,6 +530,8 @@ ext = "pgm"; } else if (FORMAT == USE_PNM && BPP == 3) { ext = "pnm"; + } else if (FORMAT == USE_YUV) { + ext = "yuv"; } else if (FORMAT == USE_TGA) { ext = "tga"; } else { @@ -497,6 +543,8 @@ if (FORMAT == USE_PNM) { ret = write_pnm(filename, image); + } else if (FORMAT == USE_YUV) { + ret = write_yuv(filename, image); } else { ret = write_tga(filename, image); } @@ -504,7 +552,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]; @@ -547,21 +595,21 @@ #else { int i; - for (i=0; iversion = XVID_VERSION;