392176a122b3ab3e746653367bc4c7c89a290558
[reactos.git] / reactos / dll / win32 / winemp3.acm / layer1.c
1 /*
2 * Mpeg Layer-1 audio decoder
3 * --------------------------
4 * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README'
5 * This file has been copied from mpglib.
6 * near unoptimzed ...
7 *
8 * may have a few bugs after last optimization ...
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25 #include "mpg123.h"
26
27 static void I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],struct frame *fr)
28 {
29 unsigned int *ba=balloc;
30 unsigned int *sca = (unsigned int *) scale_index;
31
32 if(fr->stereo) {
33 int i;
34 int jsbound = fr->jsbound;
35 for (i=0;i<jsbound;i++) {
36 *ba++ = getbits(4);
37 *ba++ = getbits(4);
38 }
39 for (i=jsbound;i<SBLIMIT;i++)
40 *ba++ = getbits(4);
41
42 ba = balloc;
43
44 for (i=0;i<jsbound;i++) {
45 if ((*ba++))
46 *sca++ = getbits(6);
47 if ((*ba++))
48 *sca++ = getbits(6);
49 }
50 for (i=jsbound;i<SBLIMIT;i++)
51 if ((*ba++)) {
52 *sca++ = getbits(6);
53 *sca++ = getbits(6);
54 }
55 }
56 else {
57 int i;
58 for (i=0;i<SBLIMIT;i++)
59 *ba++ = getbits(4);
60 ba = balloc;
61 for (i=0;i<SBLIMIT;i++)
62 if ((*ba++))
63 *sca++ = getbits(6);
64 }
65 }
66
67 static void I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2*SBLIMIT],
68 unsigned int scale_index[2][SBLIMIT],struct frame *fr)
69 {
70 int i,n;
71 int smpb[2*SBLIMIT]; /* values: 0-65535 */
72 int *sample;
73 register unsigned int *ba;
74 register unsigned int *sca = (unsigned int *) scale_index;
75
76 if(fr->stereo) {
77 int jsbound = fr->jsbound;
78 register real *f0 = fraction[0];
79 register real *f1 = fraction[1];
80 ba = balloc;
81 for (sample=smpb,i=0;i<jsbound;i++) {
82 if ((n = *ba++))
83 *sample++ = getbits(n+1);
84 if ((n = *ba++))
85 *sample++ = getbits(n+1);
86 }
87 for (i=jsbound;i<SBLIMIT;i++)
88 if ((n = *ba++))
89 *sample++ = getbits(n+1);
90
91 ba = balloc;
92 for (sample=smpb,i=0;i<jsbound;i++) {
93 if((n=*ba++))
94 *f0++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
95 else
96 *f0++ = 0.0;
97 if((n=*ba++))
98 *f1++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
99 else
100 *f1++ = 0.0;
101 }
102 for (i=jsbound;i<SBLIMIT;i++) {
103 if ((n=*ba++)) {
104 real samp = ( ((-1)<<n) + (*sample++) + 1);
105 *f0++ = samp * muls[n+1][*sca++];
106 *f1++ = samp * muls[n+1][*sca++];
107 }
108 else
109 *f0++ = *f1++ = 0.0;
110 }
111 }
112 else {
113 register real *f0 = fraction[0];
114 ba = balloc;
115 for (sample=smpb,i=0;i<SBLIMIT;i++)
116 if ((n = *ba++))
117 *sample++ = getbits(n+1);
118 ba = balloc;
119 for (sample=smpb,i=0;i<SBLIMIT;i++) {
120 if((n=*ba++))
121 *f0++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
122 else
123 *f0++ = 0.0;
124 }
125 }
126 }
127
128 int do_layer1(struct frame *fr,unsigned char *pcm_sample,int *pcm_point)
129 {
130 int clip=0;
131 int i,stereo = fr->stereo;
132 unsigned int balloc[2*SBLIMIT];
133 unsigned int scale_index[2][SBLIMIT];
134 real fraction[2][SBLIMIT];
135 int single = fr->single;
136
137 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32;
138
139 if(stereo == 1 || single == 3)
140 single = 0;
141
142 I_step_one(balloc,scale_index,fr);
143
144 for (i=0;i<SCALE_BLOCK;i++)
145 {
146 I_step_two(fraction,balloc,scale_index,fr);
147
148 if(single >= 0) {
149 clip += synth_1to1_mono(fr->mp,fraction[single],pcm_sample,pcm_point);
150 }
151 else {
152 int p1 = *pcm_point;
153 clip += synth_1to1(fr->mp,fraction[0],0,pcm_sample,&p1);
154 clip += synth_1to1(fr->mp,fraction[1],1,pcm_sample,pcm_point);
155 }
156 }
157
158 return clip;
159 }