[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 1863, Wed May 27 15:52:05 2009 UTC revision 2076, Wed Mar 5 08:37:24 2014 UTC
# Line 6  Line 6 
6   *  Copyright(C) 2002-2003 Christoph Lampert <gruel@web.de>   *  Copyright(C) 2002-2003 Christoph Lampert <gruel@web.de>
7   *               2002-2003 Edouard Gomez <ed.gomez@free.fr>   *               2002-2003 Edouard Gomez <ed.gomez@free.fr>
8   *               2003      Peter Ross <pross@xvid.org>   *               2003      Peter Ross <pross@xvid.org>
9     *               2003-2010 Michael Militzer <isibaar@xvid.org>
10   *   *
11   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
12   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 21  Line 22 
22   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
23   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24   *   *
25   * $Id: xvid_encraw.c,v 1.38 2009-05-27 15:52:05 Isibaar Exp $   * $Id$
26   *   *
27   ****************************************************************************/   ****************************************************************************/
28    
# Line 39  Line 40 
40   ************************************************************************/   ************************************************************************/
41    
42  #include <stdio.h>  #include <stdio.h>
 //#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    #include <ctype.h>
47  #ifndef WIN32  #ifndef WIN32
48  #include <sys/time.h>  #include <sys/time.h>
49  #else  #else
# Line 54  Line 55 
55  #endif  #endif
56    
57  #include "xvid.h"  #include "xvid.h"
58    #include "portab.h" /* for pthread */
59    
60  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
61  #include "matroska.cpp"  #include "matroska.cpp"
# Line 61  Line 63 
63    
64  #undef READ_PNM  #undef READ_PNM
65    
66    //#define USE_APP_LEVEL_THREADING /* Should xvid_encraw app use multi-threading? */
67    
68  /*****************************************************************************  /*****************************************************************************
69   *                            Quality presets   *                            Quality presets
70   ****************************************************************************/   ****************************************************************************/
# Line 121  Line 125 
125   ****************************************************************************/   ****************************************************************************/
126    
127  #define MAX_ZONES   64  #define MAX_ZONES   64
128    #define MAX_ENC_INSTANCES 4
129    #define MAX_XDIM 4096
130    #define MAX_YDIM 4096
131  #define DEFAULT_QUANT 400  #define DEFAULT_QUANT 400
132    #define DEFAULT_BITRATE 700000 /* bitrate expressed in bps, not kbps */
133    #define DEFAULT_INTERLACING 1  /* 1:BFF, 2:TFF */
134    #define DEFAULT_SSIM 2
135    #define DEFAULT_PROGRESS 10    /* show progress every 10 frames by default */
136    
137  typedef struct  typedef struct
138  {  {
# Line 145  Line 155 
155          int quants[32];          int quants[32];
156  } frame_stats_t;  } frame_stats_t;
157    
158    typedef struct
159    {
160            pthread_t handle;       /* thread's handle */
161    
162            int start_num;          /* begin/end of sequence */
163            int stop_num;
164    
165            char *outfilename;      /* output filename */
166            char *statsfilename1;   /* pass1 statsfile */
167    
168            int input_num;
169    
170            int totalsize;          /* encoder stats */
171            double totalenctime;
172            float totalPSNR[3];
173            frame_stats_t framestats[7];
174    } enc_sequence_data_t;
175    
176  /* Maximum number of frames to encode */  /* Maximum number of frames to encode */
177  #define ABS_MAXFRAMENR -1 /* no limit */  #define ABS_MAXFRAMENR -1 /* no limit */
178    
# Line 162  Line 190 
190    
191  static zone_t ZONES[MAX_ZONES];  static zone_t ZONES[MAX_ZONES];
192  static  int NUM_ZONES = 0;  static  int NUM_ZONES = 0;
 static  frame_stats_t framestats[7];  
193    
194  static  int ARG_STATS = 0;  static  int ARG_NUM_APP_THREADS = 1;
195    static  int ARG_CPU_FLAGS = 0;
196  static  int ARG_SSIM = -1;  static  int ARG_SSIM = -1;
197  static  char* ARG_SSIM_PATH = NULL;  static  char* ARG_SSIM_PATH = NULL;
198  static  int ARG_DUMP = 0;  static  int ARG_DUMP = 0;
# Line 176  Line 204 
204  static  char *ARG_PASS2 = 0;  static  char *ARG_PASS2 = 0;
205  //static int ARG_QUALITY = ME_ELEMENTS - 1;  //static int ARG_QUALITY = ME_ELEMENTS - 1;
206  static  int ARG_QUALITY = 6;  static  int ARG_QUALITY = 6;
207  static  float ARG_FRAMERATE = 25.00f;  static  float ARG_FRAMERATE = 0.f;
208  static  int ARG_DWRATE = 25;  static  int ARG_DWRATE = 25;
209  static  int ARG_DWSCALE = 1;  static  int ARG_DWSCALE = 1;
210  static  int ARG_MAXFRAMENR = ABS_MAXFRAMENR;  static  int ARG_MAXFRAMENR = ABS_MAXFRAMENR;
# Line 187  Line 215 
215  static  int ARG_SAVEMPEGSTREAM = 0;  static  int ARG_SAVEMPEGSTREAM = 0;
216  static  int ARG_SAVEINDIVIDUAL = 0;  static  int ARG_SAVEINDIVIDUAL = 0;
217  static  char *ARG_OUTPUTFILE = NULL;  static  char *ARG_OUTPUTFILE = NULL;
 #ifdef XVID_AVI_OUTPUT  
218  static  char *ARG_AVIOUTPUTFILE = NULL;  static  char *ARG_AVIOUTPUTFILE = NULL;
 #endif  
 #ifdef XVID_MKV_OUTPUT  
219  static  char *ARG_MKVOUTPUTFILE = NULL;  static  char *ARG_MKVOUTPUTFILE = NULL;
 #endif  
 #ifdef XVID_AVI_INPUT  
 static  PAVISTREAM avi_stream = NULL;  
 static  PAVIFILE avi_file = NULL;  
 static  LPBITMAPINFOHEADER info_header = NULL;  
 static  PGETFRAME get_frame = NULL;  
 #else  
 #define get_frame NULL  
 #endif  
220  static  char *ARG_TIMECODEFILE = NULL;  static  char *ARG_TIMECODEFILE = NULL;
221  static  int XDIM = 0;  static  int XDIM = 0;
222  static  int YDIM = 0;  static  int YDIM = 0;
223  static  int ARG_BQRATIO = 150;  static  int ARG_BQRATIO = 150;
224  static  int ARG_BQOFFSET = 100;  static  int ARG_BQOFFSET = 100;
225  static  int ARG_MAXBFRAMES = 2;  static  int ARG_MAXBFRAMES = 2;
 static  int ARG_PACKED = 1;  
226  static  int ARG_DEBUG = 0;  static  int ARG_DEBUG = 0;
 static  int ARG_VOPDEBUG = 0;  
 static  int ARG_TRELLIS = 1;  
227  static  int ARG_QTYPE = 0;  static  int ARG_QTYPE = 0;
228  static  int ARG_QMATRIX = 0;  static  int ARG_QMATRIX = 0;
 static  int ARG_GMC = 0;  
229  static  int ARG_INTERLACING = 0;  static  int ARG_INTERLACING = 0;
 static  int ARG_QPEL = 0;  
 static  int ARG_TURBO = 0;  
230  static  int ARG_VHQMODE = 1;  static  int ARG_VHQMODE = 1;
231  static  int ARG_BVHQ = 0;  static  int ARG_QMETRIC = 0;
 static  int ARG_CLOSED_GOP = 1;  
 static  int ARG_CHROMAME = 1;  
232  static  int ARG_PAR = 1;  static  int ARG_PAR = 1;
233  static  int ARG_PARHEIGHT;  static  int ARG_PARHEIGHT;
234  static  int ARG_PARWIDTH;  static  int ARG_PARWIDTH;
# Line 244  Line 252 
252  static  int ARG_VBVMAXRATE = 0;  static  int ARG_VBVMAXRATE = 0;
253  static  int ARG_VBVPEAKRATE = 0;  static  int ARG_VBVPEAKRATE = 0;
254  static  int ARG_THREADS = 0;  static  int ARG_THREADS = 0;
255  static  int ARG_VFR = 0;  static  int ARG_SLICES = 1;
256  static  int ARG_PROGRESS = 0;  static  int ARG_PROGRESS = 0;
257  static  int ARG_COLORSPACE = XVID_CSP_YV12;  static  int ARG_COLORSPACE = XVID_CSP_YV12;
258          /* the path where to save output */          /* the path where to save output */
259  static char filepath[256] = "./";  static char filepath[256] = "./";
260          /* Internal structures (handles) for encoding and decoding */  
261  static  void *enc_handle = NULL;  /* on/off options */
262    static  int ARG_USE_ASSEMBLER = 1;
263    static  int ARG_TURBO         = 0;
264    static  int ARG_BVHQ          = 0;
265    static  int ARG_QPEL          = 0;
266    static  int ARG_GMC           = 0;
267    static  int ARG_PACKED        = 1;
268    static  int ARG_CLOSED_GOP    = 1;
269    static  int ARG_STATS         = 0;
270    static  int ARG_PSNRHVSM      = 0;
271    static  int ARG_VOPDEBUG      = 0;
272    static  int ARG_CHROMAME      = 1;
273    static  int ARG_TRELLIS       = 1;
274    
275  static  unsigned char qmatrix_intra[64];  static  unsigned char qmatrix_intra[64];
276  static  unsigned char qmatrix_inter[64];  static  unsigned char qmatrix_inter[64];
277    
# Line 292  Line 313 
313                                                  unsigned char *image);                                                  unsigned char *image);
314    
315  /* Encoder related functions */  /* Encoder related functions */
316  static int enc_init(int use_assembler);  static void enc_gbl(int use_assembler);
317    static int  enc_init(void **enc_handle, char *stats_pass1, int start_num);
318  static int enc_info();  static int enc_info();
319  static int enc_stop();  static int  enc_stop(void *enc_handle);
320  static int enc_main(unsigned char *image,  static int  enc_main(void *enc_handle,
321                                             unsigned char *image,
322                                          unsigned char *bitstream,                                          unsigned char *bitstream,
323                                          int *key,                                          int *key,
324                                          int *stats_type,                                          int *stats_type,
# Line 303  Line 326 
326                                          int *stats_length,                                          int *stats_length,
327                                          int stats[3],                                          int stats[3],
328                                          int framenum);                                          int framenum);
329    static void encode_sequence(enc_sequence_data_t *h);
330    
331  /* Zone Related Functions */  /* Zone Related Functions */
332  static void apply_zone_modifiers(xvid_enc_frame_t * frame, int framenum);  static void apply_zone_modifiers(xvid_enc_frame_t * frame, int framenum);
# Line 321  Line 345 
345  main(int argc,  main(int argc,
346           char *argv[])           char *argv[])
347  {  {
   
         unsigned char *mp4_buffer = NULL;  
         unsigned char *in_buffer = NULL;  
         unsigned char *out_buffer = NULL;  
   
         double enctime;  
348          double totalenctime = 0.;          double totalenctime = 0.;
349          float totalPSNR[3] = {0., 0., 0.};          float totalPSNR[3] = {0., 0., 0.};
350    
351          FILE *statsfile;          FILE *statsfile;
352            frame_stats_t framestats[7];
353    
354          int totalsize;          int input_num = 0;
355          int result;          int totalsize = 0;
356          int m4v_size;          int use_assembler = ARG_USE_ASSEMBLER;
         int key;  
         int stats_type;  
         int stats_quant;  
         int stats_length;  
         int use_assembler = 1;  
         int fakenvop = 0;  
357          int i;          int i;
         int nvop_counter;  
   
         int input_num;  
         int output_num;  
   
         char filename[256];  
   
         FILE *in_file = stdin;  
         FILE *out_file = NULL;  
         FILE *time_file = NULL;  
   
 #ifdef XVID_AVI_OUTPUT  
         int avierr;  
         PAVIFILE myAVIFile=NULL;  
         PAVISTREAM myAVIStream=NULL;  
         AVISTREAMINFO myAVIStreamInfo;  
         BITMAPINFOHEADER myBitmapInfoHeader;  
         AVIFileInit();  
 #endif  
 #ifdef XVID_MKV_OUTPUT  
         PMKVFILE myMKVFile=NULL;  
         PMKVSTREAM myMKVStream=NULL;  
         MKVSTREAMINFO myMKVStreamInfo;  
 #endif  
358    
359          printf("xvid_encraw - raw mpeg4 bitstream encoder ");          printf("xvid_encraw - raw mpeg4 bitstream encoder ");
360          printf("written by Christoph Lampert 2002-2003\n\n");          printf("written by Christoph Lampert\n\n");
361    
362          /* Is there a dumb XviD coder ? */          /* Is there a dumb Xvid coder ? */
363          if(ME_ELEMENTS != VOP_ELEMENTS) {          if(ME_ELEMENTS != VOP_ELEMENTS) {
364                  fprintf(stderr, "Presets' arrays should have the same number of elements -- Please fill a bug to xvid-devel@xvid.org\n");                  fprintf(stderr, "Presets' arrays should have the same number of elements -- Please file a bug to xvid-devel@xvid.org\n");
365                  return(-1);                  return(-1);
366          }          }
367    
368            /* Clear framestats */
369            memset(framestats, 0, sizeof(framestats));
370    
371  /*****************************************************************************  /*****************************************************************************
372   *                            Command line parsing   *                            Command line parsing
373   ****************************************************************************/   ****************************************************************************/
# Line 406  Line 399 
399                                  ARG_BITRATE = atoi(argv[i+1]);                                  ARG_BITRATE = atoi(argv[i+1]);
400                          if (ARG_BITRATE) {                          if (ARG_BITRATE) {
401                                  i++;                                  i++;
402                                  if (ARG_BITRATE <= 10000)                                  if (ARG_BITRATE <= 20000)
403                                          /* if given parameter is <= 10000, assume it means kbps */                                          /* if given parameter is <= 20000, assume it means kbps */
404                                          ARG_BITRATE *= 1000;                                          ARG_BITRATE *= 1000;
405                          }                          }
406                          else                          else
407                                  ARG_BITRATE = 700000;                                  ARG_BITRATE = DEFAULT_BITRATE;
408                  } else if (strcmp("-size", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-size", argv[i]) == 0 && i < argc - 1) {
409                          i++;                          i++;
410                          ARG_TARGETSIZE = atoi(argv[i]);                          ARG_TARGETSIZE = atoi(argv[i]);
# Line 460  Line 453 
453                          ARG_PACKED = 2;                          ARG_PACKED = 2;
454                  } else if (strcmp("-nochromame", argv[i]) == 0) {                  } else if (strcmp("-nochromame", argv[i]) == 0) {
455                          ARG_CHROMAME = 0;                          ARG_CHROMAME = 0;
456                    } else if (strcmp("-chromame", argv[i]) == 0) {
457                            ARG_CHROMAME = 1;
458                  } else if (strcmp("-threads", argv[i]) == 0 && i < argc -1) {                  } else if (strcmp("-threads", argv[i]) == 0 && i < argc -1) {
459                          i++;                          i++;
460                          ARG_THREADS = atoi(argv[i]);                          ARG_THREADS = atoi(argv[i]);
461                    } else if (strcmp("-slices", argv[i]) == 0 && i < argc -1) {
462                            i++;
463                            ARG_SLICES = atoi(argv[i]);
464                  } else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) {
465                          i++;                          i++;
466                          ARG_BQRATIO = atoi(argv[i]);                          ARG_BQRATIO = atoi(argv[i]);
# Line 497  Line 495 
495                                  memset(&ZONES[NUM_ZONES], 0, sizeof(zone_t));                                  memset(&ZONES[NUM_ZONES], 0, sizeof(zone_t));
496    
497                                  ZONES[NUM_ZONES].frame = startframe;                                  ZONES[NUM_ZONES].frame = startframe;
498                                  ZONES[NUM_ZONES].modifier = atof(options)*100;                                  ZONES[NUM_ZONES].modifier = (int)(atof(options)*100);
499                                  if (toupper(c)=='Q')                                  if (toupper(c)=='Q')
500                                          ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;                                          ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
501                                  else if (toupper(c)=='W')                                  else if (toupper(c)=='W')
# Line 510  Line 508 
508                                  if ((frameoptions=strchr(options, ','))!=NULL) {                                  if ((frameoptions=strchr(options, ','))!=NULL) {
509                                          int readchar=0, count;                                          int readchar=0, count;
510                                          frameoptions++;                                          frameoptions++;
511                                          while (readchar<strlen(frameoptions)) {                                          while (readchar<(int)strlen(frameoptions)) {
512                                                  if (sscanf(frameoptions+readchar, "%d%n", &(ZONES[NUM_ZONES].bvop_threshold), &count)==1) {                                                  if (sscanf(frameoptions+readchar, "%d%n", &(ZONES[NUM_ZONES].bvop_threshold), &count)==1) {
513                                                          readchar += count;                                                          readchar += count;
514                                                  }                                                  }
# Line 548  Line 546 
546                          else {                          else {
547                                  ZONES[NUM_ZONES].mode = XVID_ZONE_WEIGHT;                                  ZONES[NUM_ZONES].mode = XVID_ZONE_WEIGHT;
548                          }                          }
549                          ZONES[NUM_ZONES].modifier = atof(argv[i+2])*100;                          ZONES[NUM_ZONES].modifier = (int)(atof(argv[i+2])*100);
550                          i++;                          i++;
551              ZONES[NUM_ZONES].frame = atoi(argv[i]);              ZONES[NUM_ZONES].frame = atoi(argv[i]);
552                          i++;                          i++;
# Line 568  Line 566 
566                  } else if (strcmp("-vhqmode", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-vhqmode", argv[i]) == 0 && i < argc - 1) {
567                          i++;                          i++;
568                          ARG_VHQMODE = atoi(argv[i]);                          ARG_VHQMODE = atoi(argv[i]);
569                    } else if (strcmp("-metric", argv[i]) == 0 && i < argc - 1) {
570                            i++;
571                            ARG_QMETRIC = atoi(argv[i]);
572                  } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) {
573                          int exponent;                          int exponent;
574                          i++;                          i++;
575                          ARG_FRAMERATE = (float) atof(argv[i]);                          ARG_FRAMERATE = (float) atof(argv[i]);
576                          exponent = strcspn(argv[i], ".");                          exponent = (int)strcspn(argv[i], ".");
577                          if (exponent<strlen(argv[i]))                          if (exponent<(int)strlen(argv[i]))
578                                  exponent=pow(10.0, (int)(strlen(argv[i])-1-exponent));                                  exponent=(int)pow(10.0, (int)(strlen(argv[i])-1-exponent));
579                          else                          else
580                                  exponent=1;                                  exponent=1;
581                          ARG_DWRATE = atof(argv[i])*exponent;                          ARG_DWRATE = (int)(atof(argv[i])*exponent);
582                          ARG_DWSCALE = exponent;                          ARG_DWSCALE = exponent;
583                          exponent = gcd(ARG_DWRATE, ARG_DWSCALE);                          exponent = gcd(ARG_DWRATE, ARG_DWSCALE);
584                          ARG_DWRATE /= exponent;                          ARG_DWRATE /= exponent;
# Line 590  Line 591 
591                          ARG_INPUTFILE = argv[i];                          ARG_INPUTFILE = argv[i];
592                  } else if (strcmp("-stats", argv[i]) == 0) {                  } else if (strcmp("-stats", argv[i]) == 0) {
593                          ARG_STATS = 1;                          ARG_STATS = 1;
594                    } else if (strcmp("-nostats", argv[i]) == 0) {
595                            ARG_STATS = 0;
596                  } else if (strcmp("-ssim", argv[i]) == 0) {                  } else if (strcmp("-ssim", argv[i]) == 0) {
597                          ARG_SSIM = 2;                          ARG_SSIM = DEFAULT_SSIM;
598                          if ((i < argc - 1) && (*argv[i+1] != '-')) {                          if ((i < argc - 1) && (*argv[i+1] != '-')) {
599                                  i++;                                  i++;
600                                  ARG_SSIM = atoi(argv[i]);                                  ARG_SSIM = atoi(argv[i]);
601                          }                          }
602                    } else if (strcmp("-psnrhvsm", argv[i]) == 0) {
603                            ARG_PSNRHVSM = 1;
604                    } else if (strcmp("-nopsnrhvsm", argv[i]) == 0) {
605                            ARG_PSNRHVSM = 0;
606                  } else if (strcmp("-ssim_file", argv[i]) == 0 && i < argc -1) {                  } else if (strcmp("-ssim_file", argv[i]) == 0 && i < argc -1) {
607                          i++;                          i++;
608                          ARG_SSIM_PATH = argv[i];                          ARG_SSIM_PATH = argv[i];
# Line 686  Line 693 
693  #endif  #endif
694                  } else if (strcmp("-vop_debug", argv[i]) == 0) {                  } else if (strcmp("-vop_debug", argv[i]) == 0) {
695                          ARG_VOPDEBUG = 1;                          ARG_VOPDEBUG = 1;
696                    } else if (strcmp("-novop_debug", argv[i]) == 0) {
697                            ARG_VOPDEBUG = 0;
698                    } else if (strcmp("-trellis", argv[i]) == 0) {
699                            ARG_TRELLIS = 1;
700                  } else if (strcmp("-notrellis", argv[i]) == 0) {                  } else if (strcmp("-notrellis", argv[i]) == 0) {
701                          ARG_TRELLIS = 0;                          ARG_TRELLIS = 0;
702                  } else if (strcmp("-bvhq", argv[i]) == 0) {                  } else if (strcmp("-bvhq", argv[i]) == 0) {
703                          ARG_BVHQ = 1;                          ARG_BVHQ = 1;
704                    } else if (strcmp("-nobvhq", argv[i]) == 0) {
705                            ARG_BVHQ = 0;
706                  } else if (strcmp("-qpel", argv[i]) == 0) {                  } else if (strcmp("-qpel", argv[i]) == 0) {
707                          ARG_QPEL = 1;                          ARG_QPEL = 1;
708                    } else if (strcmp("-noqpel", argv[i]) == 0) {
709                            ARG_QPEL = 0;
710                  } else if (strcmp("-turbo", argv[i]) == 0) {                  } else if (strcmp("-turbo", argv[i]) == 0) {
711                          ARG_TURBO = 1;                          ARG_TURBO = 1;
712                    } else if (strcmp("-noturbo", argv[i]) == 0) {
713                            ARG_TURBO = 0;
714                  } else if (strcmp("-gmc", argv[i]) == 0) {                  } else if (strcmp("-gmc", argv[i]) == 0) {
715                          ARG_GMC = 1;                          ARG_GMC = 1;
716                    } else if (strcmp("-nogmc", argv[i]) == 0) {
717                            ARG_GMC = 0;
718                  } else if (strcmp("-interlaced", argv[i]) == 0) {                  } else if (strcmp("-interlaced", argv[i]) == 0) {
719                          if ((i < argc - 1) && (*argv[i+1] != '-')) {                          if ((i < argc - 1) && (*argv[i+1] != '-')) {
720                                  i++;                                  i++;
721                                  ARG_INTERLACING = atoi(argv[i]);                                  ARG_INTERLACING = atoi(argv[i]);
722                          } else {                          } else {
723                                  ARG_INTERLACING = 1;                                  ARG_INTERLACING = DEFAULT_INTERLACING;
724                          }                          }
725                  } else if (strcmp("-noclosed_gop", argv[i]) == 0) {                  } else if (strcmp("-noclosed_gop", argv[i]) == 0) {
726                          ARG_CLOSED_GOP = 0;                          ARG_CLOSED_GOP = 0;
# Line 715  Line 734 
734                          ARG_VBVMAXRATE = atoi(argv[i]);                          ARG_VBVMAXRATE = atoi(argv[i]);
735                  } else if (strcmp("-vbvpeak", argv[i]) == 0 && i < argc -1) {                  } else if (strcmp("-vbvpeak", argv[i]) == 0 && i < argc -1) {
736                          i++;                          i++;
737                          ARG_VBVPEAKRATE = atoi(argv[i])*3;                          ARG_VBVPEAKRATE = atoi(argv[i]);
738                  } else if (strcmp("-reaction", argv[i]) == 0 && i < argc -1) {                  } else if (strcmp("-reaction", argv[i]) == 0 && i < argc -1) {
739                          i++;                          i++;
740                          ARG_REACTION = atoi(argv[i]);                          ARG_REACTION = atoi(argv[i]);
# Line 759  Line 778 
778                          if (ARG_PROGRESS > 0)                          if (ARG_PROGRESS > 0)
779                                  i++;                                  i++;
780                          else                          else
781                                  ARG_PROGRESS = 10;                                  ARG_PROGRESS = DEFAULT_PROGRESS;
782                  } else if (strcmp("-help", argv[i])) {                  } else if (strcmp("-help", argv[i]) == 0) {
783                        if (i < argc - 1 && strcmp("zones", argv[i+1]) == 0) {
784                                    fprintf(stderr, "Zones options\n\
785    NB: You can define up to %d zones using the -zones option as described below.\n\
786    \n\
787     -zones start,mode,value[,options][/start,mode,value[,options]]...\n\
788    \n\
789     Parameters of a zone use the comma (,) as a delimiter. Multiple zones are\n\
790     separated by a slash (/). The end of each zone is defined by either the start\n\
791     of the following zone or the last frame of the input file.\n\
792    \n\
793     start     : start frame of the zone\n\
794     mode      : weight zone = w, quantizer zone = q\n\
795     value     : depending on mode either the zone's weight or quantizer\n\
796     options   : enable certain encoder features for the zone. Each feature is\n\
797                 represented by a single letter. An integer number stands for\n\
798                 b-frame sensitivity. To enable multiple features at the same time\n\
799                 combine the appropriate symbols without any delimiting characters.\n\
800                 K = begin with keyframe\n\
801                 O = enable chroma optimizer\n\
802                 G = greyscale encoding\n\
803                 C = cartoon mode\n\
804           integer = b-frame sensitivity\n\
805    \n\
806     Example:\n\
807      to create a first zone starting at frame 0 with weight 1.0, all options\n\
808      enabled and b-frame sensitivity -5, and a second zone starting at frame 1000\n\
809      with constant quant 4 and no options enabled you would use the -zones option\n\
810      like this:\n\
811    \n\
812      -zones 0,w,1.0,-5KOGC/1000,q,4\n\n", MAX_ZONES);
813                            } else
814                          usage();                          usage();
815                          return (0);                          return (0);
816                  } else {                  } else {
# Line 774  Line 824 
824   *                            Arguments checking   *                            Arguments checking
825   ****************************************************************************/   ****************************************************************************/
826    
827          if (XDIM <= 0 || XDIM >= 4096 || YDIM <= 0 || YDIM >= 4096) {          if (XDIM <= 0 || XDIM >= MAX_XDIM || YDIM <= 0 || YDIM >= MAX_YDIM) {
828                  fprintf(stderr,                  fprintf(stderr,
829                                  "Trying to retrieve width and height from input header\n");                                  "Trying to retrieve width and height from input header\n");
830                  if (!ARG_INPUTTYPE)                  if (!ARG_INPUTTYPE)
# Line 783  Line 833 
833    
834          if (ARG_QUALITY < 0 ) {          if (ARG_QUALITY < 0 ) {
835                  ARG_QUALITY = 0;                  ARG_QUALITY = 0;
836          } else if (ARG_QUALITY >= ME_ELEMENTS) {          } else if (ARG_QUALITY >= (int) ME_ELEMENTS) {
837                  ARG_QUALITY = ME_ELEMENTS - 1;                  ARG_QUALITY = ME_ELEMENTS - 1;
838          }          }
839    
# Line 830  Line 880 
880                  return (-1);                  return (-1);
881          }          }
882    
883          if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if (ARG_INPUTFILE != NULL) {
884                  in_file = stdin;  #if defined(XVID_AVI_INPUT)
         } else {  
 #ifdef XVID_AVI_INPUT  
885        if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||        if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
886            strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||            strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
887                    ARG_INPUTTYPE==2)                    ARG_INPUTTYPE==2)
888        {        {
889                      PAVIFILE avi_in = NULL;
890                      PAVISTREAM avi_in_stream = NULL;
891                      PGETFRAME get_frame = NULL;
892                      BITMAPINFOHEADER myBitmapInfoHeader;
893                    AVISTREAMINFO avi_info;                    AVISTREAMINFO avi_info;
 #ifndef XVID_AVI_OUTPUT  
                   AVIFileInit();  
 #endif  
894                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
895    
896                      AVIFileInit();
897    
898                    if (avi_fp == NULL) {                    if (avi_fp == NULL) {
899                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
900                            return (-1);                            return (-1);
901                    }                    }
902                    fclose(avi_fp);                    fclose(avi_fp);
903    
904                    if (AVIFileOpen(&avi_file, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {                    if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
905                            fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
906                            AVIFileExit();                            AVIFileExit();
907                            return(-1);                            return(-1);
908                    }                    }
909    
910                    if (AVIFileGetStream(avi_file, &avi_stream, streamtypeVIDEO, 0) != AVIERR_OK) {                    if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
911                            fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
912                            AVIFileRelease(avi_file);                            AVIFileRelease(avi_in);
913                            AVIFileExit();                            AVIFileExit();
914                            return (-1);                            return (-1);
915                    }                    }
916    
917                    AVIFileRelease(avi_file);                    AVIFileRelease(avi_in);
918    
919                    if(AVIStreamInfo(avi_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {                    if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
920                            fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE);
921                            AVIStreamRelease(avi_stream);                            AVIStreamRelease(avi_in_stream);
922                            AVIFileExit();                            AVIFileExit();
923                            return (-1);                            return (-1);
924                    }                    }
# Line 874  Line 926 
926                if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) {                if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) {
927                            LONG size;                            LONG size;
928                            fprintf(stderr, "Non YV12 input colorspace %c%c%c%c! Attempting conversion...\n",                            fprintf(stderr, "Non YV12 input colorspace %c%c%c%c! Attempting conversion...\n",
929                                    avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256,                                    (char)avi_info.fccHandler%256, (char)(avi_info.fccHandler>>8)%256, (char)(avi_info.fccHandler>>16)%256,
930                                    (avi_info.fccHandler>>24)%256);                                    (char)(avi_info.fccHandler>>24)%256);
931                            size = sizeof(myBitmapInfoHeader);                            size = sizeof(myBitmapInfoHeader);
932                            AVIStreamReadFormat(avi_stream, 0, &myBitmapInfoHeader, &size);                            AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size);
933                            if (size==0)                            if (size==0)
934                                    fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");                                    fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
935                            else {                            else {
936                                    fprintf(stderr, "AVIStreamReadFormat read %d bytes.\n", size);                                    fprintf(stderr, "AVIStreamReadFormat read %d bytes.\n", (int)size);
937                                    fprintf(stderr, "width = %d, height = %d, planes = %d\n", myBitmapInfoHeader.biWidth,                                    fprintf(stderr, "width = %d, height = %d, planes = %d\n", (int)myBitmapInfoHeader.biWidth,
938                                            myBitmapInfoHeader.biHeight, myBitmapInfoHeader.biPlanes);                                            (int)myBitmapInfoHeader.biHeight, myBitmapInfoHeader.biPlanes);
939                                    fprintf(stderr, "Compression = %c%c%c%c, %d\n",                                    fprintf(stderr, "Compression = %c%c%c%c, %d\n",
940                                            myBitmapInfoHeader.biCompression%256, (myBitmapInfoHeader.biCompression>>8)%256,                                            (char)myBitmapInfoHeader.biCompression%256, (char)(myBitmapInfoHeader.biCompression>>8)%256,
941                                            (myBitmapInfoHeader.biCompression>>16)%256, (myBitmapInfoHeader.biCompression>>24)%256,                                            (char)(myBitmapInfoHeader.biCompression>>16)%256, (char)(myBitmapInfoHeader.biCompression>>24)%256,
942                                            myBitmapInfoHeader.biCompression);                                            (int)myBitmapInfoHeader.biCompression);
943                                    fprintf(stderr, "Bits Per Pixel = %d\n", myBitmapInfoHeader.biBitCount);                                    fprintf(stderr, "Bits Per Pixel = %d\n", myBitmapInfoHeader.biBitCount);
944                                    myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');                                    myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
945                                    myBitmapInfoHeader.biBitCount = 12;                                    myBitmapInfoHeader.biBitCount = 12;
946                                    myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;                                    myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
947                                    get_frame = AVIStreamGetFrameOpen(avi_stream, &myBitmapInfoHeader);                                    get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader);
948                            }                            }
949                            if (get_frame == NULL) {                            if (get_frame == NULL) {
950                                  AVIStreamRelease(avi_stream);                                  AVIStreamRelease(avi_in_stream);
951                                  AVIFileExit();                                  AVIFileExit();
952                                  return (-1);                                  return (-1);
953                            }                            }
# Line 905  Line 957 
957                                  temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);                                  temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);
958                                  if (temp != NULL) {                                  if (temp != NULL) {
959                                          int i;                                          int i;
960                                          for (i = 0; i < ((DWORD*)temp)[0]; i++) {                                          for (i = 0; i < (int)((DWORD*)temp)[0]; i++) {
961                                                  fprintf(stderr, "%2d ", temp[i]);                                                  fprintf(stderr, "%2d ", temp[i]);
962                                          }                                          }
963                                          fprintf(stderr, "\n");                                          fprintf(stderr, "\n");
# Line 921  Line 973 
973            if (ARG_MAXFRAMENR<0)            if (ARG_MAXFRAMENR<0)
974                          ARG_MAXFRAMENR = avi_info.dwLength-ARG_STARTFRAMENR;                          ARG_MAXFRAMENR = avi_info.dwLength-ARG_STARTFRAMENR;
975                    else                    else
976                          ARG_MAXFRAMENR = min(ARG_MAXFRAMENR, avi_info.dwLength-ARG_STARTFRAMENR);                          ARG_MAXFRAMENR = min(ARG_MAXFRAMENR, (int)(avi_info.dwLength-ARG_STARTFRAMENR));
977    
978                    XDIM = avi_info.rcFrame.right - avi_info.rcFrame.left;                    XDIM = avi_info.rcFrame.right - avi_info.rcFrame.left;
979                    YDIM = avi_info.rcFrame.bottom - avi_info.rcFrame.top;                    YDIM = avi_info.rcFrame.bottom - avi_info.rcFrame.top;
# Line 932  Line 984 
984                    }                    }
985    
986                    ARG_INPUTTYPE = 2;                    ARG_INPUTTYPE = 2;
987    
988                      if (get_frame) AVIStreamGetFrameClose(get_frame);
989                      if (avi_in_stream) AVIStreamRelease(avi_in_stream);
990                      AVIFileExit();
991      }      }
992      else      else
993  #endif  #endif
994                  {                  {
995                          in_file = fopen(ARG_INPUTFILE, "rb");                          FILE *in_file = fopen(ARG_INPUTFILE, "rb");
996                          if (in_file == NULL) {                          if (in_file == NULL) {
997                                  fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);                                  fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
998                                  return (-1);                                  return (-1);
999                          }                          }
1000                  }  #ifdef USE_APP_LEVEL_THREADING
1001          }                          fseek(in_file, 0, SEEK_END); /* Determine input size */
1002                            pos = ftell(in_file);
1003          // This should be after the avi input opening stuff                          ARG_MAXFRAMENR = pos / IMAGE_SIZE(XDIM, YDIM); /* PGM, header size ?? */
1004          if (ARG_TIMECODEFILE != NULL) {  #endif
1005                  time_file = fopen(ARG_TIMECODEFILE, "r");                          fclose(in_file);
                 if (time_file==NULL) {  
                         fprintf(stderr, "Couldn't open timecode file '%s'!\n", ARG_TIMECODEFILE);  
                         return(-1);  
                 }  
                 else {  
                         fscanf(time_file, "# timecode format v2\n");  
1006                  }                  }
1007          }          }
1008    
1009          if (ARG_FRAMERATE <= 0) {          if (ARG_FRAMERATE <= 0) {
1010                  fprintf(stderr, "Wrong Framerate %f\n", ARG_FRAMERATE);                  ARG_FRAMERATE = 25.00f; /* default value */
                 return (-1);  
1011          }          }
1012    
1013          if (ARG_TARGETSIZE) {          if (ARG_TARGETSIZE) {
# Line 969  Line 1018 
1018                                  fprintf(stderr, "Parameter conflict: Do not specify both -bitrate and -size\n");                                  fprintf(stderr, "Parameter conflict: Do not specify both -bitrate and -size\n");
1019                                  goto release_all;                                  goto release_all;
1020                  } else                  } else
1021                          ARG_BITRATE = ((ARG_TARGETSIZE * 8) / (ARG_MAXFRAMENR / ARG_FRAMERATE)) * 1024;                          ARG_BITRATE = (int)(((ARG_TARGETSIZE * 8) / (ARG_MAXFRAMENR / ARG_FRAMERATE)) * 1024);
1022          }          }
1023    
1024                  /* Set constant quant to default if no bitrate given for single pass */                  /* Set constant quant to default if no bitrate given for single pass */
1025          if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))          if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))
1026                          ARG_CQ = DEFAULT_QUANT;                          ARG_CQ = DEFAULT_QUANT;
1027    
1028                    /* Init xvidcore */
1029        enc_gbl(use_assembler);
1030    
1031    #ifdef USE_APP_LEVEL_THREADING
1032            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0 ||
1033                ARG_NUM_APP_THREADS <= 1 || ARG_THREADS != 0 ||
1034                ARG_TIMECODEFILE != NULL || ARG_AVIOUTPUTFILE != NULL ||
1035                ARG_INPUTTYPE == 1 || ARG_MKVOUTPUTFILE != NULL)            /* TODO: PGM input */
1036    #endif /* Spawn just one encoder instance */
1037            {
1038                    enc_sequence_data_t enc_data;
1039                    memset(&enc_data, 0, sizeof(enc_sequence_data_t));
1040    
1041                    if (!ARG_THREADS) ARG_THREADS = ARG_NUM_APP_THREADS;
1042                    ARG_NUM_APP_THREADS = 1;
1043    
1044                    enc_data.outfilename = ARG_OUTPUTFILE;
1045                    enc_data.statsfilename1 = ARG_PASS1;
1046                    enc_data.start_num = ARG_STARTFRAMENR;
1047                    enc_data.stop_num = ARG_MAXFRAMENR;
1048    
1049                            /* Encode input */
1050                    encode_sequence(&enc_data);
1051    
1052                            /* Copy back stats */
1053                    input_num = enc_data.input_num;
1054                    totalsize = enc_data.totalsize;
1055                    totalenctime = enc_data.totalenctime;
1056                    for (i=0; i < 3; i++) totalPSNR[i] = enc_data.totalPSNR[i];
1057                    memcpy(framestats, enc_data.framestats, sizeof(framestats));
1058            }
1059    #ifdef USE_APP_LEVEL_THREADING
1060            else { /* Split input into sequences and create multiple encoder instances */
1061                    int k;
1062                    void *status;
1063                    FILE *f_out = NULL, *f_stats = NULL;
1064    
1065                    enc_sequence_data_t enc_data[MAX_ENC_INSTANCES];
1066                    char outfile[MAX_ENC_INSTANCES][256];
1067                    char statsfilename[MAX_ENC_INSTANCES][256];
1068    
1069                    for (k = 0; k < MAX_ENC_INSTANCES; k++)
1070                            memset(&enc_data[k], 0, sizeof(enc_sequence_data_t));
1071    
1072                            /* Overwrite internal encoder threading */
1073                    if (ARG_NUM_APP_THREADS > MAX_ENC_INSTANCES) {
1074                            ARG_THREADS = (int) (ARG_NUM_APP_THREADS / MAX_ENC_INSTANCES);
1075                            ARG_NUM_APP_THREADS = MAX_ENC_INSTANCES;
1076                    }
1077                    else
1078                            ARG_THREADS = -1;
1079    
1080                    enc_data[0].outfilename = ARG_OUTPUTFILE;
1081                    enc_data[0].statsfilename1 = ARG_PASS1;
1082                    enc_data[0].start_num = ARG_STARTFRAMENR;
1083                    enc_data[0].stop_num = (ARG_MAXFRAMENR-ARG_STARTFRAMENR)/ARG_NUM_APP_THREADS;
1084    
1085                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1086                            sprintf(outfile[k], "%s.%03d", ARG_OUTPUTFILE, k);
1087                            enc_data[k].outfilename = outfile[k];
1088                            if (ARG_PASS1) {
1089                                    sprintf(statsfilename[k], "%s.%03d", ARG_PASS1, k);
1090                                    enc_data[k].statsfilename1 = statsfilename[k];
1091                            }
1092                            enc_data[k].start_num = (k*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS;
1093                            enc_data[k].stop_num = ((k+1)*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS;
1094                    }
1095    
1096                            /* Start multiple encoder threads in parallel */
1097                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1098                            pthread_create(&enc_data[k].handle, NULL, (void*)encode_sequence, (void*)&enc_data[k]);
1099                    }
1100    
1101                            /* Encode first sequence in this thread */
1102                    encode_sequence(&enc_data[0]);
1103    
1104                            /* Wait until encoder threads have finished */
1105                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1106                            pthread_join(enc_data[k].handle, &status);
1107                    }
1108    
1109                            /* Join encoder stats and encoder output files */
1110                    if (ARG_OUTPUTFILE)
1111                            f_out = fopen(enc_data[0].outfilename, "ab+");
1112                    if (ARG_PASS1)
1113                            f_stats = fopen(enc_data[0].statsfilename1, "ab+");
1114    
1115                    for (k = 0; k < ARG_NUM_APP_THREADS; k++) {
1116                                    /* Join stats */
1117                            input_num += enc_data[k].input_num;
1118                            totalsize += enc_data[k].totalsize;
1119                            totalenctime = MAX(totalenctime, enc_data[k].totalenctime);
1120    
1121                            for (i=0; i < 3; i++) totalPSNR[i] += enc_data[k].totalPSNR[i];
1122                            for (i=0; i < 7; i++) {
1123                                    int l;
1124                                    framestats[i].count += enc_data[k].framestats[i].count;
1125                                    framestats[i].size += enc_data[k].framestats[i].size;
1126                                    for (l=0; l < 32; l++)
1127                                            framestats[i].quants[l] += enc_data[k].framestats[i].quants[l];
1128                            }
1129                                    /* Join output files */
1130                            if ((k > 0) && (f_out != NULL)) {
1131                                    int ch;
1132                                    FILE *f = fopen(enc_data[k].outfilename, "rb");
1133                                    while((ch = fgetc(f)) != EOF) { fputc(ch, f_out); }
1134                                    fclose(f);
1135                                    remove(enc_data[k].outfilename);
1136                            }
1137                                    /* Join first pass stats files */
1138                            if ((k > 0) && (f_stats != NULL)) {
1139                                    char str[256];
1140                                    FILE *f = fopen(enc_data[k].statsfilename1, "r");
1141                                    while(fgets(str, sizeof(str), f) != NULL) {
1142                                            if (str[0] != '#' && strlen(str) > 3)
1143                                                    fputs(str, f_stats);
1144                                    }
1145                                    fclose(f);
1146                                    remove(enc_data[k].statsfilename1);
1147                            }
1148                    }
1149                    if (f_out) fclose(f_out);
1150                    if (f_stats) fclose(f_stats);
1151            }
1152    #endif
1153    
1154    /*****************************************************************************
1155     *         Calculate totals and averages for output, print results
1156     ****************************************************************************/
1157    
1158     printf("\n");
1159            printf("Tot: enctime(ms) =%7.2f,               length(bytes) = %7d\n",
1160                       totalenctime, (int) totalsize);
1161    
1162            if (input_num > 0) {
1163                    totalsize /= input_num;
1164                    totalenctime /= input_num;
1165                    totalPSNR[0] /= input_num;
1166                    totalPSNR[1] /= input_num;
1167                    totalPSNR[2] /= input_num;
1168            } else {
1169                    totalsize = -1;
1170                    totalenctime = -1;
1171            }
1172    
1173            printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d",
1174                       totalenctime, 1000 / totalenctime, (int) totalsize);
1175       if (ARG_STATS) {
1176           printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
1177                      totalPSNR[0],totalPSNR[1],totalPSNR[2]);
1178            }
1179            printf("\n");
1180            if (framestats[XVID_TYPE_IVOP].count) {
1181                    printf("I frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1182                            framestats[XVID_TYPE_IVOP].count, framestats[XVID_TYPE_IVOP].size/framestats[XVID_TYPE_IVOP].count, \
1183                            framestats[XVID_TYPE_IVOP].size, minquant(framestats[XVID_TYPE_IVOP].quants), \
1184                            avgquant(framestats[XVID_TYPE_IVOP]), maxquant(framestats[XVID_TYPE_IVOP].quants));
1185            }
1186            if (framestats[XVID_TYPE_PVOP].count) {
1187                    printf("P frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1188                            framestats[XVID_TYPE_PVOP].count, framestats[XVID_TYPE_PVOP].size/framestats[XVID_TYPE_PVOP].count, \
1189                            framestats[XVID_TYPE_PVOP].size, minquant(framestats[XVID_TYPE_PVOP].quants), \
1190                            avgquant(framestats[XVID_TYPE_PVOP]), maxquant(framestats[XVID_TYPE_PVOP].quants));
1191            }
1192            if (framestats[XVID_TYPE_BVOP].count) {
1193                    printf("B frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1194                            framestats[XVID_TYPE_BVOP].count, framestats[XVID_TYPE_BVOP].size/framestats[XVID_TYPE_BVOP].count, \
1195                            framestats[XVID_TYPE_BVOP].size, minquant(framestats[XVID_TYPE_BVOP].quants), \
1196                            avgquant(framestats[XVID_TYPE_BVOP]), maxquant(framestats[XVID_TYPE_BVOP].quants));
1197            }
1198            if (framestats[XVID_TYPE_SVOP].count) {
1199                    printf("S frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1200                            framestats[XVID_TYPE_SVOP].count, framestats[XVID_TYPE_SVOP].size/framestats[XVID_TYPE_SVOP].count, \
1201                            framestats[XVID_TYPE_SVOP].size, minquant(framestats[XVID_TYPE_SVOP].quants), \
1202                            avgquant(framestats[XVID_TYPE_SVOP]), maxquant(framestats[XVID_TYPE_SVOP].quants));
1203            }
1204            if (framestats[5].count) {
1205                    printf("N frames: %6d frames, size = %7d/%7d\n", \
1206                            framestats[5].count, framestats[5].size/framestats[5].count, \
1207                            framestats[5].size);
1208            }
1209    
1210    
1211    /*****************************************************************************
1212     *                            Xvid PART  Stop
1213     ****************************************************************************/
1214    
1215      release_all:
1216    
1217            return (0);
1218    }
1219    
1220    /*****************************************************************************
1221     *               Encode a sequence
1222     ****************************************************************************/
1223    
1224    void encode_sequence(enc_sequence_data_t *h) {
1225    
1226            /* Internal structures (handles) for encoding */
1227            void *enc_handle = NULL;
1228    
1229            int start_num = h->start_num;
1230            int stop_num = h->stop_num;
1231            char *outfilename = h->outfilename;
1232            float *totalPSNR = h->totalPSNR;
1233    
1234            int input_num;
1235            int totalsize;
1236            double totalenctime = 0.;
1237    
1238            unsigned char *mp4_buffer = NULL;
1239            unsigned char *in_buffer = NULL;
1240            unsigned char *out_buffer = NULL;
1241    
1242            double enctime;
1243    
1244            int result;
1245            int output_num;
1246            int m4v_size;
1247            int key;
1248            int stats_type;
1249            int stats_quant;
1250            int stats_length;
1251            int fakenvop = 0;
1252    
1253            FILE *in_file = stdin;
1254            FILE *out_file = NULL;
1255            FILE *time_file = NULL;
1256    
1257            char filename[256];
1258    
1259    #ifdef XVID_MKV_OUTPUT
1260            PMKVFILE myMKVFile = NULL;
1261            PMKVSTREAM myMKVStream = NULL;
1262            MKVSTREAMINFO myMKVStreamInfo;
1263    #endif
1264    #if defined(XVID_AVI_INPUT)
1265            PAVIFILE avi_in = NULL;
1266            PAVISTREAM avi_in_stream = NULL;
1267            PGETFRAME get_frame = NULL;
1268    #else
1269    #define get_frame NULL
1270    #endif
1271    #if defined(XVID_AVI_OUTPUT)
1272            int avierr;
1273            PAVIFILE myAVIFile = NULL;
1274            PAVISTREAM myAVIStream = NULL;
1275            AVISTREAMINFO myAVIStreamInfo;
1276    #endif
1277    #if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT)
1278            BITMAPINFOHEADER myBitmapInfoHeader;
1279            if (ARG_NUM_APP_THREADS > 1)
1280                    CoInitializeEx(0, COINIT_MULTITHREADED);
1281            AVIFileInit();
1282    #endif
1283    
1284            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
1285                    in_file = stdin;
1286            } else {
1287    #ifdef XVID_AVI_INPUT
1288          if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
1289              strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
1290                      ARG_INPUTTYPE==2)
1291          {
1292                      AVISTREAMINFO avi_info;
1293                      FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
1294    
1295                      if (avi_fp == NULL) {
1296                              fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
1297                              return;
1298                      }
1299                      fclose(avi_fp);
1300    
1301                      if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
1302                              fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
1303                              AVIFileExit();
1304                              return;
1305                      }
1306    
1307                      if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
1308                              fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
1309                              AVIFileRelease(avi_in);
1310                              AVIFileExit();
1311                              return;
1312                      }
1313    
1314                      AVIFileRelease(avi_in);
1315    
1316                      if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
1317                              fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE);
1318                              AVIStreamRelease(avi_in_stream);
1319                              AVIFileExit();
1320                              return;
1321                      }
1322    
1323                  if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) {
1324                              LONG size;
1325                              fprintf(stderr, "Non YV12 input colorspace %c%c%c%c! Attempting conversion...\n",
1326                                      (char)avi_info.fccHandler%256, (char)(avi_info.fccHandler>>8)%256, (char)(avi_info.fccHandler>>16)%256,
1327                                      (char)(avi_info.fccHandler>>24)%256);
1328                              size = sizeof(myBitmapInfoHeader);
1329                              AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size);
1330                              if (size==0)
1331                                      fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
1332                              else {
1333                                      fprintf(stderr, "AVIStreamReadFormat read %d bytes.\n", (int)size);
1334                                      fprintf(stderr, "width = %d, height = %d, planes = %d\n", (int)myBitmapInfoHeader.biWidth,
1335                                              (int)myBitmapInfoHeader.biHeight, myBitmapInfoHeader.biPlanes);
1336                                      fprintf(stderr, "Compression = %c%c%c%c, %d\n",
1337                                              (char)myBitmapInfoHeader.biCompression%256, (char)(myBitmapInfoHeader.biCompression>>8)%256,
1338                                              (char)(myBitmapInfoHeader.biCompression>>16)%256, (char)(myBitmapInfoHeader.biCompression>>24)%256,
1339                                              (int)myBitmapInfoHeader.biCompression);
1340                                      fprintf(stderr, "Bits Per Pixel = %d\n", myBitmapInfoHeader.biBitCount);
1341                                      myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
1342                                      myBitmapInfoHeader.biBitCount = 12;
1343                                      myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
1344                                      get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader);
1345                              }
1346                              if (get_frame == NULL) {
1347                                    AVIStreamRelease(avi_in_stream);
1348                                    AVIFileExit();
1349                                    return;
1350                              }
1351                              else {
1352                                    unsigned char *temp;
1353                                    fprintf(stderr, "AVIStreamGetFrameOpen successful.\n");
1354                                    temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);
1355                                    if (temp != NULL) {
1356                                            int i;
1357                                            for (i = 0; i < (int)((DWORD*)temp)[0]; i++) {
1358                                                    fprintf(stderr, "%2d ", temp[i]);
1359                                            }
1360                                            fprintf(stderr, "\n");
1361                                    }
1362                              }
1363                              if (avi_info.fccHandler == MAKEFOURCC('D', 'I', 'B', ' ')) {
1364                                      AVIStreamGetFrameClose(get_frame);
1365                                      get_frame = NULL;
1366                                      ARG_COLORSPACE = XVID_CSP_BGR | XVID_CSP_VFLIP;
1367                              }
1368                      }
1369        }
1370        else
1371    #endif
1372                    {
1373                            in_file = fopen(ARG_INPUTFILE, "rb");
1374                            if (in_file == NULL) {
1375                                    fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
1376                                    return;
1377                            }
1378                    }
1379            }
1380    
1381            // This should be after the avi input opening stuff
1382            if (ARG_TIMECODEFILE != NULL) {
1383                    time_file = fopen(ARG_TIMECODEFILE, "r");
1384                    if (time_file==NULL) {
1385                            fprintf(stderr, "Couldn't open timecode file '%s'!\n", ARG_TIMECODEFILE);
1386                            return;
1387                    }
1388                    else {
1389                            fscanf(time_file, "# timecode format v2\n");
1390                    }
1391            }
1392    
1393          if (ARG_INPUTTYPE==1) {          if (ARG_INPUTTYPE==1) {
1394  #ifndef READ_PNM  #ifndef READ_PNM
1395                  if (read_pgmheader(in_file)) {                  if (read_pgmheader(in_file)) {
# Line 984  Line 1398 
1398  #endif  #endif
1399                          fprintf(stderr,                          fprintf(stderr,
1400                                          "Wrong input format, I want YUV encapsulated in PGM\n");                                          "Wrong input format, I want YUV encapsulated in PGM\n");
1401                          return (-1);                          return;
1402                  }                  }
1403          }          }
1404    
1405          /* Jump to the starting frame */          /* Jump to the starting frame */
1406          if (ARG_INPUTTYPE == 0)          if (ARG_INPUTTYPE == 0) /* TODO: Other input formats ??? */
1407                  fseek(in_file, ARG_STARTFRAMENR*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);                  fseek(in_file, start_num*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);
1408    
1409    
1410          /* now we know the sizes, so allocate memory */          /* now we know the sizes, so allocate memory */
1411          if (get_frame == NULL)          if (get_frame == NULL)
# Line 1006  Line 1421 
1421                  goto free_all_memory;                  goto free_all_memory;
1422    
1423  /*****************************************************************************  /*****************************************************************************
1424   *                            XviD PART  Start   *                            Xvid PART  Start
1425   ****************************************************************************/   ****************************************************************************/
1426    
1427    
1428          result = enc_init(use_assembler);          result = enc_init(&enc_handle, h->statsfilename1, h->start_num);
1429          if (result) {          if (result) {
1430                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);
1431                  goto release_all;                  goto release_all;
# Line 1022  Line 1437 
1437    
1438          if (ARG_SAVEMPEGSTREAM) {          if (ARG_SAVEMPEGSTREAM) {
1439    
1440                  if (ARG_OUTPUTFILE) {                  if (outfilename) {
1441                          if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {                          if ((out_file = fopen(outfilename, "w+b")) == NULL) {
1442                                  fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);                                  fprintf(stderr, "Error opening output file %s\n", outfilename);
1443                                  goto release_all;                                  goto release_all;
1444                          }                          }
1445                  }                  }
# Line 1049  Line 1464 
1464                          myAVIStreamInfo.dwRate = ARG_DWRATE;                          myAVIStreamInfo.dwRate = ARG_DWRATE;
1465                          myAVIStreamInfo.dwLength = ARG_MAXFRAMENR;                          myAVIStreamInfo.dwLength = ARG_MAXFRAMENR;
1466                          myAVIStreamInfo.dwQuality = 10000;                          myAVIStreamInfo.dwQuality = 10000;
1467                          SetRect(&myAVIStreamInfo.rcFrame, 0, 0, YDIM, XDIM);                          SetRect(&myAVIStreamInfo.rcFrame, 0, 0, XDIM, YDIM);
1468    
1469                          if (avierr=AVIFileOpen(&myAVIFile, ARG_AVIOUTPUTFILE, OF_CREATE|OF_WRITE, NULL)) {                          if ((avierr=AVIFileOpen(&myAVIFile, ARG_AVIOUTPUTFILE, OF_CREATE|OF_WRITE, NULL))) {
1470                                  fprintf(stderr, "AVIFileOpen failed opening output file %s, error code %d\n", ARG_AVIOUTPUTFILE, avierr);                                  fprintf(stderr, "AVIFileOpen failed opening output file %s, error code %d\n", ARG_AVIOUTPUTFILE, avierr);
1471                                  goto release_all;                                  goto release_all;
1472                          }                          }
1473    
1474                          if (avierr=AVIFileCreateStream(myAVIFile, &myAVIStream, &myAVIStreamInfo)) {                          if ((avierr=AVIFileCreateStream(myAVIFile, &myAVIStream, &myAVIStreamInfo))) {
1475                                  fprintf(stderr, "AVIFileCreateStream failed, error code %d\n", avierr);                                  fprintf(stderr, "AVIFileCreateStream failed, error code %d\n", avierr);
1476                                  goto release_all;                                  goto release_all;
1477                          }                          }
# Line 1069  Line 1484 
1484                          myBitmapInfoHeader.biCompression = MAKEFOURCC('X', 'V', 'I', 'D');                          myBitmapInfoHeader.biCompression = MAKEFOURCC('X', 'V', 'I', 'D');
1485                          myBitmapInfoHeader.biBitCount = 12;                          myBitmapInfoHeader.biBitCount = 12;
1486                          myBitmapInfoHeader.biSizeImage = 6*XDIM*YDIM;                          myBitmapInfoHeader.biSizeImage = 6*XDIM*YDIM;
1487                          if (avierr=AVIStreamSetFormat(myAVIStream, 0, &myBitmapInfoHeader, sizeof(BITMAPINFOHEADER))) {                          if ((avierr=AVIStreamSetFormat(myAVIStream, 0, &myBitmapInfoHeader, sizeof(BITMAPINFOHEADER)))) {
1488                                  fprintf(stderr, "AVIStreamSetFormat failed, error code %d\n", avierr);                                  fprintf(stderr, "AVIStreamSetFormat failed, error code %d\n", avierr);
1489                                  goto release_all;                                  goto release_all;
1490                          }                          }
# Line 1120  Line 1535 
1535          result = 0;          result = 0;
1536    
1537          input_num = 0;                          /* input frame counter */          input_num = 0;                          /* input frame counter */
1538          output_num = 0;                         /* output frame counter */          output_num = start_num;             /* output frame counter */
   
         nvop_counter = 0;  
1539    
1540          do {          do {
1541    
1542                  char *type;                  char *type;
1543                  int sse[3];                  int sse[3];
1544    
1545                  if (input_num >= ARG_MAXFRAMENR && ARG_MAXFRAMENR > 0) {                  if ((input_num+start_num) >= stop_num && stop_num > 0) {
1546                          result = 1;                          result = 1;
1547                  }                  }
1548    
# Line 1138  Line 1551 
1551                          if (ARG_INPUTTYPE==2) {                          if (ARG_INPUTTYPE==2) {
1552                                  /* read avs/avi data (YUV-format) */                                  /* read avs/avi data (YUV-format) */
1553                                  if (get_frame != NULL) {                                  if (get_frame != NULL) {
1554                                          in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+ARG_STARTFRAMENR);                                          in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+start_num);
1555                                          if (in_buffer == NULL)                                          if (in_buffer == NULL)
1556                                                  result = 1;                                                  result = 1;
1557                                          else                                          else
1558                                                  in_buffer += ((DWORD*)in_buffer)[0];                                                  in_buffer += ((DWORD*)in_buffer)[0];
1559                                  } else {                                  } else {
1560                                          if(AVIStreamRead(avi_stream, input_num+ARG_STARTFRAMENR, 1, in_buffer, 4*XDIM*YDIM, NULL, NULL ) != AVIERR_OK)                                          if(AVIStreamRead(avi_in_stream, input_num+start_num, 1, in_buffer, 4*XDIM*YDIM, NULL, NULL ) != AVIERR_OK)
1561                                                  result = 1;                                                  result = 1;
1562                                  }                                  }
1563                          } else                          } else
# Line 1166  Line 1579 
1579   *                       Encode and decode this frame   *                       Encode and decode this frame
1580   ****************************************************************************/   ****************************************************************************/
1581    
1582                  if (input_num >= (unsigned int)ARG_MAXFRAMENR-1 && ARG_MAXBFRAMES) {                  if ((unsigned int)(input_num+start_num) >= (unsigned int)(stop_num-1) && ARG_MAXBFRAMES) {
1583                          stats_type = XVID_TYPE_PVOP;                          stats_type = XVID_TYPE_PVOP;
1584                  }                  }
1585                  else                  else
# Line 1174  Line 1587 
1587    
1588                  enctime = msecond();                  enctime = msecond();
1589                  m4v_size =                  m4v_size =
1590                          enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type,                          enc_main(enc_handle, !result ? in_buffer : 0, mp4_buffer, &key, &stats_type,
1591                                           &stats_quant, &stats_length, sse, input_num);                                           &stats_quant, &stats_length, sse, input_num);
1592                  enctime = msecond() - enctime;                  enctime = msecond() - enctime;
1593    
# Line 1202  Line 1615 
1615                          }                          }
1616    
1617                          if (stats_length > 8) {                          if (stats_length > 8) {
1618                                  framestats[stats_type].count++;                                  h->framestats[stats_type].count++;
1619                                  framestats[stats_type].quants[stats_quant]++;                                  h->framestats[stats_type].quants[stats_quant]++;
1620                                  framestats[stats_type].size += stats_length;                                  h->framestats[stats_type].size += stats_length;
1621                          }                          }
1622                          else {                          else {
1623                                  framestats[5].count++;                                  h->framestats[5].count++;
1624                                  framestats[5].quants[stats_quant]++;                                  h->framestats[5].quants[stats_quant]++;
1625                                  framestats[5].size += stats_length;                                  h->framestats[5].size += stats_length;
1626                          }                          }
1627    
1628  #define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height)))))  #define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height)))))
1629    
1630                          if (ARG_PROGRESS == 0) {                          if (ARG_PROGRESS == 0) {
1631                                  printf("%5d: key=%i, time= %6.0f, len= %7d", !result ? input_num : -1,                                  printf("%5d: key=%i, time= %6.0f, len= %7d", !result ? (input_num+start_num) : -1,
1632                                          key, (float) enctime, (int) m4v_size);                                          key, (float) enctime, (int) m4v_size);
1633                                  printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,                                  printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,
1634                                     stats_length);                                     stats_length);
# Line 1228  Line 1641 
1641                                  }                                  }
1642                                  printf("\n");                                  printf("\n");
1643                          } else {                          } else {
1644                                  if (input_num % ARG_PROGRESS == 1) {                                  if ((input_num) % ARG_PROGRESS == 1) {
1645                                          if (ARG_MAXFRAMENR > 0) {                                          if (stop_num > 0) {
1646                                                  fprintf(stderr, "\r%7d frames(%3d%%) encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \                                                  fprintf(stderr, "\r%7d frames(%3d%%) encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \
1647                                                          input_num, input_num*100/ARG_MAXFRAMENR, input_num*1000/totalenctime, \                                                          (ARG_NUM_APP_THREADS*input_num), (input_num)*100/(stop_num-start_num), (ARG_NUM_APP_THREADS*input_num)*1000/(totalenctime), \
1648                                                          (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);                                                          ((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num));
1649                                          } else {                                          } else {
1650                                                  fprintf(stderr, "\r%7d frames encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \                                                  fprintf(stderr, "\r%7d frames encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \
1651                                                          input_num, input_num*1000/totalenctime, \                                                          (ARG_NUM_APP_THREADS*input_num), (ARG_NUM_APP_THREADS*input_num)*1000/(totalenctime), \
1652                                                          (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);                                                          ((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num));
1653                                          }                                          }
1654                                  }                                  }
1655                          }                          }
# Line 1300  Line 1713 
1713                                          removedivxp((char*)mp4_buffer, m4v_size);                                          removedivxp((char*)mp4_buffer, m4v_size);
1714    
1715                                  /* Save ES stream */                                  /* Save ES stream */
1716                                  if (ARG_OUTPUTFILE && out_file && !(fakenvop && m4v_size <= 8)) {                                  if (outfilename && out_file && !(fakenvop && m4v_size <= 8)) {
1717                                                  fwrite(mp4_buffer, 1, m4v_size, out_file);                                                  fwrite(mp4_buffer, 1, m4v_size, out_file);
1718                                  }                                  }
1719  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
# Line 1314  Line 1727 
1727                                  fakenvop=0;                                  fakenvop=0;
1728                  }                  }
1729    
1730                  input_num++;                  if (!result)
1731                            (input_num)++;
1732    
1733                  /* Read the header if it's pgm stream */                  /* Read the header if it's pgm stream */
1734                  if (!result && (ARG_INPUTTYPE==1))                  if (!result && (ARG_INPUTTYPE==1))
# Line 1326  Line 1740 
1740          } while (1);          } while (1);
1741    
1742    
   
 /*****************************************************************************  
  *         Calculate totals and averages for output, print results  
  ****************************************************************************/  
   
  printf("\n");  
         printf("Tot: enctime(ms) =%7.2f,               length(bytes) = %7d\n",  
                    totalenctime, (int) totalsize);  
   
         if (input_num > 0) {  
                 totalsize /= input_num;  
                 totalenctime /= input_num;  
                 totalPSNR[0] /= input_num;  
                 totalPSNR[1] /= input_num;  
                 totalPSNR[2] /= input_num;  
         } else {  
                 totalsize = -1;  
                 totalenctime = -1;  
         }  
   
         printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d",  
                    totalenctime, 1000 / totalenctime, (int) totalsize);  
    if (ARG_STATS) {  
        printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",  
                   totalPSNR[0],totalPSNR[1],totalPSNR[2]);  
         }  
         printf("\n");  
         if (framestats[XVID_TYPE_IVOP].count) {  
                 printf("I frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \  
                         framestats[XVID_TYPE_IVOP].count, framestats[XVID_TYPE_IVOP].size/framestats[XVID_TYPE_IVOP].count, \  
                         framestats[XVID_TYPE_IVOP].size, minquant(framestats[XVID_TYPE_IVOP].quants), \  
                         avgquant(framestats[XVID_TYPE_IVOP]), maxquant(framestats[XVID_TYPE_IVOP].quants));  
         }  
         if (framestats[XVID_TYPE_PVOP].count) {  
                 printf("P frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \  
                         framestats[XVID_TYPE_PVOP].count, framestats[XVID_TYPE_PVOP].size/framestats[XVID_TYPE_PVOP].count, \  
                         framestats[XVID_TYPE_PVOP].size, minquant(framestats[XVID_TYPE_PVOP].quants), \  
                         avgquant(framestats[XVID_TYPE_PVOP]), maxquant(framestats[XVID_TYPE_PVOP].quants));  
         }  
         if (framestats[XVID_TYPE_BVOP].count) {  
                 printf("B frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \  
                         framestats[XVID_TYPE_BVOP].count, framestats[XVID_TYPE_BVOP].size/framestats[XVID_TYPE_BVOP].count, \  
                         framestats[XVID_TYPE_BVOP].size, minquant(framestats[XVID_TYPE_BVOP].quants), \  
                         avgquant(framestats[XVID_TYPE_BVOP]), maxquant(framestats[XVID_TYPE_BVOP].quants));  
         }  
         if (framestats[XVID_TYPE_SVOP].count) {  
                 printf("S frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \  
                         framestats[XVID_TYPE_SVOP].count, framestats[XVID_TYPE_SVOP].size/framestats[XVID_TYPE_SVOP].count, \  
                         framestats[XVID_TYPE_SVOP].size, minquant(framestats[XVID_TYPE_SVOP].quants), \  
                         avgquant(framestats[XVID_TYPE_SVOP]), maxquant(framestats[XVID_TYPE_SVOP].quants));  
         }  
         if (framestats[5].count) {  
                 printf("N frames: %6d frames, size = %7d/%7d\n", \  
                         framestats[5].count, framestats[5].size/framestats[5].count, \  
                         framestats[5].size);  
         }  
   
   
 /*****************************************************************************  
  *                            XviD PART  Stop  
  ****************************************************************************/  
   
