Coverage Report

Created: 2025-11-16 06:36

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.19M
void MoveToFront(uint8_t* v, uint8_t index) {
21
1.19M
  uint8_t value = v[index];
22
1.19M
  uint8_t i = index;
23
37.8M
  for (; i; --i) v[i] = v[i - 1];
24
1.19M
  v[0] = value;
25
1.19M
}
26
27
997
void InverseMoveToFrontTransform(uint8_t* v, size_t v_len) {
28
997
  uint8_t mtf[256];
29
256k
  for (size_t i = 0; i < 256; ++i) {
30
255k
    mtf[i] = static_cast<uint8_t>(i);
31
255k
  }
32
1.24M
  for (size_t i = 0; i < v_len; ++i) {
33
1.24M
    uint8_t index = v[i];
34
1.24M
    v[i] = mtf[index];
35
1.24M
    if (index) MoveToFront(mtf, index);
36
1.24M
  }
37
997
}
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
6.72k
                               BrunsliBitReader* br) {
45
6.72k
  size_t& i = *index;
46
6.72k
  uint8_t* map = context_map->data();
47
6.72k
  const size_t length = context_map->size();
48
1.88M
  while (i < length) {
49
    // Check there is enough deta for Huffman code, RLE and IMTF bit.
50
1.88M
    if (!BrunsliBitReaderCanRead(br, 15 + max_run_length_prefix + 1)) {
51
5.26k
      return BRUNSLI_NOT_ENOUGH_DATA;
52
5.26k
    }
53
1.87M
    uint32_t code = entropy.ReadSymbol(br);
54
1.87M
    if (code == 0) {
55
216k
      map[i] = 0;
56
216k
      ++i;
57
1.66M
    } else if (code <= max_run_length_prefix) {
58
23.6k
      size_t reps = 1u + (1u << code) + (int)BrunsliBitReaderRead(br, code);
59
583k
      while (--reps) {
60
559k
        if (i >= length) return BRUNSLI_INVALID_BRN;
61
559k
        map[i] = 0;
62
559k
        ++i;
63
559k
      }
64
1.63M
    } else {
65
1.63M
      map[i] = (uint8_t)(code - max_run_length_prefix);
66
1.63M
      ++i;
67
1.63M
    }
68
1.87M
  }
69
1.31k
  if (BrunsliBitReaderRead(br, 1)) {
70
997
    InverseMoveToFrontTransform(map, length);
71
997
  }
72
1.31k
  return BrunsliBitReaderIsHealthy(br) ? BRUNSLI_OK : BRUNSLI_INVALID_BRN;
73
6.72k
}
74
75
}  // namespace brunsli