--- trunk/xvidcore/examples/xvid_encraw.c 2002/12/18 20:48:25 728 +++ trunk/xvidcore/examples/xvid_encraw.c 2005/07/05 20:39:52 1623 @@ -3,7 +3,9 @@ * XVID MPEG-4 VIDEO CODEC * - Console based test application - * - * Copyright(C) 2002 Christoph Lampert + * Copyright(C) 2002-2003 Christoph Lampert + * 2002-2003 Edouard Gomez + * 2003 Peter Ross * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,18 +21,20 @@ * 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_encraw.c,v 1.6 2002-12-18 20:48:25 edgomez Exp $ + * $Id: xvid_encraw.c,v 1.18 2005-07-05 20:39:52 chl Exp $ * ****************************************************************************/ /***************************************************************************** * Application notes : * - * A sequence of YUV pics in PGM file format is encoded and decoded - * The speed is measured and PSNR of decoded picture is calculated. + * A sequence of raw YUV I420 pics or YUV I420 PGM file format is encoded + * The speed is measured and frames' PSNR are taken from core. * * The program is plain C and needs no libraries except for libxvidcore, * and maths-lib. + * + * Use ./xvid_encraw -help for a list of options * ************************************************************************/ @@ -38,7 +42,7 @@ #include #include #include -#ifndef _MSC_VER +#ifndef WIN32 #include #else #include @@ -46,68 +50,121 @@ #include "xvid.h" +#undef READ_PNM + /***************************************************************************** * Quality presets ****************************************************************************/ -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 */ - PMV_HALFPELREFINE8, - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ - PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 +static const int motion_presets[] = { + /* quality 0 */ + 0, + + /* quality 1 */ + XVID_ME_ADVANCEDDIAMOND16, + + /* quality 2 */ + XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16, + + /* quality 3 */ + XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | + XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8, + + /* quality 4 */ + XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | + XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | + XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, + + /* quality 5 */ + XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | + XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | + XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, + + /* quality 6 */ + XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | + XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH8 | + XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, + }; +#define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0])) + +static const int vop_presets[] = { + /* quality 0 */ + 0, + + /* quality 1 */ + 0, + + /* quality 2 */ + XVID_VOP_HALFPEL, + + /* quality 3 */ + XVID_VOP_HALFPEL | XVID_VOP_INTER4V, + + /* quality 4 */ + XVID_VOP_HALFPEL | XVID_VOP_INTER4V, + + /* quality 5 */ + XVID_VOP_HALFPEL | XVID_VOP_INTER4V | + XVID_VOP_TRELLISQUANT, + + /* quality 6 */ + XVID_VOP_HALFPEL | XVID_VOP_INTER4V | + XVID_VOP_TRELLISQUANT | XVID_VOP_HQACPRED, -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 */ }; - +#define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0])) /***************************************************************************** * Command line global variables ****************************************************************************/ +#define MAX_ZONES 64 + +static xvid_enc_zone_t ZONES[MAX_ZONES]; +static int NUM_ZONES = 0; + /* 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; -static int ARG_MINQUANT = 1; -static int ARG_MAXQUANT = 31; +static int ARG_STATS = 0; +static int ARG_DUMP = 0; +static int ARG_LUMIMASKING = 0; +static int ARG_BITRATE = 0; +static int ARG_SINGLE = 0; +static char *ARG_PASS1 = 0; +static char *ARG_PASS2 = 0; +static int ARG_QUALITY = ME_ELEMENTS - 1; static float ARG_FRAMERATE = 25.00f; -static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; +static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; +static int ARG_MAXKEYINTERVAL = 0; static char *ARG_INPUTFILE = NULL; -static int ARG_INPUTTYPE = 0; -static int ARG_SAVEMPEGSTREAM = 0; -static int ARG_OUTPUTTYPE = 0; +static int ARG_INPUTTYPE = 0; +static int ARG_SAVEMPEGSTREAM = 0; +static int ARG_SAVEINDIVIDUAL = 0; static char *ARG_OUTPUTFILE = NULL; -static int ARG_HINTMODE = HINT_MODE_NONE; -static int XDIM = 0; -static int YDIM = 0; +static int XDIM = 0; +static int YDIM = 0; +static int ARG_BQRATIO = 150; +static int ARG_BQOFFSET = 100; +static int ARG_MAXBFRAMES = 0; +static int ARG_PACKED = 0; +static int ARG_DEBUG = 0; +static int ARG_VOPDEBUG = 0; +static int ARG_GREYSCALE = 0; +static int ARG_GMC = 0; +static int ARG_INTERLACING = 0; +static int ARG_QPEL = 0; +static int ARG_CLOSED_GOP = 0; + +#ifndef READ_PNM #define IMAGE_SIZE(x,y) ((x)*(y)*3/2) +#else +#define IMAGE_SIZE(x,y) ((x)*(y)*3) +#endif #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 SMALL_EPS (1e-10) #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) @@ -116,7 +173,7 @@ * Nasty global vars ;-) ***************************************************************************/ -static int i,filenr = 0; +static int i; /* the path where to save output */ static char filepath[256] = "./"; @@ -135,221 +192,250 @@ static double msecond(); /* PGM related functions */ -static int read_pgmheader(FILE* handle); -static int read_pgmdata(FILE* handle, unsigned char *image); -static int read_yuvdata(FILE* handle, unsigned char *image); +#ifndef READ_PNM +static int read_pgmheader(FILE * handle); +static int read_pgmdata(FILE * handle, + unsigned char *image); +#else +static int read_pnmheader(FILE * handle); +static int read_pnmdata(FILE * handle, + unsigned char *image); +#endif +static int read_yuvdata(FILE * handle, + unsigned char *image); /* Encoder related functions */ static int enc_init(int use_assembler); static int enc_stop(); -static int enc_main(unsigned char* image, unsigned char* bitstream, - unsigned char *hints_buffer, - long *streamlength, long* frametype, long *hints_size); +static int enc_main(unsigned char *image, + unsigned char *bitstream, + int *key, + int *stats_type, + int *stats_quant, + int *stats_length, + int stats[3]); /***************************************************************************** * Main function ****************************************************************************/ -int main(int argc, char *argv[]) +int +main(int argc, + char *argv[]) { unsigned char *mp4_buffer = NULL; unsigned char *in_buffer = NULL; unsigned char *out_buffer = NULL; - unsigned char *hints_buffer = NULL; double enctime; - double totalenctime=0.; - - long totalsize; - long hints_size; - int status; - long frame_type; - long bigendian; - - long m4v_size; - int use_assembler=0; - + double totalenctime = 0.; + float totalPSNR[3] = {0., 0., 0.}; + + int totalsize; + int result; + int m4v_size; + int key; + int stats_type; + int stats_quant; + int stats_length; + int use_assembler = 1; // this default changed! + + int input_num; + int output_num; + char filename[256]; - + FILE *in_file = stdin; FILE *out_file = NULL; - FILE *hints_file = NULL; - printf("xvid_decraw - raw mpeg4 bitstream encoder "); - printf("written by Christoph Lampert 2002\n\n"); + printf("xvid_encraw - raw mpeg4 bitstream encoder "); + printf("written by Christoph Lampert 2002-2003\n\n"); + + /* Is there a dumb XviD coder ? */ + if(ME_ELEMENTS != VOP_ELEMENTS) { + fprintf(stderr, "Presets' arrays should have the same number of elements -- Please fill a bug to xvid-devel@xvid.org\n"); + return(-1); + } /***************************************************************************** * Command line parsing ****************************************************************************/ - for (i=1; i< argc; i++) { - - if (strcmp("-asm", argv[i]) == 0 ) { + for (i = 1; i < argc; i++) { + + if (strcmp("-asm", argv[i]) == 0) { use_assembler = 1; - } - else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { + } else if (strcmp("-noasm", argv[i]) == 0) { + use_assembler = 0; + } else if (strcmp("-w", argv[i]) == 0 && i < argc - 1) { i++; XDIM = atoi(argv[i]); - } - else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { + } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) { i++; YDIM = atoi(argv[i]); - } - else if (strcmp("-b", argv[i]) == 0 && i < argc - 1 ) { + } else if (strcmp("-bitrate", argv[i]) == 0 && i < argc - 1) { i++; ARG_BITRATE = atoi(argv[i]); - } - else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { + } else if (strcmp("-single", argv[i]) == 0) { + ARG_SINGLE = 1; + } else if (strcmp("-pass1", argv[i]) == 0 && i < argc - 1) { i++; - ARG_QUALITY = atoi(argv[i]); - } - else if (strcmp("-f", argv[i]) == 0 && i < argc - 1 ) { + ARG_PASS1 = argv[i]; + } else if (strcmp("-pass2", argv[i]) == 0 && i < argc - 1) { i++; - ARG_FRAMERATE = (float)atof(argv[i]); - } - else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { + ARG_PASS2 = argv[i]; + } else if (strcmp("-max_bframes", argv[i]) == 0 && i < argc - 1) { i++; - ARG_INPUTFILE = argv[i]; - } - else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { + ARG_MAXBFRAMES = atoi(argv[i]); + } else if (strcmp("-packed", argv[i]) == 0) { + ARG_PACKED = 1; + } else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) { i++; - ARG_INPUTTYPE = atoi(argv[i]); - } - else if(strcmp("-n", argv[i]) == 0 && i < argc - 1 ) { + ARG_BQRATIO = atoi(argv[i]); + } else if (strcmp("-bquant_offset", argv[i]) == 0 && i < argc - 1) { i++; - ARG_MAXFRAMENR = atoi(argv[i]); - } - else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1 ) { + ARG_BQOFFSET = atoi(argv[i]); + + } else if ((strcmp("-zq", argv[i]) == 0 || strcmp("-zw", argv[i]) == 0) && i < argc - 2) { + + if (NUM_ZONES >= MAX_ZONES) { + fprintf(stderr,"warning: too many zones; zone ignored\n"); + continue; + } + ZONES[NUM_ZONES].mode = strcmp("-zq", argv[i])==0 ? XVID_ZONE_QUANT : XVID_ZONE_WEIGHT; i++; - ARG_QUANTI = atoi(argv[i]); - } - else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { + ZONES[NUM_ZONES].frame = atoi(argv[i]); + i++; + ZONES[NUM_ZONES].increment = (int)(atof(argv[i]) * 100); + ZONES[NUM_ZONES].base = 100; + NUM_ZONES++; + + } else if (strcmp("-quality", argv[i]) == 0 && i < argc - 1) { i++; - ARG_SAVEMPEGSTREAM = atoi(argv[i]); - } - else if (strcmp("-mt", argv[i]) == 0 && i < argc - 1 ) { + ARG_QUALITY = atoi(argv[i]); + } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) { i++; - ARG_OUTPUTTYPE = atoi(argv[i]); - } - else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { + ARG_FRAMERATE = (float) atof(argv[i]); + } else if (strcmp("-max_key_interval", argv[i]) == 0 && i < argc - 1) { i++; - ARG_HINTMODE = atoi(argv[i]); - } - else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { + ARG_MAXKEYINTERVAL = atoi(argv[i]); + } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1) { + i++; + ARG_INPUTFILE = argv[i]; + } else if (strcmp("-stats", argv[i]) == 0) { + ARG_STATS = 1; + } else if (strcmp("-dump", argv[i]) == 0) { + ARG_DUMP = 1; + } else if (strcmp("-lumimasking", argv[i]) == 0) { + ARG_LUMIMASKING = 1; + } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) { + i++; + ARG_INPUTTYPE = atoi(argv[i]); + } else if (strcmp("-frames", argv[i]) == 0 && i < argc - 1) { + i++; + ARG_MAXFRAMENR = atoi(argv[i]); + } else if (strcmp("-save", argv[i]) == 0) { + ARG_SAVEMPEGSTREAM = 1; + ARG_SAVEINDIVIDUAL = 1; + } else if (strcmp("-debug", argv[i]) == 0) { + i++; + if (sscanf(argv[i],"0x%x", &ARG_DEBUG) || sscanf(argv[i],"%d", &ARG_DEBUG)) ; + } else if (strcmp("-o", argv[i]) == 0 && i < argc - 1) { + ARG_SAVEMPEGSTREAM = 1; i++; ARG_OUTPUTFILE = argv[i]; - } - else if (strcmp("-help", argv[i])) { + } else if (strcmp("-vop_debug", argv[i]) == 0) { + ARG_VOPDEBUG = 1; + } + else if (strcmp("-grey", argv[i]) == 0) { + ARG_GREYSCALE = 1; + } + else if (strcmp("-qpel", argv[i]) == 0) { + ARG_QPEL = 1; + } else if (strcmp("-gmc", argv[i]) == 0) { + ARG_GMC = 1; + } else if (strcmp("-interlaced", argv[i]) == 0) { + ARG_INTERLACING = 1; + } else if (strcmp("-closed_gop", argv[i]) == 0) { + ARG_CLOSED_GOP = 1; + } else if (strcmp("-help", argv[i])) { usage(); - return(0); - } - else { + return (0); + } else { usage(); exit(-1); } - + } - + /***************************************************************************** * Arguments checking ****************************************************************************/ - 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 */ - } - - if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { - fprintf(stderr,"Wrong Quality\n"); - return -1; + if (XDIM <= 0 || XDIM >= 4096 || YDIM <= 0 || YDIM >= 4096) { + fprintf(stderr, + "Trying to retreive width and height from PGM header\n"); + ARG_INPUTTYPE = 1; /* pgm */ } - if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { - fprintf(stderr,"Wrong Bitrate\n"); - return -1; + if (ARG_QUALITY < 0 ) { + ARG_QUALITY = 0; + } else if (ARG_QUALITY >= ME_ELEMENTS) { + ARG_QUALITY = ME_ELEMENTS - 1; } - if ( ARG_FRAMERATE <= 0) { - fprintf(stderr,"Wrong Framerate %s \n",argv[5]); - return -1; + if (ARG_FRAMERATE <= 0) { + fprintf(stderr, "Wrong Framerate %s \n", argv[5]); + return (-1); } - if ( ARG_MAXFRAMENR <= 0) { - fprintf(stderr,"Wrong number of frames\n"); - 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_MAXFRAMENR <= 0) { + fprintf(stderr, "Wrong number of frames\n"); + return (-1); } - if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { + if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { in_file = stdin; - } - else { + } else { in_file = fopen(ARG_INPUTFILE, "rb"); if (in_file == NULL) { fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); - return -1; + return (-1); } } if (ARG_INPUTTYPE) { +#ifndef READ_PNM if (read_pgmheader(in_file)) { - fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); - return -1; +#else + if (read_pnmheader(in_file)) { +#endif + fprintf(stderr, + "Wrong input format, I want YUV encapsulated in PGM\n"); + return (-1); } } /* now we know the sizes, so allocate memory */ - - in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); + in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM)); if (!in_buffer) goto free_all_memory; /* this should really be enough memory ! */ - mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2); + mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM) * 2); if (!mp4_buffer) - goto free_all_memory; + goto free_all_memory; /***************************************************************************** * XviD PART Start ****************************************************************************/ - status = enc_init(use_assembler); - if (status) - { - fprintf(stderr, "Encore INIT problem, return value %d\n", status); + result = enc_init(use_assembler); + if (result) { + fprintf(stderr, "Encore INIT problem, return value %d\n", result); goto release_all; } @@ -357,37 +443,14 @@ * Main loop ****************************************************************************/ - totalsize = LONG_PACK('M','P','4','U'); - if(*((char *)(&totalsize)) == 'M') - bigendian = 1; - else - bigendian = 0; - - if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { + if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) { - if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) - ARG_OUTPUTFILE = "stream.mp4u"; - else if(ARG_OUTPUTFILE == NULL && !ARG_OUTPUTTYPE) - ARG_OUTPUTFILE = "stream.m4v"; - - if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { + if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); goto release_all; } - /* Write header */ - if (ARG_OUTPUTTYPE) { - - long test = LONG_PACK('M','P','4','U'); - - test = (!bigendian)?SWAP(test):test; - - fwrite(&test, sizeof(test), 1, out_file); - - } - - } - else { + } else { out_file = NULL; } @@ -397,27 +460,32 @@ totalsize = 0; + result = 0; + + input_num = 0; /* input frame counter */ + output_num = 0; /* output frame counter */ + do { - if (ARG_INPUTTYPE) - 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) */ - - if (status) - { - /* Couldn't read image, most likely end-of-file */ - continue; - } + char *type; + int sse[3]; -/***************************************************************************** - * Read hints from file - ****************************************************************************/ + if (input_num >= ARG_MAXFRAMENR) { + result = 1; + } - 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); + if (!result) { + if (ARG_INPUTTYPE) { + /* read PGM data (YUV-format) */ +#ifndef READ_PNM + result = read_pgmdata(in_file, in_buffer); +#else + result = read_pnmdata(in_file, in_buffer); +#endif + } else { + /* read raw data (YUV-format) */ + result = read_yuvdata(in_file, in_buffer); + } } /***************************************************************************** @@ -425,102 +493,150 @@ ****************************************************************************/ enctime = msecond(); - status = enc_main(in_buffer, mp4_buffer, hints_buffer, - &m4v_size, &frame_type, &hints_size); + m4v_size = + enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type, + &stats_quant, &stats_length, sse); enctime = msecond() - enctime; - totalenctime += enctime; - totalsize += m4v_size; + /* Write the Frame statistics */ - printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", - (int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); + printf("%5d: key=%i, time= %6.0f, len= %7d", !result ? input_num : -1, + key, (float) enctime, (int) m4v_size); -/***************************************************************************** - * Save hints to file - ****************************************************************************/ + if (stats_type > 0) { /* !XVID_TYPE_NOTHING */ + + switch (stats_type) { + case XVID_TYPE_IVOP: + type = "I"; + break; + case XVID_TYPE_PVOP: + type = "P"; + break; + case XVID_TYPE_BVOP: + type = "B"; + break; + case XVID_TYPE_SVOP: + type = "S"; + break; + default: + type = "U"; + break; + } + + printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant, + stats_length); + +#define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height))))) + + if (ARG_STATS) { + printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", + SSE2PSNR(sse[0], XDIM, YDIM), SSE2PSNR(sse[1], XDIM / 2, + YDIM / 2), + SSE2PSNR(sse[2], XDIM / 2, YDIM / 2)); + + totalPSNR[0] += SSE2PSNR(sse[0], XDIM, YDIM); + totalPSNR[1] += SSE2PSNR(sse[1], XDIM/2, YDIM/2); + totalPSNR[2] += SSE2PSNR(sse[2], XDIM/2, YDIM/2); + } + + } +#undef SSE2PSNR + + printf("\n"); - 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); + if (m4v_size < 0) { + break; } + /* Update encoding time stats */ + totalenctime += enctime; + totalsize += m4v_size; + /***************************************************************************** * Save stream to file ****************************************************************************/ - if (ARG_SAVEMPEGSTREAM) - { + if (m4v_size > 0 && ARG_SAVEMPEGSTREAM) { + /* Save single files */ - if (out_file == NULL) { - sprintf(filename, "%sframe%05d.m4v", filepath, filenr); - out_file = fopen(filename, "wb"); - fwrite(mp4_buffer, m4v_size, 1, out_file); - fclose(out_file); - out_file = NULL; + if (ARG_SAVEINDIVIDUAL) { + FILE *out; + sprintf(filename, "%sframe%05d.m4v", filepath, output_num); + out = fopen(filename, "w+b"); + fwrite(mp4_buffer, m4v_size, 1, out); + fclose(out); + output_num++; } - else { - /* Using mp4u container */ - if (ARG_OUTPUTTYPE) { - long size = m4v_size; - size = (!bigendian)?SWAP(size):size; - fwrite(&size, sizeof(size), 1, out_file); - } - - /* Write mp4 data */ - fwrite(mp4_buffer, m4v_size, 1, out_file); - } + /* Save ES stream */ + if (ARG_OUTPUTFILE && out_file) + fwrite(mp4_buffer, 1, m4v_size, out_file); } - /* Read the header if it's pgm stream */ - if (ARG_INPUTTYPE) - status = read_pgmheader(in_file); + input_num++; + + /* Read the header if it's pgm stream */ + if (!result && ARG_INPUTTYPE) +#ifndef READ_PNM + result = read_pgmheader(in_file); +#else + result = read_pnmheader(in_file); +#endif + } while (1); - filenr++; - } while ( (!status) && (filenr 0) { + totalsize /= input_num; + totalenctime /= input_num; + totalPSNR[0] /= input_num; + totalPSNR[1] /= input_num; + totalPSNR[2] /= input_num; + } else { + totalsize = -1; + totalenctime = -1; + } + + printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d", + totalenctime, 1000 / totalenctime, (int) totalsize); + if (ARG_STATS) { + printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", + totalPSNR[0],totalPSNR[1],totalPSNR[2]); + } + printf("\n"); - printf("Avg: enctime %5.2f ms, %5.2f fps, filesize %7d bytes\n", - totalenctime, 1000/totalenctime, (int)totalsize); /***************************************************************************** * XviD PART Stop ****************************************************************************/ - release_all: + release_all: - if (enc_handle) - { - status = enc_stop(); - if (status) - fprintf(stderr, "Encore RELEASE problem return value %d\n", status); + if (enc_handle) { + result = enc_stop(); + if (result) + fprintf(stderr, "Encore RELEASE problem return value %d\n", + result); } - if(in_file) + if (in_file) fclose(in_file); - if(out_file) + if (out_file) fclose(out_file); - if(hints_file) - fclose(hints_file); - free_all_memory: + free_all_memory: free(out_buffer); free(mp4_buffer); free(in_buffer); - if(hints_buffer) free(hints_buffer); - return 0; + return (0); } @@ -534,16 +650,19 @@ *****************************************************************************/ /* Return time elapsed time in miliseconds since the program started */ -static double msecond() -{ -#ifndef _MSC_VER - struct timeval tv; +static double +msecond() +{ +#ifndef WIN32 + struct timeval tv; + gettimeofday(&tv, 0); - return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; + return (tv.tv_sec * 1.0e3 + tv.tv_usec * 1.0e-3); #else clock_t clk; + clk = clock(); - return clk * 1000 / CLOCKS_PER_SEC; + return (clk * 1000 / CLOCKS_PER_SEC); #endif } @@ -551,32 +670,55 @@ * Usage message *****************************************************************************/ -static void usage() +static void +usage() { - - fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n"); - fprintf(stderr, "Options :\n"); - fprintf(stderr, " -w integer : frame width ([1.2048])\n"); - fprintf(stderr, " -h integer : frame height ([1.2048])\n"); - fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); - fprintf(stderr, " -f float : target framerate (>0)\n"); - fprintf(stderr, " -i string : input filename (default=stdin)\n"); - fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); - fprintf(stderr, " -n integer : number of frames to encode\n"); - 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, " -o string : output container filename (only usefull when -m 1 is used) :\n"); - fprintf(stderr, " When this option is not used : one file per encoded frame\n"); - fprintf(stderr, " When this option is used :\n"); - fprintf(stderr, " + stream.m4v with -mt 0\n"); - fprintf(stderr, " + stream.mp4u with -mt 1\n"); - fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); - fprintf(stderr, " -mv integer : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n"); - fprintf(stderr, " -help : prints this help message\n"); - fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); - fprintf(stderr, " (* means default)\n"); - + fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n\n"); + fprintf(stderr, "Input options:\n"); + fprintf(stderr, " -i string : input filename (default=stdin)\n"); + fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1)\n"); + fprintf(stderr, " -w integer: frame width ([1.2048])\n"); + fprintf(stderr, " -h integer: frame height ([1.2048])\n"); + fprintf(stderr, " -frames integer: number of frames to encode\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Output options:\n"); + fprintf(stderr, " -dump : save decoder output\n"); + fprintf(stderr, " -save : save an Elementary Stream file per frame\n"); + fprintf(stderr, " -o string: save an Elementary Stream for the complete sequence\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "BFrames options:\n"); + fprintf(stderr, " -max_bframes integer: max bframes (default=0)\n"); + fprintf(stderr, " -bquant_ratio integer: bframe quantizer ratio (default=150)\n"); + fprintf(stderr, " -bquant_offset integer: bframe quantizer offset (default=100)\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Rate control options:\n"); + fprintf(stderr, " -framerate float : target framerate (>0 | default=25.0)\n"); + fprintf(stderr, " -bitrate integer : target bitrate\n"); + fprintf(stderr, " -single : single pass mode\n"); + fprintf(stderr, " -pass1 filename : twopass mode (first pass)\n"); + fprintf(stderr, " -pass2 filename : twopass mode (2nd pass)\n"); + fprintf(stderr, " -zq starting_frame float : bitrate zone; quant\n"); + fprintf(stderr, " -zw starting_frame float : bitrate zone; weight\n"); + fprintf(stderr, " -max_key_interval integer : maximum keyframe interval\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Other options\n"); + fprintf(stderr, " -asm : use assembly optmized code\n"); + fprintf(stderr, " -noasm : do not use assembly optmized code\n"); + fprintf(stderr, " -quality integer: quality ([0..%d])\n", ME_ELEMENTS - 1); + fprintf(stderr, " -qpel : use quarter pixel ME\n"); + fprintf(stderr, " -gmc : use global motion compensation\n"); + fprintf(stderr, " -interlaced : use interlaced encoding (this is NOT a deinterlacer!)\n"); + fprintf(stderr, " -packed : packed mode\n"); + fprintf(stderr, " -closed_gop : closed GOP mode\n"); + fprintf(stderr, " -grey : grey scale coding (chroma is discarded)\n"); + fprintf(stderr, " -lumimasking : use lumimasking algorithm\n"); + fprintf(stderr, " -stats : print stats about encoded frames\n"); + fprintf(stderr, " -debug : activates xvidcore internal debugging output\n"); + fprintf(stderr, " -vop_debug : print some info directly into encoded frames\n"); + fprintf(stderr, " -help : prints this help message\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "NB: You can define %d zones repeating the -z[qw] option as many times as needed.\n", MAX_ZONES); + fprintf(stderr, "\n"); } /***************************************************************************** @@ -587,183 +729,434 @@ * *****************************************************************************/ -static int read_pgmheader(FILE* handle) -{ - int bytes,xsize,ysize,depth; +#ifndef READ_PNM +static int +read_pgmheader(FILE * handle) +{ + int bytes, xsize, ysize, depth; char dummy[2]; - - bytes = fread(dummy,1,2,handle); - if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) - return 1; + bytes = fread(dummy, 1, 2, handle); + + 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) ) - { - fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); - return 2; + fscanf(handle, "%d %d %d", &xsize, &ysize, &depth); + if ((xsize > 4096) || (ysize > 4096*3/2) || (depth != 255)) { + fprintf(stderr, "%d %d %d\n", xsize, ysize, depth); + return (2); } - if ( (XDIM==0) || (YDIM==0) ) - { - XDIM=xsize; - YDIM=ysize*2/3; + if ((XDIM == 0) || (YDIM == 0)) { + XDIM = xsize; + YDIM = ysize * 2 / 3; } - return 0; + return (0); } -static int read_pgmdata(FILE* handle, unsigned char *image) -{ +static int +read_pgmdata(FILE * handle, + unsigned char *image) +{ int i; char dummy; - + unsigned char *y = image; - unsigned char *u = image + XDIM*YDIM; - unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; + unsigned char *u = image + XDIM * YDIM; + unsigned char *v = image + XDIM * YDIM + XDIM / 2 * YDIM / 2; /* read Y component of picture */ - fread(y, 1, XDIM*YDIM, handle); - - for (i=0;i 1440) || (ysize > 2880) || (depth != 255)) { + fprintf(stderr, "%d %d %d\n", xsize, ysize, depth); + return (2); + } + + XDIM = xsize; + YDIM = ysize; + + return (0); +} + +static int +read_pnmdata(FILE * handle, + unsigned char *image) { - - if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) - return 1; - else - return 0; + int i; + char dummy; + + /* read Y component of picture */ + fread(image, 1, XDIM * YDIM * 3, handle); + + /* I don't know why, but this seems needed */ + fread(&dummy, 1, 1, handle); + + return (0); +} +#endif + +static int +read_yuvdata(FILE * handle, + unsigned char *image) +{ + + if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != + (unsigned int) IMAGE_SIZE(XDIM, YDIM)) + return (1); + else + return (0); } /***************************************************************************** * Routines for encoding: init encoder, frame step, release encoder ****************************************************************************/ +/* sample plugin */ + +int +rawenc_debug(void *handle, + int opt, + void *param1, + void *param2) +{ + switch (opt) { + case XVID_PLG_INFO: + { + xvid_plg_info_t *info = (xvid_plg_info_t *) param1; + + info->flags = XVID_REQDQUANTS; + return 0; + } + + case XVID_PLG_CREATE: + case XVID_PLG_DESTROY: + case XVID_PLG_BEFORE: + return 0; + + case XVID_PLG_AFTER: + { + xvid_plg_data_t *data = (xvid_plg_data_t *) param1; + int i, j; + + printf("---[ frame: %5i quant: %2i length: %6i ]---\n", + data->frame_num, data->quant, data->length); + for (j = 0; j < data->mb_height; j++) { + for (i = 0; i < data->mb_width; i++) + printf("%2i ", data->dquant[j * data->dquant_stride + i]); + printf("\n"); + } + + return 0; + } + } + + return XVID_ERR_FAIL; +} + + #define FRAMERATE_INCR 1001 + /* Initialize encoder for first use, pass all needed parameters to the codec */ -static int enc_init(int use_assembler) +static int +enc_init(int use_assembler) { int xerr; - - XVID_INIT_PARAM xinit; - XVID_ENC_PARAM xparam; + //xvid_plugin_cbr_t cbr; + xvid_plugin_single_t single; + xvid_plugin_2pass1_t rc2pass1; + xvid_plugin_2pass2_t rc2pass2; + //xvid_plugin_fixed_t rcfixed; + xvid_enc_plugin_t plugins[7]; + xvid_gbl_init_t xvid_gbl_init; + xvid_enc_create_t xvid_enc_create; + + /*------------------------------------------------------------------------ + * XviD core initialization + *----------------------------------------------------------------------*/ + + /* Set version -- version checking will done by xvidcore */ + memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); + xvid_gbl_init.version = XVID_VERSION; + xvid_gbl_init.debug = ARG_DEBUG; + - if(use_assembler) { + /* Do we have to enable ASM optimizations ? */ + if (use_assembler) { -#ifdef ARCH_IA64 - xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; +#ifdef ARCH_IS_IA64 + xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ASM; #else - xinit.cpu_flags = 0; + xvid_gbl_init.cpu_flags = 0; #endif + } else { + xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; } - else { - xinit.cpu_flags = XVID_CPU_FORCE; + + /* Initialize XviD core -- Should be done once per __process__ */ + xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); + + /*------------------------------------------------------------------------ + * XviD encoder initialization + *----------------------------------------------------------------------*/ + + /* Version again */ + memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); + xvid_enc_create.version = XVID_VERSION; + + /* Width and Height of input frames */ + xvid_enc_create.width = XDIM; + xvid_enc_create.height = YDIM; + xvid_enc_create.profile = XVID_PROFILE_AS_L4; + + /* init plugins */ + xvid_enc_create.zones = ZONES; + xvid_enc_create.num_zones = NUM_ZONES; + + xvid_enc_create.plugins = plugins; + xvid_enc_create.num_plugins = 0; + + if (ARG_SINGLE) { + memset(&single, 0, sizeof(xvid_plugin_single_t)); + single.version = XVID_VERSION; + single.bitrate = ARG_BITRATE; + + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single; + plugins[xvid_enc_create.num_plugins].param = &single; + xvid_enc_create.num_plugins++; } - xvid_init(NULL, 0, &xinit, NULL); + if (ARG_PASS2) { + memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t)); + rc2pass2.version = XVID_VERSION; + rc2pass2.filename = ARG_PASS2; + rc2pass2.bitrate = ARG_BITRATE; - xparam.width = XDIM; - xparam.height = YDIM; - if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) - { - xparam.fincr = 1; - xparam.fbase = (int)ARG_FRAMERATE; +/* An example of activating VBV could look like this + rc2pass2.vbv_size = 3145728; + rc2pass2.vbv_initial = 2359296; + rc2pass2.vbv_maxrate = 4000000; + rc2pass2.vbv_peakrate = 10000000; +*/ + + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; + plugins[xvid_enc_create.num_plugins].param = &rc2pass2; + xvid_enc_create.num_plugins++; } - else - { - xparam.fincr = FRAMERATE_INCR; - xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); - } - xparam.rc_reaction_delay_factor = 16; - xparam.rc_averaging_period = 100; - xparam.rc_buffer = 10; - xparam.rc_bitrate = ARG_BITRATE*1000; - xparam.min_quantizer = ARG_MINQUANT; - xparam.max_quantizer = ARG_MAXQUANT; - xparam.max_key_interval = (int)ARG_FRAMERATE*10; - /* I use a small value here, since will not encode whole movies, but short clips */ + if (ARG_PASS1) { + memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t)); + rc2pass1.version = XVID_VERSION; + rc2pass1.filename = ARG_PASS1; - xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL); - enc_handle=xparam.handle; + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1; + plugins[xvid_enc_create.num_plugins].param = &rc2pass1; + xvid_enc_create.num_plugins++; + } - return xerr; -} + if (ARG_LUMIMASKING) { + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; + plugins[xvid_enc_create.num_plugins].param = NULL; + xvid_enc_create.num_plugins++; + } -static int enc_stop() -{ - int xerr; + if (ARG_DUMP) { + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump; + plugins[xvid_enc_create.num_plugins].param = NULL; + xvid_enc_create.num_plugins++; + } - xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); - return xerr; +#if 0 + if (ARG_DEBUG) { + plugins[xvid_enc_create.num_plugins].func = rawenc_debug; + plugins[xvid_enc_create.num_plugins].param = NULL; + xvid_enc_create.num_plugins++; + } +#endif -} + /* No fancy thread tests */ + xvid_enc_create.num_threads = 0; -static int enc_main(unsigned char* image, unsigned char* bitstream, - unsigned char* hints_buffer, - long *streamlength, long *frametype, long *hints_size) -{ - int xerr; + /* Frame rate - Do some quick float fps = fincr/fbase hack */ + if ((ARG_FRAMERATE - (int) ARG_FRAMERATE) < SMALL_EPS) { + xvid_enc_create.fincr = 1; + xvid_enc_create.fbase = (int) ARG_FRAMERATE; + } else { + xvid_enc_create.fincr = FRAMERATE_INCR; + xvid_enc_create.fbase = (int) (FRAMERATE_INCR * ARG_FRAMERATE); + } + + /* Maximum key frame interval */ + if (ARG_MAXKEYINTERVAL > 0) { + xvid_enc_create.max_key_interval = ARG_MAXKEYINTERVAL; + }else { + xvid_enc_create.max_key_interval = (int) ARG_FRAMERATE *10; + } + + /* Bframes settings */ + xvid_enc_create.max_bframes = ARG_MAXBFRAMES; + xvid_enc_create.bquant_ratio = ARG_BQRATIO; + xvid_enc_create.bquant_offset = ARG_BQOFFSET; + + /* Dropping ratio frame -- we don't need that */ + xvid_enc_create.frame_drop_ratio = 0; + + /* Global encoder options */ + xvid_enc_create.global = 0; - XVID_ENC_FRAME xframe; - XVID_ENC_STATS xstats; + if (ARG_PACKED) + xvid_enc_create.global |= XVID_GLOBAL_PACKED; - xframe.bitstream = bitstream; - xframe.length = -1; /* this is written by the routine */ + if (ARG_CLOSED_GOP) + xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP; - xframe.image = image; - xframe.colorspace = XVID_CSP_YV12; /* defined in */ + if (ARG_STATS) + xvid_enc_create.global |= XVID_GLOBAL_EXTRASTATS_ENABLE; - xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */ + /* I use a small value here, since will not encode whole movies, but short clips */ + xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); - xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */ + /* Retrieve the encoder instance from the structure */ + enc_handle = xvid_enc_create.handle; - xframe.motion = motion_presets[ARG_QUALITY]; - xframe.general = general_presets[ARG_QUALITY]; - xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; + return (xerr); +} - xframe.hint.hintstream = hints_buffer; +static int +enc_stop() +{ + int xerr; - if(ARG_HINTMODE == HINT_MODE_SET) { - xframe.hint.hintlength = *hints_size; - xframe.hint.rawhints = 0; - xframe.general |= XVID_HINTEDME_SET; - } + /* Destroy the encoder instance */ + xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); - if(ARG_HINTMODE == HINT_MODE_GET) { - xframe.hint.rawhints = 0; - xframe.general |= XVID_HINTEDME_GET; - } + return (xerr); +} + +static int +enc_main(unsigned char *image, + unsigned char *bitstream, + int *key, + int *stats_type, + int *stats_quant, + int *stats_length, + int sse[3]) +{ + int ret; - xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); + xvid_enc_frame_t xvid_enc_frame; + xvid_enc_stats_t xvid_enc_stats; - if(ARG_HINTMODE == HINT_MODE_GET) - *hints_size = xframe.hint.hintlength; + /* Version for the frame and the stats */ + memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); + xvid_enc_frame.version = XVID_VERSION; + + memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); + xvid_enc_stats.version = XVID_VERSION; + + /* Bind output buffer */ + xvid_enc_frame.bitstream = bitstream; + xvid_enc_frame.length = -1; + + /* Initialize input image fields */ + if (image) { + xvid_enc_frame.input.plane[0] = image; +#ifndef READ_PNM + xvid_enc_frame.input.csp = XVID_CSP_I420; + xvid_enc_frame.input.stride[0] = XDIM; +#else + xvid_enc_frame.input.csp = XVID_CSP_BGR; + xvid_enc_frame.input.stride[0] = XDIM*3; +#endif + } else { + xvid_enc_frame.input.csp = XVID_CSP_NULL; + } - /* - * 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 - */ - *frametype = xframe.intra; - *streamlength = xframe.length; + /* Set up core's general features */ + xvid_enc_frame.vol_flags = 0; + if (ARG_STATS) + xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS; + if (ARG_QPEL) + xvid_enc_frame.vol_flags |= XVID_VOL_QUARTERPEL; + if (ARG_GMC) + xvid_enc_frame.vol_flags |= XVID_VOL_GMC; + if (ARG_INTERLACING) + xvid_enc_frame.vol_flags |= XVID_VOL_INTERLACING; + + /* Set up core's general features */ + xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; + + if (ARG_VOPDEBUG) { + xvid_enc_frame.vop_flags |= XVID_VOP_DEBUG; + } + + if (ARG_GREYSCALE) { + xvid_enc_frame.vop_flags |= XVID_VOP_GREYSCALE; + } + + /* Frame type -- let core decide for us */ + xvid_enc_frame.type = XVID_TYPE_AUTO; + + /* Force the right quantizer -- It is internally managed by RC plugins */ + xvid_enc_frame.quant = 0; + + /* Set up motion estimation flags */ + xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; + + if (ARG_GMC) + xvid_enc_frame.motion |= XVID_ME_GME_REFINE; + + if (ARG_QPEL) + xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16; + if (ARG_QPEL && (xvid_enc_frame.vop_flags & XVID_VOP_INTER4V)) + xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8; + + /* We don't use special matrices */ + xvid_enc_frame.quant_intra_matrix = NULL; + xvid_enc_frame.quant_inter_matrix = NULL; + + /* Encode the frame */ + ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, + &xvid_enc_stats); + + *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME); + *stats_type = xvid_enc_stats.type; + *stats_quant = xvid_enc_stats.quant; + *stats_length = xvid_enc_stats.length; + sse[0] = xvid_enc_stats.sse_y; + sse[1] = xvid_enc_stats.sse_u; + sse[2] = xvid_enc_stats.sse_v; - return xerr; + return (ret); }