23 |
* |
* |
24 |
* History: |
* History: |
25 |
* |
* |
26 |
|
* 07.04.2002 added max bitrate constraint, overflow controls (foxer) |
27 |
* 31.03.2002 inital version; |
* 31.03.2002 inital version; |
28 |
* |
* |
29 |
*************************************************************************/ |
*************************************************************************/ |
701 |
int bytes1, bytes2; |
int bytes1, bytes2; |
702 |
int overflow; |
int overflow; |
703 |
int credits_pos; |
int credits_pos; |
704 |
|
int capped_to_max_framesize = 0; |
705 |
|
|
706 |
if (codec->framenum == 0) |
if (codec->framenum == 0) |
707 |
{ |
{ |
710 |
for (i=0 ; i<32 ; ++i) |
for (i=0 ; i<32 ; ++i) |
711 |
{ |
{ |
712 |
quant_error[i] = 0.0; |
quant_error[i] = 0.0; |
713 |
|
twopass->quant_count[i] = 0; |
714 |
} |
} |
715 |
|
|
716 |
curve_comp_error = 0.0; |
curve_comp_error = 0.0; |
932 |
} |
} |
933 |
|
|
934 |
// Foxer: make sure overflow doesn't run away |
// Foxer: make sure overflow doesn't run away |
935 |
if (overflow > bytes2 * 6 / 10) |
if (overflow > bytes2 * codec->config.twopass_max_overflow_improvement / 100) |
936 |
{ |
{ |
937 |
bytes2 += (overflow <= bytes2) ? bytes2 * 6 / 10 : overflow * 6 / 10; |
bytes2 += (overflow <= bytes2) ? bytes2 * codec->config.twopass_max_overflow_improvement / 100 : |
938 |
|
overflow * codec->config.twopass_max_overflow_improvement / 100; |
939 |
} |
} |
940 |
else if (overflow < bytes2 * -6 / 10) |
else if (overflow < bytes2 * codec->config.twopass_max_overflow_degradation / -100) |
941 |
{ |
{ |
942 |
bytes2 += bytes2 * -6 / 10; |
bytes2 += bytes2 * codec->config.twopass_max_overflow_degradation / -100; |
943 |
} |
} |
944 |
else |
else |
945 |
{ |
{ |
946 |
bytes2 += overflow; |
bytes2 += overflow; |
947 |
} |
} |
948 |
|
|
949 |
|
if (bytes2 > twopass->max_framesize) |
950 |
|
{ |
951 |
|
capped_to_max_framesize = 1; |
952 |
|
bytes2 = twopass->max_framesize; |
953 |
|
} |
954 |
|
|
955 |
if (bytes2 < 1) |
if (bytes2 < 1) |
956 |
{ |
{ |
957 |
bytes2 = 1; |
bytes2 = 1; |
1015 |
} |
} |
1016 |
|
|
1017 |
// subsequent frame quants can only be +- 2 |
// subsequent frame quants can only be +- 2 |
1018 |
if (last_quant) |
if (last_quant && capped_to_max_framesize == 0) |
1019 |
{ |
{ |
1020 |
if (frame->quant > last_quant + 2) |
if (frame->quant > last_quant + 2) |
1021 |
{ |
{ |
1030 |
} |
} |
1031 |
} |
} |
1032 |
|
|
1033 |
|
if (capped_to_max_framesize == 0) |
1034 |
last_quant = frame->quant; |
last_quant = frame->quant; |
1035 |
|
|
1036 |
if (codec->config.quant_type == QUANT_MODE_MOD) |
if (codec->config.quant_type == QUANT_MODE_MOD) |
1049 |
|
|
1050 |
NNSTATS nns1; |
NNSTATS nns1; |
1051 |
DWORD wrote; |
DWORD wrote; |
1052 |
|
int credits_pos; |
1053 |
char* quant_type; |
char* quant_type; |
1054 |
|
|
1055 |
if (codec->framenum == 0) |
if (codec->framenum == 0) |
1096 |
case DLG_MODE_2PASS_2_INT : |
case DLG_MODE_2PASS_2_INT : |
1097 |
case DLG_MODE_2PASS_2_EXT : |
case DLG_MODE_2PASS_2_EXT : |
1098 |
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length; |
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length; |
1099 |
DEBUG2ND(frame->quant, quant_type, frame->intra, codec->twopass.bytes1, codec->twopass.desired_bytes2, frame->length, codec->twopass.overflow, codec_is_in_credits(&codec->config, codec->framenum)) |
|
1100 |
|
credits_pos = codec_is_in_credits(&codec->config, codec->framenum); |
1101 |
|
if (!credits_pos) |
1102 |
|
codec->twopass.quant_count[frame->quant]++; |
1103 |
|
|
1104 |
|
DEBUG2ND(frame->quant, quant_type, frame->intra, codec->twopass.bytes1, codec->twopass.desired_bytes2, frame->length, codec->twopass.overflow, credits_pos) |
1105 |
break; |
break; |
1106 |
|
|
1107 |
default: |
default: |
1111 |
return ICERR_OK; |
return ICERR_OK; |
1112 |
} |
} |
1113 |
|
|
1114 |
|
void codec_2pass_finish(CODEC* codec) |
1115 |
|
{ |
1116 |
|
int i; |
1117 |
|
char s[100]; |
1118 |
|
if (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT) |
1119 |
|
{ |
1120 |
|
// output the quantizer distribution for this encode. |
1121 |
|
|
1122 |
|
OutputDebugString("Quantizer distribution for 2nd pass:"); |
1123 |
|
for (i=1; i<=31; i++) |
1124 |
|
{ |
1125 |
|
if (codec->twopass.quant_count[i]) |
1126 |
|
{ |
1127 |
|
wsprintf(s, "Q:%i:%i", i, codec->twopass.quant_count[i]); |
1128 |
|
OutputDebugString(s); |
1129 |
|
} |
1130 |
|
} |
1131 |
|
return; |
1132 |
|
} |
1133 |
|
} |