~ubuntu-branches/ubuntu/karmic/asterisk/karmic

« back to all changes in this revision

Viewing changes to codecs/codec_mp3_d.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2002-04-27 21:19:32 UTC
  • Revision ID: james.westby@ubuntu.com-20020427211932-kqaertc4bg7ss5mc
Tags: upstream-0.1.11
ImportĀ upstreamĀ versionĀ 0.1.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Asterisk -- A telephony toolkit for Linux.
 
3
 *
 
4
 * MP3 Decoder
 
5
 *
 
6
 * The MP3 code is from freeamp, which in turn is from xingmp3's release
 
7
 * which I can't seem to find anywhere
 
8
 * 
 
9
 * Copyright (C) 1999, Mark Spencer
 
10
 *
 
11
 * Mark Spencer <markster@linux-support.net>
 
12
 *
 
13
 * This program is free software, distributed under the terms of
 
14
 * the GNU General Public License
 
15
 */
 
16
 
 
17
#include <asterisk/translate.h>
 
18
#include <asterisk/module.h>
 
19
#include <asterisk/logger.h>
 
20
#include <asterisk/channel.h>
 
21
#include <pthread.h>
 
22
#include <fcntl.h>
 
23
#include <errno.h>
 
24
#include <stdlib.h>
 
25
#include <unistd.h>
 
26
#include <netinet/in.h>
 
27
#include <string.h>
 
28
#include <stdio.h>
 
29
 
 
30
#include "mp3/include/L3.h"
 
31
#include "mp3/include/mhead.h"
 
32
 
 
33
#include "mp3anal.h"
 
34
 
 
35
/* Sample frame data */
 
36
#include "mp3_slin_ex.h"
 
37
 
 
38
#define MAX_OUT_FRAME 320
 
39
 
 
40
#define MAX_FRAME_SIZE 1441
 
41
#define MAX_OUTPUT_LEN 2304
 
42
 
 
43
static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
 
44
static int localusecnt=0;
 
45
 
 
46
static char *tdesc = "MP3/PCM16 (signed linear) Translator (Decoder only)";
 
47
 
 
48
struct ast_translator_pvt {
 
49
        MPEG m;
 
50
        MPEG_HEAD head;
 
51
        DEC_INFO info;
 
52
        struct ast_frame f;
 
53
        /* Space to build offset */
 
54
        char offset[AST_FRIENDLY_OFFSET];
 
55
        /* Mini buffer */
 
56
        char outbuf[MAX_OUT_FRAME];
 
57
        /* Enough to store a full second */
 
58
        short buf[32000];
 
59
        /* Tail of signed linear stuff */
 
60
        int tail;
 
61
        /* Current bitrate */
 
62
        int bitrate;
 
63
        /* XXX What's forward? XXX */
 
64
        int forward;
 
65
        /* Have we called head info yet? */
 
66
        int init;
 
67
        int copy;
 
68
};
 
69
 
 
70
#define mp3_coder_pvt ast_translator_pvt
 
71
 
 
72
static struct ast_translator_pvt *mp3_new()
 
73
{
 
74
        struct mp3_coder_pvt *tmp;
 
75
        tmp = malloc(sizeof(struct mp3_coder_pvt));
 
76
        if (tmp) {
 
77
                tmp->init = 0;
 
78
                tmp->tail = 0;
 
79
                tmp->copy = -1;
 
80
                mpeg_init(&tmp->m);
 
81
        }
 
82
        return tmp;
 
83
}
 
84
 
 
85
static struct ast_frame *mp3tolin_sample()
 
86
{
 
87
        static struct ast_frame f;
 
88
        int size;
 
89
        if (mp3_badheader(mp3_slin_ex)) {
 
90
                ast_log(LOG_WARNING, "Bad MP3 sample??\n");
 
91
                return NULL;
 
92
        }
 
93
        size = mp3_framelen(mp3_slin_ex);
 
94
        if (size < 1) {
 
95
                ast_log(LOG_WARNING, "Failed to size??\n");
 
96
                return NULL;
 
97
        }
 
98
        f.frametype = AST_FRAME_VOICE;
 
99
        f.subclass = AST_FORMAT_MP3;
 
100
        f.data = mp3_slin_ex;
 
101
        f.datalen = sizeof(mp3_slin_ex);
 
102
        /* Dunno how long an mp3 frame is -- kinda irrelevant anyway */
 
103
        f.timelen = 30;
 
104
        f.mallocd = 0;
 
105
        f.offset = 0;
 
106
        f.src = __PRETTY_FUNCTION__;
 
107
        return &f;
 
108
}
 
