]>
Commit | Line | Data |
---|---|---|
11b7501a SB |
1 | /* Copyright 2013 Google Inc. All Rights Reserved.\r |
2 | \r | |
3 | Distributed under MIT license.\r | |
4 | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT\r | |
5 | */\r | |
6 | \r | |
7 | /* This class models a sequence of literals and a backward reference copy. */\r | |
8 | \r | |
9 | #ifndef BROTLI_ENC_COMMAND_H_\r | |
10 | #define BROTLI_ENC_COMMAND_H_\r | |
11 | \r | |
12 | #include "../common/types.h"\r | |
13 | #include "../common/port.h"\r | |
14 | #include "./fast_log.h"\r | |
15 | #include "./prefix.h"\r | |
16 | \r | |
17 | #if defined(__cplusplus) || defined(c_plusplus)\r | |
18 | extern "C" {\r | |
19 | #endif\r | |
20 | \r | |
21 | static uint32_t kInsBase[] = { 0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26, 34, 50,\r | |
22 | 66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594 };\r | |
23 | static uint32_t kInsExtra[] = { 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,\r | |
24 | 5, 5, 6, 7, 8, 9, 10, 12, 14, 24 };\r | |
25 | static uint32_t kCopyBase[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 18, 22, 30,\r | |
26 | 38, 54, 70, 102, 134, 198, 326, 582, 1094, 2118 };\r | |
27 | static uint32_t kCopyExtra[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,\r | |
28 | 4, 4, 5, 5, 6, 7, 8, 9, 10, 24 };\r | |
29 | \r | |
30 | static BROTLI_INLINE uint16_t GetInsertLengthCode(size_t insertlen) {\r | |
31 | if (insertlen < 6) {\r | |
32 | return (uint16_t)insertlen;\r | |
33 | } else if (insertlen < 130) {\r | |
34 | uint32_t nbits = Log2FloorNonZero(insertlen - 2) - 1u;\r | |
35 | return (uint16_t)((nbits << 1) + ((insertlen - 2) >> nbits) + 2);\r | |
36 | } else if (insertlen < 2114) {\r | |
37 | return (uint16_t)(Log2FloorNonZero(insertlen - 66) + 10);\r | |
38 | } else if (insertlen < 6210) {\r | |
39 | return 21u;\r | |
40 | } else if (insertlen < 22594) {\r | |
41 | return 22u;\r | |
42 | } else {\r | |
43 | return 23u;\r | |
44 | }\r | |
45 | }\r | |
46 | \r | |
47 | static BROTLI_INLINE uint16_t GetCopyLengthCode(size_t copylen) {\r | |
48 | if (copylen < 10) {\r | |
49 | return (uint16_t)(copylen - 2);\r | |
50 | } else if (copylen < 134) {\r | |
51 | uint32_t nbits = Log2FloorNonZero(copylen - 6) - 1u;\r | |
52 | return (uint16_t)((nbits << 1) + ((copylen - 6) >> nbits) + 4);\r | |
53 | } else if (copylen < 2118) {\r | |
54 | return (uint16_t)(Log2FloorNonZero(copylen - 70) + 12);\r | |
55 | } else {\r | |
56 | return 23u;\r | |
57 | }\r | |
58 | }\r | |
59 | \r | |
60 | static BROTLI_INLINE uint16_t CombineLengthCodes(\r | |
61 | uint16_t inscode, uint16_t copycode, BROTLI_BOOL use_last_distance) {\r | |
62 | uint16_t bits64 =\r | |
63 | (uint16_t)((copycode & 0x7u) | ((inscode & 0x7u) << 3));\r | |
64 | if (use_last_distance && inscode < 8 && copycode < 16) {\r | |
65 | return (copycode < 8) ? bits64 : (bits64 | 64);\r | |
66 | } else {\r | |
67 | /* "To convert an insert-and-copy length code to an insert length code and\r | |
68 | a copy length code, the following table can be used" */\r | |
69 | static const uint16_t cells[9] = { 128u, 192u, 384u, 256u, 320u, 512u,\r | |
70 | 448u, 576u, 640u };\r | |
71 | return cells[(copycode >> 3) + 3 * (inscode >> 3)] | bits64;\r | |
72 | }\r | |
73 | }\r | |
74 | \r | |
75 | static BROTLI_INLINE void GetLengthCode(size_t insertlen, size_t copylen,\r | |
76 | BROTLI_BOOL use_last_distance,\r | |
77 | uint16_t* code) {\r | |
78 | uint16_t inscode = GetInsertLengthCode(insertlen);\r | |
79 | uint16_t copycode = GetCopyLengthCode(copylen);\r | |
80 | *code = CombineLengthCodes(inscode, copycode, use_last_distance);\r | |
81 | }\r | |
82 | \r | |
83 | static BROTLI_INLINE uint32_t GetInsertBase(uint16_t inscode) {\r | |
84 | return kInsBase[inscode];\r | |
85 | }\r | |
86 | \r | |
87 | static BROTLI_INLINE uint32_t GetInsertExtra(uint16_t inscode) {\r | |
88 | return kInsExtra[inscode];\r | |
89 | }\r | |
90 | \r | |
91 | static BROTLI_INLINE uint32_t GetCopyBase(uint16_t copycode) {\r | |
92 | return kCopyBase[copycode];\r | |
93 | }\r | |
94 | \r | |
95 | static BROTLI_INLINE uint32_t GetCopyExtra(uint16_t copycode) {\r | |
96 | return kCopyExtra[copycode];\r | |
97 | }\r | |
98 | \r | |
99 | typedef struct Command {\r | |
100 | uint32_t insert_len_;\r | |
101 | /* Stores copy_len in low 24 bits and copy_len XOR copy_code in high 8 bit. */\r | |
102 | uint32_t copy_len_;\r | |
103 | uint32_t dist_extra_;\r | |
104 | uint16_t cmd_prefix_;\r | |
105 | uint16_t dist_prefix_;\r | |
106 | } Command;\r | |
107 | \r | |
108 | /* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */\r | |
109 | static BROTLI_INLINE void InitCommand(Command* self, size_t insertlen,\r | |
110 | size_t copylen, size_t copylen_code, size_t distance_code) {\r | |
111 | self->insert_len_ = (uint32_t)insertlen;\r | |
112 | self->copy_len_ = (uint32_t)(copylen | ((copylen_code ^ copylen) << 24));\r | |
113 | /* The distance prefix and extra bits are stored in this Command as if\r | |
114 | npostfix and ndirect were 0, they are only recomputed later after the\r | |
115 | clustering if needed. */\r | |
116 | PrefixEncodeCopyDistance(\r | |
117 | distance_code, 0, 0, &self->dist_prefix_, &self->dist_extra_);\r | |
118 | GetLengthCode(\r | |
119 | insertlen, copylen_code, TO_BROTLI_BOOL(self->dist_prefix_ == 0),\r | |
120 | &self->cmd_prefix_);\r | |
121 | }\r | |
122 | \r | |
123 | static BROTLI_INLINE void InitInsertCommand(Command* self, size_t insertlen) {\r | |
124 | self->insert_len_ = (uint32_t)insertlen;\r | |
125 | self->copy_len_ = 4 << 24;\r | |
126 | self->dist_extra_ = 0;\r | |
127 | self->dist_prefix_ = 16;\r | |
128 | GetLengthCode(insertlen, 4, BROTLI_FALSE, &self->cmd_prefix_);\r | |
129 | }\r | |
130 | \r | |
131 | static BROTLI_INLINE uint32_t CommandDistanceCode(const Command* self) {\r | |
132 | if (self->dist_prefix_ < 16) {\r | |
133 | return self->dist_prefix_;\r | |
134 | } else {\r | |
135 | uint32_t nbits = self->dist_extra_ >> 24;\r | |
136 | uint32_t extra = self->dist_extra_ & 0xffffff;\r | |
137 | uint32_t prefix = self->dist_prefix_ - 12u - 2u * nbits;\r | |
138 | return (prefix << nbits) + extra + 12;\r | |
139 | }\r | |
140 | }\r | |
141 | \r | |
142 | static BROTLI_INLINE uint32_t CommandDistanceContext(const Command* self) {\r | |
143 | uint32_t r = self->cmd_prefix_ >> 6;\r | |
144 | uint32_t c = self->cmd_prefix_ & 7;\r | |
145 | if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) {\r | |
146 | return c;\r | |
147 | }\r | |
148 | return 3;\r | |
149 | }\r | |
150 | \r | |
151 | static BROTLI_INLINE uint32_t CommandCopyLen(const Command* self) {\r | |
152 | return self->copy_len_ & 0xFFFFFF;\r | |
153 | }\r | |
154 | \r | |
155 | static BROTLI_INLINE uint32_t CommandCopyLenCode(const Command* self) {\r | |
156 | return (self->copy_len_ & 0xFFFFFF) ^ (self->copy_len_ >> 24);\r | |
157 | }\r | |
158 | \r | |
159 | #if defined(__cplusplus) || defined(c_plusplus)\r | |
160 | } /* extern "C" */\r | |
161 | #endif\r | |
162 | \r | |
163 | #endif /* BROTLI_ENC_COMMAND_H_ */\r |