source: golgotha/src/i4/loaders/mp3/decode_4to1.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 5.7 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9/*
10 * Mpeg Layer-1,2,3 audio decoder
11 * ------------------------------
12 * copyright (c) 1995,1996,1997 by Michael Hipp, All rights reserved.
13 * See also 'README'
14 * version for slower machines .. decodes only every fourth sample
15 * dunno why it sounds THIS annoying (maybe we should adapt the window?)
16 * absolutely not optimized for this operation
17 */
18
19#include <stdlib.h>
20#include <math.h>
21#include <string.h>
22
23#include "loaders/mp3/mpg123.hh"
24
25#define WRITE_SAMPLE(samples,sum,clip) \
26  if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
27  else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
28  else { *(samples) = (short) sum; }
29
30static int bo = 1;
31
32void init_decode_4to1(void)
33{
34        bo = 1;
35}
36
37int synth_4to1_8bit(real *bandPtr,int channel,unsigned char *samples)
38{
39  short samples_tmp[16];
40  short *tmp1 = samples_tmp + channel;
41  int i,ret;
42
43  samples += channel;
44  ret = synth_4to1(bandPtr,channel,(unsigned char *) samples_tmp);
45
46  for(i=0;i<8;i++) {
47    *samples = conv16to8[*tmp1>>4];
48    samples += 2;
49    tmp1 += 2;
50  }
51
52  return ret;
53}
54
55int synth_4to1_8bit_mono(real *bandPtr,unsigned char *samples)
56{
57  short samples_tmp[16];
58  short *tmp1 = samples_tmp;
59  int i,ret;
60
61  ret = synth_4to1(bandPtr,0,(unsigned char *) samples_tmp);
62
63  for(i=0;i<8;i++) {
64    *samples++ = conv16to8[*tmp1>>4];
65    tmp1 += 2;
66  }
67
68  return ret;
69}
70
71
72int synth_4to1_8bit_mono2stereo(real *bandPtr,unsigned char *samples)
73{
74  short samples_tmp[16];
75  short *tmp1 = samples_tmp;
76  int i,ret;
77
78  ret = synth_4to1(bandPtr,0,(unsigned char *) samples_tmp);
79
80  for(i=0;i<8;i++) {
81    *samples++ = conv16to8[*tmp1>>4];
82    *samples++ = conv16to8[*tmp1>>4];
83    tmp1 += 2;
84  }
85
86  return ret;
87}
88
89int synth_4to1_mono(real *bandPtr,unsigned char *samples)
90{
91  short samples_tmp[16];
92  short *tmp1 = samples_tmp;
93  int i,ret;
94
95  ret = synth_4to1(bandPtr,0,(unsigned char *) samples_tmp);
96
97  for(i=0;i<8;i++) {
98    *( (short *)samples) = *tmp1;
99    samples+=2;
100    tmp1 += 2;
101  }
102
103  return ret;
104}
105
106int synth_4to1_mono2stereo(real *bandPtr,unsigned char *samples)
107{
108  int i,ret = synth_4to1(bandPtr,0,samples);
109  for(i=0;i<8;i++) {
110    ((short *)samples)[1] = ((short *)samples)[0];
111    samples+=4;
112  }
113  return ret;
114}
115
116int synth_4to1(real *bandPtr,int channel,unsigned char *out)
117{
118  static real buffs[2][2][0x110];
119  static const int step = 2;
120  short *samples = (short *) out;
121
122  real *b0,(*buf)[0x110];
123  int clip = 0;
124  int bo1;
125
126  if(flags.equalizer)
127    do_equalizer(bandPtr,channel);
128
129  if(!channel) {
130    bo--;
131    bo &= 0xf;
132    buf = buffs[0];
133  }
134  else {
135    samples++;
136    buf = buffs[1];
137  }
138
139  if(bo & 0x1) {
140    b0 = buf[0];
141    bo1 = bo;
142    dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr);
143  }
144  else {
145    b0 = buf[1];
146    bo1 = bo+1;
147    dct64(buf[0]+bo,buf[1]+bo+1,bandPtr);
148  }
149
150  {
151    register int j;
152    real *window = decwin + 16 - bo1;
153
154    for (j=4;j;j--,b0+=0x30,window+=0x70)
155    {
156      real sum;
157      sum  = *window++ * *b0++;
158      sum -= *window++ * *b0++;
159      sum += *window++ * *b0++;
160      sum -= *window++ * *b0++;
161      sum += *window++ * *b0++;
162      sum -= *window++ * *b0++;
163      sum += *window++ * *b0++;
164      sum -= *window++ * *b0++;
165      sum += *window++ * *b0++;
166      sum -= *window++ * *b0++;
167      sum += *window++ * *b0++;
168      sum -= *window++ * *b0++;
169      sum += *window++ * *b0++;
170      sum -= *window++ * *b0++;
171      sum += *window++ * *b0++;
172      sum -= *window++ * *b0++;
173
174      WRITE_SAMPLE(samples,sum,clip); samples += step;
175#if 0
176      WRITE_SAMPLE(samples,sum,clip); samples += step;
177      WRITE_SAMPLE(samples,sum,clip); samples += step;
178      WRITE_SAMPLE(samples,sum,clip); samples += step;
179#endif
180    }
181
182    {
183      real sum;
184      sum  = window[0x0] * b0[0x0];
185      sum += window[0x2] * b0[0x2];
186      sum += window[0x4] * b0[0x4];
187      sum += window[0x6] * b0[0x6];
188      sum += window[0x8] * b0[0x8];
189      sum += window[0xA] * b0[0xA];
190      sum += window[0xC] * b0[0xC];
191      sum += window[0xE] * b0[0xE];
192      WRITE_SAMPLE(samples,sum,clip); samples += step;
193#if 0
194      WRITE_SAMPLE(samples,sum,clip); samples += step;
195      WRITE_SAMPLE(samples,sum,clip); samples += step;
196      WRITE_SAMPLE(samples,sum,clip); samples += step;
197#endif
198      b0-=0x40,window-=0x80;
199    }
200    window += bo1<<1;
201
202    for (j=3;j;j--,b0-=0x50,window-=0x70)
203    {
204      real sum;
205      sum = -*(--window) * *b0++;
206      sum -= *(--window) * *b0++;
207      sum -= *(--window) * *b0++;
208      sum -= *(--window) * *b0++;
209      sum -= *(--window) * *b0++;
210      sum -= *(--window) * *b0++;
211      sum -= *(--window) * *b0++;
212      sum -= *(--window) * *b0++;
213      sum -= *(--window) * *b0++;
214      sum -= *(--window) * *b0++;
215      sum -= *(--window) * *b0++;
216      sum -= *(--window) * *b0++;
217      sum -= *(--window) * *b0++;
218      sum -= *(--window) * *b0++;
219      sum -= *(--window) * *b0++;
220      sum -= *(--window) * *b0++;
221
222      WRITE_SAMPLE(samples,sum,clip); samples += step;
223#if 0
224      WRITE_SAMPLE(samples,sum,clip); samples += step;
225      WRITE_SAMPLE(samples,sum,clip); samples += step;
226      WRITE_SAMPLE(samples,sum,clip); samples += step;
227#endif
228    }
229  }
230
231  return clip;
232}
233
234
Note: See TracBrowser for help on using the repository browser.