--- plugin_2pass2.c 2003/12/21 12:41:48 1.1.2.32 +++ plugin_2pass2.c 2004/05/09 14:00:35 1.1.2.38 @@ -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.38 2004/05/09 14:00:35 chl Exp $ * *****************************************************************************/ @@ -35,6 +35,10 @@ /* forces second pass not to be bigger than first */ #undef PASS_SMALLER +/* automtically alters overflow controls (strength and improvement/degradation) + to fight most common problems without user's knowladge */ +#define SMART_OVERFLOW_SETTING + #include #include #include @@ -130,18 +134,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 *--------------------------------*/ @@ -175,7 +177,7 @@ twopass_stat_t * stats; /*---------------------------------- - * Histerysis helpers + * Hysteresis helpers *--------------------------------*/ /* This field holds the int2float conversion errors of each quant per @@ -210,6 +212,8 @@ * ToDo: description */ double fq_error; + int min_quant; /* internal minimal quant, prevents wrong quants from being used */ + /*---------------------------------- * Debug *--------------------------------*/ @@ -266,6 +270,11 @@ static void first_pass_stats_prepare_data(rc_2pass2_t * rc); static void first_pass_scale_curve_internal(rc_2pass2_t *rc); static void scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc); +#ifdef VBV +static int check_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps); +static int scale_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps); +#endif + #if 0 static void stats_print(rc_2pass2_t * rc); #endif @@ -313,6 +322,7 @@ for (i=0; i<3; i++) rc->last_quant[i] = 0; rc->fq_error = 0; + rc->min_quant = 1; /* Count frames (and intra frames) in the stats file, store the result into * the rc structure */ @@ -375,6 +385,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,31 +421,51 @@ */ 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 * shape the curve in the BEFORE/AFTER pair of functions */ scaled_curve_apply_advanced_parameters(rc); + +#ifdef VBV +/* Check curve for VBV compliancy and rescale if necessary */ + + +#ifdef VBV_FORCE + if (rc->param.vbvsize==0) + { + rc->param.vbvsize = 3145728; + rc->param.vbvinitial = 2359296; + rc->param.vbv_maxrate = 4000000; + rc->param.vbv_peakrate = 10000000; + } +#endif + + if (rc->param.vbvsize>0) /* vbvsize==0 switches VBV check off */ + { + const double fps = (double)create->fbase/(double)create->fincr; + int status = check_curve_for_vbv_compliancy(rc, fps); +#ifdef VBV_DEBUG + if (status) + fprintf(stderr,"underflow detected\n Scaling Curve for compliancy... "); +#endif + + status = scale_curve_for_vbv_compliancy(rc, fps); + +#ifdef VBV_DEBUG + if (status==0) + fprintf(stderr,"done.\n"); + else + fprintf(stderr,"impossible.\n"); +#endif + } +#endif + *handle = rc; return(0); } @@ -457,19 +508,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 +664,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); } /*------------------------------------------------------------------------ @@ -700,6 +754,8 @@ data->quant = data->max_quant[s->type-1]; } + if (data->quant < rc->min_quant) data->quant = rc->min_quant; + /* To avoid big quality jumps from frame to frame, we apply a "security" * rule that makes |last_quant - new_quant| <= 2. This rule only applies * to predicted frames (P and B) */ @@ -975,7 +1031,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 +1048,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 +1058,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 +1094,6 @@ int i,j; int n = 0; - rc->avg_weight = 0.0; rc->tot_quant = 0; rc->tot_quant_invariant = 0; @@ -1044,7 +1102,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 +1110,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 +1129,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 +1139,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 +1147,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 +1165,23 @@ 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; +#ifdef SMART_OVERFLOW_SETTING + if (scaler > 0.9) { + rc->param.max_overflow_degradation *= 5; + rc->param.max_overflow_improvement *= 5; + rc->param.overflow_control_strength *= 3; + } else if (scaler > 0.6) { + rc->param.max_overflow_degradation *= 2; + rc->param.max_overflow_improvement *= 2; + rc->param.overflow_control_strength *= 2; + } else { + rc->min_quant = 2; } #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 +1223,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 +1235,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 +1245,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 +1253,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 */ @@ -1373,6 +1420,307 @@ return; } + +#ifdef VBV + +/***************************************************************************** + * VBV compliancy check and scale + * MPEG-4 standard specifies certain restrictions for bitrate/framesize in VBR + * to enable playback on devices with limited readspeed and memory (and which + * aren't...) + * + * DivX profiles have 2 criteria: VBV as in MPEG standard + * a limit on peak bitrate for any 3 seconds + * + * But if VBV is fulfilled, peakrate is automatically fulfilled in any profile + * define so far, so we check for it (for completeness) but correct only VBV + * + *****************************************************************************/ + +#define VBV_COMPLIANT 0 +#define VBV_UNDERFLOW 1 /* video buffer runs empty */ +#define VBV_OVERFLOW 2 /* doesn't exist for VBR encoding */ +#define VBV_PEAKRATE 4 /* peak bitrate (within 3s) violated */ + +static int check_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps) +{ +/* We do all calculations in float, for higher accuracy, and bytes for convenience + + typical values from DivX Home Theater profile: + vbvsize= 384*1024 (384kB), vbvinitial= 288*1024 (75% fill) + maxrate= 4000000 (4MBps), peakrate= 10000000 (10MBps) + + PAL: offset3s = 75 (3 seconds of 25fps) + NTSC: offset3s = 90 (3 seconds of 29.97fps) or 72 (3 seconds of 23.976fps) +*/ + + const float vbvsize = (float)rc->param.vbvsize/8.f; + float vbvfill = (float)rc->param.vbvinitial/8.f; + + const float maxrate = (float)rc->param.vbv_maxrate; + const float peakrate = (float)rc->param.vbv_peakrate; + const float r0 = (int)(maxrate/fps+0.5)/8.f; + + int bytes3s = 0; + int offset3s = (int)(3.f*fps+0.5); + + int i; + for (i=0; inum_frames; i++) { +/* DivX 3s peak bitrate check */ + + bytes3s += rc->stats[i].scaled_length; + if (i>=offset3s) + bytes3s -= rc->stats[i-offset3s].scaled_length; + + if (8.f*bytes3s > 3*peakrate) + return VBV_PEAKRATE; + +/* update vbv fill level */ + + vbvfill += r0 - rc->stats[i].scaled_length; + +/* this check is _NOT_ an "overflow"! only reading from disk stops then */ + if (vbvfill > vbvsize) + vbvfill = vbvsize; + +/* but THIS would be an underflow. report it! */ + if (vbvfill < 0) + return VBV_UNDERFLOW; + } + + return VBV_COMPLIANT; +} +/* idea: min(vbvfill) could be stored to print "minimum buffer fill" */ + + + +static int scale_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps) +{ +/* correct any VBV violations. Peak bitrate violations disappears + by this automatically + + This implementation follows + + Westerink, Rajagopalan, Gonzales "Two-pass MPEG-2 variable-bitrate encoding" + IBM J. RES. DEVELOP. VOL 43, No. 4, July 1999, p.471--488 + + Thanks, guys! This paper rocks!!! +*/ + +/* + For each scene of len N, we have to check up to N^2 possible buffer fills. + This works well with MPEG-2 where N==12 or so, but for MPEG-4 it's a + little slow... +*/ + const float vbvsize = (float)rc->param.vbvsize/8.f; + const float vbvinitial = (float)rc->param.vbvinitial/8.f; + + const float maxrate = 0.9*rc->param.vbv_maxrate; + const float vbvlow = 0.10f*vbvsize; + const float r0 = (int)(maxrate/fps+0.5)/8.f; + + int i,k,l,n,violation = 0; + float *scenefactor; + int *scenestart; + int *scenelength; + +/* first step: determine how many "scenes" there are and store their boundaries + we could get all this from existing keyframe_positions, somehow, but there we + don't have a min_scenelength, and it's no big deal to get it again. */ + + const int min_scenelength = 50; + int num_scenes = 0; + int last_scene = -999; + for (i=0; inum_frames; i++) { + if ( (rc->stats[i].type == XVID_TYPE_IVOP) && (i-last_scene>min_scenelength) ) + { + last_scene = i; + num_scenes++; + } + } + + scenefactor = (float*)malloc( num_scenes*sizeof(float) ); + scenestart = (int*)malloc( num_scenes*sizeof(int) ); + scenelength = (int*)malloc( num_scenes*sizeof(int) ); + + if ((!scenefactor) || (!scenestart) || (!scenelength) ) + { + free(scenefactor); + free(scenestart); + free(scenelength); + /* remember: free(0) is valid and does exactly nothing. */ + return -1; + } + +/* count again and safe the length/position */ + + num_scenes = 0; + last_scene = -999; + for (i=0; inum_frames; i++) { + if ( (rc->stats[i].type == XVID_TYPE_IVOP) && (i-last_scene>min_scenelength) ) + { + if (num_scenes>0) + scenelength[num_scenes-1]=i-last_scene; + scenestart[num_scenes]=i; + num_scenes++; + last_scene = i; + } + } + scenelength[num_scenes-1]=i-last_scene; + +/* second step: check for each scene, how much we can scale its frames up or down + such that the VBV restriction is just fulfilled +*/ + + +#define R(k,n) (((n)+1-(k))*r0) /* how much enters the buffer between frame k and n */ + for (l=0; lstats[start]; + + float S0n,Skn; + float f,minf = 99999.f; + + S0n=0.; + for (n=0;n<=length-1;n++) + { + S0n += frames[n].scaled_length; + + k=0; + Skn = S0n; + f = (R(k,n-1) + (vbvinitial - vbvlow)) / Skn; + if (f < minf) + minf = f; + + for (k=1;k<=n;k++) + { + Skn -= frames[k].scaled_length; + + f = (R(k,n-1) + (vbvsize - vbvlow)) / Skn; + if (f < minf) + minf = f; + } + } + + /* special case: at the end, fill buffer up to vbvinitial again + TODO: Allow other values for buffer fill between scenes + e.g. if n=N is smallest f-value, then check for better value */ + + n=length; + k=0; + Skn = S0n; + f = R(k,n-1)/Skn; + if (f < minf) + minf = f; + + for (k=1;k<=n-1;k++) + { + Skn -= frames[k].scaled_length; + + f = (R(k,n-1) + (vbvinitial - vbvlow)) / Skn; + if (f < minf) + minf = f; + } + +#ifdef VBV_DEBUG + printf("Scene %d (Frames %d-%d): VBVfactor %f\n", l, start, start+length-1 , minf); +#endif + + scenefactor[l] = minf; + } +#undef R + +/* last step: now we know of any scene how much it can be scaled up or down without + violating VBV. Next, distribute bits from the evil scenes to the good ones */ + + do + { + float S_red = 0.f; /* how much to redistribute */ + float S_elig = 0.f; /* sum of bit for those scenes you can still swallow something*/ + int l; + + for (l=0;lstats[start]; + + if (scenefactor[l] == 1.) /* exactly 1 means "don't touch this anymore!" */ + continue; + + if (scenefactor[l] > 1.) /* within limits */ + { + for (n= 0; n < length; n++) + S_elig += frames[n].scaled_length; + } + else /* underflowing segment */ + { + for (n= 0; n < length; n++) + { + float newbytes = (float)frames[n].scaled_length * scenefactor[l]; + S_red += (float)frames[n].scaled_length - (float)newbytes; + frames[n].scaled_length =(int)newbytes; + } + scenefactor[l] = 1.f; + } + } + + if (S_red < 1.f) /* no more underflows */ + break; + + if (S_elig < 1.f) + { +#ifdef VBV_DEBUG + fprintf(stderr,"Everything underflowing. \n"); +#endif + free(scenefactor); + free(scenestart); + free(scenelength); + return -2; + } + + const float f_red = (1.f + S_red/S_elig); + +#ifdef VBV_DEBUG + printf("Moving %.0f kB to avoid buffer underflow, correction factor: %.5f\n",S_red/1024.f,f_red); +#endif + + violation=0; + for (l=0; lstats[start]; + + if (scenefactor[l] == 1.) + continue; + + /* there shouldn't be any segments with factor<1 left, so all the rest is >1 */ + + for (n= 0; n < length; n++) + { + frames[n].scaled_length = (int)(frames[n].scaled_length * f_red + 0.5); + } + + scenefactor[l] /= f_red; + if (scenefactor[l] < 1.f) + violation=1; + } + + } while (violation); + + free(scenefactor); + free(scenestart); + free(scenelength); + return 0; +} + + +#endif + + /***************************************************************************** * Still more low level stuff (nothing to do with stats treatment) ****************************************************************************/