File: xa.c

package info (click to toggle)
sox 14.4.2%2Bgit20190427-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,720 kB
  • sloc: ansic: 43,223; sh: 11,692; makefile: 333
file content (297 lines) | stat: -rw-r--r-- 10,794 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* libSoX xa.c  Support for Maxis .XA file format
 *
 *      Copyright (C) 2006 Dwayne C. Litzenberger <dlitz@dlitz.net>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/* Thanks to Valery V. Anisimovsky <samael@avn.mccme.ru> for the
 * "Maxis XA Audio File Format Description", dated 5-01-2002. */

#include "sox_i.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define HNIBBLE(byte) (((byte) >> 4) & 0xf)
#define LNIBBLE(byte) ((byte) & 0xf)

/* .xa file header */
typedef struct {
    char magic[4];  /* "XA\0\0", "XAI\0" (sound/speech), or "XAJ\0" (music) */
    uint32_t outSize;       /* decompressed size of the stream (in bytes) */

    /* WAVEFORMATEX structure for the decompressed data */
    uint16_t tag;           /* 0x0001 - PCM data */
    uint16_t channels;      /* number of channels */
    uint32_t sampleRate;    /* sample rate (samples/sec) */
    uint32_t avgByteRate;   /* sampleRate * align */
    uint16_t align;         /* bits / 8 * channels */
    uint16_t bits;          /* 8 or 16 */
} xa_header_t;

typedef struct {
    int32_t curSample;      /* current sample */
    int32_t prevSample;     /* previous sample */
    int32_t c1;
    int32_t c2;
    unsigned int shift;
} xa_state_t;

/* Private data for .xa file */
typedef struct {
    xa_header_t header;
    xa_state_t *state;
    unsigned int blockSize;
    unsigned int bufPos;    /* position within the current block */
    unsigned char *buf;     /* buffer for the current block */
    unsigned int bytesDecoded;  /* number of decompressed bytes read */
} priv_t;

/* coefficients for EA ADPCM */
static const int32_t EA_ADPCM_Table[]= {
    0, 240,  460,  392,
    0,   0, -208, -220,
    0,   1,    3,    4,
    7,   8,   10,   11,
    0,  -1,   -3,   -4
};

/* Clip sample to 16 bits */
static inline int32_t clip16(int32_t sample)
{
    if (sample > 32767) {
        return 32767;
    } else if (sample < -32768) {
        return -32768;
    } else {
        return sample;
    }
}

