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 
33 #define LZW_MAXBITS 12
34 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
35 
36 static const uint16_t mask[17] =
37 {
38  0x0000, 0x0001, 0x0003, 0x0007,
39  0x000F, 0x001F, 0x003F, 0x007F,
40  0x00FF, 0x01FF, 0x03FF, 0x07FF,
41  0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
42 };
43 
44 struct LZWState {
45  const uint8_t *pbuf, *ebuf;
46  int bbits;
47  unsigned int bbuf;
48 
49  int mode;
50  int cursize;
51  int curmask;
52  int codesize;
54  int end_code;
55  int newcodes;
56  int top_slot;
58  int slot;
59  int fc, oc;
60  uint8_t *sp;
61  uint8_t stack[LZW_SIZTABLE];
62  uint8_t suffix[LZW_SIZTABLE];
63  uint16_t prefix[LZW_SIZTABLE];
64  int bs;
65 };
66 
67 /* get one code from stream */
68 static int lzw_get_code(struct LZWState * s)
69 {
70  int c;
71 
72  if(s->mode == FF_LZW_GIF) {
73  while (s->bbits < s->cursize) {
74  if (!s->bs) {
75  s->bs = *s->pbuf++;
76  }
77  s->bbuf |= (*s->pbuf++) << s->bbits;
78  s->bbits += 8;
79  s->bs--;
80  }
81  c = s->bbuf;
82  s->bbuf >>= s->cursize;
83  } else { // TIFF
84  while (s->bbits < s->cursize) {
85  s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
86  s->bbits += 8;
87  }
88  c = s->bbuf >> (s->bbits - s->cursize);
89  }
90  s->bbits -= s->cursize;
91  return c & s->curmask;
92 }
93 
94 const uint8_t* ff_lzw_cur_ptr(LZWState *p)
95 {
96  return ((struct LZWState*)p)->pbuf;
97 }
98 
100 {
101  struct LZWState *s = (struct LZWState *)p;
102 
103  if(s->mode == FF_LZW_GIF) {
104  while (s->bs > 0) {
105  if (s->pbuf + s->bs >= s->ebuf) {
106  s->pbuf = s->ebuf;
107  break;
108  } else {
109  s->pbuf += s->bs;
110  s->bs = *s->pbuf++;
111  }
112  }
113  }else
114  s->pbuf= s->ebuf;
115 }
116 
118 {
119  *p = av_mallocz(sizeof(struct LZWState));
120 }
121 
123 {
124  av_freep(p);
125 }
126 
135 int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
136 {
137  struct LZWState *s = (struct LZWState *)p;
138 
139  if(csize < 1 || csize >= LZW_MAXBITS)
140  return -1;
141  /* read buffer */
142  s->pbuf = buf;
143  s->ebuf = s->pbuf + buf_size;
144  s->bbuf = 0;
145  s->bbits = 0;
146  s->bs = 0;
147 
148  /* decoder */
149  s->codesize = csize;
150  s->cursize = s->codesize + 1;
151  s->curmask = mask[s->cursize];
152  s->top_slot = 1 << s->cursize;
153  s->clear_code = 1 << s->codesize;
154  s->end_code = s->clear_code + 1;
155  s->slot = s->newcodes = s->clear_code + 2;
156  s->oc = s->fc = -1;
157  s->sp = s->stack;
158 
159  s->mode = mode;
160  s->extra_slot = s->mode == FF_LZW_TIFF;
161  return 0;
162 }
163 
174 int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
175  int l, c, code, oc, fc;
176  uint8_t *sp;
177  struct LZWState *s = (struct LZWState *)p;
178 
179  if (s->end_code < 0)
180  return 0;
181 
182  l = len;
183  sp = s->sp;
184  oc = s->oc;
185  fc = s->fc;
186 
187  for (;;) {
188  while (sp > s->stack) {
189  *buf++ = *(--sp);
190  if ((--l) == 0)
191  goto the_end;
192  }
193  c = lzw_get_code(s);
194  if (c == s->end_code) {
195  break;
196  } else if (c == s->clear_code) {
197  s->cursize = s->codesize + 1;
198  s->curmask = mask[s->cursize];
199  s->slot = s->newcodes;
200  s->top_slot = 1 << s->cursize;
201  fc= oc= -1;
202  } else {
203  code = c;
204  if (code == s->slot && fc>=0) {
205  *sp++ = fc;
206  code = oc;
207  }else if(code >= s->slot)
208  break;
209  while (code >= s->newcodes) {
210  *sp++ = s->suffix[code];
211  code = s->prefix[code];
212  }
213  *sp++ = code;
214  if (s->slot < s->top_slot && oc>=0) {
215  s->suffix[s->slot] = code;
216  s->prefix[s->slot++] = oc;
217  }
218  fc = code;
219  oc = c;
220  if (s->slot >= s->top_slot - s->extra_slot) {
221  if (s->cursize < LZW_MAXBITS) {
222  s->top_slot <<= 1;
223  s->curmask = mask[++s->cursize];
224  }
225  }
226  }
227  }
228  s->end_code = -1;
229  the_end:
230  s->sp = sp;
231  s->oc = oc;
232  s->fc = fc;
233  return len - l;
234 }