[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

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

Legend:
Removed from v.566  
changed lines
  Added in v.2083

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