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

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

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

revision 1863, Wed May 27 15:52:05 2009 UTC revision 1883, Tue Mar 9 10:00:30 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.38 2009-05-27 15:52:05 Isibaar Exp $   * $Id: xvid_encraw.c,v 1.39 2010-03-09 10:00:22 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;  
347    
348          char filename[256];  #if defined(XVID_AVI_INPUT)
349            PAVIFILE avi_in = NULL;
350          FILE *in_file = stdin;          PAVISTREAM avi_in_stream = NULL;
351          FILE *out_file = NULL;          PGETFRAME get_frame = NULL;
352          FILE *time_file = NULL;  #else
353    #define get_frame NULL
354  #ifdef XVID_AVI_OUTPUT  #endif
355          int avierr;  #if defined(XVID_AVI_OUTPUT)
356          PAVIFILE myAVIFile=NULL;          PAVIFILE myAVIFile=NULL;
357          PAVISTREAM myAVIStream=NULL;          PAVISTREAM myAVIStream=NULL;
         AVISTREAMINFO myAVIStreamInfo;  
358          BITMAPINFOHEADER myBitmapInfoHeader;          BITMAPINFOHEADER myBitmapInfoHeader;
359    #endif
360    #if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT)
361          AVIFileInit();          AVIFileInit();
362  #endif  #endif
363  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
# Line 367  Line 367 
367  #endif  #endif
368    
369          printf("xvid_encraw - raw mpeg4 bitstream encoder ");          printf("xvid_encraw - raw mpeg4 bitstream encoder ");
370          printf("written by Christoph Lampert 2002-2003\n\n");          printf("written by Christoph Lampert\n\n");
371    
372          /* Is there a dumb XviD coder ? */          /* Is there a dumb Xvid coder ? */
373          if(ME_ELEMENTS != VOP_ELEMENTS) {          if(ME_ELEMENTS != VOP_ELEMENTS) {
374                  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");
375                  return(-1);                  return(-1);
376          }          }
377    
378            /* Clear framestats */
379            memset(framestats, 0, sizeof(framestats));
380    
381  /*****************************************************************************  /*****************************************************************************
382   *                            Command line parsing   *                            Command line parsing
383   ****************************************************************************/   ****************************************************************************/
# Line 830  Line 833 
833                  return (-1);                  return (-1);
834          }          }
835    
836          if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if (ARG_INPUTFILE != NULL) {
                 in_file = stdin;  
         } else {  
837  #ifdef XVID_AVI_INPUT  #ifdef XVID_AVI_INPUT
838        if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||        if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
839            strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||            strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
840                    ARG_INPUTTYPE==2)                    ARG_INPUTTYPE==2)
841        {        {
842                    AVISTREAMINFO avi_info;                    AVISTREAMINFO avi_info;
843  #ifndef XVID_AVI_OUTPUT  
                   AVIFileInit();  
 #endif  
844                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");                    FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
845                    if (avi_fp == NULL) {                    if (avi_fp == NULL) {
846                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
# Line 849  Line 848 
848                    }                    }
849                    fclose(avi_fp);                    fclose(avi_fp);
850    
851                    if (AVIFileOpen(&avi_file, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {                    if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
852                            fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
853                            AVIFileExit();                            AVIFileExit();
854                            return(-1);                            return(-1);
855                    }                    }
856    
857                    if (AVIFileGetStream(avi_file, &avi_stream, streamtypeVIDEO, 0) != AVIERR_OK) {                    if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
858                            fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);                            fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
859                            AVIFileRelease(avi_file);                            AVIFileRelease(avi_in);
860                            AVIFileExit();                            AVIFileExit();
861                            return (-1);                            return (-1);
862                    }                    }
863    
864                    AVIFileRelease(avi_file);                    AVIFileRelease(avi_in);
865    
866                    if(AVIStreamInfo(avi_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {                    if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
867                            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);
868                            AVIStreamRelease(avi_stream);                            AVIStreamRelease(avi_in_stream);
869                            AVIFileExit();                            AVIFileExit();
870                            return (-1);                            return (-1);
871                    }                    }
# Line 877  Line 876 
876                                    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,
877                                    (avi_info.fccHandler>>24)%256);                                    (avi_info.fccHandler>>24)%256);
878                            size = sizeof(myBitmapInfoHeader);                            size = sizeof(myBitmapInfoHeader);
879                            AVIStreamReadFormat(avi_stream, 0, &myBitmapInfoHeader, &size);                            AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size);
880                            if (size==0)                            if (size==0)
881                                    fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");                                    fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
882                            else {                            else {
# Line 892  Line 891 
891                                    myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');                                    myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
892                                    myBitmapInfoHeader.biBitCount = 12;                                    myBitmapInfoHeader.biBitCount = 12;
893                                    myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;                                    myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
894                                    get_frame = AVIStreamGetFrameOpen(avi_stream, &myBitmapInfoHeader);                                    get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader);
895                            }                            }
896                            if (get_frame == NULL) {                            if (get_frame == NULL) {
897                                  AVIStreamRelease(avi_stream);                                  AVIStreamRelease(avi_in_stream);
898                                  AVIFileExit();                                  AVIFileExit();
899                                  return (-1);                                  return (-1);
900                            }                            }
# Line 932  Line 931 
931                    }                    }
932    
933                    ARG_INPUTTYPE = 2;                    ARG_INPUTTYPE = 2;
934    
935                      if (get_frame) AVIStreamGetFrameClose(get_frame);
936                      if (avi_in_stream) AVIStreamRelease(avi_in_stream);
937      }      }
938      else      else
939  #endif  #endif
940                  {                  {
941                          in_file = fopen(ARG_INPUTFILE, "rb");                          FILE *in_file = fopen(ARG_INPUTFILE, "rb");
942                            int pos = 0;
943                          if (in_file == NULL) {                          if (in_file == NULL) {
944                                  fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);                                  fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
945                                  return (-1);                                  return (-1);
946                          }                          }
947                  }  #ifdef USE_APP_LEVEL_THREADING
948          }                          fseek(in_file, 0, SEEK_END); /* Determine input size */
949                            pos = ftell(in_file);
950          // This should be after the avi input opening stuff                          ARG_MAXFRAMENR = pos / IMAGE_SIZE(XDIM, YDIM); /* PGM, header size ?? */
951          if (ARG_TIMECODEFILE != NULL) {  #endif
952                  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");  
953                  }                  }
954          }          }
955    
# Line 976  Line 973 
973          if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))          if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))
974                          ARG_CQ = DEFAULT_QUANT;                          ARG_CQ = DEFAULT_QUANT;
975    
976                    /* Init xvidcore */
977        enc_gbl(use_assembler);
978    
979    #ifdef USE_APP_LEVEL_THREADING
980            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0 ||
981                ARG_NUM_APP_THREADS <= 1 || ARG_THREADS != 0 ||
982                ARG_TIMECODEFILE != NULL || ARG_AVIOUTPUTFILE != NULL ||
983                ARG_INPUTTYPE == 1 || ARG_MKVOUTPUTFILE != NULL)            /* TODO: PGM input */
984    #endif /* Spawn just one encoder instance */
985            {
986                    enc_sequence_data_t enc_data;
987                    memset(&enc_data, 0, sizeof(enc_sequence_data_t));
988    
989                    if (!ARG_THREADS) ARG_THREADS = ARG_NUM_APP_THREADS;
990                    ARG_NUM_APP_THREADS = 1;
991    
992                    enc_data.outfilename = ARG_OUTPUTFILE;
993                    enc_data.statsfilename1 = ARG_PASS1;
994                    enc_data.start_num = ARG_STARTFRAMENR;
995                    enc_data.stop_num = ARG_MAXFRAMENR;
996    
997                            /* Encode input */
998                    encode_sequence(&enc_data);
999    
1000                            /* Copy back stats */
1001                    input_num = enc_data.input_num;
1002                    totalsize = enc_data.totalsize;
1003                    totalenctime = enc_data.totalenctime;
1004                    for (i=0; i < 3; i++) totalPSNR[i] = enc_data.totalPSNR[i];
1005                    memcpy(framestats, enc_data.framestats, sizeof(framestats));
1006            }
1007    #ifdef USE_APP_LEVEL_THREADING
1008            else { /* Split input into sequences and create multiple encoder instances */
1009                    int k;
1010                    void *status;
1011                    FILE *f_out = NULL, *f_stats = NULL;
1012    
1013                    enc_sequence_data_t enc_data[MAX_ENC_INSTANCES];
1014                    char outfile[MAX_ENC_INSTANCES][256];
1015                    char statsfilename[MAX_ENC_INSTANCES][256];
1016    
1017                    for (k = 0; k < MAX_ENC_INSTANCES; k++)
1018                            memset(&enc_data[k], 0, sizeof(enc_sequence_data_t));
1019    
1020                            /* Overwrite internal encoder threading */
1021                    if (ARG_NUM_APP_THREADS > MAX_ENC_INSTANCES) {
1022                            ARG_THREADS = (int) (ARG_NUM_APP_THREADS / MAX_ENC_INSTANCES);
1023                            ARG_NUM_APP_THREADS = MAX_ENC_INSTANCES;
1024                    }
1025                    else
1026                            ARG_THREADS = -1;
1027    
1028                    enc_data[0].outfilename = ARG_OUTPUTFILE;
1029                    enc_data[0].statsfilename1 = ARG_PASS1;
1030                    enc_data[0].start_num = ARG_STARTFRAMENR;
1031                    enc_data[0].stop_num = (ARG_MAXFRAMENR-ARG_STARTFRAMENR)/ARG_NUM_APP_THREADS;
1032    
1033                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1034                            sprintf(outfile[k], "%s.%03d", ARG_OUTPUTFILE, k);
1035                            enc_data[k].outfilename = outfile[k];
1036                            if (ARG_PASS1) {
1037                                    sprintf(statsfilename[k], "%s.%03d", ARG_PASS1, k);
1038                                    enc_data[k].statsfilename1 = statsfilename[k];
1039                            }
1040                            enc_data[k].start_num = (ARG_MAXFRAMENR-ARG_STARTFRAMENR)/ARG_NUM_APP_THREADS;
1041                            enc_data[k].stop_num = ((k+1)*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS;
1042                    }
1043    
1044                            /* Start multiple encoder threads in parallel */
1045                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1046                            pthread_create(&enc_data[k].handle, NULL, (void*)encode_sequence, (void*)&enc_data[k]);
1047                    }
1048    
1049                            /* Encode first sequence in this thread */
1050                    encode_sequence(&enc_data[0]);
1051    
1052                            /* Wait until encoder threads have finished */
1053                    for (k = 1; k < ARG_NUM_APP_THREADS; k++) {
1054                            pthread_join(enc_data[k].handle, &status);
1055                    }
1056    
1057                            /* Join encoder stats and encoder output files */
1058                    if (ARG_OUTPUTFILE)
1059                            f_out = fopen(enc_data[0].outfilename, "ab+");
1060                    if (ARG_PASS1)
1061                            f_stats = fopen(enc_data[0].statsfilename1, "ab+");
1062    
1063                    for (k = 0; k < ARG_NUM_APP_THREADS; k++) {
1064                                    /* Join stats */
1065                            input_num += enc_data[k].input_num;
1066                            totalsize += enc_data[k].totalsize;
1067                            totalenctime = MAX(totalenctime, enc_data[k].totalenctime);
1068    
1069                            for (i=0; i < 3; i++) totalPSNR[i] += enc_data[k].totalPSNR[i];
1070                            for (i=0; i < 8; i++) {
1071                                    int l;
1072                                    framestats[i].count += enc_data[k].framestats[i].count;
1073                                    framestats[i].size += enc_data[k].framestats[i].size;
1074                                    for (l=0; l < 32; l++)
1075                                            framestats[i].quants[l] += enc_data[k].framestats[i].quants[l];
1076                            }
1077                                    /* Join output files */
1078                            if ((k > 0) && (f_out != NULL)) {
1079                                    int ch;
1080                                    FILE *f = fopen(enc_data[k].outfilename, "rb");
1081                                    while((ch = fgetc(f)) != EOF) { fputc(ch, f_out); }
1082                                    fclose(f);
1083                                    remove(enc_data[k].outfilename);
1084                            }
1085                                    /* Join first pass stats files */
1086                            if ((k > 0) && (f_stats != NULL)) {
1087                                    char str[256];
1088                                    FILE *f = fopen(enc_data[k].statsfilename1, "r");
1089                                    while(fgets(str, sizeof(str), f) != NULL) {
1090                                            if (str[0] != '#' && strlen(str) > 3)
1091                                                    fputs(str, f_stats);
1092                                    }
1093                                    fclose(f);
1094                                    remove(enc_data[k].statsfilename1);
1095                            }
1096                    }
1097                    if (f_out) fclose(f_out);
1098                    if (f_stats) fclose(f_stats);
1099            }
1100    #endif
1101    
1102    /*****************************************************************************
1103     *         Calculate totals and averages for output, print results
1104     ****************************************************************************/
1105    
1106     printf("\n");
1107            printf("Tot: enctime(ms) =%7.2f,               length(bytes) = %7d\n",
1108                       totalenctime, (int) totalsize);
1109    
1110            if (input_num > 0) {
1111                    totalsize /= input_num;
1112                    totalenctime /= input_num;
1113                    totalPSNR[0] /= input_num;
1114                    totalPSNR[1] /= input_num;
1115                    totalPSNR[2] /= input_num;
1116            } else {
1117                    totalsize = -1;
1118                    totalenctime = -1;
1119            }
1120    
1121            printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d",
1122                       totalenctime, 1000 / totalenctime, (int) totalsize);
1123       if (ARG_STATS) {
1124           printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
1125                      totalPSNR[0],totalPSNR[1],totalPSNR[2]);
1126            }
1127            printf("\n");
1128            if (framestats[XVID_TYPE_IVOP].count) {
1129                    printf("I frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1130                            framestats[XVID_TYPE_IVOP].count, framestats[XVID_TYPE_IVOP].size/framestats[XVID_TYPE_IVOP].count, \
1131                            framestats[XVID_TYPE_IVOP].size, minquant(framestats[XVID_TYPE_IVOP].quants), \
1132                            avgquant(framestats[XVID_TYPE_IVOP]), maxquant(framestats[XVID_TYPE_IVOP].quants));
1133            }
1134            if (framestats[XVID_TYPE_PVOP].count) {
1135                    printf("P frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1136                            framestats[XVID_TYPE_PVOP].count, framestats[XVID_TYPE_PVOP].size/framestats[XVID_TYPE_PVOP].count, \
1137                            framestats[XVID_TYPE_PVOP].size, minquant(framestats[XVID_TYPE_PVOP].quants), \
1138                            avgquant(framestats[XVID_TYPE_PVOP]), maxquant(framestats[XVID_TYPE_PVOP].quants));
1139            }
1140            if (framestats[XVID_TYPE_BVOP].count) {
1141                    printf("B frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1142                            framestats[XVID_TYPE_BVOP].count, framestats[XVID_TYPE_BVOP].size/framestats[XVID_TYPE_BVOP].count, \
1143                            framestats[XVID_TYPE_BVOP].size, minquant(framestats[XVID_TYPE_BVOP].quants), \
1144                            avgquant(framestats[XVID_TYPE_BVOP]), maxquant(framestats[XVID_TYPE_BVOP].quants));
1145            }
1146            if (framestats[XVID_TYPE_SVOP].count) {
1147                    printf("S frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1148                            framestats[XVID_TYPE_SVOP].count, framestats[XVID_TYPE_SVOP].size/framestats[XVID_TYPE_SVOP].count, \
1149                            framestats[XVID_TYPE_SVOP].size, minquant(framestats[XVID_TYPE_SVOP].quants), \
1150                            avgquant(framestats[XVID_TYPE_SVOP]), maxquant(framestats[XVID_TYPE_SVOP].quants));
1151            }
1152            if (framestats[5].count) {
1153                    printf("N frames: %6d frames, size = %7d/%7d\n", \
1154                            framestats[5].count, framestats[5].size/framestats[5].count, \
1155                            framestats[5].size);
1156            }
1157    
1158    
1159    /*****************************************************************************
1160     *                            Xvid PART  Stop
1161     ****************************************************************************/
1162    
1163      release_all:
1164    
1165    #if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT)
1166            AVIFileExit();
1167    #endif
1168    
1169            return (0);
1170    
1171    }
1172    
1173    /*****************************************************************************
1174     *               Encode a sequence
1175     ****************************************************************************/
1176    
1177    void encode_sequence(enc_sequence_data_t *h) {
1178    
1179            /* Internal structures (handles) for encoding */
1180            void *enc_handle = NULL;
1181    
1182            int start_num = h->start_num;
1183            int stop_num = h->stop_num;
1184            char *outfilename = h->outfilename;
1185            float *totalPSNR = h->totalPSNR;
1186    
1187            int input_num;
1188            int totalsize;
1189            double totalenctime = 0.;
1190    
1191            unsigned char *mp4_buffer = NULL;
1192            unsigned char *in_buffer = NULL;
1193            unsigned char *out_buffer = NULL;
1194    
1195            double enctime;
1196    
1197            int result;
1198            int output_num;
1199            int nvop_counter;
1200            int m4v_size;
1201            int key;
1202            int stats_type;
1203            int stats_quant;
1204            int stats_length;
1205            int fakenvop = 0;
1206    
1207            FILE *in_file = stdin;
1208            FILE *out_file = NULL;
1209            FILE *time_file = NULL;
1210    
1211            char filename[256];
1212    
1213    #if defined(XVID_AVI_INPUT)
1214            PAVIFILE avi_in = NULL;
1215            PAVISTREAM avi_in_stream = NULL;
1216            PGETFRAME get_frame = NULL;
1217    #else
1218    #define get_frame NULL
1219    #endif
1220    #if defined(XVID_AVI_OUTPUT)
1221            int avierr;
1222            PAVIFILE myAVIFile=NULL;
1223            PAVISTREAM myAVIStream=NULL;
1224            AVISTREAMINFO myAVIStreamInfo;
1225            BITMAPINFOHEADER myBitmapInfoHeader;
1226    #endif
1227    
1228            if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
1229                    in_file = stdin;
1230            } else {
1231    #ifdef XVID_AVI_INPUT
1232          if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
1233              strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
1234                      ARG_INPUTTYPE==2)
1235          {
1236                      AVISTREAMINFO avi_info;
1237                      FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
1238    
1239                      if (ARG_NUM_APP_THREADS > 1)
1240                              CoInitializeEx(0, COINIT_MULTITHREADED);
1241    
1242                      if (avi_fp == NULL) {
1243                              fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
1244                              return;
1245                      }
1246                      fclose(avi_fp);
1247    
1248                      if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
1249                              fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
1250                              AVIFileExit();
1251                              return;
1252                      }
1253    
1254                      if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
1255                              fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
1256                              AVIFileRelease(avi_in);
1257                              AVIFileExit();
1258                              return;
1259                      }
1260    
1261                      AVIFileRelease(avi_in);
1262    
1263                      if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
1264                              fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE);
1265                              AVIStreamRelease(avi_in_stream);
1266                              AVIFileExit();
1267                              return;
1268                      }
1269    
1270                  if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) {
1271                              LONG size;
1272                              fprintf(stderr, "Non YV12 input colorspace %c%c%c%c! Attempting conversion...\n",
1273                                      avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256,
1274                                      (avi_info.fccHandler>>24)%256);
1275                              size = sizeof(myBitmapInfoHeader);
1276                              AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size);
1277                              if (size==0)
1278                                      fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
1279                              else {
1280                                      fprintf(stderr, "AVIStreamReadFormat read %d bytes.\n", size);
1281                                      fprintf(stderr, "width = %d, height = %d, planes = %d\n", myBitmapInfoHeader.biWidth,
1282                                              myBitmapInfoHeader.biHeight, myBitmapInfoHeader.biPlanes);
1283                                      fprintf(stderr, "Compression = %c%c%c%c, %d\n",
1284                                              myBitmapInfoHeader.biCompression%256, (myBitmapInfoHeader.biCompression>>8)%256,
1285                                              (myBitmapInfoHeader.biCompression>>16)%256, (myBitmapInfoHeader.biCompression>>24)%256,
1286                                              myBitmapInfoHeader.biCompression);
1287                                      fprintf(stderr, "Bits Per Pixel = %d\n", myBitmapInfoHeader.biBitCount);
1288                                      myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
1289                                      myBitmapInfoHeader.biBitCount = 12;
1290                                      myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
1291                                      get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader);
1292                              }
1293                              if (get_frame == NULL) {
1294                                    AVIStreamRelease(avi_in_stream);
1295                                    AVIFileExit();
1296                                    return;
1297                              }
1298                              else {
1299                                    unsigned char *temp;
1300                                    fprintf(stderr, "AVIStreamGetFrameOpen successful.\n");
1301                                    temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);
1302                                    if (temp != NULL) {
1303                                            int i;
1304                                            for (i = 0; i < ((DWORD*)temp)[0]; i++) {
1305                                                    fprintf(stderr, "%2d ", temp[i]);
1306                                            }
1307                                            fprintf(stderr, "\n");
1308                                    }
1309                              }
1310                              if (avi_info.fccHandler == MAKEFOURCC('D', 'I', 'B', ' ')) {
1311                                      AVIStreamGetFrameClose(get_frame);
1312                                      get_frame = NULL;
1313                                      ARG_COLORSPACE = XVID_CSP_BGR | XVID_CSP_VFLIP;
1314                              }
1315                      }
1316        }
1317        else
1318    #endif
1319                    {
1320                            in_file = fopen(ARG_INPUTFILE, "rb");
1321                            if (in_file == NULL) {
1322                                    fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
1323                                    return;
1324                            }
1325                    }
1326            }
1327    
1328            // This should be after the avi input opening stuff
1329            if (ARG_TIMECODEFILE != NULL) {
1330                    time_file = fopen(ARG_TIMECODEFILE, "r");
1331                    if (time_file==NULL) {
1332                            fprintf(stderr, "Couldn't open timecode file '%s'!\n", ARG_TIMECODEFILE);
1333                            return;
1334                    }
1335                    else {
1336                            fscanf(time_file, "# timecode format v2\n");
1337                    }
1338            }
1339    
1340          if (ARG_INPUTTYPE==1) {          if (ARG_INPUTTYPE==1) {
1341  #ifndef READ_PNM  #ifndef READ_PNM
1342                  if (read_pgmheader(in_file)) {                  if (read_pgmheader(in_file)) {
# Line 984  Line 1345 
1345  #endif  #endif
1346                          fprintf(stderr,                          fprintf(stderr,
1347                                          "Wrong input format, I want YUV encapsulated in PGM\n");                                          "Wrong input format, I want YUV encapsulated in PGM\n");
1348                          return (-1);                          return;
1349                  }                  }
1350          }          }
1351    
1352          /* Jump to the starting frame */          /* Jump to the starting frame */
1353          if (ARG_INPUTTYPE == 0)          if (ARG_INPUTTYPE == 0) /* TODO: Other input formats ??? */
1354                  fseek(in_file, ARG_STARTFRAMENR*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);                  fseek(in_file, start_num*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);
1355    
1356    
1357          /* now we know the sizes, so allocate memory */          /* now we know the sizes, so allocate memory */
1358          if (get_frame == NULL)          if (get_frame == NULL)
# Line 1006  Line 1368 
1368                  goto free_all_memory;                  goto free_all_memory;
1369    
1370  /*****************************************************************************  /*****************************************************************************
1371   *                            XviD PART  Start   *                            Xvid PART  Start
1372   ****************************************************************************/   ****************************************************************************/
1373    
1374    
1375          result = enc_init(use_assembler);          result = enc_init(&enc_handle, h->statsfilename1, h->start_num);
1376          if (result) {          if (result) {
1377                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);
1378                  goto release_all;                  goto release_all;
# Line 1022  Line 1384 
1384    
1385          if (ARG_SAVEMPEGSTREAM) {          if (ARG_SAVEMPEGSTREAM) {
1386    
1387                  if (ARG_OUTPUTFILE) {                  if (outfilename) {
1388                          if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {                          if ((out_file = fopen(outfilename, "w+b")) == NULL) {
1389                                  fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);                                  fprintf(stderr, "Error opening output file %s\n", outfilename);
1390                                  goto release_all;                                  goto release_all;
1391                          }                          }
1392                  }                  }
# Line 1120  Line 1482 
1482          result = 0;          result = 0;
1483    
1484          input_num = 0;                          /* input frame counter */          input_num = 0;                          /* input frame counter */
1485          output_num = 0;                         /* output frame counter */          output_num = start_num;             /* output frame counter */
1486    
1487          nvop_counter = 0;          nvop_counter = 0;
1488    
# Line 1129  Line 1491 
1491                  char *type;                  char *type;
1492                  int sse[3];                  int sse[3];
1493    
1494                  if (input_num >= ARG_MAXFRAMENR && ARG_MAXFRAMENR > 0) {                  if ((input_num+start_num) >= stop_num && stop_num > 0) {
1495                          result = 1;                          result = 1;
1496                  }                  }
1497    
# Line 1138  Line 1500 
1500                          if (ARG_INPUTTYPE==2) {                          if (ARG_INPUTTYPE==2) {
1501                                  /* read avs/avi data (YUV-format) */                                  /* read avs/avi data (YUV-format) */
1502                                  if (get_frame != NULL) {                                  if (get_frame != NULL) {
1503                                          in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+ARG_STARTFRAMENR);                                          in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+start_num);
1504                                          if (in_buffer == NULL)                                          if (in_buffer == NULL)
1505                                                  result = 1;                                                  result = 1;
1506                                          else                                          else
1507                                                  in_buffer += ((DWORD*)in_buffer)[0];                                                  in_buffer += ((DWORD*)in_buffer)[0];
1508                                  } else {                                  } else {
1509                                          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)
1510                                                  result = 1;                                                  result = 1;
1511                                  }                                  }
1512                          } else                          } else
# Line 1166  Line 1528 
1528   *                       Encode and decode this frame   *                       Encode and decode this frame
1529   ****************************************************************************/   ****************************************************************************/
1530    
1531                  if (input_num >= (unsigned int)ARG_MAXFRAMENR-1 && ARG_MAXBFRAMES) {                  if ((input_num+start_num) >= (unsigned int)(stop_num-1) && ARG_MAXBFRAMES) {
1532                          stats_type = XVID_TYPE_PVOP;                          stats_type = XVID_TYPE_PVOP;
1533                  }                  }
1534                  else                  else
# Line 1174  Line 1536 
1536    
1537                  enctime = msecond();                  enctime = msecond();
1538                  m4v_size =                  m4v_size =
1539                          enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type,                          enc_main(enc_handle, !result ? in_buffer : 0, mp4_buffer, &key, &stats_type,
1540                                           &stats_quant, &stats_length, sse, input_num);                                           &stats_quant, &stats_length, sse, input_num);
1541                  enctime = msecond() - enctime;                  enctime = msecond() - enctime;
1542    
# Line 1202  Line 1564 
1564                          }                          }
1565    
1566                          if (stats_length > 8) {                          if (stats_length > 8) {
1567                                  framestats[stats_type].count++;                                  h->framestats[stats_type].count++;
1568                                  framestats[stats_type].quants[stats_quant]++;                                  h->framestats[stats_type].quants[stats_quant]++;
1569                                  framestats[stats_type].size += stats_length;                                  h->framestats[stats_type].size += stats_length;
1570                          }                          }
1571                          else {                          else {
1572                                  framestats[5].count++;                                  h->framestats[5].count++;
1573                                  framestats[5].quants[stats_quant]++;                                  h->framestats[5].quants[stats_quant]++;
1574                                  framestats[5].size += stats_length;                                  h->framestats[5].size += stats_length;
1575                          }                          }
1576    
1577  #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)))))
1578    
1579                          if (ARG_PROGRESS == 0) {                          if (ARG_PROGRESS == 0) {
1580                                  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,
1581                                          key, (float) enctime, (int) m4v_size);                                          key, (float) enctime, (int) m4v_size);
1582                                  printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,                                  printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,
1583                                     stats_length);                                     stats_length);
# Line 1228  Line 1590 
1590                                  }                                  }
1591                                  printf("\n");                                  printf("\n");
1592                          } else {                          } else {
1593                                  if (input_num % ARG_PROGRESS == 1) {                                  if ((input_num) % ARG_PROGRESS == 1) {
1594                                          if (ARG_MAXFRAMENR > 0) {                                          if (stop_num > 0) {
1595                                                  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", \
1596                                                          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), \
1597                                                          (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);                                                          ((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num));
1598                                          } else {                                          } else {
1599                                                  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", \
1600                                                          input_num, input_num*1000/totalenctime, \                                                          (ARG_NUM_APP_THREADS*input_num), (ARG_NUM_APP_THREADS*input_num)*1000/(totalenctime), \
1601                                                          (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);                                                          ((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num));
1602                                          }                                          }
1603                                  }                                  }
1604                          }                          }
# Line 1300  Line 1662 
1662                                          removedivxp((char*)mp4_buffer, m4v_size);                                          removedivxp((char*)mp4_buffer, m4v_size);
1663    
1664                                  /* Save ES stream */                                  /* Save ES stream */
1665                                  if (ARG_OUTPUTFILE && out_file && !(fakenvop && m4v_size <= 8)) {                                  if (outfilename && out_file && !(fakenvop && m4v_size <= 8)) {
1666                                                  fwrite(mp4_buffer, 1, m4v_size, out_file);                                                  fwrite(mp4_buffer, 1, m4v_size, out_file);
1667                                  }                                  }
1668  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
# Line 1314  Line 1676 
1676                                  fakenvop=0;                                  fakenvop=0;
1677                  }                  }
1678    
1679                  input_num++;                  if (!result)
1680                            (input_num)++;
1681    
1682                  /* Read the header if it's pgm stream */                  /* Read the header if it's pgm stream */
1683                  if (!result && (ARG_INPUTTYPE==1))                  if (!result && (ARG_INPUTTYPE==1))
# Line 1326  Line 1689 
1689          } while (1);          } while (1);
1690    
1691    
   
 /*****************************************************************************  
  *         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  
  ****************************************************************************/  
   
