lzwenc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. * LZW encoder
  23. * @file libavcodec/lzwenc.c
  24. * @author Bartlomiej Wolowiec
  25. */
  26. #include "avcodec.h"
  27. #include "bitstream.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. }LZWEncodeState;
  56. const int ff_lzw_encode_state_size = sizeof(LZWEncodeState);
  57. /**
  58. * Hash function adding character
  59. * @param head LZW code for prefix
  60. * @param add Character to add
  61. * @return New hash value
  62. */
  63. static inline int hash(int head, const int add)
  64. {
  65. head ^= (add << LZW_HASH_SHIFT);
  66. if (head >= LZW_HASH_SIZE)
  67. head -= LZW_HASH_SIZE;
  68. assert(head >= 0 && head < LZW_HASH_SIZE);
  69. return head;
  70. }
  71. /**
  72. * Hash function calculates next hash value
  73. * @param head Actual hash code
  74. * @param offset Offset calculated by hashOffset
  75. * @return New hash value
  76. */
  77. static inline int hashNext(int head, const int offset)
  78. {
  79. head -= offset;
  80. if(head < 0)
  81. head += LZW_HASH_SIZE;
  82. return head;
  83. }
  84. /**
  85. * Hash function calculates hash offset
  86. * @param head Actual hash code
  87. * @return Hash offset
  88. */
  89. static inline int hashOffset(const int head)
  90. {
  91. return head ? LZW_HASH_SIZE - head : 1;
  92. }
  93. /**
  94. * Write one code to stream
  95. * @param s LZW state
  96. * @param c code to write
  97. */
  98. static inline void writeCode(LZWEncodeState * s, int c)
  99. {
  100. assert(0 <= c && c < 1 << s->bits);
  101. put_bits(&s->pb, s->bits, c);
  102. }
  103. /**
  104. * Find LZW code for block
  105. * @param s LZW state
  106. * @param c Last character in block
  107. * @param hash_prefix LZW code for prefix
  108. * @return LZW code for block or -1 if not found in table
  109. */
  110. static inline int findCode(LZWEncodeState * s, uint8_t c, int hash_prefix)
  111. {
  112. int h = hash(FFMAX(hash_prefix, 0), c);
  113. int hash_offset = hashOffset(h);
  114. while (s->tab[h].hash_prefix != LZW_PREFIX_FREE) {
  115. if ((s->tab[h].suffix == c)
  116. && (s->tab[h].hash_prefix == hash_prefix))
  117. return h;
  118. h = hashNext(h, hash_offset);
  119. }
  120. return h;
  121. }
  122. /**
  123. * Add block to LZW code table
  124. * @param s LZW state
  125. * @param c Last character in block
  126. * @param hash_prefix LZW code for prefix
  127. * @param hash_code LZW code for bytes block
  128. */
  129. static inline void addCode(LZWEncodeState * s, uint8_t c, int hash_prefix, int hash_code)
  130. {
  131. s->tab[hash_code].code = s->tabsize;
  132. s->tab[hash_code].suffix = c;
  133. s->tab[hash_code].hash_prefix = hash_prefix;
  134. s->tabsize++;
  135. if (s->tabsize >= 1 << s->bits)
  136. s->bits++;
  137. }
  138. /**
  139. * Clear LZW code table
  140. * @param s LZW state
  141. */
  142. static void clearTable(LZWEncodeState * s)
  143. {
  144. int i, h;
  145. writeCode(s, s->clear_code);
  146. s->bits = 9;
  147. for (i = 0; i < LZW_HASH_SIZE; i++) {
  148. s->tab[i].hash_prefix = LZW_PREFIX_FREE;
  149. }
  150. for (i = 0; i < 256; i++) {
  151. h = hash(0, i);
  152. s->tab[h].code = i;
  153. s->tab[h].suffix = i;
  154. s->tab[h].hash_prefix = LZW_PREFIX_EMPTY;
  155. }
  156. s->tabsize = 258;
  157. }
  158. /**
  159. * Calculate number of bytes written
  160. * @param s LZW encode state
  161. * @return Number of bytes written
  162. */
  163. static int writtenBytes(LZWEncodeState *s){
  164. int ret = put_bits_count(&s->pb) >> 3;
  165. ret -= s->output_bytes;
  166. s->output_bytes += ret;
  167. return ret;
  168. }
  169. /**
  170. * Initialize LZW encoder. Please set s->clear_code, s->end_code and s->maxbits before run.
  171. * @param s LZW state
  172. * @param outbuf Output buffer
  173. * @param outsize Size of output buffer
  174. * @param maxbits Maximum length of code
  175. */
  176. void ff_lzw_encode_init(LZWEncodeState * s, uint8_t * outbuf, int outsize, int maxbits)
  177. {
  178. s->clear_code = 256;
  179. s->end_code = 257;
  180. s->maxbits = maxbits;
  181. init_put_bits(&s->pb, outbuf, outsize);
  182. s->bufsize = outsize;
  183. assert(9 <= s->maxbits && s->maxbits <= s->maxbits);
  184. s->maxcode = 1 << s->maxbits;
  185. s->output_bytes = 0;
  186. s->last_code = LZW_PREFIX_EMPTY;
  187. s->bits = 9;
  188. }
  189. /**
  190. * LZW main compress function
  191. * @param s LZW state
  192. * @param inbuf Input buffer
  193. * @param insize Size of input buffer
  194. * @return Number of bytes written or -1 on error
  195. */
  196. int ff_lzw_encode(LZWEncodeState * s, const uint8_t * inbuf, int insize)
  197. {
  198. int i;
  199. if(insize * 3 > (s->bufsize - s->output_bytes) * 2){
  200. return -1;
  201. }
  202. if (s->last_code == LZW_PREFIX_EMPTY)
  203. clearTable(s);
  204. for (i = 0; i < insize; i++) {
  205. uint8_t c = *inbuf++;
  206. int code = findCode(s, c, s->last_code);
  207. if (s->tab[code].hash_prefix == LZW_PREFIX_FREE) {
  208. writeCode(s, s->last_code);
  209. addCode(s, c, s->last_code, code);
  210. code= hash(0, c);
  211. }
  212. s->last_code = s->tab[code].code;
  213. if (s->tabsize >= s->maxcode - 1) {
  214. clearTable(s);
  215. }
  216. }
  217. return writtenBytes(s);
  218. }
  219. /**
  220. * Write end code and flush bitstream
  221. * @param s LZW state
  222. * @return Number of bytes written or -1 on error
  223. */
  224. int ff_lzw_encode_flush(LZWEncodeState * s)
  225. {
  226. if (s->last_code != -1)
  227. writeCode(s, s->last_code);
  228. writeCode(s, s->end_code);
  229. flush_put_bits(&s->pb);
  230. s->last_code = -1;
  231. return writtenBytes(s);
  232. }