3 * Copyright (c) 2003 Fabrice Bellard.
4 * Copyright (c) 2006 Konstantin Shishkov.
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @brief LZW decoding routines
26 * @author Fabrice Bellard
27 * Modified for use in TIFF by Konstantin Shishkov
33 #define LZW_MAXBITS 12
34 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
36 static const uint16_t mask
[17] =
38 0x0000, 0x0001, 0x0003, 0x0007,
39 0x000F, 0x001F, 0x003F, 0x007F,
40 0x00FF, 0x01FF, 0x03FF, 0x07FF,
41 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
45 const uint8_t *pbuf
, *ebuf
;
49 int mode
; ///< Decoder mode
50 int cursize
; ///< The current code size
55 int newcodes
; ///< First available code
56 int top_slot
; ///< Highest code for current size
58 int slot
; ///< Last read code
61 uint8_t stack
[LZW_SIZTABLE
];
62 uint8_t suffix
[LZW_SIZTABLE
];
63 uint16_t prefix
[LZW_SIZTABLE
];
64 int bs
; ///< current buffer size for GIF
67 /* get one code from stream */
68 static int lzw_get_code(struct LZWState
* s
)
72 if(s
->mode
== FF_LZW_GIF
) {
73 while (s
->bbits
< s
->cursize
) {
77 s
->bbuf
|= (*s
->pbuf
++) << s
->bbits
;
82 s
->bbuf
>>= s
->cursize
;
84 while (s
->bbits
< s
->cursize
) {
85 s
->bbuf
= (s
->bbuf
<< 8) | (*s
->pbuf
++);
88 c
= s
->bbuf
>> (s
->bbits
- s
->cursize
);
90 s
->bbits
-= s
->cursize
;
91 return c
& s
->curmask
;
94 const uint8_t* ff_lzw_cur_ptr(LZWState
*p
)
96 return ((struct LZWState
*)p
)->pbuf
;
99 void ff_lzw_decode_tail(LZWState
*p
)
101 struct LZWState
*s
= (struct LZWState
*)p
;
103 if(s
->mode
== FF_LZW_GIF
) {
104 while(s
->pbuf
< s
->ebuf
&& s
->bs
>0){
112 av_cold
void ff_lzw_decode_open(LZWState
**p
)
114 *p
= av_mallocz(sizeof(struct LZWState
));
117 av_cold
void ff_lzw_decode_close(LZWState
**p
)
123 * Initialize LZW decoder
124 * @param s LZW context
125 * @param csize initial code size in bits
126 * @param buf input data
127 * @param buf_size input data size
128 * @param mode decoder working mode - either GIF or TIFF
130 int ff_lzw_decode_init(LZWState
*p
, int csize
, const uint8_t *buf
, int buf_size
, int mode
)
132 struct LZWState
*s
= (struct LZWState
*)p
;
134 if(csize
< 1 || csize
> LZW_MAXBITS
)
138 s
->ebuf
= s
->pbuf
+ buf_size
;
145 s
->cursize
= s
->codesize
+ 1;
146 s
->curmask
= mask
[s
->cursize
];
147 s
->top_slot
= 1 << s
->cursize
;
148 s
->clear_code
= 1 << s
->codesize
;
149 s
->end_code
= s
->clear_code
+ 1;
150 s
->slot
= s
->newcodes
= s
->clear_code
+ 2;
155 s
->extra_slot
= s
->mode
== FF_LZW_TIFF
;
160 * Decode given number of bytes
161 * NOTE: the algorithm here is inspired from the LZW GIF decoder
162 * written by Steven A. Bennett in 1987.
164 * @param s LZW context
165 * @param buf output buffer
166 * @param len number of bytes to decode
167 * @return number of bytes decoded
169 int ff_lzw_decode(LZWState
*p
, uint8_t *buf
, int len
){
170 int l
, c
, code
, oc
, fc
;
172 struct LZWState
*s
= (struct LZWState
*)p
;
183 while (sp
> s
->stack
) {
189 if (c
== s
->end_code
) {
191 } else if (c
== s
->clear_code
) {
192 s
->cursize
= s
->codesize
+ 1;
193 s
->curmask
= mask
[s
->cursize
];
194 s
->slot
= s
->newcodes
;
195 s
->top_slot
= 1 << s
->cursize
;
199 if (code
== s
->slot
&& fc
>=0) {
202 }else if(code
>= s
->slot
)
204 while (code
>= s
->newcodes
) {
205 *sp
++ = s
->suffix
[code
];
206 code
= s
->prefix
[code
];
209 if (s
->slot
< s
->top_slot
&& oc
>=0) {
210 s
->suffix
[s
->slot
] = code
;
211 s
->prefix
[s
->slot
++] = oc
;
215 if (s
->slot
>= s
->top_slot
- s
->extra_slot
) {
216 if (s
->cursize
< LZW_MAXBITS
) {
218 s
->curmask
= mask
[++s
->cursize
];