22 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
23 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
24 |
* |
* |
25 |
* $Id: plugin_2pass2.c,v 1.1.2.1 2003-03-25 10:58:33 suxen_drol Exp $ |
* $Id: plugin_2pass2.c,v 1.1.2.6 2003-05-20 17:28:25 edgomez Exp $ |
26 |
* |
* |
27 |
*****************************************************************************/ |
*****************************************************************************/ |
28 |
|
|
38 |
typedef struct { |
typedef struct { |
39 |
int type; /* first pass type */ |
int type; /* first pass type */ |
40 |
int quant; /* first pass quant */ |
int quant; /* first pass quant */ |
41 |
|
int blks[3]; /* k,m,y blks */ |
42 |
int length; /* first pass length */ |
int length; /* first pass length */ |
43 |
int scaled_length; /* scaled length */ |
int scaled_length; /* scaled length */ |
44 |
int desired_length; |
int desired_length; /* desired length; calcuated during encoding */ |
45 |
|
|
46 |
|
int zone_mode; /* XVID_ZONE_xxx */ |
47 |
|
double weight; |
48 |
} stat_t; |
} stat_t; |
49 |
|
|
50 |
|
|
58 |
/* constant statistical data */ |
/* constant statistical data */ |
59 |
int num_frames; |
int num_frames; |
60 |
int num_keyframes; |
int num_keyframes; |
61 |
|
uint64_t target; /* target filesize */ |
62 |
|
|
63 |
int count[3]; /* count of each frame types */ |
int count[3]; /* count of each frame types */ |
64 |
uint64_t tot_length[3]; /* total length of each frame types */ |
uint64_t tot_length[3]; /* total length of each frame types */ |
67 |
uint64_t tot_scaled_length[3]; /* total scaled length of each frame type */ |
uint64_t tot_scaled_length[3]; /* total scaled length of each frame type */ |
68 |
int max_length; /* max frame size */ |
int max_length; /* max frame size */ |
69 |
|
|
70 |
|
/* zone statistical data */ |
71 |
|
double avg_weight; /* average weight */ |
72 |
|
int64_t tot_quant; /* total length used by XVID_ZONE_QUANT zones */ |
73 |
|
|
74 |
|
|
75 |
double curve_comp_scale; |
double curve_comp_scale; |
76 |
double movie_curve; |
double movie_curve; |
77 |
|
|
98 |
int KFoverflow; |
int KFoverflow; |
99 |
int KFoverflow_partial; |
int KFoverflow_partial; |
100 |
int KF_idx; |
int KF_idx; |
101 |
|
|
102 |
|
double fq_error; |
103 |
} rc_2pass2_t; |
} rc_2pass2_t; |
104 |
|
|
105 |
|
|
122 |
if ((f = fopen(filename, "rt")) == NULL) |
if ((f = fopen(filename, "rt")) == NULL) |
123 |
return 0; |
return 0; |
124 |
|
|
125 |
while((n = fscanf(f, "%c %d %d %d %d %d\n", |
while((n = fscanf(f, "%c %d %d %d %d %d %d\n", |
126 |
&type, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) { |
&type, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) { |
127 |
if (type == 'i') { |
if (type == 'i') { |
128 |
rc->num_frames++; |
rc->num_frames++; |
129 |
rc->num_keyframes++; |
rc->num_keyframes++; |
138 |
} |
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
/* open stats file(s) and read into rc->stats array */ |
/* open stats file(s) and read into rc->stats array */ |
143 |
|
|
144 |
static int load_stats(rc_2pass2_t *rc, char * filename1, char * filename2) |
static int load_stats(rc_2pass2_t *rc, char * filename) |
145 |
{ |
{ |
146 |
FILE * f1, *f2; |
FILE * f; |
147 |
int i; |
int i, not_scaled; |
|
|
|
148 |
|
|
|
if ((f1 = fopen(filename1, "rt"))==NULL) |
|
|
return 0; |
|
149 |
|
|
150 |
if ((f2 = fopen(filename2, "rt"))==NULL) { |
if ((f = fopen(filename, "rt"))==NULL) |
|
fclose(f1); |
|
151 |
return 0; |
return 0; |
|
} |
|
152 |
|
|
153 |
i = 0; |
i = 0; |
154 |
|
not_scaled = 0; |
155 |
while(i < rc->num_frames) { |
while(i < rc->num_frames) { |
156 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
157 |
int n, ignore; |
int n; |
158 |
char type; |
char type; |
159 |
|
|
160 |
n = fscanf(f1, "%c %d %d %d %d %d\n", &type, &s->quant, &s->length, &ignore, &ignore, &ignore); |
s->scaled_length = 0; |
161 |
|
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); |
162 |
if (n == EOF) break; |
if (n == EOF) break; |
163 |
|
if (n < 7) { |
164 |
|
not_scaled = 1; |
165 |
|
} |
166 |
|
|
167 |
if (type == 'i') { |
if (type == 'i') { |
168 |
s->type = XVID_TYPE_IVOP; |
s->type = XVID_TYPE_IVOP; |
171 |
}else if (type == 'b') { |
}else if (type == 'b') { |
172 |
s->type = XVID_TYPE_BVOP; |
s->type = XVID_TYPE_BVOP; |
173 |
}else{ /* unknown type */ |
}else{ /* unknown type */ |
174 |
printf("unk\n"); |
DPRINTF(XVID_DEBUG_RC, "unknown stats frame type; assuming pvop"); |
175 |
continue; |
s->type = XVID_TYPE_PVOP; |
|
} |
|
|
|
|
|
n = fscanf(f2, "%c %d %d %d %d %d\n", &type, &ignore, &s->scaled_length, &ignore, &ignore, &ignore); |
|
|
if (n == EOF) break; |
|
|
if (type != 'i'&& type != 'p' && type != 'b' && type != 's') { |
|
|
printf("unk\n"); |
|
|
continue; /* unknown type */ |
|
176 |
} |
} |
177 |
|
|
178 |
i++; |
i++; |
179 |
} |
} |
180 |
rc->num_frames = i; |
rc->num_frames = i; |
181 |
|
|
182 |
|
fclose(f); |
|
fclose(f1); |
|
|
if (filename2) |
|
|
fclose(f2); |
|
183 |
|
|
184 |
return 1; |
return 1; |
185 |
} |
} |
186 |
|
|
187 |
|
|
188 |
/*static void internal_scale(rc_2pass2_t *rc) |
|
189 |
|
#if 0 |
190 |
|
static void print_stats(rc_2pass2_t * rc) |
191 |
{ |
{ |
|
const double avg_pvop = rc->avg_length[XVID_TYPE_PVOP-1]; |
|
|
const double avg_bvop = rc->avg_length[XVID_TYPE_BVOP-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]; |
|
|
uint64_t i_total = 0; |
|
|
double total1,total2; |
|
192 |
int i; |
int i; |
|
|
|
193 |
for (i=0; i<rc->num_frames; i++) { |
for (i=0; i<rc->num_frames; i++) { |
194 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
195 |
|
DPRINTF(XVID_DEBUG_RC, "%i %i %i %i\n", s->type, s->quant, s->length, s->scaled_length); |
|
if (s->type == XVID_TYPE_IVOP) { |
|
|
i_total += s->length + s->length * rc->param.keyframe_boost / 100; |
|
196 |
} |
} |
197 |
} |
} |
198 |
|
#endif |
199 |
|
|
200 |
// compensate for avi frame overhead |
/* pre-process the statistics data |
201 |
rc->target_size -= rc->num_frames * 24; |
- for each type, count, tot_length, min_length, max_length |
202 |
|
- set keyframes_locations |
203 |
|
*/ |
204 |
|
|
205 |
// perform prepass to compensate for over/undersizing |
void pre_process0(rc_2pass2_t * rc) |
206 |
|
{ |
207 |
|
int i,j; |
208 |
|
|
209 |
if (rc->param.use_alt_curve) { |
for (i=0; i<3; i++) { |
210 |
|
rc->count[i]=0; |
211 |
|
rc->tot_length[i] = 0; |
212 |
|
rc->last_quant[i] = 0; |
213 |
|
} |
214 |
|
|
215 |
rc->alt_curve_low = avg_pvop - avg_pvop * (double)rc->param.alt_curve_low_dist / 100.0; |
for (i=j=0; i<rc->num_frames; i++) { |
216 |
rc->alt_curve_low_diff = avg_pvop - rc->alt_curve_low; |
stat_t * s = &rc->stats[i]; |
217 |
rc->alt_curve_high = avg_pvop + avg_pvop * (double)rc->param.alt_curve_high_dist / 100.0; |
|
218 |
rc->alt_curve_high_diff = rc->alt_curve_high - avg_pvop; |
rc->count[s->type-1]++; |
219 |
if (rc->alt_curve_use_auto) { |
rc->tot_length[s->type-1] += s->length; |
220 |
if (rc->movie_curve > 1.0) { |
|
221 |
rc->param.alt_curve_min_rel_qual = (int)(100.0 - (100.0 - 100.0 / rc->movie_curve) * (double)rc->param.alt_curve_auto_str / 100.0); |
if (i == 0 || s->length < rc->min_length[s->type-1]) { |
222 |
if (rc->param.alt_curve_min_rel_qual < 20) |
rc->min_length[s->type-1] = s->length; |
|
rc->param.alt_curve_min_rel_qual = 20; |
|
|
}else{ |
|
|
rc->param.alt_curve_min_rel_qual = 100; |
|
223 |
} |
} |
224 |
|
|
225 |
|
if (i == 0 || s->length > rc->max_length) { |
226 |
|
rc->max_length = s->length; |
227 |
} |
} |
|
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; |
|
228 |
|
|
229 |
if (rc->param.alt_curve_low_dist > 100) { |
if (s->type == XVID_TYPE_IVOP) { |
230 |
switch(rc->param.alt_curve_type) { |
rc->keyframe_locations[j] = i; |
231 |
case XVID_CURVE_SINE : // Sine Curve (high aggressiveness) |
j++; |
|
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))); |
|
232 |
} |
} |
233 |
} |
} |
234 |
|
rc->keyframe_locations[j] = i; |
235 |
} |
} |
236 |
|
|
|
total1 = 0; |
|
|
total2 = 0; |
|
237 |
|
|
238 |
for (i=0; i<rc->num_frames; i++) { |
/* calculate zone weight "center" */ |
|
stat_t * s = &rc->stats[i]; |
|
239 |
|
|
240 |
if (s->type != XVID_TYPE_IVOP) { |
static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create) |
241 |
|
{ |
242 |
|
int i,j; |
243 |
|
int n = 0; |
244 |
|
|
245 |
double dbytes = s->length / rc->movie_curve; |
rc->avg_weight = 0.0; |
246 |
double dbytes2; |
rc->tot_quant = 0; |
|
total1 += dbytes; |
|
247 |
|
|
|
if (s->type == XVID_TYPE_BVOP) |
|
|
dbytes *= avg_pvop / avg_bvop; |
|
248 |
|
|
249 |
if (rc->param.use_alt_curve) { |
if (create->num_zones == 0) { |
250 |
if (dbytes > avg_pvop) { |
for (j = 0; j < rc->num_frames; j++) { |
251 |
if (dbytes >= rc->alt_curve_high) { |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
252 |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev); |
rc->stats[j].weight = 1.0; |
|
}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 - avg_pvop) * 90.0 / rc->alt_curve_high_diff))); |
|
|
break; |
|
|
case XVID_CURVE_LINEAR : |
|
|
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - avg_pvop) / 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 - avg_pvop) * 90.0 / rc->alt_curve_high_diff)))); |
|
253 |
} |
} |
254 |
|
rc->avg_weight += rc->num_frames * 1.0; |
255 |
|
n += rc->num_frames; |
256 |
} |
} |
257 |
}else{ |
|
258 |
if (dbytes <= rc->alt_curve_low){ |
|
259 |
dbytes2 = dbytes; |
for(i=0; i < create->num_zones; i++) { |
260 |
}else{ |
|
261 |
switch(rc->param.alt_curve_type){ |
int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames; |
262 |
case XVID_CURVE_SINE : |
|
263 |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - avg_pvop) * 90.0 / rc->alt_curve_low_diff))); |
if (i==0 && create->zones[i].frame > 0) { |
264 |
break; |
for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) { |
265 |
case XVID_CURVE_LINEAR : |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
266 |
dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - avg_pvop) / rc->alt_curve_low_diff); |
rc->stats[j].weight = 1.0; |
|
break; |
|
|
case XVID_CURVE_COSINE : |
|
|
dbytes2 = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - avg_pvop) * 90.0 / rc->alt_curve_low_diff)))); |
|
267 |
} |
} |
268 |
|
rc->avg_weight += create->zones[i].frame * 1.0; |
269 |
|
n += create->zones[i].frame; |
270 |
} |
} |
271 |
|
|
272 |
|
if (create->zones[i].mode == XVID_ZONE_WEIGHT) { |
273 |
|
for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { |
274 |
|
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
275 |
|
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
276 |
} |
} |
277 |
}else{ |
next -= create->zones[i].frame; |
278 |
if (dbytes > avg_pvop) { |
rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base; |
279 |
dbytes2 = ((double)dbytes + (avg_pvop - dbytes) * |
n += next; |
280 |
rc->param.curve_compression_high / 100.0); |
}else{ // XVID_ZONE_QUANT |
281 |
}else{ |
for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { |
282 |
dbytes2 = ((double)dbytes + (avg_pvop - dbytes) * |
rc->stats[j].zone_mode = XVID_ZONE_QUANT; |
283 |
rc->param.curve_compression_low / 100.0); |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
284 |
|
rc->tot_quant += rc->stats[j].length; |
285 |
} |
} |
286 |
} |
} |
|
|
|
|
if (s->type == XVID_TYPE_BVOP) { |
|
|
dbytes2 *= avg_bvop / avg_pvop; |
|
287 |
} |
} |
288 |
|
rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0; |
289 |
|
|
290 |
if (dbytes2 < rc->min_length[s->type-1]) { |
DPRINTF(XVID_DEBUG_RC, "center_weight: %f (for %i frames); fixed_bytes: %i\n", rc->avg_weight, n, rc->tot_quant); |
|
dbytes = rc->min_length[s->type-1]; |
|
291 |
} |
} |
292 |
|
|
|
total2 += dbytes2; |
|
|
} |
|
|
} |
|
293 |
|
|
294 |
rc->curve_comp_scale = total1 / total2; |
/* scale the curve */ |
295 |
|
|
296 |
if (!rc->param.use_alt_curve) { |
static void internal_scale(rc_2pass2_t *rc) |
297 |
printf("middle frame size for asymmetric curve compression: %i", |
{ |
298 |
(int)(avg_pvop * rc->curve_comp_scale)); |
int64_t target = rc->target - rc->tot_quant; |
299 |
|
int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant; |
300 |
|
int min_size[3]; |
301 |
|
double scaler; |
302 |
|
int i; |
303 |
|
|
304 |
|
|
305 |
|
/* perform an initial scale pass. |
306 |
|
if a frame size is scaled underneath our hardcoded minimums, then we force the |
307 |
|
frame size to the minimum, and deduct the original & scaled frmae length from the |
308 |
|
original and target total lengths */ |
309 |
|
|
310 |
|
min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8; |
311 |
|
min_size[1] = (rc->stats[0].blks[0] + 88) / 8; |
312 |
|
min_size[2] = 8; |
313 |
|
|
314 |
|
scaler = (double)target / (double)pass1_length; |
315 |
|
|
316 |
|
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
317 |
|
DPRINTF(XVID_DEBUG_RC, "undersize warning\n"); |
318 |
|
scaler = 1.0; |
319 |
} |
} |
|
}*/ |
|
320 |
|
|
321 |
|
DPRINTF(XVID_DEBUG_RC, "target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler); |
322 |
|
|
323 |
|
for (i=0; i<rc->num_frames; i++) { |
324 |
|
stat_t * s = &rc->stats[i]; |
325 |
|
int len; |
326 |
|
|
327 |
|
if (s->zone_mode == XVID_ZONE_QUANT) { |
328 |
|
s->scaled_length = s->length; |
329 |
|
}else { |
330 |
|
len = (int)((double)s->length * scaler * s->weight / rc->avg_weight); |
331 |
|
if (len < min_size[s->type-1]) { /* force frame size */ |
332 |
|
s->scaled_length = min_size[s->type-1]; |
333 |
|
target -= s->scaled_length; |
334 |
|
pass1_length -= s->length; |
335 |
|
}else{ |
336 |
|
s->scaled_length = 0; |
337 |
|
} |
338 |
|
} |
339 |
|
} |
340 |
|
|
341 |
|
scaler = (double)target / (double)pass1_length; |
342 |
|
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
343 |
|
DPRINTF(XVID_DEBUG_RC,"undersize warning\n"); |
344 |
|
scaler = 1.0; |
345 |
|
} |
346 |
|
|
347 |
|
DPRINTF(XVID_DEBUG_RC, "target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler); |
348 |
|
|
|
static void print_stats(rc_2pass2_t * rc) |
|
|
{ |
|
|
int i; |
|
349 |
for (i = 0; i < rc->num_frames; i++) { |
for (i = 0; i < rc->num_frames; i++) { |
350 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
|
printf("%i %i %i %i\n", s->type, s->quant, s->length, s->scaled_length); |
|
351 |
|
|
352 |
|
if (s->scaled_length==0) { /* ignore frame with forced frame sizes */ |
353 |
|
s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight); |
354 |
|
} |
355 |
} |
} |
356 |
} |
} |
357 |
|
|
358 |
|
|
|
/* pre-process the statistics data |
|
|
this is a clone of vfw/src/2pass.c:codec_2pass_init minus file reading, alt_curve, internal scale |
|
|
*/ |
|
359 |
|
|
360 |
void pre_process(rc_2pass2_t * rc) |
|
361 |
|
void pre_process1(rc_2pass2_t * rc) |
362 |
{ |
{ |
363 |
int i,j; |
int i; |
364 |
double total1, total2; |
double total1, total2; |
365 |
uint64_t ivop_boost_total; |
uint64_t ivop_boost_total; |
366 |
|
|
368 |
rc->curve_comp_error = 0; |
rc->curve_comp_error = 0; |
369 |
|
|
370 |
for (i=0; i<3; i++) { |
for (i=0; i<3; i++) { |
|
rc->count[i]=0; |
|
|
rc->tot_length[i] = 0; |
|
371 |
rc->tot_scaled_length[i] = 0; |
rc->tot_scaled_length[i] = 0; |
|
rc->last_quant[i] = 0; |
|
372 |
} |
} |
373 |
|
|
374 |
for (i=0; i<32;i++) { |
for (i=0; i<rc->num_frames; i++) { |
|
rc->pquant_error[i] = 0; |
|
|
rc->bquant_error[i] = 0; |
|
|
rc->quant_count[i] = 0; |
|
|
} |
|
|
|
|
|
for (i=j=0; i<rc->num_frames; i++) { |
|
375 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
376 |
|
|
|
rc->count[s->type-1]++; |
|
|
rc->tot_length[s->type-1] += s->length; |
|
377 |
rc->tot_scaled_length[s->type-1] += s->scaled_length; |
rc->tot_scaled_length[s->type-1] += s->scaled_length; |
378 |
|
|
|
if (i == 0 || s->length < rc->min_length[s->type-1]) { |
|
|
rc->min_length[s->type-1] = s->length; |
|
|
} |
|
|
|
|
|
if (i == 0 || s->length > rc->max_length) { |
|
|
rc->max_length = s->length; |
|
|
} |
|
|
|
|
379 |
if (s->type == XVID_TYPE_IVOP) { |
if (s->type == XVID_TYPE_IVOP) { |
380 |
ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100; |
ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100; |
|
rc->keyframe_locations[j] = i; |
|
|
j++; |
|
381 |
} |
} |
382 |
} |
} |
|
rc->keyframe_locations[j] = i; |
|
383 |
|
|
384 |
rc->movie_curve = ((double)(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1] + ivop_boost_total) / |
rc->movie_curve = ((double)(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1] + ivop_boost_total) / |
385 |
(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1])); |
(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1])); |
392 |
} |
} |
393 |
} |
} |
394 |
|
|
|
printf("--\n"); |
|
395 |
/* alt curve stuff here */ |
/* alt curve stuff here */ |
396 |
|
|
397 |
if (rc->param.use_alt_curve) { |
if (rc->param.use_alt_curve) { |
440 |
|
|
441 |
|
|
442 |
total1=total2=0; |
total1=total2=0; |
443 |
for (i=j=0; i<rc->num_frames; i++) { |
for (i=0; i<rc->num_frames; i++) { |
444 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
445 |
|
|
446 |
if (s->type != XVID_TYPE_IVOP) { |
if (s->type != XVID_TYPE_IVOP) { |
511 |
rc->curve_comp_scale = total1 / total2; |
rc->curve_comp_scale = total1 / total2; |
512 |
|
|
513 |
if (!rc->param.use_alt_curve) { |
if (!rc->param.use_alt_curve) { |
514 |
printf("middle frame size for asymmetric curve compression: %i\n", |
DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: %i\n", |
515 |
(int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale)); |
(int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale)); |
516 |
} |
} |
517 |
|
|
528 |
|
|
529 |
/* special info for alt curve: bias bonus and quantizer thresholds */ |
/* special info for alt curve: bias bonus and quantizer thresholds */ |
530 |
|
|
531 |
printf("avg scaled framesize:%i", (int)rc->avg_length[XVID_TYPE_PVOP-1]); |
DPRINTF(XVID_DEBUG_RC, "avg scaled framesize:%i", (int)rc->avg_length[XVID_TYPE_PVOP-1]); |
532 |
printf("bias bonus:%i bytes", (int)rc->alt_curve_curve_bias_bonus); |
DPRINTF(XVID_DEBUG_RC, "bias bonus:%i bytes", (int)rc->alt_curve_curve_bias_bonus); |
533 |
|
|
534 |
for (i=1; i <= (int)(rc->alt_curve_high*2)+1; i++) { |
for (i=1; i <= (int)(rc->alt_curve_high*2)+1; i++) { |
535 |
double curve_temp, dbytes; |
double curve_temp, dbytes; |
578 |
if (newquant != oldquant) { |
if (newquant != oldquant) { |
579 |
int percent = (int)((i - rc->avg_length[XVID_TYPE_PVOP-1]) * 100.0 / rc->avg_length[XVID_TYPE_PVOP-1]); |
int percent = (int)((i - rc->avg_length[XVID_TYPE_PVOP-1]) * 100.0 / rc->avg_length[XVID_TYPE_PVOP-1]); |
580 |
oldquant = newquant; |
oldquant = newquant; |
581 |
printf("quant:%i threshold at %i : %i percent", newquant, i, percent); |
DPRINTF(XVID_DEBUG_RC, "quant:%i threshold at %i : %i percent", newquant, i, percent); |
582 |
} |
} |
583 |
} |
} |
584 |
} |
} |
598 |
{ |
{ |
599 |
xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param; |
xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param; |
600 |
rc_2pass2_t * rc; |
rc_2pass2_t * rc; |
601 |
|
int i; |
602 |
|
|
603 |
rc = malloc(sizeof(rc_2pass2_t)); |
rc = malloc(sizeof(rc_2pass2_t)); |
604 |
if (rc == NULL) |
if (rc == NULL) |
613 |
if (rc->param.curve_compression_low <= 0) rc->param.curve_compression_low = 0; |
if (rc->param.curve_compression_low <= 0) rc->param.curve_compression_low = 0; |
614 |
if (rc->param.max_overflow_improvement <= 0) rc->param.max_overflow_improvement = 60; |
if (rc->param.max_overflow_improvement <= 0) rc->param.max_overflow_improvement = 60; |
615 |
if (rc->param.max_overflow_degradation <= 0) rc->param.max_overflow_degradation = 60; |
if (rc->param.max_overflow_degradation <= 0) rc->param.max_overflow_degradation = 60; |
|
if (rc->param.min_quant[0] <= 0) rc->param.min_quant[0] = 2; |
|
|
if (rc->param.max_quant[0] <= 0) rc->param.max_quant[0] = 31; |
|
|
if (rc->param.min_quant[1] <= 0) rc->param.min_quant[1] = 2; |
|
|
if (rc->param.max_quant[1] <= 0) rc->param.max_quant[1] = 31; |
|
|
if (rc->param.min_quant[2] <= 0) rc->param.min_quant[2] = 2; |
|
|
if (rc->param.max_quant[2] <= 0) rc->param.max_quant[2] = 31; |
|
616 |
|
|
617 |
if (rc->param.use_alt_curve <= 0) rc->param.use_alt_curve = 0; |
if (rc->param.use_alt_curve <= 0) rc->param.use_alt_curve = 0; |
618 |
if (rc->param.alt_curve_high_dist <= 0) rc->param.alt_curve_high_dist = 500; |
if (rc->param.alt_curve_high_dist <= 0) rc->param.alt_curve_high_dist = 500; |
628 |
if (rc->param.kfreduction <= 0) rc->param.kfreduction = 20; |
if (rc->param.kfreduction <= 0) rc->param.kfreduction = 20; |
629 |
if (rc->param.min_key_interval <= 0) rc->param.min_key_interval = 300; |
if (rc->param.min_key_interval <= 0) rc->param.min_key_interval = 300; |
630 |
|
|
631 |
if (!det_stats_length(rc, param->filename1)){ |
if (!det_stats_length(rc, param->filename)){ |
632 |
DPRINTF(DPRINTF_RC,"fopen %s failed\n", param->filename1); |
DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename); |
633 |
free(rc); |
free(rc); |
634 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
635 |
} |
} |
646 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
647 |
} |
} |
648 |
|
|
649 |
if (!load_stats(rc, param->filename1, param->filename2)) { |
if (!load_stats(rc, param->filename)) { |
650 |
DPRINTF(DPRINTF_RC,"fopen %s,%s failed\n", param->filename1, param->filename2); |
DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename); |
651 |
free(rc->keyframe_locations); |
free(rc->keyframe_locations); |
652 |
free(rc->stats); |
free(rc->stats); |
653 |
free(rc); |
free(rc); |
655 |
} |
} |
656 |
|
|
657 |
/* pre-process our stats */ |
/* pre-process our stats */ |
658 |
pre_process(rc); |
|
659 |
|
if (rc->num_frames < create->fbase/create->fincr) { |
660 |
|
rc->target = rc->param.bitrate / 8; /* one second */ |
661 |
|
}else{ |
662 |
|
rc->target = (rc->param.bitrate * rc->num_frames * create->fincr) / (create->fbase * 8); |
663 |
|
} |
664 |
|
|
665 |
|
DPRINTF(XVID_DEBUG_RC, "rc->target : %i\n", rc->target); |
666 |
|
|
667 |
|
rc->target -= rc->num_frames*24; /* avi file header */ |
668 |
|
|
669 |
|
|
670 |
|
pre_process0(rc); |
671 |
|
|
672 |
|
if (rc->param.bitrate) { |
673 |
|
zone_process(rc, create); |
674 |
|
internal_scale(rc); |
675 |
|
}else{ |
676 |
|
/* external scaler: ignore zone */ |
677 |
|
for (i=0;i<rc->num_frames;i++) { |
678 |
|
rc->stats[i].zone_mode = XVID_ZONE_WEIGHT; |
679 |
|
rc->stats[i].weight = 1.0; |
680 |
|
} |
681 |
|
rc->avg_weight = 1.0; |
682 |
|
rc->tot_quant = 0; |
683 |
|
} |
684 |
|
pre_process1(rc); |
685 |
|
|
686 |
|
for (i=0; i<32;i++) { |
687 |
|
rc->pquant_error[i] = 0; |
688 |
|
rc->bquant_error[i] = 0; |
689 |
|
rc->quant_count[i] = 0; |
690 |
|
} |
691 |
|
|
692 |
|
rc->fq_error = 0; |
693 |
|
|
694 |
*handle = rc; |
*handle = rc; |
695 |
return(0); |
return(0); |
715 |
double curve_temp; |
double curve_temp; |
716 |
int capped_to_max_framesize = 0; |
int capped_to_max_framesize = 0; |
717 |
|
|
718 |
|
if (data->quant <= 0) { |
719 |
|
|
720 |
|
if (s->zone_mode == XVID_ZONE_QUANT) { |
721 |
|
|
722 |
|
rc->fq_error += s->weight; |
723 |
|
data->quant = (int)rc->fq_error; |
724 |
|
rc->fq_error -= data->quant; |
725 |
|
|
726 |
|
s->desired_length = s->length; |
727 |
|
|
728 |
|
}else { /* XVID_ZONE_WEIGHT */ |
729 |
|
|
730 |
if (data->frame_num >= rc->num_frames) { |
if (data->frame_num >= rc->num_frames) { |
731 |
/* insufficent stats data */ |
/* insufficent stats data */ |
732 |
return 0; |
return 0; |
753 |
if (rc->param.payback_method == XVID_PAYBACK_BIAS) { |
if (rc->param.payback_method == XVID_PAYBACK_BIAS) { |
754 |
desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay); |
desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay); |
755 |
}else{ |
}else{ |
756 |
|
//printf("desired=%i, dbytes=%i\n", desired,dbytes); |
757 |
desired = (int)(rc->curve_comp_error * dbytes / |
desired = (int)(rc->curve_comp_error * dbytes / |
758 |
rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay); |
rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay); |
759 |
|
//printf("desired=%i\n", desired); |
760 |
|
|
761 |
if (labs(desired) > fabs(rc->curve_comp_error)) { |
if (labs(desired) > fabs(rc->curve_comp_error)) { |
762 |
desired = (int)rc->curve_comp_error; |
desired = (int)rc->curve_comp_error; |
841 |
rc->curve_comp_error += dbytes - (int)dbytes; |
rc->curve_comp_error += dbytes - (int)dbytes; |
842 |
} |
} |
843 |
|
|
844 |
if (desired > s->length){ |
|
845 |
|
if (desired > s->length) { /* if desired length exceeds the pass1 length.. */ |
846 |
rc->curve_comp_error += desired - s->length; |
rc->curve_comp_error += desired - s->length; |
847 |
desired = s->length; |
desired = s->length; |
848 |
}else{ |
}else{ |
942 |
|
|
943 |
/* cap to min/max quant */ |
/* cap to min/max quant */ |
944 |
|
|
945 |
if (data->quant < rc->param.min_quant[s->type-1]) { |
if (data->quant < data->min_quant[s->type-1]) { |
946 |
data->quant = rc->param.min_quant[s->type-1]; |
data->quant = data->min_quant[s->type-1]; |
947 |
}else if (data->quant > rc->param.max_quant[s->type-1]) { |
}else if (data->quant > data->max_quant[s->type-1]) { |
948 |
data->quant = rc->param.max_quant[s->type-1]; |
data->quant = data->max_quant[s->type-1]; |
949 |
} |
} |
950 |
|
|
951 |
/* subsequent p/b frame quants can only be +- 2 */ |
/* subsequent p/b frame quants can only be +- 2 */ |
953 |
|
|
954 |
if (data->quant > rc->last_quant[s->type-1] + 2) { |
if (data->quant > rc->last_quant[s->type-1] + 2) { |
955 |
data->quant = rc->last_quant[s->type-1] + 2; |
data->quant = rc->last_quant[s->type-1] + 2; |
956 |
DPRINTF(DPRINTF_RC, "p/b-frame quantizer prevented from rising too steeply"); |
DPRINTF(XVID_DEBUG_RC, "p/b-frame quantizer prevented from rising too steeply"); |
957 |
} |
} |
958 |
if (data->quant < rc->last_quant[s->type-1] - 2) { |
if (data->quant < rc->last_quant[s->type-1] - 2) { |
959 |
data->quant = rc->last_quant[s->type-1] - 2; |
data->quant = rc->last_quant[s->type-1] - 2; |
960 |
DPRINTF(DPRINTF_RC, "p/b-frame quantizer prevented from falling too steeply"); |
DPRINTF(XVID_DEBUG_RC, "p/b-frame quantizer prevented from falling too steeply"); |
961 |
} |
} |
962 |
} |
} |
963 |
|
|
965 |
rc->last_quant[s->type-1] = data->quant; |
rc->last_quant[s->type-1] = data->quant; |
966 |
} |
} |
967 |
|
|
968 |
|
|
969 |
|
} /* if */ |
970 |
|
|
971 |
|
} |
972 |
|
|
973 |
return 0; |
return 0; |
974 |
} |
} |
975 |
|
|
1006 |
rc->KFoverflow -= rc->KFoverflow_partial; |
rc->KFoverflow -= rc->KFoverflow_partial; |
1007 |
} |
} |
1008 |
|
|
1009 |
printf("[%i] quant:%i stats1:%i scaled:%i actual:%i overflow:%i\n", |
DPRINTF(XVID_DEBUG_RC, "[%i] quant:%i stats1:%i scaled:%i actual:%i overflow:%i\n", |
1010 |
data->frame_num, |
data->frame_num, |
1011 |
data->quant, |
data->quant, |
1012 |
s->length, |
s->length, |