[svn] / branches / dev-api-4 / xvidcore / examples / xvid_encraw.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/examples/xvid_encraw.c

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

revision 684, Wed Nov 27 21:09:10 2002 UTC revision 860, Sun Feb 16 05:11:39 2003 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 Christoph Lampert   *  Copyright(C) 2002-2003 Christoph Lampert
7   *   *
8   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
9   *  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 19  Line 19 
19   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21   *   *
22   * $Id: xvid_encraw.c,v 1.5 2002-11-27 21:09:10 edgomez Exp $   * $Id: xvid_encraw.c,v 1.11 2003-02-16 05:11:39 suxen_drol Exp $
23   *   *
24   ****************************************************************************/   ****************************************************************************/
25    
# Line 38  Line 38 
38  #include <stdlib.h>  #include <stdlib.h>
39  #include <string.h>  #include <string.h>
40  #include <math.h>  #include <math.h>
41  #ifndef _MSC_VER  #ifndef WIN32
42  #include <sys/time.h>  #include <sys/time.h>
43  #else  #else
44  #include <time.h>  #include <time.h>
# Line 80  Line 80 
80  /* Maximum number of frames to encode */  /* Maximum number of frames to encode */
81  #define ABS_MAXFRAMENR 9999  #define ABS_MAXFRAMENR 9999
82    
83    /* HINTMODEs */
84    #define HINT_MODE_NONE 0
85    #define HINT_MODE_GET  1
86    #define HINT_MODE_SET  2
87    #define HINT_FILE "hints.mv"
88    
89  static int   ARG_BITRATE = 900;  static int   ARG_BITRATE = 900;
90  static int   ARG_QUANTI = 0;  static int   ARG_QUANTI = 0;
91  static int   ARG_QUALITY = 6;  static int   ARG_QUALITY = 6;
# Line 90  Line 96 
96  static char *ARG_INPUTFILE = NULL;  static char *ARG_INPUTFILE = NULL;
97  static int   ARG_INPUTTYPE = 0;  static int   ARG_INPUTTYPE = 0;
98  static int   ARG_SAVEMPEGSTREAM = 0;  static int   ARG_SAVEMPEGSTREAM = 0;
 static int   ARG_OUTPUTTYPE = 0;  
99  static char *ARG_OUTPUTFILE = NULL;  static char *ARG_OUTPUTFILE = NULL;
100    static int   ARG_HINTMODE = HINT_MODE_NONE;
101  static int   XDIM = 0;  static int   XDIM = 0;
102  static int   YDIM = 0;  static int   YDIM = 0;
103    static int   ARG_BQRATIO = 120;
104    static int   ARG_BQOFFSET = 0;
105    static int   ARG_MAXBFRAMES = 0;
106  #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)  #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)
107    
108  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
109  #define SMALL_EPS 1e-10  #define SMALL_EPS 1e-10
110    
 #define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \  
                                    (((long)(c))<<8)  |((long)(d)))  
   
