517840c6 |
/*
* LZO 1x decompression
* Copyright (c) 2006 Reimar Doeffinger
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
517840c6 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
517840c6 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
517840c6 |
* 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 |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
517840c6 |
*/ |
0d08e270 |
#include "avutil.h" |
517840c6 |
#include "common.h" |
89c9ff50 |
//! Avoid e.g. MPlayers fast_memcpy, it slows things down here. |
f53a2931 |
#undef memcpy
#include <string.h> |
517840c6 |
#include "lzo.h"
|
89c9ff50 |
//! Define if we may write up to 12 bytes beyond the output buffer. |
f53a2931 |
#define OUTBUF_PADDED 1 |
89c9ff50 |
//! Define if we may read up to 8 bytes beyond the input buffer. |
f53a2931 |
#define INBUF_PADDED 1 |
517840c6 |
typedef struct LZOContext { |
5e038b4f |
const uint8_t *in, *in_end; |
f53a2931 |
uint8_t *out_start, *out, *out_end; |
517840c6 |
int error;
} LZOContext;
/** |
bfe3676f |
* \brief Reads one byte from the input buffer, avoiding an overrun. |
517840c6 |
* \return byte read
*/
static inline int get_byte(LZOContext *c) {
if (c->in < c->in_end)
return *c->in++; |
0b178e56 |
c->error |= AV_LZO_INPUT_DEPLETED; |
d3c71c50 |
return 1; |
517840c6 |
}
|
a737f1df |
#ifdef INBUF_PADDED
#define GETB(c) (*(c).in++)
#else
#define GETB(c) get_byte(&(c))
#endif
|
517840c6 |
/** |
89c9ff50 |
* \brief Decodes a length value in the coding used by lzo. |
517840c6 |
* \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;
}
|
1db8c21c |
//#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
|
517840c6 |
/** |
89c9ff50 |
* \brief Copies bytes from input to output buffer with checking. |
9b2c14df |
* \param cnt number of bytes to copy, must be >= 0 |
517840c6 |
*/
static inline void copy(LZOContext *c, int cnt) { |
5e038b4f |
register const uint8_t *src = c->in; |
f53a2931 |
register uint8_t *dst = c->out; |
c0a8b876 |
if (cnt > c->in_end - src) { |
c215e403 |
cnt = FFMAX(c->in_end - src, 0); |
0b178e56 |
c->error |= AV_LZO_INPUT_DEPLETED; |
517840c6 |
} |
c0a8b876 |
if (cnt > c->out_end - dst) { |
c215e403 |
cnt = FFMAX(c->out_end - dst, 0); |
0b178e56 |
c->error |= AV_LZO_OUTPUT_FULL; |
517840c6 |
} |
f53a2931 |
#if defined(INBUF_PADDED) && defined(OUTBUF_PADDED) |
1db8c21c |
COPY4(dst, src); |
f53a2931 |
src += 4;
dst += 4;
cnt -= 4;
if (cnt > 0)
#endif
memcpy(dst, src, cnt);
c->in = src + cnt;
c->out = dst + cnt; |
517840c6 |
}
|
6ba10f33 |
static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
|
517840c6 |
/** |
89c9ff50 |
* \brief Copies previously decoded bytes to current position. |
517840c6 |
* \param back how many bytes back we start |
9b2c14df |
* \param cnt number of bytes to copy, must be >= 0 |
517840c6 |
* |
f53a2931 |
* cnt > back is valid, this will copy the bytes we just copied,
* thus creating a repeating pattern with a period length of back. |
517840c6 |
*/
static inline void copy_backptr(LZOContext *c, int back, int cnt) { |
5e038b4f |
register const uint8_t *src = &c->out[-back]; |
f53a2931 |
register uint8_t *dst = c->out; |
cf0ef3dc |
if (src < c->out_start || src > dst) { |
0b178e56 |
c->error |= AV_LZO_INVALID_BACKPTR; |
517840c6 |
return;
} |
c0a8b876 |
if (cnt > c->out_end - dst) { |
c215e403 |
cnt = FFMAX(c->out_end - dst, 0); |
0b178e56 |
c->error |= AV_LZO_OUTPUT_FULL; |
517840c6 |
} |
6ba10f33 |
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]; |
f53a2931 |
if (back == 1) {
memset(dst, *src, cnt);
} else {
#ifdef OUTBUF_PADDED |
1db8c21c |
COPY2(dst, src);
COPY2(dst + 2, src + 2); |
f53a2931 |
src += 4;
dst += 4;
cnt -= 4;
if (cnt > 0) { |
1db8c21c |
COPY2(dst, src);
COPY2(dst + 2, src + 2);
COPY2(dst + 4, src + 4);
COPY2(dst + 6, src + 6); |
f53a2931 |
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);
}
} |
6ba10f33 |
}
void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
memcpy_backptr(dst, back, cnt); |
517840c6 |
}
|
0b178e56 |
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) { |
bf47272f |
int state= 0; |
517840c6 |
int x;
LZOContext c; |
80a173a3 |
if (*outlen <= 0 || *inlen <= 0) { |
7d508260 |
int res = 0; |
80a173a3 |
if (*outlen <= 0) |
7d508260 |
res |= AV_LZO_OUTPUT_FULL; |
80a173a3 |
if (*inlen <= 0) |
7d508260 |
res |= AV_LZO_INPUT_DEPLETED;
return res;
} |
517840c6 |
c.in = in; |
5e038b4f |
c.in_end = (const uint8_t *)in + *inlen; |
f53a2931 |
c.out = c.out_start = out; |
214019ed |
c.out_end = (uint8_t *)out + * outlen; |
517840c6 |
c.error = 0; |
a737f1df |
x = GETB(c); |
517840c6 |
if (x > 17) {
copy(&c, x - 17); |
a737f1df |
x = GETB(c); |
0b178e56 |
if (x < 16) c.error |= AV_LZO_ERROR; |
517840c6 |
} |
5fe9c42c |
if (c.in > c.in_end) |
0b178e56 |
c.error |= AV_LZO_INPUT_DEPLETED; |
517840c6 |
while (!c.error) {
int cnt, back; |
801778bc |
if (x > 15) {
if (x > 63) { |
517840c6 |
cnt = (x >> 5) - 1; |
a737f1df |
back = (GETB(c) << 3) + ((x >> 2) & 7) + 1; |
801778bc |
} else if (x > 31) { |
517840c6 |
cnt = get_len(&c, x, 31); |
a737f1df |
x = GETB(c);
back = (GETB(c) << 6) + (x >> 2) + 1; |
517840c6 |
} else {
cnt = get_len(&c, x, 7);
back = (1 << 14) + ((x & 8) << 11); |
a737f1df |
x = GETB(c);
back += (GETB(c) << 6) + (x >> 2); |
517840c6 |
if (back == (1 << 14)) {
if (cnt != 1) |
0b178e56 |
c.error |= AV_LZO_ERROR; |
517840c6 |
break;
}
} |
bf47272f |
} else if(!state){ |
517840c6 |
cnt = get_len(&c, x, 15);
copy(&c, cnt + 3); |
a737f1df |
x = GETB(c); |
960e48f8 |
if (x > 15) |
517840c6 |
continue;
cnt = 1; |
a737f1df |
back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1; |
bf47272f |
} else { |
517840c6 |
cnt = 0; |
a737f1df |
back = (GETB(c) << 2) + (x >> 2) + 1; |
517840c6 |
}
copy_backptr(&c, back, cnt + 2); |
bf47272f |
state= |
517840c6 |
cnt = x & 3; |
56f8647a |
copy(&c, cnt); |
a737f1df |
x = GETB(c); |
517840c6 |
}
*inlen = c.in_end - c.in; |
a737f1df |
if (c.in > c.in_end)
*inlen = 0; |
517840c6 |
*outlen = c.out_end - c.out;
return c.error;
} |
266aa26c |
#ifdef TEST
#include <stdio.h>
#include <lzo/lzo1x.h>
#include "log.h"
#define MAXSZ (10*1024*1024) |
985fdd53 |
/* 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
|
266aa26c |
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; |
82a6ef20 |
av_log_set_level(AV_LOG_DEBUG); |
266aa26c |
lzo1x_999_compress(orig, s, comp, &clen, tmp);
for (i = 0; i < 300; i++) {
START_TIMER
inlen = clen; outlen = MAXSZ; |
985fdd53 |
#if BENCHMARK_LIBLZO_SAFE |
d62a0c1e |
if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL)) |
985fdd53 |
#elif BENCHMARK_LIBLZO_UNSAFE |
d62a0c1e |
if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
#else |
0b178e56 |
if (av_lzo1x_decode(decomp, &outlen, comp, &inlen)) |
d62a0c1e |
#endif |
266aa26c |
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 |
89c9ff50 |
av_log(NULL, AV_LOG_ERROR, "decompression OK\n"); |
266aa26c |
return 0;
}
#endif |