5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
9 #include "../pico_int.h"
10 #include "genplus_macros.h"
14 #if defined(__GNUC__) && __GNUC__ >= 7
15 #pragma GCC diagnostic ignored "-Wformat-truncation"
18 static int handle_mp3(const char *fname, int index)
20 track_t *track = &cdd.toc.tracks[index];
25 tmp_file = fopen(fname, "rb");
29 ret = fseek(tmp_file, 0, SEEK_END);
31 fseek(tmp_file, 0, SEEK_SET);
33 #ifdef _PSP_FW_VERSION
34 // some systems (like PSP) can't have many open files at a time,
35 // so we work with their names instead.
37 tmp_file = (void *) strdup(fname);
40 kBps = mp3_get_bitrate(tmp_file, fs) / 8;
41 if (ret != 0 || kBps <= 0)
43 elprintf(EL_STATUS, "track %2i: mp3 bitrate %i", index+1, kBps);
44 #ifdef _PSP_FW_VERSION
60 static void to_upper(char *d, const char *s)
62 for (; *s != 0; d++, s++) {
63 if ('a' <= *s && *s <= 'z')
71 // cdd.c uses lba - 150
72 static void sprintf_lba(char *buf, size_t size, int lba)
75 snprintf(buf, size, "%02d:%02d:%02d", lba / 60 / 75,
76 (lba / 75) % 60, lba % 75);
79 int load_cd_image(const char *cd_img_name, int *type)
81 static const char *exts[] = {
82 "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
83 "%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3",
85 int i, j, n, lba, index, length, ret;
86 int iso_name_len, missed, cd_img_sectors;
87 char tmp_name[256], tmp_ext[10], tmp_ext_u[10];
88 track_t *tracks = cdd.toc.tracks;
89 cue_data_t *cue_data = NULL;
92 if (PicoCDLoadProgressCB != NULL)
93 PicoCDLoadProgressCB(cd_img_name, 1);
95 Pico_mcd->cdda_type = CT_UNKNOWN;
98 cue_data = cue_parse(cd_img_name);
99 if (cue_data != NULL) {
100 cd_img_name = cue_data->tracks[1].fname;
101 *type = cue_data->tracks[1].type;
104 pmf = pm_open(cd_img_name);
107 if (cue_data != NULL)
108 cue_destroy(cue_data);
114 cd_img_sectors = pmf->size >>= 11; // size in sectors
115 else cd_img_sectors = pmf->size /= 2352;
117 // cdd.c operates with lba - 150
119 tracks[0].end = cd_img_sectors;
120 tracks[0].offset = 0;
122 sprintf_lba(tmp_ext, sizeof(tmp_ext), 0);
123 elprintf(EL_STATUS, "Track 1: %s %9i DATA %s",
124 tmp_ext, tracks[0].end, cd_img_name);
126 lba = cd_img_sectors;
128 if (cue_data != NULL)
130 if (cue_data->tracks[2].fname == NULL) {
131 // NULL fname means track2 is in same file as track1
132 lba = tracks[0].end = cue_data->tracks[2].sector_offset;
134 i = 100 / cue_data->track_count + 1; // progress display
136 for (n = 2; n <= cue_data->track_count; n++)
138 if (PicoCDLoadProgressCB != NULL)
139 PicoCDLoadProgressCB(cd_img_name, i * n);
142 lba += cue_data->tracks[n].pregap;
143 if (cue_data->tracks[n].type == CT_MP3) {
144 ret = handle_mp3(cue_data->tracks[n].fname, index);
149 else if (cue_data->tracks[n].fname != NULL)
151 pm_file *f = pm_open(cue_data->tracks[n].fname);
154 // assume raw, ignore header for wav..
155 tracks[index].fd = f;
156 tracks[index].offset = cue_data->tracks[n].sector_offset;
157 length = f->size / 2352;
161 elprintf(EL_STATUS, "track %2i (%s): can't determine length",
162 n, cue_data->tracks[n].fname);
163 tracks[index].offset = 0;
169 if (n < cue_data->track_count)
170 length = cue_data->tracks[n+1].sector_offset -
171 cue_data->tracks[n].sector_offset;
173 length = cd_img_sectors - cue_data->tracks[n].sector_offset;
174 tracks[index].offset = cue_data->tracks[n].sector_offset;
177 if (cue_data->tracks[n].sector_xlength != 0)
178 // overriden by custom cue command
179 length = cue_data->tracks[n].sector_xlength;
181 Pico_mcd->cdda_type = cue_data->tracks[n].type;
183 tracks[index].start = lba;
185 tracks[index].end = lba;
187 sprintf_lba(tmp_ext, sizeof(tmp_ext), tracks[index].start);
188 elprintf(EL_STATUS, "Track %2i: %s %9i AUDIO %s",
189 n, tmp_ext, length, cue_data->tracks[n].fname);
194 /* mp3 track autosearch, Gens-like */
195 iso_name_len = strlen(cd_img_name);
196 if (iso_name_len >= sizeof(tmp_name))
197 iso_name_len = sizeof(tmp_name) - 1;
199 for (n = 2, i = 0, missed = 0; i < 100 && missed < 4; i++)
201 if (PicoCDLoadProgressCB != NULL && i > 1)
202 PicoCDLoadProgressCB(cd_img_name, i + (100-i)*missed/4);
204 for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
211 snprintf(tmp_ext, sizeof(tmp_ext), exts[j], i);
212 ext_len = strlen(tmp_ext);
213 to_upper(tmp_ext_u, tmp_ext);
215 memcpy(tmp_name, cd_img_name, iso_name_len + 1);
216 p = tmp_name + iso_name_len - 4;
219 ret = handle_mp3(tmp_name, index);
221 strcpy(p, tmp_ext_u);
222 ret = handle_mp3(tmp_name, index);
225 if (ret <= 0 && i > 1 && iso_name_len > ext_len) {
226 p = tmp_name + iso_name_len - ext_len;
228 ret = handle_mp3(tmp_name, index);
230 strcpy(p, tmp_ext_u);
231 ret = handle_mp3(tmp_name, index);
238 tracks[index].start = lba;
240 tracks[index].end = lba;
242 Pico_mcd->cdda_type = CT_MP3;
244 sprintf_lba(tmp_ext, sizeof(tmp_ext), tracks[index].start);
245 elprintf(EL_STATUS, "Track %2i: %s %9i AUDIO - %s",
246 n, tmp_ext, length, tmp_name);
253 if (ret <= 0 && i > 1)
258 cdd.toc.last = n - 1;
261 sprintf_lba(tmp_ext, sizeof(tmp_ext), cdd.toc.end);
262 elprintf(EL_STATUS, "End CD - %s\n", tmp_ext);
264 if (PicoCDLoadProgressCB != NULL)
265 PicoCDLoadProgressCB(cd_img_name, 100);
267 if (cue_data != NULL)
268 cue_destroy(cue_data);
273 // vim:shiftwidth=2:ts=2:expandtab