1 #include <stdlib.h>
2 #include <stdio.h>
3 
4 #include "mpg123.h"
5 #include "mpglib.h"
6 
7 
8 volatile int init = 0;
9 
10 
InitMP3(struct mpstr * mp)11 int InitMP3(struct mpstr *mp)
12 {
13 	memset(mp,0,sizeof(struct mpstr));
14 
15 	mp->framesize = 0;
16 	mp->fsizeold = -1;
17 	mp->bsize = 0;
18 	mp->head = mp->tail = NULL;
19 	mp->fr.single = -1;
20 	mp->bsnum = 0;
21 	mp->synth_bo = 1;
22 
23   if (!init) {
24     make_decode_tables(32767);
25 	  init_layer3(SBLIMIT);
26 
27 	  mp->fr.II_sblimit=SBLIMIT;
28 	  init_layer2();
29 
30     init = 1;
31   }
32 
33 	return !0;
34 }
35 
ExitMP3(struct mpstr * mp)36 void ExitMP3(struct mpstr *mp)
37 {
38 	struct buf *b,*bn;
39 
40 	b = mp->tail;
41 	while(b) {
42 		free(b->pnt);
43 		bn = b->next;
44 		free(b);
45 		b = bn;
46 	}
47 }
48 
addbuf(struct mpstr * mp,char * buf,int size)49 static struct buf *addbuf(struct mpstr *mp,char *buf,int size)
50 {
51 	struct buf *nbuf;
52 
53 	nbuf = (struct buf*) malloc( sizeof(struct buf) );
54 	if(!nbuf) {
55 		return NULL;
56 	}
57 	nbuf->pnt = (unsigned char*) malloc(size);
58 	if(!nbuf->pnt) {
59 		free(nbuf);
60 		return NULL;
61 	}
62 	nbuf->size = size;
63 	memcpy(nbuf->pnt,buf,size);
64 	nbuf->next = NULL;
65 	nbuf->prev = mp->head;
66 	nbuf->pos = 0;
67 
68 	if(!mp->tail) {
69 		mp->tail = nbuf;
70 	}
71 	else {
72 	  mp->head->next = nbuf;
73 	}
74 
75 	mp->head = nbuf;
76 	mp->bsize += size;
77 
78 	return nbuf;
79 }
80 
remove_buf(struct mpstr * mp)81 static void remove_buf(struct mpstr *mp)
82 {
83   struct buf *buf = mp->tail;
84 
85   mp->tail = buf->next;
86   if(mp->tail)
87     mp->tail->prev = NULL;
88   else {
89     mp->tail = mp->head = NULL;
90   }
91 
92   free(buf->pnt);
93   free(buf);
94 
95 }
96 
read_buf_byte(struct mpstr * mp)97 static int read_buf_byte(struct mpstr *mp)
98 {
99 	unsigned int b;
100 
101 	int pos;
102 
103 	pos = mp->tail->pos;
104 	while(pos >= mp->tail->size) {
105 		remove_buf(mp);
106 		pos = mp->tail->pos;
107 		if(!mp->tail) {
108 			exit(1);
109 		}
110 	}
111 
112 	b = mp->tail->pnt[pos];
113 	mp->bsize--;
114 	mp->tail->pos++;
115 
116 
117 	return b;
118 }
119 
read_head(struct mpstr * mp)120 static void read_head(struct mpstr *mp)
121 {
122 	unsigned long head;
123 
124 	head = read_buf_byte(mp);
125 	head <<= 8;
126 	head |= read_buf_byte(mp);
127 	head <<= 8;
128 	head |= read_buf_byte(mp);
129 	head <<= 8;
130 	head |= read_buf_byte(mp);
131 
132 	mp->header = head;
133 }
134 
decodeMP3(struct mpstr * mp,char * in,int isize,char * out,int osize,int * done)135 int decodeMP3(struct mpstr *mp, char *in, int isize,
136 		char *out, int osize, int *done)
137 {
138 	int len;
139 
140 	if(osize < 4608) {
141 		return MP3_ERR;
142 	}
143 
144 	if(in) {
145 		if(addbuf(mp, in, isize) == NULL) {
146 			return MP3_ERR;
147 		}
148 	}
149 
150 
151 	/* First decode header */
152 	if(mp->framesize == 0) {
153 		if(mp->bsize < 4) {
154 			return MP3_NEED_MORE;
155 		}
156 		read_head(mp);
157     if (!head_check(mp->header))
158       return MP3_ERR;
159 		if (decode_header(&mp->fr,mp->header) <= 0)
160       return MP3_ERR;
161 
162 		mp->framesize = mp->fr.framesize;
163 	}
164 
165 	if(mp->fr.framesize > mp->bsize) {
166 	  return MP3_NEED_MORE;
167 	}
168 	wordpointer = mp->bsspace[mp->bsnum] + 512;
169 	mp->bsnum = (mp->bsnum + 1) & 0x1;
170 	bitindex = 0;
171 
172 	len = 0;
173 	while(len < mp->framesize) {
174 		int nlen;
175 		int blen = mp->tail->size - mp->tail->pos;
176 		if( (mp->framesize - len) <= blen) {
177       nlen = mp->framesize-len;
178 		}
179 		else {
180       nlen = blen;
181     }
182 		memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
183            len += nlen;
184            mp->tail->pos += nlen;
185 		mp->bsize -= nlen;
186     if(mp->tail->pos == mp->tail->size) {
187       remove_buf(mp);
188     }
189 	}
190 
191 	*done = 0;
192 	if (mp->fr.error_protection)
193     getbits(16);
194 
195 	if ((&mp->fr)->do_layer(mp, &mp->fr, (unsigned char*) out, done) < 0)
196     return MP3_ERR;
197 
198 	mp->fsizeold = mp->framesize;
199 	mp->framesize = 0;
200 	return MP3_OK;
201 }
202 
almp3_set_pointer(void * mp,long backstep)203 int almp3_set_pointer(void *mp, long backstep)
204 {
205   struct mpstr *gmp = mp;
206 
207   unsigned char *bsbufold;
208   if(gmp->fsizeold < 0 && backstep > 0) {
209     return MP3_ERR;
210   }
211   bsbufold = gmp->bsspace[gmp->bsnum] + 512;
212   wordpointer -= backstep;
213   if (backstep)
214     memcpy(wordpointer,bsbufold+gmp->fsizeold-backstep,backstep);
215   bitindex = 0;
216   return MP3_OK;
217 }
218