[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.6, Tue May 20 17:28:25 2003 UTC revision 1.1.2.23, Wed Oct 1 23:23:01 2003 UTC
# Line 1  Line 1 
1  /******************************************************************************  /******************************************************************************
2   *   *
3   * XviD Bit Rate Controller Library   * XviD Bit Rate Controller Library
4   * - VBR 2 pass bitrate controler implementation -   *  - VBR 2 pass bitrate controller implementation -
5   *   *
6   * Copyright (C) 2002 Edouard Gomez <ed.gomez@wanadoo.fr>   *  Copyright (C)      2002 Foxer <email?>
7     *                     2002 Dirk Knop <dknop@gwdg.de>
8     *                2002-2003 Edouard Gomez <ed.gomez@free.fr>
9     *                     2003 Pete Ross <pross@xvid.org>
10   *   *
11   * The curve treatment algorithm is the one implemented by Foxer <email?> and   *  This curve treatment algorithm is the one originally implemented by Foxer
12   * Dirk Knop <dknop@gwdg.de> for the XviD vfw dynamic library.   *  and tuned by Dirk Knop for the XviD vfw frontend.
13   *   *
14   * This program is free software; you can redistribute it and/or modify   * This program is free software; you can redistribute it and/or modify
15   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
# Line 28  Line 31 
31    
32  #include <stdio.h>  #include <stdio.h>
33  #include <math.h>  #include <math.h>
34    #include <limits.h>
 #define RAD2DEG 57.295779513082320876798154814105  
 #define DEG2RAD 0.017453292519943295769236907684886  
35    
36  #include "../xvid.h"  #include "../xvid.h"
37  #include "../image/image.h"  #include "../image/image.h"
38    
39    /*****************************************************************************
40     * Some constants
41     ****************************************************************************/
42    
43    #define DEFAULT_KEYFRAME_BOOST 0
44    #define DEFAULT_PAYBACK_METHOD XVID_PAYBACK_PROP
45    #define DEFAULT_BITRATE_PAYBACK_DELAY 250
46    #define DEFAULT_CURVE_COMPRESSION_HIGH 0
47    #define DEFAULT_CURVE_COMPRESSION_LOW 0
48    #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60
49    #define DEFAULT_MAX_OVERFLOW_DEGRADATION 60
50    
51    /* Keyframe settings */
52    #define DEFAULT_KFTRESHOLD 10
53    #define DEFAULT_KFREDUCTION 20
54    #define DEFAULT_MIN_KEY_INTERVAL 1
55    
56    /*****************************************************************************
57     * Structures
58     ****************************************************************************/
59    
60    /* Statistics */
61  typedef struct {  typedef struct {
62      int type;               /* first pass type */      int type;               /* first pass type */
63      int quant;              /* first pass quant */      int quant;              /* first pass quant */
# Line 47  Line 70 
70      double weight;      double weight;
71  } stat_t;  } stat_t;
72    
73    /* Context struct */
   
   
 /* context struct */  
74  typedef struct  typedef struct
75  {  {
76      xvid_plugin_2pass2_t param;      xvid_plugin_2pass2_t param;
# Line 75  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 103  Line 113 
113  } rc_2pass2_t;  } rc_2pass2_t;
114    
115    
116    /*****************************************************************************
117     * Sub plugin functions prototypes
118     ****************************************************************************/
119    
120    static int rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t ** handle);
121    static int rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data);
122    static int rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data);
123    static int rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy);
124    
125    /*****************************************************************************
126     * Plugin definition
127     ****************************************************************************/
128    
129  #define BUF_SZ 1024  int
130  #define MAX_COLS    5  xvid_plugin_2pass2(void * handle, int opt, void * param1, void * param2)
   
   
 /* open stats file, and count num frames */  
   
 static int det_stats_length(rc_2pass2_t * rc, char * filename)  
