[svn] / branches / release-1_3-branch / xvidcore / examples / xvid_encraw.c Repository:
ViewVC logotype

Diff of /branches/release-1_3-branch/xvidcore/examples/xvid_encraw.c

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

revision 376, Sat Aug 17 20:03:36 2002 UTC revision 1794, Fri Nov 14 15:43:28 2008 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-2003 Christoph Lampert <gruel@web.de>
7     *               2002-2003 Edouard Gomez <ed.gomez@free.fr>
8     *               2003      Peter Ross <pross@xvid.org>
9   *   *
10   *      This program is free software; you can redistribute it and/or modify   *      This program is free software; you can redistribute it and/or modify
11   *      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 19 
19   *   *
20   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
21   *      along with this program; if not, write to the Free Software   *      along with this program; if not, write to the Free Software
22   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
24   *************************************************************************/   * $Id: xvid_encraw.c,v 1.35 2008-11-14 15:43:27 Isibaar Exp $
   
 /************************************************************************  
25   *   *
26   *  Speed test routine for XviD using the XviD-API   ****************************************************************************/
27   *  (C) Christoph Lampert, 2002/08/17  
28    /*****************************************************************************
29     *  Application notes :
30   *   *
31   *  A sequence of YUV pics in PGM or RAW file format is encoded and the   *  A sequence of raw YUV I420 pics or YUV I420 PGM file format is encoded
32   *  raw MPEG-4 stream is written to stdout.   *  The speed is measured and frames' PSNR are taken from core.
  *  The encoding speed of this is measured, too.  
33   *   *
34   *  The program is plain C and needs no libraries except for libxvidcore,   *  The program is plain C and needs no libraries except for libxvidcore,
35   *  and maths-lib, so with UN*X you simply compile by   *  and maths-lib.
  *  
  *   gcc xvid_encraw.c -lxvidcore -lm -o xvid_encraw  
  *  
  *  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)  
  *  
  *  BITRATE is in kbps (default 900),  
  *      if BITRATE<32, then value is taken is fixed QUANTIZER  
  *  
  *  FRAMERATE is a float (with or without decimal dot), default is 25.00  
  *  
  *  input/output and m4v-output is saved, if corresponding flags are set  
  *  
  *  PGM input must in a very specific format, see read_pgmheader  
  *  it can be generated e.g. from MPEG2 by    mpeg2dec -o pgmpipe  
  *  
  ************************************************************************/  
   
 /************************************************************************  
36   *   *
37   *  For EXAMPLES how to use this, see the seperate file xvid_stat.examples   *  Use ./xvid_encraw -help for a list of options
38   *   *
39   ************************************************************************/   ************************************************************************/
40    
41  #include <stdio.h>  #include <stdio.h>
42    //#include <io.h>
43  #include <stdlib.h>  #include <stdlib.h>
44  #include <math.h>               // needed for log10  #include <string.h>
45  #include <sys/time.h>           // only needed for gettimeofday  #include <math.h>
46    #ifndef WIN32
47    #include <sys/time.h>
48    #else
49    #include <windows.h>
50    #include <vfw.h>
51    #include <time.h>
52    #define XVID_AVI_INPUT
53    #define XVID_AVI_OUTPUT
54    #endif
55    
56    #include "xvid.h"
57    
58    #ifdef XVID_MKV_OUTPUT
59    #include "matroska.cpp"
60    #endif
61    
62    #undef READ_PNM
63    
64    /*****************************************************************************
65     *                            Quality presets
66     ****************************************************************************/
67    
68    // Equivalent to vfw's pmvfast_presets
69    static const int motion_presets[] = {
70            /* quality 0 */
71            0,
72    
73            /* quality 1 */
74            0,
75    
76            /* quality 2 */
77            0,
78    
79            /* quality 3 */
80            0,
81    
82            /* quality 4 */
83            0 | XVID_ME_HALFPELREFINE16 | 0,
84    
85            /* quality 5 */
86            0 | XVID_ME_HALFPELREFINE16 | 0 | XVID_ME_ADVANCEDDIAMOND16,
87    
88  #include "../src/xvid.h"                /* comes with XviD */          /* quality 6 */
89            XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16
90    
 int motion_presets[7] = {  
         0,                                                              // Q 0  
         PMV_EARLYSTOP16,                                                // Q 1  
         PMV_EARLYSTOP16,                                                // Q 2  
         PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,                          // Q 3  
         PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,                          // Q 4  
         PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8          // Q 5  
                         | PMV_HALFPELREFINE8,  
         PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16         // Q 6  
                         | PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8  
91          };          };
92    #define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0]))
93    
94  int general_presets[7] = {  static const int vop_presets[] = {
95          XVID_H263QUANT, /* or use XVID_MPEGQUANT */             // Q 0          /* quality 0 */
96          XVID_MPEGQUANT,                                         // Q 1          0,
         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  
97    
98            /* quality 1 */
99            0,
100    
101  /* my default values for encoding */          /* quality 2 */
102            0,
103    
104  #define ABS_MAXFRAMENR 9999               // max number of frames          /* quality 3 */
105            0,
106    
107  int ARG_BITRATE=900;          /* quality 4 */
108  int ARG_QUANTI=0;          0,
109    
110  int ARG_QUALITY =6;          /* quality 5 */
111  int ARG_MINQUANT=1;          XVID_VOP_INTER4V,
 int ARG_MAXQUANT=31;  
 float ARG_FRAMERATE=25.00;  
112    
113  int ARG_MAXFRAMENR=ABS_MAXFRAMENR;          /* quality 6 */
114            XVID_VOP_INTER4V,
115    
116  #ifdef BFRAMES  };
117    #define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0]))
118    
119    /*****************************************************************************
120     *                     Command line global variables
121     ****************************************************************************/
122    
123    #define MAX_ZONES   64
124    
125    #define DEFAULT_QUANT 400
126    
127    typedef struct
128    {
129            int frame;
130    
131            int type;
132            int mode;
133            int modifier;
134    
135  int ARG_MAXBFRAMES=1;          unsigned int greyscale;
136  int ARG_BQUANTRATIO=200;          unsigned int chroma_opt;
137            unsigned int bvop_threshold;
138            unsigned int cartoon_mode;
139    } zone_t;
140    
141    typedef struct
142    {
143            int count;
144            int size;
145            int quants[32];
146    } frame_stats_t;
147    
148    /* Maximum number of frames to encode */
149    #define ABS_MAXFRAMENR -1 /* no limit */
150    
151    #ifndef READ_PNM
152    #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)
153    #else
154    #define IMAGE_SIZE(x,y) ((x)*(y)*3)
155  #endif  #endif
156    
157  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
158  #define SMALL_EPS 1e-10  #define SMALL_EPS (1e-10)
159    
160  /* these are global variables. Not very elegant, but easy, and this is an easy program */  #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \
161                      (((a)&0x00ff0000)>>8)  | (((a)&0xff000000)>>24) )
162    
163    static zone_t ZONES[MAX_ZONES];
164    static  int NUM_ZONES = 0;
165    static  frame_stats_t framestats[7];
166    
167    static  int ARG_STATS = 0;
168    static  int ARG_SSIM = -1;
169    static  char* ARG_SSIM_PATH = NULL;
170    static  int ARG_DUMP = 0;
171    static  int ARG_LUMIMASKING = 0;
172    static  int ARG_BITRATE = 0;
173    static  int ARG_TARGETSIZE = 0;
174    static  int ARG_SINGLE = 1;
175    static  char *ARG_PASS1 = 0;
176    static  char *ARG_PASS2 = 0;
177    //static int ARG_QUALITY = ME_ELEMENTS - 1;
178    static  int ARG_QUALITY = 6;
179    static  float ARG_FRAMERATE = 25.00f;
180    static  int ARG_DWRATE = 25;
181    static  int ARG_DWSCALE = 1;
182    static  int ARG_MAXFRAMENR = ABS_MAXFRAMENR;
183    static  int ARG_MAXKEYINTERVAL = 300;
184    static  int ARG_STARTFRAMENR = 0;
185    static  char *ARG_INPUTFILE = NULL;
186    static  int ARG_INPUTTYPE = 0;
187    static  int ARG_SAVEMPEGSTREAM = 0;
188    static  int ARG_SAVEINDIVIDUAL = 0;
189    static  char *ARG_OUTPUTFILE = NULL;
190    #ifdef XVID_AVI_OUTPUT
191    static  char *ARG_AVIOUTPUTFILE = NULL;
192    #endif
193    #ifdef XVID_MKV_OUTPUT
194    static  char *ARG_MKVOUTPUTFILE = NULL;
195    #endif
196    #ifdef XVID_AVI_INPUT
197    static  PAVISTREAM avi_stream = NULL;
198    static  PAVIFILE avi_file = NULL;
199    static  LPBITMAPINFOHEADER info_header = NULL;
200    static  PGETFRAME get_frame = NULL;
201    #else
202    #define get_frame NULL
203    #endif
204    static  char *ARG_TIMECODEFILE = NULL;
205    static  int XDIM = 0;
206    static  int YDIM = 0;
207    static  int ARG_BQRATIO = 150;
208    static  int ARG_BQOFFSET = 100;
209    static  int ARG_MAXBFRAMES = 2;
210    static  int ARG_PACKED = 1;
211    static  int ARG_DEBUG = 0;
212    static  int ARG_VOPDEBUG = 0;
213    static  int ARG_TRELLIS = 1;
214    static  int ARG_QTYPE = 0;
215    static  int ARG_QMATRIX = 0;
216    static  int ARG_GMC = 0;
217    static  int ARG_INTERLACING = 0;
218    static  int ARG_QPEL = 0;
219    static  int ARG_TURBO = 0;
220    static  int ARG_VHQMODE = 1;
221    static  int ARG_BVHQ = 0;
222    static  int ARG_CLOSED_GOP = 1;
223    static  int ARG_CHROMAME = 1;
224    static  int ARG_PAR = 1;
225    static  int ARG_PARHEIGHT;
226    static  int ARG_PARWIDTH;
227    static  int ARG_QUANTS[6] = {2, 31, 2, 31, 2, 31};
228    static  int ARG_FRAMEDROP = 0;
229    static  double ARG_CQ = 0;
230    static  int ARG_FULL1PASS = 0;
231    static  int ARG_REACTION = 16;
232    static  int ARG_AVERAGING = 100;
233    static  int ARG_SMOOTHER = 100;
234    static  int ARG_KBOOST = 10;
235    static  int ARG_KREDUCTION = 20;
236    static  int ARG_KTHRESH = 1;
237    static  int ARG_CHIGH = 0;
238    static  int ARG_CLOW = 0;
239    static  int ARG_OVERSTRENGTH = 5;
240    static  int ARG_OVERIMPROVE = 5;
241    static  int ARG_OVERDEGRADE = 5;
242    static  int ARG_OVERHEAD = 0;
243    static  int ARG_VBVSIZE = 0;
244    static  int ARG_VBVMAXRATE = 0;
245    static  int ARG_VBVPEAKRATE = 0;
246    static  int ARG_THREADS = 0;
247    static  int ARG_VFR = 0;
248    static  int ARG_PROGRESS = 0;
249    static  int ARG_COLORSPACE = XVID_CSP_YV12;
250            /* the path where to save output */
251    static char filepath[256] = "./";
252            /* Internal structures (handles) for encoding and decoding */
253    static  void *enc_handle = NULL;
254    static  unsigned char qmatrix_intra[64];
255    static  unsigned char qmatrix_inter[64];
256    
257    /****************************************************************************
258     *                     Nasty global vars ;-)
259     ***************************************************************************/
260    
261    static const int height_ratios[] = {1, 1, 11, 11, 11, 33};
262    static const int width_ratios[] = {1, 1, 12, 10, 16, 40};
263    
264    const char userdata_start_code[] = "\0\0\x01\xb2";
265    
266    
267    /*****************************************************************************
268     *               Local prototypes
269     ****************************************************************************/
270    
271    /* Prints program usage message */
272    static void usage();
273    
274    /* Statistical functions */
275    static double msecond();
276    int gcd(int a, int b);
277    int minquant(int quants[32]);
278    int maxquant(int quants[32]);
279    double avgquant(frame_stats_t frame);
280    
281    /* PGM related functions */
282    #ifndef READ_PNM
283    static int read_pgmheader(FILE * handle);
284    static int read_pgmdata(FILE * handle,
285                                                    unsigned char *image);
286    #else
287    static int read_pnmheader(FILE * handle);
288    static int read_pnmdata(FILE * handle,
289                                                    unsigned char *image);
290    #endif
291    static int read_yuvdata(FILE * handle,
292                                                    unsigned char *image);
293    
294  int XDIM=0;  /* Encoder related functions */
295  int YDIM=0;     // will be set when reading first image  static int enc_init(int use_assembler);
296  int i,filenr = 0;  static int enc_info();
297    static int enc_stop();
298    static int enc_main(unsigned char *image,
299                                            unsigned char *bitstream,
300                                            int *key,
301                                            int *stats_type,
302                                            int *stats_quant,
303                                            int *stats_length,
304                                            int stats[3],
305                                            int framenum);
306    
307    /* Zone Related Functions */
308    static void apply_zone_modifiers(xvid_enc_frame_t * frame, int framenum);
309    static void prepare_full1pass_zones();
310    static void prepare_cquant_zones();
311    void sort_zones(zone_t * zones, int zone_num, int * sel);
312    
313    
314    void removedivxp(char *buf, int size);
315    
316    /*****************************************************************************
317     *               Main function
318     ****************************************************************************/
319    
320    int
321    main(int argc,
322             char *argv[])
323    {
324    
325  int save_m4v_flag = 1;          // output MPEG4-bytestream?          unsigned char *mp4_buffer = NULL;
326  int save_ref_flag = 0;          // save input image          unsigned char *in_buffer = NULL;
327            unsigned char *out_buffer = NULL;
328    
329  int pgmflag = 0;                // a flag, if input is in PGM format, overwritten in init-phase          double enctime;
330  char filepath[256] = "./";      // the path where to save output          double totalenctime = 0.;
331            float totalPSNR[3] = {0., 0., 0.};
332            FILE *statsfile;
333    
334  void *enc_handle = NULL;                // internal structures (handles) for encoding          int totalsize;
335            int result;
336            int m4v_size;
337            int key;
338            int stats_type;
339            int stats_quant;
340            int stats_length;
341            int use_assembler = 1;
342            int fakenvop = 0;
343            int i;
344            int nvop_counter;
345    
346            int input_num;
347            int output_num;
348    
349  /*********************************************************************/          char filename[256];
 /*                     "statistical" functions                       */  
 /*                                                                   */  
 /*  these are not needed for encoding or decoding, but for measuring */  
 /*  time and quality, there in nothing specific to XviD in these     */  
 /*                                                                   */  
 /*********************************************************************/  
350    
351  double msecond()          FILE *in_file = stdin;
352  /* return the current time in seconds(!)  */          FILE *out_file = NULL;
353  {          FILE *time_file = NULL;
354          struct timeval  tv;  
355          gettimeofday(&tv, 0);  #ifdef XVID_AVI_OUTPUT
356          return tv.tv_sec + tv.tv_usec * 1.0e-6;          int avierr;
357            PAVIFILE myAVIFile=NULL;
358            PAVISTREAM myAVIStream=NULL;
359            AVISTREAMINFO myAVIStreamInfo;
360            BITMAPINFOHEADER myBitmapInfoHeader;
361            AVIFileInit();
362    #endif
363    #ifdef XVID_MKV_OUTPUT
364            PMKVFILE myMKVFile=NULL;
365            PMKVSTREAM myMKVStream=NULL;
366            MKVSTREAMINFO myMKVStreamInfo;
367    #endif
368    
369            printf("xvid_encraw - raw mpeg4 bitstream encoder ");
370            printf("written by Christoph Lampert 2002-2003\n\n");
371    
372            /* Is there a dumb XviD coder ? */
373            if(ME_ELEMENTS != VOP_ELEMENTS) {
374                    fprintf(stderr, "Presets' arrays should have the same number of elements -- Please fill a bug to xvid-devel@xvid.org\n");
375                    return(-1);
376            }
377    
378    /*****************************************************************************
379     *                            Command line parsing
380     ****************************************************************************/
381    
382            for (i = 1; i < argc; i++) {
383    
384                    if (strcmp("-asm", argv[i]) == 0) {
385                            use_assembler = 1;
386                    } else if (strcmp("-noasm", argv[i]) == 0) {
387                            use_assembler = 0;
388                    } else if (strcmp("-w", argv[i]) == 0 && i < argc - 1) {
389                            i++;
390                            XDIM = atoi(argv[i]);
391                    } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) {
392                            i++;
393                            YDIM = atoi(argv[i]);
394                    } else if (strcmp("-csp",argv[i]) == 0 && i < argc - 1) {
395                            i++;
396                            if (strcmp(argv[i],"i420") == 0){
397                                    ARG_COLORSPACE = XVID_CSP_I420;
398                            } else if(strcmp(argv[i],"yv12") == 0){
399                                    ARG_COLORSPACE = XVID_CSP_YV12;
400                            } else {
401                                    printf("Invalid colorspace\n");
402                                    return 0;
403                            }
404                    } else if (strcmp("-bitrate", argv[i]) == 0) {
405                            if (i < argc - 1)
406                                    ARG_BITRATE = atoi(argv[i+1]);
407                            if (ARG_BITRATE) {
408                                    i++;
409                                    if (ARG_BITRATE <= 10000)
410                                            /* if given parameter is <= 10000, assume it means kbps */
411                                            ARG_BITRATE *= 1000;
412                            }
413                            else
414                                    ARG_BITRATE = 700000;
415                    } else if (strcmp("-size", argv[i]) == 0 && i < argc - 1) {
416                            i++;
417                            ARG_TARGETSIZE = atoi(argv[i]);
418            } else if (strcmp("-cq", argv[i]) == 0 && i < argc - 1) {
419                            i++;
420                            ARG_CQ = atof(argv[i])*100;
421                    } else if (strcmp("-single", argv[i]) == 0) {
422                            ARG_SINGLE = 1;
423                            ARG_PASS1 = NULL;
424                            ARG_PASS2 = NULL;
425                    } else if (strcmp("-pass1", argv[i]) == 0) {
426                            ARG_SINGLE = 0;
427                            if ((i < argc - 1) && (*argv[i+1] != '-')) {
428                                    i++;
429                                    ARG_PASS1 = argv[i];
430                            } else {
431                                    ARG_PASS1 = "xvid.stats";
432                            }
433                    } else if (strcmp("-full1pass", argv[i]) == 0) {
434                            ARG_FULL1PASS = 1;
435                    } else if (strcmp("-pass2", argv[i]) == 0) {
436                            ARG_SINGLE = 0;
437                            if ((i < argc - 1) && (*argv[i+1] != '-')) {
438                                    i++;
439                                    ARG_PASS2 = argv[i];
440                            } else {
441                                    ARG_PASS2 = "xvid.stats";
442                            }
443                    } else if (strcmp("-max_bframes", argv[i]) == 0 && i < argc - 1) {
444                            i++;
445                            ARG_MAXBFRAMES = atoi(argv[i]);
446                    } else if (strcmp("-par", argv[i]) == 0 && i < argc - 1) {
447                            i++;
448                            if (sscanf(argv[i], "%d:%d", &(ARG_PARWIDTH), &(ARG_PARHEIGHT))!=2)
449                                    ARG_PAR = atoi(argv[i]);
450                            else {
451                                    int div;
452                                    ARG_PAR = 0;
453                                    div = gcd(ARG_PARWIDTH, ARG_PARHEIGHT);
454                                    ARG_PARWIDTH /= div;
455                                    ARG_PARHEIGHT /= div;
456                            }
457                    } else if (strcmp("-nopacked", argv[i]) == 0) {
458                            ARG_PACKED = 0;
459                    } else if (strcmp("-packed", argv[i]) == 0) {
460                            ARG_PACKED = 2;
461                    } else if (strcmp("-nochromame", argv[i]) == 0) {
462                            ARG_CHROMAME = 0;
463                    } else if (strcmp("-threads", argv[i]) == 0 && i < argc -1) {
464                            i++;
465                            ARG_THREADS = atoi(argv[i]);
466                    } else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) {
467                            i++;
468                            ARG_BQRATIO = atoi(argv[i]);
469                    } else if (strcmp("-bquant_offset", argv[i]) == 0 && i < argc - 1) {
470                            i++;
471                            ARG_BQOFFSET = atoi(argv[i]);
472    
473                    } else if (strcmp("-zones", argv[i]) == 0 && i < argc -1) {
474                            char c;
475                            char *frameoptions, *rem;
476                            int startframe;
477                            char options[40];
478    
479                            i++;
480    
481                            do {
482                                    rem = strrchr(argv[i], '/');
483                                    if (rem==NULL)
484                                            rem=argv[i];
485                                    else {
486                                            *rem = '\0';
487                                            rem++;
488                                    }
489                                    if (sscanf(rem, "%d,%c,%s", &startframe, &c, options)<3) {
490                                            fprintf(stderr, "Zone error, bad parameters %s\n", rem);
491                                            continue;
492                                    }
493                                    if (NUM_ZONES >= MAX_ZONES) {
494                                            fprintf(stderr, "warning: too many zones; zone ignored\n");
495                                            continue;
496                                    }
497                                    memset(&ZONES[NUM_ZONES], 0, sizeof(zone_t));
498    
499                                    ZONES[NUM_ZONES].frame = startframe;
500                                    ZONES[NUM_ZONES].modifier = atof(options)*100;
501                                    if (toupper(c)=='Q')
502                                            ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
503                                    else if (toupper(c)=='W')
504                                            ZONES[NUM_ZONES].mode = XVID_ZONE_WEIGHT;
505                                    else {
506                                            fprintf(stderr, "Bad zone type %c\n", c);
507                                            continue;
508  }  }
509    
510  /*********************************************************************/                                  if ((frameoptions=strchr(options, ','))!=NULL) {
511  /*                    input and output functions                     */                                          int readchar=0, count;
512  /*                                                                   */                                          frameoptions++;
513  /* the are small and simple routines to read and write PGM and YUV   */                                          while (readchar<strlen(frameoptions)) {
514  /* image. It's just for convenience, again nothing specific to XviD  */                                                  if (sscanf(frameoptions+readchar, "%d%n", &(ZONES[NUM_ZONES].bvop_threshold), &count)==1) {
515  /*                                                                   */                                                          readchar += count;
516  /*********************************************************************/                                                  }
517                                                    else {
518                                                            if (toupper(frameoptions[readchar])=='K')
519                                                                    ZONES[NUM_ZONES].type = XVID_TYPE_IVOP;
520                                                            else if (toupper(frameoptions[readchar])=='G')
521                                                                    ZONES[NUM_ZONES].greyscale = 1;
522                                                            else if (toupper(frameoptions[readchar])=='O')
523                                                                    ZONES[NUM_ZONES].chroma_opt = 1;
524                                                            else if (toupper(frameoptions[readchar])=='C')
525                                                                    ZONES[NUM_ZONES].cartoon_mode = 1;
526                                                            else {
527                                                                    fprintf(stderr, "Error in zone %s option %c\n", rem, frameoptions[readchar]);
528                                                                    break;
529                                                            }
530                                                            readchar++;
531                                                    }
532                                            }
533                                    }
534                                    NUM_ZONES++;
535                            } while (rem != argv[i]);
536    
 int read_pgmheader(FILE* handle)  
 {  
         int bytes,xsize,ysize,depth;  
         char dummy[2];  
537    
538          bytes = fread(dummy,1,2,handle);                  } else if ((strcmp("-zq", argv[i]) == 0 || strcmp("-zw", argv[i]) == 0) && i < argc - 2) {
539    
540          if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' ))              if (NUM_ZONES >= MAX_ZONES) {
541                  return 1;                  fprintf(stderr,"warning: too many zones; zone ignored\n");
542          fscanf(handle,"%d %d %d",&xsize,&ysize,&depth);                  continue;
         if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) )  
         {  
                 return 2;  
543          }          }
544          if ( (XDIM==0) || (YDIM==0) )                          memset(&ZONES[NUM_ZONES], 0, sizeof(zone_t));
545          {       XDIM=xsize;                          if (strcmp("-zq", argv[i])== 0) {
546                  YDIM=ysize;                                  ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
547                            }
548                            else {
549                                    ZONES[NUM_ZONES].mode = XVID_ZONE_WEIGHT;
550                            }
551                            ZONES[NUM_ZONES].modifier = atof(argv[i+2])*100;
552                            i++;
553                ZONES[NUM_ZONES].frame = atoi(argv[i]);
554                            i++;
555                            ZONES[NUM_ZONES].type = XVID_TYPE_AUTO;
556                            ZONES[NUM_ZONES].greyscale = 0;
557                            ZONES[NUM_ZONES].chroma_opt = 0;
558                            ZONES[NUM_ZONES].bvop_threshold = 0;
559                            ZONES[NUM_ZONES].cartoon_mode = 0;
560    
561                NUM_ZONES++;
562                    } else if (strcmp("-quality", argv[i]) == 0 && i < argc - 1) {
563                            i++;
564                            ARG_QUALITY = atoi(argv[i]);
565                    } else if (strcmp("-start", argv[i]) == 0 && i < argc - 1) {
566                            i++;
567                            ARG_STARTFRAMENR = atoi(argv[i]);
568                    } else if (strcmp("-vhqmode", argv[i]) == 0 && i < argc - 1) {
569                            i++;
570                            ARG_VHQMODE = atoi(argv[i]);
571                    } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) {
572                            int exponent;
573                            i++;
574                            ARG_FRAMERATE = (float) atof(argv[i]);
575                            exponent = strcspn(argv[i], ".");
576                            if (exponent<strlen(argv[i]))
577                                    exponent=pow(10.0, (int)(strlen(argv[i])-1-exponent));
578                            else
579                                    exponent=1;
580                            ARG_DWRATE = atof(argv[i])*exponent;
581                            ARG_DWSCALE = exponent;
582                            exponent = gcd(ARG_DWRATE, ARG_DWSCALE);
583                            ARG_DWRATE /= exponent;
584                            ARG_DWSCALE /= exponent;
585                    } else if (strcmp("-max_key_interval", argv[i]) == 0 && i < argc - 1) {
586                            i++;
587                            ARG_MAXKEYINTERVAL = atoi(argv[i]);
588                    } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1) {
589                            i++;
590                            ARG_INPUTFILE = argv[i];
591                    } else if (strcmp("-stats", argv[i]) == 0) {
592                            ARG_STATS = 1;
593                    } else if (strcmp("-ssim", argv[i]) == 0) {
594                            ARG_SSIM = 2;
595                            if ((i < argc - 1) && (*argv[i+1] != '-')) {
596                                    i++;
597                                    ARG_SSIM = atoi(argv[i]);
598                            }
599                    } else if (strcmp("-ssim_file", argv[i]) == 0 && i < argc -1) {
600                            i++;
601                            ARG_SSIM_PATH = argv[i];
602                    } else if (strcmp("-timecode", argv[i]) == 0 && i < argc -1) {
603                            i++;
604                            ARG_TIMECODEFILE = argv[i];
605                    } else if (strcmp("-dump", argv[i]) == 0) {
606                            ARG_DUMP = 1;
607                    } else if (strcmp("-lumimasking", argv[i]) == 0) {
608                            ARG_LUMIMASKING = 1;
609                    } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {
610                            i++;
611                            ARG_INPUTTYPE = atoi(argv[i]);
612                    } else if (strcmp("-frames", argv[i]) == 0 && i < argc - 1) {
613                            i++;
614                            ARG_MAXFRAMENR = atoi(argv[i]);
615                    } else if (strcmp("-drop", argv[i]) == 0 && i < argc - 1) {
616                            i++;
617                            ARG_FRAMEDROP = atoi(argv[i]);
618                    } else if (strcmp("-imin", argv[i]) == 0 && i < argc - 1) {
619                            i++;
620                            ARG_QUANTS[0] = atoi(argv[i]);
621                    } else if (strcmp("-imax", argv[i]) == 0 && i < argc - 1) {
622                            i++;
623                            ARG_QUANTS[1] = atoi(argv[i]);
624                    } else if (strcmp("-pmin", argv[i]) == 0 && i < argc - 1) {
625                            i++;
626                            ARG_QUANTS[2] = atoi(argv[i]);
627                    } else if (strcmp("-pmax", argv[i]) == 0 && i < argc - 1) {
628                            i++;
629                            ARG_QUANTS[3] = atoi(argv[i]);
630                    } else if (strcmp("-bmin", argv[i]) == 0 && i < argc - 1) {
631                            i++;
632                            ARG_QUANTS[4] = atoi(argv[i]);
633                    } else if (strcmp("-bmax", argv[i]) == 0 && i < argc - 1) {
634                            i++;
635                            ARG_QUANTS[5] = atoi(argv[i]);
636                    } else if (strcmp("-qtype", argv[i]) == 0 && i < argc - 1) {
637                            i++;
638                            ARG_QTYPE = atoi(argv[i]);
639                    } else if (strcmp("-qmatrix", argv[i]) == 0 && i < argc - 1) {
640                            FILE *fp = fopen(argv[++i], "rb");
641                            if (fp == NULL) {
642                                    fprintf(stderr, "Error opening input file %s\n", argv[i]);
643                                    return (-1);
644                            }
645                            fseek(fp, 0, SEEK_END);
646                            if (ftell(fp) != 128) {
647                                    fprintf(stderr, "Unexpected size of input file %s\n", argv[i]);
648                                    return (-1);
649                            }
650    
651                            fseek(fp, 0, SEEK_SET);
652                            fread(qmatrix_intra, 1, 64, fp);
653                            fread(qmatrix_inter, 1, 64, fp);
654    
655                            ARG_QMATRIX = 1;
656                            ARG_QTYPE = 1;
657                    } else if (strcmp("-save", argv[i]) == 0) {
658                            ARG_SAVEMPEGSTREAM = 1;
659                            ARG_SAVEINDIVIDUAL = 1;
660                    } else if (strcmp("-debug", argv[i]) == 0 && i < argc -1) {
661                            i++;
662                if (!(sscanf(argv[i],"0x%x", &(ARG_DEBUG))))
663                                    sscanf(argv[i],"%d", &(ARG_DEBUG));
664                    } else if (strcmp("-o", argv[i]) == 0 && i < argc - 1) {
665                            ARG_SAVEMPEGSTREAM = 1;
666                            i++;
667                            ARG_OUTPUTFILE = argv[i];
668                    } else if (strcmp("-avi", argv[i]) == 0 && i < argc - 1) {
669    #ifdef XVID_AVI_OUTPUT
670                            ARG_SAVEMPEGSTREAM = 1;
671                            i++;
672                            ARG_AVIOUTPUTFILE = argv[i];
673    #else
674                            fprintf( stderr, "Not compiled with AVI output support.\n");
675                            return(-1);
676    #endif
677                    } else if (strcmp("-mkv", argv[i]) == 0 && i < argc - 1) {
678    #ifdef XVID_MKV_OUTPUT
679                            ARG_SAVEMPEGSTREAM = 1;
680                            i++;
681                            ARG_MKVOUTPUTFILE = argv[i];
682    #else
683                            fprintf(stderr, "Not compiled with MKV output support.\n");
684                            return(-1);
685    #endif
686                    } else if (strcmp("-vop_debug", argv[i]) == 0) {
687                            ARG_VOPDEBUG = 1;
688                    } else if (strcmp("-notrellis", argv[i]) == 0) {
689                            ARG_TRELLIS = 0;
690                    } else if (strcmp("-bvhq", argv[i]) == 0) {
691                            ARG_BVHQ = 1;
692                    } else if (strcmp("-qpel", argv[i]) == 0) {
693                            ARG_QPEL = 1;
694                    } else if (strcmp("-turbo", argv[i]) == 0) {
695                            ARG_TURBO = 1;
696                    } else if (strcmp("-gmc", argv[i]) == 0) {
697                            ARG_GMC = 1;
698                    } else if (strcmp("-interlaced", argv[i]) == 0) {
699                            if ((i < argc - 1) && (*argv[i+1] != '-')) {
700                                    i++;
701                                    ARG_INTERLACING = atoi(argv[i]);
702                            } else {
703                                    ARG_INTERLACING = 1;
704                            }
705                    } else if (strcmp("-noclosed_gop", argv[i]) == 0) {
706                            ARG_CLOSED_GOP = 0;
707                    } else if (strcmp("-closed_gop", argv[i]) == 0) {
708                            ARG_CLOSED_GOP = 2;
709                    } else if (strcmp("-vbvsize", argv[i]) == 0 && i < argc -1) {
710                            i++;
711                            ARG_VBVSIZE = atoi(argv[i]);
712                    } else if (strcmp("-vbvmax", argv[i]) == 0 && i < argc -1) {
713                            i++;
714                            ARG_VBVMAXRATE = atoi(argv[i]);
715                    } else if (strcmp("-vbvpeak", argv[i]) == 0 && i < argc -1) {
716                            i++;
717                            ARG_VBVPEAKRATE = atoi(argv[i])*3;
718                    } else if (strcmp("-reaction", argv[i]) == 0 && i < argc -1) {
719                            i++;
720                            ARG_REACTION = atoi(argv[i]);
721                    } else if (strcmp("-averaging", argv[i]) == 0 && i < argc -1) {
722                            i++;
723                            ARG_AVERAGING = atoi(argv[i]);
724                    } else if (strcmp("-smoother", argv[i]) == 0 && i < argc -1) {
725                            i++;
726                            ARG_SMOOTHER = atoi(argv[i]);
727                    } else if (strcmp("-kboost", argv[i]) == 0 && i < argc -1) {
728                            i++;
729                            ARG_KBOOST = atoi(argv[i]);
730                    } else if (strcmp("-kthresh", argv[i]) == 0 && i < argc -1) {
731                            i++;
732                            ARG_KTHRESH = atoi(argv[i]);
733                    } else if (strcmp("-chigh", argv[i]) == 0 && i < argc -1) {
734                            i++;
735                            ARG_CHIGH = atoi(argv[i]);
736                    } else if (strcmp("-clow", argv[i]) == 0 && i < argc -1) {
737                            i++;
738                            ARG_CLOW = atoi(argv[i]);
739                    } else if (strcmp("-ostrength", argv[i]) == 0 && i < argc -1) {
740                            i++;
741                            ARG_OVERSTRENGTH = atoi(argv[i]);
742                    } else if (strcmp("-oimprove", argv[i]) == 0 && i < argc -1) {
743                            i++;
744                            ARG_OVERIMPROVE = atoi(argv[i]);
745                    } else if (strcmp("-odegrade", argv[i]) == 0 && i < argc -1) {
746                            i++;
747                            ARG_OVERDEGRADE = atoi(argv[i]);
748                    } else if (strcmp("-overhead", argv[i]) == 0 && i < argc -1) {
749                            i++;
750                            ARG_OVERHEAD = atoi(argv[i]);
751                    } else if (strcmp("-kreduction", argv[i]) == 0 && i < argc -1) {
752                            i++;
753                            ARG_KREDUCTION = atoi(argv[i]);
754            } else if (strcmp("-progress", argv[i]) == 0) {
755                            if (i < argc - 1)
756                                    /* in kbps */
757                                    ARG_PROGRESS = atoi(argv[i+1]);
758                            if (ARG_PROGRESS > 0)
759                                    i++;
760                            else
761                                    ARG_PROGRESS = 10;
762                    } else if (strcmp("-help", argv[i])) {
763                            usage();
764                            return (0);
765                    } else {
766                            usage();
767                            exit(-1);
768          }          }
769    
         return 0;  
770  }  }
771    
772  int read_pgmdata(FILE* handle, unsigned char *image)  /*****************************************************************************
773  {   *                            Arguments checking
774          int i,status;   ****************************************************************************/
         char dummy;  
775    
776          unsigned char* buff1_ptr2 = image + XDIM*YDIM;          if (XDIM <= 0 || XDIM >= 4096 || YDIM <= 0 || YDIM >= 4096) {
777          unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2;                  fprintf(stderr,
778                                    "Trying to retrieve width and height from input header\n");
779                    if (!ARG_INPUTTYPE)
780                            ARG_INPUTTYPE = 1;              /* pgm */
781            }
782    
783          fread(image,XDIM*YDIM,1,stdin); // read Y component of picture          if (ARG_QUALITY < 0 ) {
784                    ARG_QUALITY = 0;
785            } else if (ARG_QUALITY >= ME_ELEMENTS) {
786                    ARG_QUALITY = ME_ELEMENTS - 1;
787            }
788    
789          for (i=0;i<YDIM/2;i++)          if (ARG_STARTFRAMENR < 0) {
790          {                  fprintf(stderr, "Bad starting frame number %d, cannot be negative\n", ARG_STARTFRAMENR);
791                  fread(buff1_ptr2,XDIM/2,1,stdin);        // read U                  return(-1);
                 buff1_ptr2 += XDIM/2;  
                 fread(buff1_ptr3,XDIM/2,1,stdin);        // read V  
                 buff1_ptr3 += XDIM/2;  
792          }          }
793          fread(&dummy,1,1,handle);       //  I don't know why, but this seems needed  
794          return 0;          if (ARG_PASS2) {
795                    if (ARG_PASS2 == ARG_PASS1) {
796                            fprintf(stderr, "Can't use the same statsfile for pass1 and pass2: %s\n", ARG_PASS2);
797                            return(-1);
798                    }
799                      statsfile = fopen(ARG_PASS2, "rb");
800                      if (statsfile == NULL) {
801                              fprintf(stderr, "Couldn't open statsfile '%s'!\n", ARG_PASS2);
802                              return (-1);
803                      }
804                      fclose(statsfile);
805  }  }
806    
807  int read_yuvdata(FILE* handle, unsigned char *image)  #ifdef XVID_AVI_OUTPUT
808  {       int i;          if (ARG_AVIOUTPUTFILE == NULL && ARG_PACKED <= 1)
809          char dummy;                  ARG_PACKED = 0;
810    #endif
811    
812          unsigned char* buff1_ptr2 = image + XDIM*YDIM;          if (ARG_BITRATE < 0) {
813          unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2;                  fprintf(stderr, "Bad bitrate %d, cannot be negative\n", ARG_BITRATE);
814                    return(-1);
815            }
816    
817          if (fread(image,XDIM,YDIM*3/2,stdin) != YDIM*3/2)          if (NUM_ZONES) {
818                  return 1;                  int i;
819          else                  sort_zones(ZONES, NUM_ZONES, &i);
820                  return 0;          }
821    
822            if (ARG_PAR > 5) {
823                    fprintf(stderr, "Bad PAR: %d. Must be [1..5] or width:height\n", ARG_PAR);
824                    return(-1);
825  }  }
826    
827  int write_pgm(char *filename, unsigned char *image)          if (ARG_MAXFRAMENR == 0) {
828                    fprintf(stderr, "Wrong number of frames\n");
829                    return (-1);
830            }
831    
832            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
833                    in_file = stdin;
834            } else {
835    #ifdef XVID_AVI_INPUT
836          if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
837              strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
838                      ARG_INPUTTYPE==2)
839  {  {
840          FILE *filehandle;                    AVISTREAMINFO avi_info;
841          filehandle=fopen(filename,"wb");  #ifndef XVID_AVI_OUTPUT
842          if (filehandle)                    AVIFileInit();
843          {  #endif
844                  fprintf(filehandle,"P5\n\n");           //                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
845                  fprintf(filehandle,"%d %d 255\n",XDIM,YDIM*3/2);                    if (avi_fp == NULL) {
846                  fwrite(image,XDIM,YDIM*3/2,filehandle);                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
847                  fclose(filehandle);                            return (-1);
848                  return 0;                    }
849                      fclose(avi_fp);
850    
851                      if (AVIFileOpen(&avi_file, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
852                              fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
853                              AVIFileExit();
854                              return(-1);
855                      }
856    
857                      if (AVIFileGetStream(avi_file, &avi_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
858                              fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
859                              AVIFileRelease(avi_file);
860                              AVIFileExit();
861                              return (-1);
862                      }
863    
864                      AVIFileRelease(avi_file);
865    
866                      if(AVIStreamInfo(avi_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
867                              fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE);
868                              AVIStreamRelease(avi_stream);
869                              AVIFileExit();
870                              return (-1);
871                      }
872    
873                  if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) {
874                              LONG size;
875                              fprintf(stderr, "Non YV12 input colorspace %c%c%c%c! Attempting conversion...\n",
876                                      avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256,
877                                      (avi_info.fccHandler>>24)%256);
878                              size = sizeof(myBitmapInfoHeader);
879                              AVIStreamReadFormat(avi_stream, 0, &myBitmapInfoHeader, &size);
880                              if (size==0)
881                                      fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
882                              else {
883                                      fprintf(stderr, "AVIStreamReadFormat read %d bytes.\n", size);
884                                      fprintf(stderr, "width = %d, height = %d, planes = %d\n", myBitmapInfoHeader.biWidth,
885                                              myBitmapInfoHeader.biHeight, myBitmapInfoHeader.biPlanes);
886                                      fprintf(stderr, "Compression = %c%c%c%c, %d\n",
887                                              myBitmapInfoHeader.biCompression%256, (myBitmapInfoHeader.biCompression>>8)%256,
888                                              (myBitmapInfoHeader.biCompression>>16)%256, (myBitmapInfoHeader.biCompression>>24)%256,
889                                              myBitmapInfoHeader.biCompression);
890                                      fprintf(stderr, "Bits Per Pixel = %d\n", myBitmapInfoHeader.biBitCount);
891                                      myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
892                                      myBitmapInfoHeader.biBitCount = 12;
893                                      myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
894                                      get_frame = AVIStreamGetFrameOpen(avi_stream, &myBitmapInfoHeader);
895                              }
896                              if (get_frame == NULL) {
897                                    AVIStreamRelease(avi_stream);
898                                    AVIFileExit();
899                                    return (-1);
900                              }
901                              else {
902                                    unsigned char *temp;
903                                    fprintf(stderr, "AVIStreamGetFrameOpen successful.\n");
904                                    temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);
905                                    if (temp != NULL) {
906                                            int i;
907                                            for (i = 0; i < ((DWORD*)temp)[0]; i++) {
908                                                    fprintf(stderr, "%2d ", temp[i]);
909                                            }
910                                            fprintf(stderr, "\n");
911                                    }
912                              }
913                              if (avi_info.fccHandler == MAKEFOURCC('D', 'I', 'B', ' ')) {
914                                      AVIStreamGetFrameClose(get_frame);
915                                      get_frame = NULL;
916                                      ARG_COLORSPACE = XVID_CSP_BGR | XVID_CSP_VFLIP;
917          }          }
         else  
                 return 1;  
918  }  }
919    
920  /*********************************************************************/            if (ARG_MAXFRAMENR<0)
921  /* Routines for encoding: init encoder, frame step, release encoder  */                          ARG_MAXFRAMENR = avi_info.dwLength-ARG_STARTFRAMENR;
922  /*********************************************************************/                    else
923                            ARG_MAXFRAMENR = min(ARG_MAXFRAMENR, avi_info.dwLength-ARG_STARTFRAMENR);
924    
925  #define FRAMERATE_INCR 1001                    XDIM = avi_info.rcFrame.right - avi_info.rcFrame.left;
926                      YDIM = avi_info.rcFrame.bottom - avi_info.rcFrame.top;
927                      if (ARG_FRAMERATE==0) {
928                            ARG_FRAMERATE = (float) avi_info.dwRate / (float) avi_info.dwScale;
929                            ARG_DWRATE = avi_info.dwRate;
930                            ARG_DWSCALE = avi_info.dwScale;
931                      }
932    
933                      ARG_INPUTTYPE = 2;
934        }
935        else
936    #endif
937                    {
938                            in_file = fopen(ARG_INPUTFILE, "rb");
939                            if (in_file == NULL) {
940                                    fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
941                                    return (-1);
942                            }
943                    }
944            }
945    
946  int enc_init(int use_assembler)          // This should be after the avi input opening stuff
947  {       /* initialize encoder for first use, pass all needed parameters to the codec */          if (ARG_TIMECODEFILE != NULL) {
948          int xerr;                  time_file = fopen(ARG_TIMECODEFILE, "r");
949                    if (time_file==NULL) {
950                            fprintf(stderr, "Couldn't open timecode file '%s'!\n", ARG_TIMECODEFILE);
951                            return(-1);
952                    }
953                    else {
954                            fscanf(time_file, "# timecode format v2\n");
955                    }
956            }
957    
958          XVID_INIT_PARAM xinit;          if (ARG_FRAMERATE <= 0) {
959          XVID_ENC_PARAM xparam;                  fprintf(stderr, "Wrong Framerate %f\n", ARG_FRAMERATE);
960                    return (-1);
961            }
962    
963          if(use_assembler)          if (ARG_TARGETSIZE) {
964                    if (ARG_MAXFRAMENR <= 0) {
965                            fprintf(stderr, "Bad target size; number of input frames unknown\n");
966                            goto release_all;
967                    } else if (ARG_BITRATE) {
968                                    fprintf(stderr, "Parameter conflict: Do not specify both -bitrate and -size\n");
969                                    goto release_all;
970                    } else
971                            ARG_BITRATE = ((ARG_TARGETSIZE * 8) / (ARG_MAXFRAMENR / ARG_FRAMERATE)) * 1024;
972            }
973    
974                    /* Set constant quant to default if no bitrate given for single pass */
975            if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))
976                            ARG_CQ = DEFAULT_QUANT;
977    
978  #ifdef ARCH_IA64          if (ARG_INPUTTYPE==1) {
979                  xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;  #ifndef READ_PNM
980                    if (read_pgmheader(in_file)) {
981  #else  #else
982                  xinit.cpu_flags = 0;                  if (read_pnmheader(in_file)) {
983  #endif  #endif
984                            fprintf(stderr,
985                                            "Wrong input format, I want YUV encapsulated in PGM\n");
986                            return (-1);
987                    }
988            }
989    
990          else          /* Jump to the starting frame */
991                  xinit.cpu_flags = XVID_CPU_FORCE;          if (ARG_INPUTTYPE == 0)
992                    fseek(in_file, ARG_STARTFRAMENR*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);
993    
994            /* now we know the sizes, so allocate memory */
995            if (get_frame == NULL)
996            {
997                    in_buffer = (unsigned char *) malloc(4*XDIM*YDIM);
998                    if (!in_buffer)
999                            goto free_all_memory;
1000            }
1001    
1002            /* this should really be enough memory ! */
1003            mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM) * 2);
1004            if (!mp4_buffer)
1005                    goto free_all_memory;
1006    
1007    /*****************************************************************************
1008     *                            XviD PART  Start
1009     ****************************************************************************/
1010    
1011    
1012            result = enc_init(use_assembler);
1013            if (result) {
1014                    fprintf(stderr, "Encore INIT problem, return value %d\n", result);
1015                    goto release_all;
1016            }
1017    
1018          xvid_init(NULL, 0, &xinit, NULL);  /*****************************************************************************
1019     *                            Main loop
1020     ****************************************************************************/
1021    
1022            if (ARG_SAVEMPEGSTREAM) {
1023    
1024                    if (ARG_OUTPUTFILE) {
1025                            if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {
1026                                    fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);
1027                                    goto release_all;
1028                            }
1029                    }
1030    
1031          xparam.width = XDIM;  #ifdef XVID_AVI_OUTPUT
1032          xparam.height = YDIM;                  if (ARG_AVIOUTPUTFILE != NULL ) {
         if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS)  
