2 * Some mp3 related code for Sega/Mega CD.
3 * Uses the Helix Fixed-point MP3 decoder
6 * This work is licensed under the terms of MAME license.
7 * See COPYING file in the top-level directory.
13 #include <pico/pico_int.h>
14 #include <pico/sound/mix.h>
15 #include "helix/pub/mp3dec.h"
19 static HMP3Decoder mp3dec = 0;
20 static FILE *mp3_current_file = NULL;
21 static int mp3_file_len = 0, mp3_file_pos = 0;
22 static int mp3_buffer_offs = 0;
23 static unsigned char mp3_input_buffer[2*1024];
26 #define mp3_update mp3_update_local
27 #define mp3_start_play mp3_start_play_local
30 static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
32 int ret, offs1, offs = 0;
36 offs1 = mp3_find_sync_word(buff + offs, 2048 - offs);
37 if (offs1 < 0) return -2;
39 if (2048 - offs < 4) return -3;
41 // printf("trying header %08x\n", *(int *)(buff + offs));
43 ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
44 if (ret == 0 && fi->bitrate != 0) break;
51 int mp3_get_bitrate(void *f_, int len)
53 unsigned char buff[2048];
58 memset(buff, 0, sizeof(buff));
61 MP3FreeDecoder(mp3dec);
62 mp3dec = MP3InitDecoder();
64 fseek(f, 0, SEEK_SET);
65 ret = fread(buff, 1, sizeof(buff), f);
66 fseek(f, 0, SEEK_SET);
70 ret = try_get_header(buff, &fi);
71 if (ret != 0 || fi.bitrate == 0) {
72 // try to read somewhere around the middle
73 fseek(f, len>>1, SEEK_SET);
74 fread(buff, 1, 2048, f);
75 fseek(f, 0, SEEK_SET);
76 ret = try_get_header(buff, &fi);
81 // printf("bitrate: %i\n", fi.bitrate / 1000);
83 return fi.bitrate / 1000;
86 static int mp3_decode(void)
88 unsigned char *readPtr;
90 int offset; // mp3 frame offset from readPtr
96 if (mp3_file_pos >= mp3_file_len)
97 return 1; /* EOF, nothing to do */
99 fseek(mp3_current_file, mp3_file_pos, SEEK_SET);
100 bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file);
102 offset = mp3_find_sync_word(mp3_input_buffer, bytesLeft);
104 lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset);
105 mp3_file_pos = mp3_file_len;
108 readPtr = mp3_input_buffer + offset;
112 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
114 if (err == ERR_MP3_MAINDATA_UNDERFLOW && !had_err) {
115 // just need another frame
116 mp3_file_pos += readPtr - mp3_input_buffer;
119 if (err == ERR_MP3_INDATA_UNDERFLOW && !had_err) {
121 // something's really wrong here, frame had to fit
122 mp3_file_pos = mp3_file_len;
124 mp3_file_pos += offset;
127 if (-12 <= err && err <= -6) {
128 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
129 // just try to skip the offending frame..
130 mp3_file_pos += offset + 1;
133 lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
134 mp3_file_pos = mp3_file_len;
137 mp3_file_pos += readPtr - mp3_input_buffer;
144 void mp3_start_play(void *f_, int pos)
148 mp3_file_len = mp3_file_pos = 0;
149 mp3_current_file = NULL;
152 if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file?
155 // must re-init decoder for new track
157 MP3FreeDecoder(mp3dec);
158 mp3dec = MP3InitDecoder();
160 mp3_current_file = f;
161 fseek(f, 0, SEEK_END);
162 mp3_file_len = ftell(f);
164 // search for first sync word, skipping stuff like ID3 tags
165 while (mp3_file_pos < 128*1024) {
168 fseek(f, mp3_file_pos, SEEK_SET);
169 bytes = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), f);
172 offs = mp3_find_sync_word(mp3_input_buffer, bytes);
174 mp3_file_pos += offs;
177 mp3_file_pos += bytes - 2;
182 unsigned long long pos64 = mp3_file_len - mp3_file_pos;
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);