1743    release_all:    release_all:
1744    
1745      h->input_num = input_num;
1746      h->totalenctime = totalenctime;
1747      h->totalsize = totalsize;
1748    
1749  #ifdef XVID_AVI_INPUT  #ifdef XVID_AVI_INPUT
1750          if (get_frame) AVIStreamGetFrameClose(get_frame);          if (get_frame) AVIStreamGetFrameClose(get_frame);
1751          if (avi_stream) AVIStreamRelease(avi_stream);          if (avi_in_stream) AVIStreamRelease(avi_in_stream);
 #ifndef XVID_AVI_OUTPUT  
         AVIFileExit();  
 #endif  
1752  #endif  #endif
1753    
1754          if (enc_handle) {          if (enc_handle) {
1755                  result = enc_stop();                  result = enc_stop(enc_handle);
1756                  if (result)                  if (result)
1757                          fprintf(stderr, "Encore RELEASE problem return value %d\n",                          fprintf(stderr, "Encore RELEASE problem return value %d\n",
1758                                          result);                                          result);
# Line 1409  Line 1762 
1762                  fclose(in_file);                  fclose(in_file);
1763          if (out_file)          if (out_file)
1764                  fclose(out_file);                  fclose(out_file);
1765            if (time_file)
1766                    fclose(time_file);
1767    
1768  #ifdef XVID_AVI_OUTPUT  #ifdef XVID_AVI_OUTPUT
1769          if (myAVIStream) AVIStreamRelease(myAVIStream);          if (myAVIStream) AVIStreamRelease(myAVIStream);
1770          if (myAVIFile) AVIFileRelease(myAVIFile);          if (myAVIFile) AVIFileRelease(myAVIFile);
         AVIFileExit();  
1771  #endif  #endif
1772  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
1773          if (myMKVStream) MKVStreamRelease(myMKVStream);          if (myMKVStream) MKVStreamRelease(myMKVStream);
1774          if (myMKVFile) MKVFileRelease(myMKVFile);          if (myMKVFile) MKVFileRelease(myMKVFile);
1775  #endif  #endif
1776    #if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT)
1777            AVIFileExit();
1778    #endif
1779    
1780    free_all_memory:    free_all_memory:
1781          free(out_buffer);          free(out_buffer);
1782          free(mp4_buffer);          free(mp4_buffer);
1783          free(in_buffer);          free(in_buffer);
   
         return (0);  
   
1784  }  }
1785    
   
1786  /*****************************************************************************  /*****************************************************************************
1787   *                        "statistical" functions   *                        "statistical" functions
1788   *   *
1789   *  these are not needed for encoding or decoding, but for measuring   *  these are not needed for encoding or decoding, but for measuring
1790   *  time and quality, there in nothing specific to XviD in these   *  time and quality, there in nothing specific to Xvid in these
1791   *   *
1792   *****************************************************************************/   *****************************************************************************/
1793    
# Line 1514  Line 1867 
1867          fprintf(stderr, "Input options:\n");          fprintf(stderr, "Input options:\n");
1868          fprintf(stderr, " -i      string : input filename (stdin)\n");          fprintf(stderr, " -i      string : input filename (stdin)\n");
1869  #ifdef XVID_AVI_INPUT  #ifdef XVID_AVI_INPUT
1870          fprintf(stderr, " -type   integer: input data type (yuv=0, pgm=1, avi/avs=2)\n");          fprintf(stderr, " -type   integer : input data type (yuv=0, pgm=1, avi/avs=2) (");
1871  #else  #else
1872          fprintf(stderr, " -type   integer: input data type (yuv=0, pgm=1)\n");          fprintf(stderr, " -type   integer : input data type (yuv=0, pgm=1) (");
1873  #endif  #endif
1874          fprintf(stderr, " -w      integer: frame width ([1.2048])\n");          fprintf(stderr, "%d)\n", ARG_INPUTTYPE);
1875          fprintf(stderr, " -h      integer: frame height ([1.2048])\n");          fprintf(stderr, " -w      integer : frame width ([1.%d])\n", MAX_XDIM);
1876          fprintf(stderr, " -csp    string : colorspace of raw input file i420, yv12 (default)\n");          fprintf(stderr, " -h      integer : frame height ([1.%d])\n", MAX_YDIM);
1877          fprintf(stderr, " -frames integer: number of frames to encode\n");          fprintf(stderr, " -csp    string  : colorspace of raw input file i420%s, yv12%s\n", (ARG_COLORSPACE == XVID_CSP_I420)?" (default)":"", (ARG_COLORSPACE == XVID_CSP_YV12)?" (default)":"");
1878          fprintf(stderr, "\n");          fprintf(stderr, " -frames integer : number of frames to encode (");
1879            if (ARG_MAXFRAMENR==-1)
1880                    fprintf(stderr, "all)");
1881            else
1882                    fprintf(stderr, "%d)", ARG_MAXFRAMENR);
1883            fprintf(stderr, "\n\n");
1884          fprintf(stderr, "Output options:\n");          fprintf(stderr, "Output options:\n");
1885          fprintf(stderr, " -dump      : save decoder output\n");          fprintf(stderr, " -dump      : save decoder output\n");
1886          fprintf(stderr, " -save      : save an Elementary Stream file per frame\n");          fprintf(stderr, " -save      : save an Elementary Stream file per frame\n");
# Line 1530  Line 1888 
1888  #ifdef XVID_AVI_OUTPUT  #ifdef XVID_AVI_OUTPUT
1889          fprintf(stderr, " -avi string: save an AVI file for the complete sequence\n");          fprintf(stderr, " -avi string: save an AVI file for the complete sequence\n");
1890  #endif  #endif
1891    #ifdef XVID_MKV_OUTPUT
1892          fprintf(stderr, " -mkv string: save a MKV file for the complete sequence\n");          fprintf(stderr, " -mkv string: save a MKV file for the complete sequence\n");
1893          fprintf(stderr, "\n");  #endif
         fprintf(stderr, "BFrames options:\n");  
         fprintf(stderr, " -max_bframes   integer: max bframes (2)\n");  
         fprintf(stderr, " -bquant_ratio  integer: bframe quantizer ratio (150)\n");  
         fprintf(stderr, " -bquant_offset integer: bframe quantizer offset (100)\n");  
