[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.30, Mon Dec 8 12:38:04 2003 UTC revision 1.1.2.31, Wed Dec 17 15:16:16 2003 UTC
# Line 29  Line 29 
29   *   *
30   *****************************************************************************/   *****************************************************************************/
31    
32    #define BQUANT_PRESCALE
33  #undef COMPENSATE_FORMULA  #undef COMPENSATE_FORMULA
34    
35  #include <stdio.h>  #include <stdio.h>
# Line 39  Line 40 
40  #include "../image/image.h"  #include "../image/image.h"
41    
42  /*****************************************************************************  /*****************************************************************************
43   * Some constants   * Some default settings
44   ****************************************************************************/   ****************************************************************************/
45    
46  #define DEFAULT_KEYFRAME_BOOST 0  #define DEFAULT_KEYFRAME_BOOST 0
47  #define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10  #define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10
48  #define DEFAULT_CURVE_COMPRESSION_HIGH 0  #define DEFAULT_CURVE_COMPRESSION_HIGH 0
49  #define DEFAULT_CURVE_COMPRESSION_LOW 0  #define DEFAULT_CURVE_COMPRESSION_LOW 0
50  #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60  #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 10
51  #define DEFAULT_MAX_OVERFLOW_DEGRADATION 60  #define DEFAULT_MAX_OVERFLOW_DEGRADATION 10
52    
53  /* Keyframe settings */  /* Keyframe settings */
54  #define DEFAULT_KFREDUCTION 20  #define DEFAULT_KFREDUCTION 20
55  #define DEFAULT_KFTHRESHOLD 1  #define DEFAULT_KFTHRESHOLD 1
56    
57  /*****************************************************************************  /*****************************************************************************
58     * Some default constants (can be tuned)
59     ****************************************************************************/
60    
61    /* Specify the invariant part of the headers bits (header+MV)
62     * as  hlength/cst */
63    #define INVARIANT_HEADER_PART_IVOP 1 /* factor 1.0f   */
64    #define INVARIANT_HEADER_PART_PVOP 2 /* factor 0.5f   */
65    #define INVARIANT_HEADER_PART_BVOP 8 /* factor 0.125f */
66    
67    /*****************************************************************************
68   * Structures   * Structures
69   ****************************************************************************/   ****************************************************************************/
70    
# Line 61  Line 72 
72  typedef struct {  typedef struct {
73          int type;               /* first pass type */          int type;               /* first pass type */
74          int quant;              /* first pass quant */          int quant;              /* first pass quant */
         int quant2;             /* Second pass quant */  
75          int blks[3];                    /* k,m,y blks */          int blks[3];                    /* k,m,y blks */
76          int length;             /* first pass length */          int length;             /* first pass length */
77            int invariant;          /* what we assume as being invariant between the two passes, it's a sub part of header + MV bits */
78          int scaled_length;      /* scaled length */          int scaled_length;      /* scaled length */
79          int desired_length;     /* desired length; calculated during encoding */          int desired_length;     /* desired length; calculated during encoding */
80          int error;          int error;
# Line 95  Line 106 
106    
107          /* Total length of each frame types (1st pass) */          /* Total length of each frame types (1st pass) */
108          uint64_t tot_length[3];          uint64_t tot_length[3];
109            uint64_t tot_invariant[3];
110    
111          /* 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
112           * then for scaled averages */           * then for scaled averages */
# Line 124  Line 136 
136          double avg_weight;          double avg_weight;
137    
138          /* Total length used by XVID_ZONE_QUANT zones */          /* Total length used by XVID_ZONE_QUANT zones */
139          int64_t tot_quant;          uint64_t tot_quant;
140            uint64_t tot_quant_invariant;
141    
142          /*----------------------------------          /*----------------------------------
143           * Advanced settings helper ratios           * Advanced settings helper ratios
# Line 367  Line 380 
380           *  - count how many times each frame type has been used.           *  - count how many times each frame type has been used.
381           *     rc->count[]           *     rc->count[]
382           *  - total bytes used per frame type           *  - total bytes used per frame type
383           *     rc->total[]           *     rc->tot_length[]
384             *  - total bytes considered invariant between the 2 passes
385           *  - store keyframe location           *  - store keyframe location
386           *     rc->keyframe_locations[]           *     rc->keyframe_locations[]
387           */           */
# Line 376  Line 390 
390          /* 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
391           * application */           * application */
392          if (rc->param.bitrate) {          if (rc->param.bitrate) {
393                  /* Apply zone settings */                  /* Apply zone settings
394                     * - set rc->tot_quant which represents the total num of bytes spent in
395                     *   fixed quant zones
396                     * - set rc->tot_quant_invariant which represents the total num of bytes spent
397                     *   in fixed quant zones for headers */
398                  zone_process(rc, create);                  zone_process(rc, create);
399                  /* Perform internal curve scaling */                  /* Perform internal curve scaling */
400                  first_pass_scale_curve_internal(rc);                  first_pass_scale_curve_internal(rc);
# Line 598  Line 616 
616           * Desired frame length <-> quantizer mapping           * Desired frame length <-> quantizer mapping
617           *-----------------------------------------------------------------------*/           *-----------------------------------------------------------------------*/
618    
619          /* For bframes we must retrieve the original quant used (sent to xvidcore)  #ifdef BQUANT_PRESCALE
620           * as core applies the bquant formula before writing the stat log entry */          /* For bframes we prescale the quantizer to avoid too high quant scaling */
621          if(s->type == XVID_TYPE_BVOP) {          if(s->type == XVID_TYPE_BVOP) {
622    
623                  twopass_stat_t *b_ref = s;                  twopass_stat_t *b_ref = s;
# Line 610  Line 628 
628    
629                  /* Compute the original quant */                  /* Compute the original quant */
630                  s->quant  = 2*(100*s->quant - data->bquant_offset);                  s->quant  = 2*(100*s->quant - data->bquant_offset);
631                  s->quant += data->bquant_ratio - 1; /* to avoid rouding issues */                  s->quant += data->bquant_ratio - 1; /* to avoid rounding issues */
632                  s->quant  = s->quant/data->bquant_ratio - b_ref->quant;                  s->quant  = s->quant/data->bquant_ratio - b_ref->quant;
633          }          }
634    #endif
635    
636          /* Don't laugh at this very 'simple' quant<->filesize relationship, it          /* Don't laugh at this very 'simple' quant<->size relationship, it
637           * proves to be acurate enough for our algorithm */           * proves to be acurate enough for our algorithm */
638          scaled_quant = (double)s->quant*(double)s->length/(double)dbytes;          scaled_quant = (double)s->quant*(double)s->length/(double)dbytes;
639    
# Line 702  Line 721 
721          /* Don't forget to force 1st pass frame type ;-) */          /* Don't forget to force 1st pass frame type ;-) */
722          data->type = s->type;          data->type = s->type;
723    
         /* Store the quantizer into the statistics -- Used to compensate the double  
          * formula symptom */  
         s->quant2 = data->quant;  
   
724          return 0;          return 0;
725  }  }
726    
# Line 767  Line 782 
782                  rc->KFoverflow -= rc->KFoverflow_partial;                  rc->KFoverflow -= rc->KFoverflow_partial;
783          }          }
784    
785          rc->overflow += s->error = s->desired_length - data->length;          rc->overflow += (s->error = s->desired_length - data->length);
786          rc->real_total += data->length;          rc->real_total += data->length;
787    
788          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 832  Line 847 
847                  /* Read the stat line from buffer */                  /* Read the stat line from buffer */
848                  fields = sscanf(ptr, "%c", &type);                  fields = sscanf(ptr, "%c", &type);
849    
850                  /* Valid stats files have at least 6 fields */                  /* Valid stats files have at least 7 fields */
851                  if (fields == 1) {                  if (fields == 1) {
852                          switch(type) {                          switch(type) {
853                          case 'i':                          case 'i':
# Line 854  Line 869 
869                  } else {                  } else {
870                                  DPRINTF(XVID_DEBUG_RC,                                  DPRINTF(XVID_DEBUG_RC,
871                                                  "[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n",                                                  "[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n",
872                                                  lines, 6-fields);                                                  lines, 7-fields);
873                  }                  }
874    
875                  /* Free the line buffer */                  /* Free the line buffer */
# Line 903  Line 918 
918    
919                  /* Convert the fields */                  /* Convert the fields */
920                  fields = sscanf(ptr,                  fields = sscanf(ptr,
921                                                  "%c %d %d %d %d %d %d\n",                                                  "%c %d %d %d %d %d %d %d\n",
922                                                  &type,                                                  &type,
923                                                  &s->quant,                                                  &s->quant,
924                                                  &s->blks[0], &s->blks[1], &s->blks[2],                                                  &s->blks[0], &s->blks[1], &s->blks[2],
925                                                  &s->length,                                                  &s->length, &s->invariant /* not really yet */,
926                                                  &s->scaled_length);                                                  &s->scaled_length);
927    
928                  /* Free line buffer, we don't need it anymore */                  /* Free line buffer, we don't need it anymore */
# Line 915  Line 930 
930    
931                  /* Fail silently, this has probably been warned in                  /* Fail silently, this has probably been warned in
932                   * statsfile_count_frames */                   * statsfile_count_frames */
933                  if(fields != 6 && fields != 7)                  if(fields != 7 && fields != 8)
934                          continue;                          continue;
935    
936                  /* Convert frame type */                  /* Convert frame type and compute the invariant length part */
937                  switch(type) {                  switch(type) {
938                  case 'i':                  case 'i':
939                  case 'I':                  case 'I':
940                          s->type = XVID_TYPE_IVOP;                          s->type = XVID_TYPE_IVOP;
941                            s->invariant /= INVARIANT_HEADER_PART_IVOP;
942                          break;                          break;
943                  case 'p':                  case 'p':
944                  case 'P':                  case 'P':
945                  case 's':                  case 's':
946                  case 'S':                  case 'S':
947                          s->type = XVID_TYPE_PVOP;                          s->type = XVID_TYPE_PVOP;
948                            s->invariant /= INVARIANT_HEADER_PART_PVOP;
949                          break;                          break;
950                  case 'b':                  case 'b':
951                  case 'B':                  case 'B':
952                          s->type = XVID_TYPE_BVOP;                          s->type = XVID_TYPE_BVOP;
953                            s->invariant /= INVARIANT_HEADER_PART_BVOP;
954                          break;                          break;
955                  default:                  default:
956                          /* Same as before, fail silently */                          /* Same as before, fail silently */
# Line 963  Line 981 
981          for (i=0; i<3; i++) {          for (i=0; i<3; i++) {
982                  rc->count[i]=0;                  rc->count[i]=0;
983                  rc->tot_length[i] = 0;                  rc->tot_length[i] = 0;
984                    rc->tot_invariant[i] = 0;
985                  rc->min_length[i] = INT_MAX;                  rc->min_length[i] = INT_MAX;
986          }          }
987    
# Line 975  Line 994 
994    
995                  rc->count[s->type-1]++;                  rc->count[s->type-1]++;
996                  rc->tot_length[s->type-1] += s->length;                  rc->tot_length[s->type-1] += s->length;
997                    rc->tot_invariant[s->type-1] += s->invariant;
998    
999                  if (s->length < rc->min_length[s->type-1]) {                  if (s->length < rc->min_length[s->type-1]) {
1000                          rc->min_length[s->type-1] = s->length;                          rc->min_length[s->type-1] = s->length;
# Line 1011  Line 1031 
1031    
1032          rc->avg_weight = 0.0;          rc->avg_weight = 0.0;
1033          rc->tot_quant = 0;          rc->tot_quant = 0;
1034            rc->tot_quant_invariant = 0;
1035    
1036          if (create->num_zones == 0) {          if (create->num_zones == 0) {
1037                  for (j = 0; j < rc->num_frames; j++) {                  for (j = 0; j < rc->num_frames; j++) {
# Line 1049  Line 1069 
1069                                  rc->stats[j].zone_mode = XVID_ZONE_QUANT;                                  rc->stats[j].zone_mode = XVID_ZONE_QUANT;
1070                                  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;
1071                                  rc->tot_quant += rc->stats[j].length;                                  rc->tot_quant += rc->stats[j].length;
1072                                    rc->tot_quant_invariant += rc->stats[j].invariant;
1073                          }                          }
1074                  }                  }
1075          }          }
# Line 1064  Line 1085 
1085  {  {
1086          int64_t target;          int64_t target;
1087          int64_t pass1_length;          int64_t pass1_length;
1088            int64_t total_invariant;
1089          double scaler;          double scaler;
1090          int i, num_MBs;          int i, num_MBs;
1091    
1092          /* We remove the bytes used by the fixed quantizer zones          /* We only scale texture data ! */
1093           * ToDo: this approach is flawed, the same amount of bytes is removed from          total_invariant  = rc->tot_invariant[XVID_TYPE_IVOP-1];
1094           *       target and first pass data, this has no sense, zone_process should          total_invariant += rc->tot_invariant[XVID_TYPE_PVOP-1];
1095           *       give us two results one for unscaled data (1pass) and the other          total_invariant += rc->tot_invariant[XVID_TYPE_BVOP-1];
1096           *       one for scaled data and we should then write:          /* don't forget to substract header bytes used in quant zones, otherwise we
1097           *       target = rc->target - rc->tot_quant_scaled;           * counting them twice */
1098           *       pass1_length = rc->i+p+b - rc->tot_quant_firstpass */          total_invariant -= rc->tot_quant_invariant;
1099          target = rc->target - rc->tot_quant;  
1100            /* We remove the bytes used by the fixed quantizer zones during first pass
1101             * with the same quants, so we know very precisely how much that
1102             * represents */
1103            target  = rc->target;
1104            target -= rc->tot_quant;
1105    
1106          /* Do the same for the first pass data */          /* Do the same for the first pass data */
1107          pass1_length  = rc->tot_length[XVID_TYPE_IVOP-1];          pass1_length  = rc->tot_length[XVID_TYPE_IVOP-1];
# Line 1083  Line 1110 
1110          pass1_length -= rc->tot_quant;          pass1_length -= rc->tot_quant;
1111    
1112          /* Let's compute a linear scaler in order to perform curve scaling */          /* Let's compute a linear scaler in order to perform curve scaling */
1113          scaler = (double)target / (double)pass1_length;          scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant);
1114    
1115          if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {          if ((target - total_invariant) <= 0 ||
1116                    (pass1_length - total_invariant) <= 0 ||
1117                    target >= pass1_length) {
1118                  DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n");                  DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n");
1119                  scaler = 1.0;                  scaler = 1.0;
1120          }          }
# Line 1130  Line 1159 
1159                          continue;                          continue;
1160                  }                  }
1161    
1162                  /* Compute the scaled length */                  /* Compute the scaled length -- only non invariant data length is scaled */
1163                  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);
1164    
1165                  /* Compare with the computed minimum */                  /* Compare with the computed minimum */
1166                  if (len < rc->min_length[s->type-1]) {                  if (len < rc->min_length[s->type-1]) {
# Line 1154  Line 1183 
1183           * total counters. Now, it's possible to scale the 'regular' frames. */           * total counters. Now, it's possible to scale the 'regular' frames. */
1184    
1185          /* Scaling factor for 'regular' frames */          /* Scaling factor for 'regular' frames */
1186          scaler = (double)target / (double)pass1_length;          scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant);
1187    
1188          /* Detect undersizing */          /* Detect undersizing */
1189          if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {          if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {
# Line 1168  Line 1197 
1197    
1198                  /* Ignore frame with forced frame sizes */                  /* Ignore frame with forced frame sizes */
1199                  if (s->scaled_length == 0)                  if (s->scaled_length == 0)
1200                          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);
1201          }          }
1202    
1203          /* Job done */          /* Job done */

Legend:
Removed from v.1.1.2.30  
changed lines
  Added in v.1.1.2.31

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