109
 
 
110
static struct ast_frame *mp3tolin_frameout(struct ast_translator_pvt *tmp)
 
111
{
 
112
        if (!tmp->tail)
 
113
                return NULL;
 
114
        /* Signed linear is no particular frame size, so just send whatever
 
115
           we have in the buffer in one lump sum */
 
116
        tmp->f.frametype = AST_FRAME_VOICE;
 
117
        tmp->f.subclass = AST_FORMAT_SLINEAR;
 
118
        tmp->f.datalen = tmp->tail * 2;
 
119
        /* Assume 8000 Hz */
 
120
        tmp->f.timelen = tmp->tail / 8;
 
121
        tmp->f.mallocd = 0;
 
122
        tmp->f.offset = AST_FRIENDLY_OFFSET;
 
123
        tmp->f.src = __PRETTY_FUNCTION__;
 
124
        tmp->f.data = tmp->buf;
 
125
        /* Reset tail pointer */
 
126
        tmp->tail = 0;
 
127
 
 
128
#if 0
 
129
        /* Save a sample frame */
 
130
        {
 
131
                static int fd = -1;
 
132
                if (fd < 0) 
 
133
                        fd = open("mp3out.raw", O_WRONLY | O_CREAT | O_TRUNC, 0644);
 
134
                write(fd, tmp->f.data, tmp->f.datalen);
 
135
        }               
 
136
#endif
 
137
        return &tmp->f; 
 
138
}
 
139
 
 
140
static int mp3_init(struct ast_translator_pvt *tmp, int len)
 
141
{       
 
142
        if (!audio_decode_init(&tmp->m, &tmp->head, len,0,0,1 /* Convert to mono */,24000)) {
 
143
                ast_log(LOG_WARNING, "audio_decode_init() failed\n");
 
144
                return -1;
 
145
        }
 
146
        audio_decode_info(&tmp->m, &tmp->info);
 
147
#if 0
 
148
        ast_verbose(
 
149
"Channels: %d\nOutValues: %d\nSample Rate: %d\nBits: %d\nFramebytes: %d\nType: %d\n",
 
150
        tmp->info.channels, tmp->info.outvalues, tmp->info.samprate, tmp->info.bits,tmp->info.framebytes,tmp->info.type);
 
151
#endif
 
152
        return 0;
 
153
}
 
154
 
 
155
#ifndef MIN
 
156
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
 
157
#endif
 
158
 
 
159
#if 1
 
160
static int add_to_buf(short *dst, int maxdst, short *src, int srclen, int samprate)
 
161
{
 
162
        float inc, cur, sum=0;
 
163
        int cnt=0, pos, ptr, lastpos = -1;
 
164
        /* Resample source to destination converting from its sampling rate to 8000 Hz */
 
165
        if (samprate == 8000) {
 
166
                /* Quickly, all we have to do is copy */
 
167
                memcpy(dst, src, 2 * MIN(maxdst, srclen));
 
168
                return MIN(maxdst, srclen);
 
169
        }
 
170
        if (samprate < 8000) {
 
171
                ast_log(LOG_WARNING, "Don't know how to resample a source less than 8000 Hz!\n");
 
172
                /* XXX Wrong thing to do XXX */
 
173
                memcpy(dst, src, 2 * MIN(maxdst, srclen));
 
174
                return MIN(maxdst, srclen);
 
175
        }
 
176
        /* Ugh, we actually *have* to resample */
 
177
        inc = 8000.0 / (float)samprate;
 
178
        cur = 0;
 
179
        ptr = 0;
 
180
        pos = 0;
 
181
#if 0
 
182
        ast_verbose("Incrementing by %f, in = %d bytes, out = %d bytes\n", inc, srclen, maxdst);
 
183
#endif
 
184
        while((pos < maxdst) && (ptr < srclen)) {
 
185
                if (pos != lastpos) {
 
186
                        if (lastpos > -1) {
 
187
                                sum = sum / (float)cnt;
 
188
                                dst[pos - 1] = (int) sum;
 
189
#if 0
 
190
                                ast_verbose("dst[%d] = %d\n", pos - 1, dst[pos - 1]);
 
191
#endif
 
192
                        }
 
193
                        /* Each time we have a first pass */
 
194
                        sum = 0;
 
195
                        cnt = 0;
 
196
                } else {
 
197
                        sum += src[ptr];
 
198
                }
 
199
                ptr++;
 
200
                cur += inc;
 
201
                cnt++;
 
202
                lastpos = pos;
 
203
                pos = (int)cur;
 
204
        }
 
205
        return pos;
 
206
}
 
