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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2002-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$
*
*/
#include "stdafx.h"
#include "base/engine.h"
#include "common/config-manager.h"
#include "common/file.h"
#include "common/system.h"
#include "common/timer.h"
#include "common/util.h"
#include "scumm/bomp.h"
#include "scumm/imuse_digi/dimuse.h"
#include "scumm/imuse.h"
#include "scumm/scumm.h"
#include "scumm/sound.h"
#include "scumm/smush/channel.h"
#include "scumm/smush/chunk_type.h"
#include "scumm/smush/chunk.h"
#include "scumm/smush/smush_font.h"
#include "scumm/smush/smush_mixer.h"
#include "scumm/smush/smush_player.h"
#include "scumm/insane/insane.h"
#include "sound/mixer.h"
#include "sound/vorbis.h"
#include "sound/mp3.h"
#ifdef DUMP_SMUSH_FRAMES
#include <png.h>
#endif
#ifdef USE_ZLIB
#include <zlib.h>
#endif
namespace Scumm {
const int MAX_STRINGS = 200;
class StringResource {
private:
struct {
int id;
char *string;
} _strings[MAX_STRINGS];
int _nbStrings;
int _lastId;
const char *_lastString;
public:
StringResource() :
_nbStrings(0),
_lastId(-1) {
};
~StringResource() {
for (int32 i = 0; i < _nbStrings; i++) {
delete []_strings[i].string;
}
}
bool init(char *buffer, int32 length) {
char *def_start = strchr(buffer, '#');
while (def_start != NULL) {
char *def_end = strchr(def_start, '\n');
assert(def_end != NULL);
char *id_end = def_end;
while (id_end >= def_start && !isdigit(*(id_end-1))) {
id_end--;
}
assert(id_end > def_start);
char *id_start = id_end;
while (isdigit(*(id_start - 1))) {
id_start--;
}
char idstring[32];
memcpy(idstring, id_start, id_end - id_start);
idstring[id_end - id_start] = 0;
int32 id = atoi(idstring);
char *data_start = def_end;
while (*data_start == '\n' || *data_start == '\r') {
data_start++;
}
char *data_end = data_start;
while (1) {
if (data_end[-2] == '\r' && data_end[-1] == '\n' && data_end[0] == '\r' && data_end[1] == '\n') {
break;
}
// In Russian Full Throttle strings are finished with
// just one pair of CR-LF
if (data_end[-2] == '\r' && data_end[-1] == '\n' && data_end[0] == '#') {
break;
}
data_end++;
if (data_end >= buffer + length) {
data_end = buffer + length;
break;
}
}
data_end -= 2;
assert(data_end > data_start);
char *value = new char[data_end - data_start + 1];
assert(value);
memcpy(value, data_start, data_end - data_start);
value[data_end - data_start] = 0;
char *line_start = value;
char *line_end;
while ((line_end = strchr(line_start, '\n'))) {
line_start = line_end+1;
if (line_start[0] == '/' && line_start[1] == '/') {
line_start += 2;
if (line_end[-1] == '\r')
line_end[-1] = ' ';
else
*line_end++ = ' ';
memmove(line_end, line_start, strlen(line_start)+1);
}
}
_strings[_nbStrings].id = id;
_strings[_nbStrings].string = value;
_nbStrings ++;
def_start = strchr(data_end + 2, '#');
}
return true;
}
const char *get(int id) {
if (id == _lastId) {
return _lastString;
}
debugC(DEBUG_SMUSH, "StringResource::get(%d)", id);
for (int i = 0; i < _nbStrings; i++) {
if (_strings[i].id == id) {
_lastId = id;
_lastString = _strings[i].string;
return _strings[i].string;
}
}
warning("invalid string id : %d", id);
_lastId = -1;
_lastString = "unknown string";
return _lastString;
}
};
static StringResource *getStrings(ScummEngine *vm, const char *file, bool is_encoded) {
debugC(DEBUG_SMUSH, "trying to read text ressources from %s", file);
ScummFile theFile;
vm->openFile(theFile, file);
if (!theFile.isOpen()) {
return 0;
}
int32 length = theFile.size();
char *filebuffer = new char [length + 1];
assert(filebuffer);
theFile.read(filebuffer, length);
filebuffer[length] = 0;
if (is_encoded) {
enum {
ETRS_HEADER_LENGTH = 16
};
assert(length > ETRS_HEADER_LENGTH);
Chunk::type type = READ_BE_UINT32(filebuffer);
if (type != TYPE_ETRS) {
delete [] filebuffer;
return getStrings(vm, file, false);
}
char *old = filebuffer;
filebuffer = new char[length - ETRS_HEADER_LENGTH + 1];
for (int32 i = ETRS_HEADER_LENGTH; i < length; i++) {
filebuffer[i - ETRS_HEADER_LENGTH] = old[i] ^ 0xCC;
}
filebuffer[length - ETRS_HEADER_LENGTH] = '\0';
delete []old;
length -= ETRS_HEADER_LENGTH;
}
StringResource *sr = new StringResource;
assert(sr);
sr->init(filebuffer, length);
delete []filebuffer;
return sr;
}
void SmushPlayer::timerCallback(void *refCon) {
((SmushPlayer *)refCon)->parseNextFrame();
#ifdef _WIN32_WCE
((SmushPlayer *)refCon)->_inTimer = true;
#endif
}
SmushPlayer::SmushPlayer(ScummEngine_v6 *scumm, int speed) {
_vm = scumm;
_version = -1;
_nbframes = 0;
_smixer = 0;
_strings = NULL;
_sf[0] = NULL;
_sf[1] = NULL;
_sf[2] = NULL;
_sf[3] = NULL;
_sf[4] = NULL;
_base = NULL;
_frameBuffer = NULL;
_specialBuffer = NULL;
_skipNext = false;
_subtitles = ConfMan.getBool("subtitles");
_dst = NULL;
_storeFrame = false;
_compressedFileMode = false;
_width = 0;
_height = 0;
_IACTpos = 0;
_soundFrequency = 22050;
_initDone = false;
_speed = speed;
_insanity = false;
_middleAudio = false;
_skipPalette = false;
#ifdef _WIN32_WCE
_inTimer = false;
#endif
}
SmushPlayer::~SmushPlayer() {
release();
}
void SmushPlayer::init() {
_frame = 0;
_alreadyInit = false;
_vm->_smushVideoShouldFinish = false;
_vm->setDirtyColors(0, 255);
_dst = _vm->virtscr[0].getPixels(0, 0);
// HACK HACK HACK: This is an *evil* trick, beware!
// We do this to fix bug #1037052. A proper solution would change all the
// drawing code to use the pitch value specified by the virtual screen.
// However, since a lot of the SMUSH code currently assumes the screen
// width and pitch to be equal, this will require lots of changes. So
// we resort to this hackish solution for now.
_origPitch = _vm->virtscr[0].pitch;
_origNumStrips = _vm->gdi._numStrips;
_vm->virtscr[0].pitch = _vm->virtscr[0].w;
_vm->gdi._numStrips = _vm->virtscr[0].w / 8;
_smixer = new SmushMixer(_vm->_mixer);
g_timer->installTimerProc(&timerCallback, _speed, this);
_initDone = true;
}
void SmushPlayer::release() {
if (!_initDone)
return;
_vm->_timer->removeTimerProc(&timerCallback);
_vm->_smushVideoShouldFinish = true;
for (int i = 0; i < 5; i++) {
if (_sf[i]) {
delete _sf[i];
_sf[i] = NULL;
}
}
if (_strings) {
delete _strings;
_strings = NULL;
}
if (_smixer) {
_smixer->stop();
delete _smixer;
_smixer = NULL;
}
if (_base) {
delete _base;
_base = NULL;
}
if (_specialBuffer) {
free(_specialBuffer);
_specialBuffer = NULL;
}
_vm->_mixer->stopHandle(_compressedFileSoundHandle);
_vm->_mixer->stopHandle(_IACTchannel);
_IACTstream = 0;
_vm->_fullRedraw = true;
// WORKAROUND bug #1035739: This is hack to workaround some ugly palette
// issues, see the mentioned bug report for details.
_vm->_doEffect = false;
// HACK HACK HACK: This is an *evil* trick, beware! See above for
// some explanation.
_vm->virtscr[0].pitch = _origPitch;
_vm->gdi._numStrips = _origNumStrips;
_initDone = false;
}
void SmushPlayer::checkBlock(const Chunk &b, Chunk::type type_expected, uint32 min_size) {
if (type_expected != b.getType()) {
error("Chunk type is different from expected : %x != %x", b.getType(), type_expected);
}
if (min_size > b.getSize()) {
error("Chunk size is inferior than minimum required size : %d < %d", b.getSize(), min_size);
}
}
void SmushPlayer::handleSoundBuffer(int32 track_id, int32 index, int32 max_frames, int32 flags, int32 vol, int32 pan, Chunk &b, int32 size) {
debugC(DEBUG_SMUSH, "SmushPlayer::handleSoundBuffer(%d, %d)", track_id, index);
// if ((flags & 128) == 128) {
// return;
// }
// if ((flags & 64) == 64) {
// return;
// }
SmushChannel *c = _smixer->findChannel(track_id);
if (c == NULL) {
c = new SaudChannel(track_id, _soundFrequency);
_smixer->addChannel(c);
}
if ((_middleAudio) && (index != 0)) {
c->setParameters(max_frames, flags, vol, pan, index);
} else if (index == 0) {
c->setParameters(max_frames, flags, vol, pan, index);
} else {
c->checkParameters(index, max_frames, flags, vol, pan);
}
_middleAudio = false;
c->appendData(b, size);
}
void SmushPlayer::handleSoundFrame(Chunk &b) {
checkBlock(b, TYPE_PSAD);
debugC(DEBUG_SMUSH, "SmushPlayer::handleSoundFrame()");
int32 track_id = b.getWord();
int32 index = b.getWord();
int32 max_frames = b.getWord();
int32 flags = b.getWord();
int32 vol = b.getByte();
int32 pan = b.getChar();
if (index == 0) {
debugC(DEBUG_SMUSH, "track_id:%d, max_frames:%d, flags:%d, vol:%d, pan:%d", track_id, max_frames, flags, vol, pan);
}
int32 size = b.getSize() - 10;
handleSoundBuffer(track_id, index, max_frames, flags, vol, pan, b, size);
}
void SmushPlayer::handleSkip(Chunk &b) {
checkBlock(b, TYPE_SKIP, 4);
int32 code = b.getDword();
debugC(DEBUG_SMUSH, "SmushPlayer::handleSkip(%d)", code);
if (code >= 0 && code < 37)
_skipNext = _skips[code];
else
_skipNext = true;
}
void SmushPlayer::handleStore(Chunk &b) {
debugC(DEBUG_SMUSH, "SmushPlayer::handleStore()");
checkBlock(b, TYPE_STOR, 4);
_storeFrame = true;
}
void SmushPlayer::handleFetch(Chunk &b) {
debugC(DEBUG_SMUSH, "SmushPlayer::handleFetch()");
checkBlock(b, TYPE_FTCH, 6);
if (_frameBuffer != NULL) {
memcpy(_dst, _frameBuffer, _width * _height);
}
}
void SmushPlayer::handleIACT(Chunk &b) {
checkBlock(b, TYPE_IACT, 8);
debugC(DEBUG_SMUSH, "SmushPlayer::handleImuseAction()");
/* int code = */ b.getWord();
int flags = b.getWord();
int unknown = b.getShort();
int track_flags = b.getWord();
assert(flags == 46 && unknown == 0);
int track_id = b.getWord();
int index = b.getWord();
int nbframes = b.getWord();
int32 size = b.getDword();
int32 bsize = b.getSize() - 18;
if (_vm->_gameId != GID_CMI) {
int32 track = track_id;
if (track_flags == 1) {
track = track_id + 100;
} else if (track_flags == 2) {
track = track_id + 200;
} else if (track_flags == 3) {
track = track_id + 300;
} else if ((track_flags >= 100) && (track_flags <= 163)) {
track = track_id + 400;
} else if ((track_flags >= 200) && (track_flags <= 263)) {
track = track_id + 500;
} else if ((track_flags >= 300) && (track_flags <= 363)) {
track = track_id + 600;
} else {
error("ImuseChannel::handleIACT(): bad track_flags: %d", track_flags);
}
debugC(DEBUG_SMUSH, "SmushPlayer::handleIACT(): %d, %d, %d", track, index, track_flags);
SmushChannel *c = _smixer->findChannel(track);
if (c == 0) {
c = new ImuseChannel(track, _soundFrequency);
_smixer->addChannel(c);
}
if (index == 0)
c->setParameters(nbframes, size, track_flags, unknown, 0);
else
c->checkParameters(index, nbframes, size, track_flags, unknown);
c->appendData(b, bsize);
} else {
byte output_data[4096];
byte *src = (byte *)malloc(bsize);
b.read(src, bsize);
byte *d_src = src;
byte value;
while (bsize > 0) {
if (_IACTpos >= 2) {
int32 len = READ_BE_UINT16(_IACToutput) + 2;
len -= _IACTpos;
if (len > bsize) {
memcpy(_IACToutput + _IACTpos, d_src, bsize);
_IACTpos += bsize;
bsize = 0;
} else {
memcpy(_IACToutput + _IACTpos, d_src, len);
byte *dst = output_data;
byte *d_src2 = _IACToutput;
d_src2 += 2;
int32 count = 1024;
byte variable1 = *d_src2++;
byte variable2 = variable1 / 16;
variable1 &= 0x0f;
do {
value = *(d_src2++);
if (value == 0x80) {
*dst++ = *d_src2++;
*dst++ = *d_src2++;
} else {
int16 val = (int8)value << variable2;
*dst++ = val >> 8;
*dst++ = (byte)(val);
}
value = *(d_src2++);
if (value == 0x80) {
*dst++ = *d_src2++;
*dst++ = *d_src2++;
} else {
int16 val = (int8)value << variable1;
*dst++ = val >> 8;
*dst++ = (byte)(val);
}
} while (--count);
if (!_IACTchannel.isActive()) {
_IACTstream = makeAppendableAudioStream(22050, SoundMixer::FLAG_STEREO | SoundMixer::FLAG_16BITS, 400000);
_vm->_mixer->playInputStream(SoundMixer::kSFXAudioDataType, &_IACTchannel, _IACTstream);
}
_IACTstream->append(output_data, 0x1000);
bsize -= len;
d_src += len;
_IACTpos = 0;
}
} else {
if (bsize > 1 && _IACTpos == 0) {
*(_IACToutput + 0) = *d_src++;
_IACTpos = 1;
bsize--;
}
*(_IACToutput + _IACTpos) = *d_src++;
_IACTpos++;
bsize--;
}
}
free(src);
}
}
void SmushPlayer::handleTextResource(Chunk &b) {
int pos_x = b.getShort();
int pos_y = b.getShort();
int flags = b.getShort();
int left = b.getShort();
int top = b.getShort();
int right = b.getShort();
/*int32 height =*/ b.getShort();
/*int32 unk2 =*/ b.getWord();
const char *str;
char *string = NULL, *string2 = NULL;
if (b.getType() == TYPE_TEXT) {
string = (char *)malloc(b.getSize() - 16);
str = string;
b.read(string, b.getSize() - 16);
} else {
int string_id = b.getWord();
if (!_strings)
return;
str = _strings->get(string_id);
}
// if subtitles disabled and bit 3 is set, then do not draw
if ((!_subtitles) && ((flags & 8) == 8))
return;
SmushFont *sf = _sf[0];
int color = 15;
while (*str == '/') {
str++; // For Full Throttle text resources
}
byte transBuf[512];
if (_vm->_gameId == GID_CMI) {
_vm->translateText((const byte *)str - 1, transBuf);
while (*str++ != '/')
;
string2 = (char *)transBuf;
// If string2 contains formatting information there probably
// wasn't any translation for it in the language.tab file. In
// that case, pretend there is no string2.
if (string2[0] == '^')
string2[0] = 0;
}
while (str[0] == '^') {
switch (str[1]) {
case 'f':
{
int id = str[3] - '0';
str += 4;
sf = _sf[id];
}
break;
case 'c':
{
color = str[4] - '0' + 10 *(str[3] - '0');
str += 5;
}
break;
default:
error("invalid escape code in text string");
}
}
assert(sf != NULL);
sf->setColor(color);
if (_vm->_gameId == GID_CMI && string2[0] != 0) {
str = string2;
}
// flags:
// bit 0 - center 1
// bit 1 - not used 2
// bit 2 - ??? 4
// bit 3 - wrap around 8
switch (flags & 9) {
case 0:
sf->drawString(str, _dst, _width, _height, pos_x, pos_y, false);
break;
case 1:
sf->drawString(str, _dst, _width, _height, pos_x, MAX(pos_y, top), true);
break;
case 8:
// FIXME: Is 'right' the maximum line width here, just
// as it is in the next case? It's used several times
// in The Dig's intro, where 'left' and 'right' are
// always 0 and 321 respectively, and apparently we
// handle that correctly.
sf->drawStringWrap(str, _dst, _width, _height, pos_x, MAX(pos_y, top), left, right, false);
break;
case 9:
// In this case, the 'right' parameter is actually the
// maximum line width. This explains why it's sometimes
// smaller than 'left'.
//
// Note that in The Dig's "Spacetime Six" movie it's
// 621. I have no idea what that means.
sf->drawStringWrap(str, _dst, _width, _height, pos_x, MAX(pos_y, top), left, MIN(left + right, _width), true);
break;
default:
warning("SmushPlayer::handleTextResource. Not handled flags: %d", flags);
}
if (string != NULL) {
free (string);
}
}
const char *SmushPlayer::getString(int id) {
return _strings->get(id);
}
bool SmushPlayer::readString(const char *file) {
const char *i = strrchr(file, '.');
if (i == NULL) {
error("invalid filename : %s", file);
}
char fname[260];
memcpy(fname, file, i - file);
strcpy(fname + (i - file), ".trs");
if ((_strings = getStrings(_vm, fname, false)) != 0) {
return true;
}
if ((_strings = getStrings(_vm, "digtxt.trs", true)) != 0) {
return true;
}
return false;
}
void SmushPlayer::readPalette(byte *out, Chunk &in) {
in.read(out, 0x300);
}
static byte delta_color(byte org_color, int16 delta_color) {
int t = (org_color * 129 + delta_color) / 128;
if (t > 255)
t = 255;
if (t < 0)
t = 0;
return (byte)t;
}
void SmushPlayer::handleDeltaPalette(Chunk &b) {
checkBlock(b, TYPE_XPAL);
debugC(DEBUG_SMUSH, "SmushPlayer::handleDeltaPalette()");
if (b.getSize() == 0x300 * 3 + 4) {
b.getWord();
b.getWord();
for (int i = 0; i < 0x300; i++) {
_deltaPal[i] = b.getWord();
}
readPalette(_pal, b);
setPalette(_pal);
} else if (b.getSize() == 6) {
b.getWord();
b.getWord();
b.getWord();
for (int i = 0; i < 0x300; i++) {
_pal[i] = delta_color(_pal[i], _deltaPal[i]);
}
setPalette(_pal);
} else {
error("SmushPlayer::handleDeltaPalette() Wrong size for DeltaPalette");
}
}
void SmushPlayer::handleNewPalette(Chunk &b) {
checkBlock(b, TYPE_NPAL, 0x300);
debugC(DEBUG_SMUSH, "SmushPlayer::handleNewPalette()");
if (_skipPalette)
return;
readPalette(_pal, b);
setPalette(_pal);
}
void smush_decode_codec1(byte *dst, byte *src, int left, int top, int height, int width, int dstWidth);
#ifdef USE_ZLIB
void SmushPlayer::handleZlibFrameObject(Chunk &b) {
if (_skipNext) {
_skipNext = false;
return;
}
int32 chunkSize = b.getSize();
byte *chunkBuffer = (byte *)malloc(chunkSize);
assert(chunkBuffer);
b.read(chunkBuffer, chunkSize);
unsigned long decompressedSize = READ_BE_UINT32(chunkBuffer);
byte *fobjBuffer = (byte *)malloc(decompressedSize);
int result = uncompress(fobjBuffer, &decompressedSize, chunkBuffer + 4, chunkSize - 4);
if (result != Z_OK)
error("SmushPlayer::handleZlibFrameObject() Zlib uncompress error");
free(chunkBuffer);
byte *ptr = fobjBuffer;
int codec = READ_LE_UINT16(ptr); ptr += 2;
int left = READ_LE_UINT16(ptr); ptr += 2;
int top = READ_LE_UINT16(ptr); ptr += 2;
int width = READ_LE_UINT16(ptr); ptr += 2;
int height = READ_LE_UINT16(ptr); ptr += 2;
if ((height == 242) && (width == 384)) {
if (_specialBuffer == 0)
_specialBuffer = (byte *)malloc(242 * 384);
_dst = _specialBuffer;
} else if ((height > _vm->_screenHeight) || (width > _vm->_screenWidth))
return;
// FT Insane uses smaller frames to draw overlays with moving objects
// Other .san files do have them as well but their purpose in unknown
// and often it causes memory overdraw. So just skip those frames
else if (!_insanity && ((height != _vm->_screenHeight) || (width != _vm->_screenWidth)))
return;
if (!_alreadyInit) {
_codec37.init(width, height);
_codec47.init(width, height);
_alreadyInit = true;
}
if ((height == 242) && (width == 384)) {
_width = width;
_height = height;
} else {
_width = _vm->_screenWidth;
_height = _vm->_screenHeight;
}
switch (codec) {
case 1:
case 3:
smush_decode_codec1(_dst, fobjBuffer + 14, left, top, height, width, _vm->_screenWidth);
break;
case 37:
_codec37.decode(_dst, fobjBuffer + 14);
break;
case 47:
_codec47.decode(_dst, fobjBuffer + 14);
break;
default:
error("Invalid codec for frame object : %d", (int)codec);
}
if (_storeFrame) {
if (_frameBuffer == NULL) {
_frameBuffer = (byte *)malloc(_width * _height);
}
memcpy(_frameBuffer, _dst, _width * _height);
_storeFrame = false;
}
free(fobjBuffer);
}
#endif
void SmushPlayer::handleFrameObject(Chunk &b) {
checkBlock(b, TYPE_FOBJ, 14);
if (_skipNext) {
_skipNext = false;
return;
}
int codec = b.getWord();
int left = b.getWord();
int top = b.getWord();
int width = b.getWord();
int height = b.getWord();
if ((height == 242) && (width == 384)) {
if (_specialBuffer == 0)
_specialBuffer = (byte *)malloc(242 * 384);
_dst = _specialBuffer;
} else if ((height > _vm->_screenHeight) || (width > _vm->_screenWidth))
return;
// FT Insane uses smaller frames to draw overlays with moving objects
// Other .san files do have them as well but their purpose in unknown
// and often it causes memory overdraw. So just skip those frames
else if (!_insanity && ((height != _vm->_screenHeight) || (width != _vm->_screenWidth)))
return;
if (!_alreadyInit) {
_codec37.init(width, height);
_codec47.init(width, height);
_alreadyInit = true;
}
if ((height == 242) && (width == 384)) {
_width = width;
_height = height;
} else {
_width = _vm->_screenWidth;
_height = _vm->_screenHeight;
}
b.getWord();
b.getWord();
int32 chunk_size = b.getSize() - 14;
byte *chunk_buffer = (byte *)malloc(chunk_size);
assert(chunk_buffer);
b.read(chunk_buffer, chunk_size);
switch (codec) {
case 1:
case 3:
smush_decode_codec1(_dst, chunk_buffer, left, top, height, width, _vm->_screenWidth);
break;
case 37:
_codec37.decode(_dst, chunk_buffer);
break;
case 47:
_codec47.decode(_dst, chunk_buffer);
break;
default:
error("Invalid codec for frame object : %d", (int)codec);
}
if (_storeFrame) {
if (_frameBuffer == NULL) {
_frameBuffer = (byte *)malloc(_width * _height);
}
memcpy(_frameBuffer, _dst, _width * _height);
_storeFrame = false;
}
free(chunk_buffer);
}
void SmushPlayer::handleFrame(Chunk &b) {
checkBlock(b, TYPE_FRME);
debugC(DEBUG_SMUSH, "SmushPlayer::handleFrame(%d)", _frame);
_skipNext = false;
uint32 start_time, end_time;
start_time = _vm->_system->getMillis();
if (_insanity) {
_vm->_insane->procPreRendering();
}
while (!b.eof()) {
Chunk *sub = b.subBlock();
if (sub->getSize() & 1) b.seek(1);
switch (sub->getType()) {
case TYPE_NPAL:
handleNewPalette(*sub);
break;
case TYPE_FOBJ:
handleFrameObject(*sub);
break;
#ifdef USE_ZLIB
case TYPE_ZFOB:
handleZlibFrameObject(*sub);
break;
#endif
case TYPE_PSAD:
if (!_compressedFileMode)
handleSoundFrame(*sub);
break;
case TYPE_TRES:
handleTextResource(*sub);
break;
case TYPE_XPAL:
handleDeltaPalette(*sub);
break;
case TYPE_IACT:
// FIXME: check parameters
if (_insanity)
_vm->_insane->procIACT(_dst, 0, 0, 0, *sub, 0, 0);
else {
if (!_compressedFileMode)
handleIACT(*sub);
}
break;
case TYPE_STOR:
handleStore(*sub);
break;
case TYPE_FTCH:
handleFetch(*sub);
break;
case TYPE_SKIP:
if (_insanity)
_vm->_insane->procSKIP(*sub);
else
handleSkip(*sub);
break;
case TYPE_TEXT:
handleTextResource(*sub);
break;
default:
error("Unknown frame subChunk found : %s, %d", Chunk::ChunkString(sub->getType()), sub->getSize());
}
delete sub;
}
if (_insanity) {
_vm->_insane->procPostRendering(_dst, 0, 0, 0, _frame, _nbframes-1);
}
end_time = _vm->_system->getMillis();
#ifdef _WIN32_WCE
if (!_inTimer)
updateScreen();
#else
updateScreen();
#endif
_smixer->handleFrame();
debugC(DEBUG_SMUSH, "Smush stats: FRME( %03d ), Limit(%d)", end_time - start_time, _speed / 1000);
_frame++;
}
void SmushPlayer::handleAnimHeader(Chunk &b) {
checkBlock(b, TYPE_AHDR, 0x300 + 6);
debugC(DEBUG_SMUSH, "SmushPlayer::handleAnimHeader()");
_version = b.getWord();
_nbframes = b.getWord();
b.getWord();
if (!_skipPalette) {
readPalette(_pal, b);
setPalette(_pal);
}
}
void SmushPlayer::setupAnim(const char *file) {
Chunk *sub;
int i;
char file_font[11];
_base = new FileChunk(file);
sub = _base->subBlock();
checkBlock(*sub, TYPE_AHDR);
handleAnimHeader(*sub);
if (_insanity) {
if (!((_vm->_features & GF_DEMO) && (_vm->_features & GF_PC)))
readString("mineroad.trs");
} else
readString(file);
if (_vm->_gameId == GID_FT) {
if (!((_vm->_features & GF_DEMO) && (_vm->_features & GF_PC))) {
_sf[0] = new SmushFont(true, false);
_sf[1] = new SmushFont(true, false);
_sf[2] = new SmushFont(true, false);
_sf[3] = new SmushFont(true, false);
_sf[0]->loadFont("scummfnt.nut");
_sf[1]->loadFont("techfnt.nut");
_sf[2]->loadFont("titlfnt.nut");
_sf[3]->loadFont("specfnt.nut");
}
} else if (_vm->_gameId == GID_DIG) {
if (!(_vm->_features & GF_DEMO)) {
for (i = 0; i < 4; i++) {
sprintf(file_font, "font%d.nut", i);
_sf[i] = new SmushFont(i != 0, false);
_sf[i]->loadFont(file_font);
}
}
} else if (_vm->_gameId == GID_CMI) {
for (i = 0; i < 5; i++) {
if ((_vm->_features & GF_DEMO) && (i == 4))
break;
sprintf(file_font, "font%d.nut", i);
_sf[i] = new SmushFont(false, true);
_sf[i]->loadFont(file_font);
}
} else {
error("SmushPlayer::setupAnim() Unknown font setup for game");
}
delete sub;
}
void SmushPlayer::parseNextFrame() {
Common::StackLock lock(_mutex);
if (_vm->_smushPaused)
return;
if (_base->eof()) {
_vm->_smushVideoShouldFinish = true;
return;
}
Chunk *sub = _base->subBlock();
switch (sub->getType()) {
case TYPE_FRME:
handleFrame(*sub);
break;
case TYPE_AHDR: // FT INSANE may seek file to the beginning
handleAnimHeader(*sub);
break;
default:
error("Unknown Chunk found at %x: %x, %d", _base->tell(), sub->getType(), sub->getSize());
}
delete sub;
if (_insanity)
_vm->_sound->processSoundQues();
_vm->_imuseDigital->flushTracks();
}
void SmushPlayer::setPalette(const byte *palette) {
byte palette_colors[1024];
byte *p = palette_colors;
for (int i = 0; i != 256; ++i) {
*p++ = _pal[i * 3 + 0] = *palette++; // red
*p++ = _pal[i * 3 + 1] = *palette++; // green
*p++ = _pal[i * 3 + 2] = *palette++; // blue
*p++ = 0;
}
_vm->_system->setPalette(palette_colors, 0, 256);
}
void SmushPlayer::setPaletteValue(int n, byte r, byte g, byte b) {
_pal[n * 3 + 0] = r;
_pal[n * 3 + 1] = g;
_pal[n * 3 + 2] = b;
_vm->_system->setPalette(_pal, n, 1);
}
void SmushPlayer::updateScreen() {
#ifdef DUMP_SMUSH_FRAMES
char fileName[100];
// change path below for dump png files
sprintf(fileName, "/path/to/somethere/%s%04d.png", _vm->getGameName(), _frame);
FILE *file = fopen(fileName, "wb");
if (file == NULL)
error("can't open file for writing png");
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
if (png_ptr == NULL) {
fclose(file);
error("can't write png header");
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fclose(file);
error("can't create png info struct");
}
if (setjmp(png_ptr->jmpbuf)) {
fclose(file);
error("png jmpbuf error");
}
png_init_io(png_ptr, file);
png_set_IHDR(png_ptr, info_ptr, _width, _height, 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_colorp palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof (png_color));
for (int i = 0; i != 256; ++i) {
(palette + i)->red = _pal[i * 3 + 0];
(palette + i)->green = _pal[i * 3 + 1];
(palette + i)->blue = _pal[i * 3 + 2];
}
png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
png_write_info(png_ptr, info_ptr);
png_set_flush(png_ptr, 10);
png_bytep row_pointers[480];
for (int y = 0 ; y < _height ; y++)
row_pointers[y] = (png_byte *) (_dst + y * _width);
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, info_ptr);
png_free(png_ptr, palette);
fclose(file);
png_destroy_write_struct(&png_ptr, &info_ptr);
#endif
uint32 end_time, start_time = _vm->_system->getMillis();
_vm->_system->copyRectToScreen(_dst, _width, 0, 0, _width, _height);
_updateNeeded = true;
end_time = _vm->_system->getMillis();
debugC(DEBUG_SMUSH, "Smush stats: updateScreen( %03d )", end_time - start_time);
}
void SmushPlayer::insanity(bool flag) {
_insanity = flag;
}
void SmushPlayer::seekSan(const char *file, int32 pos, int32 contFrame) {
Common::StackLock lock(_mutex);
if(_smixer)
_smixer->stop();
if (file) {
if (_base) {
_base->seek(0, FileChunk::seek_end);
delete _base;
}
_base = new FileChunk(file);
// In this case we need to get palette and number of frames
if (pos > 8) {
Chunk *sub = _base->subBlock();
checkBlock(*sub, TYPE_AHDR);
handleAnimHeader(*sub);
delete sub;
}
if (pos >= 8)
pos -= 8;
_skipPalette = false;
} else {
_base->reinit(pos);
_skipPalette = true;
}
if (pos != 8 && pos) {
_middleAudio = true;
}
_base->seek(pos, FileChunk::seek_start);
_frame = contFrame;
}
void SmushPlayer::tryCmpFile(const char *filename) {
_compressedFileMode = false;
const char *i = strrchr(filename, '.');
if (i == NULL) {
error("invalid filename : %s", filename);
}
char fname[260];
#ifdef USE_MAD
memcpy(fname, filename, i - filename);
strcpy(fname + (i - filename), ".mp3");
_compressedFile.open(fname);
if (_compressedFile.isOpen()) {
int size = _compressedFile.size();
_compressedFileMode = true;
_vm->_mixer->playInputStream(SoundMixer::kSFXAudioDataType, &_compressedFileSoundHandle, makeMP3Stream(&_compressedFile, size));
return;
}
#endif
#ifdef USE_VORBIS
memcpy(fname, filename, i - filename);
strcpy(fname + (i - filename), ".ogg");
_compressedFile.open(fname);
if (_compressedFile.isOpen()) {
int size = _compressedFile.size();
_compressedFileMode = true;
_vm->_mixer->playInputStream(SoundMixer::kSFXAudioDataType, &_compressedFileSoundHandle, makeVorbisStream(&_compressedFile, size));
return;
}
#endif
}
void SmushPlayer::play(const char *filename, int32 offset, int32 startFrame) {
// Verify the specified file exists
ScummFile f;
_vm->openFile(f, filename);
if (!f.isOpen()) {
warning("SmushPlayer::play() File not found %s", filename);
return;
}
f.close();
tryCmpFile(filename);
_updateNeeded = false;
// Hide mouse
bool oldMouseState = _vm->_system->showMouse(false);
// Load the video
setupAnim(filename);
init();
if (offset) {
_base->seek(offset - 8, FileChunk::seek_start);
_frame = startFrame;
_middleAudio = true;
}
for (;;) {
_vm->parseEvents();
_vm->processKbd(true);
if (_updateNeeded) {
uint32 end_time, start_time;
start_time = _vm->_system->getMillis();
_vm->_system->updateScreen();
_updateNeeded = false;
#ifdef _WIN32_WCE
_inTimer = false;
#endif
end_time = _vm->_system->getMillis();
debugC(DEBUG_SMUSH, "Smush stats: BackendUpdateScreen( %03d )", end_time - start_time);
}
if (_vm->_smushVideoShouldFinish || _vm->_quit || _vm->_saveLoadFlag)
break;
_vm->_system->delayMillis(10);
};
release();
// Reset mouse state
_vm->_system->showMouse(oldMouseState);
}
} // End of namespace Scumm
|