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