80 |
/* Maximum number of frames to encode */ |
/* Maximum number of frames to encode */ |
81 |
#define ABS_MAXFRAMENR 9999 |
#define ABS_MAXFRAMENR 9999 |
82 |
|
|
|
/* HINTMODEs */ |
|
|
#define HINT_MODE_NONE 0 |
|
|
#define HINT_MODE_GET 1 |
|
|
#define HINT_MODE_SET 2 |
|
|
#define HINT_FILE "hints.mv" |
|
|
|
|
83 |
static int ARG_BITRATE = 900; |
static int ARG_BITRATE = 900; |
84 |
static int ARG_QUANTI = 0; |
static int ARG_QUANTI = 0; |
85 |
static int ARG_QUALITY = 6; |
static int ARG_QUALITY = 6; |
92 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
93 |
static int ARG_OUTPUTTYPE = 0; |
static int ARG_OUTPUTTYPE = 0; |
94 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
|
static int ARG_HINTMODE = HINT_MODE_NONE; |
|
95 |
static int XDIM = 0; |
static int XDIM = 0; |
96 |
static int YDIM = 0; |
static int YDIM = 0; |
|
static int ARG_BQRATIO = 150; |
|
|
static int ARG_BQOFFSET = 100; |
|
|
static int ARG_MAXBFRAMES = 0; |
|
97 |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
98 |
|
|
99 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
136 |
static int enc_init(int use_assembler); |
static int enc_init(int use_assembler); |
137 |
static int enc_stop(); |
static int enc_stop(); |
138 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
139 |
unsigned char *hints_buffer, |
int *streamlength, int* frametype); |
|
long *streamlength, long* frametype, long *hints_size); |
|
140 |
|
|
141 |
/***************************************************************************** |
/***************************************************************************** |
142 |
* Main function |
* Main function |
148 |
unsigned char *mp4_buffer = NULL; |
unsigned char *mp4_buffer = NULL; |
149 |
unsigned char *in_buffer = NULL; |
unsigned char *in_buffer = NULL; |
150 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
|
unsigned char *hints_buffer = NULL; |
|
151 |
|
|
152 |
double enctime; |
double enctime; |
153 |
double totalenctime=0.; |
double totalenctime=0.; |
154 |
|
|
155 |
long totalsize; |
long totalsize; |
|
long hints_size; |
|
156 |
int status; |
int status; |
157 |
long frame_type; |
int frame_type; |
158 |
long bigendian; |
int bigendian; |
159 |
|
|
160 |
long m4v_size; |
int m4v_size; |
161 |
int use_assembler=0; |
int use_assembler=0; |
162 |
|
|
163 |
char filename[256]; |
char filename[256]; |
164 |
|
|
165 |
FILE *in_file = stdin; |
FILE *in_file = stdin; |
166 |
FILE *out_file = NULL; |
FILE *out_file = NULL; |
|
FILE *hints_file = NULL; |
|
167 |
|
|
168 |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
printf("xvid_decraw - raw mpeg4 bitstream encoder "); |
169 |
printf("written by Christoph Lampert 2002\n\n"); |
printf("written by Christoph Lampert 2002\n\n"); |
170 |
|
|
171 |
/***************************************************************************** |
/***************************************************************************** |
189 |
i++; |
i++; |
190 |
ARG_BITRATE = atoi(argv[i]); |
ARG_BITRATE = atoi(argv[i]); |
191 |
} |
} |
|
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]); |
|
|
} |
|
192 |
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
193 |
i++; |
i++; |
194 |
ARG_QUALITY = atoi(argv[i]); |
ARG_QUALITY = atoi(argv[i]); |
221 |
i++; |
i++; |
222 |
ARG_OUTPUTTYPE = atoi(argv[i]); |
ARG_OUTPUTTYPE = atoi(argv[i]); |
223 |
} |
} |
|
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_HINTMODE = atoi(argv[i]); |
|
|
} |
|
224 |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
225 |
i++; |
i++; |
226 |
ARG_OUTPUTFILE = argv[i]; |
ARG_OUTPUTFILE = argv[i]; |
265 |
return -1; |
return -1; |
266 |
} |
} |
267 |
|
|
|
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; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
268 |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
269 |
in_file = stdin; |
in_file = stdin; |
270 |
} |
} |
365 |
} |
} |
366 |
|
|
367 |
/***************************************************************************** |
/***************************************************************************** |
|
* 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); |
|
|
} |
|
|
|
|
|
/***************************************************************************** |
|
368 |
* Encode and decode this frame |
* Encode and decode this frame |
369 |
****************************************************************************/ |
****************************************************************************/ |
370 |
|
|
371 |
enctime = msecond(); |
enctime = msecond(); |
372 |
status = enc_main(in_buffer, mp4_buffer, hints_buffer, |
status = enc_main(in_buffer, mp4_buffer, &m4v_size, &frame_type); |
|
&m4v_size, &frame_type, &hints_size); |
|
373 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
374 |
|
|
375 |
totalenctime += enctime; |
totalenctime += enctime; |
376 |
totalsize += m4v_size; |
totalsize += m4v_size; |
377 |
|
|
378 |
{ |
printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", |
379 |
char *type; |
(int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); |
|
|
|
|
switch(frame_type) { |
|
|
case 0: type = "P"; break; |
|
|
case 1: type = "I"; break; |
|
|
case 2: type = "B"; break; |
|
|
default: type = "N"; break; |
|
|
} |
|
|
|
|
|
printf("Frame %5d: type %s, enctime=%6.1f ms, size=%6d bytes\n", |
|
|
(int)filenr, 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 |
|
|
****************************************************************************/ |
|
380 |
|
|
381 |
if (ARG_SAVEMPEGSTREAM) |
if (ARG_SAVEMPEGSTREAM) |
382 |
{ |
{ |
419 |
totalsize /= filenr; |
totalsize /= filenr; |
420 |
totalenctime /= filenr; |
totalenctime /= filenr; |
421 |
|
|
422 |
printf("Avg: enctime %5.2f ms, %5.2f fps, size %7d bytes\n", |
printf("Avg: enctime %5.2f ms, %5.2f fps, filesize %7d bytes\n", |
423 |
totalenctime, 1000/totalenctime, (int)totalsize); |
totalenctime, 1000/totalenctime, (int)totalsize); |
424 |
|
|
425 |
/***************************************************************************** |
/***************************************************************************** |
435 |
fprintf(stderr, "Encore RELEASE problem return value %d\n", status); |
fprintf(stderr, "Encore RELEASE problem return value %d\n", status); |
436 |
} |
} |
437 |
|
|
|
if(in_file) |
|
438 |
fclose(in_file); |
fclose(in_file); |
439 |
if(out_file) |
if(out_file) |
440 |
fclose(out_file); |
fclose(out_file); |
|
if(hints_file) |
|
|
fclose(hints_file); |
|
441 |
|
|
442 |
free_all_memory: |
free_all_memory: |
443 |
free(out_buffer); |
free(out_buffer); |
444 |
free(mp4_buffer); |
free(mp4_buffer); |
445 |
free(in_buffer); |
free(in_buffer); |
|
if(hints_buffer) free(hints_buffer); |
|
446 |
|
|
447 |
return 0; |
return 0; |
448 |
|
|
483 |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
484 |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
485 |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
fprintf(stderr, " -b integer : target bitrate (>0 | 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"); |
|
486 |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
487 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
488 |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
496 |
fprintf(stderr, " + stream.m4v with -mt 0\n"); |
fprintf(stderr, " + stream.m4v with -mt 0\n"); |
497 |
fprintf(stderr, " + stream.mp4u with -mt 1\n"); |
fprintf(stderr, " + stream.mp4u with -mt 1\n"); |
498 |
fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=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"); |
|
499 |
fprintf(stderr, " -help : prints this help message\n"); |
fprintf(stderr, " -help : prints this help message\n"); |
500 |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
501 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
622 |
xparam.min_quantizer = ARG_MINQUANT; |
xparam.min_quantizer = ARG_MINQUANT; |
623 |
xparam.max_quantizer = ARG_MAXQUANT; |
xparam.max_quantizer = ARG_MAXQUANT; |
624 |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
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; |
|
625 |
|
|
626 |
/* I use a small value here, since will not encode whole movies, but short clips */ |
/* I use a small value here, since will not encode whole movies, but short clips */ |
627 |
|
|
641 |
} |
} |
642 |
|
|
643 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
644 |
unsigned char* hints_buffer, |
int *streamlength, int* frametype) |
|
long *streamlength, long *frametype, long *hints_size) |
|
645 |
{ |
{ |
646 |
int xerr; |
int xerr; |
647 |
|
|
657 |
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) */ |
658 |
|
|
659 |
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; |
|
660 |
|
|
661 |
xframe.motion = motion_presets[ARG_QUALITY]; |
xframe.motion = motion_presets[ARG_QUALITY]; |
662 |
xframe.general = general_presets[ARG_QUALITY]; |
xframe.general = general_presets[ARG_QUALITY]; |
663 |
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
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; |
|
|
} |
|
664 |
|
|
665 |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
666 |
|
|
|
if(ARG_HINTMODE == HINT_MODE_GET) |
|
|
*hints_size = xframe.hint.hintlength; |
|
|
|
|
667 |
/* |
/* |
668 |
* This is statictical data, e.g. for 2-pass. If you are not |
* This is statictical data, e.g. for 2-pass. If you are not |
669 |
* interested in any of this, you can use NULL instead of &xstats |
* interested in any of this, you can use NULL instead of &xstats |