Statistics
| Branch: | Revision:

ffmpeg / libavutil / lzo.c @ 80a07f6e

History | View | Annotate | Download (7.92 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 0d08e270 Diego Biurrun
22
#include "avutil.h"
23 517840c6 Reimar Döffinger
#include "common.h"
24 89c9ff50 Diego Biurrun
//! Avoid e.g. MPlayers fast_memcpy, it slows things down here.
25 f53a2931 Reimar Döffinger
#undef memcpy
26
#include <string.h>
27 517840c6 Reimar Döffinger
#include "lzo.h"
28
29 89c9ff50 Diego Biurrun
//! Define if we may write up to 12 bytes beyond the output buffer.
30 f53a2931 Reimar Döffinger
#define OUTBUF_PADDED 1
31 89c9ff50 Diego Biurrun
//! Define if we may read up to 8 bytes beyond the input buffer.
32 f53a2931 Reimar Döffinger
#define INBUF_PADDED 1
33 517840c6 Reimar Döffinger
typedef struct LZOContext {
34 5e038b4f Reimar Döffinger
    const uint8_t *in, *in_end;
35 f53a2931 Reimar Döffinger
    uint8_t *out_start, *out, *out_end;
36 517840c6 Reimar Döffinger
    int error;
37
} LZOContext;
38
39
/**
40 bfe3676f Diego Biurrun
 * \brief Reads one byte from the input buffer, avoiding an overrun.
41 517840c6 Reimar Döffinger
 * \return byte read
42
 */
43
static inline int get_byte(LZOContext *c) {
44
    if (c->in < c->in_end)
45
        return *c->in++;
46 0b178e56 Reimar Döffinger
    c->error |= AV_LZO_INPUT_DEPLETED;
47 d3c71c50 Reimar Döffinger
    return 1;
48 517840c6 Reimar Döffinger
}
49
50 a737f1df Reimar Döffinger
#ifdef INBUF_PADDED
51
#define GETB(c) (*(c).in++)
52
#else
53
#define GETB(c) get_byte(&(c))
54
#endif
55
56 517840c6 Reimar Döffinger
/**
57 89c9ff50 Diego Biurrun
 * \brief Decodes a length value in the coding used by lzo.
58 517840c6 Reimar Döffinger
 * \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 1db8c21c Reimar Döffinger
//#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 517840c6 Reimar Döffinger
/**
85 89c9ff50 Diego Biurrun
 * \brief Copies bytes from input to output buffer with checking.
86 9b2c14df Reimar Döffinger
 * \param cnt number of bytes to copy, must be >= 0
87 517840c6 Reimar Döffinger
 */
88
static inline void copy(LZOContext *c, int cnt) {
89 5e038b4f Reimar Döffinger
    register const uint8_t *src = c->in;
90 f53a2931 Reimar Döffinger
    register uint8_t *dst = c->out;
91 c0a8b876 Reimar Döffinger
    if (cnt > c->in_end - src) {
92 c215e403 Reimar Döffinger
        cnt = FFMAX(c->in_end - src, 0);
93 0b178e56 Reimar Döffinger
        c->error |= AV_LZO_INPUT_DEPLETED;
94 517840c6 Reimar Döffinger
    }
95 c0a8b876 Reimar Döffinger
    if (cnt > c->out_end - dst) {
96 c215e403 Reimar Döffinger
        cnt = FFMAX(c->out_end - dst, 0);
97 0b178e56 Reimar Döffinger
        c->error |= AV_LZO_OUTPUT_FULL;
98 517840c6 Reimar Döffinger
    }
99 f53a2931 Reimar Döffinger
#if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
100 1db8c21c Reimar Döffinger
    COPY4(dst, src);
101 f53a2931 Reimar Döffinger
    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 517840c6 Reimar Döffinger
}
110
111 6ba10f33 Peter Ross
static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
112
113 517840c6 Reimar Döffinger
/**
114 89c9ff50 Diego Biurrun
 * \brief Copies previously decoded bytes to current position.
115 517840c6 Reimar Döffinger
 * \param back how many bytes back we start
116 9b2c14df Reimar Döffinger
 * \param cnt number of bytes to copy, must be >= 0
117 517840c6 Reimar Döffinger
 *
118 f53a2931 Reimar Döffinger
 * 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 517840c6 Reimar Döffinger
 */
121
static inline void copy_backptr(LZOContext *c, int back, int cnt) {
122 5e038b4f Reimar Döffinger
    register const uint8_t *src = &c->out[-back];
123 f53a2931 Reimar Döffinger
    register uint8_t *dst = c->out;
124 cf0ef3dc Reimar Döffinger
    if (src < c->out_start || src > dst) {
125 0b178e56 Reimar Döffinger
        c->error |= AV_LZO_INVALID_BACKPTR;
126 517840c6 Reimar Döffinger
        return;
127
    }
128 c0a8b876 Reimar Döffinger
    if (cnt > c->out_end - dst) {
129 c215e403 Reimar Döffinger
        cnt = FFMAX(c->out_end - dst, 0);
130 0b178e56 Reimar Döffinger
        c->error |= AV_LZO_OUTPUT_FULL;
131 517840c6 Reimar Döffinger
    }
132 6ba10f33 Peter Ross
    memcpy_backptr(dst, back, cnt);
133
    c->out = dst + cnt;
134
}
135
136
static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) {
137
    const uint8_t *src = &dst[-back];
138 f53a2931 Reimar Döffinger
    if (back == 1) {
139
        memset(dst, *src, cnt);
140
    } else {
141
#ifdef OUTBUF_PADDED
142 1db8c21c Reimar Döffinger
        COPY2(dst, src);
143
        COPY2(dst + 2, src + 2);
144 f53a2931 Reimar Döffinger
        src += 4;
145
        dst += 4;
146
        cnt -= 4;
147
        if (cnt > 0) {
148 1db8c21c Reimar Döffinger
            COPY2(dst, src);
149
            COPY2(dst + 2, src + 2);
150
            COPY2(dst + 4, src + 4);
151
            COPY2(dst + 6, src + 6);
152 f53a2931 Reimar Döffinger
            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
    }
168 6ba10f33 Peter Ross
}
169
170
void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
171
    memcpy_backptr(dst, back, cnt);
172 517840c6 Reimar Döffinger
}
173
174 0b178e56 Reimar Döffinger
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
175 bf47272f Michael Niedermayer
    int state= 0;
