--- xvid_encraw.c 2003/01/12 17:21:43 1.1.2.1 +++ xvid_encraw.c 2002/09/28 14:26:53 1.2 @@ -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.1.2.1 2003/01/12 17:21:43 edgomez Exp $ + * $Id: xvid_encraw.c,v 1.2 2002/09/28 14:26:53 edgomez Exp $ * ****************************************************************************/ @@ -36,7 +36,6 @@ #include #include -#include #include #ifndef _MSC_VER #include @@ -51,25 +50,25 @@ ****************************************************************************/ static int const motion_presets[7] = { - 0, /* Q 0 */ - PMV_EARLYSTOP16, /* Q 1 */ - PMV_EARLYSTOP16, /* Q 2 */ - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */ - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */ - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */ + 0, // Q 0 + PMV_EARLYSTOP16, // Q 1 + PMV_EARLYSTOP16, // Q 2 + PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 3 + PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 4 + PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | // Q 5 PMV_HALFPELREFINE8, - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ + PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | // Q 6 PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 }; static int const general_presets[7] = { - XVID_H263QUANT, /* Q 0 */ - XVID_MPEGQUANT, /* Q 1 */ - XVID_H263QUANT, /* Q 2 */ - XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */ - XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */ - XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */ - XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */ + XVID_H263QUANT, // Q 0 + XVID_MPEGQUANT, // Q 1 + XVID_H263QUANT, // Q 2 + XVID_H263QUANT | XVID_HALFPEL, // Q 3 + XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 4 + XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 5 + XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V // Q 6 }; @@ -80,12 +79,6 @@ /* Maximum number of frames to encode */ #define ABS_MAXFRAMENR 9999 -/* HINTMODEs */ -#define HINT_MODE_NONE 0 -#define HINT_MODE_GET 1 -#define HINT_MODE_SET 2 -#define HINT_FILE "hints.mv" - static int ARG_BITRATE = 900; static int ARG_QUANTI = 0; static int ARG_QUALITY = 6; @@ -98,7 +91,6 @@ static int ARG_SAVEMPEGSTREAM = 0; static int ARG_OUTPUTTYPE = 0; static char *ARG_OUTPUTFILE = NULL; -static int ARG_HINTMODE = HINT_MODE_NONE; static int XDIM = 0; static int YDIM = 0; #define IMAGE_SIZE(x,y) ((x)*(y)*3/2) @@ -143,8 +135,7 @@ static int enc_init(int use_assembler); static int enc_stop(); static int enc_main(unsigned char* image, unsigned char* bitstream, - unsigned char *hints_buffer, - long *streamlength, long* frametype, long *hints_size); + int *streamlength, int* frametype); /***************************************************************************** * Main function @@ -156,27 +147,24 @@ unsigned char *mp4_buffer = NULL; unsigned char *in_buffer = NULL; unsigned char *out_buffer = NULL; - unsigned char *hints_buffer = NULL; double enctime; double totalenctime=0.; long totalsize; - long hints_size; int status; - long frame_type; - long bigendian; + int frame_type; + int bigendian; - long m4v_size; + int m4v_size; int use_assembler=0; char filename[256]; FILE *in_file = stdin; FILE *out_file = NULL; - FILE *hints_file = NULL; - printf("xvid_encraw - raw mpeg4 bitstream encoder "); + printf("xvid_decraw - raw mpeg4 bitstream encoder "); printf("written by Christoph Lampert 2002\n\n"); /***************************************************************************** @@ -232,10 +220,6 @@ i++; ARG_OUTPUTTYPE = atoi(argv[i]); } - else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { - i++; - ARG_HINTMODE = atoi(argv[i]); - } else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { i++; ARG_OUTPUTFILE = argv[i]; @@ -280,37 +264,6 @@ return -1; } - if ( ARG_HINTMODE != HINT_MODE_NONE && - ARG_HINTMODE != HINT_MODE_GET && - ARG_HINTMODE != HINT_MODE_SET) - ARG_HINTMODE = HINT_MODE_NONE; - - if( ARG_HINTMODE != HINT_MODE_NONE) { - char *rights = "rb"; - - /* - * If we are getting hints from core, we will have to write them to - * hint file - */ - if(ARG_HINTMODE == HINT_MODE_GET) - rights = "w+b"; - - /* Open the hint file */ - hints_file = fopen(HINT_FILE, rights); - if(hints_file == NULL) { - fprintf(stderr, "Error opening input file %s\n", HINT_FILE); - return -1; - } - - /* Allocate hint memory space, we will be using rawhints */ - /* NB : Hope 1Mb is enough */ - if((hints_buffer = malloc(1024*1024)) == NULL) { - fprintf(stderr, "Memory allocation error\n"); - return -1; - } - - } - if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { in_file = stdin; } @@ -357,12 +310,6 @@ * Main loop ****************************************************************************/ - totalsize = LONG_PACK('M','P','4','U'); - if(*((char *)(&totalsize)) == 'M') - bigendian = 1; - else - bigendian = 0; - if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) @@ -377,8 +324,15 @@ /* Write header */ if (ARG_OUTPUTTYPE) { + char *ptr; + long test; - long test = LONG_PACK('M','P','4','U'); + test = LONG_PACK('M','P','4','U'); + ptr = (unsigned char *)&test; + if(*ptr == 'M') + bigendian = 1; + else + bigendian = 0; test = (!bigendian)?SWAP(test):test; @@ -400,9 +354,9 @@ do { if (ARG_INPUTTYPE) - status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */ + status = read_pgmdata(in_file, in_buffer); // read PGM data (YUV-format) else - status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */ + status = read_yuvdata(in_file, in_buffer); // read raw data (YUV-format) if (status) { @@ -411,22 +365,11 @@ } /***************************************************************************** - * Read hints from file - ****************************************************************************/ - - if(ARG_HINTMODE == HINT_MODE_SET) { - fread(&hints_size, 1, sizeof(long), hints_file); - hints_size = (!bigendian)?SWAP(hints_size):hints_size; - fread(hints_buffer, 1, hints_size, hints_file); - } - -/***************************************************************************** * Encode and decode this frame ****************************************************************************/ enctime = msecond(); - status = enc_main(in_buffer, mp4_buffer, hints_buffer, - &m4v_size, &frame_type, &hints_size); + status = enc_main(in_buffer, mp4_buffer, &m4v_size, &frame_type); enctime = msecond() - enctime; totalenctime += enctime; @@ -435,21 +378,6 @@ printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", (int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); -/***************************************************************************** - * Save hints to file - ****************************************************************************/ - - if(ARG_HINTMODE == HINT_MODE_GET) { - hints_size = (!bigendian)?SWAP(hints_size):hints_size; - fwrite(&hints_size, 1, sizeof(long), hints_file); - hints_size = (!bigendian)?SWAP(hints_size):hints_size; - fwrite(hints_buffer, 1, hints_size, hints_file); - } - -/***************************************************************************** - * Save stream to file - ****************************************************************************/ - if (ARG_SAVEMPEGSTREAM) { /* Save single files */ @@ -507,18 +435,14 @@ fprintf(stderr, "Encore RELEASE problem return value %d\n", status); } - if(in_file) - fclose(in_file); + fclose(in_file); if(out_file) fclose(out_file); - if(hints_file) - fclose(hints_file); free_all_memory: free(out_buffer); free(mp4_buffer); free(in_buffer); - if(hints_buffer) free(hints_buffer); return 0; @@ -572,7 +496,6 @@ fprintf(stderr, " + stream.m4v with -mt 0\n"); fprintf(stderr, " + stream.mp4u with -mt 1\n"); fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); - fprintf(stderr, " -mv integer : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n"); fprintf(stderr, " -help : prints this help message\n"); fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); fprintf(stderr, " (* means default)\n"); @@ -699,10 +622,6 @@ xparam.min_quantizer = ARG_MINQUANT; xparam.max_quantizer = ARG_MAXQUANT; xparam.max_key_interval = (int)ARG_FRAMERATE*10; - xparam.bquant_ratio = 150; - xparam.bquant_offset = 100; - xparam.max_bframes = 0; - xparam.global = 0; /* I use a small value here, since will not encode whole movies, but short clips */ @@ -722,8 +641,7 @@ } static int enc_main(unsigned char* image, unsigned char* bitstream, - unsigned char* hints_buffer, - long *streamlength, long *frametype, long *hints_size) + int *streamlength, int* frametype) { int xerr; @@ -731,39 +649,21 @@ XVID_ENC_STATS xstats; xframe.bitstream = bitstream; - xframe.length = -1; /* this is written by the routine */ + xframe.length = -1; // this is written by the routine xframe.image = image; - xframe.colorspace = XVID_CSP_YV12; /* defined in */ + xframe.colorspace = XVID_CSP_YV12; // defined in - xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */ + xframe.intra = -1; // let the codec decide between I-frame (1) and P-frame (0) + + xframe.quant = ARG_QUANTI; // is quant != 0, use a fixed quant (and ignore bitrate) - xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */ - xframe.bquant = 0; - xframe.motion = motion_presets[ARG_QUALITY]; xframe.general = general_presets[ARG_QUALITY]; xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; - xframe.stride = XDIM; - - xframe.hint.hintstream = hints_buffer; - - if(ARG_HINTMODE == HINT_MODE_SET) { - xframe.hint.hintlength = *hints_size; - xframe.hint.rawhints = 0; - xframe.general |= XVID_HINTEDME_SET; - } - - if(ARG_HINTMODE == HINT_MODE_GET) { - xframe.hint.rawhints = 0; - xframe.general |= XVID_HINTEDME_GET; - } xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); - if(ARG_HINTMODE == HINT_MODE_GET) - *hints_size = xframe.hint.hintlength; - /* * This is statictical data, e.g. for 2-pass. If you are not * interested in any of this, you can use NULL instead of &xstats