lzw.c
Go to the documentation of this file.
1 /*
2  * LZW decoder
3  * Copyright (c) 2003 Fabrice Bellard
4  * Copyright (c) 2006 Konstantin Shishkov
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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.
12  *
13  * Libav 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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
30 #include "avcodec.h"
31 #include "lzw.h"
32 #include "libavutil/mem.h"
33 
34 #define LZW_MAXBITS 12
35 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
36 
37 static const uint16_t mask[17] =
38 {
39  0x0000, 0x0001, 0x0003, 0x0007,
40  0x000F, 0x001F, 0x003F, 0x007F,
41  0x00FF, 0x01FF, 0x03FF, 0x07FF,
42  0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
43 };
44 
45 struct LZWState {
46  const uint8_t *pbuf, *ebuf;
47  int bbits;
48  unsigned int bbuf;
49 
50  int mode;
51  int cursize;
52  int curmask;
53  int codesize;
55  int end_code;
56  int newcodes;
57  int top_slot;
59  int slot;
60  int fc, oc;
64  uint16_t prefix[LZW_SIZTABLE];
65  int bs;
66 };
67 
68 /* get one code from stream */
69 static int lzw_get_code(struct LZWState * s)
70 {
71  int c;
72 
73  if(s->mode == FF_LZW_GIF) {
74  while (s->bbits < s->cursize) {
75  if (!s->bs) {
76  s->bs = *s->pbuf++;
77  }
78  s->bbuf |= (*s->pbuf++) << s->bbits;
79  s->bbits += 8;
80  s->bs--;
81  }
82  c = s->bbuf;
83  s->bbuf >>= s->cursize;
84  } else { // TIFF
85  while (s->bbits < s->cursize) {
86  s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
87  s->bbits += 8;
88  }
89  c = s->bbuf >> (s->bbits - s->cursize);
90  }
91  s->bbits -= s->cursize;
92  return c & s->curmask;
93 }
94 
96 {
97  return ((struct LZWState*)p)->pbuf;
98 }
99 
101 {
102  struct LZWState *s = (struct LZWState *)p;
103 
104  if(s->mode == FF_LZW_GIF) {
105  while (s->bs > 0) {
106  if (s->bs >= s->ebuf - s->pbuf) {
107  s->pbuf = s->ebuf;
108  break;
109  } else {
110  s->pbuf += s->bs;
111  s->bs = *s->pbuf++;
112  }
113  }
114  }else
115  s->pbuf= s->ebuf;
116 }
117 
119 {
120  *p = av_mallocz(sizeof(struct LZWState));
121 }
122 
124 {
125  av_freep(p);
126 }
127 
136 int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
137 {
138  struct LZWState *s = (struct LZWState *)p;
139 
140  if(csize < 1 || csize >= LZW_MAXBITS)
141  return -1;
142  /* read buffer */
143  s->pbuf = buf;
144  s->ebuf = s->pbuf + buf_size;
145  s->bbuf = 0;
146  s->bbits = 0;
147  s->bs = 0;
148 
149  /* decoder */
150  s->codesize = csize;
151  s->cursize = s->codesize + 1;
152  s->curmask = mask[s->cursize];
153  s->top_slot = 1 << s->cursize;
154  s->clear_code = 1 << s->codesize;
155  s->end_code = s->clear_code + 1;
156  s->slot = s->newcodes = s->clear_code + 2;
157  s->oc = s->fc = -1;
158  s->sp = s->stack;
159 
160  s->mode = mode;
161  s->extra_slot = s->mode == FF_LZW_TIFF;
162  return 0;
163 }
164 
175 int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
176  int l, c, code, oc, fc;
177  uint8_t *sp;
178  struct LZWState *s = (struct LZWState *)p;
179 
180  if (s->end_code < 0)
181  return 0;
182 
183  l = len;
184  sp = s->sp;
185  oc = s->oc;
186  fc = s->fc;
187 
188  for (;;) {
189  while (sp > s->stack) {
190  *buf++ = *(--sp);
191  if ((--l) == 0)
192  goto the_end;
193  }
194  c = lzw_get_code(s);
195  if (c == s->end_code) {
196  break;
197  } else if (c == s->clear_code) {
198  s->cursize = s->codesize + 1;
199  s->curmask = mask[s->cursize];
200  s->slot = s->newcodes;
201  s->top_slot = 1 << s->cursize;
202  fc= oc= -1;
203  } else {
204  code = c;
205  if (code == s->slot && fc>=0) {
206  *sp++ = fc;
207  code = oc;
208  }else if(code >= s->slot)
209  break;
210  while (code >= s->newcodes) {
211  *sp++ = s->suffix[code];
212  code = s->prefix[code];
213  }
214  *sp++ = code;
215  if (s->slot < s->top_slot && oc>=0) {
216  s->suffix[s->slot] = code;
217  s->prefix[s->slot++] = oc;
218  }
219  fc = code;
220  oc = c;
221  if (s->slot >= s->top_slot - s->extra_slot) {
222  if (s->cursize < LZW_MAXBITS) {
223  s->top_slot <<= 1;
224  s->curmask = mask[++s->cursize];
225  }
226  }
227  }
228  }
229  s->end_code = -1;
230  the_end:
231  s->sp = sp;
232  s->oc = oc;
233  s->fc = fc;
234  return len - l;
235 }