23 |
* |
* |
24 |
* History: |
* History: |
25 |
* |
* |
26 |
|
* 17.04.2002 changed 1st pass quant to always be 2 (2pass_update()) |
27 |
|
* 07.04.2002 added max bitrate constraint, overflow controls (foxer) |
28 |
* 31.03.2002 inital version; |
* 31.03.2002 inital version; |
29 |
* |
* |
30 |
*************************************************************************/ |
*************************************************************************/ |
163 |
{ |
{ |
164 |
i_boost_total = twopass->nns2.bytes * codec->config.keyframe_boost / 100; |
i_boost_total = twopass->nns2.bytes * codec->config.keyframe_boost / 100; |
165 |
i_total += twopass->nns2.bytes; |
i_total += twopass->nns2.bytes; |
166 |
|
twopass->keyframe_locations[i_frames] = frames; |
167 |
++i_frames; |
++i_frames; |
168 |
} |
} |
169 |
|
|
175 |
|
|
176 |
++frames; |
++frames; |
177 |
} |
} |
178 |
|
twopass->keyframe_locations[i_frames] = frames; |
179 |
|
|
180 |
twopass->movie_curve = ((double)(total_ext + i_boost_total) / total_ext); |
twopass->movie_curve = ((double)(total_ext + i_boost_total) / total_ext); |
181 |
twopass->average_frame = ((double)(total_ext - i_total) / (frames - credits_frames - i_frames) / twopass->movie_curve); |
twopass->average_frame = ((double)(total_ext - i_total) / (frames - credits_frames - i_frames) / twopass->movie_curve); |
373 |
{ |
{ |
374 |
i_total += twopass->nns1.bytes + twopass->nns1.bytes * codec->config.keyframe_boost / 100; |
i_total += twopass->nns1.bytes + twopass->nns1.bytes * codec->config.keyframe_boost / 100; |
375 |
total += twopass->nns1.bytes * codec->config.keyframe_boost / 100; |
total += twopass->nns1.bytes * codec->config.keyframe_boost / 100; |
376 |
|
twopass->keyframe_locations[i_frames] = frames; |
377 |
++i_frames; |
++i_frames; |
378 |
} |
} |
379 |
|
|
381 |
|
|
382 |
++frames; |
++frames; |
383 |
} |
} |
384 |
|
twopass->keyframe_locations[i_frames] = frames; |
385 |
|
|
386 |
// compensate for avi frame overhead |
// compensate for avi frame overhead |
387 |
desired -= frames * 24; |
desired -= frames * 24; |
686 |
} |
} |
687 |
|
|
688 |
twopass->overflow = 0; |
twopass->overflow = 0; |
689 |
|
twopass->KFoverflow = 0; |
690 |
|
twopass->KFoverflow_partial = 0; |
691 |
|
twopass->KF_idx = 1; |
692 |
|
|
693 |
break; |
break; |
694 |
} |
} |
709 |
int bytes1, bytes2; |
int bytes1, bytes2; |
710 |
int overflow; |
int overflow; |
711 |
int credits_pos; |
int credits_pos; |
712 |
|
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); |
967 |
} |
} |
968 |
|
|
969 |
// Foxer: make sure overflow doesn't run away |
// Foxer: make sure overflow doesn't run away |
970 |
if (overflow > bytes2 * 6 / 10) |
if (overflow > bytes2 * codec->config.twopass_max_overflow_improvement / 100) |
971 |
{ |
{ |
972 |
bytes2 += (overflow <= bytes2) ? bytes2 * 6 / 10 : overflow * 6 / 10; |
bytes2 += (overflow <= bytes2) ? bytes2 * codec->config.twopass_max_overflow_improvement / 100 : |
973 |
|
overflow * codec->config.twopass_max_overflow_improvement / 100; |
974 |
} |
} |
975 |
else if (overflow < bytes2 * -6 / 10) |
else if (overflow < bytes2 * codec->config.twopass_max_overflow_degradation / -100) |
976 |
{ |
{ |
977 |
bytes2 += bytes2 * -6 / 10; |
bytes2 += bytes2 * codec->config.twopass_max_overflow_degradation / -100; |
978 |
} |
} |
979 |
else |
else |
980 |
{ |
{ |
981 |
bytes2 += overflow; |
bytes2 += overflow; |
982 |
} |
} |
983 |
|
|
984 |
|
if (bytes2 > twopass->max_framesize) |
985 |
|
{ |
986 |
|
capped_to_max_framesize = 1; |
987 |
|
bytes2 = twopass->max_framesize; |
988 |
|
} |
989 |
|
|
990 |
if (bytes2 < 1) |
if (bytes2 < 1) |
991 |
{ |
{ |
992 |
bytes2 = 1; |
bytes2 = 1; |
1050 |
} |
} |
1051 |
|
|
1052 |
// subsequent frame quants can only be +- 2 |
// subsequent frame quants can only be +- 2 |
1053 |
if (last_quant) |
if (last_quant && capped_to_max_framesize == 0) |
1054 |
{ |
{ |
1055 |
if (frame->quant > last_quant + 2) |
if (frame->quant > last_quant + 2) |
1056 |
{ |
{ |
1065 |
} |
} |
1066 |
} |
} |
1067 |
|
|
1068 |
|
if (capped_to_max_framesize == 0) |
1069 |
last_quant = frame->quant; |
last_quant = frame->quant; |
1070 |
|
|
1071 |
if (codec->config.quant_type == QUANT_MODE_MOD) |
if (codec->config.quant_type == QUANT_MODE_MOD) |
1084 |
|
|
1085 |
NNSTATS nns1; |
NNSTATS nns1; |
1086 |
DWORD wrote; |
DWORD wrote; |
1087 |
int credits_pos; |
int credits_pos, tempdiv; |
1088 |
char* quant_type; |
char* quant_type; |
1089 |
|
|
1090 |
if (codec->framenum == 0) |
if (codec->framenum == 0) |
1107 |
nns1.md_u = nns1.md_y = 0; |
nns1.md_u = nns1.md_y = 0; |
1108 |
nns1.mk_u = nns1.mk_y = 0; |
nns1.mk_u = nns1.mk_y = 0; |
1109 |
|
|
1110 |
nns1.quant = stats->quant; |
// nns1.quant = stats->quant; |
1111 |
|
nns1.quant = 2; // ugly fix for lumi masking in 1st pass returning new quant |
1112 |
if (frame->intra) |
if (frame->intra) |
1113 |
{ |
{ |
1114 |
nns1.quant |= NNSTATS_KEYFRAME; |
nns1.quant |= NNSTATS_KEYFRAME; |
1131 |
|
|
1132 |
case DLG_MODE_2PASS_2_INT : |
case DLG_MODE_2PASS_2_INT : |
1133 |
case DLG_MODE_2PASS_2_EXT : |
case DLG_MODE_2PASS_2_EXT : |
|
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length; |
|
|
|
|
1134 |
credits_pos = codec_is_in_credits(&codec->config, codec->framenum); |
credits_pos = codec_is_in_credits(&codec->config, codec->framenum); |
1135 |
if (!credits_pos) |
if (!credits_pos) |
1136 |
|
{ |
1137 |
codec->twopass.quant_count[frame->quant]++; |
codec->twopass.quant_count[frame->quant]++; |
1138 |
|
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; |
1144 |
|
codec->twopass.KFoverflow = codec->twopass.desired_bytes2 - frame->length; |
1145 |
|
|
1146 |
|
tempdiv = (codec->twopass.keyframe_locations[codec->twopass.KF_idx] - |
1147 |
|
codec->twopass.keyframe_locations[codec->twopass.KF_idx - 1]); |
1148 |
|
|
1149 |
|
if (tempdiv > 1) |
1150 |
|
{ |
1151 |
|
// non-consecutive keyframes |
1152 |
|
codec->twopass.KFoverflow_partial = codec->twopass.KFoverflow / (tempdiv - 1); |
1153 |
|
} |
1154 |
|
else |
1155 |
|
{ |
1156 |
|
// consecutive keyframes |
1157 |
|
codec->twopass.overflow += codec->twopass.KFoverflow; |
1158 |
|
codec->twopass.KFoverflow = 0; |
1159 |
|
codec->twopass.KFoverflow_partial = 0; |
1160 |
|
} |
1161 |
|
codec->twopass.KF_idx++; |
1162 |
|
} |
1163 |
|
else |
1164 |
|
{ |
1165 |
|
// distribute part of the keyframe overflow |
1166 |
|
|
1167 |
|
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length + |
1168 |
|
codec->twopass.KFoverflow_partial; |
1169 |
|
codec->twopass.KFoverflow -= codec->twopass.KFoverflow_partial; |
1170 |
|
} |
1171 |
|
} |
1172 |
|
else |
1173 |
|
{ |
1174 |
|
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length; |
1175 |
|
|
1176 |
|
// ugly fix for credits.. |
1177 |
|
codec->twopass.overflow += codec->twopass.KFoverflow; |
1178 |
|
codec->twopass.KFoverflow = 0; |
1179 |
|
codec->twopass.KFoverflow_partial = 0; |
1180 |
|
// end of ugly fix. |
1181 |
|
} |
1182 |
|
|
1183 |
DEBUG2ND(frame->quant, quant_type, frame->intra, codec->twopass.bytes1, codec->twopass.desired_bytes2, frame->length, codec->twopass.overflow, credits_pos) |
DEBUG2ND(frame->quant, quant_type, frame->intra, codec->twopass.bytes1, codec->twopass.desired_bytes2, frame->length, codec->twopass.overflow, credits_pos) |
1184 |
break; |
break; |