[svn] / branches / dev-api-4 / xvidcore / src / plugins / plugin_2pass2.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/src/plugins/plugin_2pass2.c

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

revision 1043, Sat May 24 22:03:50 2003 UTC revision 1161, Wed Oct 1 23:23:01 2003 UTC
# Line 25  Line 25 
25   * along with this program; if not, write to the Free Software   * along with this program; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *   *
28   * $Id: plugin_2pass2.c,v 1.1.2.12 2003-05-24 22:03:50 edgomez Exp $   * $Id: plugin_2pass2.c,v 1.1.2.23 2003-10-01 23:23:01 edgomez Exp $
29   *   *
30   *****************************************************************************/   *****************************************************************************/
31    
# Line 40  Line 40 
40   * Some constants   * Some constants
41   ****************************************************************************/   ****************************************************************************/
42    
 #define RAD2DEG 57.295779513082320876798154814105  
 #define DEG2RAD 0.017453292519943295769236907684886  
   
43  #define DEFAULT_KEYFRAME_BOOST 0  #define DEFAULT_KEYFRAME_BOOST 0
44  #define DEFAULT_PAYBACK_METHOD XVID_PAYBACK_PROP  #define DEFAULT_PAYBACK_METHOD XVID_PAYBACK_PROP
45  #define DEFAULT_BITRATE_PAYBACK_DELAY 250  #define DEFAULT_BITRATE_PAYBACK_DELAY 250
# Line 51  Line 48 
48  #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60  #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60
49  #define DEFAULT_MAX_OVERFLOW_DEGRADATION 60  #define DEFAULT_MAX_OVERFLOW_DEGRADATION 60
50    
 /* Alt curve settings */  
 #define DEFAULT_USE_ALT_CURVE 0  
 #define DEFAULT_ALT_CURVE_HIGH_DIST 500  
 #define DEFAULT_ALT_CURVE_LOW_DIST 90  
 #define DEFAULT_ALT_CURVE_USE_AUTO 1  
 #define DEFAULT_ALT_CURVE_AUTO_STR 30  
 #define DEFAULT_ALT_CURVE_TYPE XVID_CURVE_LINEAR  
 #define DEFAULT_ALT_CURVE_MIN_REL_QUAL 50  
 #define DEFAULT_ALT_CURVE_USE_AUTO_BONUS_BIAS 1  
 #define DEFAULT_ALT_CURVE_BONUS_BIAS 50  
   
51  /* Keyframe settings */  /* Keyframe settings */
52  #define DEFAULT_KFTRESHOLD 10  #define DEFAULT_KFTRESHOLD 10
53  #define DEFAULT_KFREDUCTION 20  #define DEFAULT_KFREDUCTION 20
# Line 109  Line 95 
95      double curve_comp_scale;      double curve_comp_scale;
96      double movie_curve;      double movie_curve;
97    
         double alt_curve_low;  
         double alt_curve_high;  
         double alt_curve_low_diff;  
         double alt_curve_high_diff;  
     double alt_curve_curve_bias_bonus;  
         double alt_curve_mid_qual;  
         double alt_curve_qual_dev;  
   
98      /* dynamic */      /* dynamic */
   
99      int * keyframe_locations;      int * keyframe_locations;
100      stat_t * stats;      stat_t * stats;
101    
102      double pquant_error[32];          double quant_error[3][32];
     double bquant_error[32];  
103      int quant_count[32];      int quant_count[32];
104      int last_quant[3];      int last_quant[3];
105    
# Line 201  Line 177 
177    
178      rc->param = *param;      rc->param = *param;
179    
180            /*
181             * Initialize all defaults
182             */
183  #define _INIT(a, b) if((a) <= 0) (a) = (b)  #define _INIT(a, b) if((a) <= 0) (a) = (b)
184      /* Let's set our defaults if needed */      /* Let's set our defaults if needed */
185          _INIT(rc->param.keyframe_boost, DEFAULT_KEYFRAME_BOOST);          _INIT(rc->param.keyframe_boost, DEFAULT_KEYFRAME_BOOST);
# Line 211  Line 190 
190      _INIT(rc->param.max_overflow_improvement, DEFAULT_MAX_OVERFLOW_IMPROVEMENT);      _INIT(rc->param.max_overflow_improvement, DEFAULT_MAX_OVERFLOW_IMPROVEMENT);
191      _INIT(rc->param.max_overflow_degradation,  DEFAULT_MAX_OVERFLOW_DEGRADATION);      _INIT(rc->param.max_overflow_degradation,  DEFAULT_MAX_OVERFLOW_DEGRADATION);
192    
     /* Alt curve settings */  
         _INIT(rc->param.use_alt_curve, DEFAULT_USE_ALT_CURVE);  
     _INIT(rc->param.alt_curve_high_dist, DEFAULT_ALT_CURVE_HIGH_DIST);  
     _INIT(rc->param.alt_curve_low_dist, DEFAULT_ALT_CURVE_LOW_DIST);  
     _INIT(rc->param.alt_curve_use_auto, DEFAULT_ALT_CURVE_USE_AUTO);  
     _INIT(rc->param.alt_curve_auto_str, DEFAULT_ALT_CURVE_AUTO_STR);  
     _INIT(rc->param.alt_curve_type, DEFAULT_ALT_CURVE_TYPE);  
     _INIT(rc->param.alt_curve_min_rel_qual, DEFAULT_ALT_CURVE_MIN_REL_QUAL);  
     _INIT(rc->param.alt_curve_use_auto_bonus_bias, DEFAULT_ALT_CURVE_USE_AUTO_BONUS_BIAS);  
     _INIT(rc->param.alt_curve_bonus_bias, DEFAULT_ALT_CURVE_BONUS_BIAS);  
   
