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.32 2003-12-21 12:41:48 syskin 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> |
538 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
539 |
|
|
540 |
/* Compute the overflow we should compensate */ |
/* Compute the overflow we should compensate */ |
541 |
if (s->type != XVID_TYPE_IVOP) { |
if (s->type != XVID_TYPE_IVOP || rc->overflow > 0) { |
542 |
double frametype_factor; |
double frametype_factor; |
543 |
double framesize_factor; |
double framesize_factor; |
544 |
|
|
569 |
/* Apply the overflow strength imposed by the user */ |
/* Apply the overflow strength imposed by the user */ |
570 |
overflow *= (rc->param.overflow_control_strength/100.0f); |
overflow *= (rc->param.overflow_control_strength/100.0f); |
571 |
} else { |
} else { |
572 |
/* no overflow applied in IFrames because: |
/* no negative overflow applied in IFrames because: |
573 |
* - their role is important as they're references for P/BFrames. |
* - their role is important as they're references for P/BFrames. |
574 |
* - 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 |
575 |
* much, this overflow may impact the next IFrame too much and generate |
* much, this overflow may impact the next IFrame too much and generate |
601 |
* pass nor smaller than the allowed minimum. |
* pass nor smaller than the allowed minimum. |
602 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
603 |
|
|
604 |
|
#ifdef PASS_SMALLER |
605 |
if (dbytes > s->length) { |
if (dbytes > s->length) { |
606 |
dbytes = s->length; |
dbytes = s->length; |
607 |
} else if (dbytes < rc->min_length[s->type-1]) { |
} else |
608 |
|
#endif |
609 |
|
if (dbytes < rc->min_length[s->type-1]) { |
610 |
dbytes = rc->min_length[s->type-1]; |
dbytes = rc->min_length[s->type-1]; |
611 |
} else if (dbytes > rc->max_length) { |
} else if (dbytes > rc->max_length) { |
612 |
/* ToDo: this condition is always wrong as max_length == maximum frame |
/* ToDo: this condition is always wrong as max_length == maximum frame |
1118 |
/* Let's compute a linear scaler in order to perform curve scaling */ |
/* Let's compute a linear scaler in order to perform curve scaling */ |
1119 |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
1120 |
|
|
1121 |
|
#ifdef PASS_SMALLER |
1122 |
if ((target - total_invariant) <= 0 || |
if ((target - total_invariant) <= 0 || |
1123 |
(pass1_length - total_invariant) <= 0 || |
(pass1_length - total_invariant) <= 0 || |
1124 |
target >= pass1_length) { |
target >= pass1_length) { |
1125 |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n"); |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n"); |
1126 |
scaler = 1.0; |
scaler = 1.0; |
1127 |
} |
} |
1128 |
|
#endif |
1129 |
/* Compute min frame lengths (for each frame type) according to the number |
/* Compute min frame lengths (for each frame type) according to the number |
1130 |
* 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 |
1131 |
* number of MBs. |
* number of MBs. |
1192 |
/* Scaling factor for 'regular' frames */ |
/* Scaling factor for 'regular' frames */ |
1193 |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
1194 |
|
|
1195 |
|
#ifdef PASS_SMALLER |
1196 |
/* Detect undersizing */ |
/* Detect undersizing */ |
1197 |
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
1198 |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected after correction\n"); |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected after correction\n"); |
1199 |
scaler = 1.0; |
scaler = 1.0; |
1200 |
} |
} |
1201 |
|
#endif |
1202 |
|
|
1203 |
/* Do another pass with the new scaler */ |
/* Do another pass with the new scaler */ |
1204 |
for (i=0; i<rc->num_frames; i++) { |
for (i=0; i<rc->num_frames; i++) { |