]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/lzw.c
break if eob is reached to avoid reading one too much byte
[frescor/ffmpeg.git] / libavcodec / lzw.c
1 /*
2  * LZW decoder
3  * Copyright (c) 2003 Fabrice Bellard.
4  * Copyright (c) 2006 Konstantin Shishkov.
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /**
24  * @file lzw.c
25  * @brief LZW decoding routines
26  * @author Fabrice Bellard
27  * Modified for use in TIFF by Konstantin Shishkov
28  */
29
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     int eob_reached;
46     uint8_t *pbuf, *ebuf;
47     int bbits;
48     unsigned int bbuf;
49
50     int mode;                   ///< Decoder mode
51     int cursize;                ///< The current code size
52     int curmask;
53     int codesize;
54     int clear_code;
55     int end_code;
56     int newcodes;               ///< First available code
57     int top_slot;               ///< Highest code for current size
58     int top_slot2;              ///< Highest possible code for current size (<=top_slot)
59     int slot;                   ///< Last read code
60     int fc, oc;
61     uint8_t *sp;
62     uint8_t stack[LZW_SIZTABLE];
63     uint8_t suffix[LZW_SIZTABLE];
64     uint16_t prefix[LZW_SIZTABLE];
65     int bs;                     ///< current buffer size for GIF
66 };
67
68 /* get one code from stream */
69 static int lzw_get_code(struct LZWState * s)
70 {
71     int c, sizbuf;
72
73     if(s->mode == FF_LZW_GIF) {
74         while (s->bbits < s->cursize) {
75             if (!s->bs) {
76                 sizbuf = *s->pbuf++;
77                 s->bs = sizbuf;
78                 if(!sizbuf) {
79                     s->eob_reached = 1;
80                     break;
81                 }
82             }
83             s->bbuf |= (*s->pbuf++) << s->bbits;
84             s->bbits += 8;
85             s->bs--;
86         }
87         c = s->bbuf & s->curmask;
88        s->bbuf >>= s->cursize;
89     } else { // TIFF
90         while (s->bbits < s->cursize) {
91             if (s->pbuf >= s->ebuf) {
92                 s->eob_reached = 1;
93             }
94             s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
95             s->bbits += 8;
96         }
97         c = (s->bbuf >> (s->bbits - s->cursize)) & s->curmask;
98     }
99     s->bbits -= s->cursize;
100     return c;
101 }
102
103 uint8_t* ff_lzw_cur_ptr(LZWState *p)
104 {
105     return ((struct LZWState*)p)->pbuf;
106 }
107
108 void ff_lzw_decode_tail(LZWState *p)
109 {
110     struct LZWState *s = (struct LZWState *)p;
111     while(!s->eob_reached)
112         lzw_get_code(s);
113 }
114
115 void ff_lzw_decode_open(LZWState **p)
116 {
117     *p = av_mallocz(sizeof(struct LZWState));
118 }
119
120 void ff_lzw_decode_close(LZWState **p)
121 {
122     av_freep(p);
123 }
124
125 /**
126  * Initialize LZW decoder
127  * @param s LZW context
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  */
133 int ff_lzw_decode_init(LZWState *p, int csize, uint8_t *buf, int buf_size, int mode)
134 {
135     struct LZWState *s = (struct LZWState *)p;
136
137     if(csize < 1 || csize > LZW_MAXBITS)
138         return -1;
139     /* read buffer */
140     s->eob_reached = 0;
141     s->pbuf = buf;
142     s->ebuf = s->pbuf + buf_size;
143     s->bbuf = 0;
144     s->bbits = 0;
145     s->bs = 0;
146
147     /* decoder */
148     s->codesize = csize;
149     s->cursize = s->codesize + 1;
150     s->curmask = mask[s->cursize];
151     s->top_slot = 1 << s->cursize;
152     s->clear_code = 1 << s->codesize;
153     s->end_code = s->clear_code + 1;
154     s->slot = s->newcodes = s->clear_code + 2;
155     s->oc = s->fc = 0;
156     s->sp = s->stack;
157
158     s->mode = mode;
159     switch(s->mode){
160     case FF_LZW_GIF:
161         s->top_slot2 = s->top_slot;
162         break;
163     case FF_LZW_TIFF:
164         s->top_slot2 = s->top_slot - 1;
165         break;
166     default:
167         return -1;
168     }
169     return 0;
170 }
171
172 /**
173  * Decode given number of bytes
174  * NOTE: the algorithm here is inspired from the LZW GIF decoder
175  *  written by Steven A. Bennett in 1987.
176  *
177  * @param s LZW context
178  * @param buf output buffer
179  * @param len number of bytes to decode
180  * @return number of bytes decoded
181  */
182 int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
183     int l, c, code, oc, fc;
184     uint8_t *sp;
185     struct LZWState *s = (struct LZWState *)p;
186
187     if (s->end_code < 0)
188         return 0;
189
190     l = len;
191     sp = s->sp;
192     oc = s->oc;
193     fc = s->fc;
194
195     while (sp > s->stack) {
196         *buf++ = *(--sp);
197         if ((--l) == 0)
198             goto the_end;
199     }
200
201     for (;;) {
202         c = lzw_get_code(s);
203         if (c == s->end_code) {
204             s->end_code = -1;
205             break;
206         } else if (c == s->clear_code) {
207             s->cursize = s->codesize + 1;
208             s->curmask = mask[s->cursize];
209             s->slot = s->newcodes;
210             s->top_slot = 1 << s->cursize;
211             s->top_slot2 = s->top_slot;
212             if(s->mode == FF_LZW_TIFF)
213                 s->top_slot2--;
214             while ((c = lzw_get_code(s)) == s->clear_code);
215             if (c == s->end_code) {
216                 s->end_code = -1;
217                 break;
218             }
219             /* test error */
220             if (c >= s->slot)
221                 c = 0;
222             fc = oc = c;
223             *buf++ = c;
224             if ((--l) == 0)
225                 break;
226         } else {
227             code = c;
228             if (code >= s->slot) {
229                 *sp++ = fc;
230                 code = oc;
231             }
232             while (code >= s->newcodes) {
233                 *sp++ = s->suffix[code];
234                 code = s->prefix[code];
235             }
236             *sp++ = code;
237             if (s->slot < s->top_slot) {
238                 s->suffix[s->slot] = fc = code;
239                 s->prefix[s->slot++] = oc;
240                 oc = c;
241             }
242             if (s->slot >= s->top_slot2) {
243                 if (s->cursize < LZW_MAXBITS) {
244                     s->top_slot <<= 1;
245                     s->top_slot2 = s->top_slot;
246                     if(s->mode == FF_LZW_TIFF)
247                         s->top_slot2--;
248                     s->curmask = mask[++s->cursize];
249                 }
250             }
251             while (sp > s->stack) {
252                 *buf++ = *(--sp);
253                 if ((--l) == 0)
254                     goto the_end;
255             }
256         }
257     }
258   the_end:
259     s->sp = sp;
260     s->oc = oc;
261     s->fc = fc;
262     return len - l;
263 }