23 |
* |
* |
24 |
* History: |
* History: |
25 |
* |
* |
26 |
|
* 23.03.2002 daniel smith <danielsmith@astroboymail.com> |
27 |
|
* changed inter4v to only be in modes 5 or 6 |
28 |
|
* fixed null mode crash ? |
29 |
|
* merged foxer's alternative 2-pass code |
30 |
|
* added DEBUGERR output on errors instead of returning |
31 |
* 16.03.2002 daniel smith <danielsmith@astroboymail.com> |
* 16.03.2002 daniel smith <danielsmith@astroboymail.com> |
32 |
* changed BITMAPV4HEADER to BITMAPINFOHEADER |
* changed BITMAPV4HEADER to BITMAPINFOHEADER |
33 |
* - prevents memcpy crash in compress_get_format() |
* - prevents memcpy crash in compress_get_format() |
61 |
{ |
{ |
62 |
if (hdr->biHeight < 0) |
if (hdr->biHeight < 0) |
63 |
{ |
{ |
64 |
DEBUG("colorspace: inverted input format not supported"); |
DEBUGERR("colorspace: inverted input format not supported"); |
65 |
return XVID_CSP_NULL; |
return XVID_CSP_NULL; |
66 |
} |
} |
67 |
|
|
233 |
switch (codec->config.mode) |
switch (codec->config.mode) |
234 |
{ |
{ |
235 |
case DLG_MODE_CBR : |
case DLG_MODE_CBR : |
|
DEBUG1("bitrate: ", codec->config.bitrate); |
|
236 |
param.bitrate = codec->config.bitrate; |
param.bitrate = codec->config.bitrate; |
237 |
param.rc_buffersize = codec->config.rc_buffersize; |
param.rc_buffersize = codec->config.rc_buffersize; |
238 |
break; |
break; |
253 |
param.bitrate = 0; |
param.bitrate = 0; |
254 |
break; |
break; |
255 |
|
|
256 |
|
case DLG_MODE_NULL : |
257 |
|
return ICERR_OK; |
258 |
|
|
259 |
default : |
default : |
260 |
break; |
break; |
261 |
} |
} |
346 |
|
|
347 |
frame.general |= XVID_HALFPEL; |
frame.general |= XVID_HALFPEL; |
348 |
|
|
349 |
if(codec->config.motion_search > 3) |
if(codec->config.motion_search > 4) |
350 |
frame.general |= XVID_INTER4V; |
frame.general |= XVID_INTER4V; |
351 |
|
|
352 |
if(((codec->config.mode == DLG_MODE_2PASS_1) ? 0 : codec->config.lum_masking) == 1) |
if(((codec->config.mode == DLG_MODE_2PASS_1) ? 0 : codec->config.lum_masking) == 1) |
397 |
return ICERR_OK; |
return ICERR_OK; |
398 |
|
|
399 |
default : |
default : |
400 |
DEBUG("Invalid encoding mode"); |
DEBUGERR("Invalid encoding mode"); |
401 |
return ICERR_ERROR; |
return ICERR_ERROR; |
402 |
} |
} |
403 |
|
|
638 |
twopass->stats1 = CreateFile(codec->config.stats1, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); |
twopass->stats1 = CreateFile(codec->config.stats1, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); |
639 |
if (twopass->stats1 == INVALID_HANDLE_VALUE) |
if (twopass->stats1 == INVALID_HANDLE_VALUE) |
640 |
{ |
{ |
641 |
DEBUG("2pass init error - couldn't create stats1"); |
DEBUGERR("2pass init error - couldn't create stats1"); |
642 |
return ICERR_ERROR; |
return ICERR_ERROR; |
643 |
} |
} |
644 |
if (WriteFile(twopass->stats1, &version, sizeof(DWORD), &wrote, 0) == 0 || wrote != sizeof(DWORD)) |
if (WriteFile(twopass->stats1, &version, sizeof(DWORD), &wrote, 0) == 0 || wrote != sizeof(DWORD)) |
645 |
{ |
{ |
646 |
CloseHandle(twopass->stats1); |
CloseHandle(twopass->stats1); |
647 |
twopass->stats1 = INVALID_HANDLE_VALUE; |
twopass->stats1 = INVALID_HANDLE_VALUE; |
648 |
DEBUG("2pass init error - couldn't write to stats1"); |
DEBUGERR("2pass init error - couldn't write to stats1"); |
649 |
return ICERR_ERROR; |
return ICERR_ERROR; |
650 |
} |
} |
651 |
break; |
break; |
655 |
twopass->stats1 = CreateFile(codec->config.stats1, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
twopass->stats1 = CreateFile(codec->config.stats1, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
656 |
if (twopass->stats1 == INVALID_HANDLE_VALUE) |
if (twopass->stats1 == INVALID_HANDLE_VALUE) |
657 |
{ |
{ |
658 |
DEBUG("2pass init error - couldn't open stats1"); |
DEBUGERR("2pass init error - couldn't open stats1"); |
659 |
return ICERR_ERROR; |
return ICERR_ERROR; |
660 |
} |
} |
661 |
if (ReadFile(twopass->stats1, &version, sizeof(DWORD), &read, 0) == 0 || read != sizeof(DWORD)) |
if (ReadFile(twopass->stats1, &version, sizeof(DWORD), &read, 0) == 0 || read != sizeof(DWORD)) |
662 |
{ |
{ |
663 |
CloseHandle(twopass->stats1); |
CloseHandle(twopass->stats1); |
664 |
twopass->stats1 = INVALID_HANDLE_VALUE; |
twopass->stats1 = INVALID_HANDLE_VALUE; |
665 |
DEBUG("2pass init error - couldn't read from stats1"); |
DEBUGERR("2pass init error - couldn't read from stats1"); |
666 |
return ICERR_ERROR; |
return ICERR_ERROR; |
667 |
} |
} |
668 |
if (version != -20) |
if (version != -20) |
669 |
{ |
{ |
670 |
CloseHandle(twopass->stats1); |
CloseHandle(twopass->stats1); |
671 |
twopass->stats1 = INVALID_HANDLE_VALUE; |
twopass->stats1 = INVALID_HANDLE_VALUE; |
672 |
DEBUG("2pass init error - wrong .stats version"); |
DEBUGERR("2pass init error - wrong .stats version"); |
673 |
return ICERR_ERROR; |
return ICERR_ERROR; |
674 |
} |
} |
675 |
|
|
686 |
{ |
{ |
687 |
CloseHandle(twopass->stats1); |
CloseHandle(twopass->stats1); |
688 |
twopass->stats1 = INVALID_HANDLE_VALUE; |
twopass->stats1 = INVALID_HANDLE_VALUE; |
689 |
DEBUG("2pass init error - couldn't open stats2"); |
DEBUGERR("2pass init error - couldn't open stats2"); |
690 |
return ICERR_ERROR; |
return ICERR_ERROR; |
691 |
} |
} |
692 |
if (ReadFile(twopass->stats2, &version, sizeof(DWORD), &read, 0) == 0 || read != sizeof(DWORD)) |
if (ReadFile(twopass->stats2, &version, sizeof(DWORD), &read, 0) == 0 || read != sizeof(DWORD)) |
695 |
twopass->stats1 = INVALID_HANDLE_VALUE; |
twopass->stats1 = INVALID_HANDLE_VALUE; |
696 |
CloseHandle(twopass->stats2); |
CloseHandle(twopass->stats2); |
697 |
twopass->stats2 = INVALID_HANDLE_VALUE; |
twopass->stats2 = INVALID_HANDLE_VALUE; |
698 |
DEBUG("2pass init error - couldn't read from stats2"); |
DEBUGERR("2pass init error - couldn't read from stats2"); |
699 |
return ICERR_ERROR; |
return ICERR_ERROR; |
700 |
} |
} |
701 |
if (version != -20) |
if (version != -20) |
704 |
twopass->stats1 = INVALID_HANDLE_VALUE; |
twopass->stats1 = INVALID_HANDLE_VALUE; |
705 |
CloseHandle(twopass->stats2); |
CloseHandle(twopass->stats2); |
706 |
twopass->stats2 = INVALID_HANDLE_VALUE; |
twopass->stats2 = INVALID_HANDLE_VALUE; |
707 |
DEBUG("2pass init error - wrong .stats version"); |
DEBUGERR("2pass init error - wrong .stats version"); |
708 |
return ICERR_ERROR; |
return ICERR_ERROR; |
709 |
} |
} |
710 |
|
|
725 |
CloseHandle(twopass->stats2); |
CloseHandle(twopass->stats2); |
726 |
twopass->stats1 = INVALID_HANDLE_VALUE; |
twopass->stats1 = INVALID_HANDLE_VALUE; |
727 |
twopass->stats2 = INVALID_HANDLE_VALUE; |
twopass->stats2 = INVALID_HANDLE_VALUE; |
728 |
DEBUG("2pass init error - incomplete stats1/stats2 record?"); |
DEBUGERR("2pass init error - incomplete stats1/stats2 record?"); |
729 |
return ICERR_ERROR; |
return ICERR_ERROR; |
730 |
} |
} |
731 |
} |
} |
756 |
// perform prepass to compensate for over/undersizing |
// perform prepass to compensate for over/undersizing |
757 |
frames = 0; |
frames = 0; |
758 |
|
|
759 |
|
if (codec->config.use_alt_curve) |
760 |
|
{ |
761 |
|
twopass->alt_curve_low = twopass->average_frame - twopass->average_frame * (double)codec->config.alt_curve_low_dist / 100.0; |
762 |
|
twopass->alt_curve_low_diff = twopass->average_frame - twopass->alt_curve_low; |
763 |
|
twopass->alt_curve_high = twopass->average_frame + twopass->average_frame * (double)codec->config.alt_curve_high_dist / 100.0; |
764 |
|
twopass->alt_curve_high_diff = twopass->alt_curve_high - twopass->average_frame; |
765 |
|
if (codec->config.alt_curve_use_auto) |
766 |
|
{ |
767 |
|
if (twopass->movie_curve > 1.0f) |
768 |
|
{ |
769 |
|
codec->config.alt_curve_min_rel_qual = (int)(100.0 - (100.0 - 100.0 / twopass->movie_curve) * (double)codec->config.alt_curve_auto_str / 100.0); |
770 |
|
if (codec->config.alt_curve_min_rel_qual < 20) |
771 |
|
codec->config.alt_curve_min_rel_qual = 20; |
772 |
|
} |
773 |
|
else |
774 |
|
codec->config.alt_curve_min_rel_qual = 100; |
775 |
|
} |
776 |
|
twopass->alt_curve_mid_qual = (1.0 + (double)codec->config.alt_curve_min_rel_qual / 100.0) / 2.0; |
777 |
|
twopass->alt_curve_qual_dev = 1.0 - twopass->alt_curve_mid_qual; |
778 |
|
if (codec->config.alt_curve_low_dist > 100) |
779 |
|
{ |
780 |
|
switch(codec->config.alt_curve_type) |
781 |
|
{ |
782 |
|
case 2: // Sine Curve (high aggressiveness) |
783 |
|
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
784 |
|
sin(DEG2RAD * (twopass->average_frame * 90.0 / twopass->alt_curve_low_diff))); |
785 |
|
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
786 |
|
sin(DEG2RAD * (twopass->average_frame * 90.0 / twopass->alt_curve_low_diff)); |
787 |
|
break; |
788 |
|
case 1: // Linear (medium aggressiveness) |
789 |
|
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
790 |
|
twopass->average_frame / twopass->alt_curve_low_diff); |
791 |
|
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
792 |
|
twopass->average_frame / twopass->alt_curve_low_diff; |
793 |
|
break; |
794 |
|
case 0: // Cosine Curve (low aggressiveness) |
795 |
|
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
796 |
|
(1.0 - cos(DEG2RAD * (twopass->average_frame * 90.0 / twopass->alt_curve_low_diff)))); |
797 |
|
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
798 |
|
(1.0 - cos(DEG2RAD * (twopass->average_frame * 90.0 / twopass->alt_curve_low_diff))); |
799 |
|
} |
800 |
|
} |
801 |
|
} |
802 |
|
|
803 |
while (1) |
while (1) |
804 |
{ |
{ |
805 |
if (!ReadFile(twopass->stats1, &twopass->nns1, sizeof(NNSTATS), &read, NULL) || read != sizeof(NNSTATS) || |
if (!ReadFile(twopass->stats1, &twopass->nns1, sizeof(NNSTATS), &read, NULL) || read != sizeof(NNSTATS) || |
817 |
CloseHandle(twopass->stats2); |
CloseHandle(twopass->stats2); |
818 |
twopass->stats1 = INVALID_HANDLE_VALUE; |
twopass->stats1 = INVALID_HANDLE_VALUE; |
819 |
twopass->stats2 = INVALID_HANDLE_VALUE; |
twopass->stats2 = INVALID_HANDLE_VALUE; |
820 |
DEBUG("2pass init error - incomplete stats1/stats2 record?"); |
DEBUGERR("2pass init error - incomplete stats1/stats2 record?"); |
821 |
return ICERR_ERROR; |
return ICERR_ERROR; |
822 |
} |
} |
823 |
} |
} |
828 |
double dbytes = twopass->nns2.bytes / twopass->movie_curve; |
double dbytes = twopass->nns2.bytes / twopass->movie_curve; |
829 |
total1 += dbytes; |
total1 += dbytes; |
830 |
|
|
831 |
|
if (codec->config.use_alt_curve) |
832 |
|
{ |
833 |
|
if (dbytes > twopass->average_frame) |
834 |
|
{ |
835 |
|
if (dbytes >= twopass->alt_curve_high) |
836 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev); |
837 |
|
else |
838 |
|
{ |
839 |
|
switch(codec->config.alt_curve_type) |
840 |
|
{ |
841 |
|
case 2: |
842 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
843 |
|
sin(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_high_diff))); |
844 |
|
break; |
845 |
|
case 1: |
846 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
847 |
|
(dbytes - twopass->average_frame) / twopass->alt_curve_high_diff); |
848 |
|
break; |
849 |
|
case 0: |
850 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
851 |
|
(1.0 - cos(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_high_diff)))); |
852 |
|
} |
853 |
|
} |
854 |
|
} |
855 |
|
else |
856 |
|
{ |
857 |
|
if (dbytes <= twopass->alt_curve_low) |
858 |
|
total2 += dbytes; |
859 |
|
else |
860 |
|
{ |
861 |
|
switch(codec->config.alt_curve_type) |
862 |
|
{ |
863 |
|
case 2: |
864 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
865 |
|
sin(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_low_diff))); |
866 |
|
break; |
867 |
|
case 1: |
868 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
869 |
|
(dbytes - twopass->average_frame) / twopass->alt_curve_low_diff); |
870 |
|
break; |
871 |
|
case 0: |
872 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual + twopass->alt_curve_qual_dev * |
873 |
|
(1.0 - cos(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_low_diff)))); |
874 |
|
} |
875 |
|
} |
876 |
|
} |
877 |
|
} |
878 |
|
else |
879 |
|
{ |
880 |
if (dbytes > twopass->average_frame) |
if (dbytes > twopass->average_frame) |
881 |
{ |
{ |
882 |
total2 += ((double)dbytes + (twopass->average_frame - dbytes) * |
total2 += ((double)dbytes + (twopass->average_frame - dbytes) * |
888 |
codec->config.curve_compression_low / 100.0); |
codec->config.curve_compression_low / 100.0); |
889 |
} |
} |
890 |
} |
} |
891 |
|
} |
892 |
|
|
893 |
++frames; |
++frames; |
894 |
} |
} |
895 |
|
|
896 |
twopass->curve_comp_scale = total1 / total2; |
twopass->curve_comp_scale = total1 / total2; |
897 |
|
|
898 |
|
if (!codec->config.use_alt_curve) |
899 |
{ |
{ |
900 |
int asymmetric_average_frame; |
int asymmetric_average_frame; |
901 |
char s[100]; |
char s[100]; |
924 |
{ |
{ |
925 |
CloseHandle(twopass->stats1); |
CloseHandle(twopass->stats1); |
926 |
twopass->stats1 = INVALID_HANDLE_VALUE; |
twopass->stats1 = INVALID_HANDLE_VALUE; |
927 |
DEBUG("2pass init error - incomplete stats2 record?"); |
DEBUGERR("2pass init error - incomplete stats2 record?"); |
928 |
return ICERR_ERROR; |
return ICERR_ERROR; |
929 |
} |
} |
930 |
} |
} |
1016 |
// perform prepass to compensate for over/undersizing |
// perform prepass to compensate for over/undersizing |
1017 |
frames = 0; |
frames = 0; |
1018 |
|
|
1019 |
|
if (codec->config.use_alt_curve) |
1020 |
|
{ |
1021 |
|
twopass->alt_curve_low = twopass->average_frame - twopass->average_frame * (double)codec->config.alt_curve_low_dist / 100.0; |
1022 |
|
twopass->alt_curve_low_diff = twopass->average_frame - twopass->alt_curve_low; |
1023 |
|
twopass->alt_curve_high = twopass->average_frame + twopass->average_frame * (double)codec->config.alt_curve_high_dist / 100.0; |
1024 |
|
twopass->alt_curve_high_diff = twopass->alt_curve_high - twopass->average_frame; |
1025 |
|
if (codec->config.alt_curve_use_auto) |
1026 |
|
{ |
1027 |
|
if (twopass->movie_curve > 1.0f) |
1028 |
|
{ |
1029 |
|
codec->config.alt_curve_min_rel_qual = (int)(100.0 - (100.0 - 100.0 / twopass->movie_curve) * (double)codec->config.alt_curve_auto_str / 100.0); |
1030 |
|
if (codec->config.alt_curve_min_rel_qual < 20) |
1031 |
|
codec->config.alt_curve_min_rel_qual = 20; |
1032 |
|
} |
1033 |
|
else |
1034 |
|
codec->config.alt_curve_min_rel_qual = 100; |
1035 |
|
} |
1036 |
|
twopass->alt_curve_mid_qual = (1.0 + (double)codec->config.alt_curve_min_rel_qual / 100.0) / 2.0; |
1037 |
|
twopass->alt_curve_qual_dev = 1.0 - twopass->alt_curve_mid_qual; |
1038 |
|
if (codec->config.alt_curve_low_dist > 100) |
1039 |
|
{ |
1040 |
|
switch(codec->config.alt_curve_type) |
1041 |
|
{ |
1042 |
|
case 2: // Sine Curve (high aggressiveness) |
1043 |
|
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
1044 |
|
sin(DEG2RAD * (twopass->average_frame * 90.0 / twopass->alt_curve_low_diff))); |
1045 |
|
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
1046 |
|
sin(DEG2RAD * (twopass->average_frame * 90.0 / twopass->alt_curve_low_diff)); |
1047 |
|
break; |
1048 |
|
case 1: // Linear (medium aggressiveness) |
1049 |
|
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
1050 |
|
twopass->average_frame / twopass->alt_curve_low_diff); |
1051 |
|
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
1052 |
|
twopass->average_frame / twopass->alt_curve_low_diff; |
1053 |
|
break; |
1054 |
|
case 0: // Cosine Curve (low aggressiveness) |
1055 |
|
twopass->alt_curve_qual_dev *= 2.0 / (1.0 + |
1056 |
|
(1.0 - cos(DEG2RAD * (twopass->average_frame * 90.0 / twopass->alt_curve_low_diff)))); |
1057 |
|
twopass->alt_curve_mid_qual = 1.0 - twopass->alt_curve_qual_dev * |
1058 |
|
(1.0 - cos(DEG2RAD * (twopass->average_frame * 90.0 / twopass->alt_curve_low_diff))); |
1059 |
|
} |
1060 |
|
} |
1061 |
|
} |
1062 |
|
|
1063 |
while (1) |
while (1) |
1064 |
{ |
{ |
1065 |
if (!ReadFile(twopass->stats1, &twopass->nns1, sizeof(NNSTATS), &read, NULL) || read != sizeof(NNSTATS)) |
if (!ReadFile(twopass->stats1, &twopass->nns1, sizeof(NNSTATS), &read, NULL) || read != sizeof(NNSTATS)) |
1074 |
{ |
{ |
1075 |
CloseHandle(twopass->stats1); |
CloseHandle(twopass->stats1); |
1076 |
twopass->stats1 = INVALID_HANDLE_VALUE; |
twopass->stats1 = INVALID_HANDLE_VALUE; |
1077 |
DEBUG("2pass init error - incomplete stats2 record?"); |
DEBUGERR("2pass init error - incomplete stats2 record?"); |
1078 |
return ICERR_ERROR; |
return ICERR_ERROR; |
1079 |
} |
} |
1080 |
} |
} |
1085 |
double dbytes = twopass->nns1.bytes / twopass->movie_curve; |
double dbytes = twopass->nns1.bytes / twopass->movie_curve; |
1086 |
total1 += dbytes; |
total1 += dbytes; |
1087 |
|
|
1088 |
|
if (codec->config.use_alt_curve) |
1089 |
|
{ |
1090 |
|
if (dbytes > twopass->average_frame) |
1091 |
|
{ |
1092 |
|
if (dbytes >= twopass->alt_curve_high) |
1093 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev); |
1094 |
|
else |
1095 |
|
{ |
1096 |
|
switch(codec->config.alt_curve_type) |
1097 |
|
{ |
1098 |
|
case 2: |
1099 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
1100 |
|
sin(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_high_diff))); |
1101 |
|
break; |
1102 |
|
case 1: |
1103 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
1104 |
|
(dbytes - twopass->average_frame) / twopass->alt_curve_high_diff); |
1105 |
|
break; |
1106 |
|
case 0: |
1107 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
1108 |
|
(1.0 - cos(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_high_diff)))); |
1109 |
|
} |
1110 |
|
} |
1111 |
|
} |
1112 |
|
else |
1113 |
|
{ |
1114 |
|
if (dbytes <= twopass->alt_curve_low) |
1115 |
|
total2 += dbytes; |
1116 |
|
else |
1117 |
|
{ |
1118 |
|
switch(codec->config.alt_curve_type) |
1119 |
|
{ |
1120 |
|
case 2: |
1121 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
1122 |
|
sin(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_low_diff))); |
1123 |
|
break; |
1124 |
|
case 1: |
1125 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
1126 |
|
(dbytes - twopass->average_frame) / twopass->alt_curve_low_diff); |
1127 |
|
break; |
1128 |
|
case 0: |
1129 |
|
total2 += dbytes * (twopass->alt_curve_mid_qual + twopass->alt_curve_qual_dev * |
1130 |
|
(1.0 - cos(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_low_diff)))); |
1131 |
|
} |
1132 |
|
} |
1133 |
|
} |
1134 |
|
} |
1135 |
|
else |
1136 |
|
{ |
1137 |
if (dbytes > twopass->average_frame) |
if (dbytes > twopass->average_frame) |
1138 |
{ |
{ |
1139 |
total2 += ((double)dbytes + (twopass->average_frame - dbytes) * |
total2 += ((double)dbytes + (twopass->average_frame - dbytes) * |
1145 |
codec->config.curve_compression_low / 100.0); |
codec->config.curve_compression_low / 100.0); |
1146 |
} |
} |
1147 |
} |
} |
1148 |
|
} |
1149 |
|
|
1150 |
++frames; |
++frames; |
1151 |
} |
} |
1152 |
|
|
1153 |
twopass->curve_comp_scale = total1 / total2; |
twopass->curve_comp_scale = total1 / total2; |
1154 |
|
|
1155 |
|
if (!codec->config.use_alt_curve) |
1156 |
{ |
{ |
1157 |
int asymmetric_average_frame; |
int asymmetric_average_frame; |
1158 |
char s[100]; |
char s[100]; |
1165 |
SetFilePointer(twopass->stats1, sizeof(DWORD), 0, FILE_BEGIN); |
SetFilePointer(twopass->stats1, sizeof(DWORD), 0, FILE_BEGIN); |
1166 |
} |
} |
1167 |
|
|
1168 |
|
if (codec->config.use_alt_curve) |
1169 |
|
{ |
1170 |
|
if (codec->config.alt_curve_use_auto_bonus_bias) |
1171 |
|
codec->config.alt_curve_bonus_bias = codec->config.alt_curve_min_rel_qual; |
1172 |
|
|
1173 |
|
twopass->curve_bias_bonus = (total1 - total2) * (double)codec->config.alt_curve_bonus_bias / 100.0 / (double)(frames - credits_frames - i_frames); |
1174 |
|
twopass->curve_comp_scale = ((total1 - total2) * (1.0 - (double)codec->config.alt_curve_bonus_bias / 100.0) + total2) / total2; |
1175 |
|
} |
1176 |
|
|
1177 |
twopass->overflow = 0; |
twopass->overflow = 0; |
1178 |
|
|
1179 |
break; |
break; |
1201 |
break; |
break; |
1202 |
|
|
1203 |
default : |
default : |
1204 |
DEBUG("Can't use credits size mode in quality mode"); |
DEBUGERR("Can't use credits size mode in quality mode"); |
1205 |
return ICERR_ERROR; |
return ICERR_ERROR; |
1206 |
} |
} |
1207 |
} |
} |
1227 |
break; |
break; |
1228 |
|
|
1229 |
default : |
default : |
1230 |
DEBUG("Can't use credits size mode in quantizer mode"); |
DEBUGERR("Can't use credits size mode in quantizer mode"); |
1231 |
return ICERR_ERROR; |
return ICERR_ERROR; |
1232 |
} |
} |
1233 |
} |
} |
1256 |
return ICERR_OK; |
return ICERR_OK; |
1257 |
|
|
1258 |
default: |
default: |
1259 |
DEBUG("get quant: invalid mode"); |
DEBUGERR("get quant: invalid mode"); |
1260 |
return ICERR_ERROR; |
return ICERR_ERROR; |
1261 |
} |
} |
1262 |
} |
} |
1290 |
|
|
1291 |
if (ReadFile(twopass->stats1, &twopass->nns1, sizeof(NNSTATS), &read, 0) == 0 || read != sizeof(NNSTATS)) |
if (ReadFile(twopass->stats1, &twopass->nns1, sizeof(NNSTATS), &read, 0) == 0 || read != sizeof(NNSTATS)) |
1292 |
{ |
{ |
1293 |
DEBUG("2ndpass quant: couldn't read from stats1"); |
DEBUGERR("2ndpass quant: couldn't read from stats1"); |
1294 |
return ICERR_ERROR; |
return ICERR_ERROR; |
1295 |
} |
} |
1296 |
if (codec->config.mode == DLG_MODE_2PASS_2_EXT) |
if (codec->config.mode == DLG_MODE_2PASS_2_EXT) |
1297 |
{ |
{ |
1298 |
if (ReadFile(twopass->stats2, &twopass->nns2, sizeof(NNSTATS), &read, 0) == 0 || read != sizeof(NNSTATS)) |
if (ReadFile(twopass->stats2, &twopass->nns2, sizeof(NNSTATS), &read, 0) == 0 || read != sizeof(NNSTATS)) |
1299 |
{ |
{ |
1300 |
DEBUG("2ndpass quant: couldn't read from stats2"); |
DEBUGERR("2ndpass quant: couldn't read from stats2"); |
1301 |
return ICERR_ERROR; |
return ICERR_ERROR; |
1302 |
} |
} |
1303 |
} |
} |
1393 |
|
|
1394 |
curve_comp_error -= bytes2; |
curve_comp_error -= bytes2; |
1395 |
|
|
1396 |
if ((codec->config.curve_compression_high + codec->config.curve_compression_low) && |
if (codec->config.use_alt_curve) |
1397 |
|
{ |
1398 |
|
if (!frame->intra) |
1399 |
|
{ |
1400 |
|
if (dbytes > twopass->average_frame) |
1401 |
|
{ |
1402 |
|
if (dbytes >= twopass->alt_curve_high) |
1403 |
|
curve_temp = dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev); |
1404 |
|
else |
1405 |
|
{ |
1406 |
|
switch(codec->config.alt_curve_type) |
1407 |
|
{ |
1408 |
|
case 2: |
1409 |
|
curve_temp = dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
1410 |
|
sin(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_high_diff))); |
1411 |
|
break; |
1412 |
|
case 1: |
1413 |
|
curve_temp = dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
1414 |
|
(dbytes - twopass->average_frame) / twopass->alt_curve_high_diff); |
1415 |
|
break; |
1416 |
|
case 0: |
1417 |
|
curve_temp = dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
1418 |
|
(1.0 - cos(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_high_diff)))); |
1419 |
|
} |
1420 |
|
} |
1421 |
|
} |
1422 |
|
else |
1423 |
|
{ |
1424 |
|
if (dbytes <= twopass->alt_curve_low) |
1425 |
|
curve_temp = dbytes; |
1426 |
|
else |
1427 |
|
{ |
1428 |
|
switch(codec->config.alt_curve_type) |
1429 |
|
{ |
1430 |
|
case 2: |
1431 |
|
curve_temp = dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
1432 |
|
sin(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_low_diff))); |
1433 |
|
break; |
1434 |
|
case 1: |
1435 |
|
curve_temp = dbytes * (twopass->alt_curve_mid_qual - twopass->alt_curve_qual_dev * |
1436 |
|
(dbytes - twopass->average_frame) / twopass->alt_curve_low_diff); |
1437 |
|
break; |
1438 |
|
case 0: |
1439 |
|
curve_temp = dbytes * (twopass->alt_curve_mid_qual + twopass->alt_curve_qual_dev * |
1440 |
|
(1.0 - cos(DEG2RAD * ((dbytes - twopass->average_frame) * 90.0 / twopass->alt_curve_low_diff)))); |
1441 |
|
} |
1442 |
|
} |
1443 |
|
} |
1444 |
|
curve_temp = curve_temp * twopass->curve_comp_scale + twopass->curve_bias_bonus; |
1445 |
|
|
1446 |
|
bytes2 += ((int)curve_temp); |
1447 |
|
curve_comp_error += curve_temp - ((int)curve_temp); |
1448 |
|
} |
1449 |
|
else |
1450 |
|
{ |
1451 |
|
curve_comp_error += dbytes - ((int)dbytes); |
1452 |
|
bytes2 += ((int)dbytes); |
1453 |
|
} |
1454 |
|
} |
1455 |
|
else if ((codec->config.curve_compression_high + codec->config.curve_compression_low) && |
1456 |
!frame->intra) |
!frame->intra) |
1457 |
{ |
{ |
1458 |
if (dbytes > twopass->average_frame) |
if (dbytes > twopass->average_frame) |
1650 |
|
|
1651 |
if (WriteFile(codec->twopass.stats1, &nns1, sizeof(NNSTATS), &wrote, 0) == 0 || wrote != sizeof(NNSTATS)) |
if (WriteFile(codec->twopass.stats1, &nns1, sizeof(NNSTATS), &wrote, 0) == 0 || wrote != sizeof(NNSTATS)) |
1652 |
{ |
{ |
1653 |
DEBUG("stats1: WriteFile error"); |
DEBUGERR("stats1: WriteFile error"); |
1654 |
return ICERR_ERROR; |
return ICERR_ERROR; |
1655 |
} |
} |
1656 |
break; |
break; |