| 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 | |
| 14 | static HMP3Decoder mp3dec = 0; |
| 15 | static int mp3_buffer_offs = 0; |
| 16 | |
| 17 | |
| 18 | static int find_sync_word(unsigned char *buf, int nBytes) |
| 19 | { |
| 20 | unsigned char *p, *pe; |
| 21 | |
| 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++) |
| 24 | { |
| 25 | int pn; |
| 26 | if (p[0] != 0xff) continue; |
| 27 | pn = p[1]; |
| 28 | if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1 |
| 29 | (pn & 6) == 0) { // invalid layer |
| 30 | p++; continue; |
| 31 | } |
| 32 | pn = p[2]; |
| 33 | if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates |
| 34 | (pn & 0x0c) != 0) { // not 44kHz |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | return p - buf; |
| 39 | } |
| 40 | |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | static int try_get_header(unsigned char *buff, MP3FrameInfo *fi) |
| 46 | { |
| 47 | int ret, offs1, offs = 0; |
| 48 | |
| 49 | while (1) |
| 50 | { |
| 51 | offs1 = find_sync_word(buff + offs, 2048 - offs); |
| 52 | if (offs1 < 0) return -2; |
| 53 | offs += offs1; |
| 54 | if (2048 - offs < 4) return -3; |
| 55 | |
| 56 | // printf("trying header %08x\n", *(int *)(buff + offs)); |
| 57 | |
| 58 | ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs); |
| 59 | if (ret == 0 && fi->bitrate != 0) break; |
| 60 | offs++; |
| 61 | } |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | int mp3_get_bitrate(FILE *f, int len) |
| 67 | { |
| 68 | unsigned char buff[2048]; |
| 69 | MP3FrameInfo fi; |
| 70 | int ret; |
| 71 | |
| 72 | memset(buff, 0, 2048); |
| 73 | |
| 74 | if (mp3dec) MP3FreeDecoder(mp3dec); |
| 75 | mp3dec = MP3InitDecoder(); |
| 76 | |
| 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; |
| 81 | |
| 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); |
| 89 | } |
| 90 | if (ret != 0) return ret; |
| 91 | |
| 92 | // printf("bitrate: %i\n", fi.bitrate / 1000); |
| 93 | |
| 94 | return fi.bitrate / 1000; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | #ifdef __GP2X__ |
| 99 | |
| 100 | #include "../gp2x/code940/940shared.h" |
| 101 | |
| 102 | extern _940_ctl_t *shared_ctl; |
| 103 | extern unsigned char *mp3_mem; |
| 104 | |
| 105 | static int mp3_decode(void) |
| 106 | { |
| 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; |
| 113 | |
| 114 | if (bytesLeft <= 0) return 1; // EOF, nothing to do |
| 115 | |
| 116 | retry: |
| 117 | offset = find_sync_word(readPtr, bytesLeft); |
| 118 | if (offset < 0) { |
| 119 | shared_ctl->mp3_offs = shared_ctl->mp3_len; |
| 120 | return 1; // EOF |
| 121 | } |
| 122 | readPtr += offset; |
| 123 | bytesLeft -= offset; |
| 124 | |
| 125 | err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0); |
| 126 | if (err) { |
| 127 | if (err == ERR_MP3_INDATA_UNDERFLOW) { |
| 128 | shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF |
| 129 | return 1; |
| 130 | } else if (err <= -6 && err >= -12) { |
| 131 | // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_* |
| 132 | // just try to skip the offending frame.. |
| 133 | readPtr++; |
| 134 | bytesLeft--; |
| 135 | if (retries++ < 2) goto retry; |
| 136 | else lprintf("mp3 decode failed with %i after %i retries\n", err, retries); |
| 137 | } |
| 138 | shared_ctl->mp3_errors++; |
| 139 | shared_ctl->mp3_lasterr = err; |
| 140 | } |
| 141 | shared_ctl->mp3_offs = readPtr - mp3_mem; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | void mp3_start_local(void) |
| 146 | { |
| 147 | // must re-init decoder for new track |
| 148 | if (mp3dec) MP3FreeDecoder(mp3dec); |
| 149 | mp3dec = MP3InitDecoder(); |
| 150 | |
| 151 | mp3_buffer_offs = 0; |
| 152 | mp3_decode(); |
| 153 | } |
| 154 | |
| 155 | #define mp3_update mp3_update_local |
| 156 | |
| 157 | #else // !__GP2X__ |
| 158 | |
| 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]; |
| 162 | |
| 163 | static int mp3_decode(void) |
| 164 | { |
| 165 | unsigned char *readPtr; |
| 166 | int bytesLeft; |
| 167 | int offset; // mp3 frame offset from readPtr |
| 168 | int err; |
| 169 | |
| 170 | do |
| 171 | { |
| 172 | if (mp3_file_pos >= mp3_file_len) return 1; // EOF, nothing to do |
| 173 | |
| 174 | fseek(mp3_current_file, mp3_file_pos, SEEK_SET); |
| 175 | bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file); |
| 176 | |
| 177 | offset = find_sync_word(mp3_input_buffer, bytesLeft); |
| 178 | if (offset < 0) { |
| 179 | //lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset); |
| 180 | mp3_file_pos = mp3_file_len; |
| 181 | return 1; // EOF |
| 182 | } |
| 183 | readPtr = mp3_input_buffer + offset; |
| 184 | bytesLeft -= offset; |
| 185 | |
| 186 | err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0); |
| 187 | if (err) { |
| 188 | //lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err); |
| 189 | if (err == ERR_MP3_INDATA_UNDERFLOW) { |
| 190 | if (offset == 0) |
| 191 | // something's really wrong here, frame had to fit |
| 192 | mp3_file_pos = mp3_file_len; |
| 193 | else |
| 194 | mp3_file_pos += offset; |
| 195 | continue; |
| 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; |
| 200 | continue; |
| 201 | } |
| 202 | mp3_file_pos = mp3_file_len; |
| 203 | return 1; |
| 204 | } |
| 205 | mp3_file_pos += readPtr - mp3_input_buffer; |
| 206 | } |
| 207 | while (0); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | void mp3_start_play(FILE *f, int pos) |
| 213 | { |
| 214 | mp3_file_len = mp3_file_pos = 0; |
| 215 | mp3_current_file = NULL; |
| 216 | mp3_buffer_offs = 0; |
| 217 | |
| 218 | // must re-init decoder for new track |
| 219 | if (mp3dec) MP3FreeDecoder(mp3dec); |
| 220 | mp3dec = MP3InitDecoder(); |
| 221 | |
| 222 | if (!(PicoOpt&POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file? |
| 223 | return; |
| 224 | |
| 225 | //lprintf("mp3_start_play %p %i\n", f, pos); |
| 226 | |
| 227 | mp3_current_file = f; |
| 228 | fseek(f, 0, SEEK_END); |
| 229 | mp3_file_len = ftell(f); |
| 230 | |
| 231 | // seek.. |
| 232 | if (pos) { |
| 233 | mp3_file_pos = (mp3_file_len << 6) >> 10; |
| 234 | mp3_file_pos *= pos; |
| 235 | mp3_file_pos >>= 6; |
| 236 | } |
| 237 | |
| 238 | mp3_decode(); |
| 239 | } |
| 240 | |
| 241 | int mp3_get_offset(void) |
| 242 | { |
| 243 | unsigned int offs1024 = 0; |
| 244 | int cdda_on; |
| 245 | |
| 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; |
| 248 | |
| 249 | if (cdda_on) { |
| 250 | offs1024 = mp3_file_pos << 7; |
| 251 | offs1024 /= mp3_file_len >> 3; |
| 252 | } |
| 253 | //lprintf("mp3_get_offset offs1024=%u (%i/%i)\n", offs1024, mp3_file_pos, mp3_file_len); |
| 254 | |
| 255 | return offs1024; |
| 256 | } |
| 257 | |
| 258 | #endif // ifndef __GP2X__ |
| 259 | |
| 260 | void mp3_update(int *buffer, int length, int stereo) |
| 261 | { |
| 262 | int length_mp3, shr = 0; |
| 263 | void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32; |
| 264 | |
| 265 | #ifndef __GP2X__ |
| 266 | if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len) return; // no file / EOF |
| 267 | #endif |
| 268 | |
| 269 | length_mp3 = length; |
| 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; } |
| 272 | |
| 273 | if (1152 - mp3_buffer_offs >= length_mp3) { |
| 274 | mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1); |
| 275 | |
| 276 | mp3_buffer_offs += length_mp3; |
| 277 | } else { |
| 278 | int ret, left = 1152 - mp3_buffer_offs; |
| 279 | |
| 280 | mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1); |
| 281 | ret = mp3_decode(); |
| 282 | if (ret == 0) { |
| 283 | mp3_buffer_offs = length_mp3 - left; |
| 284 | mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1); |
| 285 | } else |
| 286 | mp3_buffer_offs = 0; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |