--- plugin_2pass2.c 2003/12/21 12:41:48 1.1.2.32 +++ plugin_2pass2.c 2004/01/22 20:54:31 1.1.2.36 @@ -25,7 +25,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: plugin_2pass2.c,v 1.1.2.32 2003/12/21 12:41:48 syskin Exp $ + * $Id: plugin_2pass2.c,v 1.1.2.36 2004/01/22 20:54:31 edgomez Exp $ * *****************************************************************************/ @@ -130,18 +130,16 @@ /*---------------------------------- * Zones statistical data - * - * ToDo: Fix zones, current - * implementation is buggy *--------------------------------*/ - /* Average weight of the zones */ - double avg_weight; - /* Total length used by XVID_ZONE_QUANT zones */ uint64_t tot_quant; uint64_t tot_quant_invariant; + /* Holds the total amount of frame bytes, zone weighted (only scalable + * part of frame bytes) */ + uint64_t tot_weighted; + /*---------------------------------- * Advanced settings helper ratios *--------------------------------*/ @@ -375,6 +373,27 @@ if(rc->param.container_frame_overhead) DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- New target filesize after container compensation: %lld\n", rc->target); + /* When bitrate is not given it means it has been scaled by an external + * application */ + if (rc->param.bitrate) { + /* Apply zone settings + * - set rc->tot_quant which represents the total num of bytes spent in + * fixed quant zones + * - set rc->tot_weighted which represents the total amount of bytes + * spent in normal or weighted zones in first pass (normal zones can + * be considered weight=1) + * - set rc->tot_quant_invariant which represents the total num of bytes + * spent in fixed quant zones for headers */ + zone_process(rc, create); + } else { + /* External scaling -- zones are ignored */ + for (i=0;inum_frames;i++) { + rc->stats[i].zone_mode = XVID_ZONE_WEIGHT; + rc->stats[i].weight = 1.0; + } + rc->tot_quant = 0; + } + /* Gathers some information about first pass stats: * - finds the minimum frame length for each frame type during 1st pass. * rc->min_size[] @@ -390,25 +409,10 @@ */ first_pass_stats_prepare_data(rc); - /* When bitrate is not given it means it has been scaled by an external - * application */ + /* If we have a user bitrate, it means it's an internal curve scaling */ if (rc->param.bitrate) { - /* Apply zone settings - * - set rc->tot_quant which represents the total num of bytes spent in - * fixed quant zones - * - set rc->tot_quant_invariant which represents the total num of bytes spent - * in fixed quant zones for headers */ - zone_process(rc, create); /* Perform internal curve scaling */ first_pass_scale_curve_internal(rc); - } else { - /* External scaling -- zones are ignored */ - for (i=0;inum_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; } /* Apply advanced curve options, and compute some parameters in order to @@ -457,19 +461,28 @@ if (data->quant > 0) return(0); - /* Second case: insufficent stats data */ + /* Second case: insufficent stats data + * We can't guess much what we should do, let core decide all alone */ if (data->frame_num >= rc->num_frames) { DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)", data->frame_num); return(0); } - /* Third case: We are in a Quant zone */ + /* Third case: We are in a Quant zone + * Quant zones must just ensure we use the same settings as first pass + * So set the quantizer and the type */ if (s->zone_mode == XVID_ZONE_QUANT) { + /* Quant stuff */ rc->fq_error += s->weight; data->quant = (int)rc->fq_error; rc->fq_error -= data->quant; + /* The type stuff */ + data->type = s->type; + + /* The only required data for AFTER step is this one for the overflow + * control */ s->desired_length = s->length; return(0); @@ -604,18 +617,12 @@ #ifdef PASS_SMALLER if (dbytes > s->length) { dbytes = s->length; - } else + } #endif - if (dbytes < rc->min_length[s->type-1]) { + + /* Prevent stupid desired sizes under logical values */ + if (dbytes < rc->min_length[s->type-1]) { dbytes = rc->min_length[s->type-1]; - } else if (dbytes > rc->max_length) { - /* ToDo: this condition is always wrong as max_length == maximum frame - * length of first pass, so the first condition already caps the frame - * size... */ - capped_to_max_framesize = 1; - dbytes = rc->max_length; - DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- frame:%d Capped to maximum frame size\n", - data->frame_num); } /*------------------------------------------------------------------------ @@ -975,7 +982,7 @@ /* pre-process the statistics data * - for each type, count, tot_length, min_length, max_length - * - set keyframes_locations */ + * - set keyframes_locations, tot_prescaled */ static void first_pass_stats_prepare_data(rc_2pass2_t * rc) { @@ -992,6 +999,7 @@ } rc->max_length = INT_MIN; + rc->tot_weighted = 0; /* Loop through all frames and find/compute all the stuff this function * is supposed to do */ @@ -1001,6 +1009,8 @@ rc->count[s->type-1]++; rc->tot_length[s->type-1] += s->length; rc->tot_invariant[s->type-1] += s->invariant; + if (s->zone_mode != XVID_ZONE_QUANT) + rc->tot_weighted += (int)(s->weight*(s->length - s->invariant)); if (s->length < rc->min_length[s->type-1]) { rc->min_length[s->type-1] = s->length; @@ -1035,7 +1045,6 @@ int i,j; int n = 0; - rc->avg_weight = 0.0; rc->tot_quant = 0; rc->tot_quant_invariant = 0; @@ -1044,7 +1053,6 @@ 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; } @@ -1053,12 +1061,16 @@ int next = (i+1num_zones) ? create->zones[i+1].frame : rc->num_frames; + /* Zero weight make no sense */ + if (create->zones[i].increment == 0) create->zones[i].increment = 1; + /* And obviously an undetermined infinite makes even less sense */ + if (create->zones[i].base == 0) create->zones[i].base = 1; + if (i==0 && create->zones[i].frame > 0) { for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) { rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; rc->stats[j].weight = 1.0; } - rc->avg_weight += create->zones[i].frame * 1.0; n += create->zones[i].frame; } @@ -1068,7 +1080,6 @@ rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; } next -= create->zones[i].frame; - rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base; n += next; } else{ /* XVID_ZONE_QUANT */ for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { @@ -1079,9 +1090,6 @@ } } } - rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0; - - DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- center_weight:%f (for %d frames) fixed_bytes:%d\n", rc->avg_weight, n, rc->tot_quant); } @@ -1090,7 +1098,6 @@ first_pass_scale_curve_internal(rc_2pass2_t *rc) { int64_t target; - int64_t pass1_length; int64_t total_invariant; double scaler; int i, num_MBs; @@ -1109,23 +1116,9 @@ target = rc->target; target -= rc->tot_quant; - /* Do the same for the first pass data */ - pass1_length = rc->tot_length[XVID_TYPE_IVOP-1]; - pass1_length += rc->tot_length[XVID_TYPE_PVOP-1]; - pass1_length += rc->tot_length[XVID_TYPE_BVOP-1]; - pass1_length -= rc->tot_quant; - /* Let's compute a linear scaler in order to perform curve scaling */ - scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); + scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); -#ifdef PASS_SMALLER - if ((target - total_invariant) <= 0 || - (pass1_length - total_invariant) <= 0 || - target >= pass1_length) { - DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n"); - scaler = 1.0; - } -#endif /* Compute min frame lengths (for each frame type) according to the number * of MBs. We sum all block type counters of frame 0, this gives us the * number of MBs. @@ -1167,7 +1160,7 @@ } /* Compute the scaled length -- only non invariant data length is scaled */ - len = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight / rc->avg_weight); + len = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight); /* Compare with the computed minimum */ if (len < rc->min_length[s->type-1]) { @@ -1179,7 +1172,6 @@ * total counters, as we prepare a second pass for 'regular' * frames */ target -= s->scaled_length; - pass1_length -= s->length; } else { /* Do nothing for now, we'll scale this later */ s->scaled_length = 0; @@ -1190,15 +1182,7 @@ * total counters. Now, it's possible to scale the 'regular' frames. */ /* Scaling factor for 'regular' frames */ - scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); - -#ifdef PASS_SMALLER - /* Detect undersizing */ - if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { - DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected after correction\n"); - scaler = 1.0; - } -#endif + scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); /* Do another pass with the new scaler */ for (i=0; inum_frames; i++) { @@ -1206,7 +1190,7 @@ /* Ignore frame with forced frame sizes */ if (s->scaled_length == 0) - s->scaled_length = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight / rc->avg_weight); + s->scaled_length = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight); } /* Job done */