File: converter.c

package info (click to toggle)
rat 4.2.20-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,888 kB
  • ctags: 3,655
  • sloc: ansic: 36,042; sh: 3,481; tcl: 2,740; makefile: 293
file content (299 lines) | stat: -rw-r--r-- 8,159 bytes parent folder | download
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
/*
 * FILE:    converter.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: converter.c,v 1.17 2001/01/17 23:35:38 ucacoxh Exp $";
#endif /* HIDE_SOURCE_STRINGS */

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

#define MAGIC 0xface0ff0

typedef struct s_converter {
        int                     idx;
        struct s_converter_fmt *cfmt;
        u_char                 *state;
        uint32_t                state_len;
        uint32_t                magic;
} converter_t;

typedef int  (*cv_startup)     (void);  /* converter specific one time initialization */
typedef void (*cv_shutdown)    (void);  /* converter specific one time cleanup */
typedef int  (*cv_conv_init_f) (const converter_fmt_t *c, u_char **state, uint32_t *state_len);
typedef void (*cv_conv_do_f)   (const converter_fmt_t *c, u_char *state, 
                                sample* src_buf, int src_len, 
                                sample *dst_buf, int dst_len);
typedef void (*cv_conv_free_f) (u_char **state, uint32_t *state_len);

typedef struct s_pcm_converter{
        converter_details_t details;
        u_char         enabled;
        cv_startup     startf;
        cv_shutdown    shutdownf;
        cv_conv_init_f initf;
        cv_conv_do_f   convertf;
        cv_conv_free_f freef;
} pcm_converter_t;

/* In this table of converters the platform specific converters should go at the
 * beginning, before the default (and worst) linear interpolation conversion.  The
 * intension is to have a mechanism which enables/disables more complex default schemes
 * such as interpolation with filtering, cubic interpolation, etc...
 */

#include "convert_acm.h"
#include "convert_extra.h"
#include "convert_linear.h"
#include "convert_sinc.h"

pcm_converter_t converter_tbl[] = {
#ifdef WIN32
        {
                {0, "Microsoft Converter"},
                FALSE, 
                acm_cv_startup, 
                acm_cv_shutdown, 
                acm_cv_create, 
                acm_cv_convert,  
                acm_cv_destroy 
        },
#endif
        {
                {1, "High Quality"},
                TRUE,
                sinc_startup,
                sinc_shutdown,
                sinc_create,
                sinc_convert,
                sinc_destroy
        },
        {
                {2, "Intermediate Quality"},
                TRUE,  
                NULL,
                NULL,
                linear_create,
                linear_convert,
                linear_destroy
        },
        {
                {3, "Low Quality"},
                TRUE,
                NULL,
                NULL,
                extra_create,
                extra_convert,
                extra_destroy
        }
};

#define NUM_CONVERTERS sizeof(converter_tbl)/sizeof(pcm_converter_t)

/* Index to converter_id_t mapping macros */
#define CONVERTER_ID_TO_IDX(x) (((x)>>2) - 17)
#define IDX_TO_CONVERTER_ID(x) ((x+17) << 2)

int 
converter_create(const converter_id_t   cid, 
                 const converter_fmt_t *cfmt,
                 converter_t          **cvtr)
{
        converter_t *c  = NULL;
        uint32_t      tbl_idx;
        
        tbl_idx = CONVERTER_ID_TO_IDX(cid);

        if (tbl_idx >= NUM_CONVERTERS) {
                debug_msg("Converter ID invalid\n");
                return FALSE;
        }

        if (cfmt == NULL) {
                debug_msg("No format specified\n");
                return FALSE;
        }
        
        c  = (converter_t*)xmalloc(sizeof(converter_t));
        if (c == NULL) {
                debug_msg("Could not allocate converter\n");
                return FALSE;
        }

        memset(c, 0, sizeof(converter_t));

        /* Copy format */
        c->cfmt = (converter_fmt_t*)xmalloc(sizeof(converter_fmt_t));
        if (c->cfmt == NULL) {
                converter_destroy(&c); 
                return FALSE;
        }
        memcpy(c->cfmt, cfmt, sizeof(converter_fmt_t));
        c->idx = tbl_idx;

        /* Initialize */
        if ((converter_tbl[tbl_idx].initf) && 
            (converter_tbl[tbl_idx].initf(cfmt, &c->state, &c->state_len) == FALSE)) {
		xfree(c->cfmt);
		xfree(c);
		debug_msg("Failed to create converter\n");
                return FALSE;
        }

        c->magic = MAGIC; /* debugging */
        *cvtr = c;
        
        xmemchk();
        return TRUE;
}

