[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 1712, Sat Jul 8 14:19:04 2006 UTC revision 1932, Thu Dec 30 11:47:06 2010 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.26 2006-07-08 14:19:04 Skal Exp $   * $Id: xvid_encraw.c,v 1.48 2010-12-30 11:46:17 Isibaar Exp $
26   *   *
27   ****************************************************************************/   ****************************************************************************/
28    
# 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 DEFAULT_QUANT 400  #define DEFAULT_QUANT 400
130    
131  typedef struct  typedef struct
# Line 145  Line 149 
149          int quants[32];          int quants[32];
150  } frame_stats_t;  } frame_stats_t;
151    
152    typedef struct
153    {
154            pthread_t handle;       /* thread's handle */
155    
156            int start_num;          /* begin/end of sequence */
157            int stop_num;
158    
159            char *outfilename;      /* output filename */
160            char *statsfilename1;   /* pass1 statsfile */
161    
162            int input_num;
163    
164            int totalsize;          /* encoder stats */
165            double totalenctime;
166            float totalPSNR[3];
167            frame_stats_t framestats[7];
168    } enc_sequence_data_t;
169    
170  /* Maximum number of frames to encode */  /* Maximum number of frames to encode */
171  #define ABS_MAXFRAMENR -1 /* no limit */  #define ABS_MAXFRAMENR -1 /* no limit */
172    
# Line 162  Line 184 
184    
185  static zone_t ZONES[MAX_ZONES];  static zone_t ZONES[MAX_ZONES];
186  static  int NUM_ZONES = 0;  static  int NUM_ZONES = 0;
 static  frame_stats_t framestats[7];  
187    
188    static  int ARG_NUM_APP_THREADS = 1;
189    static  int ARG_CPU_FLAGS = 0;
190  static  int ARG_STATS = 0;  static  int ARG_STATS = 0;
191    static  int ARG_SSIM = -1;
192    static  int ARG_PSNRHVSM = 0;
193    static  char* ARG_SSIM_PATH = NULL;
194  static  int ARG_DUMP = 0;  static  int ARG_DUMP = 0;
195  static  int ARG_LUMIMASKING = 0;  static  int ARG_LUMIMASKING = 0;
196  static  int ARG_BITRATE = 0;  static  int ARG_BITRATE = 0;
# Line 174  Line 200 
200  static  char *ARG_PASS2 = 0;  static  char *ARG_PASS2 = 0;
201  //static int ARG_QUALITY = ME_ELEMENTS - 1;  //static int ARG_QUALITY = ME_ELEMENTS - 1;
202  static  int ARG_QUALITY = 6;  static  int ARG_QUALITY = 6;
203  static  float ARG_FRAMERATE = 0.00f;  static  float ARG_FRAMERATE = 25.00f;
204  static  int ARG_DWRATE = 25;  static  int ARG_DWRATE = 25;
205  static  int ARG_DWSCALE = 1;  static  int ARG_DWSCALE = 1;
206  static  int ARG_MAXFRAMENR = ABS_MAXFRAMENR;  static  int ARG_MAXFRAMENR = ABS_MAXFRAMENR;
# Line 185  Line 211 
211  static  int ARG_SAVEMPEGSTREAM = 0;  static  int ARG_SAVEMPEGSTREAM = 0;
212  static  int ARG_SAVEINDIVIDUAL = 0;  static  int ARG_SAVEINDIVIDUAL = 0;
213  static  char *ARG_OUTPUTFILE = NULL;  static  char *ARG_OUTPUTFILE = NULL;
 #ifdef XVID_AVI_OUTPUT  
214  static  char *ARG_AVIOUTPUTFILE = NULL;  static  char *ARG_AVIOUTPUTFILE = NULL;
 #endif  
 #ifdef XVID_MKV_OUTPUT  
215  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  
216  static  char *ARG_TIMECODEFILE = NULL;  static  char *ARG_TIMECODEFILE = NULL;
217  static  int XDIM = 0;  static  int XDIM = 0;
218  static  int YDIM = 0;  static  int YDIM = 0;
# Line 217  Line 231 
231  static  int ARG_TURBO = 0;  static  int ARG_TURBO = 0;
232  static  int ARG_VHQMODE = 1;  static  int ARG_VHQMODE = 1;
233  static  int ARG_BVHQ = 0;  static  int ARG_BVHQ = 0;
234    static  int ARG_QMETRIC = 0;
235  static  int ARG_CLOSED_GOP = 1;  static  int ARG_CLOSED_GOP = 1;
236  static  int ARG_CHROMAME = 1;  static  int ARG_CHROMAME = 1;
237  static  int ARG_PAR = 1;  static  int ARG_PAR = 1;
# Line 242  Line 257 
257  static  int ARG_VBVMAXRATE = 0;  static  int ARG_VBVMAXRATE = 0;
258  static  int ARG_VBVPEAKRATE = 0;  static  int ARG_VBVPEAKRATE = 0;
259  static  int ARG_THREADS = 0;  static  int ARG_THREADS = 0;
260    static  int ARG_SLICES = 1;
261  static  int ARG_VFR = 0;  static  int ARG_VFR = 0;
262  static  int ARG_PROGRESS = 0;  static  int ARG_PROGRESS = 0;
263  static  int ARG_COLORSPACE = XVID_CSP_YV12;  static  int ARG_COLORSPACE = XVID_CSP_YV12;
264          /* the path where to save output */          /* the path where to save output */
265  static char filepath[256] = "./";  static char filepath[256] = "./";
266          /* Internal structures (handles) for encoding and decoding */  
 static  void *enc_handle = NULL;  