1894          fprintf(stderr, "\n");          fprintf(stderr, "\n");
1895          fprintf(stderr, "Rate control options:\n");          fprintf(stderr, "Rate control options:\n");
1896          fprintf(stderr, " -framerate float               : target framerate (25.0)\n");          fprintf(stderr, " -framerate        float      : target framerate (auto)\n");
1897          fprintf(stderr, " -bitrate   [integer]           : target bitrate in kbps (700)\n");          fprintf(stderr, " -bitrate          [integer]  : target bitrate in kbps (%d)\n", DEFAULT_BITRATE/1000);
1898          fprintf(stderr, " -size      integer                     : target size in kilobytes\n");          fprintf(stderr, " -size      integer                     : target size in kilobytes\n");
1899      fprintf(stderr,     " -single                        : single pass mode (default)\n");          fprintf(stderr, " -single                      : single pass mode%s\n", (ARG_SINGLE)?" (default)":"");
1900          fprintf(stderr, " -cq        float               : single pass constant quantizer\n");          fprintf(stderr, " -cq        float               : single pass constant quantizer\n");
1901          fprintf(stderr, " -pass1     [filename]          : twopass mode (first pass)\n");          fprintf(stderr, " -pass1     [filename]          : twopass mode (first pass)\n");
1902          fprintf(stderr, " -full1pass                     : perform full first pass\n");          fprintf(stderr, " -full1pass                   : perform full quality first pass (disabled)\n");
1903          fprintf(stderr, " -pass2     [filename]          : twopass mode (2nd pass)\n");          fprintf(stderr, " -pass2     [filename]          : twopass mode (2nd pass)\n");
1904            fprintf(stderr, " -max_key_interval integer    : maximum keyframe interval (%d)\n", ARG_MAXKEYINTERVAL);
1905            fprintf(stderr, " -zones            [zones]    : use `-help zones' for usage guidelines\n");
1906          fprintf(stderr, " -zq starting_frame float       : bitrate zone; quant\n");          fprintf(stderr, " -zq starting_frame float       : bitrate zone; quant\n");
1907          fprintf(stderr, " -zw starting_frame float       : bitrate zone; weight\n");          fprintf(stderr, " -zw starting_frame float       : bitrate zone; weight\n");
     fprintf(stderr, " -max_key_interval integer      : maximum keyframe interval (300)\n");  