193      /* Keyframe settings */      /* Keyframe settings */
194          _INIT(rc->param.kftreshold, DEFAULT_KFTRESHOLD);          _INIT(rc->param.kftreshold, DEFAULT_KFTRESHOLD);
195      _INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION);      _INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION);
196      _INIT(rc->param.min_key_interval, DEFAULT_MIN_KEY_INTERVAL);      _INIT(rc->param.min_key_interval, DEFAULT_MIN_KEY_INTERVAL);
197  #undef _INIT  #undef _INIT
198    
199            /* Initialize some stuff to zero */
200            for(i=0; i<32; i++) rc->quant_count[i] = 0;
201    
202            for(i=0; i<3; i++) {
203                    int j;
204                    for (j=0; j<32; j++)
205                            rc->quant_error[i][j] = 0;
206            }
207    
208            for (i=0; i<3; i++)
209                    rc->last_quant[i] = 0;
210    
211            rc->fq_error = 0;
212    
213          /* Count frames in the stats file */          /* Count frames in the stats file */
214      if (!det_stats_length(rc, param->filename)){      if (!det_stats_length(rc, param->filename)){
215          DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename);                  DPRINTF(XVID_DEBUG_RC,"ERROR: fopen %s failed\n", param->filename);
216          free(rc);          free(rc);
217          return XVID_ERR_FAIL;          return XVID_ERR_FAIL;
218      }      }
# Line 245  Line 227 
227           * Allocate keyframes location's memory           * Allocate keyframes location's memory
228           * PS: see comment in pre_process0 for the +1 location requirement           * PS: see comment in pre_process0 for the +1 location requirement
229           */           */
230      if ((rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int))) == NULL) {          rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int));
231            if (rc->keyframe_locations == NULL) {
232          free(rc->stats);          free(rc->stats);
233          free(rc);          free(rc);
234          return XVID_ERR_MEMORY;          return XVID_ERR_MEMORY;
235      }      }
236    
237      if (!load_stats(rc, param->filename)) {      if (!load_stats(rc, param->filename)) {
238          DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename);                  DPRINTF(XVID_DEBUG_RC,"ERROR: fopen %s failed\n", param->filename);
239          free(rc->keyframe_locations);          free(rc->keyframe_locations);
240          free(rc->stats);          free(rc->stats);
241          free(rc);          free(rc);
242          return XVID_ERR_FAIL;          return XVID_ERR_FAIL;
243      }      }
244    
245      /* pre-process our stats */          /* Compute the target filesize */
246            if (rc->param.bitrate<0) {
247          if (rc->num_frames  < create->fbase/create->fincr) {                  /* if negative, bitrate equals the target (in kbytes) */
248                  rc->target = rc->param.bitrate / 8;     /* one second */                  rc->target = (-rc->param.bitrate) * 1024;
249            } else if (rc->num_frames  < create->fbase/create->fincr) {
250                    /* Source sequence is less than 1s long, we do as if it was 1s long */
251                    rc->target = rc->param.bitrate / 8;
252          } else {          } else {
253                    /* Target filesize = bitrate/8 * numframes / framerate */
254                  rc->target =                  rc->target =
255                          ((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * (uint64_t)create->fincr) / \                          ((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * \
256                             (uint64_t)create->fincr) / \
257                          ((uint64_t)create->fbase * 8);                          ((uint64_t)create->fbase * 8);
258          }          }
259    
260            DPRINTF(XVID_DEBUG_RC, "Frame rate: %d/%d (%ffps)\n",
261                            create->fbase, create->fincr,
262                            (double)create->fbase/(double)create->fincr);
263      DPRINTF(XVID_DEBUG_RC, "Number of frames: %d\n", rc->num_frames);      DPRINTF(XVID_DEBUG_RC, "Number of frames: %d\n", rc->num_frames);
         DPRINTF(XVID_DEBUG_RC, "Frame rate: %d/%d\n", create->fbase, create->fincr);  
264          DPRINTF(XVID_DEBUG_RC, "Target bitrate: %ld\n", rc->param.bitrate);          DPRINTF(XVID_DEBUG_RC, "Target bitrate: %ld\n", rc->param.bitrate);
265          DPRINTF(XVID_DEBUG_RC, "Target filesize: %lld\n", rc->target);          DPRINTF(XVID_DEBUG_RC, "Target filesize: %lld\n", rc->target);
266    
267          /* Compensate the mean frame overhead caused by the container */          /* Compensate the average frame overhead caused by the container */
268          rc->target -= rc->num_frames*rc->param.container_frame_overhead;          rc->target -= rc->num_frames*rc->param.container_frame_overhead;
269          DPRINTF(XVID_DEBUG_RC, "Container Frame overhead: %d\n", rc->param.container_frame_overhead);          DPRINTF(XVID_DEBUG_RC, "Container Frame overhead: %d\n", rc->param.container_frame_overhead);
270          DPRINTF(XVID_DEBUG_RC, "Target filesize (after container compensation): %lld\n", rc->target);          DPRINTF(XVID_DEBUG_RC, "Target filesize (after container compensation): %lld\n", rc->target);
271    
272            /*
273             * First data pre processing:
274             *  - finds the minimum frame length for each frame type during 1st pass.
275             *     rc->min_size[]
276             *  - determines the maximum frame length observed (no frame type distinction).
277             *     rc->max_size
278             *  - count how many times each frame type has been used.
279             *     rc->count[]
280             *  - total bytes used per frame type
281             *     rc->total[]
282             *  - store keyframe location
283             *     rc->keyframe_locations[]
284             */
285          pre_process0(rc);          pre_process0(rc);
286    
287            /*
288             * When bitrate is not given it means it has been scaled by an external
289             * application
290             */
291          if (rc->param.bitrate) {          if (rc->param.bitrate) {
292                    /* Apply zone settings */
293          zone_process(rc, create);          zone_process(rc, create);
294                    /* Perform curve scaling */
295                  internal_scale(rc);                  internal_scale(rc);
296      }else{      }else{
297          /* external scaler: ignore zone */                  /* External scaling -- zones are ignored */
298          for (i=0;i<rc->num_frames;i++) {          for (i=0;i<rc->num_frames;i++) {
299              rc->stats[i].zone_mode = XVID_ZONE_WEIGHT;              rc->stats[i].zone_mode = XVID_ZONE_WEIGHT;
300              rc->stats[i].weight = 1.0;              rc->stats[i].weight = 1.0;
# Line 293  Line 302 
302          rc->avg_weight = 1.0;          rc->avg_weight = 1.0;
303          rc->tot_quant = 0;          rc->tot_quant = 0;
304      }      }
         pre_process1(rc);  
   
     for (i=0; i<32;i++) {  
         rc->pquant_error[i] = 0;  
         rc->bquant_error[i] = 0;  
         rc->quant_count[i] = 0;  
     }  
305    
306      rc->fq_error = 0;          pre_process1(rc);
307    
308      *handle = rc;      *handle = rc;
309          return(0);          return(0);
# Line 330  Line 332 
332      int desired;      int desired;
333      double dbytes;      double dbytes;
334      double curve_temp;      double curve_temp;
335            double scaled_quant;
336      int capped_to_max_framesize = 0;      int capped_to_max_framesize = 0;
337    
338          /*          /*
# Line 343  Line 346 
346    
347          /* Second case: We are in a Quant zone */          /* Second case: We are in a Quant zone */
348          if (s->zone_mode == XVID_ZONE_QUANT) {          if (s->zone_mode == XVID_ZONE_QUANT) {
   
349                  rc->fq_error += s->weight;                  rc->fq_error += s->weight;
350                  data->quant = (int)rc->fq_error;                  data->quant = (int)rc->fq_error;
351                  rc->fq_error -= data->quant;                  rc->fq_error -= data->quant;
# Line 351  Line 353 
353                  s->desired_length = s->length;                  s->desired_length = s->length;
354    
355                  return(0);                  return(0);
   
356          }          }
357    
358          /* Third case: insufficent stats data */          /* Third case: insufficent stats data */
359          if (data->frame_num >= rc->num_frames)          if (data->frame_num >= rc->num_frames)
360                  return 0;                  return 0;
361    
         /*  
          * The last case is the one every normal minded developer should fear to  
          * maintain in a project :-)  
          */  
   
362          /* XXX: why by 8 */          /* XXX: why by 8 */
363          overflow = rc->overflow / 8;          overflow = rc->overflow / 8;
364    
# Line 370  Line 366 
366           * The rc->overflow field represents the overflow in current scene (between two           * The rc->overflow field represents the overflow in current scene (between two
367           * IFrames) so we must not forget to reset it if we are entering a new scene           * IFrames) so we must not forget to reset it if we are entering a new scene
368           */           */
369          if (s->type == XVID_TYPE_IVOP) {          if (s->type == XVID_TYPE_IVOP)
370                  overflow = 0;                  overflow = 0;
         }  
371    
372          desired = s->scaled_length;          desired = s->scaled_length;
373    
374          dbytes = desired;          dbytes = desired;
375          if (s->type == XVID_TYPE_IVOP) {          if (s->type == XVID_TYPE_IVOP)
376                  dbytes += desired * rc->param.keyframe_boost / 100;                  dbytes += desired * rc->param.keyframe_boost / 100;
         }  
377          dbytes /= rc->movie_curve;          dbytes /= rc->movie_curve;
378    
379          /*          /*
          * We are now entering in the hard part of the algo, it was first designed  
          * to work with i/pframes only streams, so the way it computes things is  
          * adapted to pframes only. However we can use it if we just take care to  
          * scale the bframes sizes to pframes sizes using the ratio avg_p/avg_p and  
          * then before really using values depending on frame sizes, scaling the  
          * value again with the inverse ratio  
          */  
         if (s->type == XVID_TYPE_BVOP)  
                 dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];  
   
         /*  
380           * Apply user's choosen Payback method. Payback helps bitrate to follow the           * Apply user's choosen Payback method. Payback helps bitrate to follow the
381           * scaled curve "paying back" past errors in curve previsions.           * scaled curve "paying back" past errors in curve previsions.
382           */           */
# Line 401  Line 384 
384                  desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay);                  desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay);
385          } else {          } else {
386                  desired = (int)(rc->curve_comp_error * dbytes /                  desired = (int)(rc->curve_comp_error * dbytes /
387                                                  rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay);                                                  rc->avg_length[s->type-1] / rc->param.bitrate_payback_delay);
388    
389                  if (labs(desired) > fabs(rc->curve_comp_error)) {                  if (labs(desired) > fabs(rc->curve_comp_error))
390                          desired = (int)rc->curve_comp_error;                          desired = (int)rc->curve_comp_error;
391                  }                  }
         }  