267  static  unsigned char qmatrix_intra[64];  static  unsigned char qmatrix_intra[64];
268  static  unsigned char qmatrix_inter[64];  static  unsigned char qmatrix_inter[64];
269    
# Line 290  Line 305 
305                                                  unsigned char *image);                                                  unsigned char *image);
306    
307  /* Encoder related functions */  /* Encoder related functions */
308  static int enc_init(int use_assembler);  static void enc_gbl(int use_assembler);
309    static int  enc_init(void **enc_handle, char *stats_pass1, int start_num);
310  static int enc_info();  static int enc_info();
311  static int enc_stop();  static int  enc_stop(void *enc_handle);
312  static int enc_main(unsigned char *image,  static int  enc_main(void *enc_handle,
313                                             unsigned char *image,
314                                          unsigned char *bitstream,                                          unsigned char *bitstream,
315                                          int *key,                                          int *key,
316                                          int *stats_type,                                          int *stats_type,
# Line 301  Line 318 
318                                          int *stats_length,                                          int *stats_length,
319                                          int stats[3],                                          int stats[3],
320                                          int framenum);                                          int framenum);
321    static void encode_sequence(enc_sequence_data_t *h);
322    
323  /* Zone Related Functions */  /* Zone Related Functions */
324  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 319  Line 337 
337  main(int argc,  main(int argc,
338           char *argv[])           char *argv[])
339  {  {
   
         unsigned char *mp4_buffer = NULL;  
         unsigned char *in_buffer = NULL;  
         unsigned char *out_buffer = NULL;  
   
         double enctime;  
340          double totalenctime = 0.;          double totalenctime = 0.;
341          float totalPSNR[3] = {0., 0., 0.};          float totalPSNR[3] = {0., 0., 0.};
342    
343          FILE *statsfile;          FILE *statsfile;
344            frame_stats_t framestats[7];
345    
346          int totalsize;          int input_num = 0;
347          int result;          int totalsize = 0;
         int m4v_size;  
         int key;  
         int stats_type;  
         int stats_quant;  
         int stats_length;  
348          int use_assembler = 1;          int use_assembler = 1;
         int fakenvop = 0;  
349          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  
350    
351          printf("xvid_encraw - raw mpeg4 bitstream encoder ");          printf("xvid_encraw - raw mpeg4 bitstream encoder ");
352          printf("written by Christoph Lampert 2002-2003\n\n");          printf("written by Christoph Lampert\n\n");
353    
354          /* Is there a dumb XviD coder ? */          /* Is there a dumb Xvid coder ? */
355          if(ME_ELEMENTS != VOP_ELEMENTS) {          if(ME_ELEMENTS != VOP_ELEMENTS) {
356                  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 fill a bug to xvid-devel@xvid.org\n");
357                  return(-1);                  return(-1);
358          }          }
359    
360            /* Clear framestats */
361            memset(framestats, 0, sizeof(framestats));
362    
363  /*****************************************************************************  /*****************************************************************************
364   *                            Command line parsing   *                            Command line parsing
365   ****************************************************************************/   ****************************************************************************/
# Line 389  Line 376 
376                  } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) {
377                          i++;                          i++;
378                          YDIM = atoi(argv[i]);                          YDIM = atoi(argv[i]);
379                    } else if (strcmp("-csp",argv[i]) == 0 && i < argc - 1) {
380                            i++;
381                            if (strcmp(argv[i],"i420") == 0){
382                                    ARG_COLORSPACE = XVID_CSP_I420;
383                            } else if(strcmp(argv[i],"yv12") == 0){
384                                    ARG_COLORSPACE = XVID_CSP_YV12;
385                            } else {
386                                    printf("Invalid colorspace\n");
387                                    return 0;
388                            }
389          } else if (strcmp("-bitrate", argv[i]) == 0) {          } else if (strcmp("-bitrate", argv[i]) == 0) {
390                          if (i < argc - 1)                          if (i < argc - 1)
391                                  ARG_BITRATE = atoi(argv[i+1]);                                  ARG_BITRATE = atoi(argv[i+1]);
392                          if (ARG_BITRATE) {                          if (ARG_BITRATE) {
393                                  i++;                                  i++;
394                                  if (ARG_BITRATE <= 10000)                                  if (ARG_BITRATE <= 20000)
395                                          /* if given parameter is <= 10000, assume it means kbps */                                          /* if given parameter is <= 20000, assume it means kbps */
396                                          ARG_BITRATE *= 1000;                                          ARG_BITRATE *= 1000;
397                          }                          }
398                          else                          else
# Line 451  Line 448 
448                  } else if (strcmp("-threads", argv[i]) == 0 && i < argc -1) {                  } else if (strcmp("-threads", argv[i]) == 0 && i < argc -1) {
449                          i++;                          i++;
450                          ARG_THREADS = atoi(argv[i]);                          ARG_THREADS = atoi(argv[i]);
451                    } else if (strcmp("-slices", argv[i]) == 0 && i < argc -1) {
452                            i++;
453                            ARG_SLICES = atoi(argv[i]);
454                  } else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) {
455                          i++;                          i++;
456                          ARG_BQRATIO = atoi(argv[i]);                          ARG_BQRATIO = atoi(argv[i]);
# Line 485  Line 485 
485                                  memset(&ZONES[NUM_ZONES], 0, sizeof(zone_t));                                  memset(&ZONES[NUM_ZONES], 0, sizeof(zone_t));
486    
487                                  ZONES[NUM_ZONES].frame = startframe;                                  ZONES[NUM_ZONES].frame = startframe;
488                                  ZONES[NUM_ZONES].modifier = atof(options)*100;                                  ZONES[NUM_ZONES].modifier = (int)(atof(options)*100);
489                                  if (toupper(c)=='Q')                                  if (toupper(c)=='Q')
490                                          ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;                                          ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
491                                  else if (toupper(c)=='W')                                  else if (toupper(c)=='W')
# Line 498  Line 498 
498                                  if ((frameoptions=strchr(options, ','))!=NULL) {                                  if ((frameoptions=strchr(options, ','))!=NULL) {
499                                          int readchar=0, count;                                          int readchar=0, count;
500                                          frameoptions++;                                          frameoptions++;
501                                          while (readchar<strlen(frameoptions)) {                                          while (readchar<(int)strlen(frameoptions)) {
502                                                  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) {
503                                                          readchar += count;                                                          readchar += count;
504                                                  }                                                  }
# Line 536  Line 536 
536                          else {                          else {
537                                  ZONES[NUM_ZONES].mode = XVID_ZONE_WEIGHT;                                  ZONES[NUM_ZONES].mode = XVID_ZONE_WEIGHT;
538                          }                          }
539                          ZONES[NUM_ZONES].modifier = atof(argv[i+2])*100;                          ZONES[NUM_ZONES].modifier = (int)(atof(argv[i+2])*100);
540                          i++;                          i++;
541              ZONES[NUM_ZONES].frame = atoi(argv[i]);              ZONES[NUM_ZONES].frame = atoi(argv[i]);
542                          i++;                          i++;
# Line 556  Line 556 
556                  } else if (strcmp("-vhqmode", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-vhqmode", argv[i]) == 0 && i < argc - 1) {
557                          i++;                          i++;
558                          ARG_VHQMODE = atoi(argv[i]);                          ARG_VHQMODE = atoi(argv[i]);
559                    } else if (strcmp("-metric", argv[i]) == 0 && i < argc - 1) {
560                            i++;
561                            ARG_QMETRIC = atoi(argv[i]);
562                  } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) {
563                          int exponent;                          int exponent;
564                          i++;                          i++;
565                          ARG_FRAMERATE = (float) atof(argv[i]);                          ARG_FRAMERATE = (float) atof(argv[i]);
566                          exponent = strcspn(argv[i], ".");                          exponent = (int)strcspn(argv[i], ".");
567                          if (exponent<strlen(argv[i]))                          if (exponent<(int)strlen(argv[i]))
568                                  exponent=pow(10.0, (int)(strlen(argv[i])-1-exponent));                                  exponent=(int)pow(10.0, (int)(strlen(argv[i])-1-exponent));
569                          else                          else
570                                  exponent=1;                                  exponent=1;
571                          ARG_DWRATE = atof(argv[i])*exponent;                          ARG_DWRATE = (int)(atof(argv[i])*exponent);
572                          ARG_DWSCALE = exponent;                          ARG_DWSCALE = exponent;
573                          exponent = gcd(ARG_DWRATE, ARG_DWSCALE);                          exponent = gcd(ARG_DWRATE, ARG_DWSCALE);
574                          ARG_DWRATE /= exponent;                          ARG_DWRATE /= exponent;
# Line 578  Line 581 
581                          ARG_INPUTFILE = argv[i];                          ARG_INPUTFILE = argv[i];
582                  } else if (strcmp("-stats", argv[i]) == 0) {                  } else if (strcmp("-stats", argv[i]) == 0) {
583                          ARG_STATS = 1;                          ARG_STATS = 1;
584                    } else if (strcmp("-ssim", argv[i]) == 0) {
585                            ARG_SSIM = 2;
586                            if ((i < argc - 1) && (*argv[i+1] != '-')) {
587                                    i++;
588                                    ARG_SSIM = atoi(argv[i]);
589                            }
590                    } else if (strcmp("-psnrhvsm", argv[i]) == 0) {
591                            ARG_PSNRHVSM = 1;
592                    } else if (strcmp("-ssim_file", argv[i]) == 0 && i < argc -1) {
593                            i++;
594                            ARG_SSIM_PATH = argv[i];
595                  } else if (strcmp("-timecode", argv[i]) == 0 && i < argc -1) {                  } else if (strcmp("-timecode", argv[i]) == 0 && i < argc -1) {
596                          i++;                          i++;
597                          ARG_TIMECODEFILE = argv[i];                          ARG_TIMECODEFILE = argv[i];
598                  } else if (strcmp("-dump", argv[i]) == 0) {                  } else if (strcmp("-dump", argv[i]) == 0) {
599                          ARG_DUMP = 1;                          ARG_DUMP = 1;
600                  } else if (strcmp("-lumimasking", argv[i]) == 0) {                  } else if (strcmp("-masking", argv[i]) == 0 && i < argc -1) {
601                          ARG_LUMIMASKING = 1;                          i++;
602                            ARG_LUMIMASKING = atoi(argv[i]);
603                  } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {
604                          i++;                          i++;
605                          ARG_INPUTTYPE = atoi(argv[i]);                          ARG_INPUTTYPE = atoi(argv[i]);
# Line 693  Line 708 
708                          ARG_VBVMAXRATE = atoi(argv[i]);                          ARG_VBVMAXRATE = atoi(argv[i]);
709                  } else if (strcmp("-vbvpeak", argv[i]) == 0 && i < argc -1) {                  } else if (strcmp("-vbvpeak", argv[i]) == 0 && i < argc -1) {
710                          i++;                          i++;
711                          ARG_VBVPEAKRATE = atoi(argv[i])*3;                          ARG_VBVPEAKRATE = atoi(argv[i]);
712                  } else if (strcmp("-reaction", argv[i]) == 0 && i < argc -1) {                  } else if (strcmp("-reaction", argv[i]) == 0 && i < argc -1) {
713                          i++;                          i++;
714                          ARG_REACTION = atoi(argv[i]);                          ARG_REACTION = atoi(argv[i]);
# Line 808  Line 823 
823                  return (-1);                  return (-1);
824          }          }
825    
826          if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if (ARG_INPUTFILE != NULL) {
827                  in_file = stdin;  #if defined(XVID_AVI_INPUT)
         } else {  
 #ifdef XVID_AVI_INPUT  
828        if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||        if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
829            strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||            strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
830                    ARG_INPUTTYPE==2)                    ARG_INPUTTYPE==2)
831        {        {
832                      PAVIFILE avi_in = NULL;
833                      PAVISTREAM avi_in_stream = NULL;
834                      PGETFRAME get_frame = NULL;
835                      BITMAPINFOHEADER myBitmapInfoHeader;
836                    AVISTREAMINFO avi_info;                    AVISTREAMINFO avi_info;
 #ifndef XVID_AVI_OUTPUT  
                   AVIFileInit();  
 #endif  
837                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
838    
839                      AVIFileInit();
840    
841                    if (avi_fp == NULL) {                    if (avi_fp == NULL) {
842                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
843                            return (-1);                            return (-1);
844                    }                    }
845                    fclose(avi_fp);                    fclose(avi_fp);
846    
847                    if (AVIFileOpen(&avi_file, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {                    if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
848                            fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
849                            AVIFileExit();                            AVIFileExit();
850                            return(-1);                            return(-1);
851                    }                    }
852    
853                    if (AVIFileGetStream(avi_file, &avi_stream, streamtypeVIDEO, 0) != AVIERR_OK) {                    if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
854                            fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
855                            AVIFileRelease(avi_file);                            AVIFileRelease(avi_in);
856                            AVIFileExit();                            AVIFileExit();
857                            return (-1);                            return (-1);
858                    }                    }
859    
860                    AVIFileRelease(avi_file);                    AVIFileRelease(avi_in);
861    
862                    if(AVIStreamInfo(avi_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {                    if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
863                            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);
864                            AVIStreamRelease(avi_stream);                            AVIStreamRelease(avi_in_stream);
865                            AVIFileExit();                            AVIFileExit();
866                            return (-1);                            return (-1);
867                    }                    }
# Line 855  Line 872 
872                                    avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256,                                    avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256,
873                                    (avi_info.fccHandler>>24)%256);                                    (avi_info.fccHandler>>24)%256);
874                            size = sizeof(myBitmapInfoHeader);                            size = sizeof(myBitmapInfoHeader);
875                            AVIStreamReadFormat(avi_stream, 0, &myBitmapInfoHeader, &size);                            AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size);
876                            if (size==0)                            if (size==0)
877                                    fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");                                    fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
878                            else {                            else {
# Line 870  Line 887 
887                                    myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');                                    myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
888                                    myBitmapInfoHeader.biBitCount = 12;                                    myBitmapInfoHeader.biBitCount = 12;
889                                    myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;                                    myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
890                                    get_frame = AVIStreamGetFrameOpen(avi_stream, &myBitmapInfoHeader);                                    get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader);
891                            }                            }
892                            if (get_frame == NULL) {                            if (get_frame == NULL) {
893                                  AVIStreamRelease(avi_stream);                                  AVIStreamRelease(avi_in_stream);
894                                  AVIFileExit();                                  AVIFileExit();
895                                  return (-1);                                  return (-1);
896                            }                            }
# Line 883  Line 900 
900                                  temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);                                  temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);
901                                  if (temp != NULL) {                                  if (temp != NULL) {
902                                          int i;                                          int i;
903                                          for (i = 0; i < ((DWORD*)temp)[0]; i++) {                                          for (i = 0; i < (int)((DWORD*)temp)[0]; i++) {
904                                                  fprintf(stderr, "%2d ", temp[i]);                                                  fprintf(stderr, "%2d ", temp[i]);
905                                          }                                          }
906                                          fprintf(stderr, "\n");                                          fprintf(stderr, "\n");
# Line 899  Line 916 
916            if (ARG_MAXFRAMENR<0)            if (ARG_MAXFRAMENR<0)
917                          ARG_MAXFRAMENR = avi_info.dwLength-ARG_STARTFRAMENR;                          ARG_MAXFRAMENR = avi_info.dwLength-ARG_STARTFRAMENR;
918                    else                    else
919                          ARG_MAXFRAMENR = min(ARG_MAXFRAMENR, avi_info.dwLength-ARG_STARTFRAMENR);                          ARG_MAXFRAMENR = min(ARG_MAXFRAMENR, (int)(avi_info.dwLength-ARG_STARTFRAMENR));
920    
921                    XDIM = avi_info.rcFrame.right - avi_info.rcFrame.left;                    XDIM = avi_info.rcFrame.right - avi_info.rcFrame.left;
922                    YDIM = avi_info.rcFrame.bottom - avi_info.rcFrame.top;                    YDIM = avi_info.rcFrame.bottom - avi_info.rcFrame.top;
# Line 910  Line 927 
927                    }                    }
928    
929                    ARG_INPUTTYPE = 2;                    ARG_INPUTTYPE = 2;
930    
931                      if (get_frame) AVIStreamGetFrameClose(get_frame);
932                      if (avi_in_stream) AVIStreamRelease(avi_in_stream);
933                      AVIFileExit();
934      }      }
935      else      else
936  #endif  #endif
937                  {                  {
938                          in_file = fopen(ARG_INPUTFILE, "rb");                          FILE *in_file = fopen(ARG_INPUTFILE, "rb");
939                            int pos = 0;
940                          if (in_file == NULL) {                          if (in_file == NULL) {
941                                  fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);                                  fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
942                                  return (-1);                                  return (-1);
943                          }                          }
944                  }  #ifdef USE_APP_LEVEL_THREADING
945          }                          fseek(in_file, 0, SEEK_END); /* Determine input size */
946                            pos = ftell(in_file);
947          // This should be after the avi input opening stuff                          ARG_MAXFRAMENR = pos / IMAGE_SIZE(XDIM, YDIM); /* PGM, header size ?? */
948          if (ARG_TIMECODEFILE != NULL) {  #endif
949                  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");  
950                  }                  }
951          }          }
952    
# Line 947  Line 963 
963                                  fprintf(stderr, "Parameter conflict: Do not specify both -bitrate and -size\n");                                  fprintf(stderr, "Parameter conflict: Do not specify both -bitrate and -size\n");
964                                  goto release_all;                                  goto release_all;
965                  } else                  } else
966                          ARG_BITRATE = ((ARG_TARGETSIZE * 8) / (ARG_MAXFRAMENR / ARG_FRAMERATE)) * 1024;                          ARG_BITRATE = (int)(((ARG_TARGETSIZE * 8) / (ARG_MAXFRAMENR / ARG_FRAMERATE)) * 1024);
967          }          }
968    
969                  /* Set constant quant to default if no bitrate given for single pass */                  /* Set constant quant to default if no bitrate given for single pass */
970          if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))          if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))
971                          ARG_CQ = DEFAULT_QUANT;                          ARG_CQ = DEFAULT_QUANT;
972    
973                    /* Init xvidcore */
974        enc_gbl(use_assembler);
975    
976    #ifdef USE_APP_LEVEL_THREADING
977            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0 ||
978                ARG_NUM_APP_THREADS <= 1 || ARG_THREADS != 0 ||
979                ARG_TIMECODEFILE != NULL || ARG_AVIOUTPUTFILE != NULL ||
980                ARG_INPUTTYPE == 1 || ARG_MKVOUTPUTFILE != NULL)            /* TODO: PGM input */
981    #endif /* Spawn just one encoder instance */
982            {
983                    enc_sequence_data_t enc_data;
984                    memset(&enc_data, 0, sizeof(enc_sequence_data_t));
985    
986                    if (!ARG_THREADS) ARG_THREADS = ARG_NUM_APP_THREADS;
987                    ARG_NUM_APP_THREADS = 1;
988    
989                    enc_data.outfilename = ARG_OUTPUTFILE;
990                    enc_data.statsfilename1 = ARG_PASS1;
991                    enc_data.start_num = ARG_STARTFRAMENR;
992                    enc_data.stop_num = ARG_MAXFRAMENR;
993    
994                            /* Encode input */
995                    encode_sequence(&enc_data);
996    
997                            /* Copy back stats */
998                    input_num = enc_data.input_num;
999                    totalsize = enc_data.totalsize;
1000                    totalenctime = enc_data.totalenctime;
1001                    for (i=0; i < 3; i++) totalPSNR[i] = enc_data.totalPSNR[i];
1002                    memcpy(framestats, enc_data.framestats, sizeof(framestats));
1003            }
1004    #ifdef USE_APP_LEVEL_THREADING
1005            else { /* Split input into sequences and create multiple encoder instances */
1006                    int k;
1007                    void *status;
1008                    FILE *f_out = NULL, *f_stats = NULL;
1009    
1010                    enc_sequence_data_t enc_data[MAX_ENC_INSTANCES];
1011                    char outfile[MAX_ENC_INSTANCES][256];
1012                    char statsfilename[MAX_ENC_INSTANCES][256];
1013    
1014                    for (k = 0; k < MAX_ENC_INSTANCES; k++)
1015                            memset(&enc_data[k], 0, sizeof(enc_sequence_data_t));
1016    
1017                            /* Overwrite internal encoder threading */
1018                    if (ARG_NUM_APP_THREADS > MAX_ENC_INSTANCES) {
1019                            ARG_THREADS = (int) (ARG_NUM_APP_THREADS / MAX_ENC_INSTANCES);
1020                            ARG_NUM_APP_THREADS = MAX_ENC_INSTANCES;
1021                    }
1022                    else
1023                            ARG_THREADS = -1;
1024    
1025                    enc_data[0].outfilename = ARG_OUTPUTFILE;
1026                    enc_data[0].statsfilename1 = ARG_PASS1;
1027                    enc_data[0].start_num = ARG_STARTFRAMENR;
1028                    enc_data[0].stop_num = (ARG_MAXFRAMENR-ARG_STARTFRAMENR)/ARG_NUM_APP_THREADS;
1029    
1030                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1031                            sprintf(outfile[k], "%s.%03d", ARG_OUTPUTFILE, k);
1032                            enc_data[k].outfilename = outfile[k];
1033                            if (ARG_PASS1) {
1034                                    sprintf(statsfilename[k], "%s.%03d", ARG_PASS1, k);
1035                                    enc_data[k].statsfilename1 = statsfilename[k];
1036                            }
1037                            enc_data[k].start_num = (k*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS;
1038                            enc_data[k].stop_num = ((k+1)*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS;
1039                    }
1040    
1041                            /* Start multiple encoder threads in parallel */
1042                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1043                            pthread_create(&enc_data[k].handle, NULL, (void*)encode_sequence, (void*)&enc_data[k]);
1044                    }
1045    
1046                            /* Encode first sequence in this thread */
1047                    encode_sequence(&enc_data[0]);
1048    
1049                            /* Wait until encoder threads have finished */
1050                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1051                            pthread_join(enc_data[k].handle, &status);
1052                    }
1053    
1054                            /* Join encoder stats and encoder output files */
1055                    if (ARG_OUTPUTFILE)
1056                            f_out = fopen(enc_data[0].outfilename, "ab+");
1057                    if (ARG_PASS1)
1058                            f_stats = fopen(enc_data[0].statsfilename1, "ab+");
1059    
1060                    for (k = 0; k < ARG_NUM_APP_THREADS; k++) {
1061                                    /* Join stats */
1062                            input_num += enc_data[k].input_num;
1063                            totalsize += enc_data[k].totalsize;
1064                            totalenctime = MAX(totalenctime, enc_data[k].totalenctime);
1065    
1066                            for (i=0; i < 3; i++) totalPSNR[i] += enc_data[k].totalPSNR[i];
1067                            for (i=0; i < 8; i++) {
1068                                    int l;
1069                                    framestats[i].count += enc_data[k].framestats[i].count;
1070                                    framestats[i].size += enc_data[k].framestats[i].size;
1071                                    for (l=0; l < 32; l++)
1072                                            framestats[i].quants[l] += enc_data[k].framestats[i].quants[l];
1073                            }
1074                                    /* Join output files */
1075                            if ((k > 0) && (f_out != NULL)) {
1076                                    int ch;
1077                                    FILE *f = fopen(enc_data[k].outfilename, "rb");
1078                                    while((ch = fgetc(f)) != EOF) { fputc(ch, f_out); }
1079                                    fclose(f);
1080                                    remove(enc_data[k].outfilename);
1081                            }
1082                                    /* Join first pass stats files */
1083                            if ((k > 0) && (f_stats != NULL)) {
1084                                    char str[256];
1085                                    FILE *f = fopen(enc_data[k].statsfilename1, "r");
1086                                    while(fgets(str, sizeof(str), f) != NULL) {
1087                                            if (str[0] != '#' && strlen(str) > 3)
1088                                                    fputs(str, f_stats);
1089                                    }
1090                                    fclose(f);
1091                                    remove(enc_data[k].statsfilename1);
1092                            }
1093                    }
1094                    if (f_out) fclose(f_out);
1095                    if (f_stats) fclose(f_stats);
1096            }
1097    #endif
1098    
1099    /*****************************************************************************
1100     *         Calculate totals and averages for output, print results
1101     ****************************************************************************/
1102    
1103     printf("\n");
1104            printf("Tot: enctime(ms) =%7.2f,               length(bytes) = %7d\n",
1105                       totalenctime, (int) totalsize);
1106    
1107            if (input_num > 0) {
1108                    totalsize /= input_num;
1109                    totalenctime /= input_num;
1110                    totalPSNR[0] /= input_num;
1111                    totalPSNR[1] /= input_num;
1112                    totalPSNR[2] /= input_num;
1113            } else {
1114                    totalsize = -1;
1115                    totalenctime = -1;
1116            }
1117    
1118            printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d",
1119                       totalenctime, 1000 / totalenctime, (int) totalsize);
1120       if (ARG_STATS) {
1121           printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
1122                      totalPSNR[0],totalPSNR[1],totalPSNR[2]);
1123            }
1124            printf("\n");
1125            if (framestats[XVID_TYPE_IVOP].count) {
1126                    printf("I frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1127                            framestats[XVID_TYPE_IVOP].count, framestats[XVID_TYPE_IVOP].size/framestats[XVID_TYPE_IVOP].count, \
1128                            framestats[XVID_TYPE_IVOP].size, minquant(framestats[XVID_TYPE_IVOP].quants), \
1129                            avgquant(framestats[XVID_TYPE_IVOP]), maxquant(framestats[XVID_TYPE_IVOP].quants));
1130            }
1131            if (framestats[XVID_TYPE_PVOP].count) {
1132                    printf("P frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1133                            framestats[XVID_TYPE_PVOP].count, framestats[XVID_TYPE_PVOP].size/framestats[XVID_TYPE_PVOP].count, \
1134                            framestats[XVID_TYPE_PVOP].size, minquant(framestats[XVID_TYPE_PVOP].quants), \
1135                            avgquant(framestats[XVID_TYPE_PVOP]), maxquant(framestats[XVID_TYPE_PVOP].quants));
1136            }
1137            if (framestats[XVID_TYPE_BVOP].count) {
1138                    printf("B frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1139                            framestats[XVID_TYPE_BVOP].count, framestats[XVID_TYPE_BVOP].size/framestats[XVID_TYPE_BVOP].count, \
1140                            framestats[XVID_TYPE_BVOP].size, minquant(framestats[XVID_TYPE_BVOP].quants), \
1141                            avgquant(framestats[XVID_TYPE_BVOP]), maxquant(framestats[XVID_TYPE_BVOP].quants));
1142            }
1143            if (framestats[XVID_TYPE_SVOP].count) {
1144                    printf("S frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1145                            framestats[XVID_TYPE_SVOP].count, framestats[XVID_TYPE_SVOP].size/framestats[XVID_TYPE_SVOP].count, \
1146                            framestats[XVID_TYPE_SVOP].size, minquant(framestats[XVID_TYPE_SVOP].quants), \
1147                            avgquant(framestats[XVID_TYPE_SVOP]), maxquant(framestats[XVID_TYPE_SVOP].quants));
1148            }
1149            if (framestats[5].count) {
1150                    printf("N frames: %6d frames, size = %7d/%7d\n", \
1151                            framestats[5].count, framestats[5].size/framestats[5].count, \
1152                            framestats[5].size);
1153            }
1154    
1155    
1156    /*****************************************************************************
1157     *                            Xvid PART  Stop
1158     ****************************************************************************/
1159    
1160      release_all:
1161    
1162            return (0);
1163    }
1164    
1165    /*****************************************************************************
1166     *               Encode a sequence
1167     ****************************************************************************/
1168    
1169    void encode_sequence(enc_sequence_data_t *h) {
1170    
1171            /* Internal structures (handles) for encoding */
1172            void *enc_handle = NULL;
1173    
1174            int start_num = h->start_num;
1175            int stop_num = h->stop_num;
1176            char *outfilename = h->outfilename;
1177            float *totalPSNR = h->totalPSNR;
1178    
1179            int input_num;
1180            int totalsize;
1181            double totalenctime = 0.;
1182    
1183            unsigned char *mp4_buffer = NULL;
1184            unsigned char *in_buffer = NULL;
1185            unsigned char *out_buffer = NULL;
1186    
1187            double enctime;
1188    
1189            int result;
1190            int output_num;
1191            int nvop_counter;
1192            int m4v_size;
1193            int key;
1194            int stats_type;
1195            int stats_quant;
1196            int stats_length;
1197            int fakenvop = 0;
1198    
1199            FILE *in_file = stdin;
1200            FILE *out_file = NULL;
1201            FILE *time_file = NULL;
1202    
1203            char filename[256];
1204    
1205    #ifdef XVID_MKV_OUTPUT
1206            PMKVFILE myMKVFile = NULL;
1207            PMKVSTREAM myMKVStream = NULL;
1208            MKVSTREAMINFO myMKVStreamInfo;
1209    #endif
1210    #if defined(XVID_AVI_INPUT)
1211            PAVIFILE avi_in = NULL;
1212            PAVISTREAM avi_in_stream = NULL;
1213            PGETFRAME get_frame = NULL;
1214            BITMAPINFOHEADER myBitmapInfoHeader;
1215    #else
1216    #define get_frame NULL
1217    #endif
1218    #if defined(XVID_AVI_OUTPUT)
1219            int avierr;
1220            PAVIFILE myAVIFile = NULL;
1221            PAVISTREAM myAVIStream = NULL;
1222            AVISTREAMINFO myAVIStreamInfo;
1223    #endif
1224    #if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT)
1225            if (ARG_NUM_APP_THREADS > 1)
1226                    CoInitializeEx(0, COINIT_MULTITHREADED);
1227            AVIFileInit();
1228    #endif
1229    
1230            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
1231                    in_file = stdin;
1232            } else {
1233    #ifdef XVID_AVI_INPUT
1234          if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
1235              strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
1236                      ARG_INPUTTYPE==2)
1237          {
1238                      AVISTREAMINFO avi_info;
1239                      FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
1240    
1241                      if (avi_fp == NULL) {
1242                              fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
1243                              return;
1244                      }
1245                      fclose(avi_fp);
1246    
1247                      if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
1248                              fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
1249                              AVIFileExit();
1250                              return;
1251                      }
1252    
1253                      if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
1254                              fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
1255                              AVIFileRelease(avi_in);
1256                              AVIFileExit();
1257                              return;
1258                      }
1259    
1260                      AVIFileRelease(avi_in);
1261    
1262                      if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
1263                              fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE);
1264                              AVIStreamRelease(avi_in_stream);
1265                              AVIFileExit();
1266                              return;
1267                      }
1268    
1269                  if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) {
1270                              LONG size;
1271                              fprintf(stderr, "Non YV12 input colorspace %c%c%c%c! Attempting conversion...\n",
1272                                      avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256,
1273                                      (avi_info.fccHandler>>24)%256);
1274                              size = sizeof(myBitmapInfoHeader);
1275                              AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size);
1276                              if (size==0)
1277                                      fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
1278                              else {
1279                                      fprintf(stderr, "AVIStreamReadFormat read %d bytes.\n", size);
1280                                      fprintf(stderr, "width = %d, height = %d, planes = %d\n", myBitmapInfoHeader.biWidth,
1281                                              myBitmapInfoHeader.biHeight, myBitmapInfoHeader.biPlanes);
1282                                      fprintf(stderr, "Compression = %c%c%c%c, %d\n",
1283                                              myBitmapInfoHeader.biCompression%256, (myBitmapInfoHeader.biCompression>>8)%256,
1284                                              (myBitmapInfoHeader.biCompression>>16)%256, (myBitmapInfoHeader.biCompression>>24)%256,
1285                                              myBitmapInfoHeader.biCompression);
1286                                      fprintf(stderr, "Bits Per Pixel = %d\n", myBitmapInfoHeader.biBitCount);
1287                                      myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
1288                                      myBitmapInfoHeader.biBitCount = 12;
1289                                      myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
1290                                      get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader);
1291                              }
1292                              if (get_frame == NULL) {
1293                                    AVIStreamRelease(avi_in_stream);
1294                                    AVIFileExit();
1295                                    return;
1296                              }
1297                              else {
1298                                    unsigned char *temp;
1299                                    fprintf(stderr, "AVIStreamGetFrameOpen successful.\n");
1300                                    temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);
1301                                    if (temp != NULL) {
1302                                            int i;
1303                                            for (i = 0; i < (int)((DWORD*)temp)[0]; i++) {
1304                                                    fprintf(stderr, "%2d ", temp[i]);
1305                                            }
1306                                            fprintf(stderr, "\n");
1307                                    }
1308                              }
1309                              if (avi_info.fccHandler == MAKEFOURCC('D', 'I', 'B', ' ')) {
1310                                      AVIStreamGetFrameClose(get_frame);
1311                                      get_frame = NULL;
1312                                      ARG_COLORSPACE = XVID_CSP_BGR | XVID_CSP_VFLIP;
1313                              }
1314                      }
1315        }
1316        else
1317    #endif
1318                    {
1319                            in_file = fopen(ARG_INPUTFILE, "rb");
1320                            if (in_file == NULL) {
1321                                    fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
1322                                    return;
1323                            }
1324                    }
1325            }
1326    
1327            // This should be after the avi input opening stuff
1328            if (ARG_TIMECODEFILE != NULL) {
1329                    time_file = fopen(ARG_TIMECODEFILE, "r");
1330                    if (time_file==NULL) {
1331                            fprintf(stderr, "Couldn't open timecode file '%s'!\n", ARG_TIMECODEFILE);
1332                            return;
1333                    }
1334                    else {
1335                            fscanf(time_file, "# timecode format v2\n");
1336                    }
1337            }
1338    
1339          if (ARG_INPUTTYPE==1) {          if (ARG_INPUTTYPE==1) {
1340  #ifndef READ_PNM  #ifndef READ_PNM
1341                  if (read_pgmheader(in_file)) {                  if (read_pgmheader(in_file)) {
# Line 962  Line 1344 
1344  #endif  #endif
1345                          fprintf(stderr,                          fprintf(stderr,
1346                                          "Wrong input format, I want YUV encapsulated in PGM\n");                                          "Wrong input format, I want YUV encapsulated in PGM\n");
1347                          return (-1);                          return;
1348                  }                  }
1349          }          }
1350    
1351          /* Jump to the starting frame */          /* Jump to the starting frame */
1352          if (ARG_INPUTTYPE == 0)          if (ARG_INPUTTYPE == 0) /* TODO: Other input formats ??? */
1353                  fseek(in_file, ARG_STARTFRAMENR*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);                  fseek(in_file, start_num*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);
1354    
1355    
1356          /* now we know the sizes, so allocate memory */          /* now we know the sizes, so allocate memory */
1357          if (get_frame == NULL)          if (get_frame == NULL)
# Line 984  Line 1367 
1367                  goto free_all_memory;                  goto free_all_memory;
1368    
1369  /*****************************************************************************  /*****************************************************************************
1370   *                            XviD PART  Start   *                            Xvid PART  Start
1371   ****************************************************************************/   ****************************************************************************/
1372    
1373    
1374          result = enc_init(use_assembler);          result = enc_init(&enc_handle, h->statsfilename1, h->start_num);
1375          if (result) {          if (result) {
1376                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);
1377                  goto release_all;                  goto release_all;
# Line 1000  Line 1383 
1383    
1384          if (ARG_SAVEMPEGSTREAM) {          if (ARG_SAVEMPEGSTREAM) {
1385    
1386                  if (ARG_OUTPUTFILE) {                  if (outfilename) {
1387                          if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {                          if ((out_file = fopen(outfilename, "w+b")) == NULL) {
1388                                  fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);                                  fprintf(stderr, "Error opening output file %s\n", outfilename);
1389                                  goto release_all;                                  goto release_all;
1390                          }                          }
1391                  }                  }
# Line 1098  Line 1481 
1481          result = 0;          result = 0;
1482    
1483          input_num = 0;                          /* input frame counter */          input_num = 0;                          /* input frame counter */
1484          output_num = 0;                         /* output frame counter */          output_num = start_num;             /* output frame counter */
1485    
1486          nvop_counter = 0;          nvop_counter = 0;
1487    
# Line 1107  Line 1490 
1490                  char *type;                  char *type;
1491                  int sse[3];                  int sse[3];
1492    
1493                  if (input_num >= ARG_MAXFRAMENR && ARG_MAXFRAMENR > 0) {                  if ((input_num+start_num) >= stop_num && stop_num > 0) {
1494                          result = 1;                          result = 1;
1495                  }                  }
1496    
# Line 1116  Line 1499 
1499                          if (ARG_INPUTTYPE==2) {                          if (ARG_INPUTTYPE==2) {
1500                                  /* read avs/avi data (YUV-format) */                                  /* read avs/avi data (YUV-format) */
1501                                  if (get_frame != NULL) {                                  if (get_frame != NULL) {
1502                                          in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+ARG_STARTFRAMENR);                                          in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+start_num);
1503                                          if (in_buffer == NULL)                                          if (in_buffer == NULL)
1504                                                  result = 1;                                                  result = 1;
1505                                          else                                          else
1506                                                  in_buffer += ((DWORD*)in_buffer)[0];                                                  in_buffer += ((DWORD*)in_buffer)[0];
1507                                  } else {                                  } else {
1508                                          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)
1509                                                  result = 1;                                                  result = 1;
1510                                  }                                  }
1511                          } else                          } else
# Line 1144  Line 1527 
1527   *                       Encode and decode this frame   *                       Encode and decode this frame
1528   ****************************************************************************/   ****************************************************************************/
1529    
1530                  if (input_num >= (unsigned int)ARG_MAXFRAMENR-1 && ARG_MAXBFRAMES) {                  if ((unsigned int)(input_num+start_num) >= (unsigned int)(stop_num-1) && ARG_MAXBFRAMES) {
1531                          stats_type = XVID_TYPE_PVOP;                          stats_type = XVID_TYPE_PVOP;
1532                  }                  }
1533                  else                  else
# Line 1152  Line 1535 
1535    
1536                  enctime = msecond();                  enctime = msecond();
1537                  m4v_size =                  m4v_size =
1538                          enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type,                          enc_main(enc_handle, !result ? in_buffer : 0, mp4_buffer, &key, &stats_type,
1539                                           &stats_quant, &stats_length, sse, input_num);                                           &stats_quant, &stats_length, sse, input_num);
1540                  enctime = msecond() - enctime;                  enctime = msecond() - enctime;
1541    
# Line 1180  Line 1563 
1563                          }                          }
1564    
1565                          if (stats_length > 8) {                          if (stats_length > 8) {
1566                                  framestats[stats_type].count++;                                  h->framestats[stats_type].count++;
1567                                  framestats[stats_type].quants[stats_quant]++;                                  h->framestats[stats_type].quants[stats_quant]++;
1568                                  framestats[stats_type].size += stats_length;                                  h->framestats[stats_type].size += stats_length;
1569                          }                          }
1570                          else {                          else {
1571                                  framestats[5].count++;                                  h->framestats[5].count++;
1572                                  framestats[5].quants[stats_quant]++;                                  h->framestats[5].quants[stats_quant]++;
1573                                  framestats[5].size += stats_length;                                  h->framestats[5].size += stats_length;
1574                          }                          }
1575    
1576    #define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height)))))
1577    
1578                          if (ARG_PROGRESS == 0) {                          if (ARG_PROGRESS == 0) {
1579                                  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,
1580                                          key, (float) enctime, (int) m4v_size);                                          key, (float) enctime, (int) m4v_size);
1581                                  printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,                                  printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,
1582                                     stats_length);                                     stats_length);
1583    
 #define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height)))))  
