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

Diff of /xvidcore/examples/xvid_encraw.c

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

revision 1.11.2.23, Thu May 15 17:21:08 2003 UTC revision 1.23, Tue Jan 17 19:06:25 2006 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - Console based test application  -   *  - Console based test application  -
5   *   *
6   *  Copyright(C) 2002-2003 Christoph Lampert   *  Copyright(C) 2002-2003 Christoph Lampert <gruel@web.de>
7     *               2002-2003 Edouard Gomez <ed.gomez@free.fr>
8     *               2003      Peter Ross <pross@xvid.org>
9   *   *
10   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
11   *  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 32  Line 34 
34   *  The program is plain C and needs no libraries except for libxvidcore,   *  The program is plain C and needs no libraries except for libxvidcore,
35   *  and maths-lib.   *  and maths-lib.
36   *   *
37     *  Use ./xvid_encraw -help for a list of options
38     *
39   ************************************************************************/   ************************************************************************/
40    
41  #include <stdio.h>  #include <stdio.h>
# Line 41  Line 45 
45  #ifndef WIN32  #ifndef WIN32
46  #include <sys/time.h>  #include <sys/time.h>
47  #else  #else
48    #include <windows.h>
49    #include <vfw.h>
50  #include <time.h>  #include <time.h>
51    #define XVID_AVI_INPUT
52  #endif  #endif
53    
54  #include "xvid.h"  #include "xvid.h"
55    
56    #undef READ_PNM
57    
58  /*****************************************************************************  /*****************************************************************************
59   *                            Quality presets   *                            Quality presets
60   ****************************************************************************/   ****************************************************************************/
61  static xvid_motion_t const motion_presets[] = {  
62    static const int motion_presets[] = {
63          /* quality 0 */          /* quality 0 */
64          0,          0,
65    
# Line 67  Line 76 
76          /* quality 4 */          /* quality 4 */
77          XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 |          XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 |
78          XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 |          XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 |
79          XVID_ME_CHROMA16 | XVID_ME_CHROMA8,          XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP,
80    
81          /* quality 5 */          /* quality 5 */
82          XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 |          XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 |
83          XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 |          XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 |
84          XVID_ME_CHROMA16 | XVID_ME_CHROMA8,          XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP,
85    
86          /* quality 6 */          /* quality 6 */
87          XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 |          XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 |
88          XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH8 |          XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH8 |
89          XVID_ME_CHROMA16 | XVID_ME_CHROMA8 ,          XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP,
90    
91  };  };
92  #define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0]))  #define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0]))
93    
94  static xvid_vop_t const vop_presets[] = {  static const int vop_presets[] = {
95          /* quality 0 */          /* quality 0 */
96          0,          0,
97    
98          /* quality 1 */          /* quality 1 */
99          XVID_VOP_DYNAMIC_BFRAMES,          0,
100    
101          /* quality 2 */          /* quality 2 */
102          XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL,          XVID_VOP_HALFPEL,
103    
104          /* quality 3 */          /* quality 3 */
105          XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL |          XVID_VOP_HALFPEL | XVID_VOP_INTER4V,
         XVID_VOP_INTER4V,  
106    
107          /* quality 4 */          /* quality 4 */
108          XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL |          XVID_VOP_HALFPEL | XVID_VOP_INTER4V,
         XVID_VOP_INTER4V,  
109    
110          /* quality 5 */          /* quality 5 */
111          XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL |          XVID_VOP_HALFPEL | XVID_VOP_INTER4V |
112          XVID_VOP_INTER4V | XVID_VOP_TRELLISQUANT,          XVID_VOP_TRELLISQUANT,
113    
114          /* quality 6 */          /* quality 6 */
115          XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL |          XVID_VOP_HALFPEL | XVID_VOP_INTER4V |
116          XVID_VOP_INTER4V | XVID_VOP_TRELLISQUANT |          XVID_VOP_TRELLISQUANT | XVID_VOP_HQACPRED,
         XVID_VOP_HQACPRED,  
117    
118  };  };
119  #define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0]))  #define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0]))
# Line 122  Line 128 
128  static int NUM_ZONES = 0;  static int NUM_ZONES = 0;
129    
130  /* Maximum number of frames to encode */  /* Maximum number of frames to encode */
131  #define ABS_MAXFRAMENR 9999  #define ABS_MAXFRAMENR -1 /* no limit */
132    
133  static int ARG_STATS = 0;  static int ARG_STATS = 0;
134  static int ARG_DUMP = 0;  static int ARG_DUMP = 0;
# Line 134  Line 140 
140  static int ARG_QUALITY = ME_ELEMENTS - 1;  static int ARG_QUALITY = ME_ELEMENTS - 1;
141  static float ARG_FRAMERATE = 25.00f;  static float ARG_FRAMERATE = 25.00f;
142  static int ARG_MAXFRAMENR = ABS_MAXFRAMENR;  static int ARG_MAXFRAMENR = ABS_MAXFRAMENR;
143    static int ARG_MAXKEYINTERVAL = 0;
144  static char *ARG_INPUTFILE = NULL;  static char *ARG_INPUTFILE = NULL;
145  static int ARG_INPUTTYPE = 0;  static int ARG_INPUTTYPE = 0;
146  static int ARG_SAVEMPEGSTREAM = 0;  static int ARG_SAVEMPEGSTREAM = 0;
147    static int ARG_SAVEINDIVIDUAL = 0;
148  static char *ARG_OUTPUTFILE = NULL;  static char *ARG_OUTPUTFILE = NULL;
149  static int XDIM = 0;  static int XDIM = 0;
150  static int YDIM = 0;  static int YDIM = 0;
# Line 145  Line 153 
153  static int ARG_MAXBFRAMES = 0;  static int ARG_MAXBFRAMES = 0;
154  static int ARG_PACKED = 0;  static int ARG_PACKED = 0;
155  static int ARG_DEBUG = 0;  static int ARG_DEBUG = 0;
156    static int ARG_VOPDEBUG = 0;
157    static int ARG_GREYSCALE = 0;
158    static int ARG_QTYPE = 0;
159    static int ARG_QMATRIX = 0;
160    static int ARG_GMC = 0;
161    static int ARG_INTERLACING = 0;
162    static int ARG_QPEL = 0;
163    static int ARG_TURBO = 0;
164    static int ARG_VHQMODE = 0;
165    static int ARG_BVHQ = 0;
166    static int ARG_CLOSED_GOP = 0;
167    
168    #ifndef READ_PNM
169  #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)  #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)
170    #else
171    #define IMAGE_SIZE(x,y) ((x)*(y)*3)
172    #endif
173    
174  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
175  #define SMALL_EPS (1e-10)  #define SMALL_EPS (1e-10)
# Line 166  Line 189 
189  /* Internal structures (handles) for encoding and decoding */  /* Internal structures (handles) for encoding and decoding */
190  static void *enc_handle = NULL;  static void *enc_handle = NULL;
191    
192    static unsigned char qmatrix_intra[64];
193    static unsigned char qmatrix_inter[64];
194    
195    #ifdef XVID_AVI_INPUT
196    static PAVISTREAM avi_stream = NULL;
197    #endif
198    
199  /*****************************************************************************  /*****************************************************************************
200   *               Local prototypes   *               Local prototypes
201   ****************************************************************************/   ****************************************************************************/
# Line 177  Line 207 
207  static double msecond();  static double msecond();
208    
209  /* PGM related functions */  /* PGM related functions */
210    #ifndef READ_PNM
211  static int read_pgmheader(FILE * handle);  static int read_pgmheader(FILE * handle);
212  static int read_pgmdata(FILE * handle,  static int read_pgmdata(FILE * handle,
213                                                  unsigned char *image);                                                  unsigned char *image);
214    #else
215    static int read_pnmheader(FILE * handle);
216    static int read_pnmdata(FILE * handle,
217                                                    unsigned char *image);
218    #endif
219  static int read_yuvdata(FILE * handle,  static int read_yuvdata(FILE * handle,
220                                                  unsigned char *image);                                                  unsigned char *image);
221    
# Line 218  Line 254 
254          int stats_type;          int stats_type;
255          int stats_quant;          int stats_quant;
256          int stats_length;          int stats_length;
257          int use_assembler = 0;          int use_assembler = 1;
258    
259          int input_num;          int input_num;
260          int output_num;          int output_num;
# Line 245  Line 281 
281    
282                  if (strcmp("-asm", argv[i]) == 0) {                  if (strcmp("-asm", argv[i]) == 0) {
283                          use_assembler = 1;                          use_assembler = 1;
284                    } else if (strcmp("-noasm", argv[i]) == 0) {
285                            use_assembler = 0;
286                  } else if (strcmp("-w", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-w", argv[i]) == 0 && i < argc - 1) {
287                          i++;                          i++;
288                          XDIM = atoi(argv[i]);                          XDIM = atoi(argv[i]);
# Line 291  Line 329 
329                  } else if (strcmp("-quality", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-quality", argv[i]) == 0 && i < argc - 1) {
330                          i++;                          i++;
331                          ARG_QUALITY = atoi(argv[i]);                          ARG_QUALITY = atoi(argv[i]);
332                    } else if (strcmp("-vhqmode", argv[i]) == 0 && i < argc - 1) {
333                            i++;
334                            ARG_VHQMODE = atoi(argv[i]);
335                  } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) {
336                          i++;                          i++;
337                          ARG_FRAMERATE = (float) atof(argv[i]);                          ARG_FRAMERATE = (float) atof(argv[i]);
338                    } else if (strcmp("-max_key_interval", argv[i]) == 0 && i < argc - 1) {
339                            i++;
340                            ARG_MAXKEYINTERVAL = atoi(argv[i]);
341                  } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1) {
342                          i++;                          i++;
343                          ARG_INPUTFILE = argv[i];                          ARG_INPUTFILE = argv[i];
# Line 306  Line 350 
350                  } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {
351                          i++;                          i++;
352                          ARG_INPUTTYPE = atoi(argv[i]);                          ARG_INPUTTYPE = atoi(argv[i]);
353                  } else if (strcmp("-nframes", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-frames", argv[i]) == 0 && i < argc - 1) {
354                          i++;                          i++;
355                          ARG_MAXFRAMENR = atoi(argv[i]);                          ARG_MAXFRAMENR = atoi(argv[i]);
356                    } else if (strcmp("-qtype", argv[i]) == 0 && i < argc - 1) {
357                            i++;
358                            ARG_QTYPE = atoi(argv[i]);
359                    } else if (strcmp("-qmatrix", argv[i]) == 0 && i < argc - 1) {
360                            FILE *fp = fopen(argv[++i], "rb");
361                            if (fp == NULL) {
362                                    fprintf(stderr, "Error opening input file %s\n", argv[i]);
363                                    return (-1);
364                            }
365                            fseek(fp, 0, SEEK_END);
366                            if (ftell(fp) != 128) {
367                                    fprintf(stderr, "Unexpected size of input file %s\n", argv[i]);
368                                    return (-1);
369                            }
370    
371                            fseek(fp, 0, SEEK_SET);
372                            fread(qmatrix_intra, 1, 64, fp);
373                            fread(qmatrix_inter, 1, 64, fp);
374    
375                            ARG_QMATRIX = 1;
376                  } else if (strcmp("-save", argv[i]) == 0) {                  } else if (strcmp("-save", argv[i]) == 0) {
377                          ARG_SAVEMPEGSTREAM = 1;                          ARG_SAVEMPEGSTREAM = 1;
378                            ARG_SAVEINDIVIDUAL = 1;
379                  } else if (strcmp("-debug", argv[i]) == 0) {                  } else if (strcmp("-debug", argv[i]) == 0) {
380                          ARG_DEBUG = 1;                          i++;
381                if (sscanf(argv[i],"0x%x", &ARG_DEBUG) || sscanf(argv[i],"%d", &ARG_DEBUG)) ;
382                  } else if (strcmp("-o", argv[i]) == 0 && i < argc - 1) {                  } else if (strcmp("-o", argv[i]) == 0 && i < argc - 1) {
383                            ARG_SAVEMPEGSTREAM = 1;
384                          i++;                          i++;
385                          ARG_OUTPUTFILE = argv[i];                          ARG_OUTPUTFILE = argv[i];
386                    } else if (strcmp("-vop_debug", argv[i]) == 0) {
387                            ARG_VOPDEBUG = 1;
388                    } else if (strcmp("-grey", argv[i]) == 0) {
389                            ARG_GREYSCALE = 1;
390                    } else if (strcmp("-bvhq", argv[i]) == 0) {
391                            ARG_BVHQ = 1;
392                    } else if (strcmp("-qpel", argv[i]) == 0) {
393                            ARG_QPEL = 1;
394                    } else if (strcmp("-turbo", argv[i]) == 0) {
395                            ARG_TURBO = 1;
396                    } else if (strcmp("-gmc", argv[i]) == 0) {
397                            ARG_GMC = 1;
398                    } else if (strcmp("-interlaced", argv[i]) == 0) {
399                            ARG_INTERLACING = 1;
400                    } else if (strcmp("-closed_gop", argv[i]) == 0) {
401                            ARG_CLOSED_GOP = 1;
402                  } else if (strcmp("-help", argv[i])) {                  } else if (strcmp("-help", argv[i])) {
403                          usage();                          usage();
404                          return (0);                          return (0);
# Line 330  Line 413 
413   *                            Arguments checking   *                            Arguments checking
414   ****************************************************************************/   ****************************************************************************/
415    
416          if (XDIM <= 0 || XDIM >= 2048 || YDIM <= 0 || YDIM >= 2048) {          if (XDIM <= 0 || XDIM >= 4096 || YDIM <= 0 || YDIM >= 4096) {
417                  fprintf(stderr,                  fprintf(stderr,
418                                  "Trying to retreive width and height from PGM header\n");                                  "Trying to retrieve width and height from input header\n");
419                    if (!ARG_INPUTTYPE)
420                  ARG_INPUTTYPE = 1;              /* pgm */                  ARG_INPUTTYPE = 1;              /* pgm */
421          }          }
422    
# Line 347  Line 431 
431                  return (-1);                  return (-1);
432          }          }
433    
434          if (ARG_MAXFRAMENR <= 0) {          if (ARG_MAXFRAMENR == 0) {
435                  fprintf(stderr, "Wrong number of frames\n");                  fprintf(stderr, "Wrong number of frames\n");
436                  return (-1);                  return (-1);
437          }          }
# Line 355  Line 439 
439          if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
440                  in_file = stdin;                  in_file = stdin;
441          } else {          } else {
442    #ifdef XVID_AVI_INPUT
443          if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
444              strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
445                      ARG_INPUTTYPE==2)
446          {
447                      AVISTREAMINFO avi_info;
448                      FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
449                      if (avi_fp == NULL) {
450                              fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
451                              return (-1);
452                      }
453                      fclose(avi_fp);
454    
455                      AVIFileInit();
456                      if (AVIStreamOpenFromFile(&avi_stream, ARG_INPUTFILE, streamtypeVIDEO, 0, OF_READ, NULL) != AVIERR_OK) {
457                              fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
458                              AVIFileExit();
459                              return (-1);
460                      }
461    
462                      if(AVIStreamInfo(avi_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
463                              fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE);
464                              AVIStreamRelease(avi_stream);
465                              AVIFileExit();
466                              return (-1);
467                      }
468    
469                  if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) {
470                              fprintf(stderr, "Unsupported input colorspace! Only YV12 is supported!\n");
471                              AVIStreamRelease(avi_stream);
472                          AVIFileExit();
473                  return (-1);
474                      }
475    
476    
477              if (ARG_MAXFRAMENR<0)
478                            ARG_MAXFRAMENR = avi_info.dwLength;
479                      else
480                            ARG_MAXFRAMENR = min(ARG_MAXFRAMENR, avi_info.dwLength);
481    
482                      XDIM = avi_info.rcFrame.right - avi_info.rcFrame.left;
483                      YDIM = avi_info.rcFrame.bottom - avi_info.rcFrame.top;
484                      ARG_FRAMERATE = (float) avi_info.dwRate / (float) avi_info.dwScale;
485    
486                      ARG_INPUTTYPE = 2;
487        }
488        else
489    #endif
490                    {
491                  in_file = fopen(ARG_INPUTFILE, "rb");                  in_file = fopen(ARG_INPUTFILE, "rb");
492                  if (in_file == NULL) {                  if (in_file == NULL) {
493                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
494                          return (-1);                          return (-1);
495                  }                  }
496          }          }
497            }
498    
499          if (ARG_INPUTTYPE) {          if (ARG_INPUTTYPE==1) {
500    #ifndef READ_PNM
501                  if (read_pgmheader(in_file)) {                  if (read_pgmheader(in_file)) {
502    #else
503                    if (read_pnmheader(in_file)) {
504    #endif
505                          fprintf(stderr,                          fprintf(stderr,
506                                          "Wrong input format, I want YUV encapsulated in PGM\n");                                          "Wrong input format, I want YUV encapsulated in PGM\n");
507                          return (-1);                          return (-1);
# Line 423  Line 560 
560                  char *type;                  char *type;
561                  int sse[3];                  int sse[3];
562    
563                  if (input_num >= ARG_MAXFRAMENR) {                  if (input_num >= ARG_MAXFRAMENR && ARG_MAXFRAMENR > 0) {
564                          result = 1;                          result = 1;
565                  }                  }
566    
567                  if (!result) {                  if (!result) {
568                          if (ARG_INPUTTYPE) {  #ifdef XVID_AVI_INPUT
569                            if (ARG_INPUTTYPE==2) {
570                                    /* read avs/avi data (YUV-format) */
571                                    if(AVIStreamRead(avi_stream, input_num, 1, in_buffer, IMAGE_SIZE(XDIM, YDIM), NULL, NULL ) != AVIERR_OK)
572                                            result = 1;
573                            } else
574    #endif
575                                    if (ARG_INPUTTYPE==1) {
576                                  /* read PGM data (YUV-format) */                                  /* read PGM data (YUV-format) */
577    #ifndef READ_PNM
578                                  result = read_pgmdata(in_file, in_buffer);                                  result = read_pgmdata(in_file, in_buffer);
579    #else
580                                    result = read_pnmdata(in_file, in_buffer);
581    #endif
582                          } else {                          } else {
583                                  /* read raw data (YUV-format) */                                  /* read raw data (YUV-format) */
584                                  result = read_yuvdata(in_file, in_buffer);                                  result = read_yuvdata(in_file, in_buffer);
# Line 449  Line 597 
597    
598                  /* Write the Frame statistics */                  /* Write the Frame statistics */
599    
600                  printf("%5d: key=%i, time= %6.0f, length= %7d", !result ? input_num : -1,                  printf("%5d: key=%i, time= %6.0f, len= %7d", !result ? input_num : -1,
601                             key, (float) enctime, (int) m4v_size);                             key, (float) enctime, (int) m4v_size);
602    
603                  if (stats_type > 0) {   /* !XVID_TYPE_NOTHING */                  if (stats_type > 0) {   /* !XVID_TYPE_NOTHING */
# Line 472  Line 620 
620                                  break;                                  break;
621                          }                          }
622    
623                          printf(" | type=%s, quant= %2d, length= %7d", type, stats_quant,                          printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,
624                                     stats_length);                                     stats_length);
625    
626  #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)))))
# Line 506  Line 654 
654   ****************************************************************************/   ****************************************************************************/
655    
656                  if (m4v_size > 0 && ARG_SAVEMPEGSTREAM) {                  if (m4v_size > 0 && ARG_SAVEMPEGSTREAM) {
657    
658                          /* Save single files */                          /* Save single files */
659                          if (out_file == NULL) {                          if (ARG_SAVEINDIVIDUAL) {
660                                    FILE *out;
661                                  sprintf(filename, "%sframe%05d.m4v", filepath, output_num);                                  sprintf(filename, "%sframe%05d.m4v", filepath, output_num);
662                                  out_file = fopen(filename, "wb");                                  out = fopen(filename, "w+b");
663                                  fwrite(mp4_buffer, m4v_size, 1, out_file);                                  fwrite(mp4_buffer, m4v_size, 1, out);
664                                  fclose(out_file);                                  fclose(out);
                                 out_file = NULL;  
665                                  output_num++;                                  output_num++;
666                          } else {                          }
667    
668                                  /* Write mp4 data */                          /* Save ES stream */
669                            if (ARG_OUTPUTFILE && out_file)
670                                  fwrite(mp4_buffer, 1, m4v_size, out_file);                                  fwrite(mp4_buffer, 1, m4v_size, out_file);
   
                         }  
671                  }                  }
672    
673                  input_num++;                  input_num++;
674    
675                  /* Read the header if it's pgm stream */                  /* Read the header if it's pgm stream */
676                  if (!result && ARG_INPUTTYPE)                  if (!result && (ARG_INPUTTYPE==1))
677    #ifndef READ_PNM
678                          result = read_pgmheader(in_file);                          result = read_pgmheader(in_file);
679    #else
680                            result = read_pnmheader(in_file);
681    #endif
682          } while (1);          } while (1);
683    
684    
# Line 565  Line 716 
716    
717    release_all:    release_all:
718    
719    #ifdef XVID_AVI_INPUT
720            if (avi_stream) {
721                    AVIStreamRelease(avi_stream);
722                    AVIFileExit();
723            }
724    #endif
725    
726          if (enc_handle) {          if (enc_handle) {
727                  result = enc_stop();                  result = enc_stop();
728                  if (result)                  if (result)
# Line 608  Line 766 
766          clock_t clk;          clock_t clk;
767    
768          clk = clock();          clk = clock();
769          return (clk * 1000 / CLOCKS_PER_SEC);          return (clk * 1000.0 / CLOCKS_PER_SEC);
770  #endif  #endif
771  }  }
772    
# Line 622  Line 780 
780          fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n\n");          fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n\n");
781          fprintf(stderr, "Input options:\n");          fprintf(stderr, "Input options:\n");
782          fprintf(stderr, " -i       string : input filename (default=stdin)\n");          fprintf(stderr, " -i       string : input filename (default=stdin)\n");
783    #ifdef XVID_AVI_INPUT
784            fprintf(stderr, " -type   integer: input data type (yuv=0, pgm=1, avi/avs=2)\n");
785    #else
786          fprintf(stderr, " -type    integer: input data type (yuv=0, pgm=1)\n");          fprintf(stderr, " -type    integer: input data type (yuv=0, pgm=1)\n");
787    #endif
788          fprintf(stderr, " -w       integer: frame width ([1.2048])\n");          fprintf(stderr, " -w       integer: frame width ([1.2048])\n");
789          fprintf(stderr, " -h       integer: frame height ([1.2048])\n");          fprintf(stderr, " -h       integer: frame height ([1.2048])\n");
790          fprintf(stderr, " -nframes integer: number of frames to encode\n");          fprintf(stderr, " -frames integer: number of frames to encode\n");
791          fprintf(stderr, "\n");          fprintf(stderr, "\n");
792          fprintf(stderr, "Output options:\n");          fprintf(stderr, "Output options:\n");
793          fprintf(stderr, " -dump    : save decoder output\n");          fprintf(stderr, " -dump    : save decoder output\n");
794          fprintf(stderr, " -save    : save mpeg4 raw stream\n");          fprintf(stderr, " -save    : save an Elementary Stream file per frame\n");
795          fprintf(stderr, " -o string: output filename\n");          fprintf(stderr, " -o string: save an Elementary Stream for the complete sequence\n");
796          fprintf(stderr, "\n");          fprintf(stderr, "\n");
797          fprintf(stderr, "BFrames options:\n");          fprintf(stderr, "BFrames options:\n");
798          fprintf(stderr, " -max_bframes   integer: max bframes (default=0)\n");          fprintf(stderr, " -max_bframes   integer: max bframes (default=0)\n");
# Line 645  Line 807 
807          fprintf(stderr, " -pass2     filename            : twopass mode (2nd pass)\n");          fprintf(stderr, " -pass2     filename            : twopass mode (2nd pass)\n");
808          fprintf(stderr, " -zq starting_frame float       : bitrate zone; quant\n");          fprintf(stderr, " -zq starting_frame float       : bitrate zone; quant\n");
809          fprintf(stderr, " -zw starting_frame float       : bitrate zone; weight\n");          fprintf(stderr, " -zw starting_frame float       : bitrate zone; weight\n");
810        fprintf(stderr, " -max_key_interval integer      : maximum keyframe interval\n");
811          fprintf(stderr, "\n");          fprintf(stderr, "\n");
812          fprintf(stderr, "Other options\n");          fprintf(stderr, "Other options\n");
813          fprintf(stderr, " -asm            : use assembly optmized code\n");          fprintf(stderr, " -noasm           : do not use assembly optmized code\n");
814            fprintf(stderr, " -turbo           : use turbo presets for higher encoding speed\n");
815          fprintf(stderr, " -quality integer: quality ([0..%d])\n", ME_ELEMENTS - 1);          fprintf(stderr, " -quality integer: quality ([0..%d])\n", ME_ELEMENTS - 1);
816            fprintf(stderr, " -vhqmode integer : level of Rate-Distortion optimizations ([0..4]) (default=0)\n");
817            fprintf(stderr, " -bvhq            : use Rate-Distortion optimizations for B-frames too\n");
818            fprintf(stderr, " -qpel            : use quarter pixel ME\n");
819            fprintf(stderr, " -gmc             : use global motion compensation\n");
820            fprintf(stderr, " -qtype   integer : quantization type (H263:0, MPEG4:1) (default=0)\n");
821            fprintf(stderr, " -qmatrix filename: use custom MPEG4 quantization matrix\n");
822            fprintf(stderr, " -interlaced      : use interlaced encoding (this is NOT a deinterlacer!)\n");
823          fprintf(stderr, " -packed         : packed mode\n");          fprintf(stderr, " -packed         : packed mode\n");
824            fprintf(stderr, " -closed_gop      : closed GOP mode\n");
825            fprintf(stderr, " -grey            : grey scale coding (chroma is discarded)\n");
826          fprintf(stderr, " -lumimasking    : use lumimasking algorithm\n");          fprintf(stderr, " -lumimasking    : use lumimasking algorithm\n");
827          fprintf(stderr, " -stats          : print stats about encoded frames\n");          fprintf(stderr, " -stats          : print stats about encoded frames\n");
828          fprintf(stderr, " -debug          : print all MB dquants\n");          fprintf(stderr, " -debug           : activates xvidcore internal debugging output\n");
829            fprintf(stderr, " -vop_debug       : print some info directly into encoded frames\n");
830          fprintf(stderr, " -help           : prints this help message\n");          fprintf(stderr, " -help           : prints this help message\n");
831          fprintf(stderr, "\n");          fprintf(stderr, "\n");
832          fprintf(stderr, "NB: You can define %d zones repeating the -z[qw] option as many times as needed.\n", MAX_ZONES);          fprintf(stderr, "NB: You can define %d zones repeating the -z[qw] option as many times as needed.\n", MAX_ZONES);
# Line 667  Line 841 
841   *   *
842   *****************************************************************************/   *****************************************************************************/
843    
844    #ifndef READ_PNM
845  static int  static int
846  read_pgmheader(FILE * handle)  read_pgmheader(FILE * handle)
847  {  {
# Line 679  Line 854 
854                  return (1);                  return (1);
855    
856          fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);          fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);
857          if ((xsize > 1440) || (ysize > 2880) || (depth != 255)) {          if ((xsize > 4096) || (ysize > 4096*3/2) || (depth != 255)) {
858                  fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);                  fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);
859                  return (2);                  return (2);
860          }          }
# Line 722  Line 897 
897    
898          return (0);          return (0);
899  }  }
900    #else
901    static int
902    read_pnmheader(FILE * handle)
903    {
904            int bytes, xsize, ysize, depth;
905            char dummy[2];
906    
907            bytes = fread(dummy, 1, 2, handle);
908    
909            if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '6'))
910                    return (1);
911    
912            fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);
913            if ((xsize > 1440) || (ysize > 2880) || (depth != 255)) {
914                    fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);
915                    return (2);
916            }
917    
918            XDIM = xsize;
919            YDIM = ysize;
920    
921            return (0);
922    }
923    
924    static int
925    read_pnmdata(FILE * handle,
926                             unsigned char *image)
927    {
928            int i;
929            char dummy;
930    
931            /* read Y component of picture */
932            fread(image, 1, XDIM * YDIM * 3, handle);
933    
934            /*  I don't know why, but this seems needed */
935            fread(&dummy, 1, 1, handle);
936    
937            return (0);
938    }
939    #endif
940    
941  static int  static int
942  read_yuvdata(FILE * handle,  read_yuvdata(FILE * handle,
# Line 806  Line 1021 
1021          /* Set version -- version checking will done by xvidcore */          /* Set version -- version checking will done by xvidcore */
1022          memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));          memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
1023          xvid_gbl_init.version = XVID_VERSION;          xvid_gbl_init.version = XVID_VERSION;
1024        xvid_gbl_init.debug = ARG_DEBUG;
1025    
1026    
1027          /* Do we have to enable ASM optimizations ? */          /* Do we have to enable ASM optimizations ? */
1028          if (use_assembler) {          if (use_assembler) {
1029    
1030  #ifdef ARCH_IS_IA64  #ifdef ARCH_IS_IA64
1031                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ASM;
1032  #else  #else
1033                  xvid_gbl_init.cpu_flags = 0;                  xvid_gbl_init.cpu_flags = 0;
1034  #endif  #endif
# Line 834  Line 1050 
1050          /* Width and Height of input frames */          /* Width and Height of input frames */
1051          xvid_enc_create.width = XDIM;          xvid_enc_create.width = XDIM;
1052          xvid_enc_create.height = YDIM;          xvid_enc_create.height = YDIM;
1053            xvid_enc_create.profile = XVID_PROFILE_AS_L4;
1054    
1055          /* init plugins  */          /* init plugins  */
1056      xvid_enc_create.zones = ZONES;      xvid_enc_create.zones = ZONES;
# Line 858  Line 1075 
1075                  rc2pass2.filename = ARG_PASS2;                  rc2pass2.filename = ARG_PASS2;
1076                  rc2pass2.bitrate = ARG_BITRATE;                  rc2pass2.bitrate = ARG_BITRATE;
1077    
1078    /*              An example of activating VBV could look like this
1079                    rc2pass2.vbv_size     =  3145728;
1080                    rc2pass2.vbv_initial  =  2359296;
1081                    rc2pass2.vbv_maxrate  =  4000000;
1082                    rc2pass2.vbv_peakrate = 10000000;
1083    */
1084    
1085                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;                  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
1086                  plugins[xvid_enc_create.num_plugins].param = &rc2pass2;                  plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
1087                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
# Line 885  Line 1109 
1109                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
1110          }          }
1111    
1112    #if 0
1113          if (ARG_DEBUG) {          if (ARG_DEBUG) {
1114                  plugins[xvid_enc_create.num_plugins].func = rawenc_debug;                  plugins[xvid_enc_create.num_plugins].func = rawenc_debug;
1115                  plugins[xvid_enc_create.num_plugins].param = NULL;                  plugins[xvid_enc_create.num_plugins].param = NULL;
1116                  xvid_enc_create.num_plugins++;                  xvid_enc_create.num_plugins++;
1117          }          }
1118    #endif
1119    
1120          /* No fancy thread tests */          /* No fancy thread tests */
1121          xvid_enc_create.num_threads = 0;          xvid_enc_create.num_threads = 0;
# Line 904  Line 1130 
1130          }          }
1131    
1132          /* Maximum key frame interval */          /* Maximum key frame interval */
1133        if (ARG_MAXKEYINTERVAL > 0) {
1134            xvid_enc_create.max_key_interval = ARG_MAXKEYINTERVAL;
1135        }else {
1136          xvid_enc_create.max_key_interval = (int) ARG_FRAMERATE *10;          xvid_enc_create.max_key_interval = (int) ARG_FRAMERATE *10;
1137        }
1138    
1139          /* Bframes settings */          /* Bframes settings */
1140          xvid_enc_create.max_bframes = ARG_MAXBFRAMES;          xvid_enc_create.max_bframes = ARG_MAXBFRAMES;
# Line 920  Line 1150 
1150          if (ARG_PACKED)          if (ARG_PACKED)
1151                  xvid_enc_create.global |=XVID_GLOBAL_PACKED;                  xvid_enc_create.global |=XVID_GLOBAL_PACKED;
1152    
1153            if (ARG_CLOSED_GOP)
1154                    xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
1155    
1156          if (ARG_STATS)          if (ARG_STATS)
1157                  xvid_enc_create.global |=XVID_GLOBAL_EXTRASTATS_ENABLE;                  xvid_enc_create.global |=XVID_GLOBAL_EXTRASTATS_ENABLE;
1158    
# Line 971  Line 1204 
1204          /* Initialize input image fields */          /* Initialize input image fields */
1205          if (image) {          if (image) {
1206                  xvid_enc_frame.input.plane[0] = image;                  xvid_enc_frame.input.plane[0] = image;
1207    #ifndef READ_PNM
1208                    if (ARG_INPUTTYPE==2)
1209                            xvid_enc_frame.input.csp = XVID_CSP_YV12;
1210                    else
1211                  xvid_enc_frame.input.csp = XVID_CSP_I420;                  xvid_enc_frame.input.csp = XVID_CSP_I420;
1212                  xvid_enc_frame.input.stride[0] = XDIM;                  xvid_enc_frame.input.stride[0] = XDIM;
1213    #else
1214                    xvid_enc_frame.input.csp = XVID_CSP_BGR;
1215                    xvid_enc_frame.input.stride[0] = XDIM*3;
1216    #endif
1217          } else {          } else {
1218                  xvid_enc_frame.input.csp = XVID_CSP_NULL;                  xvid_enc_frame.input.csp = XVID_CSP_NULL;
1219          }          }
# Line 981  Line 1222 
1222          xvid_enc_frame.vol_flags = 0;          xvid_enc_frame.vol_flags = 0;
1223          if (ARG_STATS)          if (ARG_STATS)
1224                  xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS;                  xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS;
1225            if (ARG_QTYPE)
1226                    xvid_enc_frame.vol_flags |= XVID_VOL_MPEGQUANT;
1227            if (ARG_QPEL)
1228                    xvid_enc_frame.vol_flags |= XVID_VOL_QUARTERPEL;
1229            if (ARG_GMC)
1230                    xvid_enc_frame.vol_flags |= XVID_VOL_GMC;
1231            if (ARG_INTERLACING)
1232                    xvid_enc_frame.vol_flags |= XVID_VOL_INTERLACING;
1233    
1234          /* Set up core's general features */          /* Set up core's general features */
1235          xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY];          xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY];
1236    
1237        if (ARG_VOPDEBUG) {
1238            xvid_enc_frame.vop_flags |= XVID_VOP_DEBUG;
1239        }
1240    
1241        if (ARG_GREYSCALE) {
1242            xvid_enc_frame.vop_flags |= XVID_VOP_GREYSCALE;
1243        }
1244    
1245          /* Frame type -- let core decide for us */          /* Frame type -- let core decide for us */
1246          xvid_enc_frame.type = XVID_TYPE_AUTO;          xvid_enc_frame.type = XVID_TYPE_AUTO;
1247    
# Line 994  Line 1251 
1251          /* Set up motion estimation flags */          /* Set up motion estimation flags */
1252          xvid_enc_frame.motion = motion_presets[ARG_QUALITY];          xvid_enc_frame.motion = motion_presets[ARG_QUALITY];
1253    
1254            if (ARG_GMC)
1255                    xvid_enc_frame.motion |= XVID_ME_GME_REFINE;
1256    
1257            if (ARG_QPEL)
1258                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16;
1259            if (ARG_QPEL && (xvid_enc_frame.vop_flags & XVID_VOP_INTER4V))
1260                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8;
1261    
1262            if (ARG_TURBO)
1263                    xvid_enc_frame.motion |= XVID_ME_FASTREFINE16 | XVID_ME_FASTREFINE8 |
1264                                                                     XVID_ME_SKIP_DELTASEARCH | XVID_ME_FAST_MODEINTERPOLATE |
1265                                                                     XVID_ME_BFRAME_EARLYSTOP;
1266    
1267            if (ARG_BVHQ)
1268                    xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP;
1269    
1270            switch (ARG_VHQMODE) /* this is the same code as for vfw */
1271            {
1272            case 1: /* VHQ_MODE_DECISION */
1273                    xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
1274                    break;
1275    
1276            case 2: /* VHQ_LIMITED_SEARCH */
1277                    xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
1278                    xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD;
1279                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
1280                    break;
1281    
1282            case 3: /* VHQ_MEDIUM_SEARCH */
1283                    xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
1284                    xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD;
1285                    xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE8_RD;
1286                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
1287                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8_RD;
1288                    xvid_enc_frame.motion |= XVID_ME_CHECKPREDICTION_RD;
1289                    break;
1290    
1291            case 4: /* VHQ_WIDE_SEARCH */
1292                    xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
1293                    xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD;
1294                    xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE8_RD;
1295                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
1296                    xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8_RD;
1297                    xvid_enc_frame.motion |= XVID_ME_CHECKPREDICTION_RD;
1298                    xvid_enc_frame.motion |= XVID_ME_EXTSEARCH_RD;
1299                    break;
1300    
1301            default :
1302                    break;
1303            }
1304    
1305            if (ARG_QMATRIX) {
1306                    /* We don't use special matrices */
1307                    xvid_enc_frame.quant_intra_matrix = qmatrix_intra;
1308                    xvid_enc_frame.quant_inter_matrix = qmatrix_inter;
1309            }
1310            else {
1311          /* We don't use special matrices */          /* We don't use special matrices */
1312          xvid_enc_frame.quant_intra_matrix = NULL;          xvid_enc_frame.quant_intra_matrix = NULL;
1313          xvid_enc_frame.quant_inter_matrix = NULL;          xvid_enc_frame.quant_inter_matrix = NULL;
1314            }
1315    
1316          /* Encode the frame */          /* Encode the frame */
1317          ret =          ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
                 xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame,  
1318                                          &xvid_enc_stats);                                          &xvid_enc_stats);
1319    
1320          *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME);          *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME);

Legend:
Removed from v.1.11.2.23  
changed lines
  Added in v.1.23

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