--- plugin_2pass2.c 2004/05/09 14:00:35 1.1.2.38 +++ plugin_2pass2.c 2004/06/11 08:44:30 1.4 @@ -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.38 2004/05/09 14:00:35 chl Exp $ + * $Id: plugin_2pass2.c,v 1.4 2004/06/11 08:44:30 suxen_drol Exp $ * *****************************************************************************/ @@ -270,11 +270,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); -#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 @@ -293,7 +290,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) @@ -431,22 +452,19 @@ * 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) + if (rc->param.vbv_size==0) { - rc->param.vbvsize = 3145728; - rc->param.vbvinitial = 2359296; + rc->param.vbv_size = 3145728; + rc->param.vbv_initial = 2359296; rc->param.vbv_maxrate = 4000000; rc->param.vbv_peakrate = 10000000; } #endif - if (rc->param.vbvsize>0) /* vbvsize==0 switches VBV check off */ + if (rc->param.vbv_size>0) /* vbv_size==0 switches VBV check off */ { const double fps = (double)create->fbase/(double)create->fincr; int status = check_curve_for_vbv_compliancy(rc, fps); @@ -464,8 +482,6 @@ fprintf(stderr,"impossible.\n"); #endif } -#endif - *handle = rc; return(0); } @@ -1420,9 +1436,6 @@ return; } - -#ifdef VBV - /***************************************************************************** * VBV compliancy check and scale * MPEG-4 standard specifies certain restrictions for bitrate/framesize in VBR @@ -1444,18 +1457,19 @@ 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 +/* We do all calculations in float, for higher accuracy, + and in bytes for convenience typical values from DivX Home Theater profile: - vbvsize= 384*1024 (384kB), vbvinitial= 288*1024 (75% fill) + vbv_size= 384*1024 (384kB), vbv_initial= 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 vbv_size = (float)rc->param.vbv_size/8.f; + float vbvfill = (float)rc->param.vbv_initial/8.f; const float maxrate = (float)rc->param.vbv_maxrate; const float peakrate = (float)rc->param.vbv_peakrate; @@ -1480,8 +1494,8 @@ vbvfill += r0 - rc->stats[i].scaled_length; /* this check is _NOT_ an "overflow"! only reading from disk stops then */ - if (vbvfill > vbvsize) - vbvfill = vbvsize; + if (vbvfill > vbv_size) + vbvfill = vbv_size; /* but THIS would be an underflow. report it! */ if (vbvfill < 0) @@ -1490,8 +1504,7 @@ return VBV_COMPLIANT; } -/* idea: min(vbvfill) could be stored to print "minimum buffer fill" */ - +/* TODO: store min(vbvfill) and print "minimum buffer fill" */ static int scale_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps) @@ -1511,12 +1524,15 @@ 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 vbvsize = (float)rc->param.vbvsize/8.f; - const float vbvinitial = (float)rc->param.vbvinitial/8.f; + + 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 vbvlow = 0.10f*vbvsize; + 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; @@ -1528,7 +1544,7 @@ 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; + const int min_scenelength = (int)(fps+0.5); int num_scenes = 0; int last_scene = -999; for (i=0; inum_frames; i++) { @@ -1590,7 +1606,7 @@ k=0; Skn = S0n; - f = (R(k,n-1) + (vbvinitial - vbvlow)) / Skn; + f = (R(k,n-1) + (vbv_initial - vbv_low)) / Skn; if (f < minf) minf = f; @@ -1598,13 +1614,13 @@ { Skn -= frames[k].scaled_length; - f = (R(k,n-1) + (vbvsize - vbvlow)) / Skn; + f = (R(k,n-1) + (vbv_size - vbv_low)) / Skn; if (f < minf) minf = f; } } - /* special case: at the end, fill buffer up to vbvinitial again + /* 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 */ @@ -1619,7 +1635,7 @@ { Skn -= frames[k].scaled_length; - f = (R(k,n-1) + (vbvinitial - vbvlow)) / Skn; + f = (R(k,n-1) + (vbv_initial - vbv_low)) / Skn; if (f < minf) minf = f; } @@ -1639,6 +1655,7 @@ { 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; for (l=0;l