[cvs] / xvidcore / src / plugins / plugin_2pass2.c Repository:
ViewVC logotype

Diff of /xvidcore/src/plugins/plugin_2pass2.c

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

revision 1.1.2.29, Sun Dec 7 15:08:15 2003 UTC revision 1.1.2.37, Sat Jan 31 14:51:56 2004 UTC
# Line 29  Line 29 
29   *   *
30   *****************************************************************************/   *****************************************************************************/
31    
32    #define BQUANT_PRESCALE
33  #undef COMPENSATE_FORMULA  #undef COMPENSATE_FORMULA
34    
35    /* forces second pass not to be bigger than first */
36    #undef PASS_SMALLER
37    
38    /* automtically alters overflow controls (strength and improvement/degradation)
39            to fight most common problems without user's knowladge */
40    #define SMART_OVERFLOW_SETTING
41    
42  #include <stdio.h>  #include <stdio.h>
43  #include <math.h>  #include <math.h>
44  #include <limits.h>  #include <limits.h>
# Line 39  Line 47 
47  #include "../image/image.h"  #include "../image/image.h"
48    
49  /*****************************************************************************  /*****************************************************************************
50   * Some constants   * Some default settings
51   ****************************************************************************/   ****************************************************************************/
52    
53  #define DEFAULT_KEYFRAME_BOOST 0  #define DEFAULT_KEYFRAME_BOOST 0
54  #define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10  #define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10
55  #define DEFAULT_CURVE_COMPRESSION_HIGH 0  #define DEFAULT_CURVE_COMPRESSION_HIGH 0
56  #define DEFAULT_CURVE_COMPRESSION_LOW 0  #define DEFAULT_CURVE_COMPRESSION_LOW 0
57  #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60  #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 10
58  #define DEFAULT_MAX_OVERFLOW_DEGRADATION 60  #define DEFAULT_MAX_OVERFLOW_DEGRADATION 10
59    
60  /* Keyframe settings */  /* Keyframe settings */
61  #define DEFAULT_KFREDUCTION 20  #define DEFAULT_KFREDUCTION 20
62  #define DEFAULT_KFTHRESHOLD 1  #define DEFAULT_KFTHRESHOLD 1
63    
64  /*****************************************************************************  /*****************************************************************************
65     * Some default constants (can be tuned)
66     ****************************************************************************/
67    
68    /* Specify the invariant part of the headers bits (header+MV)
69     * as  hlength/cst */
70    #define INVARIANT_HEADER_PART_IVOP 1 /* factor 1.0f   */
71    #define INVARIANT_HEADER_PART_PVOP 2 /* factor 0.5f   */
72    #define INVARIANT_HEADER_PART_BVOP 8 /* factor 0.125f */
73    
74    /*****************************************************************************
75   * Structures   * Structures
76   ****************************************************************************/   ****************************************************************************/
77    
# Line 61  Line 79 
79  typedef struct {  typedef struct {
80          int type;               /* first pass type */          int type;               /* first pass type */
81          int quant;              /* first pass quant */          int quant;              /* first pass quant */
         int quant2;             /* Second pass quant */  
82          int blks[3];                    /* k,m,y blks */          int blks[3];                    /* k,m,y blks */
83          int length;             /* first pass length */          int length;             /* first pass length */
84            int invariant;          /* what we assume as being invariant between the two passes, it's a sub part of header + MV bits */
85          int scaled_length;      /* scaled length */          int scaled_length;      /* scaled length */
86          int desired_length;     /* desired length; calculated during encoding */          int desired_length;     /* desired length; calculated during encoding */
87          int error;          int error;
# Line 95  Line 113 
113    
114          /* Total length of each frame types (1st pass) */          /* Total length of each frame types (1st pass) */
115          uint64_t tot_length[3];          uint64_t tot_length[3];
116            uint64_t tot_invariant[3];
117    
118          /* Average length of each frame types (used first for 1st pass data and          /* Average length of each frame types (used first for 1st pass data and
119           * then for scaled averages */           * then for scaled averages */
# Line 115  Line 134 
134    
135          /*----------------------------------          /*----------------------------------
136           * Zones statistical data           * Zones statistical data
          *  
          * ToDo: Fix zones, current  
          *       implementation is buggy  
137           *--------------------------------*/           *--------------------------------*/
138    
         /* Average weight of the zones */  
         double avg_weight;  
   
139          /* Total length used by XVID_ZONE_QUANT zones */          /* Total length used by XVID_ZONE_QUANT zones */
140          int64_t tot_quant;          uint64_t tot_quant;
141            uint64_t tot_quant_invariant;
142    
143            /* Holds the total amount of frame bytes, zone weighted (only scalable
144             * part of frame bytes) */
145            uint64_t tot_weighted;
146    
147          /*----------------------------------          /*----------------------------------
148           * Advanced settings helper ratios           * Advanced settings helper ratios
# Line 194  Line 212 
212           * ToDo: description */           * ToDo: description */
213          double fq_error;          double fq_error;
214    
215            int min_quant; /* internal minimal quant, prevents wrong quants from being used */
216    
217          /*----------------------------------          /*----------------------------------
218           * Debug           * Debug
219           *--------------------------------*/           *--------------------------------*/
# Line 297  Line 317 
317          for (i=0; i<3; i++) rc->last_quant[i] = 0;          for (i=0; i<3; i++) rc->last_quant[i] = 0;
318    
319          rc->fq_error = 0;          rc->fq_error = 0;
320            rc->min_quant = 1;
321    
322          /* Count frames (and intra frames) in the stats file, store the result into          /* Count frames (and intra frames) in the stats file, store the result into
323           * the rc structure */           * the rc structure */
# Line 359  Line 380 
380          if(rc->param.container_frame_overhead)          if(rc->param.container_frame_overhead)
381                  DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- New target filesize after container compensation: %lld\n", rc->target);                  DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- New target filesize after container compensation: %lld\n", rc->target);
382    
383            /* When bitrate is not given it means it has been scaled by an external
384             * application */
385            if (rc->param.bitrate) {
386                    /* Apply zone settings
387                     * - set rc->tot_quant which represents the total num of bytes spent in
388                     *   fixed quant zones
389                     * - set rc->tot_weighted which represents the total amount of bytes
390                     *   spent in normal or weighted zones in first pass (normal zones can
391                     *   be considered weight=1)
392                     * - set rc->tot_quant_invariant which represents the total num of bytes
393                     *   spent in fixed quant zones for headers */
394                    zone_process(rc, create);
395            } else {
396                    /* External scaling -- zones are ignored */
397                    for (i=0;i<rc->num_frames;i++) {
398                            rc->stats[i].zone_mode = XVID_ZONE_WEIGHT;
399                            rc->stats[i].weight = 1.0;
400                    }
401                    rc->tot_quant = 0;
402            }
403    
404          /* Gathers some information about first pass stats:          /* Gathers some information about first pass stats:
405           *  - finds the minimum frame length for each frame type during 1st pass.           *  - finds the minimum frame length for each frame type during 1st pass.
406           *     rc->min_size[]           *     rc->min_size[]
# Line 367  Line 409 
409           *  - count how many times each frame type has been used.           *  - count how many times each frame type has been used.
410           *     rc->count[]           *     rc->count[]
411           *  - total bytes used per frame type           *  - total bytes used per frame type
412           *     rc->total[]           *     rc->tot_length[]
413             *  - total bytes considered invariant between the 2 passes
414           *  - store keyframe location           *  - store keyframe location
415           *     rc->keyframe_locations[]           *     rc->keyframe_locations[]
416           */           */
417          first_pass_stats_prepare_data(rc);          first_pass_stats_prepare_data(rc);
418    
419          /* When bitrate is not given it means it has been scaled by an external          /* If we have a user bitrate, it means it's an internal curve scaling */
          * application */  
420          if (rc->param.bitrate) {          if (rc->param.bitrate) {
                 /* Apply zone settings */  
                 zone_process(rc, create);  
421                  /* Perform internal curve scaling */                  /* Perform internal curve scaling */
422                  first_pass_scale_curve_internal(rc);                  first_pass_scale_curve_internal(rc);
         } else {  
                 /* External scaling -- zones are ignored */  
                 for (i=0;i<rc->num_frames;i++) {  
                         rc->stats[i].zone_mode = XVID_ZONE_WEIGHT;  
                         rc->stats[i].weight = 1.0;  
                 }  
                 rc->avg_weight = 1.0;  
                 rc->tot_quant = 0;  
423          }          }
424    
425          /* Apply advanced curve options, and compute some parameters in order to          /* Apply advanced curve options, and compute some parameters in order to
# Line 436  Line 468 
468          if (data->quant > 0)          if (data->quant > 0)
469                  return(0);                  return(0);
470    
471          /* Second case: We are in a Quant zone */          /* Second case: insufficent stats data
472             * We can't guess much what we should do, let core decide all alone */
473            if (data->frame_num >= rc->num_frames) {
474                    DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)",
475                            data->frame_num);
476                    return(0);
477            }
478    
479            /* Third case: We are in a Quant zone
480             * Quant zones must just ensure we use the same settings as first pass
481             * So set the quantizer and the type */
482          if (s->zone_mode == XVID_ZONE_QUANT) {          if (s->zone_mode == XVID_ZONE_QUANT) {
483                    /* Quant stuff */
484                  rc->fq_error += s->weight;                  rc->fq_error += s->weight;
485                  data->quant = (int)rc->fq_error;                  data->quant = (int)rc->fq_error;
486                  rc->fq_error -= data->quant;                  rc->fq_error -= data->quant;
487    
488                    /* The type stuff */
489                    data->type = s->type;
490    
491                    /* The only required data for AFTER step is this one for the overflow
492                     * control */
493                  s->desired_length = s->length;                  s->desired_length = s->length;
494    
495                  return(0);                  return(0);
496          }          }
497    
         /* Third case: insufficent stats data */  
         if (data->frame_num >= rc->num_frames)  
                 return(0);  