1908      fprintf(stderr, "\n");      fprintf(stderr, "\n");
1909          fprintf(stderr, "Single Pass options:\n");          fprintf(stderr, "Single Pass options:\n");
1910          fprintf(stderr, "-reaction   integer             : reaction delay factor (16)\n");          fprintf(stderr, " -reaction         integer    : reaction delay factor (%d)\n", ARG_REACTION);
1911          fprintf(stderr, "-averaging  integer             : averaging period (100)\n");          fprintf(stderr, " -averaging        integer    : averaging period (%d)\n", ARG_AVERAGING);
1912          fprintf(stderr, "-smoother   integer             : smoothing buffer (100)\n");          fprintf(stderr, " -smoother         integer    : smoothing buffer (%d)\n", ARG_SMOOTHER);
1913          fprintf(stderr, "\n");          fprintf(stderr, "\n");
1914          fprintf(stderr, "Second Pass options:\n");          fprintf(stderr, "Second Pass options:\n");
1915          fprintf(stderr, "-kboost     integer             : I frame boost (10)\n");          fprintf(stderr, " -kboost           integer    : I frame boost (%d)\n", ARG_KBOOST);
1916          fprintf(stderr, "-kthresh    integer             : I frame reduction threshold (1)\n");          fprintf(stderr, " -kthresh          integer    : I frame reduction threshold (%d)\n", ARG_KTHRESH);
1917          fprintf(stderr, "-kreduction integer             : I frame reduction amount (20)\n");          fprintf(stderr, " -kreduction       integer    : I frame reduction amount (%d)\n", ARG_KREDUCTION);
1918          fprintf(stderr, "-ostrength  integer             : overflow control strength (5)\n");          fprintf(stderr, " -ostrength        integer    : overflow control strength (%d)\n", ARG_OVERSTRENGTH);
1919          fprintf(stderr, "-oimprove   integer             : max overflow improvement (5)\n");          fprintf(stderr, " -oimprove         integer    : max overflow improvement (%d)\n", ARG_OVERIMPROVE);
1920          fprintf(stderr, "-odegrade   integer             : max overflow degradation (5)\n");          fprintf(stderr, " -odegrade         integer    : max overflow degradation (%d)\n", ARG_OVERDEGRADE);
1921          fprintf(stderr, "-chigh      integer             : high bitrate scenes degradation (0)\n");          fprintf(stderr, " -chigh            integer    : high bitrate scenes degradation (%d)\n", ARG_CHIGH);
1922          fprintf(stderr, "-clow       integer             : low bitrate scenes improvement (0)\n");          fprintf(stderr, " -clow             integer    : low bitrate scenes improvement (%d)\n", ARG_CLOW);
1923          fprintf(stderr, "-overhead   integer             : container frame overhead (24)\n");          fprintf(stderr, " -overhead         integer    : container frame overhead (%d)\n", ARG_OVERHEAD);
1924          fprintf(stderr, "-vbvsize    integer             : use vbv buffer size\n");          fprintf(stderr, "-vbvsize    integer             : use vbv buffer size\n");
1925          fprintf(stderr, "-vbvmax     integer             : vbv max bitrate\n");          fprintf(stderr, "-vbvmax     integer             : vbv max bitrate\n");
1926          fprintf(stderr, "-vbvpeak    integer             : vbv peak bitrate over 1 second\n");          fprintf(stderr, "-vbvpeak    integer             : vbv peak bitrate over 1 second\n");
1927          fprintf(stderr, "\n");          fprintf(stderr, "\n");
1928            fprintf(stderr, "BFrames options:\n");
1929            fprintf(stderr, " -max_bframes      integer    : max bframes (%d)\n", ARG_MAXBFRAMES);
1930            fprintf(stderr, " -bquant_ratio     integer    : bframe quantizer ratio (%d)\n", ARG_BQRATIO);
1931            fprintf(stderr, " -bquant_offset    integer    : bframe quantizer offset (%d)\n", ARG_BQOFFSET);
1932            fprintf(stderr, "\n");
1933          fprintf(stderr, "Other options\n");          fprintf(stderr, "Other options\n");
1934          fprintf(stderr, " -noasm                         : do not use assembly optmized code\n");          if (ARG_USE_ASSEMBLER)
1935          fprintf(stderr, " -turbo                         : use turbo presets for higher encoding speed\n");                  fprintf(stderr, " -noasm                       : do not use assembly optimized code (use)\n");
1936          fprintf(stderr, " -quality integer               : quality ([0..%d]) (6)\n", ME_ELEMENTS - 1);          else
1937          fprintf(stderr, " -vhqmode integer               : level of R-D optimizations ([0..4]) (1)\n");                  fprintf(stderr, " -asm                         : use assembly optimized code (don't use)\n");
1938          fprintf(stderr, " -bvhq                          : use R-D optimizations for B-frames\n");          if (ARG_TURBO)
1939          fprintf(stderr, " -qpel                          : use quarter pixel ME\n");                  fprintf(stderr, " -noturbo                     : do not use turbo presets for higher encoding speed (use)\n");
1940          fprintf(stderr, " -gmc                           : use global motion compensation\n");          else
1941          fprintf(stderr, " -qtype   integer               : quantization type (H263:0, MPEG4:1) (0)\n");                  fprintf(stderr, " -turbo                       : use turbo presets for higher encoding speed (don't use)\n");
1942            fprintf(stderr, " -quality          integer    : quality ([0..%d]) (%d)\n", ME_ELEMENTS - 1, ARG_QUALITY);
1943            fprintf(stderr, " -vhqmode          integer    : level of R-D optimizations ([0..4]) (%d)\n", ARG_VHQMODE);
1944            if (ARG_BVHQ)
1945                    fprintf(stderr, " -nobvhq                      : do not use R-D optimizations for B-frames (use)\n");
1946            else
1947                    fprintf(stderr, " -bvhq                        : use R-D optimizations for B-frames (don't use)\n");
1948            fprintf(stderr, " -metric           integer    : distortion metric for R-D opt (PSNR:0, PSNRHVSM:1) (%d)\n", ARG_QMETRIC);
1949            if (ARG_QPEL)
1950                    fprintf(stderr, " -noqpel                      : do not use quarter pixel ME (use)\n");
1951            else
1952                    fprintf(stderr, " -qpel                        : use quarter pixel ME (don't use)\n");
1953            if (ARG_GMC)
1954                    fprintf(stderr, " -nogmc                       : do not use global motion compensation (use)\n");
1955            else
1956                    fprintf(stderr, " -gmc                         : use global motion compensation (don't use)\n");
1957            fprintf(stderr, " -qtype            integer    : quantization type (H263:0, MPEG4:1) (%d)\n", ARG_QTYPE);
1958          fprintf(stderr, " -qmatrix filename              : use custom MPEG4 quantization matrix\n");          fprintf(stderr, " -qmatrix filename              : use custom MPEG4 quantization matrix\n");
1959          fprintf(stderr, " -interlaced [integer]          : interlaced encoding (BFF:1, TFF:2) (1)\n");          fprintf(stderr, " -interlaced       [integer]  : interlaced encoding (BFF:1, TFF:2) (%d)\n", DEFAULT_INTERLACING);
1960          fprintf(stderr, " -nopacked                      : Disable packed mode\n");          if (ARG_PACKED)
1961          fprintf(stderr, " -noclosed_gop                  : Disable closed GOP mode\n");                  fprintf(stderr, " -nopacked                    : disable packed B-frames mode (enabled)\n");
1962          fprintf(stderr, " -masking [integer]             : HVS masking mode (None:0, Lumi:1, Variance:2) (0)\n");          else
1963          fprintf(stderr, " -stats                         : print stats about encoded frames\n");                  fprintf(stderr, " -packed                      : enable packed B-frames mode (disabled)\n");
1964          fprintf(stderr, " -ssim [integer]                : prints ssim for every frame (accurate: 0 fast: 4) (2)\n");          if (ARG_CLOSED_GOP)
1965          fprintf(stderr, " -ssim_file filename            : outputs the ssim stats into a file\n");                  fprintf(stderr, " -noclosed_gop                : disable closed GOP mode (enabled)\n");
1966          fprintf(stderr, " -debug                         : activates xvidcore internal debugging output\n");          else
1967          fprintf(stderr, " -vop_debug                     : print some info directly into encoded frames\n");                  fprintf(stderr, " -closed_gop                  : enable closed GOP mode (disabled)\n");
1968          fprintf(stderr, " -nochromame                    : Disable chroma motion estimation\n");          fprintf(stderr, " -masking          [integer]  : HVS masking mode (None:0, Lumi:1, Variance:2) (%d)\n", ARG_LUMIMASKING);
1969          fprintf(stderr, " -notrellis                     : Disable trellis quantization\n");          if (ARG_STATS)
1970          fprintf(stderr, " -imin    integer               : Minimum I Quantizer (1..31) (2)\n");                  fprintf(stderr, " -nostats                     : do not print stats about encoded frames (print)\n");
1971          fprintf(stderr, " -imax    integer               : Maximum I quantizer (1..31) (31)\n");          else
1972          fprintf(stderr, " -bmin    integer               : Minimum B Quantizer (1..31) (2)\n");                  fprintf(stderr, " -stats                       : print stats about encoded frames (don't print)\n");
1973          fprintf(stderr, " -bmax    integer               : Maximum B quantizer (1..31) (31)\n");          fprintf(stderr, " -ssim             [integer]  : print ssim for every frame (accurate: 0 fast: 4) (%d)\n", DEFAULT_SSIM);
1974          fprintf(stderr, " -pmin    integer               : Minimum P Quantizer (1..31) (2)\n");          fprintf(stderr, " -ssim_file        filename   : output the ssim stats into a file\n");
1975          fprintf(stderr, " -pmax    integer               : Maximum P quantizer (1..31) (31)\n");          if (ARG_PSNRHVSM)
1976          fprintf(stderr, " -drop    integer               : Frame Drop Ratio (0..100) (0)\n");                  fprintf(stderr, " -nopsnrhvsm                  : do not print PSNRHVSM metric for every frame (print)\n");
1977          fprintf(stderr, " -start   integer               : Starting frame number\n");          else
1978          fprintf(stderr, " -threads integer               : Number of threads\n");                  fprintf(stderr, " -psnrhvsm                    : print PSNRHVSM metric for every frame (don't print)\n");
1979          fprintf(stderr, " -progress [integer]            : Show progress updates every n frames (10)\n");          fprintf(stderr, " -debug            integer    : activate xvidcore internal debugging output (don't activate)\n");
1980          fprintf(stderr, " -par     integer[:integer]     : Set Pixel Aspect Ratio.\n");          if (ARG_VOPDEBUG)
1981                    fprintf(stderr, " -novop_debug                 : do not print debug info directly into encoded frames (print)\n");
1982            else
1983                    fprintf(stderr, " -vop_debug                   : print some info directly into encoded frames (don't print)\n");
1984            if (ARG_CHROMAME)
1985                    fprintf(stderr, " -nochromame                  : disable chroma motion estimation (enabled)\n");
1986            else
1987                    fprintf(stderr, " -chromame                    : enable chroma motion estimation (disabled)\n");
1988            if (ARG_TRELLIS)
1989                    fprintf(stderr, " -notrellis                   : disable trellis quantization (enabled)\n");
1990            else
1991                    fprintf(stderr, " -trellis                     : enable trellis quantization (disabled)\n");
1992            fprintf(stderr, " -imin             integer    : minimum I Quantizer (1..31) (%d)\n", ARG_QUANTS[0]);
1993            fprintf(stderr, " -imax             integer    : maximum I quantizer (1..31) (%d)\n", ARG_QUANTS[1]);
1994            fprintf(stderr, " -bmin             integer    : minimum B Quantizer (1..31) (%d)\n", ARG_QUANTS[4]);
1995            fprintf(stderr, " -bmax             integer    : maximum B quantizer (1..31) (%d)\n", ARG_QUANTS[5]);
1996            fprintf(stderr, " -pmin             integer    : minimum P Quantizer (1..31) (%d)\n", ARG_QUANTS[2]);
1997            fprintf(stderr, " -pmax             integer    : maximum P quantizer (1..31) (%d)\n", ARG_QUANTS[3]);
1998            fprintf(stderr, " -drop             integer    : frame Drop Ratio (0..100) (%d)\n", ARG_FRAMEDROP);
1999            fprintf(stderr, " -start            integer    : starting frame number (%d)\n", ARG_STARTFRAMENR);
2000            fprintf(stderr, " -threads          integer    : number of threads (auto)\n");
2001            fprintf(stderr, " -slices           integer    : number of slices (%d)\n", ARG_SLICES);
2002            fprintf(stderr, " -progress         [integer]  : show progress updates every n frames (%d)\n", DEFAULT_PROGRESS);
2003            fprintf(stderr, " -par       integer[:integer] : set Pixel Aspect Ratio (%d)\n", ARG_PAR);
2004          fprintf(stderr, "                                  1 = 1:1\n");          fprintf(stderr, "                                  1 = 1:1\n");
2005          fprintf(stderr, "                                  2 = 12:11 (4:3 PAL)\n");          fprintf(stderr, "                                  2 = 12:11 (4:3 PAL)\n");
2006          fprintf(stderr, "                                  3 = 10:11 (4:3 NTSC)\n");          fprintf(stderr, "                                  3 = 10:11 (4:3 NTSC)\n");
2007          fprintf(stderr, "                                  4 = 16:11 (16:9 PAL)\n");          fprintf(stderr, "                                  4 = 16:11 (16:9 PAL)\n");
2008          fprintf(stderr, "                                  5 = 40:33 (16:9 NTSC)\n");          fprintf(stderr, "                                  5 = 40:33 (16:9 NTSC)\n");
2009          fprintf(stderr, "                              other = custom (width:height)\n");          fprintf(stderr, "                              other = custom (width:height)\n");
2010          fprintf(stderr, " -help                          : prints this help message\n");          fprintf(stderr, " -help                        : print this help message\n");
2011          fprintf(stderr, "\n");          fprintf(stderr, "\n");
2012          fprintf(stderr, "NB: You can define %d zones repeating the -z[qw] option as needed.\n", MAX_ZONES);          fprintf(stderr, "NB: You can define up to %d zones via both the -zones and the -z[qw] options as needed.\n", MAX_ZONES);
2013  }  }
2014    
2015  /*****************************************************************************  /*****************************************************************************
2016   *                       Input and output functions   *                       Input and output functions
2017   *   *
2018   *      the are small and simple routines to read and write PGM and YUV   *      the are small and simple routines to read and write PGM and YUV
2019   *      image. It's just for convenience, again nothing specific to XviD   *      image. It's just for convenience, again nothing specific to Xvid
2020   *   *
2021   *****************************************************************************/   *****************************************************************************/
2022    
# Line 1627  Line 2027 
2027          int bytes, xsize, ysize, depth;          int bytes, xsize, ysize, depth;
2028          char dummy[2];          char dummy[2];
2029    
2030          bytes = fread(dummy, 1, 2, handle);          bytes = (int) fread(dummy, 1, 2, handle);
2031    
2032          if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5'))          if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5'))
2033                  return (1);                  return (1);
2034    
2035          fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);          fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);
2036            fread(&dummy, 1, 1, handle);
2037          if ((xsize > 4096) || (ysize > 4096*3/2) || (depth != 255)) {          if ((xsize > 4096) || (ysize > 4096*3/2) || (depth != 255)) {
2038                  fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);                  fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);
2039                  return (2);                  return (2);
# Line 1650  Line 2051 
2051                           unsigned char *image)                           unsigned char *image)
2052  {  {
2053          int i;          int i;
         char dummy;  
2054    
2055          unsigned char *y = image;          unsigned char *y = image;
2056          unsigned char *u = image + XDIM * YDIM;          unsigned char *u = image + XDIM * YDIM;
# Line 1671  Line 2071 
2071                  v += XDIM / 2;                  v += XDIM / 2;
2072          }          }
2073    
         /*  I don't know why, but this seems needed */  
         fread(&dummy, 1, 1, handle);  
   
