/
dreamroq-player.c
397 lines (330 loc) · 12 KB
/
dreamroq-player.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Dreamroq by Mike Melanson
* Updated by Josh Pearson to add audio support
*
* This is the sample Dreamcast player app, designed to be run under
* the KallistiOS operating system.
*/
/*
Name: Ian micheal
Copyright:
Author: Ian micheal
Date: 12/08/23 05:17
Description: kos 2.0 up port threading fix and wrappers and all warnings fixed
Redone threading and main added benchmarking for timing acia and roq decoding audio
redone rendering order and code commented to be much easier to read.
example OUTPUT:> Wait for AICA Driver: 88 ms
OUTPUT:> Wait for RoQ Decoder: 1 ms
OUTPUT:> Copy PCM Samples: 1 ms
OUTPUT:> Inform AICA Driver: 0 ms
OUTPUT:> Wait for AICA Driver: 88 ms
OUTPUT:> Wait for RoQ Decoder: 0 ms
Before
OUTPUT:> Wait for AICA Driver: 168 ms
OUTPUT:> Wait for RoQ Decoder: 0 ms
OUTPUT:> Copy PCM Samples: 1 ms
OUTPUT:> Inform AICA Driver: 0 ms
OUTPUT:> Wait for AICA Driver: 187 ms
OUTPUT:> Wait for RoQ Decoder: 0 ms
OUTPUT:> Copy PCM Samples: 1 ms
OUTPUT:> Inform AICA Driver: 0 ms
OUTPUT:> Wait for AICA Driver: 197 ms
*/
/*
Name: Ian micheal
Date: 26/08/23 20:43
Description: PVR DMA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <kos.h>
#include <dc/pvr.h>
#include <dc/maple.h>
#include <dc/maple/controller.h>
#include <kos/mutex.h>
#include <kos/thread.h>
#include "dreamroqlib.h"
#include "dc_timer.h"
#include "libdcmc/snddrv.h"
#include <dc/sound/sound.h>
#include <stdio.h>
static unsigned char *pcm_buf = NULL;
static int pcm_size = 0;
#define AUDIO_THREAD_PRIO 0
kthread_t *audio_thread; // Thread handle for the audio thread
int audio_init = 0; // Flag to indicate audio initialization status
static mutex_t pcm_mut = MUTEX_INITIALIZER;
/* Video Global variables */
static pvr_ptr_t textures[2];
static int current_frame = 0;
static int graphics_initialized = 0;
static float video_delay;
// Define the target frame rate
#define TARGET_FRAME_RATE 31
#define MB (1024*1024)
#define PVR_DMA_WAIT() while(!pvr_dma_ready());
// Define a preprocessor macro for enabling or disabling debugging
#define DEBUG_SND_THD 0 // Set to 1 to enable debugging, 0 to disable
static void snd_thd()
{
do
{
unsigned int start_time, end_time;
// Measure time taken by waiting for AICA Driver request
start_time = dc_get_time();
while (snddrv.buf_status != SNDDRV_STATUS_NEEDBUF)
thd_pass();
end_time = dc_get_time();
#if DEBUG_SND_THD
printf("Wait for AICA Driver: %u ms\n", end_time - start_time);
#endif
// Measure time taken by waiting for RoQ Decoder
start_time = dc_get_time();
while (pcm_size < snddrv.pcm_needed)
{
if (snddrv.dec_status == SNDDEC_STATUS_DONE)
goto done;
thd_pass();
}
end_time = dc_get_time();
#if DEBUG_SND_THD
printf("Wait for RoQ Decoder: %u ms\n", end_time - start_time);
#endif
// Measure time taken by copying PCM samples
start_time = dc_get_time();
mutex_lock(&pcm_mut);
memcpy(snddrv.pcm_buffer, pcm_buf, snddrv.pcm_needed);
pcm_size -= snddrv.pcm_needed;
memmove(pcm_buf, pcm_buf + snddrv.pcm_needed, pcm_size);
mutex_unlock(&pcm_mut);
end_time = dc_get_time();
#if DEBUG_SND_THD
printf("Copy PCM Samples: %u ms\n", end_time - start_time);
#endif
// Measure time taken by informing AICA Driver
start_time = dc_get_time();
snddrv.buf_status = SNDDRV_STATUS_HAVEBUF;
end_time = dc_get_time();
#if DEBUG_SND_THD
printf("Inform AICA Driver: %u ms\n", end_time - start_time);
#endif
} while (snddrv.dec_status == SNDDEC_STATUS_STREAMING);
done:
snddrv.dec_status = SNDDEC_STATUS_NULL;
}
static int render_cb(unsigned short *buf, int width, int height, int stride,
int texture_height)
{
pvr_poly_cxt_t cxt;
static pvr_poly_hdr_t hdr[2];
static pvr_vertex_t vert[4]; // Keep 4 vertices for the main frame
float ratio;
// screen coordinates of upper left and bottom right corners
static int ul_x, ul_y, br_x, br_y;
// Initialize textures, drawing coordinates, and other parameters
if (!graphics_initialized)
{
textures[0] = pvr_mem_malloc(stride * texture_height * 2);
textures[1] = pvr_mem_malloc(stride * texture_height * 2);
if (!textures[0] || !textures[1])
{
return ROQ_RENDER_PROBLEM;
}
// Precompile the poly headers
for (int i = 0; i < 2; i++) {
pvr_poly_cxt_txr(&cxt, PVR_LIST_OP_POLY, PVR_TXRFMT_RGB565 | PVR_TXRFMT_NONTWIDDLED,
stride, texture_height, textures[i], PVR_FILTER_BILINEAR);
pvr_poly_compile(&hdr[i], &cxt);
}
// Calculate drawing coordinates
ratio = 640.0 / width;
// Calculate drawing coordinates with increased coverage at the bottom
// Ian micheal added mask to bottom of screen hiding the glitch
ul_x = 0;
br_x = (int)(ratio * stride);
ul_y = (int)((480 - ratio * height) / 2);
br_y = ul_y + (int)(ratio * texture_height) + 20; // Increase the coverage by adding 20 pixels to the height
// Set common vertex properties
for (int i = 0; i < 4; i++) {
vert[i].z = 1.0f;
vert[i].argb = PVR_PACK_COLOR(1.0f, 1.0f, 1.0f, 1.0f); // White color for the main frame
vert[i].oargb = 0;
vert[i].flags = (i < 3) ? PVR_CMD_VERTEX : PVR_CMD_VERTEX_EOL;
}
// Initialize vertex coordinates and UV coordinates
vert[0].x = ul_x;
vert[0].y = ul_y;
vert[0].u = 0.0;
vert[0].v = 0.0;
vert[1].x = br_x;
vert[1].y = ul_y;
vert[1].u = 1.0;
vert[1].v = 0.0;
vert[2].x = ul_x;
vert[2].y = br_y;
vert[2].u = 0.0;
vert[2].v = 1.0;
vert[3].x = br_x;
vert[3].y = br_y;
vert[3].u = 1.0;
vert[3].v = 1.0;
// Get the current hardware timing
video_delay = (float)dc_get_time();
graphics_initialized = 1;
}
// PVR_DMA_WAIT();
//
//TapamN
//If OCI is used, purge only the correct side of the cache
//If OCI is not used, pctPurgeCache will purge the entire cache regardless of parameter
// Prolly not using this correct just here nothing bad happens when i use it this way.
pctPurgeCache((uintptr_t)buf & (1<<25) ? PCT_PURGE_OCI1 : PCT_PURGE_OCI0);
// dcache_flush_range((uintptr_t)buf, stride * texture_height * 2); Correct TapamN But causes glitch in sound when tales apears
dcache_flush_range((uintptr_t)textures[current_frame], (uintptr_t)(textures[current_frame] + stride * texture_height * 2));// wrong but works better no glitch in sound have to find out why?
// Send the video frame as a texture over to video RAM
pvr_txr_load_dma(buf, textures[current_frame], stride * texture_height * 2, 1, NULL, 0);
// Calculate the elapsed time since the last frame
unsigned int current_time = dc_get_time();
unsigned int elapsed_time = current_time - video_delay;
unsigned int target_frame_time = 1000 / TARGET_FRAME_RATE;
// If the elapsed time is less than the target frame time, introduce a delay
if (elapsed_time < target_frame_time) {
unsigned int delay_time = target_frame_time - elapsed_time;
thd_sleep(delay_time);
}
pvr_wait_ready();
// pvr_check_ready (); uncomment for perfect sync
pvr_scene_begin();
pvr_list_begin(PVR_LIST_OP_POLY);
// Render the frame using precompiled headers and vertices
pvr_prim(&hdr[current_frame], sizeof(pvr_poly_hdr_t));
for (int i = 0; i < 4; i++) {
pvr_prim(&vert[i], sizeof(pvr_vertex_t));
}
pvr_list_finish();
pvr_scene_finish();
// Update the hardware timing for the current frame
video_delay = (float)current_time;
// Toggle between frames
current_frame = 1 - current_frame;
return ROQ_SUCCESS;
}
static int audio_cb(unsigned char *buf, int size, int channels)
{
// Copy the decoded PCM samples to our local PCM buffer
mutex_lock(&pcm_mut);
memcpy(pcm_buf + pcm_size, buf, size);
pcm_size += size;
mutex_unlock(&pcm_mut);
return ROQ_SUCCESS;
}
// Audio thread function
static void *snd_thd_wrapper(void *arg)
{
printf("Audio Thread: Started\n");
unsigned int start_time = dc_get_time();
// Call the actual audio thread function
snd_thd();
unsigned int end_time = dc_get_time();
unsigned int elapsed_time = end_time - start_time;
// printf("Audio Thread: Finished (Time: %u ms)\n", elapsed_time);
return NULL;
}
static int quit_cb()
{
// Check if the video has ended and the audio decoding status is done
if (snddrv.dec_status == SNDDEC_STATUS_DONE) {
printf("Exiting due to audio decoding status\n");
return 1; // Exit the loop
}
// Check if the "Start" button is pressed
MAPLE_FOREACH_BEGIN(MAPLE_FUNC_CONTROLLER, cont_state_t, st)
if (st->buttons & CONT_START) {
printf("Exiting due to Start button\n");
return 1; // Exit the loop
}
MAPLE_FOREACH_END()
// Continue the loop
return 0;
}
pvr_init_params_t params = {
{ PVR_BINSIZE_32, PVR_BINSIZE_0, PVR_BINSIZE_0, PVR_BINSIZE_0, PVR_BINSIZE_0 },
2.6 * MB,
0, //dma
0, //fsaa
1 //autosort disable
};
KOS_INIT_FLAGS(INIT_DEFAULT|INIT_MALLOCSTATS);
int main()
{
int status = 0;
vid_set_mode(DM_640x480, PM_RGB565);
pvr_init(¶ms); /* Init the PVR */
printf("dreamroq_play(C) Multimedia Mike Melanson & Josh PH3NOM Pearson 2011\n");
printf("dreamroq_play(C) Ian micheal Up port to Kos2.0 sound fix and threading\n");
printf("dreamroq_play(C) Ian micheal Kos2.0 free and exit when loop ends 2023\n");
printf("dreamroq_play(C) Ian micheal kos filesystem pvr dma 2023 8/26/2023\n");
// Initialize audio resources and create the audio thread
if (!audio_init)
{
pcm_buf = malloc(PCM_BUF_SIZE);
if (pcm_buf == NULL)
{
printf("Failed to allocate PCM buffer\n");
return 1;
}
snddrv_start(22050, 2);
snddrv.dec_status = SNDDEC_STATUS_STREAMING;
printf("Creating Audio Thread\n");
audio_thread = thd_create(AUDIO_THREAD_PRIO, snd_thd_wrapper, NULL);
if (!audio_thread)
{
printf("Failed to create audio thread\n");
free(pcm_buf);
pcm_buf = NULL;
return 1;
}
audio_init = 1;
}
/* To disable a callback, simply replace the function name by 0 */
status = dreamroq_play("/cd/movie.roq", 0, render_cb, audio_cb, quit_cb);
printf("dreamroq_play() status = %d\n", status);
// Terminate and clean up the audio thread
if (audio_init)
{
snddrv.dec_status = SNDDEC_STATUS_DONE;
while (snddrv.dec_status != SNDDEC_STATUS_NULL)
{
snddrv_exit();
snd_stream_shutdown();
thd_sleep(50);
printf("Waiting for audio thread to finish...\n");
fflush(stdout); // Flush the output buffer to ensure immediate display
}
mutex_unlock(&pcm_mut);
thd_destroy(audio_thread); // Destroy the audio thread
printf("Destroy the audio thread...\n");
free(pcm_buf);
pcm_buf = NULL;
pcm_size = 0;
}
if (graphics_initialized)
{ vid_clear(0,0,0);
pvr_mem_free(textures[0]);
pvr_mem_free(textures[1]);
printf("Freed PVR memory\n");
fflush(stdout); // Flush the output buffer to ensure immediate display
thd_sleep(10);
}
thd_sleep(2000);
printf("Exiting main()\n");
fflush(stdout); // Flush the output buffer to ensure immediate display
pvr_shutdown(); // Clean up PVR resources
vid_shutdown(); // This function reinitializes the video system to what dcload and friends expect it to be.
malloc_stats();
//Ending
spu_disable();
arch_exit();
return 0;
}