yading@10
|
1 /*
|
yading@10
|
2 * LZW encoder
|
yading@10
|
3 * Copyright (c) 2007 Bartlomiej Wolowiec
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 /**
|
yading@10
|
23 * @file
|
yading@10
|
24 * LZW encoder
|
yading@10
|
25 * @author Bartlomiej Wolowiec
|
yading@10
|
26 */
|
yading@10
|
27
|
yading@10
|
28 #include "avcodec.h"
|
yading@10
|
29 #include "put_bits.h"
|
yading@10
|
30 #include "lzw.h"
|
yading@10
|
31
|
yading@10
|
32 #define LZW_MAXBITS 12
|
yading@10
|
33 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
|
yading@10
|
34 #define LZW_HASH_SIZE 16411
|
yading@10
|
35 #define LZW_HASH_SHIFT 6
|
yading@10
|
36
|
yading@10
|
37 #define LZW_PREFIX_EMPTY -1
|
yading@10
|
38 #define LZW_PREFIX_FREE -2
|
yading@10
|
39
|
yading@10
|
40 /** One code in hash table */
|
yading@10
|
41 typedef struct Code{
|
yading@10
|
42 /// Hash code of prefix, LZW_PREFIX_EMPTY if empty prefix, or LZW_PREFIX_FREE if no code
|
yading@10
|
43 int hash_prefix;
|
yading@10
|
44 int code; ///< LZW code
|
yading@10
|
45 uint8_t suffix; ///< Last character in code block
|
yading@10
|
46 }Code;
|
yading@10
|
47
|
yading@10
|
48 /** LZW encode state */
|
yading@10
|
49 typedef struct LZWEncodeState {
|
yading@10
|
50 int clear_code; ///< Value of clear code
|
yading@10
|
51 int end_code; ///< Value of end code
|
yading@10
|
52 Code tab[LZW_HASH_SIZE]; ///< Hash table
|
yading@10
|
53 int tabsize; ///< Number of values in hash table
|
yading@10
|
54 int bits; ///< Actual bits code
|
yading@10
|
55 int bufsize; ///< Size of output buffer
|
yading@10
|
56 PutBitContext pb; ///< Put bit context for output
|
yading@10
|
57 int maxbits; ///< Max bits code
|
yading@10
|
58 int maxcode; ///< Max value of code
|
yading@10
|
59 int output_bytes; ///< Number of written bytes
|
yading@10
|
60 int last_code; ///< Value of last output code or LZW_PREFIX_EMPTY
|
yading@10
|
61 enum FF_LZW_MODES mode; ///< TIFF or GIF
|
yading@10
|
62 void (*put_bits)(PutBitContext *, int, unsigned); ///< GIF is LE while TIFF is BE
|
yading@10
|
63 }LZWEncodeState;
|
yading@10
|
64
|
yading@10
|
65
|
yading@10
|
66 const int ff_lzw_encode_state_size = sizeof(LZWEncodeState);
|
yading@10
|
67
|
yading@10
|
68 /**
|
yading@10
|
69 * Hash function adding character
|
yading@10
|
70 * @param head LZW code for prefix
|
yading@10
|
71 * @param add Character to add
|
yading@10
|
72 * @return New hash value
|
yading@10
|
73 */
|
yading@10
|
74 static inline int hash(int head, const int add)
|
yading@10
|
75 {
|
yading@10
|
76 head ^= (add << LZW_HASH_SHIFT);
|
yading@10
|
77 if (head >= LZW_HASH_SIZE)
|
yading@10
|
78 head -= LZW_HASH_SIZE;
|
yading@10
|
79 av_assert2(head >= 0 && head < LZW_HASH_SIZE);
|
yading@10
|
80 return head;
|
yading@10
|
81 }
|
yading@10
|
82
|
yading@10
|
83 /**
|
yading@10
|
84 * Hash function calculates next hash value
|
yading@10
|
85 * @param head Actual hash code
|
yading@10
|
86 * @param offset Offset calculated by hashOffset
|
yading@10
|
87 * @return New hash value
|
yading@10
|
88 */
|
yading@10
|
89 static inline int hashNext(int head, const int offset)
|
yading@10
|
90 {
|
yading@10
|
91 head -= offset;
|
yading@10
|
92 if(head < 0)
|
yading@10
|
93 head += LZW_HASH_SIZE;
|
yading@10
|
94 return head;
|
yading@10
|
95 }
|
yading@10
|
96
|
yading@10
|
97 /**
|
yading@10
|
98 * Hash function calculates hash offset
|
yading@10
|
99 * @param head Actual hash code
|
yading@10
|
100 * @return Hash offset
|
yading@10
|
101 */
|
yading@10
|
102 static inline int hashOffset(const int head)
|
yading@10
|
103 {
|
yading@10
|
104 return head ? LZW_HASH_SIZE - head : 1;
|
yading@10
|
105 }
|
yading@10
|
106
|
yading@10
|
107 /**
|
yading@10
|
108 * Write one code to stream
|
yading@10
|
109 * @param s LZW state
|
yading@10
|
110 * @param c code to write
|
yading@10
|
111 */
|
yading@10
|
112 static inline void writeCode(LZWEncodeState * s, int c)
|
yading@10
|
113 {
|
yading@10
|
114 assert(0 <= c && c < 1 << s->bits);
|
yading@10
|
115 s->put_bits(&s->pb, s->bits, c);
|
yading@10
|
116 }
|
yading@10
|
117
|
yading@10
|
118
|
yading@10
|
119 /**
|
yading@10
|
120 * Find LZW code for block
|
yading@10
|
121 * @param s LZW state
|
yading@10
|
122 * @param c Last character in block
|
yading@10
|
123 * @param hash_prefix LZW code for prefix
|
yading@10
|
124 * @return LZW code for block or -1 if not found in table
|
yading@10
|
125 */
|
yading@10
|
126 static inline int findCode(LZWEncodeState * s, uint8_t c, int hash_prefix)
|
yading@10
|
127 {
|
yading@10
|
128 int h = hash(FFMAX(hash_prefix, 0), c);
|
yading@10
|
129 int hash_offset = hashOffset(h);
|
yading@10
|
130
|
yading@10
|
131 while (s->tab[h].hash_prefix != LZW_PREFIX_FREE) {
|
yading@10
|
132 if ((s->tab[h].suffix == c)
|
yading@10
|
133 && (s->tab[h].hash_prefix == hash_prefix))
|
yading@10
|
134 return h;
|
yading@10
|
135 h = hashNext(h, hash_offset);
|
yading@10
|
136 }
|
yading@10
|
137
|
yading@10
|
138 return h;
|
yading@10
|
139 }
|
yading@10
|
140
|
yading@10
|
141 /**
|
yading@10
|
142 * Add block to LZW code table
|
yading@10
|
143 * @param s LZW state
|
yading@10
|
144 * @param c Last character in block
|
yading@10
|
145 * @param hash_prefix LZW code for prefix
|
yading@10
|
146 * @param hash_code LZW code for bytes block
|
yading@10
|
147 */
|
yading@10
|
148 static inline void addCode(LZWEncodeState * s, uint8_t c, int hash_prefix, int hash_code)
|
yading@10
|
149 {
|
yading@10
|
150 s->tab[hash_code].code = s->tabsize;
|
yading@10
|
151 s->tab[hash_code].suffix = c;
|
yading@10
|
152 s->tab[hash_code].hash_prefix = hash_prefix;
|
yading@10
|
153
|
yading@10
|
154 s->tabsize++;
|
yading@10
|
155
|
yading@10
|
156 if (s->tabsize >= (1 << s->bits) + (s->mode == FF_LZW_GIF))
|
yading@10
|
157 s->bits++;
|
yading@10
|
158 }
|
yading@10
|
159
|
yading@10
|
160 /**
|
yading@10
|
161 * Clear LZW code table
|
yading@10
|
162 * @param s LZW state
|
yading@10
|
163 */
|
yading@10
|
164 static void clearTable(LZWEncodeState * s)
|
yading@10
|
165 {
|
yading@10
|
166 int i, h;
|
yading@10
|
167
|
yading@10
|
168 writeCode(s, s->clear_code);
|
yading@10
|
169 s->bits = 9;
|
yading@10
|
170 for (i = 0; i < LZW_HASH_SIZE; i++) {
|
yading@10
|
171 s->tab[i].hash_prefix = LZW_PREFIX_FREE;
|
yading@10
|
172 }
|
yading@10
|
173 for (i = 0; i < 256; i++) {
|
yading@10
|
174 h = hash(0, i);
|
yading@10
|
175 s->tab[h].code = i;
|
yading@10
|
176 s->tab[h].suffix = i;
|
yading@10
|
177 s->tab[h].hash_prefix = LZW_PREFIX_EMPTY;
|
yading@10
|
178 }
|
yading@10
|
179 s->tabsize = 258;
|
yading@10
|
180 }
|
yading@10
|
181
|
yading@10
|
182 /**
|
yading@10
|
183 * Calculate number of bytes written
|
yading@10
|
184 * @param s LZW encode state
|
yading@10
|
185 * @return Number of bytes written
|
yading@10
|
186 */
|
yading@10
|
187 static int writtenBytes(LZWEncodeState *s){
|
yading@10
|
188 int ret = put_bits_count(&s->pb) >> 3;
|
yading@10
|
189 ret -= s->output_bytes;
|
yading@10
|
190 s->output_bytes += ret;
|
yading@10
|
191 return ret;
|
yading@10
|
192 }
|
yading@10
|
193
|
yading@10
|
194 /**
|
yading@10
|
195 * Initialize LZW encoder. Please set s->clear_code, s->end_code and s->maxbits before run.
|
yading@10
|
196 * @param s LZW state
|
yading@10
|
197 * @param outbuf Output buffer
|
yading@10
|
198 * @param outsize Size of output buffer
|
yading@10
|
199 * @param maxbits Maximum length of code
|
yading@10
|
200 */
|
yading@10
|
201 void ff_lzw_encode_init(LZWEncodeState *s, uint8_t *outbuf, int outsize,
|
yading@10
|
202 int maxbits, enum FF_LZW_MODES mode,
|
yading@10
|
203 void (*lzw_put_bits)(PutBitContext *, int, unsigned))
|
yading@10
|
204 {
|
yading@10
|
205 s->clear_code = 256;
|
yading@10
|
206 s->end_code = 257;
|
yading@10
|
207 s->maxbits = maxbits;
|
yading@10
|
208 init_put_bits(&s->pb, outbuf, outsize);
|
yading@10
|
209 s->bufsize = outsize;
|
yading@10
|
210 assert(s->maxbits >= 9 && s->maxbits <= LZW_MAXBITS);
|
yading@10
|
211 s->maxcode = 1 << s->maxbits;
|
yading@10
|
212 s->output_bytes = 0;
|
yading@10
|
213 s->last_code = LZW_PREFIX_EMPTY;
|
yading@10
|
214 s->bits = 9;
|
yading@10
|
215 s->mode = mode;
|
yading@10
|
216 s->put_bits = lzw_put_bits;
|
yading@10
|
217 }
|
yading@10
|
218
|
yading@10
|
219 /**
|
yading@10
|
220 * LZW main compress function
|
yading@10
|
221 * @param s LZW state
|
yading@10
|
222 * @param inbuf Input buffer
|
yading@10
|
223 * @param insize Size of input buffer
|
yading@10
|
224 * @return Number of bytes written or -1 on error
|
yading@10
|
225 */
|
yading@10
|
226 int ff_lzw_encode(LZWEncodeState * s, const uint8_t * inbuf, int insize)
|
yading@10
|
227 {
|
yading@10
|
228 int i;
|
yading@10
|
229
|
yading@10
|
230 if(insize * 3 > (s->bufsize - s->output_bytes) * 2){
|
yading@10
|
231 return -1;
|
yading@10
|
232 }
|
yading@10
|
233
|
yading@10
|
234 if (s->last_code == LZW_PREFIX_EMPTY)
|
yading@10
|
235 clearTable(s);
|
yading@10
|
236
|
yading@10
|
237 for (i = 0; i < insize; i++) {
|
yading@10
|
238 uint8_t c = *inbuf++;
|
yading@10
|
239 int code = findCode(s, c, s->last_code);
|
yading@10
|
240 if (s->tab[code].hash_prefix == LZW_PREFIX_FREE) {
|
yading@10
|
241 writeCode(s, s->last_code);
|
yading@10
|
242 addCode(s, c, s->last_code, code);
|
yading@10
|
243 code= hash(0, c);
|
yading@10
|
244 }
|
yading@10
|
245 s->last_code = s->tab[code].code;
|
yading@10
|
246 if (s->tabsize >= s->maxcode - 1) {
|
yading@10
|
247 clearTable(s);
|
yading@10
|
248 }
|
yading@10
|
249 }
|
yading@10
|
250
|
yading@10
|
251 return writtenBytes(s);
|
yading@10
|
252 }
|
yading@10
|
253
|
yading@10
|
254 /**
|
yading@10
|
255 * Write end code and flush bitstream
|
yading@10
|
256 * @param s LZW state
|
yading@10
|
257 * @return Number of bytes written or -1 on error
|
yading@10
|
258 */
|
yading@10
|
259 int ff_lzw_encode_flush(LZWEncodeState *s,
|
yading@10
|
260 void (*lzw_flush_put_bits)(PutBitContext *))
|
yading@10
|
261 {
|
yading@10
|
262 if (s->last_code != -1)
|
yading@10
|
263 writeCode(s, s->last_code);
|
yading@10
|
264 writeCode(s, s->end_code);
|
yading@10
|
265 lzw_flush_put_bits(&s->pb);
|
yading@10
|
266 s->last_code = -1;
|
yading@10
|
267
|
yading@10
|
268 return writtenBytes(s);
|
yading@10
|
269 }
|