-
Notifications
You must be signed in to change notification settings - Fork 16
/
EntropyCodecFactory.go
190 lines (140 loc) · 4.16 KB
/
EntropyCodecFactory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
Copyright 2011-2017 Frederic Langlet
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package entropy
import (
"fmt"
"strings"
kanzi "github.com/flanglet/kanzi-go"
)
const (
NONE_TYPE = uint32(0) // No compression
HUFFMAN_TYPE = uint32(1) // Huffman
FPAQ_TYPE = uint32(2) // Fast PAQ (order 0)
PAQ_TYPE = uint32(3) // Obsolete
RANGE_TYPE = uint32(4) // Range
ANS0_TYPE = uint32(5) // Asymmetric Numerical System order 0
CM_TYPE = uint32(6) // Context Model
TPAQ_TYPE = uint32(7) // Tangelo PAQ
ANS1_TYPE = uint32(8) // Asymmetric Numerical System order 1
TPAQX_TYPE = uint32(9) // Tangelo PAQ Extra
)
// NewEntropyDecoder creates a new entropy decoder using the provided type and bitstream
func NewEntropyDecoder(ibs kanzi.InputBitStream, ctx map[string]interface{},
entropyType uint32) (kanzi.EntropyDecoder, error) {
switch entropyType {
case HUFFMAN_TYPE:
return NewHuffmanDecoder(ibs)
case ANS0_TYPE:
return NewANSRangeDecoder(ibs, 0)
case ANS1_TYPE:
return NewANSRangeDecoder(ibs, 1)
case RANGE_TYPE:
return NewRangeDecoder(ibs)
case FPAQ_TYPE:
predictor, _ := NewFPAQPredictor()
return NewBinaryEntropyDecoder(ibs, predictor)
case CM_TYPE:
predictor, _ := NewCMPredictor()
return NewBinaryEntropyDecoder(ibs, predictor)
case TPAQ_TYPE:
predictor, _ := NewTPAQPredictor(&ctx)
return NewBinaryEntropyDecoder(ibs, predictor)
case TPAQX_TYPE:
predictor, _ := NewTPAQPredictor(&ctx)
return NewBinaryEntropyDecoder(ibs, predictor)
case NONE_TYPE:
return NewNullEntropyDecoder(ibs)
default:
return nil, fmt.Errorf("Unsupported entropy codec type: '%c'", entropyType)
}
}
// NewEntropyEncoder creates a new entropy encoder using the provided type and bitstream
func NewEntropyEncoder(obs kanzi.OutputBitStream, ctx map[string]interface{},
entropyType uint32) (kanzi.EntropyEncoder, error) {
switch entropyType {
case HUFFMAN_TYPE:
return NewHuffmanEncoder(obs)
case ANS0_TYPE:
return NewANSRangeEncoder(obs, 0)
case ANS1_TYPE:
return NewANSRangeEncoder(obs, 1)
case RANGE_TYPE:
return NewRangeEncoder(obs)
case FPAQ_TYPE:
predictor, _ := NewFPAQPredictor()
return NewBinaryEntropyEncoder(obs, predictor)
case CM_TYPE:
predictor, _ := NewCMPredictor()
return NewBinaryEntropyEncoder(obs, predictor)
case TPAQ_TYPE:
predictor, _ := NewTPAQPredictor(&ctx)
return NewBinaryEntropyEncoder(obs, predictor)
case TPAQX_TYPE:
predictor, _ := NewTPAQPredictor(&ctx)
return NewBinaryEntropyEncoder(obs, predictor)
case NONE_TYPE:
return NewNullEntropyEncoder(obs)
default:
return nil, fmt.Errorf("Unsupported entropy codec type: '%c'", entropyType)
}
}
// GetName returns the name of the entropy codec given its type
func GetName(entropyType uint32) string {
switch entropyType {
case HUFFMAN_TYPE:
return "HUFFMAN"
case ANS0_TYPE:
return "ANS0"
case ANS1_TYPE:
return "ANS1"
case RANGE_TYPE:
return "RANGE"
case FPAQ_TYPE:
return "FPAQ"
case CM_TYPE:
return "CM"
case TPAQ_TYPE:
return "TPAQ"
case TPAQX_TYPE:
return "TPAQX"
case NONE_TYPE:
return "NONE"
default:
panic(fmt.Errorf("Unsupported entropy codec type: '%c'", entropyType))
}
}
// GetType returns the type of the entropy codec given its name
func GetType(entropyName string) uint32 {
switch strings.ToUpper(entropyName) {
case "HUFFMAN":
return HUFFMAN_TYPE
case "ANS0":
return ANS0_TYPE
case "ANS1":
return ANS1_TYPE
case "RANGE":
return RANGE_TYPE
case "FPAQ":
return FPAQ_TYPE
case "CM":
return CM_TYPE
case "TPAQ":
return TPAQ_TYPE
case "TPAQX":
return TPAQX_TYPE
case "NONE":
return NONE_TYPE
default:
panic(fmt.Errorf("Unsupported entropy codec type: '%s'", entropyName))
}
}