2074          return (0);          return (0);
2075  }  }
2076  #else  #else
# Line 1735  Line 2132 
2132    
2133  /* sample plugin */  /* sample plugin */
2134    
2135    #if 0
2136  int  int
2137  rawenc_debug(void *handle,  rawenc_debug(void *handle,
2138                           int opt,                           int opt,
# Line 1774  Line 2172 
2172    
2173          return XVID_ERR_FAIL;          return XVID_ERR_FAIL;
2174  }  }
2175    #endif
2176    
2177  #define FRAMERATE_INCR 1001  #define FRAMERATE_INCR 1001
2178    
2179    /* Gobal encoder init, once per process */
2180  /* Initialize encoder for first use, pass all needed parameters to the codec */  void
2181  static int  enc_gbl(int use_assembler)
 enc_init(int use_assembler)  
2182  {  {
         int xerr;  
         //xvid_plugin_cbr_t cbr;  
     xvid_plugin_single_t single;  
         xvid_plugin_2pass1_t rc2pass1;  
         xvid_plugin_2pass2_t rc2pass2;  
         xvid_plugin_ssim_t ssim;  
         xvid_plugin_lumimasking_t masking;  
         //xvid_plugin_fixed_t rcfixed;  
         xvid_enc_plugin_t plugins[8];  
2183          xvid_gbl_init_t xvid_gbl_init;          xvid_gbl_init_t xvid_gbl_init;
         xvid_enc_create_t xvid_enc_create;  
         int i;  
2184    
2185          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
2186           * XviD core initialization           * Xvid core initialization
2187           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
2188    
2189          /* Set version -- version checking will done by xvidcore */          /* Set version -- version checking will done by xvidcore */
# Line 1818  Line 2204 
2204                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
2205          }          }
2206    
2207          /* Initialize XviD core -- Should be done once per __process__ */          /* Initialize Xvid core -- Should be done once per __process__ */
2208          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
2209        ARG_CPU_FLAGS = xvid_gbl_init.cpu_flags;
2210          enc_info();          enc_info();
2211    }
2212    
2213    /* Initialize encoder for first use, pass all needed parameters to the codec */
2214    static int
2215    enc_init(void **enc_handle, char *stats_pass1, int start_num)
2216    {
2217            int xerr;
2218            //xvid_plugin_cbr_t cbr;
2219        xvid_plugin_single_t single;
2220            xvid_plugin_2pass1_t rc2pass1;
2221            xvid_plugin_2pass2_t rc2pass2;
2222            xvid_plugin_ssim_t ssim;
2223        xvid_plugin_lumimasking_t masking;
2224            //xvid_plugin_fixed_t rcfixed;
2225            xvid_enc_plugin_t plugins[8];
2226            xvid_enc_create_t xvid_enc_create;
2227            int i;
2228    
2229          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
2230           * XviD encoder initialization           * Xvid encoder initialization
2231           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
2232    
2233          /* Version again */          /* Version again */
# Line 1878  Line 2282 
2282                  rc2pass2.vbv_size     =  ARG_VBVSIZE;                  rc2pass2.vbv_size     =  ARG_VBVSIZE;
2283                  rc2pass2.vbv_initial  =  (ARG_VBVSIZE*3)/4;                  rc2pass2.vbv_initial  =  (ARG_VBVSIZE*3)/4;
2284                  rc2pass2.vbv_maxrate  =  ARG_VBVMAXRATE;                  rc2pass2.vbv_maxrate  =  ARG_VBVMAXRATE;
2285                  rc2pass2.vbv_peakrate =  ARG_VBVPEAKRATE*3;                  rc2pass2.vbv_peakrate =  ARG_VBVPEAKRATE;
2286    
2287    
2288                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
# Line 1886  Line 2290 
2290                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2291          }          }
2292    
2293          if (ARG_PASS1) {          if (stats_pass1) {
2294                  memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));                  memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));
2295                  rc2pass1.version = XVID_VERSION;                  rc2pass1.version = XVID_VERSION;
2296                  rc2pass1.filename = ARG_PASS1;                  rc2pass1.filename = stats_pass1;
2297                  if (ARG_FULL1PASS)                  if (ARG_FULL1PASS)
2298                          prepare_full1pass_zones();                          prepare_full1pass_zones();
2299                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;
# Line 1939  Line 2343 
2343                          ssim.stat_path = ARG_SSIM_PATH;                          ssim.stat_path = ARG_SSIM_PATH;
2344                  }                  }
2345    
2346          ssim.cpu_flags = xvid_gbl_init.cpu_flags;          ssim.cpu_flags = ARG_CPU_FLAGS;
2347                  ssim.b_visualize = 0;                  ssim.b_visualize = 0;
2348                  plugins[xvid_enc_create.num_plugins].param = &ssim;                  plugins[xvid_enc_create.num_plugins].param = &ssim;
2349                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2350          }          }
2351    
2352            if (ARG_PSNRHVSM>0) {
2353            plugins[xvid_enc_create.num_plugins].func = xvid_plugin_psnrhvsm;
2354                    plugins[xvid_enc_create.num_plugins].param = NULL;
2355                    xvid_enc_create.num_plugins++;
2356            }
2357    
2358  #if 0  #if 0
2359          if (ARG_DEBUG) {          if (ARG_DEBUG) {
2360                  plugins[xvid_enc_create.num_plugins].func = rawenc_debug;                  plugins[xvid_enc_create.num_plugins].func = rawenc_debug;
# Line 1954  Line 2364 
2364  #endif  #endif
2365    
2366          xvid_enc_create.num_threads = ARG_THREADS;          xvid_enc_create.num_threads = ARG_THREADS;
2367            xvid_enc_create.num_slices  = ARG_SLICES;
2368    
2369          /* Frame rate  */          /* Frame rate  */
2370          xvid_enc_create.fincr = ARG_DWSCALE;          xvid_enc_create.fincr = ARG_DWSCALE;
# Line 1981  Line 2392 
2392          /* Frame drop ratio */          /* Frame drop ratio */
2393          xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;          xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;
2394    
2395            /* Start frame number */
2396            xvid_enc_create.start_frame_num = start_num;
2397    
2398          /* Global encoder options */          /* Global encoder options */
2399          xvid_enc_create.global = 0;          xvid_enc_create.global = 0;
2400    
# Line 1997  Line 2411 
2411          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
2412    
2413          /* Retrieve the encoder instance from the structure */          /* Retrieve the encoder instance from the structure */
2414          enc_handle = xvid_enc_create.handle;          *enc_handle = xvid_enc_create.handle;
2415    
2416          free(xvid_enc_create.zones);          free(xvid_enc_create.zones);
2417    
# Line 2040  Line 2454 
2454                  fprintf(stderr, "TSC ");                  fprintf(stderr, "TSC ");
2455          fprintf(stderr, "\n");          fprintf(stderr, "\n");
2456          fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);          fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);
2457          if (!ARG_THREADS)          ARG_NUM_APP_THREADS = xvid_gbl_info.num_threads;
2458                  ARG_THREADS = xvid_gbl_info.num_threads;          fprintf(stderr, " using %d threads.\n", (!ARG_THREADS) ? ARG_NUM_APP_THREADS : ARG_THREADS);
         fprintf(stderr, " using %d threads.\n", ARG_THREADS);  
