1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/*
* fbmngplay - fb console MNG player.
* (c) 2001-2002 by Stefan Reinauer, <stepan@suse.de>
*
* This program is based on mngplay, part of libmng, written and (C) by
* Ralph Giles <giles@ashlu.bc.ca>
*
* This program my be redistributed under the terms of the
* GNU General Public Licence, version 2, or at your preference,
* any later version.
*/
#include <unistd.h>
#include <sys/time.h>
#include "fbmngplay.h"
#include "console.h"
#include "mng.h"
mngstuff *mng;
unsigned char *bufferstream;
unsigned long bufferpos = 0, buffersize = 0;
/*
* callbacks for the mng decoder
*/
/* memory allocation; data must be zeroed */
mng_ptr mngalloc(mng_uint32 size)
{
return (mng_ptr) calloc(1, size);
}
/* memory deallocation */
void mngfree(mng_ptr p, mng_uint32 size)
{
free(p);
return;
}
mng_bool mngopenstream(mng_handle mng)
{
mngstuff *mymng;
/* look up our stream struct */
mymng = (mngstuff *) mng_get_userdata(mng);
/* open the file */
mymng->file = fopen(mymng->filename, "rb");
if (mymng->file == NULL) {
fprintf(stderr, "unable to open '%s'\n", mymng->filename);
run = 0;
return MNG_FALSE;
}
if (buffered) {
unsigned long len;
fseek(mymng->file, 0, SEEK_END);
len = ftell(mymng->file);
rewind(mymng->file);
bufferstream = malloc(len);
if (!bufferstream) {
/* Not enough memory for buffers
* -> we go back to unbuffered mode
*/
printf("Reverted to non buffered mode.\n");
buffered = 0;
return MNG_TRUE;
}
buffersize = len;
fread(bufferstream, 1, len, mymng->file);
bufferpos = 0;
fclose(mymng->file);
mymng->file = NULL;
}
return MNG_TRUE;
}
mng_bool mngclosestream(mng_handle mng)
{
mngstuff *mymng;
/* look up our stream struct */
mymng = (mngstuff *) mng_get_userdata(mng);
/* close the file */
if (mymng->file)
fclose(mymng->file);
mymng->file = NULL; /* for safety */
if (bufferstream) {
free(bufferstream);
bufferstream = 0;
buffersize = 0;
bufferpos = 0;
}
return MNG_TRUE;
}
/* feed data to the decoder */
mng_bool mngreadstream(mng_handle mng, mng_ptr buffer,
mng_uint32 size, mng_uint32 * bytesread)
{
mngstuff *mymng;
/* look up our stream struct */
mymng = (mngstuff *) mng_get_userdata(mng);
if (!buffered) {
/* read the requested amount of data from the file */
*bytesread = fread(buffer, 1, size, mymng->file);
} else {
*bytesread = (buffersize - bufferpos) <
size ? (buffersize - bufferpos) : size;
memcpy(buffer, bufferstream + bufferpos, *bytesread);
bufferpos += (*bytesread);
}
return MNG_TRUE;
}
mng_bool mnggetbackgroundbuffer(mng_handle mng)
{
unsigned char *background, *src;
mngstuff *mymng=mng_get_userdata(mng);
mng_uint32 width=mymng->width, height=mymng->height;
int bytes=(mymng->fbbpp >> 3);
if (mymng->background)
return MNG_TRUE;
/* If we're not on the right terminal, don't
* initialize background yet.
*/
if (sconly && current_console()!=start_console)
return MNG_FALSE;
background = (unsigned char *) malloc(width * height * bytes);
if (background == NULL) {
fprintf(stderr, "could not allocate background buffer.\n");
exit(0);
}
mymng->background = background;
src = mymng->display + (mymng->fbwidth * mymng->fby + mymng->fbx) * bytes;
while (height--) {
memcpy(background, src, width * bytes);
background += width * bytes;
src += mymng->fbrow;
}
return MNG_TRUE;
}
/* the header's been read. set up the display stuff */
mng_bool mngprocessheader(mng_handle mng,
mng_uint32 width, mng_uint32 height)
{
mngstuff *mymng;
unsigned char *copybuffer;
mymng = (mngstuff *) mng_get_userdata(mng);
mymng->width = width;
mymng->height = height;
copybuffer = (unsigned char *) malloc(width * height * 4);
if (copybuffer == NULL) {
fprintf(stderr, "could not allocate copy buffer.\n");
exit(0);
}
mymng->copybuffer = copybuffer;
/* Try to get background buffer */
mnggetbackgroundbuffer(mng);
/* tell the mng decoder about our bit-depth choice */
/* FIXME: this works on intel. is it correct in general? */
mng_set_canvasstyle(mng, MNG_CANVAS_BGRA8);
return MNG_TRUE;
}
/* return a row pointer for the decoder to fill */
mng_ptr mnggetcanvasline(mng_handle mng, mng_uint32 line)
{
mngstuff *mymng;
mng_ptr row;
/* dereference our structure */
mymng = (mngstuff *) mng_get_userdata(mng);
/* we assume any necessary locking has happened
outside, in the frame level code */
row = mymng->copybuffer + mymng->width * 4 * line;
return (row);
}
/* timer */
mng_uint32 mnggetticks(mng_handle mng)
{
mng_uint32 ticks;
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
ticks = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
return (ticks);
}
static inline void copyline(unsigned char *dest, unsigned char *src,
unsigned char *background, mngstuff * mymng)
{
// BGRA8
unsigned int i = mymng->width;
unsigned int fr, fg, fb, br, bg, bb, r, g, b, a;
unsigned short output, input;
while (i--) {
fb = *src++;
fg = *src++;
fr = *src++;
a = *src++;
a = a * mymng->alpha / 100;
switch (mymng->fbbpp) {
case 16:
input = *((unsigned short *) background)++;
br = (input >> mng->fbredo) << (8-mng->fbredl);
bg = (input >> mng->fbgreeno) << (8-mng->fbgreenl);
bb = (input >> mng->fbblueo) << (8-mng->fbbluel);
br &= 0xf8;
bg &= 0xfc;
bb &= 0xff;
#if 0
br = (input >> 8) & 0xf8;
bg = (input >> 3) & 0xfc;
bb = input << 3 & 0xff;
#endif
break;
case 24:
bb = *background++;
bg = *background++;
br = *background++;
break;
case 32:
bb = *background++;
bg = *background++;
br = *background++;
background++;
break;
default:
br = 0;
bg = 0;
bb = 0;
printf("depth not supported.\n");
run = 0;
break;
}
r = ((fr * a) + (br * (0x100 - a))) >> 8;
g = ((fg * a) + (bg * (0x100 - a))) >> 8;
b = ((fb * a) + (bb * (0x100 - a))) >> 8;
switch (mymng->fbbpp) {
case 16:
// dumb 24->16 bit conversion.
r >>= (8-mng->fbredl);
g >>= (8-mng->fbgreenl);
b >>= (8-mng->fbbluel);
output = (r << mng->fbredo) | (g << mng->fbgreeno) | (b << mng->fbblueo);
*((unsigned short *) dest)++ = output;
break;
case 24:
*dest++ = b;
*dest++ = g;
*dest++ = r;
break;
case 32:
*dest++ = b;
*dest++ = g;
*dest++ = r;
dest++;
break;
default:
break;
}
}
}
mng_bool mngrefresh(mng_handle mng, mng_uint32 x, mng_uint32 y,
mng_uint32 w, mng_uint32 h)
{
mngstuff *mymng=mng_get_userdata(mng);
unsigned char *background;
unsigned char *dest, *src;
int bytes=(mymng->fbbpp >> 3);
if (sconly && current_console()!=start_console)
return MNG_TRUE;
/* When we read the header, we might still
* have been on a different console
*/
if (!(mymng->background))
mnggetbackgroundbuffer(mng);
background = mymng->background;
dest = mymng->display+(mymng->fby*mymng->fbwidth + mymng->fbx)*bytes;
src = mymng->copybuffer;
/* refresh the screen with the new frame */
while (h-- > 0) {
copyline(dest, src, background, mymng);
dest += mymng->fbrow;
background += mymng->width * bytes;
/* 4 bytes per pixel due to RGBA */
src += 4 * mymng->width;
}
/* remove traces in alpha transparent pictures. */
memset(mymng->copybuffer, 0, 4 * mymng->width * mymng->height);
return MNG_TRUE;
}
/* interframe delay callback */
mng_bool mngsettimer(mng_handle mng, mng_uint32 msecs)
{
mngstuff *mymng;
/* look up our stream struct */
mymng = (mngstuff *) mng_get_userdata(mng);
/* set the timer for when the decoder wants to be woken */
mymng->delay = msecs;
return MNG_TRUE;
}
mng_bool mngerror(mng_handle mng, mng_int32 code, mng_int8 severity,
mng_chunkid chunktype, mng_uint32 chunkseq,
mng_int32 extra1, mng_int32 extra2, mng_pchar text)
{
mngstuff *mymng;
char chunk[5];
/* dereference our data so we can get the filename */
mymng = (mngstuff *) mng_get_userdata(mng);
/* pull out the chuck type as a string */
// FIXME: does this assume unsigned char?
chunk[0] = (char) ((chunktype >> 24) & 0xFF);
chunk[1] = (char) ((chunktype >> 16) & 0xFF);
chunk[2] = (char) ((chunktype >> 8) & 0xFF);
chunk[3] = (char) ((chunktype) & 0xFF);
chunk[4] = '\0';
/* output the error */
fprintf(stderr, "error playing '%s' chunk %s (%d):\n",
mymng->filename, chunk, chunkseq);
fprintf(stderr, "%s\n", text);
return 0;
}
int mngquit(mng_handle mng)
{
mngstuff *mymng;
/* dereference our data so we can free it */
mymng = (mngstuff *) mng_get_userdata(mng);
/* cleanup. this will call mymngclosestream */
mng_cleanup(&mng);
/* free our data */
free(mymng);
exit(0);
}
void cleanup(void)
{
mngquit(mng->mng);
exit(0);
}
void restore_area(void)
{
int height, width;
unsigned char *dest, *background;
if (sconly && current_console()!=start_console)
return;
/* when we didn't manage to get a background until
* now, we don't have anything to restore anyways.
*/
if (!(mng->background))
return;
background = mng->background;
height = mng->height;
width = mng->width;
dest = mng->display + (( mng->fbwidth*mng->fby + mng->fbx) *
(mng->fbbpp>>3));
while (height--) {
memcpy(dest, background, width * (mng->fbbpp >> 3));
background += width * (mng->fbbpp >> 3);
dest += mng->fbrow;
}
}
|