3 * Copyright (c) 2007 Bartlomiej Wolowiec
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Bartlomiej Wolowiec
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
41 /** One code in hash table */
43 /// Hash code of prefix, LZW_PREFIX_EMPTY if empty prefix, or LZW_PREFIX_FREE if no code
45 int code; ///< LZW code
46 uint8_t suffix; ///< Last character in code block
49 /** LZW encode state */
50 typedef struct LZWEncodeState {
51 int clear_code; ///< Value of clear code
52 int end_code; ///< Value of end code
53 Code tab[LZW_HASH_SIZE]; ///< Hash table
54 int tabsize; ///< Number of values in hash table
55 int bits; ///< Actual bits code
56 int bufsize; ///< Size of output buffer
57 PutBitContext pb; ///< Put bit context for output
58 int maxbits; ///< Max bits code
59 int maxcode; ///< Max value of code
60 int output_bytes; ///< Number of written bytes
61 int last_code; ///< Value of last output code or LZW_PREFIX_EMPTY
62 enum FF_LZW_MODES mode; ///< TIFF or GIF
63 void (*put_bits)(PutBitContext *, int, unsigned); ///< GIF is LE while TIFF is BE
67 const int ff_lzw_encode_state_size = sizeof(LZWEncodeState);
70 * Hash function adding character
71 * @param head LZW code for prefix
72 * @param add Character to add
73 * @return New hash value
75 static inline int hash(int head, const int add)
77 head ^= (add << LZW_HASH_SHIFT);
78 if (head >= LZW_HASH_SIZE)
79 head -= LZW_HASH_SIZE;
80 av_assert2(head >= 0 && head < LZW_HASH_SIZE);
85 * Hash function calculates next hash value
86 * @param head Actual hash code
87 * @param offset Offset calculated by hashOffset
88 * @return New hash value
90 static inline int hashNext(int head, const int offset)
94 head += LZW_HASH_SIZE;
99 * Hash function calculates hash offset
100 * @param head Actual hash code
101 * @return Hash offset
103 static inline int hashOffset(const int head)
105 return head ? LZW_HASH_SIZE - head : 1;
109 * Write one code to stream
111 * @param c code to write
113 static inline void writeCode(LZWEncodeState * s, int c)
115 av_assert2(0 <= c && c < 1 << s->bits);
116 s->put_bits(&s->pb, s->bits, c);
121 * Find LZW code for block
123 * @param c Last character in block
124 * @param hash_prefix LZW code for prefix
125 * @return LZW code for block or -1 if not found in table
127 static inline int findCode(LZWEncodeState * s, uint8_t c, int hash_prefix)
129 int h = hash(FFMAX(hash_prefix, 0), c);
130 int hash_offset = hashOffset(h);
132 while (s->tab[h].hash_prefix != LZW_PREFIX_FREE) {
133 if ((s->tab[h].suffix == c)
134 && (s->tab[h].hash_prefix == hash_prefix))
136 h = hashNext(h, hash_offset);
143 * Add block to LZW code table
145 * @param c Last character in block
146 * @param hash_prefix LZW code for prefix
147 * @param hash_code LZW code for bytes block
149 static inline void addCode(LZWEncodeState * s, uint8_t c, int hash_prefix, int hash_code)
151 s->tab[hash_code].code = s->tabsize;
152 s->tab[hash_code].suffix = c;
153 s->tab[hash_code].hash_prefix = hash_prefix;
157 if (s->tabsize >= (1 << s->bits) + (s->mode == FF_LZW_GIF))
162 * Clear LZW code table
165 static void clearTable(LZWEncodeState * s)
169 writeCode(s, s->clear_code);
171 for (i = 0; i < LZW_HASH_SIZE; i++) {
172 s->tab[i].hash_prefix = LZW_PREFIX_FREE;
174 for (i = 0; i < 256; i++) {
177 s->tab[h].suffix = i;
178 s->tab[h].hash_prefix = LZW_PREFIX_EMPTY;
184 * Calculate number of bytes written
185 * @param s LZW encode state
186 * @return Number of bytes written
188 static int writtenBytes(LZWEncodeState *s){
189 int ret = put_bits_count(&s->pb) >> 3;
190 ret -= s->output_bytes;
191 s->output_bytes += ret;
196 * Initialize LZW encoder. Please set s->clear_code, s->end_code and s->maxbits before run.
198 * @param outbuf Output buffer
199 * @param outsize Size of output buffer
200 * @param maxbits Maximum length of code
202 void ff_lzw_encode_init(LZWEncodeState *s, uint8_t *outbuf, int outsize,
203 int maxbits, enum FF_LZW_MODES mode,
204 void (*lzw_put_bits)(PutBitContext *, int, unsigned))
208 s->maxbits = maxbits;
209 init_put_bits(&s->pb, outbuf, outsize);
210 s->bufsize = outsize;
211 av_assert0(s->maxbits >= 9 && s->maxbits <= LZW_MAXBITS);
212 s->maxcode = 1 << s->maxbits;
214 s->last_code = LZW_PREFIX_EMPTY;
217 s->put_bits = lzw_put_bits;
221 * LZW main compress function
223 * @param inbuf Input buffer
224 * @param insize Size of input buffer
225 * @return Number of bytes written or -1 on error
227 int ff_lzw_encode(LZWEncodeState * s, const uint8_t * inbuf, int insize)
231 if(insize * 3 > (s->bufsize - s->output_bytes) * 2){
235 if (s->last_code == LZW_PREFIX_EMPTY)
238 for (i = 0; i < insize; i++) {
239 uint8_t c = *inbuf++;
240 int code = findCode(s, c, s->last_code);
241 if (s->tab[code].hash_prefix == LZW_PREFIX_FREE) {
242 writeCode(s, s->last_code);
243 addCode(s, c, s->last_code, code);
246 s->last_code = s->tab[code].code;
247 if (s->tabsize >= s->maxcode - 1) {
252 return writtenBytes(s);
256 * Write end code and flush bitstream
258 * @return Number of bytes written or -1 on error
260 int ff_lzw_encode_flush(LZWEncodeState *s,
261 void (*lzw_flush_put_bits)(PutBitContext *))
263 if (s->last_code != -1)
264 writeCode(s, s->last_code);
265 writeCode(s, s->end_code);
266 if (s->mode == FF_LZW_GIF)
267 s->put_bits(&s->pb, 1, 0);
269 lzw_flush_put_bits(&s->pb);
272 return writtenBytes(s);