392    
393          rc->curve_comp_error -= desired;          rc->curve_comp_error -= desired;
394    
         /*  
          * Alt curve treatment is not that hard to understand though the formulas  
          * seem to be huge. Alt treatment is basically a way to soft/harden the  
          * curve flux applying sine/linear/cosine ratios  
          */  
   
395          /* XXX: warning */          /* XXX: warning */
396          curve_temp = 0;          curve_temp = 0;
397    
398          if (rc->param.use_alt_curve) {          if ((rc->param.curve_compression_high + rc->param.curve_compression_low) &&     s->type != XVID_TYPE_IVOP) {
                 if (s->type != XVID_TYPE_IVOP)  {  
                         if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {  
                                 if (dbytes >= rc->alt_curve_high) {  
                                         curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev);  
                                 } else {  
                                         switch(rc->param.alt_curve_type) {  
                                         case XVID_CURVE_SINE :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)));  
                                                 break;  
                                         case XVID_CURVE_LINEAR :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff);  
                                                 break;  
                                         case XVID_CURVE_COSINE :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))));  
                                         }  
                                 }  
                         } else {  
                                 if (dbytes <= rc->alt_curve_low){  
                                         curve_temp = dbytes;  
                                 } else {  
                                         switch(rc->param.alt_curve_type) {  
                                         case XVID_CURVE_SINE :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)));  
                                                 break;  
                                         case XVID_CURVE_LINEAR :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff);  
                                                 break;  
                                         case XVID_CURVE_COSINE :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))));  
                                         }  
                                 }  
                         }  
   
                         /*  
                          * End of code path for curve_temp, as told earlier, we are now  
                          * obliged to scale the value to a bframe one using the inverse  
                          * ratio applied earlier  
                          */  
                         if (s->type == XVID_TYPE_BVOP)  
                                 curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];  
   
                         curve_temp = curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus;  
   
                         desired += ((int)curve_temp);  
                         rc->curve_comp_error += curve_temp - (int)curve_temp;  
                 } else {  
                         /*  
                          * End of code path for dbytes, as told earlier, we are now  
                          * obliged to scale the value to a bframe one using the inverse  
                          * ratio applied earlier  
                          */  
                         if (s->type == XVID_TYPE_BVOP)  
                                 dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];  
   
                         desired += ((int)dbytes);  
                         rc->curve_comp_error += dbytes - (int)dbytes;  
                 }  
   
         } else if ((rc->param.curve_compression_high + rc->param.curve_compression_low) &&      s->type != XVID_TYPE_IVOP) {  
399    
400                  curve_temp = rc->curve_comp_scale;                  curve_temp = rc->curve_comp_scale;
401                  if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {                  if (dbytes > rc->avg_length[s->type-1]) {
402                          curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0);                          curve_temp *= ((double)dbytes + (rc->avg_length[s->type-1] - dbytes) * rc->param.curve_compression_high / 100.0);
403                  } else {                  } else {
404                          curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);                          curve_temp *= ((double)dbytes + (rc->avg_length[s->type-1] - dbytes) * rc->param.curve_compression_low / 100.0);
405                  }                  }
406    
                 /*  
                  * End of code path for curve_temp, as told earlier, we are now  
                  * obliged to scale the value to a bframe one using the inverse  
                  * ratio applied earlier  
                  */  
                 if (s->type == XVID_TYPE_BVOP)  
                         curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];  
   
