| 1 |
#define WRAP_LIBC_MALLOC |
| 2 |
#include "asterisk.h" |
| 3 |
#include "asterisk/logger.h" |
| 4 |
|
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
#include "mpg123.h" |
| 9 |
#include "mpglib.h" |
| 10 |
|
| 11 |
|
| 12 |
void InitMP3Constants(void) |
| 13 |
{ |
| 14 |
init_layer3_const(); |
| 15 |
make_decode_tables_const(); |
| 16 |
|
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
BOOL InitMP3(struct mpstr *mp, long outscale) |
| 21 |
{ |
| 22 |
/* quiet 4096 med 8192 */ |
| 23 |
|
| 24 |
memset(mp,0,sizeof(struct mpstr)); |
| 25 |
|
| 26 |
mp->framesize = 0; |
| 27 |
mp->fsizeold = -1; |
| 28 |
mp->bsize = 0; |
| 29 |
mp->head = mp->tail = NULL; |
| 30 |
mp->fr.single = 3; /* force mono */ |
| 31 |
mp->bsnum = 0; |
| 32 |
mp->synth_bo = 1; |
| 33 |
mp->outsamplerate = 8000; |
| 34 |
|
| 35 |
make_decode_tables_scale(mp, outscale); |
| 36 |
|
| 37 |
init_layer3_sample_limits(mp, SBLIMIT); |
| 38 |
|
| 39 |
return !0; |
| 40 |
} |
| 41 |
|
| 42 |
void ExitMP3(struct mpstr *mp) |
| 43 |
{ |
| 44 |
struct buf *b,*bn; |
| 45 |
|
| 46 |
b = mp->tail; |
| 47 |
while(b) { |
| 48 |
free(b->pnt); |
| 49 |
bn = b->next; |
| 50 |
free(b); |
| 51 |
b = bn; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static struct buf *addbuf(struct mpstr *mp,char *buf,int size) |
| 56 |
{ |
| 57 |
struct buf *nbuf; |
| 58 |
|
| 59 |
nbuf = malloc( sizeof(struct buf) ); |
| 60 |
if(!nbuf) { |
| 61 |
ast_log(LOG_WARNING,"Out of memory!\n"); |
| 62 |
return NULL; |
| 63 |
} |
| 64 |
nbuf->pnt = malloc(size); |
| 65 |
if(!nbuf->pnt) { |
| 66 |
free(nbuf); |
| 67 |
return NULL; |
| 68 |
} |
| 69 |
nbuf->size = size; |
| 70 |
memcpy(nbuf->pnt,buf,size); |
| 71 |
nbuf->next = NULL; |
| 72 |
nbuf->prev = mp->head; |
| 73 |
nbuf->pos = 0; |
| 74 |
|
| 75 |
if(!mp->tail) { |
| 76 |
mp->tail = nbuf; |
| 77 |
} |
| 78 |
else { |
| 79 |
mp->head->next = nbuf; |
| 80 |
} |
| 81 |
|
| 82 |
mp->head = nbuf; |
| 83 |
mp->bsize += size; |
| 84 |
|
| 85 |
return nbuf; |
| 86 |
} |
| 87 |
|
| 88 |
static void remove_buf(struct mpstr *mp) |
| 89 |
{ |
| 90 |
struct buf *buf = mp->tail; |
| 91 |
|
| 92 |
mp->tail = buf->next; |
| 93 |
if(mp->tail) |
| 94 |
mp->tail->prev = NULL; |
| 95 |
else { |
| 96 |
mp->tail = mp->head = NULL; |
| 97 |
} |
| 98 |
|
| 99 |
free(buf->pnt); |
| 100 |
free(buf); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
static int read_buf_byte(int *error, struct mpstr *mp) |
| 105 |
{ |
| 106 |
unsigned int b;int pos; |
| 107 |
|
| 108 |
pos = mp->tail->pos; |
| 109 |
while(pos >= mp->tail->size) { |
| 110 |
remove_buf(mp); |
| 111 |
pos = mp->tail->pos; |
| 112 |
if(!mp->tail) { |
| 113 |
/* We may pick up this error a few times*/ |
| 114 |
/* But things have gone pear shaped */ |
| 115 |
ast_log(LOG_WARNING,"Fatal Buffer error!\n"); |
| 116 |
*error = 1; |
| 117 |
return (0); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
b = mp->tail->pnt[pos]; |
| 122 |
mp->bsize--; |
| 123 |
mp->tail->pos++; |
| 124 |
|
| 125 |
|
| 126 |
return b; |
| 127 |
} |
| 128 |
|
| 129 |
static int read_head(struct mpstr *mp) |
| 130 |
{ |
| 131 |
unsigned long head; |
| 132 |
int error=0; |
| 133 |
|
| 134 |
|
| 135 |
head = read_buf_byte(&error, mp); |
| 136 |
head <<= 8; |
| 137 |
head |= read_buf_byte(&error, mp); |
| 138 |
head <<= 8; |
| 139 |
head |= read_buf_byte(&error, mp); |
| 140 |
head <<= 8; |
| 141 |
head |= read_buf_byte(&error, mp); |
| 142 |
|
| 143 |
mp->header = head; |
| 144 |
|
| 145 |
if(error){ |
| 146 |
return (1); |
| 147 |
}else |
| 148 |
return (0); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
static int head_check(unsigned long head) |
| 153 |
{ |
| 154 |
if( (head & 0xffe00000) != 0xffe00000) |
| 155 |
return FALSE; |
| 156 |
if(!((head>>17)&3)) |
| 157 |
return FALSE; |
| 158 |
if( ((head>>12)&0xf) == 0xf || ((head>>12)&0xf) == 0) |
| 159 |
return FALSE; |
| 160 |
if( ((head>>10)&0x3) == 0x3 ) |
| 161 |
return FALSE; |
| 162 |
if ((head & 0xffff0000) == 0xfffe0000) |
| 163 |
return FALSE; |
| 164 |
|
| 165 |
return TRUE; |
| 166 |
} |
| 167 |
|
| 168 |
static int head_shift(struct mpstr *mp) |
| 169 |
{ |
| 170 |
unsigned long head; |
| 171 |
int error = 0; |
| 172 |
|
| 173 |
head = mp->header; |
| 174 |
|
| 175 |
head <<= 8; |
| 176 |
head |= read_buf_byte(&error, mp); |
| 177 |
|
| 178 |
mp->header = head; |
| 179 |
|
| 180 |
if (error){ |
| 181 |
return (1); |
| 182 |
}else |
| 183 |
return (0); |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
int decodeMP3(struct mpstr *mp,char *in,int isize,char *out, |
| 189 |
int osize,int *done) |
| 190 |
{ |
| 191 |
int len; |
| 192 |
long n,m; |
| 193 |
int down_sample_sblimit; |
| 194 |
|
| 195 |
if(osize < 4608) { |
| 196 |
ast_log(LOG_WARNING,"To less out space\n"); |
| 197 |
return MP3_ERR; |
| 198 |
} |
| 199 |
|
| 200 |
if(in) { |
| 201 |
if(addbuf(mp,in,isize) == NULL) { |
| 202 |
return MP3_ERR; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/* First decode header */ |
| 207 |
if(mp->framesize == 0) { |
| 208 |
if(mp->bsize < 4) { |
| 209 |
return MP3_NEED_MORE; |
| 210 |
} |
| 211 |
if (read_head(mp)) |
| 212 |
return MP3_ERR; |
| 213 |
|
| 214 |
if(!head_check(mp->header) ) { |
| 215 |
int i; |
| 216 |
|
| 217 |
ast_log(LOG_WARNING,"Junk at the beginning of frame %08lx\n",mp->header); |
| 218 |
|
| 219 |
/* step in byte steps through next 64K */ |
| 220 |
for(i=0;i<65536;i++) { |
| 221 |
if(!mp->bsize) |
| 222 |
return MP3_NEED_MORE; |
| 223 |
|
| 224 |
if(head_shift(mp)) |
| 225 |
return MP3_ERR; |
| 226 |
|
| 227 |
if(head_check(mp->header)) |
| 228 |
break; |
| 229 |
} |
| 230 |
if(i == 65536) { |
| 231 |
ast_log(LOG_WARNING,"Giving up searching valid MPEG header\n"); |
| 232 |
return MP3_ERR; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
decode_header(&mp->fr,mp->header); |
| 237 |
mp->framesize = mp->fr.framesize; |
| 238 |
|
| 239 |
if (!mp->initmp3){ |
| 240 |
mp->initmp3 = 1; |
| 241 |
|
| 242 |
n = freqs[mp->fr.sampling_frequency]; |
| 243 |
if (mp->outsamplerate) { |
| 244 |
m = mp->outsamplerate; |
| 245 |
} |
| 246 |
else { |
| 247 |
m =n; |
| 248 |
} |
| 249 |
|
| 250 |
if (synth_ntom_set_step(n,m)) |
| 251 |
return MP3_ERR; |
| 252 |
|
| 253 |
|
| 254 |
if(n>m) { |
| 255 |
down_sample_sblimit = SBLIMIT * m; |
| 256 |
down_sample_sblimit /= n; |
| 257 |
} |
| 258 |
else { |
| 259 |
down_sample_sblimit = SBLIMIT; |
| 260 |
} |
| 261 |
|
| 262 |
init_layer3_sample_limits(mp, down_sample_sblimit); |
| 263 |
|
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
if(mp->fr.framesize > mp->bsize) |
| 269 |
return MP3_NEED_MORE; |
| 270 |
|
| 271 |
(mp->worksample).wordpointer = mp->bsspace[mp->bsnum] + 512; |
| 272 |
mp->bsnum = (mp->bsnum + 1) & 0x1; |
| 273 |
(mp->worksample).bitindex = 0; |
| 274 |
|
| 275 |
len = 0; |
| 276 |
while(len < mp->framesize) { |
| 277 |
int nlen; |
| 278 |
int blen = mp->tail->size - mp->tail->pos; |
| 279 |
if( (mp->framesize - len) <= blen) { |
| 280 |
nlen = mp->framesize-len; |
| 281 |
} |
| 282 |
else { |
| 283 |
nlen = blen; |
| 284 |
} |
| 285 |
memcpy((mp->worksample).wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen); |
| 286 |
len += nlen; |
| 287 |
mp->tail->pos += nlen; |
| 288 |
mp->bsize -= nlen; |
| 289 |
if(mp->tail->pos == mp->tail->size) { |
| 290 |
remove_buf(mp); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
*done = 0; |
| 295 |
if(mp->fr.error_protection) |
| 296 |
getbits(mp, 16); |
| 297 |
|
| 298 |
if (do_layer3(mp,(unsigned char *) out,done)) |
| 299 |
return MP3_ERR; |
| 300 |
|
| 301 |
mp->fsizeold = mp->framesize; |
| 302 |
mp->framesize = 0; |
| 303 |
|
| 304 |
return MP3_OK; |
| 305 |
} |
| 306 |
|
| 307 |
int set_pointer(struct mpstr *mp, long backstep) |
| 308 |
{ |
| 309 |
unsigned char *bsbufold; |
| 310 |
if(mp->fsizeold < 0 && backstep > 0) { |
| 311 |
ast_log(LOG_WARNING,"Can't step back %ld!\n",backstep); |
| 312 |
return MP3_ERR; |
| 313 |
} |
| 314 |
bsbufold = mp->bsspace[mp->bsnum] + 512; |
| 315 |
(mp->worksample).wordpointer -= backstep; |
| 316 |
if (backstep) |
| 317 |
memcpy((mp->worksample).wordpointer,bsbufold+mp->fsizeold-backstep,backstep); |
| 318 |
(mp->worksample).bitindex = 0; |
| 319 |
return MP3_OK; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|