1033          {          {
1034                  xparam.fincr = 1;                                  /* Open the .avi output then close it */
1035                  xparam.fbase = (int)ARG_FRAMERATE;                                  /* Resets the file size to 0, which AVIFile doesn't seem to do */
1036                                    FILE *scrub;
1037                                    if ((scrub = fopen(ARG_AVIOUTPUTFILE, "w+b")) == NULL) {
1038                                            fprintf(stderr, "Error opening output file %s\n", ARG_AVIOUTPUTFILE);
1039                                            goto release_all;
1040          }          }
1041          else          else
1042                                            fclose(scrub);
1043                            }
1044                            memset(&myAVIStreamInfo, 0, sizeof(AVISTREAMINFO));
1045                            myAVIStreamInfo.fccType = streamtypeVIDEO;
1046                            myAVIStreamInfo.fccHandler = MAKEFOURCC('x', 'v', 'i', 'd');
1047                            myAVIStreamInfo.dwScale = ARG_DWSCALE;
1048                            myAVIStreamInfo.dwRate = ARG_DWRATE;
1049                            myAVIStreamInfo.dwLength = ARG_MAXFRAMENR;
1050                            myAVIStreamInfo.dwQuality = 10000;
1051                            SetRect(&myAVIStreamInfo.rcFrame, 0, 0, YDIM, XDIM);
1052    
1053                            if (avierr=AVIFileOpen(&myAVIFile, ARG_AVIOUTPUTFILE, OF_CREATE|OF_WRITE, NULL)) {
1054                                    fprintf(stderr, "AVIFileOpen failed opening output file %s, error code %d\n", ARG_AVIOUTPUTFILE, avierr);
1055                                    goto release_all;
1056                            }
1057    
1058                            if (avierr=AVIFileCreateStream(myAVIFile, &myAVIStream, &myAVIStreamInfo)) {
1059                                    fprintf(stderr, "AVIFileCreateStream failed, error code %d\n", avierr);
1060                                    goto release_all;
1061                            }
1062    
1063                            memset(&myBitmapInfoHeader, 0, sizeof(BITMAPINFOHEADER));
1064                            myBitmapInfoHeader.biHeight = YDIM;
1065                            myBitmapInfoHeader.biWidth = XDIM;
1066                            myBitmapInfoHeader.biPlanes = 1;
1067                            myBitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
1068                            myBitmapInfoHeader.biCompression = MAKEFOURCC('X', 'V', 'I', 'D');
1069                            myBitmapInfoHeader.biBitCount = 12;
1070                            myBitmapInfoHeader.biSizeImage = 6*XDIM*YDIM;
1071                            if (avierr=AVIStreamSetFormat(myAVIStream, 0, &myBitmapInfoHeader, sizeof(BITMAPINFOHEADER))) {
1072                                    fprintf(stderr, "AVIStreamSetFormat failed, error code %d\n", avierr);
1073                                    goto release_all;
1074                            }
1075                    }
1076    #endif
1077    #ifdef XVID_MKV_OUTPUT
1078                    if (ARG_MKVOUTPUTFILE != NULL) {
1079          {          {
1080                  xparam.fincr = FRAMERATE_INCR;                                  /* Open the .mkv output then close it */
1081                  xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE);                                  /* Just to make sure we can write to it */
1082                                    FILE *scrub;
1083                                    if ((scrub = fopen(ARG_MKVOUTPUTFILE, "w+b")) == NULL) {
1084                                            fprintf(stderr, "Error opening output file %s\n", ARG_MKVOUTPUTFILE);
1085                                            goto release_all;
1086                                    }
1087                                    else
1088                                            fclose(scrub);
1089          }          }
         xparam.rc_reaction_delay_factor = 16;  
         xparam.rc_averaging_period = 100;  
         xparam.rc_buffer = 10;  
         xparam.rc_bitrate = ARG_BITRATE*1000;  
         xparam.min_quantizer = 1;  
         xparam.max_quantizer = 31;  
         xparam.max_key_interval = (int)ARG_FRAMERATE*10;  
