[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 1886, Thu Apr 1 12:16:48 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.42 2010-04-01 12:16:48 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  char* ARG_SSIM_PATH = NULL;  static  char* ARG_SSIM_PATH = NULL;
# Line 187  Line 210 
210  static  int ARG_SAVEMPEGSTREAM = 0;  static  int ARG_SAVEMPEGSTREAM = 0;
211  static  int ARG_SAVEINDIVIDUAL = 0;  static  int ARG_SAVEINDIVIDUAL = 0;
212  static  char *ARG_OUTPUTFILE = NULL;  static  char *ARG_OUTPUTFILE = NULL;
 #ifdef XVID_AVI_OUTPUT  
213  static  char *ARG_AVIOUTPUTFILE = NULL;  static  char *ARG_AVIOUTPUTFILE = NULL;
 #endif  
 #ifdef XVID_MKV_OUTPUT  
214  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  
215  static  char *ARG_TIMECODEFILE = NULL;  static  char *ARG_TIMECODEFILE = NULL;
216  static  int XDIM = 0;  static  int XDIM = 0;
217  static  int YDIM = 0;  static  int YDIM = 0;
# Line 249  Line 260 
260  static  int ARG_COLORSPACE = XVID_CSP_YV12;  static  int ARG_COLORSPACE = XVID_CSP_YV12;
261          /* the path where to save output */          /* the path where to save output */
262  static char filepath[256] = "./";  static char filepath[256] = "./";
263          /* Internal structures (handles) for encoding and decoding */  
 static  void *enc_handle = NULL;  
264  static  unsigned char qmatrix_intra[64];  static  unsigned char qmatrix_intra[64];
265  static  unsigned char qmatrix_inter[64];  static  unsigned char qmatrix_inter[64];
266    
# Line 292  Line 302 
302                                                  unsigned char *image);                                                  unsigned char *image);
303    
304  /* Encoder related functions */  /* Encoder related functions */
305  static int enc_init(int use_assembler);  static void enc_gbl(int use_assembler);
306    static int  enc_init(void **enc_handle, char *stats_pass1, int start_num);
307  static int enc_info();  static int enc_info();
308  static int enc_stop();  static int  enc_stop(void *enc_handle);
309  static int enc_main(unsigned char *image,  static int  enc_main(void *enc_handle,
310                                             unsigned char *image,
311                                          unsigned char *bitstream,                                          unsigned char *bitstream,
312                                          int *key,                                          int *key,
313                                          int *stats_type,                                          int *stats_type,
# Line 303  Line 315 
315                                          int *stats_length,                                          int *stats_length,
316                                          int stats[3],                                          int stats[3],
317                                          int framenum);                                          int framenum);
318    static void encode_sequence(enc_sequence_data_t *h);
319    
320  /* Zone Related Functions */  /* Zone Related Functions */
321  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 334 
334  main(int argc,  main(int argc,
335           char *argv[])           char *argv[])
336  {  {
   
         unsigned char *mp4_buffer = NULL;  
         unsigned char *in_buffer = NULL;  
         unsigned char *out_buffer = NULL;  
   
         double enctime;  
337          double totalenctime = 0.;          double totalenctime = 0.;
338          float totalPSNR[3] = {0., 0., 0.};          float totalPSNR[3] = {0., 0., 0.};
339    
340          FILE *statsfile;          FILE *statsfile;
341            frame_stats_t framestats[7];
342    
343          int totalsize;          int input_num = 0;
344          int result;          int totalsize = 0;
         int m4v_size;  
         int key;  
         int stats_type;  
         int stats_quant;  
         int stats_length;  
345          int use_assembler = 1;          int use_assembler = 1;
         int fakenvop = 0;  
346          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  
347    
348          printf("xvid_encraw - raw mpeg4 bitstream encoder ");          printf("xvid_encraw - raw mpeg4 bitstream encoder ");
349          printf("written by Christoph Lampert 2002-2003\n\n");          printf("written by Christoph Lampert\n\n");
350    
351          /* Is there a dumb XviD coder ? */          /* Is there a dumb Xvid coder ? */
352          if(ME_ELEMENTS != VOP_ELEMENTS) {          if(ME_ELEMENTS != VOP_ELEMENTS) {
353                  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");
354                  return(-1);                  return(-1);
355          }          }
356    
357            /* Clear framestats */
358            memset(framestats, 0, sizeof(framestats));
359    
360  /*****************************************************************************  /*****************************************************************************
361   *                            Command line parsing   *                            Command line parsing
362   ****************************************************************************/   ****************************************************************************/
# Line 391  Line 373 
373                  } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) {
374                          i++;                          i++;
375                          YDIM = atoi(argv[i]);                          YDIM = atoi(argv[i]);
376                    } else if (strcmp("-csp",argv[i]) == 0 && i < argc - 1) {
377                            i++;
378                            if (strcmp(argv[i],"i420") == 0){
379                                    ARG_COLORSPACE = XVID_CSP_I420;
380                            } else if(strcmp(argv[i],"yv12") == 0){
381                                    ARG_COLORSPACE = XVID_CSP_YV12;
382                            } else {
383                                    printf("Invalid colorspace\n");
384                                    return 0;
385                            }
386          } else if (strcmp("-bitrate", argv[i]) == 0) {          } else if (strcmp("-bitrate", argv[i]) == 0) {
387                          if (i < argc - 1)                          if (i < argc - 1)
388                                  ARG_BITRATE = atoi(argv[i+1]);                                  ARG_BITRATE = atoi(argv[i+1]);
# Line 594  Line 586 
586                          ARG_TIMECODEFILE = argv[i];                          ARG_TIMECODEFILE = argv[i];
587                  } else if (strcmp("-dump", argv[i]) == 0) {                  } else if (strcmp("-dump", argv[i]) == 0) {
588                          ARG_DUMP = 1;                          ARG_DUMP = 1;
589                  } else if (strcmp("-lumimasking", argv[i]) == 0) {                  } else if (strcmp("-masking", argv[i]) == 0 && i < argc -1) {
590                          ARG_LUMIMASKING = 1;                          i++;
591                            ARG_LUMIMASKING = atoi(argv[i]);
592                  } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {
593                          i++;                          i++;
594                          ARG_INPUTTYPE = atoi(argv[i]);                          ARG_INPUTTYPE = atoi(argv[i]);
# Line 819  Line 812 
812                  return (-1);                  return (-1);
813          }          }
814    
815          if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if (ARG_INPUTFILE != NULL) {
816                  in_file = stdin;  #if defined(XVID_AVI_INPUT)
         } else {  
 #ifdef XVID_AVI_INPUT  
817        if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||        if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
818            strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||            strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
819                    ARG_INPUTTYPE==2)                    ARG_INPUTTYPE==2)
820        {        {
821                      PAVIFILE avi_in = NULL;
822                      PAVISTREAM avi_in_stream = NULL;
823                      PGETFRAME get_frame = NULL;
824                      BITMAPINFOHEADER myBitmapInfoHeader;
825                    AVISTREAMINFO avi_info;                    AVISTREAMINFO avi_info;
 #ifndef XVID_AVI_OUTPUT  
                   AVIFileInit();  
 #endif  
826                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
827    
828                      AVIFileInit();
829    
830                    if (avi_fp == NULL) {                    if (avi_fp == NULL) {
831                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
832                            return (-1);                            return (-1);
833                    }                    }
834                    fclose(avi_fp);                    fclose(avi_fp);
835    
836                    if (AVIFileOpen(&avi_file, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {                    if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
837                            fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
838                            AVIFileExit();                            AVIFileExit();
839                            return(-1);                            return(-1);
840                    }                    }
841    
842                    if (AVIFileGetStream(avi_file, &avi_stream, streamtypeVIDEO, 0) != AVIERR_OK) {                    if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
843                            fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
844                            AVIFileRelease(avi_file);                            AVIFileRelease(avi_in);
845                            AVIFileExit();                            AVIFileExit();
846                            return (-1);                            return (-1);
847                    }                    }
848    
849                    AVIFileRelease(avi_file);                    AVIFileRelease(avi_in);
850    
851                    if(AVIStreamInfo(avi_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {                    if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
852                            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);
853                            AVIStreamRelease(avi_stream);                            AVIStreamRelease(avi_in_stream);
854                            AVIFileExit();                            AVIFileExit();
855                            return (-1);                            return (-1);
856                    }                    }
# Line 866  Line 861 
861                                    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,
862                                    (avi_info.fccHandler>>24)%256);                                    (avi_info.fccHandler>>24)%256);
863                            size = sizeof(myBitmapInfoHeader);                            size = sizeof(myBitmapInfoHeader);
864                            AVIStreamReadFormat(avi_stream, 0, &myBitmapInfoHeader, &size);                            AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size);
865                            if (size==0)                            if (size==0)
866                                    fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");                                    fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
867                            else {                            else {
# Line 881  Line 876 
876                                    myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');                                    myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
877                                    myBitmapInfoHeader.biBitCount = 12;                                    myBitmapInfoHeader.biBitCount = 12;
878                                    myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;                                    myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
879                                    get_frame = AVIStreamGetFrameOpen(avi_stream, &myBitmapInfoHeader);                                    get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader);
880                            }                            }
881                            if (get_frame == NULL) {                            if (get_frame == NULL) {
882                                  AVIStreamRelease(avi_stream);                                  AVIStreamRelease(avi_in_stream);
883                                  AVIFileExit();                                  AVIFileExit();
884                                  return (-1);                                  return (-1);
885                            }                            }
# Line 921  Line 916 
916                    }                    }
917    
918                    ARG_INPUTTYPE = 2;                    ARG_INPUTTYPE = 2;
919    
920                      if (get_frame) AVIStreamGetFrameClose(get_frame);
921                      if (avi_in_stream) AVIStreamRelease(avi_in_stream);
922                      AVIFileExit();
923      }      }
924      else      else
925  #endif  #endif
926                  {                  {
927                          in_file = fopen(ARG_INPUTFILE, "rb");                          FILE *in_file = fopen(ARG_INPUTFILE, "rb");
928                            int pos = 0;
929                          if (in_file == NULL) {                          if (in_file == NULL) {
930                                  fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);                                  fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
931                                  return (-1);                                  return (-1);
932                          }                          }
933                  }  #ifdef USE_APP_LEVEL_THREADING
934          }                          fseek(in_file, 0, SEEK_END); /* Determine input size */
935                            pos = ftell(in_file);
936          // This should be after the avi input opening stuff                          ARG_MAXFRAMENR = pos / IMAGE_SIZE(XDIM, YDIM); /* PGM, header size ?? */
937          if (ARG_TIMECODEFILE != NULL) {  #endif
938                  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");  
939                  }                  }
940          }          }
941    
# Line 965  Line 959 
959          if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))          if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))
960                          ARG_CQ = DEFAULT_QUANT;                          ARG_CQ = DEFAULT_QUANT;
961    
962                    /* Init xvidcore */
963        enc_gbl(use_assembler);
964    
965    #ifdef USE_APP_LEVEL_THREADING
966            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0 ||
967                ARG_NUM_APP_THREADS <= 1 || ARG_THREADS != 0 ||
968                ARG_TIMECODEFILE != NULL || ARG_AVIOUTPUTFILE != NULL ||
969                ARG_INPUTTYPE == 1 || ARG_MKVOUTPUTFILE != NULL)            /* TODO: PGM input */
970    #endif /* Spawn just one encoder instance */
971            {
972                    enc_sequence_data_t enc_data;
973                    memset(&enc_data, 0, sizeof(enc_sequence_data_t));
974    
975                    if (!ARG_THREADS) ARG_THREADS = ARG_NUM_APP_THREADS;
976                    ARG_NUM_APP_THREADS = 1;
977    
978                    enc_data.outfilename = ARG_OUTPUTFILE;
979                    enc_data.statsfilename1 = ARG_PASS1;
980                    enc_data.start_num = ARG_STARTFRAMENR;
981                    enc_data.stop_num = ARG_MAXFRAMENR;
982    
983                            /* Encode input */
984                    encode_sequence(&enc_data);
985    
986                            /* Copy back stats */
987                    input_num = enc_data.input_num;
988                    totalsize = enc_data.totalsize;
989                    totalenctime = enc_data.totalenctime;
990                    for (i=0; i < 3; i++) totalPSNR[i] = enc_data.totalPSNR[i];
991                    memcpy(framestats, enc_data.framestats, sizeof(framestats));
992            }
993    #ifdef USE_APP_LEVEL_THREADING
994            else { /* Split input into sequences and create multiple encoder instances */
995                    int k;
996                    void *status;
997                    FILE *f_out = NULL, *f_stats = NULL;
998    
999                    enc_sequence_data_t enc_data[MAX_ENC_INSTANCES];
1000                    char outfile[MAX_ENC_INSTANCES][256];
1001                    char statsfilename[MAX_ENC_INSTANCES][256];
1002    
1003                    for (k = 0; k < MAX_ENC_INSTANCES; k++)
1004                            memset(&enc_data[k], 0, sizeof(enc_sequence_data_t));
1005    
1006                            /* Overwrite internal encoder threading */
1007                    if (ARG_NUM_APP_THREADS > MAX_ENC_INSTANCES) {
1008                            ARG_THREADS = (int) (ARG_NUM_APP_THREADS / MAX_ENC_INSTANCES);
1009                            ARG_NUM_APP_THREADS = MAX_ENC_INSTANCES;
1010                    }
1011                    else
1012                            ARG_THREADS = -1;
1013    
1014                    enc_data[0].outfilename = ARG_OUTPUTFILE;
1015                    enc_data[0].statsfilename1 = ARG_PASS1;
1016                    enc_data[0].start_num = ARG_STARTFRAMENR;
1017                    enc_data[0].stop_num = (ARG_MAXFRAMENR-ARG_STARTFRAMENR)/ARG_NUM_APP_THREADS;
1018    
1019                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1020                            sprintf(outfile[k], "%s.%03d", ARG_OUTPUTFILE, k);
1021                            enc_data[k].outfilename = outfile[k];
1022                            if (ARG_PASS1) {
1023                                    sprintf(statsfilename[k], "%s.%03d", ARG_PASS1, k);
1024                                    enc_data[k].statsfilename1 = statsfilename[k];
1025                            }
1026                            enc_data[k].start_num = (k*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS;
1027                            enc_data[k].stop_num = ((k+1)*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS;
1028                    }
1029    
1030                            /* Start multiple encoder threads in parallel */
1031                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1032                            pthread_create(&enc_data[k].handle, NULL, (void*)encode_sequence, (void*)&enc_data[k]);
1033                    }
1034    
1035                            /* Encode first sequence in this thread */
1036                    encode_sequence(&enc_data[0]);
1037    
1038                            /* Wait until encoder threads have finished */
1039                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1040                            pthread_join(enc_data[k].handle, &status);
1041                    }
1042    
1043                            /* Join encoder stats and encoder output files */
1044                    if (ARG_OUTPUTFILE)
1045                            f_out = fopen(enc_data[0].outfilename, "ab+");
1046                    if (ARG_PASS1)
1047                            f_stats = fopen(enc_data[0].statsfilename1, "ab+");
1048    
1049                    for (k = 0; k < ARG_NUM_APP_THREADS; k++) {
1050                                    /* Join stats */
1051                            input_num += enc_data[k].input_num;
1052                            totalsize += enc_data[k].totalsize;
1053                            totalenctime = MAX(totalenctime, enc_data[k].totalenctime);
1054    
1055                            for (i=0; i < 3; i++) totalPSNR[i] += enc_data[k].totalPSNR[i];
1056                            for (i=0; i < 8; i++) {
1057                                    int l;
1058                                    framestats[i].count += enc_data[k].framestats[i].count;
1059                                    framestats[i].size += enc_data[k].framestats[i].size;
1060                                    for (l=0; l < 32; l++)
1061                                            framestats[i].quants[l] += enc_data[k].framestats[i].quants[l];
1062                            }
1063                                    /* Join output files */
1064                            if ((k > 0) && (f_out != NULL)) {
1065                                    int ch;
1066                                    FILE *f = fopen(enc_data[k].outfilename, "rb");
1067                                    while((ch = fgetc(f)) != EOF) { fputc(ch, f_out); }
1068                                    fclose(f);
1069                                    remove(enc_data[k].outfilename);
1070                            }
1071                                    /* Join first pass stats files */
1072                            if ((k > 0) && (f_stats != NULL)) {
1073                                    char str[256];
1074                                    FILE *f = fopen(enc_data[k].statsfilename1, "r");
1075                                    while(fgets(str, sizeof(str), f) != NULL) {
1076                                            if (str[0] != '#' && strlen(str) > 3)
1077                                                    fputs(str, f_stats);
1078                                    }
1079                                    fclose(f);
1080                                    remove(enc_data[k].statsfilename1);
1081                            }
1082                    }
1083                    if (f_out) fclose(f_out);
1084                    if (f_stats) fclose(f_stats);
1085            }
1086    #endif
1087    
1088    /*****************************************************************************
1089     *         Calculate totals and averages for output, print results
1090     ****************************************************************************/
1091    
1092     printf("\n");
1093            printf("Tot: enctime(ms) =%7.2f,               length(bytes) = %7d\n",
1094                       totalenctime, (int) totalsize);
1095    
1096            if (input_num > 0) {
1097                    totalsize /= input_num;
1098                    totalenctime /= input_num;
1099                    totalPSNR[0] /= input_num;
1100                    totalPSNR[1] /= input_num;
1101                    totalPSNR[2] /= input_num;
1102            } else {
1103                    totalsize = -1;
1104                    totalenctime = -1;
1105            }
1106    
1107            printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d",
1108                       totalenctime, 1000 / totalenctime, (int) totalsize);
1109       if (ARG_STATS) {
1110           printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
1111                      totalPSNR[0],totalPSNR[1],totalPSNR[2]);
1112            }
1113            printf("\n");
1114            if (framestats[XVID_TYPE_IVOP].count) {
1115                    printf("I frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1116                            framestats[XVID_TYPE_IVOP].count, framestats[XVID_TYPE_IVOP].size/framestats[XVID_TYPE_IVOP].count, \
1117                            framestats[XVID_TYPE_IVOP].size, minquant(framestats[XVID_TYPE_IVOP].quants), \
1118                            avgquant(framestats[XVID_TYPE_IVOP]), maxquant(framestats[XVID_TYPE_IVOP].quants));
1119            }
1120            if (framestats[XVID_TYPE_PVOP].count) {
1121                    printf("P frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1122                            framestats[XVID_TYPE_PVOP].count, framestats[XVID_TYPE_PVOP].size/framestats[XVID_TYPE_PVOP].count, \
1123                            framestats[XVID_TYPE_PVOP].size, minquant(framestats[XVID_TYPE_PVOP].quants), \
1124                            avgquant(framestats[XVID_TYPE_PVOP]), maxquant(framestats[XVID_TYPE_PVOP].quants));
1125            }
1126            if (framestats[XVID_TYPE_BVOP].count) {
1127                    printf("B frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1128                            framestats[XVID_TYPE_BVOP].count, framestats[XVID_TYPE_BVOP].size/framestats[XVID_TYPE_BVOP].count, \
1129                            framestats[XVID_TYPE_BVOP].size, minquant(framestats[XVID_TYPE_BVOP].quants), \
1130                            avgquant(framestats[XVID_TYPE_BVOP]), maxquant(framestats[XVID_TYPE_BVOP].quants));
1131            }
1132            if (framestats[XVID_TYPE_SVOP].count) {
1133                    printf("S frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1134                            framestats[XVID_TYPE_SVOP].count, framestats[XVID_TYPE_SVOP].size/framestats[XVID_TYPE_SVOP].count, \
1135                            framestats[XVID_TYPE_SVOP].size, minquant(framestats[XVID_TYPE_SVOP].quants), \
1136                            avgquant(framestats[XVID_TYPE_SVOP]), maxquant(framestats[XVID_TYPE_SVOP].quants));
1137            }
1138            if (framestats[5].count) {
1139                    printf("N frames: %6d frames, size = %7d/%7d\n", \
1140                            framestats[5].count, framestats[5].size/framestats[5].count, \
1141                            framestats[5].size);
1142            }
1143    
1144    
1145    /*****************************************************************************
1146     *                            Xvid PART  Stop
1147     ****************************************************************************/
1148    
1149      release_all:
1150    
1151            return (0);
1152    }
1153    
1154    /*****************************************************************************
1155     *               Encode a sequence
1156     ****************************************************************************/
1157    
1158    void encode_sequence(enc_sequence_data_t *h) {
1159    
1160            /* Internal structures (handles) for encoding */
1161            void *enc_handle = NULL;
1162    
1163            int start_num = h->start_num;
1164            int stop_num = h->stop_num;
1165            char *outfilename = h->outfilename;
1166            float *totalPSNR = h->totalPSNR;
1167    
1168            int input_num;
1169            int totalsize;
1170            double totalenctime = 0.;
1171    
1172            unsigned char *mp4_buffer = NULL;
1173            unsigned char *in_buffer = NULL;
1174            unsigned char *out_buffer = NULL;
1175    
1176            double enctime;
1177    
1178            int result;
1179            int output_num;
1180            int nvop_counter;
1181            int m4v_size;
1182            int key;
1183            int stats_type;
1184            int stats_quant;
1185            int stats_length;
1186            int fakenvop = 0;
1187    
1188            FILE *in_file = stdin;
1189            FILE *out_file = NULL;
1190            FILE *time_file = NULL;
1191    
1192            char filename[256];
1193    
1194    #ifdef XVID_MKV_OUTPUT
1195            PMKVFILE myMKVFile = NULL;
1196            PMKVSTREAM myMKVStream = NULL;
1197            MKVSTREAMINFO myMKVStreamInfo;
1198    #endif
1199    #if defined(XVID_AVI_INPUT)
1200            PAVIFILE avi_in = NULL;
1201            PAVISTREAM avi_in_stream = NULL;
1202            PGETFRAME get_frame = NULL;
1203            BITMAPINFOHEADER myBitmapInfoHeader;
1204    #else
1205    #define get_frame NULL
1206    #endif
1207    #if defined(XVID_AVI_OUTPUT)
1208            int avierr;
1209            PAVIFILE myAVIFile = NULL;
1210            PAVISTREAM myAVIStream = NULL;
1211            AVISTREAMINFO myAVIStreamInfo;
1212    #endif
1213    #if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT)
1214            if (ARG_NUM_APP_THREADS > 1)
1215                    CoInitializeEx(0, COINIT_MULTITHREADED);
1216            AVIFileInit();
1217    #endif
1218    
1219            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
1220                    in_file = stdin;
1221            } else {
1222    #ifdef XVID_AVI_INPUT
1223          if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
1224              strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
1225                      ARG_INPUTTYPE==2)
1226          {
1227                      AVISTREAMINFO avi_info;
1228                      FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
1229    
1230                      if (avi_fp == NULL) {
1231                              fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
1232                              return;
1233                      }
1234                      fclose(avi_fp);
1235    
1236                      if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
1237                              fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
1238                              AVIFileExit();
1239                              return;
1240                      }
1241    
1242                      if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
1243                              fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
1244                              AVIFileRelease(avi_in);
1245                              AVIFileExit();
1246                              return;
1247                      }
1248    
1249                      AVIFileRelease(avi_in);
1250    
1251                      if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
1252                              fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE);
1253                              AVIStreamRelease(avi_in_stream);
1254                              AVIFileExit();
1255                              return;
1256                      }
1257    
1258                  if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) {
1259                              LONG size;
1260                              fprintf(stderr, "Non YV12 input colorspace %c%c%c%c! Attempting conversion...\n",
1261                                      avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256,
1262                                      (avi_info.fccHandler>>24)%256);
1263                              size = sizeof(myBitmapInfoHeader);
1264                              AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size);
1265                              if (size==0)
1266                                      fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
1267                              else {
1268                                      fprintf(stderr, "AVIStreamReadFormat read %d bytes.\n", size);
1269                                      fprintf(stderr, "width = %d, height = %d, planes = %d\n", myBitmapInfoHeader.biWidth,
1270                                              myBitmapInfoHeader.biHeight, myBitmapInfoHeader.biPlanes);
1271                                      fprintf(stderr, "Compression = %c%c%c%c, %d\n",
1272                                              myBitmapInfoHeader.biCompression%256, (myBitmapInfoHeader.biCompression>>8)%256,
1273                                              (myBitmapInfoHeader.biCompression>>16)%256, (myBitmapInfoHeader.biCompression>>24)%256,
1274                                              myBitmapInfoHeader.biCompression);
1275                                      fprintf(stderr, "Bits Per Pixel = %d\n", myBitmapInfoHeader.biBitCount);
1276                                      myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
1277                                      myBitmapInfoHeader.biBitCount = 12;
1278                                      myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
1279                                      get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader);
1280                              }
1281                              if (get_frame == NULL) {
1282                                    AVIStreamRelease(avi_in_stream);
1283                                    AVIFileExit();
1284                                    return;
1285                              }
1286                              else {
1287                                    unsigned char *temp;
1288                                    fprintf(stderr, "AVIStreamGetFrameOpen successful.\n");
1289                                    temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);
1290                                    if (temp != NULL) {
1291                                            int i;
1292                                            for (i = 0; i < ((DWORD*)temp)[0]; i++) {
1293                                                    fprintf(stderr, "%2d ", temp[i]);
1294                                            }
1295                                            fprintf(stderr, "\n");
1296                                    }
1297                              }
1298                              if (avi_info.fccHandler == MAKEFOURCC('D', 'I', 'B', ' ')) {
1299                                      AVIStreamGetFrameClose(get_frame);
1300                                      get_frame = NULL;
1301                                      ARG_COLORSPACE = XVID_CSP_BGR | XVID_CSP_VFLIP;
1302                              }
1303                      }
1304        }
1305        else
1306    #endif
1307                    {
1308                            in_file = fopen(ARG_INPUTFILE, "rb");
1309                            if (in_file == NULL) {
1310                                    fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
1311                                    return;
1312                            }
1313                    }
1314            }
1315    
1316            // This should be after the avi input opening stuff
1317            if (ARG_TIMECODEFILE != NULL) {
1318                    time_file = fopen(ARG_TIMECODEFILE, "r");
1319                    if (time_file==NULL) {
1320                            fprintf(stderr, "Couldn't open timecode file '%s'!\n", ARG_TIMECODEFILE);
1321                            return;
1322                    }
1323                    else {
1324                            fscanf(time_file, "# timecode format v2\n");
1325                    }
1326            }
1327    
1328          if (ARG_INPUTTYPE==1) {          if (ARG_INPUTTYPE==1) {
1329  #ifndef READ_PNM  #ifndef READ_PNM
1330                  if (read_pgmheader(in_file)) {                  if (read_pgmheader(in_file)) {
# Line 973  Line 1333 
1333  #endif  #endif
1334                          fprintf(stderr,                          fprintf(stderr,
1335                                          "Wrong input format, I want YUV encapsulated in PGM\n");                                          "Wrong input format, I want YUV encapsulated in PGM\n");
1336                          return (-1);                          return;
1337                  }                  }
1338          }          }
1339    
1340          /* Jump to the starting frame */          /* Jump to the starting frame */
1341          if (ARG_INPUTTYPE == 0)          if (ARG_INPUTTYPE == 0) /* TODO: Other input formats ??? */
1342                  fseek(in_file, ARG_STARTFRAMENR*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);                  fseek(in_file, start_num*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);
1343    
1344    
1345          /* now we know the sizes, so allocate memory */          /* now we know the sizes, so allocate memory */
1346          if (get_frame == NULL)          if (get_frame == NULL)
# Line 995  Line 1356 
1356                  goto free_all_memory;                  goto free_all_memory;
1357    
1358  /*****************************************************************************  /*****************************************************************************
1359   *                            XviD PART  Start   *                            Xvid PART  Start
1360   ****************************************************************************/   ****************************************************************************/
1361    
1362    
1363          result = enc_init(use_assembler);          result = enc_init(&enc_handle, h->statsfilename1, h->start_num);
1364          if (result) {          if (result) {
1365                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);
1366                  goto release_all;                  goto release_all;
# Line 1011  Line 1372 
1372    
1373          if (ARG_SAVEMPEGSTREAM) {          if (ARG_SAVEMPEGSTREAM) {
1374    
1375                  if (ARG_OUTPUTFILE) {                  if (outfilename) {
1376                          if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {                          if ((out_file = fopen(outfilename, "w+b")) == NULL) {
1377                                  fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);                                  fprintf(stderr, "Error opening output file %s\n", outfilename);
1378                                  goto release_all;                                  goto release_all;
1379                          }                          }
1380                  }                  }
# Line 1109  Line 1470 
1470          result = 0;          result = 0;
1471    
1472          input_num = 0;                          /* input frame counter */          input_num = 0;                          /* input frame counter */
1473          output_num = 0;                         /* output frame counter */          output_num = start_num;             /* output frame counter */
1474    
1475          nvop_counter = 0;          nvop_counter = 0;
1476    
# Line 1118  Line 1479 
1479                  char *type;                  char *type;
1480                  int sse[3];                  int sse[3];
1481    
1482                  if (input_num >= ARG_MAXFRAMENR && ARG_MAXFRAMENR > 0) {                  if ((input_num+start_num) >= stop_num && stop_num > 0) {
1483                          result = 1;                          result = 1;
1484                  }                  }
1485    
# Line 1127  Line 1488 
1488                          if (ARG_INPUTTYPE==2) {                          if (ARG_INPUTTYPE==2) {
1489                                  /* read avs/avi data (YUV-format) */                                  /* read avs/avi data (YUV-format) */
1490                                  if (get_frame != NULL) {                                  if (get_frame != NULL) {
1491                                          in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+ARG_STARTFRAMENR);                                          in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+start_num);
1492                                          if (in_buffer == NULL)                                          if (in_buffer == NULL)
1493                                                  result = 1;                                                  result = 1;
1494                                          else                                          else
1495                                                  in_buffer += ((DWORD*)in_buffer)[0];                                                  in_buffer += ((DWORD*)in_buffer)[0];
1496                                  } else {                                  } else {
1497                                          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)
1498                                                  result = 1;                                                  result = 1;
1499                                  }                                  }
1500                          } else                          } else
# Line 1155  Line 1516 
1516   *                       Encode and decode this frame   *                       Encode and decode this frame
1517   ****************************************************************************/   ****************************************************************************/
1518    
1519                  if (input_num >= (unsigned int)ARG_MAXFRAMENR-1 && ARG_MAXBFRAMES) {                  if ((input_num+start_num) >= (unsigned int)(stop_num-1) && ARG_MAXBFRAMES) {
1520                          stats_type = XVID_TYPE_PVOP;                          stats_type = XVID_TYPE_PVOP;
1521                  }                  }
1522                  else                  else
# Line 1163  Line 1524 
1524    
1525                  enctime = msecond();                  enctime = msecond();
1526                  m4v_size =                  m4v_size =
1527                          enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type,                          enc_main(enc_handle, !result ? in_buffer : 0, mp4_buffer, &key, &stats_type,
1528                                           &stats_quant, &stats_length, sse, input_num);                                           &stats_quant, &stats_length, sse, input_num);
1529                  enctime = msecond() - enctime;                  enctime = msecond() - enctime;
1530    
# Line 1191  Line 1552 
1552                          }                          }
1553    
1554                          if (stats_length > 8) {                          if (stats_length > 8) {
1555                                  framestats[stats_type].count++;                                  h->framestats[stats_type].count++;
1556                                  framestats[stats_type].quants[stats_quant]++;                                  h->framestats[stats_type].quants[stats_quant]++;
1557                                  framestats[stats_type].size += stats_length;                                  h->framestats[stats_type].size += stats_length;
1558                          }                          }
1559                          else {                          else {
1560                                  framestats[5].count++;                                  h->framestats[5].count++;
1561                                  framestats[5].quants[stats_quant]++;                                  h->framestats[5].quants[stats_quant]++;
1562                                  framestats[5].size += stats_length;                                  h->framestats[5].size += stats_length;
1563                          }                          }
1564    
1565  #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)))))
1566    
1567                          if (ARG_PROGRESS == 0) {                          if (ARG_PROGRESS == 0) {
1568                                  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,
1569                                          key, (float) enctime, (int) m4v_size);                                          key, (float) enctime, (int) m4v_size);
1570                                  printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,                                  printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,
1571                                     stats_length);                                     stats_length);
# Line 1217  Line 1578 
1578                                  }                                  }
1579                                  printf("\n");                                  printf("\n");
1580                          } else {                          } else {
1581                                  if (input_num % ARG_PROGRESS == 1) {                                  if ((input_num) % ARG_PROGRESS == 1) {
1582                                          if (ARG_MAXFRAMENR > 0) {                                          if (stop_num > 0) {
1583                                                  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", \
1584                                                          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), \
1585                                                          (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);                                                          ((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num));
1586                                          } else {                                          } else {
1587                                                  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", \
1588                                                          input_num, input_num*1000/totalenctime, \                                                          (ARG_NUM_APP_THREADS*input_num), (ARG_NUM_APP_THREADS*input_num)*1000/(totalenctime), \
1589                                                          (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);                                                          ((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num));
1590                                          }                                          }
1591                                  }                                  }
1592                          }                          }
# Line 1289  Line 1650 
1650                                          removedivxp((char*)mp4_buffer, m4v_size);                                          removedivxp((char*)mp4_buffer, m4v_size);
1651    
1652                                  /* Save ES stream */                                  /* Save ES stream */
1653                                  if (ARG_OUTPUTFILE && out_file && !(fakenvop && m4v_size <= 8)) {                                  if (outfilename && out_file && !(fakenvop && m4v_size <= 8)) {
1654                                                  fwrite(mp4_buffer, 1, m4v_size, out_file);                                                  fwrite(mp4_buffer, 1, m4v_size, out_file);
1655                                  }                                  }
1656  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
# Line 1303  Line 1664 
1664                                  fakenvop=0;                                  fakenvop=0;
1665                  }                  }
1666    
1667                  input_num++;                  if (!result)
1668                            (input_num)++;
1669    
1670                  /* Read the header if it's pgm stream */                  /* Read the header if it's pgm stream */
1671                  if (!result && (ARG_INPUTTYPE==1))                  if (!result && (ARG_INPUTTYPE==1))
# Line 1315  Line 1677 
1677          } while (1);          } while (1);
1678    
1679    
   
 /*****************************************************************************  
  *         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  
  ****************************************************************************/  
   