1584    
1585                                  if (ARG_STATS) {                                  if (ARG_STATS) {
1586                                          printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",                                          printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
1587                                                  SSE2PSNR(sse[0], XDIM, YDIM), SSE2PSNR(sse[1], XDIM / 2,                                                  SSE2PSNR(sse[0], XDIM, YDIM), SSE2PSNR(sse[1], XDIM / 2, YDIM / 2),
                                                                                                                                 YDIM / 2),  
1588                                                  SSE2PSNR(sse[2], XDIM / 2, YDIM / 2));                                                  SSE2PSNR(sse[2], XDIM / 2, YDIM / 2));
   
                                         totalPSNR[0] += SSE2PSNR(sse[0], XDIM, YDIM);  
                                         totalPSNR[1] += SSE2PSNR(sse[1], XDIM/2, YDIM/2);  
                                         totalPSNR[2] += SSE2PSNR(sse[2], XDIM/2, YDIM/2);  
1589                                  }                                  }
1590                                  printf("\n");                                  printf("\n");
1591                          } else {                          } else {
1592                                  if (input_num % ARG_PROGRESS == 1) {                                  if ((input_num) % ARG_PROGRESS == 1) {
1593                                          if (ARG_MAXFRAMENR > 0) {                                          if (stop_num > 0) {
1594                                                  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", \
1595                                                          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), \
1596                                                          (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);                                                          ((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num));
1597                                          } else {                                          } else {
1598                                                  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", \
1599                                                          input_num, input_num*1000/totalenctime, \                                                          (ARG_NUM_APP_THREADS*input_num), (ARG_NUM_APP_THREADS*input_num)*1000/(totalenctime), \
1600                                                          (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);                                                          ((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num));
1601                                          }                                          }
1602                                  }                                  }
1603                          }                          }
1604    
1605                            if (ARG_STATS) {
1606                                    totalPSNR[0] += SSE2PSNR(sse[0], XDIM, YDIM);
1607                                    totalPSNR[1] += SSE2PSNR(sse[1], XDIM/2, YDIM/2);
1608                                    totalPSNR[2] += SSE2PSNR(sse[2], XDIM/2, YDIM/2);
1609                  }                  }
1610  #undef SSE2PSNR  #undef SSE2PSNR
1611                    }
1612    
1613                  if (m4v_size < 0)                  if (m4v_size < 0)
1614                          break;                          break;
# Line 1278  Line 1661 
1661                                          removedivxp((char*)mp4_buffer, m4v_size);                                          removedivxp((char*)mp4_buffer, m4v_size);
1662    
1663                                  /* Save ES stream */                                  /* Save ES stream */
1664                                  if (ARG_OUTPUTFILE && out_file && !(fakenvop && m4v_size <= 8)) {                                  if (outfilename && out_file && !(fakenvop && m4v_size <= 8)) {
1665                                                  fwrite(mp4_buffer, 1, m4v_size, out_file);                                                  fwrite(mp4_buffer, 1, m4v_size, out_file);
1666                                  }                                  }
1667  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
# Line 1292  Line 1675 
1675                                  fakenvop=0;                                  fakenvop=0;
1676                  }                  }
1677    
1678                  input_num++;                  if (!result)
1679                            (input_num)++;
1680    
1681                  /* Read the header if it's pgm stream */                  /* Read the header if it's pgm stream */
1682                  if (!result && (ARG_INPUTTYPE==1))                  if (!result && (ARG_INPUTTYPE==1))
# Line 1304  Line 1688 
1688          } while (1);          } while (1);
1689    
1690    
   
 /*****************************************************************************  
  *         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  
  ****************************************************************************/  
   
