misc: Use GCC builtins for byte-swap operations
[pcsx_rearmed.git] / plugins / dfsound / xa.c
CommitLineData
ef79bbde
P
1/***************************************************************************
2 xa.c - description
3 -------------------
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. See also the license.txt file for *
14 * additional informations. *
15 * *
16 ***************************************************************************/
17
18#include "stdafx.h"
19#define _IN_XA
20#include <stdint.h>
21
22// will be included from spu.c
23#ifdef _IN_SPU
24
25////////////////////////////////////////////////////////////////////////
26// XA GLOBALS
27////////////////////////////////////////////////////////////////////////
28
ef79bbde
P
29static int gauss_ptr = 0;
30static int gauss_window[8] = {0, 0, 0, 0, 0, 0, 0, 0};
31
32#define gvall0 gauss_window[gauss_ptr]
33#define gvall(x) gauss_window[(gauss_ptr+x)&3]
34#define gvalr0 gauss_window[4+gauss_ptr]
35#define gvalr(x) gauss_window[4+((gauss_ptr+x)&3)]
36
37////////////////////////////////////////////////////////////////////////
38// MIX XA & CDDA
39////////////////////////////////////////////////////////////////////////
40
3bd31caf 41INLINE void MixXA(int *SSumLR, int ns_to, int decode_pos)
ef79bbde 42{
215ff9e6 43 int cursor = decode_pos;
ef79bbde 44 int ns;
efce366c 45 short l, r;
b3ddf04a 46 uint32_t v = spu.XALastVal;
ef79bbde 47
3154bfab 48 if(spu.XAPlay != spu.XAFeed || spu.XARepeat > 0)
efce366c 49 {
3154bfab 50 if(spu.XAPlay == spu.XAFeed)
51 spu.XARepeat--;
efce366c 52
650adfd2 53 for(ns = 0; ns < ns_to*2; )
efce366c 54 {
3154bfab 55 if(spu.XAPlay != spu.XAFeed) v=*spu.XAPlay++;
56 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
efce366c 57
3154bfab 58 l = ((int)(short)v * spu.iLeftXAVol) >> 15;
59 r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
efce366c 60 SSumLR[ns++] += l;
61 SSumLR[ns++] += r;
1d753163 62
3154bfab 63 spu.spuMem[cursor] = v;
64 spu.spuMem[cursor + 0x400/2] = v >> 16;
efce366c 65 cursor = (cursor + 1) & 0x1ff;
66 }
3154bfab 67 spu.XALastVal = v;
efce366c 68 }
b3ddf04a 69 // occasionally CDDAFeed underflows by a few samples due to poor timing,
70 // hence this 'ns_to < 8'
71 else if(spu.CDDAPlay != spu.CDDAFeed || ns_to < 8)
72 {
73 for(ns = 0; ns < ns_to*2; )
74 {
75 if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
76 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
ef79bbde 77
b3ddf04a 78 l = ((int)(short)v * spu.iLeftXAVol) >> 15;
79 r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
80 SSumLR[ns++] += l;
81 SSumLR[ns++] += r;
1d753163 82
b3ddf04a 83 spu.spuMem[cursor] = v;
84 spu.spuMem[cursor + 0x400/2] = v >> 16;
85 cursor = (cursor + 1) & 0x1ff;
86 }
87 spu.XALastVal = v;
88 }
89 else
90 spu.XALastVal = 0;
ef79bbde
P
91}
92
93////////////////////////////////////////////////////////////////////////
94// small linux time helper... only used for watchdog
95////////////////////////////////////////////////////////////////////////
96
f05d6ca2 97static unsigned long timeGetTime_spu()
ef79bbde 98{
de4a0279 99#if defined(NO_OS)
100 return 0;
101#elif defined(_WIN32)
003cfc63 102 return GetTickCount();
103#else
ef79bbde
P
104 struct timeval tv;
105 gettimeofday(&tv, 0); // well, maybe there are better ways
106 return tv.tv_sec * 1000 + tv.tv_usec/1000; // to do that, but at least it works
003cfc63 107#endif
ef79bbde
P
108}
109
110////////////////////////////////////////////////////////////////////////
111// FEED XA
112////////////////////////////////////////////////////////////////////////
113
114INLINE void FeedXA(xa_decode_t *xap)
115{
116 int sinc,spos,i,iSize,iPlace,vl,vr;
117
3154bfab 118 if(!spu.bSPUIsOpen) return;
ef79bbde 119
3154bfab 120 spu.xapGlobal = xap; // store info for save states
121 spu.XARepeat = 100; // set up repeat
ef79bbde 122
97ea4077 123#if 0//def XA_HACK
ef79bbde
P
124 iSize=((45500*xap->nsamples)/xap->freq); // get size
125#else
126 iSize=((44100*xap->nsamples)/xap->freq); // get size
127#endif
128 if(!iSize) return; // none? bye
129
3154bfab 130 if(spu.XAFeed<spu.XAPlay) iPlace=spu.XAPlay-spu.XAFeed; // how much space in my buf?
131 else iPlace=(spu.XAEnd-spu.XAFeed) + (spu.XAPlay-spu.XAStart);
ef79bbde
P
132
133 if(iPlace==0) return; // no place at all
134
135 //----------------------------------------------------//
3154bfab 136 if(spu_config.iXAPitch) // pitch change option?
ef79bbde
P
137 {
138 static DWORD dwLT=0;
139 static DWORD dwFPS=0;
140 static int iFPSCnt=0;
141 static int iLastSize=0;
142 static DWORD dwL1=0;
143 DWORD dw=timeGetTime_spu(),dw1,dw2;
144
145 iPlace=iSize;
146
147 dwFPS+=dw-dwLT;iFPSCnt++;
148
149 dwLT=dw;
150
151 if(iFPSCnt>=10)
152 {
153 if(!dwFPS) dwFPS=1;
154 dw1=1000000/dwFPS;
155 if(dw1>=(dwL1-100) && dw1<=(dwL1+100)) dw1=dwL1;
156 else dwL1=dw1;
157 dw2=(xap->freq*100/xap->nsamples);
158 if((!dw1)||((dw2+100)>=dw1)) iLastSize=0;
159 else
160 {
161 iLastSize=iSize*dw2/dw1;
162 if(iLastSize>iPlace) iLastSize=iPlace;
163 iSize=iLastSize;
164 }
165 iFPSCnt=0;dwFPS=0;
166 }
167 else
168 {
169 if(iLastSize) iSize=iLastSize;
170 }
171 }
172 //----------------------------------------------------//
173
174 spos=0x10000L;
175 sinc = (xap->nsamples << 16) / iSize; // calc freq by num / size
176
177 if(xap->stereo)
178{
179 uint32_t * pS=(uint32_t *)xap->pcm;
180 uint32_t l=0;
181
3154bfab 182 if(spu_config.iXAPitch)
ef79bbde
P
183 {
184 int32_t l1,l2;short s;
185 for(i=0;i<iSize;i++)
186 {
3154bfab 187 if(spu_config.iUseInterpolation==2)
ef79bbde
P
188 {
189 while(spos>=0x10000L)
190 {
191 l = *pS++;
192 gauss_window[gauss_ptr] = (short)LOWORD(l);
193 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
194 gauss_ptr = (gauss_ptr+1) & 3;
195 spos -= 0x10000L;
196 }
197 vl = (spos >> 6) & ~3;
63646208
S
198 vr=(gauss[vl]*gvall0) >> 15;
199 vr+=(gauss[vl+1]*gvall(1)) >> 15;
200 vr+=(gauss[vl+2]*gvall(2)) >> 15;
201 vr+=(gauss[vl+3]*gvall(3)) >> 15;
202 l= vr & 0xffff;
203 vr=(gauss[vl]*gvalr0) >> 15;
204 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
205 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
206 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
207 l |= vr << 16;
ef79bbde
P
208 }
209 else
210 {
211 while(spos>=0x10000L)
212 {
213 l = *pS++;
214 spos -= 0x10000L;
215 }
216 }
217
218 s=(short)LOWORD(l);
219 l1=s;
220 l1=(l1*iPlace)/iSize;
381ea103 221 ssat32_to_16(l1);
ef79bbde
P
222 s=(short)HIWORD(l);
223 l2=s;
224 l2=(l2*iPlace)/iSize;
381ea103 225 ssat32_to_16(l2);
ef79bbde
P
226 l=(l1&0xffff)|(l2<<16);
227
3154bfab 228 *spu.XAFeed++=l;
ef79bbde 229
3154bfab 230 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
231 if(spu.XAFeed==spu.XAPlay)
ef79bbde 232 {
3154bfab 233 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
234 break;
235 }
236
237 spos += sinc;
238 }
239 }
240 else
241 {
242 for(i=0;i<iSize;i++)
243 {
3154bfab 244 if(spu_config.iUseInterpolation==2)
ef79bbde
P
245 {
246 while(spos>=0x10000L)
247 {
248 l = *pS++;
249 gauss_window[gauss_ptr] = (short)LOWORD(l);
250 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
251 gauss_ptr = (gauss_ptr+1) & 3;
252 spos -= 0x10000L;
253 }
254 vl = (spos >> 6) & ~3;
63646208
S
255 vr=(gauss[vl]*gvall0) >> 15;
256 vr+=(gauss[vl+1]*gvall(1)) >> 15;
257 vr+=(gauss[vl+2]*gvall(2)) >> 15;
258 vr+=(gauss[vl+3]*gvall(3)) >> 15;
259 l= vr & 0xffff;
260 vr=(gauss[vl]*gvalr0) >> 15;
261 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
262 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
263 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
264 l |= vr << 16;
ef79bbde
P
265 }
266 else
267 {
268 while(spos>=0x10000L)
269 {
270 l = *pS++;
271 spos -= 0x10000L;
272 }
273 }
274
3154bfab 275 *spu.XAFeed++=l;
ef79bbde 276
3154bfab 277 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
278 if(spu.XAFeed==spu.XAPlay)
ef79bbde 279 {
3154bfab 280 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
281 break;
282 }
283
284 spos += sinc;
285 }
286 }
287 }
288 else
289 {
290 unsigned short * pS=(unsigned short *)xap->pcm;
291 uint32_t l;short s=0;
292
3154bfab 293 if(spu_config.iXAPitch)
ef79bbde
P
294 {
295 int32_t l1;
296 for(i=0;i<iSize;i++)
297 {
3154bfab 298 if(spu_config.iUseInterpolation==2)
ef79bbde
P
299 {
300 while(spos>=0x10000L)
301 {
302 gauss_window[gauss_ptr] = (short)*pS++;
303 gauss_ptr = (gauss_ptr+1) & 3;
304 spos -= 0x10000L;
305 }
306 vl = (spos >> 6) & ~3;
63646208
S
307 vr=(gauss[vl]*gvall0) >> 15;
308 vr+=(gauss[vl+1]*gvall(1)) >> 15;
309 vr+=(gauss[vl+2]*gvall(2)) >> 15;
310 vr+=(gauss[vl+3]*gvall(3)) >> 15;
311 l1=s= vr;
ef79bbde
P
312 l1 &= 0xffff;
313 }
314 else
315 {
316 while(spos>=0x10000L)
317 {
318 s = *pS++;
319 spos -= 0x10000L;
320 }
321 l1=s;
322 }
323
324 l1=(l1*iPlace)/iSize;
381ea103 325 ssat32_to_16(l1);
ef79bbde 326 l=(l1&0xffff)|(l1<<16);
3154bfab 327 *spu.XAFeed++=l;
ef79bbde 328
3154bfab 329 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
330 if(spu.XAFeed==spu.XAPlay)
ef79bbde 331 {
3154bfab 332 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
333 break;
334 }
335
336 spos += sinc;
337 }
338 }
339 else
340 {
341 for(i=0;i<iSize;i++)
342 {
3154bfab 343 if(spu_config.iUseInterpolation==2)
ef79bbde
P
344 {
345 while(spos>=0x10000L)
346 {
347 gauss_window[gauss_ptr] = (short)*pS++;
348 gauss_ptr = (gauss_ptr+1) & 3;
349 spos -= 0x10000L;
350 }
351 vl = (spos >> 6) & ~3;
63646208
S
352 vr=(gauss[vl]*gvall0) >> 15;
353 vr+=(gauss[vl+1]*gvall(1)) >> 15;
354 vr+=(gauss[vl+2]*gvall(2)) >> 15;
355 vr+=(gauss[vl+3]*gvall(3)) >> 15;
356 l=s= vr;
ef79bbde
P
357 }
358 else
359 {
360 while(spos>=0x10000L)
361 {
362 s = *pS++;
363 spos -= 0x10000L;
364 }
365 l=s;
366 }
367
9098b863 368 l &= 0xffff;
3154bfab 369 *spu.XAFeed++=(l|(l<<16));
ef79bbde 370
3154bfab 371 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
372 if(spu.XAFeed==spu.XAPlay)
ef79bbde 373 {
3154bfab 374 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
375 break;
376 }
377
378 spos += sinc;
379 }
380 }
381 }
382}
383
384////////////////////////////////////////////////////////////////////////
385// FEED CDDA
386////////////////////////////////////////////////////////////////////////
387
983a7cfd 388INLINE int FeedCDDA(unsigned char *pcm, int nBytes)
ef79bbde 389{
983a7cfd 390 int space;
3154bfab 391 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
983a7cfd 392 if(space<nBytes)
393 return 0x7761; // rearmed_wait
394
ef79bbde
P
395 while(nBytes>0)
396 {
3154bfab 397 if(spu.CDDAFeed==spu.CDDAEnd) spu.CDDAFeed=spu.CDDAStart;
398 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
399 if(spu.CDDAFeed+space/4>spu.CDDAEnd)
400 space=(spu.CDDAEnd-spu.CDDAFeed)*4;
983a7cfd 401 if(space>nBytes)
402 space=nBytes;
403
3154bfab 404 memcpy(spu.CDDAFeed,pcm,space);
405 spu.CDDAFeed+=space/4;
983a7cfd 406 nBytes-=space;
407 pcm+=space;
ef79bbde 408 }
983a7cfd 409
410 return 0x676f; // rearmed_go
ef79bbde
P
411}
412
413#endif