[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 909, Sun Mar 9 00:28:09 2003 UTC revision 919, Thu Mar 13 11:07:20 2003 UTC
# 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.11.2.1 2003-03-09 00:28:09 edgomez Exp $   * $Id: xvid_encraw.c,v 1.11.2.5 2003-03-13 11:07:20 suxen_drol Exp $
23   *   *
24   ****************************************************************************/   ****************************************************************************/
25    
26  /*****************************************************************************  /*****************************************************************************
27   *  Application notes :   *  Application notes :
28   *   *
29   *  A sequence of YUV pics in PGM file format is encoded and decoded   *  A sequence of raw YUV I420 pics or YUV I420 PGM file format is encoded
30   *  The speed is measured and PSNR of decoded picture is calculated.   *  The speed is measured and frames' PSNR are taken from core.
31   *   *
32   *  The program is plain C and needs no libraries except for libxvidcore,   *  The program is plain C and needs no libraries except for libxvidcore,
33   *  and maths-lib.   *  and maths-lib.
# Line 56  Line 56 
56          PMV_HALFPELREFINE16,          PMV_HALFPELREFINE16,
57          PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8,          PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8,
58          PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16,          PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16,
59          PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16,          PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16 | PMV_CHROMA16 | PMV_CHROMA8,
60  };  };
61    
62  static xvid_vol_t const vol_presets[] = {  static xvid_vol_t const vol_presets[] = {
# Line 84  Line 84 
84  /* Maximum number of frames to encode */  /* Maximum number of frames to encode */
85  #define ABS_MAXFRAMENR 9999  #define ABS_MAXFRAMENR 9999
86    
87    static int   ARG_STATS = 0;
88  static int   ARG_BITRATE = 900;  static int   ARG_BITRATE = 900;
89  static int   ARG_QUANTI = 0;  static int   ARG_QUANTI = 0;
90  static int   ARG_QUALITY = 5;  static int   ARG_QUALITY = 5;
# Line 100  Line 101 
101  static int   ARG_BQRATIO = 150;  static int   ARG_BQRATIO = 150;
102  static int   ARG_BQOFFSET = 100;  static int   ARG_BQOFFSET = 100;
103  static int   ARG_MAXBFRAMES = 0;  static int   ARG_MAXBFRAMES = 0;
104    static int   ARG_PACKED = 0;
105  #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)  #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)
106    
107  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
# Line 140  Line 142 
142  static int enc_stop();  static int enc_stop();
143  static int enc_main(unsigned char* image,  static int enc_main(unsigned char* image,
144                                          unsigned char* bitstream,                                          unsigned char* bitstream,
145                                          long *frametype);                      int *key,
146                                            int *stats_type,
147                        int *stats_quant,
148                        int *stats_length,
149                                            int stats[3]);
150    
151  /*****************************************************************************  /*****************************************************************************
152   *               Main function   *               Main function
# Line 156  Line 162 
162          double enctime;          double enctime;
163          double totalenctime=0.;          double totalenctime=0.;
164    
165          long totalsize;          int totalsize;
166          int status;          int status;
167          long frame_type;          int m4v_size;
168        int key;
169          long m4v_size;          int stats_type;
170        int stats_quant;
171        int stats_length;
172          int use_assembler=0;          int use_assembler=0;
173    
174          char filename[256];          char filename[256];
# Line 196  Line 204 
204                          i++;                          i++;
205                          ARG_MAXBFRAMES = atoi(argv[i]);                          ARG_MAXBFRAMES = atoi(argv[i]);
206                  }                  }
207                    else if (strcmp("-p", argv[i]) == 0) {
208                ARG_PACKED = 1;
209                    }
210                  else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) {
211                          i++;                          i++;
212                          ARG_BQRATIO = atoi(argv[i]);                          ARG_BQRATIO = atoi(argv[i]);
# Line 216  Line 227 
227                          i++;                          i++;
228                          ARG_INPUTFILE = argv[i];                          ARG_INPUTFILE = argv[i];
229                  }                  }
230                    else if (strcmp("-s", argv[i]) == 0) {
231                            ARG_STATS = 1;
232                    }
233                  else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) {
234                          i++;                          i++;
235                          ARG_INPUTTYPE = atoi(argv[i]);                          ARG_INPUTTYPE = atoi(argv[i]);
# Line 228  Line 242 
242                          i++;                          i++;
243                          ARG_QUANTI = atoi(argv[i]);                          ARG_QUANTI = atoi(argv[i]);
244                  }                  }
245                  else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-m", argv[i]) == 0) {
246                          i++;                          ARG_SAVEMPEGSTREAM = 1;
                         ARG_SAVEMPEGSTREAM = atoi(argv[i]);  
247                  }                  }
248                  else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) {
249                          i++;                          i++;
# Line 341  Line 354 
354    
355          do {          do {
356    
357                  if (ARG_INPUTTYPE)                  char *type;
358                          status = read_pgmdata(in_file, in_buffer);      /* read PGM data (YUV-format) */                  int stats[3];
                 else  
                         status = read_yuvdata(in_file, in_buffer);      /* read raw data (YUV-format) */  
359    
360                  if (status)                  if(ARG_INPUTTYPE) {
361                  {                          /* read PGM data (YUV-format) */
362                            status = read_pgmdata(in_file, in_buffer);
363                    } else {
364                            /* read raw data (YUV-format) */
365                            status = read_yuvdata(in_file, in_buffer);
366                    }
367    
368                    if(status) {
369                          /* Couldn't read image, most likely end-of-file */                          /* Couldn't read image, most likely end-of-file */
370                          continue;                          continue;
371                  }                  }
# Line 357  Line 375 
375   ****************************************************************************/   ****************************************************************************/
376    
377                  enctime = msecond();                  enctime = msecond();
378                  m4v_size = enc_main(in_buffer, mp4_buffer, &frame_type);                  m4v_size = enc_main(in_buffer, mp4_buffer, &key, &stats_type, &stats_quant, &stats_length, stats);
379                  enctime = msecond() - enctime;                  enctime = msecond() - enctime;
380    
381                  /* Not coded frames return 0 */                  /* Write the Frame statistics */
                 if(m4v_size == 0) goto next_frame;  
382    
383                  {                  printf("Frame %5d: key=%i, time(ms)=%6.1f, length=%7d",
384                          char *type;                             (int)filenr,
385                               key,
386                               (float)enctime,
387                               (int)m4v_size);
388    
389            if (stats_type > 0) {   /* !XVID_TYPE_NOTHING */
390    
391                          switch(frame_type) {                  switch(stats_type) {
392                          case XVID_TYPE_IVOP:                          case XVID_TYPE_IVOP:
393                                  type = "I";                                  type = "I";
394                                  break;                                  break;
# Line 380  Line 402 
402                                  type = "S";                                  type = "S";
403                                  break;                                  break;
404                          default:                          default:
405                                  type = "Unknown";                          type = "U";
406                                  break;                                  break;
407                          }                          }
408    
409                          printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n",              printf(" | type=%s quant=%2d, length=%7d", type, stats_quant, stats_length);
                                    (int)filenr, type, (float)enctime, (int)m4v_size);  
410    
411                if(ARG_STATS) {
412                            printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
413                                       (stats[0] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[0]/((float)(XDIM)*(YDIM))),
414                                       (stats[1] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[1]/((float)(XDIM)*(YDIM)/4)),
415                                       (stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4)));
416                }
417                  }                  }
418    
419                    printf("\n");
420    
421    
422                  /* Update encoding time stats */                  /* Update encoding time stats */
423                  totalenctime += enctime;                  totalenctime += enctime;
424                  totalsize += m4v_size;                  totalsize += m4v_size;
425    
426            /* Not coded frames return 0 */
427                    if(m4v_size == 0) goto next_frame;
428    
429    
430  /*****************************************************************************  /*****************************************************************************
431   *                       Save stream to file   *                       Save stream to file
432   ****************************************************************************/   ****************************************************************************/
# Line 415  Line 449 
449                          }                          }
450                  }                  }
451    
452                    filenr++;
453    
454          next_frame:          next_frame:
455                  /* Read the header if it's pgm stream */                  /* Read the header if it's pgm stream */
456                  if (ARG_INPUTTYPE)                  if (ARG_INPUTTYPE)
457                          status = read_pgmheader(in_file);                          status = read_pgmheader(in_file);
458    
                 if(frame_type != 5) filenr++;  
   
