blob: 560045544571d0da508fb4a6c4b42a68b8703761 [file] [log] [blame]
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +02001// Copyright 2013 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// This class models a sequence of literals and a backward reference copy.
16
17#ifndef BROTLI_ENC_COMMAND_H_
18#define BROTLI_ENC_COMMAND_H_
19
20#include <stdint.h>
Zoltan Szabadkab4f39bf2014-10-28 13:25:22 +010021#include "./fast_log.h"
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +020022
23namespace brotli {
24
Zoltan Szabadkab4f39bf2014-10-28 13:25:22 +010025static inline void GetDistCode(int distance_code,
26 uint16_t* code, uint32_t* extra) {
27 distance_code -= 1;
28 if (distance_code < 16) {
29 *code = distance_code;
30 *extra = 0;
31 } else {
32 distance_code -= 12;
33 int numextra = Log2FloorNonZero(distance_code) - 1;
34 int prefix = distance_code >> numextra;
35 *code = 12 + 2 * numextra + prefix;
36 *extra = (numextra << 24) | (distance_code - (prefix << numextra));
37 }
38}
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +020039
Zoltan Szabadkab4f39bf2014-10-28 13:25:22 +010040static int insbase[] = { 0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26, 34, 50, 66,
41 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594 };
42static int insextra[] = { 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5,
43 5, 6, 7, 8, 9, 10, 12, 14, 24 };
44static int copybase[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 18, 22, 30, 38,
45 54, 70, 102, 134, 198, 326, 582, 1094, 2118 };
46static int copyextra[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4,
47 4, 5, 5, 6, 7, 8, 9, 10, 24 };
48
49static inline int GetInsertLengthCode(int insertlen) {
50 if (insertlen < 6) {
51 return insertlen;
52 } else if (insertlen < 130) {
53 insertlen -= 2;
54 int nbits = Log2FloorNonZero(insertlen) - 1;
55 return (nbits << 1) + (insertlen >> nbits) + 2;
56 } else if (insertlen < 2114) {
57 return Log2FloorNonZero(insertlen - 66) + 10;
58 } else if (insertlen < 6210) {
59 return 21;
60 } else if (insertlen < 22594) {
61 return 22;
62 } else {
63 return 23;
64 }
65}
66
67static inline int GetCopyLengthCode(int copylen) {
68 if (copylen < 10) {
69 return copylen - 2;
70 } else if (copylen < 134) {
71 copylen -= 6;
72 int nbits = Log2FloorNonZero(copylen) - 1;
73 return (nbits << 1) + (copylen >> nbits) + 4;
74 } else if (copylen < 2118) {
75 return Log2FloorNonZero(copylen - 70) + 12;
76 } else {
77 return 23;
78 }
79}
80
81static inline int CombineLengthCodes(
82 int inscode, int copycode, int distancecode) {
83 int bits64 = (copycode & 0x7u) | ((inscode & 0x7u) << 3);
84 if (distancecode == 0 && inscode < 8 && copycode < 16) {
85 return (copycode < 8) ? bits64 : (bits64 | 64);
86 } else {
87 // "To convert an insert-and-copy length code to an insert length code and
88 // a copy length code, the following table can be used"
89 static const int cells[9] = { 2, 3, 6, 4, 5, 8, 7, 9, 10 };
90 return (cells[(copycode >> 3) + 3 * (inscode >> 3)] << 6) | bits64;
91 }
92}
93
94static inline void GetLengthCode(int insertlen, int copylen, int distancecode,
95 uint16_t* code, uint64_t* extra) {
96 int inscode = GetInsertLengthCode(insertlen);
97 int copycode = GetCopyLengthCode(copylen);
98 uint64_t insnumextra = insextra[inscode];
99 uint64_t numextra = insnumextra + copyextra[copycode];
100 uint64_t insextraval = insertlen - insbase[inscode];
101 uint64_t copyextraval = copylen - copybase[copycode];
102 *code = CombineLengthCodes(inscode, copycode, distancecode);
103 *extra = (numextra << 48) | (copyextraval << insnumextra) | insextraval;
104}
105
106struct Command {
107 Command() {}
108
109 Command(int insertlen, int copylen, int copylen_code, int distance_code)
110 : insert_len_(insertlen), copy_len_(copylen) {
111 GetDistCode(distance_code, &dist_prefix_, &dist_extra_);
112 GetLengthCode(insertlen, copylen_code, dist_prefix_,
113 &cmd_prefix_, &cmd_extra_);
114 }
115
116 Command(int insertlen)
117 : insert_len_(insertlen), copy_len_(0), dist_prefix_(16), dist_extra_(0) {
118 GetLengthCode(insertlen, 4, dist_prefix_, &cmd_prefix_, &cmd_extra_);
119 }
120
121 int DistanceCode() const {
122 if (dist_prefix_ < 16) {
123 return dist_prefix_ + 1;
124 }
125 int nbits = dist_extra_ >> 24;
126 int extra = dist_extra_ & 0xffffff;
127 int prefix = dist_prefix_ - 12 - 2 * nbits;
128 return (prefix << nbits) + extra + 13;
129 }
130
131 int DistanceContext() const {
132 int r = cmd_prefix_ >> 6;
133 int c = cmd_prefix_ & 7;
134 if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) {
135 return c;
136 }
137 return 3;
138 }
139
140 int insert_len_;
141 int copy_len_;
142 uint16_t cmd_prefix_;
143 uint16_t dist_prefix_;
144 uint64_t cmd_extra_;
145 uint32_t dist_extra_;
Zoltan Szabadkac66e4e32013-10-23 13:06:13 +0200146};
147
148} // namespace brotli
149
150#endif // BROTLI_ENC_COMMAND_H_