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