3 * Copyright (c) 2006 Reimar Doeffinger
5 * This file is part of FFmpeg.
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.
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.
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
24 /// Avoid e.g. MPlayers fast_memcpy, it slows things down here.
29 /// Define if we may write up to 12 bytes beyond the output buffer.
30 #define OUTBUF_PADDED 1
31 /// Define if we may read up to 8 bytes beyond the input buffer.
32 #define INBUF_PADDED 1
33 typedef struct LZOContext {
34 const uint8_t *in, *in_end;
35 uint8_t *out_start, *out, *out_end;
40 * @brief Reads one byte from the input buffer, avoiding an overrun.
43 static inline int get_byte(LZOContext *c) {
44 if (c->in < c->in_end)
46 c->error |= AV_LZO_INPUT_DEPLETED;
51 #define GETB(c) (*(c).in++)
53 #define GETB(c) get_byte(&(c))
57 * @brief Decodes a length value in the coding used by lzo.
58 * @param x previous byte value
59 * @param mask bits used from x
60 * @return decoded length value
62 static inline int get_len(LZOContext *c, int x, int mask) {
65 while (!(x = get_byte(c))) cnt += 255;
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);
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];
85 * @brief Copies bytes from input to output buffer with checking.
86 * @param cnt number of bytes to copy, must be >= 0
88 static inline void copy(LZOContext *c, int cnt) {
89 register const uint8_t *src = c->in;
90 register uint8_t *dst = c->out;
91 if (cnt > c->in_end - src) {
92 cnt = FFMAX(c->in_end - src, 0);
93 c->error |= AV_LZO_INPUT_DEPLETED;
95 if (cnt > c->out_end - dst) {
96 cnt = FFMAX(c->out_end - dst, 0);
97 c->error |= AV_LZO_OUTPUT_FULL;
99 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
106 memcpy(dst, src, cnt);
111 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
114 * @brief Copies previously decoded bytes to current position.
115 * @param back how many bytes back we start, must be > 0
116 * @param cnt number of bytes to copy, must be >= 0
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.
121 static inline void copy_backptr(LZOContext *c, int back, int cnt) {
122 register const uint8_t *src = &c->out[-back];
123 register uint8_t *dst = c->out;
124 if (src < c->out_start || src > dst) {
125 c->error |= AV_LZO_INVALID_BACKPTR;
128 if (cnt > c->out_end - dst) {
129 cnt = FFMAX(c->out_end - dst, 0);
130 c->error |= AV_LZO_OUTPUT_FULL;
132 memcpy_backptr(dst, back, cnt);
136 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) {
137 const uint8_t *src = &dst[-back];
139 memset(dst, *src, cnt);
143 COPY2(dst + 2, src + 2);
149 COPY2(dst + 2, src + 2);
150 COPY2(dst + 4, src + 4);
151 COPY2(dst + 6, src + 6);
159 while (cnt > blocklen) {
160 memcpy(dst, src, blocklen);
165 memcpy(dst, src, cnt);
170 void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
171 memcpy_backptr(dst, back, cnt);
174 int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
178 if (*outlen <= 0 || *inlen <= 0) {
181 res |= AV_LZO_OUTPUT_FULL;
183 res |= AV_LZO_INPUT_DEPLETED;
187 c.in_end = (const uint8_t *)in + *inlen;
188 c.out = c.out_start = out;
189 c.out_end = (uint8_t *)out + * outlen;
195 if (x < 16) c.error |= AV_LZO_ERROR;
198 c.error |= AV_LZO_INPUT_DEPLETED;
204 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
206 cnt = get_len(&c, x, 31);
208 back = (GETB(c) << 6) + (x >> 2) + 1;
210 cnt = get_len(&c, x, 7);
211 back = (1 << 14) + ((x & 8) << 11);
213 back += (GETB(c) << 6) + (x >> 2);
214 if (back == (1 << 14)) {
216 c.error |= AV_LZO_ERROR;
221 cnt = get_len(&c, x, 15);
227 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
230 back = (GETB(c) << 2) + (x >> 2) + 1;
232 copy_backptr(&c, back, cnt + 2);
238 *inlen = c.in_end - c.in;
241 *outlen = c.out_end - c.out;
247 #include <lzo/lzo1x.h>
249 #define MAXSZ (10*1024*1024)
251 /* Define one of these to 1 if you wish to benchmark liblzo
252 * instead of our native implementation. */
253 #define BENCHMARK_LIBLZO_SAFE 0
254 #define BENCHMARK_LIBLZO_UNSAFE 0
256 int main(int argc, char *argv[]) {
257 FILE *in = fopen(argv[1], "rb");
258 uint8_t *orig = av_malloc(MAXSZ + 16);
259 uint8_t *comp = av_malloc(2*MAXSZ + 16);
260 uint8_t *decomp = av_malloc(MAXSZ + 16);
261 size_t s = fread(orig, 1, MAXSZ, in);
263 long tmp[LZO1X_MEM_COMPRESS];
266 av_log_set_level(AV_LOG_DEBUG);
267 lzo1x_999_compress(orig, s, comp, &clen, tmp);
268 for (i = 0; i < 300; i++) {
270 inlen = clen; outlen = MAXSZ;
271 #if BENCHMARK_LIBLZO_SAFE
272 if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
273 #elif BENCHMARK_LIBLZO_UNSAFE
274 if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
276 if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
278 av_log(NULL, AV_LOG_ERROR, "decompression error\n");
281 if (memcmp(orig, decomp, s))
282 av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
284 av_log(NULL, AV_LOG_ERROR, "decompression OK\n");