mp3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2007,2008
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <pspkernel.h>
  11. #include <pspsdk.h>
  12. #include <pspaudiocodec.h>
  13. #include <kubridge.h>
  14. #include "../../pico/pico_int.h"
  15. #include "../../pico/sound/mix.h"
  16. #include "../common/lprintf.h"
  17. int mp3_last_error = 0;
  18. static int initialized = 0;
  19. static SceUID thread_job_sem = -1;
  20. static SceUID thread_busy_sem = -1;
  21. static int thread_exit = 0;
  22. // MPEG-1, layer 3
  23. static int bitrates[] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0 };
  24. //static int samplerates[] = { 44100, 48000, 32000, 0 };
  25. #define MIN_INFRAME_SIZE 96
  26. #define IN_BUFFER_SIZE (2*1024)
  27. static unsigned long mp3_codec_struct[65] __attribute__((aligned(64)));
  28. static unsigned char mp3_src_buffer[2][IN_BUFFER_SIZE] __attribute__((aligned(64)));
  29. static short mp3_mix_buffer[2][1152*2] __attribute__((aligned(64)));
  30. static int working_buf = 0;
  31. static const char *mp3_fname = NULL;
  32. static SceUID mp3_handle = -1;
  33. static int mp3_src_pos = 0, mp3_src_size = 0;
  34. static int decode_thread(SceSize args, void *argp);
  35. static void psp_sem_lock(SceUID sem)
  36. {
  37. int ret = sceKernelWaitSema(sem, 1, 0);
  38. if (ret < 0) lprintf("sceKernelWaitSema(%08x) failed with %08x\n", sem, ret);
  39. }
  40. static void psp_sem_unlock(SceUID sem)
  41. {
  42. int ret = sceKernelSignalSema(sem, 1);
  43. if (ret < 0) lprintf("sceKernelSignalSema(%08x) failed with %08x\n", sem, ret);
  44. }
  45. // only accepts MPEG-1, layer3
  46. static int find_sync_word(unsigned char *data, int len)
  47. {
  48. int i;
  49. for (i = 0; i < len-1; i++)
  50. {
  51. if ( data[i+0] != 0xff) continue;
  52. if ((data[i+1] & 0xfe) == 0xfa) return i;
  53. i++;
  54. }
  55. return -1;
  56. }
  57. static int read_next_frame(int which_buffer)
  58. {
  59. int i, bytes_read, frame_offset;
  60. int bitrate, padding, frame_size = 0;
  61. for (i = 0; i < 32; i++)
  62. {
  63. bytes_read = sceIoRead(mp3_handle, mp3_src_buffer[which_buffer], sizeof(mp3_src_buffer[which_buffer]));
  64. mp3_src_pos += bytes_read;
  65. if (bytes_read < MIN_INFRAME_SIZE) {
  66. mp3_src_pos = mp3_src_size;
  67. return 0; // EOF/IO failure
  68. }
  69. frame_offset = find_sync_word(mp3_src_buffer[which_buffer], bytes_read);
  70. if (frame_offset < 0) {
  71. lprintf("missing syncword, foffs=%i\n", mp3_src_pos - bytes_read);
  72. mp3_src_pos--;
  73. sceIoLseek32(mp3_handle, mp3_src_pos, PSP_SEEK_SET);
  74. continue;
  75. }
  76. if (bytes_read - frame_offset < 4) {
  77. lprintf("syncword @ EOB, foffs=%i\n", mp3_src_pos - bytes_read);
  78. mp3_src_pos--;
  79. sceIoLseek32(mp3_handle, mp3_src_pos, PSP_SEEK_SET);
  80. continue;
  81. }
  82. bitrate = mp3_src_buffer[which_buffer][frame_offset+2] >> 4;
  83. padding = (mp3_src_buffer[which_buffer][frame_offset+2] & 2) >> 1;
  84. frame_size = 144000*bitrates[bitrate]/44100 + padding;
  85. if (frame_size <= 0) {
  86. lprintf("bad frame, foffs=%i\n", mp3_src_pos - bytes_read);
  87. continue; // bad frame
  88. }
  89. if (bytes_read - frame_offset < frame_size)
  90. {
  91. lprintf("unfit, foffs=%i\n", mp3_src_pos - bytes_read);
  92. mp3_src_pos -= bytes_read - frame_offset;
  93. if (mp3_src_size - mp3_src_pos < frame_size) {
  94. mp3_src_pos = mp3_src_size;
  95. return 0; // EOF
  96. }
  97. sceIoLseek32(mp3_handle, mp3_src_pos, PSP_SEEK_SET);
  98. continue; // didn't fit, re-read..
  99. }
  100. if (frame_offset) {
  101. //lprintf("unaligned, foffs=%i, offs=%i\n", mp3_src_pos - bytes_read, frame_offset);
  102. memmove(mp3_src_buffer[which_buffer], mp3_src_buffer[which_buffer] + frame_offset, frame_size);
  103. }
  104. // align for next frame read
  105. mp3_src_pos -= bytes_read - (frame_offset + frame_size);
  106. sceIoLseek32(mp3_handle, mp3_src_pos, PSP_SEEK_SET);
  107. break;
  108. }
  109. return frame_size > 0 ? frame_size : -1;
  110. }
  111. static SceUID load_start_module(const char *prxname)
  112. {
  113. SceUID mod, mod1;
  114. int status, ret;
  115. mod = pspSdkLoadStartModule(prxname, PSP_MEMORY_PARTITION_KERNEL);
  116. if (mod < 0) {
  117. lprintf("failed to load %s (%08x), trying kuKernelLoadModule\n", prxname, mod);
  118. mod1 = kuKernelLoadModule(prxname, 0, NULL);
  119. if (mod1 < 0) lprintf("kuKernelLoadModule failed with %08x\n", mod1);
  120. else {
  121. ret = sceKernelStartModule(mod1, 0, NULL, &status, 0);
  122. if (ret < 0) lprintf("sceKernelStartModule failed with %08x\n", ret);
  123. else mod = mod1;
  124. }
  125. }
  126. return mod;
  127. }
  128. int mp3_init(void)
  129. {
  130. SceUID thid, mod;
  131. int ret;
  132. /* load modules */
  133. /* <= 1.5 (and probably some other, not sure which) fw need this to for audiocodec to work,
  134. * so if it fails, assume we are just on new enough firmware and continue.. */
  135. load_start_module("flash0:/kd/me_for_vsh.prx");
  136. if (sceKernelDevkitVersion() < 0x02070010)
  137. mod = load_start_module("flash0:/kd/audiocodec.prx");
  138. else mod = load_start_module("flash0:/kd/avcodec.prx");
  139. if (mod < 0) {
  140. ret = mod;
  141. mod = load_start_module("flash0:/kd/audiocodec_260.prx"); // last chance..
  142. if (mod < 0) goto fail;
  143. }
  144. /* audiocodec init */
  145. memset(mp3_codec_struct, 0, sizeof(mp3_codec_struct));
  146. ret = sceAudiocodecCheckNeedMem(mp3_codec_struct, 0x1002);
  147. if (ret < 0) {
  148. lprintf("sceAudiocodecCheckNeedMem failed with %08x\n", ret);
  149. goto fail;
  150. }
  151. ret = sceAudiocodecGetEDRAM(mp3_codec_struct, 0x1002);
  152. if (ret < 0) {
  153. lprintf("sceAudiocodecGetEDRAM failed with %08x\n", ret);
  154. goto fail;
  155. }
  156. ret = sceAudiocodecInit(mp3_codec_struct, 0x1002);
  157. if (ret < 0) {
  158. lprintf("sceAudiocodecInit failed with %08x\n", ret);
  159. goto fail1;
  160. }
  161. /* thread and stuff */
  162. thread_job_sem = sceKernelCreateSema("p_mp3job_sem", 0, 0, 1, NULL);
  163. if (thread_job_sem < 0) {
  164. lprintf("sceKernelCreateSema() failed: %08x\n", thread_job_sem);
  165. ret = thread_job_sem;
  166. goto fail1;
  167. }
  168. thread_busy_sem = sceKernelCreateSema("p_mp3busy_sem", 0, 1, 1, NULL);
  169. if (thread_busy_sem < 0) {
  170. lprintf("sceKernelCreateSema() failed: %08x\n", thread_busy_sem);
  171. ret = thread_busy_sem;
  172. goto fail2;
  173. }
  174. /* use slightly higher prio then main */
  175. thread_exit = 0;
  176. thid = sceKernelCreateThread("mp3decode_thread", decode_thread, 30, 0x2000, 0, NULL);
  177. if (thid < 0) {
  178. lprintf("failed to create decode thread: %08x\n", thid);
  179. ret = thid;
  180. goto fail3;
  181. }
  182. ret = sceKernelStartThread(thid, 0, 0);
  183. if (ret < 0) {
  184. lprintf("failed to start decode thread: %08x\n", ret);
  185. goto fail3;
  186. }
  187. mp3_last_error = 0;
  188. initialized = 1;
  189. return 0;
  190. fail3:
  191. sceKernelDeleteSema(thread_busy_sem);
  192. thread_busy_sem = -1;
  193. fail2:
  194. sceKernelDeleteSema(thread_job_sem);
  195. thread_job_sem = -1;
  196. fail1:
  197. sceAudiocodecReleaseEDRAM(mp3_codec_struct);
  198. fail:
  199. mp3_last_error = ret;
  200. initialized = 0;
  201. return 1;
  202. }
  203. void mp3_deinit(void)
  204. {
  205. lprintf("mp3_deinit, initialized=%i\n", initialized);
  206. if (!initialized) return;
  207. thread_exit = 1;
  208. psp_sem_lock(thread_busy_sem);
  209. psp_sem_unlock(thread_busy_sem);
  210. sceKernelSignalSema(thread_job_sem, 1);
  211. sceKernelDelayThread(100*1000);
  212. if (mp3_handle >= 0) sceIoClose(mp3_handle);
  213. mp3_handle = -1;
  214. mp3_fname = NULL;
  215. sceKernelDeleteSema(thread_busy_sem);
  216. thread_busy_sem = -1;
  217. sceKernelDeleteSema(thread_job_sem);
  218. thread_job_sem = -1;
  219. sceAudiocodecReleaseEDRAM(mp3_codec_struct);
  220. initialized = 0;
  221. }
  222. // may overflow stack?
  223. static int decode_thread(SceSize args, void *argp)
  224. {
  225. int ret, frame_size;
  226. lprintf("decode_thread started with id %08x, priority %i\n",
  227. sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority());
  228. while (!thread_exit)
  229. {
  230. psp_sem_lock(thread_job_sem);
  231. if (thread_exit) break;
  232. psp_sem_lock(thread_busy_sem);
  233. //lprintf("{ job\n");
  234. frame_size = read_next_frame(working_buf);
  235. if (frame_size > 0)
  236. {
  237. mp3_codec_struct[6] = (unsigned long)mp3_src_buffer[working_buf];
  238. mp3_codec_struct[8] = (unsigned long)mp3_mix_buffer[working_buf];
  239. mp3_codec_struct[7] = mp3_codec_struct[10] = frame_size;
  240. mp3_codec_struct[9] = 1152 * 4;
  241. ret = sceAudiocodecDecode(mp3_codec_struct, 0x1002);
  242. if (ret < 0) lprintf("sceAudiocodecDecode failed with %08x\n", ret);
  243. }
  244. //lprintf("} job\n");
  245. psp_sem_unlock(thread_busy_sem);
  246. }
  247. lprintf("leaving decode thread\n");
  248. sceKernelExitDeleteThread(0);
  249. return 0;
  250. }
  251. // might be called before initialization
  252. int mp3_get_bitrate(void *f, int size)
  253. {
  254. int ret, retval = -1, sample_rate, bitrate;
  255. // filenames are stored instead handles in PSP, due to stupid max open file limit
  256. char *fname = f;
  257. /* make sure thread is not busy.. */
  258. if (thread_busy_sem >= 0)
  259. psp_sem_lock(thread_busy_sem);
  260. if (mp3_handle >= 0) sceIoClose(mp3_handle);
  261. mp3_handle = sceIoOpen(fname, PSP_O_RDONLY, 0777);
  262. if (mp3_handle < 0) {
  263. lprintf("sceIoOpen(%s) failed\n", fname);
  264. goto end;
  265. }
  266. mp3_src_pos = 0;
  267. ret = read_next_frame(0);
  268. if (ret <= 0) {
  269. lprintf("read_next_frame() failed (%s)\n", fname);
  270. goto end;
  271. }
  272. sample_rate = (mp3_src_buffer[0][2] & 0x0c) >> 2;
  273. bitrate = mp3_src_buffer[0][2] >> 4;
  274. if (sample_rate != 0) {
  275. lprintf("unsupported samplerate (%s)\n", fname);
  276. goto end; // only 44kHz supported..
  277. }
  278. bitrate = bitrates[bitrate];
  279. if (bitrate == 0) {
  280. lprintf("unsupported bitrate (%s)\n", fname);
  281. goto end;
  282. }
  283. /* looking good.. */
  284. retval = bitrate;
  285. end:
  286. if (mp3_handle >= 0) sceIoClose(mp3_handle);
  287. mp3_handle = -1;
  288. mp3_fname = NULL;
  289. if (thread_busy_sem >= 0)
  290. psp_sem_unlock(thread_busy_sem);
  291. if (retval < 0) mp3_last_error = -1; // remember we had a problem..
  292. return retval;
  293. }
  294. static int mp3_job_started = 0, mp3_samples_ready = 0, mp3_buffer_offs = 0, mp3_play_bufsel = 0;
  295. void mp3_start_play(void *f, int pos)
  296. {
  297. char *fname = f;
  298. if (!initialized) return;
  299. lprintf("mp3_start_play(%s) @ %i\n", fname, pos);
  300. psp_sem_lock(thread_busy_sem);
  301. if (mp3_fname != fname || mp3_handle < 0)
  302. {
  303. if (mp3_handle >= 0) sceIoClose(mp3_handle);
  304. mp3_handle = sceIoOpen(fname, PSP_O_RDONLY, 0777);
  305. if (mp3_handle < 0) {
  306. lprintf("sceIoOpen(%s) failed\n", fname);
  307. psp_sem_unlock(thread_busy_sem);
  308. return;
  309. }
  310. mp3_src_size = sceIoLseek32(mp3_handle, 0, PSP_SEEK_END);
  311. mp3_fname = fname;
  312. }
  313. // clear decoder state
  314. sceAudiocodecInit(mp3_codec_struct, 0x1002);
  315. // seek..
  316. mp3_src_pos = (int) (((float)pos / 1023.0f) * (float)mp3_src_size);
  317. sceIoLseek32(mp3_handle, mp3_src_pos, PSP_SEEK_SET);
  318. lprintf("seek %i: %i/%i\n", pos, mp3_src_pos, mp3_src_size);
  319. mp3_job_started = 1;
  320. mp3_samples_ready = mp3_buffer_offs = mp3_play_bufsel = 0;
  321. working_buf = 0;
  322. /* send a request to decode first frame */
  323. psp_sem_unlock(thread_busy_sem);
  324. psp_sem_unlock(thread_job_sem);
  325. sceKernelDelayThread(1); // reschedule
  326. }
  327. void mp3_update(int *buffer, int length, int stereo)
  328. {
  329. int length_mp3;
  330. // playback was started, track not ended
  331. if (mp3_handle < 0 || mp3_src_pos >= mp3_src_size) return;
  332. length_mp3 = length;
  333. if (PsndRate == 22050) length_mp3 <<= 1; // mp3s are locked to 44100Hz stereo
  334. else if (PsndRate == 11025) length_mp3 <<= 2; // so make length 44100ish
  335. /* do we have to wait? */
  336. if (mp3_job_started && mp3_samples_ready < length_mp3)
  337. {
  338. psp_sem_lock(thread_busy_sem);
  339. psp_sem_unlock(thread_busy_sem);
  340. mp3_job_started = 0;
  341. mp3_samples_ready += 1152;
  342. }
  343. /* mix mp3 data, only stereo */
  344. if (mp3_samples_ready >= length_mp3)
  345. {
  346. int shr = 0;
  347. void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
  348. if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; shr = 1; }
  349. else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; shr = 2; }
  350. if (1152 - mp3_buffer_offs >= length_mp3) {
  351. mix_samples(buffer, mp3_mix_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length<<1);
  352. mp3_buffer_offs += length_mp3;
  353. } else {
  354. // collect samples from both buffers..
  355. int left = 1152 - mp3_buffer_offs;
  356. if (mp3_play_bufsel == 0)
  357. {
  358. mix_samples(buffer, mp3_mix_buffer[0] + mp3_buffer_offs*2, length<<1);
  359. mp3_buffer_offs = length_mp3 - left;
  360. mp3_play_bufsel = 1;
  361. } else {
  362. mix_samples(buffer, mp3_mix_buffer[1] + mp3_buffer_offs*2, (left>>shr)<<1);
  363. mp3_buffer_offs = length_mp3 - left;
  364. mix_samples(buffer + ((left>>shr)<<1),
  365. mp3_mix_buffer[0], (mp3_buffer_offs>>shr)<<1);
  366. mp3_play_bufsel = 0;
  367. }
  368. }
  369. mp3_samples_ready -= length_mp3;
  370. }
  371. // ask to decode more if we already can
  372. if (!mp3_job_started)
  373. {
  374. mp3_job_started = 1;
  375. working_buf ^= 1;
  376. /* next job.. */
  377. psp_sem_lock(thread_busy_sem); // just in case
  378. psp_sem_unlock(thread_busy_sem);
  379. psp_sem_unlock(thread_job_sem);
  380. sceKernelDelayThread(1);
  381. }
  382. }
  383. int mp3_get_offset(void) // 0-1023
  384. {
  385. unsigned int offs1024 = 0;
  386. int cdda_on;
  387. cdda_on = (PicoIn.AHW & PAHW_MCD) && (PicoIn.opt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&
  388. (Pico_mcd->scd.Status_CDC & 1) && mp3_handle >= 0;
  389. if (cdda_on) {
  390. offs1024 = mp3_src_pos << 7;
  391. offs1024 /= mp3_src_size >> 3;
  392. }
  393. lprintf("offs1024=%u (%i/%i)\n", offs1024, mp3_src_pos, mp3_src_size);
  394. return offs1024;
  395. }
  396. void mp3_reopen_file(void)
  397. {
  398. if (mp3_fname == NULL) return;
  399. lprintf("mp3_reopen_file(%s)\n", mp3_fname);
  400. // try closing, just in case
  401. if (mp3_handle >= 0) sceIoClose(mp3_handle);
  402. mp3_handle = sceIoOpen(mp3_fname, PSP_O_RDONLY, 0777);
  403. if (mp3_handle >= 0)
  404. sceIoLseek32(mp3_handle, mp3_src_pos, PSP_SEEK_SET);
  405. lprintf("mp3_reopen_file %s\n", mp3_handle >= 0 ? "ok" : "failed");
  406. }