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