Statistics
| Branch: | Revision:

ffmpeg / libavcodec / lzw.c @ 7a886063

History | View | Annotate | Download (6.8 KB)

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
                }
81
            }
82
            s->bbuf |= (*s->pbuf++) << s->bbits;
83
            s->bbits += 8;
84
            s->bs--;
85
        }
86
        c = s->bbuf & s->curmask;
87
       s->bbuf >>= s->cursize;
88
    } else { // TIFF
89
        while (s->bbits < s->cursize) {
90
            if (s->pbuf >= s->ebuf) {
91
                s->eob_reached = 1;
92
            }
93
            s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
94
            s->bbits += 8;
95
        }
96
        c = (s->bbuf >> (s->bbits - s->cursize)) & s->curmask;
97
    }
98
    s->bbits -= s->cursize;
99
    return c;
100
}
101

    
102
uint8_t* ff_lzw_cur_ptr(LZWState *p)
103
{
104
    return ((struct LZWState*)p)->pbuf;
105
}
106

    
107
void ff_lzw_decode_tail(LZWState *p)
108
{
109
    struct LZWState *s = (struct LZWState *)p;
110
    while(!s->eob_reached)
111
        lzw_get_code(s);
112
}
113

    
114
void ff_lzw_decode_open(LZWState **p)
115
{
116
    *p = av_mallocz(sizeof(struct LZWState));
117
}
118

    
119
void ff_lzw_decode_close(LZWState **p)
120
{
121
    av_freep(p);
122
}
123

    
124
/**
125
 * Initialize LZW decoder
126
 * @param s LZW context
127
 * @param csize initial code size in bits
128
 * @param buf input data
129
 * @param buf_size input data size
130
 * @param mode decoder working mode - either GIF or TIFF
131
 */
132
int ff_lzw_decode_init(LZWState *p, int csize, uint8_t *buf, int buf_size, int mode)
133
{
134
    struct LZWState *s = (struct LZWState *)p;
135

    
136
    if(csize < 1 || csize > LZW_MAXBITS)
137
        return -1;
138
    /* read buffer */
139
    s->eob_reached = 0;
140
    s->pbuf = buf;
141
    s->ebuf = s->pbuf + buf_size;
142
    s->bbuf = 0;
143
    s->bbits = 0;
144
    s->bs = 0;
145

    
146
    /* decoder */
147
    s->codesize = csize;
148
    s->cursize = s->codesize + 1;
149
    s->curmask = mask[s->cursize];
150
    s->top_slot = 1 << s->cursize;
151
    s->clear_code = 1 << s->codesize;
152
    s->end_code = s->clear_code + 1;
153
    s->slot = s->newcodes = s->clear_code + 2;
154
    s->oc = s->fc = 0;
155
    s->sp = s->stack;
156

    
157
    s->mode = mode;
158
    switch(s->mode){
159
    case FF_LZW_GIF:
160
        s->top_slot2 = s->top_slot;
161
        break;
162
    case FF_LZW_TIFF:
163
        s->top_slot2 = s->top_slot - 1;
164
        break;
165
    default:
166
        return -1;
167
    }
168
    return 0;
169
}
170

    
171
/**
172
 * Decode given number of bytes
173
 * NOTE: the algorithm here is inspired from the LZW GIF decoder
174
 *  written by Steven A. Bennett in 1987.
175
 *
176
 * @param s LZW context
177
 * @param buf output buffer
178
 * @param len number of bytes to decode
179
 * @return number of bytes decoded
180
 */
181
int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
182
    int l, c, code, oc, fc;
183
    uint8_t *sp;
184
    struct LZWState *s = (struct LZWState *)p;
185

    
186
    if (s->end_code < 0)
187
        return 0;
188

    
189
    l = len;
190
    sp = s->sp;
191
    oc = s->oc;
192
    fc = s->fc;
193

    
194
    while (sp > s->stack) {
195
        *buf++ = *(--sp);
196
        if ((--l) == 0)
197
            goto the_end;
198
    }
199

    
200
    for (;;) {
201
        c = lzw_get_code(s);
202
        if (c == s->end_code) {
203
            s->end_code = -1;
204
            break;
205
        } else if (c == s->clear_code) {
206
            s->cursize = s->codesize + 1;
207
            s->curmask = mask[s->cursize];
208
            s->slot = s->newcodes;
209
            s->top_slot = 1 << s->cursize;
210
            s->top_slot2 = s->top_slot;
211
            if(s->mode == FF_LZW_TIFF)
212
                s->top_slot2--;
213
            while ((c = lzw_get_code(s)) == s->clear_code);
214
            if (c == s->end_code) {
215
                s->end_code = -1;
216
                break;
217
            }
218
            /* test error */
219
            if (c >= s->slot)
220
                c = 0;
221
            fc = oc = c;
222
            *buf++ = c;
223
            if ((--l) == 0)
224
                break;
225
        } else {
226
            code = c;
227
            if (code >= s->slot) {
228
                *sp++ = fc;
229
                code = oc;
230
            }
231
            while (code >= s->newcodes) {
232
                *sp++ = s->suffix[code];
233
                code = s->prefix[code];
234
            }
235
            *sp++ = code;
236
            if (s->slot < s->top_slot) {
237
                s->suffix[s->slot] = fc = code;
238
                s->prefix[s->slot++] = oc;
239
                oc = c;
240
            }
241
            if (s->slot >= s->top_slot2) {
242
                if (s->cursize < LZW_MAXBITS) {
243
                    s->top_slot <<= 1;
244
                    s->top_slot2 = s->top_slot;
245
                    if(s->mode == FF_LZW_TIFF)
246
                        s->top_slot2--;
247
                    s->curmask = mask[++s->cursize];
248
                }
249
            }
250
            while (sp > s->stack) {
251
                *buf++ = *(--sp);
252
                if ((--l) == 0)
253
                    goto the_end;
254
            }
255
        }
256
    }
257
  the_end:
258
    s->sp = sp;
259
    s->oc = oc;
260
    s->fc = fc;
261
    return len - l;
262
}