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.

264 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. break;
  73. }
  74. }
  75. s->bbuf |= (*s->pbuf++) << s->bbits;
  76. s->bbits += 8;
  77. s->bs--;
  78. }
  79. c = s->bbuf & s->curmask;
  80. s->bbuf >>= s->cursize;
  81. } else { // TIFF
  82. while (s->bbits < s->cursize) {
  83. if (s->pbuf >= s->ebuf) {
  84. s->eob_reached = 1;
  85. }
  86. s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
  87. s->bbits += 8;
  88. }
  89. c = (s->bbuf >> (s->bbits - s->cursize)) & s->curmask;
  90. }
  91. s->bbits -= s->cursize;
  92. return c;
  93. }
  94. uint8_t* ff_lzw_cur_ptr(LZWState *p)
  95. {
  96. return ((struct LZWState*)p)->pbuf;
  97. }
  98. void ff_lzw_decode_tail(LZWState *p)
  99. {
  100. struct LZWState *s = (struct LZWState *)p;
  101. while(!s->eob_reached)
  102. lzw_get_code(s);
  103. }
  104. void ff_lzw_decode_open(LZWState **p)
  105. {
  106. *p = av_mallocz(sizeof(struct LZWState));
  107. }
  108. void ff_lzw_decode_close(LZWState **p)
  109. {
  110. av_freep(p);
  111. }
  112. /**
  113. * Initialize LZW decoder
  114. * @param s LZW context
  115. * @param csize initial code size in bits
  116. * @param buf input data
  117. * @param buf_size input data size
  118. * @param mode decoder working mode - either GIF or TIFF
  119. */
  120. int ff_lzw_decode_init(LZWState *p, int csize, uint8_t *buf, int buf_size, int mode)
  121. {
  122. struct LZWState *s = (struct LZWState *)p;
  123. if(csize < 1 || csize > LZW_MAXBITS)
  124. return -1;
  125. /* read buffer */
  126. s->eob_reached = 0;
  127. s->pbuf = buf;
  128. s->ebuf = s->pbuf + buf_size;
  129. s->bbuf = 0;
  130. s->bbits = 0;
  131. s->bs = 0;
  132. /* decoder */
  133. s->codesize = csize;
  134. s->cursize = s->codesize + 1;
  135. s->curmask = mask[s->cursize];
  136. s->top_slot = 1 << s->cursize;
  137. s->clear_code = 1 << s->codesize;
  138. s->end_code = s->clear_code + 1;
  139. s->slot = s->newcodes = s->clear_code + 2;
  140. s->oc = s->fc = 0;
  141. s->sp = s->stack;
  142. s->mode = mode;
  143. switch(s->mode){
  144. case FF_LZW_GIF:
  145. s->top_slot2 = s->top_slot;
  146. break;
  147. case FF_LZW_TIFF:
  148. s->top_slot2 = s->top_slot - 1;
  149. break;
  150. default:
  151. return -1;
  152. }
  153. return 0;
  154. }
  155. /**
  156. * Decode given number of bytes
  157. * NOTE: the algorithm here is inspired from the LZW GIF decoder
  158. * written by Steven A. Bennett in 1987.
  159. *
  160. * @param s LZW context
  161. * @param buf output buffer
  162. * @param len number of bytes to decode
  163. * @return number of bytes decoded
  164. */
  165. int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
  166. int l, c, code, oc, fc;
  167. uint8_t *sp;
  168. struct LZWState *s = (struct LZWState *)p;
  169. if (s->end_code < 0)
  170. return 0;
  171. l = len;
  172. sp = s->sp;
  173. oc = s->oc;
  174. fc = s->fc;
  175. while (sp > s->stack) {
  176. *buf++ = *(--sp);
  177. if ((--l) == 0)
  178. goto the_end;
  179. }
  180. for (;;) {
  181. c = lzw_get_code(s);
  182. if (c == s->end_code) {
  183. s->end_code = -1;
  184. break;
  185. } else if (c == s->clear_code) {
  186. s->cursize = s->codesize + 1;
  187. s->curmask = mask[s->cursize];
  188. s->slot = s->newcodes;
  189. s->top_slot = 1 << s->cursize;
  190. s->top_slot2 = s->top_slot;
  191. if(s->mode == FF_LZW_TIFF)
  192. s->top_slot2--;
  193. while ((c = lzw_get_code(s)) == s->clear_code);
  194. if (c == s->end_code) {
  195. s->end_code = -1;
  196. break;
  197. }
  198. /* test error */
  199. if (c >= s->slot)
  200. c = 0;
  201. fc = oc = c;
  202. *buf++ = c;
  203. if ((--l) == 0)
  204. break;
  205. } else {
  206. code = c;
  207. if (code >= s->slot) {
  208. *sp++ = fc;
  209. code = oc;
  210. }
  211. while (code >= s->newcodes) {
  212. *sp++ = s->suffix[code];
  213. code = s->prefix[code];
  214. }
  215. *sp++ = code;
  216. if (s->slot < s->top_slot) {
  217. s->suffix[s->slot] = fc = code;
  218. s->prefix[s->slot++] = oc;
  219. oc = c;
  220. }
  221. if (s->slot >= s->top_slot2) {
  222. if (s->cursize < LZW_MAXBITS) {
  223. s->top_slot <<= 1;
  224. s->top_slot2 = s->top_slot;
  225. if(s->mode == FF_LZW_TIFF)
  226. s->top_slot2--;
  227. s->curmask = mask[++s->cursize];
  228. }
  229. }
  230. while (sp > s->stack) {
  231. *buf++ = *(--sp);
  232. if ((--l) == 0)
  233. goto the_end;
  234. }
  235. }
  236. }
  237. the_end:
  238. s->sp = sp;
  239. s->oc = oc;
  240. s->fc = fc;
  241. return len - l;
  242. }