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.7 2002-09-15 20:22:01 edgomez Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
150 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
151 |
static int XDIM = 0; |
static int XDIM = 0; |
152 |
static int YDIM = 0; |
static int YDIM = 0; |
153 |
|
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
154 |
|
|
155 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
156 |
#define SMALL_EPS 1e-10 |
#define SMALL_EPS 1e-10 |
272 |
* Arguments checking |
* Arguments checking |
273 |
****************************************************************************/ |
****************************************************************************/ |
274 |
|
|
275 |
if ( ARG_INPUTTYPE == 0 && |
if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { |
|
((XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048)) ) { |
|
276 |
fprintf(stderr, |
fprintf(stderr, |
277 |
"Wrong frame sizes width=%d height=%d, trying PGM header infos\n", |
"Trying to retreive width and height from PGM header\n", |
278 |
XDIM, |
XDIM, |
279 |
YDIM); |
YDIM); |
280 |
ARG_INPUTTYPE = 1; /* pgm */ |
ARG_INPUTTYPE = 1; /* pgm */ |
281 |
} |
} |
|
else { |
|
|
YDIM = YDIM*3/2; /* YUV */ |
|
|
} |
|
282 |
|
|
283 |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
284 |
fprintf(stderr,"Wrong Quality\n"); |
fprintf(stderr,"Wrong Quality\n"); |
315 |
if (ARG_INPUTTYPE) { |
if (ARG_INPUTTYPE) { |
316 |
if (read_pgmheader(in_file)) { |
if (read_pgmheader(in_file)) { |
317 |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
318 |
return -11; |
return -1; |
319 |
} |
} |
320 |
} |
} |
321 |
|
|
322 |
/* now we know the sizes, so allocate memory */ |
/* now we know the sizes, so allocate memory */ |
323 |
|
|
324 |
in_buffer = (unsigned char *) malloc(XDIM*YDIM); |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
325 |
if (!in_buffer) |
if (!in_buffer) |
326 |
goto free_all_memory; |
goto free_all_memory; |
327 |
|
|
328 |
/* this should really be enough memory ! */ |
/* this should really be enough memory ! */ |
329 |
divx_buffer = (unsigned char *) malloc(XDIM*YDIM*2); |
divx_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2); |
330 |
if (!divx_buffer) |
if (!divx_buffer) |
331 |
goto free_all_memory; |
goto free_all_memory; |
332 |
|
|
333 |
/* 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); |
|
334 |
if (!out_buffer) |
if (!out_buffer) |
335 |
goto free_all_memory; |
goto free_all_memory; |
336 |
|
|
651 |
|
|
652 |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
653 |
return 1; |
return 1; |
654 |
|
|
655 |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
656 |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
657 |
{ |
{ |
661 |
if ( (XDIM==0) || (YDIM==0) ) |
if ( (XDIM==0) || (YDIM==0) ) |
662 |
{ |
{ |
663 |
XDIM=xsize; |
XDIM=xsize; |
664 |
YDIM=ysize; |
YDIM=ysize*2/3; |
665 |
} |
} |
666 |
|
|
667 |
return 0; |
return 0; |
672 |
int i; |
int i; |
673 |
char dummy; |
char dummy; |
674 |
|
|
675 |
unsigned char* buff1_ptr2 = image + XDIM*YDIM; |
unsigned char *y = image; |
676 |
unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2; |
unsigned char *u = image + XDIM*YDIM; |
677 |
|
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
678 |
|
|
679 |
fread(image,XDIM*YDIM,1, handle); // read Y component of picture |
/* read Y component of picture */ |
680 |
|
fread(y, 1, XDIM*YDIM, handle); |
681 |
|
|
682 |
for (i=0;i<YDIM/2;i++) |
for (i=0;i<YDIM/2;i++) |
683 |
{ |
{ |
684 |
fread(buff1_ptr2,XDIM/2,1,handle); // read U |
/* read U */ |
685 |
buff1_ptr2 += XDIM/2; |
fread(u, 1, XDIM/2, handle); |
686 |
fread(buff1_ptr3,XDIM/2,1,handle); // read V |
|
687 |
buff1_ptr3 += XDIM/2; |
/* read V */ |
688 |
|
fread(v, 1, XDIM/2, handle); |
689 |
|
|
690 |
|
/* Update pointers */ |
691 |
|
u += XDIM/2; |
692 |
|
v += XDIM/2; |
693 |
} |
} |
694 |
fread(&dummy,1,1,handle); // I don't know why, but this seems needed |
|
695 |
|
/* I don't know why, but this seems needed */ |
696 |
|
fread(&dummy, 1, 1, handle); |
697 |
|
|
698 |
return 0; |
return 0; |
699 |
} |
} |
700 |
|
|
701 |
static int read_yuvdata(FILE* handle, unsigned char *image) |
static int read_yuvdata(FILE* handle, unsigned char *image) |
702 |
{ |
{ |
703 |
|
|
704 |
unsigned char* buff1_ptr2 = image + XDIM*YDIM; |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != 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) |
|
705 |
return 1; |
return 1; |
706 |
else |
else |
707 |
return 0; |
return 0; |
709 |
|
|
710 |
static int write_pgm(char *filename, unsigned char *image) |
static int write_pgm(char *filename, unsigned char *image) |
711 |
{ |
{ |
712 |
|
int loop; |
713 |
|
|
714 |
|
unsigned char *y = image; |
715 |
|
unsigned char *u = image + XDIM*YDIM; |
716 |
|
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
717 |
|
|
718 |
FILE *filehandle; |
FILE *filehandle; |
719 |
filehandle=fopen(filename,"wb"); |
filehandle=fopen(filename,"w+b"); |
720 |
if (filehandle) |
if (filehandle) |
721 |
{ |
{ |
722 |
fprintf(filehandle,"P5\n\n"); // |
/* Write header */ |
723 |
fprintf(filehandle,"%d %d 255\n",XDIM,YDIM*3/2); |
fprintf(filehandle,"P5\n\n%d %d 255\n", XDIM,YDIM*3/2); |
724 |
fwrite(image, XDIM*YDIM*3/2, 1 ,filehandle); |
|
725 |
|
/* Write Y data */ |
726 |
|
fwrite(y, 1, XDIM*YDIM, filehandle); |
727 |
|
|
728 |
|
for(loop=0; loop<YDIM/2; loop++) |
729 |
|
{ |
730 |
|
/* Write U scanline */ |
731 |
|
fwrite(u, 1, XDIM/2, filehandle); |
732 |
|
|
733 |
|
/* Write V scanline */ |
734 |
|
fwrite(v, 1, XDIM/2, filehandle); |
735 |
|
|
736 |
|
/* Update pointers */ |
737 |
|
u += XDIM/2; |
738 |
|
v += XDIM/2; |
739 |
|
|
740 |
|
} |
741 |
|
|
742 |
|
/* Close file */ |
743 |
fclose(filehandle); |
fclose(filehandle); |
744 |
|
|
745 |
return 0; |
return 0; |
746 |
} |
} |
747 |
else |
else |
792 |
xparam.rc_averaging_period = 100; |
xparam.rc_averaging_period = 100; |
793 |
xparam.rc_buffer = 10; |
xparam.rc_buffer = 10; |
794 |
xparam.rc_bitrate = ARG_BITRATE*1000; |
xparam.rc_bitrate = ARG_BITRATE*1000; |
795 |
xparam.min_quantizer = 1; |
xparam.min_quantizer = ARG_MINQUANT; |
796 |
xparam.max_quantizer = 31; |
xparam.max_quantizer = ARG_MAXQUANT; |
797 |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
798 |
|
|
799 |
/* 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 */ |