111  #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \  #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \
112                    (((a)&0x00ff0000)>>8)  | (((a)&0xff000000)>>24) )                    (((a)&0x00ff0000)>>8)  | (((a)&0xff000000)>>24) )
113    
# Line 136  Line 142 
142  static int enc_init(int use_assembler);  static int enc_init(int use_assembler);
143  static int enc_stop();  static int enc_stop();
144  static int enc_main(unsigned char* image, unsigned char* bitstream,  static int enc_main(unsigned char* image, unsigned char* bitstream,
145                                          int *streamlength, int* frametype);                                          unsigned char *hints_buffer,
146                                            long *streamlength, long* frametype, long *hints_size);
147    
148  /*****************************************************************************  /*****************************************************************************
149   *               Main function   *               Main function
# Line 148  Line 155 
155          unsigned char *mp4_buffer = NULL;          unsigned char *mp4_buffer = NULL;
156          unsigned char *in_buffer = NULL;          unsigned char *in_buffer = NULL;
157          unsigned char *out_buffer = NULL;          unsigned char *out_buffer = NULL;
158            unsigned char *hints_buffer = NULL;
159    
160          double enctime;          double enctime;
161          double totalenctime=0.;          double totalenctime=0.;
162    
163          long totalsize;          long totalsize;
164            long hints_size;
165          int status;          int status;
166          int frame_type;          long frame_type;
167          int bigendian;          long bigendian;
168    
169          int m4v_size;          long m4v_size;
170          int use_assembler=0;          int use_assembler=0;
171    
172          char filename[256];          char filename[256];
173    
174          FILE *in_file = stdin;          FILE *in_file = stdin;
175          FILE *out_file = NULL;          FILE *out_file = NULL;
176            FILE *hints_file = NULL;
177    
178          printf("xvid_decraw - raw mpeg4 bitstream encoder ");          printf("xvid_encraw - raw mpeg4 bitstream encoder ");
179          printf("written by Christoph Lampert 2002\n\n");          printf("written by Christoph Lampert 2002-2003\n\n");
180    
181  /*****************************************************************************  /*****************************************************************************
182   *                            Command line parsing   *                            Command line parsing
# Line 189  Line 199 
199                          i++;                          i++;
200                          ARG_BITRATE = atoi(argv[i]);                          ARG_BITRATE = atoi(argv[i]);
201                  }                  }
202                    else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) {
203                            i++;
204                            ARG_MAXBFRAMES = atoi(argv[i]);
205                    }
206                    else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) {
207                            i++;
208                            ARG_BQRATIO = atoi(argv[i]);
209                    }
210                    else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) {
211                            i++;
212                            ARG_BQOFFSET = atoi(argv[i]);
213                    }
214                  else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) {
215                          i++;                          i++;
216                          ARG_QUALITY = atoi(argv[i]);                          ARG_QUALITY = atoi(argv[i]);
# Line 217  Line 239 
239                          i++;                          i++;
240                          ARG_SAVEMPEGSTREAM = atoi(argv[i]);                          ARG_SAVEMPEGSTREAM = atoi(argv[i]);
241                  }                  }
242                  else if (strcmp("-mt", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) {
243                          i++;                          i++;
244                          ARG_OUTPUTTYPE = atoi(argv[i]);                          ARG_HINTMODE = atoi(argv[i]);
245                  }                  }
246                  else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) {
247                          i++;                          i++;
# Line 265  Line 287 
287                  return -1;                  return -1;
288          }          }
289    
290            if ( ARG_HINTMODE != HINT_MODE_NONE &&
291                     ARG_HINTMODE != HINT_MODE_GET &&
292                     ARG_HINTMODE != HINT_MODE_SET)
293                    ARG_HINTMODE = HINT_MODE_NONE;
294    
295            if( ARG_HINTMODE != HINT_MODE_NONE) {
296                    char *rights = "rb";
297    
298                    /*
299                     * If we are getting hints from core, we will have to write them to
300                     * hint file
301                     */
302                    if(ARG_HINTMODE == HINT_MODE_GET)
303                            rights = "w+b";
304    
305                    /* Open the hint file */
306                    hints_file = fopen(HINT_FILE, rights);
307                    if(hints_file == NULL) {
308                            fprintf(stderr, "Error opening input file %s\n", HINT_FILE);
309                            return -1;
310                    }
311    
312                    /* Allocate hint memory space, we will be using rawhints */
313                    /* NB : Hope 1Mb is enough */
314                    if((hints_buffer = malloc(1024*1024)) == NULL) {
315                            fprintf(stderr, "Memory allocation error\n");
316                            return -1;
317                    }
318    
319            }
320    
321          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
322                  in_file = stdin;                  in_file = stdin;
323          }          }
# Line 285  Line 338 
338          }          }
339    
340          /* now we know the sizes, so allocate memory */          /* now we know the sizes, so allocate memory */
   
