mp3.c 12 KB

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