498    
499          /*************************************************************************/          /*************************************************************************/
500          /*************************************************************************/          /*************************************************************************/
# Line 513  Line 558 
558           *-----------------------------------------------------------------------*/           *-----------------------------------------------------------------------*/
559    
560          /* Compute the overflow we should compensate */          /* Compute the overflow we should compensate */
561          if (s->type != XVID_TYPE_IVOP) {          if (s->type != XVID_TYPE_IVOP || rc->overflow > 0) {
562                  double frametype_factor;                  double frametype_factor;
563                  double framesize_factor;                  double framesize_factor;
564    
# Line 544  Line 589 
589                  /* Apply the overflow strength imposed by the user */                  /* Apply the overflow strength imposed by the user */
590                  overflow *= (rc->param.overflow_control_strength/100.0f);                  overflow *= (rc->param.overflow_control_strength/100.0f);
591          } else {          } else {
592                  /* no overflow applied in IFrames because:                  /* no negative overflow applied in IFrames because:
593                   *  - their role is important as they're references for P/BFrames.                   *  - their role is important as they're references for P/BFrames.
594                   *  - there aren't much in typical sequences, so if an IFrame overflows too                   *  - there aren't much in typical sequences, so if an IFrame overflows too
595                   *    much, this overflow may impact the next IFrame too much and generate                   *    much, this overflow may impact the next IFrame too much and generate
# Line 576  Line 621 
621           * pass nor smaller than the allowed minimum.           * pass nor smaller than the allowed minimum.
622           *-----------------------------------------------------------------------*/           *-----------------------------------------------------------------------*/
623    
624    #ifdef PASS_SMALLER
625          if (dbytes > s->length) {          if (dbytes > s->length) {
626                  dbytes = s->length;                  dbytes = s->length;
627          } else if (dbytes < rc->min_length[s->type-1]) {          }
628    #endif
629    
630            /* Prevent stupid desired sizes under logical values */
631            if (dbytes < rc->min_length[s->type-1]) {
632                  dbytes = rc->min_length[s->type-1];                  dbytes = rc->min_length[s->type-1];
         } else if (dbytes > rc->max_length) {  
                 /* ToDo: this condition is always wrong as max_length == maximum frame  
                  * length of first pass, so the first condition already caps the frame  
                  * size... */  
                 capped_to_max_framesize = 1;  
                 dbytes = rc->max_length;  
                 DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- frame:%d Capped to maximum frame size\n",  
                                 data->frame_num);  
633          }          }
634    
635          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
636           * Desired frame length <-> quantizer mapping           * Desired frame length <-> quantizer mapping
637           *-----------------------------------------------------------------------*/           *-----------------------------------------------------------------------*/
638    
639          /* For bframes we must retrieve the original quant used (sent to xvidcore)  #ifdef BQUANT_PRESCALE
640           * as core applies the bquant formula before writing the stat log entry */          /* For bframes we prescale the quantizer to avoid too high quant scaling */
641          if(s->type == XVID_TYPE_BVOP) {          if(s->type == XVID_TYPE_BVOP) {
642    
643                  twopass_stat_t *b_ref = s;                  twopass_stat_t *b_ref = s;
# Line 606  Line 648 
648    
649                  /* Compute the original quant */                  /* Compute the original quant */
650                  s->quant  = 2*(100*s->quant - data->bquant_offset);                  s->quant  = 2*(100*s->quant - data->bquant_offset);
651                  s->quant += data->bquant_ratio - 1; /* to avoid rouding issues */                  s->quant += data->bquant_ratio - 1; /* to avoid rounding issues */
652                  s->quant  = s->quant/data->bquant_ratio - b_ref->quant;                  s->quant  = s->quant/data->bquant_ratio - b_ref->quant;
653          }          }
654    #endif
655    
656          /* Don't laugh at this very 'simple' quant<->filesize relationship, it          /* Don't laugh at this very 'simple' quant<->size relationship, it
657           * proves to be acurate enough for our algorithm */           * proves to be acurate enough for our algorithm */
658          scaled_quant = (double)s->quant*(double)s->length/(double)dbytes;          scaled_quant = (double)s->quant*(double)s->length/(double)dbytes;
659    
# Line 671  Line 714 
714                  data->quant = data->max_quant[s->type-1];                  data->quant = data->max_quant[s->type-1];
715          }          }
716    
717            if (data->quant < rc->min_quant) data->quant = rc->min_quant;
718    
719          /* To avoid big quality jumps from frame to frame, we apply a "security"          /* To avoid big quality jumps from frame to frame, we apply a "security"
720           * rule that makes |last_quant - new_quant| <= 2. This rule only applies           * rule that makes |last_quant - new_quant| <= 2. This rule only applies
721           * to predicted frames (P and B) */           * to predicted frames (P and B) */
# Line 698  Line 743 
743          /* Don't forget to force 1st pass frame type ;-) */          /* Don't forget to force 1st pass frame type ;-) */
744          data->type = s->type;          data->type = s->type;
745    
         /* Store the quantizer into the statistics -- Used to compensate the double  
          * formula symptom */  
         s->quant2 = data->quant;  
   
