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