1692    release_all:    release_all:
1693    
1694      h->input_num = input_num;
1695      h->totalenctime = totalenctime;
1696      h->totalsize = totalsize;
1697    
1698  #ifdef XVID_AVI_INPUT  #ifdef XVID_AVI_INPUT
1699          if (get_frame) AVIStreamGetFrameClose(get_frame);          if (get_frame) AVIStreamGetFrameClose(get_frame);
1700          if (avi_stream) AVIStreamRelease(avi_stream);          if (avi_in_stream) AVIStreamRelease(avi_in_stream);
 #ifndef XVID_AVI_OUTPUT  
         AVIFileExit();  
 #endif  
1701  #endif  #endif
1702    
1703          if (enc_handle) {          if (enc_handle) {
1704                  result = enc_stop();                  result = enc_stop(enc_handle);
1705                  if (result)                  if (result)
1706                          fprintf(stderr, "Encore RELEASE problem return value %d\n",                          fprintf(stderr, "Encore RELEASE problem return value %d\n",
1707                                          result);                                          result);
# Line 1409  Line 1711 
1711                  fclose(in_file);                  fclose(in_file);
1712          if (out_file)          if (out_file)
1713                  fclose(out_file);                  fclose(out_file);
1714            if (time_file)
1715                    fclose(time_file);
1716    
1717  #ifdef XVID_AVI_OUTPUT  #ifdef XVID_AVI_OUTPUT
1718          if (myAVIStream) AVIStreamRelease(myAVIStream);          if (myAVIStream) AVIStreamRelease(myAVIStream);
1719          if (myAVIFile) AVIFileRelease(myAVIFile);          if (myAVIFile) AVIFileRelease(myAVIFile);
         AVIFileExit();  
1720  #endif  #endif
1721  #ifdef XVID_MKV_OUTPUT  #ifdef XVID_MKV_OUTPUT
1722          if (myMKVStream) MKVStreamRelease(myMKVStream);          if (myMKVStream) MKVStreamRelease(myMKVStream);
# Line 1424  Line 1727 
1727          free(out_buffer);          free(out_buffer);
1728          free(mp4_buffer);          free(mp4_buffer);
1729          free(in_buffer);          free(in_buffer);
   
         return (0);  
   
1730  }  }
1731    
   