746          return 0;          return 0;
747  }  }
748    
# Line 763  Line 804 
804                  rc->KFoverflow -= rc->KFoverflow_partial;                  rc->KFoverflow -= rc->KFoverflow_partial;
805          }          }
806    
807          rc->overflow += s->error = s->desired_length - data->length;          rc->overflow += (s->error = s->desired_length - data->length);
808          rc->real_total += data->length;          rc->real_total += data->length;
809    
810          DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- frame:%d type:%c quant:%d stats:%d scaled:%d desired:%d actual:%d error:%d overflow:%.2f\n",          DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- frame:%d type:%c quant:%d stats:%d scaled:%d desired:%d actual:%d error:%d overflow:%.2f\n",
# Line 828  Line 869 
869                  /* Read the stat line from buffer */                  /* Read the stat line from buffer */
870                  fields = sscanf(ptr, "%c", &type);                  fields = sscanf(ptr, "%c", &type);
871    
872                  /* Valid stats files have at least 6 fields */                  /* Valid stats files have at least 7 fields */
873                  if (fields == 1) {                  if (fields == 1) {
874                          switch(type) {                          switch(type) {
875                          case 'i':                          case 'i':
# Line 850  Line 891 
891                  } else {                  } else {
892                                  DPRINTF(XVID_DEBUG_RC,                                  DPRINTF(XVID_DEBUG_RC,
893                                                  "[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n",                                                  "[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n",
894                                                  lines, 6-fields);                                                  lines, 7-fields);
895                  }                  }
896    
897                  /* Free the line buffer */                  /* Free the line buffer */
# Line 899  Line 940 
940    
941                  /* Convert the fields */                  /* Convert the fields */
942                  fields = sscanf(ptr,                  fields = sscanf(ptr,
943                                                  "%c %d %d %d %d %d %d\n",                                                  "%c %d %d %d %d %d %d %d\n",
944                                                  &type,                                                  &type,
945                                                  &s->quant,                                                  &s->quant,
946                                                  &s->blks[0], &s->blks[1], &s->blks[2],                                                  &s->blks[0], &s->blks[1], &s->blks[2],
947                                                  &s->length,                                                  &s->length, &s->invariant /* not really yet */,
948                                                  &s->scaled_length);                                                  &s->scaled_length);
949    
950                  /* Free line buffer, we don't need it anymore */                  /* Free line buffer, we don't need it anymore */
# Line 911  Line 952 
952    
953                  /* Fail silently, this has probably been warned in                  /* Fail silently, this has probably been warned in
954                   * statsfile_count_frames */                   * statsfile_count_frames */
955                  if(fields != 6 && fields != 7)                  if(fields != 7 && fields != 8)
956                          continue;                          continue;
957    
958                  /* Convert frame type */                  /* Convert frame type and compute the invariant length part */
959                  switch(type) {                  switch(type) {
960                  case 'i':                  case 'i':
961                  case 'I':                  case 'I':
962                          s->type = XVID_TYPE_IVOP;                          s->type = XVID_TYPE_IVOP;
963                            s->invariant /= INVARIANT_HEADER_PART_IVOP;
964                          break;                          break;
965                  case 'p':                  case 'p':
966                  case 'P':                  case 'P':
967                  case 's':                  case 's':
968                  case 'S':                  case 'S':
969                          s->type = XVID_TYPE_PVOP;                          s->type = XVID_TYPE_PVOP;
970                            s->invariant /= INVARIANT_HEADER_PART_PVOP;
971                          break;                          break;
972                  case 'b':                  case 'b':
973                  case 'B':                  case 'B':
974                          s->type = XVID_TYPE_BVOP;                          s->type = XVID_TYPE_BVOP;
975                            s->invariant /= INVARIANT_HEADER_PART_BVOP;
976                          break;                          break;
977                  default:                  default:
978                          /* Same as before, fail silently */                          /* Same as before, fail silently */
# Line 947  Line 991 
991    
992  /* pre-process the statistics data  /* pre-process the statistics data
993   * - for each type, count, tot_length, min_length, max_length   * - for each type, count, tot_length, min_length, max_length
994   * - set keyframes_locations */   * - set keyframes_locations, tot_prescaled */
995  static void  static void
996  first_pass_stats_prepare_data(rc_2pass2_t * rc)  first_pass_stats_prepare_data(rc_2pass2_t * rc)
997  {  {
# Line 959  Line 1003 
1003          for (i=0; i<3; i++) {          for (i=0; i<3; i++) {
1004                  rc->count[i]=0;                  rc->count[i]=0;
1005                  rc->tot_length[i] = 0;                  rc->tot_length[i] = 0;
1006                    rc->tot_invariant[i] = 0;
1007                  rc->min_length[i] = INT_MAX;                  rc->min_length[i] = INT_MAX;
1008          }          }
1009    
1010          rc->max_length = INT_MIN;          rc->max_length = INT_MIN;
1011            rc->tot_weighted = 0;
1012    
1013          /* Loop through all frames and find/compute all the stuff this function          /* Loop through all frames and find/compute all the stuff this function
1014           * is supposed to do */           * is supposed to do */
# Line 971  Line 1017 
1017    
1018                  rc->count[s->type-1]++;                  rc->count[s->type-1]++;
1019                  rc->tot_length[s->type-1] += s->length;                  rc->tot_length[s->type-1] += s->length;
1020                    rc->tot_invariant[s->type-1] += s->invariant;
1021                    if (s->zone_mode != XVID_ZONE_QUANT)
1022                            rc->tot_weighted += (int)(s->weight*(s->length - s->invariant));
1023    
1024                  if (s->length < rc->min_length[s->type-1]) {                  if (s->length < rc->min_length[s->type-1]) {
1025                          rc->min_length[s->type-1] = s->length;                          rc->min_length[s->type-1] = s->length;
# Line 1005  Line 1054 
1054          int i,j;          int i,j;
1055          int n = 0;          int n = 0;
1056    
         rc->avg_weight = 0.0;  
1057          rc->tot_quant = 0;          rc->tot_quant = 0;
1058            rc->tot_quant_invariant = 0;
1059    
1060          if (create->num_zones == 0) {          if (create->num_zones == 0) {
1061                  for (j = 0; j < rc->num_frames; j++) {                  for (j = 0; j < rc->num_frames; j++) {
1062                          rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;                          rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
1063                          rc->stats[j].weight = 1.0;                          rc->stats[j].weight = 1.0;
1064                  }                  }
                 rc->avg_weight += rc->num_frames * 1.0;  
1065                  n += rc->num_frames;                  n += rc->num_frames;
1066          }          }
1067    
# Line 1023  Line 1070 
1070    
1071                  int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames;                  int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames;
1072    
1073                    /* Zero weight make no sense */
1074                    if (create->zones[i].increment == 0) create->zones[i].increment = 1;
1075                    /* And obviously an undetermined infinite makes even less sense */
1076                    if (create->zones[i].base == 0) create->zones[i].base = 1;
1077    
1078                  if (i==0 && create->zones[i].frame > 0) {                  if (i==0 && create->zones[i].frame > 0) {
1079                          for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) {                          for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) {
1080                                  rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;                                  rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
1081                                  rc->stats[j].weight = 1.0;                                  rc->stats[j].weight = 1.0;
1082                          }                          }
                         rc->avg_weight += create->zones[i].frame * 1.0;  
1083                          n += create->zones[i].frame;                          n += create->zones[i].frame;
1084                  }                  }
1085    
# Line 1038  Line 1089 
1089                                  rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;                                  rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
1090                          }                          }
1091                          next -= create->zones[i].frame;                          next -= create->zones[i].frame;
                         rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base;  
1092                          n += next;                          n += next;
1093                  }else{  /* XVID_ZONE_QUANT */                  }else{  /* XVID_ZONE_QUANT */
1094                          for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {                          for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
1095                                  rc->stats[j].zone_mode = XVID_ZONE_QUANT;                                  rc->stats[j].zone_mode = XVID_ZONE_QUANT;
1096                                  rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;                                  rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
1097                                  rc->tot_quant += rc->stats[j].length;                                  rc->tot_quant += rc->stats[j].length;
1098                                    rc->tot_quant_invariant += rc->stats[j].invariant;
1099                          }                          }
1100                  }                  }
1101          }          }
         rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0;  
   
         DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- center_weight:%f (for %d frames)  fixed_bytes:%d\n", rc->avg_weight, n, rc->tot_quant);  
