113 |
twopass->nns_array_pos = 0; |
twopass->nns_array_pos = 0; |
114 |
twopass->nns_update_pos = 0; |
twopass->nns_update_pos = 0; |
115 |
|
|
116 |
// read the stats file(s) into array(s) and reorder them so they |
/* read the stats file(s) into array(s) and reorder them so they */ |
117 |
// correctly represent the frames that the encoder will receive. |
/* correctly represent the frames that the encoder will receive. */ |
118 |
if (codec->config.mode == DLG_MODE_2PASS_2_EXT) |
if (codec->config.mode == DLG_MODE_2PASS_2_EXT) |
119 |
{ |
{ |
120 |
if (twopass->stats2 != INVALID_HANDLE_VALUE) |
if (twopass->stats2 != INVALID_HANDLE_VALUE) |
172 |
} |
} |
173 |
} |
} |
174 |
|
|
175 |
// increase the allocated memory if necessary |
/* increase the allocated memory if necessary */ |
176 |
if (frames >= twopass->nns_array_size) |
if (frames >= twopass->nns_array_size) |
177 |
{ |
{ |
178 |
twopass->nns1_array = (NNSTATS*)realloc(twopass->nns1_array, |
twopass->nns1_array = (NNSTATS*)realloc(twopass->nns1_array, |
182 |
twopass->nns_array_size = twopass->nns_array_size * 5 / 4 + 1; |
twopass->nns_array_size = twopass->nns_array_size * 5 / 4 + 1; |
183 |
} |
} |
184 |
|
|
185 |
// copy this frame's stats into the arrays |
/* copy this frame's stats into the arrays */ |
186 |
memcpy (&twopass->nns1_array[frames], &twopass->nns1, sizeof(NNSTATS)); |
memcpy (&twopass->nns1_array[frames], &twopass->nns1, sizeof(NNSTATS)); |
187 |
memcpy (&twopass->nns2_array[frames], &twopass->nns2, sizeof(NNSTATS)); |
memcpy (&twopass->nns2_array[frames], &twopass->nns2, sizeof(NNSTATS)); |
188 |
frames++; |
frames++; |
191 |
SetFilePointer(twopass->stats1, sizeof(DWORD), 0, FILE_BEGIN); |
SetFilePointer(twopass->stats1, sizeof(DWORD), 0, FILE_BEGIN); |
192 |
SetFilePointer(twopass->stats2, sizeof(DWORD), 0, FILE_BEGIN); |
SetFilePointer(twopass->stats2, sizeof(DWORD), 0, FILE_BEGIN); |
193 |
} |
} |
194 |
else // DLG_MODE_2PASS_2_INT |
else /* DLG_MODE_2PASS_2_INT */ |
195 |
{ |
{ |
196 |
while (1) |
while (1) |
197 |
{ |
{ |
212 |
} |
} |
213 |
} |
} |
214 |
|
|
215 |
// increase the allocated memory if necessary |
/* increase the allocated memory if necessary */ |
216 |
if (frames >= twopass->nns_array_size) |
if (frames >= twopass->nns_array_size) |
217 |
{ |
{ |
218 |
twopass->nns1_array = (NNSTATS*)realloc(twopass->nns1_array, |
twopass->nns1_array = (NNSTATS*)realloc(twopass->nns1_array, |
220 |
twopass->nns_array_size = twopass->nns_array_size * 5 / 4 + 1; |
twopass->nns_array_size = twopass->nns_array_size * 5 / 4 + 1; |
221 |
} |
} |
222 |
|
|
223 |
// copy this frame's stats into the array |
/* copy this frame's stats into the array */ |
224 |
memcpy (&twopass->nns1_array[frames], &twopass->nns1, sizeof(NNSTATS)); |
memcpy (&twopass->nns1_array[frames], &twopass->nns1, sizeof(NNSTATS)); |
225 |
frames++; |
frames++; |
226 |
} |
} |
233 |
twopass->nns_array_length = frames; |
twopass->nns_array_length = frames; |
234 |
frames = 0; |
frames = 0; |
235 |
|
|
236 |
/* // this isn't necessary with the current core. |
/* /* this isn't necessary with the current core. */ |
237 |
// reorder the array(s) so they are in the order that they were received |
/* reorder the array(s) so they are in the order that they were received */ |
238 |
// IPBBPBB to |
/* IPBBPBB to */ |
239 |
// IBBPBBP |
/* IBBPBBP */ |
240 |
for (i=0; i<twopass->nns_array_length; i++) |
for (i=0; i<twopass->nns_array_length; i++) |
241 |
{ |
{ |
242 |
NNSTATS temp_nns, temp_nns2; |
NNSTATS temp_nns, temp_nns2; |
271 |
} |
} |
272 |
} |
} |
273 |
*/ |
*/ |
274 |
// continue with the initialization.. |
/* continue with the initialization.. */ |
275 |
if (codec->config.mode == DLG_MODE_2PASS_2_EXT) |
if (codec->config.mode == DLG_MODE_2PASS_2_EXT) |
276 |
{ |
{ |
277 |
while (1) |
while (1) |
324 |
|
|
325 |
if (twopass->nns1.quant & NNSTATS_KEYFRAME) |
if (twopass->nns1.quant & NNSTATS_KEYFRAME) |
326 |
{ |
{ |
327 |
// this test needs to be corrected.. |
/* this test needs to be corrected.. */ |
328 |
if (!(twopass->nns1.kblk + twopass->nns1.mblk)) |
if (!(twopass->nns1.kblk + twopass->nns1.mblk)) |
329 |
recminisize = twopass->nns1.bytes; |
recminisize = twopass->nns1.bytes; |
330 |
} |
} |
353 |
twopass->average_pframe = (double)pframe_total_ext / pframes / twopass->movie_curve; |
twopass->average_pframe = (double)pframe_total_ext / pframes / twopass->movie_curve; |
354 |
else |
else |
355 |
if (bframes) |
if (bframes) |
356 |
twopass->average_pframe = twopass->average_bframe; // b-frame packed bitstream fix |
twopass->average_pframe = twopass->average_bframe; /* b-frame packed bitstream fix */ |
357 |
else |
else |
358 |
{ |
{ |
359 |
DEBUGERR("ERROR: No p-frames or b-frames were present in the 1st pass. Rate control cannot function properly!"); |
DEBUGERR("ERROR: No p-frames or b-frames were present in the 1st pass. Rate control cannot function properly!"); |
362 |
|
|
363 |
|
|
364 |
|
|
365 |
// perform prepass to compensate for over/undersizing |
/* perform prepass to compensate for over/undersizing */ |
366 |
frames = 0; |
frames = 0; |
367 |
|
|
368 |
if (codec->config.use_alt_curve) |
if (codec->config.use_alt_curve) |
391 |
{ |
{ |
392 |
switch(codec->config.alt_curve_type) |
switch(codec->config.alt_curve_type) |
393 |
{ |
{ |
394 |
case 2: // Sine Curve (high aggressiveness) |
case 2: /* Sine Curve (high aggressiveness) */ |
395 |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
396 |
sin(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff))); |
sin(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff))); |
397 |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
398 |
sin(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff)); |
sin(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff)); |
399 |
break; |
break; |
400 |
case 1: // Linear (medium aggressiveness) |
case 1: /* Linear (medium aggressiveness) */ |
401 |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
402 |
twopass->average_pframe / twopass->alt_curve_low_diff); |
twopass->average_pframe / twopass->alt_curve_low_diff); |
403 |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
404 |
twopass->average_pframe / twopass->alt_curve_low_diff; |
twopass->average_pframe / twopass->alt_curve_low_diff; |
405 |
break; |
break; |
406 |
case 0: // Cosine Curve (low aggressiveness) |
case 0: /* Cosine Curve (low aggressiveness) */ |
407 |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
408 |
(1.0 - cos(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff)))); |
(1.0 - cos(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff)))); |
409 |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
545 |
DEBUG2P(s); |
DEBUG2P(s); |
546 |
} |
} |
547 |
} |
} |
548 |
else // DLG_MODE_2PASS_2_INT |
else /* DLG_MODE_2PASS_2_INT */ |
549 |
{ |
{ |
550 |
while (1) |
while (1) |
551 |
{ |
{ |
598 |
|
|
599 |
if (twopass->nns1.quant & NNSTATS_KEYFRAME) |
if (twopass->nns1.quant & NNSTATS_KEYFRAME) |
600 |
{ |
{ |
601 |
// this test needs to be corrected.. |
/* this test needs to be corrected.. */ |
602 |
if (!(twopass->nns1.kblk + twopass->nns1.mblk)) |
if (!(twopass->nns1.kblk + twopass->nns1.mblk)) |
603 |
recminisize = twopass->nns1.bytes; |
recminisize = twopass->nns1.bytes; |
604 |
} |
} |
617 |
} |
} |
618 |
twopass->keyframe_locations[i_frames] = frames; |
twopass->keyframe_locations[i_frames] = frames; |
619 |
|
|
620 |
// compensate for avi frame overhead |
/* compensate for avi frame overhead */ |
621 |
desired -= frames * 24; |
desired -= frames * 24; |
622 |
|
|
623 |
switch (codec->config.credits_mode) |
switch (codec->config.credits_mode) |
624 |
{ |
{ |
625 |
case CREDITS_MODE_RATE : |
case CREDITS_MODE_RATE : |
626 |
|
|
627 |
// credits curve = (total / desired_size) * (100 / credits_rate) |
/* credits curve = (total / desired_size) * (100 / credits_rate) */ |
628 |
twopass->credits_start_curve = twopass->credits_end_curve = |
twopass->credits_start_curve = twopass->credits_end_curve = |
629 |
((double)(bframe_total + pframe_total + i_total + start + end) / desired) * |
((double)(bframe_total + pframe_total + i_total + start + end) / desired) * |
630 |
((double)100 / codec->config.credits_rate); |
((double)100 / codec->config.credits_rate); |
632 |
start_curved = (__int64)(start / twopass->credits_start_curve); |
start_curved = (__int64)(start / twopass->credits_start_curve); |
633 |
end_curved = (__int64)(end / twopass->credits_end_curve); |
end_curved = (__int64)(end / twopass->credits_end_curve); |
634 |
|
|
635 |
// movie curve = (total - credits) / (desired_size - curved credits) |
/* movie curve = (total - credits) / (desired_size - curved credits) */ |
636 |
twopass->movie_curve = (double) |
twopass->movie_curve = (double) |
637 |
(bframe_total + pframe_total + i_total) / |
(bframe_total + pframe_total + i_total) / |
638 |
(desired - start_curved - end_curved); |
(desired - start_curved - end_curved); |
641 |
|
|
642 |
case CREDITS_MODE_QUANT : |
case CREDITS_MODE_QUANT : |
643 |
|
|
644 |
// movie curve = (total - credits) / (desired_size - credits) |
/* movie curve = (total - credits) / (desired_size - credits) */ |
645 |
twopass->movie_curve = (double) |
twopass->movie_curve = (double) |
646 |
(bframe_total + pframe_total + i_total) / (desired - start - end); |
(bframe_total + pframe_total + i_total) / (desired - start - end); |
647 |
|
|
648 |
// aid the average asymmetric frame calculation below |
/* aid the average asymmetric frame calculation below */ |
649 |
start_curved = start; |
start_curved = start; |
650 |
end_curved = end; |
end_curved = end; |
651 |
|
|
653 |
|
|
654 |
case CREDITS_MODE_SIZE : |
case CREDITS_MODE_SIZE : |
655 |
|
|
656 |
// start curve = (start / start desired size) |
/* start curve = (start / start desired size) */ |
657 |
twopass->credits_start_curve = (double) |
twopass->credits_start_curve = (double) |
658 |
(start / 1024) / codec->config.credits_start_size; |
(start / 1024) / codec->config.credits_start_size; |
659 |
|
|
660 |
// end curve = (end / end desired size) |
/* end curve = (end / end desired size) */ |
661 |
twopass->credits_end_curve = (double) |
twopass->credits_end_curve = (double) |
662 |
(end / 1024) / codec->config.credits_end_size; |
(end / 1024) / codec->config.credits_end_size; |
663 |
|
|
664 |
start_curved = (__int64)(start / twopass->credits_start_curve); |
start_curved = (__int64)(start / twopass->credits_start_curve); |
665 |
end_curved = (__int64)(end / twopass->credits_end_curve); |
end_curved = (__int64)(end / twopass->credits_end_curve); |
666 |
|
|
667 |
// movie curve = (total - credits) / (desired_size - curved credits) |
/* movie curve = (total - credits) / (desired_size - curved credits) */ |
668 |
twopass->movie_curve = (double) |
twopass->movie_curve = (double) |
669 |
(bframe_total + pframe_total + i_total) / |
(bframe_total + pframe_total + i_total) / |
670 |
(desired - start_curved - end_curved); |
(desired - start_curved - end_curved); |
679 |
twopass->average_pframe = (double)pframe_total / pframes / twopass->movie_curve; |
twopass->average_pframe = (double)pframe_total / pframes / twopass->movie_curve; |
680 |
else |
else |
681 |
if (bframes) |
if (bframes) |
682 |
twopass->average_pframe = twopass->average_bframe; // b-frame packed bitstream fix |
twopass->average_pframe = twopass->average_bframe; /* b-frame packed bitstream fix */ |
683 |
else |
else |
684 |
{ |
{ |
685 |
DEBUGERR("ERROR: No p-frames or b-frames were present in the 1st pass. Rate control cannot function properly!"); |
DEBUGERR("ERROR: No p-frames or b-frames were present in the 1st pass. Rate control cannot function properly!"); |
688 |
|
|
689 |
|
|
690 |
|
|
691 |
// perform prepass to compensate for over/undersizing |
/* perform prepass to compensate for over/undersizing */ |
692 |
frames = 0; |
frames = 0; |
693 |
|
|
694 |
if (codec->config.use_alt_curve) |
if (codec->config.use_alt_curve) |
714 |
{ |
{ |
715 |
switch(codec->config.alt_curve_type) |
switch(codec->config.alt_curve_type) |
716 |
{ |
{ |
717 |
case 2: // Sine Curve (high aggressiveness) |
case 2: /* Sine Curve (high aggressiveness) */ |
718 |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
719 |
sin(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff))); |
sin(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff))); |
720 |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
721 |
sin(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff)); |
sin(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff)); |
722 |
break; |
break; |
723 |
case 1: // Linear (medium aggressiveness) |
case 1: /* Linear (medium aggressiveness) */ |
724 |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
725 |
twopass->average_pframe / twopass->alt_curve_low_diff); |
twopass->average_pframe / twopass->alt_curve_low_diff); |
726 |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
727 |
twopass->average_pframe / twopass->alt_curve_low_diff; |
twopass->average_pframe / twopass->alt_curve_low_diff; |
728 |
break; |
break; |
729 |
case 0: // Cosine Curve (low aggressiveness) |
case 0: /* Cosine Curve (low aggressiveness) */ |
730 |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
731 |
(1.0 - cos(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff)))); |
(1.0 - cos(DEG2RAD * (twopass->average_pframe * 90.0 / twopass->alt_curve_low_diff)))); |
732 |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
877 |
twopass->curve_comp_scale = ((total1 - total2) * (1.0 - (double)codec->config.alt_curve_bonus_bias / 100.0) + total2) / total2; |
twopass->curve_comp_scale = ((total1 - total2) * (1.0 - (double)codec->config.alt_curve_bonus_bias / 100.0) + total2) / total2; |
878 |
|
|
879 |
|
|
880 |
// special info for alt curve: bias bonus and quantizer thresholds, |
/* special info for alt curve: bias bonus and quantizer thresholds, */ |
881 |
{ |
{ |
882 |
double curve_temp, dbytes; |
double curve_temp, dbytes; |
883 |
char s[100]; |
char s[100]; |
1002 |
|
|
1003 |
if (twopass->nns_array_pos >= twopass->nns_array_length) |
if (twopass->nns_array_pos >= twopass->nns_array_length) |
1004 |
{ |
{ |
1005 |
// fix for VirtualDub 1.4.13 bframe handling |
/* fix for VirtualDub 1.4.13 bframe handling */ |
1006 |
if (codec->config.max_bframes > 0 && |
if (codec->config.max_bframes > 0 && |
1007 |
codec->framenum < twopass->nns_array_length + codec->config.max_bframes) |
codec->framenum < twopass->nns_array_length + codec->config.max_bframes) |
1008 |
{ |
{ |
1082 |
{ |
{ |
1083 |
bytes2 = (int)(bytes1 / twopass->credits_start_curve); |
bytes2 = (int)(bytes1 / twopass->credits_start_curve); |
1084 |
} |
} |
1085 |
else // CREDITS_END |
else /* CREDITS_END */ |
1086 |
{ |
{ |
1087 |
bytes2 = (int)(bytes1 / twopass->credits_end_curve); |
bytes2 = (int)(bytes1 / twopass->credits_end_curve); |
1088 |
} |
} |
1109 |
return ICERR_OK; |
return ICERR_OK; |
1110 |
} |
} |
1111 |
} |
} |
1112 |
else // DLG_MODE_2PASS_2_EXT |
else /* DLG_MODE_2PASS_2_EXT */ |
1113 |
{ |
{ |
1114 |
if (codec->config.credits_mode == CREDITS_MODE_QUANT) |
if (codec->config.credits_mode == CREDITS_MODE_QUANT) |
1115 |
{ |
{ |
1134 |
bytes2 = twopass->nns2.bytes; |
bytes2 = twopass->nns2.bytes; |
1135 |
} |
} |
1136 |
} |
} |
1137 |
else // Foxer: apply curve compression outside credits |
else /* Foxer: apply curve compression outside credits */ |
1138 |
{ |
{ |
1139 |
double dbytes, curve_temp; |
double dbytes, curve_temp; |
1140 |
|
|
1153 |
if (twopass->nns1.dd_v & NNSTATS_BFRAME) |
if (twopass->nns1.dd_v & NNSTATS_BFRAME) |
1154 |
dbytes *= twopass->average_pframe / twopass->average_bframe; |
dbytes *= twopass->average_pframe / twopass->average_bframe; |
1155 |
|
|
1156 |
// spread the compression error across payback_delay frames |
/* spread the compression error across payback_delay frames */ |
1157 |
if (codec->config.bitrate_payback_method == 0) |
if (codec->config.bitrate_payback_method == 0) |
1158 |
{ |
{ |
1159 |
bytes2 = (int)(curve_comp_error / codec->config.bitrate_payback_delay); |
bytes2 = (int)(curve_comp_error / codec->config.bitrate_payback_delay); |
1267 |
curve_comp_error += dbytes - ((int)dbytes); |
curve_comp_error += dbytes - ((int)dbytes); |
1268 |
} |
} |
1269 |
|
|
1270 |
// cap bytes2 to first pass size, lowers number of quant=1 frames |
/* cap bytes2 to first pass size, lowers number of quant=1 frames */ |
1271 |
if (bytes2 > bytes1) |
if (bytes2 > bytes1) |
1272 |
{ |
{ |
1273 |
curve_comp_error += bytes2 - bytes1; |
curve_comp_error += bytes2 - bytes1; |
1388 |
} |
} |
1389 |
else if (frame->type != XVID_TYPE_IVOP) |
else if (frame->type != XVID_TYPE_IVOP) |
1390 |
{ |
{ |
1391 |
// Foxer: aid desired quantizer precision by accumulating decision error |
/* Foxer: aid desired quantizer precision by accumulating decision error */ |
1392 |
if (twopass->nns1.dd_v & NNSTATS_BFRAME) |
if (twopass->nns1.dd_v & NNSTATS_BFRAME) |
1393 |
{ |
{ |
1394 |
bquant_error[frame->quant] += ((double)((twopass->nns1.quant & ~NNSTATS_KEYFRAME) * |
bquant_error[frame->quant] += ((double)((twopass->nns1.quant & ~NNSTATS_KEYFRAME) * |
1443 |
frame->quant = codec->config.min_pquant; |
frame->quant = codec->config.min_pquant; |
1444 |
} |
} |
1445 |
|
|
1446 |
// subsequent frame quants can only be +- 2 |
/* subsequent frame quants can only be +- 2 */ |
1447 |
if ((last_pquant || last_bquant) && capped_to_max_framesize == 0) |
if ((last_pquant || last_bquant) && capped_to_max_framesize == 0) |
1448 |
{ |
{ |
1449 |
if (twopass->nns1.dd_v & NNSTATS_BFRAME) |
if (twopass->nns1.dd_v & NNSTATS_BFRAME) |
1450 |
{ |
{ |
1451 |
// this bframe quantizer variation |
/* this bframe quantizer variation */ |
1452 |
// restriction needs to be redone. |
/* restriction needs to be redone. */ |
1453 |
if (frame->quant > last_bquant + 2) |
if (frame->quant > last_bquant + 2) |
1454 |
{ |
{ |
1455 |
frame->quant = last_bquant + 2; |
frame->quant = last_bquant + 2; |
1535 |
switch (codec->config.mode) |
switch (codec->config.mode) |
1536 |
{ |
{ |
1537 |
case DLG_MODE_2PASS_1 : |
case DLG_MODE_2PASS_1 : |
1538 |
nns1.bytes = stats->length; // total bytes |
nns1.bytes = stats->length; /* total bytes */ |
1539 |
nns1.dd_v = 0; |
nns1.dd_v = 0; |
1540 |
nns1.dd_u = nns1.dd_y = 0; |
nns1.dd_u = nns1.dd_y = 0; |
1541 |
nns1.dk_v = nns1.dk_u = nns1.dk_y = 0; |
nns1.dk_v = nns1.dk_u = nns1.dk_y = 0; |
1543 |
nns1.mk_u = nns1.mk_y = 0; |
nns1.mk_u = nns1.mk_y = 0; |
1544 |
|
|
1545 |
// nns1.quant = stats->quant; |
// nns1.quant = stats->quant; |
1546 |
nns1.quant = 2; // ugly fix for lumi masking in 1st pass returning new quant |
nns1.quant = 2; /* ugly fix for lumi masking in 1st pass returning new quant */ |
1547 |
nns1.lum_noise[0] = nns1.lum_noise[1] = 1; |
nns1.lum_noise[0] = nns1.lum_noise[1] = 1; |
1548 |
|
|
1549 |
switch(stats->type) |
switch(stats->type) |
1582 |
if (!credits_pos) |
if (!credits_pos) |
1583 |
{ |
{ |
1584 |
codec->twopass.quant_count[stats->quant]++; |
codec->twopass.quant_count[stats->quant]++; |
1585 |
//if ((codec->twopass.nns1.quant & NNSTATS_KEYFRAME)) |
/* if ((codec->twopass.nns1.quant & NNSTATS_KEYFRAME)) */ |
1586 |
if ((codec->twopass.nns1_array[codec->twopass.nns_update_pos].quant & NNSTATS_KEYFRAME)) |
if ((codec->twopass.nns1_array[codec->twopass.nns_update_pos].quant & NNSTATS_KEYFRAME)) |
1587 |
{ |
{ |
1588 |
// calculate how much to distribute per frame in |
/* calculate how much to distribute per frame in */ |
1589 |
// order to make up for this keyframe's overflow |
/* order to make up for this keyframe's overflow */ |
1590 |
|
|
1591 |
codec->twopass.overflow += codec->twopass.KFoverflow; |
codec->twopass.overflow += codec->twopass.KFoverflow; |
1592 |
//codec->twopass.KFoverflow = codec->twopass.desired_bytes2 - frame->length; |
/* codec->twopass.KFoverflow = codec->twopass.desired_bytes2 - frame->length; */ |
1593 |
// XXX: very ugly hack for bframs; this affects twopass quality |
/* XXX: very ugly hack for bframs; this affects twopass quality */ |
1594 |
codec->twopass.KFoverflow = codec->twopass.nns2_array[codec->twopass.nns_update_pos].bytes/*codec->twopass.desired_bytes2*/ - stats->length; |
codec->twopass.KFoverflow = codec->twopass.nns2_array[codec->twopass.nns_update_pos].bytes/*codec->twopass.desired_bytes2*/ - stats->length; |
1595 |
|
|
1596 |
tempdiv = (codec->twopass.keyframe_locations[codec->twopass.KF_idx] - |
tempdiv = (codec->twopass.keyframe_locations[codec->twopass.KF_idx] - |
1598 |
|
|
1599 |
if (tempdiv > 1) |
if (tempdiv > 1) |
1600 |
{ |
{ |
1601 |
// non-consecutive keyframes |
/* non-consecutive keyframes */ |
1602 |
codec->twopass.KFoverflow_partial = codec->twopass.KFoverflow / (tempdiv - 1); |
codec->twopass.KFoverflow_partial = codec->twopass.KFoverflow / (tempdiv - 1); |
1603 |
} |
} |
1604 |
else |
else |
1605 |
{ |
{ |
1606 |
// consecutive keyframes |
/* consecutive keyframes */ |
1607 |
codec->twopass.overflow += codec->twopass.KFoverflow; |
codec->twopass.overflow += codec->twopass.KFoverflow; |
1608 |
codec->twopass.KFoverflow = 0; |
codec->twopass.KFoverflow = 0; |
1609 |
codec->twopass.KFoverflow_partial = 0; |
codec->twopass.KFoverflow_partial = 0; |
1612 |
} |
} |
1613 |
else |
else |
1614 |
{ |
{ |
1615 |
// distribute part of the keyframe overflow |
/* distribute part of the keyframe overflow */ |
1616 |
|
|
1617 |
//codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length + |
/* codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length + */ |
1618 |
// codec->twopass.KFoverflow_partial; |
/* codec->twopass.KFoverflow_partial; */ |
1619 |
codec->twopass.overflow += codec->twopass.nns2_array[codec->twopass.nns_update_pos].bytes/*codec->twopass.desired_bytes2*/ - stats->length + |
codec->twopass.overflow += codec->twopass.nns2_array[codec->twopass.nns_update_pos].bytes/*codec->twopass.desired_bytes2*/ - stats->length + |
1620 |
codec->twopass.KFoverflow_partial; |
codec->twopass.KFoverflow_partial; |
1621 |
codec->twopass.KFoverflow -= codec->twopass.KFoverflow_partial; |
codec->twopass.KFoverflow -= codec->twopass.KFoverflow_partial; |
1623 |
} |
} |
1624 |
else |
else |
1625 |
{ |
{ |
1626 |
//codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length; |
/* codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length; */ |
1627 |
codec->twopass.overflow += codec->twopass.nns2_array[codec->twopass.nns_update_pos].bytes/*codec->twopass.desired_bytes2*/ - stats->length; |
codec->twopass.overflow += codec->twopass.nns2_array[codec->twopass.nns_update_pos].bytes/*codec->twopass.desired_bytes2*/ - stats->length; |
1628 |
|
|
1629 |
// ugly fix for credits.. |
/* ugly fix for credits.. */ |
1630 |
codec->twopass.overflow += codec->twopass.KFoverflow; |
codec->twopass.overflow += codec->twopass.KFoverflow; |
1631 |
codec->twopass.KFoverflow = 0; |
codec->twopass.KFoverflow = 0; |
1632 |
codec->twopass.KFoverflow_partial = 0; |
codec->twopass.KFoverflow_partial = 0; |
1633 |
// end of ugly fix. |
/* end of ugly fix. */ |
1634 |
} |
} |
1635 |
|
|
1636 |
/* XXX: remove |
/* XXX: remove |
1664 |
} |
} |
1665 |
*/ |
*/ |
1666 |
|
|
1667 |
//DEBUG2ND(frame->quant, quant_type, frame_type, codec->twopass.bytes1, codec->twopass.desired_bytes2, frame->length, codec->twopass.overflow, credits_pos) |
/* DEBUG2ND(frame->quant, quant_type, frame_type, codec->twopass.bytes1, codec->twopass.desired_bytes2, frame->length, codec->twopass.overflow, credits_pos) */ |
1668 |
wsprintf(tmp, "[%i] 2nd-pass: quant:%d %s %s stats1:%d scaled:%d actual:%d overflow:%d %s", |
wsprintf(tmp, "[%i] 2nd-pass: quant:%d %s %s stats1:%d scaled:%d actual:%d overflow:%d %s", |
1669 |
codec->twopass.nns_update_pos, |
codec->twopass.nns_update_pos, |
1670 |
stats->quant, |
stats->quant, |
1708 |
|
|
1709 |
if (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT) |
if (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT) |
1710 |
{ |
{ |
1711 |
// output the quantizer distribution for this encode. |
/* output the quantizer distribution for this encode. */ |
1712 |
|
|
1713 |
OutputDebugString("Quantizer distribution for 2nd pass:"); |
OutputDebugString("Quantizer distribution for 2nd pass:"); |
1714 |
for (i=1; i<=31; i++) |
for (i=1; i<=31; i++) |