1 |
/****************************************************************************** |
/****************************************************************************** |
2 |
* |
* |
3 |
* XviD Bit Rate Controller Library |
* XviD Bit Rate Controller Library |
4 |
* - VBR 2 pass bitrate controler implementation - |
* - VBR 2 pass bitrate controller implementation - |
5 |
* |
* |
6 |
* Copyright (C) 2002 Foxer <email?> |
* Copyright (C) 2002 Foxer <email?> |
7 |
* 2002 Dirk Knop <dknop@gwdg.de> |
* 2002 Dirk Knop <dknop@gwdg.de> |
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.10 2003-05-22 22:56:22 edgomez Exp $ |
* $Id: plugin_2pass2.c,v 1.1.2.11 2003-05-24 21:22:18 edgomez Exp $ |
29 |
* |
* |
30 |
*****************************************************************************/ |
*****************************************************************************/ |
31 |
|
|
33 |
#include <math.h> |
#include <math.h> |
34 |
#include <limits.h> |
#include <limits.h> |
35 |
|
|
36 |
|
#include "../xvid.h" |
37 |
|
#include "../image/image.h" |
38 |
|
|
39 |
|
/***************************************************************************** |
40 |
|
* Some constants |
41 |
|
****************************************************************************/ |
42 |
|
|
43 |
#define RAD2DEG 57.295779513082320876798154814105 |
#define RAD2DEG 57.295779513082320876798154814105 |
44 |
#define DEG2RAD 0.017453292519943295769236907684886 |
#define DEG2RAD 0.017453292519943295769236907684886 |
45 |
|
|
46 |
#include "../xvid.h" |
#define DEFAULT_KEYFRAME_BOOST 0 |
47 |
#include "../image/image.h" |
#define DEFAULT_PAYBACK_METHOD XVID_PAYBACK_PROP |
48 |
|
#define DEFAULT_BITRATE_PAYBACK_DELAY 250 |
49 |
|
#define DEFAULT_CURVE_COMPRESSION_HIGH 0 |
50 |
|
#define DEFAULT_CURVE_COMPRESSION_LOW 0 |
51 |
|
#define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60 |
52 |
|
#define DEFAULT_MAX_OVERFLOW_DEGRADATION 60 |
53 |
|
|
54 |
|
/* Alt curve settings */ |
55 |
|
#define DEFAULT_USE_ALT_CURVE 0 |
56 |
|
#define DEFAULT_ALT_CURVE_HIGH_DIST 500 |
57 |
|
#define DEFAULT_ALT_CURVE_LOW_DIST 90 |
58 |
|
#define DEFAULT_ALT_CURVE_USE_AUTO 1 |
59 |
|
#define DEFAULT_ALT_CURVE_AUTO_STR 30 |
60 |
|
#define DEFAULT_ALT_CURVE_TYPE XVID_CURVE_LINEAR |
61 |
|
#define DEFAULT_ALT_CURVE_MIN_REL_QUAL 50 |
62 |
|
#define DEFAULT_ALT_CURVE_USE_AUTO_BONUS_BIAS 1 |
63 |
|
#define DEFAULT_ALT_CURVE_BONUS_BIAS 50 |
64 |
|
|
65 |
|
/* Keyframe settings */ |
66 |
|
#define DEFAULT_KFTRESHOLD 10 |
67 |
|
#define DEFAULT_KFREDUCTION 20 |
68 |
|
#define DEFAULT_MIN_KEY_INTERVAL 1 |
69 |
|
|
70 |
|
/***************************************************************************** |
71 |
|
* Structures |
72 |
|
****************************************************************************/ |
73 |
|
|
74 |
|
/* Statistics */ |
75 |
typedef struct { |
typedef struct { |
76 |
int type; /* first pass type */ |
int type; /* first pass type */ |
77 |
int quant; /* first pass quant */ |
int quant; /* first pass quant */ |
84 |
double weight; |
double weight; |
85 |
} stat_t; |
} stat_t; |
86 |
|
|
87 |
|
/* Context struct */ |
|
|
|
|
|
|
|
/* context struct */ |
|
88 |
typedef struct |
typedef struct |
89 |
{ |
{ |
90 |
xvid_plugin_2pass2_t param; |
xvid_plugin_2pass2_t param; |
137 |
} rc_2pass2_t; |
} rc_2pass2_t; |
138 |
|
|
139 |
|
|
140 |
|
/***************************************************************************** |
141 |
|
* Sub plugin functions prototypes |
142 |
|
****************************************************************************/ |
143 |
|
|
144 |
|
static int rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t ** handle); |
145 |
|
static int rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data); |
146 |
|
static int rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data); |
147 |
|
static int rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy); |
148 |
|
|
149 |
|
/***************************************************************************** |
150 |
|
* Plugin definition |
151 |
|
****************************************************************************/ |
152 |
|
|
153 |
#define BUF_SZ 1024 |
int |
154 |
#define MAX_COLS 5 |
xvid_plugin_2pass2(void * handle, int opt, void * param1, void * param2) |
|
|
|
|
|
|
|
/* open stats file, and count num frames */ |
|
|
|
|
|
static int det_stats_length(rc_2pass2_t * rc, char * filename) |
|
155 |
{ |
{ |
156 |
FILE * f; |
switch(opt) { |
157 |
int n, ignore; |
case XVID_PLG_INFO : |
|
char type; |
|
|
|
|
|
rc->num_frames = 0; |
|
|
rc->num_keyframes = 0; |
|
|
|
|
|
if ((f = fopen(filename, "rt")) == NULL) |
|
158 |
return 0; |
return 0; |
159 |
|
|
160 |
while((n = fscanf(f, "%c %d %d %d %d %d %d\n", |
case XVID_PLG_CREATE : |
161 |
&type, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) { |
return rc_2pass2_create((xvid_plg_create_t*)param1, param2); |
|
if (type == 'i') { |
|
|
rc->num_frames++; |
|
|
rc->num_keyframes++; |
|
|
}else if (type == 'p' || type == 'b' || type == 's') { |
|
|
rc->num_frames++; |
|
|
} |
|
|
} |
|
162 |
|
|
163 |
fclose(f); |
case XVID_PLG_DESTROY : |
164 |
|
return rc_2pass2_destroy((rc_2pass2_t*)handle, (xvid_plg_destroy_t*)param1); |
165 |
|
|
166 |
return 1; |
case XVID_PLG_BEFORE : |
167 |
|
return rc_2pass2_before((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1); |
168 |
|
|
169 |
|
case XVID_PLG_AFTER : |
170 |
|
return rc_2pass2_after((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1); |
171 |
} |
} |
172 |
|
|
173 |
|
return XVID_ERR_FAIL; |
174 |
|
} |
175 |
|
|
176 |
|
/***************************************************************************** |
177 |
|
* Sub plugin functions definitions |
178 |
|
****************************************************************************/ |
179 |
|
|
180 |
|
/* First a few local helping function prototypes */ |
181 |
|
static int det_stats_length(rc_2pass2_t * rc, char * filename); |
182 |
|
static int load_stats(rc_2pass2_t *rc, char * filename); |
183 |
|
static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create); |
184 |
|
static void internal_scale(rc_2pass2_t *rc); |
185 |
|
static void pre_process0(rc_2pass2_t * rc); |
186 |
|
static void pre_process1(rc_2pass2_t * rc); |
187 |
|
|
188 |
/* open stats file(s) and read into rc->stats array */ |
/*---------------------------------------------------------------------------- |
189 |
|
*--------------------------------------------------------------------------*/ |
190 |
|
|
191 |
static int load_stats(rc_2pass2_t *rc, char * filename) |
static int |
192 |
|
rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t **handle) |
193 |
{ |
{ |
194 |
FILE * f; |
xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param; |
195 |
int i, not_scaled; |
rc_2pass2_t * rc; |
196 |
|
int i; |
197 |
|
|
198 |
|
rc = malloc(sizeof(rc_2pass2_t)); |
199 |
|
if (rc == NULL) |
200 |
|
return XVID_ERR_MEMORY; |
201 |
|
|
202 |
if ((f = fopen(filename, "rt"))==NULL) |
rc->param = *param; |
|
return 0; |
|
203 |
|
|
204 |
i = 0; |
#define _INIT(a, b) if((a) <= 0) (a) = (b) |
205 |
not_scaled = 0; |
/* Let's set our defaults if needed */ |
206 |
while(i < rc->num_frames) { |
_INIT(rc->param.keyframe_boost, DEFAULT_KEYFRAME_BOOST); |
207 |
stat_t * s = &rc->stats[i]; |
_INIT(rc->param.payback_method, DEFAULT_PAYBACK_METHOD); |
208 |
int n; |
_INIT(rc->param.bitrate_payback_delay, DEFAULT_BITRATE_PAYBACK_DELAY); |
209 |
char type; |
_INIT(rc->param.curve_compression_high, DEFAULT_CURVE_COMPRESSION_HIGH); |
210 |
|
_INIT(rc->param.curve_compression_low, DEFAULT_CURVE_COMPRESSION_LOW); |
211 |
|
_INIT(rc->param.max_overflow_improvement, DEFAULT_MAX_OVERFLOW_IMPROVEMENT); |
212 |
|
_INIT(rc->param.max_overflow_degradation, DEFAULT_MAX_OVERFLOW_DEGRADATION); |
213 |
|
|
214 |
|
/* Alt curve settings */ |
215 |
|
_INIT(rc->param.use_alt_curve, DEFAULT_USE_ALT_CURVE); |
216 |
|
_INIT(rc->param.alt_curve_high_dist, DEFAULT_ALT_CURVE_HIGH_DIST); |
217 |
|
_INIT(rc->param.alt_curve_low_dist, DEFAULT_ALT_CURVE_LOW_DIST); |
218 |
|
_INIT(rc->param.alt_curve_use_auto, DEFAULT_ALT_CURVE_USE_AUTO); |
219 |
|
_INIT(rc->param.alt_curve_auto_str, DEFAULT_ALT_CURVE_AUTO_STR); |
220 |
|
_INIT(rc->param.alt_curve_type, DEFAULT_ALT_CURVE_TYPE); |
221 |
|
_INIT(rc->param.alt_curve_min_rel_qual, DEFAULT_ALT_CURVE_MIN_REL_QUAL); |
222 |
|
_INIT(rc->param.alt_curve_use_auto_bonus_bias, DEFAULT_ALT_CURVE_USE_AUTO_BONUS_BIAS); |
223 |
|
_INIT(rc->param.alt_curve_bonus_bias, DEFAULT_ALT_CURVE_BONUS_BIAS); |
224 |
|
|
225 |
|
/* Keyframe settings */ |
226 |
|
_INIT(rc->param.kftreshold, DEFAULT_KFTRESHOLD); |
227 |
|
_INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION); |
228 |
|
_INIT(rc->param.min_key_interval, DEFAULT_MIN_KEY_INTERVAL); |
229 |
|
#undef _INIT |
230 |
|
|
231 |
s->scaled_length = 0; |
/* Count frames in the stats file */ |
232 |
n = fscanf(f, "%c %d %d %d %d %d %d\n", &type, &s->quant, &s->blks[0], &s->blks[1], &s->blks[2], &s->length, &s->scaled_length); |
if (!det_stats_length(rc, param->filename)){ |
233 |
if (n == EOF) break; |
DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename); |
234 |
if (n < 7) { |
free(rc); |
235 |
not_scaled = 1; |
return XVID_ERR_FAIL; |
236 |
} |
} |
237 |
|
|
238 |
if (type == 'i') { |
/* Allocate the stats' memory */ |
239 |
s->type = XVID_TYPE_IVOP; |
if ((rc->stats = malloc(rc->num_frames * sizeof(stat_t))) == NULL) { |
240 |
}else if (type == 'p' || type == 's') { |
free(rc); |
241 |
s->type = XVID_TYPE_PVOP; |
return XVID_ERR_MEMORY; |
|
}else if (type == 'b') { |
|
|
s->type = XVID_TYPE_BVOP; |
|
|
}else{ /* unknown type */ |
|
|
DPRINTF(XVID_DEBUG_RC, "unknown stats frame type; assuming pvop\n"); |
|
|
s->type = XVID_TYPE_PVOP; |
|
242 |
} |
} |
243 |
|
|
244 |
i++; |
/* |
245 |
|
* Allocate keyframes location's memory |
246 |
|
* PS: see comment in pre_process0 for the +1 location requirement |
247 |
|
*/ |
248 |
|
if ((rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int))) == NULL) { |
249 |
|
free(rc->stats); |
250 |
|
free(rc); |
251 |
|
return XVID_ERR_MEMORY; |
252 |
} |
} |
253 |
|
|
254 |
rc->num_frames = i; |
if (!load_stats(rc, param->filename)) { |
255 |
|
DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename); |
256 |
|
free(rc->keyframe_locations); |
257 |
|
free(rc->stats); |
258 |
|
free(rc); |
259 |
|
return XVID_ERR_FAIL; |
260 |
|
} |
261 |
|
|
262 |
fclose(f); |
/* pre-process our stats */ |
263 |
|
|
264 |
return 1; |
if (rc->num_frames < create->fbase/create->fincr) { |
265 |
|
rc->target = rc->param.bitrate / 8; /* one second */ |
266 |
|
} else { |
267 |
|
rc->target = |
268 |
|
((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * (uint64_t)create->fincr) / \ |
269 |
|
((uint64_t)create->fbase * 8); |
270 |
} |
} |
271 |
|
|
272 |
|
DPRINTF(XVID_DEBUG_RC, "Number of frames: %d\n", rc->num_frames); |
273 |
|
DPRINTF(XVID_DEBUG_RC, "Frame rate: %d/%d\n", create->fbase, create->fincr); |
274 |
|
DPRINTF(XVID_DEBUG_RC, "Target bitrate: %ld\n", rc->param.bitrate); |
275 |
|
DPRINTF(XVID_DEBUG_RC, "Target filesize: %lld\n", rc->target); |
276 |
|
|
277 |
|
/* Compensate the mean frame overhead caused by the container */ |
278 |
|
rc->target -= rc->num_frames*rc->param.container_frame_overhead; |
279 |
|
DPRINTF(XVID_DEBUG_RC, "Container Frame overhead: %d\n", rc->param.container_frame_overhead); |
280 |
|
DPRINTF(XVID_DEBUG_RC, "Target filesize (after container compensation): %lld\n", rc->target); |
281 |
|
|
282 |
|
pre_process0(rc); |
283 |
|
|
284 |
#if 0 |
if (rc->param.bitrate) { |
285 |
static void print_stats(rc_2pass2_t * rc) |
zone_process(rc, create); |
286 |
{ |
internal_scale(rc); |
287 |
int i; |
}else{ |
288 |
DPRINTF(XVID_DEBUG_RC, "type quant length scaled_length\n"); |
/* external scaler: ignore zone */ |
289 |
for (i = 0; i < rc->num_frames; i++) { |
for (i = 0; i < rc->num_frames; i++) { |
290 |
stat_t * s = &rc->stats[i]; |
rc->stats[i].zone_mode = XVID_ZONE_WEIGHT; |
291 |
DPRINTF(XVID_DEBUG_RC, "%d %d %d %d\n", s->type, s->quant, s->length, s->scaled_length); |
rc->stats[i].weight = 1.0; |
292 |
} |
} |
293 |
|
rc->avg_weight = 1.0; |
294 |
|
rc->tot_quant = 0; |
295 |
} |
} |
296 |
#endif |
pre_process1(rc); |
|
|
|
|
/* pre-process the statistics data |
|
|
- for each type, count, tot_length, min_length, max_length |
|
|
- set keyframes_locations |
|
|
*/ |
|
|
|
|
|
static void |
|
|
pre_process0(rc_2pass2_t * rc) |
|
|
{ |
|
|
int i,j; |
|
297 |
|
|
298 |
for (i=0; i<3; i++) { |
for (i=0; i<32;i++) { |
299 |
rc->count[i]=0; |
rc->pquant_error[i] = 0; |
300 |
rc->tot_length[i] = 0; |
rc->bquant_error[i] = 0; |
301 |
rc->last_quant[i] = 0; |
rc->quant_count[i] = 0; |
|
rc->min_length[i] = INT_MAX; |
|
302 |
} |
} |
303 |
|
|
304 |
rc->max_length = INT_MIN; |
rc->fq_error = 0; |
305 |
|
|
306 |
for (i=j=0; i<rc->num_frames; i++) { |
*handle = rc; |
307 |
stat_t * s = &rc->stats[i]; |
return(0); |
308 |
|
} |
309 |
|
|
310 |
rc->count[s->type-1]++; |
/*---------------------------------------------------------------------------- |
311 |
rc->tot_length[s->type-1] += s->length; |
*--------------------------------------------------------------------------*/ |
312 |
|
|
313 |
if (s->length < rc->min_length[s->type-1]) { |
static int |
314 |
rc->min_length[s->type-1] = s->length; |
rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy) |
315 |
|
{ |
316 |
|
free(rc->keyframe_locations); |
317 |
|
free(rc->stats); |
318 |
|
free(rc); |
319 |
|
return(0); |
320 |
} |
} |
321 |
|
|
322 |
if (s->length > rc->max_length) { |
/*---------------------------------------------------------------------------- |
323 |
rc->max_length = s->length; |
*--------------------------------------------------------------------------*/ |
|
} |
|
324 |
|
|
325 |
if (s->type == XVID_TYPE_IVOP) { |
static int |
326 |
rc->keyframe_locations[j] = i; |
rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data) |
327 |
j++; |
{ |
328 |
} |
stat_t * s = &rc->stats[data->frame_num]; |
329 |
} |
int overflow; |
330 |
|
int desired; |
331 |
|
double dbytes; |
332 |
|
double curve_temp; |
333 |
|
int capped_to_max_framesize = 0; |
334 |
|
|
335 |
/* |
/* |
336 |
* The "per sequence" overflow system considers a natural sequence to be |
* This function is quite long but easy to understand. In order to simplify |
337 |
* formed by all frames between two iframes, so if we want to make sure |
* the code path (a bit), we treat 3 cases that can return immediatly. |
|
* the system does not go nuts during last sequence, we force the last |
|
|
* frame to appear in the keyframe locations array. |
|
338 |
*/ |
*/ |
|
rc->keyframe_locations[j] = i; |
|
|
|
|
|
DPRINTF(XVID_DEBUG_RC, "Min 1st pass IFrame length: %d\n", rc->min_length[0]); |
|
|
DPRINTF(XVID_DEBUG_RC, "Min 1st pass PFrame length: %d\n", rc->min_length[1]); |
|
|
DPRINTF(XVID_DEBUG_RC, "Min 1st pass BFrame length: %d\n", rc->min_length[2]); |
|
|
} |
|
339 |
|
|
340 |
|
/* First case: Another plugin has already set a quantizer */ |
341 |
|
if (data->quant > 0) |
342 |
|
return(0); |
343 |
|
|
344 |
/* calculate zone weight "center" */ |
/* Second case: We are in a Quant zone */ |
345 |
|
if (s->zone_mode == XVID_ZONE_QUANT) { |
346 |
|
|
347 |
static void |
rc->fq_error += s->weight; |
348 |
zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create) |
data->quant = (int)rc->fq_error; |
349 |
{ |
rc->fq_error -= data->quant; |
|
int i,j; |
|
|
int n = 0; |
|
350 |
|
|
351 |
rc->avg_weight = 0.0; |
s->desired_length = s->length; |
|
rc->tot_quant = 0; |
|
352 |
|
|
353 |
|
return(0); |
354 |
|
|
|
if (create->num_zones == 0) { |
|
|
for (j = 0; j < rc->num_frames; j++) { |
|
|
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
|
|
rc->stats[j].weight = 1.0; |
|
|
} |
|
|
rc->avg_weight += rc->num_frames * 1.0; |
|
|
n += rc->num_frames; |
|
355 |
} |
} |
356 |
|
|
357 |
|
/* Third case: insufficent stats data */ |
358 |
|
if (data->frame_num >= rc->num_frames) |
359 |
|
return 0; |
360 |
|
|
361 |
for(i=0; i < create->num_zones; i++) { |
/* |
362 |
|
* The last case is the one every normal minded developer should fear to |
363 |
|
* maintain in a project :-) |
364 |
|
*/ |
365 |
|
|
366 |
int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames; |
/* XXX: why by 8 */ |
367 |
|
overflow = rc->overflow / 8; |
368 |
|
|
369 |
if (i==0 && create->zones[i].frame > 0) { |
/* |
370 |
for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) { |
* The rc->overflow field represents the overflow in current scene (between two |
371 |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
* IFrames) so we must not forget to reset it if we are entering a new scene |
372 |
rc->stats[j].weight = 1.0; |
*/ |
373 |
} |
if (s->type == XVID_TYPE_IVOP) { |
374 |
rc->avg_weight += create->zones[i].frame * 1.0; |
overflow = 0; |
|
n += create->zones[i].frame; |
|
375 |
} |
} |
376 |
|
|
377 |
if (create->zones[i].mode == XVID_ZONE_WEIGHT) { |
desired = s->scaled_length; |
378 |
for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { |
|
379 |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
dbytes = desired; |
380 |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
if (s->type == XVID_TYPE_IVOP) { |
381 |
|
dbytes += desired * rc->param.keyframe_boost / 100; |
382 |
} |
} |
383 |
next -= create->zones[i].frame; |
dbytes /= rc->movie_curve; |
|
rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base; |
|
|
n += next; |
|
|
}else{ // XVID_ZONE_QUANT |
|
|
for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { |
|
|
rc->stats[j].zone_mode = XVID_ZONE_QUANT; |
|
|
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
|
|
rc->tot_quant += rc->stats[j].length; |
|
|
} |
|
|
} |
|
|
} |
|
|
rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0; |
|
|
|
|
|
DPRINTF(XVID_DEBUG_RC, "center_weight: %f (for %i frames); fixed_bytes: %i\n", rc->avg_weight, n, rc->tot_quant); |
|
|
} |
|
|
|
|
|
|
|
|
/* scale the curve */ |
|
|
|
|
|
static void |
|
|
internal_scale(rc_2pass2_t *rc) |
|
|
{ |
|
|
int64_t target = rc->target - rc->tot_quant; |
|
|
int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant; |
|
|
int min_size[3]; |
|
|
double scaler; |
|
|
int i; |
|
|
|
|
384 |
|
|
385 |
/* |
/* |
386 |
* Perform an initial scale pass. |
* We are now entering in the hard part of the algo, it was first designed |
387 |
* if a frame size is scaled underneath our hardcoded minimums, then we |
* to work with i/pframes only streams, so the way it computes things is |
388 |
* force the frame size to the minimum, and deduct the original & scaled |
* adapted to pframes only. However we can use it if we just take care to |
389 |
* frame length from the original and target total lengths |
* scale the bframes sizes to pframes sizes using the ratio avg_p/avg_p and |
390 |
|
* then before really using values depending on frame sizes, scaling the |
391 |
|
* value again with the inverse ratio |
392 |
*/ |
*/ |
393 |
|
if (s->type == XVID_TYPE_BVOP) |
394 |
|
dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1]; |
395 |
|
|
396 |
min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8; |
/* |
397 |
min_size[1] = (rc->stats[0].blks[0] + 88) / 8; |
* Apply user's choosen Payback method. Payback helps bitrate to follow the |
398 |
min_size[2] = 8; |
* scaled curve "paying back" past errors in curve previsions. |
399 |
|
*/ |
400 |
scaler = (double)target / (double)pass1_length; |
if (rc->param.payback_method == XVID_PAYBACK_BIAS) { |
401 |
|
desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay); |
|
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
|
|
DPRINTF(XVID_DEBUG_RC, "undersize warning\n"); |
|
|
scaler = 1.0; |
|
|
} |
|
|
|
|
|
DPRINTF(XVID_DEBUG_RC, |
|
|
"Before any correction: target=%i, tot_length=%i, scaler=%f\n", |
|
|
(int)target, (int)pass1_length, scaler); |
|
|
|
|
|
for (i=0; i<rc->num_frames; i++) { |
|
|
stat_t * s = &rc->stats[i]; |
|
|
int len; |
|
|
|
|
|
if (s->zone_mode == XVID_ZONE_QUANT) { |
|
|
s->scaled_length = s->length; |
|
|
}else { |
|
|
len = (int)((double)s->length * scaler * s->weight / rc->avg_weight); |
|
|
if (len < min_size[s->type-1]) { /* force frame size */ |
|
|
s->scaled_length = min_size[s->type-1]; |
|
|
target -= s->scaled_length; |
|
|
pass1_length -= s->length; |
|
402 |
}else{ |
}else{ |
403 |
s->scaled_length = 0; |
desired = (int)(rc->curve_comp_error * dbytes / |
404 |
} |
rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay); |
|
} |
|
|
} |
|
|
|
|
|
scaler = (double)target / (double)pass1_length; |
|
|
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
|
|
DPRINTF(XVID_DEBUG_RC,"undersize warning\n"); |
|
|
scaler = 1.0; |
|
|
} |
|
|
|
|
|
DPRINTF(XVID_DEBUG_RC, |
|
|
"After correction: target=%i, tot_length=%i, scaler=%f\n", |
|
|
(int)target, (int)pass1_length, scaler); |
|
|
|
|
|
for (i=0; i<rc->num_frames; i++) { |
|
|
stat_t * s = &rc->stats[i]; |
|
|
|
|
|
if (s->scaled_length==0) { /* ignore frame with forced frame sizes */ |
|
|
s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void |
|
|
pre_process1(rc_2pass2_t * rc) |
|
|
{ |
|
|
int i; |
|
|
double total1, total2; |
|
|
uint64_t ivop_boost_total; |
|
|
|
|
|
ivop_boost_total = 0; |
|
|
rc->curve_comp_error = 0; |
|
|
|
|
|
for (i=0; i<3; i++) { |
|
|
rc->tot_scaled_length[i] = 0; |
|
|
} |
|
|
|
|
|
for (i=0; i<rc->num_frames; i++) { |
|
|
stat_t * s = &rc->stats[i]; |
|
|
|
|
|
rc->tot_scaled_length[s->type-1] += s->scaled_length; |
|
405 |
|
|
406 |
if (s->type == XVID_TYPE_IVOP) { |
if (labs(desired) > fabs(rc->curve_comp_error)) { |
407 |
ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100; |
desired = (int)rc->curve_comp_error; |
408 |
} |
} |
409 |
} |
} |
410 |
|
|
411 |
rc->movie_curve = ((double)(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1] + ivop_boost_total) / |
rc->curve_comp_error -= desired; |
|
(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1])); |
|
412 |
|
|
413 |
for(i=0; i<3; i++) { |
/* |
414 |
if (rc->count[i] == 0 || rc->movie_curve == 0) { |
* Alt curve treatment is not that hard to understand though the formulas |
415 |
rc->avg_length[i] = 1; |
* seem to be huge. Alt treatment is basically a way to soft/harden the |
416 |
}else{ |
* curve flux applying sine/linear/cosine ratios |
417 |
rc->avg_length[i] = rc->tot_scaled_length[i] / rc->count[i] / rc->movie_curve; |
*/ |
|
} |
|
|
} |
|
418 |
|
|
419 |
/* alt curve stuff here */ |
/* XXX: warning */ |
420 |
|
curve_temp = 0; |
421 |
|
|
422 |
if (rc->param.use_alt_curve) { |
if (rc->param.use_alt_curve) { |
|
const double avg_pvop = rc->avg_length[XVID_TYPE_PVOP-1]; |
|
|
const uint64_t tot_pvop = rc->tot_length[XVID_TYPE_PVOP-1]; |
|
|
const uint64_t tot_bvop = rc->tot_length[XVID_TYPE_BVOP-1]; |
|
|
const uint64_t tot_scaled_pvop = rc->tot_scaled_length[XVID_TYPE_PVOP-1]; |
|
|
const uint64_t tot_scaled_bvop = rc->tot_scaled_length[XVID_TYPE_BVOP-1]; |
|
|
|
|
|
rc->alt_curve_low = avg_pvop - avg_pvop * (double)rc->param.alt_curve_low_dist / 100.0; |
|
|
rc->alt_curve_low_diff = avg_pvop - rc->alt_curve_low; |
|
|
rc->alt_curve_high = avg_pvop + avg_pvop * (double)rc->param.alt_curve_high_dist / 100.0; |
|
|
rc->alt_curve_high_diff = rc->alt_curve_high - avg_pvop; |
|
|
|
|
|
if (rc->param.alt_curve_use_auto) { |
|
|
if (tot_bvop + tot_pvop > tot_scaled_bvop + tot_scaled_pvop) { |
|
|
rc->param.alt_curve_min_rel_qual = (int)(100.0 - (100.0 - 100.0 / |
|
|
((double)(tot_pvop + tot_bvop) / (double)(tot_scaled_pvop + tot_scaled_bvop))) * (double)rc->param.alt_curve_auto_str / 100.0); |
|
|
|
|
|
if (rc->param.alt_curve_min_rel_qual < 20) |
|
|
rc->param.alt_curve_min_rel_qual = 20; |
|
|
}else{ |
|
|
rc->param.alt_curve_min_rel_qual = 100; |
|
|
} |
|
|
} |
|
|
rc->alt_curve_mid_qual = (1.0 + (double)rc->param.alt_curve_min_rel_qual / 100.0) / 2.0; |
|
|
rc->alt_curve_qual_dev = 1.0 - rc->alt_curve_mid_qual; |
|
|
|
|
|
if (rc->param.alt_curve_low_dist > 100) { |
|
|
switch(rc->param.alt_curve_type) { |
|
|
case XVID_CURVE_SINE: // Sine Curve (high aggressiveness) |
|
|
rc->alt_curve_qual_dev *= 2.0 / (1.0 + sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff))); |
|
|
rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)); |
|
|
break; |
|
|
case XVID_CURVE_LINEAR: // Linear (medium aggressiveness) |
|
|
rc->alt_curve_qual_dev *= 2.0 / (1.0 + avg_pvop / rc->alt_curve_low_diff); |
|
|
rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * avg_pvop / rc->alt_curve_low_diff; |
|
|
break; |
|
|
case XVID_CURVE_COSINE: // Cosine Curve (low aggressiveness) |
|
|
rc->alt_curve_qual_dev *= 2.0 / (1.0 + (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)))); |
|
|
rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff))); |
|
|
} |
|
|
} |
|
|
} |
|
|
/* --- */ |
|
|
|
|
|
|
|
|
total1=total2=0; |
|
|
for (i=0; i<rc->num_frames; i++) { |
|
|
stat_t * s = &rc->stats[i]; |
|
|
|
|
423 |
if (s->type != XVID_TYPE_IVOP) { |
if (s->type != XVID_TYPE_IVOP) { |
|
double dbytes,dbytes2; |
|
|
|
|
|
dbytes = s->scaled_length / rc->movie_curve; |
|
|
dbytes2 = 0; /* XXX: warning */ |
|
|
total1 += dbytes; |
|
|
if (s->type == XVID_TYPE_BVOP) |
|
|
dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1]; |
|
|
|
|
|
if (rc->param.use_alt_curve) { |
|
424 |
if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
|
|
|
425 |
if (dbytes >= rc->alt_curve_high) { |
if (dbytes >= rc->alt_curve_high) { |
426 |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev); |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev); |
427 |
}else{ |
}else{ |
428 |
switch(rc->param.alt_curve_type) { |
switch(rc->param.alt_curve_type) { |
429 |
case XVID_CURVE_SINE : |
case XVID_CURVE_SINE : |
430 |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))); |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))); |
431 |
break; |
break; |
432 |
case XVID_CURVE_LINEAR : |
case XVID_CURVE_LINEAR : |
433 |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff); |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff); |
434 |
break; |
break; |
435 |
case XVID_CURVE_COSINE : |
case XVID_CURVE_COSINE : |
436 |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)))); |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)))); |
437 |
} |
} |
438 |
} |
} |
439 |
}else{ |
}else{ |
440 |
if (dbytes <= rc->alt_curve_low) { |
if (dbytes <= rc->alt_curve_low) { |
441 |
dbytes2 = dbytes; |
curve_temp = dbytes; |
442 |
}else{ |
}else{ |
443 |
switch(rc->param.alt_curve_type) { |
switch(rc->param.alt_curve_type) { |
444 |
case XVID_CURVE_SINE : |
case XVID_CURVE_SINE : |
445 |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))); |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))); |
446 |
break; |
break; |
447 |
case XVID_CURVE_LINEAR : |
case XVID_CURVE_LINEAR : |
448 |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff); |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff); |
449 |
break; |
break; |
450 |
case XVID_CURVE_COSINE : |
case XVID_CURVE_COSINE : |
451 |
dbytes2 = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)))); |
curve_temp = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)))); |
452 |
} |
} |
453 |
} |
} |
|
|
|
454 |
} |
} |
455 |
|
|
456 |
|
/* |
457 |
|
* End of code path for curve_temp, as told earlier, we are now |
458 |
|
* obliged to scale the value to a bframe one using the inverse |
459 |
|
* ratio applied earlier |
460 |
|
*/ |
461 |
|
if (s->type == XVID_TYPE_BVOP) |
462 |
|
curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
463 |
|
|
464 |
}else{ |
curve_temp = curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus; |
|
if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
|
|
dbytes2=((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0); |
|
|
}else{ |
|
|
dbytes2 = ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0); |
|
|
} |
|
|
} |
|
465 |
|
|
466 |
if (s->type == XVID_TYPE_BVOP) { |
desired += ((int)curve_temp); |
467 |
dbytes2 *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
rc->curve_comp_error += curve_temp - (int)curve_temp; |
|
if (dbytes2 < rc->min_length[XVID_TYPE_BVOP-1]) |
|
|
dbytes2 = rc->min_length[XVID_TYPE_BVOP-1]; |
|
468 |
}else{ |
}else{ |
469 |
if (dbytes2 < rc->min_length[XVID_TYPE_PVOP-1]) |
/* |
470 |
dbytes2 = rc->min_length[XVID_TYPE_PVOP-1]; |
* End of code path for dbytes, as told earlier, we are now |
471 |
} |
* obliged to scale the value to a bframe one using the inverse |
472 |
total2 += dbytes2; |
* ratio applied earlier |
473 |
} |
*/ |
474 |
|
if (s->type == XVID_TYPE_BVOP) |
475 |
|
dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
476 |
|
|
477 |
|
desired += ((int)dbytes); |
478 |
|
rc->curve_comp_error += dbytes - (int)dbytes; |
479 |
} |
} |
480 |
|
|
481 |
rc->curve_comp_scale = total1 / total2; |
} else if ((rc->param.curve_compression_high + rc->param.curve_compression_low) && s->type != XVID_TYPE_IVOP) { |
482 |
|
|
483 |
if (!rc->param.use_alt_curve) { |
curve_temp = rc->curve_comp_scale; |
484 |
DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: %i\n", |
if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
485 |
(int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale)); |
curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0); |
486 |
|
} else { |
487 |
|
curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0); |
488 |
} |
} |
489 |
|
|
490 |
if (rc->param.use_alt_curve) { |
/* |
491 |
int bonus_bias = rc->param.alt_curve_bonus_bias; |
* End of code path for curve_temp, as told earlier, we are now |
492 |
int oldquant = 1; |
* obliged to scale the value to a bframe one using the inverse |
493 |
|
* ratio applied earlier |
494 |
if (rc->param.alt_curve_use_auto_bonus_bias) |
*/ |
495 |
bonus_bias = rc->param.alt_curve_min_rel_qual; |
if (s->type == XVID_TYPE_BVOP) |
496 |
|
curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
|
rc->alt_curve_curve_bias_bonus = (total1 - total2) * (double)bonus_bias / 100.0 / (double)(rc->num_frames /* - credits_frames */ - rc->num_keyframes); |
|
|
rc->curve_comp_scale = ((total1 - total2) * (1.0 - (double)bonus_bias / 100.0) + total2) / total2; |
|
|
|
|
497 |
|
|
498 |
/* special info for alt curve: bias bonus and quantizer thresholds */ |
desired += (int)curve_temp; |
499 |
|
rc->curve_comp_error += curve_temp - (int)curve_temp; |
500 |
|
} else { |
501 |
|
/* |
502 |
|
* End of code path for dbytes, as told earlier, we are now |
503 |
|
* obliged to scale the value to a bframe one using the inverse |
504 |
|
* ratio applied earlier |
505 |
|
*/ |
506 |
|
if (s->type == XVID_TYPE_BVOP) |
507 |
|
dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
508 |
|
|
509 |
DPRINTF(XVID_DEBUG_RC, "avg scaled framesize:%i\n", (int)rc->avg_length[XVID_TYPE_PVOP-1]); |
desired += (int)dbytes; |
510 |
DPRINTF(XVID_DEBUG_RC, "bias bonus:%i bytes\n", (int)rc->alt_curve_curve_bias_bonus); |
rc->curve_comp_error += dbytes - (int)dbytes; |
511 |
|
} |
512 |
|
|
|
for (i=1; i <= (int)(rc->alt_curve_high*2)+1; i++) { |
|
|
double curve_temp, dbytes; |
|
|
int newquant; |
|
513 |
|
|
514 |
dbytes = i; |
/* |
515 |
if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
* We can't do bigger frames than first pass, this would be stupid as first |
516 |
if (dbytes >= rc->alt_curve_high) { |
* pass is quant=2 and that reaching quant=1 is not worth it. We would lose |
517 |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev); |
* many bytes and we would not not gain much quality. |
518 |
}else{ |
*/ |
519 |
switch(rc->param.alt_curve_type) |
if (desired > s->length) { |
520 |
{ |
rc->curve_comp_error += desired - s->length; |
521 |
case XVID_CURVE_SINE : |
desired = s->length; |
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))); |
|
|
break; |
|
|
case XVID_CURVE_LINEAR : |
|
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff); |
|
|
break; |
|
|
case XVID_CURVE_COSINE : |
|
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)))); |
|
|
} |
|
|
} |
|
|
}else{ |
|
|
if (dbytes <= rc->alt_curve_low) { |
|
|
curve_temp = dbytes; |
|
522 |
}else{ |
}else{ |
523 |
switch(rc->param.alt_curve_type) |
if (desired < rc->min_length[s->type-1]) { |
524 |
{ |
if (s->type == XVID_TYPE_IVOP){ |
525 |
case XVID_CURVE_SINE : |
rc->curve_comp_error -= rc->min_length[XVID_TYPE_IVOP-1] - desired; |
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))); |
|
|
break; |
|
|
case XVID_CURVE_LINEAR : |
|
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff); |
|
|
break; |
|
|
case XVID_CURVE_COSINE : |
|
|
curve_temp = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)))); |
|
526 |
} |
} |
527 |
|
desired = rc->min_length[s->type-1]; |
528 |
} |
} |
529 |
} |
} |
530 |
|
|
531 |
if (rc->movie_curve > 1.0) |
s->desired_length = desired; |
|
dbytes *= rc->movie_curve; |
|
532 |
|
|
533 |
newquant = (int)(dbytes * 2.0 / (curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus)); |
|
534 |
if (newquant > 1) { |
/* if this keyframe is too close to the next, reduce it's byte allotment |
535 |
if (newquant != oldquant) { |
XXX: why do we do this after setting the desired length */ |
536 |
int percent = (int)((i - rc->avg_length[XVID_TYPE_PVOP-1]) * 100.0 / rc->avg_length[XVID_TYPE_PVOP-1]); |
|
537 |
oldquant = newquant; |
if (s->type == XVID_TYPE_IVOP) { |
538 |
DPRINTF(XVID_DEBUG_RC, "quant:%i threshold at %i : %i percent\n", newquant, i, percent); |
int KFdistance = rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1]; |
539 |
|
|
540 |
|
if (KFdistance < rc->param.kftreshold) { |
541 |
|
|
542 |
|
KFdistance -= rc->param.min_key_interval; |
543 |
|
|
544 |
|
if (KFdistance >= 0) { |
545 |
|
int KF_min_size; |
546 |
|
|
547 |
|
KF_min_size = desired * (100 - rc->param.kfreduction) / 100; |
548 |
|
if (KF_min_size < 1) |
549 |
|
KF_min_size = 1; |
550 |
|
|
551 |
|
desired = KF_min_size + (desired - KF_min_size) * KFdistance / |
552 |
|
(rc->param.kftreshold - rc->param.min_key_interval); |
553 |
|
|
554 |
|
if (desired < 1) |
555 |
|
desired = 1; |
556 |
} |
} |
557 |
} |
} |
558 |
} |
} |
559 |
|
|
560 |
|
overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]); |
561 |
|
|
562 |
|
/* Reign in overflow with huge frames */ |
563 |
|
if (labs(overflow) > labs(rc->overflow)) { |
564 |
|
overflow = rc->overflow; |
565 |
} |
} |
566 |
|
|
567 |
rc->overflow = 0; |
/* Make sure overflow doesn't run away */ |
568 |
rc->KFoverflow = 0; |
if (overflow > desired * rc->param.max_overflow_improvement / 100) { |
569 |
rc->KFoverflow_partial = 0; |
desired += (overflow <= desired) ? desired * rc->param.max_overflow_improvement / 100 : |
570 |
rc->KF_idx = 1; |
overflow * rc->param.max_overflow_improvement / 100; |
571 |
|
} else if (overflow < desired * rc->param.max_overflow_degradation / -100){ |
572 |
|
desired += desired * rc->param.max_overflow_degradation / -100; |
573 |
|
} else { |
574 |
|
desired += overflow; |
575 |
} |
} |
576 |
|
|
577 |
|
/* Make sure we are not higher than desired frame size */ |
578 |
|
if (desired > rc->max_length) { |
579 |
|
capped_to_max_framesize = 1; |
580 |
|
desired = rc->max_length; |
581 |
|
DPRINTF(XVID_DEBUG_RC,"[%i] Capped to maximum frame size\n", |
582 |
|
data->frame_num); |
583 |
|
} |
584 |
|
|
585 |
|
/* Make sure to not scale below the minimum framesize */ |
586 |
|
if (desired < rc->min_length[s->type-1]) { |
587 |
|
desired = rc->min_length[s->type-1]; |
588 |
|
DPRINTF(XVID_DEBUG_RC,"[%i] Capped to minimum frame size\n", |
589 |
|
data->frame_num); |
590 |
|
} |
591 |
|
|
592 |
|
/* |
593 |
|
* Don't laugh at this very 'simple' quant<->filesize relationship, it |
594 |
|
* proves to be acurate enough for our algorithm |
595 |
|
*/ |
596 |
|
data->quant = s->quant*s->length/desired; |
597 |
|
|
598 |
static int rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t ** handle) |
/* Let's clip the computed quantizer, if needed */ |
599 |
{ |
if (data->quant < 1) { |
600 |
xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param; |
data->quant = 1; |
601 |
rc_2pass2_t * rc; |
} else if (data->quant > 31) { |
602 |
int i; |
data->quant = 31; |
603 |
|
} else if (s->type != XVID_TYPE_IVOP) { |
604 |
|
|
605 |
rc = malloc(sizeof(rc_2pass2_t)); |
/* |
606 |
if (rc == NULL) |
* The frame quantizer has not been clipped, this appear to be a good |
607 |
return XVID_ERR_MEMORY; |
* computed quantizer, however past frames give us some info about how |
608 |
|
* this quantizer performs against the algo prevision. Let's use this |
609 |
|
* prevision to increase the quantizer when we observe a too big |
610 |
|
* accumulated error |
611 |
|
*/ |
612 |
|
if (s->type == XVID_TYPE_BVOP) { |
613 |
|
rc->bquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant; |
614 |
|
|
615 |
rc->param = *param; |
if (rc->bquant_error[data->quant] >= 1.0) { |
616 |
|
rc->bquant_error[data->quant] -= 1.0; |
617 |
|
data->quant++; |
618 |
|
} |
619 |
|
} else { |
620 |
|
rc->pquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant; |
621 |
|
|
622 |
if (rc->param.keyframe_boost <= 0) rc->param.keyframe_boost = 0; |
if (rc->pquant_error[data->quant] >= 1.0) { |
623 |
if (rc->param.payback_method <= 0) rc->param.payback_method = XVID_PAYBACK_PROP; |
rc->pquant_error[data->quant] -= 1.0; |
624 |
if (rc->param.bitrate_payback_delay <= 0) rc->param.bitrate_payback_delay = 250; |
data->quant++; |
625 |
if (rc->param.curve_compression_high <= 0) rc->param.curve_compression_high = 0; |
} |
626 |
if (rc->param.curve_compression_low <= 0) rc->param.curve_compression_low = 0; |
} |
627 |
if (rc->param.max_overflow_improvement <= 0) rc->param.max_overflow_improvement = 60; |
} |
|
if (rc->param.max_overflow_degradation <= 0) rc->param.max_overflow_degradation = 60; |
|
|
|
|
|
if (rc->param.use_alt_curve <= 0) rc->param.use_alt_curve = 0; |
|
|
if (rc->param.alt_curve_high_dist <= 0) rc->param.alt_curve_high_dist = 500; |
|
|
if (rc->param.alt_curve_low_dist <= 0) rc->param.alt_curve_low_dist = 90; |
|
|
if (rc->param.alt_curve_use_auto <= 0) rc->param.alt_curve_use_auto = 1; |
|
|
if (rc->param.alt_curve_auto_str <= 0) rc->param.alt_curve_auto_str = 30; |
|
|
if (rc->param.alt_curve_type <= 0) rc->param.alt_curve_type = XVID_CURVE_LINEAR; |
|
|
if (rc->param.alt_curve_min_rel_qual <= 0) rc->param.alt_curve_min_rel_qual = 50; |
|
|
if (rc->param.alt_curve_use_auto_bonus_bias <= 0) rc->param.alt_curve_use_auto_bonus_bias = 1; |
|
|
if (rc->param.alt_curve_bonus_bias <= 0) rc->param.alt_curve_bonus_bias = 50; |
|
|
|
|
|
if (rc->param.kftreshold <= 0) rc->param.kftreshold = 10; |
|
|
if (rc->param.kfreduction <= 0) rc->param.kfreduction = 20; |
|
|
if (rc->param.min_key_interval <= 0) rc->param.min_key_interval = 300; |
|
628 |
|
|
629 |
if (!det_stats_length(rc, param->filename)){ |
/* |
630 |
DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename); |
* Now we have a computed quant that is in the right quante range, with a |
631 |
free(rc); |
* possible +1 correction due to cumulated error. We can now safely clip |
632 |
return XVID_ERR_FAIL; |
* the quantizer again with user's quant ranges. "Safely" means the Rate |
633 |
|
* Control could learn more about this quantizer, this knowledge is useful |
634 |
|
* for future frames even if it this quantizer won't be really used atm, |
635 |
|
* that's why we don't perform this clipping earlier. |
636 |
|
*/ |
637 |
|
if (data->quant < data->min_quant[s->type-1]) { |
638 |
|
data->quant = data->min_quant[s->type-1]; |
639 |
|
} else if (data->quant > data->max_quant[s->type-1]) { |
640 |
|
data->quant = data->max_quant[s->type-1]; |
641 |
} |
} |
642 |
|
|
643 |
if ((rc->stats = malloc(rc->num_frames * sizeof(stat_t))) == NULL) { |
/* |
644 |
free(rc); |
* To avoid big quality jumps from frame to frame, we apply a "security" |
645 |
return XVID_ERR_MEMORY; |
* rule that makes |last_quant - new_quant| <= 2. This rule only applies |
646 |
|
* to predicted frames (P and B) |
647 |
|
*/ |
648 |
|
if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) { |
649 |
|
|
650 |
|
if (data->quant > rc->last_quant[s->type-1] + 2) { |
651 |
|
data->quant = rc->last_quant[s->type-1] + 2; |
652 |
|
DPRINTF(XVID_DEBUG_RC, |
653 |
|
"[%i] p/b-frame quantizer prevented from rising too steeply\n", |
654 |
|
data->frame_num); |
655 |
|
} |
656 |
|
if (data->quant < rc->last_quant[s->type-1] - 2) { |
657 |
|
data->quant = rc->last_quant[s->type-1] - 2; |
658 |
|
DPRINTF(XVID_DEBUG_RC, |
659 |
|
"[%i] p/b-frame quantizer prevented from falling too steeply\n", |
660 |
|
data->frame_num); |
661 |
|
} |
662 |
} |
} |
663 |
|
|
664 |
/* |
/* |
665 |
* We need an extra location because we do as if the last frame were an |
* We don't want to pollute the RC history results when our computed quant |
666 |
* IFrame. This is needed because our code consider that frames between |
* has been computed from a capped frame size |
|
* 2 IFrames form a natural sequence. So we store last frame as a |
|
|
* keyframe location. |
|
667 |
*/ |
*/ |
668 |
if ((rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int))) == NULL) { |
if (capped_to_max_framesize == 0) { |
669 |
free(rc->stats); |
rc->last_quant[s->type-1] = data->quant; |
|
free(rc); |
|
|
return XVID_ERR_MEMORY; |
|
670 |
} |
} |
671 |
|
|
672 |
if (!load_stats(rc, param->filename)) { |
return 0; |
|
DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename); |
|
|
free(rc->keyframe_locations); |
|
|
free(rc->stats); |
|
|
free(rc); |
|
|
return XVID_ERR_FAIL; |
|
673 |
} |
} |
674 |
|
|
675 |
/* pre-process our stats */ |
/*---------------------------------------------------------------------------- |
676 |
|
*--------------------------------------------------------------------------*/ |
677 |
|
|
678 |
if (rc->num_frames < create->fbase/create->fincr) { |
static int |
679 |
rc->target = rc->param.bitrate / 8; /* one second */ |
rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data) |
680 |
}else{ |
{ |
681 |
rc->target = |
stat_t * s = &rc->stats[data->frame_num]; |
|
((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * (uint64_t)create->fincr) / \ |
|
|
((uint64_t)create->fbase * 8); |
|
|
} |
|
682 |
|
|
683 |
DPRINTF(XVID_DEBUG_RC, "Number of frames: %d\n", rc->num_frames); |
/* Insufficent stats data */ |
684 |
DPRINTF(XVID_DEBUG_RC, "Frame rate: %d/%d\n", create->fbase, create->fincr); |
if (data->frame_num >= rc->num_frames) |
685 |
DPRINTF(XVID_DEBUG_RC, "Target bitrate: %ld\n", rc->param.bitrate); |
return 0; |
|
DPRINTF(XVID_DEBUG_RC, "Target filesize: %lld\n", rc->target); |
|
686 |
|
|
687 |
/* Compensate the mean frame overhead caused by the container */ |
rc->quant_count[data->quant]++; |
|
rc->target -= rc->num_frames*rc->param.container_frame_overhead; |
|
|
DPRINTF(XVID_DEBUG_RC, "Container Frame overhead: %d\n", rc->param.container_frame_overhead); |
|
|
DPRINTF(XVID_DEBUG_RC, "Target filesize (after container compensation): %lld\n", rc->target); |
|
688 |
|
|
689 |
pre_process0(rc); |
if (data->type == XVID_TYPE_IVOP) { |
690 |
|
int kfdiff = (rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1]); |
691 |
|
|
692 |
if (rc->param.bitrate) { |
rc->overflow += rc->KFoverflow; |
693 |
zone_process(rc, create); |
rc->KFoverflow = s->desired_length - data->length; |
|
internal_scale(rc); |
|
|
}else{ |
|
|
/* external scaler: ignore zone */ |
|
|
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; |
|
|
} |
|
|
pre_process1(rc); |
|
694 |
|
|
695 |
for (i=0; i<32;i++) { |
if (kfdiff > 1) { // non-consecutive keyframes |
696 |
rc->pquant_error[i] = 0; |
rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1); |
697 |
rc->bquant_error[i] = 0; |
}else{ // consecutive keyframes |
698 |
rc->quant_count[i] = 0; |
rc->overflow += rc->KFoverflow; |
699 |
|
rc->KFoverflow = 0; |
700 |
|
rc->KFoverflow_partial = 0; |
701 |
|
} |
702 |
|
rc->KF_idx++; |
703 |
|
} else { |
704 |
|
// distribute part of the keyframe overflow |
705 |
|
rc->overflow += s->desired_length - data->length + rc->KFoverflow_partial; |
706 |
|
rc->KFoverflow -= rc->KFoverflow_partial; |
707 |
} |
} |
708 |
|
|
709 |
rc->fq_error = 0; |
DPRINTF(XVID_DEBUG_RC, "[%i] quant:%i stats1:%i scaled:%i actual:%i overflow:%i\n", |
710 |
|
data->frame_num, |
711 |
|
data->quant, |
712 |
|
s->length, |
713 |
|
s->scaled_length, |
714 |
|
data->length, |
715 |
|
rc->overflow); |
716 |
|
|
|
*handle = rc; |
|
717 |
return(0); |
return(0); |
718 |
} |
} |
719 |
|
|
720 |
|
/***************************************************************************** |
721 |
|
* Helper functions definition |
722 |
|
****************************************************************************/ |
723 |
|
|
724 |
|
#define BUF_SZ 1024 |
725 |
|
#define MAX_COLS 5 |
726 |
|
|
727 |
static int rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy) |
/* open stats file, and count num frames */ |
728 |
|
static int |
729 |
|
det_stats_length(rc_2pass2_t * rc, char * filename) |
730 |
{ |
{ |
731 |
free(rc->keyframe_locations); |
FILE * f; |
732 |
free(rc->stats); |
int n, ignore; |
733 |
free(rc); |
char type; |
734 |
return(0); |
|
735 |
|
rc->num_frames = 0; |
736 |
|
rc->num_keyframes = 0; |
737 |
|
|
738 |
|
if ((f = fopen(filename, "rt")) == NULL) |
739 |
|
return 0; |
740 |
|
|
741 |
|
while((n = fscanf(f, "%c %d %d %d %d %d %d\n", |
742 |
|
&type, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) { |
743 |
|
if (type == 'i') { |
744 |
|
rc->num_frames++; |
745 |
|
rc->num_keyframes++; |
746 |
|
}else if (type == 'p' || type == 'b' || type == 's') { |
747 |
|
rc->num_frames++; |
748 |
|
} |
749 |
} |
} |
750 |
|
|
751 |
|
fclose(f); |
752 |
|
|
753 |
|
return 1; |
754 |
|
} |
755 |
|
|
756 |
|
/* open stats file(s) and read into rc->stats array */ |
757 |
|
|
758 |
static int rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data) |
static int |
759 |
|
load_stats(rc_2pass2_t *rc, char * filename) |
760 |
{ |
{ |
761 |
stat_t * s = &rc->stats[data->frame_num]; |
FILE * f; |
762 |
int overflow; |
int i, not_scaled; |
|
int desired; |
|
|
double dbytes; |
|
|
double curve_temp; |
|
|
int capped_to_max_framesize = 0; |
|
763 |
|
|
|
/* |
|
|
* This function is quite long but easy to understand. In order to simplify |
|
|
* the code path (a bit), we treat 3 cases that can return immediatly. |
|
|
*/ |
|
764 |
|
|
765 |
/* First case: Another plugin has already set a quantizer */ |
if ((f = fopen(filename, "rt"))==NULL) |
766 |
if (data->quant > 0) |
return 0; |
|
return(0); |
|
767 |
|
|
768 |
/* Second case: We are in a Quant zone */ |
i = 0; |
769 |
if (s->zone_mode == XVID_ZONE_QUANT) { |
not_scaled = 0; |
770 |
|
while(i < rc->num_frames) { |
771 |
|
stat_t * s = &rc->stats[i]; |
772 |
|
int n; |
773 |
|
char type; |
774 |
|
|
775 |
rc->fq_error += s->weight; |
s->scaled_length = 0; |
776 |
data->quant = (int)rc->fq_error; |
n = fscanf(f, "%c %d %d %d %d %d %d\n", &type, &s->quant, &s->blks[0], &s->blks[1], &s->blks[2], &s->length, &s->scaled_length); |
777 |
rc->fq_error -= data->quant; |
if (n == EOF) break; |
778 |
|
if (n < 7) { |
779 |
|
not_scaled = 1; |
780 |
|
} |
781 |
|
|
782 |
s->desired_length = s->length; |
if (type == 'i') { |
783 |
|
s->type = XVID_TYPE_IVOP; |
784 |
|
}else if (type == 'p' || type == 's') { |
785 |
|
s->type = XVID_TYPE_PVOP; |
786 |
|
}else if (type == 'b') { |
787 |
|
s->type = XVID_TYPE_BVOP; |
788 |
|
}else{ /* unknown type */ |
789 |
|
DPRINTF(XVID_DEBUG_RC, "unknown stats frame type; assuming pvop\n"); |
790 |
|
s->type = XVID_TYPE_PVOP; |
791 |
|
} |
792 |
|
|
793 |
return(0); |
i++; |
794 |
|
} |
795 |
|
|
796 |
|
rc->num_frames = i; |
797 |
|
|
798 |
|
fclose(f); |
799 |
|
|
800 |
|
return 1; |
801 |
} |
} |
802 |
|
|
803 |
/* Third case: insufficent stats data */ |
#if 0 |
804 |
if (data->frame_num >= rc->num_frames) |
static void print_stats(rc_2pass2_t * rc) |
805 |
return 0; |
{ |
806 |
|
int i; |
807 |
|
DPRINTF(XVID_DEBUG_RC, "type quant length scaled_length\n"); |
808 |
|
for (i = 0; i < rc->num_frames; i++) { |
809 |
|
stat_t * s = &rc->stats[i]; |
810 |
|
DPRINTF(XVID_DEBUG_RC, "%d %d %d %d\n", s->type, s->quant, s->length, s->scaled_length); |
811 |
|
} |
812 |
|
} |
813 |
|
#endif |
814 |
|
|
815 |
/* |
/* pre-process the statistics data |
816 |
* The last case is the one every normal minded developer should fear to |
- for each type, count, tot_length, min_length, max_length |
817 |
* maintain in a project :-) |
- set keyframes_locations |
818 |
*/ |
*/ |
819 |
|
|
820 |
/* XXX: why by 8 */ |
static void |
821 |
overflow = rc->overflow / 8; |
pre_process0(rc_2pass2_t * rc) |
822 |
|
{ |
823 |
|
int i,j; |
824 |
|
|
825 |
/* |
for (i=0; i<3; i++) { |
826 |
* The rc->overflow field represents the overflow in current scene (between two |
rc->count[i]=0; |
827 |
* IFrames) so we must not forget to reset it if we are enetring a new scene |
rc->tot_length[i] = 0; |
828 |
*/ |
rc->last_quant[i] = 0; |
829 |
if (s->type == XVID_TYPE_IVOP) { |
rc->min_length[i] = INT_MAX; |
|
overflow = 0; |
|
830 |
} |
} |
831 |
|
|
832 |
desired = s->scaled_length; |
rc->max_length = INT_MIN; |
833 |
|
|
834 |
|
for (i=j=0; i<rc->num_frames; i++) { |
835 |
|
stat_t * s = &rc->stats[i]; |
836 |
|
|
837 |
|
rc->count[s->type-1]++; |
838 |
|
rc->tot_length[s->type-1] += s->length; |
839 |
|
|
840 |
|
if (s->length < rc->min_length[s->type-1]) { |
841 |
|
rc->min_length[s->type-1] = s->length; |
842 |
|
} |
843 |
|
|
844 |
|
if (s->length > rc->max_length) { |
845 |
|
rc->max_length = s->length; |
846 |
|
} |
847 |
|
|
|
dbytes = desired; |
|
848 |
if (s->type == XVID_TYPE_IVOP) { |
if (s->type == XVID_TYPE_IVOP) { |
849 |
dbytes += desired * rc->param.keyframe_boost / 100; |
rc->keyframe_locations[j] = i; |
850 |
|
j++; |
851 |
|
} |
852 |
} |
} |
|
dbytes /= rc->movie_curve; |
|
853 |
|
|
854 |
/* |
/* |
855 |
* We are now entering in the hard part of the algo, it was first designed |
* Nota Bene: |
856 |
* to work with i/pframes only streams, so the way it computes things is |
* The "per sequence" overflow system considers a natural sequence to be |
857 |
* adapted to pframes only. However we can use it if we just take care to |
* formed by all frames between two iframes, so if we want to make sure |
858 |
* scale the bframes sizes to pframes sizes using the ratio avg_p/avg_p and |
* the system does not go nuts during last sequence, we force the last |
859 |
* then before really using values depending on frame sizes, scaling the |
* frame to appear in the keyframe locations array. |
|
* value again with the inverse ratio |
|
860 |
*/ |
*/ |
861 |
if (s->type == XVID_TYPE_BVOP) { |
rc->keyframe_locations[j] = i; |
862 |
dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1]; |
|
863 |
|
DPRINTF(XVID_DEBUG_RC, "Min 1st pass IFrame length: %d\n", rc->min_length[0]); |
864 |
|
DPRINTF(XVID_DEBUG_RC, "Min 1st pass PFrame length: %d\n", rc->min_length[1]); |
865 |
|
DPRINTF(XVID_DEBUG_RC, "Min 1st pass BFrame length: %d\n", rc->min_length[2]); |
866 |
} |
} |
867 |
|
|
868 |
/* |
/* calculate zone weight "center" */ |
869 |
* Apply user's choosen Payback method. Payback helps bitrate to follow the |
|
870 |
* scaled curve "paying back" past errors in curve previsions. |
static void |
871 |
*/ |
zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create) |
872 |
if (rc->param.payback_method == XVID_PAYBACK_BIAS) { |
{ |
873 |
desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay); |
int i,j; |
874 |
}else{ |
int n = 0; |
|
desired = (int)(rc->curve_comp_error * dbytes / |
|
|
rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay); |
|
875 |
|
|
876 |
if (labs(desired) > fabs(rc->curve_comp_error)) { |
rc->avg_weight = 0.0; |
877 |
desired = (int)rc->curve_comp_error; |
rc->tot_quant = 0; |
878 |
|
|
879 |
|
|
880 |
|
if (create->num_zones == 0) { |
881 |
|
for (j = 0; j < rc->num_frames; j++) { |
882 |
|
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
883 |
|
rc->stats[j].weight = 1.0; |
884 |
} |
} |
885 |
|
rc->avg_weight += rc->num_frames * 1.0; |
886 |
|
n += rc->num_frames; |
887 |
} |
} |
888 |
|
|
|
rc->curve_comp_error -= desired; |
|
889 |
|
|
890 |
/* |
for(i=0; i < create->num_zones; i++) { |
|
* Alt curve treatment is not that hard to understand though the formulas |
|
|
* seem to be huge. Alt treatment is basically a way to soft/harden the |
|
|
* curve flux applying sine/linear/cosine ratios |
|
|
*/ |
|
891 |
|
|
892 |
/* XXX: warning */ |
int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames; |
|
curve_temp = 0; |
|
893 |
|
|
894 |
if (rc->param.use_alt_curve) { |
if (i==0 && create->zones[i].frame > 0) { |
895 |
if (s->type != XVID_TYPE_IVOP) { |
for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) { |
896 |
if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
897 |
if (dbytes >= rc->alt_curve_high) { |
rc->stats[j].weight = 1.0; |
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev); |
|
|
} else { |
|
|
switch(rc->param.alt_curve_type) { |
|
|
case XVID_CURVE_SINE : |
|
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))); |
|
|
break; |
|
|
case XVID_CURVE_LINEAR : |
|
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff); |
|
|
break; |
|
|
case XVID_CURVE_COSINE : |
|
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)))); |
|
898 |
} |
} |
899 |
|
rc->avg_weight += create->zones[i].frame * 1.0; |
900 |
|
n += create->zones[i].frame; |
901 |
} |
} |
902 |
} else { |
|
903 |
if (dbytes <= rc->alt_curve_low){ |
if (create->zones[i].mode == XVID_ZONE_WEIGHT) { |
904 |
curve_temp = dbytes; |
for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { |
905 |
} else { |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
906 |
switch(rc->param.alt_curve_type) { |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
907 |
case XVID_CURVE_SINE : |
} |
908 |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))); |
next -= create->zones[i].frame; |
909 |
break; |
rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base; |
910 |
case XVID_CURVE_LINEAR : |
n += next; |
911 |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff); |
}else{ // XVID_ZONE_QUANT |
912 |
break; |
for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { |
913 |
case XVID_CURVE_COSINE : |
rc->stats[j].zone_mode = XVID_ZONE_QUANT; |
914 |
curve_temp = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)))); |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
915 |
|
rc->tot_quant += rc->stats[j].length; |
916 |
} |
} |
917 |
} |
} |
918 |
} |
} |
919 |
|
rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0; |
920 |
|
|
921 |
/* |
DPRINTF(XVID_DEBUG_RC, "center_weight: %f (for %i frames); fixed_bytes: %i\n", rc->avg_weight, n, rc->tot_quant); |
922 |
* End of code path for curve_temp, as told earlier, we are now |
} |
|
* obliged to scale the value to a bframe one using the inverse |
|
|
* ratio applied earlier |
|
|
*/ |
|
|
if (s->type == XVID_TYPE_BVOP) |
|
|
curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
|
923 |
|
|
|
curve_temp = curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus; |
|
924 |
|
|
925 |
desired += ((int)curve_temp); |
/* scale the curve */ |
|
rc->curve_comp_error += curve_temp - (int)curve_temp; |
|
|
} else { |
|
|
/* |
|
|
* End of code path for dbytes, as told earlier, we are now |
|
|
* obliged to scale the value to a bframe one using the inverse |
|
|
* ratio applied earlier |
|
|
*/ |
|
|
if (s->type == XVID_TYPE_BVOP) |
|
|
dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
|
926 |
|
|
927 |
desired += ((int)dbytes); |
static void |
928 |
rc->curve_comp_error += dbytes - (int)dbytes; |
internal_scale(rc_2pass2_t *rc) |
929 |
} |
{ |
930 |
|
int64_t target = rc->target - rc->tot_quant; |
931 |
|
int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant; |
932 |
|
double scaler; |
933 |
|
int i; |
934 |
|
|
|
} else if ((rc->param.curve_compression_high + rc->param.curve_compression_low) && s->type != XVID_TYPE_IVOP) { |
|
935 |
|
|
936 |
curve_temp = rc->curve_comp_scale; |
/* Let's compute a linear scaler in order to perform curve scaling */ |
937 |
if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
scaler = (double)target / (double)pass1_length; |
938 |
curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0); |
|
939 |
} else { |
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
940 |
curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0); |
DPRINTF(XVID_DEBUG_RC, "WARNING: Undersize detected\n"); |
941 |
|
scaler = 1.0; |
942 |
} |
} |
943 |
|
|
944 |
|
DPRINTF(XVID_DEBUG_RC, |
945 |
|
"Before correction: target=%i, tot_length=%i, scaler=%f\n", |
946 |
|
(int)target, (int)pass1_length, scaler); |
947 |
|
|
948 |
/* |
/* |
949 |
* End of code path for curve_temp, as told earlier, we are now |
* Perform an initial scale pass. |
950 |
* obliged to scale the value to a bframe one using the inverse |
* If a frame size is scaled underneath our hardcoded minimums, then we |
951 |
* ratio applied earlier |
* force the frame size to the minimum, and deduct the original & scaled |
952 |
|
* frame length from the original and target total lengths |
953 |
*/ |
*/ |
|
if (s->type == XVID_TYPE_BVOP) |
|
|
curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
|
954 |
|
|
955 |
desired += (int)curve_temp; |
for (i=0; i<rc->num_frames; i++) { |
956 |
rc->curve_comp_error += curve_temp - (int)curve_temp; |
stat_t * s = &rc->stats[i]; |
957 |
|
int min_size[3]; |
958 |
|
int len; |
959 |
|
|
960 |
|
/* Compute min frame lengths (oe for each frame type) */ |
961 |
|
min_size[0] = ((s->blks[0]*22) + 240) / 8; |
962 |
|
min_size[1] = (s->blks[0] + 88) / 8; |
963 |
|
min_size[2] = 8; |
964 |
|
|
965 |
|
if (s->zone_mode == XVID_ZONE_QUANT) { |
966 |
|
s->scaled_length = s->length; |
967 |
|
continue; |
968 |
|
} |
969 |
|
|
970 |
|
/* Compute teh scaled length */ |
971 |
|
len = (int)((double)s->length * scaler * s->weight / rc->avg_weight); |
972 |
|
|
973 |
|
/* Compare with the computed minimum */ |
974 |
|
if (len < min_size[s->type-1]) { |
975 |
|
/* force frame size to our computed minimum */ |
976 |
|
s->scaled_length = min_size[s->type-1]; |
977 |
|
target -= s->scaled_length; |
978 |
|
pass1_length -= s->length; |
979 |
} else { |
} else { |
980 |
/* |
/* Do nothing for now, we'll scale this later */ |
981 |
* End of code path for dbytes, as told earlier, we are now |
s->scaled_length = 0; |
|
* obliged to scale the value to a bframe one using the inverse |
|
|
* ratio applied earlier |
|
|
*/ |
|
|
if (s->type == XVID_TYPE_BVOP){ |
|
|
dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
|
982 |
} |
} |
983 |
|
|
|
desired += (int)dbytes; |
|
|
rc->curve_comp_error += dbytes - (int)dbytes; |
|
984 |
} |
} |
985 |
|
|
986 |
|
/* Correct the scaler for all non forced frames */ |
987 |
|
scaler = (double)target / (double)pass1_length; |
988 |
|
|
989 |
/* |
/* Detect undersizing */ |
990 |
* We can't do bigger frames than first pass, this would be stupid as first |
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
991 |
* pass is quant=2 and that reaching quant=1 is not worth it. We would lose |
DPRINTF(XVID_DEBUG_RC, "WARNING: Undersize detected\n"); |
992 |
* many bytes and we would not not gain much quality. |
scaler = 1.0; |
|
*/ |
|
|
if (desired > s->length) { |
|
|
rc->curve_comp_error += desired - s->length; |
|
|
desired = s->length; |
|
|
}else{ |
|
|
if (desired < rc->min_length[s->type-1]) { |
|
|
if (s->type == XVID_TYPE_IVOP){ |
|
|
rc->curve_comp_error -= rc->min_length[XVID_TYPE_IVOP-1] - desired; |
|
993 |
} |
} |
994 |
desired = rc->min_length[s->type-1]; |
|
995 |
|
DPRINTF(XVID_DEBUG_RC, |
996 |
|
"After correction: target=%i, tot_length=%i, scaler=%f\n", |
997 |
|
(int)target, (int)pass1_length, scaler); |
998 |
|
|
999 |
|
/* Do another pass with the new scaler */ |
1000 |
|
for (i=0; i<rc->num_frames; i++) { |
1001 |
|
stat_t * s = &rc->stats[i]; |
1002 |
|
|
1003 |
|
/* Ignore frame with forced frame sizes */ |
1004 |
|
if (s->scaled_length == 0) |
1005 |
|
s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight); |
1006 |
} |
} |
1007 |
|
|
1008 |
} |
} |
1009 |
|
|
1010 |
s->desired_length = desired; |
static void |
1011 |
|
pre_process1(rc_2pass2_t * rc) |
1012 |
|
{ |
1013 |
|
int i; |
1014 |
|
double total1, total2; |
1015 |
|
uint64_t ivop_boost_total; |
1016 |
|
|
1017 |
|
ivop_boost_total = 0; |
1018 |
|
rc->curve_comp_error = 0; |
1019 |
|
|
1020 |
/* if this keyframe is too close to the next, reduce it's byte allotment |
for (i=0; i<3; i++) { |
1021 |
XXX: why do we do this after setting the desired length */ |
rc->tot_scaled_length[i] = 0; |
1022 |
|
} |
1023 |
|
|
1024 |
|
for (i=0; i<rc->num_frames; i++) { |
1025 |
|
stat_t * s = &rc->stats[i]; |
1026 |
|
|
1027 |
|
rc->tot_scaled_length[s->type-1] += s->scaled_length; |
1028 |
|
|
1029 |
if (s->type == XVID_TYPE_IVOP) { |
if (s->type == XVID_TYPE_IVOP) { |
1030 |
int KFdistance = rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1]; |
ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100; |
1031 |
|
} |
1032 |
|
} |
1033 |
|
|
1034 |
if (KFdistance < rc->param.kftreshold) { |
rc->movie_curve = ((double)(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1] + ivop_boost_total) / |
1035 |
|
(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1])); |
1036 |
|
|
1037 |
KFdistance = KFdistance - rc->param.min_key_interval; |
for(i=0; i<3; i++) { |
1038 |
|
if (rc->count[i] == 0 || rc->movie_curve == 0) { |
1039 |
|
rc->avg_length[i] = 1; |
1040 |
|
}else{ |
1041 |
|
rc->avg_length[i] = rc->tot_scaled_length[i] / rc->count[i] / rc->movie_curve; |
1042 |
|
} |
1043 |
|
} |
1044 |
|
|
1045 |
if (KFdistance >= 0) { |
/* alt curve stuff here */ |
|
int KF_min_size; |
|
1046 |
|
|
1047 |
KF_min_size = desired * (100 - rc->param.kfreduction) / 100; |
if (rc->param.use_alt_curve) { |
1048 |
if (KF_min_size < 1) |
const double avg_pvop = rc->avg_length[XVID_TYPE_PVOP-1]; |
1049 |
KF_min_size = 1; |
const uint64_t tot_pvop = rc->tot_length[XVID_TYPE_PVOP-1]; |
1050 |
|
const uint64_t tot_bvop = rc->tot_length[XVID_TYPE_BVOP-1]; |
1051 |
|
const uint64_t tot_scaled_pvop = rc->tot_scaled_length[XVID_TYPE_PVOP-1]; |
1052 |
|
const uint64_t tot_scaled_bvop = rc->tot_scaled_length[XVID_TYPE_BVOP-1]; |
1053 |
|
|
1054 |
desired = KF_min_size + (desired - KF_min_size) * KFdistance / |
rc->alt_curve_low = avg_pvop - avg_pvop * (double)rc->param.alt_curve_low_dist / 100.0; |
1055 |
(rc->param.kftreshold - rc->param.min_key_interval); |
rc->alt_curve_low_diff = avg_pvop - rc->alt_curve_low; |
1056 |
|
rc->alt_curve_high = avg_pvop + avg_pvop * (double)rc->param.alt_curve_high_dist / 100.0; |
1057 |
|
rc->alt_curve_high_diff = rc->alt_curve_high - avg_pvop; |
1058 |
|
|
1059 |
if (desired < 1) |
if (rc->param.alt_curve_use_auto) { |
1060 |
desired = 1; |
if (tot_bvop + tot_pvop > tot_scaled_bvop + tot_scaled_pvop) { |
1061 |
|
rc->param.alt_curve_min_rel_qual = (int)(100.0 - (100.0 - 100.0 / |
1062 |
|
((double)(tot_pvop + tot_bvop) / (double)(tot_scaled_pvop + tot_scaled_bvop))) * (double)rc->param.alt_curve_auto_str / 100.0); |
1063 |
|
|
1064 |
|
if (rc->param.alt_curve_min_rel_qual < 20) |
1065 |
|
rc->param.alt_curve_min_rel_qual = 20; |
1066 |
|
}else{ |
1067 |
|
rc->param.alt_curve_min_rel_qual = 100; |
1068 |
|
} |
1069 |
} |
} |
1070 |
|
rc->alt_curve_mid_qual = (1.0 + (double)rc->param.alt_curve_min_rel_qual / 100.0) / 2.0; |
1071 |
|
rc->alt_curve_qual_dev = 1.0 - rc->alt_curve_mid_qual; |
1072 |
|
|
1073 |
|
if (rc->param.alt_curve_low_dist > 100) { |
1074 |
|
switch(rc->param.alt_curve_type) { |
1075 |
|
case XVID_CURVE_SINE: // Sine Curve (high aggressiveness) |
1076 |
|
rc->alt_curve_qual_dev *= 2.0 / (1.0 + sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff))); |
1077 |
|
rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)); |
1078 |
|
break; |
1079 |
|
case XVID_CURVE_LINEAR: // Linear (medium aggressiveness) |
1080 |
|
rc->alt_curve_qual_dev *= 2.0 / (1.0 + avg_pvop / rc->alt_curve_low_diff); |
1081 |
|
rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * avg_pvop / rc->alt_curve_low_diff; |
1082 |
|
break; |
1083 |
|
case XVID_CURVE_COSINE: // Cosine Curve (low aggressiveness) |
1084 |
|
rc->alt_curve_qual_dev *= 2.0 / (1.0 + (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)))); |
1085 |
|
rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff))); |
1086 |
} |
} |
1087 |
} |
} |
|
|
|
|
overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]); |
|
|
|
|
|
/* Reign in overflow with huge frames */ |
|
|
if (labs(overflow) > labs(rc->overflow)) { |
|
|
overflow = rc->overflow; |
|
1088 |
} |
} |
1089 |
|
|
1090 |
/* Make sure overflow doesn't run away */ |
/* --- */ |
|
if (overflow > desired * rc->param.max_overflow_improvement / 100) { |
|
|
desired += (overflow <= desired) ? desired * rc->param.max_overflow_improvement / 100 : |
|
|
overflow * rc->param.max_overflow_improvement / 100; |
|
|
} else if (overflow < desired * rc->param.max_overflow_degradation / -100){ |
|
|
desired += desired * rc->param.max_overflow_degradation / -100; |
|
|
} else { |
|
|
desired += overflow; |
|
|
} |
|
1091 |
|
|
1092 |
/* Make sure we are not higher than desired frame size */ |
total1=total2=0; |
|
if (desired > rc->max_length) { |
|
|
capped_to_max_framesize = 1; |
|
|
desired = rc->max_length; |
|
|
DPRINTF(XVID_DEBUG_RC,"[%i] Capped to maximum frame size\n", |
|
|
data->frame_num); |
|
|
} |
|
1093 |
|
|
1094 |
/* Make sure to not scale below the minimum framesize */ |
for (i=0; i<rc->num_frames; i++) { |
1095 |
if (desired < rc->min_length[s->type-1]) { |
stat_t * s = &rc->stats[i]; |
|
desired = rc->min_length[s->type-1]; |
|
|
DPRINTF(XVID_DEBUG_RC,"[%i] Capped to minimum frame size\n", |
|
|
data->frame_num); |
|
|
} |
|
1096 |
|
|
1097 |
/* |
if (s->type != XVID_TYPE_IVOP) { |
1098 |
* Don't laugh at this very 'simple' quant<->filesize relationship, it |
double dbytes,dbytes2; |
|
* proves to be acurate enough for our algorithm |
|
|
*/ |
|
|
data->quant= (s->quant * s->length) / desired; |
|
1099 |
|
|
1100 |
/* Let's clip the computed quantizer, if needed */ |
dbytes = s->scaled_length / rc->movie_curve; |
1101 |
if (data->quant < 1) { |
dbytes2 = 0; /* XXX: warning */ |
1102 |
data->quant = 1; |
total1 += dbytes; |
1103 |
} else if (data->quant > 31) { |
if (s->type == XVID_TYPE_BVOP) |
1104 |
data->quant = 31; |
dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1]; |
|
} else if (s->type != XVID_TYPE_IVOP) { |
|
1105 |
|
|
1106 |
/* |
if (rc->param.use_alt_curve) { |
1107 |
* The frame quantizer has not been clipped, this appear to be a good |
if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
|
* computed quantizer, however past frames give us some info about how |
|
|
* this quantizer performs against the algo prevision. Let's use this |
|
|
* prevision to increase the quantizer when we observe a too big |
|
|
* accumulated error |
|
|
*/ |
|
|
if (s->type== XVID_TYPE_BVOP) { |
|
|
rc->bquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant; |
|
1108 |
|
|
1109 |
if (rc->bquant_error[data->quant] >= 1.0) { |
if (dbytes >= rc->alt_curve_high) { |
1110 |
rc->bquant_error[data->quant] -= 1.0; |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev); |
|
data->quant++; |
|
|
} |
|
1111 |
} else { |
} else { |
1112 |
rc->pquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant; |
switch(rc->param.alt_curve_type) { |
1113 |
|
case XVID_CURVE_SINE : |
1114 |
if (rc->pquant_error[data->quant] >= 1.0) { |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))); |
1115 |
rc->pquant_error[data->quant] -= 1.0; |
break; |
1116 |
++data->quant; |
case XVID_CURVE_LINEAR : |
1117 |
|
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff); |
1118 |
|
break; |
1119 |
|
case XVID_CURVE_COSINE : |
1120 |
|
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)))); |
1121 |
|
} |
1122 |
} |
} |
1123 |
|
}else{ |
1124 |
|
if (dbytes <= rc->alt_curve_low) { |
1125 |
|
dbytes2 = dbytes; |
1126 |
|
}else{ |
1127 |
|
switch(rc->param.alt_curve_type) { |
1128 |
|
case XVID_CURVE_SINE : |
1129 |
|
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))); |
1130 |
|
break; |
1131 |
|
case XVID_CURVE_LINEAR : |
1132 |
|
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff); |
1133 |
|
break; |
1134 |
|
case XVID_CURVE_COSINE : |
1135 |
|
dbytes2 = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)))); |
1136 |
} |
} |
1137 |
} |
} |
1138 |
|
|
|
/* |
|
|
* Now we have a computed quant that is in the right quante range, with a |
|
|
* possible +1 correction due to cumulated error. We can now safely clip |
|
|
* the quantizer again with user's quant ranges. "Safely" means the Rate |
|
|
* Control could learn more about this quantizer, this knowledge is useful |
|
|
* for future frames even if it this quantizer won't be really used atm, |
|
|
* that's why we don't perform this clipping earlier. |
|
|
*/ |
|
|
if (data->quant < data->min_quant[s->type-1]) { |
|
|
data->quant = data->min_quant[s->type-1]; |
|
|
} else if (data->quant > data->max_quant[s->type-1]) { |
|
|
data->quant = data->max_quant[s->type-1]; |
|
1139 |
} |
} |
1140 |
|
|
|
/* |
|
|
* To avoid big quality jumps from frame to frame, we apply a "security" |
|
|
* rule that makes |last_quant - new_quant| <= 2. This rule only applies |
|
|
* to predicted frames (P and B) |
|
|
*/ |
|
|
if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) { |
|
1141 |
|
|
1142 |
if (data->quant > rc->last_quant[s->type-1] + 2) { |
}else{ |
1143 |
data->quant = rc->last_quant[s->type-1] + 2; |
if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
1144 |
DPRINTF(XVID_DEBUG_RC, |
dbytes2=((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0); |
1145 |
"[%i] p/b-frame quantizer prevented from rising too steeply\n", |
}else{ |
1146 |
data->frame_num); |
dbytes2 = ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0); |
|
} |
|
|
if (data->quant < rc->last_quant[s->type-1] - 2) { |
|
|
data->quant = rc->last_quant[s->type-1] - 2; |
|
|
DPRINTF(XVID_DEBUG_RC, |
|
|
"[%i] p/b-frame quantizer prevented from falling too steeply\n", |
|
|
data->frame_num); |
|
1147 |
} |
} |
1148 |
} |
} |
1149 |
|
|
1150 |
/* |
if (s->type == XVID_TYPE_BVOP) { |
1151 |
* We don't want to pollute the RC history results when our computed quant |
dbytes2 *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
1152 |
* has been computed from a capped frame size |
if (dbytes2 < rc->min_length[XVID_TYPE_BVOP-1]) |
1153 |
*/ |
dbytes2 = rc->min_length[XVID_TYPE_BVOP-1]; |
1154 |
if (capped_to_max_framesize == 0) { |
}else{ |
1155 |
rc->last_quant[s->type-1] = data->quant; |
if (dbytes2 < rc->min_length[XVID_TYPE_PVOP-1]) |
1156 |
|
dbytes2 = rc->min_length[XVID_TYPE_PVOP-1]; |
1157 |
|
} |
1158 |
|
total2 += dbytes2; |
1159 |
|
} |
1160 |
} |
} |
1161 |
|
|
1162 |
return 0; |
rc->curve_comp_scale = total1 / total2; |
1163 |
|
|
1164 |
|
if (!rc->param.use_alt_curve) { |
1165 |
|
DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: %i\n", |
1166 |
|
(int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale)); |
1167 |
} |
} |
1168 |
|
|
1169 |
|
if (rc->param.use_alt_curve) { |
1170 |
|
int bonus_bias = rc->param.alt_curve_bonus_bias; |
1171 |
|
int oldquant = 1; |
1172 |
|
|
1173 |
|
if (rc->param.alt_curve_use_auto_bonus_bias) |
1174 |
|
bonus_bias = rc->param.alt_curve_min_rel_qual; |
1175 |
|
|
1176 |
static int rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data) |
rc->alt_curve_curve_bias_bonus = (total1 - total2) * (double)bonus_bias / 100.0 / (double)(rc->num_frames /* - credits_frames */ - rc->num_keyframes); |
1177 |
{ |
rc->curve_comp_scale = ((total1 - total2) * (1.0 - (double)bonus_bias / 100.0) + total2) / total2; |
|
stat_t * s = &rc->stats[data->frame_num]; |
|
1178 |
|
|
|
/* Insufficent stats data */ |
|
|
if (data->frame_num >= rc->num_frames) |
|
|
return 0; |
|
1179 |
|
|
1180 |
rc->quant_count[data->quant]++; |
/* special info for alt curve: bias bonus and quantizer thresholds */ |
1181 |
|
|
1182 |
if (data->type == XVID_TYPE_IVOP) { |
DPRINTF(XVID_DEBUG_RC, "avg scaled framesize:%i\n", (int)rc->avg_length[XVID_TYPE_PVOP-1]); |
1183 |
int kfdiff = (rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1]); |
DPRINTF(XVID_DEBUG_RC, "bias bonus:%i bytes\n", (int)rc->alt_curve_curve_bias_bonus); |
1184 |
|
|
1185 |
rc->overflow += rc->KFoverflow; |
for (i=1; i <= (int)(rc->alt_curve_high*2)+1; i++) { |
1186 |
rc->KFoverflow = s->desired_length - data->length; |
double curve_temp, dbytes; |
1187 |
|
int newquant; |
1188 |
|
|
1189 |
if (kfdiff > 1) { // non-consecutive keyframes |
dbytes = i; |
1190 |
rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1); |
if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
1191 |
}else{ // consecutive keyframes |
if (dbytes >= rc->alt_curve_high) { |
1192 |
rc->overflow += rc->KFoverflow; |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev); |
|
rc->KFoverflow = 0; |
|
|
rc->KFoverflow_partial = 0; |
|
|
} |
|
|
rc->KF_idx++; |
|
1193 |
}else{ |
}else{ |
1194 |
// distribute part of the keyframe overflow |
switch(rc->param.alt_curve_type) |
1195 |
rc->overflow += s->desired_length - data->length + rc->KFoverflow_partial; |
{ |
1196 |
rc->KFoverflow -= rc->KFoverflow_partial; |
case XVID_CURVE_SINE : |
1197 |
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))); |
1198 |
|
break; |
1199 |
|
case XVID_CURVE_LINEAR : |
1200 |
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff); |
1201 |
|
break; |
1202 |
|
case XVID_CURVE_COSINE : |
1203 |
|
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)))); |
1204 |
} |
} |
|
|
|
|
DPRINTF(XVID_DEBUG_RC, "[%i] quant:%i stats1:%i scaled:%i actual:%i overflow:%i\n", |
|
|
data->frame_num, |
|
|
data->quant, |
|
|
s->length, |
|
|
s->scaled_length, |
|
|
data->length, |
|
|
rc->overflow); |
|
|
|
|
|
return(0); |
|
1205 |
} |
} |
1206 |
|
}else{ |
1207 |
|
if (dbytes <= rc->alt_curve_low) { |
1208 |
|
curve_temp = dbytes; |
1209 |
int xvid_plugin_2pass2(void * handle, int opt, void * param1, void * param2) |
}else{ |
1210 |
{ |
switch(rc->param.alt_curve_type) |
|
switch(opt) |
|
1211 |
{ |
{ |
1212 |
case XVID_PLG_INFO : |
case XVID_CURVE_SINE : |
1213 |
return 0; |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))); |
1214 |
|
break; |
1215 |
case XVID_PLG_CREATE : |
case XVID_CURVE_LINEAR : |
1216 |
return rc_2pass2_create((xvid_plg_create_t*)param1, param2); |
curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff); |
1217 |
|
break; |
1218 |
|
case XVID_CURVE_COSINE : |
1219 |
|
curve_temp = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)))); |
1220 |
|
} |
1221 |
|
} |
1222 |
|
} |
1223 |
|
|
1224 |
case XVID_PLG_DESTROY : |
if (rc->movie_curve > 1.0) |
1225 |
return rc_2pass2_destroy((rc_2pass2_t*)handle, (xvid_plg_destroy_t*)param1); |
dbytes *= rc->movie_curve; |
1226 |
|
|
1227 |
case XVID_PLG_BEFORE : |
newquant = (int)(dbytes * 2.0 / (curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus)); |
1228 |
return rc_2pass2_before((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1); |
if (newquant > 1) { |
1229 |
|
if (newquant != oldquant) { |
1230 |
|
int percent = (int)((i - rc->avg_length[XVID_TYPE_PVOP-1]) * 100.0 / rc->avg_length[XVID_TYPE_PVOP-1]); |
1231 |
|
oldquant = newquant; |
1232 |
|
DPRINTF(XVID_DEBUG_RC, "quant:%i threshold at %i : %i percent\n", newquant, i, percent); |
1233 |
|
} |
1234 |
|
} |
1235 |
|
} |
1236 |
|
|
|
case XVID_PLG_AFTER : |
|
|
return rc_2pass2_after((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1); |
|
1237 |
} |
} |
1238 |
|
|
1239 |
return XVID_ERR_FAIL; |
rc->overflow = 0; |
1240 |
|
rc->KFoverflow = 0; |
1241 |
|
rc->KFoverflow_partial = 0; |
1242 |
|
rc->KF_idx = 1; |
1243 |
} |
} |