[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.9, Thu May 22 22:17:44 2003 UTC
# Line 3  Line 3 
3   * XviD Bit Rate Controller Library   * XviD Bit Rate Controller Library
4   * - VBR 2 pass bitrate controler implementation -   * - VBR 2 pass bitrate controler 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>
35    
36  #define RAD2DEG 57.295779513082320876798154814105  #define RAD2DEG 57.295779513082320876798154814105
37  #define DEG2RAD 0.017453292519943295769236907684886  #define DEG2RAD 0.017453292519943295769236907684886
# Line 171  Line 175 
175          }else if (type == 'b') {          }else if (type == 'b') {
176              s->type = XVID_TYPE_BVOP;              s->type = XVID_TYPE_BVOP;
177          }else{  /* unknown type */          }else{  /* unknown type */
178              DPRINTF(XVID_DEBUG_RC, "unknown stats frame type; assuming pvop");              DPRINTF(XVID_DEBUG_RC, "unknown stats frame type; assuming pvop\n");
179              s->type = XVID_TYPE_PVOP;              s->type = XVID_TYPE_PVOP;
180          }          }
181    
182          i++;          i++;
183      }      }
184    
185      rc->num_frames = i;      rc->num_frames = i;
186    
187      fclose(f);      fclose(f);
# Line 190  Line 195 
195  static void print_stats(rc_2pass2_t * rc)  static void print_stats(rc_2pass2_t * rc)
196  {  {
197      int i;      int i;
198        DPRINTF(XVID_DEBUG_RC, "type quant length scaled_length\n");
199      for (i = 0; i < rc->num_frames; i++) {      for (i = 0; i < rc->num_frames; i++) {
200          stat_t * s = &rc->stats[i];          stat_t * s = &rc->stats[i];
201          DPRINTF(XVID_DEBUG_RC, "%i %i %i %i\n", s->type, s->quant, s->length, s->scaled_length);          DPRINTF(XVID_DEBUG_RC, "%d %d %d %d\n", s->type, s->quant, s->length, s->scaled_length);
202      }      }
203  }  }
204  #endif  #endif
# Line 202  Line 208 
208      - set keyframes_locations      - set keyframes_locations
209  */  */
210    
211  void pre_process0(rc_2pass2_t * rc)  static void
212    pre_process0(rc_2pass2_t * rc)
213  {  {
214      int i,j;      int i,j;
215    
# Line 210  Line 217 
217          rc->count[i]=0;          rc->count[i]=0;
218          rc->tot_length[i] = 0;          rc->tot_length[i] = 0;
219          rc->last_quant[i] = 0;          rc->last_quant[i] = 0;
220                    rc->min_length[i] = INT_MAX;
221      }      }
222    
223            rc->max_length = INT_MIN;
224    
225      for (i=j=0; i<rc->num_frames; i++) {      for (i=j=0; i<rc->num_frames; i++) {
226          stat_t * s = &rc->stats[i];          stat_t * s = &rc->stats[i];
227    
228          rc->count[s->type-1]++;          rc->count[s->type-1]++;
229          rc->tot_length[s->type-1] += s->length;          rc->tot_length[s->type-1] += s->length;
230    
231          if (i == 0 || s->length < rc->min_length[s->type-1]) {          if (s->length < rc->min_length[s->type-1]) {
232              rc->min_length[s->type-1] = s->length;              rc->min_length[s->type-1] = s->length;
233          }          }
234    
235          if (i == 0 || s->length > rc->max_length) {          if (s->length > rc->max_length) {
236              rc->max_length = s->length;              rc->max_length = s->length;
237          }          }
238    
# Line 231  Line 241 
241              j++;              j++;
242          }          }
243      }      }
244    
245            /*
246             * The "per sequence" overflow system considers a natural sequence to be
247             * formed by all frames between two iframes, so if we want to make sure
248             * the system does not go nuts during last sequence, we force the last
249             * frame to appear in the keyframe locations array.
250             */
251      rc->keyframe_locations[j] = i;      rc->keyframe_locations[j] = i;
252    
253            DPRINTF(XVID_DEBUG_RC, "Min 1st pass IFrame length: %d\n", rc->min_length[0]);
254            DPRINTF(XVID_DEBUG_RC, "Min 1st pass PFrame length: %d\n", rc->min_length[1]);
255            DPRINTF(XVID_DEBUG_RC, "Min 1st pass BFrame length: %d\n", rc->min_length[2]);
256  }  }
257    
258    
259  /* calculate zone weight "center" */  /* calculate zone weight "center" */
260    
261  static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create)  static void
262    zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create)
263  {  {
264      int i,j;      int i,j;
265      int n = 0;      int n = 0;
# Line 293  Line 315 
315    
316  /* scale the curve */  /* scale the curve */
317    
318  static void internal_scale(rc_2pass2_t *rc)  static void
319    internal_scale(rc_2pass2_t *rc)
320  {  {
321          int64_t target  = rc->target - rc->tot_quant;          int64_t target  = rc->target - rc->tot_quant;
322          int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant;          int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant;
# Line 302  Line 325 
325          int i;          int i;
326    
327    
328          /* perform an initial scale pass.          /*
329             if a frame size is scaled underneath our hardcoded minimums, then we force the           * Perform an initial scale pass.
330             frame size to the minimum, and deduct the original & scaled frmae length from the           * if a frame size is scaled underneath our hardcoded minimums, then we
331             original and target total lengths */           * force the frame size to the minimum, and deduct the original & scaled
332             * frame length from the original and target total lengths
333             */
334    
335          min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8;          min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8;
336          min_size[1] = (rc->stats[0].blks[0] + 88) / 8;          min_size[1] = (rc->stats[0].blks[0] + 88) / 8;
# Line 318  Line 343 
343          scaler = 1.0;          scaler = 1.0;
344          }          }
345    
346      DPRINTF(XVID_DEBUG_RC, "target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler);      DPRINTF(XVID_DEBUG_RC,
347                            "Before any correction: target=%i, tot_length=%i, scaler=%f\n",
348                            (int)target, (int)pass1_length, scaler);
349    
350          for (i=0; i<rc->num_frames; i++) {          for (i=0; i<rc->num_frames; i++) {
351                  stat_t * s = &rc->stats[i];                  stat_t * s = &rc->stats[i];
# Line 344  Line 371 
371                  scaler = 1.0;                  scaler = 1.0;
372          }          }
373    
374          DPRINTF(XVID_DEBUG_RC, "target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler);          DPRINTF(XVID_DEBUG_RC,
375                            "After correction: target=%i, tot_length=%i, scaler=%f\n",
376                            (int)target, (int)pass1_length, scaler);
377    
378          for (i=0; i<rc->num_frames; i++) {          for (i=0; i<rc->num_frames; i++) {
379                  stat_t * s = &rc->stats[i];                  stat_t * s = &rc->stats[i];
# Line 358  Line 387 
387    
388    
389    
390  void pre_process1(rc_2pass2_t * rc)  static void
391    pre_process1(rc_2pass2_t * rc)
392  {  {
393      int i;      int i;
394      double total1, total2;      double total1, total2;
# Line 528  Line 558 
558    
559          /* special info for alt curve:  bias bonus and quantizer thresholds */          /* special info for alt curve:  bias bonus and quantizer thresholds */
560    
561                  DPRINTF(XVID_DEBUG_RC, "avg scaled framesize:%i", (int)rc->avg_length[XVID_TYPE_PVOP-1]);                  DPRINTF(XVID_DEBUG_RC, "avg scaled framesize:%i\n", (int)rc->avg_length[XVID_TYPE_PVOP-1]);
562                  DPRINTF(XVID_DEBUG_RC, "bias bonus:%i bytes", (int)rc->alt_curve_curve_bias_bonus);                  DPRINTF(XVID_DEBUG_RC, "bias bonus:%i bytes\n", (int)rc->alt_curve_curve_bias_bonus);
563    
564                  for (i=1; i <= (int)(rc->alt_curve_high*2)+1; i++) {                  for (i=1; i <= (int)(rc->alt_curve_high*2)+1; i++) {
565              double curve_temp, dbytes;              double curve_temp, dbytes;
# Line 578  Line 608 
608                                  if (newquant != oldquant) {                                  if (newquant != oldquant) {
609                      int percent = (int)((i - rc->avg_length[XVID_TYPE_PVOP-1]) * 100.0 / rc->avg_length[XVID_TYPE_PVOP-1]);                      int percent = (int)((i - rc->avg_length[XVID_TYPE_PVOP-1]) * 100.0 / rc->avg_length[XVID_TYPE_PVOP-1]);
610                                          oldquant = newquant;                                          oldquant = newquant;
611                                          DPRINTF(XVID_DEBUG_RC, "quant:%i threshold at %i : %i percent", newquant, i, percent);                                          DPRINTF(XVID_DEBUG_RC, "quant:%i threshold at %i : %i percent\n", newquant, i, percent);
612                                  }                                  }
613                          }                          }
614                  }                  }
# Line 639  Line 669 
669          return XVID_ERR_MEMORY;          return XVID_ERR_MEMORY;
670      }      }
671    
672      /* XXX: do we need an addition location */      /*
673             * We need an extra location because we do as if the last frame were an
674             * IFrame. This is needed because our code consider that frames between
675             * 2 IFrames form a natural sequence. So we store last frame as a
676             * keyframe location.
677             */
678      if ((rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int))) == NULL) {      if ((rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int))) == NULL) {
679          free(rc->stats);          free(rc->stats);
680          free(rc);          free(rc);
# Line 659  Line 694 
694          if (rc->num_frames  < create->fbase/create->fincr) {          if (rc->num_frames  < create->fbase/create->fincr) {
695                  rc->target = rc->param.bitrate / 8;     /* one second */                  rc->target = rc->param.bitrate / 8;     /* one second */
696          }else{          }else{
697                  rc->target = (rc->param.bitrate * rc->num_frames * create->fincr) / (create->fbase * 8);                  rc->target =
698                            ((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * (uint64_t)create->fincr) / \
699                            ((uint64_t)create->fbase * 8);
700          }          }
701    
702      DPRINTF(XVID_DEBUG_RC, "rc->target : %i\n", rc->target);      DPRINTF(XVID_DEBUG_RC, "Number of frames: %d\n", rc->num_frames);
703            DPRINTF(XVID_DEBUG_RC, "Frame rate: %d/%d\n", create->fbase, create->fincr);
704            DPRINTF(XVID_DEBUG_RC, "Target bitrate: %ld\n", rc->param.bitrate);
705            DPRINTF(XVID_DEBUG_RC, "Target filesize: %lld\n", rc->target);
706    
707    #if 0
708          rc->target -= rc->num_frames*24;        /* avi file header */          rc->target -= rc->num_frames*24;        /* avi file header */
709    #endif
710    
711    
712          pre_process0(rc);          pre_process0(rc);
# Line 715  Line 757 
757      double curve_temp;      double curve_temp;
758      int capped_to_max_framesize = 0;      int capped_to_max_framesize = 0;
759    
760      if (data->quant <= 0) {          /*
761             * This function is quite long but easy to understand. In order to simplify
762             * the code path (a bit), we treat 3 cases that can return immediatly.
763             */
764    
765            /* First case: Another plugin has already set a quantizer */
766        if (data->quant > 0)
767                    return(0);
768    
769            /* Second case: We are in a Quant zone */
770          if (s->zone_mode == XVID_ZONE_QUANT) {          if (s->zone_mode == XVID_ZONE_QUANT) {
771    
772              rc->fq_error += s->weight;              rc->fq_error += s->weight;
# Line 725  Line 775 
775    
776              s->desired_length = s->length;              s->desired_length = s->length;
777    
778          }else { /* XVID_ZONE_WEIGHT */                  return(0);
779    
             if (data->frame_num >= rc->num_frames) {  
                 /* insufficent stats data */  
                 return 0;  
780              }              }
781    
782              overflow = rc->overflow / 8;        /* XXX: why by 8 */          /* Third case: insufficent stats data */
783            if (data->frame_num >= rc->num_frames)
784                    return 0;
785    
786            /*
787             * The last case is the one every normal minded developer should fear to
788             * maintain in a project :-)
789             */
790    
791            /* XXX: why by 8 */
792            overflow = rc->overflow / 8;
793    
794              if (s->type == XVID_TYPE_IVOP) {        /* XXX: why */          /*
795             * The rc->overflow field represents the overflow in current scene (between two
796             * IFrames) so we must not forget to reset it if we are enetring a new scene
797             */
798            if (s->type == XVID_TYPE_IVOP) {
799                  overflow = 0;                  overflow = 0;
800              }              }
801    
# Line 746  Line 807 
807              }              }
808              dbytes /= rc->movie_curve;              dbytes /= rc->movie_curve;
809    
810            /*
811             * We are now entering in the hard part of the algo, it was first designed
812             * to work with i/pframes only streams, so the way it computes things is
813             * adapted to pframes only. However we can use it if we just take care to
814             * scale the bframes sizes to pframes sizes using the ratio avg_p/avg_p and
815             * then before really using values depending on frame sizes, scaling the
816             * value again with the inverse ratio
817             */
818              if (s->type == XVID_TYPE_BVOP) {              if (s->type == XVID_TYPE_BVOP) {
819                  dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];                  dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];
820              }              }
821    
822            /*
823             * Apply user's choosen Payback method. Payback helps bitrate to follow the
824             * scaled curve "paying back" past errors in curve previsions.
825             */
826              if (rc->param.payback_method == XVID_PAYBACK_BIAS) {              if (rc->param.payback_method == XVID_PAYBACK_BIAS) {
827                  desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay);                  desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay);
828              }else{              }else{
                         //printf("desired=%i, dbytes=%i\n", desired,dbytes);  
829                          desired = (int)(rc->curve_comp_error * dbytes /                          desired = (int)(rc->curve_comp_error * dbytes /
830                                  rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay);                                  rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay);
                         //printf("desired=%i\n", desired);  
831    
832                          if (labs(desired) > fabs(rc->curve_comp_error)) {                          if (labs(desired) > fabs(rc->curve_comp_error)) {
833                                  desired = (int)rc->curve_comp_error;                                  desired = (int)rc->curve_comp_error;
# Line 765  Line 836 
836    
837              rc->curve_comp_error -= desired;              rc->curve_comp_error -= desired;
838    
839              /* alt curve */          /*
840             * Alt curve treatment is not that hard to understand though the formulas
841             * seem to be huge. Alt treatment is basically a way to soft/harden the
842             * curve flux applying sine/linear/cosine ratios
843             */
844    
845              curve_temp = 0; /* XXX: warning */          /* XXX: warning */
846            curve_temp = 0;
847    
848              if (rc->param.use_alt_curve) {              if (rc->param.use_alt_curve) {
849                  if (s->type != XVID_TYPE_IVOP)  {                  if (s->type != XVID_TYPE_IVOP)  {
# Line 802  Line 878 
878                              }                              }
879                                          }                                          }
880                                  }                                  }
881    
882                            /*
883                             * End of code path for curve_temp, as told earlier, we are now
884                             * obliged to scale the value to a bframe one using the inverse
885                             * ratio applied earlier
886                             */
887                                  if (s->type == XVID_TYPE_BVOP)                                  if (s->type == XVID_TYPE_BVOP)
888                                          curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];                                          curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
889    
# Line 810  Line 892 
892                                  desired += ((int)curve_temp);                                  desired += ((int)curve_temp);
893                                  rc->curve_comp_error += curve_temp - (int)curve_temp;                                  rc->curve_comp_error += curve_temp - (int)curve_temp;
894                          }else{                          }else{
895                            /*
896                             * End of code path for dbytes, as told earlier, we are now
897                             * obliged to scale the value to a bframe one using the inverse
898                             * ratio applied earlier
899                             */
900                                  if (s->type == XVID_TYPE_BVOP)                                  if (s->type == XVID_TYPE_BVOP)
901                                          dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];                                          dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
902    
# Line 826  Line 913 
913                      curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);                      curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);
914                  }                  }
915    
916                  if (s->type == XVID_TYPE_BVOP){                  /*
917                     * End of code path for curve_temp, as told earlier, we are now
918                     * obliged to scale the value to a bframe one using the inverse
919                     * ratio applied earlier
920                     */
921                    if (s->type == XVID_TYPE_BVOP)
922                      curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];                      curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
                 }  