static int startread(sox_format_t * ft)
{
    priv_t * xa = (priv_t *) ft->priv;
    char *magic = xa->header.magic;

    /* Check for the magic value */
    if (lsx_readbuf(ft, xa->header.magic, (size_t)4) != 4 ||
        (memcmp("XA\0\0", xa->header.magic, (size_t)4) != 0 &&
         memcmp("XAI\0", xa->header.magic, (size_t)4) != 0 &&
         memcmp("XAJ\0", xa->header.magic, (size_t)4) != 0))
    {
        lsx_fail_errno(ft, SOX_EHDR, "XA: Header not found");
        return SOX_EOF;
    }

    /* Read the rest of the header */
    if (lsx_readdw(ft, &xa->header.outSize) != SOX_SUCCESS) return SOX_EOF;
    if (lsx_readw(ft, &xa->header.tag) != SOX_SUCCESS) return SOX_EOF;
    if (lsx_readw(ft, &xa->header.channels) != SOX_SUCCESS) return SOX_EOF;
    if (lsx_readdw(ft, &xa->header.sampleRate) != SOX_SUCCESS) return SOX_EOF;
    if (lsx_readdw(ft, &xa->header.avgByteRate) != SOX_SUCCESS) return SOX_EOF;
    if (lsx_readw(ft, &xa->header.align) != SOX_SUCCESS) return SOX_EOF;
    if (lsx_readw(ft, &xa->header.bits) != SOX_SUCCESS) return SOX_EOF;

    /* Output the data from the header */
    lsx_debug("XA Header:");
    lsx_debug(" szID:          %02x %02x %02x %02x  |%c%c%c%c|",
        magic[0], magic[1], magic[2], magic[3],
        (magic[0] >= 0x20 && magic[0] <= 0x7e) ? magic[0] : '.',
        (magic[1] >= 0x20 && magic[1] <= 0x7e) ? magic[1] : '.',
        (magic[2] >= 0x20 && magic[2] <= 0x7e) ? magic[2] : '.',
        (magic[3] >= 0x20 && magic[3] <= 0x7e) ? magic[3] : '.');
    lsx_debug(" dwOutSize:     %u", xa->header.outSize);
    lsx_debug(" wTag:          0x%04x", xa->header.tag);
    lsx_debug(" wChannels:     %u", xa->header.channels);
    lsx_debug(" dwSampleRate:  %u", xa->header.sampleRate);
    lsx_debug(" dwAvgByteRate: %u", xa->header.avgByteRate);
    lsx_debug(" wAlign:        %u", xa->header.align);
    lsx_debug(" wBits:         %u", xa->header.bits);

    /* Populate the sox_soundstream structure */
    ft->encoding.encoding = SOX_ENCODING_SIGN2;

    if (!ft->encoding.bits_per_sample || ft->encoding.bits_per_sample == xa->header.bits) {
        ft->encoding.bits_per_sample = xa->header.bits;
    } else {
        lsx_report("User options overriding size read in .xa header");
    }

    if (ft->signal.channels == 0 || ft->signal.channels == xa->header.channels) {
        ft->signal.channels = xa->header.channels;
    } else {
        lsx_report("User options overriding channels read in .xa header");
    }

    if (ft->signal.rate == 0 || ft->signal.rate == xa->header.sampleRate) {
        ft->signal.rate = xa->header.sampleRate;
    } else {
        lsx_report("User options overriding rate read in .xa header");
    }

    if (ft->signal.channels == 0 || ft->signal.channels > UINT16_MAX) {
        lsx_fail_errno(ft, SOX_EFMT, "invalid channel count %d",
                       ft->signal.channels);
        return SOX_EOF;
    }

    /* Check for supported formats */
    if (ft->encoding.bits_per_sample != 16) {
        lsx_fail_errno(ft, SOX_EFMT, "%d-bit sample resolution not supported.",
            ft->encoding.bits_per_sample);
        return SOX_EOF;
    }

    /* Validate the header */
    if (xa->header.bits != ft->encoding.bits_per_sample) {
        lsx_report("Invalid sample resolution %d bits.  Assuming %d bits.",
            xa->header.bits, ft->encoding.bits_per_sample);
        xa->header.bits = ft->encoding.bits_per_sample;
    }
    if (xa->header.align != (ft->encoding.bits_per_sample >> 3) * xa->header.channels) {
        lsx_report("Invalid sample alignment value %d.  Assuming %d.",
            xa->header.align, (ft->encoding.bits_per_sample >> 3) * xa->header.channels);
        xa->header.align = (ft->encoding.bits_per_sample >> 3) * xa->header.channels;
    }
    if (xa->header.avgByteRate != (xa->header.align * xa->header.sampleRate)) {
        lsx_report("Invalid dwAvgByteRate value %d.  Assuming %d.",
            xa->header.avgByteRate, xa->header.align * xa->header.sampleRate);
        xa->header.avgByteRate = xa->header.align * xa->header.sampleRate;
    }

    /* Set up the block buffer */
    xa->blockSize = ft->signal.channels * 0xf;
    xa->bufPos = xa->blockSize;

    /* Allocate memory for the block buffer */
    xa->buf = lsx_calloc(1, (size_t)xa->blockSize);

    /* Allocate memory for the state */
    xa->state = lsx_calloc(sizeof(xa_state_t), ft->signal.channels);

    /* Final initialization */
    xa->bytesDecoded = 0;

    return SOX_SUCCESS;
}