2459          return ret;          return ret;
2460  }  }
2461    
2462  static int  static int
2463  enc_stop()  enc_stop(void *enc_handle)
2464  {  {
2465          int xerr;          int xerr;
2466    
# Line 2058  Line 2471 
2471  }  }
2472    
2473  static int  static int
2474  enc_main(unsigned char *image,  enc_main(void *enc_handle,
2475                     unsigned char *image,
2476                   unsigned char *bitstream,                   unsigned char *bitstream,
2477                   int *key,                   int *key,
2478                   int *stats_type,                   int *stats_type,
# Line 2173  Line 2587 
2587          if (ARG_BVHQ)          if (ARG_BVHQ)
2588                  xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP;                  xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP;
2589    
2590            if (ARG_QMETRIC == 1)
2591                    xvid_enc_frame.vop_flags |= XVID_VOP_RD_PSNRHVSM;
2592    
2593          switch (ARG_VHQMODE) /* this is the same code as for vfw */          switch (ARG_VHQMODE) /* this is the same code as for vfw */
2594          {          {
2595          case 1: /* VHQ_MODE_DECISION */          case 1: /* VHQ_MODE_DECISION */
# Line 2268  Line 2685 
2685    
2686                  ZONES[NUM_ZONES].frame = 0;                  ZONES[NUM_ZONES].frame = 0;
2687                  ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;                  ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
2688                  ZONES[NUM_ZONES].modifier = ARG_CQ;                  ZONES[NUM_ZONES].modifier = (int)ARG_CQ;
2689                  ZONES[NUM_ZONES].type = XVID_TYPE_AUTO;                  ZONES[NUM_ZONES].type = XVID_TYPE_AUTO;
2690                  ZONES[NUM_ZONES].greyscale = 0;                  ZONES[NUM_ZONES].greyscale = 0;
2691                  ZONES[NUM_ZONES].chroma_opt = 0;                  ZONES[NUM_ZONES].chroma_opt = 0;
# Line 2284  Line 2701 
2701          for(i = 0; i < NUM_ZONES; i++)          for(i = 0; i < NUM_ZONES; i++)
2702                  if (ZONES[i].mode == XVID_ZONE_WEIGHT) {                  if (ZONES[i].mode == XVID_ZONE_WEIGHT) {
2703                          ZONES[i].mode = XVID_ZONE_QUANT;                          ZONES[i].mode = XVID_ZONE_QUANT;
2704                          ZONES[i].modifier = (100*ARG_CQ) / ZONES[i].modifier;                          ZONES[i].modifier = (int) ((100*ARG_CQ) / ZONES[i].modifier);
2705                  }                  }
2706  }  }
2707    
# Line 2353  Line 2770 
2770          int i;          int i;
2771          char* userdata;          char* userdata;
2772    
2773          for (i=0; i <= (bufsize-sizeof(userdata_start_code)); i++) {          for (i=0; i <= (int)(bufsize-sizeof(userdata_start_code)); i++) {
2774                  if (memcmp((void*)userdata_start_code, (void*)(buf+i), strlen(userdata_start_code))==0) {                  if (memcmp((void*)userdata_start_code, (void*)(buf+i), strlen(userdata_start_code))==0) {
2775                          if ((userdata = strstr(buf+i+4, "DivX"))!=NULL) {                          if ((userdata = strstr(buf+i+4, "DivX"))!=NULL) {
2776                                  userdata[strlen(userdata)-1] = '\0';                                  userdata[strlen(userdata)-1] = '\0';

Legend:
Removed from v.1863  
changed lines
  Added in v.2076

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