--- trunk/xvidcore/examples/xvid_encraw.c 2002/09/28 14:26:53 558 +++ trunk/xvidcore/examples/xvid_encraw.c 2003/02/16 05:11:39 860 @@ -3,7 +3,7 @@ * XVID MPEG-4 VIDEO CODEC * - Console based test application - * - * Copyright(C) 2002 Christoph Lampert + * Copyright(C) 2002-2003 Christoph Lampert * * 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 +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.2 2002-09-28 14:26:53 edgomez Exp $ + * $Id: xvid_encraw.c,v 1.11 2003-02-16 05:11:39 suxen_drol Exp $ * ****************************************************************************/ @@ -36,8 +36,9 @@ #include #include +#include #include -#ifndef _MSC_VER +#ifndef WIN32 #include #else #include @@ -50,25 +51,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 */ }; @@ -79,6 +80,12 @@ /* 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; @@ -89,18 +96,18 @@ static char *ARG_INPUTFILE = NULL; static int ARG_INPUTTYPE = 0; 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; +static int ARG_BQRATIO = 120; +static int ARG_BQOFFSET = 0; +static int ARG_MAXBFRAMES = 0; #define IMAGE_SIZE(x,y) ((x)*(y)*3/2) #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) #define SMALL_EPS 1e-10 -#define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \ - (((long)(c))<<8) |((long)(d))) - #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) @@ -135,7 +142,8 @@ static int enc_init(int use_assembler); static int enc_stop(); static int enc_main(unsigned char* image, unsigned char* bitstream, - int *streamlength, int* frametype); + unsigned char *hints_buffer, + long *streamlength, long* frametype, long *hints_size); /***************************************************************************** * Main function @@ -147,25 +155,28 @@ 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; - int frame_type; - int bigendian; + long frame_type; + long bigendian; - int m4v_size; + long m4v_size; int use_assembler=0; char filename[256]; FILE *in_file = stdin; FILE *out_file = NULL; + FILE *hints_file = NULL; - printf("xvid_decraw - raw mpeg4 bitstream encoder "); - printf("written by Christoph Lampert 2002\n\n"); + printf("xvid_encraw - raw mpeg4 bitstream encoder "); + printf("written by Christoph Lampert 2002-2003\n\n"); /***************************************************************************** * Command line parsing @@ -188,6 +199,18 @@ i++; ARG_BITRATE = atoi(argv[i]); } + else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) { + i++; + ARG_MAXBFRAMES = atoi(argv[i]); + } + else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { + i++; + ARG_BQRATIO = atoi(argv[i]); + } + else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) { + i++; + ARG_BQOFFSET = atoi(argv[i]); + } else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { i++; ARG_QUALITY = atoi(argv[i]); @@ -216,9 +239,9 @@ i++; ARG_SAVEMPEGSTREAM = atoi(argv[i]); } - else if (strcmp("-mt", argv[i]) == 0 && i < argc - 1 ) { + else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { i++; - ARG_OUTPUTTYPE = atoi(argv[i]); + ARG_HINTMODE = atoi(argv[i]); } else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { i++; @@ -264,6 +287,37 @@ 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; } @@ -284,7 +338,6 @@ } /* now we know the sizes, so allocate memory */ - in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); if (!in_buffer) goto free_all_memory; @@ -310,36 +363,13 @@ * Main loop ****************************************************************************/ - if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { - - if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) - ARG_OUTPUTFILE = "stream.mp4u"; - else if(ARG_OUTPUTFILE == NULL && !ARG_OUTPUTTYPE) - ARG_OUTPUTFILE = "stream.m4v"; + if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) { if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); goto release_all; } - /* Write header */ - if (ARG_OUTPUTTYPE) { - char *ptr; - long test; - - test = LONG_PACK('M','P','4','U'); - ptr = (unsigned char *)&test; - if(*ptr == 'M') - bigendian = 1; - else - bigendian = 0; - - test = (!bigendian)?SWAP(test):test; - - fwrite(&test, sizeof(test), 1, out_file); - - } - } else { out_file = NULL; @@ -354,9 +384,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) { @@ -365,18 +395,55 @@ } /***************************************************************************** + * 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, &m4v_size, &frame_type); + status = enc_main(in_buffer, mp4_buffer, hints_buffer, + &m4v_size, &frame_type, &hints_size); enctime = msecond() - enctime; + /* if it's a not coded VOP (aka NVOP) then we write nothing */ + if(frame_type == 5) goto next_frame; + + { + char *type[] = {"P", "I", "B", "S", "Packed", "N", "Unknown"}; + + if(frame_type<0 || frame_type>5) frame_type = 6; + + printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n", + (int)filenr, type[frame_type], (float)enctime, (int)m4v_size); + + } + + /* Update encoding time stats */ totalenctime += enctime; totalsize += m4v_size; - 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) { @@ -389,24 +456,19 @@ out_file = NULL; } else { - /* Using mp4u container */ - if (ARG_OUTPUTTYPE) { - long size = m4v_size; - size = (!bigendian)?SWAP(size):size; - fwrite(&size, sizeof(size), 1, out_file); - } /* Write mp4 data */ - fwrite(mp4_buffer, m4v_size, 1, out_file); + fwrite(mp4_buffer, 1, m4v_size, out_file); } } + next_frame: /* Read the header if it's pgm stream */ if (ARG_INPUTTYPE) status = read_pgmheader(in_file); - filenr++; + if(frame_type != 5) filenr++; } while ( (!status) && (filenr0 | default=900kbit)\n"); + fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); + fprintf(stderr, " -bn integer : max bframes (default=0)\n"); + fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); + fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); fprintf(stderr, " -f float : target framerate (>0)\n"); fprintf(stderr, " -i string : input filename (default=stdin)\n"); fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); @@ -492,10 +562,9 @@ fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); fprintf(stderr, " When this option is not used : one file per encoded frame\n"); - fprintf(stderr, " When this option is used :\n"); - fprintf(stderr, " + stream.m4v with -mt 0\n"); - fprintf(stderr, " + stream.mp4u with -mt 1\n"); + fprintf(stderr, " When this option is used : save to 'string' (default=stream.m4v)\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"); @@ -591,7 +660,7 @@ if(use_assembler) { -#ifdef ARCH_IA64 +#ifdef ARCH_IS_IA64 xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; #else xinit.cpu_flags = 0; @@ -622,6 +691,11 @@ xparam.min_quantizer = ARG_MINQUANT; xparam.max_quantizer = ARG_MAXQUANT; xparam.max_key_interval = (int)ARG_FRAMERATE*10; + xparam.bquant_ratio = ARG_BQRATIO; + xparam.bquant_offset = ARG_BQOFFSET; + xparam.max_bframes = ARG_MAXBFRAMES; + xparam.frame_drop_ratio = 0; + xparam.global = 0; /* I use a small value here, since will not encode whole movies, but short clips */ @@ -641,7 +715,8 @@ } static int enc_main(unsigned char* image, unsigned char* bitstream, - int *streamlength, int* frametype) + unsigned char* hints_buffer, + long *streamlength, long *frametype, long *hints_size) { int xerr; @@ -649,21 +724,39 @@ 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.intra = -1; // let the codec decide between I-frame (1) and P-frame (0) + xframe.colorspace = XVID_CSP_YV12; /* defined in */ - xframe.quant = ARG_QUANTI; // is quant != 0, use a fixed quant (and ignore bitrate) + 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.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