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.24 2003-11-09 20:49:21 edgomez Exp $ |
* $Id: plugin_2pass2.c,v 1.1.2.31 2003-12-17 15:16:16 edgomez Exp $ |
29 |
* |
* |
30 |
*****************************************************************************/ |
*****************************************************************************/ |
31 |
|
|
32 |
|
#define BQUANT_PRESCALE |
33 |
#undef COMPENSATE_FORMULA |
#undef COMPENSATE_FORMULA |
34 |
|
|
35 |
#include <stdio.h> |
#include <stdio.h> |
40 |
#include "../image/image.h" |
#include "../image/image.h" |
41 |
|
|
42 |
/***************************************************************************** |
/***************************************************************************** |
43 |
* Some constants |
* Some default settings |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
46 |
#define DEFAULT_KEYFRAME_BOOST 0 |
#define DEFAULT_KEYFRAME_BOOST 0 |
47 |
#define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10 |
#define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10 |
48 |
#define DEFAULT_CURVE_COMPRESSION_HIGH 0 |
#define DEFAULT_CURVE_COMPRESSION_HIGH 0 |
49 |
#define DEFAULT_CURVE_COMPRESSION_LOW 0 |
#define DEFAULT_CURVE_COMPRESSION_LOW 0 |
50 |
#define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60 |
#define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 10 |
51 |
#define DEFAULT_MAX_OVERFLOW_DEGRADATION 60 |
#define DEFAULT_MAX_OVERFLOW_DEGRADATION 10 |
52 |
|
|
53 |
/* Keyframe settings */ |
/* Keyframe settings */ |
54 |
#define DEFAULT_KFREDUCTION 20 |
#define DEFAULT_KFREDUCTION 20 |
55 |
#define DEFAULT_MIN_KEY_INTERVAL 1 |
#define DEFAULT_KFTHRESHOLD 1 |
56 |
|
|
57 |
|
/***************************************************************************** |
58 |
|
* Some default constants (can be tuned) |
59 |
|
****************************************************************************/ |
60 |
|
|
61 |
|
/* Specify the invariant part of the headers bits (header+MV) |
62 |
|
* as hlength/cst */ |
63 |
|
#define INVARIANT_HEADER_PART_IVOP 1 /* factor 1.0f */ |
64 |
|
#define INVARIANT_HEADER_PART_PVOP 2 /* factor 0.5f */ |
65 |
|
#define INVARIANT_HEADER_PART_BVOP 8 /* factor 0.125f */ |
66 |
|
|
67 |
/***************************************************************************** |
/***************************************************************************** |
68 |
* Structures |
* Structures |
72 |
typedef struct { |
typedef struct { |
73 |
int type; /* first pass type */ |
int type; /* first pass type */ |
74 |
int quant; /* first pass quant */ |
int quant; /* first pass quant */ |
|
int quant2; /* Second pass quant */ |
|
75 |
int blks[3]; /* k,m,y blks */ |
int blks[3]; /* k,m,y blks */ |
76 |
int length; /* first pass length */ |
int length; /* first pass length */ |
77 |
|
int invariant; /* what we assume as being invariant between the two passes, it's a sub part of header + MV bits */ |
78 |
int scaled_length; /* scaled length */ |
int scaled_length; /* scaled length */ |
79 |
int desired_length; /* desired length; calculated during encoding */ |
int desired_length; /* desired length; calculated during encoding */ |
80 |
int error; |
int error; |
106 |
|
|
107 |
/* Total length of each frame types (1st pass) */ |
/* Total length of each frame types (1st pass) */ |
108 |
uint64_t tot_length[3]; |
uint64_t tot_length[3]; |
109 |
|
uint64_t tot_invariant[3]; |
110 |
|
|
111 |
/* Average length of each frame types (used first for 1st pass data and |
/* Average length of each frame types (used first for 1st pass data and |
112 |
* then for scaled averages */ |
* then for scaled averages */ |
136 |
double avg_weight; |
double avg_weight; |
137 |
|
|
138 |
/* Total length used by XVID_ZONE_QUANT zones */ |
/* Total length used by XVID_ZONE_QUANT zones */ |
139 |
int64_t tot_quant; |
uint64_t tot_quant; |
140 |
|
uint64_t tot_quant_invariant; |
141 |
|
|
142 |
/*---------------------------------- |
/*---------------------------------- |
143 |
* Advanced settings helper ratios |
* Advanced settings helper ratios |
233 |
{ |
{ |
234 |
switch(opt) { |
switch(opt) { |
235 |
case XVID_PLG_INFO : |
case XVID_PLG_INFO : |
236 |
|
case XVID_PLG_FRAME : |
237 |
return 0; |
return 0; |
238 |
|
|
239 |
case XVID_PLG_CREATE : |
case XVID_PLG_CREATE : |
295 |
|
|
296 |
/* Keyframe settings */ |
/* Keyframe settings */ |
297 |
_INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION); |
_INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION); |
298 |
_INIT(rc->param.min_key_interval, DEFAULT_MIN_KEY_INTERVAL); |
_INIT(rc->param.kfthreshold, DEFAULT_KFTHRESHOLD); |
299 |
#undef _INIT |
#undef _INIT |
300 |
|
|
301 |
/* Initialize some stuff to zero */ |
/* Initialize some stuff to zero */ |
346 |
/* Compute the target filesize */ |
/* Compute the target filesize */ |
347 |
if (rc->param.bitrate<0) { |
if (rc->param.bitrate<0) { |
348 |
/* if negative, bitrate equals the target (in kbytes) */ |
/* if negative, bitrate equals the target (in kbytes) */ |
349 |
rc->target = (-rc->param.bitrate) * 1024; |
rc->target = ((uint64_t)(-rc->param.bitrate)) * 1024; |
350 |
} else if (rc->num_frames < create->fbase/create->fincr) { |
} else if (rc->num_frames < create->fbase/create->fincr) { |
351 |
/* Source sequence is less than 1s long, we do as if it was 1s long */ |
/* Source sequence is less than 1s long, we do as if it was 1s long */ |
352 |
rc->target = rc->param.bitrate / 8; |
rc->target = rc->param.bitrate / 8; |
380 |
* - count how many times each frame type has been used. |
* - count how many times each frame type has been used. |
381 |
* rc->count[] |
* rc->count[] |
382 |
* - total bytes used per frame type |
* - total bytes used per frame type |
383 |
* rc->total[] |
* rc->tot_length[] |
384 |
|
* - total bytes considered invariant between the 2 passes |
385 |
* - store keyframe location |
* - store keyframe location |
386 |
* rc->keyframe_locations[] |
* rc->keyframe_locations[] |
387 |
*/ |
*/ |
390 |
/* When bitrate is not given it means it has been scaled by an external |
/* When bitrate is not given it means it has been scaled by an external |
391 |
* application */ |
* application */ |
392 |
if (rc->param.bitrate) { |
if (rc->param.bitrate) { |
393 |
/* Apply zone settings */ |
/* Apply zone settings |
394 |
|
* - set rc->tot_quant which represents the total num of bytes spent in |
395 |
|
* fixed quant zones |
396 |
|
* - set rc->tot_quant_invariant which represents the total num of bytes spent |
397 |
|
* in fixed quant zones for headers */ |
398 |
zone_process(rc, create); |
zone_process(rc, create); |
399 |
/* Perform internal curve scaling */ |
/* Perform internal curve scaling */ |
400 |
first_pass_scale_curve_internal(rc); |
first_pass_scale_curve_internal(rc); |
454 |
if (data->quant > 0) |
if (data->quant > 0) |
455 |
return(0); |
return(0); |
456 |
|
|
457 |
/* Second case: We are in a Quant zone */ |
/* Second case: insufficent stats data */ |
458 |
|
if (data->frame_num >= rc->num_frames) { |
459 |
|
DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)", |
460 |
|
data->frame_num); |
461 |
|
return(0); |
462 |
|
} |
463 |
|
|
464 |
|
/* Third case: We are in a Quant zone */ |
465 |
if (s->zone_mode == XVID_ZONE_QUANT) { |
if (s->zone_mode == XVID_ZONE_QUANT) { |
466 |
rc->fq_error += s->weight; |
rc->fq_error += s->weight; |
467 |
data->quant = (int)rc->fq_error; |
data->quant = (int)rc->fq_error; |
472 |
return(0); |
return(0); |
473 |
} |
} |
474 |
|
|
|
/* Third case: insufficent stats data */ |
|
|
if (data->frame_num >= rc->num_frames) |
|
|
return(0); |
|
475 |
|
|
476 |
/*************************************************************************/ |
/*************************************************************************/ |
477 |
/*************************************************************************/ |
/*************************************************************************/ |
489 |
|
|
490 |
/* IFrame user settings*/ |
/* IFrame user settings*/ |
491 |
if (s->type == XVID_TYPE_IVOP) { |
if (s->type == XVID_TYPE_IVOP) { |
|
|
|
492 |
/* Keyframe boosting -- All keyframes benefit from it */ |
/* Keyframe boosting -- All keyframes benefit from it */ |
493 |
dbytes += dbytes*rc->param.keyframe_boost / 100; |
dbytes += dbytes*rc->param.keyframe_boost / 100; |
494 |
|
|
495 |
/* Applies keyframe penalties, but not the first frame */ |
#if 0 /* ToDo: decide how to apply kfthresholding */ |
496 |
if (rc->KF_idx) { |
#endif |
|
int penalty_distance; |
|
|
|
|
|
/* Minimum keyframe distance penalties */ |
|
|
penalty_distance = rc->param.min_key_interval; |
|
|
penalty_distance -= rc->keyframe_locations[rc->KF_idx]; |
|
|
penalty_distance += rc->keyframe_locations[rc->KF_idx-1]; |
|
|
|
|
|
/* Ah ah ! guilty keyframe, you're under arrest ! */ |
|
|
if (penalty_distance > 0) |
|
|
dbytes -= dbytes*penalty_distance*rc->param.kfreduction/100; |
|
|
} |
|
497 |
} else { |
} else { |
498 |
|
|
499 |
/* P/S/B frames must reserve some bits for iframe boosting */ |
/* P/S/B frames must reserve some bits for iframe boosting */ |
616 |
* Desired frame length <-> quantizer mapping |
* Desired frame length <-> quantizer mapping |
617 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
618 |
|
|
619 |
/* For bframes we must retrieve the original quant used (sent to xvidcore) |
#ifdef BQUANT_PRESCALE |
620 |
* as core applies the bquant formula before writing the stat log entry */ |
/* For bframes we prescale the quantizer to avoid too high quant scaling */ |
621 |
if(s->type == XVID_TYPE_BVOP) { |
if(s->type == XVID_TYPE_BVOP) { |
622 |
|
|
623 |
twopass_stat_t *b_ref = s; |
twopass_stat_t *b_ref = s; |
627 |
b_ref--; |
b_ref--; |
628 |
|
|
629 |
/* Compute the original quant */ |
/* Compute the original quant */ |
630 |
s->quant = 100*s->quant - data->bquant_offset; |
s->quant = 2*(100*s->quant - data->bquant_offset); |
631 |
s->quant += data->bquant_ratio - 1; /* to avoid rouding issues */ |
s->quant += data->bquant_ratio - 1; /* to avoid rounding issues */ |
632 |
s->quant = s->quant/data->bquant_ratio - b_ref->quant; |
s->quant = s->quant/data->bquant_ratio - b_ref->quant; |
633 |
} |
} |
634 |
|
#endif |
635 |
|
|
636 |
/* Don't laugh at this very 'simple' quant<->filesize relationship, it |
/* Don't laugh at this very 'simple' quant<->size relationship, it |
637 |
* proves to be acurate enough for our algorithm */ |
* proves to be acurate enough for our algorithm */ |
638 |
scaled_quant = (double)s->quant*(double)s->length/(double)dbytes; |
scaled_quant = (double)s->quant*(double)s->length/(double)dbytes; |
639 |
|
|
721 |
/* Don't forget to force 1st pass frame type ;-) */ |
/* Don't forget to force 1st pass frame type ;-) */ |
722 |
data->type = s->type; |
data->type = s->type; |
723 |
|
|
|
/* Store the quantizer into the statistics -- Used to compensate the double |
|
|
* formula symptom */ |
|
|
s->quant2 = data->quant; |
|
|
|
|
724 |
return 0; |
return 0; |
725 |
} |
} |
726 |
|
|
782 |
rc->KFoverflow -= rc->KFoverflow_partial; |
rc->KFoverflow -= rc->KFoverflow_partial; |
783 |
} |
} |
784 |
|
|
785 |
rc->overflow += s->error = s->desired_length - data->length; |
rc->overflow += (s->error = s->desired_length - data->length); |
786 |
rc->real_total += data->length; |
rc->real_total += data->length; |
787 |
|
|
788 |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- frame:%d type:%c quant:%d stats:%d scaled:%d desired:%d actual:%d error:%d overflow:%.2f\n", |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- frame:%d type:%c quant:%d stats:%d scaled:%d desired:%d actual:%d error:%d overflow:%.2f\n", |
831 |
|
|
832 |
char *ptr; |
char *ptr; |
833 |
char type; |
char type; |
834 |
int fields, nouse; |
int fields; |
835 |
|
|
836 |
lines++; |
lines++; |
837 |
|
|
838 |
/* We skip spaces */ |
/* We skip spaces */ |
839 |
ptr = skipspaces(line); |
ptr = skipspaces(line); |
840 |
|
|
841 |
/* Skip coment lines */ |
/* Skip coment lines or empty lines */ |
842 |
if(iscomment(ptr)) { |
if(iscomment(ptr) || *ptr == '\0') { |
843 |
free(line); |
free(line); |
844 |
continue; |
continue; |
845 |
} |
} |
846 |
|
|
847 |
/* Read the stat line from buffer */ |
/* Read the stat line from buffer */ |
848 |
fields = sscanf(ptr, |
fields = sscanf(ptr, "%c", &type); |
|
"%c %d %d %d %d %d", |
|
|
&type, &nouse, &nouse, &nouse, &nouse, &nouse); |
|
849 |
|
|
850 |
/* Valid stats files have at least 6 fields */ |
/* Valid stats files have at least 7 fields */ |
851 |
if (fields == 6) { |
if (fields == 1) { |
852 |
switch(type) { |
switch(type) { |
853 |
case 'i': |
case 'i': |
854 |
case 'I': |
case 'I': |
869 |
} else { |
} else { |
870 |
DPRINTF(XVID_DEBUG_RC, |
DPRINTF(XVID_DEBUG_RC, |
871 |
"[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n", |
"[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n", |
872 |
lines, 6-fields); |
lines, 7-fields); |
873 |
} |
} |
874 |
|
|
875 |
/* Free the line buffer */ |
/* Free the line buffer */ |
907 |
/* We skip spaces */ |
/* We skip spaces */ |
908 |
ptr = skipspaces(line); |
ptr = skipspaces(line); |
909 |
|
|
910 |
/* Skip comment lines */ |
/* Skip comment lines or empty lines */ |
911 |
if(iscomment(ptr)) { |
if(iscomment(ptr) || *ptr == '\0') { |
912 |
free(line); |
free(line); |
913 |
continue; |
continue; |
914 |
} |
} |
918 |
|
|
919 |
/* Convert the fields */ |
/* Convert the fields */ |
920 |
fields = sscanf(ptr, |
fields = sscanf(ptr, |
921 |
"%c %d %d %d %d %d %d\n", |
"%c %d %d %d %d %d %d %d\n", |
922 |
&type, |
&type, |
923 |
&s->quant, |
&s->quant, |
924 |
&s->blks[0], &s->blks[1], &s->blks[2], |
&s->blks[0], &s->blks[1], &s->blks[2], |
925 |
&s->length, |
&s->length, &s->invariant /* not really yet */, |
926 |
&s->scaled_length); |
&s->scaled_length); |
927 |
|
|
928 |
/* Free line buffer, we don't need it anymore */ |
/* Free line buffer, we don't need it anymore */ |
930 |
|
|
931 |
/* Fail silently, this has probably been warned in |
/* Fail silently, this has probably been warned in |
932 |
* statsfile_count_frames */ |
* statsfile_count_frames */ |
933 |
if(fields != 6 && fields != 7) |
if(fields != 7 && fields != 8) |
934 |
continue; |
continue; |
935 |
|
|
936 |
/* Convert frame type */ |
/* Convert frame type and compute the invariant length part */ |
937 |
switch(type) { |
switch(type) { |
938 |
case 'i': |
case 'i': |
939 |
case 'I': |
case 'I': |
940 |
s->type = XVID_TYPE_IVOP; |
s->type = XVID_TYPE_IVOP; |
941 |
|
s->invariant /= INVARIANT_HEADER_PART_IVOP; |
942 |
break; |
break; |
943 |
case 'p': |
case 'p': |
944 |
case 'P': |
case 'P': |
945 |
case 's': |
case 's': |
946 |
case 'S': |
case 'S': |
947 |
s->type = XVID_TYPE_PVOP; |
s->type = XVID_TYPE_PVOP; |
948 |
|
s->invariant /= INVARIANT_HEADER_PART_PVOP; |
949 |
break; |
break; |
950 |
case 'b': |
case 'b': |
951 |
case 'B': |
case 'B': |
952 |
s->type = XVID_TYPE_BVOP; |
s->type = XVID_TYPE_BVOP; |
953 |
|
s->invariant /= INVARIANT_HEADER_PART_BVOP; |
954 |
break; |
break; |
955 |
default: |
default: |
956 |
/* Same as before, fail silently */ |
/* Same as before, fail silently */ |
981 |
for (i=0; i<3; i++) { |
for (i=0; i<3; i++) { |
982 |
rc->count[i]=0; |
rc->count[i]=0; |
983 |
rc->tot_length[i] = 0; |
rc->tot_length[i] = 0; |
984 |
|
rc->tot_invariant[i] = 0; |
985 |
rc->min_length[i] = INT_MAX; |
rc->min_length[i] = INT_MAX; |
986 |
} |
} |
987 |
|
|
994 |
|
|
995 |
rc->count[s->type-1]++; |
rc->count[s->type-1]++; |
996 |
rc->tot_length[s->type-1] += s->length; |
rc->tot_length[s->type-1] += s->length; |
997 |
|
rc->tot_invariant[s->type-1] += s->invariant; |
998 |
|
|
999 |
if (s->length < rc->min_length[s->type-1]) { |
if (s->length < rc->min_length[s->type-1]) { |
1000 |
rc->min_length[s->type-1] = s->length; |
rc->min_length[s->type-1] = s->length; |
1031 |
|
|
1032 |
rc->avg_weight = 0.0; |
rc->avg_weight = 0.0; |
1033 |
rc->tot_quant = 0; |
rc->tot_quant = 0; |
1034 |
|
rc->tot_quant_invariant = 0; |
1035 |
|
|
1036 |
if (create->num_zones == 0) { |
if (create->num_zones == 0) { |
1037 |
for (j = 0; j < rc->num_frames; j++) { |
for (j = 0; j < rc->num_frames; j++) { |
1069 |
rc->stats[j].zone_mode = XVID_ZONE_QUANT; |
rc->stats[j].zone_mode = XVID_ZONE_QUANT; |
1070 |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
1071 |
rc->tot_quant += rc->stats[j].length; |
rc->tot_quant += rc->stats[j].length; |
1072 |
|
rc->tot_quant_invariant += rc->stats[j].invariant; |
1073 |
} |
} |
1074 |
} |
} |
1075 |
} |
} |
1085 |
{ |
{ |
1086 |
int64_t target; |
int64_t target; |
1087 |
int64_t pass1_length; |
int64_t pass1_length; |
1088 |
|
int64_t total_invariant; |
1089 |
double scaler; |
double scaler; |
1090 |
int i, num_MBs; |
int i, num_MBs; |
1091 |
|
|
1092 |
/* We remove the bytes used by the fixed quantizer zones |
/* We only scale texture data ! */ |
1093 |
* ToDo: this approach is flawed, the same amount of bytes is removed from |
total_invariant = rc->tot_invariant[XVID_TYPE_IVOP-1]; |
1094 |
* target and first pass data, this has no sense, zone_process should |
total_invariant += rc->tot_invariant[XVID_TYPE_PVOP-1]; |
1095 |
* give us two results one for unscaled data (1pass) and the other |
total_invariant += rc->tot_invariant[XVID_TYPE_BVOP-1]; |
1096 |
* one for scaled data and we should then write: |
/* don't forget to substract header bytes used in quant zones, otherwise we |
1097 |
* target = rc->target - rc->tot_quant_scaled; |
* counting them twice */ |
1098 |
* pass1_length = rc->i+p+b - rc->tot_quant_firstpass */ |
total_invariant -= rc->tot_quant_invariant; |
1099 |
target = rc->target - rc->tot_quant; |
|
1100 |
|
/* We remove the bytes used by the fixed quantizer zones during first pass |
1101 |
|
* with the same quants, so we know very precisely how much that |
1102 |
|
* represents */ |
1103 |
|
target = rc->target; |
1104 |
|
target -= rc->tot_quant; |
1105 |
|
|
1106 |
/* Do the same for the first pass data */ |
/* Do the same for the first pass data */ |
1107 |
pass1_length = rc->tot_length[XVID_TYPE_IVOP-1]; |
pass1_length = rc->tot_length[XVID_TYPE_IVOP-1]; |
1110 |
pass1_length -= rc->tot_quant; |
pass1_length -= rc->tot_quant; |
1111 |
|
|
1112 |
/* Let's compute a linear scaler in order to perform curve scaling */ |
/* Let's compute a linear scaler in order to perform curve scaling */ |
1113 |
scaler = (double)target / (double)pass1_length; |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
1114 |
|
|
1115 |
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
if ((target - total_invariant) <= 0 || |
1116 |
|
(pass1_length - total_invariant) <= 0 || |
1117 |
|
target >= pass1_length) { |
1118 |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n"); |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n"); |
1119 |
scaler = 1.0; |
scaler = 1.0; |
1120 |
} |
} |
1159 |
continue; |
continue; |
1160 |
} |
} |
1161 |
|
|
1162 |
/* Compute the scaled length */ |
/* Compute the scaled length -- only non invariant data length is scaled */ |
1163 |
len = (int)((double)s->length * scaler * s->weight / rc->avg_weight); |
len = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight / rc->avg_weight); |
1164 |
|
|
1165 |
/* Compare with the computed minimum */ |
/* Compare with the computed minimum */ |
1166 |
if (len < rc->min_length[s->type-1]) { |
if (len < rc->min_length[s->type-1]) { |
1183 |
* total counters. Now, it's possible to scale the 'regular' frames. */ |
* total counters. Now, it's possible to scale the 'regular' frames. */ |
1184 |
|
|
1185 |
/* Scaling factor for 'regular' frames */ |
/* Scaling factor for 'regular' frames */ |
1186 |
scaler = (double)target / (double)pass1_length; |
scaler = (double)(target - total_invariant) / (double)(pass1_length - total_invariant); |
1187 |
|
|
1188 |
/* Detect undersizing */ |
/* Detect undersizing */ |
1189 |
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
1197 |
|
|
1198 |
/* Ignore frame with forced frame sizes */ |
/* Ignore frame with forced frame sizes */ |
1199 |
if (s->scaled_length == 0) |
if (s->scaled_length == 0) |
1200 |
s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight); |
s->scaled_length = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight / rc->avg_weight); |
1201 |
} |
} |
1202 |
|
|
1203 |
/* Job done */ |
/* Job done */ |
1212 |
scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc) |
scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc) |
1213 |
{ |
{ |
1214 |
int i; |
int i; |
1215 |
uint64_t ivop_boost_total; |
int64_t ivop_boost_total; |
1216 |
|
|
1217 |
/* Reset the rate controller (per frame type) total byte counters */ |
/* Reset the rate controller (per frame type) total byte counters */ |
1218 |
for (i=0; i<3; i++) rc->tot_scaled_length[i] = 0; |
for (i=0; i<3; i++) rc->tot_scaled_length[i] = 0; |
1235 |
|
|
1236 |
/* Some more work is needed for I frames */ |
/* Some more work is needed for I frames */ |
1237 |
if (s->type == XVID_TYPE_IVOP) { |
if (s->type == XVID_TYPE_IVOP) { |
|
int penalty_distance; |
|
1238 |
int ivop_boost; |
int ivop_boost; |
1239 |
|
|
1240 |
/* Accumulate bytes needed for keyframe boosting */ |
/* Accumulate bytes needed for keyframe boosting */ |
1241 |
ivop_boost = s->scaled_length*rc->param.keyframe_boost/100; |
ivop_boost = s->scaled_length*rc->param.keyframe_boost/100; |
1242 |
|
|
1243 |
if (rc->KF_idx) { |
#if 0 /* ToDo: decide how to apply kfthresholding */ |
1244 |
/* Minimum keyframe distance penalties */ |
#endif |
|
penalty_distance = rc->param.min_key_interval; |
|
|
penalty_distance -= rc->keyframe_locations[rc->KF_idx]; |
|
|
penalty_distance += rc->keyframe_locations[rc->KF_idx-1]; |
|
|
|
|
|
/* Ah ah ! guilty keyframe, you're under arrest ! */ |
|
|
if (penalty_distance > 0) |
|
|
ivop_boost -= (s->scaled_length + ivop_boost)*penalty_distance*rc->param.kfreduction/100; |
|
|
} |
|
|
|
|
1245 |
/* If the frame size drops under the minimum length, then cap ivop_boost */ |
/* If the frame size drops under the minimum length, then cap ivop_boost */ |
1246 |
if (ivop_boost + s->scaled_length < rc->min_length[XVID_TYPE_IVOP-1]) |
if (ivop_boost + s->scaled_length < rc->min_length[XVID_TYPE_IVOP-1]) |
1247 |
ivop_boost = rc->min_length[XVID_TYPE_IVOP-1] - s->scaled_length; |
ivop_boost = rc->min_length[XVID_TYPE_IVOP-1] - s->scaled_length; |
1271 |
|
|
1272 |
/* Compute the ratio described above |
/* Compute the ratio described above |
1273 |
* taxed_total = sum(0, n, tax*scaled_length) |
* taxed_total = sum(0, n, tax*scaled_length) |
1274 |
* <=> taxed_total = tax.sum(0, n, tax*scaled_length) |
* <=> taxed_total = tax.sum(0, n, scaled_length) |
1275 |
* <=> tax = taxed_total / original_total */ |
* <=> tax = taxed_total / original_total */ |
1276 |
rc->pb_iboost_tax_ratio = |
rc->pb_iboost_tax_ratio = |
1277 |
(rc->pb_iboost_tax_ratio - ivop_boost_total) / |
(rc->pb_iboost_tax_ratio - ivop_boost_total) / |
1288 |
} else { |
} else { |
1289 |
rc->avg_length[i] = rc->tot_scaled_length[i]; |
rc->avg_length[i] = rc->tot_scaled_length[i]; |
1290 |
|
|
1291 |
if (i == XVID_TYPE_IVOP) { |
if (i == (XVID_TYPE_IVOP-1)) { |
1292 |
/* I Frames total has to be added the boost total */ |
/* I Frames total has to be added the boost total */ |
1293 |
rc->avg_length[i] += ivop_boost_total; |
rc->avg_length[i] += ivop_boost_total; |
1294 |
} else { |
} else { |