341          in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM));          in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM));
342          if (!in_buffer)          if (!in_buffer)
343                  goto free_all_memory;                  goto free_all_memory;
# Line 311  Line 363 
363   *                            Main loop   *                            Main loop
364   ****************************************************************************/   ****************************************************************************/
365    
366          totalsize = LONG_PACK('M','P','4','U');          if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) {
         if(*((char *)(&totalsize)) == 'M')  
                 bigendian = 1;  
         else  
                 bigendian = 0;  
   
         if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) {  
   
                 if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE)  
                         ARG_OUTPUTFILE = "stream.mp4u";  
                 else if(ARG_OUTPUTFILE == NULL && !ARG_OUTPUTTYPE)  
                         ARG_OUTPUTFILE = "stream.m4v";  
367    
368                  if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {                  if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {
369                          fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);                          fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);
370                          goto release_all;                          goto release_all;
371                  }                  }
372    
                 /* Write header */  
                 if (ARG_OUTPUTTYPE) {  
   
                         long test = LONG_PACK('M','P','4','U');  
   
                         test = (!bigendian)?SWAP(test):test;  
   
                         fwrite(&test, sizeof(test), 1, out_file);  
   
                 }  
   
373          }          }
374          else {          else {
375                  out_file = NULL;                  out_file = NULL;
# Line 365  Line 395 
395                  }                  }
396    
397  /*****************************************************************************  /*****************************************************************************
398     *                       Read hints from file
399     ****************************************************************************/
400    
401                    if(ARG_HINTMODE == HINT_MODE_SET) {
402                            fread(&hints_size, 1, sizeof(long), hints_file);
403                            hints_size = (!bigendian)?SWAP(hints_size):hints_size;
404                            fread(hints_buffer, 1, hints_size, hints_file);
405                    }
406    
407    /*****************************************************************************
408   *                       Encode and decode this frame   *                       Encode and decode this frame
409   ****************************************************************************/   ****************************************************************************/
410    
411                  enctime = msecond();                  enctime = msecond();
412                  status = enc_main(in_buffer, mp4_buffer, &m4v_size, &frame_type);                  status = enc_main(in_buffer, mp4_buffer, hints_buffer,
413                                                      &m4v_size, &frame_type, &hints_size);
414                  enctime = msecond() - enctime;                  enctime = msecond() - enctime;
415    
416                    /* if it's a not coded VOP (aka NVOP) then we write nothing */
417                    if(frame_type == 5) goto next_frame;
418    
419                    {
420                            char *type[] = {"P", "I", "B", "S", "Packed", "N", "Unknown"};
421    
422                            if(frame_type<0 || frame_type>5) frame_type = 6;
423    
424                            printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n",
425                                       (int)filenr, type[frame_type], (float)enctime, (int)m4v_size);
426    
427                    }
428    
429                    /* Update encoding time stats */
430                  totalenctime += enctime;                  totalenctime += enctime;
431                  totalsize += m4v_size;                  totalsize += m4v_size;
432    
433                  printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n",  /*****************************************************************************
434                             (int)filenr, (int)frame_type, (float)enctime, (int)m4v_size);   *                       Save hints to file
435     ****************************************************************************/
436    
437                    if(ARG_HINTMODE == HINT_MODE_GET) {
438                            hints_size = (!bigendian)?SWAP(hints_size):hints_size;
439                            fwrite(&hints_size, 1, sizeof(long), hints_file);
440                            hints_size = (!bigendian)?SWAP(hints_size):hints_size;
441                            fwrite(hints_buffer, 1, hints_size, hints_file);
442                    }
443    
444    /*****************************************************************************
445     *                       Save stream to file
446     ****************************************************************************/
447    
448                  if (ARG_SAVEMPEGSTREAM)                  if (ARG_SAVEMPEGSTREAM)
449                  {                  {
# Line 389  Line 456 
456                                  out_file = NULL;                                  out_file = NULL;
457                          }                          }
458                          else {                          else {
                                 /* Using mp4u container */  
                                 if (ARG_OUTPUTTYPE) {  
                                         long size = m4v_size;  
                                         size = (!bigendian)?SWAP(size):size;  
                                         fwrite(&size, sizeof(size), 1, out_file);  
                                 }  
459    
460                                  /* Write mp4 data */                                  /* Write mp4 data */
461                                  fwrite(mp4_buffer, m4v_size, 1, out_file);                                  fwrite(mp4_buffer, 1, m4v_size, out_file);
462    
463                          }                          }
464                  }                  }
465    
466            next_frame:
467                  /* Read the header if it's pgm stream */                  /* Read the header if it's pgm stream */
468                  if (ARG_INPUTTYPE)                  if (ARG_INPUTTYPE)
469                          status = read_pgmheader(in_file);                          status = read_pgmheader(in_file);
470    
471                  filenr++;                  if(frame_type != 5) filenr++;
472    
473          } while ( (!status) && (filenr<ARG_MAXFRAMENR) );          } while ( (!status) && (filenr<ARG_MAXFRAMENR) );
474    
# Line 419  Line 481 
481          totalsize    /= filenr;          totalsize    /= filenr;
482          totalenctime /= filenr;          totalenctime /= filenr;
483    
484          printf("Avg: enctime %5.2f ms, %5.2f fps, filesize %7d bytes\n",          printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n",
485                     totalenctime, 1000/totalenctime, (int)totalsize);                     totalenctime, 1000/totalenctime, (int)totalsize);
486    
487  /*****************************************************************************  /*****************************************************************************
# Line 435  Line 497 
497                          fprintf(stderr, "Encore RELEASE problem return value %d\n", status);                          fprintf(stderr, "Encore RELEASE problem return value %d\n", status);
498          }          }
499    
500            if(in_file)
501          fclose(in_file);          fclose(in_file);
502          if(out_file)          if(out_file)
503                  fclose(out_file);                  fclose(out_file);
504            if(hints_file)
505                    fclose(hints_file);
506    
507   free_all_memory:   free_all_memory:
508          free(out_buffer);          free(out_buffer);
509          free(mp4_buffer);          free(mp4_buffer);
510          free(in_buffer);          free(in_buffer);
511            if(hints_buffer) free(hints_buffer);
512    
513          return 0;          return 0;
514    
# Line 460  Line 526 
526  /* Return time elapsed time in miliseconds since the program started */  /* Return time elapsed time in miliseconds since the program started */
527  static double msecond()  static double msecond()
528  {  {
529  #ifndef _MSC_VER  #ifndef WIN32
530          struct timeval  tv;          struct timeval  tv;
531          gettimeofday(&tv, 0);          gettimeofday(&tv, 0);
532          return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3;          return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3;
# Line 483  Line 549 
549          fprintf(stderr, " -w integer     : frame width ([1.2048])\n");          fprintf(stderr, " -w integer     : frame width ([1.2048])\n");
550          fprintf(stderr, " -h integer     : frame height ([1.2048])\n");          fprintf(stderr, " -h integer     : frame height ([1.2048])\n");
551          fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");          fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");
552            fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");
553            fprintf(stderr, " -bn integer    : max bframes (default=0)\n");
554            fprintf(stderr, " -bqr integer   : bframe quantizer ratio (default=150)\n");
555            fprintf(stderr, " -bqo integer   : bframe quantizer offset (default=100)\n");
556          fprintf(stderr, " -f float       : target framerate (>0)\n");          fprintf(stderr, " -f float       : target framerate (>0)\n");
557          fprintf(stderr, " -i string      : input filename (default=stdin)\n");          fprintf(stderr, " -i string      : input filename (default=stdin)\n");
558          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 492  Line 562 
562          fprintf(stderr, " -m boolean     : save mpeg4 raw stream (0 False*, !=0 True)\n");          fprintf(stderr, " -m boolean     : save mpeg4 raw stream (0 False*, !=0 True)\n");
563          fprintf(stderr, " -o string      : output container filename (only usefull when -m 1 is used) :\n");          fprintf(stderr, " -o string      : output container filename (only usefull when -m 1 is used) :\n");
564          fprintf(stderr, "                  When this option is not used : one file per encoded frame\n");          fprintf(stderr, "                  When this option is not used : one file per encoded frame\n");
565          fprintf(stderr, "                  When this option is used :\n");          fprintf(stderr, "                  When this option is used : save to 'string' (default=stream.m4v)\n");
         fprintf(stderr, "                    + stream.m4v with -mt 0\n");  
         fprintf(stderr, "                    + stream.mp4u with -mt 1\n");  
566          fprintf(stderr, " -mt integer    : output type (m4v=0, mp4u=1)\n");          fprintf(stderr, " -mt integer    : output type (m4v=0, mp4u=1)\n");
567            fprintf(stderr, " -mv integer    : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n");
568          fprintf(stderr, " -help          : prints this help message\n");          fprintf(stderr, " -help          : prints this help message\n");
569          fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n");          fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n");
570          fprintf(stderr, " (* means default)\n");          fprintf(stderr, " (* means default)\n");
# Line 591  Line 660 
660    
661          if(use_assembler) {          if(use_assembler) {
662    
663  #ifdef ARCH_IA64  #ifdef ARCH_IS_IA64
664                  xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;                  xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
665  #else  #else
666                  xinit.cpu_flags = 0;                  xinit.cpu_flags = 0;
# Line 622  Line 691 
691          xparam.min_quantizer = ARG_MINQUANT;          xparam.min_quantizer = ARG_MINQUANT;
692          xparam.max_quantizer = ARG_MAXQUANT;          xparam.max_quantizer = ARG_MAXQUANT;
693          xparam.max_key_interval = (int)ARG_FRAMERATE*10;          xparam.max_key_interval = (int)ARG_FRAMERATE*10;
694            xparam.bquant_ratio = ARG_BQRATIO;
695            xparam.bquant_offset = ARG_BQOFFSET;
696            xparam.max_bframes = ARG_MAXBFRAMES;
697            xparam.frame_drop_ratio = 0;
698            xparam.global = 0;
699    
700          /* 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 */
701    
# Line 641  Line 715 
715  }  }
716    
717  static int enc_main(unsigned char* image, unsigned char* bitstream,  static int enc_main(unsigned char* image, unsigned char* bitstream,
718                                          int *streamlength, int* frametype)                                          unsigned char* hints_buffer,
719                                            long *streamlength, long *frametype, long *hints_size)
720  {  {
721          int xerr;          int xerr;
722    
# Line 657  Line 732 
732          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) */
733    
734          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) */
735            xframe.bquant = 0;
736    
737          xframe.motion = motion_presets[ARG_QUALITY];          xframe.motion = motion_presets[ARG_QUALITY];
738          xframe.general = general_presets[ARG_QUALITY];          xframe.general = general_presets[ARG_QUALITY];
739          xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL;          xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL;
740            xframe.stride = XDIM;
741    
742            xframe.hint.hintstream = hints_buffer;
743    
744            if(ARG_HINTMODE == HINT_MODE_SET) {
745                    xframe.hint.hintlength = *hints_size;
746                    xframe.hint.rawhints = 0;
747                    xframe.general |= XVID_HINTEDME_SET;
748            }
749    
750            if(ARG_HINTMODE == HINT_MODE_GET) {
751                    xframe.hint.rawhints = 0;
752                    xframe.general |= XVID_HINTEDME_GET;
753            }
754    
755          xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats);          xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats);
756    
757            if(ARG_HINTMODE == HINT_MODE_GET)
758                    *hints_size = xframe.hint.hintlength;
759    
760          /*          /*
761           * 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
762           * 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.684  
changed lines
  Added in v.860

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