00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avutil.h"
00023 #include "common.h"
00025 #undef memcpy
00026 #include <string.h>
00027 #include "lzo.h"
00028
00030 #define OUTBUF_PADDED 1
00031
00032 #define INBUF_PADDED 1
00033 typedef struct LZOContext {
00034 const uint8_t *in, *in_end;
00035 uint8_t *out_start, *out, *out_end;
00036 int error;
00037 } LZOContext;
00038
00043 static inline int get_byte(LZOContext *c) {
00044 if (c->in < c->in_end)
00045 return *c->in++;
00046 c->error |= AV_LZO_INPUT_DEPLETED;
00047 return 1;
00048 }
00049
00050 #ifdef INBUF_PADDED
00051 #define GETB(c) (*(c).in++)
00052 #else
00053 #define GETB(c) get_byte(&(c))
00054 #endif
00055
00062 static inline int get_len(LZOContext *c, int x, int mask) {
00063 int cnt = x & mask;
00064 if (!cnt) {
00065 while (!(x = get_byte(c))) cnt += 255;
00066 cnt += mask + x;
00067 }
00068 return cnt;
00069 }
00070
00071
00072 #define BUILTIN_MEMCPY
00073 #ifdef UNALIGNED_LOADSTORE
00074 #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s);
00075 #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s);
00076 #elif defined(BUILTIN_MEMCPY)
00077 #define COPY2(d, s) memcpy(d, s, 2);
00078 #define COPY4(d, s) memcpy(d, s, 4);
00079 #else
00080 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
00081 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
00082 #endif
00083
00088 static inline void copy(LZOContext *c, int cnt) {
00089 register const uint8_t *src = c->in;
00090 register uint8_t *dst = c->out;
00091 if (cnt > c->in_end - src) {
00092 cnt = FFMAX(c->in_end - src, 0);
00093 c->error |= AV_LZO_INPUT_DEPLETED;
00094 }
00095 if (cnt > c->out_end - dst) {
00096 cnt = FFMAX(c->out_end - dst, 0);
00097 c->error |= AV_LZO_OUTPUT_FULL;
00098 }
00099 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
00100 COPY4(dst, src);
00101 src += 4;
00102 dst += 4;
00103 cnt -= 4;
00104 if (cnt > 0)
00105 #endif
00106 memcpy(dst, src, cnt);
00107 c->in = src + cnt;
00108 c->out = dst + cnt;
00109 }
00110
00111 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
00112
00121 static inline void copy_backptr(LZOContext *c, int back, int cnt) {
00122 register uint8_t *dst = c->out;
00123 if (dst - c->out_start < back) {
00124 c->error |= AV_LZO_INVALID_BACKPTR;
00125 return;
00126 }
00127 if (cnt > c->out_end - dst) {
00128 cnt = FFMAX(c->out_end - dst, 0);
00129 c->error |= AV_LZO_OUTPUT_FULL;
00130 }
00131 memcpy_backptr(dst, back, cnt);
00132 c->out = dst + cnt;
00133 }
00134
00135 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) {
00136 const uint8_t *src = &dst[-back];
00137 if (back == 1) {
00138 memset(dst, *src, cnt);
00139 } else {
00140 #ifdef OUTBUF_PADDED
00141 COPY2(dst, src);
00142 COPY2(dst + 2, src + 2);
00143 src += 4;
00144 dst += 4;
00145 cnt -= 4;
00146 if (cnt > 0) {
00147 COPY2(dst, src);
00148 COPY2(dst + 2, src + 2);
00149 COPY2(dst + 4, src + 4);
00150 COPY2(dst + 6, src + 6);
00151 src += 8;
00152 dst += 8;
00153 cnt -= 8;
00154 }
00155 #endif
00156 if (cnt > 0) {
00157 int blocklen = back;
00158 while (cnt > blocklen) {
00159 memcpy(dst, src, blocklen);
00160 dst += blocklen;
00161 cnt -= blocklen;
00162 blocklen <<= 1;
00163 }
00164 memcpy(dst, src, cnt);
00165 }
00166 }
00167 }
00168
00169 void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
00170 memcpy_backptr(dst, back, cnt);
00171 }
00172
00173 int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
00174 int state= 0;
00175 int x;
00176 LZOContext c;
00177 if (!*outlen || !*inlen) {
00178 int res = 0;
00179 if (!*outlen)
00180 res |= AV_LZO_OUTPUT_FULL;
00181 if (!*inlen)
00182 res |= AV_LZO_INPUT_DEPLETED;
00183 return res;
00184 }
00185 c.in = in;
00186 c.in_end = (const uint8_t *)in + *inlen;
00187 c.out = c.out_start = out;
00188 c.out_end = (uint8_t *)out + * outlen;
00189 c.error = 0;
00190 x = GETB(c);
00191 if (x > 17) {
00192 copy(&c, x - 17);
00193 x = GETB(c);
00194 if (x < 16) c.error |= AV_LZO_ERROR;
00195 }
00196 if (c.in > c.in_end)
00197 c.error |= AV_LZO_INPUT_DEPLETED;
00198 while (!c.error) {
00199 int cnt, back;
00200 if (x > 15) {
00201 if (x > 63) {
00202 cnt = (x >> 5) - 1;
00203 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
00204 } else if (x > 31) {
00205 cnt = get_len(&c, x, 31);
00206 x = GETB(c);
00207 back = (GETB(c) << 6) + (x >> 2) + 1;
00208 } else {
00209 cnt = get_len(&c, x, 7);
00210 back = (1 << 14) + ((x & 8) << 11);
00211 x = GETB(c);
00212 back += (GETB(c) << 6) + (x >> 2);
00213 if (back == (1 << 14)) {
00214 if (cnt != 1)
00215 c.error |= AV_LZO_ERROR;
00216 break;
00217 }
00218 }
00219 } else if(!state){
00220 cnt = get_len(&c, x, 15);
00221 copy(&c, cnt + 3);
00222 x = GETB(c);
00223 if (x > 15)
00224 continue;
00225 cnt = 1;
00226 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
00227 } else {
00228 cnt = 0;
00229 back = (GETB(c) << 2) + (x >> 2) + 1;
00230 }
00231 copy_backptr(&c, back, cnt + 2);
00232 state=
00233 cnt = x & 3;
00234 copy(&c, cnt);
00235 x = GETB(c);
00236 }
00237 *inlen = c.in_end - c.in;
00238 if (c.in > c.in_end)
00239 *inlen = 0;
00240 *outlen = c.out_end - c.out;
00241 return c.error;
00242 }
00243
00244 #ifdef TEST
00245 #include <stdio.h>
00246 #include <lzo/lzo1x.h>
00247 #include "log.h"
00248 #define MAXSZ (10*1024*1024)
00249
00250
00251
00252 #define BENCHMARK_LIBLZO_SAFE 0
00253 #define BENCHMARK_LIBLZO_UNSAFE 0
00254
00255 int main(int argc, char *argv[]) {
00256 FILE *in = fopen(argv[1], "rb");
00257 uint8_t *orig = av_malloc(MAXSZ + 16);
00258 uint8_t *comp = av_malloc(2*MAXSZ + 16);
00259 uint8_t *decomp = av_malloc(MAXSZ + 16);
00260 size_t s = fread(orig, 1, MAXSZ, in);
00261 lzo_uint clen = 0;
00262 long tmp[LZO1X_MEM_COMPRESS];
00263 int inlen, outlen;
00264 int i;
00265 av_log_set_level(AV_LOG_DEBUG);
00266 lzo1x_999_compress(orig, s, comp, &clen, tmp);
00267 for (i = 0; i < 300; i++) {
00268 START_TIMER
00269 inlen = clen; outlen = MAXSZ;
00270 #if BENCHMARK_LIBLZO_SAFE
00271 if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
00272 #elif BENCHMARK_LIBLZO_UNSAFE
00273 if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
00274 #else
00275 if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
00276 #endif
00277 av_log(NULL, AV_LOG_ERROR, "decompression error\n");
00278 STOP_TIMER("lzod")
00279 }
00280 if (memcmp(orig, decomp, s))
00281 av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
00282 else
00283 av_log(NULL, AV_LOG_ERROR, "decompression OK\n");
00284 return 0;
00285 }
00286 #endif