matroskademux: configurable timestamp gap handling
[platform/upstream/gstreamer.git] / gst / matroska / lzo.c
1 /*
2  * LZO 1x decompression
3  * Copyright (c) 2006 Reimar Doeffinger
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <gst/gst.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "_stdint.h"
26 #include "lzo.h"
27
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
33 {
34   const uint8_t *in, *in_end;
35   uint8_t *out_start, *out, *out_end;
36   int error;
37 } LZOContext;
38
39 /*
40  * \brief read one byte from input buffer, avoiding overrun
41  * \return byte read
42  */
43 static inline int
44 get_byte (LZOContext * c)
45 {
46   if (c->in < c->in_end)
47     return *c->in++;
48   c->error |= LZO_INPUT_DEPLETED;
49   return 1;
50 }
51
52 #ifdef INBUF_PADDED
53 #define GETB(c) (*(c).in++)
54 #else
55 #define GETB(c) get_byte(&(c))
56 #endif
57
58 /*
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
63  */
64 static inline int
65 get_len (LZOContext * c, int x, int mask)
66 {
67   int cnt = x & mask;
68   if (!cnt) {
69     while (!(x = get_byte (c)))
70       cnt += 255;
71     cnt += mask + x;
72   }
73   return cnt;
74 }
75
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);
84 #else
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];
87 #endif
88
89 /*
90  * \brief copy bytes from input to output buffer with checking
91  * \param cnt number of bytes to copy, must be >= 0
92  */
93 static inline void
94 copy (LZOContext * c, int cnt)
95 {
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;
101   }
102   if (cnt > c->out_end - dst) {
103     cnt = MAX (c->out_end - dst, 0);
104     c->error |= LZO_OUTPUT_FULL;
105   }
106 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
107   COPY4 (dst, src);
108   src += 4;
109   dst += 4;
110   cnt -= 4;
111   if (cnt > 0)
112 #endif
113     memcpy (dst, src, cnt);
114   c->in = src + cnt;
115   c->out = dst + cnt;
116 }
117
118 /*
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
122  *
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.
125  */
126 static inline void
127 copy_backptr (LZOContext * c, int back, int cnt)
128 {
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;
133     return;
134   }
135   if (cnt > c->out_end - dst) {
136     cnt = MAX (c->out_end - dst, 0);
137     c->error |= LZO_OUTPUT_FULL;
138   }
139   if (back == 1) {
140     memset (dst, *src, cnt);
141     dst += cnt;
142   } else {
143 #ifdef OUTBUF_PADDED
144     COPY2 (dst, src);
145     COPY2 (dst + 2, src + 2);
146     src += 4;
147     dst += 4;
148     cnt -= 4;
149     if (cnt > 0) {
150       COPY2 (dst, src);
151       COPY2 (dst + 2, src + 2);
152       COPY2 (dst + 4, src + 4);
153       COPY2 (dst + 6, src + 6);
154       src += 8;
155       dst += 8;
156       cnt -= 8;
157     }
158 #endif
159     if (cnt > 0) {
160       int blocklen = back;
161       while (cnt > blocklen) {
162         memcpy (dst, src, blocklen);
163         dst += blocklen;
164         cnt -= blocklen;
165         blocklen <<= 1;
166       }
167       memcpy (dst, src, cnt);
168     }
169     dst += cnt;
170   }
171   c->out = dst;
172 }
173
174 /*
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
181  *
182  * make sure all buffers are appropriately padded, in must provide
183  * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
184  */
185 int
186 lzo1x_decode (void *out, int *outlen, const void *in, int *inlen)
187 {
188   int state = 0;
189   int x;
190   LZOContext c;
191   c.in = in;
192   c.in_end = (const uint8_t *) in + *inlen;
193   c.out = c.out_start = out;
194   c.out_end = (uint8_t *) out + *outlen;
195   c.error = 0;
196   x = GETB (c);
197   if (x > 17) {
198     copy (&c, x - 17);
199     x = GETB (c);
200     if (x < 16)
201       c.error |= LZO_ERROR;
202   }
203   if (c.in > c.in_end)
204     c.error |= LZO_INPUT_DEPLETED;
205   while (!c.error) {
206     int cnt, back;
207     if (x > 15) {
208       if (x > 63) {
209         cnt = (x >> 5) - 1;
210         back = (GETB (c) << 3) + ((x >> 2) & 7) + 1;
211       } else if (x > 31) {
212         cnt = get_len (&c, x, 31);
213         x = GETB (c);
214         back = (GETB (c) << 6) + (x >> 2) + 1;
215       } else {
216         cnt = get_len (&c, x, 7);
217         back = (1 << 14) + ((x & 8) << 11);
218         x = GETB (c);
219         back += (GETB (c) << 6) + (x >> 2);
220         if (back == (1 << 14)) {
221           if (cnt != 1)
222             c.error |= LZO_ERROR;
223           break;
224         }
225       }
226     } else if (!state) {
227       cnt = get_len (&c, x, 15);
228       copy (&c, cnt + 3);
229       x = GETB (c);
230       if (x > 15)
231         continue;
232       cnt = 1;
233       back = (1 << 11) + (GETB (c) << 2) + (x >> 2) + 1;
234     } else {
235       cnt = 0;
236       back = (GETB (c) << 2) + (x >> 2) + 1;
237     }
238     copy_backptr (&c, back, cnt + 2);
239     state = cnt = x & 3;
240     copy (&c, cnt);
241     x = GETB (c);
242   }
243   *inlen = c.in_end - c.in;
244   if (c.in > c.in_end)
245     *inlen = 0;
246   *outlen = c.out_end - c.out;
247   return c.error;
248 }
249
250 #ifdef TEST
251 #include <stdio.h>
252 #include <lzo/lzo1x.h>
253 #include "log.h"
254 #define MAXSZ (10*1024*1024)
255 int
256 main (int argc, char *argv[])
257 {
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);
263   lzo_uint clen = 0;
264   long tmp[LZO1X_MEM_COMPRESS];
265   int inlen, outlen;
266   int i;
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;
271     outlen = MAXSZ;
272 #ifdef LIBLZO
273     if (lzo1x_decompress_safe (comp, inlen, decomp, &outlen, NULL))
274 #elif defined(LIBLZO_UNSAFE)
275     if (lzo1x_decompress (comp, inlen, decomp, &outlen, NULL))
276 #else
277     if (lzo1x_decode (decomp, &outlen, comp, &inlen))
278 #endif
279       av_log (NULL, AV_LOG_ERROR, "decompression error\n");
280     STOP_TIMER ("lzod")
281   }
282   if (memcmp (orig, decomp, s))
283     av_log (NULL, AV_LOG_ERROR, "decompression incorrect\n");
284   else
285     av_log (NULL, AV_LOG_ERROR, "decompression ok\n");
286   return 0;
287 }
288 #endif