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 static int handle_mp3(const char *fname, int index)
16 track_t *track = &cdd.toc.tracks[index];
21 tmp_file = fopen(fname, "rb");
25 ret = fseek(tmp_file, 0, SEEK_END);
27 fseek(tmp_file, 0, SEEK_SET);
29 #ifdef _PSP_FW_VERSION
30 // some systems (like PSP) can't have many open files at a time,
31 // so we work with their names instead.
33 tmp_file = (void *) strdup(fname);
36 kBps = mp3_get_bitrate(tmp_file, fs) / 8;
37 if (ret != 0 || kBps <= 0)
39 elprintf(EL_STATUS, "track %2i: mp3 bitrate %i", index+1, kBps);
40 #ifdef _PSP_FW_VERSION
56 static void to_upper(char *d, const char *s)
58 for (; *s != 0; d++, s++) {
59 if ('a' <= *s && *s <= 'z')
66 // cdd.c uses lba - 150
67 static void sprintf_lba(char *buf, size_t size, int lba)
70 snprintf(buf, size, "%02d:%02d:%02d", lba / 60 / 75,
71 (lba / 75) % 60, lba % 75);
74 int load_cd_image(const char *cd_img_name, int *type)
76 static const char *exts[] = {
77 "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
78 "%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3",
80 int i, j, n, lba, index, length, ret;
81 int iso_name_len, missed, cd_img_sectors;
82 char tmp_name[256], tmp_ext[10], tmp_ext_u[10];
83 track_t *tracks = cdd.toc.tracks;
84 cue_data_t *cue_data = NULL;
87 if (PicoCDLoadProgressCB != NULL)
88 PicoCDLoadProgressCB(cd_img_name, 1);
90 Pico_mcd->cdda_type = CT_UNKNOWN;
93 cue_data = cue_parse(cd_img_name);
94 if (cue_data != NULL) {
95 cd_img_name = cue_data->tracks[1].fname;
96 *type = cue_data->tracks[1].type;
99 pmf = pm_open(cd_img_name);
102 if (cue_data != NULL)
103 cue_destroy(cue_data);
109 cd_img_sectors = pmf->size >>= 11; // size in sectors
110 else cd_img_sectors = pmf->size /= 2352;
112 // cdd.c operates with lba - 150
114 tracks[0].end = cd_img_sectors;
115 tracks[0].offset = 0;
117 sprintf_lba(tmp_ext, sizeof(tmp_ext), 0);
118 elprintf(EL_STATUS, "Track 1: %s %9i DATA %s",
119 tmp_ext, tracks[0].end, cd_img_name);
121 lba = cd_img_sectors;
123 if (cue_data != NULL)
125 if (cue_data->tracks[2].fname == NULL) {
126 // NULL fname means track2 is in same file as track1
127 lba = tracks[0].end = cue_data->tracks[2].sector_offset;
129 i = 100 / cue_data->track_count + 1; // progress display
131 for (n = 2; n <= cue_data->track_count; n++)
133 if (PicoCDLoadProgressCB != NULL)
134 PicoCDLoadProgressCB(cd_img_name, i * n);
137 lba += cue_data->tracks[n].pregap;
138 if (cue_data->tracks[n].type == CT_MP3) {
139 ret = handle_mp3(cue_data->tracks[n].fname, index);
144 else if (cue_data->tracks[n].fname != NULL)
146 pm_file *f = pm_open(cue_data->tracks[n].fname);
149 // assume raw, ignore header for wav..
150 tracks[index].fd = f;
151 tracks[index].offset = cue_data->tracks[n].sector_offset;
152 length = f->size / 2352;
156 elprintf(EL_STATUS, "track %2i (%s): can't determine length",
157 n, cue_data->tracks[n].fname);
158 tracks[index].offset = 0;
164 if (n < cue_data->track_count)
165 length = cue_data->tracks[n+1].sector_offset -
166 cue_data->tracks[n].sector_offset;
168 length = cd_img_sectors - cue_data->tracks[n].sector_offset;
169 tracks[index].offset = cue_data->tracks[n].sector_offset;
172 if (cue_data->tracks[n].sector_xlength != 0)
173 // overriden by custom cue command
174 length = cue_data->tracks[n].sector_xlength;
176 Pico_mcd->cdda_type = cue_data->tracks[n].type;
178 tracks[index].start = lba;
180 tracks[index].end = lba;
182 sprintf_lba(tmp_ext, sizeof(tmp_ext), tracks[index].start);
183 elprintf(EL_STATUS, "Track %2i: %s %9i AUDIO %s",
184 n, tmp_ext, length, cue_data->tracks[n].fname);
186 cue_destroy(cue_data);
190 /* mp3 track autosearch, Gens-like */
191 iso_name_len = strlen(cd_img_name);
192 if (iso_name_len >= sizeof(tmp_name))
193 iso_name_len = sizeof(tmp_name) - 1;
195 for (n = 2, i = 0, missed = 0; i < 100 && missed < 4; i++)
197 if (PicoCDLoadProgressCB != NULL && i > 1)
198 PicoCDLoadProgressCB(cd_img_name, i + (100-i)*missed/4);
200 for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
207 snprintf(tmp_ext, sizeof(tmp_ext), exts[j], i);
208 ext_len = strlen(tmp_ext);
209 to_upper(tmp_ext_u, tmp_ext);
211 memcpy(tmp_name, cd_img_name, iso_name_len + 1);
212 p = tmp_name + iso_name_len - 4;
215 ret = handle_mp3(tmp_name, index);
217 strcpy(p, tmp_ext_u);
218 ret = handle_mp3(tmp_name, index);
221 if (ret <= 0 && i > 1 && iso_name_len > ext_len) {
222 p = tmp_name + iso_name_len - ext_len;
224 ret = handle_mp3(tmp_name, index);
226 strcpy(p, tmp_ext_u);
227 ret = handle_mp3(tmp_name, index);
234 tracks[index].start = lba;
236 tracks[index].end = lba;
238 Pico_mcd->cdda_type = CT_MP3;
240 sprintf_lba(tmp_ext, sizeof(tmp_ext), tracks[index].start);
241 elprintf(EL_STATUS, "Track %2i: %s %9i AUDIO - %s",
242 n, tmp_ext, length, tmp_name);
249 if (ret <= 0 && i > 1)
254 cdd.toc.last = n - 1;
257 sprintf_lba(tmp_ext, sizeof(tmp_ext), cdd.toc.end);
258 elprintf(EL_STATUS, "End CD - %s\n", tmp_ext);
260 if (PicoCDLoadProgressCB != NULL)
261 PicoCDLoadProgressCB(cd_img_name, 100);
266 // vim:shiftwidth=2:ts=2:expandtab