1691    release_all:    release_all:
1692    
1693      h->input_num = input_num;
1694      h->totalenctime = totalenctime;
1695      h->totalsize = totalsize;
1696    
1697  #ifdef XVID_AVI_INPUT  #ifdef XVID_AVI_INPUT
1698          if (get_frame) AVIStreamGetFrameClose(get_frame);          if (get_frame) AVIStreamGetFrameClose(get_frame);
1699          if (avi_stream) AVIStreamRelease(avi_stream);          if (avi_in_stream) AVIStreamRelease(avi_in_stream);
 #ifndef XVID_AVI_OUTPUT  
         AVIFileExit();  
 #endif  
1700  #endif  #endif
1701    
1702          if (enc_handle) {          if (enc_handle) {
1703                  result = enc_stop();                  result = enc_stop(enc_handle);
1704                  if (result)                  if (result)
1705                          fprintf(stderr, "Encore RELEASE problem return value %d\n",                          fprintf(stderr, "Encore RELEASE problem return value %d\n",
1706                                          result);                                          result);
# Line 1387  Line 1710 
1710                  fclose(in_file);                  fclose(in_file);
1711          if (out_file)          if (out_file)
1712                  fclose(out_file);                  fclose(out_file);
1713            if (time_file)
1714                    fclose(time_file);
1715    
1716  #ifdef XVID_AVI_OUTPUT  #ifdef XVID_AVI_OUTPUT
1717          if (myAVIStream) AVIStreamRelease(myAVIStream);          if (myAVIStream) AVIStreamRelease(myAVIStream);
1718          if (myAVIFile) AVIFileRelease(myAVIFile);          if (myAVIFile) AVIFileRelease(myAVIFile);
         AVIFileExit();  
1719  #endif  #endif
1720  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
1721          if (myMKVStream) MKVStreamRelease(myMKVStream);          if (myMKVStream) MKVStreamRelease(myMKVStream);
1722          if (myMKVFile) MKVFileRelease(myMKVFile);          if (myMKVFile) MKVFileRelease(myMKVFile);
1723  #endif  #endif
1724    #if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT)
1725            AVIFileExit();
1726    #endif
1727    
1728    free_all_memory:    free_all_memory:
1729          free(out_buffer);          free(out_buffer);
1730          free(mp4_buffer);          free(mp4_buffer);
1731          free(in_buffer);          free(in_buffer);
   
         return (0);  
   
1732  }  }
1733    
   