1680    release_all:    release_all:
1681    
1682      h->input_num = input_num;
1683      h->totalenctime = totalenctime;
1684      h->totalsize = totalsize;
1685    
1686  #ifdef XVID_AVI_INPUT  #ifdef XVID_AVI_INPUT
1687          if (get_frame) AVIStreamGetFrameClose(get_frame);          if (get_frame) AVIStreamGetFrameClose(get_frame);
1688          if (avi_stream) AVIStreamRelease(avi_stream);          if (avi_in_stream) AVIStreamRelease(avi_in_stream);
 #ifndef XVID_AVI_OUTPUT  
         AVIFileExit();  
 #endif  
1689  #endif  #endif
1690    
1691          if (enc_handle) {          if (enc_handle) {
1692                  result = enc_stop();                  result = enc_stop(enc_handle);
1693                  if (result)                  if (result)
1694                          fprintf(stderr, "Encore RELEASE problem return value %d\n",                          fprintf(stderr, "Encore RELEASE problem return value %d\n",
1695                                          result);                                          result);
# Line 1398  Line 1699 
1699                  fclose(in_file);                  fclose(in_file);
1700          if (out_file)          if (out_file)
1701                  fclose(out_file);                  fclose(out_file);
1702            if (time_file)
1703                    fclose(time_file);
1704    
1705  #ifdef XVID_AVI_OUTPUT  #ifdef XVID_AVI_OUTPUT
1706          if (myAVIStream) AVIStreamRelease(myAVIStream);          if (myAVIStream) AVIStreamRelease(myAVIStream);
1707          if (myAVIFile) AVIFileRelease(myAVIFile);          if (myAVIFile) AVIFileRelease(myAVIFile);
         AVIFileExit();  
1708  #endif  #endif
1709  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
1710          if (myMKVStream) MKVStreamRelease(myMKVStream);          if (myMKVStream) MKVStreamRelease(myMKVStream);
1711          if (myMKVFile) MKVFileRelease(myMKVFile);          if (myMKVFile) MKVFileRelease(myMKVFile);
1712  #endif  #endif
1713    #if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT)
1714            AVIFileExit();
1715    #endif
1716    
1717    free_all_memory:    free_all_memory:
1718          free(out_buffer);          free(out_buffer);
1719          free(mp4_buffer);          free(mp4_buffer);
1720          free(in_buffer);          free(in_buffer);
   
         return (0);  
   
1721  }  }
1722    
   
