Mercurial > libavcodec.hg
annotate lzo.c @ 4447:ed710b7b5f72 libavcodec
Documentation fix: Copy functions should "work" fine for cnt == 0
author | reimar |
---|---|
date | Wed, 31 Jan 2007 20:26:32 +0000 |
parents | b8b844ae5937 |
children | 2dd1ace1919c |
rev | line source |
---|---|
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
1 /* |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
2 * LZO 1x decompression |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
3 * Copyright (c) 2006 Reimar Doeffinger |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
4 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3060
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3060
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3060
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
9 * License as published by the Free Software Foundation; either |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3060
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3060
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
15 * Lesser General Public License for more details. |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
16 * |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3060
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
3034
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
20 */ |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
21 #include "common.h" |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
22 //! avoid e.g. MPlayers fast_memcpy, it slows things down here |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
23 #undef memcpy |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
24 #include <string.h> |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
25 #include "lzo.h" |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
26 |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
27 //! define if we may write up to 12 bytes beyond the output buffer |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
28 #define OUTBUF_PADDED 1 |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
29 //! define if we may read up to 8 bytes beyond the input buffer |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
30 #define INBUF_PADDED 1 |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
31 typedef struct LZOContext { |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
32 uint8_t *in, *in_end; |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
33 uint8_t *out_start, *out, *out_end; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
34 int error; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
35 } LZOContext; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
36 |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
37 /** |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
38 * \brief read one byte from input buffer, avoiding overrun |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
39 * \return byte read |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
40 */ |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
41 static inline int get_byte(LZOContext *c) { |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
42 if (c->in < c->in_end) |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
43 return *c->in++; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
44 c->error |= LZO_INPUT_DEPLETED; |
3049
9f85c9cf6034
10l, get_byte returning 0 on error can cause a hang. So let's try with 1 instead...
reimar
parents:
3042
diff
changeset
|
45 return 1; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
46 } |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
47 |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
48 #ifdef INBUF_PADDED |
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
49 #define GETB(c) (*(c).in++) |
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
50 #else |
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
51 #define GETB(c) get_byte(&(c)) |
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
52 #endif |
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
53 |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
54 /** |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
55 * \brief decode a length value in the coding used by lzo |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
56 * \param x previous byte value |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
57 * \param mask bits used from x |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
58 * \return decoded length value |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
59 */ |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
60 static inline int get_len(LZOContext *c, int x, int mask) { |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
61 int cnt = x & mask; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
62 if (!cnt) { |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
63 while (!(x = get_byte(c))) cnt += 255; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
64 cnt += mask + x; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
65 } |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
66 return cnt; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
67 } |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
68 |
4446 | 69 //#define UNALIGNED_LOADSTORE |
70 #define BUILTIN_MEMCPY | |
71 #ifdef UNALIGNED_LOADSTORE | |
72 #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s); | |
73 #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s); | |
74 #elif defined(BUILTIN_MEMCPY) | |
75 #define COPY2(d, s) memcpy(d, s, 2); | |
76 #define COPY4(d, s) memcpy(d, s, 4); | |
77 #else | |
78 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; | |
79 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3]; | |
80 #endif | |
81 | |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
82 /** |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
83 * \brief copy bytes from input to output buffer with checking |
4447
ed710b7b5f72
Documentation fix: Copy functions should "work" fine for cnt == 0
reimar
parents:
4446
diff
changeset
|
84 * \param cnt number of bytes to copy, must be >= 0 |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
85 */ |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
86 static inline void copy(LZOContext *c, int cnt) { |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
87 register uint8_t *src = c->in; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
88 register uint8_t *dst = c->out; |
4419
4ade01ded107
Fix buffer end checks in lzo copy code to work in all cases.
reimar
parents:
4417
diff
changeset
|
89 if (src + cnt > c->in_end || src + cnt < src) { |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
90 cnt = c->in_end - src; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
91 c->error |= LZO_INPUT_DEPLETED; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
92 } |
4419
4ade01ded107
Fix buffer end checks in lzo copy code to work in all cases.
reimar
parents:
4417
diff
changeset
|
93 if (dst + cnt > c->out_end || dst + cnt < dst) { |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
94 cnt = c->out_end - dst; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
95 c->error |= LZO_OUTPUT_FULL; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
96 } |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
97 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED) |
4446 | 98 COPY4(dst, src); |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
99 src += 4; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
100 dst += 4; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
101 cnt -= 4; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
102 if (cnt > 0) |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
103 #endif |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
104 memcpy(dst, src, cnt); |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
105 c->in = src + cnt; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
106 c->out = dst + cnt; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
107 } |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
108 |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
109 /** |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
110 * \brief copy previously decoded bytes to current position |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
111 * \param back how many bytes back we start |
4447
ed710b7b5f72
Documentation fix: Copy functions should "work" fine for cnt == 0
reimar
parents:
4446
diff
changeset
|
112 * \param cnt number of bytes to copy, must be >= 0 |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
113 * |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
114 * cnt > back is valid, this will copy the bytes we just copied, |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
115 * thus creating a repeating pattern with a period length of back. |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
116 */ |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
117 static inline void copy_backptr(LZOContext *c, int back, int cnt) { |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
118 register uint8_t *src = &c->out[-back]; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
119 register uint8_t *dst = c->out; |
4419
4ade01ded107
Fix buffer end checks in lzo copy code to work in all cases.
reimar
parents:
4417
diff
changeset
|
120 if (src < c->out_start || src > dst) { |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
121 c->error |= LZO_INVALID_BACKPTR; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
122 return; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
123 } |
4419
4ade01ded107
Fix buffer end checks in lzo copy code to work in all cases.
reimar
parents:
4417
diff
changeset
|
124 if (dst + cnt > c->out_end || dst + cnt < dst) { |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
125 cnt = c->out_end - dst; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
126 c->error |= LZO_OUTPUT_FULL; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
127 } |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
128 if (back == 1) { |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
129 memset(dst, *src, cnt); |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
130 dst += cnt; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
131 } else { |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
132 #ifdef OUTBUF_PADDED |
4446 | 133 COPY2(dst, src); |
134 COPY2(dst + 2, src + 2); | |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
135 src += 4; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
136 dst += 4; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
137 cnt -= 4; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
138 if (cnt > 0) { |
4446 | 139 COPY2(dst, src); |
140 COPY2(dst + 2, src + 2); | |
141 COPY2(dst + 4, src + 4); | |
142 COPY2(dst + 6, src + 6); | |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
143 src += 8; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
144 dst += 8; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
145 cnt -= 8; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
146 } |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
147 #endif |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
148 if (cnt > 0) { |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
149 int blocklen = back; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
150 while (cnt > blocklen) { |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
151 memcpy(dst, src, blocklen); |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
152 dst += blocklen; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
153 cnt -= blocklen; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
154 blocklen <<= 1; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
155 } |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
156 memcpy(dst, src, cnt); |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
157 } |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
158 dst += cnt; |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
159 } |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
160 c->out = dst; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
161 } |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
162 |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
163 /** |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
164 * \brief decode LZO 1x compressed data |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
165 * \param out output buffer |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
166 * \param outlen size of output buffer, number of bytes left are returned here |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
167 * \param in input buffer |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
168 * \param inlen size of input buffer, number of bytes left are returned here |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
169 * \return 0 on success, otherwise error flags, see lzo.h |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
170 * |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
171 * make sure all buffers are appropriately padded, in must provide |
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
172 * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
173 */ |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
174 int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) { |
4443 | 175 int state= 0; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
176 int x; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
177 LZOContext c; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
178 c.in = in; |
3958 | 179 c.in_end = (uint8_t *)in + *inlen; |
3060
a2f611d6c34d
faster copy functions for lzo decoder that also need padding
reimar
parents:
3049
diff
changeset
|
180 c.out = c.out_start = out; |
3958 | 181 c.out_end = (uint8_t *)out + * outlen; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
182 c.error = 0; |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
183 x = GETB(c); |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
184 if (x > 17) { |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
185 copy(&c, x - 17); |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
186 x = GETB(c); |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
187 if (x < 16) c.error |= LZO_ERROR; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
188 } |
4442 | 189 if (c.in > c.in_end) |
190 c.error |= LZO_INPUT_DEPLETED; | |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
191 while (!c.error) { |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
192 int cnt, back; |
4441
fdb91110889b
replace if(x>>b) by if(x>C) as shifts are slow on some cpus and i have my doubts that gcc can replace the shifts as x is signed, it could in theory but well its gcc ...
michael
parents:
4440
diff
changeset
|
193 if (x > 15) { |
fdb91110889b
replace if(x>>b) by if(x>C) as shifts are slow on some cpus and i have my doubts that gcc can replace the shifts as x is signed, it could in theory but well its gcc ...
michael
parents:
4440
diff
changeset
|
194 if (x > 63) { |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
195 cnt = (x >> 5) - 1; |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
196 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1; |
4441
fdb91110889b
replace if(x>>b) by if(x>C) as shifts are slow on some cpus and i have my doubts that gcc can replace the shifts as x is signed, it could in theory but well its gcc ...
michael
parents:
4440
diff
changeset
|
197 } else if (x > 31) { |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
198 cnt = get_len(&c, x, 31); |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
199 x = GETB(c); |
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
200 back = (GETB(c) << 6) + (x >> 2) + 1; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
201 } else { |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
202 cnt = get_len(&c, x, 7); |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
203 back = (1 << 14) + ((x & 8) << 11); |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
204 x = GETB(c); |
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
205 back += (GETB(c) << 6) + (x >> 2); |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
206 if (back == (1 << 14)) { |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
207 if (cnt != 1) |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
208 c.error |= LZO_ERROR; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
209 break; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
210 } |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
211 } |
4443 | 212 } else if(!state){ |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
213 cnt = get_len(&c, x, 15); |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
214 copy(&c, cnt + 3); |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
215 x = GETB(c); |
4442 | 216 if (c.in > c.in_end) { |
217 c.error |= LZO_INPUT_DEPLETED; | |
218 continue; | |
219 } | |
4444 | 220 if (x > 15) |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
221 continue; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
222 cnt = 1; |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
223 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1; |
4443 | 224 } else { |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
225 cnt = 0; |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
226 back = (GETB(c) << 2) + (x >> 2) + 1; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
227 } |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
228 copy_backptr(&c, back, cnt + 2); |
4443 | 229 state= |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
230 cnt = x & 3; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
231 if (cnt) |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
232 copy(&c, cnt); |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
233 x = GETB(c); |
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
234 if (c.in > c.in_end) |
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
235 c.error |= LZO_INPUT_DEPLETED; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
236 } |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
237 *inlen = c.in_end - c.in; |
4426
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
238 if (c.in > c.in_end) |
65ef5fd314ad
LZO optimization: check input buffer bounds less frequently if padded
reimar
parents:
4419
diff
changeset
|
239 *inlen = 0; |
3034
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
240 *outlen = c.out_end - c.out; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
241 return c.error; |
d37065d8aeff
Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff
changeset
|
242 } |
4417 | 243 |
244 #ifdef TEST | |
245 #include <stdio.h> | |
246 #include <lzo/lzo1x.h> | |
247 #include "log.h" | |
248 #define MAXSZ (10*1024*1024) | |
249 int main(int argc, char *argv[]) { | |
250 FILE *in = fopen(argv[1], "rb"); | |
251 uint8_t *orig = av_malloc(MAXSZ + 16); | |
252 uint8_t *comp = av_malloc(2*MAXSZ + 16); | |
253 uint8_t *decomp = av_malloc(MAXSZ + 16); | |
254 size_t s = fread(orig, 1, MAXSZ, in); | |
255 lzo_uint clen = 0; | |
256 long tmp[LZO1X_MEM_COMPRESS]; | |
257 int inlen, outlen; | |
258 int i; | |
259 av_log_level = AV_LOG_DEBUG; | |
260 lzo1x_999_compress(orig, s, comp, &clen, tmp); | |
261 for (i = 0; i < 300; i++) { | |
262 START_TIMER | |
263 inlen = clen; outlen = MAXSZ; | |
4440
b80ad08b0f68
Add code to testcode to ease comparing with liblzo.
reimar
parents:
4426
diff
changeset
|
264 #ifdef LIBLZO |
b80ad08b0f68
Add code to testcode to ease comparing with liblzo.
reimar
parents:
4426
diff
changeset
|
265 if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL)) |
b80ad08b0f68
Add code to testcode to ease comparing with liblzo.
reimar
parents:
4426
diff
changeset
|
266 #elif defined(LIBLZO_UNSAFE) |
b80ad08b0f68
Add code to testcode to ease comparing with liblzo.
reimar
parents:
4426
diff
changeset
|
267 if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL)) |
b80ad08b0f68
Add code to testcode to ease comparing with liblzo.
reimar
parents:
4426
diff
changeset
|
268 #else |
4417 | 269 if (lzo1x_decode(decomp, &outlen, comp, &inlen)) |
4440
b80ad08b0f68
Add code to testcode to ease comparing with liblzo.
reimar
parents:
4426
diff
changeset
|
270 #endif |
4417 | 271 av_log(NULL, AV_LOG_ERROR, "decompression error\n"); |
272 STOP_TIMER("lzod") | |
273 } | |
274 if (memcmp(orig, decomp, s)) | |
275 av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n"); | |
276 else | |
277 av_log(NULL, AV_LOG_ERROR, "decompression ok\n"); | |
278 return 0; | |
279 } | |
280 #endif |