--- plugin_2pass2.c 2003/11/13 22:35:30 1.1.2.25 +++ plugin_2pass2.c 2004/01/22 20:54:31 1.1.2.36 @@ -25,12 +25,16 @@ * 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.25 2003/11/13 22:35:30 edgomez Exp $ + * $Id: plugin_2pass2.c,v 1.1.2.36 2004/01/22 20:54:31 edgomez Exp $ * *****************************************************************************/ +#define BQUANT_PRESCALE #undef COMPENSATE_FORMULA +/* forces second pass not to be bigger than first */ +#undef PASS_SMALLER + #include #include #include @@ -39,19 +43,29 @@ #include "../image/image.h" /***************************************************************************** - * Some constants + * Some default settings ****************************************************************************/ #define DEFAULT_KEYFRAME_BOOST 0 #define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10 #define DEFAULT_CURVE_COMPRESSION_HIGH 0 #define DEFAULT_CURVE_COMPRESSION_LOW 0 -#define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60 -#define DEFAULT_MAX_OVERFLOW_DEGRADATION 60 +#define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 10 +#define DEFAULT_MAX_OVERFLOW_DEGRADATION 10 /* Keyframe settings */ #define DEFAULT_KFREDUCTION 20 -#define DEFAULT_MIN_KEY_INTERVAL 1 +#define DEFAULT_KFTHRESHOLD 1 + +/***************************************************************************** + * Some default constants (can be tuned) + ****************************************************************************/ + +/* Specify the invariant part of the headers bits (header+MV) + * as hlength/cst */ +#define INVARIANT_HEADER_PART_IVOP 1 /* factor 1.0f */ +#define INVARIANT_HEADER_PART_PVOP 2 /* factor 0.5f */ +#define INVARIANT_HEADER_PART_BVOP 8 /* factor 0.125f */ /***************************************************************************** * Structures @@ -61,9 +75,9 @@ typedef struct { int type; /* first pass type */ int quant; /* first pass quant */ - int quant2; /* Second pass quant */ - int blks[3]; /* k,m,y blks */ + int blks[3]; /* k,m,y blks */ int length; /* first pass length */ + int invariant; /* what we assume as being invariant between the two passes, it's a sub part of header + MV bits */ int scaled_length; /* scaled length */ int desired_length; /* desired length; calculated during encoding */ int error; @@ -95,6 +109,7 @@ /* Total length of each frame types (1st pass) */ uint64_t tot_length[3]; + uint64_t tot_invariant[3]; /* Average length of each frame types (used first for 1st pass data and * then for scaled averages */ @@ -115,16 +130,15 @@ /*---------------------------------- * 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 */ - int64_t tot_quant; + 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 @@ -220,6 +234,7 @@ { switch(opt) { case XVID_PLG_INFO : + case XVID_PLG_FRAME : return 0; case XVID_PLG_CREATE : @@ -281,7 +296,7 @@ /* Keyframe settings */ _INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION); - _INIT(rc->param.min_key_interval, DEFAULT_MIN_KEY_INTERVAL); + _INIT(rc->param.kfthreshold, DEFAULT_KFTHRESHOLD); #undef _INIT /* Initialize some stuff to zero */ @@ -332,7 +347,7 @@ /* Compute the target filesize */ if (rc->param.bitrate<0) { /* if negative, bitrate equals the target (in kbytes) */ - rc->target = (-rc->param.bitrate) * 1024; + rc->target = ((uint64_t)(-rc->param.bitrate)) * 1024; } else if (rc->num_frames < create->fbase/create->fincr) { /* Source sequence is less than 1s long, we do as if it was 1s long */ rc->target = rc->param.bitrate / 8; @@ -358,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[] @@ -366,27 +402,17 @@ * - count how many times each frame type has been used. * rc->count[] * - total bytes used per frame type - * rc->total[] + * rc->tot_length[] + * - total bytes considered invariant between the 2 passes * - store keyframe location * rc->keyframe_locations[] */ 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 */ - 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 @@ -435,20 +461,33 @@ if (data->quant > 0) return(0); - /* Second case: We are in a Quant zone */ + /* 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 + * 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); } - /* Third case: insufficent stats data */ - if (data->frame_num >= rc->num_frames) - return(0); /*************************************************************************/ /*************************************************************************/ @@ -466,23 +505,11 @@ /* IFrame user settings*/ if (s->type == XVID_TYPE_IVOP) { - /* Keyframe boosting -- All keyframes benefit from it */ dbytes += dbytes*rc->param.keyframe_boost / 100; - /* Applies keyframe penalties, but not the first frame */ - if (rc->KF_idx) { - 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; - } +#if 0 /* ToDo: decide how to apply kfthresholding */ +#endif } else { /* P/S/B frames must reserve some bits for iframe boosting */ @@ -524,7 +551,7 @@ *-----------------------------------------------------------------------*/ /* Compute the overflow we should compensate */ - if (s->type != XVID_TYPE_IVOP) { + if (s->type != XVID_TYPE_IVOP || rc->overflow > 0) { double frametype_factor; double framesize_factor; @@ -555,7 +582,7 @@ /* Apply the overflow strength imposed by the user */ overflow *= (rc->param.overflow_control_strength/100.0f); } else { - /* no overflow applied in IFrames because: + /* no negative overflow applied in IFrames because: * - their role is important as they're references for P/BFrames. * - there aren't much in typical sequences, so if an IFrame overflows too * much, this overflow may impact the next IFrame too much and generate @@ -587,26 +614,23 @@ * pass nor smaller than the allowed minimum. *-----------------------------------------------------------------------*/ +#ifdef PASS_SMALLER if (dbytes > s->length) { dbytes = s->length; - } else if (dbytes < rc->min_length[s->type-1]) { + } +#endif + + /* 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); } /*------------------------------------------------------------------------ * Desired frame length <-> quantizer mapping *-----------------------------------------------------------------------*/ - /* For bframes we must retrieve the original quant used (sent to xvidcore) - * as core applies the bquant formula before writing the stat log entry */ +#ifdef BQUANT_PRESCALE + /* For bframes we prescale the quantizer to avoid too high quant scaling */ if(s->type == XVID_TYPE_BVOP) { twopass_stat_t *b_ref = s; @@ -616,12 +640,13 @@ b_ref--; /* Compute the original quant */ - s->quant = 100*s->quant - data->bquant_offset; - s->quant += data->bquant_ratio - 1; /* to avoid rouding issues */ + s->quant = 2*(100*s->quant - data->bquant_offset); + s->quant += data->bquant_ratio - 1; /* to avoid rounding issues */ s->quant = s->quant/data->bquant_ratio - b_ref->quant; } +#endif - /* Don't laugh at this very 'simple' quant<->filesize relationship, it + /* Don't laugh at this very 'simple' quant<->size relationship, it * proves to be acurate enough for our algorithm */ scaled_quant = (double)s->quant*(double)s->length/(double)dbytes; @@ -709,10 +734,6 @@ /* Don't forget to force 1st pass frame type ;-) */ data->type = s->type; - /* Store the quantizer into the statistics -- Used to compensate the double - * formula symptom */ - s->quant2 = data->quant; - return 0; } @@ -774,7 +795,7 @@ rc->KFoverflow -= rc->KFoverflow_partial; } - rc->overflow += s->error = s->desired_length - data->length; + rc->overflow += (s->error = s->desired_length - data->length); rc->real_total += data->length; 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", @@ -839,7 +860,7 @@ /* Read the stat line from buffer */ fields = sscanf(ptr, "%c", &type); - /* Valid stats files have at least 6 fields */ + /* Valid stats files have at least 7 fields */ if (fields == 1) { switch(type) { case 'i': @@ -861,7 +882,7 @@ } else { DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n", - lines, 6-fields); + lines, 7-fields); } /* Free the line buffer */ @@ -910,11 +931,11 @@ /* Convert the fields */ fields = sscanf(ptr, - "%c %d %d %d %d %d %d\n", + "%c %d %d %d %d %d %d %d\n", &type, &s->quant, &s->blks[0], &s->blks[1], &s->blks[2], - &s->length, + &s->length, &s->invariant /* not really yet */, &s->scaled_length); /* Free line buffer, we don't need it anymore */ @@ -922,24 +943,27 @@ /* Fail silently, this has probably been warned in * statsfile_count_frames */ - if(fields != 6 && fields != 7) + if(fields != 7 && fields != 8) continue; - /* Convert frame type */ + /* Convert frame type and compute the invariant length part */ switch(type) { case 'i': case 'I': s->type = XVID_TYPE_IVOP; + s->invariant /= INVARIANT_HEADER_PART_IVOP; break; case 'p': case 'P': case 's': case 'S': s->type = XVID_TYPE_PVOP; + s->invariant /= INVARIANT_HEADER_PART_PVOP; break; case 'b': case 'B': s->type = XVID_TYPE_BVOP; + s->invariant /= INVARIANT_HEADER_PART_BVOP; break; default: /* Same as before, fail silently */ @@ -958,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) { @@ -970,10 +994,12 @@ for (i=0; i<3; i++) { rc->count[i]=0; rc->tot_length[i] = 0; + rc->tot_invariant[i] = 0; rc->min_length[i] = INT_MAX; } 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 */ @@ -982,6 +1008,9 @@ 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; @@ -1016,16 +1045,14 @@ int i,j; int n = 0; - rc->avg_weight = 0.0; rc->tot_quant = 0; - + rc->tot_quant_invariant = 0; if (create->num_zones == 0) { for (j = 0; j < rc->num_frames; j++) { 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; } @@ -1034,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; } @@ -1049,19 +1080,16 @@ 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 */ + } else{ /* XVID_ZONE_QUANT */ for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { rc->stats[j].zone_mode = XVID_ZONE_QUANT; rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; rc->tot_quant += rc->stats[j].length; + rc->tot_quant_invariant += rc->stats[j].invariant; } } } - 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); } @@ -1070,32 +1098,26 @@ 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; - /* We remove the bytes used by the fixed quantizer zones - * ToDo: this approach is flawed, the same amount of bytes is removed from - * target and first pass data, this has no sense, zone_process should - * give us two results one for unscaled data (1pass) and the other - * one for scaled data and we should then write: - * target = rc->target - rc->tot_quant_scaled; - * pass1_length = rc->i+p+b - rc->tot_quant_firstpass */ - target = rc->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; + /* We only scale texture data ! */ + total_invariant = rc->tot_invariant[XVID_TYPE_IVOP-1]; + total_invariant += rc->tot_invariant[XVID_TYPE_PVOP-1]; + total_invariant += rc->tot_invariant[XVID_TYPE_BVOP-1]; + /* don't forget to substract header bytes used in quant zones, otherwise we + * counting them twice */ + total_invariant -= rc->tot_quant_invariant; + + /* We remove the bytes used by the fixed quantizer zones during first pass + * with the same quants, so we know very precisely how much that + * represents */ + target = rc->target; + target -= rc->tot_quant; /* Let's compute a linear scaler in order to perform curve scaling */ - scaler = (double)target / (double)pass1_length; - - if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { - DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n"); - scaler = 1.0; - } + scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); /* 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 @@ -1137,8 +1159,8 @@ continue; } - /* Compute the scaled length */ - len = (int)((double)s->length * scaler * s->weight / rc->avg_weight); + /* Compute the scaled length -- only non invariant data length is scaled */ + 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]) { @@ -1150,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; @@ -1161,13 +1182,7 @@ * total counters. Now, it's possible to scale the 'regular' frames. */ /* Scaling factor for 'regular' frames */ - scaler = (double)target / (double)pass1_length; - - /* 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; - } + scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); /* Do another pass with the new scaler */ for (i=0; inum_frames; i++) { @@ -1175,7 +1190,7 @@ /* Ignore frame with forced frame sizes */ if (s->scaled_length == 0) - 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); } /* Job done */ @@ -1190,7 +1205,7 @@ scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc) { int i; - uint64_t ivop_boost_total; + int64_t ivop_boost_total; /* Reset the rate controller (per frame type) total byte counters */ for (i=0; i<3; i++) rc->tot_scaled_length[i] = 0; @@ -1213,23 +1228,13 @@ /* Some more work is needed for I frames */ if (s->type == XVID_TYPE_IVOP) { - int penalty_distance; int ivop_boost; /* Accumulate bytes needed for keyframe boosting */ ivop_boost = s->scaled_length*rc->param.keyframe_boost/100; - if (rc->KF_idx) { - /* 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) - ivop_boost -= (s->scaled_length + ivop_boost)*penalty_distance*rc->param.kfreduction/100; - } - +#if 0 /* ToDo: decide how to apply kfthresholding */ +#endif /* If the frame size drops under the minimum length, then cap ivop_boost */ if (ivop_boost + s->scaled_length < rc->min_length[XVID_TYPE_IVOP-1]) ivop_boost = rc->min_length[XVID_TYPE_IVOP-1] - s->scaled_length; @@ -1259,7 +1264,7 @@ /* Compute the ratio described above * taxed_total = sum(0, n, tax*scaled_length) - * <=> taxed_total = tax.sum(0, n, tax*scaled_length) + * <=> taxed_total = tax.sum(0, n, scaled_length) * <=> tax = taxed_total / original_total */ rc->pb_iboost_tax_ratio = (rc->pb_iboost_tax_ratio - ivop_boost_total) /