~ubuntu-branches/ubuntu/raring/mame/raring-proposed

« back to all changes in this revision

Viewing changes to mess/src/lib/formats/csw_cas.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach, Jordi Mallach, Emmanuel Kasper
  • Date: 2011-12-19 22:56:27 UTC
  • mfrom: (0.1.2)
  • Revision ID: package-import@ubuntu.com-20111219225627-ub5oga1oys4ogqzm
Tags: 0.144-1
[ Jordi Mallach ]
* Fix syntax errors in DEP5 copyright file (lintian).
* Use a versioned copyright Format specification field.
* Update Vcs-* URLs.
* Move transitional packages to the new metapackages section, and make
  them priority extra.
* Remove references to GNU/Linux and MESS sources from copyright.
* Add build variables for s390x.
* Use .xz tarballs as it cuts 4MB for the upstream sources.
* Add nplayers.ini as a patch. Update copyright file to add CC-BY-SA-3.0.

[ Emmanuel Kasper ]
* New upstream release. Closes: #651538.
* Add Free Desktop compliant png icons of various sizes taken from
  the hydroxygen iconset
* Mess is now built from a new source package, to avoid possible source
  incompatibilities between mame and the mess overlay.
* Mame-tools are not built from the mame source package anymore, but
  from the mess source package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
CSW format
3
 
----------
4
 
Header Description
5
 
 
6
 
Offset  Value   Type    Description
7
 
0x00    (note)  ASCII   22 bytes "Compressed Square Wave"  signature
8
 
0x16    0x1A    BYTE    Terminator code
9
 
0x17    0x02    BYTE    CSW major revision number
10
 
0x18    0x00    BYTE    CSW minor revision number
11
 
0x19            DWORD   Sample rate
12
 
0x1D            DWORD   Total number of pulses (after decompression)
13
 
0x21            BYTE    Compression type  0x01: RLE    0x02: Z-RLE
14
 
0x22            BYTE    Flags   b0: initial polarity: if set, the signal starts at logical high
15
 
0x23    HDR     BYTE    Header extension length in bytes (0x00)
16
 
0x24            ASCII   16 bytes free use
17
 
0x34            BYTE    Start of Header is HDR>0
18
 
 
19
 
 
20
 
 
21
 
*/
22
 
 
23
 
#include <string.h>
24
 
 
25
 
#include "zlib.h"
26
 
#include "uef_cas.h"
27
 
#include "csw_cas.h"
28
 
 
29
 
 
30
 
#define CSW_WAV_FREQUENCY       44100
31
 
 
32
 
static const UINT8 CSW_HEADER[] = { "Compressed Square Wave" };
33
 
 
34
 
static UINT32 get_leuint32(const void *ptr)
35
 
{
36
 
        UINT32 value;
37
 
        memcpy(&value, ptr, sizeof(value));
38
 
        return LITTLE_ENDIANIZE_INT32(value);
39
 
}
40
 
 
41
 
static int mycaslen;
42
 
 
43
 
static int csw_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
44
 
UINT32 SampleRate;
45
 
UINT32 NumberOfPulses;
46
 
UINT8  MajorRevision;
47
 
UINT8  MinorRevision;
48
 
UINT8  CompressionType;
49
 
UINT8  Flags;
50
 
UINT8  HeaderExtensionLength;
51
 
UINT8 *gz_ptr = NULL;
52
 
 
53
 
int                     total_size;
54
 
z_stream        d_stream;
55
 
int             err;
56
 
UINT8           *in_ptr;
57
 
int             bsize=0;
58
 
 
59
 
        if ( memcmp( casdata, CSW_HEADER, sizeof(CSW_HEADER) ) ) {
60
 
                LOG_FORMATS( "csw_cas_to_wav_size: cassette image has incompatible header\n" );
61
 
                goto cleanup;
62
 
        }
63
 
 
64
 
        if (casdata[0x16]!=0x1a) {
65
 
                LOG_FORMATS( "csw_cas_to_wav_size: Terminator Code Not Found\n" );
66
 
                goto cleanup;
67
 
        }
68
 
 
69
 
        MajorRevision=casdata[0x17];
70
 
        MinorRevision=casdata[0x18];
71
 
 
72
 
        LOG_FORMATS("Version %d : %d\n",MajorRevision,MinorRevision);
73
 
 
74
 
        if (casdata[0x17]!=2){
75
 
                LOG_FORMATS( "csw_cas_to_wav_size: Unsuported Major Version\n" );
76
 
                goto cleanup;
77
 
        }
78
 
 
79
 
        SampleRate=get_leuint32(casdata+0x19);
80
 
        LOG_FORMATS("Sample rate %d\n",SampleRate);
81
 
 
82
 
        NumberOfPulses=get_leuint32(casdata+0x1d);
