--- branches/dev-api-4/xvidcore/examples/xvid_encraw.c 2003/04/19 11:26:47 991 +++ branches/dev-api-4/xvidcore/examples/xvid_encraw.c 2003/05/15 17:21:08 1026 @@ -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.18 2003-04-19 11:26:47 chl Exp $ + * $Id: xvid_encraw.c,v 1.11.2.23 2003-05-15 17:21:08 edgomez Exp $ * ****************************************************************************/ @@ -50,41 +50,77 @@ /***************************************************************************** * Quality presets ****************************************************************************/ - static xvid_motion_t const motion_presets[] = { + /* quality 0 */ 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 xvid_vol_t const vol_presets[] = { - XVID_VOL_MPEGQUANT, - 0, - 0, - XVID_VOL_QUARTERPEL, - XVID_VOL_QUARTERPEL | XVID_VOL_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[] = { + /* quality 0 */ + 0, + + /* quality 1 */ XVID_VOP_DYNAMIC_BFRAMES, - XVID_VOP_DYNAMIC_BFRAMES, + + /* quality 2 */ 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 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 ****************************************************************************/ +#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,11 +128,10 @@ 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_PASS2_BITRATE = 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 char *ARG_INPUTFILE = NULL; @@ -196,6 +231,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,17 +251,17 @@ } 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) { + } else if (strcmp("-pass2", argv[i]) == 0 && i < argc - 1) { 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]); @@ -232,6 +273,21 @@ } 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]); @@ -253,9 +309,6 @@ } 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) { @@ -283,9 +336,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) { @@ -496,10 +550,10 @@ totalenctime = -1; } - printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d, ", + 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", + printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", totalPSNR[0],totalPSNR[1],totalPSNR[2]); } printf("\n"); @@ -585,19 +639,24 @@ 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, " -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, "\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, " -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"); } /***************************************************************************** @@ -731,10 +790,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; @@ -776,29 +836,19 @@ 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) { - 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].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); + 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_fixed; - plugins[xvid_enc_create.num_plugins].param = &rcfixed; + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single; + plugins[xvid_enc_create.num_plugins].param = &single; xvid_enc_create.num_plugins++; } @@ -806,7 +856,7 @@ memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t)); rc2pass2.version = XVID_VERSION; rc2pass2.filename = ARG_PASS2; - rc2pass2.bitrate = ARG_PASS2_BITRATE; + rc2pass2.bitrate = ARG_BITRATE; plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; plugins[xvid_enc_create.num_plugins].param = &rc2pass2; @@ -928,7 +978,7 @@ } /* 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_VOL_EXTRASTATS;