Last active
August 8, 2023 00:54
-
-
Save nm004/13dde8a01465300f3c3e308f6305cccf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import mmap | |
import sys | |
# Author: Nozomi Miyamori | |
# This program is in the Public Domain. | |
test_quan_table = [2, 1, 1, 1, 1, 0, 0, 0] | |
test_rate_table = [50, 40, 5, 4, 1, 0, 0, 0] | |
# usage this_prog.py brem [brem...] | |
def main(): | |
quan_exp_base = math.e ** ((1+5**(1/2))/2) | |
for i in sys.argv[1:]: | |
with open(i, "r+b") as f: | |
with mmap.mmap(f.fileno(), 0) as mm: | |
if i.endswith('.brem'): | |
rare_thresh = 12.50 | |
rare_drop_prob_coef = math.e | |
Qs = slice(0x33,0x33+8) | |
Rs = slice(0x3b,0x3b+8) | |
else: | |
rare_thresh = 6.250 | |
rare_drop_prob_coef = math.e ** (1/2) | |
Qs = slice(0x52,0x52+0x10) | |
Rs = slice(0x62,0x62+0x10) | |
Q = list(mm[Qs]) | |
R = list(mm[Rs]) | |
update_quan_rate_table(Q, quan_exp_base, R, rare_thresh, rare_drop_prob_coef) | |
mm[Rs] = bytes(R) | |
mm[Qs] = bytes(Q) | |
def argmax(l): | |
return max(enumerate(l), key=lambda x: x[1])[0] | |
def update_quan_rate_table(quan_table, quan_exp_base, rate_table, rare_thresh, rare_drop_prob_coef): | |
for k,v in enumerate(quan_table): | |
quan_table[k] = max(round(math.log(max(v * rate_table[k], 1), quan_exp_base) * v), v) | |
def new_rare_drop_rate(r): | |
return round(min(rare_drop_prob_coef * r, rare_thresh)) | |
S = sum(new_rare_drop_rate(i) - i for i in rate_table if 0 < i <= rare_thresh) | |
S2 = sum(i for i in rate_table if i > rare_thresh) | |
if S2 == 0: | |
return | |
for k,v in enumerate(rate_table): | |
if 0 < v <= rare_thresh: | |
rate_table[k] = new_rare_drop_rate(v) | |
else: | |
rate_table[k] = rate_table[k] - round(S * v/S2) | |
for i in range(sum(rate_table) - 100): | |
rate_table[argmax(rate_table)] -= 1 | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment