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
61 static void to_upper(char *d, const char *s)
63 for (; *s != 0; d++, s++) {
64 if ('a' <= *s && *s <= 'z')
72 // cdd.c uses lba - 150
73 static void sprintf_lba(char *buf, size_t size, int lba)
76 snprintf(buf, size, "%02d:%02d:%02d", lba / 60 / 75,
77 (lba / 75) % 60, lba % 75);
80 int load_cd_image(const char *cd_img_name, int *type)
82 static const char *exts[] = {
83 "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
84 "%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3",
86 int i, j, n, lba, index, length, ret;
87 int iso_name_len, missed, cd_img_sectors;
88 char tmp_name[256], tmp_ext[10], tmp_ext_u[10];
89 track_t *tracks = cdd.toc.tracks;
90 cd_data_t *cue_data = NULL;
93 if (PicoCDLoadProgressCB != NULL)
94 PicoCDLoadProgressCB(cd_img_name, 1);
97 cue_data = cue_parse(cd_img_name);
98 if (cue_data != NULL) {
99 cd_img_name = cue_data->tracks[1].fname;
100 *type = cue_data->tracks[1].type;
102 cue_data = chd_parse(cd_img_name);
103 if (cue_data != NULL)
104 *type = cue_data->tracks[1].type;
107 pmf = pm_open(cd_img_name);
110 if (cue_data != NULL)
111 cdparse_destroy(cue_data);
115 tracks[0].fname = strdup(cd_img_name);
116 tracks[0].type = *type;
119 cd_img_sectors = pmf->size >> 11; // size in sectors
120 else cd_img_sectors = pmf->size / 2352;
122 // cdd.c operates with lba - 150
124 tracks[0].end = cd_img_sectors;
125 tracks[0].offset = 0;
127 sprintf_lba(tmp_ext, sizeof(tmp_ext), 0);
128 elprintf(EL_STATUS, "Track 1: %s %9i %s %s",
129 tmp_ext, tracks[0].end, tracks[0].type ? "AUDIO" : "DATA ", cd_img_name);
131 lba = cd_img_sectors;
133 if (cue_data != NULL)
135 if (cue_data->track_count > 1 && cue_data->tracks[2].fname == NULL) {
136 // NULL fname means track2 is in same file as track1
137 lba = tracks[0].end = cue_data->tracks[2].sector_offset;
139 i = 100 / cue_data->track_count + 1; // progress display
141 for (n = 2; n <= cue_data->track_count; n++)
143 if (PicoCDLoadProgressCB != NULL)
144 PicoCDLoadProgressCB(cd_img_name, i * n);
147 lba += cue_data->tracks[n].pregap;
148 if (cue_data->tracks[n].type == CT_MP3) {
149 ret = handle_mp3(cue_data->tracks[n].fname, index);
154 else if (cue_data->tracks[n].fname != NULL)
156 pm_file *f = pm_open(cue_data->tracks[n].fname);
159 // assume raw, ignore header for wav..
160 tracks[index].fd = f;
161 tracks[index].fname = strdup(cue_data->tracks[n].fname);
162 tracks[index].offset = cue_data->tracks[n].sector_offset;
163 length = f->size / 2352;
167 elprintf(EL_STATUS, "track %2i (%s): can't determine length",
168 n, cue_data->tracks[n].fname);
169 tracks[index].offset = 0;
175 if (n < cue_data->track_count)
176 length = cue_data->tracks[n+1].sector_offset -
177 cue_data->tracks[n].sector_offset;
179 length = cd_img_sectors - cue_data->tracks[n].sector_offset;
180 tracks[index].offset = cue_data->tracks[n].sector_offset;
183 if (cue_data->tracks[n].sector_xlength != 0)
184 // overriden by custom cue command
185 length = cue_data->tracks[n].sector_xlength;
187 tracks[index].type = cue_data->tracks[n].type;
189 tracks[index].start = lba;
191 tracks[index].end = lba;
193 // weird MEGASD cue file extensions
194 tracks[index].loop = cue_data->tracks[n].loop;
195 tracks[index].loop_lba = cue_data->tracks[n].loop_lba;
197 sprintf_lba(tmp_ext, sizeof(tmp_ext), tracks[index].start);
198 elprintf(EL_STATUS, "Track %2i: %s %9i %s %s", n, tmp_ext, length,
199 tracks[index].type ? "AUDIO" : "DATA ",
200 cue_data->tracks[n].fname ? cue_data->tracks[n].fname : "");
202 if (tracks[index].end > 99*60*75-151) {
203 tracks[index].end = 99*60*75-151;
210 /* mp3 track autosearch, Gens-like */
211 iso_name_len = strlen(cd_img_name);
212 if (iso_name_len >= sizeof(tmp_name))
213 iso_name_len = sizeof(tmp_name) - 1;
215 for (n = 2, i = 0, missed = 0; i < 100 && missed < 4; i++)
217 if (PicoCDLoadProgressCB != NULL && i > 1)
218 PicoCDLoadProgressCB(cd_img_name, i + (100-i)*missed/4);
220 for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
227 snprintf(tmp_ext, sizeof(tmp_ext), exts[j], i);
228 ext_len = strlen(tmp_ext);
229 to_upper(tmp_ext_u, tmp_ext);
231 memcpy(tmp_name, cd_img_name, iso_name_len + 1);
232 p = tmp_name + iso_name_len - 4;
235 ret = handle_mp3(tmp_name, index);
237 strcpy(p, tmp_ext_u);
238 ret = handle_mp3(tmp_name, index);
241 if (ret <= 0 && i > 1 && iso_name_len > ext_len) {
242 p = tmp_name + iso_name_len - ext_len;
244 ret = handle_mp3(tmp_name, index);
246 strcpy(p, tmp_ext_u);
247 ret = handle_mp3(tmp_name, index);
254 tracks[index].start = lba;
256 tracks[index].end = lba;
258 tracks[index].type = CT_MP3;
260 sprintf_lba(tmp_ext, sizeof(tmp_ext), tracks[index].start);
261 elprintf(EL_STATUS, "Track %2i: %s %9i AUDIO - %s",
262 n, tmp_ext, length, tmp_name);
269 if (ret <= 0 && i > 1)
271 else if (tracks[index].end > 99*60*75-151) {
272 tracks[index].end = 99*60*75-151;
278 cdd.toc.last = n - 1;
280 tracks[n].start = cdd.toc.end;
282 sprintf_lba(tmp_ext, sizeof(tmp_ext), cdd.toc.end);
283 elprintf(EL_STATUS, "End CD - %s\n", tmp_ext);
285 if (PicoCDLoadProgressCB != NULL)
286 PicoCDLoadProgressCB(cd_img_name, 100);
288 if (cue_data != NULL)
289 cdparse_destroy(cue_data);
294 // vim:shiftwidth=2:ts=2:expandtab