25 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
27 |
* |
* |
28 |
* $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.30 2003-12-08 12:38:04 syskin Exp $ |
29 |
* |
* |
30 |
*****************************************************************************/ |
*****************************************************************************/ |
31 |
|
|
51 |
|
|
52 |
/* Keyframe settings */ |
/* Keyframe settings */ |
53 |
#define DEFAULT_KFREDUCTION 20 |
#define DEFAULT_KFREDUCTION 20 |
54 |
#define DEFAULT_MIN_KEY_INTERVAL 1 |
#define DEFAULT_KFTHRESHOLD 1 |
55 |
|
|
56 |
/***************************************************************************** |
/***************************************************************************** |
57 |
* Structures |
* Structures |
220 |
{ |
{ |
221 |
switch(opt) { |
switch(opt) { |
222 |
case XVID_PLG_INFO : |
case XVID_PLG_INFO : |
223 |
|
case XVID_PLG_FRAME : |
224 |
return 0; |
return 0; |
225 |
|
|
226 |
case XVID_PLG_CREATE : |
case XVID_PLG_CREATE : |
282 |
|
|
283 |
/* Keyframe settings */ |
/* Keyframe settings */ |
284 |
_INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION); |
_INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION); |
285 |
_INIT(rc->param.min_key_interval, DEFAULT_MIN_KEY_INTERVAL); |
_INIT(rc->param.kfthreshold, DEFAULT_KFTHRESHOLD); |
286 |
#undef _INIT |
#undef _INIT |
287 |
|
|
288 |
/* Initialize some stuff to zero */ |
/* Initialize some stuff to zero */ |
333 |
/* Compute the target filesize */ |
/* Compute the target filesize */ |
334 |
if (rc->param.bitrate<0) { |
if (rc->param.bitrate<0) { |
335 |
/* if negative, bitrate equals the target (in kbytes) */ |
/* if negative, bitrate equals the target (in kbytes) */ |
336 |
rc->target = (-rc->param.bitrate) * 1024; |
rc->target = ((uint64_t)(-rc->param.bitrate)) * 1024; |
337 |
} else if (rc->num_frames < create->fbase/create->fincr) { |
} else if (rc->num_frames < create->fbase/create->fincr) { |
338 |
/* Source sequence is less than 1s long, we do as if it was 1s long */ |
/* Source sequence is less than 1s long, we do as if it was 1s long */ |
339 |
rc->target = rc->param.bitrate / 8; |
rc->target = rc->param.bitrate / 8; |
436 |
if (data->quant > 0) |
if (data->quant > 0) |
437 |
return(0); |
return(0); |
438 |
|
|
439 |
/* Second case: We are in a Quant zone */ |
/* Second case: insufficent stats data */ |
440 |
|
if (data->frame_num >= rc->num_frames) { |
441 |
|
DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)", |
442 |
|
data->frame_num); |
443 |
|
return(0); |
444 |
|
} |
445 |
|
|
446 |
|
/* Third case: We are in a Quant zone */ |
447 |
if (s->zone_mode == XVID_ZONE_QUANT) { |
if (s->zone_mode == XVID_ZONE_QUANT) { |
448 |
rc->fq_error += s->weight; |
rc->fq_error += s->weight; |
449 |
data->quant = (int)rc->fq_error; |
data->quant = (int)rc->fq_error; |
454 |
return(0); |
return(0); |
455 |
} |
} |
456 |
|
|
|
/* Third case: insufficent stats data */ |
|
|
if (data->frame_num >= rc->num_frames) |
|
|
return(0); |
|
457 |
|
|
458 |
/*************************************************************************/ |
/*************************************************************************/ |
459 |
/*************************************************************************/ |
/*************************************************************************/ |
471 |
|
|
472 |
/* IFrame user settings*/ |
/* IFrame user settings*/ |
473 |
if (s->type == XVID_TYPE_IVOP) { |
if (s->type == XVID_TYPE_IVOP) { |
|
|
|
474 |
/* Keyframe boosting -- All keyframes benefit from it */ |
/* Keyframe boosting -- All keyframes benefit from it */ |
475 |
dbytes += dbytes*rc->param.keyframe_boost / 100; |
dbytes += dbytes*rc->param.keyframe_boost / 100; |
476 |
|
|
477 |
/* Applies keyframe penalties, but not the first frame */ |
#if 0 /* ToDo: decide how to apply kfthresholding */ |
478 |
if (rc->KF_idx) { |
#endif |
|
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; |
|
|
} |
|
479 |
} else { |
} else { |
480 |
|
|
481 |
/* P/S/B frames must reserve some bits for iframe boosting */ |
/* P/S/B frames must reserve some bits for iframe boosting */ |
609 |
b_ref--; |
b_ref--; |
610 |
|
|
611 |
/* Compute the original quant */ |
/* Compute the original quant */ |
612 |
s->quant = 100*s->quant - data->bquant_offset; |
s->quant = 2*(100*s->quant - data->bquant_offset); |
613 |
s->quant += data->bquant_ratio - 1; /* to avoid rouding issues */ |
s->quant += data->bquant_ratio - 1; /* to avoid rouding issues */ |
614 |
s->quant = s->quant/data->bquant_ratio - b_ref->quant; |
s->quant = s->quant/data->bquant_ratio - b_ref->quant; |
615 |
} |
} |
1183 |
scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc) |
scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc) |
1184 |
{ |
{ |
1185 |
int i; |
int i; |
1186 |
uint64_t ivop_boost_total; |
int64_t ivop_boost_total; |
1187 |
|
|
1188 |
/* Reset the rate controller (per frame type) total byte counters */ |
/* Reset the rate controller (per frame type) total byte counters */ |
1189 |
for (i=0; i<3; i++) rc->tot_scaled_length[i] = 0; |
for (i=0; i<3; i++) rc->tot_scaled_length[i] = 0; |
1206 |
|
|
1207 |
/* Some more work is needed for I frames */ |
/* Some more work is needed for I frames */ |
1208 |
if (s->type == XVID_TYPE_IVOP) { |
if (s->type == XVID_TYPE_IVOP) { |
|
int penalty_distance; |
|
1209 |
int ivop_boost; |
int ivop_boost; |
1210 |
|
|
1211 |
/* Accumulate bytes needed for keyframe boosting */ |
/* Accumulate bytes needed for keyframe boosting */ |
1212 |
ivop_boost = s->scaled_length*rc->param.keyframe_boost/100; |
ivop_boost = s->scaled_length*rc->param.keyframe_boost/100; |
1213 |
|
|
1214 |
if (rc->KF_idx) { |
#if 0 /* ToDo: decide how to apply kfthresholding */ |
1215 |
/* Minimum keyframe distance penalties */ |
#endif |
|
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; |
|
|
} |
|
|
|
|
1216 |
/* If the frame size drops under the minimum length, then cap ivop_boost */ |
/* If the frame size drops under the minimum length, then cap ivop_boost */ |
1217 |
if (ivop_boost + s->scaled_length < rc->min_length[XVID_TYPE_IVOP-1]) |
if (ivop_boost + s->scaled_length < rc->min_length[XVID_TYPE_IVOP-1]) |
1218 |
ivop_boost = rc->min_length[XVID_TYPE_IVOP-1] - s->scaled_length; |
ivop_boost = rc->min_length[XVID_TYPE_IVOP-1] - s->scaled_length; |
1242 |
|
|
1243 |
/* Compute the ratio described above |
/* Compute the ratio described above |
1244 |
* taxed_total = sum(0, n, tax*scaled_length) |
* taxed_total = sum(0, n, tax*scaled_length) |
1245 |
* <=> taxed_total = tax.sum(0, n, tax*scaled_length) |
* <=> taxed_total = tax.sum(0, n, scaled_length) |
1246 |
* <=> tax = taxed_total / original_total */ |
* <=> tax = taxed_total / original_total */ |
1247 |
rc->pb_iboost_tax_ratio = |
rc->pb_iboost_tax_ratio = |
1248 |
(rc->pb_iboost_tax_ratio - ivop_boost_total) / |
(rc->pb_iboost_tax_ratio - ivop_boost_total) / |