aboutsummaryrefslogtreecommitdiff
path: root/backends/ps2/smushio.cpp
blob: ef7c9f4517893d52e5476206b09a120b265b06cf (plain)
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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2005 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

/*
	The Smush player uses at least two handles for accessing the same SMUSH file, 
	to avoid threading issues.	One handle for video, one for audio apparently.
	Each of the handles always skips the data that the other one read before
	(or will read later).

	This behaviour makes it difficult to do read ahead caching without reading
	any given file twice, so this class tries to "reunite" the read accesses and
	do the necessary caching.
*/

#include "backends/ps2/fileio.h"
#include "backends/ps2/asyncfio.h"
#include <kernel.h>
#include <assert.h>
#include <string.h>
#include <sio.h>

extern AsyncFio fio;
extern void sioprintf(const char *zFormat, ...);

#define SMUSH_CACHE_SIZE (5 * 1024 * 1024)
#define READ_STEP (32 * 1024)
#define SMUSH_IN_USE -2

class SmushReader {
public:
	SmushReader(void);
	~SmushReader(void);
	int open(const char *name);
	void close(void);
	uint32 read(void *dest, uint32 from, uint32 len);
	void virtSeek(uint32 from, uint32 to);
	uint32 size(void);
	bool keepOpened(void);
private:
	void processCache(bool sync);
	char _fname[256];
	int _sema, _fd, _refCount;
	uint8 *_cacheBuf;
	volatile uint32 _cacheFilePos, _bytesInCache, _fileSize, _cacheBufOfs;
	volatile uint32 _lastRead[2];
	volatile bool _cacheOp;
};

SmushReader::SmushReader(void) {
	_cacheBuf = NULL;
	_fd = -1;
	_refCount = 0;

	ee_sema_t newSema;
	newSema.init_count = 1;
	newSema.max_count = 1;
	_sema = CreateSema(&newSema);
	assert(_sema >= 0);
}

SmushReader::~SmushReader(void) {
    DeleteSema(_sema);
}

int SmushReader::open(const char *name) {
	WaitSema(_sema);
	if (_refCount) {
		if (stricmp(_fname, name)) {
			sioprintf("SmushReader is already open to file\n%s\nGot open request for %s", _fname, name);
			SignalSema(_sema);
			return SMUSH_IN_USE;
		}
	} else {
		assert(_fd < 0);
		_fd = fio.open(name, O_RDONLY);
		if (_fd < 0) {
			SignalSema(_sema);
			return -1;
		}
		_fileSize = fio.seek(_fd, 0, SEEK_END);
		fio.seek(_fd, 0, SEEK_SET);

		_cacheBuf = (uint8*)malloc(SMUSH_CACHE_SIZE);
		if (!_cacheBuf) {
			sioprintf("Smush Reader ran out of memory");
			fio.close(_fd);
			_fd = -1;
			SignalSema(_sema);
			return -1;
		}
		_lastRead[0] = _lastRead[1] = 0;
		_cacheBufOfs = _bytesInCache = _cacheFilePos = 0;
		fio.read(_fd, _cacheBuf, READ_STEP);
		_cacheOp = true;
		strcpy(_fname, name);
	}
	_refCount++;
	sioprintf("SmushReader %s ref count %d", _fname, _refCount);
	SignalSema(_sema);
	return 0;
}

void SmushReader::close(void) {
	WaitSema(_sema);
	sioprintf("Closing Ref to %s", _fname);
	assert(_refCount > 0);
	_refCount--;
	if (!_refCount) {
		sioprintf("SmushReader: All references to %s closed", _fname);
		processCache(true);
		_fname[0] = '\0';
		fio.close(_fd);
		_fd = -1;
		free(_cacheBuf);
		_cacheBuf = NULL;
	}
	SignalSema(_sema);
}

#define MIN(a, b) ((a < b) ? (a) : (b))

