Repository navigation
Expand file tree
/
Copy pathsmacker.c
More file actions
1798 lines (1491 loc) · 46.7 KB
/
Copy pathsmacker.c
File metadata and controls
1798 lines (1491 loc) · 46.7 KB
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
/**
libsmacker - A C library for decoding .smk Smacker Video files
Copyright (C) 2012-2021 Greg Kennedy
See smacker.h for more information.
smacker.c
Main implementation file of libsmacker.
Open, close, query, render, advance and seek an smk
*/
#include "smacker.h"
#include "smk_malloc.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* ************************************************************************* */
/* BITSTREAM Structure */
/* ************************************************************************* */
/* Wraps a block of memory and adds functions to read 1 or 8 bits at a time */
struct smk_bit_t {
const unsigned char * buffer, * end;
unsigned int bit_num;
};
/* ************************************************************************* */
/* BITSTREAM Functions */
/* ************************************************************************* */
/** Initialize a bitstream wrapper */
static void smk_bs_init(struct smk_bit_t * const bs, const unsigned char * const b, const size_t size)
{
/* null check */
assert(bs);
assert(b);
/* set up the pointer to bitstream start and end, and set the bit pointer to 0 */
bs->buffer = b;
bs->end = b + size;
bs->bit_num = 0;
}
/* Reads a bit
Returns -1 if error encountered */
static int smk_bs_read_1(struct smk_bit_t * const bs)
{
int ret;
/* null check */
assert(bs);
/* don't die when running out of bits, but signal */
if (bs->buffer >= bs->end) {
fputs("libsmacker::smk_bs_read_1(): ERROR: bitstream exhausted.\n", stderr);
return -1;
}
/* get next bit and store for return */
ret = (*bs->buffer >> bs->bit_num) & 1;
/* advance to next bit */
if (bs->bit_num >= 7) {
/* Out of bits in this byte: next! */
bs->buffer ++;
bs->bit_num = 0;
} else
bs->bit_num ++;
return ret;
}
/* Reads a byte
Returns -1 if error. */
static int smk_bs_read_8(struct smk_bit_t * const bs)
{
/* null check */
assert(bs);
/* don't die when running out of bits, but signal */
if (bs->buffer + (bs->bit_num > 0) >= bs->end) {
fputs("libsmacker::smk_bs_read_8(): ERROR: bitstream exhausted.\n", stderr);
return -1;
}
if (bs->bit_num) {
/* unaligned read */
int ret = *bs->buffer >> bs->bit_num;
bs->buffer ++;
return ret | (*bs->buffer << (8 - bs->bit_num) & 0xFF);
}
/* aligned read */
return *bs->buffer++;
}
/* ************************************************************************* */
/* HUFF8 Structure */
/* ************************************************************************* */
#define SMK_HUFF8_BRANCH 0x8000
#define SMK_HUFF8_LEAF_MASK 0x7FFF
struct smk_huff8_t {
/* Unfortunately, smk files do not store the alloc size of a small tree.
511 entries is the pessimistic case (N codes and N-1 branches,
with N=256 for 8 bits) */
size_t size;
unsigned short tree[511];
};
/* ************************************************************************* */
/* HUFF8 Functions */
/* ************************************************************************* */
/* Recursive sub-func for building a tree into an array. */
static int _smk_huff8_build_rec(struct smk_huff8_t * const t, struct smk_bit_t * const bs)
{
int bit, value;
assert(t);
assert(bs);
/* Make sure we aren't running out of bounds */
if (t->size >= 511) {
fputs("libsmacker::_smk_huff8_build_rec() - ERROR: size exceeded\n", stderr);
return 0;
}
/* Read the next bit */
if ((bit = smk_bs_read_1(bs)) < 0) {
fputs("libsmacker::_smk_huff8_build_rec() - ERROR: get_bit returned -1\n", stderr);
return 0;
}
if (bit) {
/* Bit set: this forms a Branch node.
what we have to do is build the left-hand branch,
assign the "jump" address,
then build the right hand branch from there.
*/
/* track the current index */
value = t->size ++;
/* go build the left branch */
if (! _smk_huff8_build_rec(t, bs)) {
fputs("libsmacker::_smk_huff8_build_rec() - ERROR: failed to build left sub-tree\n", stderr);
return 0;
}
/* now go back to our current location, and
mark our location as a "jump" */
t->tree[value] = SMK_HUFF8_BRANCH | t->size;
/* continue building the right side */
if (! _smk_huff8_build_rec(t, bs)) {
fputs("libsmacker::_smk_huff8_build_rec() - ERROR: failed to build right sub-tree\n", stderr);
return 0;
}
} else {
/* Bit unset signifies a Leaf node. */
/* Attempt to read value */
if ((value = smk_bs_read_8(bs)) < 0) {
fputs("libsmacker::_smk_huff8_build_rec() - ERROR: get_byte returned -1\n", stderr);
return 0;
}
/* store to tree */
t->tree[t->size ++] = value;
}
return 1;
}
/**
Build an 8-bit Hufftree out of a Bitstream.
*/
static int smk_huff8_build(struct smk_huff8_t * const t, struct smk_bit_t * const bs)
{
int bit;
/* null check */
assert(t);
assert(bs);
/* Smacker huff trees begin with a set-bit. */
if ((bit = smk_bs_read_1(bs)) < 0) {
fputs("libsmacker::smk_huff8_build() - ERROR: initial get_bit returned -1\n", stderr);
return 0;
}
/* OK to fill out the struct now */
t->size = 0;
/* First bit indicates whether a tree is present or not. */
/* Very small or audio-only files may have no tree. */
if (bit) {
if (! _smk_huff8_build_rec(t, bs)) {
fputs("libsmacker::smk_huff8_build() - ERROR: tree build failed\n", stderr);
return 0;
}
} else
t->tree[0] = 0;
/* huff trees end with an unset-bit */
if ((bit = smk_bs_read_1(bs)) < 0) {
fputs("libsmacker::smk_huff8_build() - ERROR: final get_bit returned -1\n", stderr);
return 0;
}
/* a 0 is expected here, a 1 generally indicates a problem! */
if (bit) {
fputs("libsmacker::smk_huff8_build() - ERROR: final get_bit returned 1\n", stderr);
return 0;
}
return 1;
}
/* Look up an 8-bit value from a basic huff tree.
Return -1 on error. */
static int smk_huff8_lookup(const struct smk_huff8_t * const t, struct smk_bit_t * const bs)
{
int bit, index = 0;
/* null check */
assert(t);
assert(bs);
while (t->tree[index] & SMK_HUFF8_BRANCH) {
if ((bit = smk_bs_read_1(bs)) < 0) {
fputs("libsmacker::smk_huff8_lookup() - ERROR: get_bit returned -1\n", stderr);
return -1;
}
if (bit) {
/* take the right branch */
index = t->tree[index] & SMK_HUFF8_LEAF_MASK;
} else {
/* take the left branch */
index ++;
}
}
/* at leaf node. return the value at this point. */
return t->tree[index];
}
/* ************************************************************************* */
/* HUFF16 Structure */
/* ************************************************************************* */
#define SMK_HUFF16_BRANCH 0x80000000
#define SMK_HUFF16_CACHE 0x40000000
#define SMK_HUFF16_LEAF_MASK 0x3FFFFFFF
struct smk_huff16_t {
unsigned int * tree;
size_t size;
/* recently-used values cache */
unsigned short cache[3];
};
/* ************************************************************************* */
/* HUFF16 Functions */
/* ************************************************************************* */
/* Recursive sub-func for building a tree into an array. */
static int _smk_huff16_build_rec(struct smk_huff16_t * const t, struct smk_bit_t * const bs, const struct smk_huff8_t * const low8, const struct smk_huff8_t * const hi8, const size_t limit)
{
int bit, value;
assert(t);
assert(bs);
assert(low8);
assert(hi8);
/* Make sure we aren't running out of bounds */
if (t->size >= limit) {
fputs("libsmacker::_smk_huff16_build_rec() - ERROR: size exceeded\n", stderr);
return 0;
}
/* Read the first bit */
if ((bit = smk_bs_read_1(bs)) < 0) {
fputs("libsmacker::_smk_huff16_build_rec() - ERROR: get_bit returned -1\n", stderr);
return 0;
}
if (bit) {
/* See tree-in-array explanation for HUFF8 above */
/* track the current index */
value = t->size ++;
/* go build the left branch */
if (! _smk_huff16_build_rec(t, bs, low8, hi8, limit)) {
fputs("libsmacker::_smk_huff16_build_rec() - ERROR: failed to build left sub-tree\n", stderr);
return 0;
}
/* now go back to our current location, and
mark our location as a "jump" */
t->tree[value] = SMK_HUFF16_BRANCH | t->size;
/* continue building the right side */
if (! _smk_huff16_build_rec(t, bs, low8, hi8, limit)) {
fputs("libsmacker::_smk_huff16_build_rec() - ERROR: failed to build right sub-tree\n", stderr);
return 0;
}
} else {
/* Bit unset signifies a Leaf node. */
/* Attempt to read LOW value */
if ((value = smk_huff8_lookup(low8, bs)) < 0) {
fputs("libsmacker::_smk_huff16_build_rec() - ERROR: get LOW value returned -1\n", stderr);
return 0;
}
t->tree[t->size] = value;
/* now read HIGH value */
if ((value = smk_huff8_lookup(hi8, bs)) < 0) {
fputs("libsmacker::_smk_huff16_build_rec() - ERROR: get HIGH value returned -1\n", stderr);
return 0;
}
/* Looks OK: we got low and hi values. Return a new LEAF */
t->tree[t->size] |= (value << 8);
/* Last: when building the tree, some Values may correspond to cache positions.
Identify these values and set the Escape code byte accordingly. */
if (t->tree[t->size] == t->cache[0])
t->tree[t->size] = SMK_HUFF16_CACHE;
else if (t->tree[t->size] == t->cache[1])
t->tree[t->size] = SMK_HUFF16_CACHE | 1;
else if (t->tree[t->size] == t->cache[2])
t->tree[t->size] = SMK_HUFF16_CACHE | 2;
t->size ++;
}
return 1;
}
/* Entry point for building a big 16-bit tree. */
static int smk_huff16_build(struct smk_huff16_t * const t, struct smk_bit_t * const bs, const unsigned int alloc_size)
{
struct smk_huff8_t low8, hi8;
size_t limit;
int value, i, bit;
/* null check */
assert(t);
assert(bs);
/* Smacker huff trees begin with a set-bit. */
if ((bit = smk_bs_read_1(bs)) < 0) {
fputs("libsmacker::smk_huff16_build() - ERROR: initial get_bit returned -1\n", stderr);
return 0;
}
t->size = 0;
/* First bit indicates whether a tree is present or not. */
/* Very small or audio-only files may have no tree. */
if (bit) {
/* build low-8-bits tree */
if (! smk_huff8_build(&low8, bs)) {
fputs("libsmacker::smk_huff16_build() - ERROR: failed to build LOW tree\n", stderr);
return 0;
}
/* build hi-8-bits tree */
if (! smk_huff8_build(&hi8, bs)) {
fputs("libsmacker::smk_huff16_build() - ERROR: failed to build HIGH tree\n", stderr);
return 0;
}
/* Init the escape code cache. */
for (i = 0; i < 3; i ++) {
if ((value = smk_bs_read_8(bs)) < 0) {
fprintf(stderr, "libsmacker::smk_huff16_build() - ERROR: get LOW value for cache %d returned -1\n", i);
return 0;
}
t->cache[i] = value;
/* now read HIGH value */
if ((value = smk_bs_read_8(bs)) < 0) {
fprintf(stderr, "libsmacker::smk_huff16_build() - ERROR: get HIGH value for cache %d returned -1\n", i);
return 0;
}
t->cache[i] |= (value << 8);
}
/* Everything looks OK so far. Time to malloc structure. */
if (alloc_size < 12 || alloc_size % 4) {
fprintf(stderr, "libsmacker::smk_huff16_build() - ERROR: illegal value %u for alloc_size\n", alloc_size);
return 0;
}
limit = (alloc_size - 12) / 4;
if ((t->tree = malloc(limit * sizeof(unsigned int))) == NULL) {
perror("libsmacker::smk_huff16_build() - ERROR: failed to malloc() huff16 tree");
return 0;
}
/* Finally, call recursive function to retrieve the Bigtree. */
if (! _smk_huff16_build_rec(t, bs, &low8, &hi8, limit)) {
fputs("libsmacker::smk_huff16_build() - ERROR: failed to build huff16 tree\n", stderr);
free(t->tree);
t->tree = NULL;
return 0;
}
/* check that we completely filled the tree */
if (limit != t->size && 0) {
fputs("libsmacker::smk_huff16_build() - ERROR: failed to completely decode huff16 tree\n", stderr);
free(t->tree);
t->tree = NULL;
return 0;
}
} else {
if ((t->tree = malloc(sizeof(unsigned int))) == NULL) {
perror("libsmacker::smk_huff16_build() - ERROR: failed to malloc() huff16 tree");
return 0;
}
t->tree[0] = 0;
t->cache[0] = t->cache[1] = t->cache[2] = 0;
return 1;
}
/* Check final end tag. */
if ((bit = smk_bs_read_1(bs)) < 0) {
fputs("libsmacker::smk_huff16_build() - ERROR: final get_bit returned -1\n", stderr);
free(t->tree);
t->tree = NULL;
return 0;
}
/* a 0 is expected here, a 1 generally indicates a problem! */
if (bit) {
fputs("libsmacker::smk_huff16_build() - ERROR: final get_bit returned 1\n", stderr);
free(t->tree);
t->tree = NULL;
return 0;
}
return 1;
}
/* Look up a 16-bit value from a large huff tree.
Return -1 on error.
Note that this also updates the recently-used-values cache. */
static int smk_huff16_lookup(struct smk_huff16_t * const t, struct smk_bit_t * const bs)
{
int bit, value, index = 0;
/* null check */
assert(t);
assert(bs);
while (t->tree[index] & SMK_HUFF16_BRANCH) {
if ((bit = smk_bs_read_1(bs)) < 0) {
fputs("libsmacker::smk_huff16_lookup() - ERROR: get_bit returned -1\n", stderr);
return -1;
}
if (bit) {
/* take the right branch */
index = t->tree[index] & SMK_HUFF16_LEAF_MASK;
} else {
/* take the left branch */
index ++;
}
}
/* Get the value at this point */
value = t->tree[index];
if (value & SMK_HUFF16_CACHE) {
/* uses cached value instead of actual value */
value = t->cache[value & SMK_HUFF16_LEAF_MASK];
}
if (t->cache[0] != value) {
/* Update the cache, by moving val to the front of the queue,
if it isn't already there. */
t->cache[2] = t->cache[1];
t->cache[1] = t->cache[0];
t->cache[0] = value;
}
return value;
}
/* ************************************************************************* */
/* SMACKER Structure */
/* ************************************************************************* */
/* tree processing order */
#define SMK_TREE_MMAP 0
#define SMK_TREE_MCLR 1
#define SMK_TREE_FULL 2
#define SMK_TREE_TYPE 3
struct smk_t {
/* meta-info */
/* file mode: see flags, smacker.h */
unsigned char mode;
/* microsec per frame - stored as a double to handle scaling
(large positive millisec / frame values may overflow a ul) */
double usf;
/* total frames */
unsigned long f;
/* does file have a ring frame? (in other words, does file loop?) */
unsigned char ring_frame;
/* Index of current frame */
unsigned long cur_frame;
/* SOURCE union.
Where the data is going to be read from (or be stored),
depending on the file mode. */
union {
struct {
/* on-disk mode */
FILE * fp;
unsigned long * chunk_offset;
} file;
/* in-memory mode: unprocessed chunks */
unsigned char ** chunk_data;
} source;
/* shared array of "chunk sizes"*/
unsigned long * chunk_size;
/* Holds per-frame flags (i.e. 'keyframe') */
unsigned char * keyframe;
/* Holds per-frame type mask (e.g. 'audio track 3, 2, and palette swap') */
unsigned char * frame_type;
/* video and audio structures */
/* Video data type: enable/disable decode switch,
video info and flags,
pointer to last-decoded-palette */
struct smk_video_t {
/* enable/disable decode switch */
unsigned char enable;
/* video info */
unsigned long w;
unsigned long h;
/* Y scale mode (constants defined in smacker.h)
0: unscaled
1: doubled
2: interlaced */
unsigned char y_scale_mode;
/* version ('2' or '4') */
unsigned char v;
/* Huffman trees */
unsigned long tree_size[4];
struct smk_huff16_t tree[4];
/* Palette data type: pointer to last-decoded-palette */
unsigned char palette[256][3];
/* Last-unpacked frame */
unsigned char * frame;
} video;
/* audio structure */
struct smk_audio_t {
/* set if track exists in file */
unsigned char exists;
/* enable/disable switch (per track) */
unsigned char enable;
/* Info */
unsigned char channels;
unsigned char bitdepth;
unsigned long rate;
long max_buffer;
/* compression type
0: raw PCM
1: SMK DPCM
2: Bink (Perceptual), unsupported */
unsigned char compress;
/* pointer to last-decoded-audio-buffer */
void * buffer;
unsigned long buffer_size;
} audio[7];
};
union smk_read_t {
FILE * file;
unsigned char * ram;
};
/* ************************************************************************* */
/* SMACKER Functions */
/* ************************************************************************* */
/* An fread wrapper: consumes N bytes, or returns -1
on failure (when size doesn't match expected) */
static char smk_read_file(void * buf, const size_t size, FILE * fp)
{
/* don't bother checking buf or fp, fread does it for us */
size_t bytesRead = fread(buf, 1, size, fp);
if (bytesRead != size) {
fprintf(stderr, "libsmacker::smk_read_file(buf,%lu,fp) - ERROR: Short read, %lu bytes returned\n", (unsigned long)size, (unsigned long)bytesRead);
perror("\tReason");
return -1;
}
return 0;
}
/* A memcpy wrapper: consumes N bytes, or returns -1
on failure (when size too low) */
static char smk_read_memory(void * buf, const unsigned long size, unsigned char ** p, unsigned long * p_size)
{
if (size > *p_size) {
fprintf(stderr, "libsmacker::smk_read_memory(buf,%lu,p,%lu) - ERROR: Short read\n", (unsigned long)size, (unsigned long)*p_size);
return -1;
}
memcpy(buf, *p, size);
*p += size;
*p_size -= size;
return 0;
}
/* Helper functions to do the reading, plus
byteswap from LE to host order */
/* read n bytes from (source) into ret */
#define smk_read(ret,n) \
{ \
if (m) \
{ \
r = (smk_read_file(ret,n,fp.file)); \
} \
else \
{ \
r = (smk_read_memory(ret,n,&fp.ram,&size)); \
} \
if (r < 0) \
{ \
fprintf(stderr,"libsmacker::smk_read(...) - Errors encountered on read, bailing out (file: %s, line: %lu)\n", __FILE__, (unsigned long)__LINE__); \
goto error; \
} \
}
/* Calls smk_read, but returns a ul */
#define smk_read_ul(p) \
{ \
smk_read(buf,4); \
p = ((unsigned long) buf[3] << 24) | \
((unsigned long) buf[2] << 16) | \
((unsigned long) buf[1] << 8) | \
((unsigned long) buf[0]); \
}
/* PUBLIC FUNCTIONS */
/* open an smk (from a generic Source) */
static smk smk_open_generic(const unsigned char m, union smk_read_t fp, unsigned long size, const unsigned char process_mode)
{
/* Smacker structure we intend to work on / return */
smk s;
/* Temporary variables */
long temp_l;
unsigned long temp_u;
/* r is used by macros above for return code */
char r;
unsigned char buf[4] = {'\0'};
/* video hufftrees are stored as a large chunk (bitstream)
these vars are used to load, then decode them */
unsigned char * hufftree_chunk = NULL;
unsigned long tree_size;
/* a bitstream struct */
struct smk_bit_t bs;
/** **/
/* safe malloc the structure */
if ((s = calloc(1, sizeof(struct smk_t))) == NULL) {
perror("libsmacker::smk_open_generic() - ERROR: failed to malloc() smk structure");
return NULL;
}
/* Check for a valid signature */
smk_read(buf, 3);
if (buf[0] != 'S' || buf[1] != 'M' || buf[2] != 'K') {
fprintf(stderr, "libsmacker::smk_open_generic - ERROR: invalid SMKn signature (got: %s)\n", buf);
goto error;
}
/* Read .smk file version */
smk_read(&s->video.v, 1);
if (s->video.v != '2' && s->video.v != '4') {
fprintf(stderr, "libsmacker::smk_open_generic - Warning: invalid SMK version %c (expected: 2 or 4)\n", s->video.v);
/* take a guess */
if (s->video.v < '4')
s->video.v = '2';
else
s->video.v = '4';
fprintf(stderr, "\tProcessing will continue as type %c\n", s->video.v);
}
/* width, height, total num frames */
smk_read_ul(s->video.w);
smk_read_ul(s->video.h);
smk_read_ul(s->f);
/* frames per second calculation */
smk_read_ul(temp_u);
temp_l = (int)temp_u;
if (temp_l > 0) {
/* millisec per frame */
s->usf = temp_l * 1000;
} else if (temp_l < 0) {
/* 10 microsec per frame */
s->usf = temp_l * -10;
} else {
/* defaults to 10 usf (= 100000 microseconds) */
s->usf = 100000;
}
/* Video flags follow.
Ring frame is important to libsmacker.
Y scale / Y interlace go in the Video flags.
The user should scale appropriately. */
smk_read_ul(temp_u);
if (temp_u & 0x01)
s->ring_frame = 1;
if (temp_u & 0x02)
s->video.y_scale_mode = SMK_FLAG_Y_DOUBLE;
if (temp_u & 0x04) {
if (s->video.y_scale_mode == SMK_FLAG_Y_DOUBLE)
fputs("libsmacker::smk_open_generic - Warning: SMK file specifies both Y-Double AND Y-Interlace.\n", stderr);
s->video.y_scale_mode = SMK_FLAG_Y_INTERLACE;
}
/* Max buffer size for each audio track - used to pre-allocate buffers */
for (temp_l = 0; temp_l < 7; temp_l ++)
smk_read_ul(s->audio[temp_l].max_buffer);
/* Read size of "hufftree chunk" - save for later. */
smk_read_ul(tree_size);
/* "unpacked" sizes of each huff tree */
for (temp_l = 0; temp_l < 4; temp_l ++)
smk_read_ul(s->video.tree_size[temp_l]);
/* read audio rate data */
for (temp_l = 0; temp_l < 7; temp_l ++) {
smk_read_ul(temp_u);
if (temp_u & 0x40000000) {
/* Audio track specifies "exists" flag, malloc structure and copy components. */
s->audio[temp_l].exists = 1;
/* and for all audio tracks */
smk_malloc(s->audio[temp_l].buffer, s->audio[temp_l].max_buffer);
if (temp_u & 0x80000000)
s->audio[temp_l].compress = 1;
s->audio[temp_l].bitdepth = ((temp_u & 0x20000000) ? 16 : 8);
s->audio[temp_l].channels = ((temp_u & 0x10000000) ? 2 : 1);
if (temp_u & 0x0c000000) {
fprintf(stderr, "libsmacker::smk_open_generic - Warning: audio track %ld is compressed with Bink (perceptual) Audio Codec: this is currently unsupported by libsmacker\n", temp_l);
s->audio[temp_l].compress = 2;
}
/* Bits 25 & 24 are unused. */
s->audio[temp_l].rate = (temp_u & 0x00FFFFFF);
}
}
/* Skip over Dummy field */
smk_read_ul(temp_u);
/* FrameSizes and Keyframe marker are stored together. */
smk_malloc(s->keyframe, (s->f + s->ring_frame));
smk_malloc(s->chunk_size, (s->f + s->ring_frame) * sizeof(unsigned long));
for (temp_u = 0; temp_u < (s->f + s->ring_frame); temp_u ++) {
smk_read_ul(s->chunk_size[temp_u]);
/* Set Keyframe */
if (s->chunk_size[temp_u] & 0x01)
s->keyframe[temp_u] = 1;
/* Bits 1 is used, but the purpose is unknown. */
s->chunk_size[temp_u] &= 0xFFFFFFFC;
}
/* That was easy... Now read FrameTypes! */
smk_malloc(s->frame_type, (s->f + s->ring_frame));
for (temp_u = 0; temp_u < (s->f + s->ring_frame); temp_u ++)
smk_read(&s->frame_type[temp_u], 1);
/* HuffmanTrees
We know the sizes already: read and assemble into
something actually parse-able at run-time */
smk_malloc(hufftree_chunk, tree_size);
smk_read(hufftree_chunk, tree_size);
/* set up a Bitstream */
smk_bs_init(&bs, hufftree_chunk, tree_size);
/* create some tables */
for (temp_u = 0; temp_u < 4; temp_u ++) {
if (! smk_huff16_build(&s->video.tree[temp_u], &bs, s->video.tree_size[temp_u])) {
fprintf(stderr, "libsmacker::smk_open_generic - ERROR: failed to create huff16 tree %lu\n", temp_u);
goto error;
}
}
/* clean up */
smk_free(hufftree_chunk);
/* Go ahead and malloc storage for the video frame */
smk_malloc(s->video.frame, s->video.w * s->video.h);
/* final processing: depending on ProcessMode, handle what to do with rest of file data */
s->mode = process_mode;
/* Handle the rest of the data.
For MODE_MEMORY, read the chunks and store */
if (s->mode == SMK_MODE_MEMORY) {
smk_malloc(s->source.chunk_data, (s->f + s->ring_frame) * sizeof(unsigned char *));
for (temp_u = 0; temp_u < (s->f + s->ring_frame); temp_u ++) {
smk_malloc(s->source.chunk_data[temp_u], s->chunk_size[temp_u]);
smk_read(s->source.chunk_data[temp_u], s->chunk_size[temp_u]);
}
} else {
/* MODE_STREAM: don't read anything now, just precompute offsets.
use fseek to verify that the file is "complete" */
smk_malloc(s->source.file.chunk_offset, (s->f + s->ring_frame) * sizeof(unsigned long));
for (temp_u = 0; temp_u < (s->f + s->ring_frame); temp_u ++) {
s->source.file.chunk_offset[temp_u] = ftell(fp.file);
if (fseek(fp.file, s->chunk_size[temp_u], SEEK_CUR)) {
fprintf(stderr, "libsmacker::smk_open_generic - ERROR: fseek to frame %lu not OK.\n", temp_u);
perror("\tError reported was");
goto error;
}
}
}
return s;
error:
smk_free(hufftree_chunk);
smk_close(s);
return NULL;
}
/* open an smk (from a memory buffer) */
smk smk_open_memory(const unsigned char * buffer, const unsigned long size)
{
smk s = NULL;
union smk_read_t fp;
if (buffer == NULL) {
fputs("libsmacker::smk_open_memory() - ERROR: buffer pointer is NULL\n", stderr);
return NULL;
}
/* set up the read union for Memory mode */
fp.ram = (unsigned char *)buffer;
if (!(s = smk_open_generic(0, fp, size, SMK_MODE_MEMORY)))
fprintf(stderr, "libsmacker::smk_open_memory(buffer,%lu) - ERROR: Fatal error in smk_open_generic, returning NULL.\n", size);
return s;
}
/* open an smk (from a file) */
smk smk_open_filepointer(FILE * file, const unsigned char mode)
{
smk s = NULL;
union smk_read_t fp;
if (file == NULL) {
fputs("libsmacker::smk_open_filepointer() - ERROR: file pointer is NULL\n", stderr);
return NULL;
}
/* Copy file ptr to internal union */
fp.file = file;
if (!(s = smk_open_generic(1, fp, 0, mode))) {
fprintf(stderr, "libsmacker::smk_open_filepointer(file,%u) - ERROR: Fatal error in smk_open_generic, returning NULL.\n", mode);
fclose(fp.file);
goto error;
}
if (mode == SMK_MODE_MEMORY)
fclose(fp.file);
else
s->source.file.fp = fp.file;
/* fall through, return s or null */
error:
return s;
}
/* open an smk (from a file) */
smk smk_open_file(const char * filename, const unsigned char mode)
{
FILE * fp;
if (filename == NULL) {
fputs("libsmacker::smk_open_file() - ERROR: filename is NULL\n", stderr);
return NULL;
}
if (!(fp = fopen(filename, "rb"))) {
fprintf(stderr, "libsmacker::smk_open_file(%s,%u) - ERROR: could not open file\n", filename, mode);
perror("\tError reported was");
goto error;
}
/* kick processing to smk_open_filepointer */
return smk_open_filepointer(fp, mode);
/* fall through, return s or null */
error:
return NULL;
}
/* close out an smk file and clean up memory */
void smk_close(smk s)
{
unsigned long u;
if (s == NULL) {
fputs("libsmacker::smk_close() - ERROR: smk is NULL\n", stderr);
return;
}
/* free video sub-components */
for (u = 0; u < 4; u ++) {
if (s->video.tree[u].tree) free(s->video.tree[u].tree);
}
smk_free(s->video.frame);
/* free audio sub-components */
for (u = 0; u < 7; u++) {
if (s->audio[u].buffer)
smk_free(s->audio[u].buffer);
}
smk_free(s->keyframe);
smk_free(s->frame_type);
if (s->mode == SMK_MODE_DISK) {
/* disk-mode */
if (s->source.file.fp)
fclose(s->source.file.fp);
smk_free(s->source.file.chunk_offset);
} else {
/* mem-mode */
if (s->source.chunk_data != NULL) {
for (u = 0; u < (s->f + s->ring_frame); u++)
smk_free(s->source.chunk_data[u]);
smk_free(s->source.chunk_data);
}
}
smk_free(s->chunk_size);
smk_free(s);
}
/* tell some info about the file */
char smk_info_all(const smk object, unsigned long * frame, unsigned long * frame_count, double * usf)
{
/* null check */
if (object == NULL) {
fputs("libsmacker::smk_info_all() - ERROR: smk is NULL\n", stderr);
return -1;
}
if (!frame && !frame_count && !usf) {
fputs("libsmacker::smk_info_all(object,frame,frame_count,usf) - ERROR: Request for info with all-NULL return references\n", stderr);
goto error;
}
if (frame)
*frame = (object->cur_frame % object->f);