Debian Codegraph @

smacker.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /**
2 libsmacker - A C library for decoding .smk Smacker Video files
3 Copyright (C) 2012-2021 Greg Kennedy
4
5 See smacker.h for more information.
6
7 smacker.c
8 Main implementation file of libsmacker.
9 Open, close, query, render, advance and seek an smk
10 */
11
12 #include "smacker.h"
13
14 #include "smk_malloc.h"
15
16 #include <assert.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 /* ************************************************************************* */
22 /* BITSTREAM Structure */
23 /* ************************************************************************* */
24 /* Wraps a block of memory and adds functions to read 1 or 8 bits at a time */
25 struct smk_bit_t {
26 const unsigned char * buffer, * end;
27 unsigned int bit_num;
28 };
29
30 /* ************************************************************************* */
31 /* BITSTREAM Functions */
32 /* ************************************************************************* */
33 /** Initialize a bitstream wrapper */
34 static void smk_bs_init(struct smk_bit_t * const bs, const unsigned char * const b, const size_t size)
35 {
36 /* null check */
37 assert(bs);
38 assert(b);
39 /* set up the pointer to bitstream start and end, and set the bit pointer to 0 */
40 bs->buffer = b;
41 bs->end = b + size;
42 bs->bit_num = 0;
43 }
44
45 /* Reads a bit
46 Returns -1 if error encountered */
47 static int smk_bs_read_1(struct smk_bit_t * const bs)
48 {
49 int ret;
50 /* null check */
51 assert(bs);
52
53 /* don't die when running out of bits, but signal */
54 if (bs->buffer >= bs->end) {
55 fputs("libsmacker::smk_bs_read_1(): ERROR: bitstream exhausted.\n", stderr);
56 return -1;
57 }
58
59 /* get next bit and store for return */
60 ret = (*bs->buffer >> bs->bit_num) & 1;
61
62 /* advance to next bit */
63 if (bs->bit_num >= 7) {
64 /* Out of bits in this byte: next! */
65 bs->buffer ++;
66 bs->bit_num = 0;
67 } else
68 bs->bit_num ++;
69
70 return ret;
71 }
72
73 /* Reads a byte
74 Returns -1 if error. */
75 static int smk_bs_read_8(struct smk_bit_t * const bs)
76 {
77 /* null check */
78 assert(bs);
79
80 /* don't die when running out of bits, but signal */
81 if (bs->buffer + (bs->bit_num > 0) >= bs->end) {
82 fputs("libsmacker::smk_bs_read_8(): ERROR: bitstream exhausted.\n", stderr);
83 return -1;
84 }
85
86 if (bs->bit_num) {
87 /* unaligned read */
88 int ret = *bs->buffer >> bs->bit_num;
89 bs->buffer ++;
90 return ret | (*bs->buffer << (8 - bs->bit_num) & 0xFF);
91 }
92
93 /* aligned read */
94 return *bs->buffer++;
95 }
96
97 /* ************************************************************************* */
98 /* HUFF8 Structure */
99 /* ************************************************************************* */
100 #define SMK_HUFF8_BRANCH 0x8000
101 #define SMK_HUFF8_LEAF_MASK 0x7FFF
102
103 struct smk_huff8_t {
104 /* Unfortunately, smk files do not store the alloc size of a small tree.
105 511 entries is the pessimistic case (N codes and N-1 branches,
106 with N=256 for 8 bits) */
107 size_t size;
108 unsigned short tree[511];
109 };
110
111 /* ************************************************************************* */
112 /* HUFF8 Functions */
113 /* ************************************************************************* */
114 /* Recursive sub-func for building a tree into an array. */
115 static int _smk_huff8_build_rec(struct smk_huff8_t * const t, struct smk_bit_t * const bs)
116 {
117 int bit, value;
118 assert(t);
119 assert(bs);
120
121 /* Make sure we aren't running out of bounds */
122 if (t->size >= 511) {
123 fputs("libsmacker::_smk_huff8_build_rec() - ERROR: size exceeded\n", stderr);
124 return 0;
125 }
126
127 /* Read the next bit */
128 if ((bit = smk_bs_read_1(bs)) < 0) {
129 fputs("libsmacker::_smk_huff8_build_rec() - ERROR: get_bit returned -1\n", stderr);
130 return 0;
131 }
132
133 if (bit) {
134 /* Bit set: this forms a Branch node.
135 what we have to do is build the left-hand branch,
136 assign the "jump" address,
137 then build the right hand branch from there.
138 */
139 /* track the current index */
140 value = t->size ++;
141
142 /* go build the left branch */
143 if (! _smk_huff8_build_rec(t, bs)) {
144 fputs("libsmacker::_smk_huff8_build_rec() - ERROR: failed to build left sub-tree\n", stderr);
145 return 0;
146 }
147
148 /* now go back to our current location, and
149 mark our location as a "jump" */
150 t->tree[value] = SMK_HUFF8_BRANCH | t->size;
151
152 /* continue building the right side */
153 if (! _smk_huff8_build_rec(t, bs)) {
154 fputs("libsmacker::_smk_huff8_build_rec() - ERROR: failed to build right sub-tree\n", stderr);
155 return 0;
156 }
157 } else {
158 /* Bit unset signifies a Leaf node. */
159 /* Attempt to read value */
160 if ((value = smk_bs_read_8(bs)) < 0) {
161 fputs("libsmacker::_smk_huff8_build_rec() - ERROR: get_byte returned -1\n", stderr);
162 return 0;
163 }
164
165 /* store to tree */
166 t->tree[t->size ++] = value;
167 }
168
169 return 1;
170 }
171
172 /**
173 Build an 8-bit Hufftree out of a Bitstream.
174 */
175 static int smk_huff8_build(struct smk_huff8_t * const t, struct smk_bit_t * const bs)
176 {
177 int bit;
178 /* null check */
179 assert(t);
180 assert(bs);
181
182 /* Smacker huff trees begin with a set-bit. */
183 if ((bit = smk_bs_read_1(bs)) < 0) {
184 fputs("libsmacker::smk_huff8_build() - ERROR: initial get_bit returned -1\n", stderr);
185 return 0;
186 }
187
188 /* OK to fill out the struct now */
189 t->size = 0;
190
191 /* First bit indicates whether a tree is present or not. */
192 /* Very small or audio-only files may have no tree. */
193 if (bit) {
194 if (! _smk_huff8_build_rec(t, bs)) {
195 fputs("libsmacker::smk_huff8_build() - ERROR: tree build failed\n", stderr);
196 return 0;
197 }
198 } else
199 t->tree[0] = 0;
200
201 /* huff trees end with an unset-bit */
202 if ((bit = smk_bs_read_1(bs)) < 0) {
203 fputs("libsmacker::smk_huff8_build() - ERROR: final get_bit returned -1\n", stderr);
204 return 0;
205 }
206
207 /* a 0 is expected here, a 1 generally indicates a problem! */
208 if (bit) {
209 fputs("libsmacker::smk_huff8_build() - ERROR: final get_bit returned 1\n", stderr);
210 return 0;
211 }
212
213 return 1;
214 }
215
216 /* Look up an 8-bit value from a basic huff tree.
217 Return -1 on error. */
218 static int smk_huff8_lookup(const struct smk_huff8_t * const t, struct smk_bit_t * const bs)
219 {
220 int bit, index = 0;
221 /* null check */
222 assert(t);
223 assert(bs);
224
225 while (t->tree[index] & SMK_HUFF8_BRANCH) {
226 if ((bit = smk_bs_read_1(bs)) < 0) {
227 fputs("libsmacker::smk_huff8_lookup() - ERROR: get_bit returned -1\n", stderr);
228 return -1;
229 }
230
231 if (bit) {
232 /* take the right branch */
233 index = t->tree[index] & SMK_HUFF8_LEAF_MASK;
234 } else {
235 /* take the left branch */
236 index ++;
237 }
238 }
239
240 /* at leaf node. return the value at this point. */
241 return t->tree[index];
242 }
243
244 /* ************************************************************************* */
245 /* HUFF16 Structure */
246 /* ************************************************************************* */
247 #define SMK_HUFF16_BRANCH 0x80000000
248 #define SMK_HUFF16_CACHE 0x40000000
249 #define SMK_HUFF16_LEAF_MASK 0x3FFFFFFF
250
251 struct smk_huff16_t {
252 unsigned int * tree;
253 size_t size;
254
255 /* recently-used values cache */
256 unsigned short cache[3];
257 };
258
259 /* ************************************************************************* */
260 /* HUFF16 Functions */
261 /* ************************************************************************* */
262 /* Recursive sub-func for building a tree into an array. */
263 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)
264 {
265 int bit, value;
266 assert(t);
267 assert(bs);
268 assert(low8);
269 assert(hi8);
270
271 /* Make sure we aren't running out of bounds */
272 if (t->size >= limit) {
273 fputs("libsmacker::_smk_huff16_build_rec() - ERROR: size exceeded\n", stderr);
274 return 0;
275 }
276
277 /* Read the first bit */
278 if ((bit = smk_bs_read_1(bs)) < 0) {
279 fputs("libsmacker::_smk_huff16_build_rec() - ERROR: get_bit returned -1\n", stderr);
280 return 0;
281 }
282
283 if (bit) {
284 /* See tree-in-array explanation for HUFF8 above */
285 /* track the current index */
286 value = t->size ++;
287
288 /* go build the left branch */
289 if (! _smk_huff16_build_rec(t, bs, low8, hi8, limit)) {
290 fputs("libsmacker::_smk_huff16_build_rec() - ERROR: failed to build left sub-tree\n", stderr);
291 return 0;
292 }
293
294 /* now go back to our current location, and
295 mark our location as a "jump" */
296 t->tree[value] = SMK_HUFF16_BRANCH | t->size;
297
298 /* continue building the right side */
299 if (! _smk_huff16_build_rec(t, bs, low8, hi8, limit)) {
300 fputs("libsmacker::_smk_huff16_build_rec() - ERROR: failed to build right sub-tree\n", stderr);
301 return 0;
302 }
303 } else {
304 /* Bit unset signifies a Leaf node. */
305 /* Attempt to read LOW value */
306 if ((value = smk_huff8_lookup(low8, bs)) < 0) {
307 fputs("libsmacker::_smk_huff16_build_rec() - ERROR: get LOW value returned -1\n", stderr);
308 return 0;
309 }
310
311 t->tree[t->size] = value;
312
313 /* now read HIGH value */
314 if ((value = smk_huff8_lookup(hi8, bs)) < 0) {
315 fputs("libsmacker::_smk_huff16_build_rec() - ERROR: get HIGH value returned -1\n", stderr);
316 return 0;
317 }
318
319 /* Looks OK: we got low and hi values. Return a new LEAF */
320 t->tree[t->size] |= (value << 8);
321
322 /* Last: when building the tree, some Values may correspond to cache positions.
323 Identify these values and set the Escape code byte accordingly. */
324 if (t->tree[t->size] == t->cache[0])
325 t->tree[t->size] = SMK_HUFF16_CACHE;
326 else if (t->tree[t->size] == t->cache[1])
327 t->tree[t->size] = SMK_HUFF16_CACHE | 1;
328 else if (t->tree[t->size] == t->cache[2])
329 t->tree[t->size] = SMK_HUFF16_CACHE | 2;
330
331 t->size ++;
332 }
333
334 return 1;
335 }
336
337 /* Entry point for building a big 16-bit tree. */
338 static int smk_huff16_build(struct smk_huff16_t * const t, struct smk_bit_t * const bs, const unsigned int alloc_size)
339 {
340 struct smk_huff8_t low8, hi8;
341 size_t limit;
342 int value, i, bit;
343 /* null check */
344 assert(t);
345 assert(bs);
346
347 /* Smacker huff trees begin with a set-bit. */
348 if ((bit = smk_bs_read_1(bs)) < 0) {
349 fputs("libsmacker::smk_huff16_build() - ERROR: initial get_bit returned -1\n", stderr);
350 return 0;
351 }
352
353 t->size = 0;
354
355 /* First bit indicates whether a tree is present or not. */
356 /* Very small or audio-only files may have no tree. */
357 if (bit) {
358 /* build low-8-bits tree */
359 if (! smk_huff8_build(&low8, bs)) {
360 fputs("libsmacker::smk_huff16_build() - ERROR: failed to build LOW tree\n", stderr);
361 return 0;
362 }
363
364 /* build hi-8-bits tree */
365 if (! smk_huff8_build(&hi8, bs)) {
366 fputs("libsmacker::smk_huff16_build() - ERROR: failed to build HIGH tree\n", stderr);
367 return 0;
368 }
369
370 /* Init the escape code cache. */
371 for (i = 0; i < 3; i ++) {
372 if ((value = smk_bs_read_8(bs)) < 0) {
373 fprintf(stderr, "libsmacker::smk_huff16_build() - ERROR: get LOW value for cache %d returned -1\n", i);
374 return 0;
375 }
376
377 t->cache[i] = value;
378
379 /* now read HIGH value */
380 if ((value = smk_bs_read_8(bs)) < 0) {
381 fprintf(stderr, "libsmacker::smk_huff16_build() - ERROR: get HIGH value for cache %d returned -1\n", i);
382 return 0;
383 }
384
385 t->cache[i] |= (value << 8);
386 }
387
388 /* Everything looks OK so far. Time to malloc structure. */
389 if (alloc_size < 12 || alloc_size % 4) {
390 fprintf(stderr, "libsmacker::smk_huff16_build() - ERROR: illegal value %u for alloc_size\n", alloc_size);
391 return 0;
392 }
393
394 limit = (alloc_size - 12) / 4;
395
396 if ((t->tree = malloc(limit * sizeof(unsigned int))) == NULL) {
397 perror("libsmacker::smk_huff16_build() - ERROR: failed to malloc() huff16 tree");
398 return 0;
399 }
400
401 /* Finally, call recursive function to retrieve the Bigtree. */
402 if (! _smk_huff16_build_rec(t, bs, &low8, &hi8, limit)) {
403 fputs("libsmacker::smk_huff16_build() - ERROR: failed to build huff16 tree\n", stderr);
404 free(t->tree);
405 t->tree = NULL;
406 return 0;
407 }
408 } else {
409 if ((t->tree = malloc(sizeof(unsigned int))) == NULL) {
410 perror("libsmacker::smk_huff16_build() - ERROR: failed to malloc() huff16 tree");
411 return 0;
412 }
413
414 t->tree[0] = 0;
•415 t->cache[0] = t->cache[1] = t->cache[2] = 0;
416 return 1;
417 }
418
419 /* Check final end tag. */
420 if ((bit = smk_bs_read_1(bs)) < 0) {
421 fputs("libsmacker::smk_huff16_build() - ERROR: final get_bit returned -1\n", stderr);
422 free(t->tree);
423 t->tree = NULL;
424 return 0;
425 }
426
427 /* a 0 is expected here, a 1 generally indicates a problem! */
428 if (bit) {
429 fputs("libsmacker::smk_huff16_build() - ERROR: final get_bit returned 1\n", stderr);
430 free(t->tree);
431 t->tree = NULL;
432 return 0;
433 }
434
435 return 1;
436 }
437
438 /* Look up a 16-bit value from a large huff tree.
439 Return -1 on error.
440 Note that this also updates the recently-used-values cache. */
441 static int smk_huff16_lookup(struct smk_huff16_t * const t, struct smk_bit_t * const bs)
442 {
443 int bit, value, index = 0;
444 /* null check */
445 assert(t);
446 assert(bs);
447
448 while (t->tree[index] & SMK_HUFF16_BRANCH) {
449 if ((bit = smk_bs_read_1(bs)) < 0) {
450 fputs("libsmacker::smk_huff16_lookup() - ERROR: get_bit returned -1\n", stderr);
451 return -1;
452 }
453
454 if (bit) {
455 /* take the right branch */
456 index = t->tree[index] & SMK_HUFF16_LEAF_MASK;
457 } else {
458 /* take the left branch */
459 index ++;
460 }
461 }
462
463 /* Get the value at this point */
464 value = t->tree[index];
465
466 if (value & SMK_HUFF16_CACHE) {
467 /* uses cached value instead of actual value */
468 value = t->cache[value & SMK_HUFF16_LEAF_MASK];
469 }
470
471 if (t->cache[0] != value) {
472 /* Update the cache, by moving val to the front of the queue,
473 if it isn't already there. */
474 t->cache[2] = t->cache[1];
475 t->cache[1] = t->cache[0];
476 t->cache[0] = value;
477 }
478
479 return value;
480 }
481
482 /* ************************************************************************* */
483 /* SMACKER Structure */
484 /* ************************************************************************* */
485 /* tree processing order */
486 #define SMK_TREE_MMAP 0
487 #define SMK_TREE_MCLR 1
488 #define SMK_TREE_FULL 2
489 #define SMK_TREE_TYPE 3
490
491 struct smk_t {
492 /* meta-info */
493 /* file mode: see flags, smacker.h */
494 unsigned char mode;
495
496 /* microsec per frame - stored as a double to handle scaling
497 (large positive millisec / frame values may overflow a ul) */
498 double usf;
499
500 /* total frames */
501 unsigned long f;
502 /* does file have a ring frame? (in other words, does file loop?) */
503 unsigned char ring_frame;
504
505 /* Index of current frame */
506 unsigned long cur_frame;
507
508 /* SOURCE union.
509 Where the data is going to be read from (or be stored),
510 depending on the file mode. */
511 union {
512 struct {
513 /* on-disk mode */
514 FILE * fp;
515 unsigned long * chunk_offset;
516 } file;
517
518 /* in-memory mode: unprocessed chunks */
519 unsigned char ** chunk_data;
520 } source;
521
522 /* shared array of "chunk sizes"*/
523 unsigned long * chunk_size;
524
525 /* Holds per-frame flags (i.e. 'keyframe') */
526 unsigned char * keyframe;
527 /* Holds per-frame type mask (e.g. 'audio track 3, 2, and palette swap') */
528 unsigned char * frame_type;
529
530 /* video and audio structures */
531 /* Video data type: enable/disable decode switch,
532 video info and flags,
533 pointer to last-decoded-palette */
534 struct smk_video_t {
535 /* enable/disable decode switch */
536 unsigned char enable;
537
538 /* video info */
539 unsigned long w;
540 unsigned long h;
541 /* Y scale mode (constants defined in smacker.h)
542 0: unscaled
543 1: doubled
544 2: interlaced */
545 unsigned char y_scale_mode;
546
547 /* version ('2' or '4') */
548 unsigned char v;
549
550 /* Huffman trees */
551 unsigned long tree_size[4];
552 struct smk_huff16_t tree[4];
553
554 /* Palette data type: pointer to last-decoded-palette */
555 unsigned char palette[256][3];
556 /* Last-unpacked frame */
557 unsigned char * frame;
558 } video;
559
560 /* audio structure */
561 struct smk_audio_t {
562 /* set if track exists in file */
563 unsigned char exists;
564
565 /* enable/disable switch (per track) */
566 unsigned char enable;
567
568 /* Info */
569 unsigned char channels;
570 unsigned char bitdepth;
571 unsigned long rate;
572 long max_buffer;
573
574 /* compression type
575 0: raw PCM
576 1: SMK DPCM
577 2: Bink (Perceptual), unsupported */
578 unsigned char compress;
579
580 /* pointer to last-decoded-audio-buffer */
581 void * buffer;
582 unsigned long buffer_size;
583 } audio[7];
584 };
585
586 union smk_read_t {
587 FILE * file;
588 unsigned char * ram;
589 };
590
591 /* ************************************************************************* */
592 /* SMACKER Functions */
593 /* ************************************************************************* */
594 /* An fread wrapper: consumes N bytes, or returns -1
595 on failure (when size doesn't match expected) */
596 static char smk_read_file(void * buf, const size_t size, FILE * fp)
597 {
598 /* don't bother checking buf or fp, fread does it for us */
599 size_t bytesRead = fread(buf, 1, size, fp);
600
601 if (bytesRead != size) {
602 fprintf(stderr, "libsmacker::smk_read_file(buf,%lu,fp) - ERROR: Short read, %lu bytes returned\n", (unsigned long)size, (unsigned long)bytesRead);
603 perror("\tReason");
604 return -1;
605 }
606
607 return 0;
608 }
609
610 /* A memcpy wrapper: consumes N bytes, or returns -1
611 on failure (when size too low) */
612 static char smk_read_memory(void * buf, const unsigned long size, unsigned char ** p, unsigned long * p_size)
613 {
614 if (size > *p_size) {
615 fprintf(stderr, "libsmacker::smk_read_memory(buf,%lu,p,%lu) - ERROR: Short read\n", (unsigned long)size, (unsigned long)*p_size);
616 return -1;
617 }
618
619 memcpy(buf, *p, size);
620 *p += size;
621 *p_size -= size;
622 return 0;
623 }
624
625 /* Helper functions to do the reading, plus
626 byteswap from LE to host order */
627 /* read n bytes from (source) into ret */
628 #define smk_read(ret,n) \
629 { \
630 if (m) \
631 { \
632 r = (smk_read_file(ret,n,fp.file)); \
633 } \
634 else \
635 { \
636 r = (smk_read_memory(ret,n,&fp.ram,&size)); \
637 } \
638 if (r < 0) \
639 { \
640 fprintf(stderr,"libsmacker::smk_read(...) - Errors encountered on read, bailing out (file: %s, line: %lu)\n", __FILE__, (unsigned long)__LINE__); \
641 goto error; \
642 } \
643 }
644
645 /* Calls smk_read, but returns a ul */
646 #define smk_read_ul(p) \
647 { \
648 smk_read(buf,4); \
649 p = ((unsigned long) buf[3] << 24) | \
650 ((unsigned long) buf[2] << 16) | \
651 ((unsigned long) buf[1] << 8) | \
652 ((unsigned long) buf[0]); \
653 }
654
655 /* PUBLIC FUNCTIONS */
656 /* open an smk (from a generic Source) */
657 static smk smk_open_generic(const unsigned char m, union smk_read_t fp, unsigned long size, const unsigned char process_mode)
658 {
659 /* Smacker structure we intend to work on / return */
660 smk s;
661 /* Temporary variables */
662 long temp_l;
663 unsigned long temp_u;
664 /* r is used by macros above for return code */
665 char r;
666 unsigned char buf[4] = {'\0'};
667 /* video hufftrees are stored as a large chunk (bitstream)
668 these vars are used to load, then decode them */
669 unsigned char * hufftree_chunk = NULL;
670 unsigned long tree_size;
671 /* a bitstream struct */
672 struct smk_bit_t bs;
673
674 /** **/
675 /* safe malloc the structure */
676 if ((s = calloc(1, sizeof(struct smk_t))) == NULL) {
677 perror("libsmacker::smk_open_generic() - ERROR: failed to malloc() smk structure");
678 return NULL;
679 }
680
681 /* Check for a valid signature */
682 smk_read(buf, 3);
683
684 if (buf[0] != 'S' || buf[1] != 'M' || buf[2] != 'K') {
685 fprintf(stderr, "libsmacker::smk_open_generic - ERROR: invalid SMKn signature (got: %s)\n", buf);
686 goto error;
687 }
688
689 /* Read .smk file version */
690 smk_read(&s->video.v, 1);
691
692 if (s->video.v != '2' && s->video.v != '4') {
693 fprintf(stderr, "libsmacker::smk_open_generic - Warning: invalid SMK version %c (expected: 2 or 4)\n", s->video.v);
694
695 /* take a guess */
696 if (s->video.v < '4')
697 s->video.v = '2';
698 else
699 s->video.v = '4';
700
701 fprintf(stderr, "\tProcessing will continue as type %c\n", s->video.v);
702 }
703
704 /* width, height, total num frames */
705 smk_read_ul(s->video.w);
706 smk_read_ul(s->video.h);
707 smk_read_ul(s->f);
708 /* frames per second calculation */
709 smk_read_ul(temp_u);
710 temp_l = (int)temp_u;
711
712 if (temp_l > 0) {
713 /* millisec per frame */
714 s->usf = temp_l * 1000;
715 } else if (temp_l < 0) {
716 /* 10 microsec per frame */
717 s->usf = temp_l * -10;
718 } else {
719 /* defaults to 10 usf (= 100000 microseconds) */
720 s->usf = 100000;
721 }
722
723 /* Video flags follow.
724 Ring frame is important to libsmacker.
725 Y scale / Y interlace go in the Video flags.
726 The user should scale appropriately. */
727 smk_read_ul(temp_u);
728
729 if (temp_u & 0x01)
730 s->ring_frame = 1;
731
732 if (temp_u & 0x02)
733 s->video.y_scale_mode = SMK_FLAG_Y_DOUBLE;
734
735 if (temp_u & 0x04) {
736 if (s->video.y_scale_mode == SMK_FLAG_Y_DOUBLE)
737 fputs("libsmacker::smk_open_generic - Warning: SMK file specifies both Y-Double AND Y-Interlace.\n", stderr);
738
739 s->video.y_scale_mode = SMK_FLAG_Y_INTERLACE;
740 }
741
742 /* Max buffer size for each audio track - used to pre-allocate buffers */
743 for (temp_l = 0; temp_l < 7; temp_l ++)
744 smk_read_ul(s->audio[temp_l].max_buffer);
745
746 /* Read size of "hufftree chunk" - save for later. */
747 smk_read_ul(tree_size);
748
749 /* "unpacked" sizes of each huff tree */
750 for (temp_l = 0; temp_l < 4; temp_l ++)
751 smk_read_ul(s->video.tree_size[temp_l]);
752
753 /* read audio rate data */
754 for (temp_l = 0; temp_l < 7; temp_l ++) {
755 smk_read_ul(temp_u);
756
757 if (temp_u & 0x40000000) {
758 /* Audio track specifies "exists" flag, malloc structure and copy components. */
759 s->audio[temp_l].exists = 1;
760 /* and for all audio tracks */
761 smk_malloc(s->audio[temp_l].buffer, s->audio[temp_l].max_buffer);
762
763 if (temp_u & 0x80000000)
764 s->audio[temp_l].compress = 1;
765
766 s->audio[temp_l].bitdepth = ((temp_u & 0x20000000) ? 16 : 8);
767 s->audio[temp_l].channels = ((temp_u & 0x10000000) ? 2 : 1);
768
769 if (temp_u & 0x0c000000) {
770 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);
771 s->audio[temp_l].compress = 2;
772 }
773
774 /* Bits 25 & 24 are unused. */
775 s->audio[temp_l].rate = (temp_u & 0x00FFFFFF);
776 }
777 }
778
779 /* Skip over Dummy field */
780 smk_read_ul(temp_u);
781 /* FrameSizes and Keyframe marker are stored together. */
782 smk_malloc(s->keyframe, (s->f + s->ring_frame));
783 smk_malloc(s->chunk_size, (s->f + s->ring_frame) * sizeof(unsigned long));
784
785 for (temp_u = 0; temp_u < (s->f + s->ring_frame); temp_u ++) {
786 smk_read_ul(s->chunk_size[temp_u]);
787
788 /* Set Keyframe */
789 if (s->chunk_size[temp_u] & 0x01)
790 s->keyframe[temp_u] = 1;
791
792 /* Bits 1 is used, but the purpose is unknown. */
793 s->chunk_size[temp_u] &= 0xFFFFFFFC;
794 }
795
796 /* That was easy... Now read FrameTypes! */
797 smk_malloc(s->frame_type, (s->f + s->ring_frame));
798
799 for (temp_u = 0; temp_u < (s->f + s->ring_frame); temp_u ++)
800 smk_read(&s->frame_type[temp_u], 1);
801
802 /* HuffmanTrees
803 We know the sizes already: read and assemble into
804 something actually parse-able at run-time */
805 smk_malloc(hufftree_chunk, tree_size);
806 smk_read(hufftree_chunk, tree_size);
807 /* set up a Bitstream */
808 smk_bs_init(&bs, hufftree_chunk, tree_size);
809
810 /* create some tables */
811 for (temp_u = 0; temp_u < 4; temp_u ++) {
812 if (! smk_huff16_build(&s->video.tree[temp_u], &bs, s->video.tree_size[temp_u])) {
813 fprintf(stderr, "libsmacker::smk_open_generic - ERROR: failed to create huff16 tree %lu\n", temp_u);
814 goto error;
815 }
816 }
817
818 /* clean up */
819 smk_free(hufftree_chunk);
820 /* Go ahead and malloc storage for the video frame */
821 smk_malloc(s->video.frame, s->video.w * s->video.h);
822 /* final processing: depending on ProcessMode, handle what to do with rest of file data */
823 s->mode = process_mode;
824
825 /* Handle the rest of the data.
826 For MODE_MEMORY, read the chunks and store */
827 if (s->mode == SMK_MODE_MEMORY) {
828 smk_malloc(s->source.chunk_data, (s->f + s->ring_frame) * sizeof(unsigned char *));
829
830 for (temp_u = 0; temp_u < (s->f + s->ring_frame); temp_u ++) {
831 smk_malloc(s->source.chunk_data[temp_u], s->chunk_size[temp_u]);
832 smk_read(s->source.chunk_data[temp_u], s->chunk_size[temp_u]);
833 }
834 } else {
835 /* MODE_STREAM: don't read anything now, just precompute offsets.
836 use fseek to verify that the file is "complete" */
837 smk_malloc(s->source.file.chunk_offset, (s->f + s->ring_frame) * sizeof(unsigned long));
838
839 for (temp_u = 0; temp_u < (s->f + s->ring_frame); temp_u ++) {
840 s->source.file.chunk_offset[temp_u] = ftell(fp.file);
841
842 if (fseek(fp.file, s->chunk_size[temp_u], SEEK_CUR)) {
843 fprintf(stderr, "libsmacker::smk_open - ERROR: fseek to frame %lu not OK.\n", temp_u);
844 perror("\tError reported was");
845 goto error;
846 }
847 }
848 }
849
850 return s;
851 error:
852 smk_free(hufftree_chunk);
853 smk_close(s);
854 return NULL;
855 }
856
857 /* open an smk (from a memory buffer) */
858 smk smk_open_memory(const unsigned char * buffer, const unsigned long size)
859 {
860 smk s = NULL;
861 union smk_read_t fp;
862
863 if (buffer == NULL) {
864 fputs("libsmacker::smk_open_memory() - ERROR: buffer pointer is NULL\n", stderr);
865 return NULL;
866 }
867
868 /* set up the read union for Memory mode */
869 fp.ram = (unsigned char *)buffer;
870
871 if (!(s = smk_open_generic(0, fp, size, SMK_MODE_MEMORY)))
872 fprintf(stderr, "libsmacker::smk_open_memory(buffer,%lu) - ERROR: Fatal error in smk_open_generic, returning NULL.\n", size);
873
874 return s;
875 }
876
877 /* open an smk (from a file) */
878 smk smk_open_filepointer(FILE * file, const unsigned char mode)
879 {
880 smk s = NULL;
881 union smk_read_t fp;
882
883 if (file == NULL) {
884 fputs("libsmacker::smk_open_filepointer() - ERROR: file pointer is NULL\n", stderr);
885 return NULL;
886 }
887
888 /* Copy file ptr to internal union */
889 fp.file = file;
890
891 if (!(s = smk_open_generic(1, fp, 0, mode))) {
892 fprintf(stderr, "libsmacker::smk_open_filepointer(file,%u) - ERROR: Fatal error in smk_open_generic, returning NULL.\n", mode);
893 fclose(fp.file);
894 goto error;
895 }
896
897 if (mode == SMK_MODE_MEMORY)
898 fclose(fp.file);
899 else
900 s->source.file.fp = fp.file;
901
902 /* fall through, return s or null */
903 error:
904 return s;
905 }
906
907 /* open an smk (from a file) */
908 smk smk_open_file(const char * filename, const unsigned char mode)
909 {
910 FILE * fp;
911
912 if (filename == NULL) {
913 fputs("libsmacker::smk_open_file() - ERROR: filename is NULL\n", stderr);
914 return NULL;
915 }
916
917 if (!(fp = fopen(filename, "rb"))) {
918 fprintf(stderr, "libsmacker::smk_open_file(%s,%u) - ERROR: could not open file\n", filename, mode);
919 perror("\tError reported was");
920 goto error;
921 }
922
923 /* kick processing to smk_open_filepointer */
924 return smk_open_filepointer(fp, mode);
925 /* fall through, return s or null */
926 error:
927 return NULL;
928 }
929
930 /* close out an smk file and clean up memory */
931 void smk_close(smk s)
932 {
933 unsigned long u;
934
935 if (s == NULL) {
936 fputs("libsmacker::smk_close() - ERROR: smk is NULL\n", stderr);
937 return;
938 }
939
940 /* free video sub-components */
941 for (u = 0; u < 4; u ++) {
942 if (s->video.tree[u].tree) free(s->video.tree[u].tree);
943 }
944
945 smk_free(s->video.frame);
946
947 /* free audio sub-components */
948 for (u = 0; u < 7; u++) {
949 if (s->audio[u].buffer)
950 smk_free(s->audio[u].buffer);
951 }
952
953 smk_free(s->keyframe);
954 smk_free(s->frame_type);
955
956 if (s->mode == SMK_MODE_DISK) {
957 /* disk-mode */
958 if (s->source.file.fp)
959 fclose(s->source.file.fp);
960
961 smk_free(s->source.file.chunk_offset);
962 } else {
963 /* mem-mode */
964 if (s->source.chunk_data != NULL) {
965 for (u = 0; u < (s->f + s->ring_frame); u++)
966 smk_free(s->source.chunk_data[u]);
967
968 smk_free(s->source.chunk_data);
969 }
970 }
971
972 smk_free(s->chunk_size);
973 smk_free(s);
974 }
975
976 /* tell some info about the file */
977 char smk_info_all(const smk object, unsigned long * frame, unsigned long * frame_count, double * usf)
978 {
979 /* null check */
980 if (object == NULL) {
981 fputs("libsmacker::smk_info_all() - ERROR: smk is NULL\n", stderr);
982 return -1;
983 }
984
985 if (!frame && !frame_count && !usf) {
986 fputs("libsmacker::smk_info_all(object,frame,frame_count,usf) - ERROR: Request for info with all-NULL return references\n", stderr);
987 goto error;
988 }
989
990 if (frame)
991 *frame = (object->cur_frame % object->f);
992
993 if (frame_count)
994 *frame_count = object->f;
995
996 if (usf)
997 *usf = object->usf;
998
999 return 0;
1000 error:
1001 return -1;
1002 }
1003
1004 char smk_info_video(const smk object, unsigned long * w, unsigned long * h, unsigned char * y_scale_mode)
1005 {
1006 /* null check */
1007 if (object == NULL) {
1008 fputs("libsmacker::smk_info_video() - ERROR: smk is NULL\n", stderr);
1009 return -1;
1010 }
1011
1012 if (!w && !h && !y_scale_mode) {
1013 fputs("libsmacker::smk_info_all(object,w,h,y_scale_mode) - ERROR: Request for info with all-NULL return references\n", stderr);
1014 return -1;
1015 }
1016
1017 if (w)
1018 *w = object->video.w;
1019
1020 if (h)
1021 *h = object->video.h;
1022
1023 if (y_scale_mode)
1024 *y_scale_mode = object->video.y_scale_mode;
1025
1026 return 0;
1027 }
1028
1029 char smk_info_audio(const smk object, unsigned char * track_mask, unsigned char channels[7], unsigned char bitdepth[7], unsigned long audio_rate[7])
1030 {
1031 unsigned char i;
1032
1033 /* null check */
1034 if (object == NULL) {
1035 fputs("libsmacker::smk_info_audio() - ERROR: smk is NULL\n", stderr);
1036 return -1;
1037 }
1038
1039 if (!track_mask && !channels && !bitdepth && !audio_rate) {
1040 fputs("libsmacker::smk_info_audio(object,track_mask,channels,bitdepth,audio_rate) - ERROR: Request for info with all-NULL return references\n", stderr);
1041 return -1;
1042 }
1043
1044 if (track_mask) {
1045 *track_mask = ((object->audio[0].exists) |
1046 ((object->audio[1].exists) << 1) |
1047 ((object->audio[2].exists) << 2) |
1048 ((object->audio[3].exists) << 3) |
1049 ((object->audio[4].exists) << 4) |
1050 ((object->audio[5].exists) << 5) |
1051 ((object->audio[6].exists) << 6));
1052 }
1053
1054 if (channels) {
1055 for (i = 0; i < 7; i ++)
1056 channels[i] = object->audio[i].channels;
1057 }
1058
1059 if (bitdepth) {
1060 for (i = 0; i < 7; i ++)
1061 bitdepth[i] = object->audio[i].bitdepth;
1062 }
1063
1064 if (audio_rate) {
1065 for (i = 0; i < 7; i ++)
1066 audio_rate[i] = object->audio[i].rate;
1067 }
1068
1069 return 0;
1070 }
1071
1072 /* Enable-disable switches */
1073 char smk_enable_all(smk object, const unsigned char mask)
1074 {
1075 unsigned char i;
1076
1077 /* null check */
1078 if (object == NULL) {
1079 fputs("libsmacker::smk_enable_all() - ERROR: smk is NULL\n", stderr);
1080 return -1;
1081 }
1082
1083 /* set video-enable */
1084 object->video.enable = (mask & 0x80);
1085
1086 for (i = 0; i < 7; i ++) {
1087 if (object->audio[i].exists)
1088 object->audio[i].enable = (mask & (1 << i));
1089 }
1090
1091 return 0;
1092 }
1093
1094 char smk_enable_video(smk object, const unsigned char enable)
1095 {
1096 /* null check */
1097 if (object == NULL) {
1098 fputs("libsmacker::smk_enable_video() - ERROR: smk is NULL\n", stderr);
1099 return -1;
1100 }
1101
1102 object->video.enable = enable;
1103 return 0;
1104 }
1105
1106 char smk_enable_audio(smk object, const unsigned char track, const unsigned char enable)
1107 {
1108 /* null check */
1109 if (object == NULL) {
1110 fputs("libsmacker::smk_enable_audio() - ERROR: smk is NULL\n", stderr);
1111 return -1;
1112 }
1113
1114 object->audio[track].enable = enable;
1115 return 0;
1116 }
1117
1118 const unsigned char * smk_get_palette(const smk object)
1119 {
1120 /* null check */
1121 if (object == NULL) {
1122 fputs("libsmacker::smk_get_palette() - ERROR: smk is NULL\n", stderr);
1123 return NULL;
1124 }
1125
1126 return (unsigned char *)object->video.palette;
1127 }
1128 const unsigned char * smk_get_video(const smk object)
1129 {
1130 /* null check */
1131 if (object == NULL) {
1132 fputs("libsmacker::smk_get_video() - ERROR: smk is NULL\n", stderr);
1133 return NULL;
1134 }
1135
1136 return object->video.frame;
1137 }
1138 const unsigned char * smk_get_audio(const smk object, const unsigned char t)
1139 {
1140 /* null check */
1141 if (object == NULL) {
1142 fputs("libsmacker::smk_get_audio() - ERROR: smk is NULL\n", stderr);
1143 return NULL;
1144 }
1145
1146 return object->audio[t].buffer;
1147 }
1148 unsigned long smk_get_audio_size(const smk object, const unsigned char t)
1149 {
1150 /* null check */
1151 if (object == NULL) {
1152 fputs("libsmacker::smk_get_audio_size() - ERROR: smk is NULL\n", stderr);
1153 return 0;
1154 }
1155
1156 return object->audio[t].buffer_size;
1157 }
1158
1159 /* Decompresses a palette-frame. */
1160 static char smk_render_palette(struct smk_video_t * s, unsigned char * p, unsigned long size)
1161 {
1162 /* Index into palette */
1163 unsigned short i = 0;
1164 /* Helper variables */
1165 unsigned short count, src;
1166 static unsigned char oldPalette[256][3];
1167 /* Smacker palette map: smk colors are 6-bit, this table expands them to 8. */
1168 const unsigned char palmap[64] = {
1169 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
1170 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
1171 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
1172 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
1173 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
1174 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
1175 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
1176 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
1177 };
1178 /* null check */
1179 assert(s);
1180 assert(p);
1181 /* Copy palette to old palette */
1182 memcpy(oldPalette, s->palette, 256 * 3);
1183
1184 /* Loop until palette is complete, or we are out of bytes to process */
1185 while ((i < 256) && (size > 0)) {
1186 if ((*p) & 0x80) {
1187 /* 0x80: Skip block
1188 (preserve C+1 palette entries from previous palette) */
1189 count = ((*p) & 0x7F) + 1;
1190 p ++;
1191 size --;
1192
1193 /* check for overflow condition */
1194 if (i + count > 256) {
1195 fprintf(stderr, "libsmacker::palette_render(s,p,size) - ERROR: overflow, 0x80 attempt to skip %d entries from %d\n", count, i);
1196 goto error;
1197 }
1198
1199 /* finally: advance the index. */
1200 i += count;
1201 } else if ((*p) & 0x40) {
1202 /* 0x40: Color-shift block
1203 Copy (c + 1) color entries of the previous palette,
1204 starting from entry (s),
1205 to the next entries of the new palette. */
1206 if (size < 2) {
1207 fputs("libsmacker::palette_render(s,p,size) - ERROR: 0x40 ran out of bytes for copy\n", stderr);
1208 goto error;
1209 }
1210
1211 /* pick "count" items to copy */
1212 count = ((*p) & 0x3F) + 1;
1213 p ++;
1214 size --;
1215 /* start offset of old palette */
1216 src = *p;
1217 p ++;
1218 size --;
1219
1220 /* overflow: see if we write/read beyond 256colors, or overwrite own palette */
1221 if (i + count > 256 || src + count > 256 ||
1222 (src < i && src + count > i)) {
1223 fprintf(stderr, "libsmacker::palette_render(s,p,size) - ERROR: overflow, 0x40 attempt to copy %d entries from %d to %d\n", count, src, i);
1224 goto error;
1225 }
1226
1227 /* OK! Copy the color-palette entries. */
1228 memmove(&s->palette[i][0], &oldPalette[src][0], count * 3);
1229 i += count;
1230 } else {
1231 /* 0x00: Set Color block
1232 Direct-set the next 3 bytes for palette index */
1233 if (size < 3) {
1234 fprintf(stderr, "libsmacker::palette_render - ERROR: 0x3F ran out of bytes for copy, size=%lu\n", size);
1235 goto error;
1236 }
1237
1238 for (count = 0; count < 3; count ++) {
1239 if (*p > 0x3F) {
1240 fprintf(stderr, "libsmacker::palette_render - ERROR: palette index exceeds 0x3F (entry [%u][%u])\n", i, count);
1241 goto error;
1242 }
1243
1244 s->palette[i][count] = palmap[*p];
1245 p++;
1246 size --;
1247 }
1248
1249 i ++;
1250 }
1251 }
1252
1253 if (i < 256) {
1254 fprintf(stderr, "libsmacker::palette_render - ERROR: did not completely fill palette (idx=%u)\n", i);
1255 goto error;
1256 }
1257
1258 return 0;
1259 error:
1260 /* Error, return -1
1261 The new palette probably has errors but is preferrable to a black screen */
1262 return -1;
1263 }
1264
1265 static char smk_render_video(struct smk_video_t * s, unsigned char * p, unsigned int size)
1266 {
1267 unsigned char * t = s->frame;
1268 unsigned char s1, s2;
1269 unsigned short temp;
1270 unsigned long i, j, k, row, col, skip;
1271 /* used for video decoding */
1272 struct smk_bit_t bs;
1273 /* results from a tree lookup */
1274 int unpack;
1275 /* unpack, broken into pieces */
1276 unsigned char type;
1277 unsigned char blocklen;
1278 unsigned char typedata;
1279 char bit;
1280 const unsigned short sizetable[64] = {
1281 1, 2, 3, 4, 5, 6, 7, 8,
1282 9, 10, 11, 12, 13, 14, 15, 16,
1283 17, 18, 19, 20, 21, 22, 23, 24,
1284 25, 26, 27, 28, 29, 30, 31, 32,
1285 33, 34, 35, 36, 37, 38, 39, 40,
1286 41, 42, 43, 44, 45, 46, 47, 48,
1287 49, 50, 51, 52, 53, 54, 55, 56,
1288 57, 58, 59, 128, 256, 512, 1024, 2048
1289 };
1290 /* null check */
1291 assert(s);
1292 assert(p);
1293 row = 0;
1294 col = 0;
1295 /* Set up a bitstream for video unpacking */
1296 smk_bs_init(&bs, p, size);
1297
1298 /* Reset the cache on all bigtrees */
1299 for (i = 0; i < 4; i++)
1300 memset(&s->tree[i].cache, 0, 3 * sizeof(unsigned short));
1301
1302 while (row < s->h) {
1303 if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_TYPE], &bs)) < 0) {
1304 fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from TYPE tree.\n", stderr);
1305 return -1;
1306 }
1307
1308 type = ((unpack & 0x0003));
1309 blocklen = ((unpack & 0x00FC) >> 2);
1310 typedata = ((unpack & 0xFF00) >> 8);
1311
1312 /* support for v4 full-blocks */
1313 if (type == 1 && s->v == '4') {
1314 bit = smk_bs_read_1(&bs);
1315
1316 if (bit)
1317 type = 4;
1318 else {
1319 bit = smk_bs_read_1(&bs);
1320
1321 if (bit)
1322 type = 5;
1323 }
1324 }
1325
1326 for (j = 0; (j < sizetable[blocklen]) && (row < s->h); j ++) {
1327 skip = (row * s->w) + col;
1328
1329 switch (type) {
1330 case 0:
1331 if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_MCLR], &bs)) < 0) {
1332 fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from MCLR tree.\n", stderr);
1333 return -1;
1334 }
1335
1336 s1 = (unpack & 0xFF00) >> 8;
1337 s2 = (unpack & 0x00FF);
1338
1339 if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_MMAP], &bs)) < 0) {
1340 fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from MMAP tree.\n", stderr);
1341 return -1;
1342 }
1343
1344 temp = 0x01;
1345
1346 for (k = 0; k < 4; k ++) {
1347 for (i = 0; i < 4; i ++) {
1348 if (unpack & temp)
1349 t[skip + i] = s1;
1350 else
1351 t[skip + i] = s2;
1352
1353 temp = temp << 1;
1354 }
1355
1356 skip += s->w;
1357 }
1358
1359 break;
1360
1361 case 1: /* FULL BLOCK */
1362 for (k = 0; k < 4; k ++) {
1363 if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_FULL], &bs)) < 0) {
1364 fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderr);
1365 return -1;
1366 }
1367
1368 t[skip + 3] = ((unpack & 0xFF00) >> 8);
1369 t[skip + 2] = (unpack & 0x00FF);
1370
1371 if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_FULL], &bs)) < 0) {
1372 fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderr);
1373 return -1;
1374 }
1375
1376 t[skip + 1] = ((unpack & 0xFF00) >> 8);
1377 t[skip] = (unpack & 0x00FF);
1378 skip += s->w;
1379 }
1380
1381 break;
1382
1383 case 2: /* VOID BLOCK */
1384 /* break;
1385 if (s->frame)
1386 {
1387 memcpy(&t[skip], &s->frame[skip], 4);
1388 skip += s->w;
1389 memcpy(&t[skip], &s->frame[skip], 4);
1390 skip += s->w;
1391 memcpy(&t[skip], &s->frame[skip], 4);
1392 skip += s->w;
1393 memcpy(&t[skip], &s->frame[skip], 4);
1394 } */
1395 break;
1396
1397 case 3: /* SOLID BLOCK */
1398 memset(&t[skip], typedata, 4);
1399 skip += s->w;
1400 memset(&t[skip], typedata, 4);
1401 skip += s->w;
1402 memset(&t[skip], typedata, 4);
1403 skip += s->w;
1404 memset(&t[skip], typedata, 4);
1405 break;
1406
1407 case 4: /* V4 DOUBLE BLOCK */
1408 for (k = 0; k < 2; k ++) {
1409 if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_FULL], &bs)) < 0) {
1410 fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderr);
1411 return -1;
1412 }
1413
1414 for (i = 0; i < 2; i ++) {
1415 memset(&t[skip + 2], (unpack & 0xFF00) >> 8, 2);
1416 memset(&t[skip], (unpack & 0x00FF), 2);
1417 skip += s->w;
1418 }
1419 }
1420
1421 break;
1422
1423 case 5: /* V4 HALF BLOCK */
1424 for (k = 0; k < 2; k ++) {
1425 if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_FULL], &bs)) < 0) {
1426 fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderr);
1427 return -1;
1428 }
1429
1430 t[skip + 3] = ((unpack & 0xFF00) >> 8);
1431 t[skip + 2] = (unpack & 0x00FF);
1432 t[skip + s->w + 3] = ((unpack & 0xFF00) >> 8);
1433 t[skip + s->w + 2] = (unpack & 0x00FF);
1434
1435 if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_FULL], &bs)) < 0) {
1436 fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderr);
1437 return -1;
1438 }
1439
1440 t[skip + 1] = ((unpack & 0xFF00) >> 8);
1441 t[skip] = (unpack & 0x00FF);
1442 t[skip + s->w + 1] = ((unpack & 0xFF00) >> 8);
1443 t[skip + s->w] = (unpack & 0x00FF);
1444 skip += (s->w << 1);
1445 }
1446
1447 break;
1448 }
1449
1450 col += 4;
1451
1452 if (col >= s->w) {
1453 col = 0;
1454 row += 4;
1455 }
1456 }
1457 }
1458
1459 return 0;
1460 }
1461
1462 /* Decompress audio track i. */
1463 static char smk_render_audio(struct smk_audio_t * s, unsigned char * p, unsigned long size)
1464 {
1465 unsigned int j, k;
1466 unsigned char * t = s->buffer;
1467 struct smk_bit_t bs;
1468 char bit;
1469 short unpack, unpack2;
1470 /* used for audio decoding */
1471 struct smk_huff8_t aud_tree[4];
1472 /* null check */
1473 assert(s);
1474 assert(p);
1475
1476 if (!s->compress) {
1477 /* Raw PCM data, update buffer size and perform copy */
1478 s->buffer_size = size;
1479 memcpy(t, p, size);
1480 } else if (s->compress == 1) {
1481 /* SMACKER DPCM compression */
1482 /* need at least 4 bytes to process */
1483 if (size < 4) {
1484 fputs("libsmacker::smk_render_audio() - ERROR: need 4 bytes to get unpacked output buffer size.\n", stderr);
1485 goto error;
1486 }
1487
1488 /* chunk is compressed (huff-compressed dpcm), retrieve unpacked buffer size */
1489 s->buffer_size = ((unsigned int) p[3] << 24) |
1490 ((unsigned int) p[2] << 16) |
1491 ((unsigned int) p[1] << 8) |
1492 ((unsigned int) p[0]);
1493 p += 4;
1494 size -= 4;
1495 /* Compressed audio: must unpack here */
1496 /* Set up a bitstream */
1497 smk_bs_init(&bs, p, size);
1498 bit = smk_bs_read_1(&bs);
1499
1500 if (!bit) {
1501 fputs("libsmacker::smk_render_audio - ERROR: initial get_bit returned 0\n", stderr);
1502 goto error;
1503 }
1504
1505 bit = smk_bs_read_1(&bs);
1506
1507 if (s->channels != (bit == 1 ? 2 : 1))
1508 fputs("libsmacker::smk_render - ERROR: mono/stereo mismatch\n", stderr);
1509
1510 bit = smk_bs_read_1(&bs);
1511
1512 if (s->bitdepth != (bit == 1 ? 16 : 8))
1513 fputs("libsmacker::smk_render - ERROR: 8-/16-bit mismatch\n", stderr);
1514
1515 /* build the trees */
1516 smk_huff8_build(&aud_tree[0], &bs);
1517 j = 1;
1518 k = 1;
1519
1520 if (s->bitdepth == 16) {
1521 smk_huff8_build(&aud_tree[1], &bs);
1522 k = 2;
1523 }
1524
1525 if (s->channels == 2) {
1526 smk_huff8_build(&aud_tree[2], &bs);
1527 j = 2;
1528 k = 2;
1529
1530 if (s->bitdepth == 16) {
1531 smk_huff8_build(&aud_tree[3], &bs);
1532 k = 4;
1533 }
1534 }
1535
1536 /* read initial sound level */
1537 if (s->channels == 2) {
1538 unpack = smk_bs_read_8(&bs);
1539
1540 if (s->bitdepth == 16) {
1541 ((short *)t)[1] = smk_bs_read_8(&bs);
1542 ((short *)t)[1] |= (unpack << 8);
1543 } else
1544 ((unsigned char *)t)[1] = (unsigned char)unpack;
1545 }
1546
1547 unpack = smk_bs_read_8(&bs);
1548
1549 if (s->bitdepth == 16) {
1550 ((short *)t)[0] = smk_bs_read_8(&bs);
1551 ((short *)t)[0] |= (unpack << 8);
1552 } else
1553 ((unsigned char *)t)[0] = (unsigned char)unpack;
1554
1555 /* All set: let's read some DATA! */
1556 while (k < s->buffer_size) {
1557 if (s->bitdepth == 8) {
1558 unpack = smk_huff8_lookup(&aud_tree[0], &bs);
1559 ((unsigned char *)t)[j] = (char)unpack + ((unsigned char *)t)[j - s->channels];
1560 j ++;
1561 k++;
1562 } else {
1563 unpack = smk_huff8_lookup(&aud_tree[0], &bs);
1564 unpack2 = smk_huff8_lookup(&aud_tree[1], &bs);
1565 ((short *)t)[j] = (short)(unpack | (unpack2 << 8)) + ((short *)t)[j - s->channels];
1566 j ++;
1567 k += 2;
1568 }
1569
1570 if (s->channels == 2) {
1571 if (s->bitdepth == 8) {
1572 unpack = smk_huff8_lookup(&aud_tree[2], &bs);
1573 ((unsigned char *)t)[j] = (char)unpack + ((unsigned char *)t)[j - 2];
1574 j ++;
1575 k++;
1576 } else {
1577 unpack = smk_huff8_lookup(&aud_tree[2], &bs);
1578 unpack2 = smk_huff8_lookup(&aud_tree[3], &bs);
1579 ((short *)t)[j] = (short)(unpack | (unpack2 << 8)) + ((short *)t)[j - 2];
1580 j ++;
1581 k += 2;
1582 }
1583 }
1584 }
1585 }
1586
1587 return 0;
1588 error:
1589 return -1;
1590 }
1591
1592 /* "Renders" (unpacks) the frame at cur_frame
1593 Preps all the image and audio pointers */
1594 static char smk_render(smk s)
1595 {
1596 unsigned long i, size;
1597 unsigned char * buffer = NULL, * p, track;
1598 /* null check */
1599 assert(s);
1600
1601 /* Retrieve current chunk_size for this frame. */
1602 if (!(i = s->chunk_size[s->cur_frame])) {
1603 fprintf(stderr, "libsmacker::smk_render(s) - Warning: frame %lu: chunk_size is 0.\n", s->cur_frame);
1604 goto error;
1605 }
1606
1607 if (s->mode == SMK_MODE_DISK) {
1608 /* Skip to frame in file */
1609 if (fseek(s->source.file.fp, s->source.file.chunk_offset[s->cur_frame], SEEK_SET)) {
1610 fprintf(stderr, "libsmacker::smk_render(s) - ERROR: fseek to frame %lu (offset %lu) failed.\n", s->cur_frame, s->source.file.chunk_offset[s->cur_frame]);
1611 perror("\tError reported was");
1612 goto error;
1613 }
1614
1615 /* In disk-streaming mode: make way for our incoming chunk buffer */
1616 if ((buffer = malloc(i)) == NULL) {
1617 perror("libsmacker::smk_render() - ERROR: failed to malloc() buffer");
1618 return -1;
1619 }
1620
1621 /* Read into buffer */
1622 if (smk_read_file(buffer, s->chunk_size[s->cur_frame], s->source.file.fp) < 0) {
1623 fprintf(stderr, "libsmacker::smk_render(s) - ERROR: frame %lu (offset %lu): smk_read had errors.\n", s->cur_frame, s->source.file.chunk_offset[s->cur_frame]);
1624 goto error;
1625 }
1626 } else {
1627 /* Just point buffer at the right place */
1628 if (!s->source.chunk_data[s->cur_frame]) {
1629 fprintf(stderr, "libsmacker::smk_render(s) - ERROR: frame %lu: memory chunk is a NULL pointer.\n", s->cur_frame);
1630 goto error;
1631 }
1632
1633 buffer = s->source.chunk_data[s->cur_frame];
1634 }
1635
1636 p = buffer;
1637
1638 /* Palette record first */
1639 if (s->frame_type[s->cur_frame] & 0x01) {
1640 /* need at least 1 byte to process */
1641 if (!i) {
1642 fprintf(stderr, "libsmacker::smk_render(s) - ERROR: frame %lu: insufficient data for a palette rec.\n", s->cur_frame);
1643 goto error;
1644 }
1645
1646 /* Byte 1 in block, times 4, tells how many
1647 subsequent bytes are present */
1648 size = 4 * (*p);
1649
1650 /* If video rendering enabled, kick this off for decode. */
1651 if (s->video.enable)
1652 smk_render_palette(&(s->video), p + 1, size - 1);
1653
1654 p += size;
1655 i -= size;
1656 }
1657
1658 /* Unpack audio chunks */
1659 for (track = 0; track < 7; track ++) {
1660 if (s->frame_type[s->cur_frame] & (0x02 << track)) {
1661 /* need at least 4 byte to process */
1662 if (i < 4) {
1663 fprintf(stderr, "libsmacker::smk_render(s) - ERROR: frame %lu: insufficient data for audio[%u] rec.\n", s->cur_frame, track);
1664 goto error;
1665 }
1666
1667 /* First 4 bytes in block tell how many
1668 subsequent bytes are present */
1669 size = (((unsigned int) p[3] << 24) |
1670 ((unsigned int) p[2] << 16) |
1671 ((unsigned int) p[1] << 8) |
1672 ((unsigned int) p[0]));
1673
1674 /* If audio rendering enabled, kick this off for decode. */
1675 if (s->audio[track].enable)
1676 smk_render_audio(&s->audio[track], p + 4, size - 4);
1677
1678 p += size;
1679 i -= size;
1680 } else
1681 s->audio[track].buffer_size = 0;
1682 }
1683
1684 /* Unpack video chunk */
1685 if (s->video.enable) {
1686 if (smk_render_video(&(s->video), p, i) < 0) {
1687 fprintf(stderr, "libsmacker::smk_render(s) - ERROR: frame %lu: failed to render video.\n", s->cur_frame);
1688 goto error;
1689 }
1690 }
1691
1692 if (s->mode == SMK_MODE_DISK) {
1693 /* Remember that buffer we allocated? Trash it */
1694 smk_free(buffer);
1695 }
1696
1697 return 0;
1698 error:
1699
1700 if (s->mode == SMK_MODE_DISK) {
1701 /* Remember that buffer we allocated? Trash it */
1702 smk_free(buffer);
1703 }
1704
1705 return -1;
1706 }
1707
1708 /* rewind to first frame and unpack */
1709 char smk_first(smk s)
1710 {
1711 /* null check */
1712 if (s == NULL) {
1713 fputs("libsmacker::smk_first() - ERROR: smk is NULL\n", stderr);
1714 return -1;
1715 }
1716
1717 s->cur_frame = 0;
1718
1719 if (smk_render(s) < 0) {
1720 fprintf(stderr, "libsmacker::smk_first(s) - Warning: frame %lu: smk_render returned errors.\n", s->cur_frame);
1721 return -1;
1722 }
1723
1724 if (s->f == 1) return SMK_LAST;
1725
1726 return SMK_MORE;
1727 }
1728
1729 /* advance to next frame */
1730 char smk_next(smk s)
1731 {
1732 /* null check */
1733 if (s == NULL) {
1734 fputs("libsmacker::smk_next() - ERROR: smk is NULL\n", stderr);
1735 return -1;
1736 }
1737
1738 if (s->cur_frame + 1 < (s->f + s->ring_frame)) {
1739 s->cur_frame ++;
1740
1741 if (smk_render(s) < 0) {
1742 fprintf(stderr, "libsmacker::smk_next(s) - Warning: frame %lu: smk_render returned errors.\n", s->cur_frame);
1743 return -1;
1744 }
1745
1746 if (s->cur_frame + 1 == (s->f + s->ring_frame))
1747 return SMK_LAST;
1748
1749 return SMK_MORE;
1750 } else if (s->ring_frame) {
1751 s->cur_frame = 1;
1752
1753 if (smk_render(s) < 0) {
1754 fprintf(stderr, "libsmacker::smk_next(s) - Warning: frame %lu: smk_render returned errors.\n", s->cur_frame);
1755 return -1;
1756 }
1757
1758 if (s->cur_frame + 1 == (s->f + s->ring_frame))
1759 return SMK_LAST;
1760
1761 return SMK_MORE;
1762 }
1763
1764 return SMK_DONE;
1765 }
1766
1767 /* seek to a keyframe in an smk */
1768 char smk_seek_keyframe(smk s, unsigned long f)
1769 {
1770 /* null check */
1771 if (s == NULL) {
1772 fputs("libsmacker::smk_seek_keyframe() - ERROR: smk is NULL\n", stderr);
1773 return -1;
1774 }
1775
1776 /* rewind (or fast forward!) exactly to f */
1777 s->cur_frame = f;
1778
1779 /* roll back to previous keyframe in stream, or 0 if no keyframes exist */
1780 while (s->cur_frame > 0 && !(s->keyframe[s->cur_frame]))
1781 s->cur_frame --;
1782
1783 /* render the frame: we're ready */
1784 if (smk_render(s) < 0) {
1785 fprintf(stderr, "libsmacker::smk_seek_keyframe(s,%lu) - Warning: frame %lu: smk_render returned errors.\n", f, s->cur_frame);
1786 return -1;
1787 }
1788
1789 return 0;
1790 }