6 #include "dpvsimpledecode.h"
9 #define EMBEDDEDHZREAD 1
11 #ifndef EMBEDDEDHZREAD
15 #define HZREADERROR_OK 0
16 #define HZREADERROR_EOF 1
17 #define HZREADERROR_MALLOCFAILED 2
19 #define HZREADBLOCKSIZE 16000
28 typedef struct hz_bitstream_readblock_s
30 struct hz_bitstream_readblock_s *next;
32 unsigned char data[HZREADBLOCKSIZE];
34 hz_bitstream_readblock_t;
38 hz_bitstream_readblock_t *blocks;
39 hz_bitstream_readblock_t *current;
40 unsigned int position;
44 hz_bitstream_readblocks_t;
46 hz_bitstream_read_t *hz_bitstream_read_open(char *filename)
49 hz_bitstream_read_t *stream;
50 if ((file = FS_Open (filename, "rb", false)))
52 stream = malloc(sizeof(hz_bitstream_read_t));
53 memset(stream, 0, sizeof(*stream));
61 void hz_bitstream_read_close(hz_bitstream_read_t *stream)
65 FS_Close(stream->file);
70 hz_bitstream_readblocks_t *hz_bitstream_read_blocks_new(void)
72 hz_bitstream_readblocks_t *blocks;
73 blocks = malloc(sizeof(hz_bitstream_readblocks_t));
76 memset(blocks, 0, sizeof(hz_bitstream_readblocks_t));
80 void hz_bitstream_read_blocks_free(hz_bitstream_readblocks_t *blocks)
82 hz_bitstream_readblock_t *b, *n;
85 for (b = blocks->blocks;b;b = n)
93 void hz_bitstream_read_flushbits(hz_bitstream_readblocks_t *blocks)
99 int hz_bitstream_read_blocks_read(hz_bitstream_readblocks_t *blocks, hz_bitstream_read_t *stream, unsigned int size)
102 hz_bitstream_readblock_t *b, *p;
110 b = malloc(sizeof(hz_bitstream_readblock_t));
112 return HZREADERROR_MALLOCFAILED;
120 if (s > HZREADBLOCKSIZE)
121 b->size = HZREADBLOCKSIZE;
125 if (FS_Read(stream->file, b->data, b->size) != b->size)
127 stream->endoffile = 1;
138 blocks->current = blocks->blocks;
139 blocks->position = 0;
140 hz_bitstream_read_flushbits(blocks);
141 if (stream->endoffile)
142 return HZREADERROR_EOF;
143 return HZREADERROR_OK;
146 unsigned int hz_bitstream_read_blocks_getbyte(hz_bitstream_readblocks_t *blocks)
148 while (blocks->current != NULL && blocks->position >= blocks->current->size)
150 blocks->position = 0;
151 blocks->current = blocks->current->next;
153 if (blocks->current == NULL)
155 return blocks->current->data[blocks->position++];
158 int hz_bitstream_read_bit(hz_bitstream_readblocks_t *blocks)
164 blocks->store |= hz_bitstream_read_blocks_getbyte(blocks) & 0xFF;
167 return (blocks->store >> blocks->count) & 1;
170 unsigned int hz_bitstream_read_bits(hz_bitstream_readblocks_t *blocks, int size)
172 unsigned int num = 0;
173 // we can only handle about 24 bits at a time safely
174 // (there might be up to 7 bits more than we need in the bit store)
178 num |= hz_bitstream_read_bits(blocks, 8) << size;
180 while (blocks->count < size)
184 blocks->store |= hz_bitstream_read_blocks_getbyte(blocks) & 0xFF;
186 blocks->count -= size;
187 num |= (blocks->store >> blocks->count) & ((1 << size) - 1);
191 unsigned int hz_bitstream_read_byte(hz_bitstream_readblocks_t *blocks)
193 return hz_bitstream_read_blocks_getbyte(blocks);
196 unsigned int hz_bitstream_read_short(hz_bitstream_readblocks_t *blocks)
198 return (hz_bitstream_read_byte(blocks) << 8)
199 | (hz_bitstream_read_byte(blocks));
202 unsigned int hz_bitstream_read_int(hz_bitstream_readblocks_t *blocks)
204 return (hz_bitstream_read_byte(blocks) << 24)
205 | (hz_bitstream_read_byte(blocks) << 16)
206 | (hz_bitstream_read_byte(blocks) << 8)
207 | (hz_bitstream_read_byte(blocks));
210 void hz_bitstream_read_bytes(hz_bitstream_readblocks_t *blocks, void *outdata, unsigned int size)
215 *out++ = hz_bitstream_read_byte(blocks);
221 typedef struct dpvsimpledecodestream_s
223 hz_bitstream_read_t *bitstream;
224 hz_bitstream_readblocks_t *framedatablocks;
228 double info_framerate;
229 unsigned int info_frames;
231 unsigned int info_imagewidth;
232 unsigned int info_imageheight;
233 unsigned int info_imagebpp;
234 unsigned int info_imageRloss;
235 unsigned int info_imageRmask;
236 unsigned int info_imageRshift;
237 unsigned int info_imageGloss;
238 unsigned int info_imageGmask;
239 unsigned int info_imageGshift;
240 unsigned int info_imageBloss;
241 unsigned int info_imageBmask;
242 unsigned int info_imageBshift;
243 unsigned int info_imagesize;
245 // current video frame (needed because of delta compression)
247 // current video frame data (needed because of delta compression)
248 unsigned int *videopixels;
250 // wav file the sound is being read from
251 wavefile_t *wavefile;
253 dpvsimpledecodestream_t;
255 static int dpvsimpledecode_setpixelformat(dpvsimpledecodestream_t *s, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel)
257 int Rshift, Rbits, Gshift, Gbits, Bshift, Bbits;
260 s->error = DPVSIMPLEDECODEERROR_INVALIDRMASK;
265 s->error = DPVSIMPLEDECODEERROR_INVALIDGMASK;
270 s->error = DPVSIMPLEDECODEERROR_INVALIDBMASK;
273 if (Rmask & Gmask || Rmask & Bmask || Gmask & Bmask)
275 s->error = DPVSIMPLEDECODEERROR_COLORMASKSOVERLAP;
278 switch (bytesperpixel)
281 if ((Rmask | Gmask | Bmask) > 65536)
283 s->error = DPVSIMPLEDECODEERROR_COLORMASKSEXCEEDBPP;
290 s->error = DPVSIMPLEDECODEERROR_UNSUPPORTEDBPP;
294 for (Rshift = 0;!(Rmask & 1);Rshift++, Rmask >>= 1);
295 for (Gshift = 0;!(Gmask & 1);Gshift++, Gmask >>= 1);
296 for (Bshift = 0;!(Bmask & 1);Bshift++, Bmask >>= 1);
297 if (((Rmask + 1) & Rmask) != 0)
299 s->error = DPVSIMPLEDECODEERROR_INVALIDRMASK;
302 if (((Gmask + 1) & Gmask) != 0)
304 s->error = DPVSIMPLEDECODEERROR_INVALIDGMASK;
307 if (((Bmask + 1) & Bmask) != 0)
309 s->error = DPVSIMPLEDECODEERROR_INVALIDBMASK;
312 for (Rbits = 0;Rmask & 1;Rbits++, Rmask >>= 1);
313 for (Gbits = 0;Gmask & 1;Gbits++, Gmask >>= 1);
314 for (Bbits = 0;Bmask & 1;Bbits++, Bmask >>= 1);
317 Rshift += (Rbits - 8);
322 Gshift += (Gbits - 8);
327 Bshift += (Bbits - 8);
330 s->info_imagebpp = bytesperpixel;
331 s->info_imageRloss = 16 + (8 - Rbits);
332 s->info_imageGloss = 8 + (8 - Gbits);
333 s->info_imageBloss = 0 + (8 - Bbits);
334 s->info_imageRmask = (1 << Rbits) - 1;
335 s->info_imageGmask = (1 << Gbits) - 1;
336 s->info_imageBmask = (1 << Bbits) - 1;
337 s->info_imageRshift = Rshift;
338 s->info_imageGshift = Gshift;
339 s->info_imageBshift = Bshift;
340 s->info_imagesize = s->info_imagewidth * s->info_imageheight * s->info_imagebpp;
344 // opening and closing streams
346 static void StripExtension(char *in, char *out)
352 if (*c == ':' || *c == '\\' || *c == '/')
365 memcpy(out, in, dot - in);
371 void *dpvsimpledecode_open(char *filename, char **errorstring)
373 dpvsimpledecodestream_t *s;
374 char t[8], *wavename;
375 if (errorstring != NULL)
377 s = malloc(sizeof(dpvsimpledecodestream_t));
380 s->bitstream = hz_bitstream_read_open(filename);
381 if (s->bitstream != NULL)
383 // check file identification
384 s->framedatablocks = hz_bitstream_read_blocks_new();
385 if (s->framedatablocks != NULL)
387 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 8);
388 hz_bitstream_read_bytes(s->framedatablocks, t, 8);
389 if (!memcmp(t, "DPVideo", 8))
391 // check version number
392 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 2);
393 if (hz_bitstream_read_short(s->framedatablocks) == 1)
395 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 12);
396 s->info_imagewidth = hz_bitstream_read_short(s->framedatablocks);
397 s->info_imageheight = hz_bitstream_read_short(s->framedatablocks);
398 s->info_framerate = (double) hz_bitstream_read_int(s->framedatablocks) * (1.0 / 65536.0);
400 if (s->info_framerate > 0.0)
402 s->videopixels = malloc(s->info_imagewidth * s->info_imageheight * sizeof(*s->videopixels));
403 if (s->videopixels != NULL)
405 wavename = malloc(strlen(filename) + 10);
408 StripExtension(filename, wavename);
409 strcat(wavename, ".wav");
410 s->wavefile = waveopen(wavename, NULL);
414 s->videoframenum = -10000;
417 else if (errorstring != NULL)
418 *errorstring = "unable to allocate video image buffer";
420 else if (errorstring != NULL)
421 *errorstring = "error in video info chunk";
423 else if (errorstring != NULL)
424 *errorstring = "read error";
426 else if (errorstring != NULL)
427 *errorstring = "not a dpvideo file";
428 hz_bitstream_read_blocks_free(s->framedatablocks);
430 else if (errorstring != NULL)
431 *errorstring = "unable to allocate memory for reading buffer";
432 hz_bitstream_read_close(s->bitstream);
434 else if (errorstring != NULL)
435 *errorstring = "unable to open file";
438 else if (errorstring != NULL)
439 *errorstring = "unable to allocate memory for stream info structure";
444 void dpvsimpledecode_close(void *stream)
446 dpvsimpledecodestream_t *s = stream;
450 free(s->videopixels);
452 waveclose(s->wavefile);
453 if (s->framedatablocks)
454 hz_bitstream_read_blocks_free(s->framedatablocks);
456 hz_bitstream_read_close(s->bitstream);
460 // utilitarian functions
462 // returns the current error number for the stream, and resets the error
463 // number to DPVSIMPLEDECODEERROR_NONE
464 // if the supplied string pointer variable is not NULL, it will be set to the
466 int dpvsimpledecode_error(void *stream, char **errorstring)
468 dpvsimpledecodestream_t *s = stream;
476 case DPVSIMPLEDECODEERROR_NONE:
477 *errorstring = "no error";
479 case DPVSIMPLEDECODEERROR_EOF:
480 *errorstring = "end of file reached (this is not an error)";
482 case DPVSIMPLEDECODEERROR_READERROR:
483 *errorstring = "read error (corrupt or incomplete file)";
485 case DPVSIMPLEDECODEERROR_SOUNDBUFFERTOOSMALL:
486 *errorstring = "sound buffer is too small for decoding frame (please allocate it as large as dpvsimpledecode_getneededsoundbufferlength suggests)";
488 case DPVSIMPLEDECODEERROR_INVALIDRMASK:
489 *errorstring = "invalid red bits mask";
491 case DPVSIMPLEDECODEERROR_INVALIDGMASK:
492 *errorstring = "invalid green bits mask";
494 case DPVSIMPLEDECODEERROR_INVALIDBMASK:
495 *errorstring = "invalid blue bits mask";
497 case DPVSIMPLEDECODEERROR_COLORMASKSOVERLAP:
498 *errorstring = "color bit masks overlap";
500 case DPVSIMPLEDECODEERROR_COLORMASKSEXCEEDBPP:
501 *errorstring = "color masks too big for specified bytes per pixel";
503 case DPVSIMPLEDECODEERROR_UNSUPPORTEDBPP:
504 *errorstring = "unsupported bytes per pixel (must be 2 for 16bit, or 4 for 32bit)";
507 *errorstring = "unknown error";
514 // returns the width of the image data
515 unsigned int dpvsimpledecode_getwidth(void *stream)
517 dpvsimpledecodestream_t *s = stream;
518 return s->info_imagewidth;
521 // returns the height of the image data
522 unsigned int dpvsimpledecode_getheight(void *stream)
524 dpvsimpledecodestream_t *s = stream;
525 return s->info_imageheight;
528 // returns the sound sample rate of the stream
529 unsigned int dpvsimpledecode_getsoundrate(void *stream)
531 dpvsimpledecodestream_t *s = stream;
533 return s->wavefile->info_rate;
538 // returns the framerate of the stream
539 double dpvsimpledecode_getframerate(void *stream)
541 dpvsimpledecodestream_t *s = stream;
542 return s->info_framerate;
549 static int dpvsimpledecode_convertpixels(dpvsimpledecodestream_t *s, void *imagedata, int imagebytesperrow)
551 unsigned int a, x, y, width, height;
552 unsigned int Rloss, Rmask, Rshift, Gloss, Gmask, Gshift, Bloss, Bmask, Bshift;
555 width = s->info_imagewidth;
556 height = s->info_imageheight;
558 Rloss = s->info_imageRloss;
559 Rmask = s->info_imageRmask;
560 Rshift = s->info_imageRshift;
561 Gloss = s->info_imageGloss;
562 Gmask = s->info_imageGmask;
563 Gshift = s->info_imageGshift;
564 Bloss = s->info_imageBloss;
565 Bmask = s->info_imageBmask;
566 Bshift = s->info_imageBshift;
569 if (s->info_imagebpp == 4)
571 unsigned int *outrow;
572 for (y = 0;y < height;y++)
574 outrow = (void *)((unsigned char *)imagedata + y * imagebytesperrow);
575 for (x = 0;x < width;x++)
578 outrow[x] = (((a >> Rloss) & Rmask) << Rshift) | (((a >> Gloss) & Gmask) << Gshift) | (((a >> Bloss) & Bmask) << Bshift);
584 unsigned short *outrow;
585 for (y = 0;y < height;y++)
587 outrow = (void *)((unsigned char *)imagedata + y * imagebytesperrow);
588 if (Rloss == 19 && Gloss == 10 && Bloss == 3 && Rshift == 11 && Gshift == 5 && Bshift == 0)
591 for (x = 0;x < width;x++)
594 outrow[x] = ((a >> 8) & 0xF800) | ((a >> 5) & 0x07E0) | ((a >> 3) & 0x001F);
599 for (x = 0;x < width;x++)
602 outrow[x] = (((a >> Rloss) & Rmask) << Rshift) | (((a >> Gloss) & Gmask) << Gshift) | (((a >> Bloss) & Bmask) << Bshift);
610 static int dpvsimpledecode_decompressimage(dpvsimpledecodestream_t *s)
612 int i, a, b, colors, g, x1, y1, bw, bh, width, height, palettebits;
613 unsigned int palette[256], *outrow, *out;
615 width = s->info_imagewidth;
616 height = s->info_imageheight;
617 for (y1 = 0;y1 < height;y1 += g)
619 outrow = s->videopixels + y1 * width;
621 if (y1 + bh > height)
623 for (x1 = 0;x1 < width;x1 += g)
629 if (hz_bitstream_read_bit(s->framedatablocks))
632 palettebits = hz_bitstream_read_bits(s->framedatablocks, 3);
633 colors = 1 << palettebits;
634 for (i = 0;i < colors;i++)
635 palette[i] = hz_bitstream_read_bits(s->framedatablocks, 24);
638 for (b = 0;b < bh;b++, out += width)
639 for (a = 0;a < bw;a++)
640 out[a] = palette[hz_bitstream_read_bits(s->framedatablocks, palettebits)];
644 for (b = 0;b < bh;b++, out += width)
645 for (a = 0;a < bw;a++)
654 // decodes a video frame to the supplied output pixels
655 int dpvsimpledecode_video(void *stream, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow)
657 dpvsimpledecodestream_t *s = stream;
658 unsigned int framedatasize;
660 s->error = DPVSIMPLEDECODEERROR_NONE;
661 if (dpvsimpledecode_setpixelformat(s, Rmask, Gmask, Bmask, bytesperpixel))
664 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 8);
665 hz_bitstream_read_bytes(s->framedatablocks, t, 4);
666 if (memcmp(t, "VID0", 4))
669 return (s->error = DPVSIMPLEDECODEERROR_EOF);
671 return (s->error = DPVSIMPLEDECODEERROR_READERROR);
673 framedatasize = hz_bitstream_read_int(s->framedatablocks);
674 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, framedatasize);
675 if (dpvsimpledecode_decompressimage(s))
678 dpvsimpledecode_convertpixels(s, imagedata, imagebytesperrow);
682 // (note: sound is 16bit stereo native-endian, left channel first)
683 int dpvsimpledecode_audio(void *stream, short *soundbuffer, int requestedlength)
686 dpvsimpledecodestream_t *s = stream;
687 s->error = DPVSIMPLEDECODEERROR_NONE;
691 if (s->wavefile && requestedlength)
692 samples = waveread16stereo(s->wavefile, soundbuffer, requestedlength);
693 if (samples < requestedlength)
694 memset(soundbuffer + samples * 2, 0, (requestedlength - samples) * sizeof(short[2]));