--- xvid_encraw.c 2003/05/18 12:24:55 1.11.2.26 +++ xvid_encraw.c 2003/12/10 22:58:32 1.11.2.38 @@ -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.26 2003/05/18 12:24:55 edgomez Exp $ + * $Id: xvid_encraw.c,v 1.11.2.38 2003/12/10 22:58:32 edgomez 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,22 +73,22 @@ /* 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, @@ -135,6 +141,7 @@ 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; @@ -144,8 +151,15 @@ 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) @@ -176,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); @@ -308,19 +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) { 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); @@ -369,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); @@ -435,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); @@ -511,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); @@ -626,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"); @@ -655,7 +694,10 @@ 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 : activates xvidcore internal debugging output\n"); @@ -674,6 +716,7 @@ * *****************************************************************************/ +#ifndef READ_PNM static int read_pgmheader(FILE * handle) { @@ -729,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, @@ -820,7 +903,7 @@ 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 @@ -842,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; @@ -932,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); @@ -985,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; } @@ -995,9 +1087,16 @@ 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.vop_flags |= XVID_ME_GME_REFINE; + if (ARG_VOPDEBUG) { xvid_enc_frame.vop_flags |= XVID_VOP_DEBUG; } @@ -1011,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;