This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcutscenedps.cpp
307 lines (286 loc) · 7.32 KB
/
cutscenedps.cpp
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
#include <math.h>
#include "cutscene.h"
#include "render.h"
#include "sound.h"
#include "mdec.h"
enum {
kSectorSize = 2352,
kAudioDataSize = 2304,
kAudioHeaderSize = 24,
kVideoDataSize = 2016,
kVideoHeaderSize = 56,
kCutscenePsxVideoWidth = 320,
kCutscenePsxVideoHeight = 240,
};
struct DpsHeader {
uint16_t w, h;
uint16_t framesCount;
uint8_t xaStereo;
uint16_t xaSampleRate;
uint8_t xaBits;
};
struct CutscenePlayer_Dps: CutscenePlayer {
File *_fp;
uint8_t _sector[kSectorSize];
DpsHeader _header;
int _frameCounter;
int _sectorCounter;
uint8_t *_rgbaBuffer;
CutscenePlayer_Dps();
virtual ~CutscenePlayer_Dps();
bool readSector();
bool play();
bool readHeader(DpsHeader *header);
virtual bool load(int num);
virtual void unload();
virtual bool update(uint32_t ticks);
};
CutscenePlayer_Dps::CutscenePlayer_Dps()
: _fp(0), _rgbaBuffer(0) {
}
CutscenePlayer_Dps::~CutscenePlayer_Dps() {
free(_rgbaBuffer);
}
static const char *_namesTable[] = {
// 0
"moar",
"moba",
"moex",
"moel",
// 4
"molz",
"modp",
"mopl",
"momc",
// 8
"mogr",
"mobu",
"moir",
"mott",
// 12
"g", // ?
"intr",
"evas",
"evbm",
// 16
"prof",
"dsta",
"exbm",
"exvb",
// 20
"agee",
"anci",
"supm",
"aspi",
// 24
"bupp",
"arbu",
"cerv",
"evam",
// 28
"bomb",
"int2",
"bast",
"fuit",
// 32
"fin1",
"boum",
"mosm",
"hcle",
// 36
"scle",
"titl",
"motv",
"logo",
// 40
"movr",
"mogl",
"mogo",
"cong",
// 44
"fad1",
"fad2",
"pakk",
"ealo",
// 48
"mgmm",
"exsh",
"fin2",
"bass",
// 52
"fui2",
"gend"
};
static const struct {
const char *name;
int num;
} _namesTable_gr[] = {
{ "grex", 2 },
{ "grlz", 4 },
{ "grmc", 7 },
{ "grsm", 34 }
};
static const uint8_t _cdSync[12] = { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
bool CutscenePlayer_Dps::readSector() {
// mode2 form2 : cdSync (12 bytes) header (4 bytes) subheader (8 bytes) data (2324 bytes) edc (4 bytes)
++_sectorCounter;
const int count = fileRead(_fp, _sector, sizeof(_sector));
return count == kSectorSize && memcmp(_sector, _cdSync, sizeof(_cdSync)) == 0;
}
static uint32_t yuv420_to_rgba(int y, int u, int v) {
const int r = CLIP((int)round(y + 1.402 * v), 0, 255);
const int g = CLIP((int)round(y - 0.344 * u - 0.714 * v), 0, 255);
const int b = CLIP((int)round(y + 1.772 * u), 0, 255);
return 0xFF000000 | (b << 16) | (g << 8) | r;
}
static void outputMdecCb(const MdecOutput *output, void *userdata) {
CutscenePlayer_Dps *cut = (CutscenePlayer_Dps *)userdata;
uint32_t *dst = (uint32_t *)cut->_rgbaBuffer + (cut->_header.h - 1) * cut->_header.w;
const uint8_t *luma = output->planes[0].ptr;
const uint8_t *cb = output->planes[1].ptr;
const uint8_t *cr = output->planes[2].ptr;
for (int y = 0; y < output->h; ++y) {
for (int x = 0; x < output->w; ++x) {
dst[x] = yuv420_to_rgba(luma[x], cb[x / 2] - 128, cr[x / 2] - 128);
}
dst -= cut->_header.w;
luma += output->planes[0].pitch;
cb += output->planes[1].pitch * (y & 1);
cr += output->planes[2].pitch * (y & 1);
}
}
bool CutscenePlayer_Dps::play() {
bool err = false;
// demux audio and video frames
uint8_t *videoData = 0;
int videoCurrentSector = -1;
int videoSectorsCount = 0;
do {
if (!readSector()) {
warning("CutscenePlayer_Dps::play() Invalid sector %d", _sectorCounter);
err = true;
break;
}
const int type = _sector[0x12] & 0xE;
if (type == 2 && READ_LE_UINT32(_sector + 0x18) == 0x80010160) {
const int currentSector = READ_LE_UINT16(&_sector[0x1C]);
if (videoCurrentSector != -1 && videoCurrentSector != currentSector - 1) {
err = true;
break;
}
videoCurrentSector = currentSector;
const int sectorsCount = READ_LE_UINT16(&_sector[0x1E]);
if (videoSectorsCount != 0 && videoSectorsCount != sectorsCount) {
err = true;
break;
}
videoSectorsCount = sectorsCount;
const int frameSize = READ_LE_UINT32(&_sector[0x24]);
if (frameSize != 0 && currentSector < sectorsCount) {
if (currentSector == 0) {
videoData = (uint8_t *)malloc(sectorsCount * kVideoDataSize);
}
memcpy(videoData + currentSector * kVideoDataSize, _sector + kVideoHeaderSize, kVideoDataSize);
if (currentSector == videoSectorsCount - 1) {
decodeMDEC(videoData, videoSectorsCount * kVideoDataSize, _header.w, _header.h, this, outputMdecCb);
const int y = (kCutscenePsxVideoHeight - _header.h) / 2;
_render->copyToOverlay(0, y, _header.w, _header.h, _rgbaBuffer, true);
++_frameCounter;
free(videoData);
videoData = 0;
}
}
} else if (type == 4) {
if (_header.xaSampleRate == 0) { // this is the first audio sector, get the format
_header.xaStereo = (_sector[0x13] & 1) != 0;
_header.xaSampleRate = (_sector[0x13] & 4) != 0 ? 18900 : 37800;
_header.xaBits = (_sector[0x13] & 0x10) != 0 ? 4 : 8;
if (!_header.xaStereo || _header.xaSampleRate != 37800 || _header.xaBits != 8) {
warning("CutscenePlayer_Dps::play() Unsupported audio format, stereo %d bits %d freq %d", _header.xaStereo, _header.xaBits, _header.xaSampleRate);
err = true;
break;
}
_snd->_mix.playQueue(4, kMixerQueueType_XA);
}
_snd->_mix.appendToQueue(_sector + kAudioHeaderSize, kAudioDataSize);
}
} while (videoCurrentSector != videoSectorsCount - 1 && !fileEof(_fp) && !err);
return !err;
}
bool CutscenePlayer_Dps::readHeader(DpsHeader *hdr) {
if (!readSector()) {
warning("CutscenePlayer_Dps::readHeader() Invalid first sector");
} else {
const uint8_t *header = _sector + 0x18;
if (memcmp(header, "DPS", 3) != 0) {
warning("CutscenePlayer_Dps::readHeader() Invalid header signature");
} else {
hdr->w = READ_LE_UINT16(header + 0x10);
hdr->h = READ_LE_UINT16(header + 0x12);
hdr->framesCount = READ_LE_UINT16(header + 0x1C);
int sector = 1;
for (; sector < 5; ++sector) {
if (!readSector()) {
warning("CutscenePlayer_Dps::readHeader() Invalid sector %d", sector);
break;
}
}
return sector == 5;
}
}
return false;
}
bool CutscenePlayer_Dps::load(int num) {
memset(&_header, 0, sizeof(_header));
_frameCounter = 0;
_sectorCounter = -1;
const char *name = _namesTable[num];
if (fileLanguage() == kFileLanguage_GR) {
for (int i = 0; i < ARRAYSIZE(_namesTable_gr); ++i) {
if (_namesTable_gr[i].num == num) {
name = _namesTable_gr[i].name;
break;
}
}
}
char filename[16];
snprintf(filename, sizeof(filename), "%s.dps", name);
_fp = fileOpenPsx(filename, kFileType_PSX_VIDEO);
if (_fp) {
const int size = fileSize(_fp);
if ((size % kSectorSize) != 0) {
warning("Unexpected file size (%d bytes) for '%s', not a multiple of a raw CD sector size", size, filename);
fileClose(_fp);
_fp = 0;
} else if (!readHeader(&_header)) {
warning("Unable to read cutscene file header");
fileClose(_fp);
_fp = 0;
} else {
_rgbaBuffer = (uint8_t *)malloc(_header.w * _header.h * sizeof(uint32_t));
_render->clearScreen();
_render->resizeOverlay(_header.w, _header.h, true, kCutscenePsxVideoWidth, kCutscenePsxVideoHeight);
}
}
return _fp != 0;
}
void CutscenePlayer_Dps::unload() {
if (_rgbaBuffer) {
free(_rgbaBuffer);
_rgbaBuffer = 0;
}
if (_fp) {
fileClose(_fp);
_fp = 0;
}
_snd->_mix.stopQueue();
}
bool CutscenePlayer_Dps::update(uint32_t ticks) {
if (!_fp) {
return false;
}
return play();
}
CutscenePlayer *CutscenePlayer_Dps_create() {
return new CutscenePlayer_Dps();
}