Coverage Report

Created: 2025-07-11 06:10

/src/brunsli/c/common/constants.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_CONSTANTS_H_
8
#define BRUNSLI_COMMON_CONSTANTS_H_
9
10
#include <brunsli/jpeg_data.h>
11
#include <brunsli/types.h>
12
13
namespace brunsli {
14
15
// Refuse to allocate more than 1 GB of memory for the coefficients,
16
// that is 2M blocks x 64 coeffs x 2 bytes per coeff x max 4 components.
17
// TODO(eustas): this should be the minimal guaranteed limit, rather than
18
//               hard limit; 4GPx images should be eligible, if users would
19
//               want them.
20
static const size_t kBrunsliMaxNumBlocks = 1ull << 21;
21
22
// The maximum absolute value brunsli can encode is 2054 (8 values for direct
23
// codes and num bits from 1 to 10, so a total of 8 + 2 + 4 + ... + 1024).
24
static const int kBrunsliMaxDCAbsVal = 2054;
25
26
// We use only the context map alphabet in brunsli, where the maximum alphabet
27
// size is 256 + 16 = 272. (We can have 256 clusters and 16 run length codes).
28
static const size_t kMaxContextMapAlphabetSize = 272;
29
30
static const uint32_t kHuffmanTableBits = 8u;
31
static const size_t kMaxHuffmanBits = 15u;
32
33
// Total number of short markers allowed. Short marker represents popular marker
34
// and is encoded with 1 or 2 bytes in brunsli, but expanded into 15..3161 bytes
35
// in JPEG (see AppData_0x and GenerateApp0Marker). This poses "ZIP BOMB" threat
36
// because sequence of N bytes with value 0x80 will expand to 1580.5 * N bytes,
37
// but it could be compressed by brotli into just few bytes...
38
// On the other side, there is no reason to repeat any of those markers.
39
// Software that generates JPEG files might contain issues that would place
40
// repeated markers; to mitigate this, brunsli allows repetition of short
41
// markers, but sets the limit: the number of all unique marker variants.
42
static const int kBrunsliShortMarkerLimit = 0x40 + 3 * 0x100;
43
static const int kBrunsliMultibyteMarkerLimit = 0x400;
44
45
static const uint8_t kBrunsliWiringTypeVarint = 0x0;
46
static const uint8_t kBrunsliWiringTypeLengthDelimited = 0x2;
47
48
// The maximum supported V / H sampling factor.
49
static const int kBrunsliMaxSampling = 15;
50
51
0
constexpr uint8_t ValueMarker(uint8_t tag) {
52
0
  return (tag << 3) | kBrunsliWiringTypeVarint;
53
0
}
54
55
0
constexpr uint8_t SectionMarker(uint8_t tag) {
56
0
  return (tag << 3) | kBrunsliWiringTypeLengthDelimited;
57
0
}
58
59
static const uint8_t kBrunsliSignatureTag = 0x1;
60
static const uint8_t kBrunsliHeaderTag = 0x2;
61
static const uint8_t kBrunsliMetaDataTag = 0x3;
62
static const uint8_t kBrunsliJPEGInternalsTag = 0x4;
63
static const uint8_t kBrunsliQuantDataTag = 0x5;
64
static const uint8_t kBrunsliHistogramDataTag = 0x6;
65
static const uint8_t kBrunsliDCDataTag = 0x7;
66
static const uint8_t kBrunsliACDataTag = 0x8;
67
static const uint8_t kBrunsliOriginalJpgTag = 0x9;
68
69
// Header section. All fields are varints.
70
static const uint8_t kBrunsliHeaderWidthTag = 0x1;
71
static const uint8_t kBrunsliHeaderHeightTag = 0x2;
72
static const uint8_t kBrunsliHeaderVersionCompTag = 0x3;
73
static const uint8_t kBrunsliHeaderSubsamplingTag = 0x4;
74
75
static const size_t kBrunsliSignatureSize = 6;
76
extern const uint8_t kBrunsliSignature[kBrunsliSignatureSize];
77
78
static const size_t kMaxApp0Densities = 8;
79
static const uint16_t kApp0Densities[kMaxApp0Densities] = {
80
  1, 72, 96, 100, 150, 180, 240, 300
81
};
82
83
// TODO(eustas): reintroduce as a bit-length constant.
84
static const int kNumStockQuantTables = 8;
85
static const uint8_t
86
    kStockQuantizationTables[2][kNumStockQuantTables][64] = {
87
  {  // LUMA
88
    {  3,  2,  2,  3,  5,  8, 10, 12,
89
       2,  2,  3,  4,  5, 12, 12, 11,
90
       3,  3,  3,  5,  8, 11, 14, 11,
91
       3,  3,  4,  6, 10, 17, 16, 12,
92
       4,  4,  7, 11, 14, 22, 21, 15,
93
       5,  7, 11, 13, 16, 21, 23, 18,
94
      10, 13, 16, 17, 21, 24, 24, 20,
95
      14, 18, 19, 20, 22, 20, 21, 20 },
96
    {  8,  6,  5,  8, 12, 20, 26, 31,
97
       6,  6,  7, 10, 13, 29, 30, 28,
98
       7,  7,  8, 12, 20, 29, 35, 28,
99
       7,  9, 11, 15, 26, 44, 40, 31,
100
       9, 11, 19, 28, 34, 55, 52, 39,
101
      12, 18, 28, 32, 41, 52, 57, 46,
102
      25, 32, 39, 44, 52, 61, 60, 51,
103
      36, 46, 48, 49, 56, 50, 52, 50 },
104
    {  6,  4,  4,  6, 10, 16, 20, 24,
105
       5,  5,  6,  8, 10, 23, 24, 22,
106
       6,  5,  6, 10, 16, 23, 28, 22,
107
       6,  7,  9, 12, 20, 35, 32, 25,
108
       7,  9, 15, 22, 27, 44, 41, 31,
109
      10, 14, 22, 26, 32, 42, 45, 37,
110
      20, 26, 31, 35, 41, 48, 48, 40,
111
      29, 37, 38, 39, 45, 40, 41, 40 },
112
    {  5,  3,  3,  5,  7, 12, 15, 18,
113
       4,  4,  4,  6,  8, 17, 18, 17,
114
       4,  4,  5,  7, 12, 17, 21, 17,
115
       4,  5,  7,  9, 15, 26, 24, 19,
116
       5,  7, 11, 17, 20, 33, 31, 23,
117
       7, 11, 17, 19, 24, 31, 34, 28,
118
      15, 19, 23, 26, 31, 36, 36, 30,
119
      22, 28, 29, 29, 34, 30, 31, 30 },
120
    {  1,  1,  1,  1,  1,  1,  1,  1,
121
       1,  1,  1,  1,  1,  1,  1,  1,
122
       1,  1,  1,  1,  1,  1,  1,  1,
123
       1,  1,  1,  1,  1,  1,  1,  1,
124
       1,  1,  1,  1,  1,  1,  1,  1,
125
       1,  1,  1,  1,  1,  1,  1,  1,
126
       1,  1,  1,  1,  1,  1,  1,  1,
127
       1,  1,  1,  1,  1,  1,  1,  1 },
128
    {  2,  1,  1,  2,  2,  4,  5,  6,
129
       1,  1,  1,  2,  3,  6,  6,  6,
130
       1,  1,  2,  2,  4,  6,  7,  6,
131
       1,  2,  2,  3,  5,  9,  8,  6,
132
       2,  2,  4,  6,  7, 11, 10,  8,
133
       2,  4,  6,  6,  8, 10, 11,  9,
134
       5,  6,  8,  9, 10, 12, 12, 10,
135
       7,  9, 10, 10, 11, 10, 10, 10 },
136
    {  1,  1,  1,  1,  1,  1,  1,  1,
137
       1,  1,  1,  1,  1,  1,  1,  1,
138
       1,  1,  1,  1,  1,  1,  1,  2,
139
       1,  1,  1,  1,  1,  1,  2,  2,
140
       1,  1,  1,  1,  1,  2,  2,  3,
141
       1,  1,  1,  1,  2,  2,  3,  3,
142
       1,  1,  1,  2,  2,  3,  3,  3,
143
       1,  1,  2,  2,  3,  3,  3,  3 },
144
    { 10,  7,  6, 10, 14, 24, 31, 37,
145
       7,  7,  8, 11, 16, 35, 36, 33,
146
       8,  8, 10, 14, 24, 34, 41, 34,
147
       8, 10, 13, 17, 31, 52, 48, 37,
148
      11, 13, 22, 34, 41, 65, 62, 46,
149
      14, 21, 33, 38, 49, 62, 68, 55,
150
      29, 38, 47, 52, 62, 73, 72, 61,
151
      43, 55, 57, 59, 67, 60, 62, 59 }
152
  },
153
  {   // CHROMA
154
    {  9,  9,  9, 12, 11, 12, 24, 13,
155
      13, 24, 50, 33, 28, 33, 50, 50,
156
      50, 50, 50, 50, 50, 50, 50, 50,
157
      50, 50, 50, 50, 50, 50, 50, 50,
158
      50, 50, 50, 50, 50, 50, 50, 50,
159
      50, 50, 50, 50, 50, 50, 50, 50,
160
      50, 50, 50, 50, 50, 50, 50, 50,
161
      50, 50, 50, 50, 50, 50, 50, 50 },
162
    {  3,  4,  5,  9, 20, 20, 20, 20,
163
       4,  4,  5, 13, 20, 20, 20, 20,
164
       5,  5, 11, 20, 20, 20, 20, 20,
165
       9, 13, 20, 20, 20, 20, 20, 20,
166
      20, 20, 20, 20, 20, 20, 20, 20,
167
      20, 20, 20, 20, 20, 20, 20, 20,
168
      20, 20, 20, 20, 20, 20, 20, 20,
169
      20, 20, 20, 20, 20, 20, 20, 20 },
170
    {  9,  9, 12, 24, 50, 50, 50, 50,
171
       9, 11, 13, 33, 50, 50, 50, 50,
172
      12, 13, 28, 50, 50, 50, 50, 50,
173
      24, 33, 50, 50, 50, 50, 50, 50,
174
      50, 50, 50, 50, 50, 50, 50, 50,
175
      50, 50, 50, 50, 50, 50, 50, 50,
176
      50, 50, 50, 50, 50, 50, 50, 50,
177
      50, 50, 50, 50, 50, 50, 50, 50 },
178
    {  5,  5,  7, 14, 30, 30, 30, 30,
179
       5,  6,  8, 20, 30, 30, 30, 30,
180
       7,  8, 17, 30, 30, 30, 30, 30,
181
      14, 20, 30, 30, 30, 30, 30, 30,
182
      30, 30, 30, 30, 30, 30, 30, 30,
183
      30, 30, 30, 30, 30, 30, 30, 30,
184
      30, 30, 30, 30, 30, 30, 30, 30,
185
      30, 30, 30, 30, 30, 30, 30, 30 },
186
    {  7,  7, 10, 19, 40, 40, 40, 40,
187
       7,  8, 10, 26, 40, 40, 40, 40,
188
      10, 10, 22, 40, 40, 40, 40, 40,
189
      19, 26, 40, 40, 40, 40, 40, 40,
190
      40, 40, 40, 40, 40, 40, 40, 40,
191
      40, 40, 40, 40, 40, 40, 40, 40,
192
      40, 40, 40, 40, 40, 40, 40, 40,
193
      40, 40, 40, 40, 40, 40, 40, 40 },
194
    {  1,  1,  1,  1,  1,  1,  1,  1,
195
       1,  1,  1,  1,  1,  1,  1,  1,
196
       1,  1,  1,  1,  1,  1,  1,  1,
197
       1,  1,  1,  1,  1,  1,  1,  1,
198
       1,  1,  1,  1,  1,  1,  1,  1,
199
       1,  1,  1,  1,  1,  1,  1,  1,
200
       1,  1,  1,  1,  1,  1,  1,  1,
201
       1,  1,  1,  1,  1,  1,  1,  1 },
202
    {  2,  2,  2,  5, 10, 10, 10, 10,
203
       2,  2,  3,  7, 10, 10, 10, 10,
204
       2,  3,  6, 10, 10, 10, 10, 10,
205
       5,  7, 10, 10, 10, 10, 10, 10,
206
      10, 10, 10, 10, 10, 10, 10, 10,
207
      10, 10, 10, 10, 10, 10, 10, 10,
208
      10, 10, 10, 10, 10, 10, 10, 10,
209
      10, 10, 10, 10, 10, 10, 10, 10 },
210
    { 10, 11, 14, 28, 59, 59, 59, 59,
211
      11, 13, 16, 40, 59, 59, 59, 59,
212
      14, 16, 34, 59, 59, 59, 59, 59,
213
      28, 40, 59, 59, 59, 59, 59, 59,
214
      59, 59, 59, 59, 59, 59, 59, 59,
215
      59, 59, 59, 59, 59, 59, 59, 59,
216
      59, 59, 59, 59, 59, 59, 59, 59,
217
      59, 59, 59, 59, 59, 59, 59, 59 }
218
  }
219
};
220
221
// TODO(eustas): reintroduce with bit-length.
222
static const int kComponentIds123 = 0;
223
static const int kComponentIdsGray = 1;
224
static const int kComponentIdsRGB = 2;
225
static const int kComponentIdsCustom = 3;
226
227
// TODO(eustas): reintroduce as bit-length
228
static const int kNumStockDCHuffmanCodes = 2;
229
static const int kStockDCHuffmanCodeCounts[kNumStockDCHuffmanCodes][
230
    kJpegHuffmanMaxBitLength] = {
231
  { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, },
232
  { 0, 1, 5, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, },
233
};
234
// TODO(eustas): replace the final "256" with marker constant.
235
static const int kStockDCHuffmanCodeValues[kNumStockDCHuffmanCodes][
236
    kJpegDCAlphabetSize + 1] = {
237
  { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 256 },
238
  { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 256 },
239
};
240
241
// TODO(eustas): reintroduce as bit-length
242
static const int kNumStockACHuffmanCodes = 2;
243
static const int kStockACHuffmanCodeCounts[kNumStockACHuffmanCodes][
244
    kJpegHuffmanMaxBitLength] = {
245
  { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 126, },
246
  { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 120, },
247
};
248
static const int kStockACHuffmanCodeTotalCount = 163;
249
// TODO(eustas): replace the final "256" with marker constant.
250
static const int kStockACHuffmanCodeValues[kNumStockACHuffmanCodes][
251
    kStockACHuffmanCodeTotalCount] = {
252
  {
253
      1,   2,   3,   0,   4,  17,   5,  18,
254
     33,  49,  65,   6,  19,  81,  97,   7,
255
     34, 113,  20,  50, 129, 145, 161,   8,
256
     35,  66, 177, 193,  21,  82, 209, 240,
257
     36,  51,  98, 114, 130,   9,  10,  22,
258
     23,  24,  25,  26,  37,  38,  39,  40,
259
      41, 42,  52,  53,  54,  55,  56,  57,
260
     58,  67,  68,  69,  70,  71,  72,  73,
261
     74,  83,  84,  85,  86,  87,  88,  89,
262
     90,  99, 100, 101, 102, 103, 104, 105,
263
    106, 115, 116, 117, 118, 119, 120, 121,
264
    122, 131, 132, 133, 134, 135, 136, 137,
265
    138, 146, 147, 148, 149, 150, 151, 152,
266
    153, 154, 162, 163, 164, 165, 166, 167,
267
    168, 169, 170, 178, 179, 180, 181, 182,
268
    183, 184, 185, 186, 194, 195, 196, 197,
269
    198, 199, 200, 201, 202, 210, 211, 212,
270
    213, 214, 215, 216, 217, 218, 225, 226,
271
    227, 228, 229, 230, 231, 232, 233, 234,
272
    241, 242, 243, 244, 245, 246, 247, 248,
273
    249, 250, 256,
274
  },
275
  {
276
      0,   1,   2,   3,  17,   4,   5,  33,
277
     49,   6,  18,  65,  81,   7,  97, 113,
278
     19,  34,  50, 129,   8,  20,  66, 145,
279
    161, 177, 193,   9,  35,  51,  82, 240,
280
     21,  98, 114, 209,  10,  22,  36,  52,
281
    225,  37, 241,  23,  24,  25,  26,  38,
282
     39,  40,  41,  42,  53,  54,  55,  56,
283
     57,  58,  67,  68,  69,  70,  71,  72,
284
     73,  74,  83,  84,  85,  86,  87,  88,
285
     89,  90,  99, 100, 101, 102, 103, 104,
286
    105, 106, 115, 116, 117, 118, 119, 120,
287
    121, 122, 130, 131, 132, 133, 134, 135,
288
    136, 137, 138, 146, 147, 148, 149, 150,
289
    151, 152, 153, 154, 162, 163, 164, 165,
290
    166, 167, 168, 169, 170, 178, 179, 180,
291
    181, 182, 183, 184, 185, 186, 194, 195,
292
    196, 197, 198, 199, 200, 201, 202, 210,
293
    211, 212, 213, 214, 215, 216, 217, 218,
294
    226, 227, 228, 229, 230, 231, 232, 233,
295
    234, 242, 243, 244, 245, 246, 247, 248,
296
    249, 250, 256,
297
  },
298
};
299
300
// Pre-defined tables for PermutationCoder.
301
static const uint8_t kDefaultDCValues[16] = {
302
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
303
};
304
static const uint8_t kDefaultACValues[256] = {
305
  1, 0, 2, 3, 17, 4, 5, 33, 18, 49, 65, 6, 81, 19, 97, 7,
306
  34, 113, 50, 129, 20, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240,
307
  36, 51, 98, 114, 9, 130, 10, 22, 52, 225, 23, 37, 241, 24, 25, 26,
308
  38, 39, 40, 41, 42, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71,
309
  72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103,
310
  104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 131, 132, 133, 134,
311
  135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163,
312
  164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185,
313
  186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214,
314
  215, 216, 217, 218, 226, 227, 228, 229, 230, 231, 232, 233, 234, 242, 243,
315
  244, 245, 246, 247, 248, 249, 250, 16, 32, 48, 64, 80, 96, 112, 128, 144,
316
  160, 176, 192, 208,
317
  // extra fill-in entries for missing values
318
  11, 12, 13, 14, 15, 27, 28, 29, 30, 31, 43, 44, 45, 46, 47, 59, 60,
319
  61, 62, 63, 75, 76, 77, 78, 79, 91, 92, 93, 94, 95, 107, 108, 109,
320
  110, 111, 123, 124, 125, 126, 127, 139, 140, 141, 142, 143, 155, 156,
321
  157, 158, 159, 171, 172, 173, 174, 175, 187, 188, 189, 190, 191, 203,
322
  204, 205, 206, 207, 219, 220, 221, 222, 223, 224, 235, 236, 237, 238,
323
  239, 251, 252, 253, 254, 255
324
};
325
326
// Common app-data chunks
327
extern const uint8_t AppData_0xe0[17];
328
extern const uint8_t AppData_0xe2[3161];  // special byte at offset 84
329
extern const uint8_t AppData_0xec[18];    // special byte at offset 15
330
extern const uint8_t AppData_0xee[15];    // special byte at offset 10
331
332
}  // namespace brunsli
333
334
#endif  // BRUNSLI_COMMON_CONSTANTS_H_