Coverage Report

Created: 2024-09-08 06:07

/src/brunsli/c/common/context.h
Line
Count
Source (jump to first uncovered line)
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
#ifndef BRUNSLI_COMMON_CONTEXT_H_
8
#define BRUNSLI_COMMON_CONTEXT_H_
9
10
#include <vector>
11
12
#include "./distributions.h"
13
#include <brunsli/jpeg_data.h>
14
#include "./platform.h"
15
#include <brunsli/types.h>
16
17
namespace brunsli {
18
19
static const size_t kMaxAverageContext = 8;
20
static const size_t kNumAvrgContexts = kMaxAverageContext + 1u;
21
// 6 bits allow encoding values 0..63; this range represents the possible
22
// quantities of non-zero AC coefficients in the DCT block.
23
static const size_t kNumNonZeroBits = 6u;
24
/**
25
 * "number of non-zeros" value is decoded as a series of bits,
26
 * highest to lowest.
27
 *
28
 * Partially decoded value is used as a context for reading the next bit.
29
 * Contexts are organized in a binary tree. There are 64 final values, thus
30
 * there are 1-less non-leaf nodes.
31
 * Also, this constant also denotes the maximal value that could be encoded.
32
 *
33
 * static_assert(kNumNonZeroTreeSize == kDCTBlockSize - 1u)
34
 */
35
static const size_t kNumNonZeroTreeSize = (1u << kNumNonZeroBits) - 1u;
36
static const size_t kNumNonZeroQuant = 2u;
37
static const size_t kNumNonZeroContextMax =
38
    kNumNonZeroTreeSize / kNumNonZeroQuant;
39
static const size_t kNumNonZeroContextCount = kNumNonZeroContextMax + 1u;
40
41
static const uint8_t kNonzeroBuckets[64] = {
42
    0,  1,  2,  3,  4,  4,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,
43
    7,  7,  7,  7,  7,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
44
    9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  10, 10, 10,
45
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
46
};
47
// kNonzeroBuckets[i] < kNumNonzeroBuckets
48
static const uint8_t kNumNonzeroBuckets = 11;
49
50
static const int kNumSchemes = 7;
51
52
static const uint8_t kFreqContext[kNumSchemes][64] = {
53
    {
54
        0,
55
    },
56
57
    {
58
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59
        0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
60
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
61
    },
62
63
    {
64
        0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
65
        2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
66
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1,
67
    },
68
69
    {
70
        0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5,
71
        5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7,
72
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 2, 2, 2,
73
    },
74
75
    {
76
        0,  1,  2,  3,  4,  4,  5,  5,  6,  6,  7,  7,  8,  8,  8,  8,
77
        9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12,
78
        13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14,
79
        15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
80
    },
81
82
    {
83
        0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15,
84
        16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
85
        24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27,
86
        28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31,
87
    },
88
89
    {
90
        0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15,
91
        16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
92
        32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
93
        48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
94
    },
95
};
96
97
static const uint16_t kNumNonzeroContext[kNumSchemes][64] = {
98
    {0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5,
99
     5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7,
100
     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
101
    {0,  2,  2,  4,  4,  4,  6,  6,  6,  6,  8,  8,  8,  8,  8,  8,
102
     10, 10, 10, 10, 10, 10, 10, 10, 12, 12, 12, 12, 12, 12, 12, 12,
103
     12, 12, 12, 12, 12, 12, 12, 12, 14, 14, 14, 14, 14, 14, 14, 14,
104
     14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
105
    {0,  4,  4,  8,  8,  8,  12, 12, 12, 12, 16, 16, 16, 16, 16, 16,
106
     20, 20, 20, 20, 20, 20, 20, 20, 24, 24, 24, 24, 24, 24, 24, 24,
107
     24, 24, 24, 24, 24, 24, 24, 24, 28, 28, 28, 28, 28, 28, 28, 28,
108
     28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28},
109
    {0,  8,  8,  16, 16, 16, 24, 24, 24, 24, 32, 32, 32, 32, 32, 32,
110
     40, 40, 40, 40, 40, 40, 40, 40, 48, 48, 48, 48, 48, 48, 48, 48,
111
     48, 48, 48, 48, 48, 48, 48, 48, 55, 55, 55, 55, 55, 55, 55, 55,
112
     55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55},
113
    {0,   16,  16,  32,  32,  32,  48,  48,  48,  48,  64,  64,  64,
114
     64,  64,  64,  80,  80,  80,  80,  80,  80,  80,  80,  95,  95,
115
     95,  95,  95,  95,  95,  95,  95,  95,  95,  95,  95,  95,  95,
116
     95,  109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
117
     109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109},
118
    {0,   32,  32,  64,  64,  64,  96,  96,  96,  96,  127, 127, 127,
119
     127, 127, 127, 157, 157, 157, 157, 157, 157, 157, 157, 185, 185,
120
     185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185,
121
     185, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211,
122
     211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211},
123
    {0,   64,  64,  127, 127, 127, 188, 188, 188, 188, 246, 246, 246,
124
     246, 246, 246, 300, 300, 300, 300, 300, 300, 300, 300, 348, 348,
125
     348, 348, 348, 348, 348, 348, 348, 348, 348, 348, 348, 348, 348,
126
     348, 388, 388, 388, 388, 388, 388, 388, 388, 388, 388, 388, 388,
127
     388, 388, 388, 388, 388, 388, 388, 388, 388, 388, 388, 388}};
128
129
static const uint16_t kNumNonzeroContextSkip[kNumSchemes] = {8,   15,  31, 61,
130
                                                             120, 231, 412};
131
132
/**
133
 * Table that specifies, how context is calculated.
134
 *
135
 * Each value corresponds to DCT coefficient and is a sum of flags:
136
 *  - 1: context should be calculated using ACPredictContextRow
137
 *  - 2: context should be calculated using ACPredictContextCol
138
 */
139
static const uint8_t kContextAlgorithm[128] = {
140
    // JPEG XL layout
141
    0, 1, 1, 1, 1, 0, 0, 0,  //
142
    2, 3, 1, 1, 1, 0, 0, 0,  //
143
    2, 2, 0, 0, 0, 0, 0, 0,  //
144
    2, 2, 0, 0, 0, 0, 0, 0,  //
145
    2, 2, 0, 0, 0, 0, 0, 0,  //
146
    0, 0, 0, 0, 0, 0, 0, 0,  //
147
    0, 0, 0, 0, 0, 0, 0, 0,  //
148
    0, 0, 0, 0, 0, 0, 0, 0,
149
    // Legacy layout
150
    0, 1, 1, 1, 1, 1, 1, 1,  //
151
    2, 0, 0, 0, 0, 0, 0, 0,  //
152
    2, 0, 0, 0, 0, 0, 0, 0,  //
153
    2, 0, 0, 0, 0, 0, 0, 0,  //
154
    2, 0, 0, 0, 0, 0, 0, 0,  //
155
    2, 0, 0, 0, 0, 0, 0, 0,  //
156
    2, 0, 0, 0, 0, 0, 0, 0,  //
157
    2, 0, 0, 0, 0, 0, 0, 0,
158
};
159
160
inline uint16_t ZeroDensityContext(size_t nonzeros_left, size_t k,
161
515k
                                   size_t bits) {
162
515k
  return kNumNonzeroContext[bits][nonzeros_left] + kFreqContext[bits][k];
163
515k
}
164
165
// Returns the context for the absolute value of the prediction error of
166
// the next DC coefficient in column x, using the one row size ringbuffer of
167
// previous absolute prediction errors in vals.
168
93.3k
inline int WeightedAverageContextDC(const int* vals, int x) {
169
  // Since vals is a ringbuffer, vals[x] and vals[x + 1] refer to the
170
  // previous row.
171
93.3k
  int sum = 1 + vals[x - 2] + vals[x - 1] + vals[x] + vals[x + 1];
172
93.3k
  if ((sum >> kMaxAverageContext) != 0) {
173
1.90k
    return kMaxAverageContext;
174
1.90k
  }
175
91.4k
  return Log2FloorNonZero(sum);
176
93.3k
}
177
178
/**
179
 * Calculates the context on the base of average of already decoded
180
 * neighbour values.
181
 *
182
 * It is considered that vals[0] represents the value 2 rows above the current,
183
 * while the (locally) previous elements represent the current row. If y < 2,
184
 * then vals[0] should be 0.
185
 * Elements (locally) around vals[prev_row_delta] correspond to the row above
186
 * currnent one.
187
 *
188
 * Values are summed up with the following weights:
189
 *
190
 * 0|0|1|0
191
 * -+-+-+-
192
 * 0|1|2|1
193
 * -+-+-+-
194
 * 1|2|*|
195
 *     ^
196
 *     current position
197
 *
198
 * This method should not be invoked on the 0-th row or 0-th column.
199
 * It is also considered, that there are 2 extra fence columns before the 0-th
200
 * column and 1 fence column to the right of the last column,
201
 * all initialized with zeroes.
202
 */
203
429k
inline int WeightedAverageContext(const int* vals, int prev_row_delta) {
204
429k
  int sum = 4 + vals[0] + (vals[-kDCTBlockSize] + vals[prev_row_delta]) * 2 +
205
429k
            vals[-2 * kDCTBlockSize] + vals[prev_row_delta - kDCTBlockSize] +
206
429k
            vals[prev_row_delta + kDCTBlockSize];
207
429k
  if ((sum >> (kMaxAverageContext + 2)) != 0) {
208
4.29k
    return kMaxAverageContext;
209
4.29k
  }
210
425k
  return Log2FloorNonZero(sum) - 2;
211
429k
}
212
213
static const int kACPredictPrecisionBits = 13;
214
static const int kACPredictPrecision = 1 << kACPredictPrecisionBits;
215
216
void ComputeACPredictMultipliers(const int* quant, int* mult_row,
217
                                 int* mult_col);
218
219
// Computes average and sign context from the AC prediction.
220
69.0k
inline void ACPredictContext(int64_t p, size_t* avg_ctx, size_t* sgn) {
221
69.0k
  int multiplier;
222
69.0k
  if (p >= 0) {
223
42.1k
    multiplier = 1;
224
42.1k
  } else {
225
26.8k
    multiplier = -1;
226
26.8k
    p = -p;
227
26.8k
  }
228
69.0k
  size_t ctx;
229
69.0k
  if (p >= (1u << kMaxAverageContext)) {
230
3.03k
    ctx = kMaxAverageContext;
231
65.9k
  } else {
232
    // 0 -> 0, 1 -> 1, 2..3 -> 2, 4..7 -> 3, etc.
233
65.9k
    ctx = Log2FloorNonZero(2 * static_cast<uint32_t>(p)+ 1);
234
65.9k
  }
235
69.0k
  *avg_ctx = ctx;
236
69.0k
  *sgn = kMaxAverageContext + multiplier * ctx;
237
69.0k
}
238
239
inline void ACPredictContextCol(const coeff_t* prev, const coeff_t* cur,
240
32.8k
                                const int* mult, size_t* avg_ctx, size_t* sgn) {
241
32.8k
  coeff_t terms[8];
242
32.8k
  terms[0] = 0;
243
32.8k
  terms[1] = cur[1] + prev[1];
244
32.8k
  terms[2] = cur[2] - prev[2];
245
32.8k
  terms[3] = cur[3] + prev[3];
246
32.8k
  terms[4] = cur[4] - prev[4];
247
32.8k
  terms[5] = cur[5] + prev[5];
248
32.8k
  terms[6] = cur[6] - prev[6];
249
32.8k
  terms[7] = cur[7] + prev[7];
250
32.8k
  int64_t delta = terms[0] * static_cast<int64_t>(mult[0]) +
251
32.8k
                  terms[1] * static_cast<int64_t>(mult[1]) +
252
32.8k
                  terms[2] * static_cast<int64_t>(mult[2]) +
253
32.8k
                  terms[3] * static_cast<int64_t>(mult[3]) +
254
32.8k
                  terms[4] * static_cast<int64_t>(mult[4]) +
255
32.8k
                  terms[5] * static_cast<int64_t>(mult[5]) +
256
32.8k
                  terms[6] * static_cast<int64_t>(mult[6]) +
257
32.8k
                  terms[7] * static_cast<int64_t>(mult[7]);
258
32.8k
  ACPredictContext(prev[0] - delta / kACPredictPrecision, avg_ctx, sgn);
259
32.8k
}
260
261
inline void ACPredictContextRow(const coeff_t* prev, const coeff_t* cur,
262
36.1k
                               const int* mult, size_t* avg_ctx, size_t* sgn) {
263
36.1k
  coeff_t terms[8];
264
36.1k
  terms[0] = 0;
265
36.1k
  terms[1] = cur[8] + prev[8];
266
36.1k
  terms[2] = cur[16] - prev[16];
267
36.1k
  terms[3] = cur[24] + prev[24];
268
36.1k
  terms[4] = cur[32] - prev[32];
269
36.1k
  terms[5] = cur[40] + prev[40];
270
36.1k
  terms[6] = cur[48] - prev[48];
271
36.1k
  terms[7] = cur[56] + prev[56];
272
36.1k
  int64_t delta = terms[0] * static_cast<int64_t>(mult[0]) +
273
36.1k
                  terms[1] * static_cast<int64_t>(mult[1]) +
274
36.1k
                  terms[2] * static_cast<int64_t>(mult[2]) +
275
36.1k
                  terms[3] * static_cast<int64_t>(mult[3]) +
276
36.1k
                  terms[4] * static_cast<int64_t>(mult[4]) +
277
36.1k
                  terms[5] * static_cast<int64_t>(mult[5]) +
278
36.1k
                  terms[6] * static_cast<int64_t>(mult[6]) +
279
36.1k
                  terms[7] * static_cast<int64_t>(mult[7]);
280
36.1k
  ACPredictContext(prev[0] - delta / kACPredictPrecision, avg_ctx, sgn);
281
36.1k
}
282
283
/**
284
 * PRECONDITION: 0 <= prev[i] <= 63
285
 * PRECONDITION: elements of prev at and after x correspond to previous
286
 *               row; elements before x correspond to current row
287
 */
288
1.68M
inline uint8_t NumNonzerosContext(const uint8_t* prev, int x, int y) {
289
1.68M
  size_t prediction;
290
1.68M
  if (y == 0) {
291
146k
    if (x == 0) {
292
      // Special case: top-left block.
293
2.22k
      prediction = 0;
294
144k
    } else {
295
      // No row above; use block at left.
296
144k
      prediction = prev[x - 1];
297
144k
    }
298
1.53M
  } else if (x == 0) {
299
    // No column to the left; use block above.
300
20.4k
    prediction = prev[x];
301
1.51M
  } else {
302
    // Average of left and above blocks.
303
1.51M
    prediction = (prev[x - 1] + prev[x] + 1) / 2;
304
1.51M
  }
305
1.68M
  BRUNSLI_DCHECK(prediction <= kNumNonZeroTreeSize);
306
1.68M
  return static_cast<uint8_t>(prediction / kNumNonZeroQuant);
307
1.68M
}
308
309
// Context for the emptyness of a block is the number of non-empty blocks in the
310
// previous and up neighborhood (blocks beyond the border are assumed
311
// non-empty).
312
static const int kNumIsEmptyBlockContexts = 3;
313
20.8M
inline int IsEmptyBlockContext(const int* prev, int x) {
314
20.8M
  return prev[x - 1] + prev[x];
315
20.8M
}
316
317
// Holds all encoding/decoding state for an image component that is needed to
318
// switch between components during interleaved encoding/decoding.
319
struct ComponentStateDC {
320
  ComponentStateDC()
321
      : width(0),
322
        is_empty_block_prob(kNumIsEmptyBlockContexts),
323
        sign_prob(9),
324
4.30k
        first_extra_bit_prob(10) {
325
4.30k
    InitAll();
326
4.30k
  }
327
328
4.30k
  void SetWidth(int w) {
329
4.30k
    width = w;
330
4.30k
    prev_is_nonempty.resize(w + 1, 1);
331
4.30k
    prev_abs_coeff.resize(w + 3);
332
4.30k
    prev_sign.resize(w + 1);
333
4.30k
  }
334
335
  int width;
336
  Prob is_zero_prob;
337
  std::vector<Prob> is_empty_block_prob;
338
  std::vector<Prob> sign_prob;
339
  std::vector<Prob> first_extra_bit_prob;
340
  std::vector<int> prev_is_nonempty;
341
  std::vector<int> prev_abs_coeff;
342
  std::vector<int> prev_sign;
343
344
 protected:
345
  void InitAll();
346
};
347
348
struct ComponentState {
349
  ComponentState()
350
      : width(0),
351
        is_zero_prob(kNumNonzeroBuckets * kDCTBlockSize),
352
        sign_prob((2 * kMaxAverageContext + 1) * kDCTBlockSize),
353
3.88k
        first_extra_bit_prob(10 * kDCTBlockSize) {
354
3.88k
    InitAll();
355
3.88k
  }
356
357
3.88k
  void SetWidth(int w) {
358
3.88k
    width = w;
359
3.88k
    prev_is_nonempty.resize(w + 1, 1);
360
3.88k
    prev_num_nonzeros.resize(w);
361
3.88k
    prev_abs_coeff.resize(kDCTBlockSize * 2 * (w + 3));
362
3.88k
    prev_sign.resize(kDCTBlockSize * (w + 1));
363
3.88k
  }
364
365
  // Returns the size of the object after constructor and SetWidth(w).
366
  // Used in estimating peak heap memory usage of the brunsli codec.
367
0
  static size_t SizeInBytes(int w) {
368
0
    return (4 + (10 + 3 * w) * kDCTBlockSize + 2 * w) * sizeof(int) +
369
0
           ((kNumNonzeroBuckets + 2 * kMaxAverageContext + 11) * kDCTBlockSize +
370
0
            kNumNonZeroContextCount * kNumNonZeroTreeSize) *
371
0
               sizeof(Prob);
372
0
  }
373
374
  int width;
375
  int context_offset;
376
  uint32_t order[kDCTBlockSize];
377
  int mult_row[kDCTBlockSize];
378
  // mult_col is transposed for more effective ACPredictContextRow execution.
379
  int mult_col[kDCTBlockSize];
380
  std::vector<Prob> is_zero_prob;
381
  std::vector<Prob> sign_prob;
382
  Prob num_nonzero_prob[kNumNonZeroContextCount * kNumNonZeroTreeSize];
383
  std::vector<Prob> first_extra_bit_prob;
384
  std::vector<int> prev_is_nonempty;
385
  std::vector<uint8_t> prev_num_nonzeros;
386
  std::vector<int> prev_abs_coeff;
387
  std::vector<int> prev_sign;
388
389
 protected:
390
  void InitAll();
391
};
392
393
}  // namespace brunsli
394
395
#endif  // BRUNSLI_COMMON_CONTEXT_H_