Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/brotli/c/enc/literal_cost.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2013 Google Inc. All Rights Reserved.
2
3
   Distributed under MIT license.
4
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
/* Literal cost model to allow backward reference replacement to be efficient.
8
*/
9
10
#include "literal_cost.h"
11
12
#include <string.h>  /* memset */
13
14
#include <brotli/types.h>
15
16
#include "../common/platform.h"
17
#include "fast_log.h"
18
#include "utf8_util.h"
19
20
#if defined(__cplusplus) || defined(c_plusplus)
21
extern "C" {
22
#endif
23
24
0
static size_t UTF8Position(size_t last, size_t c, size_t clamp) {
25
0
  if (c < 128) {
26
0
    return 0;  /* Next one is the 'Byte 1' again. */
27
0
  } else if (c >= 192) {  /* Next one is the 'Byte 2' of utf-8 encoding. */
28
0
    return BROTLI_MIN(size_t, 1, clamp);
29
0
  } else {
30
    /* Let's decide over the last byte if this ends the sequence. */
31
0
    if (last < 0xE0) {
32
0
      return 0;  /* Completed two or three byte coding. */
33
0
    } else {  /* Next one is the 'Byte 3' of utf-8 encoding. */
34
0
      return BROTLI_MIN(size_t, 2, clamp);
35
0
    }
36
0
  }
37
0
}
38
39
static size_t DecideMultiByteStatsLevel(size_t pos, size_t len, size_t mask,
40
0
                                        const uint8_t* data) {
41
0
  size_t counts[3] = { 0 };
42
0
  size_t max_utf8 = 1;  /* should be 2, but 1 compresses better. */
43
0
  size_t last_c = 0;
44
0
  size_t i;
45
0
  for (i = 0; i < len; ++i) {
46
0
    size_t c = data[(pos + i) & mask];
47
0
    ++counts[UTF8Position(last_c, c, 2)];
48
0
    last_c = c;
49
0
  }
50
0
  if (counts[2] < 500) {
51
0
    max_utf8 = 1;
52
0
  }
53
0
  if (counts[1] + counts[2] < 25) {
54
0
    max_utf8 = 0;
55
0
  }
56
0
  return max_utf8;
57
0
}
58
59
static void EstimateBitCostsForLiteralsUTF8(size_t pos, size_t len, size_t mask,
60
                                            const uint8_t* data,
61
0
                                            size_t* histogram, float* cost) {
62
  /* max_utf8 is 0 (normal ASCII single byte modeling),
63
     1 (for 2-byte UTF-8 modeling), or 2 (for 3-byte UTF-8 modeling). */
64
0
  const size_t max_utf8 = DecideMultiByteStatsLevel(pos, len, mask, data);
65
0
  size_t window_half = 495;
66
0
  size_t in_window = BROTLI_MIN(size_t, window_half, len);
67
0
  size_t in_window_utf8[3] = { 0 };
68
0
  size_t i;
69
0
  memset(histogram, 0, 3 * 256 * sizeof(histogram[0]));
70
71
0
  {  /* Bootstrap histograms. */
72
0
    size_t last_c = 0;
73
0
    size_t utf8_pos = 0;
74
0
    for (i = 0; i < in_window; ++i) {
75
0
      size_t c = data[(pos + i) & mask];
76
0
      ++histogram[256 * utf8_pos + c];
77
0
      ++in_window_utf8[utf8_pos];
78
0
      utf8_pos = UTF8Position(last_c, c, max_utf8);
79
0
      last_c = c;
80
0
    }
81
0
  }
82
83
  /* Compute bit costs with sliding window. */
84
0
  for (i = 0; i < len; ++i) {
85
0
    if (i >= window_half) {
86
      /* Remove a byte in the past. */
87
0
      size_t c =
88
0
          i < window_half + 1 ? 0 : data[(pos + i - window_half - 1) & mask];
89
0
      size_t last_c =
90
0
          i < window_half + 2 ? 0 : data[(pos + i - window_half - 2) & mask];
91
0
      size_t utf8_pos2 = UTF8Position(last_c, c, max_utf8);
92
0
      --histogram[256 * utf8_pos2 + data[(pos + i - window_half) & mask]];
93
0
      --in_window_utf8[utf8_pos2];
94
0
    }
95
0
    if (i + window_half < len) {
96
      /* Add a byte in the future. */
97
0
      size_t c = data[(pos + i + window_half - 1) & mask];
98
0
      size_t last_c = data[(pos + i + window_half - 2) & mask];
99
0
      size_t utf8_pos2 = UTF8Position(last_c, c, max_utf8);
100
0
      ++histogram[256 * utf8_pos2 + data[(pos + i + window_half) & mask]];
101
0
      ++in_window_utf8[utf8_pos2];
102
0
    }
103
0
    {
104
0
      size_t c = i < 1 ? 0 : data[(pos + i - 1) & mask];
105
0
      size_t last_c = i < 2 ? 0 : data[(pos + i - 2) & mask];
106
0
      size_t utf8_pos = UTF8Position(last_c, c, max_utf8);
107
0
      size_t masked_pos = (pos + i) & mask;
108
0
      size_t histo = histogram[256 * utf8_pos + data[masked_pos]];
109
0
      double lit_cost;
110
0
      if (histo == 0) {
111
0
        histo = 1;
112
0
      }
113
0
      lit_cost = FastLog2(in_window_utf8[utf8_pos]) - FastLog2(histo);
114
0
      lit_cost += 0.02905;
115
0
      if (lit_cost < 1.0) {
116
0
        lit_cost *= 0.5;
117
0
        lit_cost += 0.5;
118
0
      }
119
      /* Make the first bytes more expensive -- seems to help, not sure why.
120
         Perhaps because the entropy source is changing its properties
121
         rapidly in the beginning of the file, perhaps because the beginning
122
         of the data is a statistical "anomaly". */
123
0
      if (i < 2000) {
124
0
        lit_cost += 0.7 - ((double)(2000 - i) / 2000.0 * 0.35);
125
0
      }
126
0
      cost[i] = (float)lit_cost;
127
0
    }
128
0
  }
129
0
}
130
131
void BrotliEstimateBitCostsForLiterals(size_t pos, size_t len, size_t mask,
132
                                       const uint8_t* data,
133
0
                                       size_t* histogram, float* cost) {
134
0
  if (BrotliIsMostlyUTF8(data, pos, mask, len, kMinUTF8Ratio)) {
135
0
    EstimateBitCostsForLiteralsUTF8(pos, len, mask, data, histogram, cost);
136
0
    return;
137
0
  } else {
138
0
    size_t window_half = 2000;
139
0
    size_t in_window = BROTLI_MIN(size_t, window_half, len);
140
0
    size_t i;
141
0
    memset(histogram, 0, 256 * sizeof(histogram[0]));
142
143
    /* Bootstrap histogram. */
144
0
    for (i = 0; i < in_window; ++i) {
145
0
      ++histogram[data[(pos + i) & mask]];
146
0
    }
147
148
    /* Compute bit costs with sliding window. */
149
0
    for (i = 0; i < len; ++i) {
150
0
      size_t histo;
151
0
      if (i >= window_half) {
152
        /* Remove a byte in the past. */
153
0
        --histogram[data[(pos + i - window_half) & mask]];
154
0
        --in_window;
155
0
      }
156
0
      if (i + window_half < len) {
157
        /* Add a byte in the future. */
158
0
        ++histogram[data[(pos + i + window_half) & mask]];
159
0
        ++in_window;
160
0
      }
161
0
      histo = histogram[data[(pos + i) & mask]];
162
0
      if (histo == 0) {
163
0
        histo = 1;
164
0
      }
165
0
      {
166
0
        double lit_cost = FastLog2(in_window) - FastLog2(histo);
167
0
        lit_cost += 0.029;
168
0
        if (lit_cost < 1.0) {
169
0
          lit_cost *= 0.5;
170
0
          lit_cost += 0.5;
171
0
        }
172
0
        cost[i] = (float)lit_cost;
173
0
      }
174
0
    }
175
0
  }
176
0
}
177
178
#if defined(__cplusplus) || defined(c_plusplus)
179
}  /* extern "C" */
180
#endif