source: golgotha/src/i4/loaders/mp3/decode_2to1.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.4 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 by Michael Hipp, All rights reserved. See also 'README'
13 * version for slower machines .. decodes only every second sample
14 * sounds like 24000,22050 or 16000 kHz .. (depending on original sample freq.)
15 *
16 */
17
18#include <stdlib.h>
19#include <math.h>
20#include <string.h>
21
22#include "loaders/mp3/mpg123.hh"
23
24#define WRITE_SAMPLE(samples,sum,clip) \
25  if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
26  else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
27  else { *(samples) = (short) sum; }
28
29static int bo = 1;
30
31void init_decode_2to1(void)
32{
33        bo = 1;
34}
35
36int synth_2to1_8bit(real *bandPtr,int channel,unsigned char *samples)
37{
38  short samples_tmp[32];
39  short *tmp1 = samples_tmp + channel;
40  int i,ret;
41
42  samples += channel;
43  ret = synth_2to1(bandPtr,channel,(unsigned char *) samples_tmp);
44
45  for(i=0;i<16;i++) {
46    *samples = conv16to8[*tmp1>>4];
47    samples += 2;
48    tmp1 += 2;
49  }
50
51  return ret;
52}
53
54int synth_2to1_8bit_mono(real *bandPtr,unsigned char *samples)
55{
56  short samples_tmp[32];
57  short *tmp1 = samples_tmp;
58  int i,ret;
59
60  ret = synth_2to1(bandPtr,0,(unsigned char *) samples_tmp);
61
62  for(i=0;i<16;i++) {
63    *samples++ = conv16to8[*tmp1>>4];
64    tmp1 += 2;
65  }
66
67  return ret;
68}
69
70
71int synth_2to1_8bit_mono2stereo(real *bandPtr,unsigned char *samples)
72{
73  short samples_tmp[32];
74  short *tmp1 = samples_tmp;
75  int i,ret;
76
77  ret = synth_2to1(bandPtr,0,(unsigned char *) samples_tmp);
78
79  for(i=0;i<16;i++) {
80    *samples++ = conv16to8[*tmp1>>4];
81    *samples++ = conv16to8[*tmp1>>4];
82    tmp1 += 2;
83  }
84
85  return ret;
86}
87
88int synth_2to1_mono(real *bandPtr,unsigned char *samples)
89{
90  short samples_tmp[32];
91  short *tmp1 = samples_tmp;
92  int i,ret;
93
94  ret = synth_2to1(bandPtr,0,(unsigned char *) samples_tmp);
95
96  for(i=0;i<16;i++)
97  {
98    *( (short *) samples) = *tmp1;
99    samples+=2;
100
101    tmp1 += 2;
102  }
103
104  return ret;
105}
106
107int synth_2to1_mono2stereo(real *bandPtr,unsigned char *samples)
108{
109  int i,ret = synth_2to1(bandPtr,0,samples);
110  for(i=0;i<16;i++) {
111    ((short *)samples)[1] = ((short *)samples)[0];
112    samples+=4;
113  }
114  return ret;
115}
116
117int synth_2to1(real *bandPtr,int channel,unsigned char *out)
118{
119  static real buffs[2][2][0x110];
120  static const int step = 2;
121  short *samples = (short *) out;
122
123  real *b0,(*buf)[0x110];
124  int clip = 0;
125  int bo1;
126
127  if(flags.equalizer)
128    do_equalizer(bandPtr,channel);
129
130  if(!channel) {
131    bo--;
132    bo &= 0xf;
133    buf = buffs[0];
134  }
135  else {
136    samples++;
137    buf = buffs[1];
138  }
139
140  if(bo & 0x1) {
141    b0 = buf[0];
142    bo1 = bo;
143    dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr);
144  }
145  else {
146    b0 = buf[1];
147    bo1 = bo+1;
148    dct64(buf[0]+bo,buf[1]+bo+1,bandPtr);
149  }
150
151  {
152    register int j;
153    real *window = decwin + 16 - bo1;
154
155    for (j=8;j;j--,b0+=0x10,window+=0x30)
156    {
157      real sum;
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      sum -= *window++ * *b0++;
174
175      WRITE_SAMPLE(samples,sum,clip); samples += step;
176#if 0
177      WRITE_SAMPLE(samples,sum,clip); samples += step;
178#endif
179    }
180
181    {
182      real sum;
183      sum  = window[0x0] * b0[0x0];
184      sum += window[0x2] * b0[0x2];
185      sum += window[0x4] * b0[0x4];
186      sum += window[0x6] * b0[0x6];
187      sum += window[0x8] * b0[0x8];
188      sum += window[0xA] * b0[0xA];
189      sum += window[0xC] * b0[0xC];
190      sum += window[0xE] * b0[0xE];
191      WRITE_SAMPLE(samples,sum,clip); samples += step;
192#if 0
193      WRITE_SAMPLE(samples,sum,clip); samples += step;
194#endif
195      b0-=0x20,window-=0x40;
196    }
197    window += bo1<<1;
198
199    for (j=7;j;j--,b0-=0x30,window-=0x30)
200    {
201      real sum;
202      sum = -*(--window) * *b0++;
203      sum -= *(--window) * *b0++;
204      sum -= *(--window) * *b0++;
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
219      WRITE_SAMPLE(samples,sum,clip); samples += step;
220#if 0
221      WRITE_SAMPLE(samples,sum,clip); samples += step;
222#endif
223    }
224  }
225
226  return clip;
227}
228
229
Note: See TracBrowser for help on using the repository browser.