cd_image.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * CD image handler
  3. * (C) notaz, 2007,2013
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include "../pico_int.h"
  9. #include "genplus_macros.h"
  10. #include "cdd.h"
  11. #include "cue.h"
  12. #ifdef USE_LIBRETRO_VFS
  13. #include "file_stream_transforms.h"
  14. #endif
  15. #if defined(__GNUC__) && __GNUC__ >= 7
  16. #pragma GCC diagnostic ignored "-Wformat-truncation"
  17. #endif
  18. static int handle_mp3(const char *fname, int index)
  19. {
  20. track_t *track = &cdd.toc.tracks[index];
  21. FILE *tmp_file;
  22. int kBps;
  23. int fs, ret;
  24. tmp_file = fopen(fname, "rb");
  25. if (tmp_file == NULL)
  26. return -1;
  27. ret = fseek(tmp_file, 0, SEEK_END);
  28. fs = ftell(tmp_file);
  29. fseek(tmp_file, 0, SEEK_SET);
  30. #ifdef _PSP_FW_VERSION
  31. // some systems (like PSP) can't have many open files at a time,
  32. // so we work with their names instead.
  33. fclose(tmp_file);
  34. tmp_file = (void *) strdup(fname);
  35. #endif
  36. kBps = mp3_get_bitrate(tmp_file, fs) / 8;
  37. if (ret != 0 || kBps <= 0)
  38. {
  39. elprintf(EL_STATUS, "track %2i: mp3 bitrate %i", index+1, kBps);
  40. #ifdef _PSP_FW_VERSION
  41. free(tmp_file);
  42. #else
  43. fclose(tmp_file);
  44. #endif
  45. return -1;
  46. }
  47. track->fd = tmp_file;
  48. track->offset = 0;
  49. fs *= 75;
  50. fs /= kBps * 1000;
  51. return fs;
  52. }
  53. static void to_upper(char *d, const char *s)
  54. {
  55. for (; *s != 0; d++, s++) {
  56. if ('a' <= *s && *s <= 'z')
  57. *d = *s - 'a' + 'A';
  58. else
  59. *d = *s;
  60. }
  61. *d = 0;
  62. }
  63. // cdd.c uses lba - 150
  64. static void sprintf_lba(char *buf, size_t size, int lba)
  65. {
  66. lba += 150;
  67. snprintf(buf, size, "%02d:%02d:%02d", lba / 60 / 75,
  68. (lba / 75) % 60, lba % 75);
  69. }
  70. int load_cd_image(const char *cd_img_name, int *type)
  71. {
  72. static const char *exts[] = {
  73. "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
  74. "%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3",
  75. };
  76. int i, j, n, lba, index, length, ret;
  77. int iso_name_len, missed, cd_img_sectors;
  78. char tmp_name[256], tmp_ext[10], tmp_ext_u[10];
  79. track_t *tracks = cdd.toc.tracks;
  80. cue_data_t *cue_data = NULL;
  81. pm_file *pmf;
  82. if (PicoCDLoadProgressCB != NULL)
  83. PicoCDLoadProgressCB(cd_img_name, 1);
  84. Pico_mcd->cdda_type = CT_UNKNOWN;
  85. /* is this a .cue? */
  86. cue_data = cue_parse(cd_img_name);
  87. if (cue_data != NULL) {
  88. cd_img_name = cue_data->tracks[1].fname;
  89. *type = cue_data->tracks[1].type;
  90. }
  91. pmf = pm_open(cd_img_name);
  92. if (pmf == NULL)
  93. {
  94. if (cue_data != NULL)
  95. cue_destroy(cue_data);
  96. return -1;
  97. }
  98. tracks[0].fd = pmf;
  99. if (*type == CT_ISO)
  100. cd_img_sectors = pmf->size >>= 11; // size in sectors
  101. else cd_img_sectors = pmf->size /= 2352;
  102. // cdd.c operates with lba - 150
  103. tracks[0].start = 0;
  104. tracks[0].end = cd_img_sectors;
  105. tracks[0].offset = 0;
  106. sprintf_lba(tmp_ext, sizeof(tmp_ext), 0);
  107. elprintf(EL_STATUS, "Track 1: %s %9i DATA %s",
  108. tmp_ext, tracks[0].end, cd_img_name);
  109. lba = cd_img_sectors;
  110. if (cue_data != NULL)
  111. {
  112. if (cue_data->tracks[2].fname == NULL) {
  113. // NULL fname means track2 is in same file as track1
  114. lba = tracks[0].end = cue_data->tracks[2].sector_offset;
  115. }
  116. i = 100 / cue_data->track_count + 1; // progress display
  117. for (n = 2; n <= cue_data->track_count; n++)
  118. {
  119. if (PicoCDLoadProgressCB != NULL)
  120. PicoCDLoadProgressCB(cd_img_name, i * n);
  121. index = n - 1;
  122. lba += cue_data->tracks[n].pregap;
  123. if (cue_data->tracks[n].type == CT_MP3) {
  124. ret = handle_mp3(cue_data->tracks[n].fname, index);
  125. if (ret < 0)
  126. break;
  127. length = ret;
  128. }
  129. else if (cue_data->tracks[n].fname != NULL)
  130. {
  131. pm_file *f = pm_open(cue_data->tracks[n].fname);
  132. if (f != NULL)
  133. {
  134. // assume raw, ignore header for wav..
  135. tracks[index].fd = f;
  136. tracks[index].offset = cue_data->tracks[n].sector_offset;
  137. length = f->size / 2352;
  138. }
  139. else
  140. {
  141. elprintf(EL_STATUS, "track %2i (%s): can't determine length",
  142. n, cue_data->tracks[n].fname);
  143. tracks[index].offset = 0;
  144. length = 2*75;
  145. }
  146. }
  147. else
  148. {
  149. if (n < cue_data->track_count)
  150. length = cue_data->tracks[n+1].sector_offset -
  151. cue_data->tracks[n].sector_offset;
  152. else
  153. length = cd_img_sectors - cue_data->tracks[n].sector_offset;
  154. tracks[index].offset = cue_data->tracks[n].sector_offset;
  155. }
  156. if (cue_data->tracks[n].sector_xlength != 0)
  157. // overriden by custom cue command
  158. length = cue_data->tracks[n].sector_xlength;
  159. Pico_mcd->cdda_type = cue_data->tracks[n].type;
  160. tracks[index].start = lba;
  161. lba += length;
  162. tracks[index].end = lba;
  163. sprintf_lba(tmp_ext, sizeof(tmp_ext), tracks[index].start);
  164. elprintf(EL_STATUS, "Track %2i: %s %9i AUDIO %s",
  165. n, tmp_ext, length, cue_data->tracks[n].fname);
  166. }
  167. goto finish;
  168. }
  169. /* mp3 track autosearch, Gens-like */
  170. iso_name_len = strlen(cd_img_name);
  171. if (iso_name_len >= sizeof(tmp_name))
  172. iso_name_len = sizeof(tmp_name) - 1;
  173. for (n = 2, i = 0, missed = 0; i < 100 && missed < 4; i++)
  174. {
  175. if (PicoCDLoadProgressCB != NULL && i > 1)
  176. PicoCDLoadProgressCB(cd_img_name, i + (100-i)*missed/4);
  177. for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
  178. {
  179. int ext_len;
  180. char *p;
  181. index = n - 1;
  182. snprintf(tmp_ext, sizeof(tmp_ext), exts[j], i);
  183. ext_len = strlen(tmp_ext);
  184. to_upper(tmp_ext_u, tmp_ext);
  185. memcpy(tmp_name, cd_img_name, iso_name_len + 1);
  186. p = tmp_name + iso_name_len - 4;
  187. strcpy(p, tmp_ext);
  188. ret = handle_mp3(tmp_name, index);
  189. if (ret <= 0) {
  190. strcpy(p, tmp_ext_u);
  191. ret = handle_mp3(tmp_name, index);
  192. }
  193. if (ret <= 0 && i > 1 && iso_name_len > ext_len) {
  194. p = tmp_name + iso_name_len - ext_len;
  195. strcpy(p, tmp_ext);
  196. ret = handle_mp3(tmp_name, index);
  197. if (ret <= 0) {
  198. strcpy(p, tmp_ext_u);
  199. ret = handle_mp3(tmp_name, index);
  200. }
  201. }
  202. if (ret > 0)
  203. {
  204. length = ret;
  205. tracks[index].start = lba;
  206. lba += length;
  207. tracks[index].end = lba;
  208. Pico_mcd->cdda_type = CT_MP3;
  209. sprintf_lba(tmp_ext, sizeof(tmp_ext), tracks[index].start);
  210. elprintf(EL_STATUS, "Track %2i: %s %9i AUDIO - %s",
  211. n, tmp_ext, length, tmp_name);
  212. n++;
  213. missed = 0;
  214. break;
  215. }
  216. }
  217. if (ret <= 0 && i > 1)
  218. missed++;
  219. }
  220. finish:
  221. cdd.toc.last = n - 1;
  222. cdd.toc.end = lba;
  223. sprintf_lba(tmp_ext, sizeof(tmp_ext), cdd.toc.end);
  224. elprintf(EL_STATUS, "End CD - %s\n", tmp_ext);
  225. if (PicoCDLoadProgressCB != NULL)
  226. PicoCDLoadProgressCB(cd_img_name, 100);
  227. if (cue_data != NULL)
  228. cue_destroy(cue_data);
  229. return 0;
  230. }
  231. // vim:shiftwidth=2:ts=2:expandtab