[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.1.2.2, Mon Jan 13 00:36:27 2003 UTC revision 1.3, Sat Sep 28 14:53:40 2002 UTC
# Line 36  Line 36 
36    
37  #include <stdio.h>  #include <stdio.h>
38  #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  
39  #include <math.h>  #include <math.h>
40  #ifndef _MSC_VER  #ifndef _MSC_VER
41  #include <sys/time.h>  #include <sys/time.h>
# Line 51  Line 50 
50   ****************************************************************************/   ****************************************************************************/
51    
52  static int const motion_presets[7] = {  static int const motion_presets[7] = {
53          0,                                                        /* Q 0 */          0,                                                        // Q 0
54          PMV_EARLYSTOP16,                                          /* Q 1 */          PMV_EARLYSTOP16,                                          // Q 1
55          PMV_EARLYSTOP16,                                          /* Q 2 */          PMV_EARLYSTOP16,                                          // Q 2
56          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,                    /* Q 3 */          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,                    // Q 3
57          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,                    /* Q 4 */          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,                    // Q 4
58          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 |  /* Q 5 */          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 |  // Q 5
59          PMV_HALFPELREFINE8,          PMV_HALFPELREFINE8,
60          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | // Q 6
61          PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8          PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8
62  };  };
63    
64  static int const general_presets[7] = {  static int const general_presets[7] = {
65          XVID_H263QUANT,                               /* Q 0 */          XVID_H263QUANT,                               // Q 0
66          XVID_MPEGQUANT,                               /* Q 1 */          XVID_MPEGQUANT,                               // Q 1
67          XVID_H263QUANT,                               /* Q 2 */          XVID_H263QUANT,                               // Q 2
68          XVID_H263QUANT | XVID_HALFPEL,                /* Q 3 */          XVID_H263QUANT | XVID_HALFPEL,                // Q 3
69          XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */          XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 4
70          XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */          XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 5
71          XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V  /* Q 6 */          XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V  // Q 6
72  };  };
73    
74    
# Line 80  Line 79 
79  /* Maximum number of frames to encode */  /* Maximum number of frames to encode */
80  #define ABS_MAXFRAMENR 9999  #define ABS_MAXFRAMENR 9999
81    
 /* HINTMODEs */  
 #define HINT_MODE_NONE 0  
 #define HINT_MODE_GET  1  
 #define HINT_MODE_SET  2  
 #define HINT_FILE "hints.mv"  
   
82  static int   ARG_BITRATE = 900;  static int   ARG_BITRATE = 900;
83  static int   ARG_QUANTI = 0;  static int   ARG_QUANTI = 0;
84  static int   ARG_QUALITY = 6;  static int   ARG_QUALITY = 6;
# Line 98  Line 91 
91  static int   ARG_SAVEMPEGSTREAM = 0;  static int   ARG_SAVEMPEGSTREAM = 0;
92  static int   ARG_OUTPUTTYPE = 0;  static int   ARG_OUTPUTTYPE = 0;
93  static char *ARG_OUTPUTFILE = NULL;  static char *ARG_OUTPUTFILE = NULL;
 static int   ARG_HINTMODE = HINT_MODE_NONE;  
94  static int   XDIM = 0;  static int   XDIM = 0;
95  static int   YDIM = 0;  static int   YDIM = 0;
 static int   ARG_BQRATIO = 150;  
 static int   ARG_BQOFFSET = 100;  
 static int   ARG_MAXBFRAMES = 0;  
96  #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)  #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)
97    
98  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
# Line 146  Line 135 
135  static int enc_init(int use_assembler);  static int enc_init(int use_assembler);
136  static int enc_stop();  static int enc_stop();
137  static int enc_main(unsigned char* image, unsigned char* bitstream,  static int enc_main(unsigned char* image, unsigned char* bitstream,
138                                          unsigned char *hints_buffer,                                          int *streamlength, int* frametype);
                                         long *streamlength, long* frametype, long *hints_size);  
139    
140  /*****************************************************************************  /*****************************************************************************
141   *               Main function   *               Main function
# Line 159  Line 147 
147          unsigned char *mp4_buffer = NULL;          unsigned char *mp4_buffer = NULL;
148          unsigned char *in_buffer = NULL;          unsigned char *in_buffer = NULL;
149          unsigned char *out_buffer = NULL;          unsigned char *out_buffer = NULL;
         unsigned char *hints_buffer = NULL;  
150    
151          double enctime;          double enctime;
152          double totalenctime=0.;          double totalenctime=0.;
153    
154          long totalsize;          long totalsize;
         long hints_size;  
155          int status;          int status;
156          long frame_type;          int frame_type;
157          long bigendian;          int bigendian;
158    
159          long m4v_size;          int m4v_size;
160          int use_assembler=0;          int use_assembler=0;
161    
162          char filename[256];          char filename[256];
163    
164          FILE *in_file = stdin;          FILE *in_file = stdin;
165          FILE *out_file = NULL;          FILE *out_file = NULL;
         FILE *hints_file = NULL;  
166    
167          printf("xvid_encraw - raw mpeg4 bitstream encoder ");          printf("xvid_decraw - raw mpeg4 bitstream encoder ");
168          printf("written by Christoph Lampert 2002\n\n");          printf("written by Christoph Lampert 2002\n\n");
169    
170  /*****************************************************************************  /*****************************************************************************
# Line 203  Line 188 
188                          i++;                          i++;
189                          ARG_BITRATE = atoi(argv[i]);                          ARG_BITRATE = atoi(argv[i]);
190                  }                  }
                 else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) {  
                         i++;  
                         ARG_MAXBFRAMES = atoi(argv[i]);  
                 }  
                 else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) {  
                         i++;  
                         ARG_BQRATIO = atoi(argv[i]);  
                 }  
                 else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) {  
                         i++;  
                         ARG_BQOFFSET = atoi(argv[i]);  
                 }  
191                  else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) {
192                          i++;                          i++;
193                          ARG_QUALITY = atoi(argv[i]);                          ARG_QUALITY = atoi(argv[i]);
# Line 247  Line 220 
220                          i++;                          i++;
221                          ARG_OUTPUTTYPE = atoi(argv[i]);                          ARG_OUTPUTTYPE = atoi(argv[i]);
222                  }                  }
                 else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) {  
                         i++;  
                         ARG_HINTMODE = atoi(argv[i]);  
                 }  
223                  else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) {
224                          i++;                          i++;
225                          ARG_OUTPUTFILE = argv[i];                          ARG_OUTPUTFILE = argv[i];
# Line 295  Line 264 
264                  return -1;                  return -1;
265          }          }
266    
         if ( ARG_HINTMODE != HINT_MODE_NONE &&  
                  ARG_HINTMODE != HINT_MODE_GET &&  
                  ARG_HINTMODE != HINT_MODE_SET)  
                 ARG_HINTMODE = HINT_MODE_NONE;  
   
         if( ARG_HINTMODE != HINT_MODE_NONE) {  
                 char *rights = "rb";  
   
                 /*  
                  * If we are getting hints from core, we will have to write them to  
                  * hint file  
                  */  
                 if(ARG_HINTMODE == HINT_MODE_GET)  
                         rights = "w+b";  
   
                 /* Open the hint file */  
                 hints_file = fopen(HINT_FILE, rights);  
                 if(hints_file == NULL) {  
                         fprintf(stderr, "Error opening input file %s\n", HINT_FILE);  
                         return -1;  
                 }  
   
                 /* Allocate hint memory space, we will be using rawhints */  
                 /* NB : Hope 1Mb is enough */  
                 if((hints_buffer = malloc(1024*1024)) == NULL) {  
                         fprintf(stderr, "Memory allocation error\n");  
                         return -1;  
                 }  
   
         }  
   