207
#endif
 
208
 
 
209
static int mp3tolin_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
 
210
{
 
211
        /* Assuming there's space left, decode into the current buffer at
 
212
           the tail location */
 
213
        int framelen;
 
214
        short tmpbuf[8000];
 
215
        IN_OUT x;
 
216
#if 0
 
217
        if (tmp->copy < 0) {
 
218
                tmp->copy = open("sample.out", O_WRONLY | O_CREAT | O_TRUNC, 0700);
 
219
        }
 
220
        if (tmp->copy > -1)
 
221
                write(tmp->copy, f->data, f->datalen);
 
222
#endif
 
223
        /* Check if it's a valid frame */
 
224
        if (mp3_badheader((unsigned char *)f->data)) {
 
225
                ast_log(LOG_WARNING, "Invalid MP3 header\n");
 
226
                return -1;
 
227
        }
 
228
        if ((framelen = mp3_framelen((unsigned char *)f->data) != f->datalen)) {
 
229
                ast_log(LOG_WARNING, "Calculated length %d does not match real length %d\n", framelen, f->datalen);
 
230
                return -1;
 
231
        }
 
232
        /* Start by putting this in the mp3 buffer */
 
233
        if((framelen = head_info3(f->data, 
 
234
                        f->datalen, &tmp->head, &tmp->bitrate, &tmp->forward)) > 0) {
 
235
                if (!tmp->init) {
 
236
                        if (mp3_init(tmp, framelen))
 
237
                                return -1;
 
238
                        else
 
239
                                tmp->init++;
 
240
                }
 
241
                if (tmp->tail + MAX_OUTPUT_LEN/2  < sizeof(tmp->buf)/2) {       
 
242
                        x = audio_decode(&tmp->m, f->data, tmpbuf);
 
243
                        audio_decode_info(&tmp->m, &tmp->info);
 
244
                        if (!x.in_bytes) {
 
245
                                ast_log(LOG_WARNING, "Invalid MP3 data\n");
 
246
                        } else {
 
247
#if 1
 
248
                                /* Resample to 8000 Hz */
 
249
                                tmp->tail += add_to_buf(tmp->buf + tmp->tail, 
 
250
                                   sizeof(tmp->buf) / 2 - tmp->tail, 
 
251
                                           tmpbuf,
 
252
                                           x.out_bytes/2,
 
253
                                           tmp->info.samprate);
 
254
#else
 
255
                                memcpy(tmp->buf + tmp->tail, tmpbuf, x.out_bytes);
 
256
                                /* Signed linear output */
 
257
                                tmp->tail+=x.out_bytes/2;
 
258
#endif
 
259
                        }
 
260
                } else {
 
261
                        ast_log(LOG_WARNING, "Out of buffer space\n");
 
262
                        return -1;
 
263
                }
 
264
        } else {
 
265
                ast_log(LOG_WARNING, "Not a valid MP3 frame\n");
 
266
        }
 
267
        return 0;
 
268
}
 
269
 
 
270
static void mp3_destroy_stuff(struct ast_translator_pvt *pvt)
 
271
{
 
272
        close(pvt->copy);
 
273
        free(pvt);
 
274
}
 
275
 
 
276
static struct ast_translator mp3tolin =
 
277
        { "mp3tolin", 
 
278
           AST_FORMAT_MP3, AST_FORMAT_SLINEAR,
 
279
           mp3_new,
 
280
           mp3tolin_framein,
 
281
           mp3tolin_frameout,
 
282
           mp3_destroy_stuff,
 
283
           mp3tolin_sample
 
284
           };
 
285
 
 
286
int unload_module(void)
 
287
{
 
288
        int res;
 
289
        ast_pthread_mutex_lock(&localuser_lock);
 
290
        res = ast_unregister_translator(&mp3tolin);
 
291
        if (localusecnt)
 
292
                res = -1;
 
293
        ast_pthread_mutex_unlock(&localuser_lock);
 
294
        return res;
 
295
}
 
296
 
 
297
int load_module(void)
 
298
{
 
299
        int res;
 
300
        res=ast_register_translator(&mp3tolin);
 
301
        return res;
 
302
}
 
303
 
 
304
char *description(void)
 
305
{
 
306
        return tdesc;
 
307
}
 
308
 
 
309
int usecount(void)
 
310
{
 
311
        int res;
 
312
        STANDARD_USECOUNT(res);
 
313
        return res;
 
314
}
 
315
 
 
316
char *key()
 
317
{
 
318
        return ASTERISK_GPL_KEY;
 
319
}