1090    
1091  #ifdef BFRAMES                          MKVFileOpen(&myMKVFile, ARG_MKVOUTPUTFILE, OF_CREATE|OF_WRITE, NULL);
1092          xparam.global = XVID_GLOBAL_DX50BVOP;                          if (ARG_PAR) {
1093          xparam.max_bframes = ARG_MAXBFRAMES;                                  myMKVStreamInfo.display_height = YDIM*height_ratios[ARG_PAR];
1094          xparam.bquant_ratio = ARG_BQUANTRATIO;                                  myMKVStreamInfo.display_width = XDIM*width_ratios[ARG_PAR];
1095          xparam.frame_drop_ratio=0;                          }
1096                            else {
1097                                    myMKVStreamInfo.display_height = YDIM*ARG_PARHEIGHT;
1098                                    myMKVStreamInfo.display_width = XDIM*ARG_PARWIDTH;
1099                            }
1100                            myMKVStreamInfo.height = YDIM;
1101                            myMKVStreamInfo.width = XDIM;
1102                            myMKVStreamInfo.framerate = ARG_DWRATE;
1103                            myMKVStreamInfo.framescale = ARG_DWSCALE;
1104                            myMKVStreamInfo.length = ARG_MAXFRAMENR;
1105                            MKVFileCreateStream(myMKVFile, &myMKVStream, &myMKVStreamInfo);
1106                    }
1107  #endif  #endif
1108            } else {
1109                    out_file = NULL;
1110            }
1111    
1112    
1113    /*****************************************************************************
1114     *                       Encoding loop
1115     ****************************************************************************/
1116    
1117            totalsize = 0;
1118    
1119            result = 0;
1120    
1121            input_num = 0;                          /* input frame counter */
1122            output_num = 0;                         /* output frame counter */
1123    
1124            nvop_counter = 0;
1125    
1126          /* I use a small value here, since will not encode whole movies,          do {
                 but short clips */  
1127    
1128          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL);                  char *type;
1129          enc_handle=xparam.handle;                  int sse[3];
1130    
1131          return xerr;                  if (input_num >= ARG_MAXFRAMENR && ARG_MAXFRAMENR > 0) {
1132                            result = 1;
1133                    }
1134    
1135                    if (!result) {
1136    #ifdef XVID_AVI_INPUT
1137                            if (ARG_INPUTTYPE==2) {
1138                                    /* read avs/avi data (YUV-format) */
1139                                    if (get_frame != NULL) {
1140                                            in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+ARG_STARTFRAMENR);
1141                                            if (in_buffer == NULL)
1142                                                    result = 1;
1143                                            else
1144                                                    in_buffer += ((DWORD*)in_buffer)[0];
1145                                    } else {
1146                                            if(AVIStreamRead(avi_stream, input_num+ARG_STARTFRAMENR, 1, in_buffer, 4*XDIM*YDIM, NULL, NULL ) != AVIERR_OK)
1147                                                    result = 1;
1148                                    }
1149                            } else
1150    #endif
1151                            if (ARG_INPUTTYPE==1) {
1152                                    /* read PGM data (YUV-format) */
1153    #ifndef READ_PNM
1154                                    result = read_pgmdata(in_file, in_buffer);
1155    #else
1156                                    result = read_pnmdata(in_file, in_buffer);
1157    #endif
1158                            } else {
1159                                    /* read raw data (YUV-format) */
1160                                    result = read_yuvdata(in_file, in_buffer);
1161                            }
1162  }  }
1163    
1164  int  enc_stop()  /*****************************************************************************
1165  {       int xerr;   *                       Encode and decode this frame
1166     ****************************************************************************/
1167    
1168          xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL);                  if (input_num >= (unsigned int)ARG_MAXFRAMENR-1 && ARG_MAXBFRAMES) {
1169          return xerr;                          stats_type = XVID_TYPE_PVOP;
1170                    }
1171                    else
1172                            stats_type = XVID_TYPE_AUTO;
1173    
1174                    enctime = msecond();
1175                    m4v_size =
1176                            enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type,
1177                                             &stats_quant, &stats_length, sse, input_num);
1178                    enctime = msecond() - enctime;
1179    
1180                    /* Write the Frame statistics */
1181    
1182                    if (stats_type > 0) {   /* !XVID_TYPE_NOTHING */
1183                            switch (stats_type) {
1184                                    case XVID_TYPE_IVOP:
1185                                            type = "I";
1186                                            break;
1187                                    case XVID_TYPE_PVOP:
1188                                            type = "P";
1189                                            break;
1190                                    case XVID_TYPE_BVOP:
1191                                            type = "B";
1192                                            if (ARG_PACKED)
1193                                                    fakenvop = 1;
1194                                            break;
1195                                    case XVID_TYPE_SVOP:
1196                                            type = "S";
1197                                            break;
1198                                    default:
1199                                            type = "U";
1200                                            break;
1201  }  }
1202    
1203  int  enc_main(unsigned char* image, unsigned char* bitstream, int *streamlength, int* frametype)                          if (stats_length > 8) {
1204  {       int xerr;                                  framestats[stats_type].count++;
1205                                    framestats[stats_type].quants[stats_quant]++;
1206                                    framestats[stats_type].size += stats_length;
1207                            }
1208                            else {
1209                                    framestats[5].count++;
1210                                    framestats[5].quants[stats_quant]++;
1211                                    framestats[5].size += stats_length;
1212                            }
1213    
1214          XVID_ENC_FRAME xframe;  #define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height)))))
         XVID_ENC_STATS xstats;  
1215    
1216          xframe.bitstream = bitstream;                          if (ARG_PROGRESS == 0) {
1217          xframe.length = -1;     // this is written by the routine                                  printf("%5d: key=%i, time= %6.0f, len= %7d", !result ? input_num : -1,
1218                                            key, (float) enctime, (int) m4v_size);
1219                                    printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,
1220                                       stats_length);
1221    
         xframe.image = image;  
         xframe.colorspace = XVID_CSP_YV12;      // defined in <xvid.h>  
