3 * Copyright (c) 2003 Fabrice Bellard.
4 * Copyright (c) 2006 Konstantin Shishkov.
6 * This file is part of FFmpeg.
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.
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.
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
24 #ifdef COMPRESSED_CONFIG_FILE
26 #include "firmware_extractor.h"
31 static const uint16_t mask[17] =
33 0x0000, 0x0001, 0x0003, 0x0007,
34 0x000F, 0x001F, 0x003F, 0x007F,
35 0x00FF, 0x01FF, 0x03FF, 0x07FF,
36 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
46 /* get one code from stream */
47 static int lzw_get_code(LZWDecoderState *s)
51 /* always use TIFF mode */
53 while (s->bbits < s->cursize) {
54 s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
57 // printf("TIFF: bbuf=0x%08x bbits=%d cursize=%d\n", s->bbuf, s->bbits, s->cursize);
58 c = s->bbuf >> (s->bbits - s->cursize);
60 s->bbits -= s->cursize;
62 // printf("bbits=%d c=0x%08x curmask=0x%08x\n", s->bbits, c, s->curmask);
63 return c & s->curmask;
67 void ff_lzw_decode_tail(LZWDecoderState *s)
69 /* always use TIFF mode */
74 CmsRet cmsLzw_initDecoder(LZWDecoderState **p, UINT8 *inbuf, UINT32 inbuf_size)
77 int mode = FF_LZW_TIFF; /* always use TIFF mode */
78 int csize = 8; /* the encoder side has this hardcoded, so hardcode here too */
80 *p = (LZWDecoderState *) cmsMem_alloc(sizeof(LZWDecoderState), ALLOC_ZEROIZE);
83 cmsLog_error("could not allocate %d bytes for decoder state", sizeof(LZWDecoderState));
84 return CMSRET_RESOURCE_EXCEEDED;
88 cmsLog_debug("%d bytes allocated for decoder state", sizeof(LZWDecoderState));
95 s->ebuf = s->pbuf + inbuf_size;
102 s->cursize = s->codesize + 1;
103 s->curmask = mask[s->cursize];
104 s->top_slot = 1 << s->cursize;
105 s->clear_code = 1 << s->codesize;
106 s->end_code = s->clear_code + 1;
107 s->slot = s->newcodes = s->clear_code + 2;
112 s->extra_slot = (s->mode == FF_LZW_TIFF);
114 return CMSRET_SUCCESS;
118 SINT32 cmsLzw_decode(LZWDecoderState *s, UINT8 *outbuf, UINT32 outlen)
134 while (sp > s->stack) {
135 // printf("transfer stack to buf, sp=0x%02x buf=%p\n", *sp, outbuf);
142 if (c == s->end_code) {
143 cmsLog_debug("got end code %d", c);
145 } else if (c == s->clear_code) {
146 cmsLog_debug("got clear code %d", c);
147 s->cursize = s->codesize + 1;
148 s->curmask = mask[s->cursize];
149 s->slot = s->newcodes;
150 s->top_slot = 1 << s->cursize;
154 // printf("got valid code %d (0x%02x)\n", c, c);
156 if (code == s->slot && fc>=0) {
159 }else if(code >= s->slot) {
160 cmsLog_error("code %d greater than slot %d", code, s->slot);
164 while (code >= s->newcodes) {
165 // printf("transfer suffix to to sp \n");
166 *sp++ = s->suffix[code];
167 code = s->prefix[code];
170 // printf("sp=%p gets code %d\n", sp, code);
174 if (s->slot < s->top_slot && oc>=0) {
175 // printf("suffix[%d]=%d prefix[%d]=%d\n", s->slot, code, s->slot, oc);
176 s->suffix[s->slot] = code;
177 s->prefix[s->slot++] = oc;
182 if (s->slot >= s->top_slot - s->extra_slot) {
183 if (s->cursize < LZW_MAXBITS) {
185 s->curmask = mask[++s->cursize];
186 // printf("new top_slot=0x%x curmask=0x%x\n", s->top_slot, s->curmask);
199 cmsLog_debug("about to return, outlen=%d l=%d\n", outlen, l);
205 void cmsLzw_cleanupDecoder(LZWDecoderState **s)
213 #endif /* COMPRESSED_CONFIG_FILE */