131  {  {
132      FILE * f;          switch(opt) {
133      int n, ignore;          case XVID_PLG_INFO :
134      char type;                  return 0;
135    
136      rc->num_frames = 0;          case XVID_PLG_CREATE :
137      rc->num_keyframes = 0;                  return rc_2pass2_create((xvid_plg_create_t*)param1, param2);
138    
139      if ((f = fopen(filename, "rt")) == NULL)          case XVID_PLG_DESTROY :
140          return 0;                  return rc_2pass2_destroy((rc_2pass2_t*)handle, (xvid_plg_destroy_t*)param1);
141    
142      while((n = fscanf(f, "%c %d %d %d %d %d %d\n",          case XVID_PLG_BEFORE :
143          &type, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) {                  return rc_2pass2_before((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1);
144          if (type == 'i') {  
145              rc->num_frames++;          case XVID_PLG_AFTER :
146              rc->num_keyframes++;                  return rc_2pass2_after((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1);
         }else if (type == 'p' || type == 'b' || type == 's') {  
             rc->num_frames++;  
147          }          }
148    
149            return XVID_ERR_FAIL;
150      }      }
151    
152      fclose(f);  /*****************************************************************************
153     * Sub plugin functions definitions
154     ****************************************************************************/
155    
156    /* First a few local helping function prototypes */
157    static  int det_stats_length(rc_2pass2_t * rc, char * filename);
158    static  int load_stats(rc_2pass2_t *rc, char * filename);
159    static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create);
160    static void internal_scale(rc_2pass2_t *rc);
161    static void pre_process0(rc_2pass2_t * rc);
162    static void pre_process1(rc_2pass2_t * rc);
163    
164      return 1;  /*----------------------------------------------------------------------------
165  }   *--------------------------------------------------------------------------*/
166    
167    static int
168    rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t **handle)
169    {
170            xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param;
171            rc_2pass2_t * rc;
172            int i;
173    
174            rc = malloc(sizeof(rc_2pass2_t));
175            if (rc == NULL)
176                    return XVID_ERR_MEMORY;
177    
178  /* open stats file(s) and read into rc->stats array */          rc->param = *param;
179    
180  static int load_stats(rc_2pass2_t *rc, char * filename)          /*
181  {           * Initialize all defaults
182      FILE * f;           */
183      int i, not_scaled;  #define _INIT(a, b) if((a) <= 0) (a) = (b)
184            /* Let's set our defaults if needed */
185            _INIT(rc->param.keyframe_boost, DEFAULT_KEYFRAME_BOOST);
186            _INIT(rc->param.payback_method, DEFAULT_PAYBACK_METHOD);
187            _INIT(rc->param.bitrate_payback_delay, DEFAULT_BITRATE_PAYBACK_DELAY);
188            _INIT(rc->param.curve_compression_high, DEFAULT_CURVE_COMPRESSION_HIGH);
189            _INIT(rc->param.curve_compression_low, DEFAULT_CURVE_COMPRESSION_LOW);
190            _INIT(rc->param.max_overflow_improvement, DEFAULT_MAX_OVERFLOW_IMPROVEMENT);
191            _INIT(rc->param.max_overflow_degradation,  DEFAULT_MAX_OVERFLOW_DEGRADATION);
192    
193            /* Keyframe settings */
194            _INIT(rc->param.kftreshold, DEFAULT_KFTRESHOLD);
195            _INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION);
196            _INIT(rc->param.min_key_interval, DEFAULT_MIN_KEY_INTERVAL);
197    #undef _INIT
198    
199            /* Initialize some stuff to zero */
200            for(i=0; i<32; i++) rc->quant_count[i] = 0;
201    
202      if ((f = fopen(filename, "rt"))==NULL)          for(i=0; i<3; i++) {
203          return 0;                  int j;
204                    for (j=0; j<32; j++)
205                            rc->quant_error[i][j] = 0;
206            }
207    
208      i = 0;          for (i=0; i<3; i++)
209          not_scaled = 0;                  rc->last_quant[i] = 0;
     while(i < rc->num_frames) {  
         stat_t * s = &rc->stats[i];  
         int n;  
         char type;  
210    
211                  s->scaled_length = 0;          rc->fq_error = 0;
212          n = fscanf(f, "%c %d %d %d %d %d %d\n", &type, &s->quant, &s->blks[0], &s->blks[1], &s->blks[2], &s->length, &s->scaled_length);  
213          if (n == EOF) break;          /* Count frames in the stats file */
214                  if (n < 7) {          if (!det_stats_length(rc, param->filename)) {
215                          not_scaled = 1;                  DPRINTF(XVID_DEBUG_RC,"ERROR: fopen %s failed\n", param->filename);
216                    free(rc);
217                    return XVID_ERR_FAIL;
218                  }                  }
219    
220          if (type == 'i') {          /* Allocate the stats' memory */
221              s->type = XVID_TYPE_IVOP;          if ((rc->stats = malloc(rc->num_frames * sizeof(stat_t))) == NULL) {
222          }else if (type == 'p' || type == 's') {                  free(rc);
223              s->type = XVID_TYPE_PVOP;                  return XVID_ERR_MEMORY;
         }else if (type == 'b') {  
             s->type = XVID_TYPE_BVOP;  
         }else{  /* unknown type */  
             DPRINTF(XVID_DEBUG_RC, "unknown stats frame type; assuming pvop");  
             s->type = XVID_TYPE_PVOP;  
224          }          }
225    
226          i++;          /*
227             * Allocate keyframes location's memory
228             * PS: see comment in pre_process0 for the +1 location requirement
229             */
230            rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int));
231            if (rc->keyframe_locations == NULL) {
232                    free(rc->stats);
233                    free(rc);
234                    return XVID_ERR_MEMORY;
235      }      }
     rc->num_frames = i;  
236    
237      fclose(f);          if (!load_stats(rc, param->filename)) {
238                    DPRINTF(XVID_DEBUG_RC,"ERROR: fopen %s failed\n", param->filename);
239                    free(rc->keyframe_locations);
240                    free(rc->stats);
241                    free(rc);
242                    return XVID_ERR_FAIL;
243            }
244    
245      return 1;          /* Compute the target filesize */
246            if (rc->param.bitrate<0) {
247                    /* if negative, bitrate equals the target (in kbytes) */
248                    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 {
253                    /* Target filesize = bitrate/8 * numframes / framerate */
254                    rc->target =
255                            ((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * \
256                             (uint64_t)create->fincr) / \
257                            ((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);
264            DPRINTF(XVID_DEBUG_RC, "Target bitrate: %ld\n", rc->param.bitrate);
265            DPRINTF(XVID_DEBUG_RC, "Target filesize: %lld\n", rc->target);
266    
267            /* Compensate the average frame overhead caused by the container */
268            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);
270            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);
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) {
292                    /* Apply zone settings */
293                    zone_process(rc, create);
294                    /* Perform curve scaling */
295                    internal_scale(rc);
296            } else {
297                    /* External scaling -- zones are ignored */
298                    for (i=0;i<rc->num_frames;i++) {
299                            rc->stats[i].zone_mode = XVID_ZONE_WEIGHT;
300                            rc->stats[i].weight = 1.0;
301                    }
302                    rc->avg_weight = 1.0;
303                    rc->tot_quant = 0;
304  }  }
305    
306            pre_process1(rc);
307    
308            *handle = rc;
309            return(0);
310    }
311    
312    /*----------------------------------------------------------------------------
313     *--------------------------------------------------------------------------*/
314    
315  #if 0  static int
316  static void print_stats(rc_2pass2_t * rc)  rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy)
317  {  {
318      int i;          free(rc->keyframe_locations);
319      for (i = 0; i < rc->num_frames; i++) {          free(rc->stats);
320          stat_t * s = &rc->stats[i];          free(rc);
321          DPRINTF(XVID_DEBUG_RC, "%i %i %i %i\n", s->type, s->quant, s->length, s->scaled_length);          return(0);
     }  
322  }  }
 #endif  
323    
324  /* pre-process the statistics data  /*----------------------------------------------------------------------------
325      - for each type, count, tot_length, min_length, max_length   *--------------------------------------------------------------------------*/
     - set keyframes_locations  
 */  
326    
327  void pre_process0(rc_2pass2_t * rc)  static int
328    rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data)
329  {  {
330      int i,j;          stat_t * s = &rc->stats[data->frame_num];
331            int overflow;
332            int desired;
333            double dbytes;
334            double curve_temp;
335            double scaled_quant;
336            int capped_to_max_framesize = 0;
337    
338      for (i=0; i<3; i++) {          /*
339          rc->count[i]=0;           * This function is quite long but easy to understand. In order to simplify
340          rc->tot_length[i] = 0;           * the code path (a bit), we treat 3 cases that can return immediatly.
341          rc->last_quant[i] = 0;           */
     }  
342    
343      for (i=j=0; i<rc->num_frames; i++) {          /* First case: Another plugin has already set a quantizer */
344          stat_t * s = &rc->stats[i];          if (data->quant > 0)
345                    return(0);
346    
347          rc->count[s->type-1]++;          /* Second case: We are in a Quant zone */
348          rc->tot_length[s->type-1] += s->length;          if (s->zone_mode == XVID_ZONE_QUANT) {
349                    rc->fq_error += s->weight;
350                    data->quant = (int)rc->fq_error;
351                    rc->fq_error -= data->quant;
352    
353          if (i == 0 || s->length < rc->min_length[s->type-1]) {                  s->desired_length = s->length;
             rc->min_length[s->type-1] = s->length;  
         }  
354    
355          if (i == 0 || s->length > rc->max_length) {                  return(0);
             rc->max_length = s->length;  
356          }          }
357    
358          if (s->type == XVID_TYPE_IVOP) {          /* Third case: insufficent stats data */
359              rc->keyframe_locations[j] = i;          if (data->frame_num >= rc->num_frames)
360              j++;                  return 0;
         }  
     }  
     rc->keyframe_locations[j] = i;  
 }  
361    
362            /* XXX: why by 8 */
363            overflow = rc->overflow / 8;
364    
365  /* calculate zone weight "center" */          /*
366             * 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
368             */
369            if (s->type == XVID_TYPE_IVOP)
370                    overflow = 0;
371    
372  static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create)          desired = s->scaled_length;
 {  
     int i,j;  
     int n = 0;  
373    
374      rc->avg_weight = 0.0;          dbytes = desired;
375      rc->tot_quant = 0;          if (s->type == XVID_TYPE_IVOP)
376                    dbytes += desired * rc->param.keyframe_boost / 100;
377            dbytes /= rc->movie_curve;
378    
379            /*
380             * Apply user's choosen Payback method. Payback helps bitrate to follow the
381             * scaled curve "paying back" past errors in curve previsions.
382             */
383            if (rc->param.payback_method == XVID_PAYBACK_BIAS) {
384                    desired = (int)(rc->curve_comp_error / rc->param.bitrate_payback_delay);
385            } else {
386                    desired = (int)(rc->curve_comp_error * dbytes /
387                                                    rc->avg_length[s->type-1] / rc->param.bitrate_payback_delay);
388    
389      if (create->num_zones == 0) {                  if (labs(desired) > fabs(rc->curve_comp_error))
390          for (j = 0; j < rc->num_frames; j++) {                          desired = (int)rc->curve_comp_error;
             rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;  
             rc->stats[j].weight = 1.0;  
         }  
         rc->avg_weight += rc->num_frames * 1.0;  
         n += rc->num_frames;  
391      }      }
392    
393            rc->curve_comp_error -= desired;
394    
395      for(i=0; i < create->num_zones; i++) {          /* XXX: warning */
396            curve_temp = 0;
397    
398          int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames;          if ((rc->param.curve_compression_high + rc->param.curve_compression_low) &&     s->type != XVID_TYPE_IVOP) {
399    
400          if (i==0 && create->zones[i].frame > 0) {                  curve_temp = rc->curve_comp_scale;
401              for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) {                  if (dbytes > rc->avg_length[s->type-1]) {
402                  rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;                          curve_temp *= ((double)dbytes + (rc->avg_length[s->type-1] - dbytes) * rc->param.curve_compression_high / 100.0);
403                  rc->stats[j].weight = 1.0;                  } else {
404              }                          curve_temp *= ((double)dbytes + (rc->avg_length[s->type-1] - dbytes) * rc->param.curve_compression_low / 100.0);
             rc->avg_weight += create->zones[i].frame * 1.0;  
             n += create->zones[i].frame;  
405          }          }
406    
407          if (create->zones[i].mode == XVID_ZONE_WEIGHT) {                  desired += (int)curve_temp;
408              for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {                  rc->curve_comp_error += curve_temp - (int)curve_temp;
409                  rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;          } else {
410                  rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;                  desired += (int)dbytes;
411                    rc->curve_comp_error += dbytes - (int)dbytes;
412              }              }
413              next -= create->zones[i].frame;  
414              rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base;          /*
415              n += next;           * We can't do bigger frames than first pass, this would be stupid as first
416          }else{  // XVID_ZONE_QUANT           * pass is quant=2 and that reaching quant=1 is not worth it. We would lose
417              for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {           * many bytes and we would not not gain much quality.
418                  rc->stats[j].zone_mode = XVID_ZONE_QUANT;           */
419                  rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;          if (desired > s->length) {
420                  rc->tot_quant += rc->stats[j].length;                  rc->curve_comp_error += desired - s->length;
421                    desired = s->length;
422            } else {
423                    if (desired < rc->min_length[s->type-1]) {
424                            if (s->type == XVID_TYPE_IVOP){
425                                    rc->curve_comp_error -= rc->min_length[XVID_TYPE_IVOP-1] - desired;
426              }              }
427                            desired = rc->min_length[s->type-1];
428          }          }
429      }      }
     rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0;  
430    
431      DPRINTF(XVID_DEBUG_RC, "center_weight: %f (for %i frames);   fixed_bytes: %i\n", rc->avg_weight, n, rc->tot_quant);          s->desired_length = desired;
 }  
432    
433            /*
434             * 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 ?
436             */
437    
438  /* scale the curve */          if (s->type == XVID_TYPE_IVOP) {
439                    int KFdistance = rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1];
440    
441  static void internal_scale(rc_2pass2_t *rc)                  if (KFdistance < rc->param.kftreshold) {
 {  
         int64_t target  = rc->target - rc->tot_quant;  
         int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant;  
         int min_size[3];  
         double scaler;  
         int i;  
442    
443                            KFdistance -= rc->param.min_key_interval;
444    
445          /* perform an initial scale pass.                          if (KFdistance >= 0) {
446             if a frame size is scaled underneath our hardcoded minimums, then we force the                                  int KF_min_size;
            frame size to the minimum, and deduct the original & scaled frmae length from the  
            original and target total lengths */  
   
         min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8;  
         min_size[1] = (rc->stats[0].blks[0] + 88) / 8;  
         min_size[2] = 8;  
447    
448          scaler = (double)target / (double)pass1_length;                                  KF_min_size = desired * (100 - rc->param.kfreduction) / 100;
449                                    if (KF_min_size < 1)
450                                            KF_min_size = 1;
451    
452          if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {                                  desired = KF_min_size + (desired - KF_min_size) * KFdistance /
453                  DPRINTF(XVID_DEBUG_RC, "undersize warning\n");                                          (rc->param.kftreshold - rc->param.min_key_interval);
         scaler = 1.0;  
         }  
   
     DPRINTF(XVID_DEBUG_RC, "target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler);  
   
         for (i=0; i<rc->num_frames; i++) {  
                 stat_t * s = &rc->stats[i];  
                 int len;  
   
         if (s->zone_mode == XVID_ZONE_QUANT) {  
             s->scaled_length = s->length;  
         }else {  
                     len = (int)((double)s->length * scaler * s->weight / rc->avg_weight);  
                     if (len < min_size[s->type-1]) {            /* force frame size */  
                             s->scaled_length = min_size[s->type-1];  
                             target -= s->scaled_length;  
                             pass1_length -= s->length;  
                     }else{  
                             s->scaled_length = 0;  
                     }  
         }  
         }  
   
     scaler = (double)target / (double)pass1_length;  
     if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {  
                 DPRINTF(XVID_DEBUG_RC,"undersize warning\n");  
                 scaler = 1.0;  
         }  
   
         DPRINTF(XVID_DEBUG_RC, "target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler);  
   
         for (i=0; i<rc->num_frames; i++) {  
                 stat_t * s = &rc->stats[i];  
454    
455                  if (s->scaled_length==0) {      /* ignore frame with forced frame sizes */                                  if (desired < 1)
456                          s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight);                                          desired = 1;
                 }  
457          }          }
458  }  }
   
   
   
   
 void pre_process1(rc_2pass2_t * rc)  
 {  
     int i;  
     double total1, total2;  
     uint64_t ivop_boost_total;  
   
     ivop_boost_total = 0;  
     rc->curve_comp_error = 0;  
   
     for (i=0; i<3; i++) {  
         rc->tot_scaled_length[i] = 0;  
459      }      }
460    
461      for (i=0; i<rc->num_frames; i++) {          /*
462          stat_t * s = &rc->stats[i];           * 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          rc->tot_scaled_length[s->type-1] += s->scaled_length;           * used with desired which represents bframes _and_ pframes length.
465             *
466          if (s->type == XVID_TYPE_IVOP) {           * XXX: why are we using the avg pframe length for all frame types ?
467              ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100;           */
468          }          overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]);
     }  
469    
470      rc->movie_curve = ((double)(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1] + ivop_boost_total) /          /* Reign in overflow with huge frames */
471                                          (rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1]));          if (labs(overflow) > labs(rc->overflow))
472                    overflow = rc->overflow;
473    
474      for(i=0; i<3; i++) {          /* Make sure overflow doesn't run away */
475          if (rc->count[i] == 0 || rc->movie_curve == 0) {          if (overflow > desired * rc->param.max_overflow_improvement / 100) {
476              rc->avg_length[i] = 1;                  desired += (overflow <= desired) ? desired * rc->param.max_overflow_improvement / 100 :
477                            overflow * rc->param.max_overflow_improvement / 100;
478            } else if (overflow < desired * rc->param.max_overflow_degradation / -100){
479                    desired += desired * rc->param.max_overflow_degradation / -100;
480          }else{          }else{
481              rc->avg_length[i] = rc->tot_scaled_length[i] / rc->count[i] / rc->movie_curve;                  desired += overflow;
         }  
482      }      }
483    
484      /* alt curve stuff here */          /* Make sure we are not higher than desired frame size */
485            if (desired > rc->max_length) {
486      if (rc->param.use_alt_curve) {                  capped_to_max_framesize = 1;
487          const double avg_pvop = rc->avg_length[XVID_TYPE_PVOP-1];                  desired = rc->max_length;
488          const uint64_t tot_pvop = rc->tot_length[XVID_TYPE_PVOP-1];                  DPRINTF(XVID_DEBUG_RC,"[%i] Capped to maximum frame size\n",
489          const uint64_t tot_bvop = rc->tot_length[XVID_TYPE_BVOP-1];                                  data->frame_num);
         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;  
             }  
490          }          }
                 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;  
491    
492          if (rc->param.alt_curve_low_dist > 100) {          /* Make sure to not scale below the minimum framesize */
493                          switch(rc->param.alt_curve_type) {          if (desired < rc->min_length[s->type-1]) {
494              case XVID_CURVE_SINE: // Sine Curve (high aggressiveness)                  desired = rc->min_length[s->type-1];
495                                  rc->alt_curve_qual_dev *= 2.0 / (1.0 + sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)));                  DPRINTF(XVID_DEBUG_RC,"[%i] Capped to minimum frame size\n",
496                                  rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff));                                  data->frame_num);
                                 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)));  
                         }  
                 }  
497      }      }
     /* --- */  
   
   
     total1=total2=0;  
     for (i=0; i<rc->num_frames; i++) {  
         stat_t * s = &rc->stats[i];  
   
         if (s->type != XVID_TYPE_IVOP) {  
             double dbytes,dbytes2;  
   
             dbytes = s->scaled_length / rc->movie_curve;  
             dbytes2 = 0; /* XXX: warning */  
             total1 += dbytes;  
             if (s->type == XVID_TYPE_BVOP)  
                 dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];  
498    
499              if (rc->param.use_alt_curve) {          /*
500                  if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {           * Don't laugh at this very 'simple' quant<->filesize relationship, it
501             * proves to be acurate enough for our algorithm
502             */
503            scaled_quant = (double)s->quant*(double)s->length/(double)desired;
504    
505                      if (dbytes >= rc->alt_curve_high) {          /*
506                                                  dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev);           * Quantizer has been scaled using floating point operations/results, we
507                      }else{           * must cast it to integer
508                                                  switch(rc->param.alt_curve_type) {           */
509                          case XVID_CURVE_SINE :          data->quant = (int)scaled_quant;
                                                     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))));  
                                                 }  
                                         }  
510    
511                  }          /* Let's clip the computed quantizer, if needed */
512            if (data->quant < 1) {
513                    data->quant = 1;
514            } else if (data->quant > 31) {
515                    data->quant = 31;
516            } else if (s->type != XVID_TYPE_IVOP) {
517    
518                    /*
519                     * The frame quantizer has not been clipped, this appears to be a good
520                     * computed quantizer, do not loose quantizer decimal part that we
521                     * accumulate for later reuse when its sum represents a complete unit.
522                     */
523                    rc->quant_error[s->type-1][data->quant] += scaled_quant - (double)data->quant;
524    
525              }else{                  if (rc->quant_error[s->type-1][data->quant] >= 1.0) {
526                  if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {                          rc->quant_error[s->type-1][data->quant] -= 1.0;
527                      dbytes2=((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0);                          data->quant++;
528                  }else{                  } else if (rc->quant_error[s->type-1][data->quant] <= -1.0) {
529                                  dbytes2 = ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);                          rc->quant_error[s->type-1][data->quant] += 1.0;
530                  }                          data->quant--;
531              }              }
532    
             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];  
             }  
             total2 += dbytes2;  
         }  
533      }      }
534    
535      rc->curve_comp_scale = total1 / total2;          /*
536             * Now we have a computed quant that is in the right quante range, with a
537      if (!rc->param.use_alt_curve) {           * possible +1 correction due to cumulated error. We can now safely clip
538          DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: %i\n",           * the quantizer again with user's quant ranges. "Safely" means the Rate
539              (int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale));           * Control could learn more about this quantizer, this knowledge is useful
540             * for future frames even if it this quantizer won't be really used atm,
541             * that's why we don't perform this clipping earlier.
542             */
543            if (data->quant < data->min_quant[s->type-1]) {
544                    data->quant = data->min_quant[s->type-1];
545            } else if (data->quant > data->max_quant[s->type-1]) {
546                    data->quant = data->max_quant[s->type-1];
547      }      }
548    
549      if (rc->param.use_alt_curve) {          /*
550          int bonus_bias = rc->param.alt_curve_bonus_bias;           * To avoid big quality jumps from frame to frame, we apply a "security"
551          int oldquant = 1;           * rule that makes |last_quant - new_quant| <= 2. This rule only applies
552             * to predicted frames (P and B)
553              if (rc->param.alt_curve_use_auto_bonus_bias)           */
554                      bonus_bias = rc->param.alt_curve_min_rel_qual;          if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) {
   
             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", (int)rc->avg_length[XVID_TYPE_PVOP-1]);  
                 DPRINTF(XVID_DEBUG_RC, "bias bonus:%i bytes", (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;  
555    
556              dbytes = i;                  if (data->quant > rc->last_quant[s->type-1] + 2) {
557                          if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {                          data->quant = rc->last_quant[s->type-1] + 2;
558                  if (dbytes >= rc->alt_curve_high) {                          DPRINTF(XVID_DEBUG_RC,
559                                          curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev);                                          "[%i] p/b-frame quantizer prevented from rising too steeply\n",
560                  }else{                                          data->frame_num);
                                         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))));  
561                                          }                                          }
562                    if (data->quant < rc->last_quant[s->type-1] - 2) {
563                            data->quant = rc->last_quant[s->type-1] - 2;
564                            DPRINTF(XVID_DEBUG_RC,
565                                            "[%i] p/b-frame quantizer prevented from falling too steeply\n",
566                                            data->frame_num);
567                                  }                                  }
568                          }                          }
569    
570                          if (rc->movie_curve > 1.0)          /*
571                                  dbytes *= rc->movie_curve;           * We don't want to pollute the RC history results when our computed quant
572             * has been computed from a capped frame size
573                          newquant = (int)(dbytes * 2.0 / (curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus));           */
574                          if (newquant > 1) {          if (capped_to_max_framesize == 0)
575                                  if (newquant != oldquant) {                  rc->last_quant[s->type-1] = data->quant;
                     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", newquant, i, percent);  
                                 }  
                         }  
                 }  
576    
577      }          /* Force frame type */
578            data->type = s->type;
579    
580      rc->overflow = 0;          return 0;
     rc->KFoverflow = 0;  
     rc->KFoverflow_partial = 0;  
     rc->KF_idx = 1;  
581  }  }
582    
583    /*----------------------------------------------------------------------------
584     *--------------------------------------------------------------------------*/
585    
586    static int
587    rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data)
 static int rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t ** handle)  
588  {  {
589      xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param;          const char frame_type[4] = { 'i', 'p', 'b', 's'};
590      rc_2pass2_t * rc;          stat_t * s = &rc->stats[data->frame_num];
     int i;  
   
     rc = malloc(sizeof(rc_2pass2_t));  
     if (rc == NULL)  
         return XVID_ERR_MEMORY;  
591    
592      rc->param = *param;          /* Insufficent stats data */
593            if (data->frame_num >= rc->num_frames)
594                    return 0;
595    
596      if (rc->param.keyframe_boost <= 0) rc->param.keyframe_boost = 0;          rc->quant_count[data->quant]++;
     if (rc->param.payback_method <= 0) rc->param.payback_method = XVID_PAYBACK_PROP;  
     if (rc->param.bitrate_payback_delay <= 0) rc->param.bitrate_payback_delay = 250;  
     if (rc->param.curve_compression_high <= 0) rc->param.curve_compression_high = 0;  
     if (rc->param.curve_compression_low <= 0) rc->param.curve_compression_low = 0;  
     if (rc->param.max_overflow_improvement <= 0) rc->param.max_overflow_improvement = 60;  
     if (rc->param.max_overflow_degradation <= 0) rc->param.max_overflow_degradation = 60;  
   
     if (rc->param.use_alt_curve <= 0) rc->param.use_alt_curve = 0;  
     if (rc->param.alt_curve_high_dist <= 0) rc->param.alt_curve_high_dist = 500;  
     if (rc->param.alt_curve_low_dist <= 0) rc->param.alt_curve_low_dist = 90;  
     if (rc->param.alt_curve_use_auto <= 0) rc->param.alt_curve_use_auto = 1;  
     if (rc->param.alt_curve_auto_str <= 0) rc->param.alt_curve_auto_str = 30;  
     if (rc->param.alt_curve_type <= 0) rc->param.alt_curve_type = XVID_CURVE_LINEAR;  
     if (rc->param.alt_curve_min_rel_qual <= 0) rc->param.alt_curve_min_rel_qual = 50;  
     if (rc->param.alt_curve_use_auto_bonus_bias <= 0) rc->param.alt_curve_use_auto_bonus_bias = 1;  
     if (rc->param.alt_curve_bonus_bias <= 0) rc->param.alt_curve_bonus_bias = 50;  
   
     if (rc->param.kftreshold <= 0) rc->param.kftreshold = 10;  
     if (rc->param.kfreduction <= 0) rc->param.kfreduction = 20;  
     if (rc->param.min_key_interval <= 0) rc->param.min_key_interval = 300;  
597    
598      if (!det_stats_length(rc, param->filename)){          if (data->type == XVID_TYPE_IVOP) {
599          DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename);                  int kfdiff = (rc->keyframe_locations[rc->KF_idx] -      rc->keyframe_locations[rc->KF_idx - 1]);
         free(rc);  
         return XVID_ERR_FAIL;  
     }  
600    
601      if ((rc->stats = malloc(rc->num_frames * sizeof(stat_t))) == NULL) {                  rc->overflow += rc->KFoverflow;
602          free(rc);                  rc->KFoverflow = s->desired_length - data->length;
         return XVID_ERR_MEMORY;  
     }  
603    
604      /* XXX: do we need an addition location */                  if (kfdiff > 1) {  /* non-consecutive keyframes */
605      if ((rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int))) == NULL) {                          rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1);
606          free(rc->stats);                  }else{ /* consecutive keyframes */
607          free(rc);                          rc->overflow += rc->KFoverflow;
608          return XVID_ERR_MEMORY;                          rc->KFoverflow = 0;
609                            rc->KFoverflow_partial = 0;
610      }      }
611                    rc->KF_idx++;
612      if (!load_stats(rc, param->filename)) {          } else {
613          DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename);                  /* distribute part of the keyframe overflow */
614          free(rc->keyframe_locations);                  rc->overflow += s->desired_length - data->length + rc->KFoverflow_partial;
615          free(rc->stats);                  rc->KFoverflow -= rc->KFoverflow_partial;
         free(rc);  
         return XVID_ERR_FAIL;  
616      }      }
617    
618      /* pre-process our stats */          DPRINTF(XVID_DEBUG_RC, "[%i] type:%c quant:%i stats1:%i scaled:%i actual:%i desired:%d overflow:%i\n",
619                            data->frame_num,
620                            frame_type[data->type-1],
621                            data->quant,
622                            s->length,
623                            s->scaled_length,
624                            data->length,
625                            s->desired_length,
626                            rc->overflow);
627    
628          if (rc->num_frames  < create->fbase/create->fincr) {          return(0);
                 rc->target = rc->param.bitrate / 8;     /* one second */  
         }else{  
                 rc->target = (rc->param.bitrate * rc->num_frames * create->fincr) / (create->fbase * 8);  
629          }          }
630    
631      DPRINTF(XVID_DEBUG_RC, "rc->target : %i\n", rc->target);  /*****************************************************************************
632     * Helper functions definition
633     ****************************************************************************/
634    
635          rc->target -= rc->num_frames*24;        /* avi file header */  #define BUF_SZ   1024
636    #define MAX_COLS 5
637    
638    /* open stats file, and count num frames */
639    static int
640    det_stats_length(rc_2pass2_t * rc, char * filename)
641    {
642            FILE * f;
643            int n, ignore;
644            char type;
645    
646          pre_process0(rc);          rc->num_frames = 0;
647            rc->num_keyframes = 0;
648    
649          if (rc->param.bitrate) {          if ((f = fopen(filename, "rt")) == NULL)
650          zone_process(rc, create);                  return 0;
                 internal_scale(rc);  
     }else{  
         /* external scaler: ignore zone */  
         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;  
     }  
         pre_process1(rc);  
651    
652      for (i=0; i<32;i++) {          while((n = fscanf(f, "%c %d %d %d %d %d %d\n",
653          rc->pquant_error[i] = 0;                                            &type, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) {
654          rc->bquant_error[i] = 0;                  if (type == 'i') {
655          rc->quant_count[i] = 0;                          rc->num_frames++;
656                            rc->num_keyframes++;
657                    }else if (type == 'p' || type == 'b' || type == 's') {
658                            rc->num_frames++;
659      }      }
   
     rc->fq_error = 0;  
   
     *handle = rc;  
         return(0);  
660  }  }
661    
662            fclose(f);
663    
664  static int rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy)          return 1;
 {  
     free(rc->keyframe_locations);  
     free(rc->stats);  
         free(rc);  
         return(0);  
665  }  }
666    
667    /* open stats file(s) and read into rc->stats array */
668    
669    static int
670  static int rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data)  load_stats(rc_2pass2_t *rc, char * filename)
671  {  {
672      stat_t * s = &rc->stats[data->frame_num];          FILE * f;
673      int overflow;          int i, not_scaled;
     int desired;  
     double dbytes;  
     double curve_temp;  
     int capped_to_max_framesize = 0;  
   
     if (data->quant <= 0) {  
   
         if (s->zone_mode == XVID_ZONE_QUANT) {  
   
             rc->fq_error += s->weight;  
             data->quant = (int)rc->fq_error;  
             rc->fq_error -= data->quant;  
   
             s->desired_length = s->length;  
674    
         }else { /* XVID_ZONE_WEIGHT */  
675    
676              if (data->frame_num >= rc->num_frames) {          if ((f = fopen(filename, "rt"))==NULL)
                 /* insufficent stats data */  
677                  return 0;                  return 0;
             }  
   
             overflow = rc->overflow / 8;        /* XXX: why by 8 */  
   
             if (s->type == XVID_TYPE_IVOP) {        /* XXX: why */  
                 overflow = 0;  
             }  
678    
679              desired = s->scaled_length;          i = 0;
680            not_scaled = 0;
681            while(i < rc->num_frames) {
682                    stat_t * s = &rc->stats[i];
683                    int n;
684                    char type;
685    
686              dbytes = desired;                  s->scaled_length = 0;
687              if (s->type == XVID_TYPE_IVOP) {                  n = fscanf(f, "%c %d %d %d %d %d %d\n", &type, &s->quant, &s->blks[0], &s->blks[1], &s->blks[2], &s->length, &s->scaled_length);
688                  dbytes += desired * rc->param.keyframe_boost / 100;                  if (n == EOF) break;
689                    if (n < 7) {
690                            not_scaled = 1;
691              }              }
             dbytes /= rc->movie_curve;  
692    
693              if (s->type == XVID_TYPE_BVOP) {                  if (type == 'i') {
694                  dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];                          s->type = XVID_TYPE_IVOP;
695                    }else if (type == 'p' || type == 's') {
696                            s->type = XVID_TYPE_PVOP;
697                    }else if (type == 'b') {
698                            s->type = XVID_TYPE_BVOP;
699                    }else{  /* unknown type */
700                            DPRINTF(XVID_DEBUG_RC, "WARNING: unknown stats frame type, assuming pvop\n");
701                            s->type = XVID_TYPE_PVOP;
702              }              }
703    
704              if (rc->param.payback_method == XVID_PAYBACK_BIAS) {                  i++;
                 desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay);  
             }else{  
                         //printf("desired=%i, dbytes=%i\n", desired,dbytes);  
                         desired = (int)(rc->curve_comp_error * dbytes /  
                                 rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay);  
                         //printf("desired=%i\n", desired);  
   
                         if (labs(desired) > fabs(rc->curve_comp_error)) {  
                                 desired = (int)rc->curve_comp_error;  
                         }  
705              }              }
706    
707              rc->curve_comp_error -= desired;          rc->num_frames = i;
708    
709              /* alt curve */          fclose(f);
710    
711              curve_temp = 0; /* XXX: warning */          return 1;
   
             if (rc->param.use_alt_curve) {  
                 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))));  
712                              }                              }
713    
714    #if 0
715    static void print_stats(rc_2pass2_t * rc)
716    {
717            int i;
718            DPRINTF(XVID_DEBUG_RC, "type quant length scaled_length\n");
719            for (i = 0; i < rc->num_frames; i++) {
720                    stat_t * s = &rc->stats[i];
721                    DPRINTF(XVID_DEBUG_RC, "%d %d %d %d\n", s->type, s->quant, s->length, s->scaled_length);
722                                          }                                          }
723                                  }                                  }
724                                  if (s->type == XVID_TYPE_BVOP)  #endif
                                         curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];  
725    
726                                  curve_temp = curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus;  /* pre-process the statistics data
727       - for each type, count, tot_length, min_length, max_length
728       - set keyframes_locations
729    */
730    
731                                  desired += ((int)curve_temp);  static void
732                                  rc->curve_comp_error += curve_temp - (int)curve_temp;  pre_process0(rc_2pass2_t * rc)
733                          }else{  {
734                                  if (s->type == XVID_TYPE_BVOP)          int i,j;
                                         dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];  
735    
736                                  desired += ((int)dbytes);          /*
737                                  rc->curve_comp_error += dbytes - (int)dbytes;           * *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++) {
742                    rc->count[i]=0;
743                    rc->tot_length[i] = 0;
744                    rc->min_length[i] = INT_MAX;
745                          }                          }
746    
747              }else if ((rc->param.curve_compression_high + rc->param.curve_compression_low) &&   s->type != XVID_TYPE_IVOP) {          rc->max_length = INT_MIN;
748    
749                  curve_temp = rc->curve_comp_scale;          /*
750                  if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {           * Loop through all frames and find/compute all the stuff this function
751                      curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0);           * is supposed to do
752                  } else {           */
753                      curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);          for (i=j=0; i<rc->num_frames; i++) {
754                  }                  stat_t * s = &rc->stats[i];
755    
756                  if (s->type == XVID_TYPE_BVOP){                  rc->count[s->type-1]++;
757                      curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];                  rc->tot_length[s->type-1] += s->length;
                 }  
