Mercurial > libavcodec.hg
comparison lzwenc.c @ 10634:e15eb76d9e47 libavcodec
use lzw compression in gif encoder
author | bcoudurier |
---|---|
date | Thu, 03 Dec 2009 19:17:39 +0000 |
parents | 4cb7c65fc775 |
children | 7dd2a45249a9 |
comparison
equal
deleted
inserted
replaced
10633:66242b8fbd32 | 10634:e15eb76d9e47 |
---|---|
56 PutBitContext pb; ///< Put bit context for output | 56 PutBitContext pb; ///< Put bit context for output |
57 int maxbits; ///< Max bits code | 57 int maxbits; ///< Max bits code |
58 int maxcode; ///< Max value of code | 58 int maxcode; ///< Max value of code |
59 int output_bytes; ///< Number of written bytes | 59 int output_bytes; ///< Number of written bytes |
60 int last_code; ///< Value of last output code or LZW_PREFIX_EMPTY | 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 | |
61 }LZWEncodeState; | 63 }LZWEncodeState; |
62 | 64 |
63 | 65 |
64 const int ff_lzw_encode_state_size = sizeof(LZWEncodeState); | 66 const int ff_lzw_encode_state_size = sizeof(LZWEncodeState); |
65 | 67 |
108 * @param c code to write | 110 * @param c code to write |
109 */ | 111 */ |
110 static inline void writeCode(LZWEncodeState * s, int c) | 112 static inline void writeCode(LZWEncodeState * s, int c) |
111 { | 113 { |
112 assert(0 <= c && c < 1 << s->bits); | 114 assert(0 <= c && c < 1 << s->bits); |
113 put_bits(&s->pb, s->bits, c); | 115 s->put_bits(&s->pb, s->bits, c); |
114 } | 116 } |
115 | 117 |
116 | 118 |
117 /** | 119 /** |
118 * Find LZW code for block | 120 * Find LZW code for block |
149 s->tab[hash_code].suffix = c; | 151 s->tab[hash_code].suffix = c; |
150 s->tab[hash_code].hash_prefix = hash_prefix; | 152 s->tab[hash_code].hash_prefix = hash_prefix; |
151 | 153 |
152 s->tabsize++; | 154 s->tabsize++; |
153 | 155 |
154 if (s->tabsize >= 1 << s->bits) | 156 if (s->tabsize >= (1 << s->bits) + (s->mode == FF_LZW_GIF)) |
155 s->bits++; | 157 s->bits++; |
156 } | 158 } |
157 | 159 |
158 /** | 160 /** |
159 * Clear LZW code table | 161 * Clear LZW code table |
194 * @param s LZW state | 196 * @param s LZW state |
195 * @param outbuf Output buffer | 197 * @param outbuf Output buffer |
196 * @param outsize Size of output buffer | 198 * @param outsize Size of output buffer |
197 * @param maxbits Maximum length of code | 199 * @param maxbits Maximum length of code |
198 */ | 200 */ |
199 void ff_lzw_encode_init(LZWEncodeState * s, uint8_t * outbuf, int outsize, int maxbits) | 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)) | |
200 { | 204 { |
201 s->clear_code = 256; | 205 s->clear_code = 256; |
202 s->end_code = 257; | 206 s->end_code = 257; |
203 s->maxbits = maxbits; | 207 s->maxbits = maxbits; |
204 init_put_bits(&s->pb, outbuf, outsize); | 208 init_put_bits(&s->pb, outbuf, outsize); |
206 assert(s->maxbits >= 9 && s->maxbits <= LZW_MAXBITS); | 210 assert(s->maxbits >= 9 && s->maxbits <= LZW_MAXBITS); |
207 s->maxcode = 1 << s->maxbits; | 211 s->maxcode = 1 << s->maxbits; |
208 s->output_bytes = 0; | 212 s->output_bytes = 0; |
209 s->last_code = LZW_PREFIX_EMPTY; | 213 s->last_code = LZW_PREFIX_EMPTY; |
210 s->bits = 9; | 214 s->bits = 9; |
215 s->mode = mode; | |
216 s->put_bits = lzw_put_bits; | |
211 } | 217 } |
212 | 218 |
213 /** | 219 /** |
214 * LZW main compress function | 220 * LZW main compress function |
215 * @param s LZW state | 221 * @param s LZW state |
248 /** | 254 /** |
249 * Write end code and flush bitstream | 255 * Write end code and flush bitstream |
250 * @param s LZW state | 256 * @param s LZW state |
251 * @return Number of bytes written or -1 on error | 257 * @return Number of bytes written or -1 on error |
252 */ | 258 */ |
253 int ff_lzw_encode_flush(LZWEncodeState * s) | 259 int ff_lzw_encode_flush(LZWEncodeState *s, |
260 void (*lzw_flush_put_bits)(PutBitContext *)) | |
254 { | 261 { |
255 if (s->last_code != -1) | 262 if (s->last_code != -1) |
256 writeCode(s, s->last_code); | 263 writeCode(s, s->last_code); |
257 writeCode(s, s->end_code); | 264 writeCode(s, s->end_code); |
258 flush_put_bits(&s->pb); | 265 lzw_flush_put_bits(&s->pb); |
259 s->last_code = -1; | 266 s->last_code = -1; |
260 | 267 |
261 return writtenBytes(s); | 268 return writtenBytes(s); |
262 } | 269 } |