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.

270 lines
7.1KB

  1. /*
  2. * LZW encoder
  3. * Copyright (c) 2007 Bartlomiej Wolowiec
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * LZW encoder
  24. * @author Bartlomiej Wolowiec
  25. */
  26. #include "avcodec.h"
  27. #include "put_bits.h"
  28. #include "lzw.h"
  29. #define LZW_MAXBITS 12
  30. #define LZW_SIZTABLE (1<<LZW_MAXBITS)
  31. #define LZW_HASH_SIZE 16411
  32. #define LZW_HASH_SHIFT 6
  33. #define LZW_PREFIX_EMPTY -1
  34. #define LZW_PREFIX_FREE -2
  35. /** One code in hash table */
  36. typedef struct Code{
  37. /// Hash code of prefix, LZW_PREFIX_EMPTY if empty prefix, or LZW_PREFIX_FREE if no code
  38. int hash_prefix;
  39. int code; ///< LZW code
  40. uint8_t suffix; ///< Last character in code block
  41. }Code;
  42. /** LZW encode state */
  43. typedef struct LZWEncodeState {
  44. int clear_code; ///< Value of clear code
  45. int end_code; ///< Value of end code
  46. Code tab[LZW_HASH_SIZE]; ///< Hash table
  47. int tabsize; ///< Number of values in hash table
  48. int bits; ///< Actual bits code
  49. int bufsize; ///< Size of output buffer
  50. PutBitContext pb; ///< Put bit context for output
  51. int maxbits; ///< Max bits code
  52. int maxcode; ///< Max value of code
  53. int output_bytes; ///< Number of written bytes
  54. int last_code; ///< Value of last output code or LZW_PREFIX_EMPTY
  55. enum FF_LZW_MODES mode; ///< TIFF or GIF
  56. void (*put_bits)(PutBitContext *, int, unsigned); ///< GIF is LE while TIFF is BE
  57. }LZWEncodeState;
  58. const int ff_lzw_encode_state_size = sizeof(LZWEncodeState);
  59. /**
  60. * Hash function adding character
  61. * @param head LZW code for prefix
  62. * @param add Character to add
  63. * @return New hash value
  64. */
  65. static inline int hash(int head, const int add)
  66. {
  67. head ^= (add << LZW_HASH_SHIFT);
  68. if (head >= LZW_HASH_SIZE)
  69. head -= LZW_HASH_SIZE;
  70. assert(head >= 0 && head < LZW_HASH_SIZE);
  71. return head;
  72. }
  73. /**
  74. * Hash function calculates next hash value
  75. * @param head Actual hash code
  76. * @param offset Offset calculated by hashOffset
  77. * @return New hash value
  78. */
  79. static inline int hashNext(int head, const int offset)
  80. {
  81. head -= offset;
  82. if(head < 0)
  83. head += LZW_HASH_SIZE;
  84. return head;
  85. }
  86. /**
  87. * Hash function calculates hash offset
  88. * @param head Actual hash code
  89. * @return Hash offset
  90. */
  91. static inline int hashOffset(const int head)
  92. {
  93. return head ? LZW_HASH_SIZE - head : 1;
  94. }
  95. /**
  96. * Write one code to stream
  97. * @param s LZW state
  98. * @param c code to write
  99. */
  100. static inline void writeCode(LZWEncodeState * s, int c)
  101. {
  102. assert(0 <= c && c < 1 << s->bits);
  103. s->put_bits(&s->pb, s->bits, c);
  104. }
  105. /**
  106. * Find LZW code for block
  107. * @param s LZW state
  108. * @param c Last character in block
  109. * @param hash_prefix LZW code for prefix
  110. * @return LZW code for block or -1 if not found in table
  111. */
  112. static inline int findCode(LZWEncodeState * s, uint8_t c, int hash_prefix)
  113. {
  114. int h = hash(FFMAX(hash_prefix, 0), c);
  115. int hash_offset = hashOffset(h);
  116. while (s->tab[h].hash_prefix != LZW_PREFIX_FREE) {
  117. if ((s->tab[h].suffix == c)
  118. && (s->tab[h].hash_prefix == hash_prefix))
  119. return h;
  120. h = hashNext(h, hash_offset);
  121. }
  122. return h;
  123. }
  124. /**
  125. * Add block to LZW code table
  126. * @param s LZW state
  127. * @param c Last character in block
  128. * @param hash_prefix LZW code for prefix
  129. * @param hash_code LZW code for bytes block
  130. */
  131. static inline void addCode(LZWEncodeState * s, uint8_t c, int hash_prefix, int hash_code)
  132. {
  133. s->tab[hash_code].code = s->tabsize;
  134. s->tab[hash_code].suffix = c;
  135. s->tab[hash_code].hash_prefix = hash_prefix;
  136. s->tabsize++;
  137. if (s->tabsize >= (1 << s->bits) + (s->mode == FF_LZW_GIF))
  138. s->bits++;
  139. }
  140. /**
  141. * Clear LZW code table
  142. * @param s LZW state
  143. */
  144. static void clearTable(LZWEncodeState * s)
  145. {
  146. int i, h;
  147. writeCode(s, s->clear_code);
  148. s->bits = 9;
  149. for (i = 0; i < LZW_HASH_SIZE; i++) {
  150. s->tab[i].hash_prefix = LZW_PREFIX_FREE;
  151. }
  152. for (i = 0; i < 256; i++) {
  153. h = hash(0, i);
  154. s->tab[h].code = i;
  155. s->tab[h].suffix = i;
  156. s->tab[h].hash_prefix = LZW_PREFIX_EMPTY;
  157. }
  158. s->tabsize = 258;
  159. }
  160. /**
  161. * Calculate number of bytes written
  162. * @param s LZW encode state
  163. * @return Number of bytes written
  164. */
  165. static int writtenBytes(LZWEncodeState *s){
  166. int ret = put_bits_count(&s->pb) >> 3;
  167. ret -= s->output_bytes;
  168. s->output_bytes += ret;
  169. return ret;
  170. }
  171. /**
  172. * Initialize LZW encoder. Please set s->clear_code, s->end_code and s->maxbits before run.
  173. * @param s LZW state
  174. * @param outbuf Output buffer
  175. * @param outsize Size of output buffer
  176. * @param maxbits Maximum length of code
  177. */
  178. void ff_lzw_encode_init(LZWEncodeState *s, uint8_t *outbuf, int outsize,
  179. int maxbits, enum FF_LZW_MODES mode,
  180. void (*lzw_put_bits)(PutBitContext *, int, unsigned))
  181. {
  182. s->clear_code = 256;
  183. s->end_code = 257;
  184. s->maxbits = maxbits;
  185. init_put_bits(&s->pb, outbuf, outsize);
  186. s->bufsize = outsize;
  187. assert(s->maxbits >= 9 && s->maxbits <= LZW_MAXBITS);
  188. s->maxcode = 1 << s->maxbits;
  189. s->output_bytes = 0;
  190. s->last_code = LZW_PREFIX_EMPTY;
  191. s->bits = 9;
  192. s->mode = mode;
  193. s->put_bits = lzw_put_bits;
  194. }
  195. /**
  196. * LZW main compress function
  197. * @param s LZW state
  198. * @param inbuf Input buffer
  199. * @param insize Size of input buffer
  200. * @return Number of bytes written or -1 on error
  201. */
  202. int ff_lzw_encode(LZWEncodeState * s, const uint8_t * inbuf, int insize)
  203. {
  204. int i;
  205. if(insize * 3 > (s->bufsize - s->output_bytes) * 2){
  206. return -1;
  207. }
  208. if (s->last_code == LZW_PREFIX_EMPTY)
  209. clearTable(s);
  210. for (i = 0; i < insize; i++) {
  211. uint8_t c = *inbuf++;
  212. int code = findCode(s, c, s->last_code);
  213. if (s->tab[code].hash_prefix == LZW_PREFIX_FREE) {
  214. writeCode(s, s->last_code);
  215. addCode(s, c, s->last_code, code);
  216. code= hash(0, c);
  217. }
  218. s->last_code = s->tab[code].code;
  219. if (s->tabsize >= s->maxcode - 1) {
  220. clearTable(s);
  221. }
  222. }
  223. return writtenBytes(s);
  224. }
  225. /**
  226. * Write end code and flush bitstream
  227. * @param s LZW state
  228. * @return Number of bytes written or -1 on error
  229. */
  230. int ff_lzw_encode_flush(LZWEncodeState *s,
  231. void (*lzw_flush_put_bits)(PutBitContext *))
  232. {
  233. if (s->last_code != -1)
  234. writeCode(s, s->last_code);
  235. writeCode(s, s->end_code);
  236. lzw_flush_put_bits(&s->pb);
  237. s->last_code = -1;
  238. return writtenBytes(s);
  239. }