1734  /*****************************************************************************  /*****************************************************************************
1735   *                        "statistical" functions   *                        "statistical" functions
1736   *   *
1737   *  these are not needed for encoding or decoding, but for measuring   *  these are not needed for encoding or decoding, but for measuring
1738   *  time and quality, there in nothing specific to XviD in these   *  time and quality, there in nothing specific to Xvid in these
1739   *   *
1740   *****************************************************************************/   *****************************************************************************/
1741    
# Line 1498  Line 1821 
1821  #endif  #endif
1822          fprintf(stderr, " -w      integer: frame width ([1.2048])\n");          fprintf(stderr, " -w      integer: frame width ([1.2048])\n");
1823          fprintf(stderr, " -h      integer: frame height ([1.2048])\n");          fprintf(stderr, " -h      integer: frame height ([1.2048])\n");
1824            fprintf(stderr, " -csp    string : colorspace of raw input file i420, yv12 (default)\n");
1825          fprintf(stderr, " -frames integer: number of frames to encode\n");          fprintf(stderr, " -frames integer: number of frames to encode\n");
1826          fprintf(stderr, "\n");          fprintf(stderr, "\n");
1827          fprintf(stderr, "Output options:\n");          fprintf(stderr, "Output options:\n");
# Line 1541  Line 1865 
1865          fprintf(stderr, "-odegrade   integer             : max overflow degradation (5)\n");          fprintf(stderr, "-odegrade   integer             : max overflow degradation (5)\n");
1866          fprintf(stderr, "-chigh      integer             : high bitrate scenes degradation (0)\n");          fprintf(stderr, "-chigh      integer             : high bitrate scenes degradation (0)\n");
1867          fprintf(stderr, "-clow       integer             : low bitrate scenes improvement (0)\n");          fprintf(stderr, "-clow       integer             : low bitrate scenes improvement (0)\n");
1868          fprintf(stderr, "-overhead   integer             : container frame overhead (24)\n");          fprintf(stderr, "-overhead   integer             : container frame overhead (0)\n");
1869          fprintf(stderr, "-vbvsize    integer             : use vbv buffer size\n");          fprintf(stderr, "-vbvsize    integer             : use vbv buffer size\n");
1870          fprintf(stderr, "-vbvmax     integer             : vbv max bitrate\n");          fprintf(stderr, "-vbvmax     integer             : vbv max bitrate\n");
1871          fprintf(stderr, "-vbvpeak    integer             : vbv peak bitrate over 1 second\n");          fprintf(stderr, "-vbvpeak    integer             : vbv peak bitrate over 1 second\n");
# Line 1552  Line 1876 
1876          fprintf(stderr, " -quality integer               : quality ([0..%d]) (6)\n", ME_ELEMENTS - 1);          fprintf(stderr, " -quality integer               : quality ([0..%d]) (6)\n", ME_ELEMENTS - 1);
1877          fprintf(stderr, " -vhqmode integer               : level of R-D optimizations ([0..4]) (1)\n");          fprintf(stderr, " -vhqmode integer               : level of R-D optimizations ([0..4]) (1)\n");
1878          fprintf(stderr, " -bvhq                          : use R-D optimizations for B-frames\n");          fprintf(stderr, " -bvhq                          : use R-D optimizations for B-frames\n");
1879            fprintf(stderr, " -metric integer                : distortion metric for R-D opt (PSNR:0, PSNRHVSM: 1)\n");
1880          fprintf(stderr, " -qpel                          : use quarter pixel ME\n");          fprintf(stderr, " -qpel                          : use quarter pixel ME\n");
1881          fprintf(stderr, " -gmc                           : use global motion compensation\n");          fprintf(stderr, " -gmc                           : use global motion compensation\n");
1882          fprintf(stderr, " -qtype   integer               : quantization type (H263:0, MPEG4:1) (0)\n");          fprintf(stderr, " -qtype   integer               : quantization type (H263:0, MPEG4:1) (0)\n");
# Line 1559  Line 1884 
1884          fprintf(stderr, " -interlaced [integer]          : interlaced encoding (BFF:1, TFF:2) (1)\n");          fprintf(stderr, " -interlaced [integer]          : interlaced encoding (BFF:1, TFF:2) (1)\n");
1885          fprintf(stderr, " -nopacked                      : Disable packed mode\n");          fprintf(stderr, " -nopacked                      : Disable packed mode\n");
1886          fprintf(stderr, " -noclosed_gop                  : Disable closed GOP mode\n");          fprintf(stderr, " -noclosed_gop                  : Disable closed GOP mode\n");
1887          fprintf(stderr, " -lumimasking                   : use lumimasking algorithm\n");          fprintf(stderr, " -masking [integer]             : HVS masking mode (None:0, Lumi:1, Variance:2) (0)\n");
1888          fprintf(stderr, " -stats                         : print stats about encoded frames\n");          fprintf(stderr, " -stats                         : print stats about encoded frames\n");
1889            fprintf(stderr, " -ssim [integer]                : prints ssim for every frame (accurate: 0 fast: 4) (2)\n");
1890            fprintf(stderr, " -ssim_file filename            : outputs the ssim stats into a file\n");
1891            fprintf(stderr, " -psnrhvsm                      : prints PSNRHVSM metric for every frame\n");
1892          fprintf(stderr, " -debug                         : activates xvidcore internal debugging output\n");          fprintf(stderr, " -debug                         : activates xvidcore internal debugging output\n");
1893          fprintf(stderr, " -vop_debug                     : print some info directly into encoded frames\n");          fprintf(stderr, " -vop_debug                     : print some info directly into encoded frames\n");
1894          fprintf(stderr, " -nochromame                    : Disable chroma motion estimation\n");          fprintf(stderr, " -nochromame                    : Disable chroma motion estimation\n");
# Line 1574  Line 1902 
1902          fprintf(stderr, " -drop    integer               : Frame Drop Ratio (0..100) (0)\n");          fprintf(stderr, " -drop    integer               : Frame Drop Ratio (0..100) (0)\n");
1903          fprintf(stderr, " -start   integer               : Starting frame number\n");          fprintf(stderr, " -start   integer               : Starting frame number\n");
1904          fprintf(stderr, " -threads integer               : Number of threads\n");          fprintf(stderr, " -threads integer               : Number of threads\n");
1905            fprintf(stderr, " -slices  integer               : Number of slices\n");
1906          fprintf(stderr, " -progress [integer]            : Show progress updates every n frames (10)\n");          fprintf(stderr, " -progress [integer]            : Show progress updates every n frames (10)\n");
1907          fprintf(stderr, " -par     integer[:integer]     : Set Pixel Aspect Ratio.\n");          fprintf(stderr, " -par     integer[:integer]     : Set Pixel Aspect Ratio.\n");
1908          fprintf(stderr, "                                  1 = 1:1\n");          fprintf(stderr, "                                  1 = 1:1\n");
# Line 1591  Line 1920 
1920   *                       Input and output functions   *                       Input and output functions
1921   *   *
1922   *      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
1923   *      image. It's just for convenience, again nothing specific to XviD   *      image. It's just for convenience, again nothing specific to Xvid
1924   *   *
1925   *****************************************************************************/   *****************************************************************************/
1926    
# Line 1602  Line 1931 
1931          int bytes, xsize, ysize, depth;          int bytes, xsize, ysize, depth;
1932          char dummy[2];          char dummy[2];
1933    
1934          bytes = fread(dummy, 1, 2, handle);          bytes = (int) fread(dummy, 1, 2, handle);
1935    
1936          if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5'))          if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5'))
1937                  return (1);                  return (1);
# Line 1753  Line 2082 
2082    
2083  #define FRAMERATE_INCR 1001  #define FRAMERATE_INCR 1001
2084    
2085    /* Gobal encoder init, once per process */
2086  /* Initialize encoder for first use, pass all needed parameters to the codec */  void
2087  static int  enc_gbl(int use_assembler)
 enc_init(int use_assembler)  
