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 
13 #include <stdlib.h>
14 #include <math.h>
15 #include <string.h>
16 
17 #include "mpg123_sdlsound.h"
18 #include "mpglib_sdlsound.h"
19 
20  /* old WRITE_SAMPLE */
21 #define WRITE_SAMPLE(samples,sum,clip) \
22   if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
23   else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
24   else { *(samples) = sum; }
25 
synth_1to1_mono(real * bandPtr,unsigned char * samples,int * pnt,struct mpstr * mp)26 int synth_1to1_mono(real *bandPtr,unsigned char *samples,
27                     int *pnt, struct mpstr *mp)
28 {
29   short samples_tmp[64];
30   short *tmp1 = samples_tmp;
31   int i,ret;
32   int pnt1 = 0;
33 
34   ret = synth_1to1(bandPtr,0,(unsigned char *) samples_tmp,&pnt1,mp);
35   samples += *pnt;
36 
37   for(i=0;i<32;i++) {
38     *( (short *) samples) = *tmp1;
39     samples += 2;
40     tmp1 += 2;
41   }
42   *pnt += 64;
43 
44   return ret;
45 }
46 
47 
synth_1to1(real * bandPtr,int channel,unsigned char * out,int * pnt,struct mpstr * mp)48 int synth_1to1(real *bandPtr,int channel,unsigned char *out,
49                int *pnt, struct mpstr *mp)
50 {
51   static const int step = 2;
52   int bo;
53   short *samples = (short *) (out + *pnt);
54 
55   real *b0,(*buf)[0x110];
56   int clip = 0;
57   int bo1;
58 
59   bo = mp->synth_bo;
60 
61   if(!channel) {
62     bo--;
63     bo &= 0xf;
64     buf = mp->synth_buffs[0];
65   }
66   else {
67     samples++;
68     buf = mp->synth_buffs[1];
69   }
70 
71   if(bo & 0x1) {
72     b0 = buf[0];
73     bo1 = bo;
74     dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr);
75   }
76   else {
77     b0 = buf[1];
78     bo1 = bo+1;
79     dct64(buf[0]+bo,buf[1]+bo+1,bandPtr);
80   }
81 
82   mp->synth_bo = bo;
83 
84   {
85     register int j;
86     real *window = decwin + 16 - bo1;
87 
88     for (j=16;j;j--,b0+=0x10,window+=0x20,samples+=step)
89     {
90       real sum;
91       sum  = window[0x0] * b0[0x0];
92       sum -= window[0x1] * b0[0x1];
93       sum += window[0x2] * b0[0x2];
94       sum -= window[0x3] * b0[0x3];
95       sum += window[0x4] * b0[0x4];
96       sum -= window[0x5] * b0[0x5];
97       sum += window[0x6] * b0[0x6];
98       sum -= window[0x7] * b0[0x7];
99       sum += window[0x8] * b0[0x8];
100       sum -= window[0x9] * b0[0x9];
101       sum += window[0xA] * b0[0xA];
102       sum -= window[0xB] * b0[0xB];
103       sum += window[0xC] * b0[0xC];
104       sum -= window[0xD] * b0[0xD];
105       sum += window[0xE] * b0[0xE];
106       sum -= window[0xF] * b0[0xF];
107 
108       WRITE_SAMPLE(samples,sum,clip);
109     }
110 
111     {
112       real sum;
113       sum  = window[0x0] * b0[0x0];
114       sum += window[0x2] * b0[0x2];
115       sum += window[0x4] * b0[0x4];
116       sum += window[0x6] * b0[0x6];
117       sum += window[0x8] * b0[0x8];
118       sum += window[0xA] * b0[0xA];
119       sum += window[0xC] * b0[0xC];
120       sum += window[0xE] * b0[0xE];
121       WRITE_SAMPLE(samples,sum,clip);
122       b0-=0x10,window-=0x20,samples+=step;
123     }
124     window += bo1<<1;
125 
126     for (j=15;j;j--,b0-=0x10,window-=0x20,samples+=step)
127     {
128       real sum;
129       sum = -window[-0x1] * b0[0x0];
130       sum -= window[-0x2] * b0[0x1];
131       sum -= window[-0x3] * b0[0x2];
132       sum -= window[-0x4] * b0[0x3];
133       sum -= window[-0x5] * b0[0x4];
134       sum -= window[-0x6] * b0[0x5];
135       sum -= window[-0x7] * b0[0x6];
136       sum -= window[-0x8] * b0[0x7];
137       sum -= window[-0x9] * b0[0x8];
138       sum -= window[-0xA] * b0[0x9];
139       sum -= window[-0xB] * b0[0xA];
140       sum -= window[-0xC] * b0[0xB];
141       sum -= window[-0xD] * b0[0xC];
142       sum -= window[-0xE] * b0[0xD];
143       sum -= window[-0xF] * b0[0xE];
144       sum -= window[-0x0] * b0[0xF];
145 
146       WRITE_SAMPLE(samples,sum,clip);
147     }
148   }
149   *pnt += 128;
150 
151   return clip;
152 }
153 
154