standalone: allow scaler to cut off the letterbox
[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"
b71a957f 19#include "spu.h"
ef79bbde
P
20#define _IN_XA
21#include <stdint.h>
22
23// will be included from spu.c
24#ifdef _IN_SPU
25
26////////////////////////////////////////////////////////////////////////
27// XA GLOBALS
28////////////////////////////////////////////////////////////////////////
29
ef79bbde
P
30static int gauss_ptr = 0;
31static int gauss_window[8] = {0, 0, 0, 0, 0, 0, 0, 0};
32
33#define gvall0 gauss_window[gauss_ptr]
34#define gvall(x) gauss_window[(gauss_ptr+x)&3]
35#define gvalr0 gauss_window[4+gauss_ptr]
36#define gvalr(x) gauss_window[4+((gauss_ptr+x)&3)]
37
38////////////////////////////////////////////////////////////////////////
39// MIX XA & CDDA
40////////////////////////////////////////////////////////////////////////
41
740eb9bc 42INLINE void SkipCD(int ns_to, int decode_pos)
ef79bbde 43{
215ff9e6 44 int cursor = decode_pos;
ef79bbde 45 int ns;
740eb9bc 46
47 if(spu.XAPlay != spu.XAFeed)
48 {
49 for(ns = 0; ns < ns_to*2; ns += 2)
50 {
51 if(spu.XAPlay != spu.XAFeed) spu.XAPlay++;
52 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
53
54 spu.spuMem[cursor] = 0;
55 spu.spuMem[cursor + 0x400/2] = 0;
56 cursor = (cursor + 1) & 0x1ff;
57 }
58 }
59 else if(spu.CDDAPlay != spu.CDDAFeed)
60 {
61 for(ns = 0; ns < ns_to*2; ns += 2)
62 {
63 if(spu.CDDAPlay != spu.CDDAFeed) spu.CDDAPlay++;
64 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
65
66 spu.spuMem[cursor] = 0;
67 spu.spuMem[cursor + 0x400/2] = 0;
68 cursor = (cursor + 1) & 0x1ff;
69 }
70 }
71 spu.XALastVal = 0;
72}
73
74INLINE void MixCD(int *SSumLR, int *RVB, int ns_to, int decode_pos)
75{
76 int vll = spu.iLeftXAVol * spu.cdv.ll >> 7;
77 int vrl = spu.iLeftXAVol * spu.cdv.rl >> 7;
78 int vlr = spu.iRightXAVol * spu.cdv.lr >> 7;
79 int vrr = spu.iRightXAVol * spu.cdv.rr >> 7;
80 int cursor = decode_pos;
81 int l1, r1, l, r;
82 int ns;
b3ddf04a 83 uint32_t v = spu.XALastVal;
ef79bbde 84
5ccf556f 85 // note: spu volume doesn't affect cd capture
86 if ((spu.cdv.ll | spu.cdv.lr | spu.cdv.rl | spu.cdv.rr) == 0)
740eb9bc 87 {
88 SkipCD(ns_to, decode_pos);
89 return;
90 }
91
3154bfab 92 if(spu.XAPlay != spu.XAFeed || spu.XARepeat > 0)
efce366c 93 {
3154bfab 94 if(spu.XAPlay == spu.XAFeed)
95 spu.XARepeat--;
efce366c 96
e81f182d 97 for(ns = 0; ns < ns_to*2; ns += 2)
efce366c 98 {
3154bfab 99 if(spu.XAPlay != spu.XAFeed) v=*spu.XAPlay++;
100 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
efce366c 101
740eb9bc 102 l1 = (short)v, r1 = (short)(v >> 16);
103 l = (l1 * vll + r1 * vrl) >> 15;
104 r = (r1 * vrr + l1 * vlr) >> 15;
105 ssat32_to_16(l);
106 ssat32_to_16(r);
e81f182d 107 if (spu.spuCtrl & CTRL_CD)
108 {
109 SSumLR[ns+0] += l;
110 SSumLR[ns+1] += r;
111 }
112 if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
113 {
114 RVB[ns+0] += l;
115 RVB[ns+1] += r;
116 }
1d753163 117
b71a957f
PC
118 spu.spuMem[cursor] = HTOLE16(v);
119 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
efce366c 120 cursor = (cursor + 1) & 0x1ff;
121 }
3154bfab 122 spu.XALastVal = v;
efce366c 123 }
b3ddf04a 124 // occasionally CDDAFeed underflows by a few samples due to poor timing,
125 // hence this 'ns_to < 8'
126 else if(spu.CDDAPlay != spu.CDDAFeed || ns_to < 8)
127 {
e81f182d 128 for(ns = 0; ns < ns_to*2; ns += 2)
b3ddf04a 129 {
130 if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
131 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
ef79bbde 132
740eb9bc 133 l1 = (short)v, r1 = (short)(v >> 16);
134 l = (l1 * vll + r1 * vrl) >> 15;
135 r = (r1 * vrr + l1 * vlr) >> 15;
136 ssat32_to_16(l);
137 ssat32_to_16(r);
e81f182d 138 if (spu.spuCtrl & CTRL_CD)
139 {
140 SSumLR[ns+0] += l;
141 SSumLR[ns+1] += r;
142 }
143 if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
144 {
145 RVB[ns+0] += l;
146 RVB[ns+1] += r;
147 }
1d753163 148
b71a957f
PC
149 spu.spuMem[cursor] = HTOLE16(v);
150 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
b3ddf04a 151 cursor = (cursor + 1) & 0x1ff;
152 }
153 spu.XALastVal = v;
154 }
155 else
156 spu.XALastVal = 0;
ef79bbde
P
157}
158
159////////////////////////////////////////////////////////////////////////
160// small linux time helper... only used for watchdog
161////////////////////////////////////////////////////////////////////////
162
db3bfe5c 163#if 0
f05d6ca2 164static unsigned long timeGetTime_spu()
ef79bbde 165{
de4a0279 166#if defined(NO_OS)
167 return 0;
168#elif defined(_WIN32)
003cfc63 169 return GetTickCount();
170#else
ef79bbde
P
171 struct timeval tv;
172 gettimeofday(&tv, 0); // well, maybe there are better ways
173 return tv.tv_sec * 1000 + tv.tv_usec/1000; // to do that, but at least it works
003cfc63 174#endif
ef79bbde 175}
db3bfe5c 176#endif
ef79bbde
P
177
178////////////////////////////////////////////////////////////////////////
179// FEED XA
180////////////////////////////////////////////////////////////////////////
181
403a6290 182void FeedXA(const xa_decode_t *xap)
ef79bbde
P
183{
184 int sinc,spos,i,iSize,iPlace,vl,vr;
185
3154bfab 186 if(!spu.bSPUIsOpen) return;
ef79bbde 187
cdaef86c 188 spu.XARepeat = 3; // set up repeat
ef79bbde 189
97ea4077 190#if 0//def XA_HACK
ef79bbde
P
191 iSize=((45500*xap->nsamples)/xap->freq); // get size
192#else
193 iSize=((44100*xap->nsamples)/xap->freq); // get size
194#endif
195 if(!iSize) return; // none? bye
196
3154bfab 197 if(spu.XAFeed<spu.XAPlay) iPlace=spu.XAPlay-spu.XAFeed; // how much space in my buf?
198 else iPlace=(spu.XAEnd-spu.XAFeed) + (spu.XAPlay-spu.XAStart);
ef79bbde
P
199
200 if(iPlace==0) return; // no place at all
201
202 //----------------------------------------------------//
db3bfe5c 203#if 0
3154bfab 204 if(spu_config.iXAPitch) // pitch change option?
ef79bbde
P
205 {
206 static DWORD dwLT=0;
207 static DWORD dwFPS=0;
208 static int iFPSCnt=0;
209 static int iLastSize=0;
210 static DWORD dwL1=0;
211 DWORD dw=timeGetTime_spu(),dw1,dw2;
212
213 iPlace=iSize;
214
215 dwFPS+=dw-dwLT;iFPSCnt++;
216
217 dwLT=dw;
218
219 if(iFPSCnt>=10)
220 {
221 if(!dwFPS) dwFPS=1;
222 dw1=1000000/dwFPS;
223 if(dw1>=(dwL1-100) && dw1<=(dwL1+100)) dw1=dwL1;
224 else dwL1=dw1;
225 dw2=(xap->freq*100/xap->nsamples);
226 if((!dw1)||((dw2+100)>=dw1)) iLastSize=0;
227 else
228 {
229 iLastSize=iSize*dw2/dw1;
230 if(iLastSize>iPlace) iLastSize=iPlace;
231 iSize=iLastSize;
232 }
233 iFPSCnt=0;dwFPS=0;
234 }
235 else
236 {
237 if(iLastSize) iSize=iLastSize;
238 }
239 }
db3bfe5c 240#endif
ef79bbde
P
241 //----------------------------------------------------//
242
243 spos=0x10000L;
244 sinc = (xap->nsamples << 16) / iSize; // calc freq by num / size
245
246 if(xap->stereo)
247{
248 uint32_t * pS=(uint32_t *)xap->pcm;
249 uint32_t l=0;
250
db3bfe5c 251#if 0
3154bfab 252 if(spu_config.iXAPitch)
ef79bbde
P
253 {
254 int32_t l1,l2;short s;
255 for(i=0;i<iSize;i++)
256 {
3154bfab 257 if(spu_config.iUseInterpolation==2)
ef79bbde
P
258 {
259 while(spos>=0x10000L)
260 {
261 l = *pS++;
262 gauss_window[gauss_ptr] = (short)LOWORD(l);
263 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
264 gauss_ptr = (gauss_ptr+1) & 3;
265 spos -= 0x10000L;
266 }
267 vl = (spos >> 6) & ~3;
63646208
S
268 vr=(gauss[vl]*gvall0) >> 15;
269 vr+=(gauss[vl+1]*gvall(1)) >> 15;
270 vr+=(gauss[vl+2]*gvall(2)) >> 15;
271 vr+=(gauss[vl+3]*gvall(3)) >> 15;
272 l= vr & 0xffff;
273 vr=(gauss[vl]*gvalr0) >> 15;
274 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
275 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
276 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
277 l |= vr << 16;
ef79bbde
P
278 }
279 else
280 {
281 while(spos>=0x10000L)
282 {
283 l = *pS++;
284 spos -= 0x10000L;
285 }
286 }
287
288 s=(short)LOWORD(l);
289 l1=s;
290 l1=(l1*iPlace)/iSize;
381ea103 291 ssat32_to_16(l1);
ef79bbde
P
292 s=(short)HIWORD(l);
293 l2=s;
294 l2=(l2*iPlace)/iSize;
381ea103 295 ssat32_to_16(l2);
ef79bbde
P
296 l=(l1&0xffff)|(l2<<16);
297
3154bfab 298 *spu.XAFeed++=l;
ef79bbde 299
3154bfab 300 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
301 if(spu.XAFeed==spu.XAPlay)
ef79bbde 302 {
3154bfab 303 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
304 break;
305 }
306
307 spos += sinc;
308 }
309 }
310 else
db3bfe5c 311#endif
ef79bbde
P
312 {
313 for(i=0;i<iSize;i++)
314 {
3154bfab 315 if(spu_config.iUseInterpolation==2)
ef79bbde
P
316 {
317 while(spos>=0x10000L)
318 {
319 l = *pS++;
320 gauss_window[gauss_ptr] = (short)LOWORD(l);
321 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
322 gauss_ptr = (gauss_ptr+1) & 3;
323 spos -= 0x10000L;
324 }
325 vl = (spos >> 6) & ~3;
63646208
S
326 vr=(gauss[vl]*gvall0) >> 15;
327 vr+=(gauss[vl+1]*gvall(1)) >> 15;
328 vr+=(gauss[vl+2]*gvall(2)) >> 15;
329 vr+=(gauss[vl+3]*gvall(3)) >> 15;
330 l= vr & 0xffff;
331 vr=(gauss[vl]*gvalr0) >> 15;
332 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
333 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
334 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
335 l |= vr << 16;
ef79bbde
P
336 }
337 else
338 {
339 while(spos>=0x10000L)
340 {
341 l = *pS++;
342 spos -= 0x10000L;
343 }
344 }
345
3154bfab 346 *spu.XAFeed++=l;
ef79bbde 347
3154bfab 348 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
349 if(spu.XAFeed==spu.XAPlay)
ef79bbde 350 {
3154bfab 351 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
352 break;
353 }
354
355 spos += sinc;
356 }
357 }
358 }
359 else
360 {
361 unsigned short * pS=(unsigned short *)xap->pcm;
362 uint32_t l;short s=0;
363
db3bfe5c 364#if 0
3154bfab 365 if(spu_config.iXAPitch)
ef79bbde
P
366 {
367 int32_t l1;
368 for(i=0;i<iSize;i++)
369 {
3154bfab 370 if(spu_config.iUseInterpolation==2)
ef79bbde
P
371 {
372 while(spos>=0x10000L)
373 {
374 gauss_window[gauss_ptr] = (short)*pS++;
375 gauss_ptr = (gauss_ptr+1) & 3;
376 spos -= 0x10000L;
377 }
378 vl = (spos >> 6) & ~3;
63646208
S
379 vr=(gauss[vl]*gvall0) >> 15;
380 vr+=(gauss[vl+1]*gvall(1)) >> 15;
381 vr+=(gauss[vl+2]*gvall(2)) >> 15;
382 vr+=(gauss[vl+3]*gvall(3)) >> 15;
383 l1=s= vr;
ef79bbde
P
384 l1 &= 0xffff;
385 }
386 else
387 {
388 while(spos>=0x10000L)
389 {
390 s = *pS++;
391 spos -= 0x10000L;
392 }
393 l1=s;
394 }
395
396 l1=(l1*iPlace)/iSize;
381ea103 397 ssat32_to_16(l1);
ef79bbde 398 l=(l1&0xffff)|(l1<<16);
3154bfab 399 *spu.XAFeed++=l;
ef79bbde 400
3154bfab 401 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
402 if(spu.XAFeed==spu.XAPlay)
ef79bbde 403 {
3154bfab 404 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
405 break;
406 }
407
408 spos += sinc;
409 }
410 }
411 else
db3bfe5c 412#endif
ef79bbde
P
413 {
414 for(i=0;i<iSize;i++)
415 {
3154bfab 416 if(spu_config.iUseInterpolation==2)
ef79bbde
P
417 {
418 while(spos>=0x10000L)
419 {
420 gauss_window[gauss_ptr] = (short)*pS++;
421 gauss_ptr = (gauss_ptr+1) & 3;
422 spos -= 0x10000L;
423 }
424 vl = (spos >> 6) & ~3;
63646208
S
425 vr=(gauss[vl]*gvall0) >> 15;
426 vr+=(gauss[vl+1]*gvall(1)) >> 15;
427 vr+=(gauss[vl+2]*gvall(2)) >> 15;
428 vr+=(gauss[vl+3]*gvall(3)) >> 15;
429 l=s= vr;
ef79bbde
P
430 }
431 else
432 {
433 while(spos>=0x10000L)
434 {
435 s = *pS++;
436 spos -= 0x10000L;
437 }
438 l=s;
439 }
440
9098b863 441 l &= 0xffff;
3154bfab 442 *spu.XAFeed++=(l|(l<<16));
ef79bbde 443
3154bfab 444 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
445 if(spu.XAFeed==spu.XAPlay)
ef79bbde 446 {
3154bfab 447 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
448 break;
449 }
450
451 spos += sinc;
452 }
453 }
454 }
455}
456
457////////////////////////////////////////////////////////////////////////
458// FEED CDDA
459////////////////////////////////////////////////////////////////////////
460
403a6290 461void FeedCDDA(unsigned char *pcm, int nBytes)
ef79bbde 462{
983a7cfd 463 int space;
3154bfab 464 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
6109649c 465 if (space < nBytes) {
466 log_unhandled("FeedCDDA: %d/%d\n", nBytes, space);
403a6290 467 return;
6109649c 468 }
983a7cfd 469
ef79bbde
P
470 while(nBytes>0)
471 {
3154bfab 472 if(spu.CDDAFeed==spu.CDDAEnd) spu.CDDAFeed=spu.CDDAStart;
473 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
474 if(spu.CDDAFeed+space/4>spu.CDDAEnd)
475 space=(spu.CDDAEnd-spu.CDDAFeed)*4;
983a7cfd 476 if(space>nBytes)
477 space=nBytes;
478
3154bfab 479 memcpy(spu.CDDAFeed,pcm,space);
480 spu.CDDAFeed+=space/4;
983a7cfd 481 nBytes-=space;
482 pcm+=space;
ef79bbde
P
483 }
484}
485
486#endif
e81f182d 487// vim:shiftwidth=1:expandtab