/*
 * Read up to len samples from a file, converted to signed longs.
 * Return the number of samples read.
 */
static size_t read_samples(sox_format_t * ft, sox_sample_t *buf, size_t len)
{
    priv_t * xa = (priv_t *) ft->priv;
    int32_t sample;
    unsigned char inByte;
    size_t i, done, bytes;

    ft->sox_errno = SOX_SUCCESS;
    done = 0;
    while (done < len) {
        if (xa->bufPos >= xa->blockSize) {
            /* Read the next block */
            bytes = lsx_readbuf(ft, xa->buf, (size_t) xa->blockSize);
            if (bytes < xa->blockSize) {
                if (lsx_eof(ft)) {
                    if (done > 0) {
                        return done;
                    }
                    lsx_fail_errno(ft,SOX_EOF,"Premature EOF on .xa input file");
                    return 0;
                } else {
                    /* error */
                    lsx_fail_errno(ft,SOX_EOF,"read error on input stream");
                    return 0;
                }
            }
            xa->bufPos = 0;

            for (i = 0; i < ft->signal.channels; i++) {
                inByte = xa->buf[i];
                xa->state[i].c1 = EA_ADPCM_Table[HNIBBLE(inByte)];
                xa->state[i].c2 = EA_ADPCM_Table[HNIBBLE(inByte) + 4];
                xa->state[i].shift = LNIBBLE(inByte) + 8;
            }
            xa->bufPos += ft->signal.channels;
        } else {
            /* Process the block */
            for (i = 0; i < ft->signal.channels && done < len; i++) {
                /* high nibble */
                sample = HNIBBLE(xa->buf[xa->bufPos+i]);
                sample = (sample << 28) >> xa->state[i].shift;
                sample = (sample +
                          xa->state[i].curSample * xa->state[i].c1 +
                          xa->state[i].prevSample * xa->state[i].c2 + 0x80) >> 8;
                sample = clip16(sample);
                xa->state[i].prevSample = xa->state[i].curSample;
                xa->state[i].curSample = sample;

                buf[done++] = SOX_SIGNED_16BIT_TO_SAMPLE(sample,);
                xa->bytesDecoded += (ft->encoding.bits_per_sample >> 3);
            }
            for (i = 0; i < ft->signal.channels && done < len; i++) {
                /* low nibble */
                sample = LNIBBLE(xa->buf[xa->bufPos+i]);
                sample = (sample << 28) >> xa->state[i].shift;
                sample = (sample +
                          xa->state[i].curSample * xa->state[i].c1 +
                          xa->state[i].prevSample * xa->state[i].c2 + 0x80) >> 8;
                sample = clip16(sample);
                xa->state[i].prevSample = xa->state[i].curSample;
                xa->state[i].curSample = sample;

                buf[done++] = SOX_SIGNED_16BIT_TO_SAMPLE(sample,);
                xa->bytesDecoded += (ft->encoding.bits_per_sample >> 3);
            }

            xa->bufPos += ft->signal.channels;
        }
    }
    if (done == 0) {
        return 0;
    }
    return done;
}

static int stopread(sox_format_t * ft)
{
    priv_t * xa = (priv_t *) ft->priv;

    ft->sox_errno = SOX_SUCCESS;

    /* Free memory */
    free(xa->buf);
    xa->buf = NULL;
    free(xa->state);
    xa->state = NULL;

    return SOX_SUCCESS;
}

LSX_FORMAT_HANDLER(xa)
{
  static char const * const names[] = {"xa", NULL };
  static sox_format_handler_t const handler = {SOX_LIB_VERSION_CODE,
    "16-bit ADPCM audio files used by Maxis games",
    names, SOX_FILE_LIT_END,
    startread, read_samples, stopread,
    NULL, NULL, NULL,
    NULL, NULL, NULL, sizeof(priv_t)
  };
  return &handler;
}