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 < 0) {
00092 c->error |= AV_LZO_ERROR;
00093 return;
00094 }
00095 if (cnt > c->in_end - src) {
00096 cnt = FFMAX(c->in_end - src, 0);
00097 c->error |= AV_LZO_INPUT_DEPLETED;
00098 }
00099 if (cnt > c->out_end - dst) {
00100 cnt = FFMAX(c->out_end - dst, 0);
00101 c->error |= AV_LZO_OUTPUT_FULL;
00102 }
00103 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
00104 COPY4(dst, src);
00105 src += 4;
00106 dst += 4;
00107 cnt -= 4;
00108 if (cnt > 0)
00109 #endif
00110 memcpy(dst, src, cnt);
00111 c->in = src + cnt;
00112 c->out = dst + cnt;
00113 }
00114
00115 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
00116
00125 static inline void copy_backptr(LZOContext *c, int back, int cnt) {
00126 register uint8_t *dst = c->out;
00127 if (cnt <= 0) {
00128 c->error |= AV_LZO_ERROR;
00129 return;
00130 }
00131 if (dst - c->out_start < back) {
00132 c->error |= AV_LZO_INVALID_BACKPTR;
00133 return;
00134 }
00135 if (cnt > c->out_end - dst) {
00136 cnt = FFMAX(c->out_end - dst, 0);
00137 c->error |= AV_LZO_OUTPUT_FULL;
00138 }
00139 memcpy_backptr(dst, back, cnt);
00140 c->out = dst + cnt;
00141 }
00142
00143 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) {
00144 const uint8_t *src = &dst[-back];
00145 if (back == 1) {
00146 memset(dst, *src, cnt);
00147 } else {
00148 #ifdef OUTBUF_PADDED
00149 COPY2(dst, src);
00150 COPY2(dst + 2, src + 2);
00151 src += 4;
00152 dst += 4;
00153 cnt -= 4;
00154 if (cnt > 0) {
00155 COPY2(dst, src);
00156 COPY2(dst + 2, src + 2);
00157 COPY2(dst + 4, src + 4);
00158 COPY2(dst + 6, src + 6);
00159 src += 8;
00160 dst += 8;
00161 cnt -= 8;
00162 }
00163 #endif
00164 if (cnt > 0) {
00165 int blocklen = back;
00166 while (cnt > blocklen) {
00167 memcpy(dst, src, blocklen);
00168 dst += blocklen;
00169 cnt -= blocklen;
00170 blocklen <<= 1;
00171 }
00172 memcpy(dst, src, cnt);
00173 }
00174 }
00175 }
00176
00177 void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
00178 memcpy_backptr(dst, back, cnt);
00179 }
00180
00181 int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
00182 int state= 0;
00183 int x;
00184 LZOContext c;
00185 if (!*outlen || !*inlen) {
00186 int res = 0;
00187 if (!*outlen)
00188 res |= AV_LZO_OUTPUT_FULL;
00189 if (!*inlen)
00190 res |= AV_LZO_INPUT_DEPLETED;
00191 return res;
00192 }
00193 c.in = in;
00194 c.in_end = (const uint8_t *)in + *inlen;
00195 c.out = c.out_start = out;
00196 c.out_end = (uint8_t *)out + * outlen;
00197 c.error = 0;
00198 x = GETB(c);
00199 if (x > 17) {
00200 copy(&c, x - 17);
00201 x = GETB(c);
00202 if (x < 16) c.error |= AV_LZO_ERROR;
00203 }
00204 if (c.in > c.in_end)
00205 c.error |= AV_LZO_INPUT_DEPLETED;
00206 while (!c.error) {
00207 int cnt, back;
00208 if (x > 15) {
00209 if (x > 63) {
00210 cnt = (x >> 5) - 1;
00211 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
00212 } else if (x > 31) {
00213 cnt = get_len(&c, x, 31);
00214 x = GETB(c);
00215 back = (GETB(c) << 6) + (x >> 2) + 1;
00216 } else {
00217 cnt = get_len(&c, x, 7);
00218 back = (1 << 14) + ((x & 8) << 11);
00219 x = GETB(c);
00220 back += (GETB(c) << 6) + (x >> 2);
00221 if (back == (1 << 14)) {
00222 if (cnt != 1)
00223 c.error |= AV_LZO_ERROR;
00224 break;
00225 }
00226 }
00227 } else if(!state){
00228 cnt = get_len(&c, x, 15);
00229 copy(&c, cnt + 3);
00230 x = GETB(c);
00231 if (x > 15)
00232 continue;
00233 cnt = 1;
00234 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
00235 } else {
00236 cnt = 0;
00237 back = (GETB(c) << 2) + (x >> 2) + 1;
00238 }
00239 copy_backptr(&c, back, cnt + 2);
00240 state=
00241 cnt = x & 3;
00242 copy(&c, cnt);
00243 x = GETB(c);
00244 }
00245 *inlen = c.in_end - c.in;
00246 if (c.in > c.in_end)
00247 *inlen = 0;
00248 *outlen = c.out_end - c.out;
00249 return c.error;
00250 }
00251
00252 #ifdef TEST
00253 #include <stdio.h>
00254 #include <lzo/lzo1x.h>
00255 #include "log.h"
00256 #define MAXSZ (10*1024*1024)
00257
00258
00259
00260 #define BENCHMARK_LIBLZO_SAFE 0
00261 #define BENCHMARK_LIBLZO_UNSAFE 0
00262
00263 int main(int argc, char *argv[]) {
00264 FILE *in = fopen(argv[1], "rb");
00265 uint8_t *orig = av_malloc(MAXSZ + 16);
00266 uint8_t *comp = av_malloc(2*MAXSZ + 16);
00267 uint8_t *decomp = av_malloc(MAXSZ + 16);
00268 size_t s = fread(orig, 1, MAXSZ, in);
00269 lzo_uint clen = 0;
00270 long tmp[LZO1X_MEM_COMPRESS];
00271 int inlen, outlen;
00272 int i;
00273 av_log_set_level(AV_LOG_DEBUG);
00274 lzo1x_999_compress(orig, s, comp, &clen, tmp);
00275 for (i = 0; i < 300; i++) {
00276 START_TIMER
00277 inlen = clen; outlen = MAXSZ;
00278 #if BENCHMARK_LIBLZO_SAFE
00279 if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
00280 #elif BENCHMARK_LIBLZO_UNSAFE
00281 if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
00282 #else
00283 if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
00284 #endif
00285 av_log(NULL, AV_LOG_ERROR, "decompression error\n");
00286 STOP_TIMER("lzod")
00287 }
00288 if (memcmp(orig, decomp, s))
00289 av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
00290 else
00291 av_log(NULL, AV_LOG_ERROR, "decompression OK\n");
00292 return 0;
00293 }
00294 #endif