19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
* $Id: xvid_encraw.c,v 1.2 2002-09-28 14:26:53 edgomez Exp $ |
* $Id: xvid_encraw.c,v 1.9 2003-02-11 21:56:31 edgomez Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
36 |
|
|
37 |
#include <stdio.h> |
#include <stdio.h> |
38 |
#include <stdlib.h> |
#include <stdlib.h> |
39 |
|
#include <string.h> |
40 |
#include <math.h> |
#include <math.h> |
41 |
#ifndef _MSC_VER |
#ifndef _MSC_VER |
42 |
#include <sys/time.h> |
#include <sys/time.h> |
51 |
****************************************************************************/ |
****************************************************************************/ |
52 |
|
|
53 |
static int const motion_presets[7] = { |
static int const motion_presets[7] = { |
54 |
0, // Q 0 |
0, /* Q 0 */ |
55 |
PMV_EARLYSTOP16, // Q 1 |
PMV_EARLYSTOP16, /* Q 1 */ |
56 |
PMV_EARLYSTOP16, // Q 2 |
PMV_EARLYSTOP16, /* Q 2 */ |
57 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 3 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */ |
58 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 4 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */ |
59 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | // Q 5 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */ |
60 |
PMV_HALFPELREFINE8, |
PMV_HALFPELREFINE8, |
61 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | // Q 6 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ |
62 |
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
63 |
}; |
}; |
64 |
|
|
65 |
static int const general_presets[7] = { |
static int const general_presets[7] = { |
66 |
XVID_H263QUANT, // Q 0 |
XVID_H263QUANT, /* Q 0 */ |
67 |
XVID_MPEGQUANT, // Q 1 |
XVID_MPEGQUANT, /* Q 1 */ |
68 |
XVID_H263QUANT, // Q 2 |
XVID_H263QUANT, /* Q 2 */ |
69 |
XVID_H263QUANT | XVID_HALFPEL, // Q 3 |
XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */ |
70 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 4 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */ |
71 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 5 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */ |
72 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V // Q 6 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */ |
73 |
}; |
}; |
74 |
|
|
75 |
|
|
80 |
/* Maximum number of frames to encode */ |
/* Maximum number of frames to encode */ |
81 |
#define ABS_MAXFRAMENR 9999 |
#define ABS_MAXFRAMENR 9999 |
82 |
|
|
83 |
|
/* HINTMODEs */ |
84 |
|
#define HINT_MODE_NONE 0 |
85 |
|
#define HINT_MODE_GET 1 |
86 |
|
#define HINT_MODE_SET 2 |
87 |
|
#define HINT_FILE "hints.mv" |
88 |
|
|
89 |
static int ARG_BITRATE = 900; |
static int ARG_BITRATE = 900; |
90 |
static int ARG_QUANTI = 0; |
static int ARG_QUANTI = 0; |
91 |
static int ARG_QUALITY = 6; |
static int ARG_QUALITY = 6; |
98 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
99 |
static int ARG_OUTPUTTYPE = 0; |
static int ARG_OUTPUTTYPE = 0; |
100 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
101 |
|
static int ARG_HINTMODE = HINT_MODE_NONE; |
102 |
static int XDIM = 0; |
static int XDIM = 0; |
103 |
static int YDIM = 0; |
static int YDIM = 0; |
104 |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
143 |
static int enc_init(int use_assembler); |
static int enc_init(int use_assembler); |
144 |
static int enc_stop(); |
static int enc_stop(); |
145 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
146 |
int *streamlength, int* frametype); |
unsigned char *hints_buffer, |
147 |
|
long *streamlength, long* frametype, long *hints_size); |
148 |
|
|
149 |
/***************************************************************************** |
/***************************************************************************** |
150 |
* Main function |
* Main function |
156 |
unsigned char *mp4_buffer = NULL; |
unsigned char *mp4_buffer = NULL; |
157 |
unsigned char *in_buffer = NULL; |
unsigned char *in_buffer = NULL; |
158 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
159 |
|
unsigned char *hints_buffer = NULL; |
160 |
|
|
161 |
double enctime; |
double enctime; |
162 |
double totalenctime=0.; |
double totalenctime=0.; |
163 |
|
|
164 |
long totalsize; |
long totalsize; |
165 |
|
long hints_size; |
166 |
int status; |
int status; |
167 |
int frame_type; |
long frame_type; |
168 |
int bigendian; |
long bigendian; |
169 |
|
|
170 |
int m4v_size; |
long m4v_size; |
171 |
int use_assembler=0; |
int use_assembler=0; |
172 |
|
|
173 |
char filename[256]; |
char filename[256]; |
174 |
|
|
175 |
FILE *in_file = stdin; |
FILE *in_file = stdin; |
176 |
FILE *out_file = NULL; |
FILE *out_file = NULL; |
177 |
|
FILE *hints_file = NULL; |
178 |
|
|
179 |
printf("xvid_decraw - raw mpeg4 bitstream encoder "); |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
180 |
printf("written by Christoph Lampert 2002\n\n"); |
printf("written by Christoph Lampert 2002\n\n"); |
181 |
|
|
182 |
/***************************************************************************** |
/***************************************************************************** |
232 |
i++; |
i++; |
233 |
ARG_OUTPUTTYPE = atoi(argv[i]); |
ARG_OUTPUTTYPE = atoi(argv[i]); |
234 |
} |
} |
235 |
|
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
236 |
|
i++; |
237 |
|
ARG_HINTMODE = atoi(argv[i]); |
238 |
|
} |
239 |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
240 |
i++; |
i++; |
241 |
ARG_OUTPUTFILE = argv[i]; |
ARG_OUTPUTFILE = argv[i]; |
280 |
return -1; |
return -1; |
281 |
} |
} |
282 |
|
|
283 |
|
if ( ARG_HINTMODE != HINT_MODE_NONE && |
284 |
|
ARG_HINTMODE != HINT_MODE_GET && |
285 |
|
ARG_HINTMODE != HINT_MODE_SET) |
286 |
|
ARG_HINTMODE = HINT_MODE_NONE; |
287 |
|
|
288 |
|
if( ARG_HINTMODE != HINT_MODE_NONE) { |
289 |
|
char *rights = "rb"; |
290 |
|
|
291 |
|
/* |
292 |
|
* If we are getting hints from core, we will have to write them to |
293 |
|
* hint file |
294 |
|
*/ |
295 |
|
if(ARG_HINTMODE == HINT_MODE_GET) |
296 |
|
rights = "w+b"; |
297 |
|
|
298 |
|
/* Open the hint file */ |
299 |
|
hints_file = fopen(HINT_FILE, rights); |
300 |
|
if(hints_file == NULL) { |
301 |
|
fprintf(stderr, "Error opening input file %s\n", HINT_FILE); |
302 |
|
return -1; |
303 |
|
} |
304 |
|
|
305 |
|
/* Allocate hint memory space, we will be using rawhints */ |
306 |
|
/* NB : Hope 1Mb is enough */ |
307 |
|
if((hints_buffer = malloc(1024*1024)) == NULL) { |
308 |
|
fprintf(stderr, "Memory allocation error\n"); |
309 |
|
return -1; |
310 |
|
} |
311 |
|
|
312 |
|
} |
313 |
|
|
314 |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
315 |
in_file = stdin; |
in_file = stdin; |
316 |
} |
} |
357 |
* Main loop |
* Main loop |
358 |
****************************************************************************/ |
****************************************************************************/ |
359 |
|
|
360 |
|
totalsize = LONG_PACK('M','P','4','U'); |
361 |
|
if(*((char *)(&totalsize)) == 'M') |
362 |
|
bigendian = 1; |
363 |
|
else |
364 |
|
bigendian = 0; |
365 |
|
|
366 |
if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { |
if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { |
367 |
|
|
368 |
if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) |
if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) |
377 |
|
|
378 |
/* Write header */ |
/* Write header */ |
379 |
if (ARG_OUTPUTTYPE) { |
if (ARG_OUTPUTTYPE) { |
|
char *ptr; |
|
|
long test; |
|
380 |
|
|
381 |
test = LONG_PACK('M','P','4','U'); |
long test = LONG_PACK('M','P','4','U'); |
|
ptr = (unsigned char *)&test; |
|
|
if(*ptr == 'M') |
|
|
bigendian = 1; |
|
|
else |
|
|
bigendian = 0; |
|
382 |
|
|
383 |
test = (!bigendian)?SWAP(test):test; |
test = (!bigendian)?SWAP(test):test; |
384 |
|
|
400 |
do { |
do { |
401 |
|
|
402 |
if (ARG_INPUTTYPE) |
if (ARG_INPUTTYPE) |
403 |
status = read_pgmdata(in_file, in_buffer); // read PGM data (YUV-format) |
status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */ |
404 |
else |
else |
405 |
status = read_yuvdata(in_file, in_buffer); // read raw data (YUV-format) |
status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */ |
406 |
|
|
407 |
if (status) |
if (status) |
408 |
{ |
{ |
411 |
} |
} |
412 |
|
|
413 |
/***************************************************************************** |
/***************************************************************************** |
414 |
|
* Read hints from file |
415 |
|
****************************************************************************/ |
416 |
|
|
417 |
|
if(ARG_HINTMODE == HINT_MODE_SET) { |
418 |
|
fread(&hints_size, 1, sizeof(long), hints_file); |
419 |
|
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
420 |
|
fread(hints_buffer, 1, hints_size, hints_file); |
421 |
|
} |
422 |
|
|
423 |
|
/***************************************************************************** |
424 |
* Encode and decode this frame |
* Encode and decode this frame |
425 |
****************************************************************************/ |
****************************************************************************/ |
426 |
|
|
427 |
enctime = msecond(); |
enctime = msecond(); |
428 |
status = enc_main(in_buffer, mp4_buffer, &m4v_size, &frame_type); |
status = enc_main(in_buffer, mp4_buffer, hints_buffer, |
429 |
|
&m4v_size, &frame_type, &hints_size); |
430 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
431 |
|
|
432 |
totalenctime += enctime; |
totalenctime += enctime; |
435 |
printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", |
printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", |
436 |
(int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); |
(int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); |
437 |
|
|
438 |
|
/***************************************************************************** |
439 |
|
* Save hints to file |
440 |
|
****************************************************************************/ |
441 |
|
|
442 |
|
if(ARG_HINTMODE == HINT_MODE_GET) { |
443 |
|
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
444 |
|
fwrite(&hints_size, 1, sizeof(long), hints_file); |
445 |
|
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
446 |
|
fwrite(hints_buffer, 1, hints_size, hints_file); |
447 |
|
} |
448 |
|
|
449 |
|
/***************************************************************************** |
450 |
|
* Save stream to file |
451 |
|
****************************************************************************/ |
452 |
|
|
453 |
if (ARG_SAVEMPEGSTREAM) |
if (ARG_SAVEMPEGSTREAM) |
454 |
{ |
{ |
455 |
/* Save single files */ |
/* Save single files */ |
507 |
fprintf(stderr, "Encore RELEASE problem return value %d\n", status); |
fprintf(stderr, "Encore RELEASE problem return value %d\n", status); |
508 |
} |
} |
509 |
|
|
510 |
|
if(in_file) |
511 |
fclose(in_file); |
fclose(in_file); |
512 |
if(out_file) |
if(out_file) |
513 |
fclose(out_file); |
fclose(out_file); |
514 |
|
if(hints_file) |
515 |
|
fclose(hints_file); |
516 |
|
|
517 |
free_all_memory: |
free_all_memory: |
518 |
free(out_buffer); |
free(out_buffer); |
519 |
free(mp4_buffer); |
free(mp4_buffer); |
520 |
free(in_buffer); |
free(in_buffer); |
521 |
|
if(hints_buffer) free(hints_buffer); |
522 |
|
|
523 |
return 0; |
return 0; |
524 |
|
|
536 |
/* Return time elapsed time in miliseconds since the program started */ |
/* Return time elapsed time in miliseconds since the program started */ |
537 |
static double msecond() |
static double msecond() |
538 |
{ |
{ |
539 |
#ifndef _MSC_VER |
#ifndef WIN32 |
540 |
struct timeval tv; |
struct timeval tv; |
541 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
542 |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
554 |
static void usage() |
static void usage() |
555 |
{ |
{ |
556 |
|
|
557 |
fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n"); |
fprintf(stderr, "Usage : xvid_encraw [OPTIONS]\n"); |
558 |
fprintf(stderr, "Options :\n"); |
fprintf(stderr, "Options :\n"); |
559 |
|
fprintf(stderr, " -asm : use assembly code\n"); |
560 |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
561 |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
562 |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
565 |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
566 |
fprintf(stderr, " -n integer : number of frames to encode\n"); |
fprintf(stderr, " -n integer : number of frames to encode\n"); |
567 |
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
|
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
|
568 |
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
569 |
fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); |
fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); |
570 |
fprintf(stderr, " When this option is not used : one file per encoded frame\n"); |
fprintf(stderr, " When this option is not used : one file per encoded frame\n"); |
572 |
fprintf(stderr, " + stream.m4v with -mt 0\n"); |
fprintf(stderr, " + stream.m4v with -mt 0\n"); |
573 |
fprintf(stderr, " + stream.mp4u with -mt 1\n"); |
fprintf(stderr, " + stream.mp4u with -mt 1\n"); |
574 |
fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); |
fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); |
575 |
|
fprintf(stderr, " -mv integer : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n"); |
576 |
fprintf(stderr, " -help : prints this help message\n"); |
fprintf(stderr, " -help : prints this help message\n"); |
577 |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
578 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
718 |
} |
} |
719 |
|
|
720 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
721 |
int *streamlength, int* frametype) |
unsigned char* hints_buffer, |
722 |
|
long *streamlength, long *frametype, long *hints_size) |
723 |
{ |
{ |
724 |
int xerr; |
int xerr; |
725 |
|
|
727 |
XVID_ENC_STATS xstats; |
XVID_ENC_STATS xstats; |
728 |
|
|
729 |
xframe.bitstream = bitstream; |
xframe.bitstream = bitstream; |
730 |
xframe.length = -1; // this is written by the routine |
xframe.length = -1; /* this is written by the routine */ |
731 |
|
|
732 |
xframe.image = image; |
xframe.image = image; |
733 |
xframe.colorspace = XVID_CSP_YV12; // defined in <xvid.h> |
xframe.colorspace = XVID_CSP_YV12; /* defined in <xvid.h> */ |
734 |
|
|
735 |
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) */ |
736 |
|
|
737 |
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) */ |
738 |
|
|
739 |
xframe.motion = motion_presets[ARG_QUALITY]; |
xframe.motion = motion_presets[ARG_QUALITY]; |
740 |
xframe.general = general_presets[ARG_QUALITY]; |
xframe.general = general_presets[ARG_QUALITY]; |
741 |
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
742 |
|
|
743 |
|
xframe.hint.hintstream = hints_buffer; |
744 |
|
|
745 |
|
if(ARG_HINTMODE == HINT_MODE_SET) { |
746 |
|
xframe.hint.hintlength = *hints_size; |
747 |
|
xframe.hint.rawhints = 0; |
748 |
|
xframe.general |= XVID_HINTEDME_SET; |
749 |
|
} |
750 |
|
|
751 |
|
if(ARG_HINTMODE == HINT_MODE_GET) { |
752 |
|
xframe.hint.rawhints = 0; |
753 |
|
xframe.general |= XVID_HINTEDME_GET; |
754 |
|
} |
755 |
|
|
756 |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
757 |
|
|
758 |
|
if(ARG_HINTMODE == HINT_MODE_GET) |
759 |
|
*hints_size = xframe.hint.hintlength; |
760 |
|
|
761 |
/* |
/* |
762 |
* 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 |
763 |
* interested in any of this, you can use NULL instead of &xstats |
* interested in any of this, you can use NULL instead of &xstats |