11cb0ef41Sopenharmony_ci/* Copyright 2010 Google Inc. All Rights Reserved. 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci Distributed under MIT license. 41cb0ef41Sopenharmony_ci See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 51cb0ef41Sopenharmony_ci*/ 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci/* Entropy encoding (Huffman) utilities. */ 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci#include "./entropy_encode.h" 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci#include <string.h> /* memset */ 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci#include "../common/constants.h" 141cb0ef41Sopenharmony_ci#include "../common/platform.h" 151cb0ef41Sopenharmony_ci#include <brotli/types.h> 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus) 181cb0ef41Sopenharmony_ciextern "C" { 191cb0ef41Sopenharmony_ci#endif 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciconst size_t kBrotliShellGaps[] = {132, 57, 23, 10, 4, 1}; 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciBROTLI_BOOL BrotliSetDepth( 241cb0ef41Sopenharmony_ci int p0, HuffmanTree* pool, uint8_t* depth, int max_depth) { 251cb0ef41Sopenharmony_ci int stack[16]; 261cb0ef41Sopenharmony_ci int level = 0; 271cb0ef41Sopenharmony_ci int p = p0; 281cb0ef41Sopenharmony_ci BROTLI_DCHECK(max_depth <= 15); 291cb0ef41Sopenharmony_ci stack[0] = -1; 301cb0ef41Sopenharmony_ci while (BROTLI_TRUE) { 311cb0ef41Sopenharmony_ci if (pool[p].index_left_ >= 0) { 321cb0ef41Sopenharmony_ci level++; 331cb0ef41Sopenharmony_ci if (level > max_depth) return BROTLI_FALSE; 341cb0ef41Sopenharmony_ci stack[level] = pool[p].index_right_or_value_; 351cb0ef41Sopenharmony_ci p = pool[p].index_left_; 361cb0ef41Sopenharmony_ci continue; 371cb0ef41Sopenharmony_ci } else { 381cb0ef41Sopenharmony_ci depth[pool[p].index_right_or_value_] = (uint8_t)level; 391cb0ef41Sopenharmony_ci } 401cb0ef41Sopenharmony_ci while (level >= 0 && stack[level] == -1) level--; 411cb0ef41Sopenharmony_ci if (level < 0) return BROTLI_TRUE; 421cb0ef41Sopenharmony_ci p = stack[level]; 431cb0ef41Sopenharmony_ci stack[level] = -1; 441cb0ef41Sopenharmony_ci } 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci/* Sort the root nodes, least popular first. */ 481cb0ef41Sopenharmony_cistatic BROTLI_INLINE BROTLI_BOOL SortHuffmanTree( 491cb0ef41Sopenharmony_ci const HuffmanTree* v0, const HuffmanTree* v1) { 501cb0ef41Sopenharmony_ci if (v0->total_count_ != v1->total_count_) { 511cb0ef41Sopenharmony_ci return TO_BROTLI_BOOL(v0->total_count_ < v1->total_count_); 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci return TO_BROTLI_BOOL(v0->index_right_or_value_ > v1->index_right_or_value_); 541cb0ef41Sopenharmony_ci} 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci/* This function will create a Huffman tree. 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci The catch here is that the tree cannot be arbitrarily deep. 591cb0ef41Sopenharmony_ci Brotli specifies a maximum depth of 15 bits for "code trees" 601cb0ef41Sopenharmony_ci and 7 bits for "code length code trees." 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci count_limit is the value that is to be faked as the minimum value 631cb0ef41Sopenharmony_ci and this minimum value is raised until the tree matches the 641cb0ef41Sopenharmony_ci maximum length requirement. 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci This algorithm is not of excellent performance for very long data blocks, 671cb0ef41Sopenharmony_ci especially when population counts are longer than 2**tree_limit, but 681cb0ef41Sopenharmony_ci we are not planning to use this with extremely long blocks. 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci See http://en.wikipedia.org/wiki/Huffman_coding */ 711cb0ef41Sopenharmony_civoid BrotliCreateHuffmanTree(const uint32_t* data, 721cb0ef41Sopenharmony_ci const size_t length, 731cb0ef41Sopenharmony_ci const int tree_limit, 741cb0ef41Sopenharmony_ci HuffmanTree* tree, 751cb0ef41Sopenharmony_ci uint8_t* depth) { 761cb0ef41Sopenharmony_ci uint32_t count_limit; 771cb0ef41Sopenharmony_ci HuffmanTree sentinel; 781cb0ef41Sopenharmony_ci InitHuffmanTree(&sentinel, BROTLI_UINT32_MAX, -1, -1); 791cb0ef41Sopenharmony_ci /* For block sizes below 64 kB, we never need to do a second iteration 801cb0ef41Sopenharmony_ci of this loop. Probably all of our block sizes will be smaller than 811cb0ef41Sopenharmony_ci that, so this loop is mostly of academic interest. If we actually 821cb0ef41Sopenharmony_ci would need this, we would be better off with the Katajainen algorithm. */ 831cb0ef41Sopenharmony_ci for (count_limit = 1; ; count_limit *= 2) { 841cb0ef41Sopenharmony_ci size_t n = 0; 851cb0ef41Sopenharmony_ci size_t i; 861cb0ef41Sopenharmony_ci size_t j; 871cb0ef41Sopenharmony_ci size_t k; 881cb0ef41Sopenharmony_ci for (i = length; i != 0;) { 891cb0ef41Sopenharmony_ci --i; 901cb0ef41Sopenharmony_ci if (data[i]) { 911cb0ef41Sopenharmony_ci const uint32_t count = BROTLI_MAX(uint32_t, data[i], count_limit); 921cb0ef41Sopenharmony_ci InitHuffmanTree(&tree[n++], count, -1, (int16_t)i); 931cb0ef41Sopenharmony_ci } 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci if (n == 1) { 971cb0ef41Sopenharmony_ci depth[tree[0].index_right_or_value_] = 1; /* Only one element. */ 981cb0ef41Sopenharmony_ci break; 991cb0ef41Sopenharmony_ci } 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci SortHuffmanTreeItems(tree, n, SortHuffmanTree); 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci /* The nodes are: 1041cb0ef41Sopenharmony_ci [0, n): the sorted leaf nodes that we start with. 1051cb0ef41Sopenharmony_ci [n]: we add a sentinel here. 1061cb0ef41Sopenharmony_ci [n + 1, 2n): new parent nodes are added here, starting from 1071cb0ef41Sopenharmony_ci (n+1). These are naturally in ascending order. 1081cb0ef41Sopenharmony_ci [2n]: we add a sentinel at the end as well. 1091cb0ef41Sopenharmony_ci There will be (2n+1) elements at the end. */ 1101cb0ef41Sopenharmony_ci tree[n] = sentinel; 1111cb0ef41Sopenharmony_ci tree[n + 1] = sentinel; 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci i = 0; /* Points to the next leaf node. */ 1141cb0ef41Sopenharmony_ci j = n + 1; /* Points to the next non-leaf node. */ 1151cb0ef41Sopenharmony_ci for (k = n - 1; k != 0; --k) { 1161cb0ef41Sopenharmony_ci size_t left, right; 1171cb0ef41Sopenharmony_ci if (tree[i].total_count_ <= tree[j].total_count_) { 1181cb0ef41Sopenharmony_ci left = i; 1191cb0ef41Sopenharmony_ci ++i; 1201cb0ef41Sopenharmony_ci } else { 1211cb0ef41Sopenharmony_ci left = j; 1221cb0ef41Sopenharmony_ci ++j; 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci if (tree[i].total_count_ <= tree[j].total_count_) { 1251cb0ef41Sopenharmony_ci right = i; 1261cb0ef41Sopenharmony_ci ++i; 1271cb0ef41Sopenharmony_ci } else { 1281cb0ef41Sopenharmony_ci right = j; 1291cb0ef41Sopenharmony_ci ++j; 1301cb0ef41Sopenharmony_ci } 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci { 1331cb0ef41Sopenharmony_ci /* The sentinel node becomes the parent node. */ 1341cb0ef41Sopenharmony_ci size_t j_end = 2 * n - k; 1351cb0ef41Sopenharmony_ci tree[j_end].total_count_ = 1361cb0ef41Sopenharmony_ci tree[left].total_count_ + tree[right].total_count_; 1371cb0ef41Sopenharmony_ci tree[j_end].index_left_ = (int16_t)left; 1381cb0ef41Sopenharmony_ci tree[j_end].index_right_or_value_ = (int16_t)right; 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci /* Add back the last sentinel node. */ 1411cb0ef41Sopenharmony_ci tree[j_end + 1] = sentinel; 1421cb0ef41Sopenharmony_ci } 1431cb0ef41Sopenharmony_ci } 1441cb0ef41Sopenharmony_ci if (BrotliSetDepth((int)(2 * n - 1), &tree[0], depth, tree_limit)) { 1451cb0ef41Sopenharmony_ci /* We need to pack the Huffman tree in tree_limit bits. If this was not 1461cb0ef41Sopenharmony_ci successful, add fake entities to the lowest values and retry. */ 1471cb0ef41Sopenharmony_ci break; 1481cb0ef41Sopenharmony_ci } 1491cb0ef41Sopenharmony_ci } 1501cb0ef41Sopenharmony_ci} 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_cistatic void Reverse(uint8_t* v, size_t start, size_t end) { 1531cb0ef41Sopenharmony_ci --end; 1541cb0ef41Sopenharmony_ci while (start < end) { 1551cb0ef41Sopenharmony_ci uint8_t tmp = v[start]; 1561cb0ef41Sopenharmony_ci v[start] = v[end]; 1571cb0ef41Sopenharmony_ci v[end] = tmp; 1581cb0ef41Sopenharmony_ci ++start; 1591cb0ef41Sopenharmony_ci --end; 1601cb0ef41Sopenharmony_ci } 1611cb0ef41Sopenharmony_ci} 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_cistatic void BrotliWriteHuffmanTreeRepetitions( 1641cb0ef41Sopenharmony_ci const uint8_t previous_value, 1651cb0ef41Sopenharmony_ci const uint8_t value, 1661cb0ef41Sopenharmony_ci size_t repetitions, 1671cb0ef41Sopenharmony_ci size_t* tree_size, 1681cb0ef41Sopenharmony_ci uint8_t* tree, 1691cb0ef41Sopenharmony_ci uint8_t* extra_bits_data) { 1701cb0ef41Sopenharmony_ci BROTLI_DCHECK(repetitions > 0); 1711cb0ef41Sopenharmony_ci if (previous_value != value) { 1721cb0ef41Sopenharmony_ci tree[*tree_size] = value; 1731cb0ef41Sopenharmony_ci extra_bits_data[*tree_size] = 0; 1741cb0ef41Sopenharmony_ci ++(*tree_size); 1751cb0ef41Sopenharmony_ci --repetitions; 1761cb0ef41Sopenharmony_ci } 1771cb0ef41Sopenharmony_ci if (repetitions == 7) { 1781cb0ef41Sopenharmony_ci tree[*tree_size] = value; 1791cb0ef41Sopenharmony_ci extra_bits_data[*tree_size] = 0; 1801cb0ef41Sopenharmony_ci ++(*tree_size); 1811cb0ef41Sopenharmony_ci --repetitions; 1821cb0ef41Sopenharmony_ci } 1831cb0ef41Sopenharmony_ci if (repetitions < 3) { 1841cb0ef41Sopenharmony_ci size_t i; 1851cb0ef41Sopenharmony_ci for (i = 0; i < repetitions; ++i) { 1861cb0ef41Sopenharmony_ci tree[*tree_size] = value; 1871cb0ef41Sopenharmony_ci extra_bits_data[*tree_size] = 0; 1881cb0ef41Sopenharmony_ci ++(*tree_size); 1891cb0ef41Sopenharmony_ci } 1901cb0ef41Sopenharmony_ci } else { 1911cb0ef41Sopenharmony_ci size_t start = *tree_size; 1921cb0ef41Sopenharmony_ci repetitions -= 3; 1931cb0ef41Sopenharmony_ci while (BROTLI_TRUE) { 1941cb0ef41Sopenharmony_ci tree[*tree_size] = BROTLI_REPEAT_PREVIOUS_CODE_LENGTH; 1951cb0ef41Sopenharmony_ci extra_bits_data[*tree_size] = repetitions & 0x3; 1961cb0ef41Sopenharmony_ci ++(*tree_size); 1971cb0ef41Sopenharmony_ci repetitions >>= 2; 1981cb0ef41Sopenharmony_ci if (repetitions == 0) { 1991cb0ef41Sopenharmony_ci break; 2001cb0ef41Sopenharmony_ci } 2011cb0ef41Sopenharmony_ci --repetitions; 2021cb0ef41Sopenharmony_ci } 2031cb0ef41Sopenharmony_ci Reverse(tree, start, *tree_size); 2041cb0ef41Sopenharmony_ci Reverse(extra_bits_data, start, *tree_size); 2051cb0ef41Sopenharmony_ci } 2061cb0ef41Sopenharmony_ci} 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_cistatic void BrotliWriteHuffmanTreeRepetitionsZeros( 2091cb0ef41Sopenharmony_ci size_t repetitions, 2101cb0ef41Sopenharmony_ci size_t* tree_size, 2111cb0ef41Sopenharmony_ci uint8_t* tree, 2121cb0ef41Sopenharmony_ci uint8_t* extra_bits_data) { 2131cb0ef41Sopenharmony_ci if (repetitions == 11) { 2141cb0ef41Sopenharmony_ci tree[*tree_size] = 0; 2151cb0ef41Sopenharmony_ci extra_bits_data[*tree_size] = 0; 2161cb0ef41Sopenharmony_ci ++(*tree_size); 2171cb0ef41Sopenharmony_ci --repetitions; 2181cb0ef41Sopenharmony_ci } 2191cb0ef41Sopenharmony_ci if (repetitions < 3) { 2201cb0ef41Sopenharmony_ci size_t i; 2211cb0ef41Sopenharmony_ci for (i = 0; i < repetitions; ++i) { 2221cb0ef41Sopenharmony_ci tree[*tree_size] = 0; 2231cb0ef41Sopenharmony_ci extra_bits_data[*tree_size] = 0; 2241cb0ef41Sopenharmony_ci ++(*tree_size); 2251cb0ef41Sopenharmony_ci } 2261cb0ef41Sopenharmony_ci } else { 2271cb0ef41Sopenharmony_ci size_t start = *tree_size; 2281cb0ef41Sopenharmony_ci repetitions -= 3; 2291cb0ef41Sopenharmony_ci while (BROTLI_TRUE) { 2301cb0ef41Sopenharmony_ci tree[*tree_size] = BROTLI_REPEAT_ZERO_CODE_LENGTH; 2311cb0ef41Sopenharmony_ci extra_bits_data[*tree_size] = repetitions & 0x7; 2321cb0ef41Sopenharmony_ci ++(*tree_size); 2331cb0ef41Sopenharmony_ci repetitions >>= 3; 2341cb0ef41Sopenharmony_ci if (repetitions == 0) { 2351cb0ef41Sopenharmony_ci break; 2361cb0ef41Sopenharmony_ci } 2371cb0ef41Sopenharmony_ci --repetitions; 2381cb0ef41Sopenharmony_ci } 2391cb0ef41Sopenharmony_ci Reverse(tree, start, *tree_size); 2401cb0ef41Sopenharmony_ci Reverse(extra_bits_data, start, *tree_size); 2411cb0ef41Sopenharmony_ci } 2421cb0ef41Sopenharmony_ci} 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_civoid BrotliOptimizeHuffmanCountsForRle(size_t length, uint32_t* counts, 2451cb0ef41Sopenharmony_ci uint8_t* good_for_rle) { 2461cb0ef41Sopenharmony_ci size_t nonzero_count = 0; 2471cb0ef41Sopenharmony_ci size_t stride; 2481cb0ef41Sopenharmony_ci size_t limit; 2491cb0ef41Sopenharmony_ci size_t sum; 2501cb0ef41Sopenharmony_ci const size_t streak_limit = 1240; 2511cb0ef41Sopenharmony_ci /* Let's make the Huffman code more compatible with RLE encoding. */ 2521cb0ef41Sopenharmony_ci size_t i; 2531cb0ef41Sopenharmony_ci for (i = 0; i < length; i++) { 2541cb0ef41Sopenharmony_ci if (counts[i]) { 2551cb0ef41Sopenharmony_ci ++nonzero_count; 2561cb0ef41Sopenharmony_ci } 2571cb0ef41Sopenharmony_ci } 2581cb0ef41Sopenharmony_ci if (nonzero_count < 16) { 2591cb0ef41Sopenharmony_ci return; 2601cb0ef41Sopenharmony_ci } 2611cb0ef41Sopenharmony_ci while (length != 0 && counts[length - 1] == 0) { 2621cb0ef41Sopenharmony_ci --length; 2631cb0ef41Sopenharmony_ci } 2641cb0ef41Sopenharmony_ci if (length == 0) { 2651cb0ef41Sopenharmony_ci return; /* All zeros. */ 2661cb0ef41Sopenharmony_ci } 2671cb0ef41Sopenharmony_ci /* Now counts[0..length - 1] does not have trailing zeros. */ 2681cb0ef41Sopenharmony_ci { 2691cb0ef41Sopenharmony_ci size_t nonzeros = 0; 2701cb0ef41Sopenharmony_ci uint32_t smallest_nonzero = 1 << 30; 2711cb0ef41Sopenharmony_ci for (i = 0; i < length; ++i) { 2721cb0ef41Sopenharmony_ci if (counts[i] != 0) { 2731cb0ef41Sopenharmony_ci ++nonzeros; 2741cb0ef41Sopenharmony_ci if (smallest_nonzero > counts[i]) { 2751cb0ef41Sopenharmony_ci smallest_nonzero = counts[i]; 2761cb0ef41Sopenharmony_ci } 2771cb0ef41Sopenharmony_ci } 2781cb0ef41Sopenharmony_ci } 2791cb0ef41Sopenharmony_ci if (nonzeros < 5) { 2801cb0ef41Sopenharmony_ci /* Small histogram will model it well. */ 2811cb0ef41Sopenharmony_ci return; 2821cb0ef41Sopenharmony_ci } 2831cb0ef41Sopenharmony_ci if (smallest_nonzero < 4) { 2841cb0ef41Sopenharmony_ci size_t zeros = length - nonzeros; 2851cb0ef41Sopenharmony_ci if (zeros < 6) { 2861cb0ef41Sopenharmony_ci for (i = 1; i < length - 1; ++i) { 2871cb0ef41Sopenharmony_ci if (counts[i - 1] != 0 && counts[i] == 0 && counts[i + 1] != 0) { 2881cb0ef41Sopenharmony_ci counts[i] = 1; 2891cb0ef41Sopenharmony_ci } 2901cb0ef41Sopenharmony_ci } 2911cb0ef41Sopenharmony_ci } 2921cb0ef41Sopenharmony_ci } 2931cb0ef41Sopenharmony_ci if (nonzeros < 28) { 2941cb0ef41Sopenharmony_ci return; 2951cb0ef41Sopenharmony_ci } 2961cb0ef41Sopenharmony_ci } 2971cb0ef41Sopenharmony_ci /* 2) Let's mark all population counts that already can be encoded 2981cb0ef41Sopenharmony_ci with an RLE code. */ 2991cb0ef41Sopenharmony_ci memset(good_for_rle, 0, length); 3001cb0ef41Sopenharmony_ci { 3011cb0ef41Sopenharmony_ci /* Let's not spoil any of the existing good RLE codes. 3021cb0ef41Sopenharmony_ci Mark any seq of 0's that is longer as 5 as a good_for_rle. 3031cb0ef41Sopenharmony_ci Mark any seq of non-0's that is longer as 7 as a good_for_rle. */ 3041cb0ef41Sopenharmony_ci uint32_t symbol = counts[0]; 3051cb0ef41Sopenharmony_ci size_t step = 0; 3061cb0ef41Sopenharmony_ci for (i = 0; i <= length; ++i) { 3071cb0ef41Sopenharmony_ci if (i == length || counts[i] != symbol) { 3081cb0ef41Sopenharmony_ci if ((symbol == 0 && step >= 5) || 3091cb0ef41Sopenharmony_ci (symbol != 0 && step >= 7)) { 3101cb0ef41Sopenharmony_ci size_t k; 3111cb0ef41Sopenharmony_ci for (k = 0; k < step; ++k) { 3121cb0ef41Sopenharmony_ci good_for_rle[i - k - 1] = 1; 3131cb0ef41Sopenharmony_ci } 3141cb0ef41Sopenharmony_ci } 3151cb0ef41Sopenharmony_ci step = 1; 3161cb0ef41Sopenharmony_ci if (i != length) { 3171cb0ef41Sopenharmony_ci symbol = counts[i]; 3181cb0ef41Sopenharmony_ci } 3191cb0ef41Sopenharmony_ci } else { 3201cb0ef41Sopenharmony_ci ++step; 3211cb0ef41Sopenharmony_ci } 3221cb0ef41Sopenharmony_ci } 3231cb0ef41Sopenharmony_ci } 3241cb0ef41Sopenharmony_ci /* 3) Let's replace those population counts that lead to more RLE codes. 3251cb0ef41Sopenharmony_ci Math here is in 24.8 fixed point representation. */ 3261cb0ef41Sopenharmony_ci stride = 0; 3271cb0ef41Sopenharmony_ci limit = 256 * (counts[0] + counts[1] + counts[2]) / 3 + 420; 3281cb0ef41Sopenharmony_ci sum = 0; 3291cb0ef41Sopenharmony_ci for (i = 0; i <= length; ++i) { 3301cb0ef41Sopenharmony_ci if (i == length || good_for_rle[i] || 3311cb0ef41Sopenharmony_ci (i != 0 && good_for_rle[i - 1]) || 3321cb0ef41Sopenharmony_ci (256 * counts[i] - limit + streak_limit) >= 2 * streak_limit) { 3331cb0ef41Sopenharmony_ci if (stride >= 4 || (stride >= 3 && sum == 0)) { 3341cb0ef41Sopenharmony_ci size_t k; 3351cb0ef41Sopenharmony_ci /* The stride must end, collapse what we have, if we have enough (4). */ 3361cb0ef41Sopenharmony_ci size_t count = (sum + stride / 2) / stride; 3371cb0ef41Sopenharmony_ci if (count == 0) { 3381cb0ef41Sopenharmony_ci count = 1; 3391cb0ef41Sopenharmony_ci } 3401cb0ef41Sopenharmony_ci if (sum == 0) { 3411cb0ef41Sopenharmony_ci /* Don't make an all zeros stride to be upgraded to ones. */ 3421cb0ef41Sopenharmony_ci count = 0; 3431cb0ef41Sopenharmony_ci } 3441cb0ef41Sopenharmony_ci for (k = 0; k < stride; ++k) { 3451cb0ef41Sopenharmony_ci /* We don't want to change value at counts[i], 3461cb0ef41Sopenharmony_ci that is already belonging to the next stride. Thus - 1. */ 3471cb0ef41Sopenharmony_ci counts[i - k - 1] = (uint32_t)count; 3481cb0ef41Sopenharmony_ci } 3491cb0ef41Sopenharmony_ci } 3501cb0ef41Sopenharmony_ci stride = 0; 3511cb0ef41Sopenharmony_ci sum = 0; 3521cb0ef41Sopenharmony_ci if (i < length - 2) { 3531cb0ef41Sopenharmony_ci /* All interesting strides have a count of at least 4, */ 3541cb0ef41Sopenharmony_ci /* at least when non-zeros. */ 3551cb0ef41Sopenharmony_ci limit = 256 * (counts[i] + counts[i + 1] + counts[i + 2]) / 3 + 420; 3561cb0ef41Sopenharmony_ci } else if (i < length) { 3571cb0ef41Sopenharmony_ci limit = 256 * counts[i]; 3581cb0ef41Sopenharmony_ci } else { 3591cb0ef41Sopenharmony_ci limit = 0; 3601cb0ef41Sopenharmony_ci } 3611cb0ef41Sopenharmony_ci } 3621cb0ef41Sopenharmony_ci ++stride; 3631cb0ef41Sopenharmony_ci if (i != length) { 3641cb0ef41Sopenharmony_ci sum += counts[i]; 3651cb0ef41Sopenharmony_ci if (stride >= 4) { 3661cb0ef41Sopenharmony_ci limit = (256 * sum + stride / 2) / stride; 3671cb0ef41Sopenharmony_ci } 3681cb0ef41Sopenharmony_ci if (stride == 4) { 3691cb0ef41Sopenharmony_ci limit += 120; 3701cb0ef41Sopenharmony_ci } 3711cb0ef41Sopenharmony_ci } 3721cb0ef41Sopenharmony_ci } 3731cb0ef41Sopenharmony_ci} 3741cb0ef41Sopenharmony_ci 3751cb0ef41Sopenharmony_cistatic void DecideOverRleUse(const uint8_t* depth, const size_t length, 3761cb0ef41Sopenharmony_ci BROTLI_BOOL* use_rle_for_non_zero, 3771cb0ef41Sopenharmony_ci BROTLI_BOOL* use_rle_for_zero) { 3781cb0ef41Sopenharmony_ci size_t total_reps_zero = 0; 3791cb0ef41Sopenharmony_ci size_t total_reps_non_zero = 0; 3801cb0ef41Sopenharmony_ci size_t count_reps_zero = 1; 3811cb0ef41Sopenharmony_ci size_t count_reps_non_zero = 1; 3821cb0ef41Sopenharmony_ci size_t i; 3831cb0ef41Sopenharmony_ci for (i = 0; i < length;) { 3841cb0ef41Sopenharmony_ci const uint8_t value = depth[i]; 3851cb0ef41Sopenharmony_ci size_t reps = 1; 3861cb0ef41Sopenharmony_ci size_t k; 3871cb0ef41Sopenharmony_ci for (k = i + 1; k < length && depth[k] == value; ++k) { 3881cb0ef41Sopenharmony_ci ++reps; 3891cb0ef41Sopenharmony_ci } 3901cb0ef41Sopenharmony_ci if (reps >= 3 && value == 0) { 3911cb0ef41Sopenharmony_ci total_reps_zero += reps; 3921cb0ef41Sopenharmony_ci ++count_reps_zero; 3931cb0ef41Sopenharmony_ci } 3941cb0ef41Sopenharmony_ci if (reps >= 4 && value != 0) { 3951cb0ef41Sopenharmony_ci total_reps_non_zero += reps; 3961cb0ef41Sopenharmony_ci ++count_reps_non_zero; 3971cb0ef41Sopenharmony_ci } 3981cb0ef41Sopenharmony_ci i += reps; 3991cb0ef41Sopenharmony_ci } 4001cb0ef41Sopenharmony_ci *use_rle_for_non_zero = 4011cb0ef41Sopenharmony_ci TO_BROTLI_BOOL(total_reps_non_zero > count_reps_non_zero * 2); 4021cb0ef41Sopenharmony_ci *use_rle_for_zero = TO_BROTLI_BOOL(total_reps_zero > count_reps_zero * 2); 4031cb0ef41Sopenharmony_ci} 4041cb0ef41Sopenharmony_ci 4051cb0ef41Sopenharmony_civoid BrotliWriteHuffmanTree(const uint8_t* depth, 4061cb0ef41Sopenharmony_ci size_t length, 4071cb0ef41Sopenharmony_ci size_t* tree_size, 4081cb0ef41Sopenharmony_ci uint8_t* tree, 4091cb0ef41Sopenharmony_ci uint8_t* extra_bits_data) { 4101cb0ef41Sopenharmony_ci uint8_t previous_value = BROTLI_INITIAL_REPEATED_CODE_LENGTH; 4111cb0ef41Sopenharmony_ci size_t i; 4121cb0ef41Sopenharmony_ci BROTLI_BOOL use_rle_for_non_zero = BROTLI_FALSE; 4131cb0ef41Sopenharmony_ci BROTLI_BOOL use_rle_for_zero = BROTLI_FALSE; 4141cb0ef41Sopenharmony_ci 4151cb0ef41Sopenharmony_ci /* Throw away trailing zeros. */ 4161cb0ef41Sopenharmony_ci size_t new_length = length; 4171cb0ef41Sopenharmony_ci for (i = 0; i < length; ++i) { 4181cb0ef41Sopenharmony_ci if (depth[length - i - 1] == 0) { 4191cb0ef41Sopenharmony_ci --new_length; 4201cb0ef41Sopenharmony_ci } else { 4211cb0ef41Sopenharmony_ci break; 4221cb0ef41Sopenharmony_ci } 4231cb0ef41Sopenharmony_ci } 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_ci /* First gather statistics on if it is a good idea to do RLE. */ 4261cb0ef41Sopenharmony_ci if (length > 50) { 4271cb0ef41Sopenharmony_ci /* Find RLE coding for longer codes. 4281cb0ef41Sopenharmony_ci Shorter codes seem not to benefit from RLE. */ 4291cb0ef41Sopenharmony_ci DecideOverRleUse(depth, new_length, 4301cb0ef41Sopenharmony_ci &use_rle_for_non_zero, &use_rle_for_zero); 4311cb0ef41Sopenharmony_ci } 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_ci /* Actual RLE coding. */ 4341cb0ef41Sopenharmony_ci for (i = 0; i < new_length;) { 4351cb0ef41Sopenharmony_ci const uint8_t value = depth[i]; 4361cb0ef41Sopenharmony_ci size_t reps = 1; 4371cb0ef41Sopenharmony_ci if ((value != 0 && use_rle_for_non_zero) || 4381cb0ef41Sopenharmony_ci (value == 0 && use_rle_for_zero)) { 4391cb0ef41Sopenharmony_ci size_t k; 4401cb0ef41Sopenharmony_ci for (k = i + 1; k < new_length && depth[k] == value; ++k) { 4411cb0ef41Sopenharmony_ci ++reps; 4421cb0ef41Sopenharmony_ci } 4431cb0ef41Sopenharmony_ci } 4441cb0ef41Sopenharmony_ci if (value == 0) { 4451cb0ef41Sopenharmony_ci BrotliWriteHuffmanTreeRepetitionsZeros( 4461cb0ef41Sopenharmony_ci reps, tree_size, tree, extra_bits_data); 4471cb0ef41Sopenharmony_ci } else { 4481cb0ef41Sopenharmony_ci BrotliWriteHuffmanTreeRepetitions(previous_value, 4491cb0ef41Sopenharmony_ci value, reps, tree_size, 4501cb0ef41Sopenharmony_ci tree, extra_bits_data); 4511cb0ef41Sopenharmony_ci previous_value = value; 4521cb0ef41Sopenharmony_ci } 4531cb0ef41Sopenharmony_ci i += reps; 4541cb0ef41Sopenharmony_ci } 4551cb0ef41Sopenharmony_ci} 4561cb0ef41Sopenharmony_ci 4571cb0ef41Sopenharmony_cistatic uint16_t BrotliReverseBits(size_t num_bits, uint16_t bits) { 4581cb0ef41Sopenharmony_ci static const size_t kLut[16] = { /* Pre-reversed 4-bit values. */ 4591cb0ef41Sopenharmony_ci 0x00, 0x08, 0x04, 0x0C, 0x02, 0x0A, 0x06, 0x0E, 4601cb0ef41Sopenharmony_ci 0x01, 0x09, 0x05, 0x0D, 0x03, 0x0B, 0x07, 0x0F 4611cb0ef41Sopenharmony_ci }; 4621cb0ef41Sopenharmony_ci size_t retval = kLut[bits & 0x0F]; 4631cb0ef41Sopenharmony_ci size_t i; 4641cb0ef41Sopenharmony_ci for (i = 4; i < num_bits; i += 4) { 4651cb0ef41Sopenharmony_ci retval <<= 4; 4661cb0ef41Sopenharmony_ci bits = (uint16_t)(bits >> 4); 4671cb0ef41Sopenharmony_ci retval |= kLut[bits & 0x0F]; 4681cb0ef41Sopenharmony_ci } 4691cb0ef41Sopenharmony_ci retval >>= ((0 - num_bits) & 0x03); 4701cb0ef41Sopenharmony_ci return (uint16_t)retval; 4711cb0ef41Sopenharmony_ci} 4721cb0ef41Sopenharmony_ci 4731cb0ef41Sopenharmony_ci/* 0..15 are values for bits */ 4741cb0ef41Sopenharmony_ci#define MAX_HUFFMAN_BITS 16 4751cb0ef41Sopenharmony_ci 4761cb0ef41Sopenharmony_civoid BrotliConvertBitDepthsToSymbols(const uint8_t* depth, 4771cb0ef41Sopenharmony_ci size_t len, 4781cb0ef41Sopenharmony_ci uint16_t* bits) { 4791cb0ef41Sopenharmony_ci /* In Brotli, all bit depths are [1..15] 4801cb0ef41Sopenharmony_ci 0 bit depth means that the symbol does not exist. */ 4811cb0ef41Sopenharmony_ci uint16_t bl_count[MAX_HUFFMAN_BITS] = { 0 }; 4821cb0ef41Sopenharmony_ci uint16_t next_code[MAX_HUFFMAN_BITS]; 4831cb0ef41Sopenharmony_ci size_t i; 4841cb0ef41Sopenharmony_ci int code = 0; 4851cb0ef41Sopenharmony_ci for (i = 0; i < len; ++i) { 4861cb0ef41Sopenharmony_ci ++bl_count[depth[i]]; 4871cb0ef41Sopenharmony_ci } 4881cb0ef41Sopenharmony_ci bl_count[0] = 0; 4891cb0ef41Sopenharmony_ci next_code[0] = 0; 4901cb0ef41Sopenharmony_ci for (i = 1; i < MAX_HUFFMAN_BITS; ++i) { 4911cb0ef41Sopenharmony_ci code = (code + bl_count[i - 1]) << 1; 4921cb0ef41Sopenharmony_ci next_code[i] = (uint16_t)code; 4931cb0ef41Sopenharmony_ci } 4941cb0ef41Sopenharmony_ci for (i = 0; i < len; ++i) { 4951cb0ef41Sopenharmony_ci if (depth[i]) { 4961cb0ef41Sopenharmony_ci bits[i] = BrotliReverseBits(depth[i], next_code[depth[i]]++); 4971cb0ef41Sopenharmony_ci } 4981cb0ef41Sopenharmony_ci } 4991cb0ef41Sopenharmony_ci} 5001cb0ef41Sopenharmony_ci 5011cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus) 5021cb0ef41Sopenharmony_ci} /* extern "C" */ 5031cb0ef41Sopenharmony_ci#endif 504