[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 1202, Sun Nov 9 20:49:21 2003 UTC revision 1296, Sun Dec 21 17:38:17 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.24 2003-11-09 20:49:21 edgomez Exp $   * $Id: plugin_2pass2.c,v 1.1.2.33 2003-12-21 17:38:17 edgomez Exp $
29   *   *
30   *****************************************************************************/   *****************************************************************************/
31    
32    #define BQUANT_PRESCALE
33  #undef COMPENSATE_FORMULA  #undef COMPENSATE_FORMULA
34    
35    /* forces second pass not to be bigger than first */
36    #undef PASS_SMALLER
37    
38  #include <stdio.h>  #include <stdio.h>
39  #include <math.h>  #include <math.h>
40  #include <limits.h>  #include <limits.h>
# Line 39  Line 43 
43  #include "../image/image.h"  #include "../image/image.h"
44    
45  /*****************************************************************************  /*****************************************************************************
46   * Some constants   * Some default settings
47   ****************************************************************************/   ****************************************************************************/
48    
49  #define DEFAULT_KEYFRAME_BOOST 0  #define DEFAULT_KEYFRAME_BOOST 0
50  #define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10  #define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10
51  #define DEFAULT_CURVE_COMPRESSION_HIGH 0  #define DEFAULT_CURVE_COMPRESSION_HIGH 0
52  #define DEFAULT_CURVE_COMPRESSION_LOW 0  #define DEFAULT_CURVE_COMPRESSION_LOW 0
53  #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60  #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 10
54  #define DEFAULT_MAX_OVERFLOW_DEGRADATION 60  #define DEFAULT_MAX_OVERFLOW_DEGRADATION 10
55    
56  /* Keyframe settings */  /* Keyframe settings */
57  #define DEFAULT_KFREDUCTION 20  #define DEFAULT_KFREDUCTION 20
58  #define DEFAULT_MIN_KEY_INTERVAL 1  #define DEFAULT_KFTHRESHOLD 1
59    
60    /*****************************************************************************
61     * Some default constants (can be tuned)
62     ****************************************************************************/
63    
64    /* Specify the invariant part of the headers bits (header+MV)
65     * as  hlength/cst */
66    #define INVARIANT_HEADER_PART_IVOP 1 /* factor 1.0f   */
67    #define INVARIANT_HEADER_PART_PVOP 2 /* factor 0.5f   */
68    #define INVARIANT_HEADER_PART_BVOP 8 /* factor 0.125f */
69    
70  /*****************************************************************************  /*****************************************************************************
71   * Structures   * Structures
# Line 61  Line 75 
75  typedef struct {  typedef struct {
76          int type;               /* first pass type */          int type;               /* first pass type */
77          int quant;              /* first pass quant */          int quant;              /* first pass quant */
         int quant2;             /* Second pass quant */  
78          int blks[3];                    /* k,m,y blks */          int blks[3];                    /* k,m,y blks */
79          int length;             /* first pass length */          int length;             /* first pass length */
80            int invariant;          /* what we assume as being invariant between the two passes, it's a sub part of header + MV bits */
81          int scaled_length;      /* scaled length */          int scaled_length;      /* scaled length */
82          int desired_length;     /* desired length; calculated during encoding */          int desired_length;     /* desired length; calculated during encoding */
83          int error;          int error;
# Line 95  Line 109 
109    
110          /* Total length of each frame types (1st pass) */          /* Total length of each frame types (1st pass) */
111          uint64_t tot_length[3];          uint64_t tot_length[3];
112            uint64_t tot_invariant[3];
113    
114          /* Average length of each frame types (used first for 1st pass data and          /* Average length of each frame types (used first for 1st pass data and
115           * then for scaled averages */           * then for scaled averages */
# Line 124  Line 139 
139          double avg_weight;          double avg_weight;
140    
141          /* Total length used by XVID_ZONE_QUANT zones */          /* Total length used by XVID_ZONE_QUANT zones */
142          int64_t tot_quant;          uint64_t tot_quant;
143            uint64_t tot_quant_invariant;
144    
145          /*----------------------------------          /*----------------------------------
146           * Advanced settings helper ratios           * Advanced settings helper ratios
# Line 220  Line 236 
236  {  {
237          switch(opt) {          switch(opt) {
238          case XVID_PLG_INFO :          case XVID_PLG_INFO :
239            case XVID_PLG_FRAME :
240                  return 0;                  return 0;
241    
242          case XVID_PLG_CREATE :          case XVID_PLG_CREATE :
# Line 281  Line 298 
298    
299          /* Keyframe settings */          /* Keyframe settings */
300          _INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION);          _INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION);
301          _INIT(rc->param.min_key_interval, DEFAULT_MIN_KEY_INTERVAL);          _INIT(rc->param.kfthreshold, DEFAULT_KFTHRESHOLD);
302  #undef _INIT  #undef _INIT
303    
304          /* Initialize some stuff to zero */          /* Initialize some stuff to zero */
# Line 332  Line 349 
349          /* Compute the target filesize */          /* Compute the target filesize */
350          if (rc->param.bitrate<0) {          if (rc->param.bitrate<0) {
351                  /* if negative, bitrate equals the target (in kbytes) */                  /* if negative, bitrate equals the target (in kbytes) */
352                  rc->target = (-rc->param.bitrate) * 1024;                  rc->target = ((uint64_t)(-rc->param.bitrate)) * 1024;
353          } else if (rc->num_frames  < create->fbase/create->fincr) {          } else if (rc->num_frames  < create->fbase/create->fincr) {
354                  /* Source sequence is less than 1s long, we do as if it was 1s long */                  /* Source sequence is less than 1s long, we do as if it was 1s long */
355                  rc->target = rc->param.bitrate / 8;                  rc->target = rc->param.bitrate / 8;
# Line 366  Line 383 
383           *  - count how many times each frame type has been used.           *  - count how many times each frame type has been used.
384           *     rc->count[]           *     rc->count[]
385           *  - total bytes used per frame type           *  - total bytes used per frame type
386           *     rc->total[]           *     rc->tot_length[]
387             *  - total bytes considered invariant between the 2 passes
388           *  - store keyframe location           *  - store keyframe location
389           *     rc->keyframe_locations[]           *     rc->keyframe_locations[]
390           */           */
# Line 375  Line 393 
393          /* When bitrate is not given it means it has been scaled by an external          /* When bitrate is not given it means it has been scaled by an external
394           * application */           * application */
395          if (rc->param.bitrate) {          if (rc->param.bitrate) {
396                  /* Apply zone settings */                  /* Apply zone settings
397                     * - set rc->tot_quant which represents the total num of bytes spent in
398                     *   fixed quant zones
399                     * - set rc->tot_quant_invariant which represents the total num of bytes spent
400                     *   in fixed quant zones for headers */
401                  zone_process(rc, create);                  zone_process(rc, create);
402                  /* Perform internal curve scaling */                  /* Perform internal curve scaling */
403                  first_pass_scale_curve_internal(rc);                  first_pass_scale_curve_internal(rc);
# Line 435  Line 457 
457          if (data->quant > 0)          if (data->quant > 0)
458                  return(0);                  return(0);
459    
460          /* Second case: We are in a Quant zone */          /* Second case: insufficent stats data
461             * We can't guess much what we should do, let core decide all alone */
462            if (data->frame_num >= rc->num_frames) {
463                    DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)",
464                            data->frame_num);
465                    return(0);
466            }
467    
468            /* Third case: We are in a Quant zone
469             * Quant zones must just ensure we use the same settings as first pass
470             * So set the quantizer and the type */
471          if (s->zone_mode == XVID_ZONE_QUANT) {          if (s->zone_mode == XVID_ZONE_QUANT) {
472                    /* Quant stuff */
473                  rc->fq_error += s->weight;                  rc->fq_error += s->weight;
474                  data->quant = (int)rc->fq_error;                  data->quant = (int)rc->fq_error;
475                  rc->fq_error -= data->quant;                  rc->fq_error -= data->quant;
476    
477                    /* The type stuff */
478                    data->type = s->type;
479    
480                    /* The only required data for AFTER step is this one for the overflow
481                     * control */
482                  s->desired_length = s->length;                  s->desired_length = s->length;
483    
484                  return(0);                  return(0);
485          }          }
486    
         /* Third case: insufficent stats data */  
         if (data->frame_num >= rc->num_frames)  
                 return(0);  
487    
488          /*************************************************************************/          /*************************************************************************/
489          /*************************************************************************/          /*************************************************************************/
# Line 466  Line 501 
501    
502          /* IFrame user settings*/          /* IFrame user settings*/
503          if (s->type == XVID_TYPE_IVOP) {          if (s->type == XVID_TYPE_IVOP) {
   
504                  /* Keyframe boosting -- All keyframes benefit from it */                  /* Keyframe boosting -- All keyframes benefit from it */
505                  dbytes += dbytes*rc->param.keyframe_boost / 100;                  dbytes += dbytes*rc->param.keyframe_boost / 100;
506    
507                  /* Applies keyframe penalties, but not the first frame */  #if 0 /* ToDo: decide how to apply kfthresholding */
508                  if (rc->KF_idx) {  #endif
                         int penalty_distance;  
   
                         /* Minimum keyframe distance penalties */  
                         penalty_distance  = rc->param.min_key_interval;  
                         penalty_distance -= rc->keyframe_locations[rc->KF_idx];  
                         penalty_distance += rc->keyframe_locations[rc->KF_idx-1];  
   
                         /* Ah ah ! guilty keyframe, you're under arrest ! */  
                         if (penalty_distance > 0)  
                                 dbytes -= dbytes*penalty_distance*rc->param.kfreduction/100;  
                 }  
509          } else {          } else {
510    
511                  /* P/S/B frames must reserve some bits for iframe boosting */                  /* P/S/B frames must reserve some bits for iframe boosting */
# Line 524  Line 547 
547           *-----------------------------------------------------------------------*/           *-----------------------------------------------------------------------*/
548    
549          /* Compute the overflow we should compensate */          /* Compute the overflow we should compensate */
550          if (s->type != XVID_TYPE_IVOP) {          if (s->type != XVID_TYPE_IVOP || rc->overflow > 0) {
551                  double frametype_factor;                  double frametype_factor;
552                  double framesize_factor;                  double framesize_factor;
553    
# Line 555  Line 578 
578                  /* Apply the overflow strength imposed by the user */                  /* Apply the overflow strength imposed by the user */
579                  overflow *= (rc->param.overflow_control_strength/100.0f);                  overflow *= (rc->param.overflow_control_strength/100.0f);
580          } else {          } else {
581                  /* no overflow applied in IFrames because:                  /* no negative overflow applied in IFrames because:
582                   *  - their role is important as they're references for P/BFrames.                   *  - their role is important as they're references for P/BFrames.
583                   *  - there aren't much in typical sequences, so if an IFrame overflows too                   *  - there aren't much in typical sequences, so if an IFrame overflows too
584                   *    much, this overflow may impact the next IFrame too much and generate                   *    much, this overflow may impact the next IFrame too much and generate
# Line 587  Line 610 
610           * pass nor smaller than the allowed minimum.           * pass nor smaller than the allowed minimum.
611           *-----------------------------------------------------------------------*/           *-----------------------------------------------------------------------*/
612    
613    #ifdef PASS_SMALLER
614          if (dbytes > s->length) {          if (dbytes > s->length) {
615                  dbytes = s->length;                  dbytes = s->length;
616          } else if (dbytes < rc->min_length[s->type-1]) {          } else
617    #endif
618                    if (dbytes < rc->min_length[s->type-1]) {
619                  dbytes = rc->min_length[s->type-1];                  dbytes = rc->min_length[s->type-1];
620          } else if (dbytes > rc->max_length) {          } else if (dbytes > rc->max_length) {
621                  /* ToDo: this condition is always wrong as max_length == maximum frame                  /* ToDo: this condition is always wrong as max_length == maximum frame
# Line 605  Line 631 
631           * Desired frame length <-> quantizer mapping           * Desired frame length <-> quantizer mapping
632           *-----------------------------------------------------------------------*/           *-----------------------------------------------------------------------*/
633    
634          /* For bframes we must retrieve the original quant used (sent to xvidcore)  #ifdef BQUANT_PRESCALE
635           * as core applies the bquant formula before writing the stat log entry */          /* For bframes we prescale the quantizer to avoid too high quant scaling */
636          if(s->type == XVID_TYPE_BVOP) {          if(s->type == XVID_TYPE_BVOP) {
637    
638                  twopass_stat_t *b_ref = s;                  twopass_stat_t *b_ref = s;
# Line 616  Line 642 
642                          b_ref--;                          b_ref--;
643    
644                  /* Compute the original quant */                  /* Compute the original quant */
645                  s->quant  = 100*s->quant - data->bquant_offset;                  s->quant  = 2*(100*s->quant - data->bquant_offset);
646                  s->quant += data->bquant_ratio - 1; /* to avoid rouding issues */                  s->quant += data->bquant_ratio - 1; /* to avoid rounding issues */
647                  s->quant  = s->quant/data->bquant_ratio - b_ref->quant;                  s->quant  = s->quant/data->bquant_ratio - b_ref->quant;
648          }          }
649    #endif
650    
651          /* Don't laugh at this very 'simple' quant<->filesize relationship, it          /* Don't laugh at this very 'simple' quant<->size relationship, it
652           * proves to be acurate enough for our algorithm */           * proves to be acurate enough for our algorithm */
653          scaled_quant = (double)s->quant*(double)s->length/(double)dbytes;          scaled_quant = (double)s->quant*(double)s->length/(double)dbytes;
654    
# Line 709  Line 736 
736          /* Don't forget to force 1st pass frame type ;-) */          /* Don't forget to force 1st pass frame type ;-) */
737          data->type = s->type;          data->type = s->type;
738    
         /* Store the quantizer into the statistics -- Used to compensate the double  
          * formula symptom */  
         s->quant2 = data->quant;  
   
739          return 0;          return 0;
740  }  }
741    
# Line 774  Line 797 
797                  rc->KFoverflow -= rc->KFoverflow_partial;                  rc->KFoverflow -= rc->KFoverflow_partial;
798          }          }
799    
800          rc->overflow += s->error = s->desired_length - data->length;          rc->overflow += (s->error = s->desired_length - data->length);
801          rc->real_total += data->length;          rc->real_total += data->length;
802    
803          DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- frame:%d type:%c quant:%d stats:%d scaled:%d desired:%d actual:%d error:%d overflow:%.2f\n",          DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- frame:%d type:%c quant:%d stats:%d scaled:%d desired:%d actual:%d error:%d overflow:%.2f\n",
# Line 823  Line 846 
846    
847                  char *ptr;                  char *ptr;
848                  char type;                  char type;
849                  int fields, nouse;                  int fields;
850    
851                  lines++;                  lines++;
852    
853                  /* We skip spaces */                  /* We skip spaces */
854                  ptr = skipspaces(line);                  ptr = skipspaces(line);
855    
856                  /* Skip coment lines */                  /* Skip coment lines or empty lines */
857                  if(iscomment(ptr)) {                  if(iscomment(ptr) || *ptr == '\0') {
858                          free(line);                          free(line);
859                          continue;                          continue;
860                  }                  }
861    
862                  /* Read the stat line from buffer */                  /* Read the stat line from buffer */
863                  fields = sscanf(ptr,                  fields = sscanf(ptr, "%c", &type);
                                                 "%c %d %d %d %d %d",  
                                                 &type, &nouse, &nouse, &nouse, &nouse, &nouse);  
864    
865                  /* Valid stats files have at least 6 fields */                  /* Valid stats files have at least 7 fields */
866                  if (fields == 6) {                  if (fields == 1) {
867                          switch(type) {                          switch(type) {
868                          case 'i':                          case 'i':
869                          case 'I':                          case 'I':
# Line 863  Line 884 
884                  } else {                  } else {
885                                  DPRINTF(XVID_DEBUG_RC,                                  DPRINTF(XVID_DEBUG_RC,
886                                                  "[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n",                                                  "[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n",
887                                                  lines, 6-fields);                                                  lines, 7-fields);
888                  }                  }
889    
890                  /* Free the line buffer */                  /* Free the line buffer */
# Line 901  Line 922 
922                  /* We skip spaces */                  /* We skip spaces */
923                  ptr = skipspaces(line);                  ptr = skipspaces(line);
924    
925                  /* Skip comment lines */                  /* Skip comment lines or empty lines */
926                  if(iscomment(ptr)) {                  if(iscomment(ptr) || *ptr == '\0') {
927                          free(line);                          free(line);
928                          continue;                          continue;
929                  }                  }
# Line 912  Line 933 
933    
934                  /* Convert the fields */                  /* Convert the fields */
935                  fields = sscanf(ptr,                  fields = sscanf(ptr,
936                                                  "%c %d %d %d %d %d %d\n",                                                  "%c %d %d %d %d %d %d %d\n",
937                                                  &type,                                                  &type,
938                                                  &s->quant,                                                  &s->quant,
939                                                  &s->blks[0], &s->blks[1], &s->blks[2],                                                  &s->blks[0], &s->blks[1], &s->blks[2],
940                                                  &s->length,                                                  &s->length, &s->invariant /* not really yet */,
941                                                  &s->scaled_length);                                                  &s->scaled_length);
942    
943                  /* Free line buffer, we don't need it anymore */                  /* Free line buffer, we don't need it anymore */
# Line 924  Line 945 
945    
946                  /* Fail silently, this has probably been warned in                  /* Fail silently, this has probably been warned in
947                   * statsfile_count_frames */                   * statsfile_count_frames */
948                  if(fields != 6 && fields != 7)                  if(fields != 7 && fields != 8)
949                          continue;                          continue;
950    
951                  /* Convert frame type */                  /* Convert frame type and compute the invariant length part */
952                  switch(type) {                  switch(type) {
953                  case 'i':                  case 'i':
954                  case 'I':                  case 'I':
955                          s->type = XVID_TYPE_IVOP;                          s->type = XVID_TYPE_IVOP;
956                            s->invariant /= INVARIANT_HEADER_PART_IVOP;
957                          break;                          break;
958                  case 'p':                  case 'p':
959                  case 'P':                  case 'P':
960                  case 's':                  case 's':
961                  case 'S':                  case 'S':
962                          s->type = XVID_TYPE_PVOP;                          s->type = XVID_TYPE_PVOP;
963                            s->invariant /= INVARIANT_HEADER_PART_PVOP;
964                          break;                          break;
965                  case 'b':                  case 'b':
966                  case 'B':                  case 'B':
967                          s->type = XVID_TYPE_BVOP;                          s->type = XVID_TYPE_BVOP;
968                            s->invariant /= INVARIANT_HEADER_PART_BVOP;
969                          break;                          break;
970                  default:                  default:
971                          /* Same as before, fail silently */                          /* Same as before, fail silently */
# Line 972  Line 996 
996          for (i=0; i<3; i++) {          for (i=0; i<3; i++) {
997                  rc->count[i]=0;                  rc->count[i]=0;
998                  rc->tot_length[i] = 0;                  rc->tot_length[i] = 0;
999                    rc->tot_invariant[i] = 0;
1000                  rc->min_length[i] = INT_MAX;                  rc->min_length[i] = INT_MAX;
1001          }          }
1002    
# Line 984  Line 1009 
1009    
1010                  rc->count[s->type-1]++;                  rc->count[s->type-1]++;
1011                  rc->tot_length[s->type-1] += s->length;                  rc->tot_length[s->type-1] += s->length;
1012                    rc->tot_invariant[s->type-1] += s->invariant;
1013    
1014                  if (s->length < rc->min_length[s->type-1]) {                  if (s->length < rc->min_length[s->type-1]) {
1015                          rc->min_length[s->type-1] = s->length;                          rc->min_length[s->type-1] = s->length;
# Line 1020  Line 1046 
1046    
1047          rc->avg_weight = 0.0;          rc->avg_weight = 0.0;
1048          rc->tot_quant = 0;          rc->tot_quant = 0;
1049            rc->tot_quant_invariant = 0;
1050    
1051          if (create->num_zones == 0) {          if (create->num_zones == 0) {
1052                  for (j = 0; j < rc->num_frames; j++) {                  for (j = 0; j < rc->num_frames; j++) {
# Line 1058  Line 1084 
1084                                  rc->stats[j].zone_mode = XVID_ZONE_QUANT;                                  rc->stats[j].zone_mode = XVID_ZONE_QUANT;
1085                                  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;
1086                                  rc->tot_quant += rc->stats[j].length;                                  rc->tot_quant += rc->stats[j].length;
1087                                    rc->tot_quant_invariant += rc->stats[j].invariant;
1088                          }                          }
1089                  }                  }
1090          }          }
# Line 1073  Line 1100 
1100  {  {
1101          int64_t target;          int64_t target;
1102          int64_t pass1_length;          int64_t pass1_length;
1103            int64_t total_invariant;
1104          double scaler;          double scaler;
1105          int i, num_MBs;          int i, num_MBs;
1106    
1107          /* We remove the bytes used by the fixed quantizer zones          /* We only scale texture data ! */
1108           * ToDo: this approach is flawed, the same amount of bytes is removed from          total_invariant  = rc->tot_invariant[XVID_TYPE_IVOP-1];
1109           *       target and first pass data, this has no sense, zone_process should          total_invariant += rc->tot_invariant[XVID_TYPE_PVOP-1];
1110           *       give us two results one for unscaled data (1pass) and the other          total_invariant += rc->tot_invariant[XVID_TYPE_BVOP-1];
1111           *       one for scaled data and we should then write:          /* don't forget to substract header bytes used in quant zones, otherwise we
1112           *       target = rc->target - rc->tot_quant_scaled;           * counting them twice */
1113           *       pass1_length = rc->i+p+b - rc->tot_quant_firstpass */          total_invariant -= rc->tot_quant_invariant;
1114          target = rc->target - rc->tot_quant;  
1115            /* We remove the bytes used by the fixed quantizer zones during first pass
1116             * with the same quants, so we know very precisely how much that
1117             * represents */
1118            target  = rc->target;
1119            target -= rc->tot_quant;
1120    
1121          /* Do the same for the first pass data */          /* Do the same for the first pass data */
1122          pass1_length  = rc->tot_length[XVID_TYPE_IVOP-1];          pass1_length  = rc->tot_length[XVID_TYPE_IVOP-1];
# Line 1092  Line 1125 
1125          pass1_length -= rc->tot_quant;          pass1_length -= rc->tot_quant;
1126    
1127          /* Let's compute a linear scaler in order to perform curve scaling */          /* Let's compute a linear scaler in order to perform curve scaling */
1128          scaler = (double)target / (double)pass1_length;          scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant);
1129    
1130          if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {  #ifdef PASS_SMALLER
1131            if ((target - total_invariant) <= 0 ||
1132                    (pass1_length - total_invariant) <= 0 ||
1133                    target >= pass1_length) {
1134                  DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n");                  DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n");
1135                  scaler = 1.0;                  scaler = 1.0;
1136          }          }
1137    #endif
1138    
1139          /* Compute min frame lengths (for each frame type) according to the number          /* Compute min frame lengths (for each frame type) according to the number
1140           * of MBs. We sum all block type counters of frame 0, this gives us the           * of MBs. We sum all block type counters of frame 0, this gives us the
# Line 1139  Line 1176 
1176                          continue;                          continue;
1177                  }                  }
1178    
1179                  /* Compute the scaled length */                  /* Compute the scaled length -- only non invariant data length is scaled */
1180                  len = (int)((double)s->length * scaler * s->weight / rc->avg_weight);                  len = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight / rc->avg_weight);
1181    
1182                  /* Compare with the computed minimum */                  /* Compare with the computed minimum */
1183                  if (len < rc->min_length[s->type-1]) {                  if (len < rc->min_length[s->type-1]) {
# Line 1163  Line 1200 
1200           * total counters. Now, it's possible to scale the 'regular' frames. */           * total counters. Now, it's possible to scale the 'regular' frames. */
1201    
1202          /* Scaling factor for 'regular' frames */          /* Scaling factor for 'regular' frames */
1203          scaler = (double)target / (double)pass1_length;          scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant);
1204    
1205    #ifdef PASS_SMALLER
1206          /* Detect undersizing */          /* Detect undersizing */
1207          if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {          if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {
1208                  DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected after correction\n");                  DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected after correction\n");
1209                  scaler = 1.0;                  scaler = 1.0;
1210          }          }
1211    #endif
1212    
1213          /* Do another pass with the new scaler */          /* Do another pass with the new scaler */
1214          for (i=0; i<rc->num_frames; i++) {          for (i=0; i<rc->num_frames; i++) {
# Line 1177  Line 1216 
1216    
1217                  /* Ignore frame with forced frame sizes */                  /* Ignore frame with forced frame sizes */
1218                  if (s->scaled_length == 0)                  if (s->scaled_length == 0)
1219                          s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight);                          s->scaled_length = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight / rc->avg_weight);
1220          }          }
1221    
1222          /* Job done */          /* Job done */
# Line 1192  Line 1231 
1231  scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc)  scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc)
1232  {  {
1233          int i;          int i;
1234          uint64_t ivop_boost_total;          int64_t ivop_boost_total;
1235    
1236          /* Reset the rate controller (per frame type) total byte counters */          /* Reset the rate controller (per frame type) total byte counters */
1237          for (i=0; i<3; i++) rc->tot_scaled_length[i] = 0;          for (i=0; i<3; i++) rc->tot_scaled_length[i] = 0;
# Line 1215  Line 1254 
1254    
1255                  /* Some more work is needed for I frames */                  /* Some more work is needed for I frames */
1256                  if (s->type == XVID_TYPE_IVOP) {                  if (s->type == XVID_TYPE_IVOP) {
                         int penalty_distance;  
1257                          int ivop_boost;                          int ivop_boost;
1258    
1259                          /* Accumulate bytes needed for keyframe boosting */                          /* Accumulate bytes needed for keyframe boosting */
1260                          ivop_boost = s->scaled_length*rc->param.keyframe_boost/100;                          ivop_boost = s->scaled_length*rc->param.keyframe_boost/100;
1261    
1262                          if (rc->KF_idx) {  #if 0 /* ToDo: decide how to apply kfthresholding */
1263                                  /* Minimum keyframe distance penalties */  #endif
                                 penalty_distance  = rc->param.min_key_interval;  
                                 penalty_distance -= rc->keyframe_locations[rc->KF_idx];  
                                 penalty_distance += rc->keyframe_locations[rc->KF_idx-1];  
   
                                 /* Ah ah ! guilty keyframe, you're under arrest ! */  
                                 if (penalty_distance > 0)  
                                          ivop_boost -= (s->scaled_length + ivop_boost)*penalty_distance*rc->param.kfreduction/100;  
                         }  
   
1264                          /* If the frame size drops under the minimum length, then cap ivop_boost */                          /* If the frame size drops under the minimum length, then cap ivop_boost */
1265                          if (ivop_boost + s->scaled_length < rc->min_length[XVID_TYPE_IVOP-1])                          if (ivop_boost + s->scaled_length < rc->min_length[XVID_TYPE_IVOP-1])
1266                                  ivop_boost = rc->min_length[XVID_TYPE_IVOP-1] - s->scaled_length;                                  ivop_boost = rc->min_length[XVID_TYPE_IVOP-1] - s->scaled_length;
# Line 1261  Line 1290 
1290    
1291          /* Compute the ratio described above          /* Compute the ratio described above
1292           *     taxed_total = sum(0, n, tax*scaled_length)           *     taxed_total = sum(0, n, tax*scaled_length)
1293           * <=> taxed_total = tax.sum(0, n, tax*scaled_length)           * <=> taxed_total = tax.sum(0, n, scaled_length)
1294           * <=> tax = taxed_total / original_total */           * <=> tax = taxed_total / original_total */
1295          rc->pb_iboost_tax_ratio =          rc->pb_iboost_tax_ratio =
1296                  (rc->pb_iboost_tax_ratio - ivop_boost_total) /                  (rc->pb_iboost_tax_ratio - ivop_boost_total) /
# Line 1278  Line 1307 
1307                  } else {                  } else {
1308                          rc->avg_length[i] = rc->tot_scaled_length[i];                          rc->avg_length[i] = rc->tot_scaled_length[i];
1309    
1310                          if (i == XVID_TYPE_IVOP) {                          if (i == (XVID_TYPE_IVOP-1)) {
1311                                  /* I Frames total has to be added the boost total */                                  /* I Frames total has to be added the boost total */
1312                                  rc->avg_length[i] += ivop_boost_total;                                  rc->avg_length[i] += ivop_boost_total;
1313                          } else {                          } else {

Legend:
Removed from v.1202  
changed lines
  Added in v.1296

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