forked from xjsxjs197/WiiSXRX_2022
-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode_xa.c
351 lines (299 loc) · 13.1 KB
/
decode_xa.c
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/***************************************************************************
* Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/
/*
* XA audio decoding functions (Kazzuya).
*/
#include "decode_xa.h"
#include "psxcommon.h"
#include "Gamecube/DEBUG.h"
#define NOT(_X_) (!(_X_))
#define XACLAMP(_X_,_MI_,_MA_) {if(_X_<_MI_)_X_=_MI_;else if(_X_>_MA_)_X_=_MA_;}
//============================================
//=== ADPCM DECODING ROUTINES
//============================================
static f32 FK0[4] = {
0.0 ,
0.9375 ,
1.796875 ,
1.53125
};
static f32 FK1[4] = {
0.0 ,
0.0 ,
-0.8125 ,
-0.859375
};
#define BLKSIZ 28 /* block size (32 - 4 nibbles) */
//===========================================
void ADPCM_InitDecode(ADPCM_Decode_t *decp) {
decp->y0 = 0.0;
decp->y1 = 0.0;
}
//===========================================
static typPcmDecode decodeTmp;
extern void psDecodeBlock16(register void* decodeTmp, register s16 *destp, register s32 inc);
static __inline void ADPCM_DecodeBlock16( ADPCM_Decode_t *decp, u8 filter_range, const void *vblockp, short *destp, int inc ) {
int i;
int range, filterid;
filterid = (filter_range >> 4) & 0x0f;
range = (filter_range >> 0) & 0x0f;
decodeTmp.IK0 = FIK0(filterid);
decodeTmp.IK1 = FIK1(filterid);
decodeTmp.vblockp = (u32)(vblockp);
decodeTmp.range = range;
decodeTmp.decpAddr = (u32)(decp);
psDecodeBlock16(&decodeTmp, destp, inc << 1);
}
static int headtable[4] = {0,2,8,10};
//===========================================
static void xa_decode_data( xa_decode_t *xdp, const unsigned char *srcp ) {
const u8 *sound_groupsp;
const u8 *sound_datap, *sound_datap2;
int i, j, k, nbits;
u16 data[4096], *datap;
short *destp;
destp = xdp->pcm;
nbits = xdp->nbits == 4 ? 4 : 2;
if (xdp->stereo) { // stereo
if ((xdp->nbits == 8) && (xdp->freq == 37800)) { // level A
for (j=0; j < 18; j++) {
sound_groupsp = srcp + j * 128; // sound groups header
sound_datap = sound_groupsp + 16; // sound data just after the header
for (i=0; i < nbits; i++) {
datap = data;
sound_datap2 = sound_datap + i;
for (k=0; k < 14; k++, sound_datap2 += 8) {
*(datap++) = (u16)sound_datap2[0] |
(u16)(sound_datap2[4] << 8);
}
ADPCM_DecodeBlock16( &xdp->left, sound_groupsp[headtable[i]+0], data,
destp+0, 2 );
datap = data;
sound_datap2 = sound_datap + i;
for (k=0; k < 14; k++, sound_datap2 += 8) {
*(datap++) = (u16)sound_datap2[0] |
(u16)(sound_datap2[4] << 8);
}
ADPCM_DecodeBlock16( &xdp->right, sound_groupsp[headtable[i]+1], data,
destp+1, 2 );
destp += 28*2;
}
}
} else { // level B/C
for (j=0; j < 18; j++) {
sound_groupsp = srcp + j * 128; // sound groups header
sound_datap = sound_groupsp + 16; // sound data just after the header
for (i=0; i < nbits; i++) {
datap = data;
sound_datap2 = sound_datap + i;
for (k=0; k < 7; k++, sound_datap2 += 16) {
*(datap++) = (u16)(sound_datap2[ 0] & 0x0f) |
((u16)(sound_datap2[ 4] & 0x0f) << 4) |
((u16)(sound_datap2[ 8] & 0x0f) << 8) |
((u16)(sound_datap2[12] & 0x0f) << 12);
}
ADPCM_DecodeBlock16( &xdp->left, sound_groupsp[headtable[i]+0], data,
destp+0, 2 );
datap = data;
sound_datap2 = sound_datap + i;
for (k=0; k < 7; k++, sound_datap2 += 16) {
*(datap++) = (u16)(sound_datap2[ 0] >> 4) |
((u16)(sound_datap2[ 4] >> 4) << 4) |
((u16)(sound_datap2[ 8] >> 4) << 8) |
((u16)(sound_datap2[12] >> 4) << 12);
}
ADPCM_DecodeBlock16( &xdp->right, sound_groupsp[headtable[i]+1], data,
destp+1, 2 );
destp += 28*2;
}
}
}
} else { // mono
if ((xdp->nbits == 8) && (xdp->freq == 37800)) { // level A
for (j=0; j < 18; j++) {
sound_groupsp = srcp + j * 128; // sound groups header
sound_datap = sound_groupsp + 16; // sound data just after the header
for (i=0; i < nbits; i++) {
datap = data;
sound_datap2 = sound_datap + i;
for (k=0; k < 14; k++, sound_datap2 += 8) {
*(datap++) = (u16)sound_datap2[0] |
(u16)(sound_datap2[4] << 8);
}
ADPCM_DecodeBlock16( &xdp->left, sound_groupsp[headtable[i]+0], data,
destp, 1 );
destp += 28;
datap = data;
sound_datap2 = sound_datap + i;
for (k=0; k < 14; k++, sound_datap2 += 8) {
*(datap++) = (u16)sound_datap2[0] |
(u16)(sound_datap2[4] << 8);
}
ADPCM_DecodeBlock16( &xdp->left, sound_groupsp[headtable[i]+1], data,
destp, 1 );
destp += 28;
}
}
} else { // level B/C
for (j=0; j < 18; j++) {
sound_groupsp = srcp + j * 128; // sound groups header
sound_datap = sound_groupsp + 16; // sound data just after the header
for (i=0; i < nbits; i++) {
datap = data;
sound_datap2 = sound_datap + i;
for (k=0; k < 7; k++, sound_datap2 += 16) {
*(datap++) = (u16)(sound_datap2[ 0] & 0x0f) |
((u16)(sound_datap2[ 4] & 0x0f) << 4) |
((u16)(sound_datap2[ 8] & 0x0f) << 8) |
((u16)(sound_datap2[12] & 0x0f) << 12);
}
ADPCM_DecodeBlock16( &xdp->left, sound_groupsp[headtable[i]+0], data,
destp, 1 );
destp += 28;
datap = data;
sound_datap2 = sound_datap + i;
for (k=0; k < 7; k++, sound_datap2 += 16) {
*(datap++) = (u16)(sound_datap2[ 0] >> 4) |
((u16)(sound_datap2[ 4] >> 4) << 4) |
((u16)(sound_datap2[ 8] >> 4) << 8) |
((u16)(sound_datap2[12] >> 4) << 12);
}
ADPCM_DecodeBlock16( &xdp->left, sound_groupsp[headtable[i]+1], data,
destp, 1 );
destp += 28;
}
}
}
}
}
//============================================
//=== XA SPECIFIC ROUTINES
//============================================
typedef struct {
u8 filenum;
u8 channum;
u8 submode;
u8 coding;
u8 filenum2;
u8 channum2;
u8 submode2;
u8 coding2;
} xa_subheader_t;
#define SUB_SUB_EOF (1<<7) // end of file
#define SUB_SUB_RT (1<<6) // real-time sector
#define SUB_SUB_FORM (1<<5) // 0 form1 1 form2
#define SUB_SUB_TRIGGER (1<<4) // used for interrupt
#define SUB_SUB_DATA (1<<3) // contains data
#define SUB_SUB_AUDIO (1<<2) // contains audio
#define SUB_SUB_VIDEO (1<<1) // contains video
#define SUB_SUB_EOR (1<<0) // end of record
#define AUDIO_CODING_GET_STEREO(_X_) ( (_X_) & 3)
#define AUDIO_CODING_GET_FREQ(_X_) (((_X_) >> 2) & 3)
#define AUDIO_CODING_GET_BPS(_X_) (((_X_) >> 4) & 3)
#define AUDIO_CODING_GET_EMPHASIS(_X_) (((_X_) >> 6) & 1)
#define SUB_UNKNOWN 0
#define SUB_VIDEO 1
#define SUB_AUDIO 2
//============================================
static int parse_xa_audio_sector( xa_decode_t *xdp,
const xa_subheader_t *subheadp,
const unsigned char *sectorp,
int is_first_sector ) {
if ( is_first_sector ) {
switch ( AUDIO_CODING_GET_FREQ(subheadp->coding) ) {
case 0: xdp->freq = 37800; break;
case 1: xdp->freq = 18900; break;
default: xdp->freq = 0; break;
}
switch ( AUDIO_CODING_GET_BPS(subheadp->coding) ) {
case 0: xdp->nbits = 4; break;
case 1: xdp->nbits = 8; break;
default: xdp->nbits = 0; break;
}
switch ( AUDIO_CODING_GET_STEREO(subheadp->coding) ) {
case 0: xdp->stereo = 0; break;
case 1: xdp->stereo = 1; break;
default: xdp->stereo = 0; break;
}
if ( xdp->freq == 0 )
return -1;
ADPCM_InitDecode( &xdp->left );
ADPCM_InitDecode( &xdp->right );
xdp->nsamples = 18 * 28 * 8;
if (xdp->freq == 37800) {
xdp->newSize = ((PS_SPU_FREQ * 18 * 28 * 8) / 37800);
xdp->sinc = ((u32)1 << 16) * 37800 / (PS_SPU_FREQ);
} else {
xdp->newSize = ((PS_SPU_FREQ * 18 * 28 * 8) / 18900);
xdp->sinc = ((u32)1 << 16) * 18900 / (PS_SPU_FREQ);
}
if (xdp->stereo == 1)
{
xdp->nsamples >>= 1;
xdp->newSize >>= 1;
}
}
xa_decode_data( xdp, sectorp );
return 0;
}
//================================================================
//=== THIS IS WHAT YOU HAVE TO CALL
//=== xdp - structure were all important data are returned
//=== sectorp - data in input
//=== pcmp - data in output
//=== is_first_sector - 1 if it's the 1st sector of the stream
//=== - 0 for any other successive sector
//=== return -1 if error
//================================================================
#ifdef DISP_DEBUG
// prototypes
//long long gettime(void);
//unsigned int diff_usec(long long start, long long end);
//u32 diff_nsec(u64 start,u64 end);
#endif // DISP_DEBUG
s32 xa_decode_sector( xa_decode_t *xdp, const unsigned char *sectorp, int is_first_sector ) {
#ifdef DISP_DEBUG
// long long curticks;
// curticks = gettime();
#endif // DISP_DEBUG
if (parse_xa_audio_sector(xdp, (xa_subheader_t *)sectorp, sectorp + sizeof(xa_subheader_t), is_first_sector))
return -1;
#ifdef DISP_DEBUG
// long long lastticks;
// lastticks = gettime();
// sprintf(txtbuffer, "XA %d ", diff_nsec(curticks, lastticks));
// DEBUG_print(txtbuffer, DBG_CORE2);
// curticks = lastticks;
#endif // DISP_DEBUG
return 0;
}
/* EXAMPLE:
"nsamples" is the number of 16 bit samples
every sample is 2 bytes in mono and 4 bytes in stereo
xa_decode_t xa;
sectorp = read_first_sector();
xa_decode_sector( &xa, sectorp, 1 );
play_wave( xa.pcm, xa.freq, xa.nsamples );
while ( --n_sectors )
{
sectorp = read_next_sector();
xa_decode_sector( &xa, sectorp, 0 );
play_wave( xa.pcm, xa.freq, xa.nsamples );
}
*/