da42200b |
1 | // Some mp3 related code for Sega/Mega CD. |
2 | // Uses the Helix Fixed-point MP3 decoder |
3 | |
4 | // (c) Copyright 2007, Grazvydas "notaz" Ignotas |
5 | |
6 | #include <stdio.h> |
7 | #include <string.h> |
8 | |
9 | #include "../../Pico/PicoInt.h" |
10 | #include "../../Pico/sound/mix.h" |
11 | #include "helix/pub/mp3dec.h" |
12 | #include "lprintf.h" |
13 | |
da42200b |
14 | static HMP3Decoder mp3dec = 0; |
15 | static int mp3_buffer_offs = 0; |
16 | |
17 | |
18 | static int try_get_header(unsigned char *buff, MP3FrameInfo *fi) |
19 | { |
20 | int ret, offs1, offs = 0; |
21 | |
22 | while (1) |
23 | { |
24 | offs1 = MP3FindSyncWord(buff + offs, 2048 - offs); |
25 | if (offs1 < 0) return -2; |
26 | offs += offs1; |
27 | if (2048 - offs < 4) return -3; |
28 | |
29 | // printf("trying header %08x\n", *(int *)(buff + offs)); |
30 | |
31 | ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs); |
32 | if (ret == 0 && fi->bitrate != 0) break; |
33 | offs++; |
34 | } |
35 | |
36 | return ret; |
37 | } |
38 | |
39 | int mp3_get_bitrate(FILE *f, int len) |
40 | { |
41 | unsigned char buff[2048]; |
42 | MP3FrameInfo fi; |
43 | int ret; |
44 | |
45 | memset(buff, 0, 2048); |
46 | |
47 | if (!mp3dec) mp3dec = MP3InitDecoder(); |
48 | |
49 | fseek(f, 0, SEEK_SET); |
50 | ret = fread(buff, 1, 2048, f); |
51 | fseek(f, 0, SEEK_SET); |
52 | if (ret <= 0) return -1; |
53 | |
54 | ret = try_get_header(buff, &fi); |
55 | if (ret != 0 || fi.bitrate == 0) { |
56 | // try to read somewhere around the middle |
57 | fseek(f, len>>1, SEEK_SET); |
58 | fread(buff, 1, 2048, f); |
59 | fseek(f, 0, SEEK_SET); |
60 | ret = try_get_header(buff, &fi); |
61 | } |
62 | if (ret != 0) return ret; |
63 | |
64 | // printf("bitrate: %i\n", fi.bitrate / 1000); |
65 | |
66 | return fi.bitrate / 1000; |
67 | } |
68 | |
69 | |
70 | #ifdef __GP2X__ |
71 | |
72 | #include "../gp2x/code940/940shared.h" |
73 | |
74 | extern _940_ctl_t *shared_ctl; |
75 | extern unsigned char *mp3_mem; |
76 | |
77 | static int mp3_decode(void) |
78 | { |
79 | // tried copying this to cached mem, no improvement noticed |
80 | int mp3_offs = shared_ctl->mp3_offs; |
81 | unsigned char *readPtr = mp3_mem + mp3_offs; |
82 | int bytesLeft = shared_ctl->mp3_len - mp3_offs; |
83 | int offset; // frame offset from readPtr |
84 | int err; |
85 | |
86 | if (bytesLeft <= 0) return 1; // EOF, nothing to do |
87 | |
88 | offset = MP3FindSyncWord(readPtr, bytesLeft); |
89 | if (offset < 0) { |
90 | shared_ctl->mp3_offs = shared_ctl->mp3_len; |
91 | return 1; // EOF |
92 | } |
93 | readPtr += offset; |
94 | bytesLeft -= offset; |
95 | |
c9e1affc |
96 | err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0); |
da42200b |
97 | if (err) { |
98 | if (err == ERR_MP3_INDATA_UNDERFLOW) { |
99 | shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF |
100 | return 1; |
101 | } else if (err <= -6 && err >= -12) { |
102 | // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_* |
103 | // just try to skip the offending frame.. |
104 | readPtr++; |
105 | } |
106 | shared_ctl->mp3_errors++; |
107 | shared_ctl->mp3_lasterr = err; |
108 | } |
109 | shared_ctl->mp3_offs = readPtr - mp3_mem; |
110 | return 0; |
111 | } |
112 | |
113 | void mp3_start_local(void) |
114 | { |
115 | mp3_buffer_offs = 0; |
116 | mp3_decode(); |
117 | } |
118 | |
119 | #define mp3_update mp3_update_local |
120 | |
c9e1affc |
121 | #else // !__GP2X__ |
da42200b |
122 | |
123 | static FILE *mp3_current_file = NULL; |
124 | static int mp3_file_len = 0, mp3_file_pos = 0; |
125 | static unsigned char mp3_input_buffer[2*1024]; |
126 | |
127 | static int mp3_decode(void) |
128 | { |
129 | unsigned char *readPtr; |
130 | int bytesLeft; |
131 | int offset; // mp3 frame offset from readPtr |
132 | int err; |
133 | |
134 | do |
135 | { |
136 | if (mp3_file_pos >= mp3_file_len) return 1; // EOF, nothing to do |
137 | |
138 | fseek(mp3_current_file, mp3_file_pos, SEEK_SET); |
139 | bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file); |
140 | |
141 | offset = MP3FindSyncWord(mp3_input_buffer, bytesLeft); |
142 | if (offset < 0) { |
143 | //lprintf("MP3FindSyncWord (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset); |
144 | mp3_file_pos = mp3_file_len; |
145 | return 1; // EOF |
146 | } |
147 | readPtr = mp3_input_buffer + offset; |
148 | bytesLeft -= offset; |
149 | |
c9e1affc |
150 | err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0); |
da42200b |
151 | if (err) { |
152 | //lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err); |
153 | if (err == ERR_MP3_INDATA_UNDERFLOW) { |
154 | if (offset == 0) |
155 | // something's really wrong here, frame had to fit |
156 | mp3_file_pos = mp3_file_len; |
157 | else |
158 | mp3_file_pos += offset; |
159 | continue; |
160 | } else if (err <= -6 && err >= -12) { |
161 | // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_* |
162 | // just try to skip the offending frame.. |
163 | mp3_file_pos += offset + 1; |
164 | continue; |
165 | } |
166 | mp3_file_pos = mp3_file_len; |
167 | return 1; |
168 | } |
169 | mp3_file_pos += readPtr - mp3_input_buffer; |
170 | } |
171 | while (0); |
172 | |
173 | return 0; |
174 | } |
175 | |
176 | void mp3_start_play(FILE *f, int pos) |
177 | { |
178 | mp3_file_len = mp3_file_pos = 0; |
179 | mp3_current_file = NULL; |
180 | mp3_buffer_offs = 0; |
181 | |
602133e1 |
182 | if (!(PicoOpt&POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file? |
da42200b |
183 | return; |
184 | |
185 | //lprintf("mp3_start_play %p %i\n", f, pos); |
186 | |
187 | mp3_current_file = f; |
188 | fseek(f, 0, SEEK_END); |
189 | mp3_file_len = ftell(f); |
190 | |
191 | // seek.. |
192 | if (pos) { |
193 | mp3_file_pos = (mp3_file_len << 6) >> 10; |
194 | mp3_file_pos *= pos; |
195 | mp3_file_pos >>= 6; |
196 | } |
197 | |
198 | mp3_decode(); |
199 | } |
200 | |
201 | int mp3_get_offset(void) |
202 | { |
203 | unsigned int offs1024 = 0; |
204 | int cdda_on; |
205 | |
602133e1 |
206 | cdda_on = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_CDDA) && !(Pico_mcd->s68k_regs[0x36] & 1) && |
da42200b |
207 | (Pico_mcd->scd.Status_CDC & 1) && mp3_current_file != NULL; |
208 | |
209 | if (cdda_on) { |
210 | offs1024 = mp3_file_pos << 7; |
211 | offs1024 /= mp3_file_len >> 3; |
212 | } |
213 | //lprintf("mp3_get_offset offs1024=%u (%i/%i)\n", offs1024, mp3_file_pos, mp3_file_len); |
214 | |
215 | return offs1024; |
216 | } |
217 | |
218 | #endif // ifndef __GP2X__ |
219 | |
220 | void mp3_update(int *buffer, int length, int stereo) |
221 | { |
222 | int length_mp3, shr = 0; |
223 | void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32; |
224 | |
225 | #ifndef __GP2X__ |
226 | if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len) return; // no file / EOF |
227 | #endif |
228 | |
229 | length_mp3 = length; |
230 | if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; length_mp3 <<= 1; shr = 1; } |
231 | else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; length_mp3 <<= 2; shr = 2; } |
232 | |
233 | if (1152 - mp3_buffer_offs >= length_mp3) { |
c9e1affc |
234 | mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1); |
da42200b |
235 | |
236 | mp3_buffer_offs += length_mp3; |
237 | } else { |
238 | int ret, left = 1152 - mp3_buffer_offs; |
239 | |
c9e1affc |
240 | mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1); |
da42200b |
241 | ret = mp3_decode(); |
242 | if (ret == 0) { |
243 | mp3_buffer_offs = length_mp3 - left; |
c9e1affc |
244 | mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1); |
da42200b |
245 | } else |
246 | mp3_buffer_offs = 0; |
247 | } |
248 | } |
249 | |
250 | |