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 c.in = in;
00179 c.in_end = (const uint8_t *)in + *inlen;
00180 c.out = c.out_start = out;
00181 c.out_end = (uint8_t *)out + * outlen;
00182 c.error = 0;
00183 x = GETB(c);
00184 if (x > 17) {
00185 copy(&c, x - 17);
00186 x = GETB(c);
00187 if (x < 16) c.error |= AV_LZO_ERROR;
00188 }
00189 if (c.in > c.in_end)
00190 c.error |= AV_LZO_INPUT_DEPLETED;
00191 while (!c.error) {
00192 int cnt, back;
00193 if (x > 15) {
00194 if (x > 63) {
00195 cnt = (x >> 5) - 1;
00196 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
00197 } else if (x > 31) {
00198 cnt = get_len(&c, x, 31);
00199 x = GETB(c);
00200 back = (GETB(c) << 6) + (x >> 2) + 1;
00201 } else {
00202 cnt = get_len(&c, x, 7);
00203 back = (1 << 14) + ((x & 8) << 11);
00204 x = GETB(c);
00205 back += (GETB(c) << 6) + (x >> 2);
00206 if (back == (1 << 14)) {
00207 if (cnt != 1)
00208 c.error |= AV_LZO_ERROR;
00209 break;
00210 }
00211 }
00212 } else if(!state){
00213 cnt = get_len(&c, x, 15);
00214 copy(&c, cnt + 3);
00215 x = GETB(c);
00216 if (x > 15)
00217 continue;
00218 cnt = 1;
00219 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
00220 } else {
00221 cnt = 0;
00222 back = (GETB(c) << 2) + (x >> 2) + 1;
00223 }
00224 copy_backptr(&c, back, cnt + 2);
00225 state=
00226 cnt = x & 3;
00227 copy(&c, cnt);
00228 x = GETB(c);
00229 }
00230 *inlen = c.in_end - c.in;
00231 if (c.in > c.in_end)
00232 *inlen = 0;
00233 *outlen = c.out_end - c.out;
00234 return c.error;
00235 }
00236
00237 #if LIBAVUTIL_VERSION_MAJOR < 50
00238 int lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
00239 return av_lzo1x_decode(out, outlen, in, inlen);
00240 }
00241 #endif
00242
00243 #ifdef TEST
00244 #include <stdio.h>
00245 #include <lzo/lzo1x.h>
00246 #include "log.h"
00247 #define MAXSZ (10*1024*1024)
00248 int main(int argc, char *argv[]) {
00249 FILE *in = fopen(argv[1], "rb");
00250 uint8_t *orig = av_malloc(MAXSZ + 16);
00251 uint8_t *comp = av_malloc(2*MAXSZ + 16);
00252 uint8_t *decomp = av_malloc(MAXSZ + 16);
00253 size_t s = fread(orig, 1, MAXSZ, in);
00254 lzo_uint clen = 0;
00255 long tmp[LZO1X_MEM_COMPRESS];
00256 int inlen, outlen;
00257 int i;
00258 av_log_level = AV_LOG_DEBUG;
00259 lzo1x_999_compress(orig, s, comp, &clen, tmp);
00260 for (i = 0; i < 300; i++) {
00261 START_TIMER
00262 inlen = clen; outlen = MAXSZ;
00263 #ifdef LIBLZO
00264 if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
00265 #elif defined(LIBLZO_UNSAFE)
00266 if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
00267 #else
00268 if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
00269 #endif
00270 av_log(NULL, AV_LOG_ERROR, "decompression error\n");
00271 STOP_TIMER("lzod")
00272 }
00273 if (memcmp(orig, decomp, s))
00274 av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
00275 else
00276 av_log(NULL, AV_LOG_ERROR, "decompression OK\n");
00277 return 0;
00278 }
00279 #endif