Browse code

Our own LZO (1X) implementation, under LGPL and optimized for readability. Tested on CamStudio sample files.

Originally committed as revision 4840 to svn://svn.ffmpeg.org/ffmpeg/trunk

Reimar Döffinger authored on 2006/01/13 02:52:41
Showing 4 changed files
... ...
@@ -169,6 +169,7 @@ ifeq ($(CONFIG_TSCC_DECODER),yes)
169 169
 endif
170 170
 ifeq ($(CONFIG_CSCD_DECODER),yes)
171 171
     OBJS+= cscd.o
172
+    OBJS+= lzo.o
172 173
 endif
173 174
 ifeq ($(CONFIG_ULTI_DECODER),yes)
174 175
     OBJS+= ulti.o
... ...
@@ -25,9 +25,7 @@
25 25
 #ifdef CONFIG_ZLIB
26 26
 #include <zlib.h>
27 27
 #endif
28
-#ifdef CONFIG_LZO
29
-#include <lzo1x.h>
30
-#endif
28
+#include "lzo.h"
31 29
 
32 30
 typedef struct {
33 31
     AVFrame pic;
... ...
@@ -158,16 +156,10 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
158 158
     // decompress data
159 159
     switch ((buf[0] >> 1) & 7) {
160 160
         case 0: { // lzo compression
161
-#ifdef CONFIG_LZO
162
-            unsigned int dlen = c->decomp_size;
163
-            if (lzo1x_decompress_safe(&buf[2], buf_size - 2,
164
-                       c->decomp_buf, &dlen, NULL) != LZO_E_OK)
161
+            int outlen = c->decomp_size, inlen = buf_size - 2;
162
+            if (lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
165 163
                 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
166 164
             break;
167
-#else
168
-            av_log(avctx, AV_LOG_ERROR, "compiled without lzo (GPL) support\n");
169
-            return -1;
170
-#endif
171 165
         }
172 166
         case 1: { // zlib compression
173 167
 #ifdef CONFIG_ZLIB
174 168
new file mode 100644
... ...
@@ -0,0 +1,167 @@
0
+/*
1
+ * LZO 1x decompression
2
+ * Copyright (c) 2006 Reimar Doeffinger
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17
+ */
18
+#include "common.h"
19
+#include "lzo.h"
20
+
21
+typedef struct LZOContext {
22
+    uint8_t *in, *in_end;
23
+    uint8_t *out, *out_end;
24
+    int out_size;
25
+    int error;
26
+} LZOContext;
27
+
28
+/**
29
+ * \brief read one byte from input buffer, avoiding overrun
30
+ * \return byte read
31
+ */
32
+static inline int get_byte(LZOContext *c) {
33
+    if (c->in < c->in_end)
34
+        return *c->in++;
35
+    c->error |= LZO_INPUT_DEPLETED;
36
+    return -1;
37
+}
38
+
39
+/**
40
+ * \brief decode a length value in the coding used by lzo
41
+ * \param x previous byte value
42
+ * \param mask bits used from x
43
+ * \return decoded length value
44
+ */
45
+static inline int get_len(LZOContext *c, int x, int mask) {
46
+    int cnt = x & mask;
47
+    if (!cnt) {
48
+        while (!(x = get_byte(c))) cnt += 255;
49
+        cnt += mask + x;
50
+    }
51
+    return cnt;
52
+}
53
+
54
+/**
55
+ * \brief copy bytes from input to output buffer with checking
56
+ * \param cnt number of bytes to copy, must be > 0
57
+ */
58
+static inline void copy(LZOContext *c, int cnt) {
59
+    if (c->in + cnt > c->in_end) {
60
+        cnt = c->in_end - c->in;
61
+        c->error |= LZO_INPUT_DEPLETED;
62
+    }
63
+    if (c->out + cnt > c->out_end) {
64
+        cnt = c->out_end - c->out;
65
+        c->error |= LZO_OUTPUT_FULL;
66
+    }
67
+    do {
68
+        *c->out++ = *c->in++;
69
+    } while (--cnt);
70
+}
71
+
72
+/**
73
+ * \brief copy previously decoded bytes to current position
74
+ * \param back how many bytes back we start
75
+ * \param cnt number of bytes to copy, must be > 0
76
+ *
77
+ * cnt > back is valid, this will copy the bytes we just copied.
78
+ */
79
+static inline void copy_backptr(LZOContext *c, int back, int cnt) {
80
+    if (c->out - back < c->out_end - c->out_size) {
81
+        c->error |= LZO_INVALID_BACKPTR;
82
+        return;
83
+    }
84
+    if (c->out + cnt > c->out_end) {
85
+        cnt = c->out_end - c->out;
86
+        c->error |= LZO_OUTPUT_FULL;
87
+    }
88
+    do {
89
+        *c->out++ = c->out[-back];
90
+    } while (--cnt);
91
+}
92
+
93
+/**
94
+ * \brief decode LZO 1x compressed data
95
+ * \param out output buffer
96
+ * \param outlen size of output buffer, number of bytes left are returned here
97
+ * \param in input buffer
98
+ * \param inlen size of input buffer, number of bytes left are returned here
99
+ * \return 0 on success, otherwise error flags, see lzo.h
100
+ */
101
+int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) {
102
+    enum {COPY, BACKPTR} state = COPY;
103
+    int x;
104
+    LZOContext c;
105
+    c.in = in;
106
+    c.in_end = in + *inlen;
107
+    c.out = out;
108
+    c.out_end = out + * outlen;
109
+    c.out_size = *outlen;
110
+    c.error = 0;
111
+    x = get_byte(&c);
112
+    if (x > 17) {
113
+        copy(&c, x - 17);
114
+        x = get_byte(&c);
115
+        if (x < 16) c.error |= LZO_ERROR;
116
+    }
117
+    while (!c.error) {
118
+        int cnt, back;
119
+        if (x >> 4) {
120
+            state = BACKPTR;
121
+            if (x >> 6) {
122
+                cnt = (x >> 5) - 1;
123
+                back = (get_byte(&c) << 3) + ((x >> 2) & 7) + 1;
124
+            } else if (x >> 5) {
125
+                cnt = get_len(&c, x, 31);
126
+                x = get_byte(&c);
127
+                back = (get_byte(&c) << 6) + (x >> 2) + 1;
128
+            } else {
129
+                cnt = get_len(&c, x, 7);
130
+                back = (1 << 14) + ((x & 8) << 11);
131
+                x = get_byte(&c);
132
+                back += (get_byte(&c) << 6) + (x >> 2);
133
+                if (back == (1 << 14)) {
134
+                    if (cnt != 1)
135
+                        c.error |= LZO_ERROR;
136
+                    break;
137
+                }
138
+            }
139
+        } else
140
+        switch (state) {
141
+            case COPY:
142
+                cnt = get_len(&c, x, 15);
143
+                copy(&c, cnt + 3);
144
+                x = get_byte(&c);
145
+                if (x >> 4)
146
+                    continue;
147
+                cnt = 1;
148
+                back = (1 << 11) + (get_byte(&c) << 2) + (x >> 2) + 1;
149
+                break;
150
+            case BACKPTR:
151
+                cnt = 0;
152
+                back = (get_byte(&c) << 2) + (x >> 2) + 1;
153
+                break;
154
+        }
155
+        copy_backptr(&c, back, cnt + 2);
156
+        cnt = x & 3;
157
+        if (cnt)
158
+            copy(&c, cnt);
159
+        else
160
+            state = (state == COPY) ? BACKPTR : COPY;
161
+        x = get_byte(&c);
162
+    }
163
+    *inlen = c.in_end - c.in;
164
+    *outlen = c.out_end - c.out;
165
+    return c.error;
166
+}
0 167
new file mode 100644
... ...
@@ -0,0 +1,11 @@
0
+#ifndef _LZO_H
1
+#define LZO_H
2
+
3
+#define LZO_INPUT_DEPLETED 1
4
+#define LZO_OUTPUT_FULL 2
5
+#define LZO_INVALID_BACKPTR 4
6
+#define LZO_ERROR 8
7
+
8
+int lzo1x_decode(void *out, int *outlen, void *in, int *inlen);
9
+
10
+#endif