83
 
        LOG_FORMATS("Number Of Pulses %d\n",NumberOfPulses);
84
 
 
85
 
 
86
 
        CompressionType=casdata[0x21];
87
 
        Flags=casdata[0x22];
88
 
        HeaderExtensionLength=casdata[0x23];
89
 
 
90
 
        LOG_FORMATS("CompressionType %d   Flast %d   HeaderExtensionLength %d\n",CompressionType,Flags,HeaderExtensionLength);
91
 
 
92
 
        mycaslen=caslen;
93
 
        //from here on down for now I am assuming it is compressed csw file.
94
 
        in_ptr = (UINT8*) casdata+0x34+HeaderExtensionLength;
95
 
 
96
 
        gz_ptr = (UINT8*)malloc( 8 );
97
 
 
98
 
 
99
 
        d_stream.next_in = (unsigned char *)in_ptr;
100
 
        d_stream.avail_in = caslen - ( in_ptr - casdata );
101
 
        d_stream.total_in=0;
102
 
 
103
 
        d_stream.next_out = gz_ptr;
104
 
        d_stream.avail_out = 1;
105
 
        d_stream.total_out=0;
106
 
 
107
 
        d_stream.zalloc = 0;
108
 
        d_stream.zfree = 0;
109
 
        d_stream.opaque = 0;
110
 
        d_stream.data_type=0;
111
 
 
112
 
        err = inflateInit( &d_stream );
113
 
        if ( err != Z_OK ) {
114
 
                LOG_FORMATS( "inflateInit2 error: %d\n", err );
115
 
                goto cleanup;
116
 
        }
117
 
 
118
 
 
119
 
        total_size=1;
120
 
        do
121
 
        {
122
 
                d_stream.next_out = gz_ptr;
123
 
                d_stream.avail_out=1;
124
 
                err=inflate( &d_stream, Z_SYNC_FLUSH );
125
 
                if (err==Z_OK) {
126
 
                        bsize=gz_ptr[0];
127
 
                        if (bsize==0) {
128
 
                                d_stream.avail_out=4;
129
 
                                d_stream.next_out = gz_ptr;
130
 
                                err=inflate( &d_stream, Z_SYNC_FLUSH );
131
 
                                bsize=get_leuint32(gz_ptr);
132
 
                        }
133
 
                        total_size=total_size+bsize;
134
 
                }
135
 
        }
136
 
        while (err==Z_OK);
137
 
 
138
 
        if ( err != Z_STREAM_END ) {
139
 
                LOG_FORMATS( "inflate error: %d\n", err );
140
 
                goto cleanup;
141
 
        }
142
 
 
143
 
        err = inflateEnd( &d_stream );
144
 
        if ( err != Z_OK ) {
145
 
                LOG_FORMATS( "inflateEnd error: %d\n", err );
146
 
                goto cleanup;
147
 
        }
148
 
 
149
 
 
150
 
 
151
 
 
152
 
 
153
 
 
154
 
        if ( gz_ptr ) {
155
 
                free( gz_ptr );
156
 
                gz_ptr = NULL;
157
 
        }
158
 
 
159
 
 
160
 
 
161
 
        return total_size;
162
 
 
163
 
cleanup:
164
 
        if ( gz_ptr ) {
165
 
                free( gz_ptr );
166
 
                gz_ptr = NULL;
167
 
        }
168
 
        return -1;
169
 
 
170
 
}
171
 
 
172
 
static int csw_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
173
 
UINT32 SampleRate;
174
 
UINT32 NumberOfPulses;
175
 
UINT8  CompressionType;
176
 
UINT8  Flags;
177
 
UINT8  HeaderExtensionLength;
178
 
INT8   Bit;
179
 
 
180
 
UINT8 *gz_ptr = NULL;
181
 
int                     total_size;
182
 
z_stream        d_stream;
183
 
int             err;
184
 
UINT8           *in_ptr;
185
 
int             bsize=0;
186
 
int             i;
187
 
 
188
 
 
189
 
        LOG_FORMATS("Length %d\n",length);
190
 
 
191
 
        SampleRate=get_leuint32(bytes+0x19);
192
 
        LOG_FORMATS("Sample rate %d\n",SampleRate);
193
 
 
194
 
        NumberOfPulses=get_leuint32(bytes+0x1d);
195
 
        LOG_FORMATS("Number Of Pulses %d\n",NumberOfPulses);
196
 
 
197
 
        CompressionType=bytes[0x21];
198
 
        Flags=bytes[0x22];
199
 
        HeaderExtensionLength=bytes[0x23];
200
 
 
201
 
        if ((Flags&0)==0)
202
 
        {
203
 
                Bit=-100;
204
 
        } else {
205
 
                Bit=100;
206
 
        }
207
 
 
208
 
        LOG_FORMATS("CompressionType %d   Flast %d   HeaderExtensionLength %d\n",CompressionType,Flags,HeaderExtensionLength);