1723  /*****************************************************************************  /*****************************************************************************
1724   *                        "statistical" functions   *                        "statistical" functions
1725   *   *
1726   *  these are not needed for encoding or decoding, but for measuring   *  these are not needed for encoding or decoding, but for measuring
1727   *  time and quality, there in nothing specific to XviD in these   *  time and quality, there in nothing specific to Xvid in these
1728   *   *
1729   *****************************************************************************/   *****************************************************************************/
1730    
# Line 1509  Line 1810 
1810  #endif  #endif
1811          fprintf(stderr, " -w      integer: frame width ([1.2048])\n");          fprintf(stderr, " -w      integer: frame width ([1.2048])\n");
1812          fprintf(stderr, " -h      integer: frame height ([1.2048])\n");          fprintf(stderr, " -h      integer: frame height ([1.2048])\n");
1813            fprintf(stderr, " -csp    string : colorspace of raw input file i420, yv12 (default)\n");
1814          fprintf(stderr, " -frames integer: number of frames to encode\n");          fprintf(stderr, " -frames integer: number of frames to encode\n");
1815          fprintf(stderr, "\n");          fprintf(stderr, "\n");
1816          fprintf(stderr, "Output options:\n");          fprintf(stderr, "Output options:\n");
# Line 1552  Line 1854 
1854          fprintf(stderr, "-odegrade   integer             : max overflow degradation (5)\n");          fprintf(stderr, "-odegrade   integer             : max overflow degradation (5)\n");
1855          fprintf(stderr, "-chigh      integer             : high bitrate scenes degradation (0)\n");          fprintf(stderr, "-chigh      integer             : high bitrate scenes degradation (0)\n");
1856          fprintf(stderr, "-clow       integer             : low bitrate scenes improvement (0)\n");          fprintf(stderr, "-clow       integer             : low bitrate scenes improvement (0)\n");
1857          fprintf(stderr, "-overhead   integer             : container frame overhead (24)\n");          fprintf(stderr, "-overhead   integer             : container frame overhead (0)\n");
1858          fprintf(stderr, "-vbvsize    integer             : use vbv buffer size\n");          fprintf(stderr, "-vbvsize    integer             : use vbv buffer size\n");
1859          fprintf(stderr, "-vbvmax     integer             : vbv max bitrate\n");          fprintf(stderr, "-vbvmax     integer             : vbv max bitrate\n");
1860          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 1872 
1872          fprintf(stderr, " -interlaced [integer]          : interlaced encoding (BFF:1, TFF:2) (1)\n");          fprintf(stderr, " -interlaced [integer]          : interlaced encoding (BFF:1, TFF:2) (1)\n");
1873          fprintf(stderr, " -nopacked                      : Disable packed mode\n");          fprintf(stderr, " -nopacked                      : Disable packed mode\n");
1874          fprintf(stderr, " -noclosed_gop                  : Disable closed GOP mode\n");          fprintf(stderr, " -noclosed_gop                  : Disable closed GOP mode\n");
1875          fprintf(stderr, " -lumimasking                   : use lumimasking algorithm\n");          fprintf(stderr, " -masking [integer]             : HVS masking mode (None:0, Lumi:1, Variance:2) (0)\n");
1876          fprintf(stderr, " -stats                         : print stats about encoded frames\n");          fprintf(stderr, " -stats                         : print stats about encoded frames\n");
1877          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");
1878          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");
# Line 1604  Line 1906 
1906   *                       Input and output functions   *                       Input and output functions
1907   *   *
1908   *      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
1909   *      image. It's just for convenience, again nothing specific to XviD   *      image. It's just for convenience, again nothing specific to Xvid
1910   *   *
1911   *****************************************************************************/   *****************************************************************************/
1912    
# Line 1766  Line 2068 
2068    
2069  #define FRAMERATE_INCR 1001  #define FRAMERATE_INCR 1001
2070    
2071    /* Gobal encoder init, once per process */
2072  /* Initialize encoder for first use, pass all needed parameters to the codec */  void
2073  static int  enc_gbl(int use_assembler)
 enc_init(int use_assembler)  
