--- branches/dev-api-4/xvidcore/examples/xvid_encraw.c 2003/03/26 14:56:49 949 +++ branches/dev-api-4/xvidcore/examples/xvid_encraw.c 2003/08/04 17:22:36 1110 @@ -3,7 +3,9 @@ * XVID MPEG-4 VIDEO CODEC * - Console based test application - * - * Copyright(C) 2002-2003 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,7 +21,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_encraw.c,v 1.11.2.14 2003-03-26 14:56:09 edgomez Exp $ + * $Id: xvid_encraw.c,v 1.11.2.31 2003-08-04 17:22:36 chl Exp $ * ****************************************************************************/ @@ -31,6 +33,8 @@ * * The program is plain C and needs no libraries except for libxvidcore, * and maths-lib. + * + * Use ./xvid_encraw -help for a list of options * ************************************************************************/ @@ -46,45 +50,80 @@ #include "xvid.h" +#undef READ_PNM /***************************************************************************** * Quality presets ****************************************************************************/ -static xvid_motion_t const motion_presets[] = { - 0, - XVID_ME_HALFPELREFINE16, - XVID_ME_HALFPELREFINE16, - XVID_ME_HALFPELREFINE16 | XVID_ME_HALFPELREFINE8, - XVID_ME_HALFPELREFINE16 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH16 | - XVID_ME_USESQUARES16, - XVID_ME_HALFPELREFINE16 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH16 | - XVID_ME_USESQUARES16 | XVID_ME_CHROMA16 | XVID_ME_CHROMA8, +static const int motion_presets[] = { + /* quality 0 */ + XVID_ME_GME_REFINE, + + /* quality 1 */ + XVID_ME_GME_REFINE | XVID_ME_ADVANCEDDIAMOND16, + + /* quality 2 */ + XVID_ME_GME_REFINE | XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16, + + /* quality 3 */ + XVID_ME_GME_REFINE | XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | + XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8, + + /* quality 4 */ + XVID_ME_GME_REFINE | XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | + XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | + XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, + + /* quality 5 */ + XVID_ME_GME_REFINE | XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | + XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | + XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, + + /* quality 6 */ + XVID_ME_GME_REFINE | 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 xvid_vol_t const vol_presets[] = { - XVID_VOL_MPEGQUANT, +static const int vop_presets[] = { + /* quality 0 */ 0, + + /* quality 1 */ 0, - XVID_VOL_QUARTERPEL, - XVID_VOL_QUARTERPEL | XVID_VOL_GMC, - 0 -}; -static xvid_vop_t const vop_presets[] = { - XVID_VOP_DYNAMIC_BFRAMES, - XVID_VOP_DYNAMIC_BFRAMES, - XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL, - XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | XVID_VOP_INTER4V, - XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | XVID_VOP_INTER4V | XVID_VOP_HQACPRED, - XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | XVID_VOP_HQACPRED | - XVID_VOP_MODEDECISION_BITS + /* 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, + }; +#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 @@ -92,12 +131,13 @@ 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 float ARG_QUANTI = 0.0f; -static int ARG_QUALITY = 5; +static int ARG_QUALITY = ME_ELEMENTS - 1; static float ARG_FRAMERATE = 25.00f; 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; @@ -109,8 +149,13 @@ static int ARG_MAXBFRAMES = 0; static int ARG_PACKED = 0; static int ARG_DEBUG = 0; +static int ARG_VOPDEBUG = 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) @@ -141,9 +186,15 @@ static double msecond(); /* PGM related functions */ +#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); @@ -173,6 +224,7 @@ double enctime; double totalenctime = 0.; + float totalPSNR[3] = {0., 0., 0.}; int totalsize; int result; @@ -194,6 +246,12 @@ 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 ****************************************************************************/ @@ -208,15 +266,15 @@ } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) { i++; YDIM = atoi(argv[i]); - } else if (strcmp("-bitrate", 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("-single", argv[i]) == 0) { + ARG_SINGLE = 1; } else if (strcmp("-pass1", argv[i]) == 0 && i < argc - 1) { i++; ARG_PASS1 = argv[i]; - } else if (strcmp("-pass2", argv[i]) == 0 && i < argc - 2) { - i++; - ARG_PASS1 = argv[i]; + } else if (strcmp("-pass2", argv[i]) == 0 && i < argc - 1) { i++; ARG_PASS2 = argv[i]; } else if (strcmp("-max_bframes", argv[i]) == 0 && i < argc - 1) { @@ -230,12 +288,30 @@ } else if (strcmp("-bquant_offset", argv[i]) == 0 && i < argc - 1) { i++; 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++; + 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_QUALITY = atoi(argv[i]); } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) { i++; ARG_FRAMERATE = (float) atof(argv[i]); + } else if (strcmp("-max_key_interval", argv[i]) == 0 && i < argc - 1) { + i++; + ARG_MAXKEYINTERVAL = atoi(argv[i]); } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1) { i++; ARG_INPUTFILE = argv[i]; @@ -251,16 +327,16 @@ } else if (strcmp("-nframes", argv[i]) == 0 && i < argc - 1) { i++; ARG_MAXFRAMENR = atoi(argv[i]); - } else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1) { - i++; - ARG_QUANTI = (float) atof(argv[i]); } else if (strcmp("-save", argv[i]) == 0) { ARG_SAVEMPEGSTREAM = 1; } else if (strcmp("-debug", argv[i]) == 0) { - ARG_DEBUG = 1; + 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) { i++; ARG_OUTPUTFILE = argv[i]; + } else if (strcmp("-vop_debug", argv[i]) == 0) { + ARG_VOPDEBUG = 1; } else if (strcmp("-help", argv[i])) { usage(); return (0); @@ -281,9 +357,10 @@ ARG_INPUTTYPE = 1; /* pgm */ } - if (ARG_QUALITY < 0 || ARG_QUALITY > 5) { - fprintf(stderr, "Wrong Quality\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) { @@ -308,7 +385,11 @@ } if (ARG_INPUTTYPE) { +#ifndef READ_PNM if (read_pgmheader(in_file)) { +#else + if (read_pnmheader(in_file)) { +#endif fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); return (-1); @@ -374,7 +455,11 @@ 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); @@ -393,7 +478,7 @@ /* Write the Frame statistics */ - printf("%5d: key=%i, time=%6.0f, length=%7d", !result ? input_num : -1, + printf("%5d: key=%i, time= %6.0f, len= %7d", !result ? input_num : -1, key, (float) enctime, (int) m4v_size); if (stats_type > 0) { /* !XVID_TYPE_NOTHING */ @@ -416,7 +501,7 @@ break; } - printf(" | type=%s, quant=%2d, length=%7d", type, stats_quant, + 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))))) @@ -426,6 +511,10 @@ 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); } } @@ -466,8 +555,11 @@ /* Read the header if it's pgm stream */ if (!result && ARG_INPUTTYPE) - result = read_pgmheader(in_file); - +#ifndef READ_PNM + result = read_pgmheader(in_file); +#else + result = read_pnmheader(in_file); +#endif } while (1); @@ -482,13 +574,21 @@ if (input_num > 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\n", + 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"); /***************************************************************************** @@ -571,19 +671,26 @@ fprintf(stderr, "\n"); fprintf(stderr, "Rate control options:\n"); fprintf(stderr, " -framerate float : target framerate (>0 | default=25.0)\n"); - fprintf(stderr, " -bitrate integer : bitrate -- for CBR/VBR pass2\n"); - fprintf(stderr, " -quant float : quantizer -- for \"Fixed\" quantizer RC\n"); - fprintf(stderr, " -pass1 filename : stats filename\n"); - fprintf(stderr, " -pass2 filename1 filename2 : first pass stats and scaled stats filename\n"); - fprintf(stderr, "\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, " -quality integer: quality ([0..5])\n"); + fprintf(stderr, " -quality integer: quality ([0..%d])\n", ME_ELEMENTS - 1); fprintf(stderr, " -packed : packed mode\n"); fprintf(stderr, " -lumimasking : use lumimasking algorithm\n"); fprintf(stderr, " -stats : print stats about encoded frames\n"); - fprintf(stderr, " -debug : print all MB dquants\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"); } /***************************************************************************** @@ -594,6 +701,7 @@ * *****************************************************************************/ +#ifndef READ_PNM static int read_pgmheader(FILE * handle) { @@ -649,6 +757,46 @@ return (0); } +#else +static int +read_pnmheader(FILE * handle) +{ + int bytes, xsize, ysize, depth; + char dummy[2]; + + bytes = fread(dummy, 1, 2, handle); + + if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '6')) + 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); + } + + XDIM = xsize; + YDIM = ysize; + + return (0); +} + +static int +read_pnmdata(FILE * handle, + unsigned char *image) +{ + 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, @@ -717,10 +865,11 @@ enc_init(int use_assembler) { int xerr; - xvid_plugin_cbr_t cbr; + //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_plugin_fixed_t rcfixed; xvid_enc_plugin_t plugins[7]; xvid_gbl_init_t xvid_gbl_init; xvid_enc_create_t xvid_enc_create; @@ -732,13 +881,14 @@ /* 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; /* Do we have to enable ASM optimizations ? */ if (use_assembler) { #ifdef ARCH_IS_IA64 - xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; + xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ASM; #else xvid_gbl_init.cpu_flags = 0; #endif @@ -762,43 +912,36 @@ xvid_enc_create.height = YDIM; /* 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_BITRATE) { - cbr.version = XVID_VERSION; - memset(&cbr, 0, sizeof(xvid_plugin_cbr_t)); - cbr.bitrate = ARG_BITRATE; + 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_cbr; - plugins[xvid_enc_create.num_plugins].param = &cbr; + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single; + plugins[xvid_enc_create.num_plugins].param = &single; xvid_enc_create.num_plugins++; } - if (ARG_QUANTI) { - rcfixed.version = XVID_VERSION; - /* We will use a 1/10 precision, just to make sure it works */ - rcfixed.quant_base = 10; - rcfixed.quant_increment = (int) (ARG_QUANTI * 10); - - plugins[xvid_enc_create.num_plugins].func = xvid_plugin_fixed; - plugins[xvid_enc_create.num_plugins].param = &rcfixed; - xvid_enc_create.num_plugins++; - } - - if (ARG_PASS1 && ARG_PASS2) { - rc2pass2.version = XVID_VERSION; + if (ARG_PASS2) { memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t)); - rc2pass2.filename1 = ARG_PASS1; - rc2pass2.filename2 = ARG_PASS2; + rc2pass2.version = XVID_VERSION; + rc2pass2.filename = ARG_PASS2; + rc2pass2.bitrate = ARG_BITRATE; plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; plugins[xvid_enc_create.num_plugins].param = &rc2pass2; xvid_enc_create.num_plugins++; - } else if (ARG_PASS1) { - rc2pass1.version = XVID_VERSION; + } + + if (ARG_PASS1) { memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t)); + rc2pass1.version = XVID_VERSION; rc2pass1.filename = ARG_PASS1; plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1; @@ -818,11 +961,13 @@ xvid_enc_create.num_plugins++; } +#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; @@ -837,7 +982,11 @@ } /* Maximum key frame interval */ - xvid_enc_create.max_key_interval = (int) ARG_FRAMERATE *10; + 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; @@ -904,19 +1053,27 @@ /* 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; } /* Set up core's general features */ - xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; + xvid_enc_frame.vol_flags = XVID_VOL_GMC; if (ARG_STATS) xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS; /* 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; + } /* Frame type -- let core decide for us */ xvid_enc_frame.type = XVID_TYPE_AUTO;