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.

240 lines
6.1KB

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