annotate decoders/mpglib/interface.c @ 450:6d328f00c20d

Added Speex decoder.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 13 Sep 2003 20:28:39 +0000
parents 9d0b5ec9cc26
children
rev   line source
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 #include <stdlib.h>
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 #include <stdio.h>
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
5 #include "SDL_sound.h"
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
6
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
7 #define __SDL_SOUND_INTERNAL__
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
8 #include "SDL_sound_internal.h"
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
9
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 #include "mpg123_sdlsound.h"
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 #include "mpglib_sdlsound.h"
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13
281
ad4c8f34136a Minor formatting updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 279
diff changeset
14 BOOL InitMP3(struct mpstr *mp)
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 static int init = 0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 memset(mp,0,sizeof(struct mpstr));
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 mp->framesize = 0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 mp->fsizeold = -1;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 mp->bsize = 0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 mp->head = mp->tail = NULL;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 mp->fr.single = -1;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 mp->bsnum = 0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 mp->synth_bo = 1;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 if(!init) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 init = 1;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 make_decode_tables(32767);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31 init_layer2();
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 init_layer3(SBLIMIT);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 return !0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 void ExitMP3(struct mpstr *mp)
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 struct buf *b,*bn;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42 b = mp->tail;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 while(b) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44 free(b->pnt);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45 bn = b->next;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 free(b);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47 b = bn;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 static struct buf *addbuf(struct mpstr *mp,char *buf,int size)
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52 {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 struct buf *nbuf;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 nbuf = malloc( sizeof(struct buf) );
387
fb519e6028e3 Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
56 BAIL_IF_MACRO(!nbuf, ERR_OUT_OF_MEMORY, NULL);
fb519e6028e3 Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
57
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 nbuf->pnt = malloc(size);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 if(!nbuf->pnt) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 free(nbuf);
387
fb519e6028e3 Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
61 BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 nbuf->size = size;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 memcpy(nbuf->pnt,buf,size);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65 nbuf->next = NULL;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 nbuf->prev = mp->head;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 nbuf->pos = 0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69 if(!mp->tail) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 mp->tail = nbuf;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 else {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 mp->head->next = nbuf;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76 mp->head = nbuf;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 mp->bsize += size;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 return nbuf;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 static void remove_buf(struct mpstr *mp)
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 struct buf *buf = mp->tail;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 mp->tail = buf->next;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 if(mp->tail)
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 mp->tail->prev = NULL;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 else {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 mp->tail = mp->head = NULL;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 free(buf->pnt);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 free(buf);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
98 static int read_buf_byte(struct mpstr *mp, unsigned long *retval)
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 int pos;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 pos = mp->tail->pos;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 while(pos >= mp->tail->size) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 remove_buf(mp);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 pos = mp->tail->pos;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 if(!mp->tail) {
387
fb519e6028e3 Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
107 BAIL_MACRO("MPGLIB: Short read in read_buf_byte()!", 0);
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
111 if (retval != NULL)
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
112 *retval = mp->tail->pnt[pos];
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
113
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 mp->bsize--;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 mp->tail->pos++;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
117 return 1;
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
120 static int read_head(struct mpstr *mp)
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 {
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
122 unsigned long val;
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 unsigned long head;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
125 if (!read_buf_byte(mp, &val))
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
126 return 0;
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
127
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
128 head = val << 8;
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
129
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
130 if (!read_buf_byte(mp, &val))
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
131 return 0;
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
132
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
133 head |= val;
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134 head <<= 8;
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
135
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
136 if (!read_buf_byte(mp, &val))
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
137 return 0;
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
138
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
139 head |= val;
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140 head <<= 8;
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
141
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
142 if (!read_buf_byte(mp, &val))
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
143 return 0;
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
145 head |= val;
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146 mp->header = head;
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
147 return 1;
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 int decodeMP3(struct mpstr *mp,char *in,int isize,char *out,
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151 int osize,int *done)
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 int len;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154
387
fb519e6028e3 Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
155 BAIL_IF_MACRO(osize < 4608, "MPGLIB: Output buffer too small", MP3_ERR);
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157 if(in) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 if(addbuf(mp,in,isize) == NULL) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 return MP3_ERR;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163 /* First decode header */
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164 if(mp->framesize == 0) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 if(mp->bsize < 4) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 return MP3_NEED_MORE;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 }
273
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
168
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
169 if (!read_head(mp))
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
170 return MP3_ERR;
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
171
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
172 if (!decode_header(&mp->fr,mp->header))
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
173 return MP3_ERR;
e1429f96aded Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 261
diff changeset
174
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 mp->framesize = mp->fr.framesize;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 if(mp->fr.framesize > mp->bsize)
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 return MP3_NEED_MORE;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181 wordpointer = mp->bsspace[mp->bsnum] + 512;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 mp->bsnum = (mp->bsnum + 1) & 0x1;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 bitindex = 0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185 len = 0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186 while(len < mp->framesize) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 int nlen;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188 int blen = mp->tail->size - mp->tail->pos;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 if( (mp->framesize - len) <= blen) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 nlen = mp->framesize-len;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 else {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 nlen = blen;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196 len += nlen;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 mp->tail->pos += nlen;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 mp->bsize -= nlen;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 if(mp->tail->pos == mp->tail->size) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 remove_buf(mp);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204 *done = 0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205 if(mp->fr.error_protection)
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206 getbits(16);
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207 switch(mp->fr.lay) {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 case 1:
279
52b9f37998db Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents: 274
diff changeset
209 do_layer1(&mp->fr,(unsigned char *) out,done,mp);
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210 break;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 case 2:
279
52b9f37998db Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents: 274
diff changeset
212 do_layer2(&mp->fr,(unsigned char *) out,done,mp);
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 break;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 case 3:
279
52b9f37998db Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents: 274
diff changeset
215 do_layer3(&mp->fr,(unsigned char *) out,done,mp);
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 break;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 mp->fsizeold = mp->framesize;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 mp->framesize = 0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222 return MP3_OK;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224
279
52b9f37998db Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents: 274
diff changeset
225 int set_pointer(long backstep, struct mpstr *mp)
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 {
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 unsigned char *bsbufold;
279
52b9f37998db Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents: 274
diff changeset
228 if(mp->fsizeold < 0 && backstep > 0) {
400
9d0b5ec9cc26 FIXME cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 387
diff changeset
229 char err[128];
9d0b5ec9cc26 FIXME cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 387
diff changeset
230 snprintf(err, sizeof (err), "MPGLIB: Can't step back! %ld!", backstep);
9d0b5ec9cc26 FIXME cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 387
diff changeset
231 BAIL_MACRO(err, MP3_ERR);
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 }
279
52b9f37998db Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents: 274
diff changeset
233 bsbufold = mp->bsspace[mp->bsnum] + 512;
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 wordpointer -= backstep;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 if (backstep)
279
52b9f37998db Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents: 274
diff changeset
236 memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep);
261
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 bitindex = 0;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 return MP3_OK;
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239 }
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242
9b6e82f7c853 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243