Debian Codegraph @

smacker.c

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