--- branches/dev-api-4/xvidcore/examples/xvid_encraw.c 2003/05/15 17:21:08 1026 +++ branches/dev-api-4/xvidcore/examples/xvid_encraw.c 2003/12/16 09:24:48 1264 @@ -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.23 2003-05-15 17:21:08 edgomez Exp $ + * $Id: xvid_encraw.c,v 1.11.2.39 2003-12-16 09:24:48 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,11 +50,13 @@ #include "xvid.h" +#undef READ_PNM /***************************************************************************** * Quality presets ****************************************************************************/ -static xvid_motion_t const motion_presets[] = { + +static const int motion_presets[] = { /* quality 0 */ 0, @@ -67,47 +73,44 @@ /* quality 4 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | - XVID_ME_CHROMA16 | XVID_ME_CHROMA8, + XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, /* quality 5 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | - XVID_ME_CHROMA16 | XVID_ME_CHROMA8, + 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_CHROMA16 | XVID_ME_CHROMA8 , + XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, }; #define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0])) -static xvid_vop_t const vop_presets[] = { +static const int vop_presets[] = { /* quality 0 */ 0, /* quality 1 */ - XVID_VOP_DYNAMIC_BFRAMES, + 0, /* quality 2 */ - XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL, + XVID_VOP_HALFPEL, /* quality 3 */ - XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | - XVID_VOP_INTER4V, + XVID_VOP_HALFPEL | XVID_VOP_INTER4V, /* quality 4 */ - XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | - XVID_VOP_INTER4V, + XVID_VOP_HALFPEL | XVID_VOP_INTER4V, /* quality 5 */ - XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | - XVID_VOP_INTER4V | XVID_VOP_TRELLISQUANT, + 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, + XVID_VOP_HALFPEL | XVID_VOP_INTER4V | + XVID_VOP_TRELLISQUANT | XVID_VOP_HQACPRED, }; #define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0])) @@ -134,9 +137,11 @@ 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; +static int ARG_SAVEINDIVIDUAL = 0; static char *ARG_OUTPUTFILE = NULL; static int XDIM = 0; static int YDIM = 0; @@ -145,8 +150,16 @@ static int ARG_MAXBFRAMES = 0; static int ARG_PACKED = 0; static int ARG_DEBUG = 0; +static int ARG_VOPDEBUG = 0; +static int ARG_GMC = 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) @@ -177,9 +190,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); @@ -294,6 +313,9 @@ } 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]; @@ -306,16 +328,27 @@ } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) { i++; ARG_INPUTTYPE = atoi(argv[i]); - } else if (strcmp("-nframes", argv[i]) == 0 && i < argc - 1) { + } 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) { - 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) { + ARG_SAVEMPEGSTREAM = 1; i++; ARG_OUTPUTFILE = argv[i]; + } else if (strcmp("-vop_debug", argv[i]) == 0) { + ARG_VOPDEBUG = 1; + } else if (strcmp("-qpel", argv[i]) == 0) { + ARG_QPEL = 1; + } else if (strcmp("-gmc", argv[i]) == 0) { + ARG_GMC = 1; + } else if (strcmp("-closed_gop", argv[i]) == 0) { + ARG_CLOSED_GOP = 1; } else if (strcmp("-help", argv[i])) { usage(); return (0); @@ -330,7 +363,7 @@ * Arguments checking ****************************************************************************/ - if (XDIM <= 0 || XDIM >= 2048 || YDIM <= 0 || YDIM >= 2048) { + 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 */ @@ -364,7 +397,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); @@ -430,7 +467,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); @@ -449,7 +490,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 */ @@ -472,7 +513,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))))) @@ -506,28 +547,31 @@ ****************************************************************************/ if (m4v_size > 0 && ARG_SAVEMPEGSTREAM) { + /* Save single files */ - if (out_file == NULL) { + if (ARG_SAVEINDIVIDUAL) { + FILE *out; sprintf(filename, "%sframe%05d.m4v", filepath, output_num); - out_file = fopen(filename, "wb"); - fwrite(mp4_buffer, m4v_size, 1, out_file); - fclose(out_file); - out_file = NULL; + out = fopen(filename, "w+b"); + fwrite(mp4_buffer, m4v_size, 1, out); + fclose(out); output_num++; - } else { + } - /* Write mp4 data */ + /* Save ES stream */ + if (ARG_OUTPUTFILE && out_file) fwrite(mp4_buffer, 1, m4v_size, out_file); - - } } input_num++; /* 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); @@ -621,16 +665,16 @@ { 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, " -nframes integer: number of frames to encode\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 mpeg4 raw stream\n"); - fprintf(stderr, " -o string: output filename\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"); @@ -645,14 +689,19 @@ 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, " -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..%d])\n", ME_ELEMENTS - 1); + fprintf(stderr, " -qpel : use quarter pixel ME\n"); + fprintf(stderr, " -gmc : use global motion compensation\n"); fprintf(stderr, " -packed : packed mode\n"); + fprintf(stderr, " -closed_gop : closed GOP 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); @@ -667,6 +716,7 @@ * *****************************************************************************/ +#ifndef READ_PNM static int read_pgmheader(FILE * handle) { @@ -722,6 +772,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, @@ -806,13 +896,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 @@ -834,6 +925,7 @@ /* 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; @@ -885,11 +977,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; @@ -904,7 +998,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; @@ -918,10 +1016,13 @@ xvid_enc_create.global = 0; if (ARG_PACKED) - xvid_enc_create.global |=XVID_GLOBAL_PACKED; + xvid_enc_create.global |= XVID_GLOBAL_PACKED; + + if (ARG_CLOSED_GOP) + xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP; if (ARG_STATS) - xvid_enc_create.global |=XVID_GLOBAL_EXTRASTATS_ENABLE; + 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); @@ -971,8 +1072,13 @@ /* 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; } @@ -981,9 +1087,19 @@ 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; /* Set up core's general features */ xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; + if (ARG_GMC) + xvid_enc_frame.vol_flags |= XVID_ME_GME_REFINE; + + 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; @@ -994,14 +1110,18 @@ /* Set up motion estimation flags */ xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; + 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); + 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;