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.5 2002-11-27 21:09:10 edgomez Exp $ |
* $Id: xvid_encraw.c,v 1.7 2002-12-26 14:36:06 edgomez Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
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 |
} |
} |
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 |
|
|
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 |
|
|
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 |