35 |
/* forces second pass not to be bigger than first */ |
/* forces second pass not to be bigger than first */ |
36 |
#undef PASS_SMALLER |
#undef PASS_SMALLER |
37 |
|
|
38 |
|
/* automtically alters overflow controls (strength and improvement/degradation) |
39 |
|
to fight most common problems without user's knowladge */ |
40 |
|
#define SMART_OVERFLOW_SETTING |
41 |
|
|
42 |
#include <stdio.h> |
#include <stdio.h> |
43 |
#include <math.h> |
#include <math.h> |
44 |
#include <limits.h> |
#include <limits.h> |
212 |
* ToDo: description */ |
* ToDo: description */ |
213 |
double fq_error; |
double fq_error; |
214 |
|
|
215 |
|
int min_quant; /* internal minimal quant, prevents wrong quants from being used */ |
216 |
|
|
217 |
/*---------------------------------- |
/*---------------------------------- |
218 |
* Debug |
* Debug |
219 |
*--------------------------------*/ |
*--------------------------------*/ |
317 |
for (i=0; i<3; i++) rc->last_quant[i] = 0; |
for (i=0; i<3; i++) rc->last_quant[i] = 0; |
318 |
|
|
319 |
rc->fq_error = 0; |
rc->fq_error = 0; |
320 |
|
rc->min_quant = 1; |
321 |
|
|
322 |
/* Count frames (and intra frames) in the stats file, store the result into |
/* Count frames (and intra frames) in the stats file, store the result into |
323 |
* the rc structure */ |
* the rc structure */ |
624 |
#ifdef PASS_SMALLER |
#ifdef PASS_SMALLER |
625 |
if (dbytes > s->length) { |
if (dbytes > s->length) { |
626 |
dbytes = s->length; |
dbytes = s->length; |
627 |
} else |
} |
628 |
|
#endif |
629 |
|
|
630 |
|
/* Prevent stupid desired sizes under logical values */ |
631 |
if (dbytes < rc->min_length[s->type-1]) { |
if (dbytes < rc->min_length[s->type-1]) { |
632 |
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); |
|
633 |
} |
} |
634 |
#endif |
|
635 |
/*------------------------------------------------------------------------ |
/*------------------------------------------------------------------------ |
636 |
* Desired frame length <-> quantizer mapping |
* Desired frame length <-> quantizer mapping |
637 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
714 |
data->quant = data->max_quant[s->type-1]; |
data->quant = data->max_quant[s->type-1]; |
715 |
} |
} |
716 |
|
|
717 |
|
if (data->quant < rc->min_quant) data->quant = rc->min_quant; |
718 |
|
|
719 |
/* To avoid big quality jumps from frame to frame, we apply a "security" |
/* To avoid big quality jumps from frame to frame, we apply a "security" |
720 |
* rule that makes |last_quant - new_quant| <= 2. This rule only applies |
* rule that makes |last_quant - new_quant| <= 2. This rule only applies |
721 |
* to predicted frames (P and B) */ |
* to predicted frames (P and B) */ |
1128 |
/* Let's compute a linear scaler in order to perform curve scaling */ |
/* Let's compute a linear scaler in order to perform curve scaling */ |
1129 |
scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); |
scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); |
1130 |
|
|
1131 |
|
#ifdef SMART_OVERFLOW_SETTING |
1132 |
|
if (scaler > 0.9) { |
1133 |
|
rc->param.max_overflow_degradation *= 5; |
1134 |
|
rc->param.max_overflow_improvement *= 5; |
1135 |
|
rc->param.overflow_control_strength *= 3; |
1136 |
|
} else if (scaler > 0.6) { |
1137 |
|
rc->param.max_overflow_degradation *= 2; |
1138 |
|
rc->param.max_overflow_improvement *= 2; |
1139 |
|
rc->param.overflow_control_strength *= 2; |
1140 |
|
} else { |
1141 |
|
rc->min_quant = 2; |
1142 |
|
} |
1143 |
|
#endif |
1144 |
|
|
1145 |
/* Compute min frame lengths (for each frame type) according to the number |
/* Compute min frame lengths (for each frame type) according to the number |
1146 |
* 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 |
1147 |
* number of MBs. |
* number of MBs. |