File: convert_linear.c

package info (click to toggle)
rat 4.2.22-2.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,896 kB
  • ctags: 3,717
  • sloc: ansic: 36,542; tcl: 2,740; sh: 2,675; makefile: 295
file content (308 lines) | stat: -rw-r--r-- 10,874 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * FILE:    convert_linear.c
 * PROGRAM: RAT
 * AUTHOR:  O.Hodson <O.Hodson@cs.ucl.ac.uk>
 *
 * Copyright (c) 1998-2001 University College London
 * All rights reserved.
 */
 
#ifndef HIDE_SOURCE_STRINGS
static const char cvsid[] = 
	"$Id: convert_linear.c,v 1.12 2001/01/18 00:24:23 ucacoxh Exp $";
#endif /* HIDE_SOURCE_STRINGS */

#include "config_unix.h"
#include "config_win32.h"
#include "audio_types.h"
#include "converter_types.h"
#include "convert_linear.h"
#include "convert_util.h"
#include "util.h"
#include "memory.h"
#include "debug.h"

/* Linear Interpolation Conversion *******************************************/

struct s_filter_state;
typedef void (*inter_cf)(int offset, int channels, sample *src, int src_len, sample *dst, int dst_len, struct s_filter_state *s);

typedef struct s_filter_state {
        int      scale;
        sample  *tmp_buf;
        int      tmp_len;
        sample  *last;
        int      last_len;
        inter_cf convert_f;
} filter_state_t;

typedef struct s_linear_state {
        int      	steps;
	filter_state_t	fs[2];
} linear_state_t;

static void 
linear_upsample(int offset, int channels, 
                sample *src, int src_len, 
                sample *dst, int dst_len, 
                filter_state_t *l)
{
        register int r, loop;
        register short *sp, *dp;
        short *last = l->last + offset;

        sp = src + offset;
        dp = dst + offset;
        
        loop = min(src_len/channels, dst_len/(channels*l->scale));

        /* On some platforms divisions by powers of 2 is way quicker */
        /* than other divisions.  To improve interpolation perf.     */
        /* approximate fractions which are not powers of two         */
        /* i.e. 1 / 3 ->  5 / 16                                     */
        /*      1 / 5 ->  6 / 32                                     */
        /*      1 / 6 -> 11 / 64                                     */

        switch (l->scale) {
        case 6:
                while(loop--) {
                        register int il, ic;
                        il = *last; ic = *sp;

                        r = 55 * il + 11 * ic; r /= 64; *dp = (sample)r; dp += channels;
                        r = 44 * il + 22 * ic; r /= 64; *dp = (sample)r; dp += channels;
                        r = 33 * il + 33 * ic; r /= 64; *dp = (sample)r; dp += channels;
                        r = 22 * il + 44 * ic; r /= 64; *dp = (sample)r; dp += channels;
                        r = 11 * il + 55 * ic; r /= 64; *dp = (sample)r; dp += channels;
                        r =           66 * ic; r /= 64; *dp = (sample)r; dp += channels;
                        last = sp;
                        sp += channels;
                }
                break;
        case 5:
                while(loop--) {
                        register int il, ic;
                        il = *last; ic = *sp;
                        r = 24 * il +  6 * ic; r /= 32; *dp = (sample)r; dp += channels;
                        r = 18 * il + 12 * ic; r /= 32; *dp = (sample)r; dp += channels;
                        r = 12 * il + 18 * ic; r /= 32; *dp = (sample)r; dp += channels;
                        r =  6 * il + 24 * ic; r /= 32; *dp = (sample)r; dp += channels;
                        r =           30 * ic; r /= 32; *dp = (sample)r; dp += channels;
                        last = sp;
                        sp  += channels;
                }
                break;
        case 4:
                while(loop--) {
                        register int il, ic;
                        il = *last; ic = *sp;
                        r = 3 * il + 1 * ic; r /= 4; *dp = (sample)r; dp += channels;
                        r = 2 * il + 2 * ic; r /= 4; *dp = (sample)r; dp += channels;
                        r = 1 * il + 3 * ic; r /= 4; *dp = (sample)r; dp += channels;
                        *dp = (sample)ic; dp += channels;
                        last = sp;
                        sp  += channels;
                }
                break;
        case 3:
                while(loop--) {
                        register int il, ic;
                        il = *last; ic = *sp;
                        r = 10 * il +  5 * ic; r /= 16; *dp = (sample)r; dp += channels;
                        r =  5 * il + 10 * ic; r /= 16; *dp = (sample)r; dp += channels;
                        r =           15 * ic; r /= 16; *dp = (sample)r; dp += channels;
                        last = sp;
                        sp  += channels;
                }
                break;
        case 2:
                while(loop--) {
                        register int il, ic;
                        il = *last; ic = *sp;
                        r = il + ic; r /= 2; *dp = (sample)r; dp += channels;
                        *dp = (sample)ic; dp += channels;
                        last = sp;
                        sp  += channels;
                }
                break;
        default:
                assert(0); /* Should never get here */
        }
        l->last[offset] = src[src_len - channels + offset];
}

static void
linear_downsample(int offset, int channels, 
                  sample *src, int src_len, 
                  sample *dst, int dst_len, 
                  filter_state_t *l)
{
        register int loop, r, c, lim;
        register short *sp, *dp;

        loop = min(src_len / (channels * l->scale), dst_len / channels);
        sp = src + offset;
        dp = dst + offset;
        lim = l->scale - 1;
        while(loop--) {
                r  = (int)*sp; sp+=channels;
                c  = lim;
                while(c--) {
                        r += (int)*sp; sp+=channels;
                } 
                r /= l->scale;
                *dp = (short)r;
                dp+= channels;
        }
}

static void
linear_init_state(filter_state_t *l, const converter_fmt_t *cfmt)
{
        if (cfmt->src_freq > cfmt->dst_freq) {
                l->scale = cfmt->src_freq / cfmt->dst_freq;
                l->last_len = 0;
                l->convert_f = linear_downsample;
        } else if (cfmt->src_freq < cfmt->dst_freq) {
                l->scale = cfmt->dst_freq / cfmt->src_freq;
                l->last_len = cfmt->src_channels;
                l->convert_f = linear_upsample;
                l->last = (sample*)xmalloc(sizeof(sample) * l->last_len);
                memset(l->last, 0, sizeof(sample) * cfmt->src_channels);
        }
}

int 
linear_create (const converter_fmt_t *cfmt, u_char **state, uint32_t *state_len)
{
	converter_fmt_t	sfmt, ufmt;
	linear_state_t	*l;
        int		g;

	if (((cfmt->src_freq % 8000) == 0 && (cfmt->dst_freq % 8000)) ||
	    ((cfmt->src_freq % 11025) == 0 && (cfmt->dst_freq % 11025))) {
		/* 11025 - 8000 not supported */
		return FALSE;
	}

	xmemchk();
        g = gcd(cfmt->src_freq, cfmt->dst_freq);        

	l = (linear_state_t*)xmalloc(sizeof(linear_state_t));
        memset(l, 0 , sizeof(linear_state_t));
        l->steps = conversion_steps(cfmt->src_freq, cfmt->dst_freq);
	xmemchk();

	sfmt = *cfmt;

	if (sfmt.src_channels != sfmt.dst_channels) {
		/* In addition to rate conversion, this requires
		 * channel number conversion, this can happen
		 * either side of rate conversion - see sinc_convert() */
		if (sfmt.src_channels == 2 && sfmt.dst_channels == 1) {
			sfmt.src_channels = 1; /* Stereo->Mono, R1->R2 */
		} 
	}

        switch(l->steps) {
        case 1:
                linear_init_state(l->fs, &sfmt);
                break;
        case 2:
		ufmt = sfmt;
		ufmt.dst_freq = g;
                linear_init_state(l->fs,     &ufmt);
		ufmt = sfmt;
		ufmt.src_freq = g;
                linear_init_state(l->fs + 1, &ufmt);
                break;
        }
	xmemchk();
        *state     = (u_char*)l;
        *state_len = sizeof(linear_state_t);
        return TRUE;
}

void
linear_convert (const converter_fmt_t *cfmt, 
                u_char *state, 
                sample* src_buf, int src_len, 
                sample *dst_buf, int dst_len)
{
        linear_state_t *l;
        int         channels, i;

        channels = cfmt->src_channels;

        l = (linear_state_t*)state;

        if (cfmt->src_channels == 2 && cfmt->dst_channels == 1) {
                /* stereo->mono then sample rate change */
                if (l->steps) {
                        /* inplace conversion needed */
                        converter_change_channels(src_buf, src_len, 2, src_buf, src_len / 2, 1); 
                        src_len /= 2;
                } else {
                        /* this is only conversion */
                        converter_change_channels(src_buf, src_len, 2, dst_buf, dst_len, 1);
                        return;
                }
                channels = 1;
        } else if (cfmt->src_channels == 1 && cfmt->dst_channels == 2) {
                dst_len /= 2;
        }
        
        switch(l->steps) {
        case 1:
                assert(l->fs->convert_f);
                for(i = 0; i < channels; i++) {
                        l->fs->convert_f(i, channels, src_buf, src_len, dst_buf, dst_len, l->fs);
                }
                break;
        case 2:
                /* first step is always downsampling for moment */
                if (l->fs->tmp_buf == NULL) {
                        l->fs->tmp_len  = src_len / l->fs->scale;
                        l->fs->tmp_buf = (sample*)xmalloc(sizeof(sample) * l->fs->tmp_len);
                }
                assert(l->fs[0].convert_f);
                assert(l->fs[1].convert_f);

                for(i = 0; i < channels; i++)
                        l->fs[0].convert_f(i, channels, src_buf, src_len, l->fs->tmp_buf, l->fs->tmp_len, l->fs);
                for(i = 0; i < channels; i++)
                        l->fs[1].convert_f(i, channels, l->fs->tmp_buf, l->fs->tmp_len, dst_buf, dst_len, l->fs + 1);
                break;
        }
        
        if (cfmt->src_channels == 1 && cfmt->dst_channels == 2) {
                /* sample rate change before mono-> stereo */
                if (l->steps) {
                        /* in place needed */
                        converter_change_channels(dst_buf, dst_len, 1, dst_buf, dst_len * 2, 2);
                } else {
                        /* this is our only conversion here */
                        converter_change_channels(src_buf, src_len, 1, dst_buf, dst_len * 2, 2);
                }
        }
}

void 
linear_destroy (u_char **state, uint32_t *state_len)
{
        linear_state_t *l = (linear_state_t*)*state;
        int i;

        assert(*state_len == sizeof(linear_state_t));
        
        for(i = 0; i < l->steps; i++) {
                if (l->fs[i].last)    xfree(l->fs[i].last);
                if (l->fs[i].tmp_buf) xfree(l->fs[i].tmp_buf);
        }
        xfree(l);
        *state     = NULL;
        *state_len = 0;
}