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