1732  /*****************************************************************************  /*****************************************************************************
1733   *                        "statistical" functions   *                        "statistical" functions
1734   *   *
1735   *  these are not needed for encoding or decoding, but for measuring   *  these are not needed for encoding or decoding, but for measuring
1736   *  time and quality, there in nothing specific to XviD in these   *  time and quality, there in nothing specific to Xvid in these
1737   *   *
1738   *****************************************************************************/   *****************************************************************************/
1739    
# Line 1616  Line 1915 
1915   *                       Input and output functions   *                       Input and output functions
1916   *   *
1917   *      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
1918   *      image. It's just for convenience, again nothing specific to XviD   *      image. It's just for convenience, again nothing specific to Xvid
1919   *   *
1920   *****************************************************************************/   *****************************************************************************/
1921    
# Line 1778  Line 2077 
2077    
2078  #define FRAMERATE_INCR 1001  #define FRAMERATE_INCR 1001
2079    
2080    /* Gobal encoder init, once per process */
2081  /* Initialize encoder for first use, pass all needed parameters to the codec */  void
2082  static int  enc_gbl(int use_assembler)
 enc_init(int use_assembler)  
2083  {  {
         int xerr;  
         //xvid_plugin_cbr_t cbr;  
     xvid_plugin_single_t single;  
         xvid_plugin_2pass1_t rc2pass1;  
         xvid_plugin_2pass2_t rc2pass2;  
         xvid_plugin_ssim_t ssim;  
         xvid_plugin_lumimasking_t masking;  
         //xvid_plugin_fixed_t rcfixed;  
         xvid_enc_plugin_t plugins[8];  
2084          xvid_gbl_init_t xvid_gbl_init;          xvid_gbl_init_t xvid_gbl_init;
         xvid_enc_create_t xvid_enc_create;  
         int i;  
2085    
2086          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
2087           * XviD core initialization           * Xvid core initialization
2088           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
2089    
2090          /* Set version -- version checking will done by xvidcore */          /* Set version -- version checking will done by xvidcore */
# Line 1818  Line 2105 
2105                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
2106          }          }
2107    
2108          /* Initialize XviD core -- Should be done once per __process__ */          /* Initialize Xvid core -- Should be done once per __process__ */
2109          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);          xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
2110        ARG_CPU_FLAGS = xvid_gbl_init.cpu_flags;
2111          enc_info();          enc_info();
2112    }
2113    
2114    /* Initialize encoder for first use, pass all needed parameters to the codec */
2115    static int
2116    enc_init(void **enc_handle, char *stats_pass1, int start_num)
2117    {
2118            int xerr;
2119            //xvid_plugin_cbr_t cbr;
2120        xvid_plugin_single_t single;
2121            xvid_plugin_2pass1_t rc2pass1;
2122            xvid_plugin_2pass2_t rc2pass2;
2123            xvid_plugin_ssim_t ssim;
2124            xvid_plugin_lumimasking_t masking;
2125            //xvid_plugin_fixed_t rcfixed;
2126            xvid_enc_plugin_t plugins[8];
2127            xvid_enc_create_t xvid_enc_create;
2128            int i;
2129    
2130          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
2131           * XviD encoder initialization           * Xvid encoder initialization
2132           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
2133    
2134          /* Version again */          /* Version again */
# Line 1886  Line 2191 
2191                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
2192          }          }
2193    
2194          if (ARG_PASS1) {          if (stats_pass1) {
2195                  memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));                  memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));
2196                  rc2pass1.version = XVID_VERSION;                  rc2pass1.version = XVID_VERSION;
2197                  rc2pass1.filename = ARG_PASS1;                  rc2pass1.filename = stats_pass1;
2198                  if (ARG_FULL1PASS)                  if (ARG_FULL1PASS)
2199                          prepare_full1pass_zones();                          prepare_full1pass_zones();
2200                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;
# Line 1939  Line 2244 
2244                          ssim.stat_path = ARG_SSIM_PATH;                          ssim.stat_path = ARG_SSIM_PATH;
2245                  }                  }
2246    
2247          ssim.cpu_flags = xvid_gbl_init.cpu_flags;          ssim.cpu_flags = ARG_CPU_FLAGS;
2248                  ssim.b_visualize = 0;                  ssim.b_visualize = 0;
2249                  plugins[xvid_enc_create.num_plugins].param = &ssim;                  plugins[xvid_enc_create.num_plugins].param = &ssim;
2250                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
# Line 1981  Line 2286 
2286          /* Frame drop ratio */          /* Frame drop ratio */
2287          xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;          xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;
2288    
2289            /* Start frame number */
2290            xvid_enc_create.start_frame_num = start_num;
2291    
2292          /* Global encoder options */          /* Global encoder options */
2293          xvid_enc_create.global = 0;          xvid_enc_create.global = 0;
2294    
# Line 1997  Line 2305 
2305          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
2306    
2307          /* Retrieve the encoder instance from the structure */          /* Retrieve the encoder instance from the structure */
2308          enc_handle = xvid_enc_create.handle;          *enc_handle = xvid_enc_create.handle;
2309    
2310          free(xvid_enc_create.zones);          free(xvid_enc_create.zones);
2311    
# Line 2040  Line 2348 
2348                  fprintf(stderr, "TSC ");                  fprintf(stderr, "TSC ");
2349          fprintf(stderr, "\n");          fprintf(stderr, "\n");
2350          fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);          fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);
2351          if (!ARG_THREADS)          ARG_NUM_APP_THREADS = xvid_gbl_info.num_threads;
2352                  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);  
2353          return ret;          return ret;
2354  }  }
2355    
2356  static int  static int
2357  enc_stop()  enc_stop(void *enc_handle)
2358  {  {
2359          int xerr;          int xerr;
2360    
# Line 2058  Line 2365 
2365  }  }
2366    
2367  static int  static int
2368  enc_main(unsigned char *image,  enc_main(void *enc_handle,
2369                     unsigned char *image,
2370                   unsigned char *bitstream,                   unsigned char *bitstream,
2371                   int *key,                   int *key,
2372                   int *stats_type,                   int *stats_type,

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

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