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 |
|
|
714 |
if (codec->framenum == 0) |
if (codec->framenum == 0) |
715 |
{ |
{ |
940 |
} |
} |
941 |
|
|
942 |
// Foxer: make sure overflow doesn't run away |
// Foxer: make sure overflow doesn't run away |
943 |
if (overflow > bytes2 * 6 / 10) |
if (overflow > bytes2 * codec->config.twopass_max_overflow_improvement / 100) |
944 |
{ |
{ |
945 |
bytes2 += (overflow <= bytes2) ? bytes2 * 6 / 10 : overflow * 6 / 10; |
bytes2 += (overflow <= bytes2) ? bytes2 * codec->config.twopass_max_overflow_improvement / 100 : |
946 |
|
overflow * codec->config.twopass_max_overflow_improvement / 100; |
947 |
} |
} |
948 |
else if (overflow < bytes2 * -6 / 10) |
else if (overflow < bytes2 * codec->config.twopass_max_overflow_degradation / -100) |
949 |
{ |
{ |
950 |
bytes2 += bytes2 * -6 / 10; |
bytes2 += bytes2 * codec->config.twopass_max_overflow_degradation / -100; |
951 |
} |
} |
952 |
else |
else |
953 |
{ |
{ |
954 |
bytes2 += overflow; |
bytes2 += overflow; |
955 |
} |
} |
956 |
|
|
957 |
|
if (bytes2 > twopass->max_framesize) |
958 |
|
{ |
959 |
|
capped_to_max_framesize = 1; |
960 |
|
bytes2 = twopass->max_framesize; |
961 |
|
} |
962 |
|
|
963 |
if (bytes2 < 1) |
if (bytes2 < 1) |
964 |
{ |
{ |
965 |
bytes2 = 1; |
bytes2 = 1; |
1023 |
} |
} |
1024 |
|
|
1025 |
// subsequent frame quants can only be +- 2 |
// subsequent frame quants can only be +- 2 |
1026 |
if (last_quant) |
if (last_quant && capped_to_max_framesize == 0) |
1027 |
{ |
{ |
1028 |
if (frame->quant > last_quant + 2) |
if (frame->quant > last_quant + 2) |
1029 |
{ |
{ |
1038 |
} |
} |
1039 |
} |
} |
1040 |
|
|
1041 |
|
if (capped_to_max_framesize == 0) |
1042 |
last_quant = frame->quant; |
last_quant = frame->quant; |
1043 |
|
|
1044 |
if (codec->config.quant_type == QUANT_MODE_MOD) |
if (codec->config.quant_type == QUANT_MODE_MOD) |
1057 |
|
|
1058 |
NNSTATS nns1; |
NNSTATS nns1; |
1059 |
DWORD wrote; |
DWORD wrote; |
1060 |
int credits_pos; |
int credits_pos, tempdiv; |
1061 |
char* quant_type; |
char* quant_type; |
1062 |
|
|
1063 |
if (codec->framenum == 0) |
if (codec->framenum == 0) |
1080 |
nns1.md_u = nns1.md_y = 0; |
nns1.md_u = nns1.md_y = 0; |
1081 |
nns1.mk_u = nns1.mk_y = 0; |
nns1.mk_u = nns1.mk_y = 0; |
1082 |
|
|
1083 |
nns1.quant = stats->quant; |
// nns1.quant = stats->quant; |
1084 |
|
nns1.quant = 2; // ugly fix for lumi masking in 1st pass returning new quant |
1085 |
if (frame->intra) |
if (frame->intra) |
1086 |
{ |
{ |
1087 |
nns1.quant |= NNSTATS_KEYFRAME; |
nns1.quant |= NNSTATS_KEYFRAME; |
1104 |
|
|
1105 |
case DLG_MODE_2PASS_2_INT : |
case DLG_MODE_2PASS_2_INT : |
1106 |
case DLG_MODE_2PASS_2_EXT : |
case DLG_MODE_2PASS_2_EXT : |
|
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length; |
|
|
|
|
1107 |
credits_pos = codec_is_in_credits(&codec->config, codec->framenum); |
credits_pos = codec_is_in_credits(&codec->config, codec->framenum); |
1108 |
if (!credits_pos) |
if (!credits_pos) |
1109 |
|
{ |
1110 |
codec->twopass.quant_count[frame->quant]++; |
codec->twopass.quant_count[frame->quant]++; |
1111 |
|
if ((codec->twopass.nns1.quant & NNSTATS_KEYFRAME)) |
1112 |
|
{ |
1113 |
|
codec->twopass.overflow += codec->twopass.KFoverflow; |
1114 |
|
codec->twopass.KFoverflow = codec->twopass.desired_bytes2 - frame->length; |
1115 |
|
|
1116 |
|
tempdiv = (codec->twopass.keyframe_locations[codec->twopass.KF_idx] - |
1117 |
|
codec->twopass.keyframe_locations[codec->twopass.KF_idx - 1]); |
1118 |
|
|
1119 |
|
if (tempdiv > 1) |
1120 |
|
{ |
1121 |
|
// non-consecutive keyframes |
1122 |
|
codec->twopass.KFoverflow_partial = codec->twopass.KFoverflow / (tempdiv - 1); |
1123 |
|
} |
1124 |
|
else |
1125 |
|
{ |
1126 |
|
// consecutive keyframes |
1127 |
|
codec->twopass.overflow += codec->twopass.KFoverflow; |
1128 |
|
codec->twopass.KFoverflow = 0; |
1129 |
|
codec->twopass.KFoverflow_partial = 0; |
1130 |
|
} |
1131 |
|
codec->twopass.KF_idx++; |
1132 |
|
} |
1133 |
|
else |
1134 |
|
{ |
1135 |
|
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length + |
1136 |
|
codec->twopass.KFoverflow_partial; |
1137 |
|
codec->twopass.KFoverflow -= codec->twopass.KFoverflow_partial; |
1138 |
|
} |
1139 |
|
} |
1140 |
|
else |
1141 |
|
{ |
1142 |
|
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length; |
1143 |
|
|
1144 |
|
// ugly fix for credits.. |
1145 |
|
codec->twopass.overflow += codec->twopass.KFoverflow; |
1146 |
|
codec->twopass.KFoverflow = 0; |
1147 |
|
codec->twopass.KFoverflow_partial = 0; |
1148 |
|
// end of ugly fix. |
1149 |
|
} |
1150 |
|
|
1151 |
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) |
1152 |
break; |
break; |