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 |
*************************************************************************/ |
*************************************************************************/ |
702 |
int bytes1, bytes2; |
int bytes1, bytes2; |
703 |
int overflow; |
int overflow; |
704 |
int credits_pos; |
int credits_pos; |
705 |
|
int capped_to_max_framesize = 0; |
706 |
|
|
707 |
if (codec->framenum == 0) |
if (codec->framenum == 0) |
708 |
{ |
{ |
711 |
for (i=0 ; i<32 ; ++i) |
for (i=0 ; i<32 ; ++i) |
712 |
{ |
{ |
713 |
quant_error[i] = 0.0; |
quant_error[i] = 0.0; |
714 |
|
twopass->quant_count[i] = 0; |
715 |
} |
} |
716 |
|
|
717 |
curve_comp_error = 0.0; |
curve_comp_error = 0.0; |
933 |
} |
} |
934 |
|
|
935 |
// Foxer: make sure overflow doesn't run away |
// Foxer: make sure overflow doesn't run away |
936 |
if (overflow > bytes2 * 6 / 10) |
if (overflow > bytes2 * codec->config.twopass_max_overflow_improvement / 100) |
937 |
{ |
{ |
938 |
bytes2 += (overflow <= bytes2) ? bytes2 * 6 / 10 : overflow * 6 / 10; |
bytes2 += (overflow <= bytes2) ? bytes2 * codec->config.twopass_max_overflow_improvement / 100 : |
939 |
|
overflow * codec->config.twopass_max_overflow_improvement / 100; |
940 |
} |
} |
941 |
else if (overflow < bytes2 * -6 / 10) |
else if (overflow < bytes2 * codec->config.twopass_max_overflow_degradation / -100) |
942 |
{ |
{ |
943 |
bytes2 += bytes2 * -6 / 10; |
bytes2 += bytes2 * codec->config.twopass_max_overflow_degradation / -100; |
944 |
} |
} |
945 |
else |
else |
946 |
{ |
{ |
947 |
bytes2 += overflow; |
bytes2 += overflow; |
948 |
} |
} |
949 |
|
|
950 |
|
if (bytes2 > twopass->max_framesize) |
951 |
|
{ |
952 |
|
capped_to_max_framesize = 1; |
953 |
|
bytes2 = twopass->max_framesize; |
954 |
|
} |
955 |
|
|
956 |
if (bytes2 < 1) |
if (bytes2 < 1) |
957 |
{ |
{ |
958 |
bytes2 = 1; |
bytes2 = 1; |
1016 |
} |
} |
1017 |
|
|
1018 |
// subsequent frame quants can only be +- 2 |
// subsequent frame quants can only be +- 2 |
1019 |
if (last_quant) |
if (last_quant && capped_to_max_framesize == 0) |
1020 |
{ |
{ |
1021 |
if (frame->quant > last_quant + 2) |
if (frame->quant > last_quant + 2) |
1022 |
{ |
{ |
1031 |
} |
} |
1032 |
} |
} |
1033 |
|
|
1034 |
|
if (capped_to_max_framesize == 0) |
1035 |
last_quant = frame->quant; |
last_quant = frame->quant; |
1036 |
|
|
1037 |
if (codec->config.quant_type == QUANT_MODE_MOD) |
if (codec->config.quant_type == QUANT_MODE_MOD) |
1050 |
|
|
1051 |
NNSTATS nns1; |
NNSTATS nns1; |
1052 |
DWORD wrote; |
DWORD wrote; |
1053 |
|
int credits_pos; |
1054 |
char* quant_type; |
char* quant_type; |
1055 |
|
|
1056 |
if (codec->framenum == 0) |
if (codec->framenum == 0) |
1073 |
nns1.md_u = nns1.md_y = 0; |
nns1.md_u = nns1.md_y = 0; |
1074 |
nns1.mk_u = nns1.mk_y = 0; |
nns1.mk_u = nns1.mk_y = 0; |
1075 |
|
|
1076 |
nns1.quant = stats->quant; |
// nns1.quant = stats->quant; |
1077 |
|
nns1.quant = 2; // ugly fix for lumi masking in 1st pass returning new quant |
1078 |
if (frame->intra) |
if (frame->intra) |
1079 |
{ |
{ |
1080 |
nns1.quant |= NNSTATS_KEYFRAME; |
nns1.quant |= NNSTATS_KEYFRAME; |
1098 |
case DLG_MODE_2PASS_2_INT : |
case DLG_MODE_2PASS_2_INT : |
1099 |
case DLG_MODE_2PASS_2_EXT : |
case DLG_MODE_2PASS_2_EXT : |
1100 |
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length; |
codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length; |
1101 |
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)) |
|
1102 |
|
credits_pos = codec_is_in_credits(&codec->config, codec->framenum); |
1103 |
|
if (!credits_pos) |
1104 |
|
codec->twopass.quant_count[frame->quant]++; |
1105 |
|
|
1106 |
|
DEBUG2ND(frame->quant, quant_type, frame->intra, codec->twopass.bytes1, codec->twopass.desired_bytes2, frame->length, codec->twopass.overflow, credits_pos) |
1107 |
break; |
break; |
1108 |
|
|
1109 |
default: |
default: |
1113 |
return ICERR_OK; |
return ICERR_OK; |
1114 |
} |
} |
1115 |
|
|
1116 |
|
void codec_2pass_finish(CODEC* codec) |
1117 |
|
{ |
1118 |
|
int i; |
1119 |
|
char s[100]; |
1120 |
|
if (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT) |
1121 |
|
{ |
1122 |
|
// output the quantizer distribution for this encode. |
1123 |
|
|
1124 |
|
OutputDebugString("Quantizer distribution for 2nd pass:"); |
1125 |
|
for (i=1; i<=31; i++) |
1126 |
|
{ |
1127 |
|
if (codec->twopass.quant_count[i]) |
1128 |
|
{ |
1129 |
|
wsprintf(s, "Q:%i:%i", i, codec->twopass.quant_count[i]); |
1130 |
|
OutputDebugString(s); |
1131 |
|
} |
1132 |
|
} |
1133 |
|
return; |
1134 |
|
} |
1135 |
|
} |