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
28 /*! define if we may write up to 12 bytes beyond the output buffer */
29 /* #define OUTBUF_PADDED 1 */
30 /*! define if we may read up to 8 bytes beyond the input buffer */
31 /* #define INBUF_PADDED 1 */
32 typedef struct LZOContext
34 const uint8_t *in, *in_end;
35 uint8_t *out_start, *out, *out_end;
40 * \brief read one byte from input buffer, avoiding overrun
44 get_byte (LZOContext * c)
46 if (c->in < c->in_end)
48 c->error |= LZO_INPUT_DEPLETED;
53 #define GETB(c) (*(c).in++)
55 #define GETB(c) get_byte(&(c))
59 * \brief decode a length value in the coding used by lzo
60 * \param x previous byte value
61 * \param mask bits used from x
62 * \return decoded length value
65 get_len (LZOContext * c, int x, int mask)
69 while (!(x = get_byte (c)))
76 /*#define UNALIGNED_LOADSTORE */
77 #define BUILTIN_MEMCPY
78 #ifdef UNALIGNED_LOADSTORE
79 #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s);
80 #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s);
81 #elif defined(BUILTIN_MEMCPY)
82 #define COPY2(d, s) memcpy(d, s, 2);
83 #define COPY4(d, s) memcpy(d, s, 4);
85 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
86 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
90 * \brief copy bytes from input to output buffer with checking
91 * \param cnt number of bytes to copy, must be >= 0
94 copy (LZOContext * c, int cnt)
96 register const uint8_t *src = c->in;
97 register uint8_t *dst = c->out;
98 if (cnt > c->in_end - src) {
99 cnt = MAX (c->in_end - src, 0);
100 c->error |= LZO_INPUT_DEPLETED;
102 if (cnt > c->out_end - dst) {
103 cnt = MAX (c->out_end - dst, 0);
104 c->error |= LZO_OUTPUT_FULL;
106 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
113 memcpy (dst, src, cnt);
119 * \brief copy previously decoded bytes to current position
120 * \param back how many bytes back we start
121 * \param cnt number of bytes to copy, must be >= 0
123 * cnt > back is valid, this will copy the bytes we just copied,
124 * thus creating a repeating pattern with a period length of back.
127 copy_backptr (LZOContext * c, int back, int cnt)
129 register const uint8_t *src = &c->out[-back];
130 register uint8_t *dst = c->out;
131 if (src < c->out_start || src > dst) {
132 c->error |= LZO_INVALID_BACKPTR;
135 if (cnt > c->out_end - dst) {
136 cnt = MAX (c->out_end - dst, 0);
137 c->error |= LZO_OUTPUT_FULL;
140 memset (dst, *src, cnt);
145 COPY2 (dst + 2, src + 2);
151 COPY2 (dst + 2, src + 2);
152 COPY2 (dst + 4, src + 4);
153 COPY2 (dst + 6, src + 6);
161 while (cnt > blocklen) {
162 memcpy (dst, src, blocklen);
167 memcpy (dst, src, cnt);
175 * \brief decode LZO 1x compressed data
176 * \param out output buffer
177 * \param outlen size of output buffer, number of bytes left are returned here
178 * \param in input buffer
179 * \param inlen size of input buffer, number of bytes left are returned here
180 * \return 0 on success, otherwise error flags, see lzo.h
182 * make sure all buffers are appropriately padded, in must provide
183 * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
186 lzo1x_decode (void *out, int *outlen, const void *in, int *inlen)
192 c.in_end = (const uint8_t *) in + *inlen;
193 c.out = c.out_start = out;
194 c.out_end = (uint8_t *) out + *outlen;
201 c.error |= LZO_ERROR;
204 c.error |= LZO_INPUT_DEPLETED;
210 back = (GETB (c) << 3) + ((x >> 2) & 7) + 1;
212 cnt = get_len (&c, x, 31);
214 back = (GETB (c) << 6) + (x >> 2) + 1;
216 cnt = get_len (&c, x, 7);
217 back = (1 << 14) + ((x & 8) << 11);
219 back += (GETB (c) << 6) + (x >> 2);
220 if (back == (1 << 14)) {
222 c.error |= LZO_ERROR;
227 cnt = get_len (&c, x, 15);
233 back = (1 << 11) + (GETB (c) << 2) + (x >> 2) + 1;
236 back = (GETB (c) << 2) + (x >> 2) + 1;
238 copy_backptr (&c, back, cnt + 2);
243 *inlen = c.in_end - c.in;
246 *outlen = c.out_end - c.out;
252 #include <lzo/lzo1x.h>
254 #define MAXSZ (10*1024*1024)
256 main (int argc, char *argv[])
258 FILE *in = fopen (argv[1], "rb");
259 uint8_t *orig = av_malloc (MAXSZ + 16);
260 uint8_t *comp = av_malloc (2 * MAXSZ + 16);
261 uint8_t *decomp = av_malloc (MAXSZ + 16);
262 size_t s = fread (orig, 1, MAXSZ, in);
264 long tmp[LZO1X_MEM_COMPRESS];
267 av_log_level = AV_LOG_DEBUG;
268 lzo1x_999_compress (orig, s, comp, &clen, tmp);
269 for (i = 0; i < 300; i++) {
270 START_TIMER inlen = clen;
273 if (lzo1x_decompress_safe (comp, inlen, decomp, &outlen, NULL))
274 #elif defined(LIBLZO_UNSAFE)
275 if (lzo1x_decompress (comp, inlen, decomp, &outlen, NULL))
277 if (lzo1x_decode (decomp, &outlen, comp, &inlen))
279 av_log (NULL, AV_LOG_ERROR, "decompression error\n");
282 if (memcmp (orig, decomp, s))
283 av_log (NULL, AV_LOG_ERROR, "decompression incorrect\n");
285 av_log (NULL, AV_LOG_ERROR, "decompression ok\n");