176 517840c6 Reimar Döffinger
    int x;
177
    LZOContext c;
178
    c.in = in;
179 5e038b4f Reimar Döffinger
    c.in_end = (const uint8_t *)in + *inlen;
180 f53a2931 Reimar Döffinger
    c.out = c.out_start = out;
181 214019ed Reimar Döffinger
    c.out_end = (uint8_t *)out + * outlen;
182 517840c6 Reimar Döffinger
    c.error = 0;
183 a737f1df Reimar Döffinger
    x = GETB(c);
184 517840c6 Reimar Döffinger
    if (x > 17) {
185
        copy(&c, x - 17);
186 a737f1df Reimar Döffinger
        x = GETB(c);
187 0b178e56 Reimar Döffinger
        if (x < 16) c.error |= AV_LZO_ERROR;
188 517840c6 Reimar Döffinger
    }
189 5fe9c42c Reimar Döffinger
    if (c.in > c.in_end)
190 0b178e56 Reimar Döffinger
        c.error |= AV_LZO_INPUT_DEPLETED;
191 517840c6 Reimar Döffinger
    while (!c.error) {
192
        int cnt, back;
193 801778bc Michael Niedermayer
        if (x > 15) {
194
            if (x > 63) {
195 517840c6 Reimar Döffinger
                cnt = (x >> 5) - 1;
196 a737f1df Reimar Döffinger
                back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
197 801778bc Michael Niedermayer
            } else if (x > 31) {
198 517840c6 Reimar Döffinger
                cnt = get_len(&c, x, 31);
199 a737f1df Reimar Döffinger
                x = GETB(c);
200
                back = (GETB(c) << 6) + (x >> 2) + 1;
201 517840c6 Reimar Döffinger
            } else {
202
                cnt = get_len(&c, x, 7);
203
                back = (1 << 14) + ((x & 8) << 11);
204 a737f1df Reimar Döffinger
                x = GETB(c);
205
                back += (GETB(c) << 6) + (x >> 2);
206 517840c6 Reimar Döffinger
                if (back == (1 << 14)) {
207
                    if (cnt != 1)
208 0b178e56 Reimar Döffinger
                        c.error |= AV_LZO_ERROR;
209 517840c6 Reimar Döffinger
                    break;
210
                }
211
            }
212 bf47272f Michael Niedermayer
        } else if(!state){
213 517840c6 Reimar Döffinger
                cnt = get_len(&c, x, 15);
214
                copy(&c, cnt + 3);
215 a737f1df Reimar Döffinger
                x = GETB(c);
216 960e48f8 Michael Niedermayer
                if (x > 15)
217 517840c6 Reimar Döffinger
                    continue;
218
                cnt = 1;
219 a737f1df Reimar Döffinger
                back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
220 bf47272f Michael Niedermayer
        } else {
221 517840c6 Reimar Döffinger
                cnt = 0;
222 a737f1df Reimar Döffinger
                back = (GETB(c) << 2) + (x >> 2) + 1;
223 517840c6 Reimar Döffinger
        }
224
        copy_backptr(&c, back, cnt + 2);
225 bf47272f Michael Niedermayer
        state=
226 517840c6 Reimar Döffinger
        cnt = x & 3;
227 56f8647a Reimar Döffinger
        copy(&c, cnt);
228 a737f1df Reimar Döffinger
        x = GETB(c);
229 517840c6 Reimar Döffinger
    }
