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.

235 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. void ff_lzw_decode_tail(LZWState *p)
  87. {
  88. struct LZWState *s = (struct LZWState *)p;
  89. if(s->mode == FF_LZW_GIF) {
  90. while (s->bs > 0) {
  91. if (s->bs >= s->ebuf - s->pbuf) {
  92. s->pbuf = s->ebuf;
  93. break;
  94. } else {
  95. s->pbuf += s->bs;
  96. s->bs = *s->pbuf++;
  97. }
  98. }
  99. }else
  100. s->pbuf= s->ebuf;
  101. }
  102. av_cold void ff_lzw_decode_open(LZWState **p)
  103. {
  104. *p = av_mallocz(sizeof(struct LZWState));
  105. }
  106. av_cold void ff_lzw_decode_close(LZWState **p)
  107. {
  108. av_freep(p);
  109. }
  110. /**
  111. * Initialize LZW decoder
  112. * @param p LZW context
  113. * @param csize initial code size in bits
  114. * @param buf input data
  115. * @param buf_size input data size
  116. * @param mode decoder working mode - either GIF or TIFF
  117. */
  118. int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
  119. {
  120. struct LZWState *s = (struct LZWState *)p;
  121. if(csize < 1 || csize >= LZW_MAXBITS)
  122. return -1;
  123. /* read buffer */
  124. s->pbuf = buf;
  125. s->ebuf = s->pbuf + buf_size;
  126. s->bbuf = 0;
  127. s->bbits = 0;
  128. s->bs = 0;
  129. /* decoder */
  130. s->codesize = csize;
  131. s->cursize = s->codesize + 1;
  132. s->curmask = mask[s->cursize];
  133. s->top_slot = 1 << s->cursize;
  134. s->clear_code = 1 << s->codesize;
  135. s->end_code = s->clear_code + 1;
  136. s->slot = s->newcodes = s->clear_code + 2;
  137. s->oc = s->fc = -1;
  138. s->sp = s->stack;
  139. s->mode = mode;
  140. s->extra_slot = s->mode == FF_LZW_TIFF;
  141. return 0;
  142. }
  143. /**
  144. * Decode given number of bytes
  145. * NOTE: the algorithm here is inspired from the LZW GIF decoder
  146. * written by Steven A. Bennett in 1987.
  147. *
  148. * @param p LZW context
  149. * @param buf output buffer
  150. * @param len number of bytes to decode
  151. * @return number of bytes decoded
  152. */
  153. int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
  154. int l, c, code, oc, fc;
  155. uint8_t *sp;
  156. struct LZWState *s = (struct LZWState *)p;
  157. if (s->end_code < 0)
  158. return 0;
  159. l = len;
  160. sp = s->sp;
  161. oc = s->oc;
  162. fc = s->fc;
  163. for (;;) {
  164. while (sp > s->stack) {
  165. *buf++ = *(--sp);
  166. if ((--l) == 0)
  167. goto the_end;
  168. }
  169. if (s->ebuf < s->pbuf) {
  170. av_log(NULL, AV_LOG_ERROR, "lzw overread\n");
  171. goto the_end;
  172. }
  173. c = lzw_get_code(s);
  174. if (c == s->end_code) {
  175. break;
  176. } else if (c == s->clear_code) {
  177. s->cursize = s->codesize + 1;
  178. s->curmask = mask[s->cursize];
  179. s->slot = s->newcodes;
  180. s->top_slot = 1 << s->cursize;
  181. fc= oc= -1;
  182. } else {
  183. code = c;
  184. if (code == s->slot && fc>=0) {
  185. *sp++ = fc;
  186. code = oc;
  187. }else if(code >= s->slot)
  188. break;
  189. while (code >= s->newcodes) {
  190. *sp++ = s->suffix[code];
  191. code = s->prefix[code];
  192. }
  193. *sp++ = code;
  194. if (s->slot < s->top_slot && oc>=0) {
  195. s->suffix[s->slot] = code;
  196. s->prefix[s->slot++] = oc;
  197. }
  198. fc = code;
  199. oc = c;
  200. if (s->slot >= s->top_slot - s->extra_slot) {
  201. if (s->cursize < LZW_MAXBITS) {
  202. s->top_slot <<= 1;
  203. s->curmask = mask[++s->cursize];
  204. }
  205. }
  206. }
  207. }
  208. s->end_code = -1;
  209. the_end:
  210. s->sp = sp;
  211. s->oc = oc;
  212. s->fc = fc;
  213. return len - l;
  214. }