[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 1757, Wed Nov 8 06:55:27 2006 UTC revision 1894, Sun Oct 10 19:24:24 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.33 2006-11-08 06:55:27 Skal Exp $   * $Id: xvid_encraw.c,v 1.43 2010-10-10 19:20:03 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;  static  int ARG_SSIM = -1;
192    static  int ARG_PSNRHVSM = 0;
193  static  char* ARG_SSIM_PATH = NULL;  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;
# Line 187  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 249  Line 261 
261  static  int ARG_COLORSPACE = XVID_CSP_YV12;  static  int ARG_COLORSPACE = XVID_CSP_YV12;
262          /* the path where to save output */          /* the path where to save output */
263  static char filepath[256] = "./";  static char filepath[256] = "./";
264          /* Internal structures (handles) for encoding and decoding */  
 static  void *enc_handle = NULL;  
265  static  unsigned char qmatrix_intra[64];  static  unsigned char qmatrix_intra[64];
266  static  unsigned char qmatrix_inter[64];  static  unsigned char qmatrix_inter[64];
267    
# Line 292  Line 303 
303                                                  unsigned char *image);                                                  unsigned char *image);
304    
305  /* Encoder related functions */  /* Encoder related functions */
306  static int enc_init(int use_assembler);  static void enc_gbl(int use_assembler);
307    static int  enc_init(void **enc_handle, char *stats_pass1, int start_num);
308  static int enc_info();  static int enc_info();
309  static int enc_stop();  static int  enc_stop(void *enc_handle);
310  static int enc_main(unsigned char *image,  static int  enc_main(void *enc_handle,
311                                             unsigned char *image,
312                                          unsigned char *bitstream,                                          unsigned char *bitstream,
313                                          int *key,                                          int *key,
314                                          int *stats_type,                                          int *stats_type,
# Line 303  Line 316 
316                                          int *stats_length,                                          int *stats_length,
317                                          int stats[3],                                          int stats[3],
318                                          int framenum);                                          int framenum);
319    static void encode_sequence(enc_sequence_data_t *h);
320    
321  /* Zone Related Functions */  /* Zone Related Functions */
322  static void apply_zone_modifiers(xvid_enc_frame_t * frame, int framenum);  static void apply_zone_modifiers(xvid_enc_frame_t * frame, int framenum);
# Line 321  Line 335 
335  main(int argc,  main(int argc,
336           char *argv[])           char *argv[])
337  {  {
   
         unsigned char *mp4_buffer = NULL;  
         unsigned char *in_buffer = NULL;  
         unsigned char *out_buffer = NULL;  
   
         double enctime;  
338          double totalenctime = 0.;          double totalenctime = 0.;
339          float totalPSNR[3] = {0., 0., 0.};          float totalPSNR[3] = {0., 0., 0.};
340    
341          FILE *statsfile;          FILE *statsfile;
342            frame_stats_t framestats[7];
343    
344          int totalsize;          int input_num = 0;
345          int result;          int totalsize = 0;
         int m4v_size;  
         int key;  
         int stats_type;  
         int stats_quant;  
         int stats_length;  
346          int use_assembler = 1;          int use_assembler = 1;
         int fakenvop = 0;  
347          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  
348    
349          printf("xvid_encraw - raw mpeg4 bitstream encoder ");          printf("xvid_encraw - raw mpeg4 bitstream encoder ");
350          printf("written by Christoph Lampert 2002-2003\n\n");          printf("written by Christoph Lampert\n\n");
351    
352          /* Is there a dumb XviD coder ? */          /* Is there a dumb Xvid coder ? */
353          if(ME_ELEMENTS != VOP_ELEMENTS) {          if(ME_ELEMENTS != VOP_ELEMENTS) {
354                  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");
355                  return(-1);                  return(-1);
356          }          }
357    
358            /* Clear framestats */
359            memset(framestats, 0, sizeof(framestats));
360    
361  /*****************************************************************************  /*****************************************************************************
362   *                            Command line parsing   *                            Command line parsing
363   ****************************************************************************/   ****************************************************************************/
# Line 391  Line 374 
374                  } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) {
375                          i++;                          i++;
376                          YDIM = atoi(argv[i]);                          YDIM = atoi(argv[i]);
377                    } else if (strcmp("-csp",argv[i]) == 0 && i < argc - 1) {
378                            i++;
379                            if (strcmp(argv[i],"i420") == 0){
380                                    ARG_COLORSPACE = XVID_CSP_I420;
381                            } else if(strcmp(argv[i],"yv12") == 0){
382                                    ARG_COLORSPACE = XVID_CSP_YV12;
383                            } else {
384                                    printf("Invalid colorspace\n");
385                                    return 0;
386                            }
387          } else if (strcmp("-bitrate", argv[i]) == 0) {          } else if (strcmp("-bitrate", argv[i]) == 0) {
388                          if (i < argc - 1)                          if (i < argc - 1)
389                                  ARG_BITRATE = atoi(argv[i+1]);                                  ARG_BITRATE = atoi(argv[i+1]);
390                          if (ARG_BITRATE) {                          if (ARG_BITRATE) {
391                                  i++;                                  i++;
392                                  if (ARG_BITRATE <= 10000)                                  if (ARG_BITRATE <= 20000)
393                                          /* if given parameter is <= 10000, assume it means kbps */                                          /* if given parameter is <= 20000, assume it means kbps */
394                                          ARG_BITRATE *= 1000;                                          ARG_BITRATE *= 1000;
395                          }                          }
396                          else                          else
# Line 586  Line 579 
579                                  i++;                                  i++;
580                                  ARG_SSIM = atoi(argv[i]);                                  ARG_SSIM = atoi(argv[i]);
581                          }                          }
582                    } else if (strcmp("-psnrhvsm", argv[i]) == 0) {
583                            ARG_PSNRHVSM = 1;
584                  } else if (strcmp("-ssim_file", argv[i]) == 0 && i < argc -1) {                  } else if (strcmp("-ssim_file", argv[i]) == 0 && i < argc -1) {
585                          i++;                          i++;
586                          ARG_SSIM_PATH = argv[i];                          ARG_SSIM_PATH = argv[i];
# Line 594  Line 589 
589                          ARG_TIMECODEFILE = argv[i];                          ARG_TIMECODEFILE = argv[i];
590                  } else if (strcmp("-dump", argv[i]) == 0) {                  } else if (strcmp("-dump", argv[i]) == 0) {
591                          ARG_DUMP = 1;                          ARG_DUMP = 1;
592                  } else if (strcmp("-lumimasking", argv[i]) == 0) {                  } else if (strcmp("-masking", argv[i]) == 0 && i < argc -1) {
593                          ARG_LUMIMASKING = 1;                          i++;
594                            ARG_LUMIMASKING = atoi(argv[i]);
595                  } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {
596                          i++;                          i++;
597                          ARG_INPUTTYPE = atoi(argv[i]);                          ARG_INPUTTYPE = atoi(argv[i]);
# Line 819  Line 815 
815                  return (-1);                  return (-1);
816          }          }
817    
818          if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if (ARG_INPUTFILE != NULL) {
819                  in_file = stdin;  #if defined(XVID_AVI_INPUT)
         } else {  
 #ifdef XVID_AVI_INPUT  
820        if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||        if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
821            strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||            strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
822                    ARG_INPUTTYPE==2)                    ARG_INPUTTYPE==2)
823        {        {
824                      PAVIFILE avi_in = NULL;
825                      PAVISTREAM avi_in_stream = NULL;
826                      PGETFRAME get_frame = NULL;
827                      BITMAPINFOHEADER myBitmapInfoHeader;
828                    AVISTREAMINFO avi_info;                    AVISTREAMINFO avi_info;
 #ifndef XVID_AVI_OUTPUT  
                   AVIFileInit();  
 #endif  
829                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
830    
831                      AVIFileInit();
832    
833                    if (avi_fp == NULL) {                    if (avi_fp == NULL) {
834                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
835                            return (-1);                            return (-1);
836                    }                    }
837                    fclose(avi_fp);                    fclose(avi_fp);
838    
839                    if (AVIFileOpen(&avi_file, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {                    if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
840                            fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
841                            AVIFileExit();                            AVIFileExit();
842                            return(-1);                            return(-1);
843                    }                    }
844    
845                    if (AVIFileGetStream(avi_file, &avi_stream, streamtypeVIDEO, 0) != AVIERR_OK) {                    if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
846                            fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
847                            AVIFileRelease(avi_file);                            AVIFileRelease(avi_in);
848                            AVIFileExit();                            AVIFileExit();
849                            return (-1);                            return (-1);
850                    }                    }
851    
852                    AVIFileRelease(avi_file);                    AVIFileRelease(avi_in);
853    
854                    if(AVIStreamInfo(avi_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {                    if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
855                            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);
856                            AVIStreamRelease(avi_stream);                            AVIStreamRelease(avi_in_stream);
857                            AVIFileExit();                            AVIFileExit();
858                            return (-1);                            return (-1);
859                    }                    }
# Line 866  Line 864 
864                                    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,
865                                    (avi_info.fccHandler>>24)%256);                                    (avi_info.fccHandler>>24)%256);
866                            size = sizeof(myBitmapInfoHeader);                            size = sizeof(myBitmapInfoHeader);
867                            AVIStreamReadFormat(avi_stream, 0, &myBitmapInfoHeader, &size);                            AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size);
868                            if (size==0)                            if (size==0)
869                                    fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");                                    fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
870                            else {                            else {
# Line 881  Line 879 
879                                    myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');                                    myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
880                                    myBitmapInfoHeader.biBitCount = 12;                                    myBitmapInfoHeader.biBitCount = 12;
881                                    myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;                                    myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
882                                    get_frame = AVIStreamGetFrameOpen(avi_stream, &myBitmapInfoHeader);                                    get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader);
883                            }                            }
884                            if (get_frame == NULL) {                            if (get_frame == NULL) {
885                                  AVIStreamRelease(avi_stream);                                  AVIStreamRelease(avi_in_stream);
886                                  AVIFileExit();                                  AVIFileExit();
887                                  return (-1);                                  return (-1);
888                            }                            }
# Line 921  Line 919 
919                    }                    }
920    
921                    ARG_INPUTTYPE = 2;                    ARG_INPUTTYPE = 2;
922    
923                      if (get_frame) AVIStreamGetFrameClose(get_frame);
924                      if (avi_in_stream) AVIStreamRelease(avi_in_stream);
925                      AVIFileExit();
926      }      }
927      else      else
928  #endif  #endif
929                  {                  {
930                          in_file = fopen(ARG_INPUTFILE, "rb");                          FILE *in_file = fopen(ARG_INPUTFILE, "rb");
931                            int pos = 0;
932                          if (in_file == NULL) {                          if (in_file == NULL) {
933                                  fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);                                  fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
934                                  return (-1);                                  return (-1);
935                          }                          }
936                  }  #ifdef USE_APP_LEVEL_THREADING
937          }                          fseek(in_file, 0, SEEK_END); /* Determine input size */
938                            pos = ftell(in_file);
939          // This should be after the avi input opening stuff                          ARG_MAXFRAMENR = pos / IMAGE_SIZE(XDIM, YDIM); /* PGM, header size ?? */
940          if (ARG_TIMECODEFILE != NULL) {  #endif
941                  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");  
942                  }                  }
943          }          }
944    
# Line 965  Line 962 
962          if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))          if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))
963                          ARG_CQ = DEFAULT_QUANT;                          ARG_CQ = DEFAULT_QUANT;
964    
965                    /* Init xvidcore */
966        enc_gbl(use_assembler);
967    
968    #ifdef USE_APP_LEVEL_THREADING
969            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0 ||
970                ARG_NUM_APP_THREADS <= 1 || ARG_THREADS != 0 ||
971                ARG_TIMECODEFILE != NULL || ARG_AVIOUTPUTFILE != NULL ||
972                ARG_INPUTTYPE == 1 || ARG_MKVOUTPUTFILE != NULL)            /* TODO: PGM input */
973    #endif /* Spawn just one encoder instance */
974            {
975                    enc_sequence_data_t enc_data;
976                    memset(&enc_data, 0, sizeof(enc_sequence_data_t));
977    
978                    if (!ARG_THREADS) ARG_THREADS = ARG_NUM_APP_THREADS;
979                    ARG_NUM_APP_THREADS = 1;
980    
981                    enc_data.outfilename = ARG_OUTPUTFILE;
982                    enc_data.statsfilename1 = ARG_PASS1;
983                    enc_data.start_num = ARG_STARTFRAMENR;
984                    enc_data.stop_num = ARG_MAXFRAMENR;
985    
986                            /* Encode input */
987                    encode_sequence(&enc_data);
988    
989                            /* Copy back stats */
990                    input_num = enc_data.input_num;
991                    totalsize = enc_data.totalsize;
992                    totalenctime = enc_data.totalenctime;
993                    for (i=0; i < 3; i++) totalPSNR[i] = enc_data.totalPSNR[i];
994                    memcpy(framestats, enc_data.framestats, sizeof(framestats));
995            }
996    #ifdef USE_APP_LEVEL_THREADING
997            else { /* Split input into sequences and create multiple encoder instances */
998                    int k;
999                    void *status;
1000                    FILE *f_out = NULL, *f_stats = NULL;
1001    
1002                    enc_sequence_data_t enc_data[MAX_ENC_INSTANCES];
1003                    char outfile[MAX_ENC_INSTANCES][256];
1004                    char statsfilename[MAX_ENC_INSTANCES][256];
1005    
1006                    for (k = 0; k < MAX_ENC_INSTANCES; k++)
1007                            memset(&enc_data[k], 0, sizeof(enc_sequence_data_t));
1008    
1009                            /* Overwrite internal encoder threading */
1010                    if (ARG_NUM_APP_THREADS > MAX_ENC_INSTANCES) {
1011                            ARG_THREADS = (int) (ARG_NUM_APP_THREADS / MAX_ENC_INSTANCES);
1012                            ARG_NUM_APP_THREADS = MAX_ENC_INSTANCES;
1013                    }
1014                    else
1015                            ARG_THREADS = -1;
1016    
1017                    enc_data[0].outfilename = ARG_OUTPUTFILE;
1018                    enc_data[0].statsfilename1 = ARG_PASS1;
1019                    enc_data[0].start_num = ARG_STARTFRAMENR;
1020                    enc_data[0].stop_num = (ARG_MAXFRAMENR-ARG_STARTFRAMENR)/ARG_NUM_APP_THREADS;
1021    
1022                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1023                            sprintf(outfile[k], "%s.%03d", ARG_OUTPUTFILE, k);
1024                            enc_data[k].outfilename = outfile[k];
1025                            if (ARG_PASS1) {
1026                                    sprintf(statsfilename[k], "%s.%03d", ARG_PASS1, k);
1027                                    enc_data[k].statsfilename1 = statsfilename[k];
1028                            }
1029                            enc_data[k].start_num = (k*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS;
1030                            enc_data[k].stop_num = ((k+1)*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS;
1031                    }
1032    
1033                            /* Start multiple encoder threads in parallel */
1034                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1035                            pthread_create(&enc_data[k].handle, NULL, (void*)encode_sequence, (void*)&enc_data[k]);
1036                    }
1037    
1038                            /* Encode first sequence in this thread */
1039                    encode_sequence(&enc_data[0]);
1040    
1041                            /* Wait until encoder threads have finished */
1042                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1043                            pthread_join(enc_data[k].handle, &status);
1044                    }
1045    
1046                            /* Join encoder stats and encoder output files */
1047                    if (ARG_OUTPUTFILE)
1048                            f_out = fopen(enc_data[0].outfilename, "ab+");
1049                    if (ARG_PASS1)
1050                            f_stats = fopen(enc_data[0].statsfilename1, "ab+");
1051    
1052                    for (k = 0; k < ARG_NUM_APP_THREADS; k++) {
1053                                    /* Join stats */
1054                            input_num += enc_data[k].input_num;
1055                            totalsize += enc_data[k].totalsize;
1056                            totalenctime = MAX(totalenctime, enc_data[k].totalenctime);
1057    
1058                            for (i=0; i < 3; i++) totalPSNR[i] += enc_data[k].totalPSNR[i];
1059                            for (i=0; i < 8; i++) {
1060                                    int l;
1061                                    framestats[i].count += enc_data[k].framestats[i].count;
1062                                    framestats[i].size += enc_data[k].framestats[i].size;
1063                                    for (l=0; l < 32; l++)
1064                                            framestats[i].quants[l] += enc_data[k].framestats[i].quants[l];
1065                            }
1066                                    /* Join output files */
1067                            if ((k > 0) && (f_out != NULL)) {
1068                                    int ch;
1069                                    FILE *f = fopen(enc_data[k].outfilename, "rb");
1070                                    while((ch = fgetc(f)) != EOF) { fputc(ch, f_out); }
1071                                    fclose(f);
1072                                    remove(enc_data[k].outfilename);
1073                            }
1074                                    /* Join first pass stats files */
1075                            if ((k > 0) && (f_stats != NULL)) {
1076                                    char str[256];
1077                                    FILE *f = fopen(enc_data[k].statsfilename1, "r");
1078                                    while(fgets(str, sizeof(str), f) != NULL) {
1079                                            if (str[0] != '#' && strlen(str) > 3)
1080                                                    fputs(str, f_stats);
1081                                    }
1082                                    fclose(f);
1083                                    remove(enc_data[k].statsfilename1);
1084                            }
1085                    }
1086                    if (f_out) fclose(f_out);
1087                    if (f_stats) fclose(f_stats);
1088            }
1089    #endif
1090    
1091    /*****************************************************************************
1092     *         Calculate totals and averages for output, print results
1093     ****************************************************************************/
1094    
1095     printf("\n");
1096            printf("Tot: enctime(ms) =%7.2f,               length(bytes) = %7d\n",
1097                       totalenctime, (int) totalsize);
1098    
1099            if (input_num > 0) {
1100                    totalsize /= input_num;
1101                    totalenctime /= input_num;
1102                    totalPSNR[0] /= input_num;
1103                    totalPSNR[1] /= input_num;
1104                    totalPSNR[2] /= input_num;
1105            } else {
1106                    totalsize = -1;
1107                    totalenctime = -1;
1108            }
1109    
1110            printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d",
1111                       totalenctime, 1000 / totalenctime, (int) totalsize);
1112       if (ARG_STATS) {
1113           printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
1114                      totalPSNR[0],totalPSNR[1],totalPSNR[2]);
1115            }
1116            printf("\n");
1117            if (framestats[XVID_TYPE_IVOP].count) {
1118                    printf("I frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1119                            framestats[XVID_TYPE_IVOP].count, framestats[XVID_TYPE_IVOP].size/framestats[XVID_TYPE_IVOP].count, \
1120                            framestats[XVID_TYPE_IVOP].size, minquant(framestats[XVID_TYPE_IVOP].quants), \
1121                            avgquant(framestats[XVID_TYPE_IVOP]), maxquant(framestats[XVID_TYPE_IVOP].quants));
1122            }
1123            if (framestats[XVID_TYPE_PVOP].count) {
1124                    printf("P frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1125                            framestats[XVID_TYPE_PVOP].count, framestats[XVID_TYPE_PVOP].size/framestats[XVID_TYPE_PVOP].count, \
1126                            framestats[XVID_TYPE_PVOP].size, minquant(framestats[XVID_TYPE_PVOP].quants), \
1127                            avgquant(framestats[XVID_TYPE_PVOP]), maxquant(framestats[XVID_TYPE_PVOP].quants));
1128            }
1129            if (framestats[XVID_TYPE_BVOP].count) {
1130                    printf("B frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1131                            framestats[XVID_TYPE_BVOP].count, framestats[XVID_TYPE_BVOP].size/framestats[XVID_TYPE_BVOP].count, \
1132                            framestats[XVID_TYPE_BVOP].size, minquant(framestats[XVID_TYPE_BVOP].quants), \
1133                            avgquant(framestats[XVID_TYPE_BVOP]), maxquant(framestats[XVID_TYPE_BVOP].quants));
1134            }
1135            if (framestats[XVID_TYPE_SVOP].count) {
1136                    printf("S frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1137                            framestats[XVID_TYPE_SVOP].count, framestats[XVID_TYPE_SVOP].size/framestats[XVID_TYPE_SVOP].count, \
1138                            framestats[XVID_TYPE_SVOP].size, minquant(framestats[XVID_TYPE_SVOP].quants), \
1139                            avgquant(framestats[XVID_TYPE_SVOP]), maxquant(framestats[XVID_TYPE_SVOP].quants));
1140            }
1141            if (framestats[5].count) {
1142                    printf("N frames: %6d frames, size = %7d/%7d\n", \
1143                            framestats[5].count, framestats[5].size/framestats[5].count, \
1144                            framestats[5].size);
1145            }
1146    
1147    
1148    /*****************************************************************************
1149     *                            Xvid PART  Stop
1150     ****************************************************************************/
1151    
1152      release_all:
1153    
1154            return (0);
1155    }
1156    
1157    /*****************************************************************************
1158     *               Encode a sequence
1159     ****************************************************************************/
1160    
1161    void encode_sequence(enc_sequence_data_t *h) {
1162    
1163            /* Internal structures (handles) for encoding */
1164            void *enc_handle = NULL;
1165    
1166            int start_num = h->start_num;
1167            int stop_num = h->stop_num;
1168            char *outfilename = h->outfilename;
1169            float *totalPSNR = h->totalPSNR;
1170    
1171            int input_num;
1172            int totalsize;
1173            double totalenctime = 0.;
1174    
1175            unsigned char *mp4_buffer = NULL;
1176            unsigned char *in_buffer = NULL;
1177            unsigned char *out_buffer = NULL;
1178    
1179            double enctime;
1180    
1181            int result;
1182            int output_num;
1183            int nvop_counter;
1184            int m4v_size;
1185            int key;
1186            int stats_type;
1187            int stats_quant;
1188            int stats_length;
1189            int fakenvop = 0;
1190    
1191            FILE *in_file = stdin;
1192            FILE *out_file = NULL;
1193            FILE *time_file = NULL;
1194    
1195            char filename[256];
1196    
1197    #ifdef XVID_MKV_OUTPUT
1198            PMKVFILE myMKVFile = NULL;
1199            PMKVSTREAM myMKVStream = NULL;
1200            MKVSTREAMINFO myMKVStreamInfo;
1201    #endif
1202    #if defined(XVID_AVI_INPUT)
1203            PAVIFILE avi_in = NULL;
1204            PAVISTREAM avi_in_stream = NULL;
1205            PGETFRAME get_frame = NULL;
1206            BITMAPINFOHEADER myBitmapInfoHeader;
1207    #else
1208    #define get_frame NULL
1209    #endif
1210    #if defined(XVID_AVI_OUTPUT)
1211            int avierr;
1212            PAVIFILE myAVIFile = NULL;
1213            PAVISTREAM myAVIStream = NULL;
1214            AVISTREAMINFO myAVIStreamInfo;
1215    #endif
1216    #if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT)
1217            if (ARG_NUM_APP_THREADS > 1)
1218                    CoInitializeEx(0, COINIT_MULTITHREADED);
1219            AVIFileInit();
1220    #endif
1221    
1222            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
1223                    in_file = stdin;
1224            } else {
1225    #ifdef XVID_AVI_INPUT
1226          if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
1227              strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
1228                      ARG_INPUTTYPE==2)
1229          {
1230                      AVISTREAMINFO avi_info;
1231                      FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
1232    
1233                      if (avi_fp == NULL) {
1234                              fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
1235                              return;
1236                      }
1237                      fclose(avi_fp);
1238    
1239                      if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
1240                              fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
1241                              AVIFileExit();
1242                              return;
1243                      }
1244    
1245                      if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
1246                              fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
1247                              AVIFileRelease(avi_in);
1248                              AVIFileExit();
1249                              return;
1250                      }
1251    
1252                      AVIFileRelease(avi_in);
1253    
1254                      if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
1255                              fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE);
1256                              AVIStreamRelease(avi_in_stream);
1257                              AVIFileExit();
1258                              return;
1259                      }
1260    
1261                  if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) {
1262                              LONG size;
1263                              fprintf(stderr, "Non YV12 input colorspace %c%c%c%c! Attempting conversion...\n",
1264                                      avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256,
1265                                      (avi_info.fccHandler>>24)%256);
1266                              size = sizeof(myBitmapInfoHeader);
1267                              AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size);
1268                              if (size==0)
1269                                      fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
1270                              else {
1271                                      fprintf(stderr, "AVIStreamReadFormat read %d bytes.\n", size);
1272                                      fprintf(stderr, "width = %d, height = %d, planes = %d\n", myBitmapInfoHeader.biWidth,
1273                                              myBitmapInfoHeader.biHeight, myBitmapInfoHeader.biPlanes);
1274                                      fprintf(stderr, "Compression = %c%c%c%c, %d\n",
1275                                              myBitmapInfoHeader.biCompression%256, (myBitmapInfoHeader.biCompression>>8)%256,
1276                                              (myBitmapInfoHeader.biCompression>>16)%256, (myBitmapInfoHeader.biCompression>>24)%256,
1277                                              myBitmapInfoHeader.biCompression);
1278                                      fprintf(stderr, "Bits Per Pixel = %d\n", myBitmapInfoHeader.biBitCount);
1279                                      myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
1280                                      myBitmapInfoHeader.biBitCount = 12;
1281                                      myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
1282                                      get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader);
1283                              }
1284                              if (get_frame == NULL) {
1285                                    AVIStreamRelease(avi_in_stream);
1286                                    AVIFileExit();
1287                                    return;
1288                              }
1289                              else {
1290                                    unsigned char *temp;
1291                                    fprintf(stderr, "AVIStreamGetFrameOpen successful.\n");
1292                                    temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);
1293                                    if (temp != NULL) {
1294                                            int i;
1295                                            for (i = 0; i < ((DWORD*)temp)[0]; i++) {
1296                                                    fprintf(stderr, "%2d ", temp[i]);
1297                                            }
1298                                            fprintf(stderr, "\n");
1299                                    }
1300                              }
1301                              if (avi_info.fccHandler == MAKEFOURCC('D', 'I', 'B', ' ')) {
1302                                      AVIStreamGetFrameClose(get_frame);
1303                                      get_frame = NULL;
1304                                      ARG_COLORSPACE = XVID_CSP_BGR | XVID_CSP_VFLIP;
1305                              }
1306                      }
1307        }
1308        else
1309    #endif
1310                    {
1311                            in_file = fopen(ARG_INPUTFILE, "rb");
1312                            if (in_file == NULL) {
1313                                    fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
1314                                    return;
1315                            }
1316                    }
1317            }
1318    
1319            // This should be after the avi input opening stuff
1320            if (ARG_TIMECODEFILE != NULL) {
1321                    time_file = fopen(ARG_TIMECODEFILE, "r");
1322                    if (time_file==NULL) {
1323                            fprintf(stderr, "Couldn't open timecode file '%s'!\n", ARG_TIMECODEFILE);
1324                            return;
1325                    }
1326                    else {
1327                            fscanf(time_file, "# timecode format v2\n");
1328                    }
1329            }
1330    
1331          if (ARG_INPUTTYPE==1) {          if (ARG_INPUTTYPE==1) {
1332  #ifndef READ_PNM  #ifndef READ_PNM
1333                  if (read_pgmheader(in_file)) {                  if (read_pgmheader(in_file)) {
# Line 973  Line 1336 
1336  #endif  #endif
1337                          fprintf(stderr,                          fprintf(stderr,
1338                                          "Wrong input format, I want YUV encapsulated in PGM\n");                                          "Wrong input format, I want YUV encapsulated in PGM\n");
1339                          return (-1);                          return;
1340                  }                  }
1341          }          }
1342    
1343          /* Jump to the starting frame */          /* Jump to the starting frame */
1344          if (ARG_INPUTTYPE == 0)          if (ARG_INPUTTYPE == 0) /* TODO: Other input formats ??? */
1345                  fseek(in_file, ARG_STARTFRAMENR*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);                  fseek(in_file, start_num*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);
1346    
1347    
1348          /* now we know the sizes, so allocate memory */          /* now we know the sizes, so allocate memory */
1349          if (get_frame == NULL)          if (get_frame == NULL)
# Line 995  Line 1359 
1359                  goto free_all_memory;                  goto free_all_memory;
1360    
1361  /*****************************************************************************  /*****************************************************************************
1362   *                            XviD PART  Start   *                            Xvid PART  Start
1363   ****************************************************************************/   ****************************************************************************/
1364    
1365    
1366          result = enc_init(use_assembler);          result = enc_init(&enc_handle, h->statsfilename1, h->start_num);
1367          if (result) {          if (result) {
1368                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);
1369                  goto release_all;                  goto release_all;
# Line 1011  Line 1375 
1375    
1376          if (ARG_SAVEMPEGSTREAM) {          if (ARG_SAVEMPEGSTREAM) {
1377    
1378                  if (ARG_OUTPUTFILE) {                  if (outfilename) {
1379                          if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {                          if ((out_file = fopen(outfilename, "w+b")) == NULL) {
1380                                  fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);                                  fprintf(stderr, "Error opening output file %s\n", outfilename);
1381                                  goto release_all;                                  goto release_all;
1382                          }                          }
1383                  }                  }
# Line 1109  Line 1473 
1473          result = 0;          result = 0;
1474    
1475          input_num = 0;                          /* input frame counter */          input_num = 0;                          /* input frame counter */
1476          output_num = 0;                         /* output frame counter */          output_num = start_num;             /* output frame counter */
1477    
1478          nvop_counter = 0;          nvop_counter = 0;
1479    
# Line 1118  Line 1482 
1482                  char *type;                  char *type;
1483                  int sse[3];                  int sse[3];
1484    
1485                  if (input_num >= ARG_MAXFRAMENR && ARG_MAXFRAMENR > 0) {                  if ((input_num+start_num) >= stop_num && stop_num > 0) {
1486                          result = 1;                          result = 1;
1487                  }                  }
1488    
# Line 1127  Line 1491 
1491                          if (ARG_INPUTTYPE==2) {                          if (ARG_INPUTTYPE==2) {
1492                                  /* read avs/avi data (YUV-format) */                                  /* read avs/avi data (YUV-format) */
1493                                  if (get_frame != NULL) {                                  if (get_frame != NULL) {
1494                                          in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+ARG_STARTFRAMENR);                                          in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+start_num);
1495                                          if (in_buffer == NULL)                                          if (in_buffer == NULL)
1496                                                  result = 1;                                                  result = 1;
1497                                          else                                          else
1498                                                  in_buffer += ((DWORD*)in_buffer)[0];                                                  in_buffer += ((DWORD*)in_buffer)[0];
1499                                  } else {                                  } else {
1500                                          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)
1501                                                  result = 1;                                                  result = 1;
1502                                  }                                  }
1503                          } else                          } else
# Line 1155  Line 1519 
1519   *                       Encode and decode this frame   *                       Encode and decode this frame
1520   ****************************************************************************/   ****************************************************************************/
1521    
1522                  if (input_num >= (unsigned int)ARG_MAXFRAMENR-1 && ARG_MAXBFRAMES) {                  if ((input_num+start_num) >= (unsigned int)(stop_num-1) && ARG_MAXBFRAMES) {
1523                          stats_type = XVID_TYPE_PVOP;                          stats_type = XVID_TYPE_PVOP;
1524                  }                  }
1525                  else                  else
# Line 1163  Line 1527 
1527    
1528                  enctime = msecond();                  enctime = msecond();
1529                  m4v_size =                  m4v_size =
1530                          enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type,                          enc_main(enc_handle, !result ? in_buffer : 0, mp4_buffer, &key, &stats_type,
1531                                           &stats_quant, &stats_length, sse, input_num);                                           &stats_quant, &stats_length, sse, input_num);
1532                  enctime = msecond() - enctime;                  enctime = msecond() - enctime;
1533    
# Line 1191  Line 1555 
1555                          }                          }
1556    
1557                          if (stats_length > 8) {                          if (stats_length > 8) {
1558                                  framestats[stats_type].count++;                                  h->framestats[stats_type].count++;
1559                                  framestats[stats_type].quants[stats_quant]++;                                  h->framestats[stats_type].quants[stats_quant]++;
1560                                  framestats[stats_type].size += stats_length;                                  h->framestats[stats_type].size += stats_length;
1561                          }                          }
1562                          else {                          else {
1563                                  framestats[5].count++;                                  h->framestats[5].count++;
1564                                  framestats[5].quants[stats_quant]++;                                  h->framestats[5].quants[stats_quant]++;
1565                                  framestats[5].size += stats_length;                                  h->framestats[5].size += stats_length;
1566                          }                          }
1567    
1568  #define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height)))))  #define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height)))))
1569    
1570                          if (ARG_PROGRESS == 0) {                          if (ARG_PROGRESS == 0) {
1571                                  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,
1572                                          key, (float) enctime, (int) m4v_size);                                          key, (float) enctime, (int) m4v_size);
1573                                  printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,                                  printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,
1574                                     stats_length);                                     stats_length);
# Line 1217  Line 1581 
1581                                  }                                  }
1582                                  printf("\n");                                  printf("\n");
1583                          } else {                          } else {
1584                                  if (input_num % ARG_PROGRESS == 1) {                                  if ((input_num) % ARG_PROGRESS == 1) {
1585                                          if (ARG_MAXFRAMENR > 0) {                                          if (stop_num > 0) {
1586                                                  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", \
1587                                                          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), \
1588                                                          (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);                                                          ((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num));
1589                                          } else {                                          } else {
1590                                                  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", \
1591                                                          input_num, input_num*1000/totalenctime, \                                                          (ARG_NUM_APP_THREADS*input_num), (ARG_NUM_APP_THREADS*input_num)*1000/(totalenctime), \
1592                                                          (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);                                                          ((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num));
1593                                          }                                          }
1594                                  }                                  }
1595                          }                          }
# Line 1289  Line 1653 
1653                                          removedivxp((char*)mp4_buffer, m4v_size);                                          removedivxp((char*)mp4_buffer, m4v_size);
1654    
1655                                  /* Save ES stream */                                  /* Save ES stream */
1656                                  if (ARG_OUTPUTFILE && out_file && !(fakenvop && m4v_size <= 8)) {                                  if (outfilename && out_file && !(fakenvop && m4v_size <= 8)) {
1657                                                  fwrite(mp4_buffer, 1, m4v_size, out_file);                                                  fwrite(mp4_buffer, 1, m4v_size, out_file);
1658                                  }                                  }
1659  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
# Line 1303  Line 1667 
1667                                  fakenvop=0;                                  fakenvop=0;
1668                  }                  }
1669    
1670                  input_num++;                  if (!result)
1671                            (input_num)++;
1672    
1673                  /* Read the header if it's pgm stream */                  /* Read the header if it's pgm stream */
1674                  if (!result && (ARG_INPUTTYPE==1))                  if (!result && (ARG_INPUTTYPE==1))
# Line 1315  Line 1680 
1680          } while (1);          } while (1);
1681    
1682    
   
 /*****************************************************************************  
  *         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  
  ****************************************************************************/  
   
