--- branches/dev-api-4/xvidcore/examples/xvid_encraw.c 2003/03/23 04:03:01 938 +++ branches/dev-api-4/xvidcore/examples/xvid_encraw.c 2003/05/12 00:06:49 1013 @@ -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_encraw.c,v 1.11.2.11 2003-03-23 04:01:32 suxen_drol Exp $ + * $Id: xvid_encraw.c,v 1.11.2.21 2003-05-12 00:06:49 edgomez Exp $ * ****************************************************************************/ @@ -50,33 +50,67 @@ /***************************************************************************** * Quality presets ****************************************************************************/ - static xvid_motion_t const motion_presets[] = { + /* quality 0 */ 0, - PMV_HALFPELREFINE16, - PMV_HALFPELREFINE16, - PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8, - PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16, - PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16 | PMV_CHROMA16 | PMV_CHROMA8, -}; -static xvid_vol_t const vol_presets[] = { - XVID_MPEGQUANT, - 0, - 0, - XVID_QUARTERPEL, - XVID_QUARTERPEL | XVID_GMC, - 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_CHROMA16 | XVID_ME_CHROMA8, + + /* quality 5 */ + XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | + XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | + XVID_ME_CHROMA16 | XVID_ME_CHROMA8, + + /* quality 6 */ + XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | + XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH8 | + XVID_ME_CHROMA16 | XVID_ME_CHROMA8 , + }; +#define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0])) static xvid_vop_t const vop_presets[] = { - XVID_DYNAMIC_BFRAMES, - XVID_DYNAMIC_BFRAMES, - XVID_DYNAMIC_BFRAMES | XVID_HALFPEL, - XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V, - XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED, - XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_HQACPRED | XVID_MODEDECISION_BITS + /* quality 0 */ + 0, + + /* quality 1 */ + XVID_VOP_DYNAMIC_BFRAMES, + + /* quality 2 */ + XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL, + + /* quality 3 */ + XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | + XVID_VOP_INTER4V, + + /* quality 4 */ + XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | + XVID_VOP_INTER4V, + + /* quality 5 */ + XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | + XVID_VOP_INTER4V | XVID_VOP_TRELLISQUANT, + + /* quality 6 */ + XVID_VOP_DYNAMIC_BFRAMES | 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 @@ -89,9 +123,11 @@ static int ARG_DUMP = 0; static int ARG_LUMIMASKING = 0; static int ARG_BITRATE = 0; -static char * ARG_PASS1 = 0; -static int ARG_QUANTI = 0; -static int ARG_QUALITY = 5; +static char *ARG_PASS1 = 0; +static char *ARG_PASS2 = 0; +static int ARG_PASS2_BITRATE = 0; +static float ARG_QUANTI = 0.0f; +static int ARG_QUALITY = 3; static float ARG_FRAMERATE = 25.00f; static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; static char *ARG_INPUTFILE = NULL; @@ -169,6 +205,7 @@ double enctime; double totalenctime = 0.; + float totalPSNR[3] = {0., 0., 0.}; int totalsize; int result; @@ -190,6 +227,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 ****************************************************************************/ @@ -210,6 +253,11 @@ } 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_PASS2 = argv[i]; + i++; + ARG_PASS2_BITRATE = atoi(argv[i]); } else if (strcmp("-max_bframes", argv[i]) == 0 && i < argc - 1) { i++; ARG_MAXBFRAMES = atoi(argv[i]); @@ -244,7 +292,7 @@ ARG_MAXFRAMENR = atoi(argv[i]); } else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1) { i++; - ARG_QUANTI = atoi(argv[i]); + ARG_QUANTI = (float) atof(argv[i]); } else if (strcmp("-save", argv[i]) == 0) { ARG_SAVEMPEGSTREAM = 1; } else if (strcmp("-debug", argv[i]) == 0) { @@ -272,9 +320,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) { @@ -384,11 +433,8 @@ /* Write the Frame statistics */ - printf("%5d: key=%i, time=%6.0f, length=%7d", - !result ? input_num : -1, - key, - (float) enctime, - (int) m4v_size); + printf("%5d: key=%i, time= %6.0f, length= %7d", !result ? input_num : -1, + key, (float) enctime, (int) m4v_size); if (stats_type > 0) { /* !XVID_TYPE_NOTHING */ @@ -410,20 +456,23 @@ break; } - printf(" | type=%s, quant=%2d, length=%7d", type, stats_quant, + printf(" | type=%s, quant= %2d, length= %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)); + 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"); @@ -477,13 +526,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"); /***************************************************************************** @@ -557,24 +614,27 @@ fprintf(stderr, "Output options:\n"); fprintf(stderr, " -dump : save decoder output\n"); fprintf(stderr, " -save : save mpeg4 raw stream\n"); - fprintf(stderr, " -o string: output filename\n"); + fprintf(stderr, " -o string: output filename\n"); fprintf(stderr, "\n"); - fprintf(stderr, "Bitrate options:\n"); - fprintf(stderr, " -bitrate integer: target bitrate (>0 | default=900kbit)\n"); - fprintf(stderr, " -quant integer: fixed quantizer (disables -b setting)\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, " -framerate float : target framerate (>0)\n"); - fprintf(stderr, " -pass1 string: fisrt pass stats file\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 : bitrate -- for CBR/VBR pass2\n"); + fprintf(stderr, " -quant float : quantizer -- for \"Fixed\" quantizer RC\n"); + fprintf(stderr, " -pass1 filename : output stats filename\n"); + fprintf(stderr, " -pass2 filename bitrate : input stats filename, target bitrate\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 quantizers\n"); + fprintf(stderr, " -debug : print all MB dquants\n"); fprintf(stderr, " -help : prints this help message\n"); } @@ -709,9 +769,11 @@ enc_init(int use_assembler) { int xerr; - xvid_plugin_cbr_t cbr; - xvid_plugin_2pass1_t rc2pass1; - xvid_enc_plugin_t plugins[5]; + xvid_plugin_cbr_t cbr; + 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; @@ -756,25 +818,48 @@ 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_BITRATE) { + memset(&cbr, 0, sizeof(xvid_plugin_cbr_t)); + cbr.version = XVID_VERSION; + cbr.bitrate = ARG_BITRATE; - plugins[xvid_enc_create.num_plugins].func = xvid_plugin_cbr; + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_cbr; plugins[xvid_enc_create.num_plugins].param = &cbr; xvid_enc_create.num_plugins++; - } + } + + if (ARG_QUANTI) { + memset(&rcfixed, 0, sizeof(xvid_plugin_fixed_t)); + 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_PASS2) { + memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t)); + rc2pass2.version = XVID_VERSION; + rc2pass2.filename = ARG_PASS2; + rc2pass2.bitrate = ARG_PASS2_BITRATE; + + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; + plugins[xvid_enc_create.num_plugins].param = &rc2pass2; + xvid_enc_create.num_plugins++; + } - if (ARG_PASS1) { - rc2pass1.version = XVID_VERSION; - memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t)); - rc2pass1.filename = ARG_PASS1; + 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; + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1; plugins[xvid_enc_create.num_plugins].param = &rc2pass1; xvid_enc_create.num_plugins++; - } + } if (ARG_LUMIMASKING) { plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; @@ -819,8 +904,12 @@ /* Global encoder options */ xvid_enc_create.global = 0; - if (ARG_PACKED) xvid_enc_create.global |=XVID_PACKED; - if (ARG_STATS) xvid_enc_create.global |=XVID_EXTRASTATS_ENABLE; + + if (ARG_PACKED) + xvid_enc_create.global |=XVID_GLOBAL_PACKED; + + if (ARG_STATS) + xvid_enc_create.global |=XVID_GLOBAL_EXTRASTATS_ENABLE; /* 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); @@ -877,9 +966,9 @@ } /* Set up core's general features */ - xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; + xvid_enc_frame.vol_flags = 0; if (ARG_STATS) - xvid_enc_frame.vol_flags |= XVID_EXTRASTATS; + xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS; /* Set up core's general features */ xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; @@ -887,8 +976,8 @@ /* Frame type -- let core decide for us */ xvid_enc_frame.type = XVID_TYPE_AUTO; - /* Force the right quantizer */ - xvid_enc_frame.quant = ARG_QUANTI; + /* 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];