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.31 2003-12-17 15:16:16 edgomez Exp $ |
* $Id: plugin_2pass2.c,v 1.1.2.36 2004-01-22 20:54:31 edgomez Exp $ |
29 |
* |
* |
30 |
*****************************************************************************/ |
*****************************************************************************/ |
31 |
|
|
32 |
#define BQUANT_PRESCALE |
#define BQUANT_PRESCALE |
33 |
#undef COMPENSATE_FORMULA |
#undef COMPENSATE_FORMULA |
34 |
|
|
35 |
|
/* forces second pass not to be bigger than first */ |
36 |
|
#undef PASS_SMALLER |
37 |
|
|
38 |
#include <stdio.h> |
#include <stdio.h> |
39 |
#include <math.h> |
#include <math.h> |
40 |
#include <limits.h> |
#include <limits.h> |
130 |
|
|
131 |
/*---------------------------------- |
/*---------------------------------- |
132 |
* Zones statistical data |
* Zones statistical data |
|
* |
|
|
* ToDo: Fix zones, current |
|
|
* implementation is buggy |
|
133 |
*--------------------------------*/ |
*--------------------------------*/ |
134 |
|
|
|
/* Average weight of the zones */ |
|
|
double avg_weight; |
|
|
|
|
135 |
/* Total length used by XVID_ZONE_QUANT zones */ |
/* Total length used by XVID_ZONE_QUANT zones */ |
136 |
uint64_t tot_quant; |
uint64_t tot_quant; |
137 |
uint64_t tot_quant_invariant; |
uint64_t tot_quant_invariant; |
138 |
|
|
139 |
|
/* Holds the total amount of frame bytes, zone weighted (only scalable |
140 |
|
* part of frame bytes) */ |
141 |
|
uint64_t tot_weighted; |
142 |
|
|
143 |
/*---------------------------------- |
/*---------------------------------- |
144 |
* Advanced settings helper ratios |
* Advanced settings helper ratios |
145 |
*--------------------------------*/ |
*--------------------------------*/ |
373 |
if(rc->param.container_frame_overhead) |
if(rc->param.container_frame_overhead) |
374 |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- New target filesize after container compensation: %lld\n", rc->target); |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- New target filesize after container compensation: %lld\n", rc->target); |
375 |
|
|
376 |
|
/* When bitrate is not given it means it has been scaled by an external |
377 |
|
* application */ |
378 |
|
if (rc->param.bitrate) { |
379 |
|
/* Apply zone settings |
380 |
|
* - set rc->tot_quant which represents the total num of bytes spent in |
381 |
|
* fixed quant zones |
382 |
|
* - set rc->tot_weighted which represents the total amount of bytes |
383 |
|
* spent in normal or weighted zones in first pass (normal zones can |
384 |
|
* be considered weight=1) |
385 |
|
* - set rc->tot_quant_invariant which represents the total num of bytes |
386 |
|
* spent in fixed quant zones for headers */ |
387 |
|
zone_process(rc, create); |
388 |
|
} else { |
389 |
|
/* External scaling -- zones are ignored */ |
390 |
|
for (i=0;i<rc->num_frames;i++) { |
391 |
|
rc->stats[i].zone_mode = XVID_ZONE_WEIGHT; |
392 |
|
rc->stats[i].weight = 1.0; |
393 |
|
} |
394 |
|
rc->tot_quant = 0; |
395 |
|
} |
396 |
|
|
397 |
/* Gathers some information about first pass stats: |
/* Gathers some information about first pass stats: |
398 |
* - finds the minimum frame length for each frame type during 1st pass. |
* - finds the minimum frame length for each frame type during 1st pass. |
399 |
* rc->min_size[] |
* rc->min_size[] |
409 |
*/ |
*/ |
410 |
first_pass_stats_prepare_data(rc); |
first_pass_stats_prepare_data(rc); |
411 |
|
|
412 |
/* When bitrate is not given it means it has been scaled by an external |
/* If we have a user bitrate, it means it's an internal curve scaling */ |
|
* application */ |
|
413 |
if (rc->param.bitrate) { |
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); |
|
414 |
/* Perform internal curve scaling */ |
/* Perform internal curve scaling */ |
415 |
first_pass_scale_curve_internal(rc); |
first_pass_scale_curve_internal(rc); |
|
} else { |
|
|
/* External scaling -- zones are ignored */ |
|
|
for (i=0;i<rc->num_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; |
|
416 |
} |
} |
417 |
|
|
418 |
/* Apply advanced curve options, and compute some parameters in order to |
/* Apply advanced curve options, and compute some parameters in order to |
461 |
if (data->quant > 0) |
if (data->quant > 0) |
462 |
return(0); |
return(0); |
463 |
|
|
464 |
/* Second case: insufficent stats data */ |
/* Second case: insufficent stats data |
465 |
|
* We can't guess much what we should do, let core decide all alone */ |
466 |
if (data->frame_num >= rc->num_frames) { |
if (data->frame_num >= rc->num_frames) { |
467 |
DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)", |
DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)", |
468 |
data->frame_num); |
data->frame_num); |
469 |
return(0); |
return(0); |
470 |
} |
} |
471 |
|
|
472 |
/* Third case: We are in a Quant zone */ |
/* Third case: We are in a Quant zone |
473 |
|
* Quant zones must just ensure we use the same settings as first pass |
474 |
|
* So set the quantizer and the type */ |
475 |
if (s->zone_mode == XVID_ZONE_QUANT) { |
if (s->zone_mode == XVID_ZONE_QUANT) { |
476 |
|
/* Quant stuff */ |
477 |
rc->fq_error += s->weight; |
rc->fq_error += s->weight; |
478 |
data->quant = (int)rc->fq_error; |
data->quant = (int)rc->fq_error; |
479 |
rc->fq_error -= data->quant; |
rc->fq_error -= data->quant; |
480 |
|
|
481 |
|
/* The type stuff */ |
482 |
|
data->type = s->type; |
483 |
|
|
484 |
|
/* The only required data for AFTER step is this one for the overflow |
485 |
|
* control */ |
486 |
s->desired_length = s->length; |
s->desired_length = s->length; |
487 |
|
|
488 |
return(0); |
return(0); |
551 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
552 |
|
|
553 |
/* Compute the overflow we should compensate */ |
/* Compute the overflow we should compensate */ |
554 |
if (s->type != XVID_TYPE_IVOP) { |
if (s->type != XVID_TYPE_IVOP || rc->overflow > 0) { |
555 |
double frametype_factor; |
double frametype_factor; |
556 |
double framesize_factor; |
double framesize_factor; |
557 |
|
|
582 |
/* Apply the overflow strength imposed by the user */ |
/* Apply the overflow strength imposed by the user */ |
583 |
overflow *= (rc->param.overflow_control_strength/100.0f); |
overflow *= (rc->param.overflow_control_strength/100.0f); |
584 |
} else { |
} else { |
585 |
/* no overflow applied in IFrames because: |
/* no negative overflow applied in IFrames because: |
586 |
* - their role is important as they're references for P/BFrames. |
* - their role is important as they're references for P/BFrames. |
587 |
* - there aren't much in typical sequences, so if an IFrame overflows too |
* - there aren't much in typical sequences, so if an IFrame overflows too |
588 |
* much, this overflow may impact the next IFrame too much and generate |
* much, this overflow may impact the next IFrame too much and generate |
614 |
* pass nor smaller than the allowed minimum. |
* pass nor smaller than the allowed minimum. |
615 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
616 |
|
|
617 |
|
#ifdef PASS_SMALLER |
618 |
if (dbytes > s->length) { |
if (dbytes > s->length) { |
619 |
dbytes = s->length; |
dbytes = s->length; |
620 |
} else if (dbytes < rc->min_length[s->type-1]) { |
} |
621 |
|
#endif |
622 |
|
|
623 |
|
/* Prevent stupid desired sizes under logical values */ |
624 |
|
if (dbytes < rc->min_length[s->type-1]) { |
625 |
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); |
|
626 |
} |
} |
627 |
|
|
628 |
/*------------------------------------------------------------------------ |
/*------------------------------------------------------------------------ |
982 |
|
|
983 |
/* pre-process the statistics data |
/* pre-process the statistics data |
984 |
* - for each type, count, tot_length, min_length, max_length |
* - for each type, count, tot_length, min_length, max_length |
985 |
* - set keyframes_locations */ |
* - set keyframes_locations, tot_prescaled */ |
986 |
static void |
static void |
987 |
first_pass_stats_prepare_data(rc_2pass2_t * rc) |
first_pass_stats_prepare_data(rc_2pass2_t * rc) |
988 |
{ |
{ |
999 |
} |
} |
1000 |
|
|
1001 |
rc->max_length = INT_MIN; |
rc->max_length = INT_MIN; |
1002 |
|
rc->tot_weighted = 0; |
1003 |
|
|
1004 |
/* Loop through all frames and find/compute all the stuff this function |
/* Loop through all frames and find/compute all the stuff this function |
1005 |
* is supposed to do */ |
* is supposed to do */ |
1009 |
rc->count[s->type-1]++; |
rc->count[s->type-1]++; |
1010 |
rc->tot_length[s->type-1] += s->length; |
rc->tot_length[s->type-1] += s->length; |
1011 |
rc->tot_invariant[s->type-1] += s->invariant; |
rc->tot_invariant[s->type-1] += s->invariant; |
1012 |
|
if (s->zone_mode != XVID_ZONE_QUANT) |
1013 |
|
rc->tot_weighted += (int)(s->weight*(s->length - s->invariant)); |
1014 |
|
|
1015 |
if (s->length < rc->min_length[s->type-1]) { |
if (s->length < rc->min_length[s->type-1]) { |
1016 |
rc->min_length[s->type-1] = s->length; |
rc->min_length[s->type-1] = s->length; |
1045 |
int i,j; |
int i,j; |
1046 |
int n = 0; |
int n = 0; |
1047 |
|
|
|
rc->avg_weight = 0.0; |
|
1048 |
rc->tot_quant = 0; |
rc->tot_quant = 0; |
1049 |
rc->tot_quant_invariant = 0; |
rc->tot_quant_invariant = 0; |
1050 |
|
|
1053 |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
1054 |
rc->stats[j].weight = 1.0; |
rc->stats[j].weight = 1.0; |
1055 |
} |
} |
|
rc->avg_weight += rc->num_frames * 1.0; |
|
1056 |
n += rc->num_frames; |
n += rc->num_frames; |
1057 |
} |
} |
1058 |
|
|
1061 |
|
|
1062 |
int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames; |
int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames; |
1063 |
|
|
1064 |
|
/* Zero weight make no sense */ |
1065 |
|
if (create->zones[i].increment == 0) create->zones[i].increment = 1; |
1066 |
|
/* And obviously an undetermined infinite makes even less sense */ |
1067 |
|
if (create->zones[i].base == 0) create->zones[i].base = 1; |
1068 |
|
|
1069 |
if (i==0 && create->zones[i].frame > 0) { |
if (i==0 && create->zones[i].frame > 0) { |
1070 |
for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) { |
for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) { |
1071 |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
1072 |
rc->stats[j].weight = 1.0; |
rc->stats[j].weight = 1.0; |
1073 |
} |
} |
|
rc->avg_weight += create->zones[i].frame * 1.0; |
|
1074 |
n += create->zones[i].frame; |
n += create->zones[i].frame; |
1075 |
} |
} |
1076 |
|
|
1080 |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
1081 |
} |
} |
1082 |
next -= create->zones[i].frame; |
next -= create->zones[i].frame; |
|
rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base; |
|
1083 |
n += next; |
n += next; |
1084 |
} else{ /* XVID_ZONE_QUANT */ |
} else{ /* XVID_ZONE_QUANT */ |
1085 |
for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { |
for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { |
1090 |
} |
} |
1091 |
} |
} |
1092 |
} |
} |
|
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); |
|
1093 |
} |
} |
1094 |
|
|
1095 |
|
|
1098 |
first_pass_scale_curve_internal(rc_2pass2_t *rc) |
first_pass_scale_curve_internal(rc_2pass2_t *rc) |
1099 |
{ |
{ |
1100 |
int64_t target; |
int64_t target; |
|
int64_t pass1_length; |
|
1101 |
int64_t total_invariant; |
int64_t total_invariant; |
1102 |
double scaler; |
double scaler; |
1103 |
int i, num_MBs; |
int i, num_MBs; |
1116 |
target = rc->target; |
target = rc->target; |
1117 |
target -= rc->tot_quant; |
target -= rc->tot_quant; |
1118 |
|
|
|
/* 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; |
|
|
|
|
1119 |
/* Let's compute a linear scaler in order to perform curve scaling */ |
/* Let's compute a linear scaler in order to perform curve scaling */ |
1120 |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); |
|
|
|
|
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; |
|
|
} |
|
1121 |
|
|
1122 |
/* Compute min frame lengths (for each frame type) according to the number |
/* Compute min frame lengths (for each frame type) according to the number |
1123 |
* of MBs. We sum all block type counters of frame 0, this gives us the |
* of MBs. We sum all block type counters of frame 0, this gives us the |
1160 |
} |
} |
1161 |
|
|
1162 |
/* Compute the scaled length -- only non invariant data length is scaled */ |
/* Compute the scaled length -- only non invariant data length is scaled */ |
1163 |
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); |
1164 |
|
|
1165 |
/* Compare with the computed minimum */ |
/* Compare with the computed minimum */ |
1166 |
if (len < rc->min_length[s->type-1]) { |
if (len < rc->min_length[s->type-1]) { |
1172 |
* total counters, as we prepare a second pass for 'regular' |
* total counters, as we prepare a second pass for 'regular' |
1173 |
* frames */ |
* frames */ |
1174 |
target -= s->scaled_length; |
target -= s->scaled_length; |
|
pass1_length -= s->length; |
|
1175 |
} else { |
} else { |
1176 |
/* Do nothing for now, we'll scale this later */ |
/* Do nothing for now, we'll scale this later */ |
1177 |
s->scaled_length = 0; |
s->scaled_length = 0; |
1182 |
* total counters. Now, it's possible to scale the 'regular' frames. */ |
* total counters. Now, it's possible to scale the 'regular' frames. */ |
1183 |
|
|
1184 |
/* Scaling factor for 'regular' frames */ |
/* Scaling factor for 'regular' frames */ |
1185 |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); |
|
|
|
|
/* 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; |
|
|
} |
|
1186 |
|
|
1187 |
/* Do another pass with the new scaler */ |
/* Do another pass with the new scaler */ |
1188 |
for (i=0; i<rc->num_frames; i++) { |
for (i=0; i<rc->num_frames; i++) { |
1190 |
|
|
1191 |
/* Ignore frame with forced frame sizes */ |
/* Ignore frame with forced frame sizes */ |
1192 |
if (s->scaled_length == 0) |
if (s->scaled_length == 0) |
1193 |
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); |
1194 |
} |
} |
1195 |
|
|
1196 |
/* Job done */ |
/* Job done */ |