209
 
 
210
 
 
211
 
        //from here on down for now I am assuming it is compressed csw file.
212
 
        in_ptr = (UINT8*) bytes+0x34+HeaderExtensionLength;
213
 
 
214
 
        gz_ptr = (UINT8*)malloc( 8 );
215
 
 
216
 
 
217
 
        d_stream.next_in = (unsigned char *)in_ptr;
218
 
        d_stream.avail_in = mycaslen - ( in_ptr - bytes );
219
 
        d_stream.total_in=0;
220
 
 
221
 
        d_stream.next_out = gz_ptr;
222
 
        d_stream.avail_out = 1;
223
 
        d_stream.total_out=0;
224
 
 
225
 
        d_stream.zalloc = 0;
226
 
        d_stream.zfree = 0;
227
 
        d_stream.opaque = 0;
228
 
        d_stream.data_type=0;
229
 
 
230
 
        err = inflateInit( &d_stream );
231
 
        if ( err != Z_OK ) {
232
 
                LOG_FORMATS( "inflateInit2 error: %d\n", err );
233
 
                goto cleanup;
234
 
        }
235
 
 
236
 
        total_size=0;
237
 
 
238
 
        do
239
 
        {
240
 
                d_stream.next_out = gz_ptr;
241
 
                d_stream.avail_out=1;
242
 
                err=inflate( &d_stream, Z_SYNC_FLUSH );
243
 
                if (err==Z_OK) {
244
 
                        bsize=gz_ptr[0];
245
 
                        if (bsize==0) {
246
 
                                d_stream.avail_out=4;
247
 
                                d_stream.next_out = gz_ptr;
248
 
                                err=inflate( &d_stream, Z_SYNC_FLUSH );
249
 
                                bsize=get_leuint32(gz_ptr);
250
 
                        }
251
 
                        for (i=0;i<bsize;i++)
252
 
                        {
253
 
                                buffer[total_size++]=Bit;
254
 
                        }
255
 
                        Bit=-Bit;
256
 
                }
257
 
        }
258
 
        while (err==Z_OK);
259
 
 
260
 
        if ( err != Z_STREAM_END ) {
261
 
                LOG_FORMATS( "inflate error: %d\n", err );
262
 
                goto cleanup;
263
 
        }
264
 
 
265
 
        err = inflateEnd( &d_stream );
266
 
        if ( err != Z_OK ) {
267
 
                LOG_FORMATS( "inflateEnd error: %d\n", err );
268
 
                goto cleanup;
269
 
        }
270
 
 
271
 
 
272
 
 
273
 
 
274
 
 
275
 
 
276
 
        if ( gz_ptr ) {
277
 
                free( gz_ptr );
278
 
                gz_ptr = NULL;
279
 
        }
280
 
 
281
 
 
282
 
 
283
 
 
284
 
        return length;
285
 
 
286
 
 
287
 
 
288
 
        cleanup:
289
 
                if ( gz_ptr ) {
290
 
                        free( gz_ptr );
291
 
                        gz_ptr = NULL;
292
 
                }
293
 
        return -1;
294
 
}
295
 
 
296
 
 
297
 
static const struct CassetteLegacyWaveFiller csw_legacy_fill_wave = {
298
 
        csw_cas_fill_wave,              /* fill_wave */
299
 
        -1,                                             /* chunk_size */
300
 
        0,                                              /* chunk_samples */
301
 
        csw_cas_to_wav_size,    /* chunk_sample_calc */
302
 
        CSW_WAV_FREQUENCY,              /* sample_frequency */
303
 
        0,                                              /* header_samples */
304
 
        0                                               /* trailer_samples */
305
 
};
306
 
 
307
 
static casserr_t csw_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
308
 
        return cassette_legacy_identify( cassette, opts, &csw_legacy_fill_wave );
309
 
}
310
 
 
311
 
static casserr_t csw_cassette_load( cassette_image *cassette ) {
312
 
        return cassette_legacy_construct( cassette, &csw_legacy_fill_wave );
313
 
}
314
 
 
315
 
static const struct CassetteFormat csw_cassette_format = {
316
 
        "csw",
317
 
        csw_cassette_identify,
318
 
        csw_cassette_load,
319
 
        NULL
320
 
};
321
 
 
322
 
CASSETTE_FORMATLIST_START(csw_cassette_formats)
323
 
        CASSETTE_FORMAT(csw_cassette_format)
324
 
CASSETTE_FORMATLIST_END
325
 
 
326
 
CASSETTE_FORMATLIST_START(bbc_cassette_formats)
327
 
        CASSETTE_FORMAT(csw_cassette_format)
328
 
        CASSETTE_FORMAT(uef_cassette_format)
329
 
CASSETTE_FORMATLIST_END