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.33 2003-12-21 17:38:17 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> |
457 |
if (data->quant > 0) |
if (data->quant > 0) |
458 |
return(0); |
return(0); |
459 |
|
|
460 |
/* Second case: insufficent stats data */ |
/* Second case: insufficent stats data |
461 |
|
* We can't guess much what we should do, let core decide all alone */ |
462 |
if (data->frame_num >= rc->num_frames) { |
if (data->frame_num >= rc->num_frames) { |
463 |
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)", |
464 |
data->frame_num); |
data->frame_num); |
465 |
return(0); |
return(0); |
466 |
} |
} |
467 |
|
|
468 |
/* Third case: We are in a Quant zone */ |
/* Third case: We are in a Quant zone |
469 |
|
* Quant zones must just ensure we use the same settings as first pass |
470 |
|
* So set the quantizer and the type */ |
471 |
if (s->zone_mode == XVID_ZONE_QUANT) { |
if (s->zone_mode == XVID_ZONE_QUANT) { |
472 |
|
/* Quant stuff */ |
473 |
rc->fq_error += s->weight; |
rc->fq_error += s->weight; |
474 |
data->quant = (int)rc->fq_error; |
data->quant = (int)rc->fq_error; |
475 |
rc->fq_error -= data->quant; |
rc->fq_error -= data->quant; |
476 |
|
|
477 |
|
/* The type stuff */ |
478 |
|
data->type = s->type; |
479 |
|
|
480 |
|
/* The only required data for AFTER step is this one for the overflow |
481 |
|
* control */ |
482 |
s->desired_length = s->length; |
s->desired_length = s->length; |
483 |
|
|
484 |
return(0); |
return(0); |
547 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
548 |
|
|
549 |
/* Compute the overflow we should compensate */ |
/* Compute the overflow we should compensate */ |
550 |
if (s->type != XVID_TYPE_IVOP) { |
if (s->type != XVID_TYPE_IVOP || rc->overflow > 0) { |
551 |
double frametype_factor; |
double frametype_factor; |
552 |
double framesize_factor; |
double framesize_factor; |
553 |
|
|
578 |
/* Apply the overflow strength imposed by the user */ |
/* Apply the overflow strength imposed by the user */ |
579 |
overflow *= (rc->param.overflow_control_strength/100.0f); |
overflow *= (rc->param.overflow_control_strength/100.0f); |
580 |
} else { |
} else { |
581 |
/* no overflow applied in IFrames because: |
/* no negative overflow applied in IFrames because: |
582 |
* - their role is important as they're references for P/BFrames. |
* - their role is important as they're references for P/BFrames. |
583 |
* - 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 |
584 |
* much, this overflow may impact the next IFrame too much and generate |
* much, this overflow may impact the next IFrame too much and generate |
610 |
* pass nor smaller than the allowed minimum. |
* pass nor smaller than the allowed minimum. |
611 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
612 |
|
|
613 |
|
#ifdef PASS_SMALLER |
614 |
if (dbytes > s->length) { |
if (dbytes > s->length) { |
615 |
dbytes = s->length; |
dbytes = s->length; |
616 |
} else if (dbytes < rc->min_length[s->type-1]) { |
} else |
617 |
|
#endif |
618 |
|
if (dbytes < rc->min_length[s->type-1]) { |
619 |
dbytes = rc->min_length[s->type-1]; |
dbytes = rc->min_length[s->type-1]; |
620 |
} else if (dbytes > rc->max_length) { |
} else if (dbytes > rc->max_length) { |
621 |
/* ToDo: this condition is always wrong as max_length == maximum frame |
/* ToDo: this condition is always wrong as max_length == maximum frame |
1127 |
/* Let's compute a linear scaler in order to perform curve scaling */ |
/* Let's compute a linear scaler in order to perform curve scaling */ |
1128 |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
1129 |
|
|
1130 |
|
#ifdef PASS_SMALLER |
1131 |
if ((target - total_invariant) <= 0 || |
if ((target - total_invariant) <= 0 || |
1132 |
(pass1_length - total_invariant) <= 0 || |
(pass1_length - total_invariant) <= 0 || |
1133 |
target >= pass1_length) { |
target >= pass1_length) { |
1134 |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n"); |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n"); |
1135 |
scaler = 1.0; |
scaler = 1.0; |
1136 |
} |
} |
1137 |
|
#endif |
1138 |
|
|
1139 |
/* Compute min frame lengths (for each frame type) according to the number |
/* Compute min frame lengths (for each frame type) according to the number |
1140 |
* 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 |
1202 |
/* Scaling factor for 'regular' frames */ |
/* Scaling factor for 'regular' frames */ |
1203 |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
1204 |
|
|
1205 |
|
#ifdef PASS_SMALLER |
1206 |
/* Detect undersizing */ |
/* Detect undersizing */ |
1207 |
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
1208 |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected after correction\n"); |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected after correction\n"); |
1209 |
scaler = 1.0; |
scaler = 1.0; |
1210 |
} |
} |
1211 |
|
#endif |
1212 |
|
|
1213 |
/* Do another pass with the new scaler */ |
/* Do another pass with the new scaler */ |
1214 |
for (i=0; i<rc->num_frames; i++) { |
for (i=0; i<rc->num_frames; i++) { |