-
Notifications
You must be signed in to change notification settings - Fork 2
/
scdcinepakencode
executable file
·454 lines (400 loc) · 14.4 KB
/
scdcinepakencode
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
#!/usr/bin/perl
###############################################################################
# Copyright (c) 2011 by bgvanbur
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
###############################################################################
# makes Ecco 2's MV001.CIN 97% of the original size
# makes Star Wars Chess's X6YO_ST.SEG 45% of the original size
# TODO (44% once frame skip works)
###############################################################################
use strict;
use warnings;
if ( $#ARGV < 0 ) {
&Help();
}
my $cinepakWidth = 32*8;
my $cinepakHeight = 14*8;
my $cinepakIdentifier = '';
my $cinepakBaseRate = 600;
my $isEcco32x = 0;
my $cinepakFrameDuration = 75;
# all known commerical cinepak videos have at least 2 palettes
# partially implemented but causes tile data corruption with original decoder
my $cinepakForceAtLeastTwoPalettes = 1;
# TODO still need calculate frame time before skipped frame before should be enabled
my $optimizeSkipUnchangedFramesIfPossible = 0;
# the PCM FD value set in the SUBPLAY.BIN (default is 0x0400)
my $pcmFD = 0x0400;
my $pcmFile = '';
# not the actual play rate,
# but the rate the SUBPLAY.BIN code thinks it is playing at (V1.2 thinks 16000)
my $pcmDataRate = 16000;
my $pcmFramePrefix = 'pcmchunk';
my $customEncodingFile = '';
# TODO can remove single palette for -paldatas=<palette>
my $singlePalette = '';
my $paldatasString = 'paldata*.bin';
my $palmapsString = 'palmap*.bin';
my $cinepaktiledatasString = 'cinepaktiledata*.bin';
# parse args
foreach my $arg (@ARGV) {
if ( $arg =~ /^-width=(\d+)$/i ) {
$cinepakWidth = $1;
} elsif ( $arg =~ /^-height=(\d+)$/i ) {
$cinepakHeight = $1;
} elsif ( $arg =~ /^-size=(\d+)x(\d+)$/i ) {
$cinepakWidth = $1;
$cinepakHeight = $2;
} elsif ( $arg =~ /^-id=(.{4})$/i ) {
$cinepakIdentifier = $1;
} elsif ( $arg =~ /^-baserate=(\d+)$/i ) {
$cinepakBaseRate = $1;
} elsif ( $arg =~ /^-frameduration=(.+)$/i ) {
$cinepakFrameDuration = $1;
} elsif ( $arg =~ /^-fps=(.+)$/i ) {
my $fps = $1;
$cinepakFrameDuration = $cinepakBaseRate/$fps*(12500000/384.0*($pcmFD/2048.0))/$pcmDataRate;
} elsif ( $arg =~ /^-custom=(.+)$/i ) {
$customEncodingFile = $1;
} elsif ( $arg =~ /^-pcmfile=(.+)$/i ) {
$pcmFile = $1;
} elsif ( $arg =~ /^-pcmfd=(\d+)$/i ) {
$pcmFD = $1;
} elsif ( $arg =~ /^-pcmrate=(.+)$/i ) {
$pcmDataRate = $1;
} elsif ( $arg =~ /^-singlepalette=(.+)$/i ) {
$singlePalette = $1;
} elsif ( $arg =~ /^-paldatas=(.+)$/i ) {
$paldatasString = $1;
} elsif ( $arg =~ /^-palmaps=(.+)$/i ) {
$palmapsString = $1;
} elsif ( $arg =~ /^-cinepaktiledatas=(.+)$/i ) {
$cinepaktiledatasString = $1;
} elsif ( $arg =~ /^-ecco32x$/i ) {
$isEcco32x = 1;
} else {
print STDERR "\nCould not parse argument: $arg\n";
&Help();
}
}
###############################################################################
if ( $cinepakIdentifier eq '' ) {
if ( $isEcco32x ) {
$cinepakIdentifier = 'cvid';
} else {
$cinepakIdentifier = 'SEGA';
}
}
my $cinepakSampleTable = '';
my $cinepakSampleData = '';
my $pcmFrameSize = 0x4000;
my $pcmFrameIndex = 0;
my @pcmFrameFiles;
# used to potentially skip frames, need to make sure palettes don't change
# if we want to skip a frame
my $imgPaletteLast = '';
my $imgPaletteMapLast = '';
my $pcmFileSize = 0;
if ( $pcmFile ne '' ) {
$pcmFileSize = -s $pcmFile;
}
# empty pcm file means no audio
if ( $pcmFileSize > 0 ) {
open( PCM, '<'.$pcmFile ) or die "Cannot read $pcmFile\n";
for ( my $pcmFileIndex = 0; $pcmFileIndex < $pcmFileSize; $pcmFileIndex += $pcmFrameSize ) {
my $pcmFrameSizeCurrent = $pcmFileSize - $pcmFileIndex;
if ( $pcmFrameSizeCurrent > $pcmFrameSize ) {
$pcmFrameSizeCurrent = $pcmFrameSize;
}
my $pcmFrameData = '';
if ( read(PCM, $pcmFrameData, $pcmFrameSizeCurrent ) != $pcmFrameSizeCurrent ) {
die "Cannot read from pcm file as expected\n";
}
my $pcmFrameFile = sprintf("%s%5.5d.pcm",$pcmFramePrefix,$pcmFrameIndex);
$pcmFrameIndex += 1;
push @pcmFrameFiles, $pcmFrameFile;
open( PCMFRAME, '>'.$pcmFrameFile ) or die "Cannot write $pcmFrameFile\n";
print PCMFRAME $pcmFrameData;
close PCMFRAME;
}
close PCM;
}
my $pcmFrameCount = $pcmFrameIndex;
$pcmFrameIndex = 0;
my $imgFrameIndex = 0;
my $pcmTimeFinal = $cinepakBaseRate * $pcmFileSize / $pcmDataRate * $pcmFD / 0x0400;
my $pcmTimeFrame = $cinepakBaseRate * $pcmFrameSize / $pcmDataRate * $pcmFD / 0x0400;
my $pcmTimeCurrent = -0.4 * $pcmTimeFrame;
my $imgTimeCurrent = 0;
###############################################################################
if ( $customEncodingFile ne '' ) {
my $customEval = '';
if ( open(CUSTOM,$customEncodingFile) ) {
binmode CUSTOM;
my $customEncodingFileSize = -s $customEncodingFile;
read(CUSTOM,$customEval,$customEncodingFileSize);
close CUSTOM;
}
eval($customEval);
} else {
my @paldatas = sort <${paldatasString}>;
my @palmaps = sort <${palmapsString}>;
my @cinepaktiledatas = sort <${cinepaktiledatasString}>;
my $paldatasCount = $#paldatas+1;
my $palmapsCount = $#palmaps+1;
for ( my $i = 0; $i <= $#cinepaktiledatas; $i++ ) {
if ( $singlePalette ) {
&AddImgFrame($cinepakFrameDuration,$singlePalette,'',$cinepaktiledatas[$i]);
} else {
my $paldata = $i < $paldatasCount ? $paldatas[$i] : '';
my $palmap = $i < $palmapsCount ? $palmaps[$i] : '';
&AddImgFrame($cinepakFrameDuration,$paldata,$palmap,$cinepaktiledatas[$i]);
}
}
}
###############################################################################
print
"FILMStart:\n".
"\tdc.b\t'FILM'\n".
"\tdc.l\t(FILMEnd-FILMStart)\n".
"\tdc.l\t0\n".
"\tdc.l\t0\n".
"FDSCStart\n".
"\tdc.b\t'FDSC'\n".
"\tdc.l\t(FDSCEnd-FDSCStart)\n".
"\tdc.b\t'$cinepakIdentifier'\n".
"\tdc.l\t$cinepakHeight\n".
"\tdc.l\t$cinepakWidth\n".
"FDSCEnd:\n".
"STABStart:\n".
"\tdc.b\t'STAB'\n".
"\tdc.l\t(STABEnd-STABStart)\n".
"\tdc.l\t$cinepakBaseRate\n".
"\tdc.l\t(STABSampleTableEnd-STABSampleTableStart)/16\n".
"STABSampleTableStart:\n".
$cinepakSampleTable.
# TODO match with Ecco 2 with unused extra bytes
#"\tdc.l\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n".
#"\tdc.l\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n".
"STABSampleTableEnd:\n".
# TODO match with Ecco 32x with unused extra bytes
"\tdc.l\t0,0,0,0\n".
"STABEnd:\n".
"FILMEnd:\n".
"SampleDataStart:\n".
$cinepakSampleData.
"SampleDataEnd:\n";
###############################################################################
sub AddImgFrame {
my ($imgDuration,$imgPalette,$imgPaletteMap,$imgMap) = @_;
# TODO option for debug info for custom?
if ( 0 ) {
print STDERR "AddImgFrame($imgDuration,$imgPalette,$imgPaletteMap,$imgMap)\n";
}
$imgDuration *= $pcmFD / 0x0400;
while ( $pcmFrameIndex < $pcmFrameCount &&
$imgTimeCurrent > $pcmTimeCurrent ) {
# add pcm frame
my $pcmDataStart = "PCMData${pcmFrameIndex}Start";
my $pcmDataEnd = "PCMData${pcmFrameIndex}End";
my $pcmFrameFile = $pcmFrameFiles[$pcmFrameIndex];
$cinepakSampleTable .=
"PCMEntry${pcmFrameIndex}:\n".
"\tdc.l\t($pcmDataStart-SampleDataStart)\n".
"\tdc.l\t($pcmDataEnd-$pcmDataStart)\n".
"\tdc.l\t0xFFFFFFFF\n".
"\tdc.l\t1\n";
$cinepakSampleData .=
"$pcmDataStart:\n".
"\tincbin\t$pcmFrameFile\n".
"\teven\n".
"$pcmDataEnd:\n";
$pcmFrameIndex += 1;
$pcmTimeCurrent += $pcmTimeFrame;
}
my $imgDataStart = "ImgData${imgFrameIndex}Start";
my $imgDataEnd = "ImgData${imgFrameIndex}End";
my $imgTimeCurrentInt = int($imgTimeCurrent);
my $imgDurationInt = int($imgTimeCurrent + $imgDuration) - $imgTimeCurrentInt;
if ( $isEcco32x ) {
$cinepakSampleTable .=
"ImgEntry${imgFrameIndex}:\n".
"\tdc.l\t($imgDataStart-SampleDataStart)\n".
"\tdc.l\t($imgDataEnd-$imgDataStart)\n".
"\tdc.l\t$imgTimeCurrentInt\n".
# TODO does not correctly indicate value for skipped frames
"\tdc.l\t$imgDurationInt\n";
$cinepakSampleData .=
"$imgDataStart:\n".
"\tincbin\t$imgMap\n".
"$imgDataEnd:\n";
} else {
# only skip frame if not custom and done with audio frames or don't need
# image frame right before next audio frame
my $addFrame = 1;
# in case of single palette or missing palettes, keep using last palette
if ( $imgPalette eq '' ) {
$imgPalette = $imgPaletteLast;
}
if ( $imgPalette eq '' ) {
die "Cannot handle unspecified palette\n";
}
if ( $optimizeSkipUnchangedFramesIfPossible &&
$customEncodingFile eq '' &&
$imgPaletteLast ne '' &&
( $pcmFrameIndex >= $pcmFrameCount ||
$imgTimeCurrent + $imgDuration + 1 < $pcmTimeCurrent ) ) {
# ensure palettes are the same
if ( &CompareFiles($imgPalette,$imgPaletteLast) ) {
# ensure palette maps are the same
if ( ( $imgPaletteMap eq '' &&
$imgPaletteMapLast eq '' ) ||
( $imgPaletteMap ne '' &&
$imgPaletteMapLast ne '' &&
&CompareFiles($imgPaletteMap,$imgPaletteMapLast) ) ) {
# ensure image map is all zeros
# (zero codebook sizes, method map all zeros)
if ( open( MAP, $imgMap ) ) {
my $imgMapSize = -s $imgMap;
my $imgMapData = '';
if ( read(MAP,$imgMapData,$imgMapSize) == $imgMapSize ) {
if ( $imgMapData !~ /[\x01-\xFF]/ ) {
$addFrame = 0;
}
}
}
close MAP;
}
}
}
if ( $addFrame ) {
my $imgPaletteSize = -s $imgPalette;
my $imgPaletteCount = $imgPaletteSize >> 5;
my $imgPaletteMapSize = $imgPaletteCount > 1 ? -s $imgPaletteMap : 0;
my $imgMapSize = 32 * $cinepakWidth/8 * $cinepakHeight/8;
my $cinepakForceAtLeastTwoPalettesThisImage = 0;
if ( $cinepakForceAtLeastTwoPalettes &&
$imgPaletteCount == 1 ) {
$cinepakForceAtLeastTwoPalettesThisImage = 1;
$imgPaletteCount = 2;
$imgPaletteSize = 2*32;
$imgPaletteMapSize = 4 * int( ( $cinepakWidth/8 * $cinepakHeight/8 + 15 ) / 16 );
}
$cinepakSampleTable .=
"ImgEntry${imgFrameIndex}:\n".
"\tdc.l\t($imgDataStart-SampleDataStart)\n".
"\tdc.l\t($imgDataEnd-$imgDataStart)\n".
"\tdc.l\t$imgTimeCurrentInt\n".
# TODO does not correctly indicate value for skipped frames
"\tdc.l\t$imgDurationInt\n";
$cinepakSampleData .=
"$imgDataStart:\n".
"\tdc.b\t'SM',0x00,0x2".($imgPaletteCount-1)."\n".
"\tdc.l\t".(12+$imgPaletteSize+$imgPaletteMapSize+$imgMapSize)."\n".
"\tdc.w\t".($cinepakWidth/8)."\n".
"\tdc.w\t".($cinepakHeight/8)."\n".
"\tincbin\t$imgPalette\n";
if ( $cinepakForceAtLeastTwoPalettesThisImage ) {
# zeroed extra palette
$cinepakSampleData .=
"\tdc.w\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n";
# zeroed palette map
$cinepakSampleData .=
"\tdc.l\t0".(",0"x($imgPaletteMapSize/4-1))."\n";
} elsif ( $imgPaletteCount > 1 ) {
$cinepakSampleData .=
"\tincbin\t$imgPaletteMap\n";
}
$cinepakSampleData .=
"\tincbin\t$imgMap\n".
"$imgDataEnd:\n";
} else {
$cinepakSampleTable .=
"\t;; image frame skipped\n";
$cinepakSampleData .=
"\t;; image frame skipped\n";
}
}
$imgPaletteLast = $imgPalette;
$imgPaletteMapLast = $imgPaletteMap;
$imgFrameIndex += 1;
$imgTimeCurrent += $imgDuration;
}
sub CompareFiles {
my ($file0,$file1) = @_;
my $result = 0;
if ( $file0 eq $file1 ) {
$result = 1;
} else {
my $file0Size = -s $file0;
my $file1Size = -s $file1;
if ( $file0Size == $file1Size ) {
if ( open(FILE0,$file0) ) {
if ( open(FILE1,$file1) ) {
my $file0Data = '';
my $file1Data = '';
if ( read(FILE0,$file0Data,$file0Size) == $file0Size &&
read(FILE1,$file1Data,$file1Size) == $file1Size ) {
if ( $file0Data eq $file1Data ) {
$result = 1;
}
}
}
close FILE1;
}
close FILE0;
}
}
return $result;
}
sub Help {
die '
scdcinepakencode [options] > <ASMFILE>
[description]
Outputs an assembly of the desired Cinepak file to the standard output.
[default usage]
When -custom is not specified, it uses paldata*.bin, palmap*.bin, and
cinepaktiledata*.bin for image frame files (these are default names
used by scdmoviedecode and scdcinepakencodeframe).
[custom usage]
When -custom is specifies a PERL script, this script should call
&AddImgFrame(...) for each image frame. The PCM data is automatically
interleaved as needed. &AddImgFrame takes four arguments: image duration,
image palette filename, image palette map filename (or empty string for no
map), and cinepak image map filename. The Nyan Cat demo is a simple example
using the custom usage.
[options]
-width=<#> sets the pixel width of the images
-height=<#> sets the pixel height of the images
-size=<#>x<#> sets the pixel width by height of the images
-id=<id> set the four character id (defaults to SEGA)
-baserate=<#> sets the base rate (ignored, not sure if used,
defaults to 600)
-frameduration=<#> sets the frame duration (only used when not custom)
-pcmfile=<file> pcm file, if not specified, will produce silent movie
-pcmfd=<#> accomodates an encoder with a different FD value
-pcmrate=<#> acoomadates an encoder with a different PCM data rate
-singlepalette=<file> all files use the same single palette
-paldatas=<expr> file expr for palette data files
(defaults to paldata*.bin)
-palmaps=<expr> file expr for palette map files
(defaults to palmap*.bin)
-cinepaktiledatas=<expr> file expr for cinepak tile data files
(defaults to cinepaktiledata*.bin)
-ecco32x encode as Ecco 32X demo deviation of cinepak
-custom=<file> indicates a PERL file to customize the frames
';
}