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
29 #include "bitstream.h"
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
64 const int ff_lzw_encode_state_size
= sizeof(LZWEncodeState
);
67 * Hash function adding character
68 * @param head LZW code for prefix
69 * @param add Character to add
70 * @return New hash value
72 static inline int hash(int head
, const int add
)
74 head
^= (add
<< LZW_HASH_SHIFT
);
75 if (head
>= LZW_HASH_SIZE
)
76 head
-= LZW_HASH_SIZE
;
77 assert(head
>= 0 && head
< LZW_HASH_SIZE
);
82 * Hash function calculates next hash value
83 * @param head Actual hash code
84 * @param offset Offset calculated by hashOffset
85 * @return New hash value
87 static inline int hashNext(int head
, const int offset
)
91 head
+= LZW_HASH_SIZE
;
96 * Hash function calculates hash offset
97 * @param head Actual hash code
100 static inline int hashOffset(const int head
)
102 return head
? LZW_HASH_SIZE
- head
: 1;
106 * Write one code to stream
108 * @param c code to write
110 static inline void writeCode(LZWEncodeState
* s
, int c
)
112 assert(0 <= c
&& c
< 1 << s
->bits
);
113 put_bits(&s
->pb
, s
->bits
, c
);
118 * Find LZW code for block
120 * @param c Last character in block
121 * @param hash_prefix LZW code for prefix
122 * @return LZW code for block or -1 if not found in table
124 static inline int findCode(LZWEncodeState
* s
, uint8_t c
, int hash_prefix
)
126 int h
= hash(FFMAX(hash_prefix
, 0), c
);
127 int hash_offset
= hashOffset(h
);
129 while (s
->tab
[h
].hash_prefix
!= LZW_PREFIX_FREE
) {
130 if ((s
->tab
[h
].suffix
== c
)
131 && (s
->tab
[h
].hash_prefix
== hash_prefix
))
133 h
= hashNext(h
, hash_offset
);
140 * Add block to LZW code table
142 * @param c Last character in block
143 * @param hash_prefix LZW code for prefix
144 * @param hash_code LZW code for bytes block
146 static inline void addCode(LZWEncodeState
* s
, uint8_t c
, int hash_prefix
, int hash_code
)
148 s
->tab
[hash_code
].code
= s
->tabsize
;
149 s
->tab
[hash_code
].suffix
= c
;
150 s
->tab
[hash_code
].hash_prefix
= hash_prefix
;
154 if (s
->tabsize
>= 1 << s
->bits
)
159 * Clear LZW code table
162 static void clearTable(LZWEncodeState
* s
)
166 writeCode(s
, s
->clear_code
);
168 for (i
= 0; i
< LZW_HASH_SIZE
; i
++) {
169 s
->tab
[i
].hash_prefix
= LZW_PREFIX_FREE
;
171 for (i
= 0; i
< 256; i
++) {
174 s
->tab
[h
].suffix
= i
;
175 s
->tab
[h
].hash_prefix
= LZW_PREFIX_EMPTY
;
181 * Calculate number of bytes written
182 * @param s LZW encode state
183 * @return Number of bytes written
185 static int writtenBytes(LZWEncodeState
*s
){
186 int ret
= put_bits_count(&s
->pb
) >> 3;
187 ret
-= s
->output_bytes
;
188 s
->output_bytes
+= ret
;
193 * Initialize LZW encoder. Please set s->clear_code, s->end_code and s->maxbits before run.
195 * @param outbuf Output buffer
196 * @param outsize Size of output buffer
197 * @param maxbits Maximum length of code
199 void ff_lzw_encode_init(LZWEncodeState
* s
, uint8_t * outbuf
, int outsize
, int maxbits
)
203 s
->maxbits
= maxbits
;
204 init_put_bits(&s
->pb
, outbuf
, outsize
);
205 s
->bufsize
= outsize
;
206 assert(9 <= s
->maxbits
&& s
->maxbits
<= s
->maxbits
);
207 s
->maxcode
= 1 << s
->maxbits
;
209 s
->last_code
= LZW_PREFIX_EMPTY
;
214 * LZW main compress function
216 * @param inbuf Input buffer
217 * @param insize Size of input buffer
218 * @return Number of bytes written or -1 on error
220 int ff_lzw_encode(LZWEncodeState
* s
, const uint8_t * inbuf
, int insize
)
224 if(insize
* 3 > (s
->bufsize
- s
->output_bytes
) * 2){
228 if (s
->last_code
== LZW_PREFIX_EMPTY
)
231 for (i
= 0; i
< insize
; i
++) {
232 uint8_t c
= *inbuf
++;
233 int code
= findCode(s
, c
, s
->last_code
);
234 if (s
->tab
[code
].hash_prefix
== LZW_PREFIX_FREE
) {
235 writeCode(s
, s
->last_code
);
236 addCode(s
, c
, s
->last_code
, code
);
239 s
->last_code
= s
->tab
[code
].code
;
240 if (s
->tabsize
>= s
->maxcode
- 1) {
245 return writtenBytes(s
);
249 * Write end code and flush bitstream
251 * @return Number of bytes written or -1 on error
253 int ff_lzw_encode_flush(LZWEncodeState
* s
)
255 if (s
->last_code
!= -1)
256 writeCode(s
, s
->last_code
);
257 writeCode(s
, s
->end_code
);
258 flush_put_bits(&s
->pb
);
261 return writtenBytes(s
);