void 
converter_destroy(converter_t **cvtr)
{
        converter_t *c = *cvtr;

        if (c == NULL) {
                return;
        }

        assert(c->magic == MAGIC);

        if (converter_tbl[c->idx].freef && c->state != NULL) {
                converter_tbl[c->idx].freef(&c->state, &c->state_len);
        }

        if (c->cfmt) {
                xfree(c->cfmt);
        }

        xfree(c); 
        (*cvtr) = NULL;
}

void         
converters_init()
{
        uint32_t i = 0;

        for(i = 0; i < NUM_CONVERTERS; i++) {
                if (converter_tbl[i].startf) {
                        converter_tbl[i].enabled = converter_tbl[i].startf();
                }
                converter_tbl[i].details.id = IDX_TO_CONVERTER_ID(i);
        }
}

void
converters_free()
{
        uint32_t i = 0;

        for(i = 0; i < NUM_CONVERTERS; i++) {
                if (converter_tbl[i].shutdownf) {
                        converter_tbl[i].shutdownf();
                }
        }
}

const converter_details_t *
converter_get_details(uint32_t idx)
{
        if (idx < NUM_CONVERTERS) {
                return &converter_tbl[idx].details;
        }
        debug_msg("Getting invalid converter details\n");
        return NULL;
}

uint32_t 
converter_get_count()
{
        return NUM_CONVERTERS;
}

#include "codec_types.h"
#include "codec.h"

int
converter_process (converter_t *c, coded_unit *in, coded_unit *out)
{
        converter_fmt_t *cf;
        uint32_t        n_in, n_out;
	uint32_t	ticks_in, ticks_out;

        assert(c->magic == MAGIC);
#ifdef DEBUG
        {
                uint16_t sample_rate, channels;
                codec_get_native_info(in->id, &sample_rate, &channels);
                assert(sample_rate == c->cfmt->src_freq);
                assert(channels == c->cfmt->src_channels);
        }
#endif /* DEBUG */

        assert(c);
        assert(in->data != NULL);
        assert(in->data_len != 0);

        cf = c->cfmt;

	ticks_in = in->data_len / (sizeof(sample) * cf->src_channels);
	ticks_out = ticks_in * cf->dst_freq / cf->src_freq;

        n_in  = ticks_in  * cf->src_channels;
        n_out = ticks_out * cf->dst_channels;
	
        assert(converter_format_valid(cf));
        assert(out->state     == NULL);
        assert(out->state_len == 0);
        assert(out->data      == NULL);
        assert(out->data_len  == 0);

        out->id       = codec_get_native_coding(cf->dst_freq, cf->dst_channels);
        out->data_len = sizeof(sample) * n_out;
        out->data     = (u_char*)block_alloc(out->data_len);

        if ((c->cfmt->src_freq     != c->cfmt->dst_freq) ||
            (c->cfmt->src_channels != c->cfmt->dst_channels)) {
                converter_tbl[c->idx].convertf(c->cfmt,
                                               c->state,
                                               (sample*)in->data, 
                                               n_in,
                                               (sample*)out->data, 
                                               n_out);
        } else {
                /* No conversion is actually necessary */
                debug_msg("No conversion necessary\n");
                memcpy(out->data, in->data, out->data_len);
        }
        assert(c->magic == MAGIC);
        xmemchk();
        return TRUE;
}

const converter_fmt_t*
converter_get_format (converter_t *c)
{
        assert(c != NULL);
        assert(c->magic == MAGIC);
        return c->cfmt;
}