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