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