-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsquaresoft-square.enix_sz_dat.bms
858 lines (847 loc) · 22 KB
/
squaresoft-square.enix_sz_dat.bms
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
# SQUARESOFT->SQUARE ENIX - .dat(/.sz) movie format
# ---
# Driving Emotion Type-S (PS2) - .sz/.dat(OP_MOVIE), .dat
# Gekikukan Pro Yakyuu AT THE END OF THE CENTURY 1999 (PS2) - .sz/dat
# The Bouncer (PS2) - .sz/dat
# Final Fantasy X (PS2) - .sz/.dat
# PlayOnline Viewer (PS2) - .sz/.dat
# Kingdom Hearts (PS2) - .dat
# Final Fantasy XI (PS2) - .dat
get NAME basename
get EXT extension
math dpcm_ver = 0
if EXT == "dat"
get DAT_SIZE asize
open FDDE "sz" 1 EXISTS
if EXISTS == 0
math dpcm_ver = 2
get dat_version long
if dat_version == 0
get dat_02 byte
get dat_03 byte
get dat_04 short
get total_frames long
get height short
get width short
get dat_08 long
get dat_09 long
get dat_10 long
get dat_11 long
if dat_03 == 0xff
get dat_12 long
endif
savepos chunk_position
do
goto chunk_position
if dat_03 == 0xff
get chunk_size long
get frames long
math chunk_position + chunk_size
math chunk_position + 8
endif
if dat_03 == 0x01
get chunk_size_01 long
get chunk_size_02 long
get chunk_number long
get chunk_04 long
math chunk_position + chunk_size_02
math chunk_position + 16
endif
while chunk_position < DAT_SIZE
elif dat_version == 1
math chunk_position = 0
do
goto chunk_position
get chunk_type long
math chunk_position + 0x10
if chunk_type == 1 # initial video info
get is_spec long
if is_spec == 1
get height short
get width short
get chunks long
endif
elif chunk_type == 2 # frame index (for easier access to any part of the video)
get chunk_index_size long
goto chunk_position
for i = 0 <= chunks
get chunk_offset long
next i
math chunk_position + chunk_index_size
elif chunk_type == 3 # I-picture frame
get height short
get width short
get frame_chunk_size long
get mpeg_params long
#callfunction transmogrify_video_frame_from_singular_dat 1
math chunk_position + frame_chunk_size
elif chunk_type == 4 # sound frame
get audio_chunk_size long
get audio_codec long
callfunction extract_sound_frame_from_singular_dat 1
math chunk_position + audio_chunk_size
endif
while chunk_position < DAT_SIZE
endif
else
math dpcm_ver = 1
math dat_file = 0
math sz_file = 1
callfunction handle_sz 1
callfunction parse_sz 1
endif
elif EXT == "sz"
math dpcm_ver = 1
math dat_file = 1
open FDDE "dat" dat_file
math sz_file = 0
callfunction handle_sz 1
callfunction parse_sz 1
endif
startfunction handle_sz
get DAT_SIZE asize dat_file
# "paired" .sz/.dat movies
# exclusive to this script is a method to check
# if .sz already has an established intra/non-intra quantizer in its header or not.
# to do so, we have to do some counter-intuitive stuff just like this.
# --- quantizer array number one ---
putarray 0 0 0x20
putarray 0 1 0x20
putarray 0 2 0x20
putarray 0 3 0x20
putarray 0 4 0x20
putarray 0 5 0x20
putarray 0 6 0x20
putarray 0 7 0x20
putarray 0 8 0x20
putarray 0 9 0x20
putarray 0 10 0x20
putarray 0 11 0x20
putarray 0 12 0x20
putarray 0 13 0x20
putarray 0 14 0x20
putarray 0 15 0x20
putarray 0 16 0x20
putarray 0 17 0x20
putarray 0 18 0x20
putarray 0 19 0x20
putarray 0 20 0x20
putarray 0 21 0x20
putarray 0 22 0x20
putarray 0 23 0x20
putarray 0 24 0x20
putarray 0 25 0x20
putarray 0 26 0x20
putarray 0 27 0x20
putarray 0 28 0x20
putarray 0 29 0x20
putarray 0 30 0x20
putarray 0 31 0x20
putarray 0 32 0x20
putarray 0 33 0x20
putarray 0 34 0x20
putarray 0 35 0x20
putarray 0 36 0x20
putarray 0 37 0x20
putarray 0 38 0x20
putarray 0 39 0x20
putarray 0 40 0x20
putarray 0 41 0x20
putarray 0 42 0x20
putarray 0 43 0x20
putarray 0 44 0x20
putarray 0 45 0x20
putarray 0 46 0x20
putarray 0 47 0x20
putarray 0 48 0x20
putarray 0 49 0x20
putarray 0 50 0x20
putarray 0 51 0x20
putarray 0 52 0x20
putarray 0 53 0x20
putarray 0 54 0x20
putarray 0 55 0x20
putarray 0 56 0x20
putarray 0 57 0x20
putarray 0 58 0x20
putarray 0 59 0x20
putarray 0 60 0x20
putarray 0 61 0x20
putarray 0 62 0x20
putarray 0 63 0x20
putarray 0 64 0x10
putarray 0 65 0x10
putarray 0 66 0x10
putarray 0 67 0x10
putarray 0 68 0x10
putarray 0 69 0x10
putarray 0 70 0x10
putarray 0 71 0x10
putarray 0 72 0x10
putarray 0 73 0x10
putarray 0 74 0x10
putarray 0 75 0x10
putarray 0 76 0x10
putarray 0 77 0x10
putarray 0 78 0x10
putarray 0 79 0x10
putarray 0 80 0x10
putarray 0 81 0x10
putarray 0 82 0x10
putarray 0 83 0x10
putarray 0 84 0x10
putarray 0 85 0x10
putarray 0 86 0x10
putarray 0 87 0x10
putarray 0 88 0x10
putarray 0 89 0x10
putarray 0 90 0x10
putarray 0 91 0x10
putarray 0 92 0x10
putarray 0 93 0x10
putarray 0 94 0x10
putarray 0 95 0x10
putarray 0 96 0x10
putarray 0 97 0x10
putarray 0 98 0x10
putarray 0 99 0x10
putarray 0 100 0x10
putarray 0 101 0x10
putarray 0 102 0x10
putarray 0 103 0x10
putarray 0 104 0x10
putarray 0 105 0x10
putarray 0 106 0x10
putarray 0 107 0x10
putarray 0 108 0x10
putarray 0 109 0x10
putarray 0 110 0x10
putarray 0 111 0x10
putarray 0 112 0x10
putarray 0 113 0x10
putarray 0 114 0x10
putarray 0 115 0x10
putarray 0 116 0x10
putarray 0 117 0x10
putarray 0 118 0x10
putarray 0 119 0x10
putarray 0 120 0x10
putarray 0 121 0x10
putarray 0 122 0x10
putarray 0 123 0x10
putarray 0 124 0x10
putarray 0 125 0x10
putarray 0 126 0x10
putarray 0 127 0x10
# --- quantizer array number two ---
putarray 1 0 0x08
putarray 1 1 0x10
putarray 1 2 0x13
putarray 1 3 0x16
putarray 1 4 0x1a
putarray 1 5 0x1b
putarray 1 6 0x1d
putarray 1 7 0x22
putarray 1 8 0x10
putarray 1 9 0x10
putarray 1 10 0x16
putarray 1 11 0x18
putarray 1 12 0x1b
putarray 1 13 0x1d
putarray 1 14 0x22
putarray 1 15 0x25
putarray 1 16 0x13
putarray 1 17 0x16
putarray 1 18 0x1a
putarray 1 19 0x1b
putarray 1 20 0x1d
putarray 1 21 0x22
putarray 1 22 0x22
putarray 1 23 0x26
putarray 1 24 0x16
putarray 1 25 0x16
putarray 1 26 0x1a
putarray 1 27 0x1b
putarray 1 28 0x1d
putarray 1 29 0x22
putarray 1 30 0x25
putarray 1 31 0x28
putarray 1 32 0x16
putarray 1 33 0x1a
putarray 1 34 0x1b
putarray 1 35 0x1d
putarray 1 36 0x20
putarray 1 37 0x23
putarray 1 38 0x28
putarray 1 39 0x30
putarray 1 40 0x1a
putarray 1 41 0x1b
putarray 1 42 0x1d
putarray 1 43 0x20
putarray 1 44 0x23
putarray 1 45 0x28
putarray 1 46 0x30
putarray 1 47 0x3a
putarray 1 48 0x1a
putarray 1 49 0x1b
putarray 1 50 0x1d
putarray 1 51 0x22
putarray 1 52 0x26
putarray 1 53 0x2e
putarray 1 54 0x38
putarray 1 55 0x45
putarray 1 56 0x1b
putarray 1 57 0x1d
putarray 1 58 0x23
putarray 1 59 0x26
putarray 1 60 0x2e
putarray 1 61 0x38
putarray 1 62 0x45
putarray 1 63 0x53
putarray 1 64 0x10
putarray 1 65 0x11
putarray 1 66 0x12
putarray 1 67 0x13
putarray 1 68 0x14
putarray 1 69 0x15
putarray 1 70 0x16
putarray 1 71 0x17
putarray 1 72 0x11
putarray 1 73 0x12
putarray 1 74 0x13
putarray 1 75 0x14
putarray 1 76 0x15
putarray 1 77 0x16
putarray 1 78 0x17
putarray 1 79 0x18
putarray 1 80 0x12
putarray 1 81 0x13
putarray 1 82 0x14
putarray 1 83 0x15
putarray 1 84 0x16
putarray 1 85 0x17
putarray 1 86 0x18
putarray 1 87 0x19
putarray 1 88 0x13
putarray 1 89 0x14
putarray 1 90 0x15
putarray 1 91 0x16
putarray 1 92 0x17
putarray 1 93 0x18
putarray 1 94 0x1a
putarray 1 95 0x1b
putarray 1 96 0x14
putarray 1 97 0x15
putarray 1 98 0x16
putarray 1 99 0x17
putarray 1 100 0x19
putarray 1 101 0x1a
putarray 1 102 0x1b
putarray 1 103 0x1c
putarray 1 104 0x15
putarray 1 105 0x16
putarray 1 106 0x17
putarray 1 107 0x18
putarray 1 108 0x1a
putarray 1 109 0x1b
putarray 1 110 0x1c
putarray 1 111 0x1e
putarray 1 112 0x16
putarray 1 113 0x17
putarray 1 114 0x18
putarray 1 115 0x1a
putarray 1 116 0x1b
putarray 1 117 0x1c
putarray 1 118 0x1e
putarray 1 119 0x1f
putarray 1 120 0x17
putarray 1 121 0x18
putarray 1 122 0x19
putarray 1 123 0x1b
putarray 1 124 0x1c
putarray 1 125 0x1e
putarray 1 126 0x1f
putarray 1 127 0x21
math no_quant = 0
math is_match = 0
goto 0 sz_file
for try = 0 < 2
if try = 0
math byte_hash = 0
math check_tre = 0
for bc = 0 < 0x80
get check byte sz_file
math check_dup = check
math byte_hash + check_dup
math check_dup << 3
math check_dup * 45
math check_tre + check_dup
math check_tre << 5
math check_tre / 5
math byte_hash + check_tre
next bc
endif
math quant_hash = 0
math match_tre = 0
for bd = 0 < 0x80
getarray match try bd
math match_dup = match
math quant_hash + match_dup
math match_dup << 3
math match_dup * 45
math match_tre + match_dup
math match_tre << 5
math match_tre / 5
math quant_hash + match_tre
next bd
math byte_hash = byte_hash
math quant_hash = quant_hash
if byte_hash = quant_hash
math is_match = 1
math no_quant = 0
break
else
math no_quant = 1
math is_match = 0
goto 0 sz_file
endif
next try
# intra/non-intra quantizer checking ends here.
if no_quant = 0
math is_match = 1
math intra_quantizer_offset = 0
math non_intra_quantizer_offset = 0x40
for quantizer = 0 < 0x40
goto intra_quantizer_offset sz_file
get intra_byte byte sz_file
math intra_quantizer_offset + 1
putarray 101 quantizer intra_byte
goto non_intra_quantizer_offset sz_file
get non_intra_byte byte sz_file
math non_intra_quantizer_offset + 1
putarray 102 quantizer non_intra_byte
next quantizer
endif
endif
endfunction
startfunction parse_sz
math chunk_offset = 0
for i = 0
print " offset %chunk_offset%"
get chunk_size long sz_file
if chunk_size == 0
break
else
log MEMORY_FILE chunk_offset chunk_size dat_file
math sub_chunk_offset = 0
do
math sound_type = 0
math sound_sub_type = 0
math is_video = 0
goto sub_chunk_offset MEMORY_FILE
get track_params_01 long MEMORY_FILE
get track_params_02 long MEMORY_FILE
get track_params_03 long MEMORY_FILE
get track_params_04 long MEMORY_FILE
math sub_chunk_offset + 0x10
math backup_chunk_offset = sub_chunk_offset
if sub_chunk_offset == 0x10
math video_track_size = track_params_01
math marcoblocks = track_params_02
math mpeg2_frame_info = track_params_03
math mpeg2_params = track_04
math sub_chunk_offset + video_track_size
endif
if track_params_01 == 0x30
if track_params_02 == 0xffffffff
if track_params_03 == 0xffffffff
if track_params_04 == 0xffffffff
math camera_coord_chunk_size = track_params_01
math sub_chunk_offset + camera_coord_chunk_size
endif
endif
endif
endif
if track_params_01 == 0x40
math sound_type = 2
math sound_sub_type = 1
elif track_params_01 >= 0x50 && track_params_01 <= 0x51
math sound_type = 2
math sound_sub_type = 2
endif
if track_params_01 == track_params_02
if track_params_02 == track_params_03
if track_params_03 == track_params_04
if track_params_04 == track_params_03
if track_params_03 == track_params_02
if track_params_02 == track_params_01
if track_params_01 != 0
math sound_type = 1
endif
endif
endif
endif
endif
endif
endif
if sound_type == 1
math audio_frame_size = track_params_01
math sub_chunk_offset + audio_frame_size
elif sound_type == 2
math audio_frame_size = track_params_03
math sub_chunk_offset + audio_frame_size
endif
callfunction handle_sound_type 1
while sub_chunk_offset < chunk_size
math chunk_offset + chunk_size
log MEMORY_FILE 0 0
endif
next i
endfunction
startfunction handle_sound_type
math is_ima = 0
math is_vag = 0
math is_pcm16 = 0
math is_ac3 = 0
math dual_codec = 0
math audio_frame_offset = backup_chunk_offset
if sound_type = 1
if audio_frame_size == 0x1c30
math is_vag = 1
elif audio_frame_size == 0x3c00
math is_pcm16 = 1
elif audio_frame_size == 0x5110
math is_pcm16 = 1
math is_ac3 = 1
math dual_codec = 1
elif audio_frame_size == 0x6400
math is_pcm16 = 1
elif audio_frame_size == 0x7800
math is_pcm16 = 1
elif audio_frame_size == 0x8000
math is_pcm16 = 1
math is_ac3 = 1
math dual_codec = 1
elif audio_frame_size == 0x8400
math is_pcm16 = 1
math is_ac3 = 1
math dual_codec = 1
elif audio_frame_size == 0x8700
math is_pcm16 = 1
math is_ac3 = 1
math dual_codec = 1
elif audio_frame_size == 0xa220
math is_pcm16 = 1
math is_ac3 = 1
math dual_codec = 1
elif audio_frame_size == 0xc800
math is_pcm16 = 1
elif audio_frame_size == 0x10800
math is_pcm16 = 1
math is_ac3 = 1
math dual_codec = 1
else
if audio_frame_size != 0
print " unknown sound frame configuration "
cleanexit
endif
endif
print " non-compressed sound data "
if dual_codec = 1
callfunction handle_dual_codec 1
else
callfunction set_file_names_for_audio 1
append
log chunk_name audio_frame_offset audio_frame_size MEMORY_FILE
append
endif
log MEMORY_FILE2 0 0
elif sound_type = 2
log MEMORY_FILE2 audio_frame_offset audio_frame_size MEMORY_FILE
math dpcm_frame_size = track_params_02
math ac3_frame_size = track_params_04
math dpcm_frame_offset = 0
xmath ac3_frame_offset "audio_frame_size - ac3_frame_size"
math dpcm_initial_size = ac3_frame_offset
string pcm_name p "%s.pcm" NAME
string ac3_name p "%s.ac3" NAME
log MEMORY_FILE3 0 dpcm_initial_size MEMORY_FILE2
if sound_sub_type = 1
math dpcm_read_element = -3
math dpcm_write_element = -4
callfunction process_dpcm 1
append
log pcm_name 0 pcm16_frame_size MEMORY_FILE4
append
elif sound_sub_type = 2
math comp_read_element = -3
math comp_write_element = -4
math compressed_frame_size = dpcm_initial_size
callfunction process_compressed_audio_chunk 1
log MEMORY_FILE3 0 dpcm_frame_size MEMORY_FILE4
log MEMORY_FILE4 0 0
math dpcm_read_element = -3
math dpcm_write_element = -4
callfunction process_dpcm 1
append
log pcm_name 0 pcm16_frame_size MEMORY_FILE4
append
endif
if ac3_frame_size != 0
append
log ac3_name ac3_frame_offset ac3_frame_size MEMORY_FILE2
append
endif
log MEMORY_FILE2 0 0
log MEMORY_FILE3 0 0
log MEMORY_FILE4 0 0
endif
endfunction
startfunction handle_dual_codec
if audio_frame_size == 0x5110
math pcm16_frame_size = 0x3c00
math ac3_frame_size = 0x1510
elif audio_frame_size == 0x8000
math pcm16_frame_size = 0x6400
math ac3_frame_size = 0x1c00
elif audio_frame_size == 0x8400
math pcm16_frame_size = 0x6400
math ac3_frame_size = 0x2000
elif audio_frame_size == 0x8700
math pcm16_frame_size = 0x6400
math ac3_frame_size = 0x2300
elif audio_frame_size == 0xa220
math pcm16_frame_size = 0x7800
math ac3_frame_size = 0x2a20
elif audio_frame_size == 0x10800
math pcm16_frame_size = 0xc800
math ac3_frame_size = 0x4000
endif
for cdc = 0 < 2
if cdc = 0
math is_pcm16 = 1
math is_ac3 = 0
elif cdc = 1
math is_pcm16 = 0
math is_ac3 = 1
endif
callfunction set_file_names_for_audio 1
if cdc = 0
append
log chunk_name audio_frame_offset pcm16_frame_size MEMORY_FILE
append
math audio_frame_offset + pcm16_frame_size
elif cdc = 1
log MEMORY_FILE2 audio_frame_offset ac3_frame_size MEMORY_FILE
math audio_frame_offset + ac3_frame_size
callfunction find_blank_areas_within_ac3_frame 1
append
log chunk_name 0 ac3_frame_size MEMORY_FILE2
append
endif
next cdc
endfunction
startfunction set_file_names_for_audio
string chunk_pre p "%s." NAME
if is_ima = 1
string chunk_name p "%sima" chunk_pre
endif
if is_vag = 1
string chunk_name p "%svag" chunk_pre
endif
if is_pcm16 = 1
string chunk_name p "%spcm" chunk_pre
endif
if is_ac3 = 1
string chunk_name p "%sac3" chunk_pre
endif
endfunction
startfunction find_blank_areas_within_ac3_frame
xmath ac3_last_offset "ac3_frame_size - 8"
for
goto ac3_last_offset MEMORY_FILE2
get zero_byte_01 long MEMORY_FILE2
get zero_byte_02 long MEMORY_FILE2
if zero_byte_02 = 0
math ac3_frame_size - 4
else
break
endif
if zero_byte_01 = 0
math ac3_frame_size - 4
else
break
endif
math ac3_last_offset - 8
next
endfunction
startfunction process_compressed_audio_chunk
goto 0 comp_read_element
get compression_level byte comp_read_element
get original_size long comp_read_element
log MEMORY_FILE4 0 0
math bytes_written = 0
math current_position = 5
do
get control_byte byte comp_read_element
if control_byte <= 0x7d
for j = 0 < control_byte
goto bytes_written comp_write_element
get bytes_to_write byte comp_read_element
put bytes_to_write byte comp_write_element
math bytes_written + 1
next j
elif control_byte == 0x7e
get bytes_to_write byte comp_read_element
math bytes_to_write + 4
get source_byte_to_write byte comp_read_element
for j = 0 < bytes_to_write
goto bytes_written comp_write_element
put source_byte_to_write byte comp_write_element
math bytes_written + 1
next j
elif control_byte == 0x7f
get bytes_to_write short comp_read_element
get source_byte_to_write byte comp_read_element
for j = 0 < bytes_to_write
goto bytes_written comp_write_element
put source_byte_to_write byte comp_write_element
math bytes_written + 1
next j
elif control_byte >= 0x80
xmath additional_control_byte_01 "((control_byte & 0x7f) >> 3) + 3"
if compression_level == 2
if additional_control_byte_01 >= 5
xmath additional_control_byte_03 "((additional_control_byte_01 - 5) >> 1) + 1"
math additional_control_byte_01 - additional_control_byte_03
endif
endif
if compression_level == 1
xmath additional_control_byte_02 "control_byte & 0x07"
elif compression_level == 2
xmath additional_control_byte_02 "control_byte & 0x0f"
endif
if additional_control_byte_02 >= 0x08
math additional_control_byte_01 - 1
endif
get previous_bytes byte comp_read_element
if additional_control_byte_02 == 0
xmath byte_recovery_position "(bytes_written - previous_bytes) - 1"
else
xmath byte_recovery_position "(bytes_written - previous_bytes) - ((additional_control_byte_02 << 8) + 1)"
endif
for j = 0 < additional_control_byte_01
if byte_recovery_position >= 0
goto byte_recovery_position comp_write_element
get recurring_bytes byte comp_write_element
else
math recurring_bytes = 0
endif
goto bytes_written comp_write_element
put recurring_bytes byte comp_write_element
math bytes_written + 1
math byte_recovery_position + 1
next j
endif
savepos current_position comp_read_element
while current_position < compressed_frame_size
get decompressed_frame_size asize comp_write_element
log MEMORY_FILE3 0 0
endfunction
startfunction process_dpcm
if dpcm_write_element = -2
log MEMORY_FILE2 0 0
elif dpcm_write_element = -4
log MEMORY_FILE4 0 0
endif
if dpcm_ver = 1
math predicted_sample = 0
math current_position = 0
endif
math dpcm_frame_fill = 0
do
goto current_position dpcm_read_element
get target byte dpcm_read_element
xmath output_sample "target << 0x19"
xmath sample_is_small "target & 0x80"
if sample_is_small == 0
math current_position + 2
get target byte dpcm_read_element
math dpcm_frame_fill + 2
math output_sample >> 0x11
math output_sample | target
else
math output_sample >> 0x19
math current_position + 1
math dpcm_frame_fill + 1
endif
math output_sample + predicted_sample
math output_sample << 0x10
math output_sample >> 0x10
math predicted_sample = output_sample
put output_sample short dpcm_write_element
while dpcm_frame_fill < dpcm_frame_size
get pcm16_frame_size asize dpcm_write_element
endfunction
startfunction extract_sound_frame_from_singular_dat
if audio_codec == 0xffffff00
string chunk_name p "%s.pcm" NAME
elif audio_codec == 0xffffff01
string chunk_name p "%s.ac3" NAME
elif audio_codec == 0xffffff02
string chunk_name p "%s.pcm" NAME
elif audio_codec == 0xffffff03
string chunk_name p "%s.vsf" NAME
endif
if dat_version == 1
if audio_codec == 0xffffff02
log MEMORY_FILE chunk_position audio_chunk_size
log MEMORY_FILE3 0 0
math dpcm_read_element = -1
math dpcm_write_element = -2
math dpcm_chunk_offset = 0
math remaining_dpcm_size = audio_chunk_size
do
goto dpcm_chunk_offset dpcm_read_element
get dpcm_flag byte dpcm_read_element
math dpcm_chunk_offset + 1
math remaining_dpcm_size - 1
if dpcm_flag == 0
append
log MEMORY_FILE3 dpcm_chunk_offset 512 dpcm_read_element
append
math dpcm_chunk_offset + 512
math remaining_dpcm_size - 512
elif dpcm_flag == 1
get dpcm_frame_size short dpcm_read_element
get predicted_sample short dpcm_read_element
xmath current_position "dpcm_chunk_offset + 4"
callfunction process_dpcm 1
append
log MEMORY_FILE3 0 pcm16_frame_size dpcm_write_element
append
log MEMORY_FILE2 0 0
math dpcm_chunk_offset + 4
math remaining_dpcm_size - 4
math dpcm_chunk_offset + dpcm_frame_size
math remaining_dpcm_size - dpcm_frame_size
else
break
endif
if remaining_dpcm_size < 16
break
endif
while dpcm_chunk_offset < audio_chunk_size
log MEMORY_FILE 0 0
savepos m3_size MEMORY_FILE3
append
log chunk_name 0 m3_size MEMORY_FILE3
append
log MEMORY_FILE3 0 0
else
append
log chunk_name chunk_position audio_chunk_size
append
endif
endif
endfunction