3 * Copyright (c) 2007 Bartlomiej Wolowiec
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Bartlomiej Wolowiec
32 #define LZW_MAXBITS 12
33 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
34 #define LZW_HASH_SIZE 16411
35 #define LZW_HASH_SHIFT 6
37 #define LZW_PREFIX_EMPTY -1
38 #define LZW_PREFIX_FREE -2
40 /** One code in hash table */
42 /// Hash code of prefix, LZW_PREFIX_EMPTY if empty prefix, or LZW_PREFIX_FREE if no code
44 int code; ///< LZW code
45 uint8_t suffix; ///< Last character in code block
48 /** LZW encode state */
49 typedef struct LZWEncodeState {
50 int clear_code; ///< Value of clear code
51 int end_code; ///< Value of end code
52 Code tab[LZW_HASH_SIZE]; ///< Hash table
53 int tabsize; ///< Number of values in hash table
54 int bits; ///< Actual bits code
55 int bufsize; ///< Size of output buffer
56 PutBitContext pb; ///< Put bit context for output
57 int maxbits; ///< Max bits code
58 int maxcode; ///< Max value of code
59 int output_bytes; ///< Number of written bytes
60 int last_code; ///< Value of last output code or LZW_PREFIX_EMPTY
61 enum FF_LZW_MODES mode; ///< TIFF or GIF
62 void (*put_bits)(PutBitContext *, int, unsigned); ///< GIF is LE while TIFF is BE
66 const int ff_lzw_encode_state_size = sizeof(LZWEncodeState);
69 * Hash function adding character
70 * @param head LZW code for prefix
71 * @param add Character to add
72 * @return New hash value
74 static inline int hash(int head, const int add)
76 head ^= (add << LZW_HASH_SHIFT);
77 if (head >= LZW_HASH_SIZE)
78 head -= LZW_HASH_SIZE;
79 assert(head >= 0 && head < LZW_HASH_SIZE);
84 * Hash function calculates next hash value
85 * @param head Actual hash code
86 * @param offset Offset calculated by hashOffset
87 * @return New hash value
89 static inline int hashNext(int head, const int offset)
93 head += LZW_HASH_SIZE;
98 * Hash function calculates hash offset
99 * @param head Actual hash code
100 * @return Hash offset
102 static inline int hashOffset(const int head)
104 return head ? LZW_HASH_SIZE - head : 1;
108 * Write one code to stream
110 * @param c code to write
112 static inline void writeCode(LZWEncodeState * s, int c)
114 assert(0 <= c && c < 1 << s->bits);
115 s->put_bits(&s->pb, s->bits, c);
120 * Find LZW code for block
122 * @param c Last character in block
123 * @param hash_prefix LZW code for prefix
124 * @return LZW code for block or -1 if not found in table
126 static inline int findCode(LZWEncodeState * s, uint8_t c, int hash_prefix)
128 int h = hash(FFMAX(hash_prefix, 0), c);
129 int hash_offset = hashOffset(h);
131 while (s->tab[h].hash_prefix != LZW_PREFIX_FREE) {
132 if ((s->tab[h].suffix == c)
133 && (s->tab[h].hash_prefix == hash_prefix))
135 h = hashNext(h, hash_offset);
142 * Add block to LZW code table
144 * @param c Last character in block
145 * @param hash_prefix LZW code for prefix
146 * @param hash_code LZW code for bytes block
148 static inline void addCode(LZWEncodeState * s, uint8_t c, int hash_prefix, int hash_code)
150 s->tab[hash_code].code = s->tabsize;
151 s->tab[hash_code].suffix = c;
152 s->tab[hash_code].hash_prefix = hash_prefix;
156 if (s->tabsize >= (1 << s->bits) + (s->mode == FF_LZW_GIF))
161 * Clear LZW code table
164 static void clearTable(LZWEncodeState * s)
168 writeCode(s, s->clear_code);
170 for (i = 0; i < LZW_HASH_SIZE; i++) {
171 s->tab[i].hash_prefix = LZW_PREFIX_FREE;
173 for (i = 0; i < 256; i++) {
176 s->tab[h].suffix = i;
177 s->tab[h].hash_prefix = LZW_PREFIX_EMPTY;
183 * Calculate number of bytes written
184 * @param s LZW encode state
185 * @return Number of bytes written
187 static int writtenBytes(LZWEncodeState *s){
188 int ret = put_bits_count(&s->pb) >> 3;
189 ret -= s->output_bytes;
190 s->output_bytes += ret;
195 * Initialize LZW encoder. Please set s->clear_code, s->end_code and s->maxbits before run.
197 * @param outbuf Output buffer
198 * @param outsize Size of output buffer
199 * @param maxbits Maximum length of code
201 void ff_lzw_encode_init(LZWEncodeState *s, uint8_t *outbuf, int outsize,
202 int maxbits, enum FF_LZW_MODES mode,
203 void (*lzw_put_bits)(PutBitContext *, int, unsigned))
207 s->maxbits = maxbits;
208 init_put_bits(&s->pb, outbuf, outsize);
209 s->bufsize = outsize;
210 assert(s->maxbits >= 9 && s->maxbits <= LZW_MAXBITS);
211 s->maxcode = 1 << s->maxbits;
213 s->last_code = LZW_PREFIX_EMPTY;
216 s->put_bits = lzw_put_bits;
220 * LZW main compress function
222 * @param inbuf Input buffer
223 * @param insize Size of input buffer
224 * @return Number of bytes written or -1 on error
226 int ff_lzw_encode(LZWEncodeState * s, const uint8_t * inbuf, int insize)
230 if(insize * 3 > (s->bufsize - s->output_bytes) * 2){
234 if (s->last_code == LZW_PREFIX_EMPTY)
237 for (i = 0; i < insize; i++) {
238 uint8_t c = *inbuf++;
239 int code = findCode(s, c, s->last_code);
240 if (s->tab[code].hash_prefix == LZW_PREFIX_FREE) {
241 writeCode(s, s->last_code);
242 addCode(s, c, s->last_code, code);
245 s->last_code = s->tab[code].code;
246 if (s->tabsize >= s->maxcode - 1) {
251 return writtenBytes(s);
255 * Write end code and flush bitstream
257 * @return Number of bytes written or -1 on error
259 int ff_lzw_encode_flush(LZWEncodeState *s,
260 void (*lzw_flush_put_bits)(PutBitContext *))
262 if (s->last_code != -1)
263 writeCode(s, s->last_code);
264 writeCode(s, s->end_code);
265 lzw_flush_put_bits(&s->pb);
268 return writtenBytes(s);