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.2 2003-04-08 14:01:09 suxen_drol 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; |
55 |
/* constant statistical data */ |
/* constant statistical data */ |
56 |
int num_frames; |
int num_frames; |
57 |
int num_keyframes; |
int num_keyframes; |
58 |
|
uint64_t target; /* target bitrate */ |
59 |
|
|
60 |
int count[3]; /* count of each frame types */ |
int count[3]; /* count of each frame types */ |
61 |
uint64_t tot_length[3]; /* total length of each frame types */ |
uint64_t tot_length[3]; /* total length of each frame types */ |
112 |
if ((f = fopen(filename, "rt")) == NULL) |
if ((f = fopen(filename, "rt")) == NULL) |
113 |
return 0; |
return 0; |
114 |
|
|
115 |
while((n = fscanf(f, "%c %d %d %d %d %d\n", |
while((n = fscanf(f, "%c %d %d %d %d %d %d\n", |
116 |
&type, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) { |
&type, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) { |
117 |
if (type == 'i') { |
if (type == 'i') { |
118 |
rc->num_frames++; |
rc->num_frames++; |
119 |
rc->num_keyframes++; |
rc->num_keyframes++; |
128 |
} |
} |
129 |
|
|
130 |
|
|
131 |
/* open stats file(s) and read into rc->stats array */ |
/* scale the curve */ |
132 |
|
|
133 |
static int load_stats(rc_2pass2_t *rc, char * filename1, char * filename2) |
static void internal_scale(rc_2pass2_t *rc) |
134 |
{ |
{ |
135 |
FILE * f1, *f2; |
int64_t target = rc->target; |
136 |
|
int64_t tot_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2]; |
137 |
|
int min_size[3]; |
138 |
|
double scaler; |
139 |
int i; |
int i; |
140 |
|
|
141 |
|
if (target <= 0 || target >= tot_length) { |
142 |
|
printf("undersize warning\n"); |
143 |
|
} |
144 |
|
|
145 |
if ((f1 = fopen(filename1, "rt"))==NULL) |
/* perform an initial scale pass. |
146 |
return 0; |
if a frame size is scaled underneath our hardcoded minimums, then we force the |
147 |
|
frame size to the minimum, and deduct the original & scaled frmae length from the |
148 |
|
original and target total lengths */ |
149 |
|
|
150 |
if ((f2 = fopen(filename2, "rt"))==NULL) { |
min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8; |
151 |
fclose(f1); |
min_size[1] = (rc->stats[0].blks[0] + 88) / 8; |
152 |
return 0; |
min_size[2] = 8; |
|
} |
|
153 |
|
|
154 |
i = 0; |
|
155 |
while(i < rc->num_frames) { |
scaler = (double)target / (double)tot_length; |
156 |
|
//printf("target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)tot_length, scaler); |
157 |
|
|
158 |
|
for (i=0; i<rc->num_frames; i++) { |
159 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
160 |
int n, ignore; |
int len; |
|
char type; |
|
161 |
|
|
162 |
n = fscanf(f1, "%c %d %d %d %d %d\n", &type, &s->quant, &s->length, &ignore, &ignore, &ignore); |
len = (int)((double)s->length * scaler); |
163 |
if (n == EOF) break; |
if (len < min_size[s->type]) { /* force frame size */ |
164 |
|
s->scaled_length = min_size[s->type]; |
165 |
|
target -= s->scaled_length; |
166 |
|
tot_length -= s->length; |
167 |
|
}else{ |
168 |
|
s->scaled_length = 0; |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
if (type == 'i') { |
if (target <= 0 || target >= tot_length) { |
173 |
s->type = XVID_TYPE_IVOP; |
printf("undersize warning\n"); |
174 |
}else if (type == 'p' || type == 's') { |
return; |
|
s->type = XVID_TYPE_PVOP; |
|
|
}else if (type == 'b') { |
|
|
s->type = XVID_TYPE_BVOP; |
|
|
}else{ /* unknown type */ |
|
|
printf("unk\n"); |
|
|
continue; |
|
175 |
} |
} |
176 |
|
|
177 |
n = fscanf(f2, "%c %d %d %d %d %d\n", &type, &ignore, &s->scaled_length, &ignore, &ignore, &ignore); |
scaler = (double)target / (double)tot_length; |
178 |
if (n == EOF) break; |
//printf("target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)tot_length, scaler); |
179 |
if (type != 'i'&& type != 'p' && type != 'b' && type != 's') { |
|
180 |
printf("unk\n"); |
for (i=0; i<rc->num_frames; i++) { |
181 |
continue; /* unknown type */ |
stat_t * s = &rc->stats[i]; |
182 |
|
|
183 |
|
if (s->scaled_length==0) { /* ignore frame with forced frame sizes */ |
184 |
|
s->scaled_length = (int)((double)s->length * scaler); |
185 |
|
} |
186 |
} |
} |
187 |
|
|
|
i++; |
|
188 |
} |
} |
|
rc->num_frames = i; |
|
189 |
|
|
190 |
|
|
|
fclose(f1); |
|
|
if (filename2) |
|
|
fclose(f2); |
|
191 |
|
|
|
return 1; |
|
|
} |
|
192 |
|
|
193 |
|
|
194 |
/*static void internal_scale(rc_2pass2_t *rc) |
/*static void internal_scale(rc_2pass2_t *rc) |
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
/* open stats file(s) and read into rc->stats array */ |
332 |
|
|
333 |
|
static int load_stats(rc_2pass2_t *rc, char * filename) |
334 |
|
{ |
335 |
|
FILE * f; |
336 |
|
int i, not_scaled; |
337 |
|
|
338 |
|
|
339 |
|
if ((f = fopen(filename, "rt"))==NULL) |
340 |
|
return 0; |
341 |
|
|
342 |
|
i = 0; |
343 |
|
not_scaled = 0; |
344 |
|
while(i < rc->num_frames) { |
345 |
|
stat_t * s = &rc->stats[i]; |
346 |
|
int n; |
347 |
|
char type; |
348 |
|
|
349 |
|
s->scaled_length = 0; |
350 |
|
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); |
351 |
|
if (n == EOF) break; |
352 |
|
if (n < 7) { |
353 |
|
not_scaled = 1; |
354 |
|
} |
355 |
|
|
356 |
|
if (type == 'i') { |
357 |
|
s->type = XVID_TYPE_IVOP; |
358 |
|
}else if (type == 'p' || type == 's') { |
359 |
|
s->type = XVID_TYPE_PVOP; |
360 |
|
}else if (type == 'b') { |
361 |
|
s->type = XVID_TYPE_BVOP; |
362 |
|
}else{ /* unknown type */ |
363 |
|
printf("unk\n"); |
364 |
|
continue; |
365 |
|
} |
366 |
|
|
367 |
|
i++; |
368 |
|
} |
369 |
|
rc->num_frames = i; |
370 |
|
|
371 |
|
fclose(f); |
372 |
|
|
373 |
|
return 1; |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
|
380 |
static void print_stats(rc_2pass2_t * rc) |
static void print_stats(rc_2pass2_t * rc) |
381 |
{ |
{ |
392 |
this is a clone of vfw/src/2pass.c:codec_2pass_init minus file reading, alt_curve, internal scale |
this is a clone of vfw/src/2pass.c:codec_2pass_init minus file reading, alt_curve, internal scale |
393 |
*/ |
*/ |
394 |
|
|
395 |
void pre_process(rc_2pass2_t * rc) |
void pre_process0(rc_2pass2_t * rc) |
396 |
{ |
{ |
397 |
int i,j; |
int i,j; |
|
double total1, total2; |
|
|
uint64_t ivop_boost_total; |
|
|
|
|
|
ivop_boost_total = 0; |
|
|
rc->curve_comp_error = 0; |
|
398 |
|
|
399 |
for (i=0; i<3; i++) { |
for (i=0; i<3; i++) { |
400 |
rc->count[i]=0; |
rc->count[i]=0; |
401 |
rc->tot_length[i] = 0; |
rc->tot_length[i] = 0; |
|
rc->tot_scaled_length[i] = 0; |
|
402 |
rc->last_quant[i] = 0; |
rc->last_quant[i] = 0; |
403 |
} |
} |
404 |
|
|
413 |
|
|
414 |
rc->count[s->type-1]++; |
rc->count[s->type-1]++; |
415 |
rc->tot_length[s->type-1] += s->length; |
rc->tot_length[s->type-1] += s->length; |
|
rc->tot_scaled_length[s->type-1] += s->scaled_length; |
|
416 |
|
|
417 |
if (i == 0 || s->length < rc->min_length[s->type-1]) { |
if (i == 0 || s->length < rc->min_length[s->type-1]) { |
418 |
rc->min_length[s->type-1] = s->length; |
rc->min_length[s->type-1] = s->length; |
423 |
} |
} |
424 |
|
|
425 |
if (s->type == XVID_TYPE_IVOP) { |
if (s->type == XVID_TYPE_IVOP) { |
|
ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100; |
|
426 |
rc->keyframe_locations[j] = i; |
rc->keyframe_locations[j] = i; |
427 |
j++; |
j++; |
428 |
} |
} |
429 |
} |
} |
430 |
rc->keyframe_locations[j] = i; |
rc->keyframe_locations[j] = i; |
431 |
|
} |
432 |
|
|
433 |
|
|
434 |
|
|
435 |
|
void pre_process1(rc_2pass2_t * rc) |
436 |
|
{ |
437 |
|
int i; |
438 |
|
double total1, total2; |
439 |
|
uint64_t ivop_boost_total; |
440 |
|
|
441 |
|
ivop_boost_total = 0; |
442 |
|
rc->curve_comp_error = 0; |
443 |
|
|
444 |
|
for (i=0; i<3; i++) { |
445 |
|
rc->tot_scaled_length[i] = 0; |
446 |
|
} |
447 |
|
|
448 |
|
for (i=0; i<rc->num_frames; i++) { |
449 |
|
stat_t * s = &rc->stats[i]; |
450 |
|
|
451 |
|
rc->tot_scaled_length[s->type-1] += s->scaled_length; |
452 |
|
|
453 |
|
if (s->type == XVID_TYPE_IVOP) { |
454 |
|
ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100; |
455 |
|
} |
456 |
|
} |
457 |
|
|
458 |
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) / |
459 |
(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])); |
466 |
} |
} |
467 |
} |
} |
468 |
|
|
|
printf("--\n"); |
|
469 |
/* alt curve stuff here */ |
/* alt curve stuff here */ |
470 |
|
|
471 |
if (rc->param.use_alt_curve) { |
if (rc->param.use_alt_curve) { |
514 |
|
|
515 |
|
|
516 |
total1=total2=0; |
total1=total2=0; |
517 |
for (i=j=0; i<rc->num_frames; i++) { |
for (i=0; i<rc->num_frames; i++) { |
518 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
519 |
|
|
520 |
if (s->type != XVID_TYPE_IVOP) { |
if (s->type != XVID_TYPE_IVOP) { |
707 |
if (rc->param.kfreduction <= 0) rc->param.kfreduction = 20; |
if (rc->param.kfreduction <= 0) rc->param.kfreduction = 20; |
708 |
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; |
709 |
|
|
710 |
if (!det_stats_length(rc, param->filename1)){ |
if (!det_stats_length(rc, param->filename)){ |
711 |
DPRINTF(DPRINTF_RC,"fopen %s failed\n", param->filename1); |
DPRINTF(DPRINTF_RC,"fopen %s failed\n", param->filename); |
712 |
free(rc); |
free(rc); |
713 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
714 |
} |
} |
725 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
726 |
} |
} |
727 |
|
|
728 |
if (!load_stats(rc, param->filename1, param->filename2)) { |
if (!load_stats(rc, param->filename)) { |
729 |
DPRINTF(DPRINTF_RC,"fopen %s,%s failed\n", param->filename1, param->filename2); |
DPRINTF(DPRINTF_RC,"fopen %s failed\n", param->filename); |
730 |
free(rc->keyframe_locations); |
free(rc->keyframe_locations); |
731 |
free(rc->stats); |
free(rc->stats); |
732 |
free(rc); |
free(rc); |
734 |
} |
} |
735 |
|
|
736 |
/* pre-process our stats */ |
/* pre-process our stats */ |
737 |
pre_process(rc); |
|
738 |
|
{ |
739 |
|
if (rc->num_frames < create->fbase/create->fincr) { |
740 |
|
rc->target = rc->param.bitrate / 8; /* one second */ |
741 |
|
}else{ |
742 |
|
rc->target = (rc->param.bitrate * rc->num_frames * create->fincr) / (create->fbase * 8); |
743 |
|
} |
744 |
|
|
745 |
|
|
746 |
|
rc->target -= rc->num_frames*24; /* avi file header */ |
747 |
|
|
748 |
|
} |
749 |
|
|
750 |
|
|
751 |
|
pre_process0(rc); |
752 |
|
if (rc->param.bitrate) { |
753 |
|
internal_scale(rc); |
754 |
|
} |
755 |
|
pre_process1(rc);pre_process1(rc);pre_process1(rc); |
756 |
|
|
757 |
*handle = rc; |
*handle = rc; |
758 |
return(0); |
return(0); |
804 |
if (rc->param.payback_method == XVID_PAYBACK_BIAS) { |
if (rc->param.payback_method == XVID_PAYBACK_BIAS) { |
805 |
desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay); |
desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay); |
806 |
}else{ |
}else{ |
807 |
|
//printf("desired=%i, dbytes=%i\n", desired,dbytes); |
808 |
desired = (int)(rc->curve_comp_error * dbytes / |
desired = (int)(rc->curve_comp_error * dbytes / |
809 |
rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay); |
rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay); |
810 |
|
//printf("desired=%i\n", desired); |
811 |
|
|
812 |
if (labs(desired) > fabs(rc->curve_comp_error)) { |
if (labs(desired) > fabs(rc->curve_comp_error)) { |
813 |
desired = (int)rc->curve_comp_error; |
desired = (int)rc->curve_comp_error; |