void SmushReader::processCache(bool sync) {
	if (_cacheOp) {
		if (sync || fio.poll(_fd)) { // has the transfer finished or were we told to wait for it to finish?
			int rdRes = fio.sync(_fd);
			assert(rdRes >= 0);
            _bytesInCache += rdRes;
			_cacheOp = false;
		}
	} else if (!sync) { 
		if (_cacheFilePos + _bytesInCache == _fileSize)
			return;

		uint32 rdPos = MIN(_lastRead[0], _lastRead[1]);
		
		int cacheOfs = (rdPos - _cacheFilePos) & ~0xF; // we'd like to keep the buffer aligned to 16 bytes
		if (cacheOfs < 0) {
			sioprintf("ERROR: smush cache too far ahead!");
			return;
		}

		if (_bytesInCache - cacheOfs < SMUSH_CACHE_SIZE - READ_STEP) {
			// we want to do some more reading
			if (_bytesInCache > cacheOfs) {
				_bytesInCache -= cacheOfs;
				_cacheBufOfs  += cacheOfs;
				_cacheFilePos += cacheOfs;
			} else {
				sioprintf("cache underrun!");
				_bytesInCache = 0;
				_cacheBufOfs  = 0;
				_cacheFilePos = rdPos;
			}

			uint32 bufEndPos = (_cacheBufOfs + _bytesInCache) % SMUSH_CACHE_SIZE;
			uint32 readLen = SMUSH_CACHE_SIZE - bufEndPos;
			if (readLen > READ_STEP)
				readLen = READ_STEP;

			fio.read(_fd, _cacheBuf + bufEndPos, readLen);
			_cacheOp = true;
		}
	}
}

uint32 SmushReader::read(void *dest, uint32 from, uint32 len) {
	uint8 *destBuf = (uint8*)dest;
	WaitSema(_sema);
	if ((from >= _cacheFilePos) && (from + len <= _cacheFilePos + _bytesInCache))
		processCache(false);
	else {
		processCache(true); // we'll have to read, sync cache before.
	}
	uint32 readEnds = from + len;
	if (from == _lastRead[0])
		_lastRead[0] += len;
	else if (from == _lastRead[1])
		_lastRead[1] += len;
	else {
		if ((_lastRead[0] > readEnds) && (_lastRead[1] < readEnds)) {
			_lastRead[1] = readEnds;
		} else if ((_lastRead[0] < readEnds) && (_lastRead[1] > readEnds)) {
			_lastRead[0] = readEnds;
		} else {
			if ((_lastRead[0] < readEnds) && (_lastRead[1] < readEnds)) {
				if (_lastRead[0] < _lastRead[1])
					_lastRead[0] = readEnds;
				else
					_lastRead[1] = readEnds;
			} else
				sioprintf("unexpected readend: %d / %d => %d", _lastRead[0], _lastRead[1], readEnds);
		}
	}

	while (len) {
		while (len && (from >= _cacheFilePos) && (from < _cacheFilePos + _bytesInCache)) {
			uint32 cpyOfs = ((from - _cacheFilePos) + _cacheBufOfs) % SMUSH_CACHE_SIZE;
			uint32 cpyLen = _bytesInCache - (from - _cacheFilePos);
			if (cpyLen > len)
				cpyLen = len;
			if (cpyOfs + cpyLen > SMUSH_CACHE_SIZE)
				cpyLen = SMUSH_CACHE_SIZE - cpyOfs;
			memcpy(destBuf, _cacheBuf + cpyOfs, cpyLen);
			destBuf += cpyLen;
			from    += cpyLen;
			len     -= cpyLen;
		}
		if (len) {
			sioprintf("Smush cache missed: read %d -> %d, cache %d -> %d", from, len, _cacheFilePos, _bytesInCache);
			assert(fio.seek(_fd, 0, SEEK_CUR) == _cacheFilePos + _bytesInCache);
			fio.seek(_fd, from, SEEK_SET);
			int rdRes;
			do {
				fio.read(_fd, destBuf, len);
				rdRes = fio.sync(_fd);
                destBuf += rdRes;
				from += rdRes;
				len -= rdRes;
			} while (len && rdRes);
			fio.seek(_fd, _cacheFilePos + _bytesInCache, SEEK_SET);
			break;
		}        
	}
	processCache(false);
	SignalSema(_sema);
	return destBuf - (uint8*)dest;
}