459          } while ( (!status) && (filenr<ARG_MAXFRAMENR) );          } while ( (!status) && (filenr<ARG_MAXFRAMENR) );
460    
461    
# Line 436  Line 470 
470          printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n",          printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n",
471                     totalenctime, 1000/totalenctime, (int)totalsize);                     totalenctime, 1000/totalenctime, (int)totalsize);
472    
473    
474  /*****************************************************************************  /*****************************************************************************
475   *                            XviD PART  Stop   *                            XviD PART  Stop
476   ****************************************************************************/   ****************************************************************************/
# Line 499  Line 534 
534          fprintf(stderr, " -h integer     : frame height ([1.2048])\n");          fprintf(stderr, " -h integer     : frame height ([1.2048])\n");
535          fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");          fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");
536          fprintf(stderr, " -bn integer    : max bframes (default=0)\n");          fprintf(stderr, " -bn integer    : max bframes (default=0)\n");
537        fprintf(stderr, " -p             : packed mode\n");
538          fprintf(stderr, " -bqr integer   : bframe quantizer ratio (default=150)\n");          fprintf(stderr, " -bqr integer   : bframe quantizer ratio (default=150)\n");
539          fprintf(stderr, " -bqo integer   : bframe quantizer offset (default=100)\n");          fprintf(stderr, " -bqo integer   : bframe quantizer offset (default=100)\n");
540          fprintf(stderr, " -f float       : target framerate (>0)\n");          fprintf(stderr, " -f float       : target framerate (>0)\n");
541          fprintf(stderr, " -i string      : input filename (default=stdin)\n");          fprintf(stderr, " -i string      : input filename (default=stdin)\n");
542            fprintf(stderr, " -s             : print stats about encoded frames\n");
543          fprintf(stderr, " -t integer     : input data type (yuv=0, pgm=1)\n");          fprintf(stderr, " -t integer     : input data type (yuv=0, pgm=1)\n");
544          fprintf(stderr, " -n integer     : number of frames to encode\n");          fprintf(stderr, " -n integer     : number of frames to encode\n");
545          fprintf(stderr, " -q integer     : quality ([0..5])\n");          fprintf(stderr, " -q integer     : quality ([0..5])\n");
546          fprintf(stderr, " -d boolean     : save decoder output (0 False*, !=0 True)\n");          fprintf(stderr, " -d boolean     : save decoder output (0 False*, !=0 True)\n");
547          fprintf(stderr, " -m boolean     : save mpeg4 raw stream (0 False*, !=0 True)\n");          fprintf(stderr, " -m             : save mpeg4 raw stream\n");
548          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");
549          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");
550          fprintf(stderr, "                  When this option is used : save to 'string' file\n");          fprintf(stderr, "                  When this option is used : save to 'string' file\n");
# Line 594  Line 631 
631   *     Routines for encoding: init encoder, frame step, release encoder   *     Routines for encoding: init encoder, frame step, release encoder
632   ****************************************************************************/   ****************************************************************************/
633    
634    /* sample plugin */
635    
636    int rawenc_debug(void * handle, int opt, void * param1, void * param2)
637    {
638        switch(opt)
639        {
640        case XVID_PLG_INFO :
641        case XVID_PLG_CREATE :
642        case XVID_PLG_DESTROY :
643        case XVID_PLG_BEFORE :
644           return 0;
645    
646        case XVID_PLG_AFTER :
647           {
648           xvid_plg_data_t * data = (xvid_plg_data_t*)param1;
649           printf("type=%i, quant=%i, length=%i\n", data->type, data->quant, data->length);
650           return 0;
651           }
652        }
653    
654        return XVID_ERR_FAIL;
655    }
656    
657    
658  #define FRAMERATE_INCR 1001  #define FRAMERATE_INCR 1001
659    
660  /* Initialize encoder for first use, pass all needed parameters to the codec */  /* Initialize encoder for first use, pass all needed parameters to the codec */
# Line 601  Line 662 
662  {  {
663          int xerr;          int xerr;
664    
665        /* xvid_enc_plugin_t plugins[1]; */
666    
667          xvid_gbl_init_t   xvid_gbl_init;          xvid_gbl_init_t   xvid_gbl_init;
668          xvid_enc_create_t xvid_enc_create;          xvid_enc_create_t xvid_enc_create;
669    
# Line 609  Line 672 
672           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
673    
674          /* Set version -- version checking will done by xvidcore*/          /* Set version -- version checking will done by xvidcore*/
675        memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
676          xvid_gbl_init.version = XVID_VERSION;          xvid_gbl_init.version = XVID_VERSION;
677    
678    
679          /* Do we have to enable ASM optimizations ? */          /* Do we have to enable ASM optimizations ? */
680          if(use_assembler) {          if(use_assembler) {
681    
# Line 632  Line 697 
697           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
698    
699          /* Version again */          /* Version again */
700        memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
701          xvid_enc_create.version = XVID_VERSION;          xvid_enc_create.version = XVID_VERSION;
702    
703          /* Width and Height of input frames */          /* Width and Height of input frames */
704          xvid_enc_create.width = XDIM;          xvid_enc_create.width = XDIM;
705          xvid_enc_create.height = YDIM;          xvid_enc_create.height = YDIM;
706    
707        /* init plugins
708        plugins[0].func =  rawenc_debug;
709        plugins[0].param = NULL;
710        xvid_enc_create.num_plugins = 1;
711        xvid_enc_create.plugins = plugins; */
712    
713          /* No fancy thread tests */          /* No fancy thread tests */
714          xvid_enc_create.num_threads = 0;          xvid_enc_create.num_threads = 0;
715    
# Line 663  Line 735 
735    
736          /* Global encoder options */          /* Global encoder options */
737          xvid_enc_create.global = 0;          xvid_enc_create.global = 0;
738        if (ARG_STATS) xvid_enc_create.global |= XVID_EXTRASTATS_ENABLE;
739        if (ARG_PACKED) xvid_enc_create.global |= XVID_PACKED;
740    
741          /* 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 */
742          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
# Line 685  Line 759 
759    
760  static int enc_main(unsigned char* image,  static int enc_main(unsigned char* image,
761                                          unsigned char* bitstream,                                          unsigned char* bitstream,
762                                          long *frametype)                      int * key,
763                                            int *stats_type,
764                        int *stats_quant,
765                        int *stats_length,
766                                            int stats[3])
767  {  {
768          int ret;          int ret;
769    
770          xvid_enc_frame_t xvid_enc_frame;          xvid_enc_frame_t xvid_enc_frame;
771          xvid_enc_stats_t xvid_enc_stats[2];          xvid_enc_stats_t xvid_enc_stats;
772    
773          /* Version for the frame and the stats */          /* Version for the frame and the stats */
774        memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
775          xvid_enc_frame.version = XVID_VERSION;          xvid_enc_frame.version = XVID_VERSION;
776          xvid_enc_stats[0].version = XVID_VERSION;  
777          xvid_enc_stats[1].version = XVID_VERSION;      memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
778            xvid_enc_stats.version = XVID_VERSION;
779    
780          /* Bind output buffer */          /* Bind output buffer */
781          xvid_enc_frame.bitstream = bitstream;          xvid_enc_frame.bitstream = bitstream;
# Line 726  Line 806 
806          xvid_enc_frame.quant_intra_matrix = NULL;          xvid_enc_frame.quant_intra_matrix = NULL;
807          xvid_enc_frame.quant_inter_matrix = NULL;          xvid_enc_frame.quant_inter_matrix = NULL;
808    
809            /* Encode the frame */
810            xvid_enc_frame.vop_flags |= (ARG_STATS)?XVID_EXTRASTATS:0;
811          ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats);          ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats);
812    
813          *frametype = xvid_enc_stats[0].type;      *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME);
814            *stats_type = xvid_enc_stats.type;
815        *stats_quant = xvid_enc_stats.quant;
816        *stats_length = xvid_enc_stats.length;
817            stats[0]   = xvid_enc_stats.sse_y;
818            stats[1]   = xvid_enc_stats.sse_u;
819            stats[2]   = xvid_enc_stats.sse_v;
820    
821          return(ret);          return(ret);
822  }  }

Legend:
Removed from v.909  
changed lines
  Added in v.919

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