1683    release_all:    release_all:
1684    
1685      h->input_num = input_num;
1686      h->totalenctime = totalenctime;
1687      h->totalsize = totalsize;
1688    
1689  #ifdef XVID_AVI_INPUT  #ifdef XVID_AVI_INPUT
1690          if (get_frame) AVIStreamGetFrameClose(get_frame);          if (get_frame) AVIStreamGetFrameClose(get_frame);
1691          if (avi_stream) AVIStreamRelease(avi_stream);          if (avi_in_stream) AVIStreamRelease(avi_in_stream);
 #ifndef XVID_AVI_OUTPUT  
         AVIFileExit();  
 #endif  
1692  #endif  #endif
1693    
1694          if (enc_handle) {          if (enc_handle) {
1695                  result = enc_stop();                  result = enc_stop(enc_handle);
1696                  if (result)                  if (result)
1697                          fprintf(stderr, "Encore RELEASE problem return value %d\n",                          fprintf(stderr, "Encore RELEASE problem return value %d\n",
1698                                          result);                                          result);
# Line 1398  Line 1702 
1702                  fclose(in_file);                  fclose(in_file);
1703          if (out_file)          if (out_file)
1704                  fclose(out_file);                  fclose(out_file);
1705            if (time_file)
1706                    fclose(time_file);
1707    
1708  #ifdef XVID_AVI_OUTPUT  #ifdef XVID_AVI_OUTPUT
1709          if (myAVIStream) AVIStreamRelease(myAVIStream);          if (myAVIStream) AVIStreamRelease(myAVIStream);
1710          if (myAVIFile) AVIFileRelease(myAVIFile);          if (myAVIFile) AVIFileRelease(myAVIFile);
         AVIFileExit();  
1711  #endif  #endif
1712  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
1713          if (myMKVStream) MKVStreamRelease(myMKVStream);          if (myMKVStream) MKVStreamRelease(myMKVStream);
1714          if (myMKVFile) MKVFileRelease(myMKVFile);          if (myMKVFile) MKVFileRelease(myMKVFile);
1715  #endif  #endif
1716    #if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT)
1717            AVIFileExit();
1718    #endif
1719    
1720    free_all_memory:    free_all_memory:
1721          free(out_buffer);          free(out_buffer);
1722          free(mp4_buffer);          free(mp4_buffer);
1723          free(in_buffer);          free(in_buffer);
   
         return (0);  
   
1724  }  }
1725    
   
1726  /*****************************************************************************  /*****************************************************************************
1727   *                        "statistical" functions   *                        "statistical" functions
1728   *   *
1729   *  these are not needed for encoding or decoding, but for measuring   *  these are not needed for encoding or decoding, but for measuring
1730   *  time and quality, there in nothing specific to XviD in these   *  time and quality, there in nothing specific to Xvid in these
1731   *   *
1732   *****************************************************************************/   *****************************************************************************/
1733    
# Line 1509  Line 1813 
1813  #endif  #endif
1814          fprintf(stderr, " -w      integer: frame width ([1.2048])\n");          fprintf(stderr, " -w      integer: frame width ([1.2048])\n");
1815          fprintf(stderr, " -h      integer: frame height ([1.2048])\n");          fprintf(stderr, " -h      integer: frame height ([1.2048])\n");
1816            fprintf(stderr, " -csp    string : colorspace of raw input file i420, yv12 (default)\n");
1817          fprintf(stderr, " -frames integer: number of frames to encode\n");          fprintf(stderr, " -frames integer: number of frames to encode\n");
1818          fprintf(stderr, "\n");          fprintf(stderr, "\n");
1819          fprintf(stderr, "Output options:\n");          fprintf(stderr, "Output options:\n");
# Line 1552  Line 1857 
1857          fprintf(stderr, "-odegrade   integer             : max overflow degradation (5)\n");          fprintf(stderr, "-odegrade   integer             : max overflow degradation (5)\n");
1858          fprintf(stderr, "-chigh      integer             : high bitrate scenes degradation (0)\n");          fprintf(stderr, "-chigh      integer             : high bitrate scenes degradation (0)\n");
1859          fprintf(stderr, "-clow       integer             : low bitrate scenes improvement (0)\n");          fprintf(stderr, "-clow       integer             : low bitrate scenes improvement (0)\n");
1860          fprintf(stderr, "-overhead   integer             : container frame overhead (24)\n");          fprintf(stderr, "-overhead   integer             : container frame overhead (0)\n");
1861          fprintf(stderr, "-vbvsize    integer             : use vbv buffer size\n");          fprintf(stderr, "-vbvsize    integer             : use vbv buffer size\n");
1862          fprintf(stderr, "-vbvmax     integer             : vbv max bitrate\n");          fprintf(stderr, "-vbvmax     integer             : vbv max bitrate\n");
1863          fprintf(stderr, "-vbvpeak    integer             : vbv peak bitrate over 1 second\n");          fprintf(stderr, "-vbvpeak    integer             : vbv peak bitrate over 1 second\n");
# Line 1570  Line 1875 
1875          fprintf(stderr, " -interlaced [integer]          : interlaced encoding (BFF:1, TFF:2) (1)\n");          fprintf(stderr, " -interlaced [integer]          : interlaced encoding (BFF:1, TFF:2) (1)\n");
1876          fprintf(stderr, " -nopacked                      : Disable packed mode\n");          fprintf(stderr, " -nopacked                      : Disable packed mode\n");
1877          fprintf(stderr, " -noclosed_gop                  : Disable closed GOP mode\n");          fprintf(stderr, " -noclosed_gop                  : Disable closed GOP mode\n");
1878          fprintf(stderr, " -lumimasking                   : use lumimasking algorithm\n");          fprintf(stderr, " -masking [integer]             : HVS masking mode (None:0, Lumi:1, Variance:2) (0)\n");
1879          fprintf(stderr, " -stats                         : print stats about encoded frames\n");          fprintf(stderr, " -stats                         : print stats about encoded frames\n");
1880          fprintf(stderr, " -ssim [integer]                : prints ssim for every frame (accurate: 0 fast: 4) (2)\n");          fprintf(stderr, " -ssim [integer]                : prints ssim for every frame (accurate: 0 fast: 4) (2)\n");
1881          fprintf(stderr, " -ssim_file filename            : outputs the ssim stats into a file\n");          fprintf(stderr, " -ssim_file filename            : outputs the ssim stats into a file\n");
1882            fprintf(stderr, " -psnrhvsm                      : prints psnr-hvs-m metric for every frame\n");
1883          fprintf(stderr, " -debug                         : activates xvidcore internal debugging output\n");          fprintf(stderr, " -debug                         : activates xvidcore internal debugging output\n");
1884          fprintf(stderr, " -vop_debug                     : print some info directly into encoded frames\n");          fprintf(stderr, " -vop_debug                     : print some info directly into encoded frames\n");
1885          fprintf(stderr, " -nochromame                    : Disable chroma motion estimation\n");          fprintf(stderr, " -nochromame                    : Disable chroma motion estimation\n");
# Line 1604  Line 1910 
1910   *                       Input and output functions   *                       Input and output functions
1911   *   *
1912   *      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
1913   *      image. It's just for convenience, again nothing specific to XviD   *      image. It's just for convenience, again nothing specific to Xvid
1914   *   *
1915   *****************************************************************************/   *****************************************************************************/
1916    
# Line 1766  Line 2072 
2072    
2073  #define FRAMERATE_INCR 1001  #define FRAMERATE_INCR 1001
2074    
2075    /* Gobal encoder init, once per process */
2076  /* Initialize encoder for first use, pass all needed parameters to the codec */  void
2077  static int  enc_gbl(int use_assembler)
 enc_init(int use_assembler)  
2078  {  {
         int xerr;  
         //xvid_plugin_cbr_t cbr;  
     xvid_plugin_single_t single;  
         xvid_plugin_2pass1_t rc2pass1;  
         xvid_plugin_2pass2_t rc2pass2;  
         xvid_plugin_ssim_t ssim;  
         //xvid_plugin_fixed_t rcfixed;  
         xvid_enc_plugin_t plugins[8];  
2079          xvid_gbl_init_t xvid_gbl_init;          xvid_gbl_init_t xvid_gbl_init;
         xvid_enc_create_t xvid_enc_create;  
         int i;  
2080    
2081          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
2082           * XviD core initialization           * Xvid core initialization
2083           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
2084    
2085          /* Set version -- version checking will done by xvidcore */          /* Set version -- version checking will done by xvidcore */
# Line 1805  Line 2100 
2100                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
2101          }          }
2102    
2103          /* Initialize XviD core -- Should be done once per __process__ */          /* Initialize Xvid core -- Should be done once per __process__ */
2104          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
2105        ARG_CPU_FLAGS = xvid_gbl_init.cpu_flags;
2106          enc_info();          enc_info();
2107    }
2108    
2109    /* Initialize encoder for first use, pass all needed parameters to the codec */
2110    static int
2111    enc_init(void **enc_handle, char *stats_pass1, int start_num)
2112    {
2113            int xerr;
2114            //xvid_plugin_cbr_t cbr;
2115        xvid_plugin_single_t single;
2116            xvid_plugin_2pass1_t rc2pass1;
2117            xvid_plugin_2pass2_t rc2pass2;
2118            xvid_plugin_ssim_t ssim;
2119        xvid_plugin_lumimasking_t masking;
2120            //xvid_plugin_fixed_t rcfixed;
2121            xvid_enc_plugin_t plugins[8];
2122            xvid_enc_create_t xvid_enc_create;
2123            int i;
2124    
2125          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
2126           * XviD encoder initialization           * Xvid encoder initialization
2127           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
2128    
2129          /* Version again */          /* Version again */
# Line 1873  Line 2186 
2186                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2187          }          }
2188    
2189          if (ARG_PASS1) {          if (stats_pass1) {
2190                  memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));                  memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));
2191                  rc2pass1.version = XVID_VERSION;                  rc2pass1.version = XVID_VERSION;
2192                  rc2pass1.filename = ARG_PASS1;                  rc2pass1.filename = stats_pass1;
2193                  if (ARG_FULL1PASS)                  if (ARG_FULL1PASS)
2194                          prepare_full1pass_zones();                          prepare_full1pass_zones();
2195                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;
# Line 1896  Line 2209 
2209    
2210    
2211          if (ARG_LUMIMASKING) {          if (ARG_LUMIMASKING) {
2212                    memset(&masking, 0, sizeof(xvid_plugin_lumimasking_t));
2213                    masking.method = (ARG_LUMIMASKING==2);
2214                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
2215                  plugins[xvid_enc_create.num_plugins].param = NULL;                  plugins[xvid_enc_create.num_plugins].param = &masking;
2216                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2217          }          }
2218    
# Line 1908  Line 2223 
2223          }          }
2224    
2225          if (ARG_SSIM>=0 || ARG_SSIM_PATH != NULL) {          if (ARG_SSIM>=0 || ARG_SSIM_PATH != NULL) {
2226            memset(&ssim, 0, sizeof(xvid_plugin_ssim_t));
2227    
2228                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim;
2229    
2230                    if( ARG_SSIM >=0){
2231                  ssim.b_printstat = 1;                  ssim.b_printstat = 1;
2232                            ssim.acc = ARG_SSIM;
2233                    } else {
2234                            ssim.b_printstat = 0;
2235                            ssim.acc = 2;
2236                    }
2237    
2238                    if(ARG_SSIM_PATH != NULL){
2239                  ssim.stat_path = ARG_SSIM_PATH;                  ssim.stat_path = ARG_SSIM_PATH;
2240                    }
2241    
2242            ssim.cpu_flags = ARG_CPU_FLAGS;
2243                  ssim.b_visualize = 0;                  ssim.b_visualize = 0;
                 ssim.acc = (ARG_SSIM_PATH != NULL && ARG_SSIM < 0) ? 2 : ARG_SSIM;  
2244                  plugins[xvid_enc_create.num_plugins].param = &ssim;                  plugins[xvid_enc_create.num_plugins].param = &ssim;
2245                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2246          }          }
2247    
2248            if (ARG_PSNRHVSM>0) {
2249            plugins[xvid_enc_create.num_plugins].func = xvid_plugin_psnrhvsm;
2250                    plugins[xvid_enc_create.num_plugins].param = NULL;
2251                    xvid_enc_create.num_plugins++;
2252            }
2253    
2254  #if 0  #if 0
2255          if (ARG_DEBUG) {          if (ARG_DEBUG) {
2256                  plugins[xvid_enc_create.num_plugins].func = rawenc_debug;                  plugins[xvid_enc_create.num_plugins].func = rawenc_debug;
# Line 1953  Line 2287 
2287          /* Frame drop ratio */          /* Frame drop ratio */
2288          xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;          xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;
2289    
2290            /* Start frame number */
2291            xvid_enc_create.start_frame_num = start_num;
2292    
2293          /* Global encoder options */          /* Global encoder options */
2294          xvid_enc_create.global = 0;          xvid_enc_create.global = 0;
2295    
# Line 1969  Line 2306 
2306          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
2307    
2308          /* Retrieve the encoder instance from the structure */          /* Retrieve the encoder instance from the structure */
2309          enc_handle = xvid_enc_create.handle;          *enc_handle = xvid_enc_create.handle;
2310    
2311          free(xvid_enc_create.zones);          free(xvid_enc_create.zones);
2312    
# Line 2000  Line 2337 
2337                  fprintf(stderr, "SSE ");                  fprintf(stderr, "SSE ");
2338          if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2)          if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2)
2339                  fprintf(stderr, "SSE2 ");                  fprintf(stderr, "SSE2 ");
2340            if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE3)
2341                    fprintf(stderr, "SSE3 ");
2342            if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE41)
2343                    fprintf(stderr, "SSE41 ");
2344          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW)          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW)
2345                  fprintf(stderr, "3DNOW ");                  fprintf(stderr, "3DNOW ");
2346          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT)          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT)
# Line 2008  Line 2349 
2349                  fprintf(stderr, "TSC ");                  fprintf(stderr, "TSC ");
2350          fprintf(stderr, "\n");          fprintf(stderr, "\n");
2351          fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);          fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);
2352          if (!ARG_THREADS)          ARG_NUM_APP_THREADS = xvid_gbl_info.num_threads;
2353                  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);  
2354          return ret;          return ret;
2355  }  }
2356    
2357  static int  static int
2358  enc_stop()  enc_stop(void *enc_handle)
2359  {  {
2360          int xerr;          int xerr;
2361    
# Line 2026  Line 2366 
2366  }  }
2367    
2368  static int  static int
2369  enc_main(unsigned char *image,  enc_main(void *enc_handle,
2370                     unsigned char *image,
2371                   unsigned char *bitstream,                   unsigned char *bitstream,
2372                   int *key,                   int *key,
2373                   int *stats_type,                   int *stats_type,
# Line 2055  Line 2396 
2396          if (image) {          if (image) {
2397                  xvid_enc_frame.input.plane[0] = image;                  xvid_enc_frame.input.plane[0] = image;
2398  #ifndef READ_PNM  #ifndef READ_PNM
                 if (ARG_INPUTTYPE==2)  
2399                          xvid_enc_frame.input.csp = ARG_COLORSPACE;                          xvid_enc_frame.input.csp = ARG_COLORSPACE;
                 else  
                         xvid_enc_frame.input.csp = XVID_CSP_I420;  
2400                  xvid_enc_frame.input.stride[0] = XDIM;                  xvid_enc_frame.input.stride[0] = XDIM;
2401  #else  #else
2402                  xvid_enc_frame.input.csp = XVID_CSP_BGR;                  xvid_enc_frame.input.csp = XVID_CSP_BGR;

Legend:
Removed from v.1757  
changed lines
  Added in v.1894

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