2088  {  {
         int xerr;  
         //xvid_plugin_cbr_t cbr;  
     xvid_plugin_single_t single;  
         xvid_plugin_2pass1_t rc2pass1;  
         xvid_plugin_2pass2_t rc2pass2;  
         //xvid_plugin_fixed_t rcfixed;  
         xvid_enc_plugin_t plugins[7];  
2089          xvid_gbl_init_t xvid_gbl_init;          xvid_gbl_init_t xvid_gbl_init;
         xvid_enc_create_t xvid_enc_create;  
         int i;  
2090    
2091          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
2092           * XviD core initialization           * Xvid core initialization
2093           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
2094    
2095          /* Set version -- version checking will done by xvidcore */          /* Set version -- version checking will done by xvidcore */
# Line 1791  Line 2110 
2110                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
2111          }          }
2112    
2113          /* Initialize XviD core -- Should be done once per __process__ */          /* Initialize Xvid core -- Should be done once per __process__ */
2114          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
2115        ARG_CPU_FLAGS = xvid_gbl_init.cpu_flags;
2116          enc_info();          enc_info();
2117    }
2118    
2119    /* Initialize encoder for first use, pass all needed parameters to the codec */
2120    static int
2121    enc_init(void **enc_handle, char *stats_pass1, int start_num)
2122    {
2123            int xerr;
2124            //xvid_plugin_cbr_t cbr;
2125        xvid_plugin_single_t single;
2126            xvid_plugin_2pass1_t rc2pass1;
2127            xvid_plugin_2pass2_t rc2pass2;
2128            xvid_plugin_ssim_t ssim;
2129        xvid_plugin_lumimasking_t masking;
2130            //xvid_plugin_fixed_t rcfixed;
2131            xvid_enc_plugin_t plugins[8];
2132            xvid_enc_create_t xvid_enc_create;
2133            int i;
2134    
2135          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
2136           * XviD encoder initialization           * Xvid encoder initialization
2137           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
2138    
2139          /* Version again */          /* Version again */
# Line 1851  Line 2188 
2188                  rc2pass2.vbv_size     =  ARG_VBVSIZE;                  rc2pass2.vbv_size     =  ARG_VBVSIZE;
2189                  rc2pass2.vbv_initial  =  (ARG_VBVSIZE*3)/4;                  rc2pass2.vbv_initial  =  (ARG_VBVSIZE*3)/4;
2190                  rc2pass2.vbv_maxrate  =  ARG_VBVMAXRATE;                  rc2pass2.vbv_maxrate  =  ARG_VBVMAXRATE;
2191                  rc2pass2.vbv_peakrate =  ARG_VBVPEAKRATE*3;                  rc2pass2.vbv_peakrate =  ARG_VBVPEAKRATE;
2192    
2193    
2194                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
# Line 1859  Line 2196 
2196                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2197          }          }
2198    
2199          if (ARG_PASS1) {          if (stats_pass1) {
2200                  memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));                  memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));
2201                  rc2pass1.version = XVID_VERSION;                  rc2pass1.version = XVID_VERSION;
2202                  rc2pass1.filename = ARG_PASS1;                  rc2pass1.filename = stats_pass1;
2203                  if (ARG_FULL1PASS)                  if (ARG_FULL1PASS)
2204                          prepare_full1pass_zones();                          prepare_full1pass_zones();
2205                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;
# Line 1882  Line 2219 
2219    
2220    
2221          if (ARG_LUMIMASKING) {          if (ARG_LUMIMASKING) {
2222                    memset(&masking, 0, sizeof(xvid_plugin_lumimasking_t));
2223                    masking.method = (ARG_LUMIMASKING==2);
2224                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
2225                  plugins[xvid_enc_create.num_plugins].param = NULL;                  plugins[xvid_enc_create.num_plugins].param = &masking;
2226                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2227          }          }
2228    
# Line 1893  Line 2232 
2232                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2233          }          }
2234    
2235            if (ARG_SSIM>=0 || ARG_SSIM_PATH != NULL) {
2236            memset(&ssim, 0, sizeof(xvid_plugin_ssim_t));
2237    
2238            plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim;
2239    
2240                    if( ARG_SSIM >=0){
2241                            ssim.b_printstat = 1;
2242                            ssim.acc = ARG_SSIM;
2243                    } else {
2244                            ssim.b_printstat = 0;
2245                            ssim.acc = 2;
2246                    }
2247    
2248                    if(ARG_SSIM_PATH != NULL){
2249                            ssim.stat_path = ARG_SSIM_PATH;
2250                    }
2251    
2252            ssim.cpu_flags = ARG_CPU_FLAGS;
2253                    ssim.b_visualize = 0;
2254                    plugins[xvid_enc_create.num_plugins].param = &ssim;
2255                    xvid_enc_create.num_plugins++;
2256            }
2257    
2258            if (ARG_PSNRHVSM>0) {
2259            plugins[xvid_enc_create.num_plugins].func = xvid_plugin_psnrhvsm;
2260                    plugins[xvid_enc_create.num_plugins].param = NULL;
2261                    xvid_enc_create.num_plugins++;
2262            }
2263    
2264  #if 0  #if 0
2265          if (ARG_DEBUG) {          if (ARG_DEBUG) {
2266                  plugins[xvid_enc_create.num_plugins].func = rawenc_debug;                  plugins[xvid_enc_create.num_plugins].func = rawenc_debug;
# Line 1902  Line 2270 
2270  #endif  #endif
2271    
2272          xvid_enc_create.num_threads = ARG_THREADS;          xvid_enc_create.num_threads = ARG_THREADS;
2273            xvid_enc_create.num_slices  = ARG_SLICES;
2274    
2275          /* Frame rate  */          /* Frame rate  */
2276          xvid_enc_create.fincr = ARG_DWSCALE;          xvid_enc_create.fincr = ARG_DWSCALE;
# Line 1929  Line 2298 
2298          /* Frame drop ratio */          /* Frame drop ratio */
2299          xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;          xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;
2300    
2301            /* Start frame number */
2302            xvid_enc_create.start_frame_num = start_num;
2303    
2304          /* Global encoder options */          /* Global encoder options */
2305          xvid_enc_create.global = 0;          xvid_enc_create.global = 0;
2306    
# Line 1945  Line 2317 
2317          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
2318    
2319          /* Retrieve the encoder instance from the structure */          /* Retrieve the encoder instance from the structure */
2320          enc_handle = xvid_enc_create.handle;          *enc_handle = xvid_enc_create.handle;
2321    
2322          free(xvid_enc_create.zones);          free(xvid_enc_create.zones);
2323    
# Line 1976  Line 2348 
2348                  fprintf(stderr, "SSE ");                  fprintf(stderr, "SSE ");
2349          if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2)          if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2)
2350                  fprintf(stderr, "SSE2 ");                  fprintf(stderr, "SSE2 ");
2351            if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE3)
2352                    fprintf(stderr, "SSE3 ");
2353            if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE41)
2354                    fprintf(stderr, "SSE41 ");
2355          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW)          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW)
2356                  fprintf(stderr, "3DNOW ");                  fprintf(stderr, "3DNOW ");
2357          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT)          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT)
# Line 1984  Line 2360 
2360                  fprintf(stderr, "TSC ");                  fprintf(stderr, "TSC ");
2361          fprintf(stderr, "\n");          fprintf(stderr, "\n");
2362          fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);          fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);
2363          if (!ARG_THREADS)          ARG_NUM_APP_THREADS = xvid_gbl_info.num_threads;
2364                  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);  
2365          return ret;          return ret;
2366  }  }
2367    
2368  static int  static int
2369  enc_stop()  enc_stop(void *enc_handle)
2370  {  {
2371          int xerr;          int xerr;
2372    
# Line 2002  Line 2377 
2377  }  }
2378    
2379  static int  static int
2380  enc_main(unsigned char *image,  enc_main(void *enc_handle,
2381                     unsigned char *image,
2382                   unsigned char *bitstream,                   unsigned char *bitstream,
2383                   int *key,                   int *key,
2384                   int *stats_type,                   int *stats_type,
# Line 2031  Line 2407 
2407          if (image) {          if (image) {
2408                  xvid_enc_frame.input.plane[0] = image;                  xvid_enc_frame.input.plane[0] = image;
2409  #ifndef READ_PNM  #ifndef READ_PNM
                 if (ARG_INPUTTYPE==2)  
2410                          xvid_enc_frame.input.csp = ARG_COLORSPACE;                          xvid_enc_frame.input.csp = ARG_COLORSPACE;
                 else  
                         xvid_enc_frame.input.csp = XVID_CSP_I420;  
2411                  xvid_enc_frame.input.stride[0] = XDIM;                  xvid_enc_frame.input.stride[0] = XDIM;
2412  #else  #else
2413                  xvid_enc_frame.input.csp = XVID_CSP_BGR;                  xvid_enc_frame.input.csp = XVID_CSP_BGR;
# Line 2120  Line 2493 
2493          if (ARG_BVHQ)          if (ARG_BVHQ)
2494                  xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP;                  xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP;
2495    
2496            if (ARG_QMETRIC == 1)
2497                    xvid_enc_frame.vop_flags |= XVID_VOP_RD_PSNRHVSM;
2498    
2499          switch (ARG_VHQMODE) /* this is the same code as for vfw */          switch (ARG_VHQMODE) /* this is the same code as for vfw */
2500          {          {
2501          case 1: /* VHQ_MODE_DECISION */          case 1: /* VHQ_MODE_DECISION */
# Line 2215  Line 2591 
2591    
2592                  ZONES[NUM_ZONES].frame = 0;                  ZONES[NUM_ZONES].frame = 0;
2593                  ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;                  ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
2594                  ZONES[NUM_ZONES].modifier = ARG_CQ;                  ZONES[NUM_ZONES].modifier = (int)ARG_CQ;
2595                  ZONES[NUM_ZONES].type = XVID_TYPE_AUTO;                  ZONES[NUM_ZONES].type = XVID_TYPE_AUTO;
2596                  ZONES[NUM_ZONES].greyscale = 0;                  ZONES[NUM_ZONES].greyscale = 0;
2597                  ZONES[NUM_ZONES].chroma_opt = 0;                  ZONES[NUM_ZONES].chroma_opt = 0;
# Line 2231  Line 2607 
2607          for(i = 0; i < NUM_ZONES; i++)          for(i = 0; i < NUM_ZONES; i++)
2608                  if (ZONES[i].mode == XVID_ZONE_WEIGHT) {                  if (ZONES[i].mode == XVID_ZONE_WEIGHT) {
2609                          ZONES[i].mode = XVID_ZONE_QUANT;                          ZONES[i].mode = XVID_ZONE_QUANT;
2610                          ZONES[i].modifier = (100*ARG_CQ) / ZONES[i].modifier;                          ZONES[i].modifier = (int) ((100*ARG_CQ) / ZONES[i].modifier);
2611                  }                  }
2612  }  }
2613    
# Line 2300  Line 2676 
2676          int i;          int i;
2677          char* userdata;          char* userdata;
2678    
2679          for (i=0; i <= (bufsize-sizeof(userdata_start_code)); i++) {          for (i=0; i <= (int)(bufsize-sizeof(userdata_start_code)); i++) {
2680                  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) {
2681                          if ((userdata = strstr(buf+i+4, "DivX"))!=NULL) {                          if ((userdata = strstr(buf+i+4, "DivX"))!=NULL) {
2682                                  userdata[strlen(userdata)-1] = '\0';                                  userdata[strlen(userdata)-1] = '\0';

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

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