710 |
int overflow; |
int overflow; |
711 |
int credits_pos; |
int credits_pos; |
712 |
int capped_to_max_framesize = 0; |
int capped_to_max_framesize = 0; |
713 |
|
int KFdistance, KF_min_size; |
714 |
|
|
715 |
if (codec->framenum == 0) |
if (codec->framenum == 0) |
716 |
{ |
{ |
930 |
|
|
931 |
twopass->desired_bytes2 = bytes2; |
twopass->desired_bytes2 = bytes2; |
932 |
|
|
933 |
|
// if this keyframe is too close to the next, |
934 |
|
// reduce it's byte allotment |
935 |
|
if (frame->intra && !credits_pos) |
936 |
|
{ |
937 |
|
KFdistance = codec->twopass.keyframe_locations[codec->twopass.KF_idx] - |
938 |
|
codec->twopass.keyframe_locations[codec->twopass.KF_idx - 1]; |
939 |
|
|
940 |
|
if (KFdistance < codec->config.kftreshold) |
941 |
|
{ |
942 |
|
KFdistance = KFdistance - codec->config.min_key_interval; |
943 |
|
|
944 |
|
if (KFdistance >= 0) |
945 |
|
{ |
946 |
|
KF_min_size = bytes2 * (100 - codec->config.kfreduction) / 100; |
947 |
|
if (KF_min_size < 1) |
948 |
|
KF_min_size = 1; |
949 |
|
|
950 |
|
bytes2 = KF_min_size + (bytes2 - KF_min_size) * KFdistance / |
951 |
|
(codec->config.kftreshold - codec->config.min_key_interval); |
952 |
|
|
953 |
|
if (bytes2 < 1) |
954 |
|
bytes2 = 1; |
955 |
|
} |
956 |
|
} |
957 |
|
} |
958 |
|
|
959 |
// Foxer: scale overflow in relation to average size, so smaller frames don't get |
// Foxer: scale overflow in relation to average size, so smaller frames don't get |
960 |
// too much/little bitrate |
// too much/little bitrate |
961 |
overflow = (int)((double)overflow * bytes2 / twopass->average_frame); |
overflow = (int)((double)overflow * bytes2 / twopass->average_frame); |
1137 |
codec->twopass.quant_count[frame->quant]++; |
codec->twopass.quant_count[frame->quant]++; |
1138 |
if ((codec->twopass.nns1.quant & NNSTATS_KEYFRAME)) |
if ((codec->twopass.nns1.quant & NNSTATS_KEYFRAME)) |
1139 |
{ |
{ |
1140 |
|
// calculate how much to distribute per frame in |
1141 |
|
// order to make up for this keyframe's overflow |
1142 |
|
|
1143 |
codec->twopass.overflow += codec->twopass.KFoverflow; |
codec->twopass.overflow += codec->twopass.KFoverflow; |
1144 |
codec->twopass.KFoverflow = codec->twopass.desired_bytes2 - frame->length; |
codec->twopass.KFoverflow = codec->twopass.desired_bytes2 - frame->length; |
1145 |
|
|
1162 |
} |
} |
1163 |
else |
else |
1164 |
{ |
{ |
1165 |
|
// distribute part of the keyframe overflow |
1166 |
|
|
1167 |
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length + |
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length + |
1168 |
codec->twopass.KFoverflow_partial; |
codec->twopass.KFoverflow_partial; |
1169 |
codec->twopass.KFoverflow -= codec->twopass.KFoverflow_partial; |
codec->twopass.KFoverflow -= codec->twopass.KFoverflow_partial; |