[svn] / trunk / xvidcore / examples / xvid_stat.c Repository:
ViewVC logotype

Diff of /trunk/xvidcore/examples/xvid_stat.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 483, Sat Sep 14 23:54:17 2002 UTC revision 559, Sat Sep 28 14:27:16 2002 UTC
# Line 19  Line 19 
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.14 2002-09-28 14:27:15 edgomez Exp $
23   *   *
24   ****************************************************************************/   ****************************************************************************/
25    
# Line 44  Line 44 
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   *   *
# Line 65  Line 65 
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
# Line 150  Line 154 
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
# Line 256  Line 261 
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                  }                  }
# Line 271  Line 276 
276   *                            Arguments checking   *                            Arguments checking
277   ****************************************************************************/   ****************************************************************************/
278    
279          if ( ARG_INPUTTYPE == 0 &&          if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) {
280                   ((XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048)) ) {                  fprintf(stderr, "Trying to retreive width and height from PGM header\n");
                 fprintf(stderr,  
                                 "Wrong frame sizes width=%d height=%d, trying PGM header infos\n",  
                                 XDIM,  
                                 YDIM);  
281                  ARG_INPUTTYPE = 1; /* pgm */                  ARG_INPUTTYPE = 1; /* pgm */
282          }          }
         else {  
                 YDIM = YDIM*3/2; /* YUV */  
         }  
283    
284          if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) {          if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) {
285                  fprintf(stderr,"Wrong Quality\n");                  fprintf(stderr,"Wrong Quality\n");
# Line 294  Line 292 
292          }          }
293    
294          if ( ARG_FRAMERATE <= 0) {          if ( ARG_FRAMERATE <= 0) {
295                  fprintf(stderr,"Wrong Fraterate %s \n",argv[5]);                  fprintf(stderr,"Wrong Framerate %s \n",argv[5]);
296                  return -1;                  return -1;
297          }          }
298    
# Line 318  Line 316 
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 -11;                          return -1;
320                  }                  }
321          }          }
322    
323          /* now we know the sizes, so allocate memory */          /* now we know the sizes, so allocate memory */
324    
325          in_buffer = (unsigned char *) malloc(XDIM*YDIM);          in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM));
326          if (!in_buffer)          if (!in_buffer)
327                  goto free_all_memory;                  goto free_all_memory;
328    
329          /* this should really be enough memory ! */          /* this should really be enough memory ! */
330          divx_buffer = (unsigned char *) malloc(XDIM*YDIM*2);          divx_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2);
331          if (!divx_buffer)          if (!divx_buffer)
332                  goto free_all_memory;                  goto free_all_memory;
333    
334          /* 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);  
335          if (!out_buffer)          if (!out_buffer)
336                  goto free_all_memory;                  goto free_all_memory;
337    
# Line 428  Line 423 
423   *             Analyse the decoded frame and compare to original   *             Analyse the decoded frame and compare to original
424   ****************************************************************************/   ****************************************************************************/
425    
426                  framepsnr[filenr] = PSNR(XDIM,YDIM, in_buffer, XDIM, out_buffer, XDIM );                  framepsnr[filenr] = PSNR(XDIM,YDIM*3/2, in_buffer, XDIM, out_buffer, XDIM);
427    
428                  printf("dectime =%6.1f ms PSNR %5.2f\n",dectime, framepsnr[filenr]);                  printf("dectime =%6.1f ms PSNR %5.2f\n",dectime, framepsnr[filenr]);
429    
# Line 503  Line 498 
498    
499          /* Print all statistics */          /* Print all statistics */
500          printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br"));          printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br"));
501          printf("%04d ",MAX(ARG_QUANTI,ARG_BITRATE));          printf("%04d ",(ARG_QUANTI)?ARG_QUANTI:ARG_BITRATE);
502          printf("( %.2f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE);          printf("( %.2f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE);
503          printf("size %6d ",totalsize);          printf("size %6d ",totalsize);
504          printf("( %4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000));          printf("( %4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000));
505          printf("/ %.2f bpp) ",(double)totalsize*8/XDIM/YDIM);          printf("/ %.2f bpp) ",(double)totalsize*8/XDIM/YDIM);
506          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);
507          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));
508          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));
509          printf("\n");          printf("\n");
# Line 557  Line 552 
552  /* Return time elapsed time in miliseconds since the program started */  /* Return time elapsed time in miliseconds since the program started */
553  static double msecond()  static double msecond()
554  {  {
555    #ifndef _MSC_VER
556            struct timeval  tv;
557            gettimeofday(&tv, 0);
558            return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3;
559    #else
560          clock_t clk;          clock_t clk;
   
561          clk = clock();          clk = clock();
   
562          return clk * 1000 / CLOCKS_PER_SEC;          return clk * 1000 / CLOCKS_PER_SEC;
563    #endif
564  }  }
565    
566    
# Line 634  Line 632 
632          fprintf(stderr, " -q integer     : quality ([0..5])\n");          fprintf(stderr, " -q integer     : quality ([0..5])\n");
633          fprintf(stderr, " -d boolean     : save decoder output (0 False*, !=0 True)\n");          fprintf(stderr, " -d boolean     : save decoder output (0 False*, !=0 True)\n");
634          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");
635          fprintf(stderr, " -h, -help      : prints this help message\n");          fprintf(stderr, " -help          : prints this help message\n");
636          fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n");          fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n");
637          fprintf(stderr, " (* means default)\n");          fprintf(stderr, " (* means default)\n");
638    
# Line 657  Line 655 
655    
656          if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' ))          if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' ))
657                  return 1;                  return 1;
658    
659          fscanf(handle,"%d %d %d",&xsize,&ysize,&depth);          fscanf(handle,"%d %d %d",&xsize,&ysize,&depth);
660          if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) )          if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) )
661          {          {
# Line 666  Line 665 
665          if ( (XDIM==0) || (YDIM==0) )          if ( (XDIM==0) || (YDIM==0) )
666          {          {
667                  XDIM=xsize;                  XDIM=xsize;
668                  YDIM=ysize;                  YDIM=ysize*2/3;
669          }          }
670    
671          return 0;          return 0;
# Line 677  Line 676 
676          int i;          int i;
677          char dummy;          char dummy;
678    
679          unsigned char* buff1_ptr2 = image + XDIM*YDIM;          unsigned char *y = image;
680          unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2;          unsigned char *u = image + XDIM*YDIM;
681            unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2;
682    
683          fread(image,XDIM*YDIM,1, handle);       // read Y component of picture          /* read Y component of picture */
684            fread(y, 1, XDIM*YDIM, handle);
685    
686          for (i=0;i<YDIM/2;i++)          for (i=0;i<YDIM/2;i++)
687          {          {
688                  fread(buff1_ptr2,XDIM/2,1,handle);        // read U                  /* read U */
689                  buff1_ptr2 += XDIM/2;                  fread(u, 1, XDIM/2, handle);
690                  fread(buff1_ptr3,XDIM/2,1,handle);               // read V  
691                  buff1_ptr3 += XDIM/2;                  /* read V */
692                    fread(v, 1, XDIM/2, handle);
693    
694                    /* Update pointers */
695                    u += XDIM/2;
696                    v += XDIM/2;
697          }          }
698          fread(&dummy,1,1,handle);       //  I don't know why, but this seems needed  
699        /*  I don't know why, but this seems needed */
700            fread(&dummy, 1, 1, handle);
701    
702          return 0;          return 0;
703  }  }
704    
705  static int read_yuvdata(FILE* handle, unsigned char *image)  static int read_yuvdata(FILE* handle, unsigned char *image)
706  {  {
707    
708          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)  
709                  return 1;                  return 1;
710          else          else
711                  return 0;                  return 0;
# Line 707  Line 713 
713    
714  static int write_pgm(char *filename, unsigned char *image)  static int write_pgm(char *filename, unsigned char *image)
715  {  {
716            int loop;
717    
718            unsigned char *y = image;
719            unsigned char *u = image + XDIM*YDIM;
720            unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2;
721    
722          FILE *filehandle;          FILE *filehandle;
723          filehandle=fopen(filename,"wb");          filehandle=fopen(filename,"w+b");
724          if (filehandle)          if (filehandle)
725          {          {
726                  fprintf(filehandle,"P5\n\n");           //                  /* Write header */
727                  fprintf(filehandle,"%d %d 255\n",XDIM,YDIM*3/2);                  fprintf(filehandle,"P5\n\n%d %d 255\n", XDIM,YDIM*3/2);
728                  fwrite(image, XDIM*YDIM*3/2, 1 ,filehandle);  
729                    /* Write Y data */
730                    fwrite(y, 1, XDIM*YDIM, filehandle);
731    
732                    for(loop=0; loop<YDIM/2; loop++)
733                    {
734                            /* Write U scanline */
735                            fwrite(u, 1, XDIM/2, filehandle);
736    
737                            /* Write V scanline */
738                            fwrite(v, 1, XDIM/2, filehandle);
739    
740                            /* Update pointers */
741                            u += XDIM/2;
742                            v += XDIM/2;
743    
744                    }
745    
746                    /* Close file */
747                  fclose(filehandle);                  fclose(filehandle);
748    
749                  return 0;                  return 0;
750          }          }
751          else          else
# Line 765  Line 796 
796      xparam.rc_averaging_period = 100;      xparam.rc_averaging_period = 100;
797      xparam.rc_buffer = 10;      xparam.rc_buffer = 10;
798          xparam.rc_bitrate = ARG_BITRATE*1000;          xparam.rc_bitrate = ARG_BITRATE*1000;
799          xparam.min_quantizer = 1;          xparam.min_quantizer = ARG_MINQUANT;
800          xparam.max_quantizer = 31;          xparam.max_quantizer = ARG_MAXQUANT;
801          xparam.max_key_interval = (int)ARG_FRAMERATE*10;          xparam.max_key_interval = (int)ARG_FRAMERATE*10;
802    
803          /* 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 */

Legend:
Removed from v.483  
changed lines
  Added in v.559

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4