source: trunk/src/data_sources/scream2ew/gcf.c @ 2148

Revision 2148, 5.2 KB checked in by paulf, 14 years ago (diff)

MKIII DM24 patch

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * gcf.c:
3 *
4 * Copyright (c) 2003 Guralp Systems Limited
5 * Author James McKenzie, contact <software@guralp.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23static char rcsid[] = "$Id$";
24
25/*
26 * $Log$
27 * Revision 1.2  2006/04/28 20:24:31  paulf
28 * MKIII DM24 patch
29 *
30 * Revision 1.1  2003/03/27 18:07:18  alex
31 * Initial revision
32 *
33 * Revision 1.4  2003/02/28 17:05:37  root
34 * #
35 *
36 */
37
38#include "project.h"
39
40static time_t
41gcf_time_to_time_t (gcf_time * t)
42{
43  time_t ret = GCF_EPOCH;
44
45  ret += (t->day) * 24 * 60 * 60;
46  ret += (t->sec);
47
48  return ret;
49}
50
51static void
52extract_8 (gcf_block b)
53{
54  uint8_t *ptr = b->buf;
55  int *optr = b->data;
56
57  int val;
58  int n = b->samples;
59
60  ptr += 16;
61
62  val = gp_int32 (ptr);
63  ptr += 4;
64
65  if (*ptr)
66    fatal (("First difference is not zero"));
67  while (n--)
68    {
69      val += gp_int8 (ptr++);
70      *(optr++) = val;
71    }
72  b->fic = val;
73  b->ric = gp_int32 (ptr);
74
75  if (b->fic != b->ric)
76    fatal (("Fic!=Ric"));
77
78}
79
80static void
81extract_16 (gcf_block b)
82{
83  uint8_t *ptr = b->buf;
84  int *optr = b->data;
85
86  int val;
87  int n = b->samples;
88
89  ptr += 16;
90
91  val = gp_int32 (ptr);
92  ptr += 4;
93
94  if (*ptr)
95    fatal (("First difference is not zero"));
96
97  while (n--)
98    {
99      val += gp_int16 (ptr);
100      ptr += 2;
101      *(optr++) = val;
102    }
103  b->fic = val;
104  b->ric = gp_int32 (ptr);
105
106  if (b->fic != b->ric)
107    fatal (("Fic!=Ric"));
108
109
110}
111
112void
113extract_24 (gcf_block b)
114{
115  uint8_t *ptr = b->buf;
116  int *optr = b->data;
117
118  int val;
119  int n = b->samples;
120
121  ptr += 16;
122
123  if ((*ptr) && (0xff != *ptr))
124    fatal (("claimed 24 bit data isn't"));
125
126  val = gp_int32 (ptr);
127  ptr += 4;
128
129  if (gp_int24 (ptr))
130    fatal (("First difference is not zero"));
131
132  while (n--)
133    {
134      val += gp_uint24 (ptr);
135      ptr += 3;
136      while (val >= 0x800000L)
137        val -= 0x1000000L;
138      *(optr++) = val;
139    }
140  b->fic = val;
141  if ((*ptr) && (0xff != *ptr))
142    fatal (("claimed 24 bit data isn't"));
143
144  b->ric = gp_int32 (ptr);
145
146
147  if (b->fic != b->ric)
148    fatal (("Fic!=Ric"));
149
150}
151
152#if 0
153void
154extract_32 (gcf_block b)
155{
156  uint8_t *ptr = b->buf;
157  int *optr = b->data;
158
159  int64_t val;
160  int n = b->samples;
161
162  ptr += 16;
163
164  val = (int64_t) gp_int32 (ptr);
165  ptr += 4;
166
167  if (*ptr)
168    fatal (("First difference is not zero"));
169
170  while (n--)
171    {
172      val += (int64_t) gp_int32 (ptr);
173      ptr += 4;
174      *(optr++) = (int) val;
175    }
176
177  b->fic = (int) val;
178  b->ric = gp_int32 (ptr);
179
180  if (b->fic != b->ric)
181    fatal (("Fic!=Ric"));
182}
183#else
184
185
186void
187extract_32 (gcf_block b)
188{
189  uint8_t *ptr = b->buf;
190  int *optr = b->data;
191
192  uint32_t uval;
193  int32_t val;
194
195  int n = b->samples;
196
197  ptr += 16;
198
199  uval = gp_uint32 (ptr);
200  ptr += 4;
201
202  if (*ptr)
203    fatal (("First difference is not zero"));
204
205  while (n--)
206    {
207      uval += gp_uint32 (ptr);
208      ptr += 4;
209
210      if (uval == 0x80000000UL)
211        {
212          val = -0x80000000L;
213        }
214      else if (uval & 0x80000000UL)
215        {
216          val = -1 - (int32_t) (0xffffffffUL - uval);
217        }
218      else
219        {
220          val = (int32_t) uval;
221        }
222
223      *(optr++) = (int) val;
224    }
225
226  b->fic = (int) val;
227  b->ric = gp_int32 (ptr);
228
229  if (b->fic != b->ric)
230    fatal (("Fic!=Ric"));
231}
232
233
234#endif
235
236
237void 
238gcf_dispatch (uint8_t * buf, int sz)
239{
240  int i;
241  struct gcf_block_struct block;
242
243  block.buf = buf;
244  block.size = sz;
245  block.csize = 0;
246
247  strcpy (block.sysid, gp_base36_to_a (gp_uint32 (buf)));
248  strcpy (block.strid, gp_base36_to_a (gp_uint32 (buf + 4)));
249
250  i = gp_uint16 (buf + 8);
251  block.start.day = i >> 1;
252  i &= 1;
253  i <<= 16;
254  block.start.sec = i | gp_uint16 (buf + 10);
255
256  block.estart = gcf_time_to_time_t (&block.start);
257
258  block.sample_rate = buf[13];
259  block.format = buf[14] & 7;
260  block.records = buf[15];
261  block.samples = (block.format) * (block.records);
262
263  block.text = buf + 16;
264
265  if (block.sample_rate)
266    {
267      switch (block.format)
268        {
269        case 4:
270          block.csize = 24 + block.samples;
271          extract_8 (&block);
272          break;
273        case 2:
274          block.csize = 24 + (2 * block.samples);
275          extract_16 (&block);
276          break;
277        case 1:
278          if ((sz - 24) == (4 * block.samples))
279            {
280              block.csize = 24 + (4 * block.samples);
281              extract_32 (&block);
282            }
283          else if ((sz - 24) == (3 * block.samples))
284            {
285              block.csize = 24 + (3 * block.samples);
286              extract_24 (&block);
287            }
288          else
289            {
290              /* Guess 32 */
291              extract_32 (&block);
292            }
293          break;
294        default:
295          fatal (("unknown GCF compression format"));
296        }
297    }
298
299  dispatch (&block);
300}
Note: See TracBrowser for help on using the repository browser.