--- trunk/xvidcore/examples/xvid_stat.c 2002/09/15 20:22:01 484 +++ trunk/xvidcore/examples/xvid_stat.c 2002/12/18 20:48:25 728 @@ -19,7 +19,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_stat.c,v 1.7 2002-09-15 20:22:01 edgomez Exp $ + * $Id: xvid_stat.c,v 1.17 2002-12-18 20:48:25 edgomez Exp $ * ****************************************************************************/ @@ -44,7 +44,8 @@ * -q integer : quality ([0..5]) * -d boolean : save decoder output (0 False*, !=0 True) * -m boolean : save mpeg4 raw stream (0 False*, !=0 True) - * -h, -help : prints this help message + * -mv integer : Hinted Motion Estimation (0 none, 1 get hints, 2 set hints) + * -help : prints this help message * -quant integer : fixed quantizer (disables -b setting) * (* means default) * @@ -65,9 +66,13 @@ #include #include #include +#ifndef _MSC_VER +#include +#else #include +#endif -#include "../src/xvid.h" +#include "xvid.h" /**************************************************************************** * Prototypes @@ -95,7 +100,8 @@ static int enc_init(int use_assembler); static int enc_stop(); static int enc_main(unsigned char* image, unsigned char* bitstream, - int *streamlength, int* frametype); + unsigned char* hints_buffer, + long *streamlength, long* frametype, long* hints_size); /* Decoder related functions */ static int dec_stop(); @@ -108,25 +114,25 @@ ****************************************************************************/ static int const motion_presets[7] = { - 0, // Q 0 - PMV_EARLYSTOP16, // Q 1 - PMV_EARLYSTOP16, // Q 2 - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 3 - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 4 - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | // Q 5 + 0, /* Q 0 */ + PMV_EARLYSTOP16, /* Q 1 */ + PMV_EARLYSTOP16, /* Q 2 */ + PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */ + PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */ + PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */ PMV_HALFPELREFINE8, - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | // Q 6 + PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 }; static int const general_presets[7] = { - XVID_H263QUANT, // Q 0 - XVID_MPEGQUANT, // Q 1 - XVID_H263QUANT, // Q 2 - XVID_H263QUANT | XVID_HALFPEL, // Q 3 - XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 4 - XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 5 - XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V // Q 6 + XVID_H263QUANT, /* Q 0 */ + XVID_MPEGQUANT, /* Q 1 */ + XVID_H263QUANT, /* Q 2 */ + XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */ + XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */ + XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */ + XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */ }; @@ -137,6 +143,12 @@ /* Maximum number of frames to encode */ #define ABS_MAXFRAMENR 9999 +/* HINTMODEs */ +#define HINT_MODE_NONE 0 +#define HINT_MODE_GET 1 +#define HINT_MODE_SET 2 +#define HINT_FILE "hints.mv" + static int ARG_BITRATE = 900; static int ARG_QUANTI = 0; static int ARG_QUALITY = 6; @@ -148,6 +160,7 @@ static int ARG_INPUTTYPE = 0; static int ARG_SAVEDECOUTPUT = 0; static int ARG_SAVEMPEGSTREAM = 0; +static int ARG_HINTMODE = HINT_MODE_NONE; static int XDIM = 0; static int YDIM = 0; #define IMAGE_SIZE(x,y) ((x)*(y)*3/2) @@ -155,6 +168,12 @@ #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) #define SMALL_EPS 1e-10 +#define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \ + (((long)(c))<<8) |((long)(d))) + +#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ + (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) + /**************************************************************************** * Nasty global vars ;-) ***************************************************************************/ @@ -179,27 +198,30 @@ unsigned char *divx_buffer = NULL; unsigned char *in_buffer = NULL; unsigned char *out_buffer = NULL; + unsigned char *hints_buffer = NULL; double enctime,dectime; double totalenctime=0.; double totaldectime=0.; long totalsize=0; + long hints_size; int status; - - int m4v_size; - int frame_type[ABS_MAXFRAMENR]; + int bigendian; + + long m4v_size; + long frame_type[ABS_MAXFRAMENR]; int Iframes=0, Pframes=0, use_assembler=0; double framepsnr[ABS_MAXFRAMENR]; double Ipsnr=0.,Imaxpsnr=0.,Iminpsnr=999.,Ivarpsnr=0.; double Ppsnr=0.,Pmaxpsnr=0.,Pminpsnr=999.,Pvarpsnr=0.; - double Bpsnr=0.,Bmaxpsnr=0.,Bminpsnr=999.,Bvarpsnr=0.; char filename[256]; FILE *filehandle; FILE *in_file = stdin; + FILE *hints_file = NULL; printf("xvid_stat - XviD core library test program "); printf("written by Christoph Lampert 2002\n\n"); @@ -257,7 +279,11 @@ i++; ARG_SAVEMPEGSTREAM = atoi(argv[i]); } - else if (strcmp("-h", argv[i]) == 0 || strcmp("-help", argv[i])) { + else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { + i++; + ARG_HINTMODE = atoi(argv[i]); + } + else if (strcmp("-help", argv[i])) { usage(); return(0); } @@ -273,10 +299,7 @@ ****************************************************************************/ if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { - fprintf(stderr, - "Trying to retreive width and height from PGM header\n", - XDIM, - YDIM); + fprintf(stderr, "Trying to retreive width and height from PGM header\n"); ARG_INPUTTYPE = 1; /* pgm */ } @@ -291,7 +314,7 @@ } if ( ARG_FRAMERATE <= 0) { - fprintf(stderr,"Wrong Fraterate %s \n",argv[5]); + fprintf(stderr,"Wrong Framerate %s \n",argv[5]); return -1; } @@ -300,6 +323,37 @@ return -1; } + if ( ARG_HINTMODE != HINT_MODE_NONE && + ARG_HINTMODE != HINT_MODE_GET && + ARG_HINTMODE != HINT_MODE_SET) + ARG_HINTMODE = HINT_MODE_NONE; + + if( ARG_HINTMODE != HINT_MODE_NONE) { + char *rights = "rb"; + + /* + * If we are getting hints from core, we will have to write them to + * hint file + */ + if(ARG_HINTMODE == HINT_MODE_GET) + rights = "w+b"; + + /* Open the hint file */ + hints_file = fopen(HINT_FILE, rights); + if(hints_file == NULL) { + fprintf(stderr, "Error opening input file %s\n", HINT_FILE); + return -1; + } + + /* Allocate hint memory space, we will be using rawhints */ + /* NB : Hope 1Mb is enough */ + if((hints_buffer = malloc(1024*1024)) == NULL) { + fprintf(stderr, "Memory allocation error\n"); + return -1; + } + + } + if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { in_file = stdin; } @@ -354,6 +408,11 @@ goto release_all; } + totalsize = LONG_PACK('M','P','4','U'); + if(*((char *)(&totalsize)) == 'M') + bigendian = 1; + else + bigendian = 0; /***************************************************************************** * Main loop @@ -362,9 +421,9 @@ do { if (ARG_INPUTTYPE) - status = read_pgmdata(in_file, in_buffer); // read PGM data (YUV-format) + status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */ else - status = read_yuvdata(in_file, in_buffer); // read raw data (YUV-format) + status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */ if (status) { @@ -390,11 +449,22 @@ */ /***************************************************************************** + * Read hints from file + ****************************************************************************/ + + if(ARG_HINTMODE == HINT_MODE_SET) { + fread(&hints_size, 1, sizeof(long), hints_file); + hints_size = (!bigendian)?SWAP(hints_size):hints_size; + fread(hints_buffer, 1, hints_size, hints_file); + } + +/***************************************************************************** * Encode and decode this frame ****************************************************************************/ enctime = msecond(); - status = enc_main(in_buffer, divx_buffer, &m4v_size, &frame_type[filenr]); + status = enc_main(in_buffer, divx_buffer, hints_buffer, + &m4v_size, &frame_type[filenr], &hints_size); enctime = msecond() - enctime; totalenctime += enctime; @@ -403,6 +473,21 @@ printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6d bytes ", (int)filenr, (int)frame_type[filenr], (float)enctime, (int)m4v_size); +/***************************************************************************** + * Save hints to file + ****************************************************************************/ + + if(ARG_HINTMODE == HINT_MODE_GET) { + hints_size = (!bigendian)?SWAP(hints_size):hints_size; + fwrite(&hints_size, 1, sizeof(long), hints_file); + hints_size = (!bigendian)?SWAP(hints_size):hints_size; + fwrite(hints_buffer, 1, hints_size, hints_file); + } + +/***************************************************************************** + * Save stream to file + ****************************************************************************/ + if (ARG_SAVEMPEGSTREAM) { sprintf(filename, "%sframe%05d.m4v", filepath, filenr); @@ -422,7 +507,7 @@ * Analyse the decoded frame and compare to original ****************************************************************************/ - framepsnr[filenr] = PSNR(XDIM,YDIM, in_buffer, XDIM, out_buffer, XDIM); + framepsnr[filenr] = PSNR(XDIM,YDIM*3/2, in_buffer, XDIM, out_buffer, XDIM); printf("dectime =%6.1f ms PSNR %5.2f\n",dectime, framepsnr[filenr]); @@ -497,12 +582,12 @@ /* Print all statistics */ printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br")); - printf("%04d ",MAX(ARG_QUANTI,ARG_BITRATE)); + printf("%04d ",(ARG_QUANTI)?ARG_QUANTI:ARG_BITRATE); printf("( %.2f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE); - printf("size %6d ",totalsize); + printf("size %6d ", (int)totalsize); printf("( %4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000)); printf("/ %.2f bpp) ",(double)totalsize*8/XDIM/YDIM); - printf("enc: %6.1f fps, dec: %6.1f fps \n",1/totalenctime, 1/totaldectime); + printf("enc: %6.1f fps, dec: %6.1f fps \n",1000/totalenctime, 1000/totaldectime); printf("PSNR P(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Pframes,Ppsnr,Pminpsnr,Pmaxpsnr,sqrt(Pvarpsnr/filenr)); printf("I(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Iframes,Ipsnr,Iminpsnr,Imaxpsnr,sqrt(Ivarpsnr/filenr)); printf("\n"); @@ -551,12 +636,15 @@ /* Return time elapsed time in miliseconds since the program started */ static double msecond() { +#ifndef _MSC_VER + struct timeval tv; + gettimeofday(&tv, 0); + return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; +#else clock_t clk; - clk = clock(); - return clk * 1000 / CLOCKS_PER_SEC; - +#endif } @@ -628,7 +716,7 @@ fprintf(stderr, " -q integer : quality ([0..5])\n"); fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); - fprintf(stderr, " -h, -help : prints this help message\n"); + fprintf(stderr, " -help : prints this help message\n"); fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); fprintf(stderr, " (* means default)\n"); @@ -701,7 +789,7 @@ static int read_yuvdata(FILE* handle, unsigned char *image) { - if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != IMAGE_SIZE(XDIM, YDIM)) + if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) return 1; else return 0; @@ -814,7 +902,8 @@ } static int enc_main(unsigned char* image, unsigned char* bitstream, - int *streamlength, int* frametype) + unsigned char* hints_buffer, + long *streamlength, long* frametype, long* hints_size) { int xerr; @@ -822,21 +911,37 @@ XVID_ENC_STATS xstats; xframe.bitstream = bitstream; - xframe.length = -1; // this is written by the routine + xframe.length = -1; /* this is written by the routine */ xframe.image = image; - xframe.colorspace = XVID_CSP_YV12; // defined in + xframe.colorspace = XVID_CSP_YV12; /* defined in */ - xframe.intra = -1; // let the codec decide between I-frame (1) and P-frame (0) + xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */ - xframe.quant = ARG_QUANTI; // is quant != 0, use a fixed quant (and ignore bitrate) + xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */ xframe.motion = motion_presets[ARG_QUALITY]; xframe.general = general_presets[ARG_QUALITY]; xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; + xframe.hint.hintstream = hints_buffer; + + if(ARG_HINTMODE == HINT_MODE_SET) { + xframe.hint.hintlength = *hints_size; + xframe.hint.rawhints = 0; + xframe.general |= XVID_HINTEDME_SET; + } + + if(ARG_HINTMODE == HINT_MODE_GET) { + xframe.hint.rawhints = 0; + xframe.general |= XVID_HINTEDME_GET; + } + xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); + if(ARG_HINTMODE == HINT_MODE_GET) + *hints_size = xframe.hint.hintlength; + /* * This is statictical data, e.g. for 2-pass. If you are not * interested in any of this, you can use NULL instead of &xstats @@ -891,7 +996,7 @@ xframe.length = m4v_size; xframe.image = out_buffer; xframe.stride = XDIM; - xframe.colorspace = XVID_CSP_YV12; // XVID_CSP_USER is fastest (no memcopy involved) + xframe.colorspace = XVID_CSP_YV12; /* XVID_CSP_USER is fastest (no memcopy involved) */ xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL);