[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 119, Sat Apr 13 22:21:46 2002 UTC revision 684, Wed Nov 27 21:09:10 2002 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC - Example for encoding and decoding   *  XVID MPEG-4 VIDEO CODEC
4     *  - Console based test application  -
5     *
6     *  Copyright(C) 2002 Christoph Lampert
7   *   *
8   *      This program is free software; you can redistribute it and/or modify   *      This program is free software; you can redistribute it and/or modify
9   *      it under the terms of the GNU General Public License as published by   *      it under the terms of the GNU General Public License as published by
# Line 14  Line 17 
17   *   *
18   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
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., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21   *   *
22   *************************************************************************/   * $Id: xvid_stat.c,v 1.16 2002-11-27 21:09:10 edgomez Exp $
   
 /************************************************************************  
23   *   *
24   *  PSNR and Speed test routine for XviD using the XviD-API   ****************************************************************************/
25   *  (C) Christoph Lampert, 2002/04/13  
26    /*****************************************************************************
27     *  Application notes :
28   *   *
29   *  A sequence of YUV pics in PGM file format is encoded and decoded   *  A sequence of YUV pics in PGM file format is encoded and decoded
30   *  The speed is measured and PSNR of decoded picture is calculated.   *  The speed is measured and PSNR of decoded picture is calculated.
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 ,so with UN*X you simply compile by   *  and maths-lib.
  *  
  *   gcc xvid_stat.c -lxvidcore -lm -o xvid_stat  
  *  
  *  Run without or with illegal parameters, then PGM input input is read  
  *  from stdin.  
  *  
  *  Parameters are: xvid_stat XDIM YDIM QUALITY BITRATE/QUANTIZER FRAMERATE  
  *  
  *  if XDIM or YDIM are illegal (e.g. 0), they are ignored and input is  
  *  considered to be PGM. Otherwise (X and Y both greater than 0) raw YUV  
  *  is expected, as e.g. the standard MPEG test-files, like "foreman"  
  *  
  *  0 <= QUALITY <= 6  (default 5)  
34   *   *
35   *  BITRATE is in kbps (default 900),   * Usage : xvid_stat [OPTIONS]
36   *      if BITRATE<32, then value is taken is fixed QUANTIZER   * Options :
37     *  -w integer     : frame width ([1.2048])
38     *  -h integer     : frame height ([1.2048])
39     *  -b integer     : target bitrate (>0 | default=900kbit)
40     *  -f float       : target framerate (>0)
41     *  -i string      : input filename (default=stdin)
42     *  -t integer     : input data type (yuv=0, pgm=1)
43     *  -n integer     : number of frames to encode
44     *  -q integer     : quality ([0..5])
45     *  -d boolean     : save decoder output (0 False*, !=0 True)
46     *  -m boolean     : save mpeg4 raw stream (0 False*, !=0 True)
47     *  -help          : prints this help message
48     *  -quant integer : fixed quantizer (disables -b setting)
49     *  (* means default)
50     *
51     *  An input file named "stdin" is treated as standard input stream.
52     *
53     *
54     *  PGM input must be in a very specific format, basically it pgm file must
55     *  contain Y plane first just like usual P5 pgm files, and then U and V
56     *  planes are stored just after Y plane so y dimension is y*3/2 in reality
57   *   *
58   *  FRAMERATE is a float (with or without decimal dot), default is 25.00   *  See read_pgmheader for more details.
59   *   *
60   *  input/output and m4v-output is saved, if corresponding flags are set   *  Such a PGM file can be generated from MPEG2 by # mpeg2dec -o pgmpipe
61   *   *
62   *  PGM input must in a very specific format, see read_pgmheader   ****************************************************************************/
  *  it can be generated e.g. from MPEG2 by    mpeg2dec -o pgmpipe  
  *  
  ************************************************************************/  
   
 /************************************************************************  
  *  
  *  For EXAMPLES how to use this, see the seperate file xvid_stat.examples  
  *  
  ************************************************************************/  
63    
64  #include <stdio.h>  #include <stdio.h>
65  #include <stdlib.h>  #include <stdlib.h>
66  #include <math.h>               // needed for log10  #include <string.h>
67  #include <sys/time.h>           // only needed for gettimeofday  #include <math.h>
68    #ifndef _MSC_VER
69  #include "xvid.h"               /* comes with XviD */  #include <sys/time.h>
70    #else
71    #include <time.h>
72    #endif
73    
74    #include "xvid.h"
75    
76    /****************************************************************************
77     *                               Prototypes
78     ***************************************************************************/
79    
80    /* Prints program usage message */
81    static void usage();
82    
83    /* Statistical functions */
84    static double msecond();
85    static double absdistq(int x, int y,
86                                               unsigned char* buf1, int stride1,
87                                               unsigned char* buf2, int stride2);
88    static double PSNR(int x, int y,
89                                       unsigned char* buf1, int stride1,
90                                       unsigned char* buf2, int stride2);
91    
92    /* PGM related functions */
93    static int read_pgmheader(FILE* handle);
94    static int read_pgmdata(FILE* handle, unsigned char *image);
95    static int read_yuvdata(FILE* handle, unsigned char *image);
96    static int write_pgm(char *filename, unsigned char *image);
97    
98    /* Encoder related functions */
99    static int enc_init(int use_assembler);
100    static int enc_stop();
101    static int enc_main(unsigned char* image, unsigned char* bitstream,
102                                            int *streamlength, int* frametype);
103    
104    /* Decoder related functions */
105    static int dec_stop();
106    static int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer,
107                                            int m4v_size);
108    static int dec_init(int use_assembler);
109    
110    /*****************************************************************************
111     *                            Quality presets
112     ****************************************************************************/
113    
114    static int const motion_presets[7] = {
115            0,                                                        /* Q 0 */
116            PMV_EARLYSTOP16,                                          /* Q 1 */
117            PMV_EARLYSTOP16,                                          /* Q 2 */
118            PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,                    /* Q 3 */
119            PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,                    /* Q 4 */
120            PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 |  /* Q 5 */
121            PMV_HALFPELREFINE8,
122            PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */
123            PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8
124    };
125    
126  int motion_presets[7] = {  static int const general_presets[7] = {
127          0,                                                              // Q 0          XVID_H263QUANT,                               /* Q 0 */
128          PMV_EARLYSTOP16,                                                // Q 1          XVID_MPEGQUANT,                               /* Q 1 */
129          PMV_EARLYSTOP16,                                                // Q 2          XVID_H263QUANT,                               /* Q 2 */
130          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,                          // Q 3          XVID_H263QUANT | XVID_HALFPEL,                /* Q 3 */
131          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8,         // Q 4          XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */
132          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8          // Q 5          XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */
133                          | PMV_HALFPELREFINE8,          XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V  /* Q 6 */
         PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16         // Q 6  
                         | PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8  
134          };          };
135    
 int general_presets[7] = {  
         XVID_H263QUANT, /* or use XVID_MPEGQUANT */             // Q 0  
         XVID_H263QUANT,                                         // Q 1  
         XVID_H263QUANT,                                         // Q 2  
         XVID_H263QUANT | XVID_HALFPEL,                          // Q 3  
         XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V,           // Q 4  
         XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V,           // Q 5  
         XVID_H263QUANT  |XVID_HALFPEL | XVID_INTER4V };         // Q 6  
   
   
 /* my default values for encoding */  
   
 int ARG_BITRATE=900;  
 int ARG_QUANTI=0;  
   
 int ARG_QUALITY =6;  
 int ARG_MINQUANT=1;  
 int ARG_MAXQUANT=31;  
 float ARG_FRAMERATE=25.00;  
136    
137    /*****************************************************************************
138     *                     Command line global variables
139     ****************************************************************************/
140    
141    /* Maximum number of frames to encode */
142    #define ABS_MAXFRAMENR 9999
143    
144    static int   ARG_BITRATE = 900;
145    static int   ARG_QUANTI = 0;
146    static int   ARG_QUALITY = 6;
147    static int   ARG_MINQUANT = 1;
148    static int   ARG_MAXQUANT = 31;
149    static float ARG_FRAMERATE = 25.00f;
150    static int   ARG_MAXFRAMENR = ABS_MAXFRAMENR;
151    static char *ARG_INPUTFILE = NULL;
152    static int   ARG_INPUTTYPE = 0;
153    static int   ARG_SAVEDECOUTPUT = 0;
154    static int   ARG_SAVEMPEGSTREAM = 0;
155    static int   XDIM = 0;
156    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
161    
162    /****************************************************************************
163     *                     Nasty global vars ;-)
164     ***************************************************************************/
165    
166    static int i,filenr = 0;
167    static int save_ref_flag = 0;
168    
169    /* the path where to save output */
170    static char filepath[256] = "./";
171    
172    /* Internal structures (handles) for encoding and decoding */
173    static void *enc_handle = NULL;
174    static void *dec_handle = NULL;
175    
176    /*****************************************************************************
177     *                               Main program
178     ****************************************************************************/
179    
180  /* these are global variables. Not very elegant, but easy, and this is an easy program */  int main(int argc, char *argv[])
181    {
 int XDIM=0;  
 int YDIM=0;     // will be set when reading first image  
 int i,filenr = 0;  
   
 int save_m4v_flag = 0;          // save MPEG4-bytestream?  
 int save_dec_flag = 1;          // save decompressed bytestream?  
 int save_ref_flag = 0;          //  
182    
183  int pgmflag = 0;                // a flag, if input is in PGM format, overwritten in init-phase          unsigned char *divx_buffer = NULL;
184  char filepath[256] = "./";      // the path where to save output          unsigned char *in_buffer = NULL;
185            unsigned char *out_buffer = NULL;
186    
187  #define MAXFILENR 9999          // max number of frames (this should be made into an option!)          double enctime,dectime;
188            double totalenctime=0.;
189            double totaldectime=0.;
190    
191  void *enc_handle = NULL;                // internal structures (handles) for encoding          long totalsize=0;
192  void *dec_handle = NULL;                // and decoding          int status;
193    
194            int m4v_size;
195            int frame_type[ABS_MAXFRAMENR];
196            int Iframes=0, Pframes=0, use_assembler=0;
197            double framepsnr[ABS_MAXFRAMENR];
198    
199  /*********************************************************************/          double Ipsnr=0.,Imaxpsnr=0.,Iminpsnr=999.,Ivarpsnr=0.;
200  /*                     "statistical" functions                       */          double Ppsnr=0.,Pmaxpsnr=0.,Pminpsnr=999.,Pvarpsnr=0.;
 /*                                                                   */  
 /*  these are not needed for encoding or decoding, but for measuring */  
 /*  time and quality, there in nothing specific to XviD in these     */  
 /*                                                                   */  
 /*********************************************************************/  
201    
202  double msecond()          char filename[256];
 /* return the current time in seconds(!)  */  
 {  
         struct timeval  tv;  
         gettimeofday(&tv, 0);  
         return tv.tv_sec + tv.tv_usec * 1.0e-6;  
 }  
203    
204            FILE *filehandle;
205            FILE *in_file = stdin;
206    
207            printf("xvid_stat - XviD core library test program ");
208            printf("written by Christoph Lampert 2002\n\n");
209    
210  double absdistq(int x,int y, unsigned char* buf1, int stride1, unsigned char* buf2, int stride2)  /*****************************************************************************
211  /* returns the sum of squared distances (SSD) between two images of dimensions x times y */   *                            Command line parsing
212  {   ****************************************************************************/
         double dist=0.;  
         int i,j,val;  
213    
214          for (i=0;i<y;i++)          for (i=1; i< argc; i++) {
         {  
                 val=0;  
                 for (j=0;j<x;j++)  
                         val+= ((int)buf1[j]-(int)buf2[j])*((int)buf1[j]-(int)buf2[j]);  
215    
216                  dist += (double)val;                  if (strcmp("-asm", argv[i]) == 0 ) {
217                  buf1 += stride1;                          use_assembler = 1;
                 buf2 += stride2;  
218          }          }
219     return dist/(x*y);                  else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) {
220                            i++;
221                            XDIM = atoi(argv[i]);
222  }  }
223                    else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) {
224                            i++;
225  double PSNR(int x,int y, unsigned char* buf1, int stride1, unsigned char* buf2, int stride2 )                          YDIM = atoi(argv[i]);
 /* return the PSNR between to images                                               */  
 /* this is a logarithmic measure for "quality" from the world of signal processing */  
 /* if you don't know what it is, simply accept that higher values are better       */  
 {  
    return 10*(log10(255*255)-log10( absdistq(x, y, buf1, stride1, buf2, stride2) ));  
226  }  }
227                    else if (strcmp("-b", argv[i]) == 0 && i < argc - 1 ) {
228                            i++;
229  /*********************************************************************/                          ARG_BITRATE = atoi(argv[i]);
 /*                    input and output functions                     */  
 /*                                                                   */  
 /* the are small and simple routines to read and write PGM and YUV   */  
 /* image. It's just for convenience, again nothing specific to XviD  */  
 /*                                                                   */  
 /*********************************************************************/  
   
 int read_pgmheader(FILE* handle)  
 {  
         int bytes,xsize,ysize,depth;  
         char dummy[2];  
   
         bytes = fread(dummy,1,2,handle);  
   
         if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' ))  
                 return 1;  
         fscanf(handle,"%d %d %d",&xsize,&ysize,&depth);  
         if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) )  
         {  
                 fprintf(stderr,"%d %d %d\n",xsize,ysize,depth);  
                 return 2;  
230          }          }
231          if ( (XDIM==0) || (YDIM==0) )                  else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) {
232          {       XDIM=xsize;                          i++;
233                  YDIM=ysize;                          ARG_QUALITY = atoi(argv[i]);
234          }          }
235                    else if (strcmp("-f", argv[i]) == 0 && i < argc - 1 ) {
236          return 0;                          i++;
237                            ARG_FRAMERATE = (float)atof(argv[i]);
238  }  }
239                    else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {
240  int read_pgmdata(FILE* handle, unsigned char *image)                          i++;
241  {                          ARG_INPUTFILE = argv[i];
         int i,status;  
         char dummy;  
   
         unsigned char* buff1_ptr2 = image + XDIM*YDIM;  
         unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2;  
   
         fread(image,XDIM*YDIM,1,stdin); // read Y component of picture  
   
         for (i=0;i<YDIM/2;i++)  
         {  
                 fread(buff1_ptr2,XDIM/2,1,stdin);        // read U  
                 buff1_ptr2 += XDIM/2;  
                 fread(buff1_ptr3,XDIM/2,1,stdin);        // read V  
                 buff1_ptr3 += XDIM/2;  
242          }          }
243          fread(&dummy,1,1,handle);       //  I don't know why, but this seems needed                  else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) {
244          return 0;                          i++;
245                            ARG_INPUTTYPE = atoi(argv[i]);
246  }  }
247                    else if(strcmp("-n", argv[i]) == 0 && i < argc - 1 ) {
248  int read_yuvdata(FILE* handle, unsigned char *image)                          i++;
249  {       int i;                          ARG_MAXFRAMENR  = atoi(argv[i]);
         char dummy;  
   
         unsigned char* buff1_ptr2 = image + XDIM*YDIM;  
         unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2;  
   
         if (fread(image,XDIM,YDIM*3/2,stdin) != YDIM*3/2)  
                 return 1;  
         else  
                 return 0;  
250  }  }
251                    else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1 ) {
252  int write_pgm(char *filename, unsigned char *image)                          i++;
253  {                          ARG_QUANTI = atoi(argv[i]);
         FILE *filehandle;  
         filehandle=fopen(filename,"wb");  
         if (filehandle)  
         {  
                 fprintf(filehandle,"P5\n\n");           //  
                 fprintf(filehandle,"%d %d 255\n",XDIM,YDIM*3/2);  
                 fwrite(image,XDIM,YDIM*3/2,filehandle);  
                 fclose(filehandle);  
                 return 0;  
254          }          }
255          else                  else if (strcmp("-d", argv[i]) == 0 && i < argc - 1 ) {
256                  return 1;                          i++;
257                            ARG_SAVEDECOUTPUT = atoi(argv[i]);
258  }  }
259                    else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) {
260                            i++;
261                            ARG_SAVEMPEGSTREAM = atoi(argv[i]);
 /*********************************************************************/  
 /* Routines for encoding: init encoder, frame step, release encoder  */  
 /*********************************************************************/  
   
 #define FRAMERATE_INCR 1001  
   
   
 int enc_init()  
 {       /* initialize encoder for first use, pass all needed parameters to the codec */  
         int xerr;  
   
         XVID_INIT_PARAM xinit;  
         XVID_ENC_PARAM xparam;  
   
         xinit.cpu_flags = XVID_CPU_FORCE;  
         xvid_init(NULL, 0, &xinit, NULL);  
   
         xparam.width = XDIM;  
         xparam.height = YDIM;  
         if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS)  
         {  
                 xparam.fincr = 1;  
                 xparam.fbase = (int)ARG_FRAMERATE;  
262          }          }
263          else                  else if (strcmp("-help", argv[i])) {
264          {                          usage();
265                  xparam.fincr = FRAMERATE_INCR;                          return(0);
                 xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE);  
266          }          }
267          xparam.bitrate = ARG_BITRATE*1000;                  else {
268          xparam.min_quantizer = 1;                          usage();
269          xparam.max_quantizer = 31;                          exit(-1);
         xparam.max_key_interval = (int)ARG_FRAMERATE*10;  
         xparam.rc_buffersize    = 2;  
                 /* I use a small value here, since will not encode whole movies, but short clips */  
   
         xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL);  
         enc_handle=xparam.handle;  
   
         return xerr;  
270  }  }
271    
 int  enc_stop()  
 {       int xerr;  
   
         xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL);  
         return xerr;  
272  }  }
273    
274  int  enc_main(unsigned char* image, unsigned char* bitstream, int *streamlength, int* frametype)  /*****************************************************************************
275  {       int xerr;   *                            Arguments checking
276     ****************************************************************************/
         XVID_ENC_FRAME xframe;  
         XVID_ENC_STATS xstats;  
   
         xframe.bitstream = bitstream;  
         xframe.length = -1;     // this is written by the routine  
   
         xframe.image = image;  
         xframe.colorspace = XVID_CSP_YV12;      // defined in <xvid.h>  
   
         xframe.intra = -1; // let the codec decide between I-frame (1) and P-frame (0)  
   
         xframe.quant = ARG_QUANTI;      // is quant != 0, use a fixed quant (and ignore bitrate)  
   
         xframe.motion = motion_presets[ARG_QUALITY];  
         xframe.general = general_presets[ARG_QUALITY];  
         xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL;  
   
         xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats);  
   
 /*              enc_result->is_key_frame = xframe.intra;  
                 enc_result->quantizer = xframe.quant;  
                 enc_result->total_bits = xframe.length * 8;  
                 enc_result->motion_bits = xstats.hlength * 8;  
                 enc_result->texture_bits = enc_result->total_bits - enc_result->motion_bits;  
 */  
   
 /*  This is statictical data, e.g. for 2-pass.  
     If you are not interested in any of this, you can use NULL instead of &xstats  
 */  
         *frametype = xframe.intra;  
         *streamlength = xframe.length;  
277    
278          return xerr;          if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) {
279                    fprintf(stderr, "Trying to retreive width and height from PGM header\n");
280                    ARG_INPUTTYPE = 1; /* pgm */
281  }  }
282    
283            if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) {
284  /*********************************************************************/                  fprintf(stderr,"Wrong Quality\n");
285  /* Routines for decoding: init encoder, frame step, release encoder  */                  return -1;
 /*********************************************************************/  
   
 int dec_init()  /* init decoder before first run */  
 {  
         int xerr;  
   
         XVID_INIT_PARAM xinit;  
         XVID_DEC_PARAM xparam;  
   
         xinit.cpu_flags = 0;  
         xvid_init(NULL, 0, &xinit, NULL);  
         xparam.width = XDIM;  
         xparam.height = YDIM;  
   
         xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);  
         dec_handle = xparam.handle;  
   
         return xerr;  
286  }  }
287    
288  int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer, int m4v_size)          if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) {
289  {       /* decode one frame  */                  fprintf(stderr,"Wrong Bitrate\n");
290                    return -1;
         int xerr;  
         XVID_DEC_FRAME xframe;  
   
         xframe.bitstream = m4v_buffer;  
         xframe.length = m4v_size;  
         xframe.image = out_buffer;  
         xframe.stride = XDIM;  
         xframe.colorspace = XVID_CSP_YV12;             // XVID_CSP_USER is fastest (no memcopy involved)  
   
         xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL);  
   
         return xerr;  
291  }  }
292    
293  int dec_stop()  /* close decoder to release resources */          if ( ARG_FRAMERATE <= 0) {
294  {                  fprintf(stderr,"Wrong Framerate %s \n",argv[5]);
295          int xerr;                  return -1;
         xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);  
   
         return xerr;  
296  }  }
297    
298            if ( ARG_MAXFRAMENR <= 0) {
299  /*********************************************************************/                  fprintf(stderr,"Wrong number of frames\n");
300  /*                          Main program                             */                  return -1;
 /*********************************************************************/  
   
 int main(int argc, char *argv[])  
 {  
   unsigned char *divx_buffer = NULL;  
   unsigned char *in_buffer = NULL;  
   unsigned char *out_buffer = NULL;  
   
   double enctime,dectime;  
   double totalenctime=0.;  
   double totaldectime=0.;  
   
   long totalsize=0;  
   int status;  
   
   int m4v_size;  
   int frame_type[MAXFILENR];  
   int Iframes=0, Pframes=0, Bframes=0;  
   double framepsnr[MAXFILENR];  
   
   double Ipsnr=0.,Imaxpsnr=0.,Iminpsnr=999.,Ivarpsnr=0.;  
   double Ppsnr=0.,Pmaxpsnr=0.,Pminpsnr=999.,Pvarpsnr=0.;  
   double Bpsnr=0.,Bmaxpsnr=0.,Bminpsnr=999.,Bvarpsnr=0.;  
   
   char filename[256];  
   
   FILE *filehandle;  
   
 /* read YUV in pgm format from stdin */  
   if (!pgmflag)  
   {  
         pgmflag = 1;  
   
         if (argc>=3)  
         {       XDIM = atoi(argv[1]);  
                 YDIM = atoi(argv[2]);  
                 if ( (XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048) )  
                 {       fprintf(stderr,"Wrong frames size %d %d, trying PGM \n",XDIM, YDIM);  
                 }  
                 else  
                 {  
                         YDIM = YDIM*3/2; /* for YUV */  
                         pgmflag = 0;  
                 }  
         }  
301    }    }
302    
303    if (pgmflag)          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
304    {     if (read_pgmheader(stdin))                  in_file = stdin;
             {  
               printf("Wrong input format, I want YUV encapsulated in PGM\n");  
               return 1;  
             }  
305    }    }
306    if (argc>=4)          else {
307    {     ARG_QUALITY = atoi(argv[3]);  
308          if ( (ARG_QUALITY < 0) || (ARG_QUALITY > 6) )                  in_file = fopen(ARG_INPUTFILE, "rb");
309                  { fprintf(stderr,"Wrong Quality\n"); return -1; }                  if (in_file == NULL) {
310          else                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
311                    printf("Quality %d\n",ARG_QUALITY);                          return -1;
312    }    }
   if (argc>=5)  
   {     ARG_BITRATE = atoi(argv[4]);  
         if ( (ARG_BITRATE <= 0) )  
                 { fprintf(stderr,"Wrong Bitrate\n"); return -1; }  
         if ( (ARG_BITRATE <= 32) )  
                 { ARG_QUANTI = ARG_BITRATE;  
                   ARG_BITRATE=0;  
                   printf("Quantizer %d\n",ARG_QUANTI);  
313                  }                  }
314          else  
315                    printf("Bitrate %d kbps\n",ARG_BITRATE);          if (ARG_INPUTTYPE) {
316                    if (read_pgmheader(in_file)) {
317                            fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n");
318                            return -1;
319    }    }
   if (argc>=6)  
   {     ARG_FRAMERATE = (float)atof(argv[5]);  
         if ( (ARG_FRAMERATE <= 0) )  
                 { fprintf(stderr,"Wrong Fraterate %s \n",argv[5]); return -1; }  
         printf("Framerate %6.3f fps\n",ARG_FRAMERATE);  
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 is one of the most underestimated instructions in C !!!                  goto free_all_memory;
327    
328    divx_buffer = (unsigned char *) malloc(XDIM*YDIM*2);  // this should really be enough memory!          /* this should really be enough memory ! */
329            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    YDIM = YDIM*2/3; // 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);
   
   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    
337    
338  /*********************************************************************/  /*****************************************************************************
339  /*                         XviD PART  Start                          */   *                            XviD PART  Start
340  /*********************************************************************/   ****************************************************************************/
341    
342    
343          status = enc_init();          status = enc_init(use_assembler);
344          if (status)          if (status)
345          {          {
346                  printf("Encore INIT problem, return value %d\n", status);                  fprintf(stderr, "Encore INIT problem, return value %d\n", status);
347                  goto release_all;                  goto release_all;
348          }          }
349    
350          status = dec_init();          status = dec_init(use_assembler);
351          if (status)          if (status)
352          {          {
353                  printf("Decore INIT problem, return value %d\n", status);                  fprintf(stderr, "Decore INIT problem, return value %d\n", status);
354                  goto release_all;                  goto release_all;
355          }          }
356    
357    
358  /*********************************************************************/  /*****************************************************************************
359  /*                               Main loop                           */   *                            Main loop
360  /*********************************************************************/   ****************************************************************************/
361    
362    do          do {
363      {  
364          if (pgmflag)                  if (ARG_INPUTTYPE)
365                status = read_pgmdata(stdin, in_buffer);  // read PGM data (YUV-format)                          status = read_pgmdata(in_file, in_buffer);      /* read PGM data (YUV-format) */
366          else          else
367                status = read_yuvdata(stdin, in_buffer);  // read raw data (YUV-format)                          status = read_yuvdata(in_file, in_buffer);      /* read raw data (YUV-format) */
368    
369        if (status)        if (status)
370          {          {
371            // Couldn't read image, most likely end-of-file                          /* Couldn't read image, most likely end-of-file */
372            continue;            continue;
373          }          }
374    
# Line 533  Line 380 
380          }          }
381    
382    
383  /*********************************************************************/  /*****************************************************************************
384  /*               analyse this frame before encoding                  */   *                       Analyse this frame before encoding
385  /*********************************************************************/   ****************************************************************************/
386    
387  //      nothing is done here at the moment, but you could e.g. create  /*
388  //      histograms or measure entropy or apply preprocessing filters...   *      nothing is done here at the moment, but you could e.g. create
389     *      histograms or measure entropy or apply preprocessing filters...
390  /*********************************************************************/   */
 /*               encode and decode this frame                        */  
 /*********************************************************************/  
391    
392          enctime = -msecond();  /*****************************************************************************
393     *                       Encode and decode this frame
394     ****************************************************************************/
395    
396                    enctime = msecond();
397          status = enc_main(in_buffer, divx_buffer, &m4v_size, &frame_type[filenr]);          status = enc_main(in_buffer, divx_buffer, &m4v_size, &frame_type[filenr]);
398          enctime += msecond();                  enctime = msecond() - enctime;
399    
400          totalenctime += enctime;          totalenctime += enctime;
401          totalsize += m4v_size;          totalsize += m4v_size;
402    
403          printf("Frame %5d: intra %d, enctime =%6.1f ms length=%7d bytes ",                  printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6d bytes ",
404                   filenr, frame_type[filenr], enctime*1000, m4v_size);                             (int)filenr, (int)frame_type[filenr], (float)enctime, (int)m4v_size);
405    
406          if (save_m4v_flag)                  if (ARG_SAVEMPEGSTREAM)
407          {          {
408                  sprintf(filename, "%sframe%05d.m4v", filepath, filenr);                  sprintf(filename, "%sframe%05d.m4v", filepath, filenr);
409                  filehandle = fopen(filename, "wb");                  filehandle = fopen(filename, "wb");
# Line 562  Line 411 
411                  fclose(filehandle);                  fclose(filehandle);
412          }          }
413    
414          dectime = -msecond();                  dectime = msecond();
415          status = dec_main(divx_buffer, out_buffer, m4v_size);          status = dec_main(divx_buffer, out_buffer, m4v_size);
416          dectime += msecond();                  dectime = msecond() - dectime;
417    
418          totaldectime += dectime;          totaldectime += dectime;
419    
420    
421  /*********************************************************************/  /*****************************************************************************
422  /*        analyse the decoded frame and compare to original          */   *             Analyse the decoded frame and compare to original
423  /*********************************************************************/   ****************************************************************************/
424    
425          framepsnr[filenr] = PSNR(XDIM,YDIM, in_buffer, XDIM, out_buffer, XDIM );                  framepsnr[filenr] = PSNR(XDIM,YDIM*3/2, in_buffer, XDIM, out_buffer, XDIM);
426    
427          printf("dectime =%6.1f ms PSNR %5.2f\n",dectime*1000, framepsnr[filenr]);                  printf("dectime =%6.1f ms PSNR %5.2f\n",dectime, framepsnr[filenr]);
428    
429          if (save_dec_flag)                  if (ARG_SAVEDECOUTPUT)
430          {          {
431                  sprintf(filename, "%sdec%05d.pgm", filepath, filenr);                  sprintf(filename, "%sdec%05d.pgm", filepath, filenr);
432                  write_pgm(filename,out_buffer);                  write_pgm(filename,out_buffer);
433          }          }
434    
435          if (pgmflag)                  /* Read the header if it's pgm stream */
436                  status = read_pgmheader(stdin);         // because if this was the last PGM, stop now                  if (ARG_INPUTTYPE)
437                            status = read_pgmheader(in_file);
438    
439          filenr++;          filenr++;
440    
441     } while ( (!status) && (filenr<MAXFILENR) );          } while ( (!status) && (filenr<ARG_MAXFRAMENR) );
442    
443    
444    
445  /*********************************************************************/  /*****************************************************************************
446  /*     calculate totals and averages for output, print results       */   *         Calculate totals and averages for output, print results
447  /*********************************************************************/   ****************************************************************************/
448    
449          totalsize    /= filenr;          totalsize    /= filenr;
450          totalenctime /= filenr;          totalenctime /= filenr;
# Line 612  Line 462 
462                          Iframes++;                          Iframes++;
463                          Ipsnr += framepsnr[i];                          Ipsnr += framepsnr[i];
464                          break;                          break;
                 case 2:  
465                  default:                  default:
                         Bframes++;  
                         Bpsnr += framepsnr[i];  
466                          break;                          break;
467                  }                  }
468          }          }
# Line 624  Line 471 
471                  Ppsnr /= Pframes;                  Ppsnr /= Pframes;
472          if (Iframes)          if (Iframes)
473                  Ipsnr /= Iframes;                  Ipsnr /= Iframes;
         if (Bframes)  
                 Bpsnr /= Bframes;  
   
474    
475          for (i=0;i<filenr;i++)  // calculate statistics for every frametype: P,I (and B)          /* calculate statistics for every frametype: P,I */
476            for (i=0;i<filenr;i++)
477          {          {
478                  switch (frame_type[i])                  switch (frame_type[i])
479                  {                  {
# Line 645  Line 490 
490                          if (framepsnr[i] < Pminpsnr)                          if (framepsnr[i] < Pminpsnr)
491                                  Iminpsnr = framepsnr[i];                                  Iminpsnr = framepsnr[i];
492                          Ivarpsnr += (framepsnr[i] - Ipsnr)*(framepsnr[i] - Ipsnr) /Iframes;                          Ivarpsnr += (framepsnr[i] - Ipsnr)*(framepsnr[i] - Ipsnr) /Iframes;
493                          break;                  default:
                 case 2:  
                         if (framepsnr[i] > Bmaxpsnr)  
                                 Bmaxpsnr = framepsnr[i];  
                         if (framepsnr[i] < Pminpsnr)  
                                 Bminpsnr = framepsnr[i];  
                         Bvarpsnr += (framepsnr[i] - Bpsnr)*(framepsnr[i] - Bpsnr) /Bframes;  
494                          break;                          break;
495                  }                  }
496          }          }
497    
498            /* Print all statistics */
499          printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br"));          printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br"));
500          printf("%04d ",MAX(ARG_QUANTI,ARG_BITRATE));          printf("%04d ",(ARG_QUANTI)?ARG_QUANTI:ARG_BITRATE);
501          printf("(%.2f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE);          printf("(%.2f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE);
502          printf("size %6d ",totalsize);          printf("size %6d ", (int)totalsize);
503          printf("(%4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000));          printf("(%4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000));
504          printf("/ %.2f bpp) ",(double)totalsize*8/XDIM/YDIM);          printf("/ %.2f bpp) ",(double)totalsize*8/XDIM/YDIM);
505          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);
506          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));
507          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));
         if (Bframes)  
                 printf("B(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Bframes,Bpsnr,Bminpsnr,Bmaxpsnr,sqrt(Bvarpsnr/filenr));  
508          printf("\n");          printf("\n");
509    
510  /*********************************************************************/  /*****************************************************************************
511  /*                         XviD PART  Stop                           */   *                            XviD PART  Stop
512  /*********************************************************************/   ****************************************************************************/
513    
514  release_all:  release_all:
515    
# Line 679  Line 517 
517          {          {
518                  status = enc_stop();                  status = enc_stop();
519                  if (status)                  if (status)
520                          printf("Encore RELEASE problem return value %d\n", status);                          fprintf(stderr, "Encore RELEASE problem return value %d\n", status);
521          }          }
522    
523          if (dec_handle)          if (dec_handle)
524          {          {
525                  status = dec_stop();                  status = dec_stop();
526                  if (status)                  if (status)
527                          printf("Decore RELEASE problem return value %d\n", status);                          fprintf(stderr, "Decore RELEASE problem return value %d\n", status);
528          }          }
529    
530            fclose(in_file);
531    
532  free_all_memory:  free_all_memory:
533          free(out_buffer);          free(out_buffer);
# Line 696  Line 535 
535          free(in_buffer);          free(in_buffer);
536    
537    return 0;    return 0;
538    
539  }  }
540    
541    /*****************************************************************************
542     *                        "statistical" functions
543     *
544     *  these are not needed for encoding or decoding, but for measuring
545     *  time and quality, there in nothing specific to XviD in these
546     *
547     *****************************************************************************/
548    
549    
550    
551    /* Return time elapsed time in miliseconds since the program started */
552    static double msecond()
553    {
554    #ifndef _MSC_VER
555            struct timeval  tv;
556            gettimeofday(&tv, 0);
557            return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3;
558    #else
559            clock_t clk;
560            clk = clock();
561            return clk * 1000 / CLOCKS_PER_SEC;
562    #endif
563    }
564    
565    
566    /*
567     * Returns the sum of squared distances (SSD) between two images of dimensions
568     * x times y
569     */
570    static double absdistq(int x, int y,
571                                               unsigned char* buf1, int stride1,
572                                               unsigned char* buf2, int stride2)
573    {
574            double dist=0.;
575            int i,j,val;
576    
577            for (i=0;i<y;i++)
578            {
579                    val=0;
580                    for (j=0;j<x;j++)
581                            val+= ((int)buf1[j]-(int)buf2[j])*((int)buf1[j]-(int)buf2[j]);
582    
583                    dist += (double)val;
584                    buf1 += stride1;
585                    buf2 += stride2;
586            }
587            return dist/(x*y);
588    }
589    
590    
591    /*
592     * Returns the PSNR between to images.
593     *
594     * This is a common logarithmic measure for "quality" from the world of signal
595     * processing if you don't know what it is, simply accept that higher values
596     * are better.
597     *
598     * PSNR represents the ratio of useful signal over noise signal. In our case,
599     * useful signal is refernce image, noise signal is the difference between
600     * reference and decoded frame from encoded bitstream.
601     *
602     * The problem is this type of value is dependant of image source and so, is
603     * not reliable as a common "quality" indicator.
604     * So PSNR computes the ratio of maximum/noise. Maximum being set to 2^bpp/channel
605     * This way, PSNR is not dependant anymore of image data type.
606     *
607     */
608    static double PSNR(int x, int y,
609                                       unsigned char* buf1, int stride1,
610                                       unsigned char* buf2, int stride2)
611    {
612            return 10*(log10(255*255)-log10( absdistq(x, y, buf1, stride1, buf2, stride2) ));
613    }
614    
615    /*****************************************************************************
616     *                             Usage message
617     *****************************************************************************/
618    
619    static void usage()
620    {
621    
622            fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n");
623            fprintf(stderr, "Options :\n");
624            fprintf(stderr, " -w integer     : frame width ([1.2048])\n");
625            fprintf(stderr, " -h integer     : frame height ([1.2048])\n");
626            fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");
627            fprintf(stderr, " -f float       : target framerate (>0)\n");
628            fprintf(stderr, " -i string      : input filename (default=stdin)\n");
629            fprintf(stderr, " -t integer     : input data type (yuv=0, pgm=1)\n");
630            fprintf(stderr, " -n integer     : number of frames to encode\n");
631            fprintf(stderr, " -q integer     : quality ([0..5])\n");
632            fprintf(stderr, " -d boolean     : save decoder output (0 False*, !=0 True)\n");
633            fprintf(stderr, " -m boolean     : save mpeg4 raw stream (0 False*, !=0 True)\n");
634            fprintf(stderr, " -help          : prints this help message\n");
635            fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n");
636            fprintf(stderr, " (* means default)\n");
637    
638    }
639    
640    /*****************************************************************************
641     *                       Input and output functions
642     *
643     *      the are small and simple routines to read and write PGM and YUV
644     *      image. It's just for convenience, again nothing specific to XviD
645     *
646     *****************************************************************************/
647    
648    static int read_pgmheader(FILE* handle)
649    {
650            int bytes,xsize,ysize,depth;
651            char dummy[2];
652    
653            bytes = fread(dummy,1,2,handle);
654    
655            if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' ))
656                    return 1;
657    
658            fscanf(handle,"%d %d %d",&xsize,&ysize,&depth);
659            if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) )
660            {
661                    fprintf(stderr,"%d %d %d\n",xsize,ysize,depth);
662                    return 2;
663            }
664            if ( (XDIM==0) || (YDIM==0) )
665            {
666                    XDIM=xsize;
667                    YDIM=ysize*2/3;
668            }
669    
670            return 0;
671    }
672    
673    static int read_pgmdata(FILE* handle, unsigned char *image)
674    {
675            int i;
676            char dummy;
677    
678            unsigned char *y = image;
679            unsigned char *u = image + XDIM*YDIM;
680            unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2;
681    
682            /* read Y component of picture */
683            fread(y, 1, XDIM*YDIM, handle);
684    
685            for (i=0;i<YDIM/2;i++)
686            {
687                    /* read U */
688                    fread(u, 1, XDIM/2, handle);
689    
690                    /* read V */
691                    fread(v, 1, XDIM/2, handle);
692    
693                    /* Update pointers */
694                    u += XDIM/2;
695                    v += XDIM/2;
696            }
697    
698        /*  I don't know why, but this seems needed */
699            fread(&dummy, 1, 1, handle);
700    
701            return 0;
702    }
703    
704    static int read_yuvdata(FILE* handle, unsigned char *image)
705    {
706    
707            if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM))
708                    return 1;
709            else
710                    return 0;
711    }
712    
713    static int write_pgm(char *filename, unsigned char *image)
714    {
715            int loop;
716    
717            unsigned char *y = image;
718            unsigned char *u = image + XDIM*YDIM;
719            unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2;
720    
721            FILE *filehandle;
722            filehandle=fopen(filename,"w+b");
723            if (filehandle)
724            {
725                    /* Write header */
726                    fprintf(filehandle,"P5\n\n%d %d 255\n", XDIM,YDIM*3/2);
727    
728                    /* Write Y data */
729                    fwrite(y, 1, XDIM*YDIM, filehandle);
730    
731                    for(loop=0; loop<YDIM/2; loop++)
732                    {
733                            /* Write U scanline */
734                            fwrite(u, 1, XDIM/2, filehandle);
735    
736                            /* Write V scanline */
737                            fwrite(v, 1, XDIM/2, filehandle);
738    
739                            /* Update pointers */
740                            u += XDIM/2;
741                            v += XDIM/2;
742    
743                    }
744    
745                    /* Close file */
746                    fclose(filehandle);
747    
748                    return 0;
749            }
750            else
751                    return 1;
752    }
753    
754    /*****************************************************************************
755     *     Routines for encoding: init encoder, frame step, release encoder
756     ****************************************************************************/
757    
758    #define FRAMERATE_INCR 1001
759    
760    /* Initialize encoder for first use, pass all needed parameters to the codec */
761    static int enc_init(int use_assembler)
762    {
763            int xerr;
764    
765            XVID_INIT_PARAM xinit;
766            XVID_ENC_PARAM xparam;
767    
768            if(use_assembler) {
769    
770    #ifdef ARCH_IA64
771                    xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
772    #else
773                    xinit.cpu_flags = 0;
774    #endif
775            }
776            else {
777                    xinit.cpu_flags = XVID_CPU_FORCE;
778            }
779    
780            xvid_init(NULL, 0, &xinit, NULL);
781    
782            xparam.width = XDIM;
783            xparam.height = YDIM;
784            if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS)
785            {
786                    xparam.fincr = 1;
787                    xparam.fbase = (int)ARG_FRAMERATE;
788            }
789            else
790            {
791                    xparam.fincr = FRAMERATE_INCR;
792                    xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE);
793            }
794            xparam.rc_reaction_delay_factor = 16;
795        xparam.rc_averaging_period = 100;
796        xparam.rc_buffer = 10;
797            xparam.rc_bitrate = ARG_BITRATE*1000;
798            xparam.min_quantizer = ARG_MINQUANT;
799            xparam.max_quantizer = ARG_MAXQUANT;
800            xparam.max_key_interval = (int)ARG_FRAMERATE*10;
801    
802            /* I use a small value here, since will not encode whole movies, but short clips */
803    
804            xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL);
805            enc_handle=xparam.handle;
806    
807            return xerr;
808    }
809    
810    static int enc_stop()
811    {
812            int xerr;
813    
814            xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL);
815            return xerr;
816    
817    }
818    
819    static int enc_main(unsigned char* image, unsigned char* bitstream,
820                                            int *streamlength, int* frametype)
821    {
822            int xerr;
823    
824            XVID_ENC_FRAME xframe;
825            XVID_ENC_STATS xstats;
826    
827            xframe.bitstream = bitstream;
828            xframe.length = -1;     /* this is written by the routine */
829    
830            xframe.image = image;
831            xframe.colorspace = XVID_CSP_YV12;      /* defined in <xvid.h> */
832    
833            xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */
834    
835            xframe.quant = ARG_QUANTI;      /* is quant != 0, use a fixed quant (and ignore bitrate) */
836    
837            xframe.motion = motion_presets[ARG_QUALITY];
838            xframe.general = general_presets[ARG_QUALITY];
839            xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL;
840    
841            xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats);
842    
843            /*
844             * This is statictical data, e.g. for 2-pass. If you are not
845             * interested in any of this, you can use NULL instead of &xstats
846             */
847            *frametype = xframe.intra;
848            *streamlength = xframe.length;
849    
850            return xerr;
851    }
852    
853    /*****************************************************************************
854     * Routines for decoding: init encoder, frame step, release encoder
855     ****************************************************************************/
856    
857    /* init decoder before first run */
858    static int dec_init(int use_assembler)
859    {
860            int xerr;
861    
862            XVID_INIT_PARAM xinit;
863            XVID_DEC_PARAM xparam;
864    
865            if(use_assembler)
866    
867    #ifdef ARCH_IA64
868                    xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
869    #else
870            xinit.cpu_flags = 0;
871    #endif
872    
873            else
874                    xinit.cpu_flags = XVID_CPU_FORCE;
875    
876            xvid_init(NULL, 0, &xinit, NULL);
877            xparam.width = XDIM;
878            xparam.height = YDIM;
879    
880            xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);
881            dec_handle = xparam.handle;
882    
883            return xerr;
884    }
885    
886    /* decode one frame  */
887    static int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer,
888                                            int m4v_size)
889    {
890            int xerr;
891            XVID_DEC_FRAME xframe;
892    
893            xframe.bitstream = m4v_buffer;
894            xframe.length = m4v_size;
895            xframe.image = out_buffer;
896            xframe.stride = XDIM;
897            xframe.colorspace = XVID_CSP_YV12;             /* XVID_CSP_USER is fastest (no memcopy involved) */
898    
899            xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL);
900    
901            return xerr;
902    }
903    
904    /* close decoder to release resources */
905    static int dec_stop()
906    {
907            int xerr;
908            xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
909    
910            return xerr;
911    }
912    
913    /* EOF */

Legend:
Removed from v.119  
changed lines
  Added in v.684

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