923    
924                  desired += (int)curve_temp;                  desired += (int)curve_temp;
925                  rc->curve_comp_error += curve_temp - (int)curve_temp;                  rc->curve_comp_error += curve_temp - (int)curve_temp;
926              }else{              }else{
927                    /*
928                     * End of code path for dbytes, as told earlier, we are now
929                     * obliged to scale the value to a bframe one using the inverse
930                     * ratio applied earlier
931                     */
932                  if (s->type == XVID_TYPE_BVOP){                  if (s->type == XVID_TYPE_BVOP){
933                                  dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];                                  dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
934                  }                  }
# Line 842  Line 938 
938              }              }
939    
940    
941                  if (desired > s->length) {  /* if desired length exceeds the pass1 length.. */          /*
942             * We can't do bigger frames than first pass, this would be stupid as first
943             * pass is quant=2 and that reaching quant=1 is not worth it. We would lose
944             * many bytes and we would not not gain much quality.
945             */
946            if (desired > s->length) {
947                          rc->curve_comp_error += desired - s->length;                          rc->curve_comp_error += desired - s->length;
948                          desired = s->length;                          desired = s->length;
949                  }else{                  }else{
# Line 885  Line 986 
986    
987              overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]);              overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]);
988    
989                  // Foxer: reign in overflow with huge frames          /* Reign in overflow with huge frames */
990                  if (labs(overflow) > labs(rc->overflow)) {                  if (labs(overflow) > labs(rc->overflow)) {
991                          overflow = rc->overflow;                          overflow = rc->overflow;
992                  }                  }
993    
994              // Foxer: make sure overflow doesn't run away          /* Make sure overflow doesn't run away */
   