407                  desired += (int)curve_temp;                  desired += (int)curve_temp;
408                  rc->curve_comp_error += curve_temp - (int)curve_temp;                  rc->curve_comp_error += curve_temp - (int)curve_temp;
409          } else {          } else {
                 /*  
                  * End of code path for dbytes, as told earlier, we are now  
                  * obliged to scale the value to a bframe one using the inverse  
                  * ratio applied earlier  
                  */  
                 if (s->type == XVID_TYPE_BVOP)  
                         dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];  
   
410                  desired += (int)dbytes;                  desired += (int)dbytes;
411                  rc->curve_comp_error += dbytes - (int)dbytes;                  rc->curve_comp_error += dbytes - (int)dbytes;
412          }          }
413    
   
414          /*          /*
415           * We can't do bigger frames than first pass, this would be stupid as first           * We can't do bigger frames than first pass, this would be stupid as first
416           * pass is quant=2 and that reaching quant=1 is not worth it. We would lose           * pass is quant=2 and that reaching quant=1 is not worth it. We would lose
# Line 530  Line 430 
430    
431          s->desired_length = desired;          s->desired_length = desired;
432    
433            /*
434          /* if this keyframe is too close to the next, reduce it's byte allotment           * if this keyframe is too close to the next, reduce it's byte allotment
435             XXX: why do we do this after setting the desired length  */           * XXX: why do we do this after setting the desired length ?
436             */
437    
438          if (s->type == XVID_TYPE_IVOP) {          if (s->type == XVID_TYPE_IVOP) {
439                  int KFdistance = rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1];                  int KFdistance = rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1];
# Line 557  Line 458 
458                  }                  }
459          }          }
460    
461            /*
462             * The "sens commun" would force us to use rc->avg_length[s->type-1] but
463             * even VFW code uses the pframe average length. Note that this length is
464             * used with desired which represents bframes _and_ pframes length.
465             *
466             * XXX: why are we using the avg pframe length for all frame types ?
467             */
468          overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]);          overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]);
469    
470          /* Reign in overflow with huge frames */          /* Reign in overflow with huge frames */
471          if (labs(overflow) > labs(rc->overflow)) {          if (labs(overflow) > labs(rc->overflow))
472                  overflow = rc->overflow;                  overflow = rc->overflow;
         }  
