aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/codec_mp3_d.c
blob: d271dfe51242a7ea1e3fedaf3da729bc7c7ee9ad (plain)
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
309
310
311
312
313
314
315
316
317
318
319
/*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * MP3 Decoder
 *
 * The MP3 code is from freeamp, which in turn is from xingmp3's release
 * which I can't seem to find anywhere
 * 
 * Copyright (C) 1999, Mark Spencer
 *
 * Mark Spencer <markster@linux-support.net>
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License
 */

#include <asterisk/translate.h>
#include <asterisk/module.h>
#include <asterisk/logger.h>
#include <asterisk/channel.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>

#include "mp3/include/L3.h"
#include "mp3/include/mhead.h"

#include "mp3anal.h"

/* Sample frame data */
#include "mp3_slin_ex.h"

#define MAX_OUT_FRAME 320

#define MAX_FRAME_SIZE 1441
#define MAX_OUTPUT_LEN 2304

static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
static int localusecnt=0;

static char *tdesc = "MP3/PCM16 (signed linear) Translator (Decoder only)";

struct ast_translator_pvt {
	MPEG m;
	MPEG_HEAD head;
	DEC_INFO info;
	struct ast_frame f;
	/* Space to build offset */
	char offset[AST_FRIENDLY_OFFSET];
	/* Mini buffer */
	char outbuf[MAX_OUT_FRAME];
	/* Enough to store a full second */
	short buf[32000];
	/* Tail of signed linear stuff */
	int tail;
	/* Current bitrate */
	int bitrate;
	/* XXX What's forward? XXX */
	int forward;
	/* Have we called head info yet? */
	int init;
	int copy;
};

#define mp3_coder_pvt ast_translator_pvt

static struct ast_translator_pvt *mp3_new()
{
	struct mp3_coder_pvt *tmp;
	tmp = malloc(sizeof(struct mp3_coder_pvt));
	if (tmp) {
		tmp->init = 0;
		tmp->tail = 0;
		tmp->copy = -1;
		mpeg_init(&tmp->m);
	}
	return tmp;
}

static struct ast_frame *mp3tolin_sample()
{
	static struct ast_frame f;
	int size;
	if (mp3_badheader(mp3_slin_ex)) {
		ast_log(LOG_WARNING, "Bad MP3 sample??\n");
		return NULL;
	}
	size = mp3_framelen(mp3_slin_ex);
	if (size < 1) {
		ast_log(LOG_WARNING, "Failed to size??\n");
		return NULL;
	}
	f.frametype = AST_FRAME_VOICE;
	f.subclass = AST_FORMAT_MP3;
	f.data = mp3_slin_ex;
	f.datalen = sizeof(mp3_slin_ex);
	/* Dunno how long an mp3 frame is -- kinda irrelevant anyway */
	f.timelen = 30;
	f.mallocd = 0;
	f.offset = 0;
	f.src = __PRETTY_FUNCTION__;
	return &f;
}

static struct ast_frame *mp3tolin_frameout(struct ast_translator_pvt *tmp)
{
	if (!tmp->tail)
		return NULL;
	/* Signed linear is no particular frame size, so just send whatever
	   we have in the buffer in one lump sum */
	tmp->f.frametype = AST_FRAME_VOICE;
	tmp->f.subclass = AST_FORMAT_SLINEAR;
	tmp->f.datalen = tmp->tail * 2;
	/* Assume 8000 Hz */
	tmp->f.timelen = tmp->tail / 8;
	tmp->f.mallocd = 0;
	tmp->f.offset = AST_FRIENDLY_OFFSET;
	tmp->f.src = __PRETTY_FUNCTION__;
	tmp->f.data = tmp->buf;
	/* Reset tail pointer */
	tmp->tail = 0;

#if 0
	/* Save a sample frame */
	{
		static int fd = -1;
		if (fd < 0) 
			fd = open("mp3out.raw", O_WRONLY | O_CREAT | O_TRUNC, 0644);
		write(fd, tmp->f.data, tmp->f.datalen);
	} 		
#endif
	return &tmp->f;	
}

static int mp3_init(struct ast_translator_pvt *tmp, int len)
{	
	if (!audio_decode_init(&tmp->m, &tmp->head, len,0,0,1 /* Convert to mono */,24000)) {
		ast_log(LOG_WARNING, "audio_decode_init() failed\n");
		return -1;
	}
	audio_decode_info(&tmp->m, &tmp->info);
#if 0
	ast_verbose(
"Channels: %d\nOutValues: %d\nSample Rate: %d\nBits: %d\nFramebytes: %d\nType: %d\n",
	tmp->info.channels, tmp->info.outvalues, tmp->info.samprate, tmp->info.bits,tmp->info.framebytes,tmp->info.type);
#endif
	return 0;
}

#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif

#if 1
static int add_to_buf(short *dst, int maxdst, short *src, int srclen, int samprate)
{
	float inc, cur, sum=0;
	int cnt=0, pos, ptr, lastpos = -1;
	/* Resample source to destination converting from its sampling rate to 8000 Hz */
	if (samprate == 8000) {
		/* Quickly, all we have to do is copy */
		memcpy(dst, src, 2 * MIN(maxdst, srclen));
		return MIN(maxdst, srclen);
	}
	if (samprate < 8000) {
		ast_log(LOG_WARNING, "Don't know how to resample a source less than 8000 Hz!\n");
		/* XXX Wrong thing to do XXX */
		memcpy(dst, src, 2 * MIN(maxdst, srclen));
		return MIN(maxdst, srclen);
	}
	/* Ugh, we actually *have* to resample */
	inc = 8000.0 / (float)samprate;
	cur = 0;
	ptr = 0;
	pos = 0;
#if 0
	ast_verbose("Incrementing by %f, in = %d bytes, out = %d bytes\n", inc, srclen, maxdst);
#endif
	while((pos < maxdst) && (ptr < srclen)) {
		if (pos != lastpos) {
			if (lastpos > -1) {
				sum = sum / (float)cnt;
				dst[pos - 1] = (int) sum;
#if 0
				ast_verbose("dst[%d] = %d\n", pos - 1, dst[pos - 1]);
#endif
			}
			/* Each time we have a first pass */
			sum = 0;
			cnt = 0;
		} else {
			sum += src[ptr];
		}
		ptr++;
		cur += inc;
		cnt++;
		lastpos = pos;
		pos = (int)cur;
	}
	return pos;
}
#endif

static int mp3tolin_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
{
	/* Assuming there's space left, decode into the current buffer at
	   the tail location */
	int framelen;
	short tmpbuf[8000];
	IN_OUT x;
#if 0
	if (tmp->copy < 0) {
		tmp->copy = open("sample.out", O_WRONLY | O_CREAT | O_TRUNC, 0700);
	}
	if (tmp->copy > -1)
		write(tmp->copy, f->data, f->datalen);
#endif
	/* Check if it's a valid frame */
	if (mp3_badheader((unsigned char *)f->data)) {
		ast_log(LOG_WARNING, "Invalid MP3 header\n");
		return -1;
	}
	if ((framelen = mp3_framelen((unsigned char *)f->data) != f->datalen)) {
		ast_log(LOG_WARNING, "Calculated length %d does not match real length %d\n", framelen, f->datalen);
		return -1;
	}
	/* Start by putting this in the mp3 buffer */
	if((framelen = head_info3(f->data, 
			f->datalen, &tmp->head, &tmp->bitrate, &tmp->forward)) > 0) {
		if (!tmp->init) {
			if (mp3_init(tmp, framelen))
				return -1;
			else
				tmp->init++;
		}
		if (tmp->tail + MAX_OUTPUT_LEN/2  < sizeof(tmp->buf)/2) {	
			x = audio_decode(&tmp->m, f->data, tmpbuf);
			audio_decode_info(&tmp->m, &tmp->info);
			if (!x.in_bytes) {
				ast_log(LOG_WARNING, "Invalid MP3 data\n");
			} else {
#if 1
				/* Resample to 8000 Hz */
				tmp->tail += add_to_buf(tmp->buf + tmp->tail, 
			           sizeof(tmp->buf) / 2 - tmp->tail, 
					   tmpbuf,
					   x.out_bytes/2,
					   tmp->info.samprate);
#else
				memcpy(tmp->buf + tmp->tail, tmpbuf, x.out_bytes);
				/* Signed linear output */
				tmp->tail+=x.out_bytes/2;
#endif
			}
		} else {
			ast_log(LOG_WARNING, "Out of buffer space\n");
			return -1;
		}
	} else {
		ast_log(LOG_WARNING, "Not a valid MP3 frame\n");
	}
	return 0;
}

static void mp3_destroy_stuff(struct ast_translator_pvt *pvt)
{
	close(pvt->copy);
	free(pvt);
}

static struct ast_translator mp3tolin =
	{ "mp3tolin", 
	   AST_FORMAT_MP3, AST_FORMAT_SLINEAR,
	   mp3_new,
	   mp3tolin_framein,
	   mp3tolin_frameout,
	   mp3_destroy_stuff,
	   mp3tolin_sample
	   };

int unload_module(void)
{
	int res;
	ast_pthread_mutex_lock(&localuser_lock);
	res = ast_unregister_translator(&mp3tolin);
	if (localusecnt)
		res = -1;
	ast_pthread_mutex_unlock(&localuser_lock);
	return res;
}

int load_module(void)
{
	int res;
	res=ast_register_translator(&mp3tolin);
	return res;
}

char *description(void)
{
	return tdesc;
}

int usecount(void)
{
	int res;
	STANDARD_USECOUNT(res);
	return res;
}

char *key()
{
	return ASTERISK_GPL_KEY;
}