Modify #includes of imported files
[firmware_extractor.git] / cms_lzw.h
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 #ifndef __CMS_LZW_H__
24 #define __CMS_LZW_H__
25
26 /*!\file cms_lzw.h
27  * \brief Header file for LZW Encoder/Decoder functions.
28  * 
29  * These functions were taken from the ffmpeg library.
30  */
31
32 #include "cms.h"
33
34 #define LZW_MAXBITS        12
35 #define LZW_SIZTABLE       (1<<LZW_MAXBITS)
36 #define LZW_HASH_SIZE      16411
37 #define LZW_HASH_SHIFT     6
38
39 #define LZW_PREFIX_EMPTY   -1
40 #define LZW_PREFIX_FREE    -2
41
42
43 /** fixed length header in front of compressed config file */
44 #define COMPRESSED_CONFIG_HEADER_LENGTH 40
45
46 /** start of compressed config file */
47 #define COMPRESSED_CONFIG_HEADER        "<compressed alg=lzw len="
48
49
50 /** One code in hash table */
51 typedef struct Code{
52     /// Hash code of prefix, LZW_PREFIX_EMPTY if empty prefix, or LZW_PREFIX_FREE if no code
53     int hash_prefix;
54     int code;               ///< LZW code
55     uint8_t suffix;         ///< Last character in code block
56 }Code;
57
58
59 typedef struct PutBitContext {
60     uint32_t bit_buf;
61     int bit_left;
62     uint8_t *buf, *buf_ptr, *buf_end;
63 } PutBitContext;
64
65
66 /** LZW encode state */
67 typedef struct {
68     int clear_code;          ///< Value of clear code
69     int end_code;            ///< Value of end code
70     Code tab[LZW_HASH_SIZE]; ///< Hash table
71     int tabsize;             ///< Number of values in hash table
72     int bits;                ///< Actual bits code
73     int bufsize;             ///< Size of output buffer
74     PutBitContext pb;        ///< Put bit context for output
75     int maxbits;             ///< Max bits code
76     int maxcode;             ///< Max value of code
77     int output_bytes;        ///< Number of written bytes
78     int last_code;           ///< Value of last output code or LZW_PREFIX_EMPTY
79 }LZWEncoderState;
80
81
82 /** LZW decoder state structure */
83 typedef struct {
84     uint8_t *pbuf, *ebuf;
85     int bbits;
86     unsigned int bbuf;
87
88     int mode;                   ///< Decoder mode
89     int cursize;                ///< The current code size
90     int curmask;
91     int codesize;
92     int clear_code;
93     int end_code;
94     int newcodes;               ///< First available code
95     int top_slot;               ///< Highest code for current size
96     int extra_slot;
97     int slot;                   ///< Last read code
98     int fc, oc;
99     uint8_t *sp;
100     uint8_t stack[LZW_SIZTABLE];
101     uint8_t suffix[LZW_SIZTABLE];
102     uint16_t prefix[LZW_SIZTABLE];
103     int bs;                     ///< current buffer size for GIF
104 } LZWDecoderState;
105
106
107 /** Create and Initialize a LZW encoder.
108  *
109  * @param s       (IN/OUT) LZW encoder state structure allocated by this function.
110  * @param outbuf  (IN)  Output buffer, caller is responsible for allocating the buffer.
111  * @param outsize (IN)  Size of output buffer
112  *
113  * @return CmsRet enum
114  */
115 CmsRet cmsLzw_initEncoder(LZWEncoderState **s, UINT8 *outbuf, UINT32 outsize);
116
117
118 /** LZW main encode/compress function
119  *
120  * @param s      (IN) LZW encoder state
121  * @param inbuf  (IN) Input buffer, contains data to be compressed
122  * @param insize (IN) Size of input buffer
123  * @return Number of bytes written or -1 on error
124  */
125 SINT32 cmsLzw_encode(LZWEncoderState *s, const UINT8 *inbuf, UINT32 insize);
126
127
128 /** Write end code and flush bitstream
129  *
130  * @param s (IN) LZW encoder state
131  * @return Number of bytes written or -1 on error.  Note the count here should
132  *         be added to the count returned by cmsLzw_encode for the total number
133  *         of encoded/compressed bytes.
134  */
135 SINT32 cmsLzw_flushEncoder(LZWEncoderState *s);
136
137
138 /** Destroy the LZW encoder.
139  *  Note the output buffer that was passed into the cmsLzw_initEncoder
140  *  is not freed.  That buffer is the responsibility of the caller.
141  *
142  * @param s      (IN) LZW encoder state
143  */
144 void cmsLzw_cleanupEncoder(LZWEncoderState **s);
145
146
147 /** Create and Initialize a LZW decoder
148  *
149  * @param s          (IN/OUT) LZW decoder context
150  * @param inbuf      (IN) input compressed data
151  * @param inbuf_size (IN) input data size
152  *
153  * @return CmsRet enum
154  */
155 CmsRet cmsLzw_initDecoder(LZWDecoderState **s, UINT8 *inbuf, UINT32 inbuf_size);
156
157
158 /** Decode given number of bytes
159  *
160  * @param s       (IN) LZW decoder context
161  * @param outbuf  (IN) output buffer, caller is responsible for allocating this buffer
162  * @param outlen  (IN) number of bytes to decode, or the length of the output buffer.
163  *                  The decompressor/decoder will stop if the end of the encoded
164  *                  data is reached or if the end of the output buffer is reached.
165  *
166  * @return number of bytes decoded, or -1 on error.
167  */
168 SINT32 cmsLzw_decode(LZWDecoderState *s, UINT8 *outbuf, UINT32 outlen);
169
170
171 /** Destroy the LZW Decoder.
172  *
173  * Note the input buffer that was passed into cmsLzw_initDecoder is not freed.
174  * That buffer is the responsibility of the caller.
175  */
176 void cmsLzw_cleanupDecoder(LZWDecoderState **s);
177
178
179
180 #endif /* __CMS_LZW_H__ */