[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 916, Tue Mar 11 01:08:10 2003 UTC revision 920, Sat Mar 15 14:32:56 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.3 2003-03-11 01:08:10 edgomez Exp $   * $Id: xvid_encraw.c,v 1.11.2.6 2003-03-15 14:32:56 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 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);  
   
                 if(ARG_STATS) {  
                         printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",  
                                    (stats[0] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[0]/((float)(XDIM)*(YDIM))),  
                                    (stats[1] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[1]/((float)(XDIM)*(YDIM)/4)),  
                                    (stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4)));  
426                  }                  }
427    
428                  printf("\n");                  printf("\n");
429    
430            if (m4v_size < 0) {
431                break;
432            }
433    
434                  /* Update encoding time stats */                  /* Update encoding time stats */
435                  totalenctime += enctime;                  totalenctime += enctime;
436                  totalsize += m4v_size;                  totalsize += m4v_size;
# Line 417  Line 439 
439   *                       Save stream to file   *                       Save stream to file
440   ****************************************************************************/   ****************************************************************************/
441    
442                  if (ARG_SAVEMPEGSTREAM)                  if (m4v_size>0 && ARG_SAVEMPEGSTREAM)
443                  {                  {
444                          /* Save single files */                          /* Save single files */
445                          if (out_file == NULL) {                          if (out_file == NULL) {
446                                  sprintf(filename, "%sframe%05d.m4v", filepath, filenr);                                  sprintf(filename, "%sframe%05d.m4v", filepath, output_num);
447                                  out_file = fopen(filename, "wb");                                  out_file = fopen(filename, "wb");
448                                  fwrite(mp4_buffer, m4v_size, 1, out_file);                                  fwrite(mp4_buffer, m4v_size, 1, out_file);
449                                  fclose(out_file);                                  fclose(out_file);
450                                  out_file = NULL;                                  out_file = NULL;
451                    output_num++;
452                          }                          }
453                          else {                          else {
454    
# Line 435  Line 458 
458                          }                          }
459                  }                  }
460    
461          next_frame:                  input_num++;
                 /* Read the header if it's pgm stream */  
                 if (ARG_INPUTTYPE)  
                         status = read_pgmheader(in_file);  