void SmushReader::virtSeek(uint32 from, uint32 to) {
	WaitSema(_sema);
	if (_lastRead[0] == from)
		_lastRead[0] = to;
	else if (_lastRead[1] == from)
		_lastRead[1] = to;
	SignalSema(_sema);
}

uint32 SmushReader::size(void) {
	assert(_fd >= 0);
	return _fileSize;
}

bool SmushReader::keepOpened(void) {
	return _refCount > 0;
}

#define MAX_READERS 3

static SmushReader *g_smushReaders[MAX_READERS] = { NULL, NULL, NULL };

static int g_openSema = -1;

Ps2SmushFile::Ps2SmushFile(int64 cacheId) : Ps2File(cacheId) {
    _filePos = _fileSize = 0;
	_id = -1;
	if (g_openSema < 0) {
		ee_sema_t newSema;
		newSema.init_count = 1;
		newSema.max_count = 1;
		g_openSema = CreateSema(&newSema);
		assert(g_openSema >= 0);
	}
}

Ps2SmushFile::~Ps2SmushFile(void) {
	WaitSema(g_openSema);
	if (_id >= 0) {
		g_smushReaders[_id]->close();
		if (!g_smushReaders[_id]->keepOpened()) {
			delete g_smushReaders[_id];
			g_smushReaders[_id] = NULL;
		}
	}
	SignalSema(g_openSema);
}

bool Ps2SmushFile::open(const char *name) {
	WaitSema(g_openSema);
	int opSlot = MAX_READERS;
	for (int i = 0; i < MAX_READERS; i++) {
		if (g_smushReaders[i]) {
			sioprintf("attaching to reader in slot %d", i);
			if (g_smushReaders[i]->open(name) == 0) {
				_id = i;
				_fileSize = g_smushReaders[i]->size();
				sioprintf("attach ok");
				break;
			}
		} else if (opSlot == MAX_READERS)
			opSlot = i;
	}
	if (_id < 0) { // smush file wasn't opened before
		sioprintf("creating new reader in slot %d", opSlot);
		if (opSlot < MAX_READERS) {
			g_smushReaders[opSlot] = new SmushReader();
			if (g_smushReaders[opSlot]->open(name) == 0) {
				_id = opSlot;
				_fileSize = g_smushReaders[opSlot]->size();
			} else {
				// can't open file
				delete g_smushReaders[opSlot];
				g_smushReaders[opSlot] = NULL;
			}
		} else
			printf("Ran out of reader slots\n");
	}
	SignalSema(g_openSema);
	return (_id >= 0);
}

uint32 Ps2SmushFile::read(void *dest, uint32 len) {
	int res = g_smushReaders[_id]->read(dest, _filePos, len);
	_filePos += res;
	return res;
}

uint32 Ps2SmushFile::write(const void *src, uint32 len) {
	printf("ERROR: Received write request on Smush reader\n");
	SleepThread();
	return 0;
}

uint32 Ps2SmushFile::tell(void) {
	return _filePos;
}

uint32 Ps2SmushFile::size(void) {
	return _fileSize;
}

int Ps2SmushFile::seek(int32 offset, int origin) {
	int32 res;
	switch (origin) {
		case SEEK_SET:
			res = offset;
			break;
		case SEEK_CUR:
			res = _filePos + offset;
			break;
		case SEEK_END:
			res = _fileSize + offset;
			break;
		default:
			return -1;
	}
	if ((res >= 0) && (res <= _fileSize)) {
		if (offset != 0)
			g_smushReaders[_id]->virtSeek(_filePos, res);
		_filePos = res;
		return 0;
	}
	return -1;
}

bool Ps2SmushFile::eof(void) {
	return _filePos == _fileSize;
}