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.
34 #define LZW_MAXBITS 12
35 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
36 #define LZW_HASH_SIZE 16411
37 #define LZW_HASH_SHIFT 6
39 #define LZW_PREFIX_EMPTY -1
40 #define LZW_PREFIX_FREE -2
43 /** fixed length header in front of compressed config file */
44 #define COMPRESSED_CONFIG_HEADER_LENGTH 40
46 /** start of compressed config file */
47 #define COMPRESSED_CONFIG_HEADER "<compressed alg=lzw len="
50 /** One code in hash table */
52 /// Hash code of prefix, LZW_PREFIX_EMPTY if empty prefix, or LZW_PREFIX_FREE if no code
54 int code; ///< LZW code
55 uint8_t suffix; ///< Last character in code block
59 typedef struct PutBitContext {
62 uint8_t *buf, *buf_ptr, *buf_end;
66 /** LZW encode state */
68 int clear_code; ///< Value of clear code
69 int end_code; ///< Value of end code
70 Code tab[LZW_HASH_SIZE]; ///< Hash table
71 int tabsize; ///< Number of values in hash table
72 int bits; ///< Actual bits code
73 int bufsize; ///< Size of output buffer
74 PutBitContext pb; ///< Put bit context for output
75 int maxbits; ///< Max bits code
76 int maxcode; ///< Max value of code
77 int output_bytes; ///< Number of written bytes
78 int last_code; ///< Value of last output code or LZW_PREFIX_EMPTY
82 /** LZW decoder state structure */
88 int mode; ///< Decoder mode
89 int cursize; ///< The current code size
94 int newcodes; ///< First available code
95 int top_slot; ///< Highest code for current size
97 int slot; ///< Last read code
100 uint8_t stack[LZW_SIZTABLE];
101 uint8_t suffix[LZW_SIZTABLE];
102 uint16_t prefix[LZW_SIZTABLE];
103 int bs; ///< current buffer size for GIF
107 /** Create and Initialize a LZW encoder.
109 * @param s (IN/OUT) LZW encoder state structure allocated by this function.
110 * @param outbuf (IN) Output buffer, caller is responsible for allocating the buffer.
111 * @param outsize (IN) Size of output buffer
113 * @return CmsRet enum
115 CmsRet cmsLzw_initEncoder(LZWEncoderState **s, UINT8 *outbuf, UINT32 outsize);
118 /** LZW main encode/compress function
120 * @param s (IN) LZW encoder state
121 * @param inbuf (IN) Input buffer, contains data to be compressed
122 * @param insize (IN) Size of input buffer
123 * @return Number of bytes written or -1 on error
125 SINT32 cmsLzw_encode(LZWEncoderState *s, const UINT8 *inbuf, UINT32 insize);
128 /** Write end code and flush bitstream
130 * @param s (IN) LZW encoder state
131 * @return Number of bytes written or -1 on error. Note the count here should
132 * be added to the count returned by cmsLzw_encode for the total number
133 * of encoded/compressed bytes.
135 SINT32 cmsLzw_flushEncoder(LZWEncoderState *s);
138 /** Destroy the LZW encoder.
139 * Note the output buffer that was passed into the cmsLzw_initEncoder
140 * is not freed. That buffer is the responsibility of the caller.
142 * @param s (IN) LZW encoder state
144 void cmsLzw_cleanupEncoder(LZWEncoderState **s);
147 /** Create and Initialize a LZW decoder
149 * @param s (IN/OUT) LZW decoder context
150 * @param inbuf (IN) input compressed data
151 * @param inbuf_size (IN) input data size
153 * @return CmsRet enum
155 CmsRet cmsLzw_initDecoder(LZWDecoderState **s, UINT8 *inbuf, UINT32 inbuf_size);
158 /** Decode given number of bytes
160 * @param s (IN) LZW decoder context
161 * @param outbuf (IN) output buffer, caller is responsible for allocating this buffer
162 * @param outlen (IN) number of bytes to decode, or the length of the output buffer.
163 * The decompressor/decoder will stop if the end of the encoded
164 * data is reached or if the end of the output buffer is reached.
166 * @return number of bytes decoded, or -1 on error.
168 SINT32 cmsLzw_decode(LZWDecoderState *s, UINT8 *outbuf, UINT32 outlen);
171 /** Destroy the LZW Decoder.
173 * Note the input buffer that was passed into cmsLzw_initDecoder is not freed.
174 * That buffer is the responsibility of the caller.
176 void cmsLzw_cleanupDecoder(LZWDecoderState **s);
180 #endif /* __CMS_LZW_H__ */