annotate lzo.c @ 574:5e52a169969c libavutil

Allow using DECLARE_ALIGNED with Sun cc.
author cehoyos
date Thu, 02 Oct 2008 10:47:05 +0000
parents b8574db98875
children 8c48a1b999a3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
234
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
1 /*
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
2 * LZO 1x decompression
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
3 * Copyright (c) 2006 Reimar Doeffinger
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
4 *
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
5 * This file is part of FFmpeg.
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
6 *
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
11 *
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
15 * Lesser General Public License for more details.
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
16 *
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
20 */
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
21 #include "common.h"
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
22 //! avoid e.g. MPlayers fast_memcpy, it slows things down here
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
23 #undef memcpy
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
24 #include <string.h>
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
25 #include "lzo.h"
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
26
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
27 //! define if we may write up to 12 bytes beyond the output buffer
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
28 #define OUTBUF_PADDED 1
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
29 //! define if we may read up to 8 bytes beyond the input buffer
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
30 #define INBUF_PADDED 1
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
31 typedef struct LZOContext {
445
1b2867913935 LZO decoder input is const
reimar
parents: 234
diff changeset
32 const uint8_t *in, *in_end;
234
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
33 uint8_t *out_start, *out, *out_end;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
34 int error;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
35 } LZOContext;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
36
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
37 /**
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
38 * \brief read one byte from input buffer, avoiding overrun
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
39 * \return byte read
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
40 */
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
41 static inline int get_byte(LZOContext *c) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
42 if (c->in < c->in_end)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
43 return *c->in++;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
44 c->error |= LZO_INPUT_DEPLETED;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
45 return 1;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
46 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
47
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
48 #ifdef INBUF_PADDED
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
49 #define GETB(c) (*(c).in++)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
50 #else
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
51 #define GETB(c) get_byte(&(c))
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
52 #endif
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
53
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
54 /**
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
55 * \brief decode a length value in the coding used by lzo
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
56 * \param x previous byte value
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
57 * \param mask bits used from x
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
58 * \return decoded length value
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
59 */
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
60 static inline int get_len(LZOContext *c, int x, int mask) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
61 int cnt = x & mask;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
62 if (!cnt) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
63 while (!(x = get_byte(c))) cnt += 255;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
64 cnt += mask + x;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
65 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
66 return cnt;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
67 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
68
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
69 //#define UNALIGNED_LOADSTORE
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
70 #define BUILTIN_MEMCPY
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
71 #ifdef UNALIGNED_LOADSTORE
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
72 #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
73 #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
74 #elif defined(BUILTIN_MEMCPY)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
75 #define COPY2(d, s) memcpy(d, s, 2);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
76 #define COPY4(d, s) memcpy(d, s, 4);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
77 #else
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
78 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
79 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
80 #endif
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
81
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
82 /**
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
83 * \brief copy bytes from input to output buffer with checking
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
84 * \param cnt number of bytes to copy, must be >= 0
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
85 */
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
86 static inline void copy(LZOContext *c, int cnt) {
445
1b2867913935 LZO decoder input is const
reimar
parents: 234
diff changeset
87 register const uint8_t *src = c->in;
234
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
88 register uint8_t *dst = c->out;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
89 if (cnt > c->in_end - src) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
90 cnt = FFMAX(c->in_end - src, 0);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
91 c->error |= LZO_INPUT_DEPLETED;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
92 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
93 if (cnt > c->out_end - dst) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
94 cnt = FFMAX(c->out_end - dst, 0);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
95 c->error |= LZO_OUTPUT_FULL;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
96 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
97 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
98 COPY4(dst, src);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
99 src += 4;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
100 dst += 4;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
101 cnt -= 4;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
102 if (cnt > 0)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
103 #endif
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
104 memcpy(dst, src, cnt);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
105 c->in = src + cnt;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
106 c->out = dst + cnt;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
107 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
108
541
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
109 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
110
234
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
111 /**
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
112 * \brief copy previously decoded bytes to current position
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
113 * \param back how many bytes back we start
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
114 * \param cnt number of bytes to copy, must be >= 0
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
115 *
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
116 * cnt > back is valid, this will copy the bytes we just copied,
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
117 * thus creating a repeating pattern with a period length of back.
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
118 */
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
119 static inline void copy_backptr(LZOContext *c, int back, int cnt) {
445
1b2867913935 LZO decoder input is const
reimar
parents: 234
diff changeset
120 register const uint8_t *src = &c->out[-back];
234
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
121 register uint8_t *dst = c->out;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
122 if (src < c->out_start || src > dst) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
123 c->error |= LZO_INVALID_BACKPTR;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
124 return;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
125 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
126 if (cnt > c->out_end - dst) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
127 cnt = FFMAX(c->out_end - dst, 0);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
128 c->error |= LZO_OUTPUT_FULL;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
129 }
541
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
130 memcpy_backptr(dst, back, cnt);
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
131 c->out = dst + cnt;
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
132 }
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
133
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
134 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) {
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
135 const uint8_t *src = &dst[-back];
234
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
136 if (back == 1) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
137 memset(dst, *src, cnt);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
138 } else {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
139 #ifdef OUTBUF_PADDED
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
140 COPY2(dst, src);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
141 COPY2(dst + 2, src + 2);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
142 src += 4;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
143 dst += 4;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
144 cnt -= 4;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
145 if (cnt > 0) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
146 COPY2(dst, src);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
147 COPY2(dst + 2, src + 2);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
148 COPY2(dst + 4, src + 4);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
149 COPY2(dst + 6, src + 6);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
150 src += 8;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
151 dst += 8;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
152 cnt -= 8;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
153 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
154 #endif
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
155 if (cnt > 0) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
156 int blocklen = back;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
157 while (cnt > blocklen) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
158 memcpy(dst, src, blocklen);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
159 dst += blocklen;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
160 cnt -= blocklen;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
161 blocklen <<= 1;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
162 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
163 memcpy(dst, src, cnt);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
164 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
165 }
541
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
166 }
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
167
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
168 /**
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
169 * \brief deliberately overlapping memcpy implementation
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
170 * \param dst destination buffer; must be padded with 12 additional bytes
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
171 * \param back how many bytes back we start (the initial size of the overlapping window)
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
172 * \param cnt number of bytes to copy, must be >= 0
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
173 *
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
174 * cnt > back is valid, this will copy the bytes we just copied,
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
175 * thus creating a repeating pattern with a period length of back.
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
176 */
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
177 void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
b8574db98875 Add av_memcpy_backptr(): deliberately overlapping memcpy variant.
pross
parents: 445
diff changeset
178 memcpy_backptr(dst, back, cnt);
234
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
179 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
180
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
181 /**
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
182 * \brief decode LZO 1x compressed data
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
183 * \param out output buffer
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
184 * \param outlen size of output buffer, number of bytes left are returned here
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
185 * \param in input buffer
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
186 * \param inlen size of input buffer, number of bytes left are returned here
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
187 * \return 0 on success, otherwise error flags, see lzo.h
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
188 *
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
189 * make sure all buffers are appropriately padded, in must provide
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
190 * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
191 */
445
1b2867913935 LZO decoder input is const
reimar
parents: 234
diff changeset
192 int lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
234
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
193 int state= 0;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
194 int x;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
195 LZOContext c;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
196 c.in = in;
445
1b2867913935 LZO decoder input is const
reimar
parents: 234
diff changeset
197 c.in_end = (const uint8_t *)in + *inlen;
234
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
198 c.out = c.out_start = out;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
199 c.out_end = (uint8_t *)out + * outlen;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
200 c.error = 0;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
201 x = GETB(c);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
202 if (x > 17) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
203 copy(&c, x - 17);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
204 x = GETB(c);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
205 if (x < 16) c.error |= LZO_ERROR;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
206 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
207 if (c.in > c.in_end)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
208 c.error |= LZO_INPUT_DEPLETED;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
209 while (!c.error) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
210 int cnt, back;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
211 if (x > 15) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
212 if (x > 63) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
213 cnt = (x >> 5) - 1;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
214 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
215 } else if (x > 31) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
216 cnt = get_len(&c, x, 31);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
217 x = GETB(c);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
218 back = (GETB(c) << 6) + (x >> 2) + 1;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
219 } else {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
220 cnt = get_len(&c, x, 7);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
221 back = (1 << 14) + ((x & 8) << 11);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
222 x = GETB(c);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
223 back += (GETB(c) << 6) + (x >> 2);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
224 if (back == (1 << 14)) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
225 if (cnt != 1)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
226 c.error |= LZO_ERROR;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
227 break;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
228 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
229 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
230 } else if(!state){
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
231 cnt = get_len(&c, x, 15);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
232 copy(&c, cnt + 3);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
233 x = GETB(c);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
234 if (x > 15)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
235 continue;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
236 cnt = 1;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
237 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
238 } else {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
239 cnt = 0;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
240 back = (GETB(c) << 2) + (x >> 2) + 1;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
241 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
242 copy_backptr(&c, back, cnt + 2);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
243 state=
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
244 cnt = x & 3;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
245 copy(&c, cnt);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
246 x = GETB(c);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
247 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
248 *inlen = c.in_end - c.in;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
249 if (c.in > c.in_end)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
250 *inlen = 0;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
251 *outlen = c.out_end - c.out;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
252 return c.error;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
253 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
254
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
255 #ifdef TEST
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
256 #include <stdio.h>
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
257 #include <lzo/lzo1x.h>
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
258 #include "log.h"
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
259 #define MAXSZ (10*1024*1024)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
260 int main(int argc, char *argv[]) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
261 FILE *in = fopen(argv[1], "rb");
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
262 uint8_t *orig = av_malloc(MAXSZ + 16);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
263 uint8_t *comp = av_malloc(2*MAXSZ + 16);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
264 uint8_t *decomp = av_malloc(MAXSZ + 16);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
265 size_t s = fread(orig, 1, MAXSZ, in);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
266 lzo_uint clen = 0;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
267 long tmp[LZO1X_MEM_COMPRESS];
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
268 int inlen, outlen;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
269 int i;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
270 av_log_level = AV_LOG_DEBUG;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
271 lzo1x_999_compress(orig, s, comp, &clen, tmp);
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
272 for (i = 0; i < 300; i++) {
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
273 START_TIMER
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
274 inlen = clen; outlen = MAXSZ;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
275 #ifdef LIBLZO
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
276 if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
277 #elif defined(LIBLZO_UNSAFE)
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
278 if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
279 #else
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
280 if (lzo1x_decode(decomp, &outlen, comp, &inlen))
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
281 #endif
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
282 av_log(NULL, AV_LOG_ERROR, "decompression error\n");
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
283 STOP_TIMER("lzod")
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
284 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
285 if (memcmp(orig, decomp, s))
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
286 av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
287 else
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
288 av_log(NULL, AV_LOG_ERROR, "decompression ok\n");
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
289 return 0;
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
290 }
5ad447ede15c Move lzo decompression to libavutil
reimar
parents:
diff changeset
291 #endif