1222    
1223          xframe.intra = -1; // let the codec decide between I-frame (1) and P-frame (0)                                  if (ARG_STATS) {
1224                                            printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
1225                                                    SSE2PSNR(sse[0], XDIM, YDIM), SSE2PSNR(sse[1], XDIM / 2, YDIM / 2),
1226                                                    SSE2PSNR(sse[2], XDIM / 2, YDIM / 2));
1227                                    }
1228                                    printf("\n");
1229                            } else {
1230                                    if (input_num % ARG_PROGRESS == 1) {
1231                                            if (ARG_MAXFRAMENR > 0) {
1232                                                    fprintf(stderr, "\r%7d frames(%3d%%) encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \
1233                                                            input_num, input_num*100/ARG_MAXFRAMENR, input_num*1000/totalenctime, \
1234                                                            (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);
1235                                            } else {
1236                                                    fprintf(stderr, "\r%7d frames encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \
1237                                                            input_num, input_num*1000/totalenctime, \
1238                                                            (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);
1239                                            }
1240                                    }
1241                            }
1242    
1243          xframe.quant = ARG_QUANTI;      // is quant != 0, use a fixed quant (and ignore bitrate)                          if (ARG_STATS) {
1244                                    totalPSNR[0] += SSE2PSNR(sse[0], XDIM, YDIM);
1245                                    totalPSNR[1] += SSE2PSNR(sse[1], XDIM/2, YDIM/2);
1246                                    totalPSNR[2] += SSE2PSNR(sse[2], XDIM/2, YDIM/2);
1247                            }
1248    #undef SSE2PSNR
1249                    }
1250    
1251          xframe.motion = motion_presets[ARG_QUALITY];                  if (m4v_size < 0)
1252          xframe.general = general_presets[ARG_QUALITY];                          break;
         xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL;  
1253    
1254  #ifdef BFRAMES                  /* Update encoding time stats */
1255          xframe.bquant = 0;                  totalenctime += enctime;
1256  #endif                  totalsize += m4v_size;
1257    
1258          xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats);  /*****************************************************************************
1259     *                       Save stream to file
1260     ****************************************************************************/
1261    
1262                    if (m4v_size > 0 && ARG_SAVEMPEGSTREAM) {
1263                            char timecode[50];
1264    
1265                            if (time_file != NULL) {
1266                                    if (fscanf(time_file, "%s\n", timecode) != 1) {
1267                                            fprintf(stderr, "Error reading timecode file, frame %d\n", output_num);
1268                                            goto release_all;
1269                                    }
1270                            }
1271                            else
1272                                    sprintf(timecode, "%f", ((double)ARG_DWSCALE/ARG_DWRATE)*1000*output_num);
1273    
1274  /*              enc_result->is_key_frame = xframe.intra;                          /* Save single files */
1275                  enc_result->quantizer = xframe.quant;                          if (ARG_SAVEINDIVIDUAL) {
1276                  enc_result->total_bits = xframe.length * 8;                                  FILE *out;
1277                  enc_result->motion_bits = xstats.hlength * 8;                                  sprintf(filename, "%sframe%05d.m4v", filepath, output_num);
1278                  enc_result->texture_bits = enc_result->total_bits - enc_result->motion_bits;                                  out = fopen(filename, "w+b");
1279  */                                  fwrite(mp4_buffer, m4v_size, 1, out);
1280                                    fclose(out);
1281                            }
1282    #ifdef XVID_AVI_OUTPUT
1283                            if (ARG_AVIOUTPUTFILE && myAVIStream) {
1284                                    int output_frame;
1285    
1286                                    if (time_file == NULL)
1287                                            output_frame = output_num;
1288                                    else {
1289                                            output_frame = (int)(atof(timecode)/1000/((double)ARG_DWSCALE/ARG_DWRATE)+.5);
1290                                    }
1291                                    if (AVIStreamWrite(myAVIStream, output_frame, 1, mp4_buffer, m4v_size, key ? AVIIF_KEYFRAME : 0, NULL, NULL)) {
1292                                            fprintf(stderr, "AVIStreamWrite failed writing frame %d\n", output_num);
1293                                            goto release_all;
1294                                    }
1295                            }
1296    #endif
1297    
1298  /*  This is statictical data, e.g. for 2-pass.                                  if (key && ARG_PACKED)
1299      If you are not interested in any of this, you can use                                          removedivxp((char*)mp4_buffer, m4v_size);
         NULL instead of &xstats  
 */  
         *frametype = xframe.intra;  
         *streamlength = xframe.length;  
1300    
1301          return xerr;                                  /* Save ES stream */
1302                                    if (ARG_OUTPUTFILE && out_file && !(fakenvop && m4v_size <= 8)) {
1303                                                    fwrite(mp4_buffer, 1, m4v_size, out_file);
1304                                    }
1305    #ifdef XVID_MKV_OUTPUT
1306                                    if (ARG_MKVOUTPUTFILE && myMKVStream) {
1307                                            MKVStreamWrite(myMKVStream, atof(timecode), 1, (ARG_PACKED && fakenvop && (m4v_size <= 8)) ? NULL : mp4_buffer, m4v_size, key ? AVIIF_KEYFRAME : 0, NULL, NULL);
1308  }  }
1309    #endif
1310    
1311  /*********************************************************************/                          output_num++;
1312  /*                          Main program                             */                          if (stats_type != XVID_TYPE_BVOP)
1313  /*********************************************************************/                                  fakenvop=0;
1314                    }
1315    
1316  int main(int argc, char *argv[])                  input_num++;
 {  
   unsigned char *divx_buffer = NULL;  
   unsigned char *in_buffer = NULL;  
1317    
1318    double enctime;                  /* Read the header if it's pgm stream */
1319    double totalenctime=0.;                  if (!result && (ARG_INPUTTYPE==1))
1320    #ifndef READ_PNM
1321                            result = read_pgmheader(in_file);
1322    #else
1323                            result = read_pnmheader(in_file);
1324    #endif
1325            } while (1);
1326    
   long totalsize=0;  
   int status;  
1327    
   int m4v_size;  
   int frame_type[ABS_MAXFRAMENR];  
   int Iframes=0, Pframes=0, Bframes=0;  
   int use_assembler=1;  