473    
474          /* Make sure overflow doesn't run away */          /* Make sure overflow doesn't run away */
475          if (overflow > desired * rc->param.max_overflow_improvement / 100) {          if (overflow > desired * rc->param.max_overflow_improvement / 100) {
# Line 593  Line 500 
500           * Don't laugh at this very 'simple' quant<->filesize relationship, it           * Don't laugh at this very 'simple' quant<->filesize relationship, it
501           * proves to be acurate enough for our algorithm           * proves to be acurate enough for our algorithm
502           */           */
503          data->quant = s->quant*s->length/desired;          scaled_quant = (double)s->quant*(double)s->length/(double)desired;
504    
505            /*
506             * Quantizer has been scaled using floating point operations/results, we
507             * must cast it to integer
508             */
509            data->quant = (int)scaled_quant;
510    
511          /* Let's clip the computed quantizer, if needed */          /* Let's clip the computed quantizer, if needed */
512          if (data->quant < 1) {          if (data->quant < 1) {
# Line 603  Line 516 
516          } else if (s->type != XVID_TYPE_IVOP) {          } else if (s->type != XVID_TYPE_IVOP) {
517    
518                  /*                  /*
519                   * The frame quantizer has not been clipped, this appear to be a good                   * The frame quantizer has not been clipped, this appears to be a good
520                   * computed quantizer, however past frames give us some info about how                   * computed quantizer, do not loose quantizer decimal part that we
521                   * this quantizer performs against the algo prevision. Let's use this                   * accumulate for later reuse when its sum represents a complete unit.
                  * prevision to increase the quantizer when we observe a too big  
                  * accumulated error  
522                   */                   */
523                  if (s->type == XVID_TYPE_BVOP) {                  rc->quant_error[s->type-1][data->quant] += scaled_quant - (double)data->quant;
                         rc->bquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant;  
524    
525                          if (rc->bquant_error[data->quant] >= 1.0) {                  if (rc->quant_error[s->type-1][data->quant] >= 1.0) {
526                                  rc->bquant_error[data->quant] -= 1.0;                          rc->quant_error[s->type-1][data->quant] -= 1.0;
527                                  data->quant++;                                  data->quant++;
528                    } else if (rc->quant_error[s->type-1][data->quant] <= -1.0) {
529                            rc->quant_error[s->type-1][data->quant] += 1.0;
530                            data->quant--;
531                          }                          }
                 } else {  
                         rc->pquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant;  
532    
                         if (rc->pquant_error[data->quant] >= 1.0) {  
                                 rc->pquant_error[data->quant] -= 1.0;  
                                 data->quant++;  
                         }  
                 }  
533          }          }
534    
535          /*          /*
# Line 665  Line 571 
571           * We don't want to pollute the RC history results when our computed quant           * We don't want to pollute the RC history results when our computed quant
572           * has been computed from a capped frame size           * has been computed from a capped frame size
573           */           */
574          if (capped_to_max_framesize == 0) {          if (capped_to_max_framesize == 0)
575                  rc->last_quant[s->type-1] = data->quant;                  rc->last_quant[s->type-1] = data->quant;
576          }  
577            /* Force frame type */
578            data->type = s->type;
579    
580          return 0;          return 0;
581  }  }
# Line 693  Line 601 
601          rc->overflow += rc->KFoverflow;          rc->overflow += rc->KFoverflow;
602          rc->KFoverflow = s->desired_length - data->length;          rc->KFoverflow = s->desired_length - data->length;
603    
604          if (kfdiff > 1) {  // non-consecutive keyframes                  if (kfdiff > 1) {  /* non-consecutive keyframes */
605              rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1);              rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1);
606          }else{ // consecutive keyframes                  }else{ /* consecutive keyframes */
607                          rc->overflow += rc->KFoverflow;                          rc->overflow += rc->KFoverflow;
608                          rc->KFoverflow = 0;                          rc->KFoverflow = 0;
609                          rc->KFoverflow_partial = 0;                          rc->KFoverflow_partial = 0;
610          }          }
611          rc->KF_idx++;          rc->KF_idx++;
612      } else {      } else {
613          // distribute part of the keyframe overflow                  /* distribute part of the keyframe overflow */
614          rc->overflow += s->desired_length - data->length + rc->KFoverflow_partial;          rc->overflow += s->desired_length - data->length + rc->KFoverflow_partial;
615          rc->KFoverflow -= rc->KFoverflow_partial;          rc->KFoverflow -= rc->KFoverflow_partial;
616      }      }
617    
618          DPRINTF(XVID_DEBUG_RC, "[%i] type:%c quant:%i stats1:%i scaled:%i actual:%i overflow:%i\n",          DPRINTF(XVID_DEBUG_RC, "[%i] type:%c quant:%i stats1:%i scaled:%i actual:%i desired:%d overflow:%i\n",
619                          data->frame_num,                          data->frame_num,
620                          frame_type[data->type-1],                          frame_type[data->type-1],
621                          data->quant,                          data->quant,
622                          s->length,                          s->length,
623                          s->scaled_length,                          s->scaled_length,
624                          data->length,                          data->length,
625                            s->desired_length,
626                          rc->overflow);                          rc->overflow);
627    
628      return(0);      return(0);
# Line 788  Line 697 
697          }else if (type == 'b') {          }else if (type == 'b') {
698              s->type = XVID_TYPE_BVOP;              s->type = XVID_TYPE_BVOP;
699          }else{  /* unknown type */          }else{  /* unknown type */
700              DPRINTF(XVID_DEBUG_RC, "unknown stats frame type; assuming pvop\n");                          DPRINTF(XVID_DEBUG_RC, "WARNING: unknown stats frame type, assuming pvop\n");
701              s->type = XVID_TYPE_PVOP;              s->type = XVID_TYPE_PVOP;
702          }          }
703    
# Line 824  Line 733 
733  {  {
734      int i,j;      int i,j;
735    
736            /*
737             * *rc fields initialization
738             * NB: INT_MAX and INT_MIN are used in order to be immediately replaced
739             *     with real values of the 1pass
740             */
741      for (i=0; i<3; i++) {      for (i=0; i<3; i++) {
742          rc->count[i]=0;          rc->count[i]=0;
743          rc->tot_length[i] = 0;          rc->tot_length[i] = 0;
         rc->last_quant[i] = 0;  
744                  rc->min_length[i] = INT_MAX;                  rc->min_length[i] = INT_MAX;
745      }      }
746    
747          rc->max_length = INT_MIN;          rc->max_length = INT_MIN;
748    
749            /*
750             * Loop through all frames and find/compute all the stuff this function
751             * is supposed to do
752             */
753      for (i=j=0; i<rc->num_frames; i++) {      for (i=j=0; i<rc->num_frames; i++) {
754          stat_t * s = &rc->stats[i];          stat_t * s = &rc->stats[i];
755    
# Line 910  Line 827 
827              next -= create->zones[i].frame;              next -= create->zones[i].frame;
828              rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base;              rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base;
829              n += next;              n += next;
830          }else{  // XVID_ZONE_QUANT                  }else{  /* XVID_ZONE_QUANT */
831              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++ ) {
832                  rc->stats[j].zone_mode = XVID_ZONE_QUANT;                  rc->stats[j].zone_mode = XVID_ZONE_QUANT;
833                  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;
# Line 932  Line 849 
849          int64_t target  = rc->target - rc->tot_quant;          int64_t target  = rc->target - rc->tot_quant;
850          int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant;          int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant;
851          double scaler;          double scaler;
852          int i;          int i, num_MBs;
   
853    
854          /* Let's compute a linear scaler in order to perform curve scaling */          /* Let's compute a linear scaler in order to perform curve scaling */
855          scaler = (double)target / (double)pass1_length;          scaler = (double)target / (double)pass1_length;
# Line 948  Line 864 
864                          (int)target, (int)pass1_length, scaler);                          (int)target, (int)pass1_length, scaler);
865    
866          /*          /*
867             * Compute min frame lengths (for each frame type) according to the number
868             * of MBs. We sum all blocks count from frame 0 (should be an IFrame, so
869             * blocks[0] should be enough) to know how many MBs there are.
870             *
871             * We compare these hardcoded values with observed values in first pass
872             * (determined in pre_process0).Then we keep the real minimum.
873             */
874            num_MBs = rc->stats[0].blks[0] + rc->stats[0].blks[1] + rc->stats[0].blks[2];
875    
876            if(rc->min_length[0] > ((num_MBs*22) + 240) / 8)
877                    rc->min_length[0] = ((num_MBs*22) + 240) / 8;
878    
879            if(rc->min_length[1] > ((num_MBs) + 88)  / 8)
880                    rc->min_length[1] = ((num_MBs) + 88)  / 8;
881    
882            if(rc->min_length[2] > 8)
883                    rc->min_length[2] = 8;
884    
885            /*
886           * Perform an initial scale pass.           * Perform an initial scale pass.
887           * If a frame size is scaled underneath our hardcoded minimums, then we           * If a frame size is scaled underneath our hardcoded minimums, then we
888           * force the frame size to the minimum, and deduct the original & scaled           * force the frame size to the minimum, and deduct the original & scaled
889           * frame length from the original and target total lengths           * frame length from the original and target total lengths
890           */           */
   
891          for (i=0; i<rc->num_frames; i++) {          for (i=0; i<rc->num_frames; i++) {
892                  stat_t * s = &rc->stats[i];                  stat_t * s = &rc->stats[i];
                 int min_size[3];  
893                  int len;                  int len;
894    
                 /* Compute min frame lengths (oe for each frame type) */  
                 min_size[0] = ((s->blks[0]*22) + 240) / 8;  
                 min_size[1] = (s->blks[0] + 88) / 8;  
                 min_size[2] = 8;  
   
895          if (s->zone_mode == XVID_ZONE_QUANT) {          if (s->zone_mode == XVID_ZONE_QUANT) {
896              s->scaled_length = s->length;              s->scaled_length = s->length;
897                          continue;                          continue;
898                  }                  }
899    
900                  /* Compute teh scaled length */                  /* Compute the scaled length */
901                  len = (int)((double)s->length * scaler * s->weight / rc->avg_weight);                  len = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
902    
903                  /* Compare with the computed minimum */                  /* Compare with the computed minimum */
904                  if (len < min_size[s->type-1]) {                  if (len < rc->min_length[s->type-1]) {
905                          /* force frame size to our computed minimum */                          /* force frame size to our computed minimum */
906                          s->scaled_length = min_size[s->type-1];                          s->scaled_length = rc->min_length[s->type-1];
907                          target -= s->scaled_length;                          target -= s->scaled_length;
908                          pass1_length -= s->length;                          pass1_length -= s->length;
909                  } else {                  } else {
910                          /* Do nothing for now, we'll scale this later */                          /* Do nothing for now, we'll scale this later */
911                          s->scaled_length = 0;                          s->scaled_length = 0;
912                  }                  }
   
913          }          }
914    
915          /* Correct the scaler for all non forced frames */          /* Correct the scaler for all non forced frames */
# Line 1006  Line 933 
933                  if (s->scaled_length == 0)                  if (s->scaled_length == 0)
934                          s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight);                          s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
935          }          }
   
