Path: blob/master/thirdparty/libwebp/src/utils/huffman_encode_utils.c
20997 views
// Copyright 2011 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Author: Jyrki Alakuijala ([email protected])10//11// Entropy encoding (Huffman) for webp lossless.1213#include <assert.h>14#include <stdlib.h>15#include <string.h>1617#include "src/utils/huffman_encode_utils.h"18#include "src/webp/types.h"19#include "src/utils/utils.h"20#include "src/webp/format_constants.h"2122// -----------------------------------------------------------------------------23// Util function to optimize the symbol map for RLE coding2425// Heuristics for selecting the stride ranges to collapse.26static int ValuesShouldBeCollapsedToStrideAverage(int a, int b) {27return abs(a - b) < 4;28}2930// Change the population counts in a way that the consequent31// Huffman tree compression, especially its RLE-part, give smaller output.32static void OptimizeHuffmanForRle(int length, uint8_t* const good_for_rle,33uint32_t* const counts) {34// 1) Let's make the Huffman code more compatible with rle encoding.35int i;36for (; length >= 0; --length) {37if (length == 0) {38return; // All zeros.39}40if (counts[length - 1] != 0) {41// Now counts[0..length - 1] does not have trailing zeros.42break;43}44}45// 2) Let's mark all population counts that already can be encoded46// with an rle code.47{48// Let's not spoil any of the existing good rle codes.49// Mark any seq of 0's that is longer as 5 as a good_for_rle.50// Mark any seq of non-0's that is longer as 7 as a good_for_rle.51uint32_t symbol = counts[0];52int stride = 0;53for (i = 0; i < length + 1; ++i) {54if (i == length || counts[i] != symbol) {55if ((symbol == 0 && stride >= 5) ||56(symbol != 0 && stride >= 7)) {57int k;58for (k = 0; k < stride; ++k) {59good_for_rle[i - k - 1] = 1;60}61}62stride = 1;63if (i != length) {64symbol = counts[i];65}66} else {67++stride;68}69}70}71// 3) Let's replace those population counts that lead to more rle codes.72{73uint32_t stride = 0;74uint32_t limit = counts[0];75uint32_t sum = 0;76for (i = 0; i < length + 1; ++i) {77if (i == length || good_for_rle[i] ||78(i != 0 && good_for_rle[i - 1]) ||79!ValuesShouldBeCollapsedToStrideAverage(counts[i], limit)) {80if (stride >= 4 || (stride >= 3 && sum == 0)) {81uint32_t k;82// The stride must end, collapse what we have, if we have enough (4).83uint32_t count = (sum + stride / 2) / stride;84if (count < 1) {85count = 1;86}87if (sum == 0) {88// Don't make an all zeros stride to be upgraded to ones.89count = 0;90}91for (k = 0; k < stride; ++k) {92// We don't want to change value at counts[i],93// that is already belonging to the next stride. Thus - 1.94counts[i - k - 1] = count;95}96}97stride = 0;98sum = 0;99if (i < length - 3) {100// All interesting strides have a count of at least 4,101// at least when non-zeros.102limit = (counts[i] + counts[i + 1] +103counts[i + 2] + counts[i + 3] + 2) / 4;104} else if (i < length) {105limit = counts[i];106} else {107limit = 0;108}109}110++stride;111if (i != length) {112sum += counts[i];113if (stride >= 4) {114limit = (sum + stride / 2) / stride;115}116}117}118}119}120121// A comparer function for two Huffman trees: sorts first by 'total count'122// (more comes first), and then by 'value' (more comes first).123static int CompareHuffmanTrees(const void* ptr1, const void* ptr2) {124const HuffmanTree* const t1 = (const HuffmanTree*)ptr1;125const HuffmanTree* const t2 = (const HuffmanTree*)ptr2;126if (t1->total_count > t2->total_count) {127return -1;128} else if (t1->total_count < t2->total_count) {129return 1;130} else {131assert(t1->value != t2->value);132return (t1->value < t2->value) ? -1 : 1;133}134}135136static void SetBitDepths(const HuffmanTree* const tree,137const HuffmanTree* const pool,138uint8_t* const bit_depths, int level) {139if (tree->pool_index_left >= 0) {140SetBitDepths(&pool[tree->pool_index_left], pool, bit_depths, level + 1);141SetBitDepths(&pool[tree->pool_index_right], pool, bit_depths, level + 1);142} else {143bit_depths[tree->value] = level;144}145}146147// Create an optimal Huffman tree.148//149// (data,length): population counts.150// tree_limit: maximum bit depth (inclusive) of the codes.151// bit_depths[]: how many bits are used for the symbol.152//153// Returns 0 when an error has occurred.154//155// The catch here is that the tree cannot be arbitrarily deep156//157// count_limit is the value that is to be faked as the minimum value158// and this minimum value is raised until the tree matches the159// maximum length requirement.160//161// This algorithm is not of excellent performance for very long data blocks,162// especially when population counts are longer than 2**tree_limit, but163// we are not planning to use this with extremely long blocks.164//165// See https://en.wikipedia.org/wiki/Huffman_coding166static void GenerateOptimalTree(const uint32_t* const histogram,167int histogram_size,168HuffmanTree* tree, int tree_depth_limit,169uint8_t* const bit_depths) {170uint32_t count_min;171HuffmanTree* tree_pool;172int tree_size_orig = 0;173int i;174175for (i = 0; i < histogram_size; ++i) {176if (histogram[i] != 0) {177++tree_size_orig;178}179}180181if (tree_size_orig == 0) { // pretty optimal already!182return;183}184185tree_pool = tree + tree_size_orig;186187// For block sizes with less than 64k symbols we never need to do a188// second iteration of this loop.189// If we actually start running inside this loop a lot, we would perhaps190// be better off with the Katajainen algorithm.191assert(tree_size_orig <= (1 << (tree_depth_limit - 1)));192for (count_min = 1; ; count_min *= 2) {193int tree_size = tree_size_orig;194// We need to pack the Huffman tree in tree_depth_limit bits.195// So, we try by faking histogram entries to be at least 'count_min'.196int idx = 0;197int j;198for (j = 0; j < histogram_size; ++j) {199if (histogram[j] != 0) {200const uint32_t count =201(histogram[j] < count_min) ? count_min : histogram[j];202tree[idx].total_count = count;203tree[idx].value = j;204tree[idx].pool_index_left = -1;205tree[idx].pool_index_right = -1;206++idx;207}208}209210// Build the Huffman tree.211qsort(tree, tree_size, sizeof(*tree), CompareHuffmanTrees);212213if (tree_size > 1) { // Normal case.214int tree_pool_size = 0;215while (tree_size > 1) { // Finish when we have only one root.216uint32_t count;217tree_pool[tree_pool_size++] = tree[tree_size - 1];218tree_pool[tree_pool_size++] = tree[tree_size - 2];219count = tree_pool[tree_pool_size - 1].total_count +220tree_pool[tree_pool_size - 2].total_count;221tree_size -= 2;222{223// Search for the insertion point.224int k;225for (k = 0; k < tree_size; ++k) {226if (tree[k].total_count <= count) {227break;228}229}230memmove(tree + (k + 1), tree + k, (tree_size - k) * sizeof(*tree));231tree[k].total_count = count;232tree[k].value = -1;233234tree[k].pool_index_left = tree_pool_size - 1;235tree[k].pool_index_right = tree_pool_size - 2;236tree_size = tree_size + 1;237}238}239SetBitDepths(&tree[0], tree_pool, bit_depths, 0);240} else if (tree_size == 1) { // Trivial case: only one element.241bit_depths[tree[0].value] = 1;242}243244{245// Test if this Huffman tree satisfies our 'tree_depth_limit' criteria.246int max_depth = bit_depths[0];247for (j = 1; j < histogram_size; ++j) {248if (max_depth < bit_depths[j]) {249max_depth = bit_depths[j];250}251}252if (max_depth <= tree_depth_limit) {253break;254}255}256}257}258259// -----------------------------------------------------------------------------260// Coding of the Huffman tree values261262static HuffmanTreeToken* CodeRepeatedValues(int repetitions,263HuffmanTreeToken* tokens,264int value, int prev_value) {265assert(value <= MAX_ALLOWED_CODE_LENGTH);266if (value != prev_value) {267tokens->code = value;268tokens->extra_bits = 0;269++tokens;270--repetitions;271}272while (repetitions >= 1) {273if (repetitions < 3) {274int i;275for (i = 0; i < repetitions; ++i) {276tokens->code = value;277tokens->extra_bits = 0;278++tokens;279}280break;281} else if (repetitions < 7) {282tokens->code = 16;283tokens->extra_bits = repetitions - 3;284++tokens;285break;286} else {287tokens->code = 16;288tokens->extra_bits = 3;289++tokens;290repetitions -= 6;291}292}293return tokens;294}295296static HuffmanTreeToken* CodeRepeatedZeros(int repetitions,297HuffmanTreeToken* tokens) {298while (repetitions >= 1) {299if (repetitions < 3) {300int i;301for (i = 0; i < repetitions; ++i) {302tokens->code = 0; // 0-value303tokens->extra_bits = 0;304++tokens;305}306break;307} else if (repetitions < 11) {308tokens->code = 17;309tokens->extra_bits = repetitions - 3;310++tokens;311break;312} else if (repetitions < 139) {313tokens->code = 18;314tokens->extra_bits = repetitions - 11;315++tokens;316break;317} else {318tokens->code = 18;319tokens->extra_bits = 0x7f; // 138 repeated 0s320++tokens;321repetitions -= 138;322}323}324return tokens;325}326327int VP8LCreateCompressedHuffmanTree(const HuffmanTreeCode* const tree,328HuffmanTreeToken* tokens, int max_tokens) {329HuffmanTreeToken* const starting_token = tokens;330HuffmanTreeToken* const ending_token = tokens + max_tokens;331const int depth_size = tree->num_symbols;332int prev_value = 8; // 8 is the initial value for rle.333int i = 0;334assert(tokens != NULL);335while (i < depth_size) {336const int value = tree->code_lengths[i];337int k = i + 1;338int runs;339while (k < depth_size && tree->code_lengths[k] == value) ++k;340runs = k - i;341if (value == 0) {342tokens = CodeRepeatedZeros(runs, tokens);343} else {344tokens = CodeRepeatedValues(runs, tokens, value, prev_value);345prev_value = value;346}347i += runs;348assert(tokens <= ending_token);349}350(void)ending_token; // suppress 'unused variable' warning351return (int)(tokens - starting_token);352}353354// -----------------------------------------------------------------------------355356// Pre-reversed 4-bit values.357static const uint8_t kReversedBits[16] = {3580x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,3590x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf360};361362static uint32_t ReverseBits(int num_bits, uint32_t bits) {363uint32_t retval = 0;364int i = 0;365while (i < num_bits) {366i += 4;367retval |= kReversedBits[bits & 0xf] << (MAX_ALLOWED_CODE_LENGTH + 1 - i);368bits >>= 4;369}370retval >>= (MAX_ALLOWED_CODE_LENGTH + 1 - num_bits);371return retval;372}373374// Get the actual bit values for a tree of bit depths.375static void ConvertBitDepthsToSymbols(HuffmanTreeCode* const tree) {376// 0 bit-depth means that the symbol does not exist.377int i;378int len;379uint32_t next_code[MAX_ALLOWED_CODE_LENGTH + 1];380int depth_count[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };381382assert(tree != NULL);383len = tree->num_symbols;384for (i = 0; i < len; ++i) {385const int code_length = tree->code_lengths[i];386assert(code_length <= MAX_ALLOWED_CODE_LENGTH);387++depth_count[code_length];388}389depth_count[0] = 0; // ignore unused symbol390next_code[0] = 0;391{392uint32_t code = 0;393for (i = 1; i <= MAX_ALLOWED_CODE_LENGTH; ++i) {394code = (code + depth_count[i - 1]) << 1;395next_code[i] = code;396}397}398for (i = 0; i < len; ++i) {399const int code_length = tree->code_lengths[i];400tree->codes[i] = ReverseBits(code_length, next_code[code_length]++);401}402}403404// -----------------------------------------------------------------------------405// Main entry point406407void VP8LCreateHuffmanTree(uint32_t* const histogram, int tree_depth_limit,408uint8_t* const buf_rle, HuffmanTree* const huff_tree,409HuffmanTreeCode* const huff_code) {410const int num_symbols = huff_code->num_symbols;411memset(buf_rle, 0, num_symbols * sizeof(*buf_rle));412OptimizeHuffmanForRle(num_symbols, buf_rle, histogram);413GenerateOptimalTree(histogram, num_symbols, huff_tree, tree_depth_limit,414huff_code->code_lengths);415// Create the actual bit codes for the bit lengths.416ConvertBitDepthsToSymbols(huff_code);417}418419420