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.

271 lines
7.1KB

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