[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.32, Sun Dec 21 12:41:48 2003 UTC revision 1.1.2.37, Sat Jan 31 14:51:56 2004 UTC
# Line 35  Line 35 
35  /* forces second pass not to be bigger than first */  /* forces second pass not to be bigger than first */
36  #undef PASS_SMALLER  #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 130  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          uint64_t tot_quant;          uint64_t tot_quant;
141          uint64_t tot_quant_invariant;          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
149           *--------------------------------*/           *--------------------------------*/
# Line 210  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 313  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 375  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 390  Line 416 
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  
                  * - set rc->tot_quant which represents the total num of bytes spent in  
                  *   fixed quant zones  
                  * - set rc->tot_quant_invariant which represents the total num of bytes spent  
                  *   in fixed quant zones for headers */  
                 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 457  Line 468 
468          if (data->quant > 0)          if (data->quant > 0)
469                  return(0);                  return(0);
470    
471          /* Second case: insufficent stats data */          /* 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) {          if (data->frame_num >= rc->num_frames) {
474                  DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)",                  DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)",
475                          data->frame_num);                          data->frame_num);
476                  return(0);                  return(0);
477          }          }
478    
479          /* Third case: We are in a Quant zone */          /* 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);
# Line 604  Line 624 
624  #ifdef PASS_SMALLER  #ifdef PASS_SMALLER
625          if (dbytes > s->length) {          if (dbytes > s->length) {
626                  dbytes = s->length;                  dbytes = s->length;
627          } else          }
628  #endif  #endif
629    
630            /* Prevent stupid desired sizes under logical values */
631                  if (dbytes < rc->min_length[s->type-1]) {                  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          /*------------------------------------------------------------------------          /*------------------------------------------------------------------------
# Line 700  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 975  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 992  Line 1008 
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 1001  Line 1018 
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;                  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 1035  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;          rc->tot_quant_invariant = 0;
1059    
# Line 1044  Line 1062 
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 1053  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 1068  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++ ) {
# Line 1079  Line 1099 
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 1090  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;
         int64_t pass1_length;  
1110          int64_t total_invariant;          int64_t total_invariant;
1111          double scaler;          double scaler;
1112          int i, num_MBs;          int i, num_MBs;
# Line 1109  Line 1125 
1125          target  = rc->target;          target  = rc->target;
1126          target -= rc->tot_quant;          target -= rc->tot_quant;
1127    
         /* Do the same for the first pass data */  
         pass1_length  = rc->tot_length[XVID_TYPE_IVOP-1];  
         pass1_length += rc->tot_length[XVID_TYPE_PVOP-1];  
         pass1_length += rc->tot_length[XVID_TYPE_BVOP-1];  
         pass1_length -= rc->tot_quant;  
   
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 - total_invariant) / (double)(pass1_length - total_invariant);          scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted);
1130    
1131  #ifdef PASS_SMALLER  #ifdef SMART_OVERFLOW_SETTING
1132          if ((target - total_invariant) <= 0 ||          if (scaler > 0.9) {
1133                  (pass1_length - total_invariant) <= 0 ||                  rc->param.max_overflow_degradation *= 5;
1134                  target >= pass1_length) {                  rc->param.max_overflow_improvement *= 5;
1135                  DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n");                  rc->param.overflow_control_strength *= 3;
1136                  scaler = 1.0;          } 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  #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
1147           * number of MBs.           * number of MBs.
# Line 1167  Line 1183 
1183                  }                  }
1184    
1185                  /* Compute the scaled length -- only non invariant data length is scaled */                  /* Compute the scaled length -- only non invariant data length is scaled */
1186                  len = s->invariant + (int)((double)(s->length-s->invariant) * 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 1179  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 1190  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 - total_invariant) / (double)(pass1_length - total_invariant);          scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted);
   
 #ifdef PASS_SMALLER  
         /* 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;  
         }  
 #endif  
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 1206  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 = s->invariant + (int)((double)(s->length-s->invariant) * 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.32  
changed lines
  Added in v.1.1.2.37

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