FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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
25  * @brief LZW decoding routines
26  * @author Fabrice Bellard
27  * @author modified for use in TIFF by Konstantin Shishkov
28  */
29 
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "lzw.h"
33 #include "libavutil/mem.h"
34 
35 #define LZW_MAXBITS 12
36 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
37 
38 static 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 
46 struct LZWState {
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;
56  int end_code;
57  int newcodes; ///< First available code
58  int top_slot; ///< Highest code for current size
60  int slot; ///< Last read code
61  int fc, oc;
65  uint16_t prefix[LZW_SIZTABLE];
66  int bs; ///< current buffer size for GIF
67 };
68 
69 /* get one code from stream */
70 static int lzw_get_code(struct LZWState * s)
71 {
72  int c;
73 
74  if(s->mode == FF_LZW_GIF) {
75  while (s->bbits < s->cursize) {
76  if (!s->bs) {
77  s->bs = bytestream2_get_byte(&s->gb);
78  }
79  s->bbuf |= bytestream2_get_byte(&s->gb) << s->bbits;
80  s->bbits += 8;
81  s->bs--;
82  }
83  c = s->bbuf;
84  s->bbuf >>= s->cursize;
85  } else { // TIFF
86  while (s->bbits < s->cursize) {
87  s->bbuf = (s->bbuf << 8) | bytestream2_get_byte(&s->gb);
88  s->bbits += 8;
89  }
90  c = s->bbuf >> (s->bbits - s->cursize);
91  }
92  s->bbits -= s->cursize;
93  return c & s->curmask;
94 }
95 
97 {
98  struct LZWState *s = (struct LZWState *)p;
99 
100  if(s->mode == FF_LZW_GIF) {
101  while (s->bs > 0 && bytestream2_get_bytes_left(&s->gb)) {
102  bytestream2_skip(&s->gb, s->bs);
103  s->bs = bytestream2_get_byte(&s->gb);
104  }
105  }else
107  return bytestream2_tell(&s->gb);
108 }
109 
111 {
112  *p = av_mallocz(sizeof(struct LZWState));
113 }
114 
116 {
117  av_freep(p);
118 }
119 
120 /**
121  * Initialize LZW decoder
122  * @param p LZW context
123  * @param csize initial code size in bits
124  * @param buf input data
125  * @param buf_size input data size
126  * @param mode decoder working mode - either GIF or TIFF
127  */
128 int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
129 {
130  struct LZWState *s = (struct LZWState *)p;
131 
132  if(csize < 1 || csize >= LZW_MAXBITS)
133  return -1;
134  /* read buffer */
135  bytestream2_init(&s->gb, buf, buf_size);
136  s->bbuf = 0;
137  s->bbits = 0;
138  s->bs = 0;
139 
140  /* decoder */
141  s->codesize = csize;
142  s->cursize = s->codesize + 1;
143  s->curmask = mask[s->cursize];
144  s->top_slot = 1 << s->cursize;
145  s->clear_code = 1 << s->codesize;
146  s->end_code = s->clear_code + 1;
147  s->slot = s->newcodes = s->clear_code + 2;
148  s->oc = s->fc = -1;
149  s->sp = s->stack;
150 
151  s->mode = mode;
152  s->extra_slot = s->mode == FF_LZW_TIFF;
153  return 0;
154 }
155 
156 /**
157  * Decode given number of bytes
158  * NOTE: the algorithm here is inspired from the LZW GIF decoder
159  * written by Steven A. Bennett in 1987.
160  *
161  * @param p LZW context
162  * @param buf output buffer
163  * @param len number of bytes to decode
164  * @return number of bytes decoded
165  */
167  int l, c, code, oc, fc;
168  uint8_t *sp;
169  struct LZWState *s = (struct LZWState *)p;
170 
171  if (s->end_code < 0)
172  return 0;
173 
174  l = len;
175  sp = s->sp;
176  oc = s->oc;
177  fc = s->fc;
178 
179  for (;;) {
180  while (sp > s->stack) {
181  *buf++ = *(--sp);
182  if ((--l) == 0)
183  goto the_end;
184  }
185  c = lzw_get_code(s);
186  if (c == s->end_code) {
187  break;
188  } else if (c == s->clear_code) {
189  s->cursize = s->codesize + 1;
190  s->curmask = mask[s->cursize];
191  s->slot = s->newcodes;
192  s->top_slot = 1 << s->cursize;
193  fc= oc= -1;
194  } else {
195  code = c;
196  if (code == s->slot && fc>=0) {
197  *sp++ = fc;
198  code = oc;
199  }else if(code >= s->slot)
200  break;
201  while (code >= s->newcodes) {
202  *sp++ = s->suffix[code];
203  code = s->prefix[code];
204  }
205  *sp++ = code;
206  if (s->slot < s->top_slot && oc>=0) {
207  s->suffix[s->slot] = code;
208  s->prefix[s->slot++] = oc;
209  }
210  fc = code;
211  oc = c;
212  if (s->slot >= s->top_slot - s->extra_slot) {
213  if (s->cursize < LZW_MAXBITS) {
214  s->top_slot <<= 1;
215  s->curmask = mask[++s->cursize];
216  }
217  }
218  }
219  }
220  s->end_code = -1;
221  the_end:
222  s->sp = sp;
223  s->oc = oc;
224  s->fc = fc;
225  return len - l;
226 }
int ff_lzw_decode(LZWState *p, uint8_t *buf, int len)
Decode given number of bytes NOTE: the algorithm here is inspired from the LZW GIF decoder written by...
Definition: lzw.c:166
const char * s
Definition: avisynth_c.h:768
int cursize
The current code size.
Definition: lzw.c:52
Memory handling functions.
uint16_t prefix[LZW_SIZTABLE]
Definition: lzw.c:65
av_cold void ff_lzw_decode_close(LZWState **p)
Definition: lzw.c:115
av_cold void ff_lzw_decode_open(LZWState **p)
Definition: lzw.c:110
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
int clear_code
Definition: lzw.c:55
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
uint8_t suffix[LZW_SIZTABLE]
Definition: lzw.c:64
int top_slot
Highest code for current size.
Definition: lzw.c:58
int ff_lzw_decode_tail(LZWState *p)
Definition: lzw.c:96
uint8_t
#define av_cold
Definition: attributes.h:82
int bbits
Definition: lzw.c:48
uint8_t stack[LZW_SIZTABLE]
Definition: lzw.c:63
int newcodes
First available code.
Definition: lzw.c:57
#define sp
Definition: regdef.h:63
int end_code
Definition: lzw.c:56
Definition: lzw.c:46
#define LZW_SIZTABLE
Definition: lzw.c:36
static const uint16_t mask[17]
Definition: lzw.c:38
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
int extra_slot
Definition: lzw.c:59
uint8_t * sp
Definition: lzw.c:62
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
static const uint16_t fc[]
Definition: dcaenc.h:43
unsigned int bbuf
Definition: lzw.c:49
Definition: lzw.h:38
int fc
Definition: lzw.c:61
int codesize
Definition: lzw.c:54
int bs
current buffer size for GIF
Definition: lzw.c:66
static int lzw_get_code(struct LZWState *s)
Definition: lzw.c:70
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
Libavcodec external API header.
GetByteContext gb
Definition: lzw.c:47
void * buf
Definition: avisynth_c.h:690
int slot
Last read code.
Definition: lzw.c:60
int oc
Definition: lzw.c:61
int curmask
Definition: lzw.c:53
LZW decoding routines.
#define LZW_MAXBITS
Definition: lzw.c:35
static double c[64]
int mode
Decoder mode.
Definition: lzw.c:51
int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
Initialize LZW decoder.
Definition: lzw.c:128
int len
#define av_freep(p)
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83