00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00030 #include "avcodec.h"
00031 #include "lzw.h"
00032 #include "libavutil/mem.h"
00033
00034 #define LZW_MAXBITS 12
00035 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
00036
00037 static const uint16_t mask[17] =
00038 {
00039 0x0000, 0x0001, 0x0003, 0x0007,
00040 0x000F, 0x001F, 0x003F, 0x007F,
00041 0x00FF, 0x01FF, 0x03FF, 0x07FF,
00042 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
00043 };
00044
00045 struct LZWState {
00046 const uint8_t *pbuf, *ebuf;
00047 int bbits;
00048 unsigned int bbuf;
00049
00050 int mode;
00051 int cursize;
00052 int curmask;
00053 int codesize;
00054 int clear_code;
00055 int end_code;
00056 int newcodes;
00057 int top_slot;
00058 int extra_slot;
00059 int slot;
00060 int fc, oc;
00061 uint8_t *sp;
00062 uint8_t stack[LZW_SIZTABLE];
00063 uint8_t suffix[LZW_SIZTABLE];
00064 uint16_t prefix[LZW_SIZTABLE];
00065 int bs;
00066 };
00067
00068
00069 static int lzw_get_code(struct LZWState * s)
00070 {
00071 int c;
00072
00073 if(s->mode == FF_LZW_GIF) {
00074 while (s->bbits < s->cursize) {
00075 if (!s->bs) {
00076 s->bs = *s->pbuf++;
00077 }
00078 s->bbuf |= (*s->pbuf++) << s->bbits;
00079 s->bbits += 8;
00080 s->bs--;
00081 }
00082 c = s->bbuf;
00083 s->bbuf >>= s->cursize;
00084 } else {
00085 while (s->bbits < s->cursize) {
00086 s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
00087 s->bbits += 8;
00088 }
00089 c = s->bbuf >> (s->bbits - s->cursize);
00090 }
00091 s->bbits -= s->cursize;
00092 return c & s->curmask;
00093 }
00094
00095 const uint8_t* ff_lzw_cur_ptr(LZWState *p)
00096 {
00097 return ((struct LZWState*)p)->pbuf;
00098 }
00099
00100 void ff_lzw_decode_tail(LZWState *p)
00101 {
00102 struct LZWState *s = (struct LZWState *)p;
00103
00104 if(s->mode == FF_LZW_GIF) {
00105 while (s->bs > 0) {
00106 if (s->bs >= s->ebuf - s->pbuf) {
00107 s->pbuf = s->ebuf;
00108 break;
00109 } else {
00110 s->pbuf += s->bs;
00111 s->bs = *s->pbuf++;
00112 }
00113 }
00114 }else
00115 s->pbuf= s->ebuf;
00116 }
00117
00118 av_cold void ff_lzw_decode_open(LZWState **p)
00119 {
00120 *p = av_mallocz(sizeof(struct LZWState));
00121 }
00122
00123 av_cold void ff_lzw_decode_close(LZWState **p)
00124 {
00125 av_freep(p);
00126 }
00127
00136 int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
00137 {
00138 struct LZWState *s = (struct LZWState *)p;
00139
00140 if(csize < 1 || csize >= LZW_MAXBITS)
00141 return -1;
00142
00143 s->pbuf = buf;
00144 s->ebuf = s->pbuf + buf_size;
00145 s->bbuf = 0;
00146 s->bbits = 0;
00147 s->bs = 0;
00148
00149
00150 s->codesize = csize;
00151 s->cursize = s->codesize + 1;
00152 s->curmask = mask[s->cursize];
00153 s->top_slot = 1 << s->cursize;
00154 s->clear_code = 1 << s->codesize;
00155 s->end_code = s->clear_code + 1;
00156 s->slot = s->newcodes = s->clear_code + 2;
00157 s->oc = s->fc = -1;
00158 s->sp = s->stack;
00159
00160 s->mode = mode;
00161 s->extra_slot = s->mode == FF_LZW_TIFF;
00162 return 0;
00163 }
00164
00175 int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
00176 int l, c, code, oc, fc;
00177 uint8_t *sp;
00178 struct LZWState *s = (struct LZWState *)p;
00179
00180 if (s->end_code < 0)
00181 return 0;
00182
00183 l = len;
00184 sp = s->sp;
00185 oc = s->oc;
00186 fc = s->fc;
00187
00188 for (;;) {
00189 while (sp > s->stack) {
00190 *buf++ = *(--sp);
00191 if ((--l) == 0)
00192 goto the_end;
00193 }
00194 if (s->ebuf < s->pbuf) {
00195 av_log(0, AV_LOG_ERROR, "lzw overread\n");
00196 goto the_end;
00197 }
00198 c = lzw_get_code(s);
00199 if (c == s->end_code) {
00200 break;
00201 } else if (c == s->clear_code) {
00202 s->cursize = s->codesize + 1;
00203 s->curmask = mask[s->cursize];
00204 s->slot = s->newcodes;
00205 s->top_slot = 1 << s->cursize;
00206 fc= oc= -1;
00207 } else {
00208 code = c;
00209 if (code == s->slot && fc>=0) {
00210 *sp++ = fc;
00211 code = oc;
00212 }else if(code >= s->slot)
00213 break;
00214 while (code >= s->newcodes) {
00215 *sp++ = s->suffix[code];
00216 code = s->prefix[code];
00217 }
00218 *sp++ = code;
00219 if (s->slot < s->top_slot && oc>=0) {
00220 s->suffix[s->slot] = code;
00221 s->prefix[s->slot++] = oc;
00222 }
00223 fc = code;
00224 oc = c;
00225 if (s->slot >= s->top_slot - s->extra_slot) {
00226 if (s->cursize < LZW_MAXBITS) {
00227 s->top_slot <<= 1;
00228 s->curmask = mask[++s->cursize];
00229 }
00230 }
00231 }
00232 }
00233 s->end_code = -1;
00234 the_end:
00235 s->sp = sp;
00236 s->oc = oc;
00237 s->fc = fc;
00238 return len - l;
00239 }