995                  if (overflow > desired * rc->param.max_overflow_improvement / 100) {                  if (overflow > desired * rc->param.max_overflow_improvement / 100) {
996                          desired += (overflow <= desired) ? desired * rc->param.max_overflow_improvement / 100 :                          desired += (overflow <= desired) ? desired * rc->param.max_overflow_improvement / 100 :
997                                  overflow * rc->param.max_overflow_improvement / 100;                                  overflow * rc->param.max_overflow_improvement / 100;
# Line 901  Line 1001 
1001                          desired += overflow;                          desired += overflow;
1002                  }                  }
1003    
1004            /* Make sure we are not higher than desired frame size */
1005              if (desired > rc->max_length) {              if (desired > rc->max_length) {
1006                          capped_to_max_framesize = 1;                          capped_to_max_framesize = 1;
1007                          desired = rc->max_length;                          desired = rc->max_length;
1008                    DPRINTF(XVID_DEBUG_RC,"[%i] Capped to maximum frame size\n",
1009                                    data->frame_num);
1010                  }                  }
1011    
1012              // make sure to not scale below the minimum framesize          /* Make sure to not scale below the minimum framesize */
1013              if (desired < rc->min_length[s->type-1]) {              if (desired < rc->min_length[s->type-1]) {
1014                  desired = rc->min_length[s->type-1];                  desired = rc->min_length[s->type-1];
1015                    DPRINTF(XVID_DEBUG_RC,"[%i] Capped to minimum frame size\n",
1016                                    data->frame_num);
1017              }              }
1018    
1019            /*
1020              // very 'simple' quant<->filesize relationship           * Don't laugh at this very 'simple' quant<->filesize relationship, it
1021             * proves to be acurate enough for our algorithm
1022             */
1023              data->quant= (s->quant * s->length) / desired;              data->quant= (s->quant * s->length) / desired;
1024    
1025            /* Let's clip the computed quantizer, if needed */
1026                  if (data->quant < 1) {                  if (data->quant < 1) {
1027                          data->quant = 1;                          data->quant = 1;
1028              } else if (data->quant > 31) {              } else if (data->quant > 31) {
1029                          data->quant = 31;                          data->quant = 31;
1030                  }          } else if (s->type != XVID_TYPE_IVOP) {
1031                  else if (s->type != XVID_TYPE_IVOP)  
1032                  {                  /*
1033                          // Foxer: aid desired quantizer precision by accumulating decision error                   * The frame quantizer has not been clipped, this appear to be a good
1034                     * computed quantizer, however past frames give us some info about how
1035                     * this quantizer performs against the algo prevision. Let's use this
1036                     * prevision to increase the quantizer when we observe a too big
1037                     * accumulated error
1038                     */
1039                          if (s->type== XVID_TYPE_BVOP) {                          if (s->type== XVID_TYPE_BVOP) {
1040                                  rc->bquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant;                                  rc->bquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant;
1041    
# Line 940  Line 1053 
1053                          }                          }
1054                  }                  }
1055    
1056              /* cap to min/max quant */          /*
1057             * Now we have a computed quant that is in the right quante range, with a
1058             * possible +1 correction due to cumulated error. We can now safely clip
1059             * the quantizer again with user's quant ranges. "Safely" means the Rate
1060             * Control could learn more about this quantizer, this knowledge is useful
1061             * for future frames even if it this quantizer won't be really used atm,
1062             * that's why we don't perform this clipping earlier.
1063             */
1064              if (data->quant < data->min_quant[s->type-1]) {              if (data->quant < data->min_quant[s->type-1]) {
1065                  data->quant = data->min_quant[s->type-1];                  data->quant = data->min_quant[s->type-1];
1066              }else if (data->quant > data->max_quant[s->type-1]) {              }else if (data->quant > data->max_quant[s->type-1]) {
1067                  data->quant = data->max_quant[s->type-1];                  data->quant = data->max_quant[s->type-1];
1068              }              }
1069    
1070              /* subsequent p/b frame quants can only be +- 2 */          /*
1071             * To avoid big quality jumps from frame to frame, we apply a "security"
1072             * rule that makes |last_quant - new_quant| <= 2. This rule only applies
1073             * to predicted frames (P and B)
1074             */
1075                  if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) {                  if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) {
1076    
1077                          if (data->quant > rc->last_quant[s->type-1] + 2) {                          if (data->quant > rc->last_quant[s->type-1] + 2) {
1078                                  data->quant = rc->last_quant[s->type-1] + 2;                                  data->quant = rc->last_quant[s->type-1] + 2;
1079                                  DPRINTF(XVID_DEBUG_RC, "p/b-frame quantizer prevented from rising too steeply");                          DPRINTF(XVID_DEBUG_RC,
1080                                            "[%i] p/b-frame quantizer prevented from rising too steeply\n",
1081                                            data->frame_num);
1082                          }                          }
1083                          if (data->quant < rc->last_quant[s->type-1] - 2) {                          if (data->quant < rc->last_quant[s->type-1] - 2) {
1084                                  data->quant = rc->last_quant[s->type-1] - 2;                                  data->quant = rc->last_quant[s->type-1] - 2;
1085                                  DPRINTF(XVID_DEBUG_RC, "p/b-frame quantizer prevented from falling too steeply");                          DPRINTF(XVID_DEBUG_RC,
1086                                            "[%i] p/b-frame quantizer prevented from falling too steeply\n",
1087                                            data->frame_num);
1088                          }                          }
1089                  }                  }
1090    
1091            /*
1092             * We don't want to pollute the RC history results when our computed quant
1093             * has been computed from a capped frame size
1094             */
1095                  if (capped_to_max_framesize == 0) {                  if (capped_to_max_framesize == 0) {
1096                  rc->last_quant[s->type-1] = data->quant;                  rc->last_quant[s->type-1] = data->quant;
1097                  }                  }
1098    
   
         }   /* if */  
   
     }  
   
1099      return 0;      return 0;
1100  }  }
1101    
# Line 979  Line 1105 
1105  {  {
1106      stat_t * s = &rc->stats[data->frame_num];      stat_t * s = &rc->stats[data->frame_num];
1107    
1108      if (data->frame_num >= rc->num_frames) {          /* Insufficent stats data */
1109          /* insufficent stats data */      if (data->frame_num >= rc->num_frames)
1110          return 0;          return 0;
     }  
1111    
1112      rc->quant_count[data->quant]++;      rc->quant_count[data->quant]++;
1113    

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

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