--- plugin_2pass2.c 2004/01/07 13:51:24 1.1.2.34 +++ plugin_2pass2.c 2010/11/16 14:42:07 1.10 @@ -1,15 +1,15 @@ /****************************************************************************** * - * XviD Bit Rate Controller Library + * Xvid Bit Rate Controller Library * - VBR 2 pass bitrate controller implementation - * - * Copyright (C) 2002 Foxer + * Copyright (C) 2002 Benjamin Lambert * 2002 Dirk Knop * 2002-2003 Edouard Gomez * 2003 Pete Ross * * This curve treatment algorithm is the one originally implemented by Foxer - * and tuned by Dirk Knop for the XviD vfw frontend. + * and tuned by Dirk Knop for the Xvid vfw frontend. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -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.34 2004/01/07 13:51:24 edgomez Exp $ + * $Id: plugin_2pass2.c,v 1.10 2010/11/16 14:42:07 Isibaar Exp $ * *****************************************************************************/ @@ -35,6 +35,10 @@ /* forces second pass not to be bigger than first */ #undef PASS_SMALLER +/* automatically alters overflow controls (strength and improvement/degradation) + to fight most common problems without user's knowladge */ +#define SMART_OVERFLOW_SETTING + #include #include #include @@ -173,7 +177,7 @@ twopass_stat_t * stats; /*---------------------------------- - * Histerysis helpers + * Hysteresis helpers *--------------------------------*/ /* This field holds the int2float conversion errors of each quant per @@ -208,11 +212,15 @@ * ToDo: description */ double fq_error; + int min_quant; /* internal minimal quant, prevents wrong quants from being used */ + /*---------------------------------- * Debug *--------------------------------*/ double desired_total; double real_total; + + int scaled_frames; } rc_2pass2_t; @@ -264,6 +272,8 @@ 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); +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); #if 0 static void stats_print(rc_2pass2_t * rc); #endif @@ -282,7 +292,31 @@ if (rc == NULL) return XVID_ERR_MEMORY; - rc->param = *param; + /* v1.0.x */ + rc->param.version = param->version; + rc->param.bitrate = param->bitrate; + rc->param.filename = param->filename; + rc->param.keyframe_boost = param->keyframe_boost; + rc->param.curve_compression_high = param->curve_compression_high; + rc->param.curve_compression_low = param->curve_compression_low; + rc->param.overflow_control_strength = param->overflow_control_strength; + rc->param.max_overflow_improvement = param->max_overflow_improvement; + rc->param.max_overflow_degradation = param->max_overflow_degradation; + rc->param.kfreduction = param->kfreduction; + rc->param.kfthreshold = param->kfthreshold; + rc->param.container_frame_overhead = param->container_frame_overhead; + + if (XVID_VERSION_MINOR(param->version) >= 1) { + rc->param.vbv_size = param->vbv_size; + rc->param.vbv_initial = param->vbv_initial; + rc->param.vbv_maxrate = param->vbv_maxrate; + rc->param.vbv_peakrate = param->vbv_peakrate; + }else{ + rc->param.vbv_size = + rc->param.vbv_initial = + rc->param.vbv_maxrate = + rc->param.vbv_peakrate = 0; + } /* Initialize all defaults */ #define _INIT(a, b) if((a) <= 0) (a) = (b) @@ -311,6 +345,8 @@ for (i=0; i<3; i++) rc->last_quant[i] = 0; rc->fq_error = 0; + rc->min_quant = 1; + rc->scaled_frames = 0; /* Count frames (and intra frames) in the stats file, store the result into * the rc structure */ @@ -419,6 +455,33 @@ * shape the curve in the BEFORE/AFTER pair of functions */ scaled_curve_apply_advanced_parameters(rc); + /* Check curve for VBV compliancy and rescale if necessary */ +#ifdef VBV_FORCE + if (rc->param.vbv_size==0) { + rc->param.vbv_size = 3145728; + rc->param.vbv_initial = 2359296; + rc->param.vbv_maxrate = 4854000; + rc->param.vbv_peakrate = 8000000; + } +#endif + + /* vbv_size==0 switches VBV check off */ + if (rc->param.vbv_size > 0) { + const double fps = (double)create->fbase/(double)create->fincr; + int status = check_curve_for_vbv_compliancy(rc, fps); + + if (status) { + DPRINTF(XVID_DEBUG_RC, "[xvid rc] Underflow detected - Scaling Curve for compliancy.\n"); + } + + status = scale_curve_for_vbv_compliancy(rc, fps); + + if (status == 0) { + DPRINTF(XVID_DEBUG_RC, "[xvid rc] VBV compliant curve scaling done.\n"); + } else { + DPRINTF(XVID_DEBUG_RC, "[xvid rc] VBV compliant curve scaling impossible.\n"); + } + } *handle = rc; return(0); } @@ -617,18 +680,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); } /*------------------------------------------------------------------------ @@ -713,6 +770,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) */ @@ -738,8 +797,10 @@ rc->last_quant[s->type-1] = data->quant; /* Don't forget to force 1st pass frame type ;-) */ - data->type = s->type; + if (rc->scaled_frames) + data->type = s->type; + rc->scaled_frames++; return 0; } @@ -801,7 +862,7 @@ rc->KFoverflow -= rc->KFoverflow_partial; } - rc->overflow += (s->error = s->desired_length - data->length); + 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", @@ -898,7 +959,8 @@ /* We are done with the file */ fclose(f); - return(0); + if (!rc->num_keyframes) return (-1); /* No keyframes? Then something is wrong */ + else return(0); } /* open stats file(s) and read into rc->stats array */ @@ -1125,6 +1187,20 @@ /* Let's compute a linear scaler in order to perform curve scaling */ scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); +#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. @@ -1364,6 +1440,300 @@ } /***************************************************************************** + * 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 1 second + * + * But if VBV is fulfilled, peakrate is automatically fulfilled in any profile + * defined 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 1s) 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 in bytes for convenience. + * + * typical values from e.g. Home profile: + * vbv_size= 384*1024 (384kB) + * vbv_initial= 288*1024 (75% fill) + * maxrate= 4854000 (4.854MBps) + * peakrate= 8000000 (8MBps) + * + * PAL: offset1s = 25 (1 second of 25fps) + * NTSC: offset1s = 30 (1 second of 29.97fps) or 24 (1 second of 23.976fps) + */ + + const float vbv_size = (float)rc->param.vbv_size/8.f; + float vbvfill = (float)rc->param.vbv_initial/8.f; + float vbvmin; + + 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 bytes1s = 0; + int offset1s = (int)(1.f*fps+0.5); + int i; + + /* 1Gbit should be enough to inuitialize the vbvmin + * an arbitrary high value */ + vbvmin = 1000*1000*1000; + + for (i=0; inum_frames; i++) { + /* DivX 1s peak bitrate check */ + bytes1s += rc->stats[i].scaled_length; + if (i>=offset1s) + bytes1s -= rc->stats[i-offset1s].scaled_length; + + /* ignore peakrate constraint if peakrate is <= 0.f */ + if (peakrate>0.f && 8.f*bytes1s > 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 > vbv_size) + vbvfill = vbv_size; + + /* but THIS would be an underflow. report it! */ + if (vbvfill < 0) + return(VBV_UNDERFLOW); + + /* Store the minimum buffer filling */ + if (vbvfill < vbvmin) + vbvmin = vbvfill; + } + + DPRINTF(XVID_DEBUG_RC, "[xvid rc] Minimum buffer fill: %f bytes\n", vbvmin); + + return(VBV_COMPLIANT); +} + + +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... + * + * TODO: Better control on VBVfill between scenes */ + + const float vbv_size = (float)rc->param.vbv_size/8.f; + const float vbv_initial = (float)rc->param.vbv_initial/8.f; + + const float maxrate = 0.9*rc->param.vbv_maxrate; + const float vbv_low = 0.10f*vbv_size; + 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 = (int)(fps+0.5); + 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) + (vbv_initial - vbv_low)) / Skn; + if (f < minf) + minf = f; + + for (k=1;k<=n;k++) { + Skn -= frames[k].scaled_length; + + f = (R(k,n-1) + (vbv_size - vbv_low)) / Skn; + if (f < minf) + minf = f; + } + } + + /* special case: at the end, fill buffer up to vbv_initial 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) + (vbv_initial - vbv_low)) / Skn; + if (f < minf) + minf = f; + } + + DPRINTF(XVID_DEBUG_RC, "[xvid rc] Scene %d (Frames %d-%d): VBVfactor %f\n", + l, start, start+length-1 , minf); + + 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*/ + float f_red; + int l; + + /* check how much is wrong */ + for (l=0;lstats[start]; + + /* exactly 1 means "don't touch this anymore!" */ + if (scenefactor[l] == 1.) + continue; + + /* within limits */ + if (scenefactor[l] > 1.) { + 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; + } + } + + /* no more underflows */ + if (S_red < 1.f) + break; + + if (S_elig < 1.f) { + DPRINTF(XVID_DEBUG_RC, "[xvid rc] Everything underflowing.\n"); + free(scenefactor); + free(scenestart); + free(scenelength); + return(-2); + } + + f_red = (1.f + S_red/S_elig); + + DPRINTF(XVID_DEBUG_RC, "[xvid rc] Moving %.0f kB to avoid buffer underflow, correction factor: %.5f\n", + S_red/1024.f, f_red); + + violation=0; + /* scale remaining scenes up to meet total size */ + 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); +} + + +/***************************************************************************** * Still more low level stuff (nothing to do with stats treatment) ****************************************************************************/