936  }  }
937    
938  static void  static void
# Line 1044  Line 970 
970          }          }
971      }      }
972    
     /* alt curve stuff here */  
   
     if (rc->param.use_alt_curve) {  
         const double avg_pvop = rc->avg_length[XVID_TYPE_PVOP-1];  
         const uint64_t tot_pvop = rc->tot_length[XVID_TYPE_PVOP-1];  
         const uint64_t tot_bvop = rc->tot_length[XVID_TYPE_BVOP-1];  
         const uint64_t tot_scaled_pvop = rc->tot_scaled_length[XVID_TYPE_PVOP-1];  
         const uint64_t tot_scaled_bvop = rc->tot_scaled_length[XVID_TYPE_BVOP-1];  
   
                 rc->alt_curve_low = avg_pvop - avg_pvop * (double)rc->param.alt_curve_low_dist / 100.0;  
                 rc->alt_curve_low_diff = avg_pvop - rc->alt_curve_low;  
                 rc->alt_curve_high = avg_pvop + avg_pvop * (double)rc->param.alt_curve_high_dist / 100.0;  
                 rc->alt_curve_high_diff = rc->alt_curve_high - avg_pvop;  
   
         if (rc->param.alt_curve_use_auto) {  
             if (tot_bvop + tot_pvop > tot_scaled_bvop + tot_scaled_pvop) {  
                                 rc->param.alt_curve_min_rel_qual = (int)(100.0 - (100.0 - 100.0 /  
                                         ((double)(tot_pvop + tot_bvop) / (double)(tot_scaled_pvop + tot_scaled_bvop))) * (double)rc->param.alt_curve_auto_str / 100.0);  
   
                                 if (rc->param.alt_curve_min_rel_qual < 20)  
                                         rc->param.alt_curve_min_rel_qual = 20;  
             }else{  
                                 rc->param.alt_curve_min_rel_qual = 100;  
             }  
         }  
                 rc->alt_curve_mid_qual = (1.0 + (double)rc->param.alt_curve_min_rel_qual / 100.0) / 2.0;  
                 rc->alt_curve_qual_dev = 1.0 - rc->alt_curve_mid_qual;  
   
         if (rc->param.alt_curve_low_dist > 100) {  
                         switch(rc->param.alt_curve_type) {  
             case XVID_CURVE_SINE: // Sine Curve (high aggressiveness)  
                                 rc->alt_curve_qual_dev *= 2.0 / (1.0 + sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)));  
                                 rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff));  
                                 break;  
                         case XVID_CURVE_LINEAR: // Linear (medium aggressiveness)  
                                 rc->alt_curve_qual_dev *= 2.0 / (1.0 + avg_pvop / rc->alt_curve_low_diff);  
                                 rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * avg_pvop / rc->alt_curve_low_diff;  
                                 break;  
                         case XVID_CURVE_COSINE: // Cosine Curve (low aggressiveness)  
                                 rc->alt_curve_qual_dev *= 2.0 / (1.0 + (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff))));  
                                 rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)));  
                         }  
                 }  
     }  
   
