This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
forked from danbolt/LunarAssault64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhvqmmain.c
437 lines (379 loc) · 13.9 KB
/
hvqmmain.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
hvqmmain.c
*/
#include "hvqm.h"
/* Global variables */
volatile u32 hvqmStatus=0; /* status variable */
u32 hvqmOffset; /* Position of image */
u32 hvqmTotalFrame; /* HVQM data's total number of frames */
void *hvqmVideoStream; /* HVQM data's video playback position */
void *hvqmAudioStream; /* HVQM data's audio playback position */
u32 hvqmFrameRate; /* HVQM data's frame rate per usec */
u32 hvqmAudioRate; /* HVQM data's audio playback frequency */
u32 hvqmTotalAudio; /* HVQM data's total number of audio records */
u32 hvqmVideoRemain; /* HVQM data's number of remaining frames */
u32 hvqmAudioRemain; /* HVQM data's number of remaining frames */
/* Local variables */
static u8 *hvqmRomAddress; /* Starting address of HVQM data */
static u8 hvqmHeaderBuf[sizeof(HVQM2Header)+16]; /* HVQM data header */
static HVQM2Header *hvqmHeader; /* Pointer to HVQM data header */
static u64 hvqmEndTime; /* End time */
static u64 hvqmNowTime; /* Present time */
static u8 hvqmRecordHeaderBuf[sizeof(HVQM2Record)+16]; /* Video header area */
static HVQM2Record *hvqmRecordHeader; /* Pointer to video header */
/* Task-related */
static HVQM2Arg hvqmTaskArg; /* Parameters for the HVQM2 microcode */
static HVQM2Info hvqmSPFifo[HVQM_SPFIFO_SIZE];
/* Internal function prototypes */
static void hvqmGetVideoRecord(HVQM2Record *hvqmRecordHeader, void *hvqmBody);
#if HVQM_SWAPBUFFER_CALLBACK == YES
static void hvqmGfxSwapCfb(void* hvqmTask);
#endif /* HVQM_SWAPBUFFER_CALLBACK */
#ifdef NU_DEBUG
static void hvqmPrintInfo(void);
#endif /* NU_DEBUG */
/*
* Initialization function
* Needs to be called only once before playing HVQM
*/
void hvqmInit(void)
{
/* Do nothing if already initialized */
if( hvqmStatus&HVQM_STATUS_INITIALIZED )
return;
/* Set INITIALIZED flag */
{
OSIntMask mask;
mask = osSetIntMask(OS_IM_NONE);
hvqmStatus |= HVQM_STATUS_INITIALIZED;
osSetIntMask(mask);
}
/* Adjust alignment of buffer for header */
hvqmHeader = OS_DCACHE_ROUNDUP_ADDR( hvqmHeaderBuf );
#if HVQM_CFB_FORMAT == 1
hvqm2InitSP1(0xff);
#else /* HVQM_CFB_FORMAT */
hvqm2InitSP2(0xff);
#endif /* HVQM_CFB_FORMAT */
}
/*
* Read data and start playback
* Activate dedicated audio manager internally
*
*/
void hvqmStart(void *hvqmData)
{
u32 hvqmOffsetWidth, hvqmOffsetHeight;
/* Delete all if currently playing */
if( hvqmStatus&HVQM_STATUS_PLAYING )
hvqmDelete();
/* Raise PLAYING flag */
{
OSIntMask mask;
mask = osSetIntMask(OS_IM_NONE);
hvqmStatus |= HVQM_STATUS_PLAYING;
osSetIntMask(mask);
}
/* Get address of HVQM data */
hvqmRomAddress = hvqmData;
/* Read HVQM data header */
nuPiReadRom( (u32)hvqmRomAddress, hvqmHeader, sizeof(HVQM2Header) );
#ifdef NU_DEBUG
hvqmPrintInfo();
#endif /* NU_DEBUG */
/* Get the various parameters */
hvqmTotalFrame = (u32)hvqmHeader->total_frames;
hvqmFrameRate = (u32)hvqmHeader->usec_per_frame;
hvqmTotalAudio = (u32)hvqmHeader->total_audio_records;
hvqmAudioRate = (u32)hvqmHeader->samples_per_sec;
hvqmEndTime = 0;
/* Determine location of image display (Fixed in the center for now) */
hvqmOffsetHeight = ( HVQM_CFB_HEIGHT - hvqmHeader->height ) / 2;
hvqmOffsetWidth = ( HVQM_CFB_WIDTH - hvqmHeader->width ) / 2;
hvqmOffset = HVQM_CFB_WIDTH * hvqmOffsetHeight + hvqmOffsetWidth;
/* Initialization required of every data set to use HVQM */
#if HVQM_CFB_FORMAT == 1
hvqm2SetupSP1( hvqmHeader, HVQM_CFB_WIDTH );
#else /* HVQM_CFB_FORMAT */
hvqm2SetupSP2( hvqmHeader, HVQM_CFB_WIDTH );
#endif /* HVQM_CFB_FORMAT */
/* Set the condition at the start of the data */
hvqmVideoRemain = hvqmTotalFrame;
hvqmAudioRemain = hvqmTotalAudio;
hvqmVideoStream = hvqmAudioStream = hvqmRomAddress+sizeof(HVQM2Header);
/* Adjust the alignment of buffer for video header */
hvqmRecordHeader = OS_DCACHE_ROUNDUP_ADDR( hvqmRecordHeaderBuf );
#if HVQM_SWAPBUFFER_CALLBACK == YES
/* Make changes to the callback functions used for switching buffers. */
nuGfxSwapCfbFuncSet(hvqmGfxSwapCfb);
/* Turn off the flag to get ready for the forcible resume when playing HVQM. */
hvqmStatus &= ~HVQM_STATUS_WAIT_SWAPBUFFER;
#endif /* HVQM_SWAPBUFFER_CALLBACK */
}
/*
* Output the video
* Must call for every frame
* Of course, if another thread is started, only needs to be called once
* The argument specifies NU_SC_SWAPBUFFER or NU_SC_NOSWAPBUFFER
*/
void hvqmVideoMgr(u32 hvqmFlag)
{
u16 hvqmFrameFormat; /* Kind of video frame */
s32 hvqmSPStatus; /* Value returned from HVQM2's CPU process */
u32 hvqmCfbPrev; /* The number of the preceeding buffer */
#if HVQM_CFB_FORMAT == 2
u32 **hvqmGfxCfb; /* 32-bit version of nuGfxCfb */
#endif /* HVQM_CFB_FORMAT */
/* Preparation OK? */
if( !(hvqmStatus&HVQM_STATUS_PLAYING) )
return;
/* Start audio manager (if not already) */
if( (hvqmTotalAudio != 0) && !(hvqmStatus&HVQM_STATUS_THREAD_CREATED) )
hvqmAudioInit();
/* Get the present time */
hvqmNowTime = OS_CYCLES_TO_USEC(osGetTime());
/* Calculate time of end of playback when first called */
if( !(hvqmStatus&HVQM_STATUS_VIDEO_STARTED) )
{
OSIntMask mask;
mask = osSetIntMask(OS_IM_NONE);
hvqmStatus |= HVQM_STATUS_VIDEO_STARTED;
osSetIntMask(mask);
hvqmEndTime = hvqmNowTime + hvqmTotalFrame * hvqmFrameRate;
}
/* If there are uncreated images */
if( hvqmVideoRemain >0 )
{
/*
* When image processing is too fast --> do nothing
* If you wish frame buffers be switched while drawing another
* image outside the area, HVQM_PERMIT_OTHER_GRAPHICS needs to be YES.
*/
if( hvqmEndTime-hvqmNowTime > hvqmVideoRemain * hvqmFrameRate )
{
#if HVQM_PERMIT_OTHER_GRAPHICS == YES
hvqmCopyFrameBuffer(hvqmHeader, hvqmFlag);
#endif /* HVQM_PERMIT_OTHER_GRAPHICS */
return;
}
/* So now let's read the header */
/* Read header and body */
hvqmGetVideoRecord(hvqmRecordHeader, hvqmRecordBody);
/* Reduce remaining video by 1 */
hvqmVideoRemain--;
/* What's the format? */
hvqmFrameFormat = (u16)hvqmRecordHeader->format;
/* If video processing is too slow --> Read the next header */
while( hvqmEndTime-hvqmNowTime < hvqmVideoRemain * hvqmFrameRate ){
/* Go to the key frame when skipping to read image!! */
do
{
/*
* Do nothing if no image is left.
* If you wish frame buffers be switched while drawing another
* image outside the area, modification is needed.
*/
if( hvqmVideoRemain == 0 )
{
#if HVQM_LAST_FRAME == YES
/*
* Time over, however, needs decoding for the last time.
* If the last image stays to show up a little while after completing
* decoding, it may be necessary to decode over again.
*/
break;
#else /* HVQM_LAST_FRAME */
/* Note: If playback flag turns off, it means it proceeds to the next frame. */
#if HVQM_PERMIT_OTHER_GRAPHICS == YES
hvqmCopyFrameBuffer(hvqmHeader, hvqmFlag);
#endif /* HVQM_PERMIT_OTHER_GRAPHICS */
return;
#endif /* HVQM_LAST_FRAME */
}
#ifdef NU_DEBUG
/*
* Skipping here means that extra (unused) data is
* contained.
* If skipping happens every time, data should be rebuilt.
*/
// osSyncPrintf("WARNING: Skip to decode a frame (Remain:%d)\n", hvqmVideoRemain);
#endif /* NU_DEBUG */
/* Read the header and body */
hvqmGetVideoRecord(hvqmRecordHeader, hvqmRecordBody);
/* decrement the number of images left */
hvqmVideoRemain--;
/* Which format of an image? */
hvqmFrameFormat = (u16)hvqmRecordHeader->format;
}
while( hvqmFrameFormat != HVQM2_VIDEO_KEYFRAME );
}
/* Video decoding process */
/*
* Do nothing if the image doesn't need to get updated.
* If you wish frame buffers be swithced while drawing another
* image outside the area, HVQM_PERMIT_OTHER_GRAPHICS needs to be set YES.
*/
if( hvqmFrameFormat == HVQM2_VIDEO_HOLD )
{
#if HVQM_PERMIT_OTHER_GRAPHICS == YES
hvqmCopyFrameBuffer(hvqmHeader, hvqmFlag);
#endif /* HVQM_PERMIT_OTHER_GRAPHICS */
return;
}
/* Initialize frame buffer (find previous buffer) */
hvqmCfbPrev = (nuGfxCfbCounter+nuGfxCfbNum-1)%nuGfxCfbNum;
/* CPU part */
/*
* Disable the cache because the RSP changes the previous frame.
*/
osInvalDCache( nuGfxCfb[hvqmCfbPrev], HVQM_CFB_WIDTH*HVQM_CFB_HEIGHT*HVQM_CFB_FORMAT*2 );
#if HVQM_CFB_FORMAT == 1
hvqmSPStatus = hvqm2DecodeSP1( hvqmRecordBody, hvqmFrameFormat,
&nuGfxCfb_ptr[hvqmOffset],
&nuGfxCfb[hvqmCfbPrev][hvqmOffset],
hvqmWorkspace, &hvqmTaskArg, hvqmSPFifo );
#else /* HVQM_CFB_FORMAT */
hvqmGfxCfb = (u32**)nuGfxCfb;
hvqmSPStatus = hvqm2DecodeSP2( hvqmRecordBody, hvqmFrameFormat,
&hvqmGfxCfb[nuGfxCfbCounter][hvqmOffset],
&hvqmGfxCfb[hvqmCfbPrev][hvqmOffset],
hvqmWorkspace, &hvqmTaskArg, hvqmSPFifo );
#endif /* HVQM_CFB_FORMAT */
/* RSP part */
if( hvqmSPStatus > 0)
{
/* Execute as a graphics task */
nuGfxTaskStart( (Gfx*)&hvqmTaskArg, sizeof(hvqmTaskArg), HVQM_UCODE_HVQM2SP, hvqmFlag|NU_SC_NORDP|NU_SC_TASK_SP_ONLY );
/*
* Note: At the present time, NU_SC_TASK_SP_ONLY is only used
* with the Z sort microcode, so it can be here or not
*/
#if HVQM_SWAPBUFFER_CALLBACK == YES
/*
* Turn on the flag to check if the buffer is switched
* after this task is processed.
*/
hvqmStatus |= HVQM_STATUS_WAIT_SWAPBUFFER;
#endif /* HVQM_SWAPBUFFER_CALLBACK */
}
}
else
{
/* End video display and just wait for end time */
/* It is end time yet? */
if(hvqmEndTime < hvqmNowTime)
{
OSIntMask mask;
mask = osSetIntMask(OS_IM_NONE);
hvqmStatus &= ~HVQM_STATUS_VIDEO_STARTED;
hvqmStatus &= ~HVQM_STATUS_PLAYING;
osSetIntMask(mask);
}
#if HVQM_PERMIT_OTHER_GRAPHICS == YES
/* Only copy here */
hvqmCopyFrameBuffer(hvqmHeader, hvqmFlag);
#endif /* HVQM_PERMIT_OTHER_GRAPHICS */
}
}
/*
* Deletion process
*/
void hvqmDelete(void)
{
#if HVQM_SWAPBUFFER_CALLBACK == YES
/* Return the callback function for switching buffer to its default state */
nuGfxSwapCfbFuncSet(nuGfxSwapCfb);
if( hvqmStatus&HVQM_STATUS_WAIT_SWAPBUFFER )
{
OSIntMask mask;
mask = osSetIntMask(OS_IM_NONE);
hvqmStatus &= ~HVQM_STATUS_WAIT_SWAPBUFFER;
osSetIntMask(mask);
}
#endif /* HVQM_SWAPBUFFER_CALLBACK */
if( hvqmStatus&HVQM_STATUS_THREAD_CREATED )
hvqmAudioDelete();
if( hvqmStatus&HVQM_STATUS_VIDEO_STARTED )
{
OSIntMask mask;
mask = osSetIntMask(OS_IM_NONE);
hvqmStatus &= ~HVQM_STATUS_VIDEO_STARTED;
osSetIntMask(mask);
}
if( hvqmStatus&HVQM_STATUS_PLAYING )
{
OSIntMask mask;
mask = osSetIntMask(OS_IM_NONE);
hvqmStatus &= ~HVQM_STATUS_PLAYING;
osSetIntMask(mask);
}
}
/*
* Read video data header and body
*/
static void hvqmGetVideoRecord(HVQM2Record *hvqmRecord, void *hvqmBody)
{
u16 hvqmRecordType;
u32 hvqmRecordSize;
/* Read header */
for(;;)
{
nuPiReadRom((u32)hvqmVideoStream, hvqmRecord, sizeof(HVQM2Record));
hvqmVideoStream = (u8 *)hvqmVideoStream + sizeof(HVQM2Record);
/* Get parameters */
hvqmRecordType = (u16)hvqmRecord->type;
hvqmRecordSize = (u32)hvqmRecord->size;
/* If this is the desired thing, leave */
if( hvqmRecordType == HVQM2_VIDEO )
break;
/* Read the header of the display list buffer */
hvqmVideoStream = (u8 *)hvqmVideoStream + hvqmRecordSize;
}
/* Read body */
if( hvqmRecordSize > 0 )
{
nuPiReadRom((u32)hvqmVideoStream, hvqmBody, hvqmRecordSize);
hvqmVideoStream = (u8 *)hvqmVideoStream + hvqmRecordSize;
}
}
#if HVQM_SWAPBUFFER_CALLBACK == YES
/*
* Callback function to be called when switching buffers.
*/
static void hvqmGfxSwapCfb(void* hvqmTask)
{
NUScTask* hvqmTaskPtr;
hvqmTaskPtr = (NUScTask*)hvqmTask;
osViSwapBuffer(hvqmTaskPtr->framebuffer);
/* The addition starts here */
hvqmStatus &= ~HVQM_STATUS_WAIT_SWAPBUFFER;
}
#endif /* HVQM_SWAPBUFFER_CALLBACK */
#ifdef NU_DEBUG
/*
* Display information about HVQM data on console
*/
static void hvqmPrintInfo(void)
{
// osSyncPrintf( "\n" );
// osSyncPrintf( "File version : %s\n", hvqmHeader->file_version );
// osSyncPrintf( "File size : %d\n", (u32)hvqmHeader->file_size );
// osSyncPrintf( "Image width : %d\n", (u16)hvqmHeader->width );
// osSyncPrintf( "Image height : %d\n", (u16)hvqmHeader->height );
// osSyncPrintf( "Compress type : %s\n", hvqmHeader->v_sampling_rate == 1 ? "4:2:2" : "4:1:1" );
// osSyncPrintf( "Total frames : %d\n", (u32)hvqmHeader->total_frames );
// osSyncPrintf( "Video rate : %f frame/sec\n", 1000000.0 / (double)(u32) hvqmHeader->usec_per_frame );
// osSyncPrintf( "Total audio records : %d\n", (u32)hvqmHeader->total_audio_records );
// osSyncPrintf( "Audio rate : %d Hz\n", (u32)hvqmHeader->samples_per_sec );
// osSyncPrintf( "\n" );
// osSyncPrintf( "Display mode : %s\n", HVQM_CFB_FORMAT == 1 ? "16-bit RGBA" : "32-bit RGBA" );
// osSyncPrintf( "\n" );
/* Check parameters */
// if( hvqmHeader->max_frame_size > HVQM_DATASIZE_MAX )
// osSyncPrintf( "ERROR: HVQM_DATASIZE_MAX is too small!!\n\tmax_frame_size is %ld\n", hvqmHeader->max_frame_size);
// if( hvqmHeader->max_audio_record_size > HVQM_AUDIO_DATASIZE_MAX )
// osSyncPrintf( "ERROR: HVQM_AUDIO_DATASIZE_MAX is too small!!\n\tmax_audio_record_size is %ld\n", hvqmHeader->max_audio_record_size);
// if( hvqmHeader->max_sp_packets > HVQM_SPFIFO_SIZE )
// osSyncPrintf( "ERROR: HVQM_SPFIFO_SIZE is too small!!\n\tmax_sp_packets is %ld\n", hvqmHeader->max_sp_packets );
}
#endif /* NU_DEBUG */