source: golgotha/src/i4/loaders/mp3/decode.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.0 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 *
15 */
16
17#include <stdlib.h>
18#include <math.h>
19#include <string.h>
20
21#include "loaders/mp3/mpg123.hh"
22
23static int bo = 1;
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
30void init_decode(void)
31{
32        bo = 1;
33}
34
35int synth_1to1_8bit(real *bandPtr,int channel,unsigned char *samples)
36{
37  short samples_tmp[64];
38  short *tmp1 = samples_tmp + channel;
39  int i,ret;
40
41  samples += channel;
42  ret = synth_1to1(bandPtr,channel,(unsigned char *) samples_tmp);
43
44  for(i=0;i<32;i++) {
45    *samples = conv16to8[*tmp1>>4];
46    samples += 2;
47    tmp1 += 2;
48  }
49
50  return ret;
51}
52
53int synth_1to1_8bit_mono(real *bandPtr,unsigned char *samples)
54{
55  short samples_tmp[64];
56  short *tmp1 = samples_tmp;
57  int i,ret;
58
59  ret = synth_1to1(bandPtr,0,(unsigned char *) samples_tmp);
60
61  for(i=0;i<32;i++) {
62    *samples++ = conv16to8[*tmp1>>4];
63    tmp1 += 2;
64  }
65 
66  return ret;
67}
68
69int synth_1to1_8bit_mono2stereo(real *bandPtr,unsigned char *samples)
70{
71  short samples_tmp[64];
72  short *tmp1 = samples_tmp;
73  int i,ret;
74
75  ret = synth_1to1(bandPtr,0,(unsigned char *) samples_tmp);
76
77  for(i=0;i<32;i++) {
78    *samples++ = conv16to8[*tmp1>>4];
79    *samples++ = conv16to8[*tmp1>>4];
80    tmp1 += 2;
81  }
82
83  return ret;
84}
85
86int synth_1to1_mono(real *bandPtr,unsigned char *samples)
87{
88  short samples_tmp[64];
89  short *tmp1 = samples_tmp;
90  int i,ret;
91
92  ret = synth_1to1(bandPtr,0,(unsigned char *) samples_tmp);
93
94  for(i=0;i<32;i++)
95  {
96    *( (short *)samples) = *tmp1;
97    samples=(unsigned char *) (((short *)samples)+1);
98    tmp1 += 2;
99  }
100
101  return ret;
102}
103
104
105int synth_1to1_mono2stereo(real *bandPtr,unsigned char *samples)
106{
107  int i,ret = synth_1to1(bandPtr,0,samples);
108  for(i=0;i<32;i++) {
109    ((short *)samples)[1] = ((short *)samples)[0];
110    samples+=4;
111  }
112  return ret;
113}
114
115
116int synth_1to1(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  {
152    register int j;
153    real *window = decwin + 16 - bo1;
154 
155    for (j=16;j;j--,window+=0x10,samples+=step)
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);
176    }
177
178    {
179      real sum;
180      sum  = window[0x0] * b0[0x0];
181      sum += window[0x2] * b0[0x2];
182      sum += window[0x4] * b0[0x4];
183      sum += window[0x6] * b0[0x6];
184      sum += window[0x8] * b0[0x8];
185      sum += window[0xA] * b0[0xA];
186      sum += window[0xC] * b0[0xC];
187      sum += window[0xE] * b0[0xE];
188      WRITE_SAMPLE(samples,sum,clip);
189      b0-=0x10,window-=0x20,samples+=step;
190    }
191    window += bo1<<1;
192
193    for (j=15;j;j--,b0-=0x20,window-=0x10,samples+=step)
194    {
195      real sum;
196      sum = -*(--window) * *b0++;
197      sum -= *(--window) * *b0++;
198      sum -= *(--window) * *b0++;
199      sum -= *(--window) * *b0++;
200      sum -= *(--window) * *b0++;
201      sum -= *(--window) * *b0++;
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
213      WRITE_SAMPLE(samples,sum,clip);
214    }
215  }
216
217  return clip;
218}
219
220
Note: See TracBrowser for help on using the repository browser.