From 42d4c7991cc648294965070cc0f053a4611408e1 Mon Sep 17 00:00:00 2001 From: Mark Spencer Date: Sat, 11 Dec 1999 20:09:45 +0000 Subject: Version 0.1.1 from FTP git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@134 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- codecs/codec_mp3_d.c | 323 ++++++++++++++++++++++++++++++++++++++++++++++++++ codecs/g723_slin_ex.h | 2 +- codecs/gsm_slin_ex.h | 2 +- codecs/mp3_slin_ex.h | 70 +++++++++++ codecs/mp3anal.h | 83 +++++++++++++ codecs/slin_g723_ex.h | 2 +- codecs/slin_gsm_ex.h | 2 +- 7 files changed, 480 insertions(+), 4 deletions(-) create mode 100755 codecs/codec_mp3_d.c create mode 100755 codecs/mp3_slin_ex.h create mode 100755 codecs/mp3anal.h (limited to 'codecs') diff --git a/codecs/codec_mp3_d.c b/codecs/codec_mp3_d.c new file mode 100755 index 000000000..95e1fb51f --- /dev/null +++ b/codecs/codec_mp3_d.c @@ -0,0 +1,323 @@ +/* + * 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 + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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) +{ + int sent; + if (!tmp->tail) + return NULL; + sent = tmp->tail; + if (sent > MAX_OUT_FRAME/2) + sent = MAX_OUT_FRAME/2; + /* 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 = sent * 2; + /* Assume 8000 Hz */ + tmp->f.timelen = sent / 8; + tmp->f.mallocd = 0; + tmp->f.offset = AST_FRIENDLY_OFFSET; + tmp->f.src = __PRETTY_FUNCTION__; + memcpy(tmp->outbuf, tmp->buf, tmp->tail * 2); + tmp->f.data = tmp->outbuf; + /* Reset tail pointer */ + tmp->tail -= sent; + if (tmp->tail) + memmove(tmp->buf, tmp->buf + sent, tmp->tail * 2); + +#if 0 + /* Save a sample frame */ + { static int samplefr = 0; + if (samplefr == 80) { + int fd; + fd = open("mp3.example", O_WRONLY | O_CREAT, 0644); + write(fd, tmp->f.data, tmp->f.datalen); + close(fd); + } + samplefr++; + } +#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; + pthread_mutex_lock(&localuser_lock); + res = ast_unregister_translator(&mp3tolin); + if (localusecnt) + res = -1; + 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; +} diff --git a/codecs/g723_slin_ex.h b/codecs/g723_slin_ex.h index df88b7b33..61eab6523 100755 --- a/codecs/g723_slin_ex.h +++ b/codecs/g723_slin_ex.h @@ -3,7 +3,7 @@ * * Source: g723.example * - * Copyright (C) 1999, Mark Spencer and Linux Support Services + * Copyright (C) 1999, Mark Spencer * * Distributed under the terms of the GNU General Public License * diff --git a/codecs/gsm_slin_ex.h b/codecs/gsm_slin_ex.h index e609cbb61..f22a633bf 100755 --- a/codecs/gsm_slin_ex.h +++ b/codecs/gsm_slin_ex.h @@ -3,7 +3,7 @@ * * Source: gsm.example * - * Copyright (C) 1999, Mark Spencer and Linux Support Services + * Copyright (C) 1999, Mark Spencer * * Distributed under the terms of the GNU General Public License * diff --git a/codecs/mp3_slin_ex.h b/codecs/mp3_slin_ex.h new file mode 100755 index 000000000..d47b445ee --- /dev/null +++ b/codecs/mp3_slin_ex.h @@ -0,0 +1,70 @@ +/* + * 8-bit raw data + * + * Source: ../sample.out + * + * Copyright (C) 1999, Mark Spencer + * + * Distributed under the terms of the GNU General Public License + * + */ + +static unsigned char mp3_slin_ex[] = { +0xff, 0xfb, 0x98, 0x4, 0x3, 000, 0x4, 0x1, 0x6f, 0x48, +0x83, 0x46, 0x1a, 0xf0, 0x8a, 0xb, 0x19, 0x10, 0x75, 0x26, +0x5c, 0x50, 0xc9, 0xc1, 0x20, 0xa, 0x18, 0xcb, 0xc2, 0xa, +0xac, 0xe4, 0x81, 0x63, 0x2d, 0x71, 0x6d, 0xdf, 0xcb, 0xf1, +0xb7, 0x89, 0x69, 0xcd, 0x86, 0x40, 0xd7, 0x2e, 0x6c, 0xc7, +0x93, 0xa5, 0xf7, 0x1b, 0x19, 0xa, 0x28, 0xf9, 0xa1, 0xab, +0x93, 0x67, 0xdc, 0xc3, 0x5e, 0x54, 0x75, 0xb1, 0xf8, 0x7e, +0x3a, 0x80, 0xd1, 0xc3, 0x96, 0x18, 0x81, 0x11, 0x1d, 0x27, +0x91, 0xdc, 0x5d, 0xb1, 0x94, 0x52, 0xae, 0x6e, 0x2c, 0x43, +0xb, 0x47, 0x1a, 0xea, 0x45, 0x8, 0x23, 0x13, 0xc5, 0x39, +0x61, 0xbb, 0xa8, 0x59, 0x8b, 0x84, 0xa4, 0x55, 0x1f, 0x56, +0xa1, 0x3, 0x2a, 0xad, 0x86, 0xf, 0x27, 0xdc, 0xa1, 0x3a, +0xa1, 0xb8, 0x47, 0xb, 0xc9, 0xe8, 0xa4, 0xc, 0xc7, 0x45, +0x21, 0x4f, 0x30, 0xac, 0xf8, 0xb7, 0x23, 0x16, 0x81, 0x13, +0x29, 0xe3, 0x77, 0x61, 0x68, 0xae, 0x85, 0x2d, 0x32, 0x77, +0x63, 0x4a, 0x37, 0x34, 0xb3, 0x37, 0xe9, 0xb3, 0x9c, 0xde, +000, 0x3e, 0xb4, 0x52, 0x99, 0xfc, 0xe4, 0x55, 0x28, 0xfe, +0x97, 0xa1, 0x5d, 0x14, 0xa8, 0x86, 0xe7, 0xf2, 0xa6, 0x44, +0x6c, 0x8a, 0xd3, 0x83, 0xe9, 0xc, 0xc5, 0x3, 0x24, 0x47, +0xe0, 0x8d, 0xe8, 0x10, 0x1d, 0x46, 0x28, 0x13, 0x96, 0x14, +0x58, 0x2e, 0x4e, 0x4d, 0x31, 0x40, 0xfa, 0x20, 0x60, 0x42, +0x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6, 0xed, 0x32, 0xb, 0x3d, +0x1a, 0x6d, 0x86, 0x3f, 0x31, 0x24, 0x2a, 0x7b, 0xba, 0x5c, +0xd8, 0xb2, 0x47, 0xfc, 0xfa, 0x66, 0x6d, 0xdd, 0x7e, 0x8d, +0x7f, 0xbb, 0x33, 0xd0, 0x22, 0xe1, 0x50, 0xcd, 0x74, 0xa5, +0x80, 0xe8, 0x4c, 0x9a, 0x26, 0xb4, 0x1d, 0x5, 0xa6, 0x71, +0x6, 0xd2, 0xfd, 0x97, 0xf2, 0xe8, 0xb6, 0xda, 0xfe, 0x2d, +0xee, 0x19, 0x52, 0x2d, 0xc, 0x22, 0x89, 0xde, 0xcd, 0x31, +0x93, 0xc, 0x69, 0x22, 0x58, 0xf3, 0x46, 0x80, 0xe8, 0xa4, +0x14, 0x10, 0x21, 0xed, 0x33, 0x12, 0xb4, 0xe, 0x38, 0xea, +0x2b, 0x45, 0x5b, 0x25, 0x87, 0x16, 0xab, 0x96, 0xb9, 0xad, +0x88, 0xfa, 0x62, 0x71, 0x75, 0x28, 0x42, 0xd7, 0x3c, 0x9a, +0xdc, 0x34, 0xe3, 0xe2, 0x66, 0xdb, 0xe, 0x39, 0x9c, 0x98, +0xb4, 0xa1, 0xd2, 0xe6, 0xec, 0x5f, 0x7f, 0xd6, 0xd8, 0xa3, +0xb2, 0x75, 0x6a, 0x77, 0x72, 0xf7, 0x5b, 0x2b, 0x98, 0xfe, +0x2f, 0x88, 0xb7, 0xd7, 0xb2, 0x5d, 0xe, 0x65, 0xf2, 0xa0, +0x14, 0xec, 0x2d, 0x9b, 0x98, 0x67, 0x20, 0x63, 0x7c, 0x52, +0x95, 0x8d, 0x62, 0xd9, 0x54, 0xa7, 0xe9, 0x8d, 0x7f, 0xdb, +0xcf, 0x11, 0x24, 0xe2, 0x81, 0xc1, 0x3e, 0xab, 0xf4, 0x86, +0xc8, 0x4c, 0xc5, 0x8d, 0xf0, 0x58 }; diff --git a/codecs/mp3anal.h b/codecs/mp3anal.h new file mode 100755 index 000000000..263e8520c --- /dev/null +++ b/codecs/mp3anal.h @@ -0,0 +1,83 @@ +/* + * Asterisk -- A telephony toolkit for Linux. + * + * MP3 Header Analysis Routines. Thanks to Robert Kaye for the logic! + * + * Copyright (C) 1999, Mark Spencer + * + * Mark Spencer + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + +static int bitrates1[] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 }; +static int bitrates2[] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 }; + +static int samplerates1[] = { 44100, 48000, 32000 }; +static int samplerates2[] = { 22050, 24000, 16000 }; + +static int outputsamples[] = { 576, 1152 }; + +static int mp3_samples(unsigned char *header) +{ + int ver = (header[1] & 0x8) >> 3; + return outputsamples[ver]; +} + +static int mp3_bitrate(unsigned char *header) +{ + int ver = (header[1] & 0x8) >> 3; + int br = (header[2] >> 4); + + if (ver > 14) { + ast_log(LOG_WARNING, "Invalid bit rate\n"); + return -1; + } + if (ver) + return bitrates1[br]; + else { + return bitrates2[br]; + } +} + +static int mp3_samplerate(unsigned char *header) +{ + int ver = (header[1] & 0x8) >> 3; + int sr = (header[2] >> 2) & 0x3; + + if (ver > 2) { + ast_log(LOG_WARNING, "Invalid sample rate\n"); + return -1; + } + + if (ver) + return samplerates1[sr]; + else + return samplerates2[sr]; +} + +static int mp3_padding(unsigned char *header) +{ + return (header[2] >> 1) & 0x1; +} + +static int mp3_badheader(unsigned char *header) +{ + if ((header[0] != 0xFF) || ((header[1] & 0xF0) != 0xF0)) + return -1; + return 0; +} + +static int mp3_framelen(unsigned char *header) +{ + int br = mp3_bitrate(header); + int sr = mp3_samplerate(header); + int size; + + if ((br < 0) || (sr < 0)) + return -1; + size = 144000 * br / sr + mp3_padding(header); + return size; +} + diff --git a/codecs/slin_g723_ex.h b/codecs/slin_g723_ex.h index dc3c087e4..fedadd409 100755 --- a/codecs/slin_g723_ex.h +++ b/codecs/slin_g723_ex.h @@ -3,7 +3,7 @@ * * Source: g723.example * - * Copyright (C) 1999, Mark Spencer and Linux Support Services + * Copyright (C) 1999, Mark Spencer * * Distributed under the terms of the GNU General Public License * diff --git a/codecs/slin_gsm_ex.h b/codecs/slin_gsm_ex.h index cb46dc71d..26dd6d7e6 100755 --- a/codecs/slin_gsm_ex.h +++ b/codecs/slin_gsm_ex.h @@ -3,7 +3,7 @@ * * Source: gsm.example * - * Copyright (C) 1999, Mark Spencer and Linux Support Services + * Copyright (C) 1999, Mark Spencer * * Distributed under the terms of the GNU General Public License * -- cgit v1.2.3