mirror of https://github.com/FFmpeg/FFmpeg.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
7.8 KiB
274 lines
7.8 KiB
19 years ago
|
/*
|
||
|
* LZO 1x decompression
|
||
|
* Copyright (c) 2006 Reimar Doeffinger
|
||
|
*
|
||
18 years ago
|
* This file is part of FFmpeg.
|
||
|
*
|
||
|
* FFmpeg is free software; you can redistribute it and/or
|
||
19 years ago
|
* modify it under the terms of the GNU Lesser General Public
|
||
|
* License as published by the Free Software Foundation; either
|
||
18 years ago
|
* version 2.1 of the License, or (at your option) any later version.
|
||
19 years ago
|
*
|
||
18 years ago
|
* FFmpeg is distributed in the hope that it will be useful,
|
||
19 years ago
|
* 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 years ago
|
* License along with FFmpeg; if not, write to the Free Software
|
||
19 years ago
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
19 years ago
|
*/
|
||
|
#include "common.h"
|
||
19 years ago
|
//! avoid e.g. MPlayers fast_memcpy, it slows things down here
|
||
|
#undef memcpy
|
||
|
#include <string.h>
|
||
19 years ago
|
#include "lzo.h"
|
||
|
|
||
19 years ago
|
//! define if we may write up to 12 bytes beyond the output buffer
|
||
|
#define OUTBUF_PADDED 1
|
||
18 years ago
|
//! define if we may read up to 8 bytes beyond the input buffer
|
||
19 years ago
|
#define INBUF_PADDED 1
|
||
19 years ago
|
typedef struct LZOContext {
|
||
|
uint8_t *in, *in_end;
|
||
19 years ago
|
uint8_t *out_start, *out, *out_end;
|
||
19 years ago
|
int error;
|
||
|
} LZOContext;
|
||
|
|
||
|
/**
|
||
|
* \brief read one byte from input buffer, avoiding overrun
|
||
|
* \return byte read
|
||
|
*/
|
||
|
static inline int get_byte(LZOContext *c) {
|
||
|
if (c->in < c->in_end)
|
||
|
return *c->in++;
|
||
|
c->error |= LZO_INPUT_DEPLETED;
|
||
19 years ago
|
return 1;
|
||
19 years ago
|
}
|
||
|
|
||
18 years ago
|
#ifdef INBUF_PADDED
|
||
|
#define GETB(c) (*(c).in++)
|
||
|
#else
|
||
|
#define GETB(c) get_byte(&(c))
|
||
|
#endif
|
||
|
|
||
19 years ago
|
/**
|
||
|
* \brief decode a length value in the coding used by lzo
|
||
|
* \param x previous byte value
|
||
|
* \param mask bits used from x
|
||
|
* \return decoded length value
|
||
|
*/
|
||
|
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;
|
||
|
}
|
||
|
|
||
18 years ago
|
//#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
|
||
|
|
||
19 years ago
|
/**
|
||
|
* \brief copy bytes from input to output buffer with checking
|
||
18 years ago
|
* \param cnt number of bytes to copy, must be >= 0
|
||
19 years ago
|
*/
|
||
|
static inline void copy(LZOContext *c, int cnt) {
|
||
19 years ago
|
register uint8_t *src = c->in;
|
||
|
register uint8_t *dst = c->out;
|
||
18 years ago
|
if (cnt > c->in_end - src) {
|
||
18 years ago
|
cnt = FFMAX(c->in_end - src, 0);
|
||
19 years ago
|
c->error |= LZO_INPUT_DEPLETED;
|
||
|
}
|
||
18 years ago
|
if (cnt > c->out_end - dst) {
|
||
18 years ago
|
cnt = FFMAX(c->out_end - dst, 0);
|
||
19 years ago
|
c->error |= LZO_OUTPUT_FULL;
|
||
|
}
|
||
19 years ago
|
#if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
|
||
18 years ago
|
COPY4(dst, src);
|
||
19 years ago
|
src += 4;
|
||
|
dst += 4;
|
||
|
cnt -= 4;
|
||
|
if (cnt > 0)
|
||
|
#endif
|
||
|
memcpy(dst, src, cnt);
|
||
|
c->in = src + cnt;
|
||
|
c->out = dst + cnt;
|
||
19 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* \brief copy previously decoded bytes to current position
|
||
|
* \param back how many bytes back we start
|
||
18 years ago
|
* \param cnt number of bytes to copy, must be >= 0
|
||
19 years ago
|
*
|
||
19 years ago
|
* cnt > back is valid, this will copy the bytes we just copied,
|
||
|
* thus creating a repeating pattern with a period length of back.
|
||
19 years ago
|
*/
|
||
|
static inline void copy_backptr(LZOContext *c, int back, int cnt) {
|
||
19 years ago
|
register uint8_t *src = &c->out[-back];
|
||
|
register uint8_t *dst = c->out;
|
||
18 years ago
|
if (src < c->out_start || src > dst) {
|
||
19 years ago
|
c->error |= LZO_INVALID_BACKPTR;
|
||
|
return;
|
||
|
}
|
||
18 years ago
|
if (cnt > c->out_end - dst) {
|
||
18 years ago
|
cnt = FFMAX(c->out_end - dst, 0);
|
||
19 years ago
|
c->error |= LZO_OUTPUT_FULL;
|
||
|
}
|
||
19 years ago
|
if (back == 1) {
|
||
|
memset(dst, *src, cnt);
|
||
|
dst += cnt;
|
||
|
} else {
|
||
|
#ifdef OUTBUF_PADDED
|
||
18 years ago
|
COPY2(dst, src);
|
||
|
COPY2(dst + 2, src + 2);
|
||
19 years ago
|
src += 4;
|
||
|
dst += 4;
|
||
|
cnt -= 4;
|
||
|
if (cnt > 0) {
|
||
18 years ago
|
COPY2(dst, src);
|
||
|
COPY2(dst + 2, src + 2);
|
||
|
COPY2(dst + 4, src + 4);
|
||
|
COPY2(dst + 6, src + 6);
|
||
19 years ago
|
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);
|
||
|
}
|
||
|
dst += cnt;
|
||
|
}
|
||
|
c->out = dst;
|
||
19 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* \brief decode LZO 1x compressed data
|
||
|
* \param out output buffer
|
||
|
* \param outlen size of output buffer, number of bytes left are returned here
|
||
|
* \param in input buffer
|
||
|
* \param inlen size of input buffer, number of bytes left are returned here
|
||
|
* \return 0 on success, otherwise error flags, see lzo.h
|
||
19 years ago
|
*
|
||
|
* make sure all buffers are appropriately padded, in must provide
|
||
|
* LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
|
||
19 years ago
|
*/
|
||
|
int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) {
|
||
18 years ago
|
int state= 0;
|
||
19 years ago
|
int x;
|
||
|
LZOContext c;
|
||
|
c.in = in;
|
||
18 years ago
|
c.in_end = (uint8_t *)in + *inlen;
|
||
19 years ago
|
c.out = c.out_start = out;
|
||
18 years ago
|
c.out_end = (uint8_t *)out + * outlen;
|
||
19 years ago
|
c.error = 0;
|
||
18 years ago
|
x = GETB(c);
|
||
19 years ago
|
if (x > 17) {
|
||
|
copy(&c, x - 17);
|
||
18 years ago
|
x = GETB(c);
|
||
19 years ago
|
if (x < 16) c.error |= LZO_ERROR;
|
||
|
}
|
||
18 years ago
|
if (c.in > c.in_end)
|
||
|
c.error |= LZO_INPUT_DEPLETED;
|
||
19 years ago
|
while (!c.error) {
|
||
|
int cnt, back;
|
||
18 years ago
|
if (x > 15) {
|
||
|
if (x > 63) {
|
||
19 years ago
|
cnt = (x >> 5) - 1;
|
||
18 years ago
|
back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
|
||
18 years ago
|
} else if (x > 31) {
|
||
19 years ago
|
cnt = get_len(&c, x, 31);
|
||
18 years ago
|
x = GETB(c);
|
||
|
back = (GETB(c) << 6) + (x >> 2) + 1;
|
||
19 years ago
|
} else {
|
||
|
cnt = get_len(&c, x, 7);
|
||
|
back = (1 << 14) + ((x & 8) << 11);
|
||
18 years ago
|
x = GETB(c);
|
||
|
back += (GETB(c) << 6) + (x >> 2);
|
||
19 years ago
|
if (back == (1 << 14)) {
|
||
|
if (cnt != 1)
|
||
|
c.error |= LZO_ERROR;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
18 years ago
|
} else if(!state){
|
||
19 years ago
|
cnt = get_len(&c, x, 15);
|
||
|
copy(&c, cnt + 3);
|
||
18 years ago
|
x = GETB(c);
|
||
18 years ago
|
if (x > 15)
|
||
19 years ago
|
continue;
|
||
|
cnt = 1;
|
||
18 years ago
|
back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
|
||
18 years ago
|
} else {
|
||
19 years ago
|
cnt = 0;
|
||
18 years ago
|
back = (GETB(c) << 2) + (x >> 2) + 1;
|
||
19 years ago
|
}
|
||
|
copy_backptr(&c, back, cnt + 2);
|
||
18 years ago
|
state=
|
||
19 years ago
|
cnt = x & 3;
|
||
18 years ago
|
copy(&c, cnt);
|
||
18 years ago
|
x = GETB(c);
|
||
19 years ago
|
}
|
||
|
*inlen = c.in_end - c.in;
|
||
18 years ago
|
if (c.in > c.in_end)
|
||
|
*inlen = 0;
|
||
19 years ago
|
*outlen = c.out_end - c.out;
|
||
|
return c.error;
|
||
|
}
|
||
18 years ago
|
|
||
|
#ifdef TEST
|
||
|
#include <stdio.h>
|
||
|
#include <lzo/lzo1x.h>
|
||
|
#include "log.h"
|
||
|
#define MAXSZ (10*1024*1024)
|
||
|
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;
|
||
|
av_log_level = AV_LOG_DEBUG;
|
||
|
lzo1x_999_compress(orig, s, comp, &clen, tmp);
|
||
|
for (i = 0; i < 300; i++) {
|
||
|
START_TIMER
|
||
|
inlen = clen; outlen = MAXSZ;
|
||
18 years ago
|
#ifdef LIBLZO
|
||
|
if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
|
||
|
#elif defined(LIBLZO_UNSAFE)
|
||
|
if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
|
||
|
#else
|
||
18 years ago
|
if (lzo1x_decode(decomp, &outlen, comp, &inlen))
|
||
18 years ago
|
#endif
|
||
18 years ago
|
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
|
||
|
av_log(NULL, AV_LOG_ERROR, "decompression ok\n");
|
||
|
return 0;
|
||
|
}
|
||
|
#endif
|