Created
April 18, 2018 02:40
-
-
Save irrationalRock/af03a1ff09236ccb326fcc833b1e937e to your computer and use it in GitHub Desktop.
BrotliEstimateBitCostsForLiterals(18.24)
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
void BrotliEstimateBitCostsForLiterals(size_t pos, size_t len, size_t mask, | |
const uint8_t* data, float* cost) { | |
if (BrotliIsMostlyUTF8(data, pos, mask, len, kMinUTF8Ratio)) { | |
EstimateBitCostsForLiteralsUTF8(pos, len, mask, data, cost); | |
return; | |
} else { | |
size_t histogram[256] = { 0 }; | |
size_t window_half = 2000; | |
size_t in_window = BROTLI_MIN(size_t, window_half, len); | |
/* Bootstrap histogram. */ | |
size_t i; | |
for (i = 0; i < in_window; ++i) { | |
++histogram[data[(pos + i) & mask]]; | |
} | |
/* Compute bit costs with sliding window. */ | |
for (i = 0; i < len; ++i) { | |
size_t histo; | |
if (i >= window_half) { | |
/* Remove a byte in the past. */ | |
--histogram[data[(pos + i - window_half) & mask]]; | |
--in_window; | |
} | |
if (i + window_half < len) { | |
/* Add a byte in the future. */ | |
++histogram[data[(pos + i + window_half) & mask]]; | |
++in_window; | |
} | |
histo = histogram[data[(pos + i) & mask]]; | |
if (histo == 0) { | |
histo = 1; | |
} | |
{ | |
double lit_cost = FastLog2(in_window) - FastLog2(histo); | |
lit_cost += 0.029; | |
if (lit_cost < 1.0) { | |
lit_cost *= 0.5; | |
lit_cost += 0.5; | |
} | |
cost[i] = (float)lit_cost; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment