Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 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
 * version for slower machines .. decodes only every fourth sample
7
 * dunno why it sounds THIS annoying (maybe we should adapt the window?)
8
 * absolutely not optimized for this operation
9
 */
10
 
11
#include <stdlib.h>
12
#include <math.h>
13
#include <string.h>
14
 
15
#include "mpg123.h"
16
 
17
#define WRITE_SAMPLE(samples,sum,clip) \
18
  if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
19
  else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
20
  else { *(samples) = sum; }
21
 
22
int synth_4to1_8bit(real *bandPtr,int channel,unsigned char *samples)
23
{
24
  short samples_tmp[16];
25
  short *tmp1 = samples_tmp + channel;
26
  int i,ret;
27
 
28
  samples += channel;
29
  ret = synth_4to1(bandPtr,channel,(unsigned char *) samples_tmp);
30
 
31
  for(i=0;i<8;i++) {
32
    *samples = conv16to8[*tmp1>>4];
33
    samples += 2;
34
    tmp1 += 2;
35
  }
36
 
37
  return ret;
38
}
39
 
40
int synth_4to1_8bit_mono(real *bandPtr,unsigned char *samples)
41
{
42
  short samples_tmp[16];
43
  short *tmp1 = samples_tmp;
44
  int i,ret;
45
 
46
  ret = synth_4to1(bandPtr,0,(unsigned char *) samples_tmp);
47
 
48
  for(i=0;i<8;i++) {
49
    *samples++ = conv16to8[*tmp1>>4];
50
    *samples++ = conv16to8[*tmp1>>4];
51
    tmp1 += 2;
52
  }
53
 
54
  return ret;
55
}
56
 
57
int synth_4to1_mono(real *bandPtr,unsigned char *samples)
58
{
59
  int i,ret = synth_4to1(bandPtr,0,samples);
60
  for(i=0;i<8;i++) {
61
    ((short *)samples)[1] = ((short *)samples)[0];
62
    samples+=4;
63
  }
64
  return ret;
65
}
66
 
67
int synth_4to1(real *bandPtr,int channel,unsigned char *out)
68
{
69
  static real buffs[2][2][0x110];
70
  static const int step = 2;
71
  static int bo = 1;
72
  short *samples = (short *) out;
73
 
74
  real *b0,(*buf)[0x110];
75
  int clip = 0;
76
  int bo1;
77
 
78
  if(!channel) {
79
    bo--;
80
    bo &= 0xf;
81
    buf = buffs[0];
82
  }
83
  else {
84
    samples++;
85
    buf = buffs[1];
86
  }
87
 
88
  if(bo & 0x1) {
89
    b0 = buf[0];
90
    bo1 = bo;
91
    dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr);
92
  }
93
  else {
94
    b0 = buf[1];
95
    bo1 = bo+1;
96
    dct64(buf[0]+bo,buf[1]+bo+1,bandPtr);
97
  }
98
 
99
  {
100
    register int j;
101
    real *window = decwin + 16 - bo1;
102
 
103
    for (j=4;j;j--,b0+=0x30,window+=0x70)
104
    {
105
      real sum;
106
      sum  = *window++ * *b0++;
107
      sum -= *window++ * *b0++;
108
      sum += *window++ * *b0++;
109
      sum -= *window++ * *b0++;
110
      sum += *window++ * *b0++;
111
      sum -= *window++ * *b0++;
112
      sum += *window++ * *b0++;
113
      sum -= *window++ * *b0++;
114
      sum += *window++ * *b0++;
115
      sum -= *window++ * *b0++;
116
      sum += *window++ * *b0++;
117
      sum -= *window++ * *b0++;
118
      sum += *window++ * *b0++;
119
      sum -= *window++ * *b0++;
120
      sum += *window++ * *b0++;
121
      sum -= *window++ * *b0++;
122
 
123
      WRITE_SAMPLE(samples,sum,clip); samples += step;
124
#if 0
125
      WRITE_SAMPLE(samples,sum,clip); samples += step;
126
      WRITE_SAMPLE(samples,sum,clip); samples += step;
127
      WRITE_SAMPLE(samples,sum,clip); samples += step;
128
#endif
129
    }
130
 
131
    {
132
      real sum;
133
      sum  = window[0x0] * b0[0x0];
134
      sum += window[0x2] * b0[0x2];
135
      sum += window[0x4] * b0[0x4];
136
      sum += window[0x6] * b0[0x6];
137
      sum += window[0x8] * b0[0x8];
138
      sum += window[0xA] * b0[0xA];
139
      sum += window[0xC] * b0[0xC];
140
      sum += window[0xE] * b0[0xE];
141
      WRITE_SAMPLE(samples,sum,clip); samples += step;
142
#if 0
143
      WRITE_SAMPLE(samples,sum,clip); samples += step;
144
      WRITE_SAMPLE(samples,sum,clip); samples += step;
145
      WRITE_SAMPLE(samples,sum,clip); samples += step;
146
#endif
147
      b0-=0x40,window-=0x80;
148
    }
149
    window += bo1<<1;
150
 
151
    for (j=3;j;j--,b0-=0x50,window-=0x70)
152
    {
153
      real sum;
154
      sum = -*(--window) * *b0++;
155
      sum -= *(--window) * *b0++;
156
      sum -= *(--window) * *b0++;
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
 
171
      WRITE_SAMPLE(samples,sum,clip); samples += step;
172
#if 0
173
      WRITE_SAMPLE(samples,sum,clip); samples += step;
174
      WRITE_SAMPLE(samples,sum,clip); samples += step;
175
      WRITE_SAMPLE(samples,sum,clip); samples += step;
176
#endif
177
    }
178
  }
179
 
180
  return clip;
181
}
182
 
183