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.

232 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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "bytestream.h"
  30. #include "lzw.h"
  31. #include "libavutil/mem.h"
  32. #define LZW_MAXBITS 12
  33. #define LZW_SIZTABLE (1<<LZW_MAXBITS)
  34. static const uint16_t mask[17] =
  35. {
  36. 0x0000, 0x0001, 0x0003, 0x0007,
  37. 0x000F, 0x001F, 0x003F, 0x007F,
  38. 0x00FF, 0x01FF, 0x03FF, 0x07FF,
  39. 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
  40. };
  41. struct LZWState {
  42. GetByteContext gb;
  43. int bbits;
  44. unsigned int bbuf;
  45. int mode; ///< Decoder mode
  46. int cursize; ///< The current code size
  47. int curmask;
  48. int codesize;
  49. int clear_code;
  50. int end_code;
  51. int newcodes; ///< First available code
  52. int top_slot; ///< Highest code for current size
  53. int extra_slot;
  54. int slot; ///< Last read code
  55. int fc, oc;
  56. uint8_t *sp;
  57. uint8_t stack[LZW_SIZTABLE];
  58. uint8_t suffix[LZW_SIZTABLE];
  59. uint16_t prefix[LZW_SIZTABLE];
  60. int bs; ///< current buffer size for GIF
  61. };
  62. /* get one code from stream */
  63. static int lzw_get_code(struct LZWState * s)
  64. {
  65. int c;
  66. if(s->mode == FF_LZW_GIF) {
  67. while (s->bbits < s->cursize) {
  68. if (!s->bs) {
  69. s->bs = bytestream2_get_byte(&s->gb);
  70. }
  71. s->bbuf |= bytestream2_get_byte(&s->gb) << s->bbits;
  72. s->bbits += 8;
  73. s->bs--;
  74. }
  75. c = s->bbuf;
  76. s->bbuf >>= s->cursize;
  77. } else { // TIFF
  78. while (s->bbits < s->cursize) {
  79. s->bbuf = (s->bbuf << 8) | bytestream2_get_byte(&s->gb);
  80. s->bbits += 8;
  81. }
  82. c = s->bbuf >> (s->bbits - s->cursize);
  83. }
  84. s->bbits -= s->cursize;
  85. return c & s->curmask;
  86. }
  87. int ff_lzw_size_read(LZWState *p)
  88. {
  89. struct LZWState *s = p;
  90. return bytestream2_tell(&s->gb);
  91. }
  92. void ff_lzw_decode_tail(LZWState *p)
  93. {
  94. struct LZWState *s = (struct LZWState *)p;
  95. if(s->mode == FF_LZW_GIF) {
  96. while (s->bs > 0 && bytestream2_get_bytes_left(&s->gb)) {
  97. bytestream2_skip(&s->gb, s->bs);
  98. s->bs = bytestream2_get_byte(&s->gb);
  99. }
  100. }else
  101. bytestream2_skip(&s->gb, bytestream2_get_bytes_left(&s->gb));
  102. }
  103. av_cold void ff_lzw_decode_open(LZWState **p)
  104. {
  105. *p = av_mallocz(sizeof(struct LZWState));
  106. }
  107. av_cold void ff_lzw_decode_close(LZWState **p)
  108. {
  109. av_freep(p);
  110. }
  111. /**
  112. * Initialize LZW decoder
  113. * @param p 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, const 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. bytestream2_init(&s->gb, buf, 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. c = lzw_get_code(s);
  170. if (c == s->end_code) {
  171. break;
  172. } else if (c == s->clear_code) {
  173. s->cursize = s->codesize + 1;
  174. s->curmask = mask[s->cursize];
  175. s->slot = s->newcodes;
  176. s->top_slot = 1 << s->cursize;
  177. fc= oc= -1;
  178. } else {
  179. code = c;
  180. if (code == s->slot && fc>=0) {
  181. *sp++ = fc;
  182. code = oc;
  183. }else if(code >= s->slot)
  184. break;
  185. while (code >= s->newcodes) {
  186. *sp++ = s->suffix[code];
  187. code = s->prefix[code];
  188. }
  189. *sp++ = code;
  190. if (s->slot < s->top_slot && oc>=0) {
  191. s->suffix[s->slot] = code;
  192. s->prefix[s->slot++] = oc;
  193. }
  194. fc = code;
  195. oc = c;
  196. if (s->slot >= s->top_slot - s->extra_slot) {
  197. if (s->cursize < LZW_MAXBITS) {
  198. s->top_slot <<= 1;
  199. s->curmask = mask[++s->cursize];
  200. }
  201. }
  202. }
  203. }
  204. s->end_code = -1;
  205. the_end:
  206. s->sp = sp;
  207. s->oc = oc;
  208. s->fc = fc;
  209. return len - l;
  210. }