Reserve extra space for conversions.
[wine] / dlls / msacm / winemp3 / decode_i386.c
1 /*
2  * Mpeg Layer-1,2,3 audio decoder
3  * ------------------------------
4  * copyright (c) 1995,1996,1997 by Michael Hipp, All rights reserved.
5  * See also 'README'
6  *
7  * slighlty optimized for machines without autoincrement/decrement.
8  * The performance is highly compiler dependend. Maybe
9  * the decode.c version for 'normal' processor may be faster
10  * even for Intel processors.
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #include <stdlib.h>
28 #include <math.h>
29 #include <string.h>
30
31 #include "mpg123.h"
32 #include "mpglib.h"
33
34 extern struct mpstr *gmp;
35
36  /* old WRITE_SAMPLE */
37 #define WRITE_SAMPLE(samples,sum,clip) \
38   if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
39   else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
40   else { *(samples) = sum; }
41
42 int synth_1to1_mono(real *bandPtr,unsigned char *samples,int *pnt)
43 {
44   short samples_tmp[64];
45   short *tmp1 = samples_tmp;
46   int i,ret;
47   int pnt1 = 0;
48
49   ret = synth_1to1(bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
50   samples += *pnt;
51
52   for(i=0;i<32;i++) {
53     *( (short *) samples) = *tmp1;
54     samples += 2;
55     tmp1 += 2;
56   }
57   *pnt += 64;
58
59   return ret;
60 }
61
62
63 int synth_1to1(real *bandPtr,int channel,unsigned char *out,int *pnt)
64 {
65   static const int step = 2;
66   int bo;
67   short *samples = (short *) (out + *pnt);
68
69   real *b0,(*buf)[0x110];
70   int clip = 0;
71   int bo1;
72
73   bo = gmp->synth_bo;
74
75   if(!channel) {
76     bo--;
77     bo &= 0xf;
78     buf = gmp->synth_buffs[0];
79   }
80   else {
81     samples++;
82     buf = gmp->synth_buffs[1];
83   }
84
85   if(bo & 0x1) {
86     b0 = buf[0];
87     bo1 = bo;
88     dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr);
89   }
90   else {
91     b0 = buf[1];
92     bo1 = bo+1;
93     dct64(buf[0]+bo,buf[1]+bo+1,bandPtr);
94   }
95
96   gmp->synth_bo = bo;
97
98   {
99     register int j;
100     real *window = decwin + 16 - bo1;
101
102     for (j=16;j;j--,b0+=0x10,window+=0x20,samples+=step)
103     {
104       real sum;
105       sum  = window[0x0] * b0[0x0];
106       sum -= window[0x1] * b0[0x1];
107       sum += window[0x2] * b0[0x2];
108       sum -= window[0x3] * b0[0x3];
109       sum += window[0x4] * b0[0x4];
110       sum -= window[0x5] * b0[0x5];
111       sum += window[0x6] * b0[0x6];
112       sum -= window[0x7] * b0[0x7];
113       sum += window[0x8] * b0[0x8];
114       sum -= window[0x9] * b0[0x9];
115       sum += window[0xA] * b0[0xA];
116       sum -= window[0xB] * b0[0xB];
117       sum += window[0xC] * b0[0xC];
118       sum -= window[0xD] * b0[0xD];
119       sum += window[0xE] * b0[0xE];
120       sum -= window[0xF] * b0[0xF];
121
122       WRITE_SAMPLE(samples,sum,clip);
123     }
124
125     {
126       real sum;
127       sum  = window[0x0] * b0[0x0];
128       sum += window[0x2] * b0[0x2];
129       sum += window[0x4] * b0[0x4];
130       sum += window[0x6] * b0[0x6];
131       sum += window[0x8] * b0[0x8];
132       sum += window[0xA] * b0[0xA];
133       sum += window[0xC] * b0[0xC];
134       sum += window[0xE] * b0[0xE];
135       WRITE_SAMPLE(samples,sum,clip);
136       b0-=0x10,window-=0x20,samples+=step;
137     }
138     window += bo1<<1;
139
140     for (j=15;j;j--,b0-=0x10,window-=0x20,samples+=step)
141     {
142       real sum;
143       sum = -window[-0x1] * b0[0x0];
144       sum -= window[-0x2] * b0[0x1];
145       sum -= window[-0x3] * b0[0x2];
146       sum -= window[-0x4] * b0[0x3];
147       sum -= window[-0x5] * b0[0x4];
148       sum -= window[-0x6] * b0[0x5];
149       sum -= window[-0x7] * b0[0x6];
150       sum -= window[-0x8] * b0[0x7];
151       sum -= window[-0x9] * b0[0x8];
152       sum -= window[-0xA] * b0[0x9];
153       sum -= window[-0xB] * b0[0xA];
154       sum -= window[-0xC] * b0[0xB];
155       sum -= window[-0xD] * b0[0xC];
156       sum -= window[-0xE] * b0[0xD];
157       sum -= window[-0xF] * b0[0xE];
158       sum -= window[-0x0] * b0[0xF];
159
160       WRITE_SAMPLE(samples,sum,clip);
161     }
162   }
163   *pnt += 128;
164
165   return clip;
166 }