~ubuntu-branches/ubuntu/lucid/mpg123/lucid

« back to all changes in this revision

Viewing changes to src/decode_2to1.c

Tags: upstream-0.60
ImportĀ upstreamĀ versionĀ 0.60

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        decode_2to1.c: ...with 2to1 downsampling
 
3
 
 
4
        copyright 1995-2006 by the mpg123 project - free software under the terms of the LGPL 2.1
 
5
        see COPYING and AUTHORS files in distribution or http://mpg123.de
 
6
        initially written by Michael Hipp
 
7
*/
 
8
 
 
9
#include <stdlib.h>
 
10
#include <math.h>
 
11
#include <string.h>
 
12
 
 
13
#include "config.h"
 
14
#include "mpg123.h"
 
15
 
 
16
#define WRITE_SAMPLE(samples,sum,clip) \
 
17
  if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
 
18
  else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
 
19
  else { *(samples) = sum; }
 
20
 
 
21
int synth_2to1_8bit(real *bandPtr,int channel,unsigned char *samples,int *pnt)
 
22
{
 
23
  short samples_tmp[32];
 
24
  short *tmp1 = samples_tmp + channel;
 
25
  int i,ret;
 
26
  int pnt1 = 0;
 
27
 
 
28
  ret = synth_2to1(bandPtr,channel,(unsigned char *) samples_tmp,&pnt1);
 
29
  samples += channel + *pnt;
 
30
 
 
31
  for(i=0;i<16;i++) {
 
32
    *samples = conv16to8[*tmp1>>AUSHIFT];
 
33
    samples += 2;
 
34
    tmp1 += 2;
 
35
  }
 
36
  *pnt += 32;
 
37
 
 
38
  return ret;
 
39
}
 
40
 
 
41
int synth_2to1_8bit_mono(real *bandPtr,unsigned char *samples,int *pnt)
 
42
{
 
43
  short samples_tmp[32];
 
44
  short *tmp1 = samples_tmp;
 
45
  int i,ret;
 
46
  int pnt1 = 0;
 
47
 
 
48
  ret = synth_2to1(bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
 
49
  samples += *pnt;
 
50
 
 
51
  for(i=0;i<16;i++) {
 
52
    *samples++ = conv16to8[*tmp1>>AUSHIFT];
 
53
    tmp1 += 2;
 
54
  }
 
55
  *pnt += 16;
 
56
 
 
57
  return ret;
 
58
}
 
59
 
 
60
 
 
61
int synth_2to1_8bit_mono2stereo(real *bandPtr,unsigned char *samples,int *pnt)
 
62
{
 
63
  short samples_tmp[32];
 
64
  short *tmp1 = samples_tmp;
 
65
  int i,ret;
 
66
  int pnt1 = 0;
 
67
 
 
68
  ret = synth_2to1(bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
 
69
  samples += *pnt;
 
70
 
 
71
  for(i=0;i<16;i++) {
 
72
    *samples++ = conv16to8[*tmp1>>AUSHIFT];
 
73
    *samples++ = conv16to8[*tmp1>>AUSHIFT];
 
74
    tmp1 += 2;
 
75
  }
 
76
  *pnt += 32;
 
77
 
 
78
  return ret;
 
79
}
 
80
 
 
81
int synth_2to1_mono(real *bandPtr,unsigned char *samples,int *pnt)
 
82
{
 
83
  short samples_tmp[32];
 
84
  short *tmp1 = samples_tmp;
 
85
  int i,ret;
 
86
  int pnt1=0;
 
87
 
 
88
  ret = synth_2to1(bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
 
89
  samples += *pnt;
 
90
 
 
91
  for(i=0;i<16;i++) {
 
92
    *( (short *) samples) = *tmp1;
 
93
    samples += 2;
 
94
    tmp1 += 2;
 
95
  }
 
96
  *pnt += 32;
 
97
 
 
98
  return ret;
 
99
}
 
100
 
 
101
int synth_2to1_mono2stereo(real *bandPtr,unsigned char *samples,int *pnt)
 
102
{
 
103
  int i,ret;
 
104
 
 
105
  ret = synth_2to1(bandPtr,0,samples,pnt);
 
106
  samples = samples + *pnt - 64;
 
107
 
 
108
  for(i=0;i<16;i++) {
 
109
    ((short *)samples)[1] = ((short *)samples)[0];
 
110
    samples+=4;
 
111
  }
 
112
  
 
113
  return ret;
 
114
}
 
115
 
 
116
int synth_2to1(real *bandPtr,int channel,unsigned char *out,int *pnt)
 
117
{
 
118
  static real buffs[2][2][0x110];
 
119
  static const int step = 2;
 
120
  static int bo = 1;
 
121
  short *samples = (short *) (out + *pnt);
 
122
 
 
123
  real *b0,(*buf)[0x110];
 
124
  int clip = 0; 
 
125
  int bo1;
 
126
 
 
127
  if(have_eq_settings)
 
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
  *pnt += 64;
 
227
 
 
228
  return clip;
 
229
}
 
230
 
 
231