Coverage Report

Created: 2025-11-09 06:58

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.16M
void MoveToFront(uint8_t* v, uint8_t index) {
21
1.16M
  uint8_t value = v[index];
22
1.16M
  uint8_t i = index;
23
50.1M
  for (; i; --i) v[i] = v[i - 1];
24
1.16M
  v[0] = value;
25
1.16M
}
26
27
932
void InverseMoveToFrontTransform(uint8_t* v, size_t v_len) {
28
932
  uint8_t mtf[256];
29
239k
  for (size_t i = 0; i < 256; ++i) {
30
238k
    mtf[i] = static_cast<uint8_t>(i);
31
238k
  }
32
1.22M
  for (size_t i = 0; i < v_len; ++i) {
33
1.22M
    uint8_t index = v[i];
34
1.22M
    v[i] = mtf[index];
35
1.22M
    if (index) MoveToFront(mtf, index);
36
1.22M
  }
37
932
}
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.89k
                               BrunsliBitReader* br) {
45
6.89k
  size_t& i = *index;
46
6.89k
  uint8_t* map = context_map->data();
47
6.89k
  const size_t length = context_map->size();
48
1.90M
  while (i < length) {
49
    // Check there is enough deta for Huffman code, RLE and IMTF bit.
50
1.90M
    if (!BrunsliBitReaderCanRead(br, 15 + max_run_length_prefix + 1)) {
51
5.45k
      return BRUNSLI_NOT_ENOUGH_DATA;
52
5.45k
    }
53
1.89M
    uint32_t code = entropy.ReadSymbol(br);
54
1.89M
    if (code == 0) {
55
277k
      map[i] = 0;
56
277k
      ++i;
57
1.62M
    } else if (code <= max_run_length_prefix) {
58
40.9k
      size_t reps = 1u + (1u << code) + (int)BrunsliBitReaderRead(br, code);
59
622k
      while (--reps) {
60
581k
        if (i >= length) return BRUNSLI_INVALID_BRN;
61
581k
        map[i] = 0;
62
581k
        ++i;
63
581k
      }
64
1.58M
    } else {
65
1.58M
      map[i] = (uint8_t)(code - max_run_length_prefix);
66
1.58M
      ++i;
67
1.58M
    }
68
1.89M
  }
69
1.30k
  if (BrunsliBitReaderRead(br, 1)) {
70
932
    InverseMoveToFrontTransform(map, length);
71
932
  }
72
1.30k
  return BrunsliBitReaderIsHealthy(br) ? BRUNSLI_OK : BRUNSLI_INVALID_BRN;
73
6.89k
}
74
75
}  // namespace brunsli