Debian Codegraph @

Xmp3/interface.c

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