973      /* --- */      /* --- */
974    
975      total1=total2=0;      total1=total2=0;
# Line 1102  Line 983 
983              dbytes = s->scaled_length / rc->movie_curve;              dbytes = s->scaled_length / rc->movie_curve;
984              dbytes2 = 0; /* XXX: warning */              dbytes2 = 0; /* XXX: warning */
985              total1 += dbytes;              total1 += dbytes;
             if (s->type == XVID_TYPE_BVOP)  
                 dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];  
986    
987              if (rc->param.use_alt_curve) {                          if (dbytes > rc->avg_length[s->type-1]) {
988                  if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {                                  dbytes2=((double)dbytes + (rc->avg_length[s->type-1] - dbytes) * rc->param.curve_compression_high / 100.0);
   
                     if (dbytes >= rc->alt_curve_high) {  
                                                 dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev);  
989                      }else{                      }else{
990                                                  switch(rc->param.alt_curve_type) {                                  dbytes2 = ((double)dbytes + (rc->avg_length[s->type-1] - dbytes) * rc->param.curve_compression_low / 100.0);
                         case XVID_CURVE_SINE :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)));  
                                                         break;  
                         case XVID_CURVE_LINEAR :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff);  
                                                         break;  
                                                 case XVID_CURVE_COSINE :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))));  
                                                 }  
                                         }  
                 }else{  
                     if (dbytes <= rc->alt_curve_low) {  
                                                 dbytes2 = dbytes;  
                     }else{  
                                                 switch(rc->param.alt_curve_type) {  
                                                 case XVID_CURVE_SINE :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)));  
                                                         break;  
                                                 case XVID_CURVE_LINEAR :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff);  
                                                         break;  
                                                 case XVID_CURVE_COSINE :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))));  
                                                 }  
                                         }  
   
