forked from xonotic/darkplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpvsimpledecode.c
656 lines (609 loc) · 17.2 KB
/
dpvsimpledecode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
#include "quakedef.h"
#include "dpvsimpledecode.h"
#define HZREADERROR_OK 0
#define HZREADERROR_EOF 1
#define HZREADERROR_MALLOCFAILED 2
//#define HZREADBLOCKSIZE 16000
#define HZREADBLOCKSIZE 1048576
typedef struct hz_bitstream_read_s
{
qfile_t *file;
int endoffile;
}
hz_bitstream_read_t;
typedef struct hz_bitstream_readblock_s
{
struct hz_bitstream_readblock_s *next;
unsigned int size;
unsigned char data[HZREADBLOCKSIZE];
}
hz_bitstream_readblock_t;
typedef struct hz_bitstream_readblocks_s
{
hz_bitstream_readblock_t *blocks;
hz_bitstream_readblock_t *current;
unsigned int position;
unsigned int store;
int count;
}
hz_bitstream_readblocks_t;
static hz_bitstream_read_t *hz_bitstream_read_open(char *filename)
{
qfile_t *file;
hz_bitstream_read_t *stream;
if ((file = FS_OpenVirtualFile(filename, false)))
{
stream = (hz_bitstream_read_t *)Z_Malloc(sizeof(hz_bitstream_read_t));
memset(stream, 0, sizeof(*stream));
stream->file = file;
return stream;
}
else
return NULL;
}
static void hz_bitstream_read_close(hz_bitstream_read_t *stream)
{
if (stream)
{
FS_Close(stream->file);
Z_Free(stream);
}
}
static hz_bitstream_readblocks_t *hz_bitstream_read_blocks_new(void)
{
hz_bitstream_readblocks_t *blocks;
blocks = (hz_bitstream_readblocks_t *)Z_Malloc(sizeof(hz_bitstream_readblocks_t));
if (blocks == NULL)
return NULL;
memset(blocks, 0, sizeof(hz_bitstream_readblocks_t));
return blocks;
}
static void hz_bitstream_read_blocks_free(hz_bitstream_readblocks_t *blocks)
{
hz_bitstream_readblock_t *b, *n;
if (blocks == NULL)
return;
for (b = blocks->blocks;b;b = n)
{
n = b->next;
Z_Free(b);
}
Z_Free(blocks);
}
static void hz_bitstream_read_flushbits(hz_bitstream_readblocks_t *blocks)
{
blocks->store = 0;
blocks->count = 0;
}
static int hz_bitstream_read_blocks_read(hz_bitstream_readblocks_t *blocks, hz_bitstream_read_t *stream, unsigned int size)
{
int s;
hz_bitstream_readblock_t *b, *p;
s = size;
p = NULL;
b = blocks->blocks;
while (s > 0)
{
if (b == NULL)
{
b = (hz_bitstream_readblock_t *)Z_Malloc(sizeof(hz_bitstream_readblock_t));
if (b == NULL)
return HZREADERROR_MALLOCFAILED;
b->next = NULL;
b->size = 0;
if (p != NULL)
p->next = b;
else
blocks->blocks = b;
}
if (s > HZREADBLOCKSIZE)
b->size = HZREADBLOCKSIZE;
else
b->size = s;
s -= b->size;
if (FS_Read(stream->file, b->data, b->size) != (fs_offset_t)b->size)
{
stream->endoffile = 1;
break;
}
p = b;
b = b->next;
}
while (b)
{
b->size = 0;
b = b->next;
}
blocks->current = blocks->blocks;
blocks->position = 0;
hz_bitstream_read_flushbits(blocks);
if (stream->endoffile)
return HZREADERROR_EOF;
return HZREADERROR_OK;
}
static unsigned int hz_bitstream_read_blocks_getbyte(hz_bitstream_readblocks_t *blocks)
{
while (blocks->current != NULL && blocks->position >= blocks->current->size)
{
blocks->position = 0;
blocks->current = blocks->current->next;
}
if (blocks->current == NULL)
return 0;
return blocks->current->data[blocks->position++];
}
static int hz_bitstream_read_bit(hz_bitstream_readblocks_t *blocks)
{
if (!blocks->count)
{
blocks->count += 8;
blocks->store <<= 8;
blocks->store |= hz_bitstream_read_blocks_getbyte(blocks) & 0xFF;
}
blocks->count--;
return (blocks->store >> blocks->count) & 1;
}
static unsigned int hz_bitstream_read_bits(hz_bitstream_readblocks_t *blocks, int size)
{
unsigned int num = 0;
// we can only handle about 24 bits at a time safely
// (there might be up to 7 bits more than we need in the bit store)
if (size > 24)
{
size -= 8;
num |= hz_bitstream_read_bits(blocks, 8) << size;
}
while (blocks->count < size)
{
blocks->count += 8;
blocks->store <<= 8;
blocks->store |= hz_bitstream_read_blocks_getbyte(blocks) & 0xFF;
}
blocks->count -= size;
num |= (blocks->store >> blocks->count) & ((1 << size) - 1);
return num;
}
static unsigned int hz_bitstream_read_byte(hz_bitstream_readblocks_t *blocks)
{
return hz_bitstream_read_blocks_getbyte(blocks);
}
static unsigned int hz_bitstream_read_short(hz_bitstream_readblocks_t *blocks)
{
return (hz_bitstream_read_byte(blocks) << 8)
| (hz_bitstream_read_byte(blocks));
}
static unsigned int hz_bitstream_read_int(hz_bitstream_readblocks_t *blocks)
{
return (hz_bitstream_read_byte(blocks) << 24)
| (hz_bitstream_read_byte(blocks) << 16)
| (hz_bitstream_read_byte(blocks) << 8)
| (hz_bitstream_read_byte(blocks));
}
static void hz_bitstream_read_bytes(hz_bitstream_readblocks_t *blocks, void *outdata, unsigned int size)
{
unsigned char *out;
out = (unsigned char *)outdata;
while (size--)
*out++ = hz_bitstream_read_byte(blocks);
}
#define BLOCKSIZE 8
typedef struct dpvsimpledecodestream_s
{
hz_bitstream_read_t *bitstream;
hz_bitstream_readblocks_t *framedatablocks;
int error;
double info_framerate;
unsigned int info_frames;
unsigned int info_imagewidth;
unsigned int info_imageheight;
unsigned int info_imagebpp;
unsigned int info_imageRloss;
unsigned int info_imageRmask;
unsigned int info_imageRshift;
unsigned int info_imageGloss;
unsigned int info_imageGmask;
unsigned int info_imageGshift;
unsigned int info_imageBloss;
unsigned int info_imageBmask;
unsigned int info_imageBshift;
unsigned int info_imagesize;
double info_aspectratio;
// current video frame (needed because of delta compression)
int videoframenum;
// current video frame data (needed because of delta compression)
unsigned int *videopixels;
// channel the sound file is being played on
int sndchan;
}
dpvsimpledecodestream_t;
static int dpvsimpledecode_setpixelformat(dpvsimpledecodestream_t *s, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel)
{
int Rshift, Rbits, Gshift, Gbits, Bshift, Bbits;
if (!Rmask)
{
s->error = DPVSIMPLEDECODEERROR_INVALIDRMASK;
return s->error;
}
if (!Gmask)
{
s->error = DPVSIMPLEDECODEERROR_INVALIDGMASK;
return s->error;
}
if (!Bmask)
{
s->error = DPVSIMPLEDECODEERROR_INVALIDBMASK;
return s->error;
}
if (Rmask & Gmask || Rmask & Bmask || Gmask & Bmask)
{
s->error = DPVSIMPLEDECODEERROR_COLORMASKSOVERLAP;
return s->error;
}
switch (bytesperpixel)
{
case 2:
if ((Rmask | Gmask | Bmask) > 65536)
{
s->error = DPVSIMPLEDECODEERROR_COLORMASKSEXCEEDBPP;
return s->error;
}
break;
case 4:
break;
default:
s->error = DPVSIMPLEDECODEERROR_UNSUPPORTEDBPP;
return s->error;
}
for (Rshift = 0;!(Rmask & 1);Rshift++, Rmask >>= 1);
for (Gshift = 0;!(Gmask & 1);Gshift++, Gmask >>= 1);
for (Bshift = 0;!(Bmask & 1);Bshift++, Bmask >>= 1);
if (((Rmask + 1) & Rmask) != 0)
{
s->error = DPVSIMPLEDECODEERROR_INVALIDRMASK;
return s->error;
}
if (((Gmask + 1) & Gmask) != 0)
{
s->error = DPVSIMPLEDECODEERROR_INVALIDGMASK;
return s->error;
}
if (((Bmask + 1) & Bmask) != 0)
{
s->error = DPVSIMPLEDECODEERROR_INVALIDBMASK;
return s->error;
}
for (Rbits = 0;Rmask & 1;Rbits++, Rmask >>= 1);
for (Gbits = 0;Gmask & 1;Gbits++, Gmask >>= 1);
for (Bbits = 0;Bmask & 1;Bbits++, Bmask >>= 1);
if (Rbits > 8)
{
Rshift += (Rbits - 8);
Rbits = 8;
}
if (Gbits > 8)
{
Gshift += (Gbits - 8);
Gbits = 8;
}
if (Bbits > 8)
{
Bshift += (Bbits - 8);
Bbits = 8;
}
s->info_imagebpp = bytesperpixel;
s->info_imageRloss = 16 + (8 - Rbits);
s->info_imageGloss = 8 + (8 - Gbits);
s->info_imageBloss = 0 + (8 - Bbits);
s->info_imageRmask = (1 << Rbits) - 1;
s->info_imageGmask = (1 << Gbits) - 1;
s->info_imageBmask = (1 << Bbits) - 1;
s->info_imageRshift = Rshift;
s->info_imageGshift = Gshift;
s->info_imageBshift = Bshift;
s->info_imagesize = s->info_imagewidth * s->info_imageheight * s->info_imagebpp;
return s->error;
}
// opening and closing streams
// opens a stream
void *dpvsimpledecode_open(clvideo_t *video, char *filename, const char **errorstring)
{
dpvsimpledecodestream_t *s;
char t[8], *wavename;
if (errorstring != NULL)
*errorstring = NULL;
s = (dpvsimpledecodestream_t *)Z_Malloc(sizeof(dpvsimpledecodestream_t));
if (s != NULL)
{
s->bitstream = hz_bitstream_read_open(filename);
if (s->bitstream != NULL)
{
// check file identification
s->framedatablocks = hz_bitstream_read_blocks_new();
if (s->framedatablocks != NULL)
{
hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 8);
hz_bitstream_read_bytes(s->framedatablocks, t, 8);
if (!memcmp(t, "DPVideo", 8))
{
// check version number
hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 2);
if (hz_bitstream_read_short(s->framedatablocks) == 1)
{
hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 12);
s->info_imagewidth = hz_bitstream_read_short(s->framedatablocks);
s->info_imageheight = hz_bitstream_read_short(s->framedatablocks);
s->info_framerate = (double) hz_bitstream_read_int(s->framedatablocks) * (1.0 / 65536.0);
s->info_aspectratio = (double)s->info_imagewidth / (double)s->info_imageheight;
if (s->info_framerate > 0.0)
{
s->videopixels = (unsigned int *)Z_Malloc(s->info_imagewidth * s->info_imageheight * sizeof(*s->videopixels));
if (s->videopixels != NULL)
{
size_t namelen;
namelen = strlen(filename) + 10;
wavename = (char *)Z_Malloc(namelen);
if (wavename)
{
sfx_t* sfx;
FS_StripExtension(filename, wavename, namelen);
strlcat(wavename, ".wav", namelen);
sfx = S_PrecacheSound (wavename, false, false);
if (sfx != NULL)
s->sndchan = S_StartSound (-1, 0, sfx, vec3_origin, 1.0f, 0);
else
s->sndchan = -1;
Z_Free(wavename);
}
// all is well...
// set the module functions
s->videoframenum = -10000;
video->close = dpvsimpledecode_close;
video->getwidth = dpvsimpledecode_getwidth;
video->getheight = dpvsimpledecode_getheight;
video->getframerate = dpvsimpledecode_getframerate;
video->decodeframe = dpvsimpledecode_video;
video->getaspectratio = dpvsimpledecode_getaspectratio;
return s;
}
else if (errorstring != NULL)
*errorstring = "unable to allocate video image buffer";
}
else if (errorstring != NULL)
*errorstring = "error in video info chunk";
}
else if (errorstring != NULL)
*errorstring = "read error";
}
else if (errorstring != NULL)
*errorstring = "not a dpvideo file";
hz_bitstream_read_blocks_free(s->framedatablocks);
}
else if (errorstring != NULL)
*errorstring = "unable to allocate memory for reading buffer";
hz_bitstream_read_close(s->bitstream);
}
else if (errorstring != NULL)
*errorstring = "unable to open file";
Z_Free(s);
}
else if (errorstring != NULL)
*errorstring = "unable to allocate memory for stream info structure";
return NULL;
}
// closes a stream
void dpvsimpledecode_close(void *stream)
{
dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
if (s == NULL)
return;
if (s->videopixels)
Z_Free(s->videopixels);
if (s->sndchan != -1)
S_StopChannel (s->sndchan, true, true);
if (s->framedatablocks)
hz_bitstream_read_blocks_free(s->framedatablocks);
if (s->bitstream)
hz_bitstream_read_close(s->bitstream);
Z_Free(s);
}
// utilitarian functions
// returns the current error number for the stream, and resets the error
// number to DPVSIMPLEDECODEERROR_NONE
// if the supplied string pointer variable is not NULL, it will be set to the
// error message
int dpvsimpledecode_error(void *stream, const char **errorstring)
{
dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
int e;
e = s->error;
s->error = 0;
if (errorstring)
{
switch (e)
{
case DPVSIMPLEDECODEERROR_NONE:
*errorstring = "no error";
break;
case DPVSIMPLEDECODEERROR_EOF:
*errorstring = "end of file reached (this is not an error)";
break;
case DPVSIMPLEDECODEERROR_READERROR:
*errorstring = "read error (corrupt or incomplete file)";
break;
case DPVSIMPLEDECODEERROR_SOUNDBUFFERTOOSMALL:
*errorstring = "sound buffer is too small for decoding frame (please allocate it as large as dpvsimpledecode_getneededsoundbufferlength suggests)";
break;
case DPVSIMPLEDECODEERROR_INVALIDRMASK:
*errorstring = "invalid red bits mask";
break;
case DPVSIMPLEDECODEERROR_INVALIDGMASK:
*errorstring = "invalid green bits mask";
break;
case DPVSIMPLEDECODEERROR_INVALIDBMASK:
*errorstring = "invalid blue bits mask";
break;
case DPVSIMPLEDECODEERROR_COLORMASKSOVERLAP:
*errorstring = "color bit masks overlap";
break;
case DPVSIMPLEDECODEERROR_COLORMASKSEXCEEDBPP:
*errorstring = "color masks too big for specified bytes per pixel";
break;
case DPVSIMPLEDECODEERROR_UNSUPPORTEDBPP:
*errorstring = "unsupported bytes per pixel (must be 2 for 16bit, or 4 for 32bit)";
break;
default:
*errorstring = "unknown error";
break;
}
}
return e;
}
// returns the width of the image data
unsigned int dpvsimpledecode_getwidth(void *stream)
{
dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
return s->info_imagewidth;
}
// returns the height of the image data
unsigned int dpvsimpledecode_getheight(void *stream)
{
dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
return s->info_imageheight;
}
// returns the framerate of the stream
double dpvsimpledecode_getframerate(void *stream)
{
dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
return s->info_framerate;
}
// return aspect ratio of the stream
double dpvsimpledecode_getaspectratio(void *stream)
{
dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
return s->info_aspectratio;
}
static int dpvsimpledecode_convertpixels(dpvsimpledecodestream_t *s, void *imagedata, int imagebytesperrow)
{
unsigned int a, x, y, width, height;
unsigned int Rloss, Rmask, Rshift, Gloss, Gmask, Gshift, Bloss, Bmask, Bshift;
unsigned int *in;
width = s->info_imagewidth;
height = s->info_imageheight;
Rloss = s->info_imageRloss;
Rmask = s->info_imageRmask;
Rshift = s->info_imageRshift;
Gloss = s->info_imageGloss;
Gmask = s->info_imageGmask;
Gshift = s->info_imageGshift;
Bloss = s->info_imageBloss;
Bmask = s->info_imageBmask;
Bshift = s->info_imageBshift;
in = s->videopixels;
if (s->info_imagebpp == 4)
{
unsigned int *outrow;
for (y = 0;y < height;y++)
{
outrow = (unsigned int *)((unsigned char *)imagedata + y * imagebytesperrow);
for (x = 0;x < width;x++)
{
a = *in++;
outrow[x] = (((a >> Rloss) & Rmask) << Rshift) | (((a >> Gloss) & Gmask) << Gshift) | (((a >> Bloss) & Bmask) << Bshift);
}
}
}
else
{
unsigned short *outrow;
for (y = 0;y < height;y++)
{
outrow = (unsigned short *)((unsigned char *)imagedata + y * imagebytesperrow);
if (Rloss == 19 && Gloss == 10 && Bloss == 3 && Rshift == 11 && Gshift == 5 && Bshift == 0)
{
// optimized
for (x = 0;x < width;x++)
{
a = *in++;
outrow[x] = ((a >> 8) & 0xF800) | ((a >> 5) & 0x07E0) | ((a >> 3) & 0x001F);
}
}
else
{
for (x = 0;x < width;x++)
{
a = *in++;
outrow[x] = (((a >> Rloss) & Rmask) << Rshift) | (((a >> Gloss) & Gmask) << Gshift) | (((a >> Bloss) & Bmask) << Bshift);
}
}
}
}
return s->error;
}
static int dpvsimpledecode_decompressimage(dpvsimpledecodestream_t *s)
{
int i, a, b, colors, g, x1, y1, bw, bh, width, height, palettebits;
unsigned int palette[256], *outrow, *out;
g = BLOCKSIZE;
width = s->info_imagewidth;
height = s->info_imageheight;
for (y1 = 0;y1 < height;y1 += g)
{
outrow = s->videopixels + y1 * width;
bh = g;
if (y1 + bh > height)
bh = height - y1;
for (x1 = 0;x1 < width;x1 += g)
{
out = outrow + x1;
bw = g;
if (x1 + bw > width)
bw = width - x1;
if (hz_bitstream_read_bit(s->framedatablocks))
{
// updated block
palettebits = hz_bitstream_read_bits(s->framedatablocks, 3);
colors = 1 << palettebits;
for (i = 0;i < colors;i++)
palette[i] = hz_bitstream_read_bits(s->framedatablocks, 24);
if (palettebits)
{
for (b = 0;b < bh;b++, out += width)
for (a = 0;a < bw;a++)
out[a] = palette[hz_bitstream_read_bits(s->framedatablocks, palettebits)];
}
else
{
for (b = 0;b < bh;b++, out += width)
for (a = 0;a < bw;a++)
out[a] = palette[0];
}
}
}
}
return s->error;
}
// decodes a video frame to the supplied output pixels
int dpvsimpledecode_video(void *stream, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow)
{
dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
unsigned int framedatasize;
char t[4];
s->error = DPVSIMPLEDECODEERROR_NONE;
if (dpvsimpledecode_setpixelformat(s, Rmask, Gmask, Bmask, bytesperpixel))
return s->error;
hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 8);
hz_bitstream_read_bytes(s->framedatablocks, t, 4);
if (memcmp(t, "VID0", 4))
{
if (t[0] == 0)
return (s->error = DPVSIMPLEDECODEERROR_EOF);
else
return (s->error = DPVSIMPLEDECODEERROR_READERROR);
}
framedatasize = hz_bitstream_read_int(s->framedatablocks);
hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, framedatasize);
if (dpvsimpledecode_decompressimage(s))
return s->error;
dpvsimpledecode_convertpixels(s, imagedata, imagebytesperrow);
return s->error;
}