267          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
268                  in_file = stdin;                  in_file = stdin;
269          }          }
# Line 415  Line 353 
353          do {          do {
354    
355                  if (ARG_INPUTTYPE)                  if (ARG_INPUTTYPE)
356                          status = read_pgmdata(in_file, in_buffer);      /* read PGM data (YUV-format) */                          status = read_pgmdata(in_file, in_buffer);      // read PGM data (YUV-format)
357                  else                  else
358                          status = read_yuvdata(in_file, in_buffer);      /* read raw data (YUV-format) */                          status = read_yuvdata(in_file, in_buffer);      // read raw data (YUV-format)
359    
360                  if (status)                  if (status)
361                  {                  {
# Line 426  Line 364 
364                  }                  }
365    
366  /*****************************************************************************  /*****************************************************************************
  *                       Read hints from file  
  ****************************************************************************/  
   
                 if(ARG_HINTMODE == HINT_MODE_SET) {  
                         fread(&hints_size, 1, sizeof(long), hints_file);  
                         hints_size = (!bigendian)?SWAP(hints_size):hints_size;  
                         fread(hints_buffer, 1, hints_size, hints_file);  
                 }  
   
 /*****************************************************************************  
367   *                       Encode and decode this frame   *                       Encode and decode this frame
368   ****************************************************************************/   ****************************************************************************/
369    
370                  enctime = msecond();                  enctime = msecond();
371                  status = enc_main(in_buffer, mp4_buffer, hints_buffer,                  status = enc_main(in_buffer, mp4_buffer, &m4v_size, &frame_type);
                                                   &m4v_size, &frame_type, &hints_size);  
372                  enctime = msecond() - enctime;                  enctime = msecond() - enctime;
373    
374                  totalenctime += enctime;                  totalenctime += enctime;
375                  totalsize += m4v_size;                  totalsize += m4v_size;
376    
377                  {                  printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n",
378                          char *type;                             (int)filenr, (int)frame_type, (float)enctime, (int)m4v_size);
   
                         switch(frame_type) {  
                         case 0: type = "P"; break;  
                         case 1: type = "I"; break;  
                         case 2: type = "B"; break;  
                         default: type = "N"; break;  
                         }  
   
                         printf("Frame %5d: type %s, enctime=%6.1f ms, size=%6d bytes\n",  
                                    (int)filenr, type, (float)enctime, (int)m4v_size);  
   
                 }  
   
 /*****************************************************************************  
  *                       Save hints to file  
  ****************************************************************************/  
   
                 if(ARG_HINTMODE == HINT_MODE_GET) {  
                         hints_size = (!bigendian)?SWAP(hints_size):hints_size;  
                         fwrite(&hints_size, 1, sizeof(long), hints_file);  
                         hints_size = (!bigendian)?SWAP(hints_size):hints_size;  
                         fwrite(hints_buffer, 1, hints_size, hints_file);  
                 }  
   
 /*****************************************************************************  
  *                       Save stream to file  
  ****************************************************************************/  
379    
380                  if (ARG_SAVEMPEGSTREAM)                  if (ARG_SAVEMPEGSTREAM)
381                  {                  {
# Line 518  Line 418 
418          totalsize    /= filenr;          totalsize    /= filenr;
419          totalenctime /= filenr;          totalenctime /= filenr;
420    
421          printf("Avg: enctime %5.2f ms, %5.2f fps, size %7d bytes\n",          printf("Avg: enctime %5.2f ms, %5.2f fps, filesize %7d bytes\n",
422                     totalenctime, 1000/totalenctime, (int)totalsize);                     totalenctime, 1000/totalenctime, (int)totalsize);
423    
424  /*****************************************************************************  /*****************************************************************************
# Line 534  Line 434 
434                          fprintf(stderr, "Encore RELEASE problem return value %d\n", status);                          fprintf(stderr, "Encore RELEASE problem return value %d\n", status);
435          }          }
436    
         if(in_file)  
437                  fclose(in_file);                  fclose(in_file);
438          if(out_file)          if(out_file)
439                  fclose(out_file);                  fclose(out_file);
         if(hints_file)  
                 fclose(hints_file);  
440    
441   free_all_memory:   free_all_memory:
442          free(out_buffer);          free(out_buffer);
443          free(mp4_buffer);          free(mp4_buffer);
444          free(in_buffer);          free(in_buffer);
         if(hints_buffer) free(hints_buffer);  
445    
446          return 0;          return 0;
447    
# Line 586  Line 482 
482          fprintf(stderr, " -w integer     : frame width ([1.2048])\n");          fprintf(stderr, " -w integer     : frame width ([1.2048])\n");
483          fprintf(stderr, " -h integer     : frame height ([1.2048])\n");          fprintf(stderr, " -h integer     : frame height ([1.2048])\n");
484          fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");          fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");
         fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");  
         fprintf(stderr, " -bn integer    : max bframes (default=0)\n");  
         fprintf(stderr, " -bqr integer   : bframe quantizer ratio (default=150)\n");  
         fprintf(stderr, " -bqo integer   : bframe quantizer offset (default=100)\n");  
485          fprintf(stderr, " -f float       : target framerate (>0)\n");          fprintf(stderr, " -f float       : target framerate (>0)\n");
486          fprintf(stderr, " -i string      : input filename (default=stdin)\n");          fprintf(stderr, " -i string      : input filename (default=stdin)\n");
487          fprintf(stderr, " -t integer     : input data type (yuv=0, pgm=1)\n");          fprintf(stderr, " -t integer     : input data type (yuv=0, pgm=1)\n");
# Line 603  Line 495 
495          fprintf(stderr, "                    + stream.m4v with -mt 0\n");          fprintf(stderr, "                    + stream.m4v with -mt 0\n");
496          fprintf(stderr, "                    + stream.mp4u with -mt 1\n");          fprintf(stderr, "                    + stream.mp4u with -mt 1\n");
497          fprintf(stderr, " -mt integer    : output type (m4v=0, mp4u=1)\n");          fprintf(stderr, " -mt integer    : output type (m4v=0, mp4u=1)\n");
         fprintf(stderr, " -mv integer    : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n");  
498          fprintf(stderr, " -help          : prints this help message\n");          fprintf(stderr, " -help          : prints this help message\n");
499          fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n");          fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n");
500          fprintf(stderr, " (* means default)\n");          fprintf(stderr, " (* means default)\n");
# Line 730  Line 621 
621          xparam.min_quantizer = ARG_MINQUANT;          xparam.min_quantizer = ARG_MINQUANT;
622          xparam.max_quantizer = ARG_MAXQUANT;          xparam.max_quantizer = ARG_MAXQUANT;
623          xparam.max_key_interval = (int)ARG_FRAMERATE*10;          xparam.max_key_interval = (int)ARG_FRAMERATE*10;
         xparam.bquant_ratio = ARG_BQRATIO;  
         xparam.bquant_offset = ARG_BQOFFSET;  
         xparam.max_bframes = ARG_MAXBFRAMES;  
         xparam.frame_drop_ratio = 0;  
         xparam.global = 0;  
624    
625          /* I use a small value here, since will not encode whole movies, but short clips */          /* I use a small value here, since will not encode whole movies, but short clips */
626    
# Line 754  Line 640 
640  }  }
641    
642  static int enc_main(unsigned char* image, unsigned char* bitstream,  static int enc_main(unsigned char* image, unsigned char* bitstream,
643                                          unsigned char* hints_buffer,                                          int *streamlength, int* frametype)
                                         long *streamlength, long *frametype, long *hints_size)  
644  {  {
645          int xerr;          int xerr;
646    
# Line 763  Line 648 
648          XVID_ENC_STATS xstats;          XVID_ENC_STATS xstats;
649    
650          xframe.bitstream = bitstream;          xframe.bitstream = bitstream;
651          xframe.length = -1;     /* this is written by the routine */          xframe.length = -1;     // this is written by the routine
652    
653          xframe.image = image;          xframe.image = image;
654          xframe.colorspace = XVID_CSP_YV12;      /* defined in <xvid.h> */          xframe.colorspace = XVID_CSP_YV12;      // defined in <xvid.h>
655    
656          xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */          xframe.intra = -1; // let the codec decide between I-frame (1) and P-frame (0)
657    
658          xframe.quant = ARG_QUANTI;      /* is quant != 0, use a fixed quant (and ignore bitrate) */          xframe.quant = ARG_QUANTI;      // is quant != 0, use a fixed quant (and ignore bitrate)
         xframe.bquant = 0;  
659    
660          xframe.motion = motion_presets[ARG_QUALITY];          xframe.motion = motion_presets[ARG_QUALITY];
661          xframe.general = general_presets[ARG_QUALITY];          xframe.general = general_presets[ARG_QUALITY];
662          xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL;          xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL;
         xframe.stride = XDIM;  
   
         xframe.hint.hintstream = hints_buffer;  
   
         if(ARG_HINTMODE == HINT_MODE_SET) {  
                 xframe.hint.hintlength = *hints_size;  
                 xframe.hint.rawhints = 0;  
                 xframe.general |= XVID_HINTEDME_SET;  
         }  
   
         if(ARG_HINTMODE == HINT_MODE_GET) {  
                 xframe.hint.rawhints = 0;  
                 xframe.general |= XVID_HINTEDME_GET;  
         }  
663    
664          xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats);          xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats);
665    
         if(ARG_HINTMODE == HINT_MODE_GET)  
                 *hints_size = xframe.hint.hintlength;  
   
666          /*          /*
667           * This is statictical data, e.g. for 2-pass. If you are not           * This is statictical data, e.g. for 2-pass. If you are not
668           * interested in any of this, you can use NULL instead of &xstats           * interested in any of this, you can use NULL instead of &xstats

Legend:
Removed from v.1.1.2.2  
changed lines
  Added in v.1.3

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