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.2.5 2003-03-13 11:07:20 suxen_drol Exp $ |
* $Id: xvid_encraw.c,v 1.11.2.6 2003-03-15 14:32:56 suxen_drol Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
46 |
|
|
47 |
#include "xvid.h" |
#include "xvid.h" |
48 |
|
|
49 |
|
|
50 |
/***************************************************************************** |
/***************************************************************************** |
51 |
* Quality presets |
* Quality presets |
52 |
****************************************************************************/ |
****************************************************************************/ |
86 |
#define ABS_MAXFRAMENR 9999 |
#define ABS_MAXFRAMENR 9999 |
87 |
|
|
88 |
static int ARG_STATS = 0; |
static int ARG_STATS = 0; |
89 |
|
static int ARG_DUMP = 0; |
90 |
static int ARG_BITRATE = 900; |
static int ARG_BITRATE = 900; |
91 |
static int ARG_QUANTI = 0; |
static int ARG_QUANTI = 0; |
92 |
static int ARG_QUALITY = 5; |
static int ARG_QUALITY = 5; |
116 |
* Nasty global vars ;-) |
* Nasty global vars ;-) |
117 |
***************************************************************************/ |
***************************************************************************/ |
118 |
|
|
119 |
static int i,filenr = 0; |
static int i; |
120 |
|
|
121 |
/* the path where to save output */ |
/* the path where to save output */ |
122 |
static char filepath[256] = "./"; |
static char filepath[256] = "./"; |
165 |
double totalenctime=0.; |
double totalenctime=0.; |
166 |
|
|
167 |
int totalsize; |
int totalsize; |
168 |
int status; |
int result; |
169 |
int m4v_size; |
int m4v_size; |
170 |
int key; |
int key; |
171 |
int stats_type; |
int stats_type; |
173 |
int stats_length; |
int stats_length; |
174 |
int use_assembler=0; |
int use_assembler=0; |
175 |
|
|
176 |
|
int input_num; |
177 |
|
int output_num; |
178 |
|
|
179 |
char filename[256]; |
char filename[256]; |
180 |
|
|
181 |
FILE *in_file = stdin; |
FILE *in_file = stdin; |
235 |
else if (strcmp("-s", argv[i]) == 0) { |
else if (strcmp("-s", argv[i]) == 0) { |
236 |
ARG_STATS = 1; |
ARG_STATS = 1; |
237 |
} |
} |
238 |
|
else if (strcmp("-dump", argv[i]) == 0) { |
239 |
|
ARG_DUMP = 1; |
240 |
|
} |
241 |
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
242 |
i++; |
i++; |
243 |
ARG_INPUTTYPE = atoi(argv[i]); |
ARG_INPUTTYPE = atoi(argv[i]); |
331 |
****************************************************************************/ |
****************************************************************************/ |
332 |
|
|
333 |
|
|
334 |
status = enc_init(use_assembler); |
result = enc_init(use_assembler); |
335 |
if (status) |
if (result) |
336 |
{ |
{ |
337 |
fprintf(stderr, "Encore INIT problem, return value %d\n", status); |
fprintf(stderr, "Encore INIT problem, return value %d\n", result); |
338 |
goto release_all; |
goto release_all; |
339 |
} |
} |
340 |
|
|
360 |
|
|
361 |
totalsize = 0; |
totalsize = 0; |
362 |
|
|
363 |
|
result = 0; |
364 |
|
|
365 |
|
input_num = 0; /* input frame counter */ |
366 |
|
output_num = 0; /* output frame counter */ |
367 |
|
|
368 |
do { |
do { |
369 |
|
|
370 |
char *type; |
char *type; |
371 |
int stats[3]; |
int stats[3]; |
372 |
|
|
373 |
|
if (input_num >= ARG_MAXFRAMENR) |
374 |
|
{ |
375 |
|
result = 1; |
376 |
|
} |
377 |
|
|
378 |
|
if (!result) |
379 |
|
{ |
380 |
if(ARG_INPUTTYPE) { |
if(ARG_INPUTTYPE) { |
381 |
/* read PGM data (YUV-format) */ |
/* read PGM data (YUV-format) */ |
382 |
status = read_pgmdata(in_file, in_buffer); |
result = read_pgmdata(in_file, in_buffer); |
383 |
} else { |
} else { |
384 |
/* read raw data (YUV-format) */ |
/* read raw data (YUV-format) */ |
385 |
status = read_yuvdata(in_file, in_buffer); |
result = read_yuvdata(in_file, in_buffer); |
386 |
} |
} |
|
|
|
|
if(status) { |
|
|
/* Couldn't read image, most likely end-of-file */ |
|
|
continue; |
|
387 |
} |
} |
388 |
|
|
389 |
/***************************************************************************** |
/***************************************************************************** |
391 |
****************************************************************************/ |
****************************************************************************/ |
392 |
|
|
393 |
enctime = msecond(); |
enctime = msecond(); |
394 |
m4v_size = enc_main(in_buffer, mp4_buffer, &key, &stats_type, &stats_quant, &stats_length, stats); |
m4v_size = enc_main(!result?in_buffer:0, mp4_buffer, &key, &stats_type, &stats_quant, &stats_length, stats); |
395 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
396 |
|
|
397 |
/* Write the Frame statistics */ |
/* Write the Frame statistics */ |
398 |
|
|
399 |
printf("Frame %5d: key=%i, time(ms)=%6.1f, length=%7d", |
printf("%5d: key=%i, time(ms)=%6.1f, length=%7d", |
400 |
(int)filenr, |
!result?input_num:-1, |
401 |
key, |
key, |
402 |
(float)enctime, |
(float)enctime, |
403 |
(int)m4v_size); |
(int)m4v_size); |
423 |
} |
} |
424 |
|
|
425 |
printf(" | type=%s quant=%2d, length=%7d", type, stats_quant, stats_length); |
printf(" | type=%s quant=%2d, length=%7d", type, stats_quant, stats_length); |
|
|
|
|
if(ARG_STATS) { |
|
|
printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", |
|
|
(stats[0] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[0]/((float)(XDIM)*(YDIM))), |
|
|
(stats[1] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[1]/((float)(XDIM)*(YDIM)/4)), |
|
|
(stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4))); |
|
|
} |
|
426 |
} |
} |
427 |
|
|
428 |
printf("\n"); |
printf("\n"); |
429 |
|
|
430 |
|
if (m4v_size < 0) { |
431 |
|
break; |
432 |
|
} |
433 |
|
|
434 |
/* Update encoding time stats */ |
/* Update encoding time stats */ |
435 |
totalenctime += enctime; |
totalenctime += enctime; |
436 |
totalsize += m4v_size; |
totalsize += m4v_size; |
437 |
|
|
|
/* Not coded frames return 0 */ |
|
|
if(m4v_size == 0) goto next_frame; |
|
|
|
|
|
|
|
438 |
/***************************************************************************** |
/***************************************************************************** |
439 |
* Save stream to file |
* Save stream to file |
440 |
****************************************************************************/ |
****************************************************************************/ |
441 |
|
|
442 |
if (ARG_SAVEMPEGSTREAM) |
if (m4v_size>0 && ARG_SAVEMPEGSTREAM) |
443 |
{ |
{ |
444 |
/* Save single files */ |
/* Save single files */ |
445 |
if (out_file == NULL) { |
if (out_file == NULL) { |
446 |
sprintf(filename, "%sframe%05d.m4v", filepath, filenr); |
sprintf(filename, "%sframe%05d.m4v", filepath, output_num); |
447 |
out_file = fopen(filename, "wb"); |
out_file = fopen(filename, "wb"); |
448 |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
449 |
fclose(out_file); |
fclose(out_file); |
450 |
out_file = NULL; |
out_file = NULL; |
451 |
|
output_num++; |
452 |
} |
} |
453 |
else { |
else { |
454 |
|
|
458 |
} |
} |
459 |
} |
} |
460 |
|
|
461 |
filenr++; |
input_num++; |
462 |
|
|
|
next_frame: |
|
463 |
/* Read the header if it's pgm stream */ |
/* Read the header if it's pgm stream */ |
464 |
if (ARG_INPUTTYPE) |
if (!result && ARG_INPUTTYPE) |
465 |
status = read_pgmheader(in_file); |
result = read_pgmheader(in_file); |
466 |
|
|
467 |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
} while (1); |
468 |
|
|
469 |
|
|
470 |
|
|
472 |
* Calculate totals and averages for output, print results |
* Calculate totals and averages for output, print results |
473 |
****************************************************************************/ |
****************************************************************************/ |
474 |
|
|
475 |
totalsize /= filenr; |
if (input_num > 0) { |
476 |
totalenctime /= filenr; |
totalsize /= input_num; |
477 |
|
totalenctime /= input_num; |
478 |
|
}else{ |
479 |
|
totalsize = -1; |
480 |
|
totalenctime = -1; |
481 |
|
} |
482 |
|
|
483 |
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", |
484 |
totalenctime, 1000/totalenctime, (int)totalsize); |
totalenctime, 1000/totalenctime, (int)totalsize); |
492 |
|
|
493 |
if (enc_handle) |
if (enc_handle) |
494 |
{ |
{ |
495 |
status = enc_stop(); |
result = enc_stop(); |
496 |
if (status) |
if (result) |
497 |
fprintf(stderr, "Encore RELEASE problem return value %d\n", status); |
fprintf(stderr, "Encore RELEASE problem return value %d\n", result); |
498 |
} |
} |
499 |
|
|
500 |
if(in_file) |
if(in_file) |
675 |
{ |
{ |
676 |
int xerr; |
int xerr; |
677 |
|
|
678 |
/* xvid_enc_plugin_t plugins[1]; */ |
xvid_enc_plugin_t plugins[2]; |
679 |
|
|
680 |
xvid_gbl_init_t xvid_gbl_init; |
xvid_gbl_init_t xvid_gbl_init; |
681 |
xvid_enc_create_t xvid_enc_create; |
xvid_enc_create_t xvid_enc_create; |
717 |
xvid_enc_create.width = XDIM; |
xvid_enc_create.width = XDIM; |
718 |
xvid_enc_create.height = YDIM; |
xvid_enc_create.height = YDIM; |
719 |
|
|
720 |
/* init plugins |
/* init plugins */ |
721 |
plugins[0].func = rawenc_debug; |
|
722 |
plugins[0].param = NULL; |
xvid_enc_create.plugins = plugins; |
723 |
xvid_enc_create.num_plugins = 1; |
xvid_enc_create.num_plugins = 0; |
724 |
xvid_enc_create.plugins = plugins; */ |
if (ARG_STATS) { |
725 |
|
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_psnr; |
726 |
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
727 |
|
xvid_enc_create.num_plugins++; |
728 |
|
} |
729 |
|
if (ARG_DUMP) { |
730 |
|
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump; |
731 |
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
732 |
|
xvid_enc_create.num_plugins++; |
733 |
|
} |
734 |
|
|
735 |
/* No fancy thread tests */ |
/* No fancy thread tests */ |
736 |
xvid_enc_create.num_threads = 0; |
xvid_enc_create.num_threads = 0; |
757 |
|
|
758 |
/* Global encoder options */ |
/* Global encoder options */ |
759 |
xvid_enc_create.global = 0; |
xvid_enc_create.global = 0; |
|
if (ARG_STATS) xvid_enc_create.global |= XVID_EXTRASTATS_ENABLE; |
|
760 |
if (ARG_PACKED) xvid_enc_create.global |= XVID_PACKED; |
if (ARG_PACKED) xvid_enc_create.global |= XVID_PACKED; |
761 |
|
|
762 |
/* 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 */ |
803 |
xvid_enc_frame.length = -1; |
xvid_enc_frame.length = -1; |
804 |
|
|
805 |
/* Initialize input image fields */ |
/* Initialize input image fields */ |
806 |
|
if (image) { |
807 |
xvid_enc_frame.input.plane[0] = image; |
xvid_enc_frame.input.plane[0] = image; |
808 |
xvid_enc_frame.input.csp = XVID_CSP_I420; |
xvid_enc_frame.input.csp = XVID_CSP_I420; |
809 |
xvid_enc_frame.input.stride[0] = XDIM; |
xvid_enc_frame.input.stride[0] = XDIM; |
810 |
|
}else{ |
811 |
|
xvid_enc_frame.input.csp = XVID_CSP_NULL; |
812 |
|
} |
813 |
|
|
814 |
/* Set up core's general features */ |
/* Set up core's general features */ |
815 |
xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; |
xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; |
832 |
xvid_enc_frame.quant_inter_matrix = NULL; |
xvid_enc_frame.quant_inter_matrix = NULL; |
833 |
|
|
834 |
/* Encode the frame */ |
/* Encode the frame */ |
|
xvid_enc_frame.vop_flags |= (ARG_STATS)?XVID_EXTRASTATS:0; |
|
835 |
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
836 |
|
|
837 |
*key = (xvid_enc_frame.out_flags & XVID_KEYFRAME); |
*key = (xvid_enc_frame.out_flags & XVID_KEYFRAME); |