lzw.c 5.9 KB

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