29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 |
30 |
31 You can contact the author at : |
31 You can contact the author at : |
32 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
32 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
33 ****************************************************************** */ |
33 ****************************************************************** */ |
34 #ifndef FSE_H |
|
35 #define FSE_H |
|
36 |
34 |
37 #if defined (__cplusplus) |
35 #if defined (__cplusplus) |
38 extern "C" { |
36 extern "C" { |
39 #endif |
37 #endif |
|
38 |
|
39 #ifndef FSE_H |
|
40 #define FSE_H |
40 |
41 |
41 |
42 |
42 /*-***************************************** |
43 /*-***************************************** |
43 * Dependencies |
44 * Dependencies |
44 ******************************************/ |
45 ******************************************/ |
45 #include <stddef.h> /* size_t, ptrdiff_t */ |
46 #include <stddef.h> /* size_t, ptrdiff_t */ |
46 |
47 |
|
48 |
|
49 /*-***************************************** |
|
50 * FSE_PUBLIC_API : control library symbols visibility |
|
51 ******************************************/ |
|
52 #if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4) |
|
53 # define FSE_PUBLIC_API __attribute__ ((visibility ("default"))) |
|
54 #elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */ |
|
55 # define FSE_PUBLIC_API __declspec(dllexport) |
|
56 #elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1) |
|
57 # define FSE_PUBLIC_API __declspec(dllimport) /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ |
|
58 #else |
|
59 # define FSE_PUBLIC_API |
|
60 #endif |
|
61 |
|
62 /*------ Version ------*/ |
|
63 #define FSE_VERSION_MAJOR 0 |
|
64 #define FSE_VERSION_MINOR 9 |
|
65 #define FSE_VERSION_RELEASE 0 |
|
66 |
|
67 #define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE |
|
68 #define FSE_QUOTE(str) #str |
|
69 #define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str) |
|
70 #define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION) |
|
71 |
|
72 #define FSE_VERSION_NUMBER (FSE_VERSION_MAJOR *100*100 + FSE_VERSION_MINOR *100 + FSE_VERSION_RELEASE) |
|
73 FSE_PUBLIC_API unsigned FSE_versionNumber(void); /**< library version number; to be used when checking dll version */ |
47 |
74 |
48 /*-**************************************** |
75 /*-**************************************** |
49 * FSE simple functions |
76 * FSE simple functions |
50 ******************************************/ |
77 ******************************************/ |
51 /*! FSE_compress() : |
78 /*! FSE_compress() : |
54 @return : size of compressed data (<= dstCapacity). |
81 @return : size of compressed data (<= dstCapacity). |
55 Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! |
82 Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! |
56 if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead. |
83 if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead. |
57 if FSE_isError(return), compression failed (more details using FSE_getErrorName()) |
84 if FSE_isError(return), compression failed (more details using FSE_getErrorName()) |
58 */ |
85 */ |
59 size_t FSE_compress(void* dst, size_t dstCapacity, |
86 FSE_PUBLIC_API size_t FSE_compress(void* dst, size_t dstCapacity, |
60 const void* src, size_t srcSize); |
87 const void* src, size_t srcSize); |
61 |
88 |
62 /*! FSE_decompress(): |
89 /*! FSE_decompress(): |
63 Decompress FSE data from buffer 'cSrc', of size 'cSrcSize', |
90 Decompress FSE data from buffer 'cSrc', of size 'cSrcSize', |
64 into already allocated destination buffer 'dst', of size 'dstCapacity'. |
91 into already allocated destination buffer 'dst', of size 'dstCapacity'. |
65 @return : size of regenerated data (<= maxDstSize), |
92 @return : size of regenerated data (<= maxDstSize), |
67 |
94 |
68 ** Important ** : FSE_decompress() does not decompress non-compressible nor RLE data !!! |
95 ** Important ** : FSE_decompress() does not decompress non-compressible nor RLE data !!! |
69 Why ? : making this distinction requires a header. |
96 Why ? : making this distinction requires a header. |
70 Header management is intentionally delegated to the user layer, which can better manage special cases. |
97 Header management is intentionally delegated to the user layer, which can better manage special cases. |
71 */ |
98 */ |
72 size_t FSE_decompress(void* dst, size_t dstCapacity, |
99 FSE_PUBLIC_API size_t FSE_decompress(void* dst, size_t dstCapacity, |
73 const void* cSrc, size_t cSrcSize); |
100 const void* cSrc, size_t cSrcSize); |
74 |
101 |
75 |
102 |
76 /*-***************************************** |
103 /*-***************************************** |
77 * Tool functions |
104 * Tool functions |
78 ******************************************/ |
105 ******************************************/ |
79 size_t FSE_compressBound(size_t size); /* maximum compressed size */ |
106 FSE_PUBLIC_API size_t FSE_compressBound(size_t size); /* maximum compressed size */ |
80 |
107 |
81 /* Error Management */ |
108 /* Error Management */ |
82 unsigned FSE_isError(size_t code); /* tells if a return value is an error code */ |
109 FSE_PUBLIC_API unsigned FSE_isError(size_t code); /* tells if a return value is an error code */ |
83 const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */ |
110 FSE_PUBLIC_API const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */ |
84 |
111 |
85 |
112 |
86 /*-***************************************** |
113 /*-***************************************** |
87 * FSE advanced functions |
114 * FSE advanced functions |
88 ******************************************/ |
115 ******************************************/ |
125 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1). |
152 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1). |
126 *maxSymbolValuePtr will be updated if detected smaller than initial value. |
153 *maxSymbolValuePtr will be updated if detected smaller than initial value. |
127 @return : the count of the most frequent symbol (which is not identified). |
154 @return : the count of the most frequent symbol (which is not identified). |
128 if return == srcSize, there is only one symbol. |
155 if return == srcSize, there is only one symbol. |
129 Can also return an error code, which can be tested with FSE_isError(). */ |
156 Can also return an error code, which can be tested with FSE_isError(). */ |
130 size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize); |
157 FSE_PUBLIC_API size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize); |
131 |
158 |
132 /*! FSE_optimalTableLog(): |
159 /*! FSE_optimalTableLog(): |
133 dynamically downsize 'tableLog' when conditions are met. |
160 dynamically downsize 'tableLog' when conditions are met. |
134 It saves CPU time, by using smaller tables, while preserving or even improving compression ratio. |
161 It saves CPU time, by using smaller tables, while preserving or even improving compression ratio. |
135 @return : recommended tableLog (necessarily <= 'maxTableLog') */ |
162 @return : recommended tableLog (necessarily <= 'maxTableLog') */ |
136 unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue); |
163 FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue); |
137 |
164 |
138 /*! FSE_normalizeCount(): |
165 /*! FSE_normalizeCount(): |
139 normalize counts so that sum(count[]) == Power_of_2 (2^tableLog) |
166 normalize counts so that sum(count[]) == Power_of_2 (2^tableLog) |
140 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1). |
167 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1). |
141 @return : tableLog, |
168 @return : tableLog, |
142 or an errorCode, which can be tested using FSE_isError() */ |
169 or an errorCode, which can be tested using FSE_isError() */ |
143 size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, const unsigned* count, size_t srcSize, unsigned maxSymbolValue); |
170 FSE_PUBLIC_API size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, const unsigned* count, size_t srcSize, unsigned maxSymbolValue); |
144 |
171 |
145 /*! FSE_NCountWriteBound(): |
172 /*! FSE_NCountWriteBound(): |
146 Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'. |
173 Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'. |
147 Typically useful for allocation purpose. */ |
174 Typically useful for allocation purpose. */ |
148 size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog); |
175 FSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog); |
149 |
176 |
150 /*! FSE_writeNCount(): |
177 /*! FSE_writeNCount(): |
151 Compactly save 'normalizedCounter' into 'buffer'. |
178 Compactly save 'normalizedCounter' into 'buffer'. |
152 @return : size of the compressed table, |
179 @return : size of the compressed table, |
153 or an errorCode, which can be tested using FSE_isError(). */ |
180 or an errorCode, which can be tested using FSE_isError(). */ |
154 size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); |
181 FSE_PUBLIC_API size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); |
155 |
182 |
156 |
183 |
157 /*! Constructor and Destructor of FSE_CTable. |
184 /*! Constructor and Destructor of FSE_CTable. |
158 Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */ |
185 Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */ |
159 typedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */ |
186 typedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */ |
160 FSE_CTable* FSE_createCTable (unsigned tableLog, unsigned maxSymbolValue); |
187 FSE_PUBLIC_API FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog); |
161 void FSE_freeCTable (FSE_CTable* ct); |
188 FSE_PUBLIC_API void FSE_freeCTable (FSE_CTable* ct); |
162 |
189 |
163 /*! FSE_buildCTable(): |
190 /*! FSE_buildCTable(): |
164 Builds `ct`, which must be already allocated, using FSE_createCTable(). |
191 Builds `ct`, which must be already allocated, using FSE_createCTable(). |
165 @return : 0, or an errorCode, which can be tested using FSE_isError() */ |
192 @return : 0, or an errorCode, which can be tested using FSE_isError() */ |
166 size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); |
193 FSE_PUBLIC_API size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); |
167 |
194 |
168 /*! FSE_compress_usingCTable(): |
195 /*! FSE_compress_usingCTable(): |
169 Compress `src` using `ct` into `dst` which must be already allocated. |
196 Compress `src` using `ct` into `dst` which must be already allocated. |
170 @return : size of compressed data (<= `dstCapacity`), |
197 @return : size of compressed data (<= `dstCapacity`), |
171 or 0 if compressed data could not fit into `dst`, |
198 or 0 if compressed data could not fit into `dst`, |
172 or an errorCode, which can be tested using FSE_isError() */ |
199 or an errorCode, which can be tested using FSE_isError() */ |
173 size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct); |
200 FSE_PUBLIC_API size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct); |
174 |
201 |
175 /*! |
202 /*! |
176 Tutorial : |
203 Tutorial : |
177 ---------- |
204 ---------- |
178 The first step is to count all symbols. FSE_count() does this job very fast. |
205 The first step is to count all symbols. FSE_count() does this job very fast. |
221 /*! FSE_readNCount(): |
248 /*! FSE_readNCount(): |
222 Read compactly saved 'normalizedCounter' from 'rBuffer'. |
249 Read compactly saved 'normalizedCounter' from 'rBuffer'. |
223 @return : size read from 'rBuffer', |
250 @return : size read from 'rBuffer', |
224 or an errorCode, which can be tested using FSE_isError(). |
251 or an errorCode, which can be tested using FSE_isError(). |
225 maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */ |
252 maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */ |
226 size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize); |
253 FSE_PUBLIC_API size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize); |
227 |
254 |
228 /*! Constructor and Destructor of FSE_DTable. |
255 /*! Constructor and Destructor of FSE_DTable. |
229 Note that its size depends on 'tableLog' */ |
256 Note that its size depends on 'tableLog' */ |
230 typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ |
257 typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ |
231 FSE_DTable* FSE_createDTable(unsigned tableLog); |
258 FSE_PUBLIC_API FSE_DTable* FSE_createDTable(unsigned tableLog); |
232 void FSE_freeDTable(FSE_DTable* dt); |
259 FSE_PUBLIC_API void FSE_freeDTable(FSE_DTable* dt); |
233 |
260 |
234 /*! FSE_buildDTable(): |
261 /*! FSE_buildDTable(): |
235 Builds 'dt', which must be already allocated, using FSE_createDTable(). |
262 Builds 'dt', which must be already allocated, using FSE_createDTable(). |
236 return : 0, or an errorCode, which can be tested using FSE_isError() */ |
263 return : 0, or an errorCode, which can be tested using FSE_isError() */ |
237 size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); |
264 FSE_PUBLIC_API size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); |
238 |
265 |
239 /*! FSE_decompress_usingDTable(): |
266 /*! FSE_decompress_usingDTable(): |
240 Decompress compressed source `cSrc` of size `cSrcSize` using `dt` |
267 Decompress compressed source `cSrc` of size `cSrcSize` using `dt` |
241 into `dst` which must be already allocated. |
268 into `dst` which must be already allocated. |
242 @return : size of regenerated data (necessarily <= `dstCapacity`), |
269 @return : size of regenerated data (necessarily <= `dstCapacity`), |
243 or an errorCode, which can be tested using FSE_isError() */ |
270 or an errorCode, which can be tested using FSE_isError() */ |
244 size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt); |
271 FSE_PUBLIC_API size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt); |
245 |
272 |
246 /*! |
273 /*! |
247 Tutorial : |
274 Tutorial : |
248 ---------- |
275 ---------- |
249 (Note : these functions only decompress FSE-compressed blocks. |
276 (Note : these functions only decompress FSE-compressed blocks. |
325 |
358 |
326 /* FSE_compress_wksp() : |
359 /* FSE_compress_wksp() : |
327 * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`). |
360 * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`). |
328 * FSE_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable. |
361 * FSE_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable. |
329 */ |
362 */ |
330 #define FSE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + (1<<((maxTableLog>2)?(maxTableLog-2):0)) ) |
363 #define FSE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + ((maxTableLog > 12) ? (1 << (maxTableLog - 2)) : 1024) ) |
331 size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); |
364 size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); |
332 |
365 |
333 size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits); |
366 size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits); |
334 /**< build a fake FSE_CTable, designed for a flat distribution, where each symbol uses nbBits */ |
367 /**< build a fake FSE_CTable, designed for a flat distribution, where each symbol uses nbBits */ |
335 |
368 |