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;
20 static unsigned char mp3_input_buffer[2 * 1024];
23 #define mp3_update mp3_update_local
24 #define mp3_start_play mp3_start_play_local
27 int mp3dec_decode(FILE *f, int *file_pos, int file_len)
29 unsigned char *readPtr;
31 int offset; // mp3 frame offset from readPtr
37 if (*file_pos >= file_len)
38 return 1; /* EOF, nothing to do */
40 fseek(f, *file_pos, SEEK_SET);
41 bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), f);
43 offset = mp3_find_sync_word(mp3_input_buffer, bytesLeft);
45 lprintf("find_sync_word (%i/%i) err %i\n",
46 *file_pos, file_len, offset);
50 readPtr = mp3_input_buffer + offset;
54 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
56 if (err == ERR_MP3_MAINDATA_UNDERFLOW && !had_err) {
57 // just need another frame
58 *file_pos += readPtr - mp3_input_buffer;
61 if (err == ERR_MP3_INDATA_UNDERFLOW && !had_err) {
63 // something's really wrong here, frame had to fit
69 if (-12 <= err && err <= -6) {
70 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
71 // just try to skip the offending frame..
72 *file_pos += offset + 1;
75 lprintf("MP3Decode err (%i/%i) %i\n",
76 *file_pos, file_len, err);
80 *file_pos += readPtr - mp3_input_buffer;
87 int mp3dec_start(FILE *f, int fpos_start)
89 // must re-init decoder for new track
91 MP3FreeDecoder(mp3dec);
92 mp3dec = MP3InitDecoder();
94 return (mp3dec == 0) ? -1 : 0;