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.10 2003-02-15 15:22:17 edgomez Exp $ |
* $Id: xvid_encraw.c,v 1.11.2.8 2003-03-16 12:04:13 suxen_drol Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
26 |
/***************************************************************************** |
/***************************************************************************** |
27 |
* Application notes : |
* Application notes : |
28 |
* |
* |
29 |
* A sequence of YUV pics in PGM file format is encoded and decoded |
* A sequence of raw YUV I420 pics or YUV I420 PGM file format is encoded |
30 |
* The speed is measured and PSNR of decoded picture is calculated. |
* The speed is measured and frames' PSNR are taken from core. |
31 |
* |
* |
32 |
* The program is plain C and needs no libraries except for libxvidcore, |
* The program is plain C and needs no libraries except for libxvidcore, |
33 |
* and maths-lib. |
* and maths-lib. |
46 |
|
|
47 |
#include "xvid.h" |
#include "xvid.h" |
48 |
|
|
49 |
|
|
50 |
/***************************************************************************** |
/***************************************************************************** |
51 |
* Quality presets |
* Quality presets |
52 |
****************************************************************************/ |
****************************************************************************/ |
53 |
|
|
54 |
static int const motion_presets[7] = { |
static xvid_motion_t const motion_presets[] = { |
55 |
0, /* Q 0 */ |
0, |
56 |
PMV_EARLYSTOP16, /* Q 1 */ |
PMV_HALFPELREFINE16, |
57 |
PMV_EARLYSTOP16, /* Q 2 */ |
PMV_HALFPELREFINE16, |
58 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */ |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8, |
59 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */ |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16, |
60 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */ |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16 | PMV_CHROMA16 | PMV_CHROMA8, |
|
PMV_HALFPELREFINE8, |
|
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ |
|
|
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
|
61 |
}; |
}; |
62 |
|
|
63 |
static int const general_presets[7] = { |
static xvid_vol_t const vol_presets[] = { |
64 |
XVID_H263QUANT, /* Q 0 */ |
XVID_MPEGQUANT, |
65 |
XVID_MPEGQUANT, /* Q 1 */ |
0, |
66 |
XVID_H263QUANT, /* Q 2 */ |
0, |
67 |
XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */ |
XVID_QUARTERPEL, |
68 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */ |
XVID_QUARTERPEL | XVID_GMC, |
69 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */ |
XVID_QUARTERPEL | XVID_GMC |
|
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */ |
|
70 |
}; |
}; |
71 |
|
|
72 |
|
static xvid_vop_t const vop_presets[] = { |
73 |
|
XVID_DYNAMIC_BFRAMES, |
74 |
|
XVID_DYNAMIC_BFRAMES, |
75 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL, |
76 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V, |
77 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED, |
78 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED | XVID_MODEDECISION_BITS |
79 |
|
}; |
80 |
|
|
81 |
/***************************************************************************** |
/***************************************************************************** |
82 |
* Command line global variables |
* Command line global variables |
85 |
/* Maximum number of frames to encode */ |
/* Maximum number of frames to encode */ |
86 |
#define ABS_MAXFRAMENR 9999 |
#define ABS_MAXFRAMENR 9999 |
87 |
|
|
88 |
/* HINTMODEs */ |
static int ARG_STATS = 0; |
89 |
#define HINT_MODE_NONE 0 |
static int ARG_DUMP = 0; |
90 |
#define HINT_MODE_GET 1 |
static int ARG_LUMIMASKING = 0; |
|
#define HINT_MODE_SET 2 |
|
|
#define HINT_FILE "hints.mv" |
|
|
|
|
91 |
static int ARG_BITRATE = 900; |
static int ARG_BITRATE = 900; |
92 |
static int ARG_QUANTI = 0; |
static int ARG_QUANTI = 0; |
93 |
static int ARG_QUALITY = 6; |
static int ARG_QUALITY = 5; |
94 |
static int ARG_MINQUANT = 1; |
static int ARG_MINQUANT = 1; |
95 |
static int ARG_MAXQUANT = 31; |
static int ARG_MAXQUANT = 31; |
96 |
static float ARG_FRAMERATE = 25.00f; |
static float ARG_FRAMERATE = 25.00f; |
99 |
static int ARG_INPUTTYPE = 0; |
static int ARG_INPUTTYPE = 0; |
100 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
101 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
|
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 |
static int ARG_BQRATIO = 120; |
static int ARG_BQRATIO = 150; |
105 |
static int ARG_BQOFFSET = 0; |
static int ARG_BQOFFSET = 100; |
106 |
static int ARG_MAXBFRAMES = 0; |
static int ARG_MAXBFRAMES = 0; |
107 |
|
static int ARG_PACKED = 0; |
108 |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
109 |
|
|
110 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
111 |
#define SMALL_EPS 1e-10 |
#define SMALL_EPS (1e-10) |
112 |
|
|
113 |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
114 |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
117 |
* Nasty global vars ;-) |
* Nasty global vars ;-) |
118 |
***************************************************************************/ |
***************************************************************************/ |
119 |
|
|
120 |
static int i,filenr = 0; |
static int i; |
121 |
|
|
122 |
/* the path where to save output */ |
/* the path where to save output */ |
123 |
static char filepath[256] = "./"; |
static char filepath[256] = "./"; |
143 |
/* Encoder related functions */ |
/* Encoder related functions */ |
144 |
static int enc_init(int use_assembler); |
static int enc_init(int use_assembler); |
145 |
static int enc_stop(); |
static int enc_stop(); |
146 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, |
147 |
unsigned char *hints_buffer, |
unsigned char* bitstream, |
148 |
long *streamlength, long* frametype, long *hints_size); |
int *key, |
149 |
|
int *stats_type, |
150 |
|
int *stats_quant, |
151 |
|
int *stats_length, |
152 |
|
int stats[3]); |
153 |
|
|
154 |
/***************************************************************************** |
/***************************************************************************** |
155 |
* Main function |
* Main function |
161 |
unsigned char *mp4_buffer = NULL; |
unsigned char *mp4_buffer = NULL; |
162 |
unsigned char *in_buffer = NULL; |
unsigned char *in_buffer = NULL; |
163 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
|
unsigned char *hints_buffer = NULL; |
|
164 |
|
|
165 |
double enctime; |
double enctime; |
166 |
double totalenctime=0.; |
double totalenctime=0.; |
167 |
|
|
168 |
long totalsize; |
int totalsize; |
169 |
long hints_size; |
int result; |
170 |
int status; |
int m4v_size; |
171 |
long frame_type; |
int key; |
172 |
long bigendian; |
int stats_type; |
173 |
|
int stats_quant; |
174 |
long m4v_size; |
int stats_length; |
175 |
int use_assembler=0; |
int use_assembler=0; |
176 |
|
|
177 |
|
int input_num; |
178 |
|
int output_num; |
179 |
|
|
180 |
char filename[256]; |
char filename[256]; |
181 |
|
|
182 |
FILE *in_file = stdin; |
FILE *in_file = stdin; |
183 |
FILE *out_file = NULL; |
FILE *out_file = NULL; |
|
FILE *hints_file = NULL; |
|
184 |
|
|
185 |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
186 |
printf("written by Christoph Lampert 2002-2003\n\n"); |
printf("written by Christoph Lampert 2002-2003\n\n"); |
210 |
i++; |
i++; |
211 |
ARG_MAXBFRAMES = atoi(argv[i]); |
ARG_MAXBFRAMES = atoi(argv[i]); |
212 |
} |
} |
213 |
|
else if (strcmp("-p", argv[i]) == 0) { |
214 |
|
ARG_PACKED = 1; |
215 |
|
} |
216 |
else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { |
217 |
i++; |
i++; |
218 |
ARG_BQRATIO = atoi(argv[i]); |
ARG_BQRATIO = atoi(argv[i]); |
233 |
i++; |
i++; |
234 |
ARG_INPUTFILE = argv[i]; |
ARG_INPUTFILE = argv[i]; |
235 |
} |
} |
236 |
|
else if (strcmp("-s", argv[i]) == 0) { |
237 |
|
ARG_STATS = 1; |
238 |
|
} |
239 |
|
else if (strcmp("-dump", argv[i]) == 0) { |
240 |
|
ARG_DUMP = 1; |
241 |
|
} |
242 |
|
else if (strcmp("-lumimasking", argv[i]) == 0) { |
243 |
|
ARG_LUMIMASKING = 1; |
244 |
|
} |
245 |
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
246 |
i++; |
i++; |
247 |
ARG_INPUTTYPE = atoi(argv[i]); |
ARG_INPUTTYPE = atoi(argv[i]); |
254 |
i++; |
i++; |
255 |
ARG_QUANTI = atoi(argv[i]); |
ARG_QUANTI = atoi(argv[i]); |
256 |
} |
} |
257 |
else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-m", argv[i]) == 0) { |
258 |
i++; |
ARG_SAVEMPEGSTREAM = 1; |
|
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_HINTMODE = atoi(argv[i]); |
|
259 |
} |
} |
260 |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
261 |
i++; |
i++; |
281 |
ARG_INPUTTYPE = 1; /* pgm */ |
ARG_INPUTTYPE = 1; /* pgm */ |
282 |
} |
} |
283 |
|
|
284 |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 5) { |
285 |
fprintf(stderr,"Wrong Quality\n"); |
fprintf(stderr,"Wrong Quality\n"); |
286 |
return -1; |
return(-1); |
287 |
} |
} |
288 |
|
|
289 |
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
290 |
fprintf(stderr,"Wrong Bitrate\n"); |
fprintf(stderr,"Wrong Bitrate\n"); |
291 |
return -1; |
return(-1); |
292 |
} |
} |
293 |
|
|
294 |
if ( ARG_FRAMERATE <= 0) { |
if ( ARG_FRAMERATE <= 0) { |
295 |
fprintf(stderr,"Wrong Framerate %s \n",argv[5]); |
fprintf(stderr,"Wrong Framerate %s \n",argv[5]); |
296 |
return -1; |
return(-1); |
297 |
} |
} |
298 |
|
|
299 |
if ( ARG_MAXFRAMENR <= 0) { |
if ( ARG_MAXFRAMENR <= 0) { |
300 |
fprintf(stderr,"Wrong number of frames\n"); |
fprintf(stderr,"Wrong number of frames\n"); |
301 |
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; |
|
|
} |
|
|
|
|
302 |
} |
} |
303 |
|
|
304 |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
309 |
in_file = fopen(ARG_INPUTFILE, "rb"); |
in_file = fopen(ARG_INPUTFILE, "rb"); |
310 |
if (in_file == NULL) { |
if (in_file == NULL) { |
311 |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
312 |
return -1; |
return(-1); |
313 |
} |
} |
314 |
} |
} |
315 |
|
|
316 |
if (ARG_INPUTTYPE) { |
if (ARG_INPUTTYPE) { |
317 |
if (read_pgmheader(in_file)) { |
if (read_pgmheader(in_file)) { |
318 |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
319 |
return -1; |
return(-1); |
320 |
} |
} |
321 |
} |
} |
322 |
|
|
335 |
****************************************************************************/ |
****************************************************************************/ |
336 |
|
|
337 |
|
|
338 |
status = enc_init(use_assembler); |
result = enc_init(use_assembler); |
339 |
if (status) |
if (result) |
340 |
{ |
{ |
341 |
fprintf(stderr, "Encore INIT problem, return value %d\n", status); |
fprintf(stderr, "Encore INIT problem, return value %d\n", result); |
342 |
goto release_all; |
goto release_all; |
343 |
} |
} |
344 |
|
|
364 |
|
|
365 |
totalsize = 0; |
totalsize = 0; |
366 |
|
|
367 |
|
result = 0; |
368 |
|
|
369 |
|
input_num = 0; /* input frame counter */ |
370 |
|
output_num = 0; /* output frame counter */ |
371 |
|
|
372 |
do { |
do { |
373 |
|
|
374 |
if (ARG_INPUTTYPE) |
char *type; |
375 |
status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */ |
int stats[3]; |
|
else |
|
|
status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */ |
|
376 |
|
|
377 |
if (status) |
if (input_num >= ARG_MAXFRAMENR) |
378 |
{ |
{ |
379 |
/* Couldn't read image, most likely end-of-file */ |
result = 1; |
|
continue; |
|
380 |
} |
} |
381 |
|
|
382 |
/***************************************************************************** |
if (!result) |
383 |
* Read hints from file |
{ |
384 |
****************************************************************************/ |
if(ARG_INPUTTYPE) { |
385 |
|
/* read PGM data (YUV-format) */ |
386 |
if(ARG_HINTMODE == HINT_MODE_SET) { |
result = read_pgmdata(in_file, in_buffer); |
387 |
fread(&hints_size, 1, sizeof(long), hints_file); |
} else { |
388 |
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
/* read raw data (YUV-format) */ |
389 |
fread(hints_buffer, 1, hints_size, hints_file); |
result = read_yuvdata(in_file, in_buffer); |
390 |
|
} |
391 |
} |
} |
392 |
|
|
393 |
/***************************************************************************** |
/***************************************************************************** |
395 |
****************************************************************************/ |
****************************************************************************/ |
396 |
|
|
397 |
enctime = msecond(); |
enctime = msecond(); |
398 |
status = enc_main(in_buffer, mp4_buffer, hints_buffer, |
m4v_size = enc_main(!result?in_buffer:0, mp4_buffer, &key, &stats_type, &stats_quant, &stats_length, stats); |
|
&m4v_size, &frame_type, &hints_size); |
|
399 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
400 |
|
|
401 |
/* if it's a not coded VOP (aka NVOP) then we write nothing */ |
/* Write the Frame statistics */ |
|
if(frame_type == 5) goto next_frame; |
|
402 |
|
|
403 |
{ |
printf("%5d: key=%i, time(ms)=%6.1f, length=%7d", |
404 |
char *type[] = {"P", "I", "B", "S", "Packed", "N", "Unknown"}; |
!result?input_num:-1, |
405 |
|
key, |
406 |
|
(float)enctime, |
407 |
|
(int)m4v_size); |
408 |
|
|
409 |
|
if (stats_type > 0) { /* !XVID_TYPE_NOTHING */ |
410 |
|
|
411 |
if(frame_type<0 || frame_type>5) frame_type = 6; |
switch(stats_type) { |
412 |
|
case XVID_TYPE_IVOP: |
413 |
|
type = "I"; |
414 |
|
break; |
415 |
|
case XVID_TYPE_PVOP: |
416 |
|
type = "P"; |
417 |
|
break; |
418 |
|
case XVID_TYPE_BVOP: |
419 |
|
type = "B"; |
420 |
|
break; |
421 |
|
case XVID_TYPE_SVOP: |
422 |
|
type = "S"; |
423 |
|
break; |
424 |
|
default: |
425 |
|
type = "U"; |
426 |
|
break; |
427 |
|
} |
428 |
|
|
429 |
printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n", |
printf(" | type=%s quant=%2d, length=%7d", type, stats_quant, stats_length); |
|
(int)filenr, type[frame_type], (float)enctime, (int)m4v_size); |
|
430 |
|
|
431 |
|
if(ARG_STATS) { |
432 |
|
printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", |
433 |
|
(stats[0] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[0]/((float)(XDIM)*(YDIM))), |
434 |
|
(stats[1] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[1]/((float)(XDIM)*(YDIM)/4)), |
435 |
|
(stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4))); |
436 |
} |
} |
437 |
|
|
438 |
/* Update encoding time stats */ |
} |
|
totalenctime += enctime; |
|
|
totalsize += m4v_size; |
|
439 |
|
|
440 |
/***************************************************************************** |
printf("\n"); |
|
* Save hints to file |
|
|
****************************************************************************/ |
|
441 |
|
|
442 |
if(ARG_HINTMODE == HINT_MODE_GET) { |
if (m4v_size < 0) { |
443 |
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
break; |
|
fwrite(&hints_size, 1, sizeof(long), hints_file); |
|
|
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
|
|
fwrite(hints_buffer, 1, hints_size, hints_file); |
|
444 |
} |
} |
445 |
|
|
446 |
|
/* Update encoding time stats */ |
447 |
|
totalenctime += enctime; |
448 |
|
totalsize += m4v_size; |
449 |
|
|
450 |
/***************************************************************************** |
/***************************************************************************** |
451 |
* Save stream to file |
* Save stream to file |
452 |
****************************************************************************/ |
****************************************************************************/ |
453 |
|
|
454 |
if (ARG_SAVEMPEGSTREAM) |
if (m4v_size>0 && ARG_SAVEMPEGSTREAM) |
455 |
{ |
{ |
456 |
/* Save single files */ |
/* Save single files */ |
457 |
if (out_file == NULL) { |
if (out_file == NULL) { |
458 |
sprintf(filename, "%sframe%05d.m4v", filepath, filenr); |
sprintf(filename, "%sframe%05d.m4v", filepath, output_num); |
459 |
out_file = fopen(filename, "wb"); |
out_file = fopen(filename, "wb"); |
460 |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
461 |
fclose(out_file); |
fclose(out_file); |
462 |
out_file = NULL; |
out_file = NULL; |
463 |
|
output_num++; |
464 |
} |
} |
465 |
else { |
else { |
466 |
|
|
470 |
} |
} |
471 |
} |
} |
472 |
|
|
473 |
next_frame: |
input_num++; |
|
/* Read the header if it's pgm stream */ |
|
|
if (ARG_INPUTTYPE) |
|
|
status = read_pgmheader(in_file); |
|
474 |
|
|
475 |
if(frame_type != 5) filenr++; |
/* Read the header if it's pgm stream */ |
476 |
|
if (!result && ARG_INPUTTYPE) |
477 |
|
result = read_pgmheader(in_file); |
478 |
|
|
479 |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
} while (1); |
480 |
|
|
481 |
|
|
482 |
|
|
484 |
* Calculate totals and averages for output, print results |
* Calculate totals and averages for output, print results |
485 |
****************************************************************************/ |
****************************************************************************/ |
486 |
|
|
487 |
totalsize /= filenr; |
printf("Tot: enctime(ms) =%7.2f, length(bytes) = %7d\n", |
488 |
totalenctime /= filenr; |
totalenctime, (int)totalsize); |
489 |
|
|
490 |
|
if (input_num > 0) { |
491 |
|
totalsize /= input_num; |
492 |
|
totalenctime /= input_num; |
493 |
|
}else{ |
494 |
|
totalsize = -1; |
495 |
|
totalenctime = -1; |
496 |
|
} |
497 |
|
|
498 |
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n", |
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n", |
499 |
totalenctime, 1000/totalenctime, (int)totalsize); |
totalenctime, 1000/totalenctime, (int)totalsize); |
500 |
|
|
501 |
|
|
502 |
/***************************************************************************** |
/***************************************************************************** |
503 |
* XviD PART Stop |
* XviD PART Stop |
504 |
****************************************************************************/ |
****************************************************************************/ |
507 |
|
|
508 |
if (enc_handle) |
if (enc_handle) |
509 |
{ |
{ |
510 |
status = enc_stop(); |
result = enc_stop(); |
511 |
if (status) |
if (result) |
512 |
fprintf(stderr, "Encore RELEASE problem return value %d\n", status); |
fprintf(stderr, "Encore RELEASE problem return value %d\n", result); |
513 |
} |
} |
514 |
|
|
515 |
if(in_file) |
if(in_file) |
516 |
fclose(in_file); |
fclose(in_file); |
517 |
if(out_file) |
if(out_file) |
518 |
fclose(out_file); |
fclose(out_file); |
|
if(hints_file) |
|
|
fclose(hints_file); |
|
519 |
|
|
520 |
free_all_memory: |
free_all_memory: |
521 |
free(out_buffer); |
free(out_buffer); |
522 |
free(mp4_buffer); |
free(mp4_buffer); |
523 |
free(in_buffer); |
free(in_buffer); |
|
if(hints_buffer) free(hints_buffer); |
|
524 |
|
|
525 |
return 0; |
return(0); |
526 |
|
|
527 |
} |
} |
528 |
|
|
541 |
#ifndef WIN32 |
#ifndef WIN32 |
542 |
struct timeval tv; |
struct timeval tv; |
543 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
544 |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
return(tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3); |
545 |
#else |
#else |
546 |
clock_t clk; |
clock_t clk; |
547 |
clk = clock(); |
clk = clock(); |
548 |
return clk * 1000 / CLOCKS_PER_SEC; |
return(clk * 1000 / CLOCKS_PER_SEC); |
549 |
#endif |
#endif |
550 |
} |
} |
551 |
|
|
561 |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
562 |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
563 |
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"); |
|
564 |
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
565 |
|
fprintf(stderr, " -p : packed mode\n"); |
566 |
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
567 |
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
568 |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
569 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
570 |
|
fprintf(stderr, " -s : print stats about encoded frames\n"); |
571 |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
572 |
fprintf(stderr, " -n integer : number of frames to encode\n"); |
fprintf(stderr, " -n integer : number of frames to encode\n"); |
573 |
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
574 |
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
fprintf(stderr, " -dump : save decoder output\n"); |
575 |
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
fprintf(stderr, " -m : save mpeg4 raw stream\n"); |
576 |
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"); |
577 |
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"); |
578 |
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"); |
|
579 |
fprintf(stderr, " -help : prints this help message\n"); |
fprintf(stderr, " -help : prints this help message\n"); |
580 |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
581 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
598 |
bytes = fread(dummy,1,2,handle); |
bytes = fread(dummy,1,2,handle); |
599 |
|
|
600 |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
601 |
return 1; |
return(1); |
602 |
|
|
603 |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
604 |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
605 |
{ |
{ |
606 |
fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
607 |
return 2; |
return(2); |
608 |
} |
} |
609 |
if ( (XDIM==0) || (YDIM==0) ) |
if ( (XDIM==0) || (YDIM==0) ) |
610 |
{ |
{ |
612 |
YDIM=ysize*2/3; |
YDIM=ysize*2/3; |
613 |
} |
} |
614 |
|
|
615 |
return 0; |
return(0); |
616 |
} |
} |
617 |
|
|
618 |
static int read_pgmdata(FILE* handle, unsigned char *image) |
static int read_pgmdata(FILE* handle, unsigned char *image) |
643 |
/* I don't know why, but this seems needed */ |
/* I don't know why, but this seems needed */ |
644 |
fread(&dummy, 1, 1, handle); |
fread(&dummy, 1, 1, handle); |
645 |
|
|
646 |
return 0; |
return(0); |
647 |
} |
} |
648 |
|
|
649 |
static int read_yuvdata(FILE* handle, unsigned char *image) |
static int read_yuvdata(FILE* handle, unsigned char *image) |
650 |
{ |
{ |
651 |
|
|
652 |
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)) |
653 |
return 1; |
return(1); |
654 |
else |
else |
655 |
return 0; |
return(0); |
656 |
} |
} |
657 |
|
|
658 |
/***************************************************************************** |
/***************************************************************************** |
659 |
* Routines for encoding: init encoder, frame step, release encoder |
* Routines for encoding: init encoder, frame step, release encoder |
660 |
****************************************************************************/ |
****************************************************************************/ |
661 |
|
|
662 |
|
/* sample plugin */ |
663 |
|
|
664 |
|
int rawenc_debug(void * handle, int opt, void * param1, void * param2) |
665 |
|
{ |
666 |
|
switch(opt) |
667 |
|
{ |
668 |
|
case XVID_PLG_INFO : |
669 |
|
{ |
670 |
|
xvid_plg_info_t * info = (xvid_plg_info_t*)param1; |
671 |
|
info->flags = XVID_REQDQUANTS; |
672 |
|
return 0; |
673 |
|
} |
674 |
|
|
675 |
|
case XVID_PLG_CREATE : |
676 |
|
case XVID_PLG_DESTROY : |
677 |
|
case XVID_PLG_BEFORE : |
678 |
|
return 0; |
679 |
|
|
680 |
|
case XVID_PLG_AFTER : |
681 |
|
{ |
682 |
|
xvid_plg_data_t * data = (xvid_plg_data_t*)param1; |
683 |
|
int i,j; |
684 |
|
|
685 |
|
printf("---[ frame: %5i quant: %2i length: %6i ]---\n", data->frame_num, data->quant, data->length); |
686 |
|
for (j=0; j<data->mb_height; j++) { |
687 |
|
for (i = 0; i<data->mb_width; i++) |
688 |
|
printf("%2i ", data->dquant[j*data->dquant_stride + i]); |
689 |
|
printf("\n"); |
690 |
|
} |
691 |
|
|
692 |
|
return 0; |
693 |
|
} |
694 |
|
} |
695 |
|
|
696 |
|
return XVID_ERR_FAIL; |
697 |
|
} |
698 |
|
|
699 |
|
|
700 |
#define FRAMERATE_INCR 1001 |
#define FRAMERATE_INCR 1001 |
701 |
|
|
702 |
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
704 |
{ |
{ |
705 |
int xerr; |
int xerr; |
706 |
|
|
707 |
XVID_INIT_PARAM xinit; |
xvid_enc_plugin_t plugins[2]; |
708 |
XVID_ENC_PARAM xparam; |
|
709 |
|
xvid_gbl_init_t xvid_gbl_init; |
710 |
|
xvid_enc_create_t xvid_enc_create; |
711 |
|
|
712 |
|
/*------------------------------------------------------------------------ |
713 |
|
* XviD core initialization |
714 |
|
*----------------------------------------------------------------------*/ |
715 |
|
|
716 |
|
/* Set version -- version checking will done by xvidcore*/ |
717 |
|
memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); |
718 |
|
xvid_gbl_init.version = XVID_VERSION; |
719 |
|
|
720 |
|
|
721 |
|
/* Do we have to enable ASM optimizations ? */ |
722 |
if(use_assembler) { |
if(use_assembler) { |
723 |
|
|
724 |
#ifdef ARCH_IA64 |
#ifdef ARCH_IS_IA64 |
725 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
726 |
#else |
#else |
727 |
xinit.cpu_flags = 0; |
xvid_gbl_init.cpu_flags = 0; |
728 |
#endif |
#endif |
729 |
} |
} |
730 |
else { |
else { |
731 |
xinit.cpu_flags = XVID_CPU_FORCE; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
732 |
} |
} |
733 |
|
|
734 |
xvid_init(NULL, 0, &xinit, NULL); |
/* Initialize XviD core -- Should be done once per __process__ */ |
735 |
|
xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); |
736 |
|
|
737 |
xparam.width = XDIM; |
/*------------------------------------------------------------------------ |
738 |
xparam.height = YDIM; |
* XviD encoder initialization |
739 |
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) |
*----------------------------------------------------------------------*/ |
740 |
{ |
|
741 |
xparam.fincr = 1; |
/* Version again */ |
742 |
xparam.fbase = (int)ARG_FRAMERATE; |
memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); |
743 |
|
xvid_enc_create.version = XVID_VERSION; |
744 |
|
|
745 |
|
/* Width and Height of input frames */ |
746 |
|
xvid_enc_create.width = XDIM; |
747 |
|
xvid_enc_create.height = YDIM; |
748 |
|
|
749 |
|
/* init plugins */ |
750 |
|
|
751 |
|
xvid_enc_create.plugins = plugins; |
752 |
|
xvid_enc_create.num_plugins = 0; |
753 |
|
|
754 |
|
if (ARG_LUMIMASKING) { |
755 |
|
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; |
756 |
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
757 |
|
xvid_enc_create.num_plugins++; |
758 |
} |
} |
759 |
else |
|
760 |
{ |
if (ARG_DUMP) { |
761 |
xparam.fincr = FRAMERATE_INCR; |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump; |
762 |
xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
plugins[xvid_enc_create.num_plugins].param = NULL; |
763 |
|
xvid_enc_create.num_plugins++; |
764 |
|
} |
765 |
|
|
766 |
|
/* plugins[xvid_enc_create.num_plugins].func = rawenc_debug; |
767 |
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
768 |
|
xvid_enc_create.num_plugins++; */ |
769 |
|
|
770 |
|
/* No fancy thread tests */ |
771 |
|
xvid_enc_create.num_threads = 0; |
772 |
|
|
773 |
|
/* Frame rate - Do some quick float fps = fincr/fbase hack */ |
774 |
|
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) { |
775 |
|
xvid_enc_create.fincr = 1; |
776 |
|
xvid_enc_create.fbase = (int)ARG_FRAMERATE; |
777 |
|
} else { |
778 |
|
xvid_enc_create.fincr = FRAMERATE_INCR; |
779 |
|
xvid_enc_create.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
780 |
} |
} |
781 |
xparam.rc_reaction_delay_factor = 16; |
|
782 |
xparam.rc_averaging_period = 100; |
/* Maximum key frame interval */ |
783 |
xparam.rc_buffer = 10; |
xvid_enc_create.max_key_interval = (int)ARG_FRAMERATE*10; |
784 |
xparam.rc_bitrate = ARG_BITRATE*1000; |
|
785 |
xparam.min_quantizer = ARG_MINQUANT; |
/* Bframes settings */ |
786 |
xparam.max_quantizer = ARG_MAXQUANT; |
xvid_enc_create.max_bframes = ARG_MAXBFRAMES; |
787 |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
xvid_enc_create.bquant_ratio = ARG_BQRATIO; |
788 |
xparam.bquant_ratio = ARG_BQRATIO; |
xvid_enc_create.bquant_offset = ARG_BQOFFSET; |
789 |
xparam.bquant_offset = ARG_BQOFFSET; |
|
790 |
xparam.max_bframes = ARG_MAXBFRAMES; |
/* Dropping ratio frame -- we don't need that */ |
791 |
xparam.frame_drop_ratio = 0; |
xvid_enc_create.frame_drop_ratio = 0; |
792 |
xparam.global = 0; |
|
793 |
|
/* Global encoder options */ |
794 |
|
xvid_enc_create.global = 0; |
795 |
|
if (ARG_PACKED) xvid_enc_create.global |= XVID_PACKED; |
796 |
|
if (ARG_STATS) xvid_enc_create.global |= XVID_EXTRASTATS_ENABLE; |
797 |
|
|
798 |
/* 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 */ |
799 |
|
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
800 |
|
|
801 |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL); |
/* Retrieve the encoder instance from the structure */ |
802 |
enc_handle=xparam.handle; |
enc_handle = xvid_enc_create.handle; |
803 |
|
|
804 |
return xerr; |
return(xerr); |
805 |
} |
} |
806 |
|
|
807 |
static int enc_stop() |
static int enc_stop() |
808 |
{ |
{ |
809 |
int xerr; |
int xerr; |
810 |
|
|
811 |
|
/* Destroy the encoder instance */ |
812 |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
|
return xerr; |
|
813 |
|
|
814 |
|
return(xerr); |
815 |
} |
} |
816 |
|
|
817 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, |
818 |
unsigned char* hints_buffer, |
unsigned char* bitstream, |
819 |
long *streamlength, long *frametype, long *hints_size) |
int * key, |
820 |
|
int *stats_type, |
821 |
|
int *stats_quant, |
822 |
|
int *stats_length, |
823 |
|
int stats[3]) |
824 |
{ |
{ |
825 |
int xerr; |
int ret; |
826 |
|
|
827 |
|
xvid_enc_frame_t xvid_enc_frame; |
828 |
|
xvid_enc_stats_t xvid_enc_stats; |
829 |
|
|
830 |
XVID_ENC_FRAME xframe; |
/* Version for the frame and the stats */ |
831 |
XVID_ENC_STATS xstats; |
memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); |
832 |
|
xvid_enc_frame.version = XVID_VERSION; |
833 |
|
|
834 |
xframe.bitstream = bitstream; |
memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); |
835 |
xframe.length = -1; /* this is written by the routine */ |
xvid_enc_stats.version = XVID_VERSION; |
836 |
|
|
837 |
xframe.image = image; |
/* Bind output buffer */ |
838 |
xframe.colorspace = XVID_CSP_YV12; /* defined in <xvid.h> */ |
xvid_enc_frame.bitstream = bitstream; |
839 |
|
xvid_enc_frame.length = -1; |
840 |
|
|
841 |
xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */ |
/* Initialize input image fields */ |
842 |
|
if (image) { |
843 |
|
xvid_enc_frame.input.plane[0] = image; |
844 |
|
xvid_enc_frame.input.csp = XVID_CSP_I420; |
845 |
|
xvid_enc_frame.input.stride[0] = XDIM; |
846 |
|
}else{ |
847 |
|
xvid_enc_frame.input.csp = XVID_CSP_NULL; |
848 |
|
} |
849 |
|
|
850 |
xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */ |
/* Set up core's general features */ |
851 |
xframe.bquant = 0; |
xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; |
852 |
|
if (ARG_STATS) xvid_enc_frame.vol_flags |= XVID_EXTRASTATS; |
853 |
|
|
854 |
xframe.motion = motion_presets[ARG_QUALITY]; |
/* Set up core's general features */ |
855 |
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; |
|
856 |
|
|
857 |
xframe.hint.hintstream = hints_buffer; |
/* Frame type -- let core decide for us */ |
858 |
|
xvid_enc_frame.type = XVID_TYPE_AUTO; |
859 |
|
|
860 |
if(ARG_HINTMODE == HINT_MODE_SET) { |
/* Force the right quantizer */ |
861 |
xframe.hint.hintlength = *hints_size; |
xvid_enc_frame.quant = ARG_QUANTI; |
|
xframe.hint.rawhints = 0; |
|
|
xframe.general |= XVID_HINTEDME_SET; |
|
|
} |
|
862 |
|
|
863 |
if(ARG_HINTMODE == HINT_MODE_GET) { |
/* Set up motion estimation flags */ |
864 |
xframe.hint.rawhints = 0; |
xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; |
|
xframe.general |= XVID_HINTEDME_GET; |
|
|
} |
|
865 |
|
|
866 |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
/* We don't use special matrices */ |
867 |
|
xvid_enc_frame.quant_intra_matrix = NULL; |
868 |
|
xvid_enc_frame.quant_inter_matrix = NULL; |
869 |
|
|
870 |
if(ARG_HINTMODE == HINT_MODE_GET) |
/* Encode the frame */ |
871 |
*hints_size = xframe.hint.hintlength; |
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
872 |
|
|
873 |
/* |
*key = (xvid_enc_frame.out_flags & XVID_KEYFRAME); |
874 |
* This is statictical data, e.g. for 2-pass. If you are not |
*stats_type = xvid_enc_stats.type; |
875 |
* interested in any of this, you can use NULL instead of &xstats |
*stats_quant = xvid_enc_stats.quant; |
876 |
*/ |
*stats_length = xvid_enc_stats.length; |
877 |
*frametype = xframe.intra; |
stats[0] = xvid_enc_stats.sse_y; |
878 |
*streamlength = xframe.length; |
stats[1] = xvid_enc_stats.sse_u; |
879 |
|
stats[2] = xvid_enc_stats.sse_v; |
880 |
|
|
881 |
return xerr; |
return(ret); |
882 |
} |
} |