230
    *inlen = c.in_end - c.in;
231 a737f1df Reimar Döffinger
    if (c.in > c.in_end)
232
        *inlen = 0;
233 517840c6 Reimar Döffinger
    *outlen = c.out_end - c.out;
234
    return c.error;
235
}
236 266aa26c Reimar Döffinger
237
#ifdef TEST
238
#include <stdio.h>
239
#include <lzo/lzo1x.h>
240
#include "log.h"
241
#define MAXSZ (10*1024*1024)
242 985fdd53 Diego Biurrun
243
/* Define one of these to 1 if you wish to benchmark liblzo
244
 * instead of our native implementation. */
245
#define BENCHMARK_LIBLZO_SAFE   0
246
#define BENCHMARK_LIBLZO_UNSAFE 0
247
248 266aa26c Reimar Döffinger
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;
258 82a6ef20 Diego Biurrun
    av_log_set_level(AV_LOG_DEBUG);
259 266aa26c Reimar Döffinger
    lzo1x_999_compress(orig, s, comp, &clen, tmp);
260
    for (i = 0; i < 300; i++) {
261
START_TIMER
262
        inlen = clen; outlen = MAXSZ;
263 985fdd53 Diego Biurrun
#if BENCHMARK_LIBLZO_SAFE
264 d62a0c1e Reimar Döffinger
        if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
265 985fdd53 Diego Biurrun
#elif BENCHMARK_LIBLZO_UNSAFE
266 d62a0c1e Reimar Döffinger
        if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
267
#else
268 0b178e56 Reimar Döffinger
        if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
269 d62a0c1e Reimar Döffinger
#endif
270 266aa26c Reimar Döffinger
            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
276 89c9ff50 Diego Biurrun
        av_log(NULL, AV_LOG_ERROR, "decompression OK\n");
277 266aa26c Reimar Döffinger
    return 0;
278
}
279
#endif