3 #include "dpvsimpledecode.h"
5 #define HZREADERROR_OK 0
6 #define HZREADERROR_EOF 1
7 #define HZREADERROR_MALLOCFAILED 2
9 #define HZREADBLOCKSIZE 16000
18 typedef struct hz_bitstream_readblock_s
20 struct hz_bitstream_readblock_s *next;
22 unsigned char data[HZREADBLOCKSIZE];
24 hz_bitstream_readblock_t;
28 hz_bitstream_readblock_t *blocks;
29 hz_bitstream_readblock_t *current;
30 unsigned int position;
34 hz_bitstream_readblocks_t;
36 hz_bitstream_read_t *hz_bitstream_read_open(char *filename)
39 hz_bitstream_read_t *stream;
40 if ((file = FS_Open (filename, "rb", false)))
42 stream = malloc(sizeof(hz_bitstream_read_t));
43 memset(stream, 0, sizeof(*stream));
51 void hz_bitstream_read_close(hz_bitstream_read_t *stream)
55 FS_Close(stream->file);
60 hz_bitstream_readblocks_t *hz_bitstream_read_blocks_new(void)
62 hz_bitstream_readblocks_t *blocks;
63 blocks = malloc(sizeof(hz_bitstream_readblocks_t));
66 memset(blocks, 0, sizeof(hz_bitstream_readblocks_t));
70 void hz_bitstream_read_blocks_free(hz_bitstream_readblocks_t *blocks)
72 hz_bitstream_readblock_t *b, *n;
75 for (b = blocks->blocks;b;b = n)
83 void hz_bitstream_read_flushbits(hz_bitstream_readblocks_t *blocks)
89 int hz_bitstream_read_blocks_read(hz_bitstream_readblocks_t *blocks, hz_bitstream_read_t *stream, unsigned int size)
92 hz_bitstream_readblock_t *b, *p;
100 b = malloc(sizeof(hz_bitstream_readblock_t));
102 return HZREADERROR_MALLOCFAILED;
110 if (s > HZREADBLOCKSIZE)
111 b->size = HZREADBLOCKSIZE;
115 if (FS_Read(stream->file, b->data, b->size) != b->size)
117 stream->endoffile = 1;
128 blocks->current = blocks->blocks;
129 blocks->position = 0;
130 hz_bitstream_read_flushbits(blocks);
131 if (stream->endoffile)
132 return HZREADERROR_EOF;
133 return HZREADERROR_OK;
136 unsigned int hz_bitstream_read_blocks_getbyte(hz_bitstream_readblocks_t *blocks)
138 while (blocks->current != NULL && blocks->position >= blocks->current->size)
140 blocks->position = 0;
141 blocks->current = blocks->current->next;
143 if (blocks->current == NULL)
145 return blocks->current->data[blocks->position++];
148 int hz_bitstream_read_bit(hz_bitstream_readblocks_t *blocks)
154 blocks->store |= hz_bitstream_read_blocks_getbyte(blocks) & 0xFF;
157 return (blocks->store >> blocks->count) & 1;
160 unsigned int hz_bitstream_read_bits(hz_bitstream_readblocks_t *blocks, int size)
162 unsigned int num = 0;
163 // we can only handle about 24 bits at a time safely
164 // (there might be up to 7 bits more than we need in the bit store)
168 num |= hz_bitstream_read_bits(blocks, 8) << size;
170 while (blocks->count < size)
174 blocks->store |= hz_bitstream_read_blocks_getbyte(blocks) & 0xFF;
176 blocks->count -= size;
177 num |= (blocks->store >> blocks->count) & ((1 << size) - 1);
181 unsigned int hz_bitstream_read_byte(hz_bitstream_readblocks_t *blocks)
183 return hz_bitstream_read_blocks_getbyte(blocks);
186 unsigned int hz_bitstream_read_short(hz_bitstream_readblocks_t *blocks)
188 return (hz_bitstream_read_byte(blocks) << 8)
189 | (hz_bitstream_read_byte(blocks));
192 unsigned int hz_bitstream_read_int(hz_bitstream_readblocks_t *blocks)
194 return (hz_bitstream_read_byte(blocks) << 24)
195 | (hz_bitstream_read_byte(blocks) << 16)
196 | (hz_bitstream_read_byte(blocks) << 8)
197 | (hz_bitstream_read_byte(blocks));
200 void hz_bitstream_read_bytes(hz_bitstream_readblocks_t *blocks, void *outdata, unsigned int size)
205 *out++ = hz_bitstream_read_byte(blocks);
210 typedef struct dpvsimpledecodestream_s
212 hz_bitstream_read_t *bitstream;
213 hz_bitstream_readblocks_t *framedatablocks;
217 double info_framerate;
218 unsigned int info_frames;
220 unsigned int info_imagewidth;
221 unsigned int info_imageheight;
222 unsigned int info_imagebpp;
223 unsigned int info_imageRloss;
224 unsigned int info_imageRmask;
225 unsigned int info_imageRshift;
226 unsigned int info_imageGloss;
227 unsigned int info_imageGmask;
228 unsigned int info_imageGshift;
229 unsigned int info_imageBloss;
230 unsigned int info_imageBmask;
231 unsigned int info_imageBshift;
232 unsigned int info_imagesize;
234 // current video frame (needed because of delta compression)
236 // current video frame data (needed because of delta compression)
237 unsigned int *videopixels;
239 // channel the sound file is being played on
242 dpvsimpledecodestream_t;
244 static int dpvsimpledecode_setpixelformat(dpvsimpledecodestream_t *s, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel)
246 int Rshift, Rbits, Gshift, Gbits, Bshift, Bbits;
249 s->error = DPVSIMPLEDECODEERROR_INVALIDRMASK;
254 s->error = DPVSIMPLEDECODEERROR_INVALIDGMASK;
259 s->error = DPVSIMPLEDECODEERROR_INVALIDBMASK;
262 if (Rmask & Gmask || Rmask & Bmask || Gmask & Bmask)
264 s->error = DPVSIMPLEDECODEERROR_COLORMASKSOVERLAP;
267 switch (bytesperpixel)
270 if ((Rmask | Gmask | Bmask) > 65536)
272 s->error = DPVSIMPLEDECODEERROR_COLORMASKSEXCEEDBPP;
279 s->error = DPVSIMPLEDECODEERROR_UNSUPPORTEDBPP;
283 for (Rshift = 0;!(Rmask & 1);Rshift++, Rmask >>= 1);
284 for (Gshift = 0;!(Gmask & 1);Gshift++, Gmask >>= 1);
285 for (Bshift = 0;!(Bmask & 1);Bshift++, Bmask >>= 1);
286 if (((Rmask + 1) & Rmask) != 0)
288 s->error = DPVSIMPLEDECODEERROR_INVALIDRMASK;
291 if (((Gmask + 1) & Gmask) != 0)
293 s->error = DPVSIMPLEDECODEERROR_INVALIDGMASK;
296 if (((Bmask + 1) & Bmask) != 0)
298 s->error = DPVSIMPLEDECODEERROR_INVALIDBMASK;
301 for (Rbits = 0;Rmask & 1;Rbits++, Rmask >>= 1);
302 for (Gbits = 0;Gmask & 1;Gbits++, Gmask >>= 1);
303 for (Bbits = 0;Bmask & 1;Bbits++, Bmask >>= 1);
306 Rshift += (Rbits - 8);
311 Gshift += (Gbits - 8);
316 Bshift += (Bbits - 8);
319 s->info_imagebpp = bytesperpixel;
320 s->info_imageRloss = 16 + (8 - Rbits);
321 s->info_imageGloss = 8 + (8 - Gbits);
322 s->info_imageBloss = 0 + (8 - Bbits);
323 s->info_imageRmask = (1 << Rbits) - 1;
324 s->info_imageGmask = (1 << Gbits) - 1;
325 s->info_imageBmask = (1 << Bbits) - 1;
326 s->info_imageRshift = Rshift;
327 s->info_imageGshift = Gshift;
328 s->info_imageBshift = Bshift;
329 s->info_imagesize = s->info_imagewidth * s->info_imageheight * s->info_imagebpp;
333 // opening and closing streams
335 static void StripExtension(char *in, char *out)
341 if (*c == ':' || *c == '\\' || *c == '/')
354 memcpy(out, in, dot - in);
360 void *dpvsimpledecode_open(char *filename, char **errorstring)
362 dpvsimpledecodestream_t *s;
363 char t[8], *wavename;
364 if (errorstring != NULL)
366 s = malloc(sizeof(dpvsimpledecodestream_t));
369 s->bitstream = hz_bitstream_read_open(filename);
370 if (s->bitstream != NULL)
372 // check file identification
373 s->framedatablocks = hz_bitstream_read_blocks_new();
374 if (s->framedatablocks != NULL)
376 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 8);
377 hz_bitstream_read_bytes(s->framedatablocks, t, 8);
378 if (!memcmp(t, "DPVideo", 8))
380 // check version number
381 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 2);
382 if (hz_bitstream_read_short(s->framedatablocks) == 1)
384 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 12);
385 s->info_imagewidth = hz_bitstream_read_short(s->framedatablocks);
386 s->info_imageheight = hz_bitstream_read_short(s->framedatablocks);
387 s->info_framerate = (double) hz_bitstream_read_int(s->framedatablocks) * (1.0 / 65536.0);
389 if (s->info_framerate > 0.0)
391 s->videopixels = malloc(s->info_imagewidth * s->info_imageheight * sizeof(*s->videopixels));
392 if (s->videopixels != NULL)
394 wavename = malloc(strlen(filename) + 10);
399 StripExtension(filename, wavename);
400 strcat(wavename, ".wav");
401 sfx = S_PrecacheSound (wavename, false, false, false);
403 s->sndchan = S_StartSound (-1, 0, sfx, vec3_origin, 1.0f, 0);
409 s->videoframenum = -10000;
412 else if (errorstring != NULL)
413 *errorstring = "unable to allocate video image buffer";
415 else if (errorstring != NULL)
416 *errorstring = "error in video info chunk";
418 else if (errorstring != NULL)
419 *errorstring = "read error";
421 else if (errorstring != NULL)
422 *errorstring = "not a dpvideo file";
423 hz_bitstream_read_blocks_free(s->framedatablocks);
425 else if (errorstring != NULL)
426 *errorstring = "unable to allocate memory for reading buffer";
427 hz_bitstream_read_close(s->bitstream);
429 else if (errorstring != NULL)
430 *errorstring = "unable to open file";
433 else if (errorstring != NULL)
434 *errorstring = "unable to allocate memory for stream info structure";
439 void dpvsimpledecode_close(void *stream)
441 dpvsimpledecodestream_t *s = stream;
445 free(s->videopixels);
446 if (s->sndchan != -1)
447 S_StopChannel (s->sndchan);
448 if (s->framedatablocks)
449 hz_bitstream_read_blocks_free(s->framedatablocks);
451 hz_bitstream_read_close(s->bitstream);
455 // utilitarian functions
457 // returns the current error number for the stream, and resets the error
458 // number to DPVSIMPLEDECODEERROR_NONE
459 // if the supplied string pointer variable is not NULL, it will be set to the
461 int dpvsimpledecode_error(void *stream, char **errorstring)
463 dpvsimpledecodestream_t *s = stream;
471 case DPVSIMPLEDECODEERROR_NONE:
472 *errorstring = "no error";
474 case DPVSIMPLEDECODEERROR_EOF:
475 *errorstring = "end of file reached (this is not an error)";
477 case DPVSIMPLEDECODEERROR_READERROR:
478 *errorstring = "read error (corrupt or incomplete file)";
480 case DPVSIMPLEDECODEERROR_SOUNDBUFFERTOOSMALL:
481 *errorstring = "sound buffer is too small for decoding frame (please allocate it as large as dpvsimpledecode_getneededsoundbufferlength suggests)";
483 case DPVSIMPLEDECODEERROR_INVALIDRMASK:
484 *errorstring = "invalid red bits mask";
486 case DPVSIMPLEDECODEERROR_INVALIDGMASK:
487 *errorstring = "invalid green bits mask";
489 case DPVSIMPLEDECODEERROR_INVALIDBMASK:
490 *errorstring = "invalid blue bits mask";
492 case DPVSIMPLEDECODEERROR_COLORMASKSOVERLAP:
493 *errorstring = "color bit masks overlap";
495 case DPVSIMPLEDECODEERROR_COLORMASKSEXCEEDBPP:
496 *errorstring = "color masks too big for specified bytes per pixel";
498 case DPVSIMPLEDECODEERROR_UNSUPPORTEDBPP:
499 *errorstring = "unsupported bytes per pixel (must be 2 for 16bit, or 4 for 32bit)";
502 *errorstring = "unknown error";
509 // returns the width of the image data
510 unsigned int dpvsimpledecode_getwidth(void *stream)
512 dpvsimpledecodestream_t *s = stream;
513 return s->info_imagewidth;
516 // returns the height of the image data
517 unsigned int dpvsimpledecode_getheight(void *stream)
519 dpvsimpledecodestream_t *s = stream;
520 return s->info_imageheight;
523 // returns the framerate of the stream
524 double dpvsimpledecode_getframerate(void *stream)
526 dpvsimpledecodestream_t *s = stream;
527 return s->info_framerate;
534 static int dpvsimpledecode_convertpixels(dpvsimpledecodestream_t *s, void *imagedata, int imagebytesperrow)
536 unsigned int a, x, y, width, height;
537 unsigned int Rloss, Rmask, Rshift, Gloss, Gmask, Gshift, Bloss, Bmask, Bshift;
540 width = s->info_imagewidth;
541 height = s->info_imageheight;
543 Rloss = s->info_imageRloss;
544 Rmask = s->info_imageRmask;
545 Rshift = s->info_imageRshift;
546 Gloss = s->info_imageGloss;
547 Gmask = s->info_imageGmask;
548 Gshift = s->info_imageGshift;
549 Bloss = s->info_imageBloss;
550 Bmask = s->info_imageBmask;
551 Bshift = s->info_imageBshift;
554 if (s->info_imagebpp == 4)
556 unsigned int *outrow;
557 for (y = 0;y < height;y++)
559 outrow = (void *)((unsigned char *)imagedata + y * imagebytesperrow);
560 for (x = 0;x < width;x++)
563 outrow[x] = (((a >> Rloss) & Rmask) << Rshift) | (((a >> Gloss) & Gmask) << Gshift) | (((a >> Bloss) & Bmask) << Bshift);
569 unsigned short *outrow;
570 for (y = 0;y < height;y++)
572 outrow = (void *)((unsigned char *)imagedata + y * imagebytesperrow);
573 if (Rloss == 19 && Gloss == 10 && Bloss == 3 && Rshift == 11 && Gshift == 5 && Bshift == 0)
576 for (x = 0;x < width;x++)
579 outrow[x] = ((a >> 8) & 0xF800) | ((a >> 5) & 0x07E0) | ((a >> 3) & 0x001F);
584 for (x = 0;x < width;x++)
587 outrow[x] = (((a >> Rloss) & Rmask) << Rshift) | (((a >> Gloss) & Gmask) << Gshift) | (((a >> Bloss) & Bmask) << Bshift);
595 static int dpvsimpledecode_decompressimage(dpvsimpledecodestream_t *s)
597 int i, a, b, colors, g, x1, y1, bw, bh, width, height, palettebits;
598 unsigned int palette[256], *outrow, *out;
600 width = s->info_imagewidth;
601 height = s->info_imageheight;
602 for (y1 = 0;y1 < height;y1 += g)
604 outrow = s->videopixels + y1 * width;
606 if (y1 + bh > height)
608 for (x1 = 0;x1 < width;x1 += g)
614 if (hz_bitstream_read_bit(s->framedatablocks))
617 palettebits = hz_bitstream_read_bits(s->framedatablocks, 3);
618 colors = 1 << palettebits;
619 for (i = 0;i < colors;i++)
620 palette[i] = hz_bitstream_read_bits(s->framedatablocks, 24);
623 for (b = 0;b < bh;b++, out += width)
624 for (a = 0;a < bw;a++)
625 out[a] = palette[hz_bitstream_read_bits(s->framedatablocks, palettebits)];
629 for (b = 0;b < bh;b++, out += width)
630 for (a = 0;a < bw;a++)
639 // decodes a video frame to the supplied output pixels
640 int dpvsimpledecode_video(void *stream, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow)
642 dpvsimpledecodestream_t *s = stream;
643 unsigned int framedatasize;
645 s->error = DPVSIMPLEDECODEERROR_NONE;
646 if (dpvsimpledecode_setpixelformat(s, Rmask, Gmask, Bmask, bytesperpixel))
649 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 8);
650 hz_bitstream_read_bytes(s->framedatablocks, t, 4);
651 if (memcmp(t, "VID0", 4))
654 return (s->error = DPVSIMPLEDECODEERROR_EOF);
656 return (s->error = DPVSIMPLEDECODEERROR_READERROR);
658 framedatasize = hz_bitstream_read_int(s->framedatablocks);
659 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, framedatasize);
660 if (dpvsimpledecode_decompressimage(s))
663 dpvsimpledecode_convertpixels(s, imagedata, imagebytesperrow);