1102  }  }
1103    
1104    
# Line 1059  Line 1107 
1107  first_pass_scale_curve_internal(rc_2pass2_t *rc)  first_pass_scale_curve_internal(rc_2pass2_t *rc)
1108  {  {
1109          int64_t target;          int64_t target;
1110          int64_t pass1_length;          int64_t total_invariant;
1111          double scaler;          double scaler;
1112          int i, num_MBs;          int i, num_MBs;
1113    
1114          /* We remove the bytes used by the fixed quantizer zones          /* We only scale texture data ! */
1115           * ToDo: this approach is flawed, the same amount of bytes is removed from          total_invariant  = rc->tot_invariant[XVID_TYPE_IVOP-1];
1116           *       target and first pass data, this has no sense, zone_process should          total_invariant += rc->tot_invariant[XVID_TYPE_PVOP-1];
1117           *       give us two results one for unscaled data (1pass) and the other          total_invariant += rc->tot_invariant[XVID_TYPE_BVOP-1];
1118           *       one for scaled data and we should then write:          /* don't forget to substract header bytes used in quant zones, otherwise we
1119           *       target = rc->target - rc->tot_quant_scaled;           * counting them twice */
1120           *       pass1_length = rc->i+p+b - rc->tot_quant_firstpass */          total_invariant -= rc->tot_quant_invariant;
1121          target = rc->target - rc->tot_quant;  
1122            /* We remove the bytes used by the fixed quantizer zones during first pass
1123          /* Do the same for the first pass data */           * with the same quants, so we know very precisely how much that
1124          pass1_length  = rc->tot_length[XVID_TYPE_IVOP-1];           * represents */
1125          pass1_length += rc->tot_length[XVID_TYPE_PVOP-1];          target  = rc->target;
1126          pass1_length += rc->tot_length[XVID_TYPE_BVOP-1];          target -= rc->tot_quant;
         pass1_length -= rc->tot_quant;  
1127    
1128          /* Let's compute a linear scaler in order to perform curve scaling */          /* Let's compute a linear scaler in order to perform curve scaling */
1129          scaler = (double)target / (double)pass1_length;          scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted);
1130    
1131          if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {  #ifdef SMART_OVERFLOW_SETTING
1132                  DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n");          if (scaler > 0.9) {
1133                  scaler = 1.0;                  rc->param.max_overflow_degradation *= 5;
1134                    rc->param.max_overflow_improvement *= 5;
1135                    rc->param.overflow_control_strength *= 3;
1136            } else if (scaler > 0.6) {
1137                    rc->param.max_overflow_degradation *= 2;
1138                    rc->param.max_overflow_improvement *= 2;
1139                    rc->param.overflow_control_strength *= 2;
1140            } else {
1141                    rc->min_quant = 2;
1142          }          }
1143    #endif
1144    
1145          /* Compute min frame lengths (for each frame type) according to the number          /* Compute min frame lengths (for each frame type) according to the number
1146           * of MBs. We sum all block type counters of frame 0, this gives us the           * of MBs. We sum all block type counters of frame 0, this gives us the
# Line 1126  Line 1182 
1182                          continue;                          continue;
1183                  }                  }
1184    
1185                  /* Compute the scaled length */                  /* Compute the scaled length -- only non invariant data length is scaled */
1186                  len = (int)((double)s->length * scaler * s->weight / rc->avg_weight);                  len = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight);
1187    
1188                  /* Compare with the computed minimum */                  /* Compare with the computed minimum */
1189                  if (len < rc->min_length[s->type-1]) {                  if (len < rc->min_length[s->type-1]) {
# Line 1139  Line 1195 
1195                           * total counters, as we prepare a second pass for 'regular'                           * total counters, as we prepare a second pass for 'regular'
1196                           * frames */                           * frames */
1197                          target -= s->scaled_length;                          target -= s->scaled_length;
                         pass1_length -= s->length;  
1198                  } else {                  } else {
1199                          /* Do nothing for now, we'll scale this later */                          /* Do nothing for now, we'll scale this later */
1200                          s->scaled_length = 0;                          s->scaled_length = 0;
# Line 1150  Line 1205 
1205           * total counters. Now, it's possible to scale the 'regular' frames. */           * total counters. Now, it's possible to scale the 'regular' frames. */
1206    
1207          /* Scaling factor for 'regular' frames */          /* Scaling factor for 'regular' frames */
1208          scaler = (double)target / (double)pass1_length;          scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted);
   
         /* Detect undersizing */  
         if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {  
                 DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected after correction\n");  
                 scaler = 1.0;  
         }  
1209    
1210          /* Do another pass with the new scaler */          /* Do another pass with the new scaler */
1211          for (i=0; i<rc->num_frames; i++) {          for (i=0; i<rc->num_frames; i++) {
# Line 1164  Line 1213 
1213    
1214                  /* Ignore frame with forced frame sizes */                  /* Ignore frame with forced frame sizes */
1215                  if (s->scaled_length == 0)                  if (s->scaled_length == 0)
1216                          s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight);                          s->scaled_length = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight);
1217          }          }
1218    
1219          /* Job done */          /* Job done */

Legend:
Removed from v.1.1.2.29  
changed lines
  Added in v.1.1.2.37

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