/src/libjxl/third_party/brotli/c/enc/command.h
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 | | /* This class models a sequence of literals and a backward reference copy. */ |
8 | | |
9 | | #ifndef BROTLI_ENC_COMMAND_H_ |
10 | | #define BROTLI_ENC_COMMAND_H_ |
11 | | |
12 | | #include <brotli/types.h> |
13 | | |
14 | | #include "../common/constants.h" |
15 | | #include "../common/platform.h" |
16 | | #include "fast_log.h" |
17 | | #include "params.h" |
18 | | #include "prefix.h" |
19 | | |
20 | | #if defined(__cplusplus) || defined(c_plusplus) |
21 | | extern "C" { |
22 | | #endif |
23 | | |
24 | | BROTLI_INTERNAL extern const uint32_t |
25 | | kBrotliInsBase[BROTLI_NUM_INS_COPY_CODES]; |
26 | | BROTLI_INTERNAL extern const uint32_t |
27 | | kBrotliInsExtra[BROTLI_NUM_INS_COPY_CODES]; |
28 | | BROTLI_INTERNAL extern const uint32_t |
29 | | kBrotliCopyBase[BROTLI_NUM_INS_COPY_CODES]; |
30 | | BROTLI_INTERNAL extern const uint32_t |
31 | | kBrotliCopyExtra[BROTLI_NUM_INS_COPY_CODES]; |
32 | | |
33 | 0 | static BROTLI_INLINE uint16_t GetInsertLengthCode(size_t insertlen) { |
34 | 0 | if (insertlen < 6) { |
35 | 0 | return (uint16_t)insertlen; |
36 | 0 | } else if (insertlen < 130) { |
37 | 0 | uint32_t nbits = Log2FloorNonZero(insertlen - 2) - 1u; |
38 | 0 | return (uint16_t)((nbits << 1) + ((insertlen - 2) >> nbits) + 2); |
39 | 0 | } else if (insertlen < 2114) { |
40 | 0 | return (uint16_t)(Log2FloorNonZero(insertlen - 66) + 10); |
41 | 0 | } else if (insertlen < 6210) { |
42 | 0 | return 21u; |
43 | 0 | } else if (insertlen < 22594) { |
44 | 0 | return 22u; |
45 | 0 | } else { |
46 | 0 | return 23u; |
47 | 0 | } |
48 | 0 | } Unexecuted instantiation: encode.c:GetInsertLengthCode Unexecuted instantiation: metablock.c:GetInsertLengthCode Unexecuted instantiation: block_splitter.c:GetInsertLengthCode Unexecuted instantiation: bit_cost.c:GetInsertLengthCode Unexecuted instantiation: cluster.c:GetInsertLengthCode Unexecuted instantiation: histogram.c:GetInsertLengthCode Unexecuted instantiation: compress_fragment_two_pass.c:GetInsertLengthCode Unexecuted instantiation: brotli_bit_stream.c:GetInsertLengthCode Unexecuted instantiation: command.c:GetInsertLengthCode Unexecuted instantiation: compress_fragment.c:GetInsertLengthCode Unexecuted instantiation: backward_references_hq.c:GetInsertLengthCode Unexecuted instantiation: backward_references.c:GetInsertLengthCode |
49 | | |
50 | 0 | static BROTLI_INLINE uint16_t GetCopyLengthCode(size_t copylen) { |
51 | 0 | if (copylen < 10) { |
52 | 0 | return (uint16_t)(copylen - 2); |
53 | 0 | } else if (copylen < 134) { |
54 | 0 | uint32_t nbits = Log2FloorNonZero(copylen - 6) - 1u; |
55 | 0 | return (uint16_t)((nbits << 1) + ((copylen - 6) >> nbits) + 4); |
56 | 0 | } else if (copylen < 2118) { |
57 | 0 | return (uint16_t)(Log2FloorNonZero(copylen - 70) + 12); |
58 | 0 | } else { |
59 | 0 | return 23u; |
60 | 0 | } |
61 | 0 | } Unexecuted instantiation: encode.c:GetCopyLengthCode Unexecuted instantiation: metablock.c:GetCopyLengthCode Unexecuted instantiation: block_splitter.c:GetCopyLengthCode Unexecuted instantiation: bit_cost.c:GetCopyLengthCode Unexecuted instantiation: cluster.c:GetCopyLengthCode Unexecuted instantiation: histogram.c:GetCopyLengthCode Unexecuted instantiation: compress_fragment_two_pass.c:GetCopyLengthCode Unexecuted instantiation: brotli_bit_stream.c:GetCopyLengthCode Unexecuted instantiation: command.c:GetCopyLengthCode Unexecuted instantiation: compress_fragment.c:GetCopyLengthCode Unexecuted instantiation: backward_references_hq.c:GetCopyLengthCode Unexecuted instantiation: backward_references.c:GetCopyLengthCode |
62 | | |
63 | | static BROTLI_INLINE uint16_t CombineLengthCodes( |
64 | 0 | uint16_t inscode, uint16_t copycode, BROTLI_BOOL use_last_distance) { |
65 | 0 | uint16_t bits64 = |
66 | 0 | (uint16_t)((copycode & 0x7u) | ((inscode & 0x7u) << 3u)); |
67 | 0 | if (use_last_distance && inscode < 8u && copycode < 16u) { |
68 | 0 | return (copycode < 8u) ? bits64 : (bits64 | 64u); |
69 | 0 | } else { |
70 | | /* Specification: 5 Encoding of ... (last table) */ |
71 | | /* offset = 2 * index, where index is in range [0..8] */ |
72 | 0 | uint32_t offset = 2u * ((copycode >> 3u) + 3u * (inscode >> 3u)); |
73 | | /* All values in specification are K * 64, |
74 | | where K = [2, 3, 6, 4, 5, 8, 7, 9, 10], |
75 | | i + 1 = [1, 2, 3, 4, 5, 6, 7, 8, 9], |
76 | | K - i - 1 = [1, 1, 3, 0, 0, 2, 0, 1, 2] = D. |
77 | | All values in D require only 2 bits to encode. |
78 | | Magic constant is shifted 6 bits left, to avoid final multiplication. */ |
79 | 0 | offset = (offset << 5u) + 0x40u + ((0x520D40u >> offset) & 0xC0u); |
80 | 0 | return (uint16_t)(offset | bits64); |
81 | 0 | } |
82 | 0 | } Unexecuted instantiation: encode.c:CombineLengthCodes Unexecuted instantiation: metablock.c:CombineLengthCodes Unexecuted instantiation: block_splitter.c:CombineLengthCodes Unexecuted instantiation: bit_cost.c:CombineLengthCodes Unexecuted instantiation: cluster.c:CombineLengthCodes Unexecuted instantiation: histogram.c:CombineLengthCodes Unexecuted instantiation: compress_fragment_two_pass.c:CombineLengthCodes Unexecuted instantiation: brotli_bit_stream.c:CombineLengthCodes Unexecuted instantiation: command.c:CombineLengthCodes Unexecuted instantiation: compress_fragment.c:CombineLengthCodes Unexecuted instantiation: backward_references_hq.c:CombineLengthCodes Unexecuted instantiation: backward_references.c:CombineLengthCodes |
83 | | |
84 | | static BROTLI_INLINE void GetLengthCode(size_t insertlen, size_t copylen, |
85 | | BROTLI_BOOL use_last_distance, |
86 | 0 | uint16_t* code) { |
87 | 0 | uint16_t inscode = GetInsertLengthCode(insertlen); |
88 | 0 | uint16_t copycode = GetCopyLengthCode(copylen); |
89 | 0 | *code = CombineLengthCodes(inscode, copycode, use_last_distance); |
90 | 0 | } Unexecuted instantiation: encode.c:GetLengthCode Unexecuted instantiation: metablock.c:GetLengthCode Unexecuted instantiation: block_splitter.c:GetLengthCode Unexecuted instantiation: bit_cost.c:GetLengthCode Unexecuted instantiation: cluster.c:GetLengthCode Unexecuted instantiation: histogram.c:GetLengthCode Unexecuted instantiation: compress_fragment_two_pass.c:GetLengthCode Unexecuted instantiation: brotli_bit_stream.c:GetLengthCode Unexecuted instantiation: command.c:GetLengthCode Unexecuted instantiation: compress_fragment.c:GetLengthCode Unexecuted instantiation: backward_references_hq.c:GetLengthCode Unexecuted instantiation: backward_references.c:GetLengthCode |
91 | | |
92 | 0 | static BROTLI_INLINE uint32_t GetInsertBase(uint16_t inscode) { |
93 | 0 | return kBrotliInsBase[inscode]; |
94 | 0 | } Unexecuted instantiation: encode.c:GetInsertBase Unexecuted instantiation: metablock.c:GetInsertBase Unexecuted instantiation: block_splitter.c:GetInsertBase Unexecuted instantiation: bit_cost.c:GetInsertBase Unexecuted instantiation: cluster.c:GetInsertBase Unexecuted instantiation: histogram.c:GetInsertBase Unexecuted instantiation: compress_fragment_two_pass.c:GetInsertBase Unexecuted instantiation: brotli_bit_stream.c:GetInsertBase Unexecuted instantiation: command.c:GetInsertBase Unexecuted instantiation: compress_fragment.c:GetInsertBase Unexecuted instantiation: backward_references_hq.c:GetInsertBase Unexecuted instantiation: backward_references.c:GetInsertBase |
95 | | |
96 | 0 | static BROTLI_INLINE uint32_t GetInsertExtra(uint16_t inscode) { |
97 | 0 | return kBrotliInsExtra[inscode]; |
98 | 0 | } Unexecuted instantiation: encode.c:GetInsertExtra Unexecuted instantiation: metablock.c:GetInsertExtra Unexecuted instantiation: block_splitter.c:GetInsertExtra Unexecuted instantiation: bit_cost.c:GetInsertExtra Unexecuted instantiation: cluster.c:GetInsertExtra Unexecuted instantiation: histogram.c:GetInsertExtra Unexecuted instantiation: compress_fragment_two_pass.c:GetInsertExtra Unexecuted instantiation: brotli_bit_stream.c:GetInsertExtra Unexecuted instantiation: command.c:GetInsertExtra Unexecuted instantiation: compress_fragment.c:GetInsertExtra Unexecuted instantiation: backward_references_hq.c:GetInsertExtra Unexecuted instantiation: backward_references.c:GetInsertExtra |
99 | | |
100 | 0 | static BROTLI_INLINE uint32_t GetCopyBase(uint16_t copycode) { |
101 | 0 | return kBrotliCopyBase[copycode]; |
102 | 0 | } Unexecuted instantiation: encode.c:GetCopyBase Unexecuted instantiation: metablock.c:GetCopyBase Unexecuted instantiation: block_splitter.c:GetCopyBase Unexecuted instantiation: bit_cost.c:GetCopyBase Unexecuted instantiation: cluster.c:GetCopyBase Unexecuted instantiation: histogram.c:GetCopyBase Unexecuted instantiation: compress_fragment_two_pass.c:GetCopyBase Unexecuted instantiation: brotli_bit_stream.c:GetCopyBase Unexecuted instantiation: command.c:GetCopyBase Unexecuted instantiation: compress_fragment.c:GetCopyBase Unexecuted instantiation: backward_references_hq.c:GetCopyBase Unexecuted instantiation: backward_references.c:GetCopyBase |
103 | | |
104 | 0 | static BROTLI_INLINE uint32_t GetCopyExtra(uint16_t copycode) { |
105 | 0 | return kBrotliCopyExtra[copycode]; |
106 | 0 | } Unexecuted instantiation: encode.c:GetCopyExtra Unexecuted instantiation: metablock.c:GetCopyExtra Unexecuted instantiation: block_splitter.c:GetCopyExtra Unexecuted instantiation: bit_cost.c:GetCopyExtra Unexecuted instantiation: cluster.c:GetCopyExtra Unexecuted instantiation: histogram.c:GetCopyExtra Unexecuted instantiation: compress_fragment_two_pass.c:GetCopyExtra Unexecuted instantiation: brotli_bit_stream.c:GetCopyExtra Unexecuted instantiation: command.c:GetCopyExtra Unexecuted instantiation: compress_fragment.c:GetCopyExtra Unexecuted instantiation: backward_references_hq.c:GetCopyExtra Unexecuted instantiation: backward_references.c:GetCopyExtra |
107 | | |
108 | | typedef struct Command { |
109 | | uint32_t insert_len_; |
110 | | /* Stores copy_len in low 25 bits and copy_code - copy_len in high 7 bit. */ |
111 | | uint32_t copy_len_; |
112 | | /* Stores distance extra bits. */ |
113 | | uint32_t dist_extra_; |
114 | | uint16_t cmd_prefix_; |
115 | | /* Stores distance code in low 10 bits |
116 | | and number of extra bits in high 6 bits. */ |
117 | | uint16_t dist_prefix_; |
118 | | } Command; |
119 | | |
120 | | /* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */ |
121 | | static BROTLI_INLINE void InitCommand(Command* self, |
122 | | const BrotliDistanceParams* dist, size_t insertlen, |
123 | 0 | size_t copylen, int copylen_code_delta, size_t distance_code) { |
124 | | /* Don't rely on signed int representation, use honest casts. */ |
125 | 0 | uint32_t delta = (uint8_t)((int8_t)copylen_code_delta); |
126 | 0 | self->insert_len_ = (uint32_t)insertlen; |
127 | 0 | self->copy_len_ = (uint32_t)(copylen | (delta << 25)); |
128 | | /* The distance prefix and extra bits are stored in this Command as if |
129 | | npostfix and ndirect were 0, they are only recomputed later after the |
130 | | clustering if needed. */ |
131 | 0 | PrefixEncodeCopyDistance( |
132 | 0 | distance_code, dist->num_direct_distance_codes, |
133 | 0 | dist->distance_postfix_bits, &self->dist_prefix_, &self->dist_extra_); |
134 | 0 | GetLengthCode( |
135 | 0 | insertlen, (size_t)((int)copylen + copylen_code_delta), |
136 | 0 | TO_BROTLI_BOOL((self->dist_prefix_ & 0x3FF) == 0), &self->cmd_prefix_); |
137 | 0 | } Unexecuted instantiation: encode.c:InitCommand Unexecuted instantiation: metablock.c:InitCommand Unexecuted instantiation: block_splitter.c:InitCommand Unexecuted instantiation: bit_cost.c:InitCommand Unexecuted instantiation: cluster.c:InitCommand Unexecuted instantiation: histogram.c:InitCommand Unexecuted instantiation: compress_fragment_two_pass.c:InitCommand Unexecuted instantiation: brotli_bit_stream.c:InitCommand Unexecuted instantiation: command.c:InitCommand Unexecuted instantiation: compress_fragment.c:InitCommand Unexecuted instantiation: backward_references_hq.c:InitCommand Unexecuted instantiation: backward_references.c:InitCommand |
138 | | |
139 | 0 | static BROTLI_INLINE void InitInsertCommand(Command* self, size_t insertlen) { |
140 | 0 | self->insert_len_ = (uint32_t)insertlen; |
141 | 0 | self->copy_len_ = 4 << 25; |
142 | 0 | self->dist_extra_ = 0; |
143 | 0 | self->dist_prefix_ = BROTLI_NUM_DISTANCE_SHORT_CODES; |
144 | 0 | GetLengthCode(insertlen, 4, BROTLI_FALSE, &self->cmd_prefix_); |
145 | 0 | } Unexecuted instantiation: encode.c:InitInsertCommand Unexecuted instantiation: metablock.c:InitInsertCommand Unexecuted instantiation: block_splitter.c:InitInsertCommand Unexecuted instantiation: bit_cost.c:InitInsertCommand Unexecuted instantiation: cluster.c:InitInsertCommand Unexecuted instantiation: histogram.c:InitInsertCommand Unexecuted instantiation: compress_fragment_two_pass.c:InitInsertCommand Unexecuted instantiation: brotli_bit_stream.c:InitInsertCommand Unexecuted instantiation: command.c:InitInsertCommand Unexecuted instantiation: compress_fragment.c:InitInsertCommand Unexecuted instantiation: backward_references_hq.c:InitInsertCommand Unexecuted instantiation: backward_references.c:InitInsertCommand |
146 | | |
147 | | static BROTLI_INLINE uint32_t CommandRestoreDistanceCode( |
148 | 0 | const Command* self, const BrotliDistanceParams* dist) { |
149 | 0 | if ((self->dist_prefix_ & 0x3FFu) < |
150 | 0 | BROTLI_NUM_DISTANCE_SHORT_CODES + dist->num_direct_distance_codes) { |
151 | 0 | return self->dist_prefix_ & 0x3FFu; |
152 | 0 | } else { |
153 | 0 | uint32_t dcode = self->dist_prefix_ & 0x3FFu; |
154 | 0 | uint32_t nbits = self->dist_prefix_ >> 10; |
155 | 0 | uint32_t extra = self->dist_extra_; |
156 | 0 | uint32_t postfix_mask = (1U << dist->distance_postfix_bits) - 1U; |
157 | 0 | uint32_t hcode = (dcode - dist->num_direct_distance_codes - |
158 | 0 | BROTLI_NUM_DISTANCE_SHORT_CODES) >> |
159 | 0 | dist->distance_postfix_bits; |
160 | 0 | uint32_t lcode = (dcode - dist->num_direct_distance_codes - |
161 | 0 | BROTLI_NUM_DISTANCE_SHORT_CODES) & postfix_mask; |
162 | 0 | uint32_t offset = ((2U + (hcode & 1U)) << nbits) - 4U; |
163 | 0 | return ((offset + extra) << dist->distance_postfix_bits) + lcode + |
164 | 0 | dist->num_direct_distance_codes + BROTLI_NUM_DISTANCE_SHORT_CODES; |
165 | 0 | } |
166 | 0 | } Unexecuted instantiation: encode.c:CommandRestoreDistanceCode Unexecuted instantiation: metablock.c:CommandRestoreDistanceCode Unexecuted instantiation: block_splitter.c:CommandRestoreDistanceCode Unexecuted instantiation: bit_cost.c:CommandRestoreDistanceCode Unexecuted instantiation: cluster.c:CommandRestoreDistanceCode Unexecuted instantiation: histogram.c:CommandRestoreDistanceCode Unexecuted instantiation: compress_fragment_two_pass.c:CommandRestoreDistanceCode Unexecuted instantiation: brotli_bit_stream.c:CommandRestoreDistanceCode Unexecuted instantiation: command.c:CommandRestoreDistanceCode Unexecuted instantiation: compress_fragment.c:CommandRestoreDistanceCode Unexecuted instantiation: backward_references_hq.c:CommandRestoreDistanceCode Unexecuted instantiation: backward_references.c:CommandRestoreDistanceCode |
167 | | |
168 | 0 | static BROTLI_INLINE uint32_t CommandDistanceContext(const Command* self) { |
169 | 0 | uint32_t r = self->cmd_prefix_ >> 6; |
170 | 0 | uint32_t c = self->cmd_prefix_ & 7; |
171 | 0 | if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) { |
172 | 0 | return c; |
173 | 0 | } |
174 | 0 | return 3; |
175 | 0 | } Unexecuted instantiation: encode.c:CommandDistanceContext Unexecuted instantiation: metablock.c:CommandDistanceContext Unexecuted instantiation: block_splitter.c:CommandDistanceContext Unexecuted instantiation: bit_cost.c:CommandDistanceContext Unexecuted instantiation: cluster.c:CommandDistanceContext Unexecuted instantiation: histogram.c:CommandDistanceContext Unexecuted instantiation: compress_fragment_two_pass.c:CommandDistanceContext Unexecuted instantiation: brotli_bit_stream.c:CommandDistanceContext Unexecuted instantiation: command.c:CommandDistanceContext Unexecuted instantiation: compress_fragment.c:CommandDistanceContext Unexecuted instantiation: backward_references_hq.c:CommandDistanceContext Unexecuted instantiation: backward_references.c:CommandDistanceContext |
176 | | |
177 | 0 | static BROTLI_INLINE uint32_t CommandCopyLen(const Command* self) { |
178 | 0 | return self->copy_len_ & 0x1FFFFFF; |
179 | 0 | } Unexecuted instantiation: encode.c:CommandCopyLen Unexecuted instantiation: metablock.c:CommandCopyLen Unexecuted instantiation: block_splitter.c:CommandCopyLen Unexecuted instantiation: bit_cost.c:CommandCopyLen Unexecuted instantiation: cluster.c:CommandCopyLen Unexecuted instantiation: histogram.c:CommandCopyLen Unexecuted instantiation: compress_fragment_two_pass.c:CommandCopyLen Unexecuted instantiation: brotli_bit_stream.c:CommandCopyLen Unexecuted instantiation: command.c:CommandCopyLen Unexecuted instantiation: compress_fragment.c:CommandCopyLen Unexecuted instantiation: backward_references_hq.c:CommandCopyLen Unexecuted instantiation: backward_references.c:CommandCopyLen |
180 | | |
181 | 0 | static BROTLI_INLINE uint32_t CommandCopyLenCode(const Command* self) { |
182 | 0 | uint32_t modifier = self->copy_len_ >> 25; |
183 | 0 | int32_t delta = (int8_t)((uint8_t)(modifier | ((modifier & 0x40) << 1))); |
184 | 0 | return (uint32_t)((int32_t)(self->copy_len_ & 0x1FFFFFF) + delta); |
185 | 0 | } Unexecuted instantiation: encode.c:CommandCopyLenCode Unexecuted instantiation: metablock.c:CommandCopyLenCode Unexecuted instantiation: block_splitter.c:CommandCopyLenCode Unexecuted instantiation: bit_cost.c:CommandCopyLenCode Unexecuted instantiation: cluster.c:CommandCopyLenCode Unexecuted instantiation: histogram.c:CommandCopyLenCode Unexecuted instantiation: compress_fragment_two_pass.c:CommandCopyLenCode Unexecuted instantiation: brotli_bit_stream.c:CommandCopyLenCode Unexecuted instantiation: command.c:CommandCopyLenCode Unexecuted instantiation: compress_fragment.c:CommandCopyLenCode Unexecuted instantiation: backward_references_hq.c:CommandCopyLenCode Unexecuted instantiation: backward_references.c:CommandCopyLenCode |
186 | | |
187 | | #if defined(__cplusplus) || defined(c_plusplus) |
188 | | } /* extern "C" */ |
189 | | #endif |
190 | | |
191 | | #endif /* BROTLI_ENC_COMMAND_H_ */ |