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"cxx . . $ `<file>/smacker.h`/
13
14 #include "smk_malloc.h"cxx . . $ `<file>/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_tcxx . . $ smk_bit_t# {
26 const unsigned char * buffercxx . . $ smk_bit_t#buffer., * endcxx . . $ smk_bit_t#end.;
27 unsigned int bit_numcxx . . $ smk_bit_t#bit_num.;
28 };
29
30 /* ************************************************************************* */
31 /* BITSTREAM Functions */
32 /* ************************************************************************* */
33 /** Initialize a bitstream wrapper */
34 static void smk_bs_initcxx . . $ smk_bs_init(7b76ca13e6b7465e).(struct smk_bit_tcxx . . $ smk_bit_t# * const bs, const unsigned char * const b, const size_t size)
35 {
36 /* null check */
37 assertcxx . . $ `/usr/include/assert.h:118:11`!(bs);
38 assertcxx . . $ `/usr/include/assert.h:118:11`!(b);
39 /* set up the pointer to bitstream start and end, and set the bit pointer to 0 */
40 bs->buffercxx . . $ smk_bit_t#buffer. = b;
41 bs->endcxx . . $ smk_bit_t#end. = b + size;
42 bs->bit_numcxx . . $ smk_bit_t#bit_num. = 0;
43 }
44
45 /* Reads a bit
46 Returns -1 if error encountered */
47 static int smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(struct smk_bit_tcxx . . $ smk_bit_t# * const bs)
48 {
49 int ret;
50 /* null check */
51 assertcxx . . $ `/usr/include/assert.h:118:11`!(bs);
52
53 /* don't die when running out of bits, but signal */
54 if (bs->buffercxx . . $ smk_bit_t#buffer. >= bs->endcxx . . $ smk_bit_t#end.) {
55 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_bs_read_1(): ERROR: bitstream exhausted.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
56 return -1;
57 }
58
59 /* get next bit and store for return */
60 ret = (*bs->buffercxx . . $ smk_bit_t#buffer. >> bs->bit_numcxx . . $ smk_bit_t#bit_num.) & 1;
61
62 /* advance to next bit */
63 if (bs->bit_numcxx . . $ smk_bit_t#bit_num. >= 7) {
64 /* Out of bits in this byte: next! */
65 bs->buffercxx . . $ smk_bit_t#buffer. ++;
66 bs->bit_numcxx . . $ smk_bit_t#bit_num. = 0;
67 } else
68 bs->bit_numcxx . . $ smk_bit_t#bit_num. ++;
69
70 return ret;
71 }
72
73 /* Reads a byte
74 Returns -1 if error. */
75 static int smk_bs_read_8cxx . . $ smk_bs_read_8(6af2747f2d15fd8b).(struct smk_bit_tcxx . . $ smk_bit_t# * const bs)
76 {
77 /* null check */
78 assertcxx . . $ `/usr/include/assert.h:118:11`!(bs);
79
80 /* don't die when running out of bits, but signal */
81 if (bs->buffercxx . . $ smk_bit_t#buffer. + (bs->bit_numcxx . . $ smk_bit_t#bit_num. > 0) >= bs->endcxx . . $ smk_bit_t#end.) {
82 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_bs_read_8(): ERROR: bitstream exhausted.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
83 return -1;
84 }
85
86 if (bs->bit_numcxx . . $ smk_bit_t#bit_num.) {
87 /* unaligned read */
88 int ret = *bs->buffercxx . . $ smk_bit_t#buffer. >> bs->bit_numcxx . . $ smk_bit_t#bit_num.;
89 bs->buffercxx . . $ smk_bit_t#buffer. ++;
90 return ret | (*bs->buffercxx . . $ smk_bit_t#buffer. << (8 - bs->bit_numcxx . . $ smk_bit_t#bit_num.) & 0xFF);
91 }
92
93 /* aligned read */
94 return *bs->buffercxx . . $ smk_bit_t#buffer.++;
95 }
96
97 /* ************************************************************************* */
98 /* HUFF8 Structure */
99 /* ************************************************************************* */
100 #define SMK_HUFF8_BRANCHcxx . . $ `smacker.c:100:9`! 0x8000
101 #define SMK_HUFF8_LEAF_MASKcxx . . $ `smacker.c:101:9`! 0x7FFF
102
103 struct smk_huff8_tcxx . . $ 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 sizecxx . . $ smk_huff8_t#size.;
108 unsigned short treecxx . . $ smk_huff8_t#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_reccxx . . $ _smk_huff8_build_rec(8adbe9b8824dfad8).(struct smk_huff8_tcxx . . $ smk_huff8_t# * const t, struct smk_bit_tcxx . . $ smk_bit_t# * const bs)
116 {
117 int bit, value;
118 assertcxx . . $ `/usr/include/assert.h:118:11`!(t);
119 assertcxx . . $ `/usr/include/assert.h:118:11`!(bs);
120
121 /* Make sure we aren't running out of bounds */
122 if (t->sizecxx . . $ smk_huff8_t#size. >= 511) {
123 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff8_build_rec() - ERROR: size exceeded\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
124 return 0;
125 }
126
127 /* Read the next bit */
128 if ((bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(bs)) < 0) {
129 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff8_build_rec() - ERROR: get_bit returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
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->sizecxx . . $ smk_huff8_t#size. ++;
141
142 /* go build the left branch */
143 if (! _smk_huff8_build_reccxx . . $ _smk_huff8_build_rec(8adbe9b8824dfad8).(t, bs)) {
144 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff8_build_rec() - ERROR: failed to build left sub-tree\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
145 return 0;
146 }
147
148 /* now go back to our current location, and
149 mark our location as a "jump" */
150 t->treecxx . . $ smk_huff8_t#tree.[value] = SMK_HUFF8_BRANCHcxx . . $ `smacker.c:100:9`! | t->sizecxx . . $ smk_huff8_t#size.;
151
152 /* continue building the right side */
153 if (! _smk_huff8_build_reccxx . . $ _smk_huff8_build_rec(8adbe9b8824dfad8).(t, bs)) {
154 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff8_build_rec() - ERROR: failed to build right sub-tree\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
155 return 0;
156 }
157 } else {
158 /* Bit unset signifies a Leaf node. */
159 /* Attempt to read value */
160 if ((value = smk_bs_read_8cxx . . $ smk_bs_read_8(6af2747f2d15fd8b).(bs)) < 0) {
161 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff8_build_rec() - ERROR: get_byte returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
162 return 0;
163 }
164
165 /* store to tree */
166 t->treecxx . . $ smk_huff8_t#tree.[t->sizecxx . . $ smk_huff8_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_buildcxx . . $ smk_huff8_build(8adbe9b8824dfad8).(struct smk_huff8_tcxx . . $ smk_huff8_t# * const t, struct smk_bit_tcxx . . $ smk_bit_t# * const bs)
176 {
177 int bit;
178 /* null check */
179 assertcxx . . $ `/usr/include/assert.h:118:11`!(t);
180 assertcxx . . $ `/usr/include/assert.h:118:11`!(bs);
181
182 /* Smacker huff trees begin with a set-bit. */
183 if ((bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(bs)) < 0) {
184 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff8_build() - ERROR: initial get_bit returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
185 return 0;
186 }
187
188 /* OK to fill out the struct now */
189 t->sizecxx . . $ smk_huff8_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_reccxx . . $ _smk_huff8_build_rec(8adbe9b8824dfad8).(t, bs)) {
195 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff8_build() - ERROR: tree build failed\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
196 return 0;
197 }
198 } else
199 t->treecxx . . $ smk_huff8_t#tree.[0] = 0;
200
201 /* huff trees end with an unset-bit */
202 if ((bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(bs)) < 0) {
203 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff8_build() - ERROR: final get_bit returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
204 return 0;
205 }
206
207 /* a 0 is expected here, a 1 generally indicates a problem! */
208 if (bit) {
209 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff8_build() - ERROR: final get_bit returned 1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
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_lookupcxx . . $ smk_huff8_lookup(5a9fcf29b5a8a256).(const struct smk_huff8_tcxx . . $ smk_huff8_t# * const t, struct smk_bit_tcxx . . $ smk_bit_t# * const bs)
219 {
220 int bit, index = 0;
221 /* null check */
222 assertcxx . . $ `/usr/include/assert.h:118:11`!(t);
223 assertcxx . . $ `/usr/include/assert.h:118:11`!(bs);
224
225 while (t->treecxx . . $ smk_huff8_t#tree.[index] & SMK_HUFF8_BRANCHcxx . . $ `smacker.c:100:9`!) {
226 if ((bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(bs)) < 0) {
227 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff8_lookup() - ERROR: get_bit returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
228 return -1;
229 }
230
231 if (bit) {
232 /* take the right branch */
233 index = t->treecxx . . $ smk_huff8_t#tree.[index] & SMK_HUFF8_LEAF_MASKcxx . . $ `smacker.c:101:9`!;
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->treecxx . . $ smk_huff8_t#tree.[index];
242 }
243
244 /* ************************************************************************* */
245 /* HUFF16 Structure */
246 /* ************************************************************************* */
247 #define SMK_HUFF16_BRANCHcxx . . $ `smacker.c:247:9`! 0x80000000
248 #define SMK_HUFF16_CACHEcxx . . $ `smacker.c:248:9`! 0x40000000
249 #define SMK_HUFF16_LEAF_MASKcxx . . $ `smacker.c:249:9`! 0x3FFFFFFF
250
251 struct smk_huff16_tcxx . . $ smk_huff16_t# {
252 unsigned int * treecxx . . $ smk_huff16_t#tree.;
253 size_t sizecxx . . $ smk_huff16_t#size.;
254
255 /* recently-used values cache */
256 unsigned short cachecxx . . $ smk_huff16_t#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_reccxx . . $ _smk_huff16_build_rec(5c81e1dc2a2c1ec2).(struct smk_huff16_tcxx . . $ smk_huff16_t# * const t, struct smk_bit_tcxx . . $ smk_bit_t# * const bs, const struct smk_huff8_tcxx . . $ smk_huff8_t# * const low8, const struct smk_huff8_tcxx . . $ smk_huff8_t# * const hi8, const size_t limit)
264 {
265 int bit, value;
266 assertcxx . . $ `/usr/include/assert.h:118:11`!(t);
267 assertcxx . . $ `/usr/include/assert.h:118:11`!(bs);
268 assertcxx . . $ `/usr/include/assert.h:118:11`!(low8);
269 assertcxx . . $ `/usr/include/assert.h:118:11`!(hi8);
270
271 /* Make sure we aren't running out of bounds */
272 if (t->sizecxx . . $ smk_huff16_t#size. >= limit) {
273 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff16_build_rec() - ERROR: size exceeded\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
274 return 0;
275 }
276
277 /* Read the first bit */
278 if ((bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(bs)) < 0) {
279 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff16_build_rec() - ERROR: get_bit returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
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->sizecxx . . $ smk_huff16_t#size. ++;
287
288 /* go build the left branch */
289 if (! _smk_huff16_build_reccxx . . $ _smk_huff16_build_rec(5c81e1dc2a2c1ec2).(t, bs, low8, hi8, limit)) {
290 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff16_build_rec() - ERROR: failed to build left sub-tree\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
291 return 0;
292 }
293
294 /* now go back to our current location, and
295 mark our location as a "jump" */
296 t->treecxx . . $ smk_huff16_t#tree.[value] = SMK_HUFF16_BRANCHcxx . . $ `smacker.c:247:9`! | t->sizecxx . . $ smk_huff16_t#size.;
297
298 /* continue building the right side */
299 if (! _smk_huff16_build_reccxx . . $ _smk_huff16_build_rec(5c81e1dc2a2c1ec2).(t, bs, low8, hi8, limit)) {
300 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff16_build_rec() - ERROR: failed to build right sub-tree\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
301 return 0;
302 }
303 } else {
304 /* Bit unset signifies a Leaf node. */
305 /* Attempt to read LOW value */
306 if ((value = smk_huff8_lookupcxx . . $ smk_huff8_lookup(5a9fcf29b5a8a256).(low8, bs)) < 0) {
307 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff16_build_rec() - ERROR: get LOW value returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
308 return 0;
309 }
310
311 t->treecxx . . $ smk_huff16_t#tree.[t->sizecxx . . $ smk_huff16_t#size.] = value;
312
313 /* now read HIGH value */
314 if ((value = smk_huff8_lookupcxx . . $ smk_huff8_lookup(5a9fcf29b5a8a256).(hi8, bs)) < 0) {
315 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::_smk_huff16_build_rec() - ERROR: get HIGH value returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
316 return 0;
317 }
318
319 /* Looks OK: we got low and hi values. Return a new LEAF */
320 t->treecxx . . $ smk_huff16_t#tree.[t->sizecxx . . $ smk_huff16_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->treecxx . . $ smk_huff16_t#tree.[t->sizecxx . . $ smk_huff16_t#size.] == t->cachecxx . . $ smk_huff16_t#cache.[0])
325 t->treecxx . . $ smk_huff16_t#tree.[t->sizecxx . . $ smk_huff16_t#size.] = SMK_HUFF16_CACHEcxx . . $ `smacker.c:248:9`!;
326 else if (t->treecxx . . $ smk_huff16_t#tree.[t->sizecxx . . $ smk_huff16_t#size.] == t->cachecxx . . $ smk_huff16_t#cache.[1])
327 t->treecxx . . $ smk_huff16_t#tree.[t->sizecxx . . $ smk_huff16_t#size.] = SMK_HUFF16_CACHEcxx . . $ `smacker.c:248:9`! | 1;
328 else if (t->treecxx . . $ smk_huff16_t#tree.[t->sizecxx . . $ smk_huff16_t#size.] == t->cachecxx . . $ smk_huff16_t#cache.[2])
329 t->treecxx . . $ smk_huff16_t#tree.[t->sizecxx . . $ smk_huff16_t#size.] = SMK_HUFF16_CACHEcxx . . $ `smacker.c:248:9`! | 2;
330
331 t->sizecxx . . $ smk_huff16_t#size. ++;
332 }
333
334 return 1;
335 }
336
337 /* Entry point for building a big 16-bit tree. */
338 static int smk_huff16_buildcxx . . $ smk_huff16_build(d170d851598e7167).(struct smk_huff16_tcxx . . $ smk_huff16_t# * const t, struct smk_bit_tcxx . . $ smk_bit_t# * const bs, const unsigned int alloc_size)
339 {
340 struct smk_huff8_tcxx . . $ smk_huff8_t# low8, hi8;
341 size_t limit;
342 int value, i, bit;
343 /* null check */
344 assertcxx . . $ `/usr/include/assert.h:118:11`!(t);
345 assertcxx . . $ `/usr/include/assert.h:118:11`!(bs);
346
347 /* Smacker huff trees begin with a set-bit. */
348 if ((bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(bs)) < 0) {
349 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff16_build() - ERROR: initial get_bit returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
350 return 0;
351 }
352
353 t->sizecxx . . $ smk_huff16_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_buildcxx . . $ smk_huff8_build(8adbe9b8824dfad8).(&low8, bs)) {
360 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff16_build() - ERROR: failed to build LOW tree\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
361 return 0;
362 }
363
364 /* build hi-8-bits tree */
365 if (! smk_huff8_buildcxx . . $ smk_huff8_build(8adbe9b8824dfad8).(&hi8, bs)) {
366 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff16_build() - ERROR: failed to build HIGH tree\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
367 return 0;
368 }
369
370 /* Init the escape code cache. */
371 for (i = 0; i < 3; i ++) {
372 if ((value = smk_bs_read_8cxx . . $ smk_bs_read_8(6af2747f2d15fd8b).(bs)) < 0) {
373 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_huff16_build() - ERROR: get LOW value for cache %d returned -1\n", i);
374 return 0;
375 }
376
377 t->cachecxx . . $ smk_huff16_t#cache.[i] = value;
378
379 /* now read HIGH value */
380 if ((value = smk_bs_read_8cxx . . $ smk_bs_read_8(6af2747f2d15fd8b).(bs)) < 0) {
381 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_huff16_build() - ERROR: get HIGH value for cache %d returned -1\n", i);
382 return 0;
383 }
384
385 t->cachecxx . . $ smk_huff16_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 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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->treecxx . . $ smk_huff16_t#tree. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(limit * sizeof(unsigned int))) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
397 perrorcxx . . $ perror(85c52e162fed56f9).("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_reccxx . . $ _smk_huff16_build_rec(5c81e1dc2a2c1ec2).(t, bs, &low8, &hi8, limit)) {
403 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff16_build() - ERROR: failed to build huff16 tree\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
404 freecxx . . $ free(5d22bdacc48458e8).(t->treecxx . . $ smk_huff16_t#tree.);
405 t->treecxx . . $ smk_huff16_t#tree. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
406 return 0;
407 }
408 } else {
409 if ((t->treecxx . . $ smk_huff16_t#tree. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(unsigned int))) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
410 perrorcxx . . $ perror(85c52e162fed56f9).("libsmacker::smk_huff16_build() - ERROR: failed to malloc() huff16 tree");
411 return 0;
412 }
413
414 t->treecxx . . $ smk_huff16_t#tree.[0] = 0;
•415 t->cachecxx . . $ smk_huff16_t#cache.[0] = t->cachecxx . . $ smk_huff16_t#cache.[1] = t->cachecxx . . $ smk_huff16_t#cache.[2] = 0;
416 return 1;
417 }
418
419 /* Check final end tag. */
420 if ((bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(bs)) < 0) {
421 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff16_build() - ERROR: final get_bit returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
422 freecxx . . $ free(5d22bdacc48458e8).(t->treecxx . . $ smk_huff16_t#tree.);
423 t->treecxx . . $ smk_huff16_t#tree. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
424 return 0;
425 }
426
427 /* a 0 is expected here, a 1 generally indicates a problem! */
428 if (bit) {
429 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff16_build() - ERROR: final get_bit returned 1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
430 freecxx . . $ free(5d22bdacc48458e8).(t->treecxx . . $ smk_huff16_t#tree.);
431 t->treecxx . . $ smk_huff16_t#tree. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
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_lookupcxx . . $ smk_huff16_lookup(e57c26c0262c4237).(struct smk_huff16_tcxx . . $ smk_huff16_t# * const t, struct smk_bit_tcxx . . $ smk_bit_t# * const bs)
442 {
443 int bit, value, index = 0;
444 /* null check */
445 assertcxx . . $ `/usr/include/assert.h:118:11`!(t);
446 assertcxx . . $ `/usr/include/assert.h:118:11`!(bs);
447
448 while (t->treecxx . . $ smk_huff16_t#tree.[index] & SMK_HUFF16_BRANCHcxx . . $ `smacker.c:247:9`!) {
449 if ((bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(bs)) < 0) {
450 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_huff16_lookup() - ERROR: get_bit returned -1\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
451 return -1;
452 }
453
454 if (bit) {
455 /* take the right branch */
456 index = t->treecxx . . $ smk_huff16_t#tree.[index] & SMK_HUFF16_LEAF_MASKcxx . . $ `smacker.c:249:9`!;
457 } else {
458 /* take the left branch */
459 index ++;
460 }
461 }
462
463 /* Get the value at this point */
464 value = t->treecxx . . $ smk_huff16_t#tree.[index];
465
466 if (value & SMK_HUFF16_CACHEcxx . . $ `smacker.c:248:9`!) {
467 /* uses cached value instead of actual value */
468 value = t->cachecxx . . $ smk_huff16_t#cache.[value & SMK_HUFF16_LEAF_MASKcxx . . $ `smacker.c:249:9`!];
469 }
470
471 if (t->cachecxx . . $ smk_huff16_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->cachecxx . . $ smk_huff16_t#cache.[2] = t->cachecxx . . $ smk_huff16_t#cache.[1];
475 t->cachecxx . . $ smk_huff16_t#cache.[1] = t->cachecxx . . $ smk_huff16_t#cache.[0];
476 t->cachecxx . . $ smk_huff16_t#cache.[0] = value;
477 }
478
479 return value;
480 }
481
482 /* ************************************************************************* */
483 /* SMACKER Structure */
484 /* ************************************************************************* */
485 /* tree processing order */
486 #define SMK_TREE_MMAPcxx . . $ `smacker.c:486:9`! 0
487 #define SMK_TREE_MCLRcxx . . $ `smacker.c:487:9`! 1
488 #define SMK_TREE_FULLcxx . . $ `smacker.c:488:9`! 2
489 #define SMK_TREE_TYPEcxx . . $ `smacker.c:489:9`! 3
490
491 struct smk_tcxx . . $ smk_t# {
492 /* meta-info */
493 /* file mode: see flags, smacker.h */
494 unsigned char modecxx . . $ smk_t#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 usfcxx . . $ smk_t#usf.;
499
500 /* total frames */
501 unsigned long fcxx . . $ smk_t#f.;
502 /* does file have a ring frame? (in other words, does file loop?) */
503 unsigned char ring_framecxx . . $ smk_t#ring_frame.;
504
505 /* Index of current frame */
506 unsigned long cur_framecxx . . $ smk_t#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 unioncxx . . $ smk_t#$anonymous_type_0# {
512 structcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1# {
513 /* on-disk mode */
514 FILEcxx . . $ FILE# * fpcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#fp.;
515 unsigned long * chunk_offsetcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#chunk_offset.;
516 } filecxx . . $ smk_t#$anonymous_type_0#file.;
517
518 /* in-memory mode: unprocessed chunks */
519 unsigned char ** chunk_datacxx . . $ smk_t#$anonymous_type_0#chunk_data.;
520 } sourcecxx . . $ smk_t#source.;
521
522 /* shared array of "chunk sizes"*/
523 unsigned long * chunk_sizecxx . . $ smk_t#chunk_size.;
524
525 /* Holds per-frame flags (i.e. 'keyframe') */
526 unsigned char * keyframecxx . . $ smk_t#keyframe.;
527 /* Holds per-frame type mask (e.g. 'audio track 3, 2, and palette swap') */
528 unsigned char * frame_typecxx . . $ smk_t#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_tcxx . . $ smk_video_t# {
535 /* enable/disable decode switch */
536 unsigned char enablecxx . . $ smk_video_t#enable.;
537
538 /* video info */
539 unsigned long wcxx . . $ smk_video_t#w.;
540 unsigned long hcxx . . $ smk_video_t#h.;
541 /* Y scale mode (constants defined in smacker.h)
542 0: unscaled
543 1: doubled
544 2: interlaced */
545 unsigned char y_scale_modecxx . . $ smk_video_t#y_scale_mode.;
546
547 /* version ('2' or '4') */
548 unsigned char vcxx . . $ smk_video_t#v.;
549
550 /* Huffman trees */
551 unsigned long tree_sizecxx . . $ smk_video_t#tree_size.[4];
552 struct smk_huff16_tcxx . . $ smk_huff16_t# treecxx . . $ smk_video_t#tree.[4];
553
554 /* Palette data type: pointer to last-decoded-palette */
555 unsigned char palettecxx . . $ smk_video_t#palette.[256][3];
556 /* Last-unpacked frame */
557 unsigned char * framecxx . . $ smk_video_t#frame.;
558 } videocxx . . $ smk_t#video.;
559
560 /* audio structure */
561 struct smk_audio_tcxx . . $ smk_audio_t# {
562 /* set if track exists in file */
563 unsigned char existscxx . . $ smk_audio_t#exists.;
564
565 /* enable/disable switch (per track) */
566 unsigned char enablecxx . . $ smk_audio_t#enable.;
567
568 /* Info */
569 unsigned char channelscxx . . $ smk_audio_t#channels.;
570 unsigned char bitdepthcxx . . $ smk_audio_t#bitdepth.;
571 unsigned long ratecxx . . $ smk_audio_t#rate.;
572 long max_buffercxx . . $ smk_audio_t#max_buffer.;
573
574 /* compression type
575 0: raw PCM
576 1: SMK DPCM
577 2: Bink (Perceptual), unsupported */
578 unsigned char compresscxx . . $ smk_audio_t#compress.;
579
580 /* pointer to last-decoded-audio-buffer */
581 void * buffercxx . . $ smk_audio_t#buffer.;
582 unsigned long buffer_sizecxx . . $ smk_audio_t#buffer_size.;
583 } audiocxx . . $ smk_t#audio.[7];
584 };
585
586 union smk_read_tcxx . . $ smk_read_t# {
587 FILEcxx . . $ FILE# * filecxx . . $ smk_read_t#file.;
588 unsigned char * ramcxx . . $ smk_read_t#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_filecxx . . $ smk_read_file(3b852f5abebdc535).(void * buf, const size_t size, FILEcxx . . $ FILE# * fp)
597 {
598 /* don't bother checking buf or fp, fread does it for us */
599 size_t bytesRead = freadcxx . . $ fread(738fc8ac1d711e58).(buf, 1, size, fp);
600
601 if (bytesRead != size) {
602 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_read_file(buf,%lu,fp) - ERROR: Short read, %lu bytes returned\n", (unsigned long)size, (unsigned long)bytesRead);
603 perrorcxx . . $ perror(85c52e162fed56f9).("\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_memorycxx . . $ smk_read_memory(4be6050485ec5b28).(void * buf, const unsigned long size, unsigned char ** p, unsigned long * p_size)
613 {
614 if (size > *p_size) {
615 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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 memcpycxx . . $ memcpy(cc817cb175f282d).(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_readcxx . . $ `smacker.c:628:9`!(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(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,"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_ulcxx . . $ `smacker.c:646:9`!(p) \
647 { \
648 smk_readcxx . . $ `smacker.c:628:9`!(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 smkcxx . . $ smk# smk_open_genericcxx . . $ smk_open_generic(3379a7cbb8dd880e).(const unsigned char m, union smk_read_tcxx . . $ smk_read_t# fp, unsigned long size, const unsigned char process_mode)
658 {
659 /* Smacker structure we intend to work on / return */
660 smkcxx . . $ 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 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
670 unsigned long tree_size;
671 /* a bitstream struct */
672 struct smk_bit_tcxx . . $ smk_bit_t# bs;
673
674 /** **/
675 /* safe malloc the structure */
676 if ((s = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(struct smk_tcxx . . $ smk_t#))) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
677 perrorcxx . . $ perror(85c52e162fed56f9).("libsmacker::smk_open_generic() - ERROR: failed to malloc() smk structure");
678 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
679 }
680
681 /* Check for a valid signature */
682 smk_readcxx . . $ `smacker.c:628:9`!(buf, 3);
683
684 if (buf[0] != 'S' || buf[1] != 'M' || buf[2] != 'K') {
685 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_open_generic - ERROR: invalid SMKn signature (got: %s)\n", buf);
686 goto error;
687 }
688
689 /* Read .smk file version */
690 smk_readcxx . . $ `smacker.c:628:9`!(&s->video.v, 1);
691
692 if (s->videocxx . . $ smk_t#video..vcxx . . $ smk_video_t#v. != '2' && s->videocxx . . $ smk_t#video..vcxx . . $ smk_video_t#v. != '4') {
693 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_open_generic - Warning: invalid SMK version %c (expected: 2 or 4)\n", s->videocxx . . $ smk_t#video..vcxx . . $ smk_video_t#v.);
694
695 /* take a guess */
696 if (s->videocxx . . $ smk_t#video..vcxx . . $ smk_video_t#v. < '4')
697 s->videocxx . . $ smk_t#video..vcxx . . $ smk_video_t#v. = '2';
698 else
699 s->videocxx . . $ smk_t#video..vcxx . . $ smk_video_t#v. = '4';
700
701 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\tProcessing will continue as type %c\n", s->videocxx . . $ smk_t#video..vcxx . . $ smk_video_t#v.);
702 }
703
704 /* width, height, total num frames */
705 smk_read_ulcxx . . $ `smacker.c:646:9`!(s->video.w);
706 smk_read_ulcxx . . $ `smacker.c:646:9`!(s->video.h);
707 smk_read_ulcxx . . $ `smacker.c:646:9`!(s->f);
708 /* frames per second calculation */
709 smk_read_ulcxx . . $ `smacker.c:646:9`!(temp_u);
710 temp_l = (int)temp_u;
711
712 if (temp_l > 0) {
713 /* millisec per frame */
714 s->usfcxx . . $ smk_t#usf. = temp_l * 1000;
715 } else if (temp_l < 0) {
716 /* 10 microsec per frame */
717 s->usfcxx . . $ smk_t#usf. = temp_l * -10;
718 } else {
719 /* defaults to 10 usf (= 100000 microseconds) */
720 s->usfcxx . . $ smk_t#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_ulcxx . . $ `smacker.c:646:9`!(temp_u);
728
729 if (temp_u & 0x01)
730 s->ring_framecxx . . $ smk_t#ring_frame. = 1;
731
732 if (temp_u & 0x02)
733 s->videocxx . . $ smk_t#video..y_scale_modecxx . . $ smk_video_t#y_scale_mode. = SMK_FLAG_Y_DOUBLEcxx . . $ `smacker.h:44:9`!;
734
735 if (temp_u & 0x04) {
736 if (s->videocxx . . $ smk_t#video..y_scale_modecxx . . $ smk_video_t#y_scale_mode. == SMK_FLAG_Y_DOUBLEcxx . . $ `smacker.h:44:9`!)
737 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_open_generic - Warning: SMK file specifies both Y-Double AND Y-Interlace.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
738
739 s->videocxx . . $ smk_t#video..y_scale_modecxx . . $ smk_video_t#y_scale_mode. = SMK_FLAG_Y_INTERLACEcxx . . $ `smacker.h:43:9`!;
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_ulcxx . . $ `smacker.c:646:9`!(s->audio[temp_l].max_buffer);
745
746 /* Read size of "hufftree chunk" - save for later. */
747 smk_read_ulcxx . . $ `smacker.c:646:9`!(tree_size);
748
749 /* "unpacked" sizes of each huff tree */
750 for (temp_l = 0; temp_l < 4; temp_l ++)
751 smk_read_ulcxx . . $ `smacker.c:646:9`!(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_ulcxx . . $ `smacker.c:646:9`!(temp_u);
756
757 if (temp_u & 0x40000000) {
758 /* Audio track specifies "exists" flag, malloc structure and copy components. */
759 s->audiocxx . . $ smk_t#audio.[temp_l].existscxx . . $ smk_audio_t#exists. = 1;
760 /* and for all audio tracks */
761 smk_malloccxx . . $ `smk_malloc.h:44:9`!(s->audio[temp_l].buffer, s->audio[temp_l].max_buffer);
762
763 if (temp_u & 0x80000000)
764 s->audiocxx . . $ smk_t#audio.[temp_l].compresscxx . . $ smk_audio_t#compress. = 1;
765
766 s->audiocxx . . $ smk_t#audio.[temp_l].bitdepthcxx . . $ smk_audio_t#bitdepth. = ((temp_u & 0x20000000) ? 16 : 8);
767 s->audiocxx . . $ smk_t#audio.[temp_l].channelscxx . . $ smk_audio_t#channels. = ((temp_u & 0x10000000) ? 2 : 1);
768
769 if (temp_u & 0x0c000000) {
770 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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->audiocxx . . $ smk_t#audio.[temp_l].compresscxx . . $ smk_audio_t#compress. = 2;
772 }
773
774 /* Bits 25 & 24 are unused. */
775 s->audiocxx . . $ smk_t#audio.[temp_l].ratecxx . . $ smk_audio_t#rate. = (temp_u & 0x00FFFFFF);
776 }
777 }
778
779 /* Skip over Dummy field */
780 smk_read_ulcxx . . $ `smacker.c:646:9`!(temp_u);
781 /* FrameSizes and Keyframe marker are stored together. */
782 smk_malloccxx . . $ `smk_malloc.h:44:9`!(s->keyframe, (s->f + s->ring_frame));
783 smk_malloccxx . . $ `smk_malloc.h:44:9`!(s->chunk_size, (s->f + s->ring_frame) * sizeof(unsigned long));
784
785 for (temp_u = 0; temp_u < (s->fcxx . . $ smk_t#f. + s->ring_framecxx . . $ smk_t#ring_frame.); temp_u ++) {
786 smk_read_ulcxx . . $ `smacker.c:646:9`!(s->chunk_size[temp_u]);
787
788 /* Set Keyframe */
789 if (s->chunk_sizecxx . . $ smk_t#chunk_size.[temp_u] & 0x01)
790 s->keyframecxx . . $ smk_t#keyframe.[temp_u] = 1;
791
792 /* Bits 1 is used, but the purpose is unknown. */
793 s->chunk_sizecxx . . $ smk_t#chunk_size.[temp_u] &= 0xFFFFFFFC;
794 }
795
796 /* That was easy... Now read FrameTypes! */
797 smk_malloccxx . . $ `smk_malloc.h:44:9`!(s->frame_type, (s->f + s->ring_frame));
798
799 for (temp_u = 0; temp_u < (s->fcxx . . $ smk_t#f. + s->ring_framecxx . . $ smk_t#ring_frame.); temp_u ++)
800 smk_readcxx . . $ `smacker.c:628:9`!(&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_malloccxx . . $ `smk_malloc.h:44:9`!(hufftree_chunk, tree_size);
806 smk_readcxx . . $ `smacker.c:628:9`!(hufftree_chunk, tree_size);
807 /* set up a Bitstream */
808 smk_bs_initcxx . . $ smk_bs_init(7b76ca13e6b7465e).(&bs, hufftree_chunk, tree_size);
809
810 /* create some tables */
811 for (temp_u = 0; temp_u < 4; temp_u ++) {
812 if (! smk_huff16_buildcxx . . $ smk_huff16_build(d170d851598e7167).(&s->videocxx . . $ smk_t#video..treecxx . . $ smk_video_t#tree.[temp_u], &bs, s->videocxx . . $ smk_t#video..tree_sizecxx . . $ smk_video_t#tree_size.[temp_u])) {
813 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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_freecxx . . $ `smk_malloc.h:30:9`!(hufftree_chunk);
820 /* Go ahead and malloc storage for the video frame */
821 smk_malloccxx . . $ `smk_malloc.h:44:9`!(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->modecxx . . $ smk_t#mode. = process_mode;
824
825 /* Handle the rest of the data.
826 For MODE_MEMORY, read the chunks and store */
827 if (s->modecxx . . $ smk_t#mode. == SMK_MODE_MEMORYcxx . . $ `smacker.h:39:9`!) {
828 smk_malloccxx . . $ `smk_malloc.h:44:9`!(s->source.chunk_data, (s->f + s->ring_frame) * sizeof(unsigned char *));
829
830 for (temp_u = 0; temp_u < (s->fcxx . . $ smk_t#f. + s->ring_framecxx . . $ smk_t#ring_frame.); temp_u ++) {
831 smk_malloccxx . . $ `smk_malloc.h:44:9`!(s->source.chunk_data[temp_u], s->chunk_size[temp_u]);
832 smk_readcxx . . $ `smacker.c:628:9`!(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_malloccxx . . $ `smk_malloc.h:44:9`!(s->source.file.chunk_offset, (s->f + s->ring_frame) * sizeof(unsigned long));
838
839 for (temp_u = 0; temp_u < (s->fcxx . . $ smk_t#f. + s->ring_framecxx . . $ smk_t#ring_frame.); temp_u ++) {
840 s->sourcecxx . . $ smk_t#source..filecxx . . $ smk_t#$anonymous_type_0#file..chunk_offsetcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#chunk_offset.[temp_u] = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(fp.filecxx . . $ smk_read_t#file.);
841
842 if (fseekcxx . . $ fseek(543fae30d249f6b1).(fp.filecxx . . $ smk_read_t#file., s->chunk_sizecxx . . $ smk_t#chunk_size.[temp_u], SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!)) {
843 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_open - ERROR: fseek to frame %lu not OK.\n", temp_u);
844 perrorcxx . . $ perror(85c52e162fed56f9).("\tError reported was");
845 goto error;
846 }
847 }
848 }
849
850 return s;
851 error:
852 smk_freecxx . . $ `smk_malloc.h:30:9`!(hufftree_chunk);
853 smk_closecxx . . $ smk_close(f506fae55897243).(s);
854 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
855 }
856
857 /* open an smk (from a memory buffer) */
858 smkcxx . . $ smk# smk_open_memorycxx . . $ smk_open_memory(2b7a6187c9c128d1).(const unsigned char * buffer, const unsigned long size)
859 {
860 smkcxx . . $ smk# s = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
861 union smk_read_tcxx . . $ smk_read_t# fp;
862
863 if (buffer == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
864 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_open_memory() - ERROR: buffer pointer is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
865 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
866 }
867
868 /* set up the read union for Memory mode */
869 fp.ramcxx . . $ smk_read_t#ram. = (unsigned char *)buffer;
870
871 if (!(s = smk_open_genericcxx . . $ smk_open_generic(3379a7cbb8dd880e).(0, fp, size, SMK_MODE_MEMORYcxx . . $ `smacker.h:39:9`!)))
872 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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 smkcxx . . $ smk# smk_open_filepointercxx . . $ smk_open_filepointer(9658e6194547add1).(FILEcxx . . $ FILE# * file, const unsigned char mode)
879 {
880 smkcxx . . $ smk# s = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
881 union smk_read_tcxx . . $ smk_read_t# fp;
882
883 if (file == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
884 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_open_filepointer() - ERROR: file pointer is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
885 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
886 }
887
888 /* Copy file ptr to internal union */
889 fp.filecxx . . $ smk_read_t#file. = file;
890
891 if (!(s = smk_open_genericcxx . . $ smk_open_generic(3379a7cbb8dd880e).(1, fp, 0, mode))) {
892 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_open_filepointer(file,%u) - ERROR: Fatal error in smk_open_generic, returning NULL.\n", mode);
893 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp.filecxx . . $ smk_read_t#file.);
894 goto error;
895 }
896
897 if (mode == SMK_MODE_MEMORYcxx . . $ `smacker.h:39:9`!)
898 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp.filecxx . . $ smk_read_t#file.);
899 else
900 s->sourcecxx . . $ smk_t#source..filecxx . . $ smk_t#$anonymous_type_0#file..fpcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#fp. = fp.filecxx . . $ smk_read_t#file.;
901
902 /* fall through, return s or null */
903 error:
904 return s;
905 }
906
907 /* open an smk (from a file) */
908 smkcxx . . $ smk# smk_open_filecxx . . $ smk_open_file(3e2900f595eb42e9).(const char * filename, const unsigned char mode)
909 {
910 FILEcxx . . $ FILE# * fp;
911
912 if (filename == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
913 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_open_file() - ERROR: filename is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
914 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
915 }
916
917 if (!(fp = fopencxx . . $ fopen(82ec0787cd37e0a3).(filename, "rb"))) {
918 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_open_file(%s,%u) - ERROR: could not open file\n", filename, mode);
919 perrorcxx . . $ perror(85c52e162fed56f9).("\tError reported was");
920 goto error;
921 }
922
923 /* kick processing to smk_open_filepointer */
924 return smk_open_filepointercxx . . $ smk_open_filepointer(9658e6194547add1).(fp, mode);
925 /* fall through, return s or null */
926 error:
927 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
928 }
929
930 /* close out an smk file and clean up memory */
931 void smk_closecxx . . $ smk_close(f506fae55897243).(smkcxx . . $ smk# s)
932 {
933 unsigned long u;
934
935 if (s == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
936 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_close() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
937 return;
938 }
939
940 /* free video sub-components */
941 for (u = 0; u < 4; u ++) {
942 if (s->videocxx . . $ smk_t#video..treecxx . . $ smk_video_t#tree.[u].treecxx . . $ smk_huff16_t#tree.) freecxx . . $ free(5d22bdacc48458e8).(s->videocxx . . $ smk_t#video..treecxx . . $ smk_video_t#tree.[u].treecxx . . $ smk_huff16_t#tree.);
943 }
944
945 smk_freecxx . . $ `smk_malloc.h:30:9`!(s->video.frame);
946
947 /* free audio sub-components */
948 for (u = 0; u < 7; u++) {
949 if (s->audiocxx . . $ smk_t#audio.[u].buffercxx . . $ smk_audio_t#buffer.)
950 smk_freecxx . . $ `smk_malloc.h:30:9`!(s->audio[u].buffer);
951 }
952
953 smk_freecxx . . $ `smk_malloc.h:30:9`!(s->keyframe);
954 smk_freecxx . . $ `smk_malloc.h:30:9`!(s->frame_type);
955
956 if (s->modecxx . . $ smk_t#mode. == SMK_MODE_DISKcxx . . $ `smacker.h:38:9`!) {
957 /* disk-mode */
958 if (s->sourcecxx . . $ smk_t#source..filecxx . . $ smk_t#$anonymous_type_0#file..fpcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#fp.)
959 fclosecxx . . $ fclose(89bc1efcdac0ca22).(s->sourcecxx . . $ smk_t#source..filecxx . . $ smk_t#$anonymous_type_0#file..fpcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#fp.);
960
961 smk_freecxx . . $ `smk_malloc.h:30:9`!(s->source.file.chunk_offset);
962 } else {
963 /* mem-mode */
964 if (s->sourcecxx . . $ smk_t#source..chunk_datacxx . . $ smk_t#$anonymous_type_0#chunk_data. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
965 for (u = 0; u < (s->fcxx . . $ smk_t#f. + s->ring_framecxx . . $ smk_t#ring_frame.); u++)
966 smk_freecxx . . $ `smk_malloc.h:30:9`!(s->source.chunk_data[u]);
967
968 smk_freecxx . . $ `smk_malloc.h:30:9`!(s->source.chunk_data);
969 }
970 }
971
972 smk_freecxx . . $ `smk_malloc.h:30:9`!(s->chunk_size);
973 smk_freecxx . . $ `smk_malloc.h:30:9`!(s);
974 }
975
976 /* tell some info about the file */
977 char smk_info_allcxx . . $ smk_info_all(e2648e085ed0bb80).(const smkcxx . . $ smk# object, unsigned long * frame, unsigned long * frame_count, double * usf)
978 {
979 /* null check */
980 if (object == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
981 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_info_all() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
982 return -1;
983 }
984
985 if (!frame && !frame_count && !usf) {
986 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_info_all(object,frame,frame_count,usf) - ERROR: Request for info with all-NULL return references\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
987 goto error;
988 }
989
990 if (frame)
991 *frame = (object->cur_framecxx . . $ smk_t#cur_frame. % object->fcxx . . $ smk_t#f.);
992
993 if (frame_count)
994 *frame_count = object->fcxx . . $ smk_t#f.;
995
996 if (usf)
997 *usf = object->usfcxx . . $ smk_t#usf.;
998
999 return 0;
1000 error:
1001 return -1;
1002 }
1003
1004 char smk_info_videocxx . . $ smk_info_video(bae4fa047bfa0863).(const smkcxx . . $ smk# object, unsigned long * w, unsigned long * h, unsigned char * y_scale_mode)
1005 {
1006 /* null check */
1007 if (object == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1008 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_info_video() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1009 return -1;
1010 }
1011
1012 if (!w && !h && !y_scale_mode) {
1013 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_info_all(object,w,h,y_scale_mode) - ERROR: Request for info with all-NULL return references\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1014 return -1;
1015 }
1016
1017 if (w)
1018 *w = object->videocxx . . $ smk_t#video..wcxx . . $ smk_video_t#w.;
1019
1020 if (h)
1021 *h = object->videocxx . . $ smk_t#video..hcxx . . $ smk_video_t#h.;
1022
1023 if (y_scale_mode)
1024 *y_scale_mode = object->videocxx . . $ smk_t#video..y_scale_modecxx . . $ smk_video_t#y_scale_mode.;
1025
1026 return 0;
1027 }
1028
1029 char smk_info_audiocxx . . $ smk_info_audio(be5882a7f08fd6d2).(const smkcxx . . $ 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 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1035 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_info_audio() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1036 return -1;
1037 }
1038
1039 if (!track_mask && !channels && !bitdepth && !audio_rate) {
1040 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_info_audio(object,track_mask,channels,bitdepth,audio_rate) - ERROR: Request for info with all-NULL return references\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1041 return -1;
1042 }
1043
1044 if (track_mask) {
1045 *track_mask = ((object->audiocxx . . $ smk_t#audio.[0].existscxx . . $ smk_audio_t#exists.) |
1046 ((object->audiocxx . . $ smk_t#audio.[1].existscxx . . $ smk_audio_t#exists.) << 1) |
1047 ((object->audiocxx . . $ smk_t#audio.[2].existscxx . . $ smk_audio_t#exists.) << 2) |
1048 ((object->audiocxx . . $ smk_t#audio.[3].existscxx . . $ smk_audio_t#exists.) << 3) |
1049 ((object->audiocxx . . $ smk_t#audio.[4].existscxx . . $ smk_audio_t#exists.) << 4) |
1050 ((object->audiocxx . . $ smk_t#audio.[5].existscxx . . $ smk_audio_t#exists.) << 5) |
1051 ((object->audiocxx . . $ smk_t#audio.[6].existscxx . . $ smk_audio_t#exists.) << 6));
1052 }
1053
1054 if (channels) {
1055 for (i = 0; i < 7; i ++)
1056 channels[i] = object->audiocxx . . $ smk_t#audio.[i].channelscxx . . $ smk_audio_t#channels.;
1057 }
1058
1059 if (bitdepth) {
1060 for (i = 0; i < 7; i ++)
1061 bitdepth[i] = object->audiocxx . . $ smk_t#audio.[i].bitdepthcxx . . $ smk_audio_t#bitdepth.;
1062 }
1063
1064 if (audio_rate) {
1065 for (i = 0; i < 7; i ++)
1066 audio_rate[i] = object->audiocxx . . $ smk_t#audio.[i].ratecxx . . $ smk_audio_t#rate.;
1067 }
1068
1069 return 0;
1070 }
1071
1072 /* Enable-disable switches */
1073 char smk_enable_allcxx . . $ smk_enable_all(abc8d3d265e238d).(smkcxx . . $ smk# object, const unsigned char mask)
1074 {
1075 unsigned char i;
1076
1077 /* null check */
1078 if (object == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1079 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_enable_all() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1080 return -1;
1081 }
1082
1083 /* set video-enable */
1084 object->videocxx . . $ smk_t#video..enablecxx . . $ smk_video_t#enable. = (mask & 0x80);
1085
1086 for (i = 0; i < 7; i ++) {
1087 if (object->audiocxx . . $ smk_t#audio.[i].existscxx . . $ smk_audio_t#exists.)
1088 object->audiocxx . . $ smk_t#audio.[i].enablecxx . . $ smk_audio_t#enable. = (mask & (1 << i));
1089 }
1090
1091 return 0;
1092 }
1093
1094 char smk_enable_videocxx . . $ smk_enable_video(abc8d3d265e238d).(smkcxx . . $ smk# object, const unsigned char enable)
1095 {
1096 /* null check */
1097 if (object == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1098 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_enable_video() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1099 return -1;
1100 }
1101
1102 object->videocxx . . $ smk_t#video..enablecxx . . $ smk_video_t#enable. = enable;
1103 return 0;
1104 }
1105
1106 char smk_enable_audiocxx . . $ smk_enable_audio(bc4f6cdd6576b964).(smkcxx . . $ smk# object, const unsigned char track, const unsigned char enable)
1107 {
1108 /* null check */
1109 if (object == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1110 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_enable_audio() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1111 return -1;
1112 }
1113
1114 object->audiocxx . . $ smk_t#audio.[track].enablecxx . . $ smk_audio_t#enable. = enable;
1115 return 0;
1116 }
1117
1118 const unsigned char * smk_get_palettecxx . . $ smk_get_palette(1c64b82bd47d1f67).(const smkcxx . . $ smk# object)
1119 {
1120 /* null check */
1121 if (object == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1122 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_get_palette() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1123 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
1124 }
1125
1126 return (unsigned char *)object->videocxx . . $ smk_t#video..palettecxx . . $ smk_video_t#palette.;
1127 }
1128 const unsigned char * smk_get_videocxx . . $ smk_get_video(1c64b82bd47d1f67).(const smkcxx . . $ smk# object)
1129 {
1130 /* null check */
1131 if (object == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1132 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_get_video() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1133 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
1134 }
1135
1136 return object->videocxx . . $ smk_t#video..framecxx . . $ smk_video_t#frame.;
1137 }
1138 const unsigned char * smk_get_audiocxx . . $ smk_get_audio(eb186ae2e68619b8).(const smkcxx . . $ smk# object, const unsigned char t)
1139 {
1140 /* null check */
1141 if (object == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1142 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_get_audio() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1143 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
1144 }
1145
1146 return object->audiocxx . . $ smk_t#audio.[t].buffercxx . . $ smk_audio_t#buffer.;
1147 }
1148 unsigned long smk_get_audio_sizecxx . . $ smk_get_audio_size(9bfa82a5f631f69e).(const smkcxx . . $ smk# object, const unsigned char t)
1149 {
1150 /* null check */
1151 if (object == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1152 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_get_audio_size() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1153 return 0;
1154 }
1155
1156 return object->audiocxx . . $ smk_t#audio.[t].buffer_sizecxx . . $ smk_audio_t#buffer_size.;
1157 }
1158
1159 /* Decompresses a palette-frame. */
1160 static char smk_render_palettecxx . . $ smk_render_palette(7db616a32878e93c).(struct smk_video_tcxx . . $ 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 assertcxx . . $ `/usr/include/assert.h:118:11`!(s);
1180 assertcxx . . $ `/usr/include/assert.h:118:11`!(p);
1181 /* Copy palette to old palette */
1182 memcpycxx . . $ memcpy(cc817cb175f282d).(oldPalette, s->palettecxx . . $ smk_video_t#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 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::palette_render(s,p,size) - ERROR: 0x40 ran out of bytes for copy\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
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 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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 memmovecxx . . $ memmove(cc817cb175f282d).(&s->palettecxx . . $ smk_video_t#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 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::palette_render - ERROR: palette index exceeds 0x3F (entry [%u][%u])\n", i, count);
1241 goto error;
1242 }
1243
1244 s->palettecxx . . $ smk_video_t#palette.[i][count] = palmap[*p];
1245 p++;
1246 size --;
1247 }
1248
1249 i ++;
1250 }
1251 }
1252
1253 if (i < 256) {
1254 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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_videocxx . . $ smk_render_video(3c519adeef334510).(struct smk_video_tcxx . . $ smk_video_t# * s, unsigned char * p, unsigned int size)
1266 {
1267 unsigned char * t = s->framecxx . . $ smk_video_t#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_tcxx . . $ 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 assertcxx . . $ `/usr/include/assert.h:118:11`!(s);
1292 assertcxx . . $ `/usr/include/assert.h:118:11`!(p);
1293 row = 0;
1294 col = 0;
1295 /* Set up a bitstream for video unpacking */
1296 smk_bs_initcxx . . $ smk_bs_init(7b76ca13e6b7465e).(&bs, p, size);
1297
1298 /* Reset the cache on all bigtrees */
1299 for (i = 0; i < 4; i++)
1300 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&s->treecxx . . $ smk_video_t#tree.[i].cachecxx . . $ smk_huff16_t#cache., 0, 3 * sizeof(unsigned short));
1301
1302 while (row < s->hcxx . . $ smk_video_t#h.) {
1303 if ((unpack = smk_huff16_lookupcxx . . $ smk_huff16_lookup(e57c26c0262c4237).(&s->treecxx . . $ smk_video_t#tree.[SMK_TREE_TYPEcxx . . $ `smacker.c:489:9`!], &bs)) < 0) {
1304 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render_video() - ERROR: failed to lookup from TYPE tree.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
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->vcxx . . $ smk_video_t#v. == '4') {
1314 bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(&bs);
1315
1316 if (bit)
1317 type = 4;
1318 else {
1319 bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(&bs);
1320
1321 if (bit)
1322 type = 5;
1323 }
1324 }
1325
1326 for (j = 0; (j < sizetable[blocklen]) && (row < s->hcxx . . $ smk_video_t#h.); j ++) {
1327 skip = (row * s->wcxx . . $ smk_video_t#w.) + col;
1328
1329 switch (type) {
1330 case 0:
1331 if ((unpack = smk_huff16_lookupcxx . . $ smk_huff16_lookup(e57c26c0262c4237).(&s->treecxx . . $ smk_video_t#tree.[SMK_TREE_MCLRcxx . . $ `smacker.c:487:9`!], &bs)) < 0) {
1332 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render_video() - ERROR: failed to lookup from MCLR tree.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1333 return -1;
1334 }
1335
1336 s1 = (unpack & 0xFF00) >> 8;
1337 s2 = (unpack & 0x00FF);
1338
1339 if ((unpack = smk_huff16_lookupcxx . . $ smk_huff16_lookup(e57c26c0262c4237).(&s->treecxx . . $ smk_video_t#tree.[SMK_TREE_MMAPcxx . . $ `smacker.c:486:9`!], &bs)) < 0) {
1340 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render_video() - ERROR: failed to lookup from MMAP tree.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
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->wcxx . . $ smk_video_t#w.;
1357 }
1358
1359 break;
1360
1361 case 1: /* FULL BLOCK */
1362 for (k = 0; k < 4; k ++) {
1363 if ((unpack = smk_huff16_lookupcxx . . $ smk_huff16_lookup(e57c26c0262c4237).(&s->treecxx . . $ smk_video_t#tree.[SMK_TREE_FULLcxx . . $ `smacker.c:488:9`!], &bs)) < 0) {
1364 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1365 return -1;
1366 }
1367
1368 t[skip + 3] = ((unpack & 0xFF00) >> 8);
1369 t[skip + 2] = (unpack & 0x00FF);
1370
1371 if ((unpack = smk_huff16_lookupcxx . . $ smk_huff16_lookup(e57c26c0262c4237).(&s->treecxx . . $ smk_video_t#tree.[SMK_TREE_FULLcxx . . $ `smacker.c:488:9`!], &bs)) < 0) {
1372 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1373 return -1;
1374 }
1375
1376 t[skip + 1] = ((unpack & 0xFF00) >> 8);
1377 t[skip] = (unpack & 0x00FF);
1378 skip += s->wcxx . . $ smk_video_t#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 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&t[skip], typedata, 4);
1399 skip += s->wcxx . . $ smk_video_t#w.;
1400 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&t[skip], typedata, 4);
1401 skip += s->wcxx . . $ smk_video_t#w.;
1402 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&t[skip], typedata, 4);
1403 skip += s->wcxx . . $ smk_video_t#w.;
1404 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&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_lookupcxx . . $ smk_huff16_lookup(e57c26c0262c4237).(&s->treecxx . . $ smk_video_t#tree.[SMK_TREE_FULLcxx . . $ `smacker.c:488:9`!], &bs)) < 0) {
1410 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1411 return -1;
1412 }
1413
1414 for (i = 0; i < 2; i ++) {
1415 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&t[skip + 2], (unpack & 0xFF00) >> 8, 2);
1416 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&t[skip], (unpack & 0x00FF), 2);
1417 skip += s->wcxx . . $ smk_video_t#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_lookupcxx . . $ smk_huff16_lookup(e57c26c0262c4237).(&s->treecxx . . $ smk_video_t#tree.[SMK_TREE_FULLcxx . . $ `smacker.c:488:9`!], &bs)) < 0) {
1426 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1427 return -1;
1428 }
1429
1430 t[skip + 3] = ((unpack & 0xFF00) >> 8);
1431 t[skip + 2] = (unpack & 0x00FF);
1432 t[skip + s->wcxx . . $ smk_video_t#w. + 3] = ((unpack & 0xFF00) >> 8);
1433 t[skip + s->wcxx . . $ smk_video_t#w. + 2] = (unpack & 0x00FF);
1434
1435 if ((unpack = smk_huff16_lookupcxx . . $ smk_huff16_lookup(e57c26c0262c4237).(&s->treecxx . . $ smk_video_t#tree.[SMK_TREE_FULLcxx . . $ `smacker.c:488:9`!], &bs)) < 0) {
1436 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1437 return -1;
1438 }
1439
1440 t[skip + 1] = ((unpack & 0xFF00) >> 8);
1441 t[skip] = (unpack & 0x00FF);
1442 t[skip + s->wcxx . . $ smk_video_t#w. + 1] = ((unpack & 0xFF00) >> 8);
1443 t[skip + s->wcxx . . $ smk_video_t#w.] = (unpack & 0x00FF);
1444 skip += (s->wcxx . . $ smk_video_t#w. << 1);
1445 }
1446
1447 break;
1448 }
1449
1450 col += 4;
1451
1452 if (col >= s->wcxx . . $ smk_video_t#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_audiocxx . . $ smk_render_audio(17800e6d966e6357).(struct smk_audio_tcxx . . $ smk_audio_t# * s, unsigned char * p, unsigned long size)
1464 {
1465 unsigned int j, k;
1466 unsigned char * t = s->buffercxx . . $ smk_audio_t#buffer.;
1467 struct smk_bit_tcxx . . $ smk_bit_t# bs;
1468 char bit;
1469 short unpack, unpack2;
1470 /* used for audio decoding */
1471 struct smk_huff8_tcxx . . $ smk_huff8_t# aud_tree[4];
1472 /* null check */
1473 assertcxx . . $ `/usr/include/assert.h:118:11`!(s);
1474 assertcxx . . $ `/usr/include/assert.h:118:11`!(p);
1475
1476 if (!s->compresscxx . . $ smk_audio_t#compress.) {
1477 /* Raw PCM data, update buffer size and perform copy */
1478 s->buffer_sizecxx . . $ smk_audio_t#buffer_size. = size;
1479 memcpycxx . . $ memcpy(cc817cb175f282d).(t, p, size);
1480 } else if (s->compresscxx . . $ smk_audio_t#compress. == 1) {
1481 /* SMACKER DPCM compression */
1482 /* need at least 4 bytes to process */
1483 if (size < 4) {
1484 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render_audio() - ERROR: need 4 bytes to get unpacked output buffer size.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1485 goto error;
1486 }
1487
1488 /* chunk is compressed (huff-compressed dpcm), retrieve unpacked buffer size */
1489 s->buffer_sizecxx . . $ smk_audio_t#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_initcxx . . $ smk_bs_init(7b76ca13e6b7465e).(&bs, p, size);
1498 bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(&bs);
1499
1500 if (!bit) {
1501 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render_audio - ERROR: initial get_bit returned 0\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1502 goto error;
1503 }
1504
1505 bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(&bs);
1506
1507 if (s->channelscxx . . $ smk_audio_t#channels. != (bit == 1 ? 2 : 1))
1508 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render - ERROR: mono/stereo mismatch\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1509
1510 bit = smk_bs_read_1cxx . . $ smk_bs_read_1(6af2747f2d15fd8b).(&bs);
1511
1512 if (s->bitdepthcxx . . $ smk_audio_t#bitdepth. != (bit == 1 ? 16 : 8))
1513 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_render - ERROR: 8-/16-bit mismatch\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1514
1515 /* build the trees */
1516 smk_huff8_buildcxx . . $ smk_huff8_build(8adbe9b8824dfad8).(&aud_tree[0], &bs);
1517 j = 1;
1518 k = 1;
1519
1520 if (s->bitdepthcxx . . $ smk_audio_t#bitdepth. == 16) {
1521 smk_huff8_buildcxx . . $ smk_huff8_build(8adbe9b8824dfad8).(&aud_tree[1], &bs);
1522 k = 2;
1523 }
1524
1525 if (s->channelscxx . . $ smk_audio_t#channels. == 2) {
1526 smk_huff8_buildcxx . . $ smk_huff8_build(8adbe9b8824dfad8).(&aud_tree[2], &bs);
1527 j = 2;
1528 k = 2;
1529
1530 if (s->bitdepthcxx . . $ smk_audio_t#bitdepth. == 16) {
1531 smk_huff8_buildcxx . . $ smk_huff8_build(8adbe9b8824dfad8).(&aud_tree[3], &bs);
1532 k = 4;
1533 }
1534 }
1535
1536 /* read initial sound level */
1537 if (s->channelscxx . . $ smk_audio_t#channels. == 2) {
1538 unpack = smk_bs_read_8cxx . . $ smk_bs_read_8(6af2747f2d15fd8b).(&bs);
1539
1540 if (s->bitdepthcxx . . $ smk_audio_t#bitdepth. == 16) {
1541 ((short *)t)[1] = smk_bs_read_8cxx . . $ smk_bs_read_8(6af2747f2d15fd8b).(&bs);
1542 ((short *)t)[1] |= (unpack << 8);
1543 } else
1544 ((unsigned char *)t)[1] = (unsigned char)unpack;
1545 }
1546
1547 unpack = smk_bs_read_8cxx . . $ smk_bs_read_8(6af2747f2d15fd8b).(&bs);
1548
1549 if (s->bitdepthcxx . . $ smk_audio_t#bitdepth. == 16) {
1550 ((short *)t)[0] = smk_bs_read_8cxx . . $ smk_bs_read_8(6af2747f2d15fd8b).(&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_sizecxx . . $ smk_audio_t#buffer_size.) {
1557 if (s->bitdepthcxx . . $ smk_audio_t#bitdepth. == 8) {
1558 unpack = smk_huff8_lookupcxx . . $ smk_huff8_lookup(5a9fcf29b5a8a256).(&aud_tree[0], &bs);
1559 ((unsigned char *)t)[j] = (char)unpack + ((unsigned char *)t)[j - s->channelscxx . . $ smk_audio_t#channels.];
1560 j ++;
1561 k++;
1562 } else {
1563 unpack = smk_huff8_lookupcxx . . $ smk_huff8_lookup(5a9fcf29b5a8a256).(&aud_tree[0], &bs);
1564 unpack2 = smk_huff8_lookupcxx . . $ smk_huff8_lookup(5a9fcf29b5a8a256).(&aud_tree[1], &bs);
1565 ((short *)t)[j] = (short)(unpack | (unpack2 << 8)) + ((short *)t)[j - s->channelscxx . . $ smk_audio_t#channels.];
1566 j ++;
1567 k += 2;
1568 }
1569
1570 if (s->channelscxx . . $ smk_audio_t#channels. == 2) {
1571 if (s->bitdepthcxx . . $ smk_audio_t#bitdepth. == 8) {
1572 unpack = smk_huff8_lookupcxx . . $ smk_huff8_lookup(5a9fcf29b5a8a256).(&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_lookupcxx . . $ smk_huff8_lookup(5a9fcf29b5a8a256).(&aud_tree[2], &bs);
1578 unpack2 = smk_huff8_lookupcxx . . $ smk_huff8_lookup(5a9fcf29b5a8a256).(&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_rendercxx . . $ smk_render(d3b1797f8e8a14cf).(smkcxx . . $ smk# s)
1595 {
1596 unsigned long i, size;
1597 unsigned char * buffer = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, * p, track;
1598 /* null check */
1599 assertcxx . . $ `/usr/include/assert.h:118:11`!(s);
1600
1601 /* Retrieve current chunk_size for this frame. */
1602 if (!(i = s->chunk_sizecxx . . $ smk_t#chunk_size.[s->cur_framecxx . . $ smk_t#cur_frame.])) {
1603 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_render(s) - Warning: frame %lu: chunk_size is 0.\n", s->cur_framecxx . . $ smk_t#cur_frame.);
1604 goto error;
1605 }
1606
1607 if (s->modecxx . . $ smk_t#mode. == SMK_MODE_DISKcxx . . $ `smacker.h:38:9`!) {
1608 /* Skip to frame in file */
1609 if (fseekcxx . . $ fseek(543fae30d249f6b1).(s->sourcecxx . . $ smk_t#source..filecxx . . $ smk_t#$anonymous_type_0#file..fpcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#fp., s->sourcecxx . . $ smk_t#source..filecxx . . $ smk_t#$anonymous_type_0#file..chunk_offsetcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#chunk_offset.[s->cur_framecxx . . $ smk_t#cur_frame.], SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!)) {
1610 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_render(s) - ERROR: fseek to frame %lu (offset %lu) failed.\n", s->cur_framecxx . . $ smk_t#cur_frame., s->sourcecxx . . $ smk_t#source..filecxx . . $ smk_t#$anonymous_type_0#file..chunk_offsetcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#chunk_offset.[s->cur_framecxx . . $ smk_t#cur_frame.]);
1611 perrorcxx . . $ perror(85c52e162fed56f9).("\tError reported was");
1612 goto error;
1613 }
1614
1615 /* In disk-streaming mode: make way for our incoming chunk buffer */
1616 if ((buffer = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(i)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1617 perrorcxx . . $ perror(85c52e162fed56f9).("libsmacker::smk_render() - ERROR: failed to malloc() buffer");
1618 return -1;
1619 }
1620
1621 /* Read into buffer */
1622 if (smk_read_filecxx . . $ smk_read_file(3b852f5abebdc535).(buffer, s->chunk_sizecxx . . $ smk_t#chunk_size.[s->cur_framecxx . . $ smk_t#cur_frame.], s->sourcecxx . . $ smk_t#source..filecxx . . $ smk_t#$anonymous_type_0#file..fpcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#fp.) < 0) {
1623 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_render(s) - ERROR: frame %lu (offset %lu): smk_read had errors.\n", s->cur_framecxx . . $ smk_t#cur_frame., s->sourcecxx . . $ smk_t#source..filecxx . . $ smk_t#$anonymous_type_0#file..chunk_offsetcxx . . $ smk_t#$anonymous_type_0#$anonymous_type_1#chunk_offset.[s->cur_framecxx . . $ smk_t#cur_frame.]);
1624 goto error;
1625 }
1626 } else {
1627 /* Just point buffer at the right place */
1628 if (!s->sourcecxx . . $ smk_t#source..chunk_datacxx . . $ smk_t#$anonymous_type_0#chunk_data.[s->cur_framecxx . . $ smk_t#cur_frame.]) {
1629 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_render(s) - ERROR: frame %lu: memory chunk is a NULL pointer.\n", s->cur_framecxx . . $ smk_t#cur_frame.);
1630 goto error;
1631 }
1632
1633 buffer = s->sourcecxx . . $ smk_t#source..chunk_datacxx . . $ smk_t#$anonymous_type_0#chunk_data.[s->cur_framecxx . . $ smk_t#cur_frame.];
1634 }
1635
1636 p = buffer;
1637
1638 /* Palette record first */
1639 if (s->frame_typecxx . . $ smk_t#frame_type.[s->cur_framecxx . . $ smk_t#cur_frame.] & 0x01) {
1640 /* need at least 1 byte to process */
1641 if (!i) {
1642 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_render(s) - ERROR: frame %lu: insufficient data for a palette rec.\n", s->cur_framecxx . . $ smk_t#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->videocxx . . $ smk_t#video..enablecxx . . $ smk_video_t#enable.)
1652 smk_render_palettecxx . . $ smk_render_palette(7db616a32878e93c).(&(s->videocxx . . $ smk_t#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_typecxx . . $ smk_t#frame_type.[s->cur_framecxx . . $ smk_t#cur_frame.] & (0x02 << track)) {
1661 /* need at least 4 byte to process */
1662 if (i < 4) {
1663 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_render(s) - ERROR: frame %lu: insufficient data for audio[%u] rec.\n", s->cur_framecxx . . $ smk_t#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->audiocxx . . $ smk_t#audio.[track].enablecxx . . $ smk_audio_t#enable.)
1676 smk_render_audiocxx . . $ smk_render_audio(17800e6d966e6357).(&s->audiocxx . . $ smk_t#audio.[track], p + 4, size - 4);
1677
1678 p += size;
1679 i -= size;
1680 } else
1681 s->audiocxx . . $ smk_t#audio.[track].buffer_sizecxx . . $ smk_audio_t#buffer_size. = 0;
1682 }
1683
1684 /* Unpack video chunk */
1685 if (s->videocxx . . $ smk_t#video..enablecxx . . $ smk_video_t#enable.) {
1686 if (smk_render_videocxx . . $ smk_render_video(3c519adeef334510).(&(s->videocxx . . $ smk_t#video.), p, i) < 0) {
1687 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_render(s) - ERROR: frame %lu: failed to render video.\n", s->cur_framecxx . . $ smk_t#cur_frame.);
1688 goto error;
1689 }
1690 }
1691
1692 if (s->modecxx . . $ smk_t#mode. == SMK_MODE_DISKcxx . . $ `smacker.h:38:9`!) {
1693 /* Remember that buffer we allocated? Trash it */
1694 smk_freecxx . . $ `smk_malloc.h:30:9`!(buffer);
1695 }
1696
1697 return 0;
1698 error:
1699
1700 if (s->modecxx . . $ smk_t#mode. == SMK_MODE_DISKcxx . . $ `smacker.h:38:9`!) {
1701 /* Remember that buffer we allocated? Trash it */
1702 smk_freecxx . . $ `smk_malloc.h:30:9`!(buffer);
1703 }
1704
1705 return -1;
1706 }
1707
1708 /* rewind to first frame and unpack */
1709 char smk_firstcxx . . $ smk_first(d3b1797f8e8a14cf).(smkcxx . . $ smk# s)
1710 {
1711 /* null check */
1712 if (s == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1713 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_first() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1714 return -1;
1715 }
1716
1717 s->cur_framecxx . . $ smk_t#cur_frame. = 0;
1718
1719 if (smk_rendercxx . . $ smk_render(d3b1797f8e8a14cf).(s) < 0) {
1720 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_first(s) - Warning: frame %lu: smk_render returned errors.\n", s->cur_framecxx . . $ smk_t#cur_frame.);
1721 return -1;
1722 }
1723
1724 if (s->fcxx . . $ smk_t#f. == 1) return SMK_LASTcxx . . $ `smacker.h:34:9`!;
1725
1726 return SMK_MOREcxx . . $ `smacker.h:33:9`!;
1727 }
1728
1729 /* advance to next frame */
1730 char smk_nextcxx . . $ smk_next(d3b1797f8e8a14cf).(smkcxx . . $ smk# s)
1731 {
1732 /* null check */
1733 if (s == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1734 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_next() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1735 return -1;
1736 }
1737
1738 if (s->cur_framecxx . . $ smk_t#cur_frame. + 1 < (s->fcxx . . $ smk_t#f. + s->ring_framecxx . . $ smk_t#ring_frame.)) {
1739 s->cur_framecxx . . $ smk_t#cur_frame. ++;
1740
1741 if (smk_rendercxx . . $ smk_render(d3b1797f8e8a14cf).(s) < 0) {
1742 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_next(s) - Warning: frame %lu: smk_render returned errors.\n", s->cur_framecxx . . $ smk_t#cur_frame.);
1743 return -1;
1744 }
1745
1746 if (s->cur_framecxx . . $ smk_t#cur_frame. + 1 == (s->fcxx . . $ smk_t#f. + s->ring_framecxx . . $ smk_t#ring_frame.))
1747 return SMK_LASTcxx . . $ `smacker.h:34:9`!;
1748
1749 return SMK_MOREcxx . . $ `smacker.h:33:9`!;
1750 } else if (s->ring_framecxx . . $ smk_t#ring_frame.) {
1751 s->cur_framecxx . . $ smk_t#cur_frame. = 1;
1752
1753 if (smk_rendercxx . . $ smk_render(d3b1797f8e8a14cf).(s) < 0) {
1754 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_next(s) - Warning: frame %lu: smk_render returned errors.\n", s->cur_framecxx . . $ smk_t#cur_frame.);
1755 return -1;
1756 }
1757
1758 if (s->cur_framecxx . . $ smk_t#cur_frame. + 1 == (s->fcxx . . $ smk_t#f. + s->ring_framecxx . . $ smk_t#ring_frame.))
1759 return SMK_LASTcxx . . $ `smacker.h:34:9`!;
1760
1761 return SMK_MOREcxx . . $ `smacker.h:33:9`!;
1762 }
1763
1764 return SMK_DONEcxx . . $ `smacker.h:32:9`!;
1765 }
1766
1767 /* seek to a keyframe in an smk */
1768 char smk_seek_keyframecxx . . $ smk_seek_keyframe(69b334dec2cf7ed0).(smkcxx . . $ smk# s, unsigned long f)
1769 {
1770 /* null check */
1771 if (s == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
1772 fputscxx . . $ fputs(874fa6a5e59ab9bd).("libsmacker::smk_seek_keyframe() - ERROR: smk is NULL\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
1773 return -1;
1774 }
1775
1776 /* rewind (or fast forward!) exactly to f */
1777 s->cur_framecxx . . $ smk_t#cur_frame. = f;
1778
1779 /* roll back to previous keyframe in stream, or 0 if no keyframes exist */
1780 while (s->cur_framecxx . . $ smk_t#cur_frame. > 0 && !(s->keyframecxx . . $ smk_t#keyframe.[s->cur_framecxx . . $ smk_t#cur_frame.]))
1781 s->cur_framecxx . . $ smk_t#cur_frame. --;
1782
1783 /* render the frame: we're ready */
1784 if (smk_rendercxx . . $ smk_render(d3b1797f8e8a14cf).(s) < 0) {
1785 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "libsmacker::smk_seek_keyframe(s,%lu) - Warning: frame %lu: smk_render returned errors.\n", f, s->cur_framecxx . . $ smk_t#cur_frame.);
1786 return -1;
1787 }
1788
1789 return 0;
1790 }