1328    
1329    char filename[256];  /*****************************************************************************
1330     *         Calculate totals and averages for output, print results
1331     ****************************************************************************/
1332    
1333     printf("\n");
1334            printf("Tot: enctime(ms) =%7.2f,               length(bytes) = %7d\n",
1335                       totalenctime, (int) totalsize);
1336    
1337            if (input_num > 0) {
1338                    totalsize /= input_num;
1339                    totalenctime /= input_num;
1340                    totalPSNR[0] /= input_num;
1341                    totalPSNR[1] /= input_num;
1342                    totalPSNR[2] /= input_num;
1343            } else {
1344                    totalsize = -1;
1345                    totalenctime = -1;
1346            }
1347    
1348            printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d",
1349                       totalenctime, 1000 / totalenctime, (int) totalsize);
1350       if (ARG_STATS) {
1351           printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
1352                      totalPSNR[0],totalPSNR[1],totalPSNR[2]);
1353            }
1354            printf("\n");
1355            if (framestats[XVID_TYPE_IVOP].count) {
1356                    printf("I frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1357                            framestats[XVID_TYPE_IVOP].count, framestats[XVID_TYPE_IVOP].size/framestats[XVID_TYPE_IVOP].count, \
1358                            framestats[XVID_TYPE_IVOP].size, minquant(framestats[XVID_TYPE_IVOP].quants), \
1359                            avgquant(framestats[XVID_TYPE_IVOP]), maxquant(framestats[XVID_TYPE_IVOP].quants));
1360            }
1361            if (framestats[XVID_TYPE_PVOP].count) {
1362                    printf("P frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1363                            framestats[XVID_TYPE_PVOP].count, framestats[XVID_TYPE_PVOP].size/framestats[XVID_TYPE_PVOP].count, \
1364                            framestats[XVID_TYPE_PVOP].size, minquant(framestats[XVID_TYPE_PVOP].quants), \
1365                            avgquant(framestats[XVID_TYPE_PVOP]), maxquant(framestats[XVID_TYPE_PVOP].quants));
1366            }
1367            if (framestats[XVID_TYPE_BVOP].count) {
1368                    printf("B frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1369                            framestats[XVID_TYPE_BVOP].count, framestats[XVID_TYPE_BVOP].size/framestats[XVID_TYPE_BVOP].count, \
1370                            framestats[XVID_TYPE_BVOP].size, minquant(framestats[XVID_TYPE_BVOP].quants), \
1371                            avgquant(framestats[XVID_TYPE_BVOP]), maxquant(framestats[XVID_TYPE_BVOP].quants));
1372            }
1373            if (framestats[XVID_TYPE_SVOP].count) {
1374                    printf("S frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1375                            framestats[XVID_TYPE_SVOP].count, framestats[XVID_TYPE_SVOP].size/framestats[XVID_TYPE_SVOP].count, \
1376                            framestats[XVID_TYPE_SVOP].size, minquant(framestats[XVID_TYPE_SVOP].quants), \
1377                            avgquant(framestats[XVID_TYPE_SVOP]), maxquant(framestats[XVID_TYPE_SVOP].quants));
1378            }
1379            if (framestats[5].count) {
1380                    printf("N frames: %6d frames, size = %7d/%7d\n", \
1381                            framestats[5].count, framestats[5].size/framestats[5].count, \
1382                            framestats[5].size);
1383            }
1384    
1385    
1386    /*****************************************************************************
1387     *                            XviD PART  Stop
1388     ****************************************************************************/
1389    
1390      release_all:
1391    
1392    #ifdef XVID_AVI_INPUT
1393            if (get_frame) AVIStreamGetFrameClose(get_frame);
1394            if (avi_stream) AVIStreamRelease(avi_stream);
1395    #ifndef XVID_AVI_OUTPUT
1396            AVIFileExit();
1397    #endif
1398    #endif
1399    
1400            if (enc_handle) {
1401                    result = enc_stop();
1402                    if (result)
1403                            fprintf(stderr, "Encore RELEASE problem return value %d\n",
1404                                            result);
1405            }
1406    
1407            if (in_file)
1408                    fclose(in_file);
1409            if (out_file)
1410                    fclose(out_file);
1411    
1412    #ifdef XVID_AVI_OUTPUT
1413            if (myAVIStream) AVIStreamRelease(myAVIStream);
1414            if (myAVIFile) AVIFileRelease(myAVIFile);
1415            AVIFileExit();
1416    #endif
1417    #ifdef XVID_MKV_OUTPUT
1418            if (myMKVStream) MKVStreamRelease(myMKVStream);
1419            if (myMKVFile) MKVFileRelease(myMKVFile);
1420    #endif
1421    
1422      free_all_memory:
1423            free(out_buffer);
1424            free(mp4_buffer);
1425            free(in_buffer);
1426    
1427            return (0);
1428    
1429    }
1430    
1431    
1432    FILE *filehandle;  /*****************************************************************************
1433     *                        "statistical" functions
1434     *
1435     *  these are not needed for encoding or decoding, but for measuring
1436     *  time and quality, there in nothing specific to XviD in these
1437     *
1438     *****************************************************************************/
1439    
1440  /* read YUV in pgm format from stdin */  /* Return time elapsed time in miliseconds since the program started */
1441    if (!pgmflag)  static double
1442    msecond()
1443    {    {
1444          pgmflag = 1;  #ifndef WIN32
1445            struct timeval tv;
1446    
1447  //      if (argc==2 && !strcmp(argv[1],"-noasm"))          gettimeofday(&tv, 0);
1448  //        use_assembler = 0;          return (tv.tv_sec * 1.0e3 + tv.tv_usec * 1.0e-3);
1449    #else
1450            clock_t clk;
1451    
1452          if (argc>=3)          clk = clock();
1453          {       XDIM = atoi(argv[1]);          return (clk * 1000.0 / CLOCKS_PER_SEC);
1454                  YDIM = atoi(argv[2]);  #endif
                 if ( (XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048) )  
                 {       fprintf(stderr,"Wrong frames size %d %d, trying PGM \n",XDIM, YDIM);  
1455                  }                  }
1456                  else  
1457    int
1458    gcd(int a, int b)
1459                  {                  {
1460                          YDIM = YDIM*3/2; /* for YUV */          int r ;
1461                          pgmflag = 0;  
1462            if (b > a) {
1463                    r = a;
1464                    a = b;
1465                    b = r;
1466                  }                  }
1467    
1468            while ((r = a % b)) {
1469                    a = b;
1470                    b = r;
1471          }          }
1472            return b;
1473    }    }
1474    
1475    if (pgmflag)  int minquant(int quants[32])
   {     if (read_pgmheader(stdin))  
1476              {              {
1477                fprintf(stderr,"Wrong input format, I want YUV encapsulated in PGM\n");          int i = 1;
1478                return 1;          while (quants[i] == 0) {
1479                    i++;
1480              }              }
1481            return i;
1482    }    }
1483    if (argc>=4)  
1484    {     ARG_QUALITY = atoi(argv[3]);  int maxquant(int quants[32])
1485          if ( (ARG_QUALITY < 0) || (ARG_QUALITY > 6) )  {
1486                  { fprintf(stderr,"Wrong Quality\n"); return -1; }          int i = 31;
1487          else          while (quants[i] == 0) {
1488                    fprintf(stderr,"Quality %d\n",ARG_QUALITY);                  i--;
1489    }    }
1490    if (argc>=5)          return i;
   {     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;  
                   fprintf(stderr,"Quantizer %d\n",ARG_QUANTI);  
1491                  }                  }
1492          else  
1493                    fprintf(stderr,"Bitrate %d kbps\n",ARG_BITRATE);  double avgquant(frame_stats_t frame)
1494    {
1495            double avg=0;
1496            int i;
1497            for (i=1; i < 32; i++) {
1498                    avg += frame.quants[i]*i;
1499    }    }
1500    if (argc>=6)          avg /= frame.count;
1501    {     ARG_FRAMERATE = (float)atof(argv[5]);          return avg;
         if ( (ARG_FRAMERATE <= 0) )  
                 { fprintf(stderr,"Wrong Fraterate %s \n",argv[5]); return -1; }  
         fprintf(stderr,"Framerate %6.3f fps\n",ARG_FRAMERATE);  
1502    }    }
1503    
1504    if (argc>=7)  /*****************************************************************************
1505    {     ARG_MAXFRAMENR = atoi(argv[6]);   *                             Usage message
1506          if ( (ARG_MAXFRAMENR <= 0) )   *****************************************************************************/
1507           { fprintf(stderr,"Wrong number of frames\n"); return -1; }  
1508          fprintf(stderr,"max. Framenr. %d\n",ARG_MAXFRAMENR);  static void
1509    usage()
1510    {
1511            fprintf(stderr, "xvid_encraw built at %s on %s\n", __TIME__, __DATE__);
1512            fprintf(stderr, "Usage : xvid_encraw [OPTIONS]\n\n");
1513            fprintf(stderr, "Input options:\n");
1514            fprintf(stderr, " -i      string : input filename (stdin)\n");
1515    #ifdef XVID_AVI_INPUT
1516            fprintf(stderr, " -type   integer: input data type (yuv=0, pgm=1, avi/avs=2)\n");
1517    #else
1518            fprintf(stderr, " -type   integer: input data type (yuv=0, pgm=1)\n");
1519    #endif
1520            fprintf(stderr, " -w      integer: frame width ([1.2048])\n");
1521            fprintf(stderr, " -h      integer: frame height ([1.2048])\n");
1522            fprintf(stderr, " -csp    string : colorspace of raw input file i420, yv12 (default)\n");
1523            fprintf(stderr, " -frames integer: number of frames to encode\n");
1524            fprintf(stderr, "\n");
1525            fprintf(stderr, "Output options:\n");
1526            fprintf(stderr, " -dump      : save decoder output\n");
1527            fprintf(stderr, " -save      : save an Elementary Stream file per frame\n");
1528            fprintf(stderr, " -o string  : save an Elementary Stream for the complete sequence\n");
1529    #ifdef XVID_AVI_OUTPUT
1530            fprintf(stderr, " -avi string: save an AVI file for the complete sequence\n");
1531    #endif
1532            fprintf(stderr, " -mkv string: save a MKV file for the complete sequence\n");
1533            fprintf(stderr, "\n");
1534            fprintf(stderr, "BFrames options:\n");
1535            fprintf(stderr, " -max_bframes   integer: max bframes (2)\n");
1536            fprintf(stderr, " -bquant_ratio  integer: bframe quantizer ratio (150)\n");
1537            fprintf(stderr, " -bquant_offset integer: bframe quantizer offset (100)\n");
1538            fprintf(stderr, "\n");
1539            fprintf(stderr, "Rate control options:\n");
1540            fprintf(stderr, " -framerate float               : target framerate (25.0)\n");
1541            fprintf(stderr, " -bitrate   [integer]           : target bitrate in kbps (700)\n");
1542            fprintf(stderr, " -size      integer                     : target size in kilobytes\n");
1543        fprintf(stderr,     " -single                        : single pass mode (default)\n");
1544            fprintf(stderr, " -cq        float               : single pass constant quantizer\n");
1545            fprintf(stderr, " -pass1     [filename]          : twopass mode (first pass)\n");
1546            fprintf(stderr, " -full1pass                     : perform full first pass\n");
1547            fprintf(stderr, " -pass2     [filename]          : twopass mode (2nd pass)\n");
1548            fprintf(stderr, " -zq starting_frame float       : bitrate zone; quant\n");
1549            fprintf(stderr, " -zw starting_frame float       : bitrate zone; weight\n");
1550        fprintf(stderr, " -max_key_interval integer      : maximum keyframe interval (300)\n");
1551        fprintf(stderr, "\n");
1552            fprintf(stderr, "Single Pass options:\n");
1553            fprintf(stderr, "-reaction   integer             : reaction delay factor (16)\n");
1554            fprintf(stderr, "-averaging  integer             : averaging period (100)\n");
1555            fprintf(stderr, "-smoother   integer             : smoothing buffer (100)\n");
1556            fprintf(stderr, "\n");
1557            fprintf(stderr, "Second Pass options:\n");
1558            fprintf(stderr, "-kboost     integer             : I frame boost (10)\n");
1559            fprintf(stderr, "-kthresh    integer             : I frame reduction threshold (1)\n");
1560            fprintf(stderr, "-kreduction integer             : I frame reduction amount (20)\n");
1561            fprintf(stderr, "-ostrength  integer             : overflow control strength (5)\n");
1562            fprintf(stderr, "-oimprove   integer             : max overflow improvement (5)\n");
1563            fprintf(stderr, "-odegrade   integer             : max overflow degradation (5)\n");
1564            fprintf(stderr, "-chigh      integer             : high bitrate scenes degradation (0)\n");
1565            fprintf(stderr, "-clow       integer             : low bitrate scenes improvement (0)\n");
1566            fprintf(stderr, "-overhead   integer             : container frame overhead (24)\n");
1567            fprintf(stderr, "-vbvsize    integer             : use vbv buffer size\n");
1568            fprintf(stderr, "-vbvmax     integer             : vbv max bitrate\n");
1569            fprintf(stderr, "-vbvpeak    integer             : vbv peak bitrate over 1 second\n");
1570            fprintf(stderr, "\n");
1571            fprintf(stderr, "Other options\n");
1572            fprintf(stderr, " -noasm                         : do not use assembly optmized code\n");
1573            fprintf(stderr, " -turbo                         : use turbo presets for higher encoding speed\n");
1574            fprintf(stderr, " -quality integer               : quality ([0..%d]) (6)\n", ME_ELEMENTS - 1);
1575            fprintf(stderr, " -vhqmode integer               : level of R-D optimizations ([0..4]) (1)\n");
1576            fprintf(stderr, " -bvhq                          : use R-D optimizations for B-frames\n");
1577            fprintf(stderr, " -qpel                          : use quarter pixel ME\n");
1578            fprintf(stderr, " -gmc                           : use global motion compensation\n");
1579            fprintf(stderr, " -qtype   integer               : quantization type (H263:0, MPEG4:1) (0)\n");
1580            fprintf(stderr, " -qmatrix filename              : use custom MPEG4 quantization matrix\n");
1581            fprintf(stderr, " -interlaced [integer]          : interlaced encoding (BFF:1, TFF:2) (1)\n");
1582            fprintf(stderr, " -nopacked                      : Disable packed mode\n");
1583            fprintf(stderr, " -noclosed_gop                  : Disable closed GOP mode\n");
1584            fprintf(stderr, " -lumimasking                   : use lumimasking algorithm\n");
1585            fprintf(stderr, " -stats                         : print stats about encoded frames\n");
1586            fprintf(stderr, " -ssim [integer]                : prints ssim for every frame (accurate: 0 fast: 4) (2)\n");
1587            fprintf(stderr, " -ssim_file filename            : outputs the ssim stats into a file\n");
1588            fprintf(stderr, " -debug                         : activates xvidcore internal debugging output\n");
1589            fprintf(stderr, " -vop_debug                     : print some info directly into encoded frames\n");
1590            fprintf(stderr, " -nochromame                    : Disable chroma motion estimation\n");
1591            fprintf(stderr, " -notrellis                     : Disable trellis quantization\n");
1592            fprintf(stderr, " -imin    integer               : Minimum I Quantizer (1..31) (2)\n");
1593            fprintf(stderr, " -imax    integer               : Maximum I quantizer (1..31) (31)\n");
1594            fprintf(stderr, " -bmin    integer               : Minimum B Quantizer (1..31) (2)\n");
1595            fprintf(stderr, " -bmax    integer               : Maximum B quantizer (1..31) (31)\n");
1596            fprintf(stderr, " -pmin    integer               : Minimum P Quantizer (1..31) (2)\n");
1597            fprintf(stderr, " -pmax    integer               : Maximum P quantizer (1..31) (31)\n");
1598            fprintf(stderr, " -drop    integer               : Frame Drop Ratio (0..100) (0)\n");
1599            fprintf(stderr, " -start   integer               : Starting frame number\n");
1600            fprintf(stderr, " -threads integer               : Number of threads\n");
1601            fprintf(stderr, " -progress [integer]            : Show progress updates every n frames (10)\n");
1602            fprintf(stderr, " -par     integer[:integer]     : Set Pixel Aspect Ratio.\n");
1603            fprintf(stderr, "                                  1 = 1:1\n");
1604            fprintf(stderr, "                                  2 = 12:11 (4:3 PAL)\n");
1605            fprintf(stderr, "                                  3 = 10:11 (4:3 NTSC)\n");
1606            fprintf(stderr, "                                  4 = 16:11 (16:9 PAL)\n");
1607            fprintf(stderr, "                                  5 = 40:33 (16:9 NTSC)\n");
1608            fprintf(stderr, "                              other = custom (width:height)\n");
1609            fprintf(stderr, " -help                          : prints this help message\n");
1610            fprintf(stderr, "\n");
1611            fprintf(stderr, "NB: You can define %d zones repeating the -z[qw] option as needed.\n", MAX_ZONES);
1612    }    }
1613    
1614  #ifdef BFRAMES  /*****************************************************************************
1615    if (argc>=8)   *                       Input and output functions
1616    {     ARG_MAXBFRAMES = atoi(argv[7]);   *
1617          if ( (ARG_MAXBFRAMES < -1) || ( ARG_MAXBFRAMES > ARG_FRAMERATE) )   *      the are small and simple routines to read and write PGM and YUV
1618           { fprintf(stderr,"Wrong maximumnumber of bframes\n"); return -1; }   *      image. It's just for convenience, again nothing specific to XviD
1619          fprintf(stderr,"max. B-frames %d\n",ARG_MAXBFRAMES);   *
1620     *****************************************************************************/
1621    
1622    #ifndef READ_PNM
1623    static int
1624    read_pgmheader(FILE * handle)
1625    {
1626            int bytes, xsize, ysize, depth;
1627            char dummy[2];
1628    
1629            bytes = fread(dummy, 1, 2, handle);
1630    
1631            if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5'))
1632                    return (1);
1633    
1634            fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);
1635            if ((xsize > 4096) || (ysize > 4096*3/2) || (depth != 255)) {
1636                    fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);
1637                    return (2);
1638            }
1639            if ((XDIM == 0) || (YDIM == 0)) {
1640                    XDIM = xsize;
1641                    YDIM = ysize * 2 / 3;
1642    }    }
1643    
1644    if (argc>=9)          return (0);
   {     ARG_MAXFRAMENR = atoi(argv[8]);  
         if ( (ARG_BQUANTRATIO <= 0) )  
          { fprintf(stderr,"Wrong B-frames Quantizer ratio \n"); return -1; }  
         fprintf(stderr,"B-frames quant-ratio %d\n",ARG_BQUANTRATIO);  
1645    }    }
 #endif  
