1 // Some mp3 related code for Sega/Mega CD.
2 // Uses the Helix Fixed-point MP3 decoder
4 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
9 #include "../../Pico/PicoInt.h"
10 #include "../../Pico/sound/mix.h"
11 #include "helix/pub/mp3dec.h"
14 static HMP3Decoder mp3dec = 0;
15 static int mp3_buffer_offs = 0;
18 static int find_sync_word(unsigned char *buf, int nBytes)
20 unsigned char *p, *pe;
22 /* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */
23 for (p = buf, pe = buf + nBytes - 4; p < pe; p++)
26 if (p[0] != 0xff) continue;
28 if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1
29 (pn & 6) == 0) { // invalid layer
33 if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates
34 (pn & 0x0c) != 0) { // not 44kHz
45 static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
47 int ret, offs1, offs = 0;
51 offs1 = find_sync_word(buff + offs, 2048 - offs);
52 if (offs1 < 0) return -2;
54 if (2048 - offs < 4) return -3;
56 // printf("trying header %08x\n", *(int *)(buff + offs));
58 ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
59 if (ret == 0 && fi->bitrate != 0) break;
66 int mp3_get_bitrate(FILE *f, int len)
68 unsigned char buff[2048];
72 memset(buff, 0, 2048);
74 if (mp3dec) MP3FreeDecoder(mp3dec);
75 mp3dec = MP3InitDecoder();
77 fseek(f, 0, SEEK_SET);
78 ret = fread(buff, 1, 2048, f);
79 fseek(f, 0, SEEK_SET);
80 if (ret <= 0) return -1;
82 ret = try_get_header(buff, &fi);
83 if (ret != 0 || fi.bitrate == 0) {
84 // try to read somewhere around the middle
85 fseek(f, len>>1, SEEK_SET);
86 fread(buff, 1, 2048, f);
87 fseek(f, 0, SEEK_SET);
88 ret = try_get_header(buff, &fi);
90 if (ret != 0) return ret;
92 // printf("bitrate: %i\n", fi.bitrate / 1000);
94 return fi.bitrate / 1000;
100 #include "../gp2x/code940/940shared.h"
102 extern _940_ctl_t *shared_ctl;
103 extern unsigned char *mp3_mem;
105 static int mp3_decode(void)
107 // tried copying this to cached mem, no improvement noticed
108 int mp3_offs = shared_ctl->mp3_offs;
109 unsigned char *readPtr = mp3_mem + mp3_offs;
110 int bytesLeft = shared_ctl->mp3_len - mp3_offs;
111 int offset; // frame offset from readPtr
112 int retries = 0, err;
114 if (bytesLeft <= 0) return 1; // EOF, nothing to do
117 offset = find_sync_word(readPtr, bytesLeft);
119 shared_ctl->mp3_offs = shared_ctl->mp3_len;
125 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
127 if (err == ERR_MP3_INDATA_UNDERFLOW) {
128 shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
130 } else if (err <= -6 && err >= -12) {
131 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
132 // just try to skip the offending frame..
135 if (retries++ < 2) goto retry;
136 else lprintf("mp3 decode failed with %i after %i retries\n", err, retries);
138 shared_ctl->mp3_errors++;
139 shared_ctl->mp3_lasterr = err;
141 shared_ctl->mp3_offs = readPtr - mp3_mem;
145 void mp3_start_local(void)
147 // must re-init decoder for new track
148 if (mp3dec) MP3FreeDecoder(mp3dec);
149 mp3dec = MP3InitDecoder();
155 #define mp3_update mp3_update_local
159 static FILE *mp3_current_file = NULL;
160 static int mp3_file_len = 0, mp3_file_pos = 0;
161 static unsigned char mp3_input_buffer[2*1024];
163 static int mp3_decode(void)
165 unsigned char *readPtr;
167 int offset; // mp3 frame offset from readPtr
172 if (mp3_file_pos >= mp3_file_len) return 1; // EOF, nothing to do
174 fseek(mp3_current_file, mp3_file_pos, SEEK_SET);
175 bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file);
177 offset = find_sync_word(mp3_input_buffer, bytesLeft);
179 //lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset);
180 mp3_file_pos = mp3_file_len;
183 readPtr = mp3_input_buffer + offset;
186 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
188 //lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
189 if (err == ERR_MP3_INDATA_UNDERFLOW) {
191 // something's really wrong here, frame had to fit
192 mp3_file_pos = mp3_file_len;
194 mp3_file_pos += offset;
196 } else if (err <= -6 && err >= -12) {
197 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
198 // just try to skip the offending frame..
199 mp3_file_pos += offset + 1;
202 mp3_file_pos = mp3_file_len;
205 mp3_file_pos += readPtr - mp3_input_buffer;
212 void mp3_start_play(FILE *f, int pos)
214 mp3_file_len = mp3_file_pos = 0;
215 mp3_current_file = NULL;
218 // must re-init decoder for new track
219 if (mp3dec) MP3FreeDecoder(mp3dec);
220 mp3dec = MP3InitDecoder();
222 if (!(PicoOpt&POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file?
225 //lprintf("mp3_start_play %p %i\n", f, pos);
227 mp3_current_file = f;
228 fseek(f, 0, SEEK_END);
229 mp3_file_len = ftell(f);
233 mp3_file_pos = (mp3_file_len << 6) >> 10;
241 int mp3_get_offset(void)
243 unsigned int offs1024 = 0;
246 cdda_on = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_CDDA) && !(Pico_mcd->s68k_regs[0x36] & 1) &&
247 (Pico_mcd->scd.Status_CDC & 1) && mp3_current_file != NULL;
250 offs1024 = mp3_file_pos << 7;
251 offs1024 /= mp3_file_len >> 3;
253 //lprintf("mp3_get_offset offs1024=%u (%i/%i)\n", offs1024, mp3_file_pos, mp3_file_len);
258 #endif // ifndef __GP2X__
260 void mp3_update(int *buffer, int length, int stereo)
262 int length_mp3, shr = 0;
263 void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
266 if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len) return; // no file / EOF
270 if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; length_mp3 <<= 1; shr = 1; }
271 else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; length_mp3 <<= 2; shr = 2; }
273 if (1152 - mp3_buffer_offs >= length_mp3) {
274 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1);
276 mp3_buffer_offs += length_mp3;
278 int ret, left = 1152 - mp3_buffer_offs;
280 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
283 mp3_buffer_offs = length_mp3 - left;
284 mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1);