lzo.c 8.1 KB
Newer Older
1 2 3 4
/*
 * LZO 1x decompression
 * Copyright (c) 2006 Reimar Doeffinger
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21 22

#include "avutil.h"
23
#include "common.h"
24
/// Avoid e.g. MPlayers fast_memcpy, it slows things down here.
25 26
#undef memcpy
#include <string.h>
27 28
#include "lzo.h"

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 {
R
Reimar Döffinger 已提交
34
    const uint8_t *in, *in_end;
35
    uint8_t *out_start, *out, *out_end;
36 37 38 39
    int error;
} LZOContext;

/**
40 41
 * @brief Reads one byte from the input buffer, avoiding an overrun.
 * @return byte read
42 43 44 45
 */
static inline int get_byte(LZOContext *c) {
    if (c->in < c->in_end)
        return *c->in++;
46
    c->error |= AV_LZO_INPUT_DEPLETED;
47
    return 1;
48 49
}

50 51 52 53 54 55
#ifdef INBUF_PADDED
#define GETB(c) (*(c).in++)
#else
#define GETB(c) get_byte(&(c))
#endif

56
/**
57 58 59 60
 * @brief Decodes a length value in the coding used by lzo.
 * @param x previous byte value
 * @param mask bits used from x
 * @return decoded length value
61 62 63 64 65 66 67 68 69 70
 */
static inline int get_len(LZOContext *c, int x, int mask) {
    int cnt = x & mask;
    if (!cnt) {
        while (!(x = get_byte(c))) cnt += 255;
        cnt += mask + x;
    }
    return cnt;
}

R
Reimar Döffinger 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83
//#define UNALIGNED_LOADSTORE
#define BUILTIN_MEMCPY
#ifdef UNALIGNED_LOADSTORE
#define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s);
#define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s);
#elif defined(BUILTIN_MEMCPY)
#define COPY2(d, s) memcpy(d, s, 2);
#define COPY4(d, s) memcpy(d, s, 4);
#else
#define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
#define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
#endif

84
/**
85 86
 * @brief Copies bytes from input to output buffer with checking.
 * @param cnt number of bytes to copy, must be >= 0
87 88
 */
static inline void copy(LZOContext *c, int cnt) {
R
Reimar Döffinger 已提交
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;
94
    }
95
    if (cnt > c->out_end - dst) {
96
        cnt = FFMAX(c->out_end - dst, 0);
97
        c->error |= AV_LZO_OUTPUT_FULL;
98
    }
99
#if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
R
Reimar Döffinger 已提交
100
    COPY4(dst, src);
101 102 103 104 105 106 107 108
    src += 4;
    dst += 4;
    cnt -= 4;
    if (cnt > 0)
#endif
        memcpy(dst, src, cnt);
    c->in = src + cnt;
    c->out = dst + cnt;
109 110
}

111 112
static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);

113
/**
114 115 116
 * @brief Copies previously decoded bytes to current position.
 * @param back how many bytes back we start
 * @param cnt number of bytes to copy, must be >= 0
117
 *
118 119
 * cnt > back is valid, this will copy the bytes we just copied,
 * thus creating a repeating pattern with a period length of back.
120 121
 */
static inline void copy_backptr(LZOContext *c, int back, int cnt) {
R
Reimar Döffinger 已提交
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;
126 127
        return;
    }
128
    if (cnt > c->out_end - dst) {
129
        cnt = FFMAX(c->out_end - dst, 0);
130
        c->error |= AV_LZO_OUTPUT_FULL;
131
    }
132 133 134 135 136 137
    memcpy_backptr(dst, back, cnt);
    c->out = dst + cnt;
}

static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) {
    const uint8_t *src = &dst[-back];
138 139 140 141
    if (back == 1) {
        memset(dst, *src, cnt);
    } else {
#ifdef OUTBUF_PADDED
R
Reimar Döffinger 已提交
142 143
        COPY2(dst, src);
        COPY2(dst + 2, src + 2);
144 145 146 147
        src += 4;
        dst += 4;
        cnt -= 4;
        if (cnt > 0) {
R
Reimar Döffinger 已提交
148 149 150 151
            COPY2(dst, src);
            COPY2(dst + 2, src + 2);
            COPY2(dst + 4, src + 4);
            COPY2(dst + 6, src + 6);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            src += 8;
            dst += 8;
            cnt -= 8;
        }
#endif
        if (cnt > 0) {
            int blocklen = back;
            while (cnt > blocklen) {
                memcpy(dst, src, blocklen);
                dst += blocklen;
                cnt -= blocklen;
                blocklen <<= 1;
            }
            memcpy(dst, src, cnt);
        }
    }
168 169 170 171
}

