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 try_get_header(unsigned char *buff, MP3FrameInfo *fi)
28 int ret, offs1, offs = 0;
32 offs1 = mp3_find_sync_word(buff + offs, 2048 - offs);
33 if (offs1 < 0) return -2;
35 if (2048 - offs < 4) return -3;
37 // printf("trying header %08x\n", *(int *)(buff + offs));
39 ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
40 if (ret == 0 && fi->bitrate != 0) break;
47 int mp3_get_bitrate(void *f_, int len)
49 unsigned char buff[2048];
54 memset(buff, 0, sizeof(buff));
57 MP3FreeDecoder(mp3dec);
58 mp3dec = MP3InitDecoder();
60 fseek(f, 0, SEEK_SET);
61 ret = fread(buff, 1, sizeof(buff), f);
62 fseek(f, 0, SEEK_SET);
66 ret = try_get_header(buff, &fi);
67 if (ret != 0 || fi.bitrate == 0) {
68 // try to read somewhere around the middle
69 fseek(f, len>>1, SEEK_SET);
70 fread(buff, 1, 2048, f);
71 fseek(f, 0, SEEK_SET);
72 ret = try_get_header(buff, &fi);
77 // printf("bitrate: %i\n", fi.bitrate / 1000);
79 return fi.bitrate / 1000;
82 static int mp3_decode(void)
84 unsigned char *readPtr;
86 int offset; // mp3 frame offset from readPtr
92 if (mp3_file_pos >= mp3_file_len)
93 return 1; /* EOF, nothing to do */
95 fseek(mp3_current_file, mp3_file_pos, SEEK_SET);
96 bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file);
98 offset = mp3_find_sync_word(mp3_input_buffer, bytesLeft);
100 lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset);
101 mp3_file_pos = mp3_file_len;
104 readPtr = mp3_input_buffer + offset;
108 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
110 if (err == ERR_MP3_MAINDATA_UNDERFLOW && !had_err) {
111 // just need another frame
112 mp3_file_pos += readPtr - mp3_input_buffer;
115 if (err == ERR_MP3_INDATA_UNDERFLOW && !had_err) {
117 // something's really wrong here, frame had to fit
118 mp3_file_pos = mp3_file_len;
120 mp3_file_pos += offset;
123 if (-12 <= err && err <= -6) {
124 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
125 // just try to skip the offending frame..
126 mp3_file_pos += offset + 1;
129 lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
130 mp3_file_pos = mp3_file_len;
133 mp3_file_pos += readPtr - mp3_input_buffer;
140 void mp3_start_play(void *f_, int pos)
144 mp3_file_len = mp3_file_pos = 0;
145 mp3_current_file = NULL;
148 if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file?
151 // must re-init decoder for new track
153 MP3FreeDecoder(mp3dec);
154 mp3dec = MP3InitDecoder();
156 mp3_current_file = f;
157 fseek(f, 0, SEEK_END);
158 mp3_file_len = ftell(f);
160 // search for first sync word, skipping stuff like ID3 tags
161 while (mp3_file_pos < 128*1024) {
164 fseek(f, mp3_file_pos, SEEK_SET);
165 bytes = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), f);
168 offs = mp3_find_sync_word(mp3_input_buffer, bytes);
170 mp3_file_pos += offs;
173 mp3_file_pos += bytes - 2;
178 unsigned long long pos64 = mp3_file_len - mp3_file_pos;
180 mp3_file_pos += pos64 >> 10;
186 void mp3_update(int *buffer, int length, int stereo)
188 int length_mp3, shr = 0;
189 void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
191 if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len)
192 return; /* no file / EOF */
195 if (PsndRate <= 11025 + 100) {
196 mix_samples = mix_16h_to_32_s2;
197 length_mp3 <<= 2; shr = 2;
199 else if (PsndRate <= 22050 + 100) {
200 mix_samples = mix_16h_to_32_s1;
201 length_mp3 <<= 1; shr = 1;
204 if (1152 - mp3_buffer_offs >= length_mp3) {
205 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1);
207 mp3_buffer_offs += length_mp3;
209 int ret, left = 1152 - mp3_buffer_offs;
211 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
214 mp3_buffer_offs = length_mp3 - left;
215 mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1);