991                  }                  }
992    
993                            if (dbytes2 < rc->min_length[s->type-1])
994                                    dbytes2 = rc->min_length[s->type-1];
995    
             }else{  
                 if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {  
                     dbytes2=((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0);  
                 }else{  
                                 dbytes2 = ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);  
                 }  
             }  
   
             if (s->type == XVID_TYPE_BVOP) {  
                             dbytes2 *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];  
                             if (dbytes2 < rc->min_length[XVID_TYPE_BVOP-1])  
                                     dbytes2 = rc->min_length[XVID_TYPE_BVOP-1];  
             }else{  
                             if (dbytes2 < rc->min_length[XVID_TYPE_PVOP-1])  
                                     dbytes2 = rc->min_length[XVID_TYPE_PVOP-1];  
             }  
996              total2 += dbytes2;              total2 += dbytes2;
997          }          }
998      }      }
999    
1000      rc->curve_comp_scale = total1 / total2;      rc->curve_comp_scale = total1 / total2;
1001    
1002      if (!rc->param.use_alt_curve) {          DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: pframe%d bframe:%d\n",
1003          DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: %i\n",                          (int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale),
1004              (int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale));                          (int)(rc->avg_length[XVID_TYPE_BVOP-1] * rc->curve_comp_scale));
     }  
   
     if (rc->param.use_alt_curve) {  
         int bonus_bias = rc->param.alt_curve_bonus_bias;  
         int oldquant = 1;  
   
             if (rc->param.alt_curve_use_auto_bonus_bias)  
                     bonus_bias = rc->param.alt_curve_min_rel_qual;  
   
             rc->alt_curve_curve_bias_bonus = (total1 - total2) * (double)bonus_bias / 100.0 / (double)(rc->num_frames /* - credits_frames */ - rc->num_keyframes);  
             rc->curve_comp_scale = ((total1 - total2) * (1.0 - (double)bonus_bias / 100.0) + total2) / total2;  
   
   
         /* special info for alt curve:  bias bonus and quantizer thresholds */  
   
                 DPRINTF(XVID_DEBUG_RC, "avg scaled framesize:%i\n", (int)rc->avg_length[XVID_TYPE_PVOP-1]);  
                 DPRINTF(XVID_DEBUG_RC, "bias bonus:%i bytes\n", (int)rc->alt_curve_curve_bias_bonus);  
   
                 for (i=1; i <= (int)(rc->alt_curve_high*2)+1; i++) {  
             double curve_temp, dbytes;  
             int newquant;  
   
             dbytes = i;  
                         if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {  
                 if (dbytes >= rc->alt_curve_high) {  
                                         curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev);  
                 }else{  
                                         switch(rc->param.alt_curve_type)  
                                         {  
                                         case XVID_CURVE_SINE :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)));  
                                                 break;  
                                         case XVID_CURVE_LINEAR :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff);  
                                                 break;  
                                         case XVID_CURVE_COSINE :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))));  
                                         }  
                                 }  
                         }else{  
                 if (dbytes <= rc->alt_curve_low) {  
                                         curve_temp = dbytes;  
                 }else{  
                                         switch(rc->param.alt_curve_type)  
                                         {  
                                         case XVID_CURVE_SINE :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)));  
                                                 break;  
                                         case XVID_CURVE_LINEAR :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff);  
                                                 break;  
                                         case XVID_CURVE_COSINE :  
                                                 curve_temp = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))));  
                                         }  
                                 }  
                         }  
   
                         if (rc->movie_curve > 1.0)  
                                 dbytes *= rc->movie_curve;  
   
                         newquant = (int)(dbytes * 2.0 / (curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus));  
                         if (newquant > 1) {  
                                 if (newquant != oldquant) {  
                     int percent = (int)((i - rc->avg_length[XVID_TYPE_PVOP-1]) * 100.0 / rc->avg_length[XVID_TYPE_PVOP-1]);  
                                         oldquant = newquant;  
                                         DPRINTF(XVID_DEBUG_RC, "quant:%i threshold at %i : %i percent\n", newquant, i, percent);  
                                 }  
                         }  
                 }  
   
     }  
1005    
1006      rc->overflow = 0;      rc->overflow = 0;
1007      rc->KFoverflow = 0;      rc->KFoverflow = 0;

Legend:
Removed from v.1043  
changed lines
  Added in v.1161

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