lzw: switch to bytestream2
[libav.git] / libavcodec / lzw.c
CommitLineData
ffc5430b
KS
1/*
2 * LZW decoder
406792e7
DB
3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2006 Konstantin Shishkov
ffc5430b 5 *
2912e87a 6 * This file is part of Libav.
ffc5430b 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
ffc5430b
KS
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.
12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
ffc5430b
KS
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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
ffc5430b
KS
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
ba87f080 24 * @file
ffc5430b
KS
25 * @brief LZW decoding routines
26 * @author Fabrice Bellard
ad4cd0c2 27 * @author modified for use in TIFF by Konstantin Shishkov
ffc5430b
KS
28 */
29
30#include "avcodec.h"
e89aa4bf 31#include "bytestream.h"
ffc5430b 32#include "lzw.h"
1d9c2dc8 33#include "libavutil/mem.h"
ffc5430b
KS
34
35#define LZW_MAXBITS 12
36#define LZW_SIZTABLE (1<<LZW_MAXBITS)
37
38static const uint16_t mask[17] =
39{
40 0x0000, 0x0001, 0x0003, 0x0007,
41 0x000F, 0x001F, 0x003F, 0x007F,
42 0x00FF, 0x01FF, 0x03FF, 0x07FF,
43 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
44};
45
46struct LZWState {
e89aa4bf 47 GetByteContext gb;
ffc5430b
KS
48 int bbits;
49 unsigned int bbuf;
50
51 int mode; ///< Decoder mode
52 int cursize; ///< The current code size
53 int curmask;
54 int codesize;
55 int clear_code;
56 int end_code;
57 int newcodes; ///< First available code
58 int top_slot; ///< Highest code for current size
62ef8925 59 int extra_slot;
ffc5430b
KS
60 int slot; ///< Last read code
61 int fc, oc;
62 uint8_t *sp;
63 uint8_t stack[LZW_SIZTABLE];
64 uint8_t suffix[LZW_SIZTABLE];
65 uint16_t prefix[LZW_SIZTABLE];
66 int bs; ///< current buffer size for GIF
67};
68
69/* get one code from stream */
70static int lzw_get_code(struct LZWState * s)
71{
6aaf3abd 72 int c;
ffc5430b
KS
73
74 if(s->mode == FF_LZW_GIF) {
75 while (s->bbits < s->cursize) {
76 if (!s->bs) {
e89aa4bf 77 s->bs = bytestream2_get_byte(&s->gb);
ffc5430b 78 }
e89aa4bf 79 s->bbuf |= bytestream2_get_byte(&s->gb) << s->bbits;
ffc5430b
KS
80 s->bbits += 8;
81 s->bs--;
82 }
f39c3c88 83 c = s->bbuf;
93e91145 84 s->bbuf >>= s->cursize;
ffc5430b
KS
85 } else { // TIFF
86 while (s->bbits < s->cursize) {
e89aa4bf 87 s->bbuf = (s->bbuf << 8) | bytestream2_get_byte(&s->gb);
ffc5430b
KS
88 s->bbits += 8;
89 }
f39c3c88 90 c = s->bbuf >> (s->bbits - s->cursize);
ffc5430b
KS
91 }
92 s->bbits -= s->cursize;
f39c3c88 93 return c & s->curmask;
ffc5430b
KS
94}
95
1f3e56b6 96int ff_lzw_size_read(LZWState *p)
ffc5430b 97{
1f3e56b6 98 struct LZWState *s = p;
e89aa4bf 99 return bytestream2_tell(&s->gb);
ffc5430b
KS
100}
101
102void ff_lzw_decode_tail(LZWState *p)
103{
104 struct LZWState *s = (struct LZWState *)p;
5f01beb5
MN
105
106 if(s->mode == FF_LZW_GIF) {
e89aa4bf
AK
107 while (s->bs > 0 && bytestream2_get_bytes_left(&s->gb)) {
108 bytestream2_skip(&s->gb, s->bs);
109 s->bs = bytestream2_get_byte(&s->gb);
5f01beb5
MN
110 }
111 }else
e89aa4bf 112 bytestream2_skip(&s->gb, bytestream2_get_bytes_left(&s->gb));
ffc5430b
KS
113}
114
98a6fff9 115av_cold void ff_lzw_decode_open(LZWState **p)
ffc5430b
KS
116{
117 *p = av_mallocz(sizeof(struct LZWState));
118}
119
98a6fff9 120av_cold void ff_lzw_decode_close(LZWState **p)
ffc5430b
KS
121{
122 av_freep(p);
123}
124
125/**
126 * Initialize LZW decoder
9a58234f 127 * @param p LZW context
ffc5430b
KS
128 * @param csize initial code size in bits
129 * @param buf input data
130 * @param buf_size input data size
131 * @param mode decoder working mode - either GIF or TIFF
132 */
7993df65 133int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
ffc5430b
KS
134{
135 struct LZWState *s = (struct LZWState *)p;
136
b08edb22 137 if(csize < 1 || csize >= LZW_MAXBITS)
ffc5430b
KS
138 return -1;
139 /* read buffer */
e89aa4bf 140 bytestream2_init(&s->gb, buf, buf_size);
ffc5430b
KS
141 s->bbuf = 0;
142 s->bbits = 0;
143 s->bs = 0;
144
145 /* decoder */
146 s->codesize = csize;
147 s->cursize = s->codesize + 1;
148 s->curmask = mask[s->cursize];
149 s->top_slot = 1 << s->cursize;
150 s->clear_code = 1 << s->codesize;
151 s->end_code = s->clear_code + 1;
152 s->slot = s->newcodes = s->clear_code + 2;
34a370cb 153 s->oc = s->fc = -1;
ffc5430b
KS
154 s->sp = s->stack;
155
156 s->mode = mode;
0abfd533 157 s->extra_slot = s->mode == FF_LZW_TIFF;
ffc5430b
KS
158 return 0;
159}
160
161/**
162 * Decode given number of bytes
163 * NOTE: the algorithm here is inspired from the LZW GIF decoder
164 * written by Steven A. Bennett in 1987.
165 *
9a58234f 166 * @param p LZW context
ffc5430b
KS
167 * @param buf output buffer
168 * @param len number of bytes to decode
169 * @return number of bytes decoded
170 */
171int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
172 int l, c, code, oc, fc;
173 uint8_t *sp;
174 struct LZWState *s = (struct LZWState *)p;
175
176 if (s->end_code < 0)
177 return 0;
178
179 l = len;
180 sp = s->sp;
181 oc = s->oc;
182 fc = s->fc;
183
ffc5430b 184 for (;;) {
bf770961
MN
185 while (sp > s->stack) {
186 *buf++ = *(--sp);
187 if ((--l) == 0)
188 goto the_end;
189 }
ffc5430b
KS
190 c = lzw_get_code(s);
191 if (c == s->end_code) {
ffc5430b
KS
192 break;
193 } else if (c == s->clear_code) {
194 s->cursize = s->codesize + 1;
195 s->curmask = mask[s->cursize];
196 s->slot = s->newcodes;
197 s->top_slot = 1 << s->cursize;
34a370cb 198 fc= oc= -1;
ffc5430b
KS
199 } else {
200 code = c;
0cb7f8a2 201 if (code == s->slot && fc>=0) {
ffc5430b
KS
202 *sp++ = fc;
203 code = oc;
0cb7f8a2
MN
204 }else if(code >= s->slot)
205 break;
ffc5430b
KS
206 while (code >= s->newcodes) {
207 *sp++ = s->suffix[code];
208 code = s->prefix[code];
209 }
210 *sp++ = code;
34a370cb
MN
211 if (s->slot < s->top_slot && oc>=0) {
212 s->suffix[s->slot] = code;
ffc5430b 213 s->prefix[s->slot++] = oc;
ffc5430b 214 }
34a370cb
MN
215 fc = code;
216 oc = c;
62ef8925 217 if (s->slot >= s->top_slot - s->extra_slot) {
ffc5430b
KS
218 if (s->cursize < LZW_MAXBITS) {
219 s->top_slot <<= 1;
ffc5430b
KS
220 s->curmask = mask[++s->cursize];
221 }
222 }
ffc5430b
KS
223 }
224 }
0cb7f8a2 225 s->end_code = -1;
ffc5430b
KS
226 the_end:
227 s->sp = sp;
228 s->oc = oc;
229 s->fc = fc;
230 return len - l;
231}