Mercurial > libavutil.hg
annotate lzo.c @ 1028:5dbb12a37c3d libavutil tip
Move av_set_options_string() from libavfilter to libavutil.
author | stefano |
---|---|
date | Mon, 27 Sep 2010 22:09:53 +0000 |
parents | 0a1867c7ec5e |
children |
rev | line source |
---|---|
234 | 1 /* |
2 * LZO 1x decompression | |
3 * Copyright (c) 2006 Reimar Doeffinger | |
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 */ | |
646
8e5654be3500
Add necessary header for LIBAVUTIL_VERSION_MAJOR, fixes the warning:
diego
parents:
644
diff
changeset
|
21 |
8e5654be3500
Add necessary header for LIBAVUTIL_VERSION_MAJOR, fixes the warning:
diego
parents:
644
diff
changeset
|
22 #include "avutil.h" |
234 | 23 #include "common.h" |
633 | 24 //! Avoid e.g. MPlayers fast_memcpy, it slows things down here. |
234 | 25 #undef memcpy |
26 #include <string.h> | |
27 #include "lzo.h" | |
28 | |
633 | 29 //! Define if we may write up to 12 bytes beyond the output buffer. |
234 | 30 #define OUTBUF_PADDED 1 |
633 | 31 //! Define if we may read up to 8 bytes beyond the input buffer. |
234 | 32 #define INBUF_PADDED 1 |
33 typedef struct LZOContext { | |
445 | 34 const uint8_t *in, *in_end; |
234 | 35 uint8_t *out_start, *out, *out_end; |
36 int error; | |
37 } LZOContext; | |
38 | |
39 /** | |
636 | 40 * \brief Reads one byte from the input buffer, avoiding an overrun. |
234 | 41 * \return byte read |
42 */ | |
43 static inline int get_byte(LZOContext *c) { | |
44 if (c->in < c->in_end) | |
45 return *c->in++; | |
643
db8f45986cff
Add av_ prefix to LZO stuff and thus make it officially part of the public API.
reimar
parents:
636
diff
changeset
|
46 c->error |= AV_LZO_INPUT_DEPLETED; |
234 | 47 return 1; |
48 } | |
49 | |
50 #ifdef INBUF_PADDED | |
51 #define GETB(c) (*(c).in++) | |
52 #else | |
53 #define GETB(c) get_byte(&(c)) | |
54 #endif | |
55 | |
56 /** | |
633 | 57 * \brief Decodes a length value in the coding used by lzo. |
234 | 58 * \param x previous byte value |
59 * \param mask bits used from x | |
60 * \return decoded length value | |
61 */ | |
62 static inline int get_len(LZOContext *c, int x, int mask) { | |
63 int cnt = x & mask; | |
64 if (!cnt) { | |
65 while (!(x = get_byte(c))) cnt += 255; | |
66 cnt += mask + x; | |
67 } | |
68 return cnt; | |
69 } | |
70 | |
71 //#define UNALIGNED_LOADSTORE | |
72 #define BUILTIN_MEMCPY | |
73 #ifdef UNALIGNED_LOADSTORE | |
74 #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s); | |
75 #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s); | |
76 #elif defined(BUILTIN_MEMCPY) | |
77 #define COPY2(d, s) memcpy(d, s, 2); | |
78 #define COPY4(d, s) memcpy(d, s, 4); | |
79 #else | |
80 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; | |
81 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3]; | |
82 #endif | |
83 | |
84 /** | |
633 | 85 * \brief Copies bytes from input to output buffer with checking. |
234 | 86 * \param cnt number of bytes to copy, must be >= 0 |
87 */ | |
88 static inline void copy(LZOContext *c, int cnt) { | |
445 | 89 register const uint8_t *src = c->in; |
234 | 90 register uint8_t *dst = c->out; |
91 if (cnt > c->in_end - src) { | |
92 cnt = FFMAX(c->in_end - src, 0); | |
643
db8f45986cff
Add av_ prefix to LZO stuff and thus make it officially part of the public API.
reimar
parents:
636
diff
changeset
|
93 c->error |= AV_LZO_INPUT_DEPLETED; |
234 | 94 } |
95 if (cnt > c->out_end - dst) { | |
96 cnt = FFMAX(c->out_end - dst, 0); | |
643
db8f45986cff
Add av_ prefix to LZO stuff and thus make it officially part of the public API.
reimar
parents:
636
diff
changeset
|
97 c->error |= AV_LZO_OUTPUT_FULL; |
234 | 98 } |
99 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED) | |
100 COPY4(dst, src); | |
101 src += 4; | |
102 dst += 4; | |
103 cnt -= 4; | |
104 if (cnt > 0) | |
105 #endif | |
106 memcpy(dst, src, cnt); | |
107 c->in = src + cnt; | |
108 c->out = dst + cnt; | |
109 } | |
110 | |
541
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
111 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt); |
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
112 |
234 | 113 /** |
633 | 114 * \brief Copies previously decoded bytes to current position. |
234 | 115 * \param back how many bytes back we start |
116 * \param cnt number of bytes to copy, must be >= 0 | |
117 * | |
118 * cnt > back is valid, this will copy the bytes we just copied, | |
119 * thus creating a repeating pattern with a period length of back. | |
120 */ | |
121 static inline void copy_backptr(LZOContext *c, int back, int cnt) { | |
445 | 122 register const uint8_t *src = &c->out[-back]; |
234 | 123 register uint8_t *dst = c->out; |
124 if (src < c->out_start || src > dst) { | |
643
db8f45986cff
Add av_ prefix to LZO stuff and thus make it officially part of the public API.
reimar
parents:
636
diff
changeset
|
125 c->error |= AV_LZO_INVALID_BACKPTR; |
234 | 126 return; |
127 } | |
128 if (cnt > c->out_end - dst) { | |
129 cnt = FFMAX(c->out_end - dst, 0); | |
643
db8f45986cff
Add av_ prefix to LZO stuff and thus make it officially part of the public API.
reimar
parents:
636
diff
changeset
|
130 c->error |= AV_LZO_OUTPUT_FULL; |
234 | 131 } |
541
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
132 memcpy_backptr(dst, back, cnt); |
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
133 c->out = dst + cnt; |
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
134 } |
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
135 |
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
136 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) { |
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
137 const uint8_t *src = &dst[-back]; |
234 | 138 if (back == 1) { |
139 memset(dst, *src, cnt); | |
140 } else { | |
141 #ifdef OUTBUF_PADDED | |
142 COPY2(dst, src); | |
143 COPY2(dst + 2, src + 2); | |
144 src += 4; | |
145 dst += 4; | |
146 cnt -= 4; | |
147 if (cnt > 0) { | |
148 COPY2(dst, src); | |
149 COPY2(dst + 2, src + 2); | |
150 COPY2(dst + 4, src + 4); | |
151 COPY2(dst + 6, src + 6); | |
152 src += 8; | |
153 dst += 8; | |
154 cnt -= 8; | |
155 } | |
156 #endif | |
157 if (cnt > 0) { | |
158 int blocklen = back; | |
159 while (cnt > blocklen) { | |
160 memcpy(dst, src, blocklen); | |
161 dst += blocklen; | |
162 cnt -= blocklen; | |
163 blocklen <<= 1; | |
164 } | |
165 memcpy(dst, src, cnt); | |
166 } | |
167 } | |
541
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
168 } |
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
169 |
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
170 void av_memcpy_backptr(uint8_t *dst, int back, int cnt) { |
b8574db98875
Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents:
445
diff
changeset
|
171 memcpy_backptr(dst, back, cnt); |
234 | 172 } |
173 | |
643
db8f45986cff
Add av_ prefix to LZO stuff and thus make it officially part of the public API.
reimar
parents:
636
diff
changeset
|
174 int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) { |
234 | 175 int state= 0; |
176 int x; | |
177 LZOContext c; | |
178 c.in = in; | |
445 | 179 c.in_end = (const uint8_t *)in + *inlen; |
234 | 180 c.out = c.out_start = out; |
181 c.out_end = (uint8_t *)out + * outlen; | |
182 c.error = 0; | |
183 x = GETB(c); | |
184 if (x > 17) { | |
185 copy(&c, x - 17); | |
186 x = GETB(c); | |
643
db8f45986cff
Add av_ prefix to LZO stuff and thus make it officially part of the public API.
reimar
parents:
636
diff
changeset
|
187 if (x < 16) c.error |= AV_LZO_ERROR; |
234 | 188 } |
189 if (c.in > c.in_end) | |
643
db8f45986cff
Add av_ prefix to LZO stuff and thus make it officially part of the public API.
reimar
parents:
636
diff
changeset
|
190 c.error |= AV_LZO_INPUT_DEPLETED; |
234 | 191 while (!c.error) { |
192 int cnt, back; | |
193 if (x > 15) { | |
194 if (x > 63) { | |
195 cnt = (x >> 5) - 1; | |
196 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1; | |
197 } else if (x > 31) { | |
198 cnt = get_len(&c, x, 31); | |
199 x = GETB(c); | |
200 back = (GETB(c) << 6) + (x >> 2) + 1; | |
201 } else { | |
202 cnt = get_len(&c, x, 7); | |
203 back = (1 << 14) + ((x & 8) << 11); | |
204 x = GETB(c); | |
205 back += (GETB(c) << 6) + (x >> 2); | |
206 if (back == (1 << 14)) { | |
207 if (cnt != 1) | |
643
db8f45986cff
Add av_ prefix to LZO stuff and thus make it officially part of the public API.
reimar
parents:
636
diff
changeset
|
208 c.error |= AV_LZO_ERROR; |
234 | 209 break; |
210 } | |
211 } | |
212 } else if(!state){ | |
213 cnt = get_len(&c, x, 15); | |
214 copy(&c, cnt + 3); | |
215 x = GETB(c); | |
216 if (x > 15) | |
217 continue; | |
218 cnt = 1; | |
219 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1; | |
220 } else { | |
221 cnt = 0; | |
222 back = (GETB(c) << 2) + (x >> 2) + 1; | |
223 } | |
224 copy_backptr(&c, back, cnt + 2); | |
225 state= | |
226 cnt = x & 3; | |
227 copy(&c, cnt); | |
228 x = GETB(c); | |
229 } | |
230 *inlen = c.in_end - c.in; | |
231 if (c.in > c.in_end) | |
232 *inlen = 0; | |
233 *outlen = c.out_end - c.out; | |
234 return c.error; | |
235 } | |
236 | |
237 #ifdef TEST | |
238 #include <stdio.h> | |
239 #include <lzo/lzo1x.h> | |
240 #include "log.h" | |
241 #define MAXSZ (10*1024*1024) | |
724
0a1867c7ec5e
Give liblzo benchmark conditionals more descriptive names and add convenience
diego
parents:
720
diff
changeset
|
242 |
0a1867c7ec5e
Give liblzo benchmark conditionals more descriptive names and add convenience
diego
parents:
720
diff
changeset
|
243 /* Define one of these to 1 if you wish to benchmark liblzo |
0a1867c7ec5e
Give liblzo benchmark conditionals more descriptive names and add convenience
diego
parents:
720
diff
changeset
|
244 * instead of our native implementation. */ |
0a1867c7ec5e
Give liblzo benchmark conditionals more descriptive names and add convenience
diego
parents:
720
diff
changeset
|
245 #define BENCHMARK_LIBLZO_SAFE 0 |
0a1867c7ec5e
Give liblzo benchmark conditionals more descriptive names and add convenience
diego
parents:
720
diff
changeset
|
246 #define BENCHMARK_LIBLZO_UNSAFE 0 |
0a1867c7ec5e
Give liblzo benchmark conditionals more descriptive names and add convenience
diego
parents:
720
diff
changeset
|
247 |
234 | 248 int main(int argc, char *argv[]) { |
249 FILE *in = fopen(argv[1], "rb"); | |
250 uint8_t *orig = av_malloc(MAXSZ + 16); | |
251 uint8_t *comp = av_malloc(2*MAXSZ + 16); | |
252 uint8_t *decomp = av_malloc(MAXSZ + 16); | |
253 size_t s = fread(orig, 1, MAXSZ, in); | |
254 lzo_uint clen = 0; | |
255 long tmp[LZO1X_MEM_COMPRESS]; | |
256 int inlen, outlen; | |
257 int i; | |
720
a44606418c82
Replace manual setting of the removed av_log_level variable by the
diego
parents:
686
diff
changeset
|
258 av_log_set_level(AV_LOG_DEBUG); |
234 | 259 lzo1x_999_compress(orig, s, comp, &clen, tmp); |
260 for (i = 0; i < 300; i++) { | |
261 START_TIMER | |
262 inlen = clen; outlen = MAXSZ; | |
724
0a1867c7ec5e
Give liblzo benchmark conditionals more descriptive names and add convenience
diego
parents:
720
diff
changeset
|
263 #if BENCHMARK_LIBLZO_SAFE |
234 | 264 if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL)) |
724
0a1867c7ec5e
Give liblzo benchmark conditionals more descriptive names and add convenience
diego
parents:
720
diff
changeset
|
265 #elif BENCHMARK_LIBLZO_UNSAFE |
234 | 266 if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL)) |
267 #else | |
643
db8f45986cff
Add av_ prefix to LZO stuff and thus make it officially part of the public API.
reimar
parents:
636
diff
changeset
|
268 if (av_lzo1x_decode(decomp, &outlen, comp, &inlen)) |
234 | 269 #endif |
270 av_log(NULL, AV_LOG_ERROR, "decompression error\n"); | |
271 STOP_TIMER("lzod") | |
272 } | |
273 if (memcmp(orig, decomp, s)) | |
274 av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n"); | |
275 else | |
633 | 276 av_log(NULL, AV_LOG_ERROR, "decompression OK\n"); |
234 | 277 return 0; |
278 } | |
279 #endif |