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

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

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

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

Legend:
Removed from v.824  
changed lines
  Added in v.1712

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