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/pico_int.h>
10 #include <pico/sound/mix.h>
11 #include "helix/pub/mp3dec.h"
15 static HMP3Decoder mp3dec = 0;
16 static FILE *mp3_current_file = NULL;
17 static int mp3_file_len = 0, mp3_file_pos = 0;
18 static int mp3_buffer_offs = 0;
19 static unsigned char mp3_input_buffer[2*1024];
22 #define mp3_update mp3_update_local
23 #define mp3_start_play mp3_start_play_local
26 static int find_sync_word(unsigned char *buf, int nBytes)
28 unsigned char *p, *pe;
30 /* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */
31 for (p = buf, pe = buf + nBytes - 4; p < pe; p++)
34 if (p[0] != 0xff) continue;
36 if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1
37 (pn & 6) == 0) { // invalid layer
41 if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates
42 (pn & 0x0c) != 0) { // not 44kHz
52 static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
54 int ret, offs1, offs = 0;
58 offs1 = find_sync_word(buff + offs, 2048 - offs);
59 if (offs1 < 0) return -2;
61 if (2048 - offs < 4) return -3;
63 // printf("trying header %08x\n", *(int *)(buff + offs));
65 ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
66 if (ret == 0 && fi->bitrate != 0) break;
73 int mp3_get_bitrate(FILE *f, int len)
75 unsigned char buff[2048];
79 memset(buff, 0, 2048);
81 if (mp3dec) MP3FreeDecoder(mp3dec);
82 mp3dec = MP3InitDecoder();
84 fseek(f, 0, SEEK_SET);
85 ret = fread(buff, 1, 2048, f);
86 fseek(f, 0, SEEK_SET);
87 if (ret <= 0) return -1;
89 ret = try_get_header(buff, &fi);
90 if (ret != 0 || fi.bitrate == 0) {
91 // try to read somewhere around the middle
92 fseek(f, len>>1, SEEK_SET);
93 fread(buff, 1, 2048, f);
94 fseek(f, 0, SEEK_SET);
95 ret = try_get_header(buff, &fi);
97 if (ret != 0) return ret;
99 // printf("bitrate: %i\n", fi.bitrate / 1000);
101 return fi.bitrate / 1000;
104 static int mp3_decode(void)
106 unsigned char *readPtr;
108 int offset; // mp3 frame offset from readPtr
114 if (mp3_file_pos >= mp3_file_len)
115 return 1; /* EOF, nothing to do */
117 fseek(mp3_current_file, mp3_file_pos, SEEK_SET);
118 bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file);
120 offset = find_sync_word(mp3_input_buffer, bytesLeft);
122 lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset);
123 mp3_file_pos = mp3_file_len;
126 readPtr = mp3_input_buffer + offset;
130 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
132 if (err == ERR_MP3_MAINDATA_UNDERFLOW && !had_err) {
133 // just need another frame
134 mp3_file_pos += readPtr - mp3_input_buffer;
137 if (err == ERR_MP3_INDATA_UNDERFLOW && !had_err) {
139 // something's really wrong here, frame had to fit
140 mp3_file_pos = mp3_file_len;
142 mp3_file_pos += offset;
145 if (-12 <= err && err <= -6) {
146 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
147 // just try to skip the offending frame..
148 mp3_file_pos += offset + 1;
151 lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
152 mp3_file_pos = mp3_file_len;
155 mp3_file_pos += readPtr - mp3_input_buffer;
162 void mp3_start_play(FILE *f, int pos)
164 mp3_file_len = mp3_file_pos = 0;
165 mp3_current_file = NULL;
168 if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file?
171 // must re-init decoder for new track
173 MP3FreeDecoder(mp3dec);
174 mp3dec = MP3InitDecoder();
176 mp3_current_file = f;
177 fseek(f, 0, SEEK_END);
178 mp3_file_len = ftell(f);
182 unsigned long long pos64 = mp3_file_len;
184 mp3_file_pos = pos64 >> 10;
190 void mp3_update(int *buffer, int length, int stereo)
192 int length_mp3, shr = 0;
193 void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
195 if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len)
196 return; /* no file / EOF */
199 if (PsndRate <= 11025 + 100) {
200 mix_samples = mix_16h_to_32_s2;
201 length_mp3 <<= 2; shr = 2;
203 else if (PsndRate <= 22050 + 100) {
204 mix_samples = mix_16h_to_32_s1;
205 length_mp3 <<= 1; shr = 1;
208 if (1152 - mp3_buffer_offs >= length_mp3) {
209 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1);
211 mp3_buffer_offs += length_mp3;
213 int ret, left = 1152 - mp3_buffer_offs;
215 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
218 mp3_buffer_offs = length_mp3 - left;
219 mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1);