1646    
1647  /* now we know the sizes, so allocate memory */  static int
1648    read_pgmdata(FILE * handle,
1649                             unsigned char *image)
1650    {
1651            int i;
1652            char dummy;
1653    
1654    in_buffer = (unsigned char *) malloc(XDIM*YDIM);          unsigned char *y = image;
1655    if (!in_buffer)          unsigned char *u = image + XDIM * YDIM;
1656      goto free_all_memory;          unsigned char *v = image + XDIM * YDIM + XDIM / 2 * YDIM / 2;
1657    
1658    divx_buffer = (unsigned char *) malloc(XDIM*YDIM*2);          /* read Y component of picture */
1659    if (!divx_buffer)          fread(y, 1, XDIM * YDIM, handle);
1660      goto free_all_memory;  
1661            for (i = 0; i < YDIM / 2; i++) {
1662                    /* read U */
1663                    fread(u, 1, XDIM / 2, handle);
1664    
1665    YDIM = YDIM*2/3; // PGM is YUV 4:2:0 format, so real image height is *2/3 of PGM picture                  /* read V */
1666                    fread(v, 1, XDIM / 2, handle);
1667    
1668                    /* Update pointers */
1669                    u += XDIM / 2;
1670                    v += XDIM / 2;
1671            }
1672    
1673  /*********************************************************************/          /*  I don't know why, but this seems needed */
1674  /*                         XviD PART  Start                          */          fread(&dummy, 1, 1, handle);
 /*********************************************************************/  
1675    
1676    status = enc_init(use_assembler);          return (0);
1677          if (status)  }
1678    #else
1679    static int
1680    read_pnmheader(FILE * handle)
1681          {          {
1682                  fprintf(stderr,"Encore INIT problem, return value %d\n", status);          int bytes, xsize, ysize, depth;
1683                  goto release_all;          char dummy[2];
1684    
1685            bytes = fread(dummy, 1, 2, handle);
1686    
1687            if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '6'))
1688                    return (1);
1689    
1690            fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);
1691            if ((xsize > 1440) || (ysize > 2880) || (depth != 255)) {
1692                    fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);
1693                    return (2);
1694          }          }
1695    
1696  /*********************************************************************/          XDIM = xsize;
1697  /*                               Main loop                           */          YDIM = ysize;
1698  /*********************************************************************/  
1699            return (0);
1700    }
1701    
1702    do  static int
1703    read_pnmdata(FILE * handle,
1704                             unsigned char *image)
1705      {      {
1706          if (pgmflag)          int i;
1707                status = read_pgmdata(stdin, in_buffer);  // read PGM data (YUV-format)          char dummy;
1708          else  
1709                status = read_yuvdata(stdin, in_buffer);  // read raw data (YUV-format)          /* read Y component of picture */
1710            fread(image, 1, XDIM * YDIM * 3, handle);
1711    
1712            /*  I don't know why, but this seems needed */
1713            fread(&dummy, 1, 1, handle);
1714    
1715      if (status)          return (0);
1716    }
1717    #endif
1718    
1719    static int
1720    read_yuvdata(FILE * handle,
1721                             unsigned char *image)
1722          {          {
1723            // Couldn't read image, most likely end-of-file  
1724            continue;          if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) !=
1725                    (unsigned int) IMAGE_SIZE(XDIM, YDIM))
1726                    return (1);
1727            else
1728                    return (0);
1729          }          }
1730    
1731    /*****************************************************************************
1732     *     Routines for encoding: init encoder, frame step, release encoder
1733     ****************************************************************************/
1734    
1735    /* sample plugin */
1736    
1737      if (save_ref_flag)  int
1738    rawenc_debug(void *handle,
1739                             int opt,
1740                             void *param1,
1741                             void *param2)
1742          {          {
1743                  sprintf(filename, "%s%05d.pgm", filepath, filenr);          switch (opt) {
1744                  write_pgm(filename,in_buffer);          case XVID_PLG_INFO:
1745                    {
1746                            xvid_plg_info_t *info = (xvid_plg_info_t *) param1;
1747    
1748                            info->flags = XVID_REQDQUANTS;
1749                            return 0;
1750          }          }
1751    
1752            case XVID_PLG_CREATE:
1753            case XVID_PLG_DESTROY:
1754            case XVID_PLG_BEFORE:
1755                    return 0;
1756    
1757  /*********************************************************************/          case XVID_PLG_AFTER:
1758  /*               analyse this frame before encoding                  */                  {
1759  /*********************************************************************/                          xvid_plg_data_t *data = (xvid_plg_data_t *) param1;
1760                            int i, j;
1761    
1762                            printf("---[ frame: %5i   quant: %2i   length: %6i ]---\n",
1763                                       data->frame_num, data->quant, data->length);
1764                            for (j = 0; j < data->mb_height; j++) {
1765                                    for (i = 0; i < data->mb_width; i++)
1766                                            printf("%2i ", data->dquant[j * data->dquant_stride + i]);
1767                                    printf("\n");
1768                            }
1769    
1770  //      nothing is done here at the moment, but you could e.g. create                          return 0;
1771  //      histograms or measure entropy or apply preprocessing filters...                  }
1772            }
1773    
1774  /*********************************************************************/          return XVID_ERR_FAIL;
1775  /*               encode and decode this frame                        */  }
 /*********************************************************************/  
1776    
         enctime = -msecond();  
         status = enc_main(in_buffer, divx_buffer, &m4v_size, &frame_type[filenr]);  
         enctime += msecond();  
1777    
1778          totalenctime += enctime;  #define FRAMERATE_INCR 1001
         totalsize += m4v_size;  
1779    
         fprintf(stderr,"Frame %5d: intra %d, enctime =%6.1f ms length=%7d bytes\n",  
                  filenr, frame_type[filenr], enctime*1000, m4v_size);  
1780    
1781          if (save_m4v_flag)  /* Initialize encoder for first use, pass all needed parameters to the codec */
1782    static int
1783    enc_init(int use_assembler)
1784          {          {
1785                  fwrite(divx_buffer, m4v_size, 1, stdout);          int xerr;
1786            //xvid_plugin_cbr_t cbr;
1787        xvid_plugin_single_t single;
1788            xvid_plugin_2pass1_t rc2pass1;
1789            xvid_plugin_2pass2_t rc2pass2;
1790            xvid_plugin_ssim_t ssim;
1791            //xvid_plugin_fixed_t rcfixed;
1792            xvid_enc_plugin_t plugins[8];
1793            xvid_gbl_init_t xvid_gbl_init;
1794            xvid_enc_create_t xvid_enc_create;
1795            int i;
1796    
1797            /*------------------------------------------------------------------------
1798             * XviD core initialization
1799             *----------------------------------------------------------------------*/
1800    
1801            /* Set version -- version checking will done by xvidcore */
1802            memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
1803            xvid_gbl_init.version = XVID_VERSION;
1804        xvid_gbl_init.debug = ARG_DEBUG;
1805    
1806    
1807            /* Do we have to enable ASM optimizations ? */
1808            if (use_assembler) {
1809    
1810    #ifdef ARCH_IS_IA64
1811                    xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ASM;
1812    #else
1813                    xvid_gbl_init.cpu_flags = 0;
1814    #endif
1815            } else {
1816                    xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
1817          }          }
1818    
1819          if (pgmflag)          /* Initialize XviD core -- Should be done once per __process__ */
1820                  status = read_pgmheader(stdin);          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
1821                                  // because if this was the last PGM, stop now          enc_info();
1822    
1823            /*------------------------------------------------------------------------
1824             * XviD encoder initialization
1825             *----------------------------------------------------------------------*/
1826    
1827          filenr++;          /* Version again */
1828            memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
1829            xvid_enc_create.version = XVID_VERSION;
1830    
1831     } while ( (!status) && (filenr<ARG_MAXFRAMENR) );          /* Width and Height of input frames */
1832            xvid_enc_create.width = XDIM;
1833            xvid_enc_create.height = YDIM;
1834            xvid_enc_create.profile = 0xf5; /* Unrestricted */
1835    
1836            /* init plugins  */
1837    //    xvid_enc_create.zones = ZONES;
1838    //    xvid_enc_create.num_zones = NUM_ZONES;
1839    
1840            xvid_enc_create.plugins = plugins;
1841            xvid_enc_create.num_plugins = 0;
1842    
1843  /*********************************************************************/          if (ARG_SINGLE) {
1844  /*     calculate totals and averages for output, print results       */                  memset(&single, 0, sizeof(xvid_plugin_single_t));
1845  /*********************************************************************/                  single.version = XVID_VERSION;
1846                    single.bitrate = ARG_BITRATE;
1847                    single.reaction_delay_factor = ARG_REACTION;
1848                    single.averaging_period = ARG_AVERAGING;
1849                    single.buffer = ARG_SMOOTHER;
1850    
         totalsize    /= filenr;  
         totalenctime /= filenr;  
1851    
1852          for (i=0;i<filenr;i++)                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
1853                    plugins[xvid_enc_create.num_plugins].param = &single;
1854                    xvid_enc_create.num_plugins++;
1855                    if (!ARG_BITRATE)
1856                            prepare_cquant_zones();
1857            }
1858    
1859            if (ARG_PASS2) {
1860                    memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t));
1861                    rc2pass2.version = XVID_VERSION;
1862                    rc2pass2.filename = ARG_PASS2;
1863                    rc2pass2.bitrate = ARG_BITRATE;
1864    
1865                    rc2pass2.keyframe_boost = ARG_KBOOST;
1866                    rc2pass2.curve_compression_high = ARG_CHIGH;
1867                    rc2pass2.curve_compression_low = ARG_CLOW;
1868                    rc2pass2.overflow_control_strength = ARG_OVERSTRENGTH;
1869                    rc2pass2.max_overflow_improvement = ARG_OVERIMPROVE;
1870                    rc2pass2.max_overflow_degradation = ARG_OVERDEGRADE;
1871                    rc2pass2.kfreduction = ARG_KREDUCTION;
1872                    rc2pass2.kfthreshold = ARG_KTHRESH;
1873                    rc2pass2.container_frame_overhead = ARG_OVERHEAD;
1874    
1875    //              An example of activating VBV could look like this
1876                    rc2pass2.vbv_size     =  ARG_VBVSIZE;
1877                    rc2pass2.vbv_initial  =  (ARG_VBVSIZE*3)/4;
1878                    rc2pass2.vbv_maxrate  =  ARG_VBVMAXRATE;
1879                    rc2pass2.vbv_peakrate =  ARG_VBVPEAKRATE*3;
1880    
1881    
1882                    plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
1883                    plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
1884                    xvid_enc_create.num_plugins++;
1885            }
1886    
1887            if (ARG_PASS1) {
1888                    memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));
1889                    rc2pass1.version = XVID_VERSION;
1890                    rc2pass1.filename = ARG_PASS1;
1891                    if (ARG_FULL1PASS)
1892                            prepare_full1pass_zones();
1893                    plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;
1894                    plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
1895                    xvid_enc_create.num_plugins++;
1896            }
1897    
1898            /* Zones stuff */
1899            xvid_enc_create.zones = (xvid_enc_zone_t*)malloc(sizeof(xvid_enc_zone_t) * NUM_ZONES);
1900            xvid_enc_create.num_zones = NUM_ZONES;
1901            for (i=0; i < xvid_enc_create.num_zones; i++) {
1902                    xvid_enc_create.zones[i].frame = ZONES[i].frame;
1903                    xvid_enc_create.zones[i].base = 100;
1904                    xvid_enc_create.zones[i].mode = ZONES[i].mode;
1905                    xvid_enc_create.zones[i].increment = ZONES[i].modifier;
1906            }
1907    
1908    
1909            if (ARG_LUMIMASKING) {
1910                    plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
1911                    plugins[xvid_enc_create.num_plugins].param = NULL;
1912                    xvid_enc_create.num_plugins++;
1913            }
1914    
1915            if (ARG_DUMP) {
1916                    plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump;
1917                    plugins[xvid_enc_create.num_plugins].param = NULL;
1918                    xvid_enc_create.num_plugins++;
1919            }
1920    
1921            if (ARG_SSIM>=0 || ARG_SSIM_PATH != NULL) {
1922                    plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim;
1923    
1924                    if( ARG_SSIM >=0){
1925                            ssim.b_printstat = 1;
1926                            ssim.acc = ARG_SSIM;
1927                    } else {
1928                            ssim.b_printstat = 0;
1929                            ssim.acc = 2;
1930                    }
1931    
1932                    if(ARG_SSIM_PATH != NULL){
1933                            ssim.stat_path = ARG_SSIM_PATH;
1934                    }
1935    
1936                    ssim.b_visualize = 0;
1937                    plugins[xvid_enc_create.num_plugins].param = &ssim;
1938                    xvid_enc_create.num_plugins++;
1939            }
1940    
1941    #if 0
1942            if (ARG_DEBUG) {
1943                    plugins[xvid_enc_create.num_plugins].func = rawenc_debug;
1944                    plugins[xvid_enc_create.num_plugins].param = NULL;
1945                    xvid_enc_create.num_plugins++;
1946            }
1947    #endif
1948    
1949            xvid_enc_create.num_threads = ARG_THREADS;
1950    
1951            /* Frame rate  */
1952            xvid_enc_create.fincr = ARG_DWSCALE;
1953            xvid_enc_create.fbase = ARG_DWRATE;
1954    
1955            /* Maximum key frame interval */
1956        if (ARG_MAXKEYINTERVAL > 0) {
1957            xvid_enc_create.max_key_interval = ARG_MAXKEYINTERVAL;
1958        }else {
1959                xvid_enc_create.max_key_interval = (int) ARG_FRAMERATE *10;
1960        }
1961    
1962            xvid_enc_create.min_quant[0]=ARG_QUANTS[0];
1963            xvid_enc_create.min_quant[1]=ARG_QUANTS[2];
1964            xvid_enc_create.min_quant[2]=ARG_QUANTS[4];
1965            xvid_enc_create.max_quant[0]=ARG_QUANTS[1];
1966            xvid_enc_create.max_quant[1]=ARG_QUANTS[3];
1967            xvid_enc_create.max_quant[2]=ARG_QUANTS[5];
1968    
1969            /* Bframes settings */
1970            xvid_enc_create.max_bframes = ARG_MAXBFRAMES;
1971            xvid_enc_create.bquant_ratio = ARG_BQRATIO;
1972            xvid_enc_create.bquant_offset = ARG_BQOFFSET;
1973    
1974            /* Frame drop ratio */
1975            xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;
1976    
1977            /* Global encoder options */
1978            xvid_enc_create.global = 0;
1979    
1980            if (ARG_PACKED)
1981                    xvid_enc_create.global |= XVID_GLOBAL_PACKED;
1982    
1983            if (ARG_CLOSED_GOP)
1984                    xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
1985    
1986            if (ARG_STATS)
1987                    xvid_enc_create.global |= XVID_GLOBAL_EXTRASTATS_ENABLE;
1988    
1989            /* I use a small value here, since will not encode whole movies, but short clips */
1990            xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
1991    
1992            /* Retrieve the encoder instance from the structure */
1993            enc_handle = xvid_enc_create.handle;
1994    
1995            free(xvid_enc_create.zones);
1996    
1997            return (xerr);
1998    }
1999    
2000    static int
2001    enc_info()
2002    {
2003            xvid_gbl_info_t xvid_gbl_info;
2004            int ret;
2005    
2006            memset(&xvid_gbl_info, 0, sizeof(xvid_gbl_info));
2007            xvid_gbl_info.version = XVID_VERSION;
2008            ret = xvid_global(NULL, XVID_GBL_INFO, &xvid_gbl_info, NULL);
2009            if (xvid_gbl_info.build != NULL) {
2010                    fprintf(stderr, "xvidcore build version: %s\n", xvid_gbl_info.build);
2011            }
2012            fprintf(stderr, "Bitstream version: %d.%d.%d\n", XVID_VERSION_MAJOR(xvid_gbl_info.actual_version), XVID_VERSION_MINOR(xvid_gbl_info.actual_version), XVID_VERSION_PATCH(xvid_gbl_info.actual_version));
2013            fprintf(stderr, "Detected CPU flags: ");
2014            if (xvid_gbl_info.cpu_flags & XVID_CPU_ASM)
2015                    fprintf(stderr, "ASM ");
2016            if (xvid_gbl_info.cpu_flags & XVID_CPU_MMX)
2017                    fprintf(stderr, "MMX ");
2018            if (xvid_gbl_info.cpu_flags & XVID_CPU_MMXEXT)
2019                    fprintf(stderr, "MMXEXT ");
2020            if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE)
2021                    fprintf(stderr, "SSE ");
2022            if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2)
2023                    fprintf(stderr, "SSE2 ");
2024            if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE3)
2025                    fprintf(stderr, "SSE3 ");
2026            if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE41)
2027                    fprintf(stderr, "SSE41 ");
2028        if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW)
2029                    fprintf(stderr, "3DNOW ");
2030            if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT)
2031                    fprintf(stderr, "3DNOWEXT ");
2032            if (xvid_gbl_info.cpu_flags & XVID_CPU_TSC)
2033                    fprintf(stderr, "TSC ");
2034            fprintf(stderr, "\n");
2035            fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);
2036            if (!ARG_THREADS)
2037                    ARG_THREADS = xvid_gbl_info.num_threads;
2038            fprintf(stderr, " using %d threads.\n", ARG_THREADS);
2039            return ret;
2040    }
2041    
2042    static int
2043    enc_stop()
2044          {          {
2045                  switch (frame_type[i])          int xerr;
2046    
2047            /* Destroy the encoder instance */
2048            xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL);
2049    
2050            return (xerr);
2051    }
2052    
2053    static int
2054    enc_main(unsigned char *image,
2055                     unsigned char *bitstream,
2056                     int *key,
2057                     int *stats_type,
2058                     int *stats_quant,
2059                     int *stats_length,
2060                     int sse[3],
2061                     int framenum)
2062    {
2063            int ret;
2064    
2065            xvid_enc_frame_t xvid_enc_frame;
2066            xvid_enc_stats_t xvid_enc_stats;
2067    
2068            /* Version for the frame and the stats */
2069            memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
2070            xvid_enc_frame.version = XVID_VERSION;
2071    
2072            memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
2073            xvid_enc_stats.version = XVID_VERSION;
2074    
2075            /* Bind output buffer */
2076            xvid_enc_frame.bitstream = bitstream;
2077            xvid_enc_frame.length = -1;
2078    
2079            /* Initialize input image fields */
2080            if (image) {
2081                    xvid_enc_frame.input.plane[0] = image;
2082    #ifndef READ_PNM
2083                    xvid_enc_frame.input.csp = ARG_COLORSPACE;
2084                    xvid_enc_frame.input.stride[0] = XDIM;
2085    #else
2086                    xvid_enc_frame.input.csp = XVID_CSP_BGR;
2087                    xvid_enc_frame.input.stride[0] = XDIM*3;
2088    #endif
2089            } else {
2090                    xvid_enc_frame.input.csp = XVID_CSP_NULL;
2091            }
2092    
2093            /* Set up core's general features */
2094            xvid_enc_frame.vol_flags = 0;
2095            if (ARG_STATS)
2096                    xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS;
2097            if (ARG_QTYPE) {
2098                    xvid_enc_frame.vol_flags |= XVID_VOL_MPEGQUANT;
2099                    if (ARG_QMATRIX) {
2100                            xvid_enc_frame.quant_intra_matrix = qmatrix_intra;
2101                            xvid_enc_frame.quant_inter_matrix = qmatrix_inter;
2102                    }
2103                    else {
2104                            /* We don't use special matrices */
2105                            xvid_enc_frame.quant_intra_matrix = NULL;
2106                            xvid_enc_frame.quant_inter_matrix = NULL;
2107                    }
2108            }
2109    
2110            if (ARG_PAR)
2111                    xvid_enc_frame.par = ARG_PAR;
2112            else {
2113                    xvid_enc_frame.par = XVID_PAR_EXT;
2114                    xvid_enc_frame.par_width = ARG_PARWIDTH;
2115                    xvid_enc_frame.par_height = ARG_PARHEIGHT;
2116            }
2117    
2118    
2119            if (ARG_QPEL) {
2120                    xvid_enc_frame.vol_flags |= XVID_VOL_QUARTERPEL;
2121                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8;
2122            }
2123            if (ARG_GMC) {
2124                    xvid_enc_frame.vol_flags |= XVID_VOL_GMC;
2125                    xvid_enc_frame.motion |= XVID_ME_GME_REFINE;
2126            }
2127    
2128            /* Set up core's general features */
2129            xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY];
2130    
2131            if (ARG_INTERLACING) {
2132                    xvid_enc_frame.vol_flags |= XVID_VOL_INTERLACING;
2133                    if (ARG_INTERLACING == 2)
2134                            xvid_enc_frame.vop_flags |= XVID_VOP_TOPFIELDFIRST;
2135            }
2136    
2137            xvid_enc_frame.vop_flags |= XVID_VOP_HALFPEL;
2138            xvid_enc_frame.vop_flags |= XVID_VOP_HQACPRED;
2139    
2140        if (ARG_VOPDEBUG) {
2141            xvid_enc_frame.vop_flags |= XVID_VOP_DEBUG;
2142        }
2143    
2144        if (ARG_TRELLIS) {
2145            xvid_enc_frame.vop_flags |= XVID_VOP_TRELLISQUANT;
2146        }
2147    
2148            /* Frame type -- taken from function call parameter */
2149            /* Sometimes we might want to force the last frame to be a P Frame */
2150            xvid_enc_frame.type = *stats_type;
2151    
2152            /* Force the right quantizer -- It is internally managed by RC plugins */
2153            xvid_enc_frame.quant = 0;
2154    
2155            if (ARG_CHROMAME)
2156                    xvid_enc_frame.motion |= XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP;
2157    
2158            /* Set up motion estimation flags */
2159            xvid_enc_frame.motion |= motion_presets[ARG_QUALITY];
2160    
2161            if (ARG_TURBO)
2162                    xvid_enc_frame.motion |= XVID_ME_FASTREFINE16 | XVID_ME_FASTREFINE8 |
2163                                                                     XVID_ME_SKIP_DELTASEARCH | XVID_ME_FAST_MODEINTERPOLATE |
2164                                                                     XVID_ME_BFRAME_EARLYSTOP;
2165    
2166            if (ARG_BVHQ)
2167                    xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP;
2168    
2169            switch (ARG_VHQMODE) /* this is the same code as for vfw */
2170                  {                  {
2171                  case 0:          case 1: /* VHQ_MODE_DECISION */
2172                          Pframes++;                  xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
2173                    break;
2174    
2175            case 2: /* VHQ_LIMITED_SEARCH */
2176                    xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
2177                    xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD;
2178                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
2179                          break;                          break;
2180                  case 1:  
2181                          Iframes++;          case 3: /* VHQ_MEDIUM_SEARCH */
2182                    xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
2183                    xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD;
2184                    xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE8_RD;
2185                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
2186                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8_RD;
2187                    xvid_enc_frame.motion |= XVID_ME_CHECKPREDICTION_RD;
2188                          break;                          break;
2189                  case 2:  
2190            case 4: /* VHQ_WIDE_SEARCH */
2191                    xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
2192                    xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD;
2193                    xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE8_RD;
2194                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
2195                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8_RD;
2196                    xvid_enc_frame.motion |= XVID_ME_CHECKPREDICTION_RD;
2197                    xvid_enc_frame.motion |= XVID_ME_EXTSEARCH_RD;
2198                    break;
2199    
2200                  default:                  default:
                         Bframes++;  
2201                          break;                          break;
2202                  }                  }
2203    
2204            /* Not sure what this does */
2205            // force keyframe spacing in 2-pass 1st pass
2206            if (ARG_QUALITY == 0)
2207                    xvid_enc_frame.type = XVID_TYPE_IVOP;
2208    
2209            /* frame-based stuff */
2210            apply_zone_modifiers(&xvid_enc_frame, framenum);
2211    
2212    
2213            /* Encode the frame */
2214            ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
2215                                              &xvid_enc_stats);
2216    
2217            *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME);
2218            *stats_type = xvid_enc_stats.type;
2219            *stats_quant = xvid_enc_stats.quant;
2220            *stats_length = xvid_enc_stats.length;
2221            sse[0] = xvid_enc_stats.sse_y;
2222            sse[1] = xvid_enc_stats.sse_u;
2223            sse[2] = xvid_enc_stats.sse_v;
2224    
2225            return (ret);
2226    }
2227    
2228    void
2229    sort_zones(zone_t * zones, int zone_num, int * sel)
2230    {
2231            int i, j;
2232            zone_t tmp;
2233            for (i = 0; i < zone_num; i++) {
2234                    int cur = i;
2235                    int min_f = zones[i].frame;
2236                    for (j = i + 1; j < zone_num; j++) {
2237                            if (zones[j].frame < min_f) {
2238                                    min_f = zones[j].frame;
2239                                    cur = j;
2240                            }
2241                    }
2242                    if (cur != i) {
2243                            tmp = zones[i];
2244                            zones[i] = zones[cur];
2245                            zones[cur] = tmp;
2246                            if (i == *sel) *sel = cur;
2247                            else if (cur == *sel) *sel = i;
2248                    }
2249            }
2250          }          }
2251    
2252          fprintf(stderr,"Avg: enctime %5.2f ms, %5.2f fps, filesize =%d\n",  /* constant-quant zones for fixed quant encoding */
2253                  1000*totalenctime, 1./totalenctime, totalsize);  static void
2254    prepare_cquant_zones() {
2255    
2256  /*********************************************************************/          int i = 0;
2257  /*                         XviD PART  Stop                           */          if (NUM_ZONES == 0 || ZONES[0].frame != 0) {
2258  /*********************************************************************/                  /* first zone does not start at frame 0 or doesn't exist */
2259    
2260  release_all:                  if (NUM_ZONES >= MAX_ZONES) NUM_ZONES--; /* we sacrifice last zone */
2261    
2262                    ZONES[NUM_ZONES].frame = 0;
2263                    ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
2264                    ZONES[NUM_ZONES].modifier = ARG_CQ;
2265                    ZONES[NUM_ZONES].type = XVID_TYPE_AUTO;
2266                    ZONES[NUM_ZONES].greyscale = 0;
2267                    ZONES[NUM_ZONES].chroma_opt = 0;
2268                    ZONES[NUM_ZONES].bvop_threshold = 0;
2269                    ZONES[NUM_ZONES].cartoon_mode = 0;
2270                    NUM_ZONES++;
2271    
2272                    sort_zones(ZONES, NUM_ZONES, &i);
2273            }
2274    
2275            /* step 2: let's change all weight zones into quant zones */
2276    
2277            for(i = 0; i < NUM_ZONES; i++)
2278                    if (ZONES[i].mode == XVID_ZONE_WEIGHT) {
2279                            ZONES[i].mode = XVID_ZONE_QUANT;
2280                            ZONES[i].modifier = (100*ARG_CQ) / ZONES[i].modifier;
2281                    }
2282    }
2283    
2284          if (enc_handle)  /* full first pass zones */
2285    static void
2286    prepare_full1pass_zones() {
2287    
2288            int i = 0;
2289            if (NUM_ZONES == 0 || ZONES[0].frame != 0) {
2290                    /* first zone does not start at frame 0 or doesn't exist */
2291    
2292                    if (NUM_ZONES >= MAX_ZONES) NUM_ZONES--; /* we sacrifice last zone */
2293    
2294                    ZONES[NUM_ZONES].frame = 0;
2295                    ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
2296                    ZONES[NUM_ZONES].modifier = 200;
2297                    ZONES[NUM_ZONES].type = XVID_TYPE_AUTO;
2298                    ZONES[NUM_ZONES].greyscale = 0;
2299                    ZONES[NUM_ZONES].chroma_opt = 0;
2300                    ZONES[NUM_ZONES].bvop_threshold = 0;
2301                    ZONES[NUM_ZONES].cartoon_mode = 0;
2302                    NUM_ZONES++;
2303    
2304                    sort_zones(ZONES, NUM_ZONES, &i);
2305            }
2306    
2307            /* step 2: let's change all weight zones into quant zones */
2308    
2309            for(i = 0; i < NUM_ZONES; i++)
2310                    if (ZONES[i].mode == XVID_ZONE_WEIGHT) {
2311                            ZONES[i].mode = XVID_ZONE_QUANT;
2312                            ZONES[i].modifier = 200;
2313                    }
2314    }
2315    
2316    static void apply_zone_modifiers(xvid_enc_frame_t * frame, int framenum)
2317          {          {
2318                  status = enc_stop();          int i;
2319                  if (status)  
2320                          fprintf(stderr,"Encore RELEASE problem return value %d\n", status);          for (i=0; i<NUM_ZONES && ZONES[i].frame <= framenum; i++) ;
2321    
2322            if (--i < 0) return; /* there are no zones, or we're before the first zone */
2323    
2324            if (framenum == ZONES[i].frame)
2325                    frame->type = ZONES[i].type;
2326    
2327            if (ZONES[i].greyscale) {
2328                    frame->vop_flags |= XVID_VOP_GREYSCALE;
2329          }          }
2330    
2331  free_all_memory:          if (ZONES[i].chroma_opt) {
2332          free(divx_buffer);                  frame->vop_flags |= XVID_VOP_CHROMAOPT;
2333          free(in_buffer);          }
2334    
2335    return 0;          if (ZONES[i].cartoon_mode) {
2336                    frame->vop_flags |= XVID_VOP_CARTOON;
2337                    frame->motion |= XVID_ME_DETECT_STATIC_MOTION;
2338            }
2339    
2340            if (ARG_MAXBFRAMES) {
2341                    frame->bframe_threshold = ZONES[i].bvop_threshold;
2342            }
2343    }
2344    
2345    void removedivxp(char *buf, int bufsize) {
2346            int i;
2347            char* userdata;
2348    
2349            for (i=0; i <= (bufsize-sizeof(userdata_start_code)); i++) {
2350                    if (memcmp((void*)userdata_start_code, (void*)(buf+i), strlen(userdata_start_code))==0) {
2351                            if ((userdata = strstr(buf+i+4, "DivX"))!=NULL) {
2352                                    userdata[strlen(userdata)-1] = '\0';
2353                                    return;
2354                            }
2355                    }
2356            }
2357  }  }

Legend:
Removed from v.376  
changed lines
  Added in v.1794

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