forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscePsmf.cpp
2098 lines (1853 loc) · 74.3 KB
/
scePsmf.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012- PPSSPP 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, version 2.0 or later versions.
// 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 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "Common/ChunkFile.h"
#include "Core/MemMapHelpers.h"
#include "Core/Reporting.h"
#include "Core/System.h"
#include "Core/FileSystems/MetaFileSystem.h"
#include "Core/HLE/HLE.h"
#include "Core/HLE/HLEHelperThread.h"
#include "Core/HLE/FunctionWrappers.h"
#include "Core/HLE/scePsmf.h"
#include "Core/HLE/sceMpeg.h"
#include "Core/HW/MediaEngine.h"
#include "GPU/GPUInterface.h"
#include "GPU/GPUState.h"
#include <map>
#include <algorithm>
// "Go Sudoku" is a good way to test this code...
const int PSMF_VIDEO_STREAM_ID = 0xE0;
const int PSMF_AUDIO_STREAM_ID = 0xBD;
const int PSMF_AVC_STREAM = 0;
const int PSMF_ATRAC_STREAM = 1;
const int PSMF_PCM_STREAM = 2;
const int PSMF_DATA_STREAM = 3;
const int PSMF_AUDIO_STREAM = 15;
const int PSMF_PLAYER_VERSION_FULL = 0;
const int PSMF_PLAYER_VERSION_BASIC = 1;
const int PSMF_PLAYER_VERSION_NET = 2;
const int PSMF_PLAYER_CONFIG_LOOP = 0;
const int PSMF_PLAYER_CONFIG_NO_LOOP = 1;
const int PSMF_PLAYER_CONFIG_MODE_LOOP = 0;
const int PSMF_PLAYER_CONFIG_MODE_PIXEL_TYPE = 1;
const int PSMF_PLAYER_WARMUP_FRAMES = 3;
static const int VIDEO_FRAME_DURATION_TS = 3003;
int audioSamples = 2048;
int audioSamplesBytes = audioSamples * 4;
int videoPixelMode = GE_CMODE_32BIT_ABGR8888;
int videoLoopStatus = PSMF_PLAYER_CONFIG_NO_LOOP;
enum PsmfPlayerError {
ERROR_PSMF_NOT_INITIALIZED = 0x80615001,
ERROR_PSMF_BAD_VERSION = 0x80615002,
ERROR_PSMF_NOT_FOUND = 0x80615025,
ERROR_PSMF_INVALID_ID = 0x80615100,
ERROR_PSMF_INVALID_VALUE = 0x806151fe,
ERROR_PSMF_INVALID_TIMESTAMP = 0x80615500,
ERROR_PSMF_INVALID_PSMF = 0x80615501,
ERROR_PSMFPLAYER_INVALID_STATUS = 0x80616001,
ERROR_PSMFPLAYER_INVALID_STREAM = 0x80616003,
ERROR_PSMFPLAYER_BUFFER_SIZE = 0x80616005,
ERROR_PSMFPLAYER_INVALID_CONFIG = 0x80616006,
ERROR_PSMFPLAYER_INVALID_PARAM = 0x80616008,
ERROR_PSMFPLAYER_NO_MORE_DATA = 0x8061600c,
};
enum PsmfPlayerStatus {
PSMF_PLAYER_STATUS_NONE = 0x0,
PSMF_PLAYER_STATUS_INIT = 0x1,
PSMF_PLAYER_STATUS_STANDBY = 0x2,
PSMF_PLAYER_STATUS_PLAYING = 0x4,
PSMF_PLAYER_STATUS_ERROR = 0x100,
PSMF_PLAYER_STATUS_PLAYING_FINISHED = 0x200,
};
enum PsmfPlayerMode {
PSMF_PLAYER_MODE_PLAY = 0,
PSMF_PLAYER_MODE_SLOWMOTION = 1,
PSMF_PLAYER_MODE_STEPFRAME = 2,
PSMF_PLAYER_MODE_PAUSE = 3,
PSMF_PLAYER_MODE_FORWARD = 4,
PSMF_PLAYER_MODE_REWIND = 5,
};
struct PsmfData {
u32_le version;
u32_le headerSize;
u32_le headerOffset;
u32_le streamSize;
u32_le streamOffset;
u32_le streamNum;
u32_le unk1;
u32_le unk2;
};
struct PsmfPlayerCreateData {
PSPPointer<u8> buffer;
u32 bufferSize;
int threadPriority;
};
struct PsmfPlayerData {
s32_le videoCodec;
s32_le videoStreamNum;
s32_le audioCodec;
s32_le audioStreamNum;
s32_le playMode;
s32_le playSpeed;
};
struct PsmfInfo {
u32_le lastFrameTS;
s32_le numVideoStreams;
s32_le numAudioStreams;
s32_le numPCMStreams;
s32_le playerVersion;
};
struct PsmfVideoData {
s32_le frameWidth;
u32_le displaybuf;
u32_le displaypts;
};
struct PsmfEntry {
int EPPts;
int EPOffset;
int EPIndex;
int EPPicOffset;
};
// Some of our platforms don't play too nice with direct unaligned access.
static u32 ReadUnalignedU32BE(const u8 *p) {
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
}
class PsmfStream;
// This does NOT match the raw structure. Due to endianness etc,
// we read it manually.
// TODO: Change to work directly with the data in RAM instead of this
// JPSCP-esque class.
typedef std::map<int, PsmfStream *> PsmfStreamMap;
class Psmf {
public:
// For savestates only.
Psmf() {}
Psmf(const u8 *ptr, u32 data);
~Psmf();
void DoState(PointerWrap &p);
bool isValidCurrentStreamNumber() const {
return currentStreamNum >= 0 && streamMap.find(currentStreamNum) != streamMap.end();
}
bool setStreamNum(int num);
bool setStreamWithType(int type, int channel);
bool setStreamWithTypeNumber(int type, int n);
int FindEPWithTimestamp(int pts) const;
u32 magic;
u32 version;
u32 streamOffset;
u32 streamSize;
u32 headerSize;
u32 headerOffset;
u32 streamType;
u32 streamChannel;
// 0x50
u32 streamDataTotalSize;
u32 presentationStartTime;
u32 presentationEndTime;
u32 streamDataNextBlockSize;
u32 streamDataNextInnerBlockSize;
int numStreams;
int currentStreamNum;
int currentStreamType;
int currentStreamChannel;
int currentAudioStreamNum;
int currentVideoStreamNum;
// parameters gotten from streams
// I guess this is the seek information?
u32 EPMapOffset;
u32 EPMapEntriesNum;
int videoWidth;
int videoHeight;
int audioChannels;
int audioFrequency;
std::vector<PsmfEntry> EPMap;
PsmfStreamMap streamMap;
};
class PsmfPlayer {
public:
// For savestates only.
PsmfPlayer() : filehandle(0), finishThread(nullptr) {
mediaengine = new MediaEngine();
}
PsmfPlayer(const PsmfPlayerCreateData *data);
~PsmfPlayer() {
if (mediaengine)
delete mediaengine;
pspFileSystem.CloseFile(filehandle);
}
void DoState(PointerWrap &p);
void ScheduleFinish(u32 handle) {
if (!finishThread) {
finishThread = new HLEHelperThread("scePsmfPlayer", "scePsmfPlayer", "__PsmfPlayerFinish", playbackThreadPriority, 0x100);
finishThread->Start(handle, 0);
}
}
void AbortFinish() {
if (finishThread) {
delete finishThread;
finishThread = nullptr;
}
}
bool HasReachedEnd() {
bool videoPtsEnd = (s64)psmfPlayerAvcAu.pts >= (s64)totalDurationTimestamp - VIDEO_FRAME_DURATION_TS;
// If we're out of video data and have no audio, it's over even if the pts isn't there yet.
return videoPtsEnd || (mediaengine->IsVideoEnd() && mediaengine->IsNoAudioData());
}
u32 filehandle;
u32 fileoffset;
int readSize;
int streamSize;
u8 tempbuf[0x10000];
int videoCodec;
int videoStreamNum;
int audioCodec;
int audioStreamNum;
int playMode;
int playSpeed;
u64 totalDurationTimestamp;
int displayBuffer;
int displayBufferSize;
int playbackThreadPriority;
int totalVideoStreams;
int totalAudioStreams;
int playerVersion;
int videoStep;
int warmUp;
s64 seekDestTimeStamp;
SceMpegAu psmfPlayerAtracAu;
SceMpegAu psmfPlayerAvcAu;
PsmfPlayerStatus status;
MediaEngine *mediaengine;
HLEHelperThread *finishThread;
};
class PsmfStream {
public:
// Used for save states.
PsmfStream() {
}
PsmfStream(int type, int channel) {
this->type = type;
this->channel = channel;
}
void readMPEGVideoStreamParams(const u8 *addr, const u8 *data, Psmf *psmf) {
int streamId = addr[0];
int privateStreamId = addr[1];
// two unknowns here
psmf->EPMapOffset = ReadUnalignedU32BE(&addr[4]);
psmf->EPMapEntriesNum = ReadUnalignedU32BE(&addr[8]);
psmf->videoWidth = addr[12] * 16;
psmf->videoHeight = addr[13] * 16;
const u32 EP_MAP_STRIDE = 1 + 1 + 4 + 4;
psmf->EPMap.clear();
for (u32 i = 0; i < psmf->EPMapEntriesNum; i++) {
const u8 *const entryAddr = data + psmf->EPMapOffset + EP_MAP_STRIDE * i;
PsmfEntry entry;
entry.EPIndex = entryAddr[0];
entry.EPPicOffset = entryAddr[1];
entry.EPPts = ReadUnalignedU32BE(&entryAddr[2]);
entry.EPOffset = ReadUnalignedU32BE(&entryAddr[6]);
psmf->EPMap.push_back(entry);
}
INFO_LOG(ME, "PSMF MPEG data found: id=%02x, privid=%02x, epmoff=%08x, epmnum=%08x, width=%i, height=%i", streamId, privateStreamId, psmf->EPMapOffset, psmf->EPMapEntriesNum, psmf->videoWidth, psmf->videoHeight);
}
void readPrivateAudioStreamParams(const u8 *addr, Psmf *psmf) {
int streamId = addr[0];
int privateStreamId = addr[1];
psmf->audioChannels = addr[14];
psmf->audioFrequency = addr[15];
// two unknowns here
INFO_LOG(ME, "PSMF private audio found: id=%02x, privid=%02x, channels=%i, freq=%i", streamId, privateStreamId, psmf->audioChannels, psmf->audioFrequency);
}
bool matchesType(int ty) {
if (ty == PSMF_AUDIO_STREAM) {
return type == PSMF_ATRAC_STREAM || type == PSMF_PCM_STREAM;
}
return type == ty;
}
void DoState(PointerWrap &p) {
auto s = p.Section("PsmfStream", 1);
if (!s)
return;
p.Do(type);
p.Do(channel);
}
int type;
int channel;
};
Psmf::Psmf(const u8 *ptr, u32 data) {
headerOffset = data;
magic = *(u32_le *)&ptr[0];
version = *(u32_le *)&ptr[4];
streamOffset = ReadUnalignedU32BE(&ptr[8]);
streamSize = ReadUnalignedU32BE(&ptr[12]);
streamDataTotalSize = ReadUnalignedU32BE(&ptr[0x50]);
presentationStartTime = getMpegTimeStamp(ptr + PSMF_FIRST_TIMESTAMP_OFFSET);
presentationEndTime = getMpegTimeStamp(ptr + PSMF_LAST_TIMESTAMP_OFFSET);
streamDataNextBlockSize = ReadUnalignedU32BE(&ptr[0x6A]);
streamDataNextInnerBlockSize = ReadUnalignedU32BE(&ptr[0x7C]);
numStreams = *(u16_be *)&ptr[0x80];
// TODO: Always?
headerSize = 0x800;
currentStreamNum = -1;
currentStreamType = -1;
currentStreamChannel = -1;
currentAudioStreamNum = -1;
currentVideoStreamNum = -1;
for (int i = 0; i < numStreams; i++) {
PsmfStream *stream = 0;
const u8 *const currentStreamAddr = ptr + 0x82 + i * 16;
int streamId = currentStreamAddr[0];
if ((streamId & PSMF_VIDEO_STREAM_ID) == PSMF_VIDEO_STREAM_ID) {
stream = new PsmfStream(PSMF_AVC_STREAM, streamId & 0x0F);
stream->readMPEGVideoStreamParams(currentStreamAddr, ptr, this);
} else if ((streamId & PSMF_AUDIO_STREAM_ID) == PSMF_AUDIO_STREAM_ID) {
int type = PSMF_ATRAC_STREAM;
int privateStreamId = currentStreamAddr[1];
if ((privateStreamId & 0xF0) != 0) {
WARN_LOG_REPORT(ME, "Unknown private stream type, assuming PCM: %02x", privateStreamId);
type = PSMF_PCM_STREAM;
}
stream = new PsmfStream(type, privateStreamId & 0x0F);
stream->readPrivateAudioStreamParams(currentStreamAddr, this);
}
if (stream) {
currentStreamNum++;
streamMap[currentStreamNum] = stream;
}
}
// Default to the first stream.
currentStreamNum = 0;
}
Psmf::~Psmf() {
for (auto it = streamMap.begin(), end = streamMap.end(); it != end; ++it) {
delete it->second;
}
streamMap.clear();
}
PsmfPlayer::PsmfPlayer(const PsmfPlayerCreateData *data) {
videoCodec = -1;
videoStreamNum = -1;
audioCodec = -1;
audioStreamNum = -1;
playMode = 0;
playSpeed = 1;
totalDurationTimestamp = 0;
status = PSMF_PLAYER_STATUS_INIT;
mediaengine = new MediaEngine;
finishThread = nullptr;
filehandle = 0;
fileoffset = 0;
readSize = 0;
streamSize = 0;
videoStep = 0;
warmUp = 0;
seekDestTimeStamp = 0;
psmfPlayerAtracAu.dts =-1;
psmfPlayerAtracAu.pts = -1;
psmfPlayerAvcAu.dts = -1;
psmfPlayerAvcAu.pts = -1;
displayBuffer = data->buffer.ptr;
displayBufferSize = data->bufferSize;
playbackThreadPriority = data->threadPriority;
}
void Psmf::DoState(PointerWrap &p) {
auto s = p.Section("Psmf", 1, 3);
if (!s)
return;
p.Do(magic);
p.Do(version);
p.Do(streamOffset);
p.Do(streamSize);
p.Do(headerOffset);
p.Do(streamDataTotalSize);
p.Do(presentationStartTime);
p.Do(presentationEndTime);
p.Do(streamDataNextBlockSize);
p.Do(streamDataNextInnerBlockSize);
p.Do(numStreams);
p.Do(currentStreamNum);
p.Do(currentAudioStreamNum);
p.Do(currentVideoStreamNum);
p.Do(EPMapOffset);
p.Do(EPMapEntriesNum);
p.Do(videoWidth);
p.Do(videoHeight);
p.Do(audioChannels);
p.Do(audioFrequency);
if (s >= 2) {
p.Do(EPMap);
}
p.Do(streamMap);
if (s >= 3) {
p.Do(currentStreamType);
p.Do(currentStreamChannel);
} else {
currentStreamType = -1;
currentStreamChannel = -1;
auto streamInfo = streamMap.find(currentStreamNum);
if (streamInfo != streamMap.end()) {
currentStreamType = streamInfo->second->type;
currentStreamChannel = streamInfo->second->channel;
}
}
}
void PsmfPlayer::DoState(PointerWrap &p) {
auto s = p.Section("PsmfPlayer", 1, 7);
if (!s)
return;
p.Do(videoCodec);
p.Do(videoStreamNum);
p.Do(audioCodec);
p.Do(audioStreamNum);
p.Do(playMode);
p.Do(playSpeed);
p.Do(displayBuffer);
p.Do(displayBufferSize);
p.Do(playbackThreadPriority);
int oldMaxAheadTimestamp = 0;
p.Do(oldMaxAheadTimestamp);
if (s >= 4) {
p.Do(totalDurationTimestamp);
} else {
long oldTimestamp;
p.Do(oldTimestamp);
totalDurationTimestamp = oldTimestamp;
}
if (s >= 2) {
p.Do(totalVideoStreams);
p.Do(totalAudioStreams);
p.Do(playerVersion);
} else {
totalVideoStreams = 1;
totalAudioStreams = 1;
playerVersion = PSMF_PLAYER_VERSION_FULL;
}
if (s >= 3) {
p.Do(videoStep);
} else {
videoStep = 0;
}
if (s >= 4) {
p.Do(warmUp);
} else {
warmUp = 10000;
}
if (s >= 5) {
p.Do(seekDestTimeStamp);
} else {
seekDestTimeStamp = 0;
}
p.DoClass(mediaengine);
p.Do(filehandle);
p.Do(fileoffset);
p.Do(readSize);
p.Do(streamSize);
p.Do(status);
if (s >= 4) {
p.Do(psmfPlayerAtracAu);
}
p.Do(psmfPlayerAvcAu);
if (s >= 7) {
bool hasFinishThread = finishThread != NULL;
p.Do(hasFinishThread);
if (hasFinishThread) {
p.Do(finishThread);
}
} else if (s >= 6) {
p.Do(finishThread);
} else {
finishThread = NULL;
}
}
bool Psmf::setStreamNum(int num) {
currentStreamNum = num;
currentStreamType = -1;
currentStreamChannel = -1;
if (!isValidCurrentStreamNumber())
return false;
PsmfStreamMap::iterator iter = streamMap.find(currentStreamNum);
if (iter == streamMap.end())
return false;
// TODO: This information is *probably* only for the scePsmf lookups?
// Not sure if we need to communicate with the media engine.
int type = iter->second->type;
switch (type) {
case PSMF_AVC_STREAM:
currentVideoStreamNum = num;
break;
case PSMF_ATRAC_STREAM:
case PSMF_PCM_STREAM:
currentAudioStreamNum = num;
break;
}
currentStreamType = type;
currentStreamChannel = iter->second->channel;
return true;
}
bool Psmf::setStreamWithType(int type, int channel) {
for (auto iter : streamMap) {
// Note: this does NOT support PSMF_AUDIO_STREAM.
if (iter.second->type == type && iter.second->channel == channel) {
return setStreamNum(iter.first);
}
}
return false;
}
bool Psmf::setStreamWithTypeNumber(int type, int n) {
for (auto iter : streamMap) {
if (iter.second->matchesType(type)) {
if (n != 0) {
// Keep counting...
n--;
continue;
}
// Okay, this is the one.
return setStreamNum(iter.first);
}
}
return false;
}
int Psmf::FindEPWithTimestamp(int pts) const {
int best = -1;
int bestPts = 0;
for (int i = 0; i < (int)EPMap.size(); ++i) {
const int matchPts = EPMap[i].EPPts;
if (matchPts == pts) {
// Exact match, take it.
return i;
}
// TODO: Does it actually do fuzzy matching?
if (matchPts < pts && matchPts >= bestPts) {
best = i;
bestPts = matchPts;
}
}
return best;
}
static std::map<u32, Psmf *> psmfMap;
static std::map<u32, PsmfPlayer *> psmfPlayerMap;
static Psmf *getPsmf(u32 psmf)
{
auto psmfstruct = PSPPointer<PsmfData>::Create(psmf);
if (!psmfstruct.IsValid())
return 0;
auto iter = psmfMap.find(psmfstruct->headerOffset);
if (iter != psmfMap.end())
return iter->second;
else
return 0;
}
static PsmfPlayer *getPsmfPlayer(u32 psmfplayer)
{
auto iter = psmfPlayerMap.find(Memory::Read_U32(psmfplayer));
if (iter != psmfPlayerMap.end())
return iter->second;
else
return 0;
}
void __PsmfInit()
{
videoPixelMode = GE_CMODE_32BIT_ABGR8888;
videoLoopStatus = PSMF_PLAYER_CONFIG_NO_LOOP;
}
void __PsmfDoState(PointerWrap &p)
{
auto s = p.Section("scePsmf", 1);
if (!s)
return;
p.Do(psmfMap);
}
void __PsmfPlayerDoState(PointerWrap &p)
{
auto s = p.Section("scePsmfPlayer", 1);
if (!s)
return;
p.Do(psmfPlayerMap);
p.Do(videoPixelMode);
p.Do(videoLoopStatus);
}
void __PsmfShutdown()
{
for (auto it = psmfMap.begin(), end = psmfMap.end(); it != end; ++it)
delete it->second;
for (auto it = psmfPlayerMap.begin(), end = psmfPlayerMap.end(); it != end; ++it)
delete it->second;
psmfMap.clear();
psmfPlayerMap.clear();
}
static u32 scePsmfSetPsmf(u32 psmfStruct, u32 psmfData)
{
if (!Memory::IsValidAddress(psmfData)) {
// TODO: Check error code.
ERROR_LOG_REPORT(ME, "scePsmfSetPsmf(%08x, %08x): bad address", psmfStruct, psmfData);
return SCE_KERNEL_ERROR_ILLEGAL_ADDRESS;
}
INFO_LOG(ME, "scePsmfSetPsmf(%08x, %08x)", psmfStruct, psmfData);
Psmf *psmf = new Psmf(Memory::GetPointer(psmfData), psmfData);
PsmfData data = {0};
data.version = psmf->version;
data.headerSize = 0x800;
data.streamSize = psmf->streamSize;
data.streamNum = psmf->numStreams;
data.headerOffset = psmf->headerOffset;
auto iter = psmfMap.find(data.headerOffset);
if (iter != psmfMap.end())
delete iter->second;
psmfMap[data.headerOffset] = psmf;
Memory::WriteStruct(psmfStruct, &data);
return 0;
}
static u32 scePsmfGetNumberOfStreams(u32 psmfStruct) {
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
return hleLogError(ME, ERROR_PSMF_NOT_INITIALIZED, "invalid psmf");
}
return hleLogSuccessI(ME, psmf->numStreams);
}
static u32 scePsmfGetNumberOfSpecificStreams(u32 psmfStruct, int streamType) {
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
return hleLogError(ME, ERROR_PSMF_NOT_INITIALIZED, "invalid psmf");
}
int streamNum = 0;
for (auto it : psmf->streamMap) {
bool match = false;
if (it.second->matchesType(streamType)) {
streamNum++;
}
}
return hleLogSuccessI(ME, streamNum);
}
static u32 scePsmfSpecifyStreamWithStreamType(u32 psmfStruct, u32 streamType, u32 channel) {
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
return hleLogError(ME, ERROR_PSMF_NOT_INITIALIZED, "invalid psmf");
}
if (!psmf->setStreamWithType(streamType, channel)) {
// An invalid type seems to make the stream number invalid, but retain the old type/channel.
psmf->currentStreamNum = ERROR_PSMF_INVALID_ID;
// Also, returns 0 even when no stream found.
return hleLogWarning(ME, 0, "no stream found");
}
return hleLogSuccessI(ME, 0);
}
static u32 scePsmfSpecifyStreamWithStreamTypeNumber(u32 psmfStruct, u32 streamType, u32 typeNum) {
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
return hleLogError(ME, ERROR_PSMF_NOT_INITIALIZED, "invalid psmf");
}
if (!psmf->setStreamWithTypeNumber(streamType, typeNum)) {
// Don't update stream, just bail out.
return hleLogWarning(ME, ERROR_PSMF_INVALID_ID, "no stream found");
}
return hleLogSuccessI(ME, 0);
}
static u32 scePsmfSpecifyStream(u32 psmfStruct, int streamNum) {
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
return hleLogError(ME, ERROR_PSMF_NOT_INITIALIZED, "invalid psmf");
}
if (!psmf->setStreamNum(streamNum)) {
psmf->setStreamNum(ERROR_PSMF_NOT_INITIALIZED);
return hleLogWarning(ME, ERROR_PSMF_INVALID_ID, "bad stream id");
}
return hleLogSuccessI(ME, 0);
}
static u32 scePsmfGetVideoInfo(u32 psmfStruct, u32 videoInfoAddr) {
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetVideoInfo(%08x, %08x): invalid psmf", psmfStruct, videoInfoAddr);
return ERROR_PSMF_NOT_FOUND;
}
INFO_LOG(ME, "scePsmfGetVideoInfo(%08x, %08x)", psmfStruct, videoInfoAddr);
if (Memory::IsValidAddress(videoInfoAddr)) {
Memory::Write_U32(psmf->videoWidth, videoInfoAddr);
Memory::Write_U32(psmf->videoHeight, videoInfoAddr + 4);
}
return 0;
}
static u32 scePsmfGetAudioInfo(u32 psmfStruct, u32 audioInfoAddr) {
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetAudioInfo(%08x, %08x): invalid psmf", psmfStruct, audioInfoAddr);
return ERROR_PSMF_NOT_FOUND;
}
INFO_LOG(ME, "scePsmfGetAudioInfo(%08x, %08x)", psmfStruct, audioInfoAddr);
if (Memory::IsValidAddress(audioInfoAddr)) {
Memory::Write_U32(psmf->audioChannels, audioInfoAddr);
Memory::Write_U32(psmf->audioFrequency, audioInfoAddr + 4);
}
return 0;
}
static u32 scePsmfGetCurrentStreamType(u32 psmfStruct, u32 typeAddr, u32 channelAddr) {
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
return hleLogError(ME, ERROR_PSMF_NOT_INITIALIZED, "invalid psmf");
}
if (psmf->currentStreamNum == ERROR_PSMF_NOT_INITIALIZED) {
return hleLogError(ME, ERROR_PSMF_NOT_INITIALIZED, "no stream set");
}
if (!Memory::IsValidAddress(typeAddr) || !Memory::IsValidAddress(channelAddr)) {
return hleLogError(ME, SCE_KERNEL_ERROR_ILLEGAL_ADDRESS, "bad pointers");
}
if (psmf->currentStreamType != -1) {
Memory::Write_U32(psmf->currentStreamType, typeAddr);
Memory::Write_U32(psmf->currentStreamChannel, channelAddr);
}
return hleLogSuccessI(ME, 0);
}
static u32 scePsmfGetStreamSize(u32 psmfStruct, u32 sizeAddr)
{
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetStreamSize(%08x, %08x): invalid psmf", psmfStruct, sizeAddr);
return ERROR_PSMF_NOT_FOUND;
}
DEBUG_LOG(ME, "scePsmfGetStreamSize(%08x, %08x)", psmfStruct, sizeAddr);
if (Memory::IsValidAddress(sizeAddr)) {
Memory::Write_U32(psmf->streamSize, sizeAddr);
}
return 0;
}
static u32 scePsmfQueryStreamOffset(u32 bufferAddr, u32 offsetAddr)
{
WARN_LOG(ME, "scePsmfQueryStreamOffset(%08x, %08x)", bufferAddr, offsetAddr);
if (Memory::IsValidAddress(offsetAddr)) {
Memory::Write_U32(bswap32(Memory::Read_U32(bufferAddr + PSMF_STREAM_OFFSET_OFFSET)), offsetAddr);
}
return 0;
}
static u32 scePsmfQueryStreamSize(u32 bufferAddr, u32 sizeAddr)
{
WARN_LOG(ME, "scePsmfQueryStreamSize(%08x, %08x)", bufferAddr, sizeAddr);
if (Memory::IsValidAddress(sizeAddr)) {
Memory::Write_U32(bswap32(Memory::Read_U32(bufferAddr + PSMF_STREAM_SIZE_OFFSET)), sizeAddr);
}
return 0;
}
static u32 scePsmfGetHeaderSize(u32 psmfStruct, u32 sizeAddr)
{
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetHeaderSize(%08x, %08x): invalid psmf", psmfStruct, sizeAddr);
return ERROR_PSMF_NOT_FOUND;
}
DEBUG_LOG(ME, "scePsmfGetHeaderSize(%08x, %08x)", psmfStruct, sizeAddr);
if (Memory::IsValidAddress(sizeAddr)) {
Memory::Write_U32(psmf->headerSize, sizeAddr);
}
return 0;
}
static u32 scePsmfGetPsmfVersion(u32 psmfStruct)
{
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetPsmfVersion(%08x): invalid psmf", psmfStruct);
return ERROR_PSMF_NOT_FOUND;
}
DEBUG_LOG(ME, "scePsmfGetPsmfVersion(%08x)", psmfStruct);
return psmf->version;
}
static u32 scePsmfVerifyPsmf(u32 psmfAddr)
{
u32 magic = Memory::Read_U32(psmfAddr);
if (magic != PSMF_MAGIC) {
ERROR_LOG(ME, "scePsmfVerifyPsmf(%08x): bad magic %08x", psmfAddr, magic);
return ERROR_PSMF_NOT_FOUND;
}
int version = Memory::Read_U32(psmfAddr + PSMF_STREAM_VERSION_OFFSET);
if (version < 0) {
ERROR_LOG(ME, "scePsmfVerifyPsmf(%08x): bad version %08x", psmfAddr, version);
return ERROR_PSMF_NOT_FOUND;
}
// Kurohyou 2 (at least the demo) uses an uninitialized value that happens to be zero on the PSP.
// It appears to be written by scePsmfVerifyPsmf(), so we write some bytes into the stack here.
Memory::Memset(currentMIPS->r[MIPS_REG_SP] - 0x20, 0, 0x20);
DEBUG_LOG(ME, "scePsmfVerifyPsmf(%08x)", psmfAddr);
return 0;
}
static u32 scePsmfGetNumberOfEPentries(u32 psmfStruct)
{
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetNumberOfEPentries(%08x): invalid psmf", psmfStruct);
return ERROR_PSMF_NOT_FOUND;
}
DEBUG_LOG(ME, "scePsmfGetNumberOfEPentries(%08x)", psmfStruct);
return psmf->EPMapEntriesNum;
}
static u32 scePsmfGetPresentationStartTime(u32 psmfStruct, u32 startTimeAddr)
{
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetPresentationStartTime(%08x, %08x): invalid psmf", psmfStruct, startTimeAddr);
return ERROR_PSMF_NOT_FOUND;
}
DEBUG_LOG(ME, "scePsmfGetPresentationStartTime(%08x, %08x)", psmfStruct, startTimeAddr);
if (Memory::IsValidAddress(startTimeAddr)) {
Memory::Write_U32(psmf->presentationStartTime, startTimeAddr);
}
return 0;
}
static u32 scePsmfGetPresentationEndTime(u32 psmfStruct, u32 endTimeAddr)
{
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetPresentationEndTime(%08x, %08x): invalid psmf", psmfStruct, endTimeAddr);
return ERROR_PSMF_NOT_FOUND;
}
DEBUG_LOG(ME, "scePsmfGetPresentationEndTime(%08x, %08x)", psmfStruct, endTimeAddr);
if (Memory::IsValidAddress(endTimeAddr)) {
Memory::Write_U32(psmf->presentationEndTime, endTimeAddr);
}
return 0;
}
static u32 scePsmfGetCurrentStreamNumber(u32 psmfStruct) {
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
return hleLogError(ME, ERROR_PSMF_NOT_INITIALIZED, "invalid psmf");
}
if (psmf->currentStreamNum < 0) {
return hleLogError(ME, psmf->currentStreamNum, "invalid stream");
}
return hleLogSuccessI(ME, psmf->currentStreamNum);
}
static u32 scePsmfCheckEPMap(u32 psmfStruct)
{
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfCheckEPMap(%08x): invalid psmf", psmfStruct);
return ERROR_PSMF_NOT_FOUND;
}
DEBUG_LOG(ME, "scePsmfCheckEPMap(%08x)", psmfStruct);
return psmf->EPMap.empty() ? ERROR_PSMF_NOT_FOUND : 0;
}
static u32 scePsmfGetEPWithId(u32 psmfStruct, int epid, u32 entryAddr)
{
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetEPWithId(%08x, %i, %08x): invalid psmf", psmfStruct, epid, entryAddr);
return ERROR_PSMF_NOT_INITIALIZED;
}
DEBUG_LOG(ME, "scePsmfGetEPWithId(%08x, %i, %08x)", psmfStruct, epid, entryAddr);
if (epid < 0 || epid >= (int)psmf->EPMap.size()) {
ERROR_LOG(ME, "scePsmfGetEPWithId(%08x, %i): invalid id", psmfStruct, epid);
return ERROR_PSMF_NOT_FOUND;
}
if (Memory::IsValidAddress(entryAddr)) {
Memory::WriteStruct(entryAddr, &psmf->EPMap[epid]);
}
return 0;
}
static u32 scePsmfGetEPWithTimestamp(u32 psmfStruct, u32 ts, u32 entryAddr)
{
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetEPWithTimestamp(%08x, %i, %08x): invalid psmf", psmfStruct, ts, entryAddr);
return ERROR_PSMF_NOT_INITIALIZED;
}
DEBUG_LOG(ME, "scePsmfGetEPWithTimestamp(%08x, %i, %08x)", psmfStruct, ts, entryAddr);
if (ts < psmf->presentationStartTime) {
ERROR_LOG(ME, "scePsmfGetEPWithTimestamp(%08x, %i): invalid timestamp", psmfStruct, ts);
return ERROR_PSMF_NOT_FOUND;
}
int epid = psmf->FindEPWithTimestamp(ts);
if (epid < 0 || epid >= (int)psmf->EPMap.size()) {
ERROR_LOG(ME, "scePsmfGetEPWithTimestamp(%08x, %i): invalid id", psmfStruct, epid);
return ERROR_PSMF_NOT_FOUND;
}
if (Memory::IsValidAddress(entryAddr)) {
Memory::WriteStruct(entryAddr, &psmf->EPMap[epid]);
}
return 0;
}
static u32 scePsmfGetEPidWithTimestamp(u32 psmfStruct, u32 ts)
{
Psmf *psmf = getPsmf(psmfStruct);
if (!psmf) {
ERROR_LOG(ME, "scePsmfGetEPidWithTimestamp(%08x, %i): invalid psmf", psmfStruct, ts);