You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
6.8KB

  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. * @file lzw.c
  24. * @brief LZW decoding routines
  25. * @author Fabrice Bellard
  26. * Modified for use in TIFF by Konstantin Shishkov
  27. */
  28. #include "avcodec.h"
  29. #include "lzw.h"
  30. #define LZW_MAXBITS 12
  31. #define LZW_SIZTABLE (1<<LZW_MAXBITS)
  32. static const uint16_t mask[17] =
  33. {
  34. 0x0000, 0x0001, 0x0003, 0x0007,
  35. 0x000F, 0x001F, 0x003F, 0x007F,
  36. 0x00FF, 0x01FF, 0x03FF, 0x07FF,
  37. 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
  38. };
  39. struct LZWState {
  40. int eob_reached;
  41. uint8_t *pbuf, *ebuf;
  42. int bbits;
  43. unsigned int bbuf;
  44. int mode; ///< Decoder mode
  45. int cursize; ///< The current code size
  46. int curmask;
  47. int codesize;
  48. int clear_code;
  49. int end_code;
  50. int newcodes; ///< First available code
  51. int top_slot; ///< Highest code for current size
  52. int top_slot2; ///< Highest possible code for current size (<=top_slot)
  53. int slot; ///< Last read code
  54. int fc, oc;
  55. uint8_t *sp;
  56. uint8_t stack[LZW_SIZTABLE];
  57. uint8_t suffix[LZW_SIZTABLE];
  58. uint16_t prefix[LZW_SIZTABLE];
  59. int bs; ///< current buffer size for GIF
  60. };
  61. /* get one code from stream */
  62. static int lzw_get_code(struct LZWState * s)
  63. {
  64. int c, sizbuf;
  65. if(s->mode == FF_LZW_GIF) {
  66. while (s->bbits < s->cursize) {
  67. if (!s->bs) {
  68. sizbuf = *s->pbuf++;
  69. s->bs = sizbuf;
  70. if(!sizbuf) {
  71. s->eob_reached = 1;
  72. }
  73. }
  74. s->bbuf |= (*s->pbuf++) << s->bbits;
  75. s->bbits += 8;
  76. s->bs--;
  77. }
  78. c = s->bbuf & s->curmask;
  79. s->bbuf >>= s->cursize;
  80. } else { // TIFF
  81. while (s->bbits < s->cursize) {
  82. if (s->pbuf >= s->ebuf) {
  83. s->eob_reached = 1;
  84. }
  85. s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
  86. s->bbits += 8;
  87. }
  88. c = (s->bbuf >> (s->bbits - s->cursize)) & s->curmask;
  89. }
  90. s->bbits -= s->cursize;
  91. return c;
  92. }
  93. uint8_t* ff_lzw_cur_ptr(LZWState *p)
  94. {
  95. return ((struct LZWState*)p)->pbuf;
  96. }
  97. void ff_lzw_decode_tail(LZWState *p)
  98. {
  99. struct LZWState *s = (struct LZWState *)p;
  100. while(!s->eob_reached)
  101. lzw_get_code(s);
  102. }
  103. void ff_lzw_decode_open(LZWState **p)
  104. {
  105. *p = av_mallocz(sizeof(struct LZWState));
  106. }
  107. void ff_lzw_decode_close(LZWState **p)
  108. {
  109. av_freep(p);
  110. }
  111. /**
  112. * Initialize LZW decoder
  113. * @param s LZW context
  114. * @param csize initial code size in bits
  115. * @param buf input data
  116. * @param buf_size input data size
  117. * @param mode decoder working mode - either GIF or TIFF
  118. */
  119. int ff_lzw_decode_init(LZWState *p, int csize, uint8_t *buf, int buf_size, int mode)
  120. {
  121. struct LZWState *s = (struct LZWState *)p;
  122. if(csize < 1 || csize > LZW_MAXBITS)
  123. return -1;
  124. /* read buffer */
  125. s->eob_reached = 0;
  126. s->pbuf = buf;
  127. s->ebuf = s->pbuf + buf_size;
  128. s->bbuf = 0;
  129. s->bbits = 0;
  130. s->bs = 0;
  131. /* decoder */
  132. s->codesize = csize;
  133. s->cursize = s->codesize + 1;
  134. s->curmask = mask[s->cursize];
  135. s->top_slot = 1 << s->cursize;
  136. s->clear_code = 1 << s->codesize;
  137. s->end_code = s->clear_code + 1;
  138. s->slot = s->newcodes = s->clear_code + 2;
  139. s->oc = s->fc = 0;
  140. s->sp = s->stack;
  141. s->mode = mode;
  142. switch(s->mode){
  143. case FF_LZW_GIF:
  144. s->top_slot2 = s->top_slot;
  145. break;
  146. case FF_LZW_TIFF:
  147. s->top_slot2 = s->top_slot - 1;
  148. break;
  149. default:
  150. return -1;
  151. }
  152. return 0;
  153. }
  154. /**
  155. * Decode given number of bytes
  156. * NOTE: the algorithm here is inspired from the LZW GIF decoder
  157. * written by Steven A. Bennett in 1987.
  158. *
  159. * @param s LZW context
  160. * @param buf output buffer
  161. * @param len number of bytes to decode
  162. * @return number of bytes decoded
  163. */
  164. int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
  165. int l, c, code, oc, fc;
  166. uint8_t *sp;
  167. struct LZWState *s = (struct LZWState *)p;
  168. if (s->end_code < 0)
  169. return 0;
  170. l = len;
  171. sp = s->sp;
  172. oc = s->oc;
  173. fc = s->fc;
  174. while (sp > s->stack) {
  175. *buf++ = *(--sp);
  176. if ((--l) == 0)
  177. goto the_end;
  178. }
  179. for (;;) {
  180. c = lzw_get_code(s);
  181. if (c == s->end_code) {
  182. s->end_code = -1;
  183. break;
  184. } else if (c == s->clear_code) {
  185. s->cursize = s->codesize + 1;
  186. s->curmask = mask[s->cursize];
  187. s->slot = s->newcodes;
  188. s->top_slot = 1 << s->cursize;
  189. s->top_slot2 = s->top_slot;
  190. if(s->mode == FF_LZW_TIFF)
  191. s->top_slot2--;
  192. while ((c = lzw_get_code(s)) == s->clear_code);
  193. if (c == s->end_code) {
  194. s->end_code = -1;
  195. break;
  196. }
  197. /* test error */
  198. if (c >= s->slot)
  199. c = 0;
  200. fc = oc = c;
  201. *buf++ = c;
  202. if ((--l) == 0)
  203. break;
  204. } else {
  205. code = c;
  206. if (code >= s->slot) {
  207. *sp++ = fc;
  208. code = oc;
  209. }
  210. while (code >= s->newcodes) {
  211. *sp++ = s->suffix[code];
  212. code = s->prefix[code];
  213. }
  214. *sp++ = code;
  215. if (s->slot < s->top_slot) {
  216. s->suffix[s->slot] = fc = code;
  217. s->prefix[s->slot++] = oc;
  218. oc = c;
  219. }
  220. if (s->slot >= s->top_slot2) {
  221. if (s->cursize < LZW_MAXBITS) {
  222. s->top_slot <<= 1;
  223. s->top_slot2 = s->top_slot;
  224. if(s->mode == FF_LZW_TIFF)
  225. s->top_slot2--;
  226. s->curmask = mask[++s->cursize];
  227. }
  228. }
  229. while (sp > s->stack) {
  230. *buf++ = *(--sp);
  231. if ((--l) == 0)
  232. goto the_end;
  233. }
  234. }
  235. }
  236. the_end:
  237. s->sp = sp;
  238. s->oc = oc;
  239. s->fc = fc;
  240. return len - l;
  241. }