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_stat.c,v 1.6 2002-09-14 23:54:17 edgomez Exp $ |
* $Id: xvid_stat.c,v 1.12 2002-09-27 18:35:35 edgomez Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
44 |
* -q integer : quality ([0..5]) |
* -q integer : quality ([0..5]) |
45 |
* -d boolean : save decoder output (0 False*, !=0 True) |
* -d boolean : save decoder output (0 False*, !=0 True) |
46 |
* -m boolean : save mpeg4 raw stream (0 False*, !=0 True) |
* -m boolean : save mpeg4 raw stream (0 False*, !=0 True) |
47 |
* -h, -help : prints this help message |
* -help : prints this help message |
48 |
* -quant integer : fixed quantizer (disables -b setting) |
* -quant integer : fixed quantizer (disables -b setting) |
49 |
* (* means default) |
* (* means default) |
50 |
* |
* |
65 |
#include <stdlib.h> |
#include <stdlib.h> |
66 |
#include <string.h> |
#include <string.h> |
67 |
#include <math.h> |
#include <math.h> |
68 |
|
#ifndef _MSC_VER |
69 |
|
#include <sys/time.h> |
70 |
|
#else |
71 |
#include <time.h> |
#include <time.h> |
72 |
|
#endif |
73 |
|
|
74 |
#include "../src/xvid.h" |
#include "xvid.h" |
75 |
|
|
76 |
/**************************************************************************** |
/**************************************************************************** |
77 |
* Prototypes |
* Prototypes |
154 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
155 |
static int XDIM = 0; |
static int XDIM = 0; |
156 |
static int YDIM = 0; |
static int YDIM = 0; |
157 |
|
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
158 |
|
|
159 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
160 |
#define SMALL_EPS 1e-10 |
#define SMALL_EPS 1e-10 |
261 |
i++; |
i++; |
262 |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
263 |
} |
} |
264 |
else if (strcmp("-h", argv[i]) == 0 || strcmp("-help", argv[i])) { |
else if (strcmp("-help", argv[i])) { |
265 |
usage(); |
usage(); |
266 |
return(0); |
return(0); |
267 |
} |
} |
276 |
* Arguments checking |
* Arguments checking |
277 |
****************************************************************************/ |
****************************************************************************/ |
278 |
|
|
279 |
if ( ARG_INPUTTYPE == 0 && |
if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { |
|
((XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048)) ) { |
|
280 |
fprintf(stderr, |
fprintf(stderr, |
281 |
"Wrong frame sizes width=%d height=%d, trying PGM header infos\n", |
"Trying to retreive width and height from PGM header\n", |
282 |
XDIM, |
XDIM, |
283 |
YDIM); |
YDIM); |
284 |
ARG_INPUTTYPE = 1; /* pgm */ |
ARG_INPUTTYPE = 1; /* pgm */ |
285 |
} |
} |
|
else { |
|
|
YDIM = YDIM*3/2; /* YUV */ |
|
|
} |
|
286 |
|
|
287 |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
288 |
fprintf(stderr,"Wrong Quality\n"); |
fprintf(stderr,"Wrong Quality\n"); |
319 |
if (ARG_INPUTTYPE) { |
if (ARG_INPUTTYPE) { |
320 |
if (read_pgmheader(in_file)) { |
if (read_pgmheader(in_file)) { |
321 |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
322 |
return -11; |
return -1; |
323 |
} |
} |
324 |
} |
} |
325 |
|
|
326 |
/* now we know the sizes, so allocate memory */ |
/* now we know the sizes, so allocate memory */ |
327 |
|
|
328 |
in_buffer = (unsigned char *) malloc(XDIM*YDIM); |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
329 |
if (!in_buffer) |
if (!in_buffer) |
330 |
goto free_all_memory; |
goto free_all_memory; |
331 |
|
|
332 |
/* this should really be enough memory ! */ |
/* this should really be enough memory ! */ |
333 |
divx_buffer = (unsigned char *) malloc(XDIM*YDIM*2); |
divx_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2); |
334 |
if (!divx_buffer) |
if (!divx_buffer) |
335 |
goto free_all_memory; |
goto free_all_memory; |
336 |
|
|
337 |
/* PGM is YUV 4:2:0 format, so real image height is *2/3 of PGM picture */ |
out_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*4); |
|
YDIM = YDIM*2/3; |
|
|
|
|
|
out_buffer = (unsigned char *) malloc(XDIM*YDIM*4); |
|
338 |
if (!out_buffer) |
if (!out_buffer) |
339 |
goto free_all_memory; |
goto free_all_memory; |
340 |
|
|
426 |
* Analyse the decoded frame and compare to original |
* Analyse the decoded frame and compare to original |
427 |
****************************************************************************/ |
****************************************************************************/ |
428 |
|
|
429 |
framepsnr[filenr] = PSNR(XDIM,YDIM, in_buffer, XDIM, out_buffer, XDIM ); |
framepsnr[filenr] = PSNR(XDIM,YDIM*3/2, in_buffer, XDIM, out_buffer, XDIM); |
430 |
|
|
431 |
printf("dectime =%6.1f ms PSNR %5.2f\n",dectime, framepsnr[filenr]); |
printf("dectime =%6.1f ms PSNR %5.2f\n",dectime, framepsnr[filenr]); |
432 |
|
|
501 |
|
|
502 |
/* Print all statistics */ |
/* Print all statistics */ |
503 |
printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br")); |
printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br")); |
504 |
printf("%04d ",MAX(ARG_QUANTI,ARG_BITRATE)); |
printf("%04d ",(ARG_QUANTI)?ARG_QUANTI:ARG_BITRATE); |
505 |
printf("( %.2f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE); |
printf("( %.2f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE); |
506 |
printf("size %6d ",totalsize); |
printf("size %6d ",totalsize); |
507 |
printf("( %4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000)); |
printf("( %4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000)); |
508 |
printf("/ %.2f bpp) ",(double)totalsize*8/XDIM/YDIM); |
printf("/ %.2f bpp) ",(double)totalsize*8/XDIM/YDIM); |
509 |
printf("enc: %6.1f fps, dec: %6.1f fps \n",1/totalenctime, 1/totaldectime); |
printf("enc: %6.1f fps, dec: %6.1f fps \n",1000/totalenctime, 1000/totaldectime); |
510 |
printf("PSNR P(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Pframes,Ppsnr,Pminpsnr,Pmaxpsnr,sqrt(Pvarpsnr/filenr)); |
printf("PSNR P(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Pframes,Ppsnr,Pminpsnr,Pmaxpsnr,sqrt(Pvarpsnr/filenr)); |
511 |
printf("I(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Iframes,Ipsnr,Iminpsnr,Imaxpsnr,sqrt(Ivarpsnr/filenr)); |
printf("I(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Iframes,Ipsnr,Iminpsnr,Imaxpsnr,sqrt(Ivarpsnr/filenr)); |
512 |
printf("\n"); |
printf("\n"); |
555 |
/* Return time elapsed time in miliseconds since the program started */ |
/* Return time elapsed time in miliseconds since the program started */ |
556 |
static double msecond() |
static double msecond() |
557 |
{ |
{ |
558 |
|
#ifndef _MSC_VER |
559 |
|
struct timeval tv; |
560 |
|
gettimeofday(&tv, 0); |
561 |
|
return tv.tv_sec*10e3 + tv.tv_usec * 1.0e-3; |
562 |
|
#else |
563 |
clock_t clk; |
clock_t clk; |
|
|
|
564 |
clk = clock(); |
clk = clock(); |
|
|
|
565 |
return clk * 1000 / CLOCKS_PER_SEC; |
return clk * 1000 / CLOCKS_PER_SEC; |
566 |
|
#endif |
567 |
} |
} |
568 |
|
|
569 |
|
|
635 |
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
636 |
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
637 |
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"); |
638 |
fprintf(stderr, " -h, -help : prints this help message\n"); |
fprintf(stderr, " -help : prints this help message\n"); |
639 |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
640 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
641 |
|
|
658 |
|
|
659 |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
660 |
return 1; |
return 1; |
661 |
|
|
662 |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
663 |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
664 |
{ |
{ |
668 |
if ( (XDIM==0) || (YDIM==0) ) |
if ( (XDIM==0) || (YDIM==0) ) |
669 |
{ |
{ |
670 |
XDIM=xsize; |
XDIM=xsize; |
671 |
YDIM=ysize; |
YDIM=ysize*2/3; |
672 |
} |
} |
673 |
|
|
674 |
return 0; |
return 0; |
679 |
int i; |
int i; |
680 |
char dummy; |
char dummy; |
681 |
|
|
682 |
unsigned char* buff1_ptr2 = image + XDIM*YDIM; |
unsigned char *y = image; |
683 |
unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2; |
unsigned char *u = image + XDIM*YDIM; |
684 |
|
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
685 |
|
|
686 |
fread(image,XDIM*YDIM,1, handle); // read Y component of picture |
/* read Y component of picture */ |
687 |
|
fread(y, 1, XDIM*YDIM, handle); |
688 |
|
|
689 |
for (i=0;i<YDIM/2;i++) |
for (i=0;i<YDIM/2;i++) |
690 |
{ |
{ |
691 |
fread(buff1_ptr2,XDIM/2,1,handle); // read U |
/* read U */ |
692 |
buff1_ptr2 += XDIM/2; |
fread(u, 1, XDIM/2, handle); |
693 |
fread(buff1_ptr3,XDIM/2,1,handle); // read V |
|
694 |
buff1_ptr3 += XDIM/2; |
/* read V */ |
695 |
|
fread(v, 1, XDIM/2, handle); |
696 |
|
|
697 |
|
/* Update pointers */ |
698 |
|
u += XDIM/2; |
699 |
|
v += XDIM/2; |
700 |
} |
} |
701 |
fread(&dummy,1,1,handle); // I don't know why, but this seems needed |
|
702 |
|
/* I don't know why, but this seems needed */ |
703 |
|
fread(&dummy, 1, 1, handle); |
704 |
|
|
705 |
return 0; |
return 0; |
706 |
} |
} |
707 |
|
|
708 |
static int read_yuvdata(FILE* handle, unsigned char *image) |
static int read_yuvdata(FILE* handle, unsigned char *image) |
709 |
{ |
{ |
710 |
|
|
711 |
unsigned char* buff1_ptr2 = image + XDIM*YDIM; |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
|
unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2; |
|
|
|
|
|
if (fread(image,XDIM,YDIM*3/2,handle) != (unsigned int)YDIM*3/2) |
|
712 |
return 1; |
return 1; |
713 |
else |
else |
714 |
return 0; |
return 0; |
716 |
|
|
717 |
static int write_pgm(char *filename, unsigned char *image) |
static int write_pgm(char *filename, unsigned char *image) |
718 |
{ |
{ |
719 |
|
int loop; |
720 |
|
|
721 |
|
unsigned char *y = image; |
722 |
|
unsigned char *u = image + XDIM*YDIM; |
723 |
|
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
724 |
|
|
725 |
FILE *filehandle; |
FILE *filehandle; |
726 |
filehandle=fopen(filename,"wb"); |
filehandle=fopen(filename,"w+b"); |
727 |
if (filehandle) |
if (filehandle) |
728 |
{ |
{ |
729 |
fprintf(filehandle,"P5\n\n"); // |
/* Write header */ |
730 |
fprintf(filehandle,"%d %d 255\n",XDIM,YDIM*3/2); |
fprintf(filehandle,"P5\n\n%d %d 255\n", XDIM,YDIM*3/2); |
731 |
fwrite(image, XDIM*YDIM*3/2, 1 ,filehandle); |
|
732 |
|
/* Write Y data */ |
733 |
|
fwrite(y, 1, XDIM*YDIM, filehandle); |
734 |
|
|
735 |
|
for(loop=0; loop<YDIM/2; loop++) |
736 |
|
{ |
737 |
|
/* Write U scanline */ |
738 |
|
fwrite(u, 1, XDIM/2, filehandle); |
739 |
|
|
740 |
|
/* Write V scanline */ |
741 |
|
fwrite(v, 1, XDIM/2, filehandle); |
742 |
|
|
743 |
|
/* Update pointers */ |
744 |
|
u += XDIM/2; |
745 |
|
v += XDIM/2; |
746 |
|
|
747 |
|
} |
748 |
|
|
749 |
|
/* Close file */ |
750 |
fclose(filehandle); |
fclose(filehandle); |
751 |
|
|
752 |
return 0; |
return 0; |
753 |
} |
} |
754 |
else |
else |
799 |
xparam.rc_averaging_period = 100; |
xparam.rc_averaging_period = 100; |
800 |
xparam.rc_buffer = 10; |
xparam.rc_buffer = 10; |
801 |
xparam.rc_bitrate = ARG_BITRATE*1000; |
xparam.rc_bitrate = ARG_BITRATE*1000; |
802 |
xparam.min_quantizer = 1; |
xparam.min_quantizer = ARG_MINQUANT; |
803 |
xparam.max_quantizer = 31; |
xparam.max_quantizer = ARG_MAXQUANT; |
804 |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
805 |
|
|
806 |
/* 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 */ |