3 * Copyright (c) 2003 Fabrice Bellard.
4 * Copyright (c) 2006 Konstantin Shishkov.
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * \brief Header file for LZW Encoder/Decoder functions.
29 * These functions were taken from the ffmpeg library.
33 #define LZW_MAXBITS 12
34 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
35 #define LZW_HASH_SIZE 16411
36 #define LZW_HASH_SHIFT 6
38 #define LZW_PREFIX_EMPTY -1
39 #define LZW_PREFIX_FREE -2
42 /** fixed length header in front of compressed config file */
43 #define COMPRESSED_CONFIG_HEADER_LENGTH 40
45 /** start of compressed config file */
46 #define COMPRESSED_CONFIG_HEADER "<compressed alg=lzw len="
49 /** One code in hash table */
51 /// Hash code of prefix, LZW_PREFIX_EMPTY if empty prefix, or LZW_PREFIX_FREE if no code
53 int code; ///< LZW code
54 uint8_t suffix; ///< Last character in code block
58 typedef struct PutBitContext {
61 uint8_t *buf, *buf_ptr, *buf_end;
65 /** LZW encode state */
67 int clear_code; ///< Value of clear code
68 int end_code; ///< Value of end code
69 Code tab[LZW_HASH_SIZE]; ///< Hash table
70 int tabsize; ///< Number of values in hash table
71 int bits; ///< Actual bits code
72 int bufsize; ///< Size of output buffer
73 PutBitContext pb; ///< Put bit context for output
74 int maxbits; ///< Max bits code
75 int maxcode; ///< Max value of code
76 int output_bytes; ///< Number of written bytes
77 int last_code; ///< Value of last output code or LZW_PREFIX_EMPTY
81 /** LZW decoder state structure */
87 int mode; ///< Decoder mode
88 int cursize; ///< The current code size
93 int newcodes; ///< First available code
94 int top_slot; ///< Highest code for current size
96 int slot; ///< Last read code
99 uint8_t stack[LZW_SIZTABLE];
100 uint8_t suffix[LZW_SIZTABLE];
101 uint16_t prefix[LZW_SIZTABLE];
102 int bs; ///< current buffer size for GIF
106 /** Create and Initialize a LZW encoder.
108 * @param s (IN/OUT) LZW encoder state structure allocated by this function.
109 * @param outbuf (IN) Output buffer, caller is responsible for allocating the buffer.
110 * @param outsize (IN) Size of output buffer
112 * @return CmsRet enum
114 CmsRet cmsLzw_initEncoder(LZWEncoderState **s, UINT8 *outbuf, UINT32 outsize);
117 /** LZW main encode/compress function
119 * @param s (IN) LZW encoder state
120 * @param inbuf (IN) Input buffer, contains data to be compressed
121 * @param insize (IN) Size of input buffer
122 * @return Number of bytes written or -1 on error
124 SINT32 cmsLzw_encode(LZWEncoderState *s, const UINT8 *inbuf, UINT32 insize);
127 /** Write end code and flush bitstream
129 * @param s (IN) LZW encoder state
130 * @return Number of bytes written or -1 on error. Note the count here should
131 * be added to the count returned by cmsLzw_encode for the total number
132 * of encoded/compressed bytes.
134 SINT32 cmsLzw_flushEncoder(LZWEncoderState *s);
137 /** Destroy the LZW encoder.
138 * Note the output buffer that was passed into the cmsLzw_initEncoder
139 * is not freed. That buffer is the responsibility of the caller.
141 * @param s (IN) LZW encoder state
143 void cmsLzw_cleanupEncoder(LZWEncoderState **s);
146 /** Create and Initialize a LZW decoder
148 * @param s (IN/OUT) LZW decoder context
149 * @param inbuf (IN) input compressed data
150 * @param inbuf_size (IN) input data size
152 * @return CmsRet enum
154 CmsRet cmsLzw_initDecoder(LZWDecoderState **s, UINT8 *inbuf, UINT32 inbuf_size);
157 /** Decode given number of bytes
159 * @param s (IN) LZW decoder context
160 * @param outbuf (IN) output buffer, caller is responsible for allocating this buffer
161 * @param outlen (IN) number of bytes to decode, or the length of the output buffer.
162 * The decompressor/decoder will stop if the end of the encoded
163 * data is reached or if the end of the output buffer is reached.
165 * @return number of bytes decoded, or -1 on error.
167 SINT32 cmsLzw_decode(LZWDecoderState *s, UINT8 *outbuf, UINT32 outlen);
170 /** Destroy the LZW Decoder.
172 * Note the input buffer that was passed into cmsLzw_initDecoder is not freed.
173 * That buffer is the responsibility of the caller.
175 void cmsLzw_cleanupDecoder(LZWDecoderState **s);
179 #endif /* __CMS_LZW_H__ */