Coverage Report

Created: 2026-04-01 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brunsli/c/dec/context_map_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 "./context_map_decode.h"
8
9
#include "../common/constants.h"
10
#include "../common/platform.h"
11
#include <brunsli/status.h>
12
#include <brunsli/types.h>
13
#include "./bit_reader.h"
14
#include "./huffman_decode.h"
15
16
namespace brunsli {
17
18
namespace {
19
20
1.00M
void MoveToFront(uint8_t* v, uint8_t index) {
21
1.00M
  uint8_t value = v[index];
22
1.00M
  uint8_t i = index;
23
31.7M
  for (; i; --i) v[i] = v[i - 1];
24
1.00M
  v[0] = value;
25
1.00M
}
26
27
788
void InverseMoveToFrontTransform(uint8_t* v, size_t v_len) {
28
788
  uint8_t mtf[256];
29
202k
  for (size_t i = 0; i < 256; ++i) {
30
201k
    mtf[i] = static_cast<uint8_t>(i);
31
201k
  }
32
1.07M
  for (size_t i = 0; i < v_len; ++i) {
33
1.07M
    uint8_t index = v[i];
34
1.07M
    v[i] = mtf[index];
35
1.07M
    if (index) MoveToFront(mtf, index);
36
1.07M
  }
37
788
}
38
39
}  // namespace
40
41
BrunsliStatus DecodeContextMap(const HuffmanDecodingData& entropy,
42
                               size_t max_run_length_prefix, size_t* index,
43
                               std::vector<uint8_t>* context_map,
44
7.25k
                               BrunsliBitReader* br) {
45
7.25k
  size_t& i = *index;
46
7.25k
  uint8_t* map = context_map->data();
47
7.25k
  const size_t length = context_map->size();
48
1.52M
  while (i < length) {
49
    // Check there is enough deta for Huffman code, RLE and IMTF bit.
50
1.51M
    if (!BrunsliBitReaderCanRead(br, 15 + max_run_length_prefix + 1)) {
51
6.09k
      return BRUNSLI_NOT_ENOUGH_DATA;
52
6.09k
    }
53
1.51M
    uint32_t code = entropy.ReadSymbol(br);
54
1.51M
    if (code == 0) {
55
220k
      map[i] = 0;
56
220k
      ++i;
57
1.29M
    } else if (code <= max_run_length_prefix) {
58
23.0k
      size_t reps = 1u + (1u << code) + (int)BrunsliBitReaderRead(br, code);
59
385k
      while (--reps) {
60
362k
        if (i >= length) return BRUNSLI_INVALID_BRN;
61
362k
        map[i] = 0;
62
362k
        ++i;
63
362k
      }
64
1.26M
    } else {
65
1.26M
      map[i] = (uint8_t)(code - max_run_length_prefix);
66
1.26M
      ++i;
67
1.26M
    }
68
1.51M
  }
69
1.05k
  if (BrunsliBitReaderRead(br, 1)) {
70
788
    InverseMoveToFrontTransform(map, length);
71
788
  }
72
1.05k
  return BrunsliBitReaderIsHealthy(br) ? BRUNSLI_OK : BRUNSLI_INVALID_BRN;
73
7.25k
}
74
75
}  // namespace brunsli