462    
463                  filenr++;                  /* Read the header if it's pgm stream */
464            if (!result && ARG_INPUTTYPE)
465                            result = read_pgmheader(in_file);
466    
467          } while ( (!status) && (filenr<ARG_MAXFRAMENR) );          } while (1);
468    
469    
470    
# Line 450  Line 472 
472   *         Calculate totals and averages for output, print results   *         Calculate totals and averages for output, print results
473   ****************************************************************************/   ****************************************************************************/
474    
475          totalsize    /= filenr;      if (input_num > 0) {
476          totalenctime /= filenr;              totalsize    /= input_num;
477                totalenctime /= input_num;
478        }else{
479            totalsize = -1;
480            totalenctime = -1;
481        }
482    
483          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",
484                     totalenctime, 1000/totalenctime, (int)totalsize);                     totalenctime, 1000/totalenctime, (int)totalsize);
# Line 465  Line 492 
492    
493          if (enc_handle)          if (enc_handle)
494          {          {
495                  status = enc_stop();                  result = enc_stop();
496                  if (status)                  if (result)
497                          fprintf(stderr, "Encore RELEASE problem return value %d\n", status);                          fprintf(stderr, "Encore RELEASE problem return value %d\n", result);
498          }          }
499    
500          if(in_file)          if(in_file)
# Line 520  Line 547 
547          fprintf(stderr, " -h integer     : frame height ([1.2048])\n");          fprintf(stderr, " -h integer     : frame height ([1.2048])\n");
548          fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");          fprintf(stderr, " -b integer     : target bitrate (>0 | default=900kbit)\n");
549          fprintf(stderr, " -bn integer    : max bframes (default=0)\n");          fprintf(stderr, " -bn integer    : max bframes (default=0)\n");
550        fprintf(stderr, " -p             : packed mode\n");
551          fprintf(stderr, " -bqr integer   : bframe quantizer ratio (default=150)\n");          fprintf(stderr, " -bqr integer   : bframe quantizer ratio (default=150)\n");
552          fprintf(stderr, " -bqo integer   : bframe quantizer offset (default=100)\n");          fprintf(stderr, " -bqo integer   : bframe quantizer offset (default=100)\n");
553          fprintf(stderr, " -f float       : target framerate (>0)\n");          fprintf(stderr, " -f float       : target framerate (>0)\n");
# Line 616  Line 644 
644   *     Routines for encoding: init encoder, frame step, release encoder   *     Routines for encoding: init encoder, frame step, release encoder
645   ****************************************************************************/   ****************************************************************************/
646    
647    /* sample plugin */
648    
649    int rawenc_debug(void * handle, int opt, void * param1, void * param2)
650    {
651        switch(opt)
652        {
653        case XVID_PLG_INFO :
654        case XVID_PLG_CREATE :
655        case XVID_PLG_DESTROY :
656        case XVID_PLG_BEFORE :
657           return 0;
658    
659        case XVID_PLG_AFTER :
660           {
661           xvid_plg_data_t * data = (xvid_plg_data_t*)param1;
662           printf("type=%i, quant=%i, length=%i\n", data->type, data->quant, data->length);
663           return 0;
664           }
665        }
666    
667        return XVID_ERR_FAIL;
668    }
669    
670    
671  #define FRAMERATE_INCR 1001  #define FRAMERATE_INCR 1001
672    
673  /* 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 675 
675  {  {
676          int xerr;          int xerr;
677    
678        xvid_enc_plugin_t plugins[2];
679    
680          xvid_gbl_init_t   xvid_gbl_init;          xvid_gbl_init_t   xvid_gbl_init;
681          xvid_enc_create_t xvid_enc_create;          xvid_enc_create_t xvid_enc_create;
682    
# Line 631  Line 685 
685           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
686    
687          /* Set version -- version checking will done by xvidcore*/          /* Set version -- version checking will done by xvidcore*/
688        memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
689          xvid_gbl_init.version = XVID_VERSION;          xvid_gbl_init.version = XVID_VERSION;
690    
691    
692          /* Do we have to enable ASM optimizations ? */          /* Do we have to enable ASM optimizations ? */
693          if(use_assembler) {          if(use_assembler) {
694    
# Line 654  Line 710 
710           *----------------------------------------------------------------------*/           *----------------------------------------------------------------------*/
711    
712          /* Version again */          /* Version again */
713        memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
714          xvid_enc_create.version = XVID_VERSION;          xvid_enc_create.version = XVID_VERSION;
715    
716          /* Width and Height of input frames */          /* Width and Height of input frames */
717          xvid_enc_create.width = XDIM;          xvid_enc_create.width = XDIM;
718          xvid_enc_create.height = YDIM;          xvid_enc_create.height = YDIM;
719    
720        /* init plugins  */
721    
722        xvid_enc_create.plugins = plugins;
723        xvid_enc_create.num_plugins = 0;
724        if (ARG_STATS) {
725            plugins[xvid_enc_create.num_plugins].func = xvid_plugin_psnr;
726            plugins[xvid_enc_create.num_plugins].param = NULL;
727            xvid_enc_create.num_plugins++;
728        }
729        if (ARG_DUMP) {
730            plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump;
731            plugins[xvid_enc_create.num_plugins].param = NULL;
732            xvid_enc_create.num_plugins++;
733        }
734    
735          /* No fancy thread tests */          /* No fancy thread tests */
736          xvid_enc_create.num_threads = 0;          xvid_enc_create.num_threads = 0;
737    
# Line 684  Line 756 
756          xvid_enc_create.frame_drop_ratio = 0;          xvid_enc_create.frame_drop_ratio = 0;
757    
758          /* Global encoder options */          /* Global encoder options */
759          xvid_enc_create.global = (ARG_STATS)?XVID_EXTRASTATS_ENABLE:0;          xvid_enc_create.global = 0;
760        if (ARG_PACKED) xvid_enc_create.global |= XVID_PACKED;
761    
762          /* 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 */
763          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 780 
780    
781  static int enc_main(unsigned char* image,  static int enc_main(unsigned char* image,
782                                          unsigned char* bitstream,                                          unsigned char* bitstream,
783                                          long *frametype,                      int * key,
784                                            int *stats_type,
785                        int *stats_quant,
786                        int *stats_length,
787                                          int stats[3])                                          int stats[3])
788  {  {
789          int ret;          int ret;
790    
791          xvid_enc_frame_t xvid_enc_frame;          xvid_enc_frame_t xvid_enc_frame;
792          xvid_enc_stats_t xvid_enc_stats[2];          xvid_enc_stats_t xvid_enc_stats;
793    
794          /* Version for the frame and the stats */          /* Version for the frame and the stats */
795        memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
796          xvid_enc_frame.version = XVID_VERSION;          xvid_enc_frame.version = XVID_VERSION;
797          xvid_enc_stats[0].version = XVID_VERSION;  
798          xvid_enc_stats[1].version = XVID_VERSION;      memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
799            xvid_enc_stats.version = XVID_VERSION;
800    
801          /* Bind output buffer */          /* Bind output buffer */
802          xvid_enc_frame.bitstream = bitstream;          xvid_enc_frame.bitstream = bitstream;
803          xvid_enc_frame.length = -1;          xvid_enc_frame.length = -1;
804    
805          /* Initialize input image fields */          /* Initialize input image fields */
806        if (image) {
807          xvid_enc_frame.input.plane[0]  = image;          xvid_enc_frame.input.plane[0]  = image;
808          xvid_enc_frame.input.csp       = XVID_CSP_I420;          xvid_enc_frame.input.csp       = XVID_CSP_I420;
809          xvid_enc_frame.input.stride[0] = XDIM;          xvid_enc_frame.input.stride[0] = XDIM;
810        }else{
811            xvid_enc_frame.input.csp       = XVID_CSP_NULL;
812        }
813    
814          /* Set up core's general features */          /* Set up core's general features */
815          xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY];          xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY];
# Line 749  Line 831 
831          xvid_enc_frame.quant_intra_matrix = NULL;          xvid_enc_frame.quant_intra_matrix = NULL;
832          xvid_enc_frame.quant_inter_matrix = NULL;          xvid_enc_frame.quant_inter_matrix = NULL;
833    
         /* Foll proof */  
         xvid_enc_stats[0].sse_y = 0;  
         xvid_enc_stats[0].sse_v = 0;  
         xvid_enc_stats[0].sse_u = 0;  
   
834          /* Encode the frame */          /* Encode the frame */
         xvid_enc_frame.vop_flags |= (ARG_STATS)?XVID_EXTRASTATS:0;  
835          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);
836    
837          *frametype = xvid_enc_stats[0].type;      *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME);
838          stats[0]   = xvid_enc_stats[0].sse_y;          *stats_type = xvid_enc_stats.type;
839          stats[1]   = xvid_enc_stats[0].sse_u;      *stats_quant = xvid_enc_stats.quant;
840          stats[2]   = xvid_enc_stats[0].sse_v;      *stats_length = xvid_enc_stats.length;
841            stats[0]   = xvid_enc_stats.sse_y;
842            stats[1]   = xvid_enc_stats.sse_u;
843            stats[2]   = xvid_enc_stats.sse_v;
844    
845          return(ret);          return(ret);
846  }  }

Legend:
Removed from v.916  
changed lines
  Added in v.920

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