2
* Asterisk -- A telephony toolkit for Linux.
6
* The MP3 code is from freeamp, which in turn is from xingmp3's release
7
* which I can't seem to find anywhere
9
* Copyright (C) 1999, Mark Spencer
11
* Mark Spencer <markster@linux-support.net>
13
* This program is free software, distributed under the terms of
14
* the GNU General Public License
17
#include <asterisk/translate.h>
18
#include <asterisk/module.h>
19
#include <asterisk/logger.h>
20
#include <asterisk/channel.h>
26
#include <netinet/in.h>
30
#include "mp3/include/L3.h"
31
#include "mp3/include/mhead.h"
35
/* Sample frame data */
36
#include "mp3_slin_ex.h"
38
#define MAX_OUT_FRAME 320
40
#define MAX_FRAME_SIZE 1441
41
#define MAX_OUTPUT_LEN 2304
43
static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
44
static int localusecnt=0;
46
static char *tdesc = "MP3/PCM16 (signed linear) Translator (Decoder only)";
48
struct ast_translator_pvt {
53
/* Space to build offset */
54
char offset[AST_FRIENDLY_OFFSET];
56
char outbuf[MAX_OUT_FRAME];
57
/* Enough to store a full second */
59
/* Tail of signed linear stuff */
63
/* XXX What's forward? XXX */
65
/* Have we called head info yet? */
70
#define mp3_coder_pvt ast_translator_pvt
72
static struct ast_translator_pvt *mp3_new()
74
struct mp3_coder_pvt *tmp;
75
tmp = malloc(sizeof(struct mp3_coder_pvt));
85
static struct ast_frame *mp3tolin_sample()
87
static struct ast_frame f;
89
if (mp3_badheader(mp3_slin_ex)) {
90
ast_log(LOG_WARNING, "Bad MP3 sample??\n");
93
size = mp3_framelen(mp3_slin_ex);
95
ast_log(LOG_WARNING, "Failed to size??\n");
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 */
106
f.src = __PRETTY_FUNCTION__;
110
static struct ast_frame *mp3tolin_frameout(struct ast_translator_pvt *tmp)
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;
120
tmp->f.timelen = tmp->tail / 8;
122
tmp->f.offset = AST_FRIENDLY_OFFSET;
123
tmp->f.src = __PRETTY_FUNCTION__;
124
tmp->f.data = tmp->buf;
125
/* Reset tail pointer */
129
/* Save a sample frame */
133
fd = open("mp3out.raw", O_WRONLY | O_CREAT | O_TRUNC, 0644);
134
write(fd, tmp->f.data, tmp->f.datalen);
140
static int mp3_init(struct ast_translator_pvt *tmp, int len)
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");
146
audio_decode_info(&tmp->m, &tmp->info);
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);
156
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
160
static int add_to_buf(short *dst, int maxdst, short *src, int srclen, int samprate)
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);
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);
176
/* Ugh, we actually *have* to resample */
177
inc = 8000.0 / (float)samprate;
182
ast_verbose("Incrementing by %f, in = %d bytes, out = %d bytes\n", inc, srclen, maxdst);
184
while((pos < maxdst) && (ptr < srclen)) {
185
if (pos != lastpos) {
187
sum = sum / (float)cnt;
188
dst[pos - 1] = (int) sum;
190
ast_verbose("dst[%d] = %d\n", pos - 1, dst[pos - 1]);
193
/* Each time we have a first pass */
209
static int mp3tolin_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
211
/* Assuming there's space left, decode into the current buffer at
218
tmp->copy = open("sample.out", O_WRONLY | O_CREAT | O_TRUNC, 0700);
221
write(tmp->copy, f->data, f->datalen);
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");
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);
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) {
236
if (mp3_init(tmp, framelen))
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);
245
ast_log(LOG_WARNING, "Invalid MP3 data\n");
248
/* Resample to 8000 Hz */
249
tmp->tail += add_to_buf(tmp->buf + tmp->tail,
250
sizeof(tmp->buf) / 2 - tmp->tail,
255
memcpy(tmp->buf + tmp->tail, tmpbuf, x.out_bytes);
256
/* Signed linear output */
257
tmp->tail+=x.out_bytes/2;
261
ast_log(LOG_WARNING, "Out of buffer space\n");
265
ast_log(LOG_WARNING, "Not a valid MP3 frame\n");
270
static void mp3_destroy_stuff(struct ast_translator_pvt *pvt)
276
static struct ast_translator mp3tolin =
278
AST_FORMAT_MP3, AST_FORMAT_SLINEAR,
286
int unload_module(void)
289
ast_pthread_mutex_lock(&localuser_lock);
290
res = ast_unregister_translator(&mp3tolin);
293
ast_pthread_mutex_unlock(&localuser_lock);
297
int load_module(void)
300
res=ast_register_translator(&mp3tolin);
304
char *description(void)
312
STANDARD_USECOUNT(res);
318
return ASTERISK_GPL_KEY;