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