2074  {  {
         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];  
2075          xvid_gbl_init_t xvid_gbl_init;          xvid_gbl_init_t xvid_gbl_init;
         xvid_enc_create_t xvid_enc_create;  
         int i;  
2076    
2077          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
2078           * XviD core initialization           * Xvid core initialization
2079           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
2080    
2081          /* Set version -- version checking will done by xvidcore */          /* Set version -- version checking will done by xvidcore */
# Line 1805  Line 2096 
2096                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
2097          }          }
2098    
2099          /* Initialize XviD core -- Should be done once per __process__ */          /* Initialize Xvid core -- Should be done once per __process__ */
2100          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
2101        ARG_CPU_FLAGS = xvid_gbl_init.cpu_flags;
2102          enc_info();          enc_info();
2103    }
2104    
2105    /* Initialize encoder for first use, pass all needed parameters to the codec */
2106    static int
2107    enc_init(void **enc_handle, char *stats_pass1, int start_num)
2108    {
2109            int xerr;
2110            //xvid_plugin_cbr_t cbr;
2111        xvid_plugin_single_t single;
2112            xvid_plugin_2pass1_t rc2pass1;
2113            xvid_plugin_2pass2_t rc2pass2;
2114            xvid_plugin_ssim_t ssim;
2115            xvid_plugin_lumimasking_t masking;
2116            //xvid_plugin_fixed_t rcfixed;
2117            xvid_enc_plugin_t plugins[8];
2118            xvid_enc_create_t xvid_enc_create;
2119            int i;
2120    
2121          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
2122           * XviD encoder initialization           * Xvid encoder initialization
2123           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
2124    
2125          /* Version again */          /* Version again */
# Line 1873  Line 2182 
2182                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2183          }          }
2184    
2185          if (ARG_PASS1) {          if (stats_pass1) {
2186                  memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));                  memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));
2187                  rc2pass1.version = XVID_VERSION;                  rc2pass1.version = XVID_VERSION;
2188                  rc2pass1.filename = ARG_PASS1;                  rc2pass1.filename = stats_pass1;
2189                  if (ARG_FULL1PASS)                  if (ARG_FULL1PASS)
2190                          prepare_full1pass_zones();                          prepare_full1pass_zones();
2191                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;
# Line 1896  Line 2205 
2205    
2206    
2207          if (ARG_LUMIMASKING) {          if (ARG_LUMIMASKING) {
2208                    memset(&masking, 0, sizeof(xvid_plugin_lumimasking_t));
2209                    masking.method = (ARG_LUMIMASKING==2);
2210                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
2211                  plugins[xvid_enc_create.num_plugins].param = NULL;                  plugins[xvid_enc_create.num_plugins].param = &masking;
2212                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2213          }          }
2214    
# Line 1908  Line 2219 
2219          }          }
2220    
2221          if (ARG_SSIM>=0 || ARG_SSIM_PATH != NULL) {          if (ARG_SSIM>=0 || ARG_SSIM_PATH != NULL) {
2222            memset(&ssim, 0, sizeof(xvid_plugin_ssim_t));
2223    
2224                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim;
2225    
2226                    if( ARG_SSIM >=0){
2227                  ssim.b_printstat = 1;                  ssim.b_printstat = 1;
2228                            ssim.acc = ARG_SSIM;
2229                    } else {
2230                            ssim.b_printstat = 0;
2231                            ssim.acc = 2;
2232                    }
2233    
2234                    if(ARG_SSIM_PATH != NULL){
2235                  ssim.stat_path = ARG_SSIM_PATH;                  ssim.stat_path = ARG_SSIM_PATH;
2236                    }
2237    
2238            ssim.cpu_flags = ARG_CPU_FLAGS;
2239                  ssim.b_visualize = 0;                  ssim.b_visualize = 0;
                 ssim.acc = (ARG_SSIM_PATH != NULL && ARG_SSIM < 0) ? 2 : ARG_SSIM;  
2240                  plugins[xvid_enc_create.num_plugins].param = &ssim;                  plugins[xvid_enc_create.num_plugins].param = &ssim;
2241                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2242          }          }
# Line 1953  Line 2277 
2277          /* Frame drop ratio */          /* Frame drop ratio */
2278          xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;          xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;
2279    
2280            /* Start frame number */
2281            xvid_enc_create.start_frame_num = start_num;
2282    
2283          /* Global encoder options */          /* Global encoder options */
2284          xvid_enc_create.global = 0;          xvid_enc_create.global = 0;
2285    
# Line 1969  Line 2296 
2296          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
2297    
2298          /* Retrieve the encoder instance from the structure */          /* Retrieve the encoder instance from the structure */
2299          enc_handle = xvid_enc_create.handle;          *enc_handle = xvid_enc_create.handle;
2300    
2301          free(xvid_enc_create.zones);          free(xvid_enc_create.zones);
2302    
# Line 2000  Line 2327 
2327                  fprintf(stderr, "SSE ");                  fprintf(stderr, "SSE ");
2328          if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2)          if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2)
2329                  fprintf(stderr, "SSE2 ");                  fprintf(stderr, "SSE2 ");
2330            if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE3)
2331                    fprintf(stderr, "SSE3 ");
2332            if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE41)
2333                    fprintf(stderr, "SSE41 ");
2334          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW)          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW)
2335                  fprintf(stderr, "3DNOW ");                  fprintf(stderr, "3DNOW ");
2336          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT)          if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT)
# Line 2008  Line 2339 
2339                  fprintf(stderr, "TSC ");                  fprintf(stderr, "TSC ");
2340          fprintf(stderr, "\n");          fprintf(stderr, "\n");
2341          fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);          fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);
2342          if (!ARG_THREADS)          ARG_NUM_APP_THREADS = xvid_gbl_info.num_threads;
2343                  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);  
2344          return ret;          return ret;
2345  }  }
2346    
2347  static int  static int
2348  enc_stop()  enc_stop(void *enc_handle)
2349  {  {
2350          int xerr;          int xerr;
2351    
# Line 2026  Line 2356 
2356  }  }
2357    
2358  static int  static int
2359  enc_main(unsigned char *image,  enc_main(void *enc_handle,
2360                     unsigned char *image,
2361                   unsigned char *bitstream,                   unsigned char *bitstream,
2362                   int *key,                   int *key,
2363                   int *stats_type,                   int *stats_type,
# Line 2055  Line 2386 
2386          if (image) {          if (image) {
2387                  xvid_enc_frame.input.plane[0] = image;                  xvid_enc_frame.input.plane[0] = image;
2388  #ifndef READ_PNM  #ifndef READ_PNM
                 if (ARG_INPUTTYPE==2)  
2389                          xvid_enc_frame.input.csp = ARG_COLORSPACE;                          xvid_enc_frame.input.csp = ARG_COLORSPACE;
                 else  
                         xvid_enc_frame.input.csp = XVID_CSP_I420;  
2390                  xvid_enc_frame.input.stride[0] = XDIM;                  xvid_enc_frame.input.stride[0] = XDIM;
2391  #else  #else
2392                  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.1886

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