--- trunk/xvidcore/examples/xvid_stat.c 2002/09/14 23:54:17 483 +++ trunk/xvidcore/examples/xvid_stat.c 2003/02/16 05:11:39 860 @@ -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.6 2002-09-14 23:54:17 edgomez Exp $ + * $Id: xvid_stat.c,v 1.21 2003-02-16 05:11:39 suxen_drol 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 WIN32 +#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,12 +160,20 @@ 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) #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 ;-) ***************************************************************************/ @@ -178,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 totalsize = 0; + long hints_size = 0; int status; - - int m4v_size; - int frame_type[ABS_MAXFRAMENR]; + int bigendian = 0; + + long m4v_size = 0; + 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"); @@ -256,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); } @@ -271,17 +298,10 @@ * Arguments checking ****************************************************************************/ - if ( ARG_INPUTTYPE == 0 && - ((XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048)) ) { - fprintf(stderr, - "Wrong frame sizes width=%d height=%d, trying PGM header infos\n", - XDIM, - YDIM); + if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { + fprintf(stderr, "Trying to retreive width and height from PGM header\n"); ARG_INPUTTYPE = 1; /* pgm */ } - else { - YDIM = YDIM*3/2; /* YUV */ - } if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { fprintf(stderr,"Wrong Quality\n"); @@ -294,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; } @@ -303,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; } @@ -318,25 +369,22 @@ if (ARG_INPUTTYPE) { if (read_pgmheader(in_file)) { fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); - return -11; + return -1; } } /* now we know the sizes, so allocate memory */ - in_buffer = (unsigned char *) malloc(XDIM*YDIM); + in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); if (!in_buffer) goto free_all_memory; /* this should really be enough memory ! */ - divx_buffer = (unsigned char *) malloc(XDIM*YDIM*2); + divx_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2); if (!divx_buffer) goto free_all_memory; - /* PGM is YUV 4:2:0 format, so real image height is *2/3 of PGM picture */ - YDIM = YDIM*2/3; - - out_buffer = (unsigned char *) malloc(XDIM*YDIM*4); + out_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*4); if (!out_buffer) goto free_all_memory; @@ -360,6 +408,12 @@ goto release_all; } + totalsize = LONG_PACK('M','P','4','U'); + if(*((char *)(&totalsize)) == 'M') + bigendian = 1; + else + bigendian = 0; + totalsize = 0; /***************************************************************************** * Main loop @@ -368,9 +422,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) { @@ -396,11 +450,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; @@ -409,6 +474,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); @@ -428,7 +508,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]); @@ -503,12 +583,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"); @@ -557,12 +637,15 @@ /* Return time elapsed time in miliseconds since the program started */ static double msecond() { +#ifndef WIN32 + 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 } @@ -634,7 +717,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"); @@ -657,6 +740,7 @@ if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) return 1; + fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) { @@ -666,7 +750,7 @@ if ( (XDIM==0) || (YDIM==0) ) { XDIM=xsize; - YDIM=ysize; + YDIM=ysize*2/3; } return 0; @@ -677,29 +761,36 @@ int i; char dummy; - unsigned char* buff1_ptr2 = image + XDIM*YDIM; - unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2; + unsigned char *y = image; + unsigned char *u = image + XDIM*YDIM; + unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; - fread(image,XDIM*YDIM,1, handle); // read Y component of picture + /* read Y component of picture */ + fread(y, 1, XDIM*YDIM, handle); for (i=0;i + 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 @@ -864,7 +997,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);