3 #include "dpvsimpledecode.h"
5 #define HZREADERROR_OK 0
6 #define HZREADERROR_EOF 1
7 #define HZREADERROR_MALLOCFAILED 2
9 //#define HZREADBLOCKSIZE 16000
10 #define HZREADBLOCKSIZE 1048576
12 typedef struct hz_bitstream_read_s
19 typedef struct hz_bitstream_readblock_s
21 struct hz_bitstream_readblock_s *next;
23 unsigned char data[HZREADBLOCKSIZE];
25 hz_bitstream_readblock_t;
27 typedef struct hz_bitstream_readblocks_s
29 hz_bitstream_readblock_t *blocks;
30 hz_bitstream_readblock_t *current;
31 unsigned int position;
35 hz_bitstream_readblocks_t;
37 hz_bitstream_read_t *hz_bitstream_read_open(char *filename)
40 hz_bitstream_read_t *stream;
41 if ((file = FS_Open (filename, "rb", false, false)))
43 stream = (hz_bitstream_read_t *)Z_Malloc(sizeof(hz_bitstream_read_t));
44 memset(stream, 0, sizeof(*stream));
52 void hz_bitstream_read_close(hz_bitstream_read_t *stream)
56 FS_Close(stream->file);
61 hz_bitstream_readblocks_t *hz_bitstream_read_blocks_new(void)
63 hz_bitstream_readblocks_t *blocks;
64 blocks = (hz_bitstream_readblocks_t *)Z_Malloc(sizeof(hz_bitstream_readblocks_t));
67 memset(blocks, 0, sizeof(hz_bitstream_readblocks_t));
71 void hz_bitstream_read_blocks_free(hz_bitstream_readblocks_t *blocks)
73 hz_bitstream_readblock_t *b, *n;
76 for (b = blocks->blocks;b;b = n)
84 void hz_bitstream_read_flushbits(hz_bitstream_readblocks_t *blocks)
90 int hz_bitstream_read_blocks_read(hz_bitstream_readblocks_t *blocks, hz_bitstream_read_t *stream, unsigned int size)
93 hz_bitstream_readblock_t *b, *p;
101 b = (hz_bitstream_readblock_t *)Z_Malloc(sizeof(hz_bitstream_readblock_t));
103 return HZREADERROR_MALLOCFAILED;
111 if (s > HZREADBLOCKSIZE)
112 b->size = HZREADBLOCKSIZE;
116 if (FS_Read(stream->file, b->data, b->size) != (fs_offset_t)b->size)
118 stream->endoffile = 1;
129 blocks->current = blocks->blocks;
130 blocks->position = 0;
131 hz_bitstream_read_flushbits(blocks);
132 if (stream->endoffile)
133 return HZREADERROR_EOF;
134 return HZREADERROR_OK;
137 unsigned int hz_bitstream_read_blocks_getbyte(hz_bitstream_readblocks_t *blocks)
139 while (blocks->current != NULL && blocks->position >= blocks->current->size)
141 blocks->position = 0;
142 blocks->current = blocks->current->next;
144 if (blocks->current == NULL)
146 return blocks->current->data[blocks->position++];
149 int hz_bitstream_read_bit(hz_bitstream_readblocks_t *blocks)
155 blocks->store |= hz_bitstream_read_blocks_getbyte(blocks) & 0xFF;
158 return (blocks->store >> blocks->count) & 1;
161 unsigned int hz_bitstream_read_bits(hz_bitstream_readblocks_t *blocks, int size)
163 unsigned int num = 0;
164 // we can only handle about 24 bits at a time safely
165 // (there might be up to 7 bits more than we need in the bit store)
169 num |= hz_bitstream_read_bits(blocks, 8) << size;
171 while (blocks->count < size)
175 blocks->store |= hz_bitstream_read_blocks_getbyte(blocks) & 0xFF;
177 blocks->count -= size;
178 num |= (blocks->store >> blocks->count) & ((1 << size) - 1);
182 unsigned int hz_bitstream_read_byte(hz_bitstream_readblocks_t *blocks)
184 return hz_bitstream_read_blocks_getbyte(blocks);
187 unsigned int hz_bitstream_read_short(hz_bitstream_readblocks_t *blocks)
189 return (hz_bitstream_read_byte(blocks) << 8)
190 | (hz_bitstream_read_byte(blocks));
193 unsigned int hz_bitstream_read_int(hz_bitstream_readblocks_t *blocks)
195 return (hz_bitstream_read_byte(blocks) << 24)
196 | (hz_bitstream_read_byte(blocks) << 16)
197 | (hz_bitstream_read_byte(blocks) << 8)
198 | (hz_bitstream_read_byte(blocks));
201 void hz_bitstream_read_bytes(hz_bitstream_readblocks_t *blocks, void *outdata, unsigned int size)
204 out = (unsigned char *)outdata;
206 *out++ = hz_bitstream_read_byte(blocks);
211 typedef struct dpvsimpledecodestream_s
213 hz_bitstream_read_t *bitstream;
214 hz_bitstream_readblocks_t *framedatablocks;
218 double info_framerate;
219 unsigned int info_frames;
221 unsigned int info_imagewidth;
222 unsigned int info_imageheight;
223 unsigned int info_imagebpp;
224 unsigned int info_imageRloss;
225 unsigned int info_imageRmask;
226 unsigned int info_imageRshift;
227 unsigned int info_imageGloss;
228 unsigned int info_imageGmask;
229 unsigned int info_imageGshift;
230 unsigned int info_imageBloss;
231 unsigned int info_imageBmask;
232 unsigned int info_imageBshift;
233 unsigned int info_imagesize;
235 // current video frame (needed because of delta compression)
237 // current video frame data (needed because of delta compression)
238 unsigned int *videopixels;
240 // channel the sound file is being played on
243 dpvsimpledecodestream_t;
245 static int dpvsimpledecode_setpixelformat(dpvsimpledecodestream_t *s, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel)
247 int Rshift, Rbits, Gshift, Gbits, Bshift, Bbits;
250 s->error = DPVSIMPLEDECODEERROR_INVALIDRMASK;
255 s->error = DPVSIMPLEDECODEERROR_INVALIDGMASK;
260 s->error = DPVSIMPLEDECODEERROR_INVALIDBMASK;
263 if (Rmask & Gmask || Rmask & Bmask || Gmask & Bmask)
265 s->error = DPVSIMPLEDECODEERROR_COLORMASKSOVERLAP;
268 switch (bytesperpixel)
271 if ((Rmask | Gmask | Bmask) > 65536)
273 s->error = DPVSIMPLEDECODEERROR_COLORMASKSEXCEEDBPP;
280 s->error = DPVSIMPLEDECODEERROR_UNSUPPORTEDBPP;
284 for (Rshift = 0;!(Rmask & 1);Rshift++, Rmask >>= 1);
285 for (Gshift = 0;!(Gmask & 1);Gshift++, Gmask >>= 1);
286 for (Bshift = 0;!(Bmask & 1);Bshift++, Bmask >>= 1);
287 if (((Rmask + 1) & Rmask) != 0)
289 s->error = DPVSIMPLEDECODEERROR_INVALIDRMASK;
292 if (((Gmask + 1) & Gmask) != 0)
294 s->error = DPVSIMPLEDECODEERROR_INVALIDGMASK;
297 if (((Bmask + 1) & Bmask) != 0)
299 s->error = DPVSIMPLEDECODEERROR_INVALIDBMASK;
302 for (Rbits = 0;Rmask & 1;Rbits++, Rmask >>= 1);
303 for (Gbits = 0;Gmask & 1;Gbits++, Gmask >>= 1);
304 for (Bbits = 0;Bmask & 1;Bbits++, Bmask >>= 1);
307 Rshift += (Rbits - 8);
312 Gshift += (Gbits - 8);
317 Bshift += (Bbits - 8);
320 s->info_imagebpp = bytesperpixel;
321 s->info_imageRloss = 16 + (8 - Rbits);
322 s->info_imageGloss = 8 + (8 - Gbits);
323 s->info_imageBloss = 0 + (8 - Bbits);
324 s->info_imageRmask = (1 << Rbits) - 1;
325 s->info_imageGmask = (1 << Gbits) - 1;
326 s->info_imageBmask = (1 << Bbits) - 1;
327 s->info_imageRshift = Rshift;
328 s->info_imageGshift = Gshift;
329 s->info_imageBshift = Bshift;
330 s->info_imagesize = s->info_imagewidth * s->info_imageheight * s->info_imagebpp;
334 // opening and closing streams
336 static void StripExtension(char *in, char *out)
342 if (*c == ':' || *c == '\\' || *c == '/')
355 memcpy(out, in, dot - in);
361 void *dpvsimpledecode_open(char *filename, char **errorstring)
363 dpvsimpledecodestream_t *s;
364 char t[8], *wavename;
365 if (errorstring != NULL)
367 s = (dpvsimpledecodestream_t *)Z_Malloc(sizeof(dpvsimpledecodestream_t));
370 s->bitstream = hz_bitstream_read_open(filename);
371 if (s->bitstream != NULL)
373 // check file identification
374 s->framedatablocks = hz_bitstream_read_blocks_new();
375 if (s->framedatablocks != NULL)
377 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 8);
378 hz_bitstream_read_bytes(s->framedatablocks, t, 8);
379 if (!memcmp(t, "DPVideo", 8))
381 // check version number
382 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 2);
383 if (hz_bitstream_read_short(s->framedatablocks) == 1)
385 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 12);
386 s->info_imagewidth = hz_bitstream_read_short(s->framedatablocks);
387 s->info_imageheight = hz_bitstream_read_short(s->framedatablocks);
388 s->info_framerate = (double) hz_bitstream_read_int(s->framedatablocks) * (1.0 / 65536.0);
390 if (s->info_framerate > 0.0)
392 s->videopixels = (unsigned int *)Z_Malloc(s->info_imagewidth * s->info_imageheight * sizeof(*s->videopixels));
393 if (s->videopixels != NULL)
397 namelen = strlen(filename) + 10;
398 wavename = (char *)Z_Malloc(namelen);
403 StripExtension(filename, wavename);
404 strlcat(wavename, ".wav", namelen);
405 sfx = S_PrecacheSound (wavename, false, false);
407 s->sndchan = S_StartSound (-1, 0, sfx, vec3_origin, 1.0f, 0);
413 s->videoframenum = -10000;
416 else if (errorstring != NULL)
417 *errorstring = "unable to allocate video image buffer";
419 else if (errorstring != NULL)
420 *errorstring = "error in video info chunk";
422 else if (errorstring != NULL)
423 *errorstring = "read error";
425 else if (errorstring != NULL)
426 *errorstring = "not a dpvideo file";
427 hz_bitstream_read_blocks_free(s->framedatablocks);
429 else if (errorstring != NULL)
430 *errorstring = "unable to allocate memory for reading buffer";
431 hz_bitstream_read_close(s->bitstream);
433 else if (errorstring != NULL)
434 *errorstring = "unable to open file";
437 else if (errorstring != NULL)
438 *errorstring = "unable to allocate memory for stream info structure";
443 void dpvsimpledecode_close(void *stream)
445 dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
449 Z_Free(s->videopixels);
450 if (s->sndchan != -1)
451 S_StopChannel (s->sndchan);
452 if (s->framedatablocks)
453 hz_bitstream_read_blocks_free(s->framedatablocks);
455 hz_bitstream_read_close(s->bitstream);
459 // utilitarian functions
461 // returns the current error number for the stream, and resets the error
462 // number to DPVSIMPLEDECODEERROR_NONE
463 // if the supplied string pointer variable is not NULL, it will be set to the
465 int dpvsimpledecode_error(void *stream, char **errorstring)
467 dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
475 case DPVSIMPLEDECODEERROR_NONE:
476 *errorstring = "no error";
478 case DPVSIMPLEDECODEERROR_EOF:
479 *errorstring = "end of file reached (this is not an error)";
481 case DPVSIMPLEDECODEERROR_READERROR:
482 *errorstring = "read error (corrupt or incomplete file)";
484 case DPVSIMPLEDECODEERROR_SOUNDBUFFERTOOSMALL:
485 *errorstring = "sound buffer is too small for decoding frame (please allocate it as large as dpvsimpledecode_getneededsoundbufferlength suggests)";
487 case DPVSIMPLEDECODEERROR_INVALIDRMASK:
488 *errorstring = "invalid red bits mask";
490 case DPVSIMPLEDECODEERROR_INVALIDGMASK:
491 *errorstring = "invalid green bits mask";
493 case DPVSIMPLEDECODEERROR_INVALIDBMASK:
494 *errorstring = "invalid blue bits mask";
496 case DPVSIMPLEDECODEERROR_COLORMASKSOVERLAP:
497 *errorstring = "color bit masks overlap";
499 case DPVSIMPLEDECODEERROR_COLORMASKSEXCEEDBPP:
500 *errorstring = "color masks too big for specified bytes per pixel";
502 case DPVSIMPLEDECODEERROR_UNSUPPORTEDBPP:
503 *errorstring = "unsupported bytes per pixel (must be 2 for 16bit, or 4 for 32bit)";
506 *errorstring = "unknown error";
513 // returns the width of the image data
514 unsigned int dpvsimpledecode_getwidth(void *stream)
516 dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
517 return s->info_imagewidth;
520 // returns the height of the image data
521 unsigned int dpvsimpledecode_getheight(void *stream)
523 dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
524 return s->info_imageheight;
527 // returns the framerate of the stream
528 double dpvsimpledecode_getframerate(void *stream)
530 dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
531 return s->info_framerate;
538 static int dpvsimpledecode_convertpixels(dpvsimpledecodestream_t *s, void *imagedata, int imagebytesperrow)
540 unsigned int a, x, y, width, height;
541 unsigned int Rloss, Rmask, Rshift, Gloss, Gmask, Gshift, Bloss, Bmask, Bshift;
544 width = s->info_imagewidth;
545 height = s->info_imageheight;
547 Rloss = s->info_imageRloss;
548 Rmask = s->info_imageRmask;
549 Rshift = s->info_imageRshift;
550 Gloss = s->info_imageGloss;
551 Gmask = s->info_imageGmask;
552 Gshift = s->info_imageGshift;
553 Bloss = s->info_imageBloss;
554 Bmask = s->info_imageBmask;
555 Bshift = s->info_imageBshift;
558 if (s->info_imagebpp == 4)
560 unsigned int *outrow;
561 for (y = 0;y < height;y++)
563 outrow = (unsigned int *)((unsigned char *)imagedata + y * imagebytesperrow);
564 for (x = 0;x < width;x++)
567 outrow[x] = (((a >> Rloss) & Rmask) << Rshift) | (((a >> Gloss) & Gmask) << Gshift) | (((a >> Bloss) & Bmask) << Bshift);
573 unsigned short *outrow;
574 for (y = 0;y < height;y++)
576 outrow = (unsigned short *)((unsigned char *)imagedata + y * imagebytesperrow);
577 if (Rloss == 19 && Gloss == 10 && Bloss == 3 && Rshift == 11 && Gshift == 5 && Bshift == 0)
580 for (x = 0;x < width;x++)
583 outrow[x] = ((a >> 8) & 0xF800) | ((a >> 5) & 0x07E0) | ((a >> 3) & 0x001F);
588 for (x = 0;x < width;x++)
591 outrow[x] = (((a >> Rloss) & Rmask) << Rshift) | (((a >> Gloss) & Gmask) << Gshift) | (((a >> Bloss) & Bmask) << Bshift);
599 static int dpvsimpledecode_decompressimage(dpvsimpledecodestream_t *s)
601 int i, a, b, colors, g, x1, y1, bw, bh, width, height, palettebits;
602 unsigned int palette[256], *outrow, *out;
604 width = s->info_imagewidth;
605 height = s->info_imageheight;
606 for (y1 = 0;y1 < height;y1 += g)
608 outrow = s->videopixels + y1 * width;
610 if (y1 + bh > height)
612 for (x1 = 0;x1 < width;x1 += g)
618 if (hz_bitstream_read_bit(s->framedatablocks))
621 palettebits = hz_bitstream_read_bits(s->framedatablocks, 3);
622 colors = 1 << palettebits;
623 for (i = 0;i < colors;i++)
624 palette[i] = hz_bitstream_read_bits(s->framedatablocks, 24);
627 for (b = 0;b < bh;b++, out += width)
628 for (a = 0;a < bw;a++)
629 out[a] = palette[hz_bitstream_read_bits(s->framedatablocks, palettebits)];
633 for (b = 0;b < bh;b++, out += width)
634 for (a = 0;a < bw;a++)
643 // decodes a video frame to the supplied output pixels
644 int dpvsimpledecode_video(void *stream, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow)
646 dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
647 unsigned int framedatasize;
649 s->error = DPVSIMPLEDECODEERROR_NONE;
650 if (dpvsimpledecode_setpixelformat(s, Rmask, Gmask, Bmask, bytesperpixel))
653 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 8);
654 hz_bitstream_read_bytes(s->framedatablocks, t, 4);
655 if (memcmp(t, "VID0", 4))
658 return (s->error = DPVSIMPLEDECODEERROR_EOF);
660 return (s->error = DPVSIMPLEDECODEERROR_READERROR);
662 framedatasize = hz_bitstream_read_int(s->framedatablocks);
663 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, framedatasize);
664 if (dpvsimpledecode_decompressimage(s))
667 dpvsimpledecode_convertpixels(s, imagedata, imagebytesperrow);