Coverage Report

Created: 2026-12-01 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brunsli/c/dec/histogram_decode.cc
Line
Count
Source
1
// Copyright (c) Google LLC 2019
2
//
3
// Use of this source code is governed by an MIT-style
4
// license that can be found in the LICENSE file or at
5
// https://opensource.org/licenses/MIT.
6
7
#include "./histogram_decode.h"
8
9
#include <brunsli/types.h>
10
11
#include <algorithm>
12
#include <vector>
13
14
#include "../common/ans_params.h"
15
#include "../common/histogram.h"
16
#include "../common/platform.h"
17
#include "./bit_reader.h"
18
19
namespace brunsli {
20
21
namespace {
22
23
// Binary trees; positive - offset to child nodes; other - minus leaf value.
24
// 3-8 bits encoding value 3..18.
25
const int8_t kLengthTree[] = {1,   2,   3,  4,  5,  6,   7, -10, -11, -12, -13,
26
                              -14, -15, 2,  3,  -9, -16, 2, 3,   -8,  -17, 2,
27
                              3,   -5,  -6, -7, 1,  -18, 1, -3,  -4};
28
// 2..6 bits encoding value 0..10.
29
const int8_t kLogCountTree[] = {1, 2, 3,  -6, 3,  4, 5, -4, -5, -7, -8,
30
                                2, 3, -1, -2, -3, 1, 0, 1,  -9, -10};
31
32
49.0k
size_t ReadShortHuffmanCode(BrunsliBitReader* br, const int8_t* tree) {
33
49.0k
  size_t pos = 0;
34
49.0k
  int8_t delta = 1;
35
192k
  while (delta > 0) {
36
143k
    pos += delta + BrunsliBitReaderRead(br, 1);
37
143k
    delta = tree[pos];
38
143k
  }
39
49.0k
  return static_cast<size_t>(-delta);
40
49.0k
}
41
42
}  // namespace
43
44
bool ReadHistogram(uint32_t precision_bits, std::vector<uint32_t>* counts,
45
23.6k
                   BrunsliBitReader* br) {
46
23.6k
  BRUNSLI_DCHECK(!counts->empty());
47
23.6k
  uint32_t space = 1u << precision_bits;
48
23.6k
  const size_t length = counts->size();
49
23.6k
  std::fill(counts->begin(), counts->end(), 0);
50
23.6k
  uint32_t* histogram = counts->data();
51
23.6k
  int simple_code = BrunsliBitReaderRead(br, 1);
52
23.6k
  if (simple_code == 1) {
53
19.5k
    size_t max_bits_counter = length - 1;
54
19.5k
    uint32_t max_bits = 0;
55
19.5k
    int symbols[2] = {0};
56
19.5k
    const size_t num_symbols = BrunsliBitReaderRead(br, 1) + 1u;
57
117k
    while (max_bits_counter) {
58
97.6k
      max_bits_counter >>= 1;
59
97.6k
      ++max_bits;
60
97.6k
    }
61
44.7k
    for (size_t i = 0; i < num_symbols; ++i) {
62
25.2k
      symbols[i] = BrunsliBitReaderRead(br, max_bits) % length;
63
25.2k
    }
64
19.5k
    if (num_symbols == 1) {
65
13.8k
      histogram[symbols[0]] = space;
66
13.8k
    } else {
67
5.70k
      if (symbols[0] == symbols[1]) {  // corrupt data
68
26
        return false;
69
26
      }
70
5.67k
      uint32_t value = BrunsliBitReaderRead(br, precision_bits);
71
5.67k
      histogram[symbols[0]] = value;
72
5.67k
      histogram[symbols[1]] = space - value;
73
5.67k
    }
74
19.5k
  } else {
75
4.09k
    size_t real_length = ReadShortHuffmanCode(br, kLengthTree);
76
4.09k
    uint32_t total_count = 0;
77
4.09k
    uint32_t log_counts[BRUNSLI_ANS_MAX_SYMBOLS];
78
4.09k
    size_t omit_pos = 0;
79
4.09k
    BRUNSLI_DCHECK(real_length > 2);
80
49.0k
    for (size_t i = 0; i < real_length; ++i) {
81
44.9k
      log_counts[i] =
82
44.9k
          static_cast<uint32_t>(ReadShortHuffmanCode(br, kLogCountTree));
83
44.9k
      if (log_counts[i] > log_counts[omit_pos]) omit_pos = i;
84
44.9k
    }
85
4.09k
    BRUNSLI_DCHECK(omit_pos >= 0);
86
49.0k
    for (size_t i = 0; i < real_length; ++i) {
87
44.9k
      uint32_t code = log_counts[i];
88
44.9k
      if (i == omit_pos) {
89
4.09k
        continue;
90
40.8k
      } else if (code == 0) {
91
1.41k
        continue;
92
39.4k
      } else if (code == 1) {
93
3.79k
        histogram[i] = 1;
94
35.6k
      } else {
95
35.6k
        uint32_t bit_count = GetPopulationCountPrecision(code - 1);
96
35.6k
        histogram[i] = (1u << (code - 1)) + (BrunsliBitReaderRead(br, bit_count)
97
35.6k
                                             << (code - 1 - bit_count));
98
35.6k
      }
99
39.4k
      total_count += histogram[i];
100
39.4k
    }
101
4.09k
    if (total_count >= space) {
102
      // The histogram we've read sums to more than total_count (including at
103
      // least 1 for the omitted value).
104
33
      return false;
105
33
    }
106
4.06k
    histogram[omit_pos] = space - total_count;
107
4.06k
  }
108
23.5k
  return BrunsliBitReaderIsHealthy(br);
109
23.6k
}
110
111
}  // namespace brunsli