Commit | Line | Data |
---|---|---|
517840c6 RD |
1 | /* |
2 | * LZO 1x decompression | |
3 | * Copyright (c) 2006 Reimar Doeffinger | |
4 | * | |
b78e7197 DB |
5 | * This file is part of FFmpeg. |
6 | * | |
7 | * FFmpeg is free software; you can redistribute it and/or | |
517840c6 RD |
8 | * modify it under the terms of the GNU Lesser General Public |
9 | * License as published by the Free Software Foundation; either | |
b78e7197 | 10 | * version 2.1 of the License, or (at your option) any later version. |
517840c6 | 11 | * |
b78e7197 | 12 | * FFmpeg is distributed in the hope that it will be useful, |
517840c6 RD |
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 | |
b78e7197 | 18 | * License along with FFmpeg; if not, write to the Free Software |
5509bffa | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
517840c6 RD |
20 | */ |
21 | #include "common.h" | |
f53a2931 RD |
22 | //! avoid e.g. MPlayers fast_memcpy, it slows things down here |
23 | #undef memcpy | |
24 | #include <string.h> | |
517840c6 RD |
25 | #include "lzo.h" |
26 | ||
f53a2931 RD |
27 | //! define if we may write up to 12 bytes beyond the output buffer |
28 | #define OUTBUF_PADDED 1 | |
a737f1df | 29 | //! define if we may read up to 8 bytes beyond the input buffer |
f53a2931 | 30 | #define INBUF_PADDED 1 |
517840c6 RD |
31 | typedef struct LZOContext { |
32 | uint8_t *in, *in_end; | |
f53a2931 | 33 | uint8_t *out_start, *out, *out_end; |
517840c6 RD |
34 | int error; |
35 | } LZOContext; | |
36 | ||
37 | /** | |
38 | * \brief read one byte from input buffer, avoiding overrun | |
39 | * \return byte read | |
40 | */ | |
41 | static inline int get_byte(LZOContext *c) { | |
42 | if (c->in < c->in_end) | |
43 | return *c->in++; | |
44 | c->error |= LZO_INPUT_DEPLETED; | |
d3c71c50 | 45 | return 1; |
517840c6 RD |
46 | } |
47 | ||
a737f1df RD |
48 | #ifdef INBUF_PADDED |
49 | #define GETB(c) (*(c).in++) | |
50 | #else | |
51 | #define GETB(c) get_byte(&(c)) | |
52 | #endif | |
53 | ||
517840c6 RD |
54 | /** |
55 | * \brief decode a length value in the coding used by lzo | |
56 | * \param x previous byte value | |
57 | * \param mask bits used from x | |
58 | * \return decoded length value | |
59 | */ | |
60 | static inline int get_len(LZOContext *c, int x, int mask) { | |
61 | int cnt = x & mask; | |
62 | if (!cnt) { | |
63 | while (!(x = get_byte(c))) cnt += 255; | |
64 | cnt += mask + x; | |
65 | } | |
66 | return cnt; | |
67 | } | |
68 | ||
1db8c21c RD |
69 | //#define UNALIGNED_LOADSTORE |
70 | #define BUILTIN_MEMCPY | |
71 | #ifdef UNALIGNED_LOADSTORE | |
72 | #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s); | |
73 | #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s); | |
74 | #elif defined(BUILTIN_MEMCPY) | |
75 | #define COPY2(d, s) memcpy(d, s, 2); | |
76 | #define COPY4(d, s) memcpy(d, s, 4); | |
77 | #else | |
78 | #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; | |
79 | #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3]; | |
80 | #endif | |
81 | ||
517840c6 RD |
82 | /** |
83 | * \brief copy bytes from input to output buffer with checking | |
9b2c14df | 84 | * \param cnt number of bytes to copy, must be >= 0 |
517840c6 RD |
85 | */ |
86 | static inline void copy(LZOContext *c, int cnt) { | |
f53a2931 RD |
87 | register uint8_t *src = c->in; |
88 | register uint8_t *dst = c->out; | |
c0a8b876 | 89 | if (cnt > c->in_end - src) { |
c215e403 | 90 | cnt = FFMAX(c->in_end - src, 0); |
517840c6 RD |
91 | c->error |= LZO_INPUT_DEPLETED; |
92 | } | |
c0a8b876 | 93 | if (cnt > c->out_end - dst) { |
c215e403 | 94 | cnt = FFMAX(c->out_end - dst, 0); |
517840c6 RD |
95 | c->error |= LZO_OUTPUT_FULL; |
96 | } | |
f53a2931 | 97 | #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED) |
1db8c21c | 98 | COPY4(dst, src); |
f53a2931 RD |
99 | src += 4; |
100 | dst += 4; | |
101 | cnt -= 4; | |
102 | if (cnt > 0) | |
103 | #endif | |
104 | memcpy(dst, src, cnt); | |
105 | c->in = src + cnt; | |
106 | c->out = dst + cnt; | |
517840c6 RD |
107 | } |
108 | ||
109 | /** | |
110 | * \brief copy previously decoded bytes to current position | |
111 | * \param back how many bytes back we start | |
9b2c14df | 112 | * \param cnt number of bytes to copy, must be >= 0 |
517840c6 | 113 | * |
f53a2931 RD |
114 | * cnt > back is valid, this will copy the bytes we just copied, |
115 | * thus creating a repeating pattern with a period length of back. | |
517840c6 RD |
116 | */ |
117 | static inline void copy_backptr(LZOContext *c, int back, int cnt) { | |
f53a2931 RD |
118 | register uint8_t *src = &c->out[-back]; |
119 | register uint8_t *dst = c->out; | |
cf0ef3dc | 120 | if (src < c->out_start || src > dst) { |
517840c6 RD |
121 | c->error |= LZO_INVALID_BACKPTR; |
122 | return; | |
123 | } | |
c0a8b876 | 124 | if (cnt > c->out_end - dst) { |
c215e403 | 125 | cnt = FFMAX(c->out_end - dst, 0); |
517840c6 RD |
126 | c->error |= LZO_OUTPUT_FULL; |
127 | } | |
f53a2931 RD |
128 | if (back == 1) { |
129 | memset(dst, *src, cnt); | |
130 | dst += cnt; | |
131 | } else { | |
132 | #ifdef OUTBUF_PADDED | |
1db8c21c RD |
133 | COPY2(dst, src); |
134 | COPY2(dst + 2, src + 2); | |
f53a2931 RD |
135 | src += 4; |
136 | dst += 4; | |
137 | cnt -= 4; | |
138 | if (cnt > 0) { | |
1db8c21c RD |
139 | COPY2(dst, src); |
140 | COPY2(dst + 2, src + 2); | |
141 | COPY2(dst + 4, src + 4); | |
142 | COPY2(dst + 6, src + 6); | |
f53a2931 RD |
143 | src += 8; |
144 | dst += 8; | |
145 | cnt -= 8; | |
146 | } | |
147 | #endif | |
148 | if (cnt > 0) { | |
149 | int blocklen = back; | |
150 | while (cnt > blocklen) { | |
151 | memcpy(dst, src, blocklen); | |
152 | dst += blocklen; | |
153 | cnt -= blocklen; | |
154 | blocklen <<= 1; | |
155 | } | |
156 | memcpy(dst, src, cnt); | |
157 | } | |
158 | dst += cnt; | |
159 | } | |
160 | c->out = dst; | |
517840c6 RD |
161 | } |
162 | ||
163 | /** | |
164 | * \brief decode LZO 1x compressed data | |
165 | * \param out output buffer | |
166 | * \param outlen size of output buffer, number of bytes left are returned here | |
167 | * \param in input buffer | |
168 | * \param inlen size of input buffer, number of bytes left are returned here | |
169 | * \return 0 on success, otherwise error flags, see lzo.h | |
f53a2931 RD |
170 | * |
171 | * make sure all buffers are appropriately padded, in must provide | |
172 | * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes | |
517840c6 RD |
173 | */ |
174 | int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) { | |
bf47272f | 175 | int state= 0; |
517840c6 RD |
176 | int x; |
177 | LZOContext c; | |
178 | c.in = in; | |
214019ed | 179 | c.in_end = (uint8_t *)in + *inlen; |
f53a2931 | 180 | c.out = c.out_start = out; |
214019ed | 181 | c.out_end = (uint8_t *)out + * outlen; |
517840c6 | 182 | c.error = 0; |
a737f1df | 183 | x = GETB(c); |
517840c6 RD |
184 | if (x > 17) { |
185 | copy(&c, x - 17); | |
a737f1df | 186 | x = GETB(c); |
517840c6 RD |
187 | if (x < 16) c.error |= LZO_ERROR; |
188 | } | |
5fe9c42c RD |
189 | if (c.in > c.in_end) |
190 | c.error |= LZO_INPUT_DEPLETED; | |
517840c6 RD |
191 | while (!c.error) { |
192 | int cnt, back; | |
801778bc MN |
193 | if (x > 15) { |
194 | if (x > 63) { | |
517840c6 | 195 | cnt = (x >> 5) - 1; |
a737f1df | 196 | back = (GETB(c) << 3) + ((x >> 2) & 7) + 1; |
801778bc | 197 | } else if (x > 31) { |
517840c6 | 198 | cnt = get_len(&c, x, 31); |
a737f1df RD |
199 | x = GETB(c); |
200 | back = (GETB(c) << 6) + (x >> 2) + 1; | |
517840c6 RD |
201 | } else { |
202 | cnt = get_len(&c, x, 7); | |
203 | back = (1 << 14) + ((x & 8) << 11); | |
a737f1df RD |
204 | x = GETB(c); |
205 | back += (GETB(c) << 6) + (x >> 2); | |
517840c6 RD |
206 | if (back == (1 << 14)) { |
207 | if (cnt != 1) | |
208 | c.error |= LZO_ERROR; | |
209 | break; | |
210 | } | |
211 | } | |
bf47272f | 212 | } else if(!state){ |
517840c6 RD |
213 | cnt = get_len(&c, x, 15); |
214 | copy(&c, cnt + 3); | |
a737f1df | 215 | x = GETB(c); |
960e48f8 | 216 | if (x > 15) |
517840c6 RD |
217 | continue; |
218 | cnt = 1; | |
a737f1df | 219 | back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1; |
bf47272f | 220 | } else { |
517840c6 | 221 | cnt = 0; |
a737f1df | 222 | back = (GETB(c) << 2) + (x >> 2) + 1; |
517840c6 RD |
223 | } |
224 | copy_backptr(&c, back, cnt + 2); | |
bf47272f | 225 | state= |
517840c6 | 226 | cnt = x & 3; |
56f8647a | 227 | copy(&c, cnt); |
a737f1df | 228 | x = GETB(c); |
517840c6 RD |
229 | } |
230 | *inlen = c.in_end - c.in; | |
a737f1df RD |
231 | if (c.in > c.in_end) |
232 | *inlen = 0; | |
517840c6 RD |
233 | *outlen = c.out_end - c.out; |
234 | return c.error; | |
235 | } | |
266aa26c RD |
236 | |
237 | #ifdef TEST | |
238 | #include <stdio.h> | |
239 | #include <lzo/lzo1x.h> | |
240 | #include "log.h" | |
241 | #define MAXSZ (10*1024*1024) | |
242 | int main(int argc, char *argv[]) { | |
243 | FILE *in = fopen(argv[1], "rb"); | |
244 | uint8_t *orig = av_malloc(MAXSZ + 16); | |
245 | uint8_t *comp = av_malloc(2*MAXSZ + 16); | |
246 | uint8_t *decomp = av_malloc(MAXSZ + 16); | |
247 | size_t s = fread(orig, 1, MAXSZ, in); | |
248 | lzo_uint clen = 0; | |
249 | long tmp[LZO1X_MEM_COMPRESS]; | |
250 | int inlen, outlen; | |
251 | int i; | |
252 | av_log_level = AV_LOG_DEBUG; | |
253 | lzo1x_999_compress(orig, s, comp, &clen, tmp); | |
254 | for (i = 0; i < 300; i++) { | |
255 | START_TIMER | |
256 | inlen = clen; outlen = MAXSZ; | |
d62a0c1e RD |
257 | #ifdef LIBLZO |
258 | if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL)) | |
259 | #elif defined(LIBLZO_UNSAFE) | |
260 | if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL)) | |
261 | #else | |
266aa26c | 262 | if (lzo1x_decode(decomp, &outlen, comp, &inlen)) |
d62a0c1e | 263 | #endif |
266aa26c RD |
264 | av_log(NULL, AV_LOG_ERROR, "decompression error\n"); |
265 | STOP_TIMER("lzod") | |
266 | } | |
267 | if (memcmp(orig, decomp, s)) | |
268 | av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n"); | |
269 | else | |
270 | av_log(NULL, AV_LOG_ERROR, "decompression ok\n"); | |
271 | return 0; | |
272 | } | |
273 | #endif |