36 |
|
|
37 |
#include <stdio.h> |
#include <stdio.h> |
38 |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <string.h> |
|
39 |
#include <math.h> |
#include <math.h> |
40 |
#ifndef _MSC_VER |
#ifndef _MSC_VER |
41 |
#include <sys/time.h> |
#include <sys/time.h> |
50 |
****************************************************************************/ |
****************************************************************************/ |
51 |
|
|
52 |
static int const motion_presets[7] = { |
static int const motion_presets[7] = { |
53 |
0, /* Q 0 */ |
0, // Q 0 |
54 |
PMV_EARLYSTOP16, /* Q 1 */ |
PMV_EARLYSTOP16, // Q 1 |
55 |
PMV_EARLYSTOP16, /* Q 2 */ |
PMV_EARLYSTOP16, // Q 2 |
56 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */ |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 3 |
57 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */ |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 4 |
58 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */ |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | // Q 5 |
59 |
PMV_HALFPELREFINE8, |
PMV_HALFPELREFINE8, |
60 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | // Q 6 |
61 |
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
62 |
}; |
}; |
63 |
|
|
64 |
static int const general_presets[7] = { |
static int const general_presets[7] = { |
65 |
XVID_H263QUANT, /* Q 0 */ |
XVID_H263QUANT, // Q 0 |
66 |
XVID_MPEGQUANT, /* Q 1 */ |
XVID_MPEGQUANT, // Q 1 |
67 |
XVID_H263QUANT, /* Q 2 */ |
XVID_H263QUANT, // Q 2 |
68 |
XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */ |
XVID_H263QUANT | XVID_HALFPEL, // Q 3 |
69 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */ |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 4 |
70 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */ |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 5 |
71 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */ |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V // Q 6 |
72 |
}; |
}; |
73 |
|
|
74 |
|
|
79 |
/* Maximum number of frames to encode */ |
/* Maximum number of frames to encode */ |
80 |
#define ABS_MAXFRAMENR 9999 |
#define ABS_MAXFRAMENR 9999 |
81 |
|
|
|
/* HINTMODEs */ |
|
|
#define HINT_MODE_NONE 0 |
|
|
#define HINT_MODE_GET 1 |
|
|
#define HINT_MODE_SET 2 |
|
|
#define HINT_FILE "hints.mv" |
|
|
|
|
82 |
static int ARG_BITRATE = 900; |
static int ARG_BITRATE = 900; |
83 |
static int ARG_QUANTI = 0; |
static int ARG_QUANTI = 0; |
84 |
static int ARG_QUALITY = 6; |
static int ARG_QUALITY = 6; |
91 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
92 |
static int ARG_OUTPUTTYPE = 0; |
static int ARG_OUTPUTTYPE = 0; |
93 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
|
static int ARG_HINTMODE = HINT_MODE_NONE; |
|
94 |
static int XDIM = 0; |
static int XDIM = 0; |
95 |
static int YDIM = 0; |
static int YDIM = 0; |
96 |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
135 |
static int enc_init(int use_assembler); |
static int enc_init(int use_assembler); |
136 |
static int enc_stop(); |
static int enc_stop(); |
137 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
138 |
unsigned char *hints_buffer, |
int *streamlength, int* frametype); |
|
long *streamlength, long* frametype, long *hints_size); |
|
139 |
|
|
140 |
/***************************************************************************** |
/***************************************************************************** |
141 |
* Main function |
* Main function |
147 |
unsigned char *mp4_buffer = NULL; |
unsigned char *mp4_buffer = NULL; |
148 |
unsigned char *in_buffer = NULL; |
unsigned char *in_buffer = NULL; |
149 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
|
unsigned char *hints_buffer = NULL; |
|
150 |
|
|
151 |
double enctime; |
double enctime; |
152 |
double totalenctime=0.; |
double totalenctime=0.; |
153 |
|
|
154 |
long totalsize; |
long totalsize; |
|
long hints_size; |
|
155 |
int status; |
int status; |
156 |
long frame_type; |
int frame_type; |
157 |
long bigendian; |
int bigendian; |
158 |
|
|
159 |
long m4v_size; |
int m4v_size; |
160 |
int use_assembler=0; |
int use_assembler=0; |
161 |
|
|
162 |
char filename[256]; |
char filename[256]; |
163 |
|
|
164 |
FILE *in_file = stdin; |
FILE *in_file = stdin; |
165 |
FILE *out_file = NULL; |
FILE *out_file = NULL; |
|
FILE *hints_file = NULL; |
|
166 |
|
|
167 |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
printf("xvid_decraw - raw mpeg4 bitstream encoder "); |
168 |
printf("written by Christoph Lampert 2002\n\n"); |
printf("written by Christoph Lampert 2002\n\n"); |
169 |
|
|
170 |
/***************************************************************************** |
/***************************************************************************** |
220 |
i++; |
i++; |
221 |
ARG_OUTPUTTYPE = atoi(argv[i]); |
ARG_OUTPUTTYPE = atoi(argv[i]); |
222 |
} |
} |
|
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_HINTMODE = atoi(argv[i]); |
|
|
} |
|
223 |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
224 |
i++; |
i++; |
225 |
ARG_OUTPUTFILE = argv[i]; |
ARG_OUTPUTFILE = argv[i]; |
264 |
return -1; |
return -1; |
265 |
} |
} |
266 |
|
|
|
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; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
267 |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
268 |
in_file = stdin; |
in_file = stdin; |
269 |
} |
} |
310 |
* Main loop |
* Main loop |
311 |
****************************************************************************/ |
****************************************************************************/ |
312 |
|
|
|
totalsize = LONG_PACK('M','P','4','U'); |
|
|
if(*((char *)(&totalsize)) == 'M') |
|
|
bigendian = 1; |
|
|
else |
|
|
bigendian = 0; |
|
|
|
|
313 |
if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { |
if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { |
314 |
|
|
315 |
if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) |
if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) |
324 |
|
|
325 |
/* Write header */ |
/* Write header */ |
326 |
if (ARG_OUTPUTTYPE) { |
if (ARG_OUTPUTTYPE) { |
327 |
|
char *ptr; |
328 |
|
long test; |
329 |
|
|
330 |
long test = LONG_PACK('M','P','4','U'); |
test = LONG_PACK('M','P','4','U'); |
331 |
|
ptr = (unsigned char *)&test; |
332 |
|
if(*ptr == 'M') |
333 |
|
bigendian = 1; |
334 |
|
else |
335 |
|
bigendian = 0; |
336 |
|
|
337 |
test = (!bigendian)?SWAP(test):test; |
test = (!bigendian)?SWAP(test):test; |
338 |
|
|
354 |
do { |
do { |
355 |
|
|
356 |
if (ARG_INPUTTYPE) |
if (ARG_INPUTTYPE) |
357 |
status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */ |
status = read_pgmdata(in_file, in_buffer); // read PGM data (YUV-format) |
358 |
else |
else |
359 |
status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */ |
status = read_yuvdata(in_file, in_buffer); // read raw data (YUV-format) |
360 |
|
|
361 |
if (status) |
if (status) |
362 |
{ |
{ |
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; |
378 |
printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", |
printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", |
379 |
(int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); |
(int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); |
380 |
|
|
|
/***************************************************************************** |
|
|
* 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 |
|
|
****************************************************************************/ |
|
|
|
|
381 |
if (ARG_SAVEMPEGSTREAM) |
if (ARG_SAVEMPEGSTREAM) |
382 |
{ |
{ |
383 |
/* Save single files */ |
/* Save single files */ |
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 |
|
|
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 = 150; |
|
|
xparam.bquant_offset = 100; |
|
|
xparam.max_bframes = 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 |
|
|
649 |
XVID_ENC_STATS xstats; |
XVID_ENC_STATS xstats; |
650 |
|
|
651 |
xframe.bitstream = bitstream; |
xframe.bitstream = bitstream; |
652 |
xframe.length = -1; /* this is written by the routine */ |
xframe.length = -1; // this is written by the routine |
653 |
|
|
654 |
xframe.image = image; |
xframe.image = image; |
655 |
xframe.colorspace = XVID_CSP_YV12; /* defined in <xvid.h> */ |
xframe.colorspace = XVID_CSP_YV12; // defined in <xvid.h> |
656 |
|
|
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 |