void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
    memcpy_backptr(dst, back, cnt);
172 173
}

174
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
175
    int state= 0;
176 177
    int x;
    LZOContext c;
178 179 180 181 182 183 184 185
    if (!*outlen || !*inlen) {
        int res = 0;
        if (!*outlen)
            res |= AV_LZO_OUTPUT_FULL;
        if (!*inlen)
            res |= AV_LZO_INPUT_DEPLETED;
        return res;
    }
186
    c.in = in;
R
Reimar Döffinger 已提交
187
    c.in_end = (const uint8_t *)in + *inlen;
188
    c.out = c.out_start = out;
R
Reimar Döffinger 已提交
189
    c.out_end = (uint8_t *)out + * outlen;
190
    c.error = 0;
191
    x = GETB(c);
192 193
    if (x > 17) {
        copy(&c, x - 17);
194
        x = GETB(c);
195
        if (x < 16) c.error |= AV_LZO_ERROR;
196
    }
197
    if (c.in > c.in_end)
198
        c.error |= AV_LZO_INPUT_DEPLETED;
199 200
    while (!c.error) {
        int cnt, back;
201 202
        if (x > 15) {
            if (x > 63) {
203
                cnt = (x >> 5) - 1;
204
                back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
205
            } else if (x > 31) {
206
                cnt = get_len(&c, x, 31);
207 208
                x = GETB(c);
                back = (GETB(c) << 6) + (x >> 2) + 1;
209 210 211
            } else {
                cnt = get_len(&c, x, 7);
                back = (1 << 14) + ((x & 8) << 11);
212 213
                x = GETB(c);
                back += (GETB(c) << 6) + (x >> 2);
214 215
                if (back == (1 << 14)) {
                    if (cnt != 1)
216
                        c.error |= AV_LZO_ERROR;
217 218 219
                    break;
                }
            }
220
        } else if(!state){
221 222
                cnt = get_len(&c, x, 15);
                copy(&c, cnt + 3);
223
                x = GETB(c);
M
Michael Niedermayer 已提交
224
                if (x > 15)
225 226
                    continue;
                cnt = 1;
227
                back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
228
        } else {
229
                cnt = 0;
230
                back = (GETB(c) << 2) + (x >> 2) + 1;
231 232
        }
        copy_backptr(&c, back, cnt + 2);
233
        state=
234
        cnt = x & 3;
235
        copy(&c, cnt);
236
        x = GETB(c);
237 238
    }
    *inlen = c.in_end - c.in;
239 240
    if (c.in > c.in_end)
        *inlen = 0;
241 242 243
    *outlen = c.out_end - c.out;
    return c.error;
}
R
Reimar Döffinger 已提交
244 245 246 247 248 249

#ifdef TEST
#include <stdio.h>
#include <lzo/lzo1x.h>
#include "log.h"
#define MAXSZ (10*1024*1024)
250 251 252 253 254 255

/* Define one of these to 1 if you wish to benchmark liblzo
 * instead of our native implementation. */
#define BENCHMARK_LIBLZO_SAFE   0
#define BENCHMARK_LIBLZO_UNSAFE 0

R
Reimar Döffinger 已提交
256 257 258 259 260 261 262 263 264 265
int main(int argc, char *argv[]) {
    FILE *in = fopen(argv[1], "rb");
    uint8_t *orig = av_malloc(MAXSZ + 16);
    uint8_t *comp = av_malloc(2*MAXSZ + 16);
    uint8_t *decomp = av_malloc(MAXSZ + 16);
    size_t s = fread(orig, 1, MAXSZ, in);
    lzo_uint clen = 0;
    long tmp[LZO1X_MEM_COMPRESS];
    int inlen, outlen;
    int i;
266
    av_log_set_level(AV_LOG_DEBUG);
R
Reimar Döffinger 已提交
267 268 269 270
    lzo1x_999_compress(orig, s, comp, &clen, tmp);
    for (i = 0; i < 300; i++) {
START_TIMER
        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 275
        if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
#else
276
        if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
277
#endif
R
Reimar Döffinger 已提交
278 279 280 281 282 283
            av_log(NULL, AV_LOG_ERROR, "decompression error\n");
STOP_TIMER("lzod")
    }
    if (memcmp(orig, decomp, s))
        av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
    else
284
        av_log(NULL, AV_LOG_ERROR, "decompression OK\n");
R
Reimar Döffinger 已提交
285 286 287
    return 0;
}
#endif