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.

226 lines
6.0KB

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