3 * Copyright (c) 2006 Reimar Doeffinger
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 /*! define if we may write up to 12 bytes beyond the output buffer */
31 /* #define OUTBUF_PADDED 1 */
32 /*! define if we may read up to 8 bytes beyond the input buffer */
33 /* #define INBUF_PADDED 1 */
34 typedef struct LZOContext
36 const guint8 *in, *in_end;
37 guint8 *out_start, *out, *out_end;
42 * \brief read one byte from input buffer, avoiding overrun
46 get_byte (LZOContext * c)
48 if (c->in < c->in_end)
50 c->error |= LZO_INPUT_DEPLETED;
55 #define GETB(c) (*(c).in++)
57 #define GETB(c) get_byte(&(c))
61 * \brief decode a length value in the coding used by lzo
62 * \param x previous byte value
63 * \param mask bits used from x
64 * \return decoded length value
67 get_len (LZOContext * c, int x, int mask)
71 while (!(x = get_byte (c)))
78 /*#define UNALIGNED_LOADSTORE */
79 #define BUILTIN_MEMCPY
80 #ifdef UNALIGNED_LOADSTORE
81 #define COPY2(d, s) *(guint16 *)(d) = *(guint16 *)(s);
82 #define COPY4(d, s) *(guint32 *)(d) = *(guint32 *)(s);
83 #elif defined(BUILTIN_MEMCPY)
84 #define COPY2(d, s) memcpy(d, s, 2);
85 #define COPY4(d, s) memcpy(d, s, 4);
87 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
88 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
92 * \brief copy bytes from input to output buffer with checking
93 * \param cnt number of bytes to copy, must be >= 0
96 copy (LZOContext * c, int cnt)
98 register const guint8 *src = c->in;
99 register guint8 *dst = c->out;
100 if (cnt > c->in_end - src) {
101 cnt = MAX (c->in_end - src, 0);
102 c->error |= LZO_INPUT_DEPLETED;
104 if (cnt > c->out_end - dst) {
105 cnt = MAX (c->out_end - dst, 0);
106 c->error |= LZO_OUTPUT_FULL;
108 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
115 memcpy (dst, src, cnt);
121 * \brief copy previously decoded bytes to current position
122 * \param back how many bytes back we start
123 * \param cnt number of bytes to copy, must be >= 0
125 * cnt > back is valid, this will copy the bytes we just copied,
126 * thus creating a repeating pattern with a period length of back.
129 copy_backptr (LZOContext * c, int back, int cnt)
131 register const guint8 *src = &c->out[-back];
132 register guint8 *dst = c->out;
133 if (src < c->out_start || src > dst) {
134 c->error |= LZO_INVALID_BACKPTR;
137 if (cnt > c->out_end - dst) {
138 cnt = MAX (c->out_end - dst, 0);
139 c->error |= LZO_OUTPUT_FULL;
142 memset (dst, *src, cnt);
147 COPY2 (dst + 2, src + 2);
153 COPY2 (dst + 2, src + 2);
154 COPY2 (dst + 4, src + 4);
155 COPY2 (dst + 6, src + 6);
163 while (cnt > blocklen) {
164 memcpy (dst, src, blocklen);
169 memcpy (dst, src, cnt);
177 * \brief decode LZO 1x compressed data
178 * \param out output buffer
179 * \param outlen size of output buffer, number of bytes left are returned here
180 * \param in input buffer
181 * \param inlen size of input buffer, number of bytes left are returned here
182 * \return 0 on success, otherwise error flags, see lzo.h
184 * make sure all buffers are appropriately padded, in must provide
185 * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
188 lzo1x_decode (void *out, int *outlen, const void *in, int *inlen)
194 c.in_end = (const guint8 *) in + *inlen;
195 c.out = c.out_start = out;
196 c.out_end = (guint8 *) out + *outlen;
203 c.error |= LZO_ERROR;
206 c.error |= LZO_INPUT_DEPLETED;
212 back = (GETB (c) << 3) + ((x >> 2) & 7) + 1;
214 cnt = get_len (&c, x, 31);
216 back = (GETB (c) << 6) + (x >> 2) + 1;
218 cnt = get_len (&c, x, 7);
219 back = (1 << 14) + ((x & 8) << 11);
221 back += (GETB (c) << 6) + (x >> 2);
222 if (back == (1 << 14)) {
224 c.error |= LZO_ERROR;
229 cnt = get_len (&c, x, 15);
235 back = (1 << 11) + (GETB (c) << 2) + (x >> 2) + 1;
238 back = (GETB (c) << 2) + (x >> 2) + 1;
240 copy_backptr (&c, back, cnt + 2);
245 *inlen = c.in_end - c.in;
248 *outlen = c.out_end - c.out;
254 #include <lzo/lzo1x.h>
256 #define MAXSZ (10*1024*1024)
258 main (int argc, char *argv[])
260 FILE *in = fopen (argv[1], "rb");
261 guint8 *orig = av_malloc (MAXSZ + 16);
262 guint8 *comp = av_malloc (2 * MAXSZ + 16);
263 guint8 *decomp = av_malloc (MAXSZ + 16);
264 gsize s = fread (orig, 1, MAXSZ, in);
266 long tmp[LZO1X_MEM_COMPRESS];
269 av_log_level = AV_LOG_DEBUG;
270 lzo1x_999_compress (orig, s, comp, &clen, tmp);
271 for (i = 0; i < 300; i++) {
272 START_TIMER inlen = clen;
275 if (lzo1x_decompress_safe (comp, inlen, decomp, &outlen, NULL))
276 #elif defined(LIBLZO_UNSAFE)
277 if (lzo1x_decompress (comp, inlen, decomp, &outlen, NULL))
279 if (lzo1x_decode (decomp, &outlen, comp, &inlen))
281 av_log (NULL, AV_LOG_ERROR, "decompression error\n");
284 if (memcmp (orig, decomp, s))
285 av_log (NULL, AV_LOG_ERROR, "decompression incorrect\n");
287 av_log (NULL, AV_LOG_ERROR, "decompression ok\n");