758    
759                  desired += (int)curve_temp;                  if (s->length < rc->min_length[s->type-1]) {
760                  rc->curve_comp_error += curve_temp - (int)curve_temp;                          rc->min_length[s->type-1] = s->length;
             }else{  
                 if (s->type == XVID_TYPE_BVOP){  
                                 dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];  
761                  }                  }
762    
763                          desired += (int)dbytes;                  if (s->length > rc->max_length) {
764                          rc->curve_comp_error += dbytes - (int)dbytes;                          rc->max_length = s->length;
765              }              }
766    
   
                 if (desired > s->length) {  /* if desired length exceeds the pass1 length.. */  
                         rc->curve_comp_error += desired - s->length;  
                         desired = s->length;  
                 }else{  
                 if (desired < rc->min_length[s->type-1]) {  
767                      if (s->type == XVID_TYPE_IVOP){                      if (s->type == XVID_TYPE_IVOP){
768                          rc->curve_comp_error -= rc->min_length[XVID_TYPE_IVOP-1] - desired;                          rc->keyframe_locations[j] = i;
769                      }                          j++;
                     desired = rc->min_length[s->type-1];  
770                  }                  }
771                  }                  }
772    
773              s->desired_length = desired;          /*
774             * Nota Bene:
775             * The "per sequence" overflow system considers a natural sequence to be
776              /* if this keyframe is too close to the next, reduce it's byte allotment           * formed by all frames between two iframes, so if we want to make sure
777              XXX: why do we do this after setting the desired length  */           * the system does not go nuts during last sequence, we force the last
778             * frame to appear in the keyframe locations array.
779                  if (s->type == XVID_TYPE_IVOP) {           */
780                          int KFdistance = rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1];          rc->keyframe_locations[j] = i;
781    
782                  if (KFdistance < rc->param.kftreshold) {          DPRINTF(XVID_DEBUG_RC, "Min 1st pass IFrame length: %d\n", rc->min_length[0]);
783            DPRINTF(XVID_DEBUG_RC, "Min 1st pass PFrame length: %d\n", rc->min_length[1]);
784            DPRINTF(XVID_DEBUG_RC, "Min 1st pass BFrame length: %d\n", rc->min_length[2]);
785    }
786    
787                      KFdistance = KFdistance - rc->param.min_key_interval;  /* calculate zone weight "center" */
788    
789                                  if (KFdistance >= 0) {  static void
790                          int KF_min_size;  zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create)
791    {
792            int i,j;
793            int n = 0;
794    
795                                          KF_min_size = desired * (100 - rc->param.kfreduction) / 100;          rc->avg_weight = 0.0;
796                                          if (KF_min_size < 1)          rc->tot_quant = 0;
                                                 KF_min_size = 1;  
797    
                                         desired = KF_min_size + (desired - KF_min_size) * KFdistance /  
                                                 (rc->param.kftreshold - rc->param.min_key_interval);  
798    
799                                          if (desired < 1)          if (create->num_zones == 0) {
800                                                  desired = 1;                  for (j = 0; j < rc->num_frames; j++) {
801                                  }                          rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
802                            rc->stats[j].weight = 1.0;
803                          }                          }
804                    rc->avg_weight += rc->num_frames * 1.0;
805                    n += rc->num_frames;
806                  }                  }
807    
             overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]);  
808    
809                  // Foxer: reign in overflow with huge frames          for(i=0; i < create->num_zones; i++) {
                 if (labs(overflow) > labs(rc->overflow)) {  
                         overflow = rc->overflow;  
                 }  
810    
811              // Foxer: make sure overflow doesn't run away                  int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames;
812    
813                  if (overflow > desired * rc->param.max_overflow_improvement / 100) {                  if (i==0 && create->zones[i].frame > 0) {
814                          desired += (overflow <= desired) ? desired * rc->param.max_overflow_improvement / 100 :                          for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) {
815                                  overflow * rc->param.max_overflow_improvement / 100;                                  rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
816                  }else if (overflow < desired * rc->param.max_overflow_degradation / -100){                                  rc->stats[j].weight = 1.0;
817                          desired += desired * rc->param.max_overflow_degradation / -100;                          }
818                  }else{                          rc->avg_weight += create->zones[i].frame * 1.0;
819                          desired += overflow;                          n += create->zones[i].frame;
820                  }                  }
821    
822              if (desired > rc->max_length) {                  if (create->zones[i].mode == XVID_ZONE_WEIGHT) {
823                          capped_to_max_framesize = 1;                          for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
824                          desired = rc->max_length;                                  rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
825                                    rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
826                            }
827                            next -= create->zones[i].frame;
828                            rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base;
829                            n += next;
830                    }else{  /* XVID_ZONE_QUANT */
831                            for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
832                                    rc->stats[j].zone_mode = XVID_ZONE_QUANT;
833                                    rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
834                                    rc->tot_quant += rc->stats[j].length;
835                            }
836                  }                  }
837            }
838            rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0;
839    
840              // make sure to not scale below the minimum framesize          DPRINTF(XVID_DEBUG_RC, "center_weight: %f (for %i frames);   fixed_bytes: %i\n", rc->avg_weight, n, rc->tot_quant);
             if (desired < rc->min_length[s->type-1]) {  
                 desired = rc->min_length[s->type-1];  
841              }              }
842    
843    
844              // very 'simple' quant<->filesize relationship  /* scale the curve */
             data->quant= (s->quant * s->length) / desired;  
845    
846                  if (data->quant < 1) {  static void
847                          data->quant = 1;  internal_scale(rc_2pass2_t *rc)
             } else if (data->quant > 31) {  
                         data->quant = 31;  
                 }  
                 else if (s->type != XVID_TYPE_IVOP)  
