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.0KB

  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 "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. const uint8_t *pbuf, *ebuf;
  41. int bbits;
  42. unsigned int bbuf;
  43. int mode; ///< Decoder mode
  44. int cursize; ///< The current code size
  45. int curmask;
  46. int codesize;
  47. int clear_code;
  48. int end_code;
  49. int newcodes; ///< First available code
  50. int top_slot; ///< Highest code for current size
  51. int extra_slot;
  52. int slot; ///< Last read code
  53. int fc, oc;
  54. uint8_t *sp;
  55. uint8_t stack[LZW_SIZTABLE];
  56. uint8_t suffix[LZW_SIZTABLE];
  57. uint16_t prefix[LZW_SIZTABLE];
  58. int bs; ///< current buffer size for GIF
  59. };
  60. /* get one code from stream */
  61. static int lzw_get_code(struct LZWState * s)
  62. {
  63. int c;
  64. if(s->mode == FF_LZW_GIF) {
  65. while (s->bbits < s->cursize) {
  66. if (!s->bs) {
  67. s->bs = *s->pbuf++;
  68. }
  69. s->bbuf |= (*s->pbuf++) << s->bbits;
  70. s->bbits += 8;
  71. s->bs--;
  72. }
  73. c = s->bbuf;
  74. s->bbuf >>= s->cursize;
  75. } else { // TIFF
  76. while (s->bbits < s->cursize) {
  77. s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
  78. s->bbits += 8;
  79. }
  80. c = s->bbuf >> (s->bbits - s->cursize);
  81. }
  82. s->bbits -= s->cursize;
  83. return c & s->curmask;
  84. }
  85. const uint8_t* ff_lzw_cur_ptr(LZWState *p)
  86. {
  87. return ((struct LZWState*)p)->pbuf;
  88. }
  89. void ff_lzw_decode_tail(LZWState *p)
  90. {
  91. struct LZWState *s = (struct LZWState *)p;
  92. if(s->mode == FF_LZW_GIF) {
  93. while (s->bs > 0) {
  94. if (s->bs >= s->ebuf - s->pbuf) {
  95. s->pbuf = s->ebuf;
  96. break;
  97. } else {
  98. s->pbuf += s->bs;
  99. s->bs = *s->pbuf++;
  100. }
  101. }
  102. }else
  103. s->pbuf= s->ebuf;
  104. }
  105. av_cold void ff_lzw_decode_open(LZWState **p)
  106. {
  107. *p = av_mallocz(sizeof(struct LZWState));
  108. }
  109. av_cold void ff_lzw_decode_close(LZWState **p)
  110. {
  111. av_freep(p);
  112. }
  113. /**
  114. * Initialize LZW decoder
  115. * @param p LZW context
  116. * @param csize initial code size in bits
  117. * @param buf input data
  118. * @param buf_size input data size
  119. * @param mode decoder working mode - either GIF or TIFF
  120. */
  121. int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
  122. {
  123. struct LZWState *s = (struct LZWState *)p;
  124. if(csize < 1 || csize >= LZW_MAXBITS)
  125. return -1;
  126. /* read buffer */
  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 = -1;
  141. s->sp = s->stack;
  142. s->mode = mode;
  143. s->extra_slot = s->mode == FF_LZW_TIFF;
  144. return 0;
  145. }
  146. /**
  147. * Decode given number of bytes
  148. * NOTE: the algorithm here is inspired from the LZW GIF decoder
  149. * written by Steven A. Bennett in 1987.
  150. *
  151. * @param p LZW context
  152. * @param buf output buffer
  153. * @param len number of bytes to decode
  154. * @return number of bytes decoded
  155. */
  156. int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
  157. int l, c, code, oc, fc;
  158. uint8_t *sp;
  159. struct LZWState *s = (struct LZWState *)p;
  160. if (s->end_code < 0)
  161. return 0;
  162. l = len;
  163. sp = s->sp;
  164. oc = s->oc;
  165. fc = s->fc;
  166. for (;;) {
  167. while (sp > s->stack) {
  168. *buf++ = *(--sp);
  169. if ((--l) == 0)
  170. goto the_end;
  171. }
  172. c = lzw_get_code(s);
  173. if (c == s->end_code) {
  174. break;
  175. } else if (c == s->clear_code) {
  176. s->cursize = s->codesize + 1;
  177. s->curmask = mask[s->cursize];
  178. s->slot = s->newcodes;
  179. s->top_slot = 1 << s->cursize;
  180. fc= oc= -1;
  181. } else {
  182. code = c;
  183. if (code == s->slot && fc>=0) {
  184. *sp++ = fc;
  185. code = oc;
  186. }else if(code >= s->slot)
  187. break;
  188. while (code >= s->newcodes) {
  189. *sp++ = s->suffix[code];
  190. code = s->prefix[code];
  191. }
  192. *sp++ = code;
  193. if (s->slot < s->top_slot && oc>=0) {
  194. s->suffix[s->slot] = code;
  195. s->prefix[s->slot++] = oc;
  196. }
  197. fc = code;
  198. oc = c;
  199. if (s->slot >= s->top_slot - s->extra_slot) {
  200. if (s->cursize < LZW_MAXBITS) {
  201. s->top_slot <<= 1;
  202. s->curmask = mask[++s->cursize];
  203. }
  204. }
  205. }
  206. }
  207. s->end_code = -1;
  208. the_end:
  209. s->sp = sp;
  210. s->oc = oc;
  211. s->fc = fc;
  212. return len - l;
  213. }