--- trunk/xvidcore/examples/xvid_stat.c 2002/09/28 14:27:16 559 +++ 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.14 2002-09-28 14:27:15 edgomez Exp $ + * $Id: xvid_stat.c,v 1.21 2003-02-16 05:11:39 suxen_drol Exp $ * ****************************************************************************/ @@ -44,6 +44,7 @@ * -q integer : quality ([0..5]) * -d boolean : save decoder output (0 False*, !=0 True) * -m boolean : save mpeg4 raw stream (0 False*, !=0 True) + * -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,7 +66,7 @@ #include #include #include -#ifndef _MSC_VER +#ifndef WIN32 #include #else #include @@ -99,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(); @@ -112,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 */ }; @@ -141,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; @@ -152,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) @@ -159,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 ;-) ***************************************************************************/ @@ -183,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"); @@ -261,6 +279,10 @@ i++; ARG_SAVEMPEGSTREAM = atoi(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); @@ -301,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; } @@ -355,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 @@ -363,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) { @@ -391,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; @@ -404,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); @@ -500,7 +585,7 @@ printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br")); 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",1000/totalenctime, 1000/totaldectime); @@ -552,7 +637,7 @@ /* Return time elapsed time in miliseconds since the program started */ static double msecond() { -#ifndef _MSC_VER +#ifndef WIN32 struct timeval tv; gettimeofday(&tv, 0); return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; @@ -768,7 +853,7 @@ if(use_assembler) { -#ifdef ARCH_IA64 +#ifdef ARCH_IS_IA64 xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; #else xinit.cpu_flags = 0; @@ -818,7 +903,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; @@ -826,21 +912,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 @@ -895,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);