848                  {                  {
849                          // Foxer: aid desired quantizer precision by accumulating decision error          int64_t target  = rc->target - rc->tot_quant;
850                          if (s->type== XVID_TYPE_BVOP) {          int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant;
851                                  rc->bquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant;          double scaler;
852            int i, num_MBs;
853    
854                                  if (rc->bquant_error[data->quant] >= 1.0) {          /* Let's compute a linear scaler in order to perform curve scaling */
855                                          rc->bquant_error[data->quant] -= 1.0;          scaler = (double)target / (double)pass1_length;
                                         data->quant++;  
                                 }  
                         }else{  
                                 rc->pquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant;  
856    
857                      if (rc->pquant_error[data->quant] >= 1.0) {          if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {
858                                          rc->pquant_error[data->quant] -= 1.0;                  DPRINTF(XVID_DEBUG_RC, "WARNING: Undersize detected\n");
859                                          ++data->quant;                  scaler = 1.0;
                                 }  
                         }  
860                  }                  }
861    
862              /* cap to min/max quant */          DPRINTF(XVID_DEBUG_RC,
863                            "Before correction: target=%i, tot_length=%i, scaler=%f\n",
864                            (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 (data->quant < data->min_quant[s->type-1]) {          if(rc->min_length[0] > ((num_MBs*22) + 240) / 8)
877                  data->quant = data->min_quant[s->type-1];                  rc->min_length[0] = ((num_MBs*22) + 240) / 8;
             }else if (data->quant > data->max_quant[s->type-1]) {  
                 data->quant = data->max_quant[s->type-1];  
             }  
878    
879              /* subsequent p/b frame quants can only be +- 2 */          if(rc->min_length[1] > ((num_MBs) + 88)  / 8)
880                  if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) {                  rc->min_length[1] = ((num_MBs) + 88)  / 8;
881    
882                          if (data->quant > rc->last_quant[s->type-1] + 2) {          if(rc->min_length[2] > 8)
883                                  data->quant = rc->last_quant[s->type-1] + 2;                  rc->min_length[2] = 8;
884                                  DPRINTF(XVID_DEBUG_RC, "p/b-frame quantizer prevented from rising too steeply");  
885            /*
886             * Perform an initial scale pass.
887             * 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
889             * frame length from the original and target total lengths
890             */
891            for (i=0; i<rc->num_frames; i++) {
892                    stat_t * s = &rc->stats[i];
893                    int len;
894    
895                    if (s->zone_mode == XVID_ZONE_QUANT) {
896                            s->scaled_length = s->length;
897                            continue;
898                          }                          }
899                          if (data->quant < rc->last_quant[s->type-1] - 2) {  
900                                  data->quant = rc->last_quant[s->type-1] - 2;                  /* Compute the scaled length */
901                                  DPRINTF(XVID_DEBUG_RC, "p/b-frame quantizer prevented from falling too steeply");                  len = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
902    
903                    /* Compare with the computed minimum */
904                    if (len < rc->min_length[s->type-1]) {
905                            /* force frame size to our computed minimum */
906                            s->scaled_length = rc->min_length[s->type-1];
907                            target -= s->scaled_length;
908                            pass1_length -= s->length;
909                    } else {
910                            /* Do nothing for now, we'll scale this later */
911                            s->scaled_length = 0;
912                          }                          }
913                  }                  }
914    
915                  if (capped_to_max_framesize == 0) {          /* Correct the scaler for all non forced frames */
916                  rc->last_quant[s->type-1] = data->quant;          scaler = (double)target / (double)pass1_length;
917    
918            /* Detect undersizing */
919            if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {
920                    DPRINTF(XVID_DEBUG_RC, "WARNING: Undersize detected\n");
921                    scaler = 1.0;
922                  }                  }
923    
924            DPRINTF(XVID_DEBUG_RC,
925                            "After correction: target=%i, tot_length=%i, scaler=%f\n",
926                            (int)target, (int)pass1_length, scaler);
927    
928          }   /* if */          /* Do another pass with the new scaler */
929            for (i=0; i<rc->num_frames; i++) {
930                    stat_t * s = &rc->stats[i];
931    
932                    /* Ignore frame with forced frame sizes */
933                    if (s->scaled_length == 0)
934                            s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
935      }      }
   
     return 0;  
936  }  }
937    
938    static void
939    pre_process1(rc_2pass2_t * rc)
 static int rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data)  
940  {  {
941      stat_t * s = &rc->stats[data->frame_num];          int i;
942            double total1, total2;
943            uint64_t ivop_boost_total;
944    
945      if (data->frame_num >= rc->num_frames) {          ivop_boost_total = 0;
946          /* insufficent stats data */          rc->curve_comp_error = 0;
         return 0;  
     }  
947    
948      rc->quant_count[data->quant]++;          for (i=0; i<3; i++) {
949                    rc->tot_scaled_length[i] = 0;
950            }
951    
952      if (data->type == XVID_TYPE_IVOP) {          for (i=0; i<rc->num_frames; i++) {
953          int kfdiff = (rc->keyframe_locations[rc->KF_idx] -      rc->keyframe_locations[rc->KF_idx - 1]);                  stat_t * s = &rc->stats[i];
954    
955          rc->overflow += rc->KFoverflow;                  rc->tot_scaled_length[s->type-1] += s->scaled_length;
         rc->KFoverflow = s->desired_length - data->length;  
956    
957          if (kfdiff > 1) {  // non-consecutive keyframes                  if (s->type == XVID_TYPE_IVOP) {
958              rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1);                          ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100;
         }else{ // consecutive keyframes  
                         rc->overflow += rc->KFoverflow;  
                         rc->KFoverflow = 0;  
                         rc->KFoverflow_partial = 0;  
959          }          }
         rc->KF_idx++;  
     }else{  
         // distribute part of the keyframe overflow  
         rc->overflow += s->desired_length - data->length + rc->KFoverflow_partial;  
         rc->KFoverflow -= rc->KFoverflow_partial;  
960      }      }
961    
962      DPRINTF(XVID_DEBUG_RC, "[%i] quant:%i stats1:%i scaled:%i actual:%i overflow:%i\n",          rc->movie_curve = ((double)(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1] + ivop_boost_total) /
963          data->frame_num,                                             (rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1]));
         data->quant,  
         s->length,  
         s->scaled_length,  
         data->length,  
         rc->overflow);  
964    
965      return(0);          for(i=0; i<3; i++) {
966                    if (rc->count[i] == 0 || rc->movie_curve == 0) {
967                            rc->avg_length[i] = 1;
968                    }else{
969                            rc->avg_length[i] = rc->tot_scaled_length[i] / rc->count[i] / rc->movie_curve;
970                    }
971  }  }
972    
973    /* --- */
974    
975            total1=total2=0;
976    
977            for (i=0; i<rc->num_frames; i++) {
978                    stat_t * s = &rc->stats[i];
979    
980  int xvid_plugin_2pass2(void * handle, int opt, void * param1, void * param2)                  if (s->type != XVID_TYPE_IVOP) {
981  {                          double dbytes,dbytes2;
     switch(opt)  
     {  
     case XVID_PLG_INFO :  
         return 0;  
982    
983      case XVID_PLG_CREATE :                          dbytes = s->scaled_length / rc->movie_curve;
984          return rc_2pass2_create((xvid_plg_create_t*)param1, param2);                          dbytes2 = 0; /* XXX: warning */
985                            total1 += dbytes;
986    
987      case XVID_PLG_DESTROY :                          if (dbytes > rc->avg_length[s->type-1]) {
988          return rc_2pass2_destroy((rc_2pass2_t*)handle, (xvid_plg_destroy_t*)param1);                                  dbytes2=((double)dbytes + (rc->avg_length[s->type-1] - dbytes) * rc->param.curve_compression_high / 100.0);
989                            } else {
990                                    dbytes2 = ((double)dbytes + (rc->avg_length[s->type-1] - dbytes) * rc->param.curve_compression_low / 100.0);
991                            }
992    
993      case XVID_PLG_BEFORE :                          if (dbytes2 < rc->min_length[s->type-1])
994          return rc_2pass2_before((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1);                                  dbytes2 = rc->min_length[s->type-1];
995    
996      case XVID_PLG_AFTER :                          total2 += dbytes2;
997          return rc_2pass2_after((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1);                  }
998      }      }
999    
1000      return XVID_ERR_FAIL;          rc->curve_comp_scale = total1 / total2;
1001    
1002            DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: pframe%d bframe:%d\n",
1003                            (int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale),
1004                            (int)(rc->avg_length[XVID_TYPE_BVOP-1] * rc->curve_comp_scale));
1005    
1006            rc->overflow = 0;
1007            rc->KFoverflow = 0;
1008            rc->KFoverflow_partial = 0;
1009            rc->KF_idx = 1;
1010  }  }

Legend:
Removed from v.1.1.2.6  
changed lines
  Added in v.1.1.2.23

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