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.11 2003-02-16 05:11:39 suxen_drol Exp $ |
* $Id: xvid_encraw.c,v 1.11.2.1 2003-03-09 00:28:09 edgomez Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
50 |
* Quality presets |
* Quality presets |
51 |
****************************************************************************/ |
****************************************************************************/ |
52 |
|
|
53 |
static int const motion_presets[7] = { |
static xvid_motion_t const motion_presets[] = { |
54 |
0, /* Q 0 */ |
0, |
55 |
PMV_EARLYSTOP16, /* Q 1 */ |
PMV_HALFPELREFINE16, |
56 |
PMV_EARLYSTOP16, /* Q 2 */ |
PMV_HALFPELREFINE16, |
57 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */ |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8, |
58 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */ |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16, |
59 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */ |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16, |
|
PMV_HALFPELREFINE8, |
|
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ |
|
|
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
|
60 |
}; |
}; |
61 |
|
|
62 |
static int const general_presets[7] = { |
static xvid_vol_t const vol_presets[] = { |
63 |
XVID_H263QUANT, /* Q 0 */ |
XVID_MPEGQUANT, |
64 |
XVID_MPEGQUANT, /* Q 1 */ |
0, |
65 |
XVID_H263QUANT, /* Q 2 */ |
0, |
66 |
XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */ |
XVID_QUARTERPEL, |
67 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */ |
XVID_QUARTERPEL | XVID_GMC, |
68 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */ |
XVID_QUARTERPEL | XVID_GMC |
|
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */ |
|
69 |
}; |
}; |
70 |
|
|
71 |
|
static xvid_vop_t const vop_presets[] = { |
72 |
|
XVID_DYNAMIC_BFRAMES, |
73 |
|
XVID_DYNAMIC_BFRAMES, |
74 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL, |
75 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V, |
76 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED, |
77 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED | XVID_MODEDECISION_BITS |
78 |
|
}; |
79 |
|
|
80 |
/***************************************************************************** |
/***************************************************************************** |
81 |
* Command line global variables |
* Command line global variables |
84 |
/* Maximum number of frames to encode */ |
/* Maximum number of frames to encode */ |
85 |
#define ABS_MAXFRAMENR 9999 |
#define ABS_MAXFRAMENR 9999 |
86 |
|
|
|
/* HINTMODEs */ |
|
|
#define HINT_MODE_NONE 0 |
|
|
#define HINT_MODE_GET 1 |
|
|
#define HINT_MODE_SET 2 |
|
|
#define HINT_FILE "hints.mv" |
|
|
|
|
87 |
static int ARG_BITRATE = 900; |
static int ARG_BITRATE = 900; |
88 |
static int ARG_QUANTI = 0; |
static int ARG_QUANTI = 0; |
89 |
static int ARG_QUALITY = 6; |
static int ARG_QUALITY = 5; |
90 |
static int ARG_MINQUANT = 1; |
static int ARG_MINQUANT = 1; |
91 |
static int ARG_MAXQUANT = 31; |
static int ARG_MAXQUANT = 31; |
92 |
static float ARG_FRAMERATE = 25.00f; |
static float ARG_FRAMERATE = 25.00f; |
95 |
static int ARG_INPUTTYPE = 0; |
static int ARG_INPUTTYPE = 0; |
96 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
97 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
|
static int ARG_HINTMODE = HINT_MODE_NONE; |
|
98 |
static int XDIM = 0; |
static int XDIM = 0; |
99 |
static int YDIM = 0; |
static int YDIM = 0; |
100 |
static int ARG_BQRATIO = 120; |
static int ARG_BQRATIO = 150; |
101 |
static int ARG_BQOFFSET = 0; |
static int ARG_BQOFFSET = 100; |
102 |
static int ARG_MAXBFRAMES = 0; |
static int ARG_MAXBFRAMES = 0; |
103 |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
104 |
|
|
105 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
106 |
#define SMALL_EPS 1e-10 |
#define SMALL_EPS (1e-10) |
107 |
|
|
108 |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
109 |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
138 |
/* Encoder related functions */ |
/* Encoder related functions */ |
139 |
static int enc_init(int use_assembler); |
static int enc_init(int use_assembler); |
140 |
static int enc_stop(); |
static int enc_stop(); |
141 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, |
142 |
unsigned char *hints_buffer, |
unsigned char* bitstream, |
143 |
long *streamlength, long* frametype, long *hints_size); |
long *frametype); |
144 |
|
|
145 |
/***************************************************************************** |
/***************************************************************************** |
146 |
* Main function |
* Main function |
152 |
unsigned char *mp4_buffer = NULL; |
unsigned char *mp4_buffer = NULL; |
153 |
unsigned char *in_buffer = NULL; |
unsigned char *in_buffer = NULL; |
154 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
|
unsigned char *hints_buffer = NULL; |
|
155 |
|
|
156 |
double enctime; |
double enctime; |
157 |
double totalenctime=0.; |
double totalenctime=0.; |
158 |
|
|
159 |
long totalsize; |
long totalsize; |
|
long hints_size; |
|
160 |
int status; |
int status; |
161 |
long frame_type; |
long frame_type; |
|
long bigendian; |
|
162 |
|
|
163 |
long m4v_size; |
long m4v_size; |
164 |
int use_assembler=0; |
int use_assembler=0; |
167 |
|
|
168 |
FILE *in_file = stdin; |
FILE *in_file = stdin; |
169 |
FILE *out_file = NULL; |
FILE *out_file = NULL; |
|
FILE *hints_file = NULL; |
|
170 |
|
|
171 |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
172 |
printf("written by Christoph Lampert 2002-2003\n\n"); |
printf("written by Christoph Lampert 2002-2003\n\n"); |
232 |
i++; |
i++; |
233 |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
234 |
} |
} |
|
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_HINTMODE = atoi(argv[i]); |
|
|
} |
|
235 |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
236 |
i++; |
i++; |
237 |
ARG_OUTPUTFILE = argv[i]; |
ARG_OUTPUTFILE = argv[i]; |
256 |
ARG_INPUTTYPE = 1; /* pgm */ |
ARG_INPUTTYPE = 1; /* pgm */ |
257 |
} |
} |
258 |
|
|
259 |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 5) { |
260 |
fprintf(stderr,"Wrong Quality\n"); |
fprintf(stderr,"Wrong Quality\n"); |
261 |
return -1; |
return(-1); |
262 |
} |
} |
263 |
|
|
264 |
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
265 |
fprintf(stderr,"Wrong Bitrate\n"); |
fprintf(stderr,"Wrong Bitrate\n"); |
266 |
return -1; |
return(-1); |
267 |
} |
} |
268 |
|
|
269 |
if ( ARG_FRAMERATE <= 0) { |
if ( ARG_FRAMERATE <= 0) { |
270 |
fprintf(stderr,"Wrong Framerate %s \n",argv[5]); |
fprintf(stderr,"Wrong Framerate %s \n",argv[5]); |
271 |
return -1; |
return(-1); |
272 |
} |
} |
273 |
|
|
274 |
if ( ARG_MAXFRAMENR <= 0) { |
if ( ARG_MAXFRAMENR <= 0) { |
275 |
fprintf(stderr,"Wrong number of frames\n"); |
fprintf(stderr,"Wrong number of frames\n"); |
276 |
return -1; |
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; |
|
|
} |
|
|
|
|
277 |
} |
} |
278 |
|
|
279 |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
284 |
in_file = fopen(ARG_INPUTFILE, "rb"); |
in_file = fopen(ARG_INPUTFILE, "rb"); |
285 |
if (in_file == NULL) { |
if (in_file == NULL) { |
286 |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
287 |
return -1; |
return(-1); |
288 |
} |
} |
289 |
} |
} |
290 |
|
|
291 |
if (ARG_INPUTTYPE) { |
if (ARG_INPUTTYPE) { |
292 |
if (read_pgmheader(in_file)) { |
if (read_pgmheader(in_file)) { |
293 |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
294 |
return -1; |
return(-1); |
295 |
} |
} |
296 |
} |
} |
297 |
|
|
353 |
} |
} |
354 |
|
|
355 |
/***************************************************************************** |
/***************************************************************************** |
|
* 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); |
|
|
} |
|
|
|
|
|
/***************************************************************************** |
|
356 |
* Encode and decode this frame |
* Encode and decode this frame |
357 |
****************************************************************************/ |
****************************************************************************/ |
358 |
|
|
359 |
enctime = msecond(); |
enctime = msecond(); |
360 |
status = enc_main(in_buffer, mp4_buffer, hints_buffer, |
m4v_size = enc_main(in_buffer, mp4_buffer, &frame_type); |
|
&m4v_size, &frame_type, &hints_size); |
|
361 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
362 |
|
|
363 |
/* if it's a not coded VOP (aka NVOP) then we write nothing */ |
/* Not coded frames return 0 */ |
364 |
if(frame_type == 5) goto next_frame; |
if(m4v_size == 0) goto next_frame; |
365 |
|
|
366 |
{ |
{ |
367 |
char *type[] = {"P", "I", "B", "S", "Packed", "N", "Unknown"}; |
char *type; |
368 |
|
|
369 |
if(frame_type<0 || frame_type>5) frame_type = 6; |
switch(frame_type) { |
370 |
|
case XVID_TYPE_IVOP: |
371 |
|
type = "I"; |
372 |
|
break; |
373 |
|
case XVID_TYPE_PVOP: |
374 |
|
type = "P"; |
375 |
|
break; |
376 |
|
case XVID_TYPE_BVOP: |
377 |
|
type = "B"; |
378 |
|
break; |
379 |
|
case XVID_TYPE_SVOP: |
380 |
|
type = "S"; |
381 |
|
break; |
382 |
|
default: |
383 |
|
type = "Unknown"; |
384 |
|
break; |
385 |
|
} |
386 |
|
|
387 |
printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n", |
printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n", |
388 |
(int)filenr, type[frame_type], (float)enctime, (int)m4v_size); |
(int)filenr, type, (float)enctime, (int)m4v_size); |
389 |
|
|
390 |
} |
} |
391 |
|
|
394 |
totalsize += m4v_size; |
totalsize += m4v_size; |
395 |
|
|
396 |
/***************************************************************************** |
/***************************************************************************** |
|
* 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); |
|
|
} |
|
|
|
|
|
/***************************************************************************** |
|
397 |
* Save stream to file |
* Save stream to file |
398 |
****************************************************************************/ |
****************************************************************************/ |
399 |
|
|
453 |
fclose(in_file); |
fclose(in_file); |
454 |
if(out_file) |
if(out_file) |
455 |
fclose(out_file); |
fclose(out_file); |
|
if(hints_file) |
|
|
fclose(hints_file); |
|
456 |
|
|
457 |
free_all_memory: |
free_all_memory: |
458 |
free(out_buffer); |
free(out_buffer); |
459 |
free(mp4_buffer); |
free(mp4_buffer); |
460 |
free(in_buffer); |
free(in_buffer); |
|
if(hints_buffer) free(hints_buffer); |
|
461 |
|
|
462 |
return 0; |
return(0); |
463 |
|
|
464 |
} |
} |
465 |
|
|
478 |
#ifndef WIN32 |
#ifndef WIN32 |
479 |
struct timeval tv; |
struct timeval tv; |
480 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
481 |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
return(tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3); |
482 |
#else |
#else |
483 |
clock_t clk; |
clock_t clk; |
484 |
clk = clock(); |
clk = clock(); |
485 |
return clk * 1000 / CLOCKS_PER_SEC; |
return(clk * 1000 / CLOCKS_PER_SEC); |
486 |
#endif |
#endif |
487 |
} |
} |
488 |
|
|
498 |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
499 |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
500 |
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"); |
|
501 |
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
502 |
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
503 |
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
510 |
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"); |
511 |
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"); |
512 |
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"); |
513 |
fprintf(stderr, " When this option is used : save to 'string' (default=stream.m4v)\n"); |
fprintf(stderr, " When this option is used : save to 'string' file\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"); |
|
514 |
fprintf(stderr, " -help : prints this help message\n"); |
fprintf(stderr, " -help : prints this help message\n"); |
515 |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
516 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
533 |
bytes = fread(dummy,1,2,handle); |
bytes = fread(dummy,1,2,handle); |
534 |
|
|
535 |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
536 |
return 1; |
return(1); |
537 |
|
|
538 |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
539 |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
540 |
{ |
{ |
541 |
fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
542 |
return 2; |
return(2); |
543 |
} |
} |
544 |
if ( (XDIM==0) || (YDIM==0) ) |
if ( (XDIM==0) || (YDIM==0) ) |
545 |
{ |
{ |
547 |
YDIM=ysize*2/3; |
YDIM=ysize*2/3; |
548 |
} |
} |
549 |
|
|
550 |
return 0; |
return(0); |
551 |
} |
} |
552 |
|
|
553 |
static int read_pgmdata(FILE* handle, unsigned char *image) |
static int read_pgmdata(FILE* handle, unsigned char *image) |
578 |
/* I don't know why, but this seems needed */ |
/* I don't know why, but this seems needed */ |
579 |
fread(&dummy, 1, 1, handle); |
fread(&dummy, 1, 1, handle); |
580 |
|
|
581 |
return 0; |
return(0); |
582 |
} |
} |
583 |
|
|
584 |
static int read_yuvdata(FILE* handle, unsigned char *image) |
static int read_yuvdata(FILE* handle, unsigned char *image) |
585 |
{ |
{ |
586 |
|
|
587 |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
588 |
return 1; |
return(1); |
589 |
else |
else |
590 |
return 0; |
return(0); |
591 |
} |
} |
592 |
|
|
593 |
/***************************************************************************** |
/***************************************************************************** |
601 |
{ |
{ |
602 |
int xerr; |
int xerr; |
603 |
|
|
604 |
XVID_INIT_PARAM xinit; |
xvid_gbl_init_t xvid_gbl_init; |
605 |
XVID_ENC_PARAM xparam; |
xvid_enc_create_t xvid_enc_create; |
606 |
|
|
607 |
|
/*------------------------------------------------------------------------ |
608 |
|
* XviD core initialization |
609 |
|
*----------------------------------------------------------------------*/ |
610 |
|
|
611 |
|
/* Set version -- version checking will done by xvidcore*/ |
612 |
|
xvid_gbl_init.version = XVID_VERSION; |
613 |
|
|
614 |
|
/* Do we have to enable ASM optimizations ? */ |
615 |
if(use_assembler) { |
if(use_assembler) { |
616 |
|
|
617 |
#ifdef ARCH_IS_IA64 |
#ifdef ARCH_IS_IA64 |
618 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
619 |
#else |
#else |
620 |
xinit.cpu_flags = 0; |
xvid_gbl_init.cpu_flags = 0; |
621 |
#endif |
#endif |
622 |
} |
} |
623 |
else { |
else { |
624 |
xinit.cpu_flags = XVID_CPU_FORCE; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
625 |
} |
} |
626 |
|
|
627 |
xvid_init(NULL, 0, &xinit, NULL); |
/* Initialize XviD core -- Should be done once per __process__ */ |
628 |
|
xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); |
629 |
|
|
630 |
xparam.width = XDIM; |
/*------------------------------------------------------------------------ |
631 |
xparam.height = YDIM; |
* XviD encoder initialization |
632 |
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) |
*----------------------------------------------------------------------*/ |
633 |
{ |
|
634 |
xparam.fincr = 1; |
/* Version again */ |
635 |
xparam.fbase = (int)ARG_FRAMERATE; |
xvid_enc_create.version = XVID_VERSION; |
636 |
} |
|
637 |
else |
/* Width and Height of input frames */ |
638 |
{ |
xvid_enc_create.width = XDIM; |
639 |
xparam.fincr = FRAMERATE_INCR; |
xvid_enc_create.height = YDIM; |
640 |
xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
|
641 |
|
/* No fancy thread tests */ |
642 |
|
xvid_enc_create.num_threads = 0; |
643 |
|
|
644 |
|
/* Frame rate - Do some quick float fps = fincr/fbase hack */ |
645 |
|
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) { |
646 |
|
xvid_enc_create.fincr = 1; |
647 |
|
xvid_enc_create.fbase = (int)ARG_FRAMERATE; |
648 |
|
} else { |
649 |
|
xvid_enc_create.fincr = FRAMERATE_INCR; |
650 |
|
xvid_enc_create.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
651 |
} |
} |
652 |
xparam.rc_reaction_delay_factor = 16; |
|
653 |
xparam.rc_averaging_period = 100; |
/* Maximum key frame interval */ |
654 |
xparam.rc_buffer = 10; |
xvid_enc_create.max_key_interval = (int)ARG_FRAMERATE*10; |
655 |
xparam.rc_bitrate = ARG_BITRATE*1000; |
|
656 |
xparam.min_quantizer = ARG_MINQUANT; |
/* Bframes settings */ |
657 |
xparam.max_quantizer = ARG_MAXQUANT; |
xvid_enc_create.max_bframes = ARG_MAXBFRAMES; |
658 |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
xvid_enc_create.bquant_ratio = ARG_BQRATIO; |
659 |
xparam.bquant_ratio = ARG_BQRATIO; |
xvid_enc_create.bquant_offset = ARG_BQOFFSET; |
660 |
xparam.bquant_offset = ARG_BQOFFSET; |
|
661 |
xparam.max_bframes = ARG_MAXBFRAMES; |
/* Dropping ratio frame -- we don't need that */ |
662 |
xparam.frame_drop_ratio = 0; |
xvid_enc_create.frame_drop_ratio = 0; |
663 |
xparam.global = 0; |
|
664 |
|
/* Global encoder options */ |
665 |
|
xvid_enc_create.global = 0; |
666 |
|
|
667 |
/* 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 */ |
668 |
|
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
669 |
|
|
670 |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL); |
/* Retrieve the encoder instance from the structure */ |
671 |
enc_handle=xparam.handle; |
enc_handle = xvid_enc_create.handle; |
672 |
|
|
673 |
return xerr; |
return(xerr); |
674 |
} |
} |
675 |
|
|
676 |
static int enc_stop() |
static int enc_stop() |
677 |
{ |
{ |
678 |
int xerr; |
int xerr; |
679 |
|
|
680 |
|
/* Destroy the encoder instance */ |
681 |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
|
return xerr; |
|
682 |
|
|
683 |
|
return(xerr); |
684 |
} |
} |
685 |
|
|
686 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, |
687 |
unsigned char* hints_buffer, |
unsigned char* bitstream, |
688 |
long *streamlength, long *frametype, long *hints_size) |
long *frametype) |
689 |
{ |
{ |
690 |
int xerr; |
int ret; |
691 |
|
|
692 |
XVID_ENC_FRAME xframe; |
xvid_enc_frame_t xvid_enc_frame; |
693 |
XVID_ENC_STATS xstats; |
xvid_enc_stats_t xvid_enc_stats[2]; |
694 |
|
|
695 |
xframe.bitstream = bitstream; |
/* Version for the frame and the stats */ |
696 |
xframe.length = -1; /* this is written by the routine */ |
xvid_enc_frame.version = XVID_VERSION; |
697 |
|
xvid_enc_stats[0].version = XVID_VERSION; |
698 |
|
xvid_enc_stats[1].version = XVID_VERSION; |
699 |
|
|
700 |
xframe.image = image; |
/* Bind output buffer */ |
701 |
xframe.colorspace = XVID_CSP_YV12; /* defined in <xvid.h> */ |
xvid_enc_frame.bitstream = bitstream; |
702 |
|
xvid_enc_frame.length = -1; |
703 |
|
|
704 |
xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */ |
/* Initialize input image fields */ |
705 |
|
xvid_enc_frame.input.plane[0] = image; |
706 |
|
xvid_enc_frame.input.csp = XVID_CSP_I420; |
707 |
|
xvid_enc_frame.input.stride[0] = XDIM; |
708 |
|
|
709 |
xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */ |
/* Set up core's general features */ |
710 |
xframe.bquant = 0; |
xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; |
711 |
|
|
712 |
xframe.motion = motion_presets[ARG_QUALITY]; |
/* Set up core's general features */ |
713 |
xframe.general = general_presets[ARG_QUALITY]; |
xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; |
|
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
|
|
xframe.stride = XDIM; |
|
714 |
|
|
715 |
xframe.hint.hintstream = hints_buffer; |
/* Frame type -- let core decide for us */ |
716 |
|
xvid_enc_frame.type = XVID_TYPE_AUTO; |
717 |
|
|
718 |
if(ARG_HINTMODE == HINT_MODE_SET) { |
/* Force the right quantizer */ |
719 |
xframe.hint.hintlength = *hints_size; |
xvid_enc_frame.quant = ARG_QUANTI; |
720 |
xframe.hint.rawhints = 0; |
xvid_enc_frame.bquant = 0; |
|
xframe.general |= XVID_HINTEDME_SET; |
|
|
} |
|
721 |
|
|
722 |
if(ARG_HINTMODE == HINT_MODE_GET) { |
/* Set up motion estimation flags */ |
723 |
xframe.hint.rawhints = 0; |
xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; |
|
xframe.general |= XVID_HINTEDME_GET; |
|
|
} |
|
724 |
|
|
725 |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
/* We don't use special matrices */ |
726 |
|
xvid_enc_frame.quant_intra_matrix = NULL; |
727 |
|
xvid_enc_frame.quant_inter_matrix = NULL; |
728 |
|
|
729 |
if(ARG_HINTMODE == HINT_MODE_GET) |
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
|
*hints_size = xframe.hint.hintlength; |
|
730 |
|
|
731 |
/* |
*frametype = xvid_enc_stats[0].type; |
|
* 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 |
|
|
*/ |
|
|
*frametype = xframe.intra; |
|
|
*streamlength = xframe.length; |
|
732 |
|
|
733 |
return xerr; |
return(ret); |
734 |
} |
} |