mp3.c 13 KB

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