[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 918, Tue Mar 11 23:39:47 2003 UTC revision 923, Sat Mar 15 16:41:32 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.4 2003-03-11 23:39:47 edgomez Exp $   * $Id: xvid_encraw.c,v 1.11.2.7 2003-03-15 16:41:32 suxen_drol Exp $
23   *   *
24   ****************************************************************************/   ****************************************************************************/
25    
# Line 46  Line 46 
46    
47  #include "xvid.h"  #include "xvid.h"
48    
49    
50  /*****************************************************************************  /*****************************************************************************
51   *                            Quality presets   *                            Quality presets
52   ****************************************************************************/   ****************************************************************************/
# Line 85  Line 86 
86  #define ABS_MAXFRAMENR 9999  #define ABS_MAXFRAMENR 9999
87    
88  static int   ARG_STATS = 0;  static int   ARG_STATS = 0;
89    static int   ARG_DUMP = 0;
90  static int   ARG_BITRATE = 900;  static int   ARG_BITRATE = 900;
91  static int   ARG_QUANTI = 0;  static int   ARG_QUANTI = 0;
92  static int   ARG_QUALITY = 5;  static int   ARG_QUALITY = 5;
# Line 101  Line 103 
103  static int   ARG_BQRATIO = 150;  static int   ARG_BQRATIO = 150;
104  static int   ARG_BQOFFSET = 100;  static int   ARG_BQOFFSET = 100;
105  static int   ARG_MAXBFRAMES = 0;  static int   ARG_MAXBFRAMES = 0;
106    static int   ARG_PACKED = 0;
107  #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)  #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)
108    
109  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )  #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
# Line 113  Line 116 
116   *                     Nasty global vars ;-)   *                     Nasty global vars ;-)
117   ***************************************************************************/   ***************************************************************************/
118    
119  static int i,filenr = 0;  static int i;
120    
121  /* the path where to save output */  /* the path where to save output */
122  static char filepath[256] = "./";  static char filepath[256] = "./";
# Line 141  Line 144 
144  static int enc_stop();  static int enc_stop();
145  static int enc_main(unsigned char* image,  static int enc_main(unsigned char* image,
146                                          unsigned char* bitstream,                                          unsigned char* bitstream,
147                                          long *frametype,                      int *key,
148                                            int *stats_type,
149                        int *stats_quant,
150                        int *stats_length,
151                                          int stats[3]);                                          int stats[3]);
152    
153  /*****************************************************************************  /*****************************************************************************
# Line 158  Line 164 
164          double enctime;          double enctime;
165          double totalenctime=0.;          double totalenctime=0.;
166    
167          long totalsize;          int totalsize;
168          int status;          int result;
169          long frame_type;          int m4v_size;
170        int key;
171          long m4v_size;          int stats_type;
172        int stats_quant;
173        int stats_length;
174          int use_assembler=0;          int use_assembler=0;
175    
176        int input_num;
177        int output_num;
178    
179          char filename[256];          char filename[256];
180    
181          FILE *in_file = stdin;          FILE *in_file = stdin;
# Line 198  Line 209 
209                          i++;                          i++;
210                          ARG_MAXBFRAMES = atoi(argv[i]);                          ARG_MAXBFRAMES = atoi(argv[i]);
211                  }                  }
212                    else if (strcmp("-p", argv[i]) == 0) {
213                ARG_PACKED = 1;
214                    }
215                  else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) {
216                          i++;                          i++;
217                          ARG_BQRATIO = atoi(argv[i]);                          ARG_BQRATIO = atoi(argv[i]);
# Line 221  Line 235 
235                  else if (strcmp("-s", argv[i]) == 0) {                  else if (strcmp("-s", argv[i]) == 0) {
236                          ARG_STATS = 1;                          ARG_STATS = 1;
237                  }                  }
238                    else if (strcmp("-dump", argv[i]) == 0) {
239                            ARG_DUMP = 1;
240                    }
241                  else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) {
242                          i++;                          i++;
243                          ARG_INPUTTYPE = atoi(argv[i]);                          ARG_INPUTTYPE = atoi(argv[i]);
# Line 314  Line 331 
331   ****************************************************************************/   ****************************************************************************/
332    
333    
334          status = enc_init(use_assembler);          result = enc_init(use_assembler);
335          if (status)          if (result)
336          {          {
337                  fprintf(stderr, "Encore INIT problem, return value %d\n", status);                  fprintf(stderr, "Encore INIT problem, return value %d\n", result);
338                  goto release_all;                  goto release_all;
339          }          }
340    
# Line 343  Line 360 
360    
361          totalsize = 0;          totalsize = 0;
362    
363        result = 0;
364    
365        input_num = 0;  /* input frame counter */
366        output_num = 0; /* output frame counter */
367    
368          do {          do {
369    
370                  char *type;                  char *type;
371                  int stats[3];                  int stats[3];
372    
373            if (input_num >= ARG_MAXFRAMENR)
374            {
375                result = 1;
376            }
377    
378            if (!result)
379            {
380                  if(ARG_INPUTTYPE) {                  if(ARG_INPUTTYPE) {
381                          /* read PGM data (YUV-format) */                          /* read PGM data (YUV-format) */
382                          status = read_pgmdata(in_file, in_buffer);                              result = read_pgmdata(in_file, in_buffer);
383                  } else {                  } else {
384                          /* read raw data (YUV-format) */                          /* read raw data (YUV-format) */
385                          status = read_yuvdata(in_file, in_buffer);                              result = read_yuvdata(in_file, in_buffer);
386                  }                  }
   
                 if(status) {  
                         /* Couldn't read image, most likely end-of-file */  
                         continue;  
387                  }                  }
388    
389  /*****************************************************************************  /*****************************************************************************
# Line 366  Line 391 
391   ****************************************************************************/   ****************************************************************************/
392    
393                  enctime = msecond();                  enctime = msecond();
394                  m4v_size = enc_main(in_buffer, mp4_buffer, &frame_type, stats);          m4v_size = enc_main(!result?in_buffer:0, mp4_buffer, &key, &stats_type, &stats_quant, &stats_length, stats);
395                  enctime = msecond() - enctime;                  enctime = msecond() - enctime;
396    
                 /* Not coded frames return 0 */  
                 if(m4v_size == 0) goto next_frame;  
   
397                  /* Write the Frame statistics */                  /* Write the Frame statistics */
398                  switch(frame_type) {  
399                    printf("%5d: key=%i, time(ms)=%6.1f, length=%7d",
400                !result?input_num:-1,
401                               key,
402                               (float)enctime,
403                               (int)m4v_size);
404    
405            if (stats_type > 0) {   /* !XVID_TYPE_NOTHING */
406    
407                    switch(stats_type) {
408                  case XVID_TYPE_IVOP:                  case XVID_TYPE_IVOP:
409                          type = "I";                          type = "I";
410                          break;                          break;
# Line 386  Line 417 
417                  case XVID_TYPE_SVOP:                  case XVID_TYPE_SVOP:
418                          type = "S";                          type = "S";
419                          break;                          break;
                 case XVID_TYPE_NOTHING:  
                         type = "N";  
                         break;  
420                  default:                  default:
421                          type = "U";                          type = "U";
422                          break;                          break;
423                  }                  }
424    
425                  printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d",              printf(" | type=%s quant=%2d, length=%7d", type, stats_quant, stats_length);
                            (int)filenr,  
                            type,  
                            (float)enctime,  
                            (int)m4v_size);  
426    
427                  if(ARG_STATS) {                  if(ARG_STATS) {
428                          printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",                          printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
# Line 407  Line 431 
431                                     (stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4)));                                     (stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4)));
432                  }                  }
433    
434                    }
435    
436                  printf("\n");                  printf("\n");
437    
438            if (m4v_size < 0) {
439                break;
440            }
441    
442                  /* Update encoding time stats */                  /* Update encoding time stats */
443                  totalenctime += enctime;                  totalenctime += enctime;
444                  totalsize += m4v_size;                  totalsize += m4v_size;
# Line 417  Line 447 
447   *                       Save stream to file   *                       Save stream to file
448   ****************************************************************************/   ****************************************************************************/
449    
450                  if (ARG_SAVEMPEGSTREAM)                  if (m4v_size>0 && ARG_SAVEMPEGSTREAM)
451                  {                  {
452                          /* Save single files */                          /* Save single files */
453                          if (out_file == NULL) {                          if (out_file == NULL) {
454                                  sprintf(filename, "%sframe%05d.m4v", filepath, filenr);                                  sprintf(filename, "%sframe%05d.m4v", filepath, output_num);
455                                  out_file = fopen(filename, "wb");                                  out_file = fopen(filename, "wb");
456                                  fwrite(mp4_buffer, m4v_size, 1, out_file);                                  fwrite(mp4_buffer, m4v_size, 1, out_file);
457                                  fclose(out_file);                                  fclose(out_file);
458                                  out_file = NULL;                                  out_file = NULL;
459                    output_num++;
460                          }                          }
461                          else {                          else {
462    
# Line 435  Line 466 
466                          }                          }
467                  }                  }
468    
469                  filenr++;                  input_num++;
470    
         next_frame:  
471                  /* Read the header if it's pgm stream */                  /* Read the header if it's pgm stream */
472                  if (ARG_INPUTTYPE)          if (!result && ARG_INPUTTYPE)
473                          status = read_pgmheader(in_file);                          result = read_pgmheader(in_file);
474    
475          } while ( (!status) && (filenr<ARG_MAXFRAMENR) );          } while (1);
476    
477    
478    
# Line 450  Line 480 
480   *         Calculate totals and averages for output, print results   *         Calculate totals and averages for output, print results
481   ****************************************************************************/   ****************************************************************************/
482    
483          totalsize    /= filenr;      if (input_num > 0) {
484          totalenctime /= filenr;              totalsize    /= input_num;
485                totalenctime /= input_num;
486        }else{
487            totalsize = -1;
488            totalenctime = -1;
489        }
490    
491          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",
492                     totalenctime, 1000/totalenctime, (int)totalsize);                     totalenctime, 1000/totalenctime, (int)totalsize);
# Line 465  Line 500 
500    
501          if (enc_handle)          if (enc_handle)
502          {          {
503                  status = enc_stop();                  result = enc_stop();
504                  if (status)                  if (result)
505                          fprintf(stderr, "Encore RELEASE problem return value %d\n", status);                          fprintf(stderr, "Encore RELEASE problem return value %d\n", result);
506          }          }
507    
508          if(in_file)          if(in_file)
# Line 520  Line 555 
555          fprintf(stderr, " -h integer     : frame height ([1.2048])\n");          fprintf(stderr, " -h integer     : frame height ([1.2048])\n");
556          fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");          fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");
557          fprintf(stderr, " -bn integer    : max bframes (default=0)\n");          fprintf(stderr, " -bn integer    : max bframes (default=0)\n");
558        fprintf(stderr, " -p             : packed mode\n");
559          fprintf(stderr, " -bqr integer   : bframe quantizer ratio (default=150)\n");          fprintf(stderr, " -bqr integer   : bframe quantizer ratio (default=150)\n");
560          fprintf(stderr, " -bqo integer   : bframe quantizer offset (default=100)\n");          fprintf(stderr, " -bqo integer   : bframe quantizer offset (default=100)\n");
561          fprintf(stderr, " -f float       : target framerate (>0)\n");          fprintf(stderr, " -f float       : target framerate (>0)\n");
# Line 528  Line 564 
564          fprintf(stderr, " -t integer     : input data type (yuv=0, pgm=1)\n");          fprintf(stderr, " -t integer     : input data type (yuv=0, pgm=1)\n");
565          fprintf(stderr, " -n integer     : number of frames to encode\n");          fprintf(stderr, " -n integer     : number of frames to encode\n");
566          fprintf(stderr, " -q integer     : quality ([0..5])\n");          fprintf(stderr, " -q integer     : quality ([0..5])\n");
567          fprintf(stderr, " -d boolean     : save decoder output (0 False*, !=0 True)\n");          fprintf(stderr, " -dump          : save decoder output\n");
568          fprintf(stderr, " -m             : save mpeg4 raw stream\n");          fprintf(stderr, " -m             : save mpeg4 raw stream\n");
569          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");
570          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");
# Line 616  Line 652 
652   *     Routines for encoding: init encoder, frame step, release encoder   *     Routines for encoding: init encoder, frame step, release encoder
653   ****************************************************************************/   ****************************************************************************/
654    
655    /* sample plugin */
656    
657    int rawenc_debug(void * handle, int opt, void * param1, void * param2)
658    {
659        switch(opt)
660        {
661        case XVID_PLG_INFO :
662        case XVID_PLG_CREATE :
663        case XVID_PLG_DESTROY :
664        case XVID_PLG_BEFORE :
665           return 0;
666    
667        case XVID_PLG_AFTER :
668           {
669           xvid_plg_data_t * data = (xvid_plg_data_t*)param1;
670           printf("type=%i, quant=%i, length=%i\n", data->type, data->quant, data->length);
671           return 0;
672           }
673        }
674    
675        return XVID_ERR_FAIL;
676    }
677    
678    
679  #define FRAMERATE_INCR 1001  #define FRAMERATE_INCR 1001
680    
681  /* 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 623  Line 683 
683  {  {
684          int xerr;          int xerr;
685    
686        xvid_enc_plugin_t plugins[1];
687    
688          xvid_gbl_init_t   xvid_gbl_init;          xvid_gbl_init_t   xvid_gbl_init;
689          xvid_enc_create_t xvid_enc_create;          xvid_enc_create_t xvid_enc_create;
690    
# Line 631  Line 693 
693           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
694    
695          /* Set version -- version checking will done by xvidcore*/          /* Set version -- version checking will done by xvidcore*/
696        memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
697          xvid_gbl_init.version = XVID_VERSION;          xvid_gbl_init.version = XVID_VERSION;
698    
699    
700          /* Do we have to enable ASM optimizations ? */          /* Do we have to enable ASM optimizations ? */
701          if(use_assembler) {          if(use_assembler) {
702    
# Line 654  Line 718 
718           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
719    
720          /* Version again */          /* Version again */
721        memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
722          xvid_enc_create.version = XVID_VERSION;          xvid_enc_create.version = XVID_VERSION;
723    
724          /* Width and Height of input frames */          /* Width and Height of input frames */
725          xvid_enc_create.width = XDIM;          xvid_enc_create.width = XDIM;
726          xvid_enc_create.height = YDIM;          xvid_enc_create.height = YDIM;
727    
728        /* init plugins  */
729    
730        xvid_enc_create.plugins = plugins;
731        xvid_enc_create.num_plugins = 0;
732        if (ARG_DUMP) {
733            plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump;
734            plugins[xvid_enc_create.num_plugins].param = NULL;
735            xvid_enc_create.num_plugins++;
736        }
737    
738          /* No fancy thread tests */          /* No fancy thread tests */
739          xvid_enc_create.num_threads = 0;          xvid_enc_create.num_threads = 0;
740    
# Line 684  Line 759 
759          xvid_enc_create.frame_drop_ratio = 0;          xvid_enc_create.frame_drop_ratio = 0;
760    
761          /* Global encoder options */          /* Global encoder options */
762          xvid_enc_create.global = (ARG_STATS)?XVID_EXTRASTATS_ENABLE:0;          xvid_enc_create.global = 0;
763        if (ARG_PACKED) xvid_enc_create.global |= XVID_PACKED;
764        if (ARG_STATS) xvid_enc_create.global |= XVID_EXTRASTATS_ENABLE;
765    
766          /* 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 */
767          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);          xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
# Line 707  Line 784 
784    
785  static int enc_main(unsigned char* image,  static int enc_main(unsigned char* image,
786                                          unsigned char* bitstream,                                          unsigned char* bitstream,
787                                          long *frametype,                      int * key,
788                                            int *stats_type,
789                        int *stats_quant,
790                        int *stats_length,
791                                          int stats[3])                                          int stats[3])
792  {  {
793          int ret;          int ret;
794    
795          xvid_enc_frame_t xvid_enc_frame;          xvid_enc_frame_t xvid_enc_frame;
796          xvid_enc_stats_t xvid_enc_stats[2];          xvid_enc_stats_t xvid_enc_stats;
797    
798          /* Version for the frame and the stats */          /* Version for the frame and the stats */
799        memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
800          xvid_enc_frame.version = XVID_VERSION;          xvid_enc_frame.version = XVID_VERSION;
801          xvid_enc_stats[0].version = XVID_VERSION;  
802          xvid_enc_stats[1].version = XVID_VERSION;      memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
803            xvid_enc_stats.version = XVID_VERSION;
804    
805          /* Bind output buffer */          /* Bind output buffer */
806          xvid_enc_frame.bitstream = bitstream;          xvid_enc_frame.bitstream = bitstream;
807          xvid_enc_frame.length = -1;          xvid_enc_frame.length = -1;
808    
809          /* Initialize input image fields */          /* Initialize input image fields */
810        if (image) {
811          xvid_enc_frame.input.plane[0]  = image;          xvid_enc_frame.input.plane[0]  = image;
812          xvid_enc_frame.input.csp       = XVID_CSP_I420;          xvid_enc_frame.input.csp       = XVID_CSP_I420;
813          xvid_enc_frame.input.stride[0] = XDIM;          xvid_enc_frame.input.stride[0] = XDIM;
814        }else{
815            xvid_enc_frame.input.csp       = XVID_CSP_NULL;
816        }
817    
818          /* Set up core's general features */          /* Set up core's general features */
819          xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY];          xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY];
820        if (ARG_STATS) xvid_enc_frame.vol_flags |= XVID_EXTRASTATS;
821    
822          /* Set up core's general features */          /* Set up core's general features */
823          xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY];          xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY];
# Line 749  Line 836 
836          xvid_enc_frame.quant_intra_matrix = NULL;          xvid_enc_frame.quant_intra_matrix = NULL;
837          xvid_enc_frame.quant_inter_matrix = NULL;          xvid_enc_frame.quant_inter_matrix = NULL;
838    
         /* Foll proof */  
         xvid_enc_stats[0].sse_y = 0;  
         xvid_enc_stats[0].sse_v = 0;  
         xvid_enc_stats[0].sse_u = 0;  
   
839          /* Encode the frame */          /* Encode the frame */
         xvid_enc_frame.vop_flags |= (ARG_STATS)?XVID_EXTRASTATS:0;  
840          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);
841    
842          *frametype = xvid_enc_stats[0].type;      *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME);
843          stats[0]   = xvid_enc_stats[0].sse_y;          *stats_type = xvid_enc_stats.type;
844          stats[1]   = xvid_enc_stats[0].sse_u;      *stats_quant = xvid_enc_stats.quant;
845          stats[2]   = xvid_enc_stats[0].sse_v;      *stats_length = xvid_enc_stats.length;
846            stats[0]   = xvid_enc_stats.sse_y;
847            stats[1]   = xvid_enc_stats.sse_u;
848            stats[2]   = xvid_enc_stats.sse_v;
849    
850          return(ret);          return(ret);
851  }  }

Legend:
Removed from v.918  
changed lines
  Added in v.923

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