make infobar height configurable
[divverent/darkplaces.git] / dpvsimpledecode.c
1 #include "quakedef.h"
2 #include "dpvsimpledecode.h"
3
4 #define HZREADERROR_OK 0
5 #define HZREADERROR_EOF 1
6 #define HZREADERROR_MALLOCFAILED 2
7
8 //#define HZREADBLOCKSIZE 16000
9 #define HZREADBLOCKSIZE 1048576
10
11 typedef struct hz_bitstream_read_s
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 hz_bitstream_readblocks_s
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_OpenVirtualFile(filename, false)))
41         {
42                 stream = (hz_bitstream_read_t *)Z_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                 Z_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 = (hz_bitstream_readblocks_t *)Z_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                 Z_Free(b);
79         }
80         Z_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 = (hz_bitstream_readblock_t *)Z_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) != (fs_offset_t)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 = (unsigned char *)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         }
282         for (Rshift = 0;!(Rmask & 1);Rshift++, Rmask >>= 1);
283         for (Gshift = 0;!(Gmask & 1);Gshift++, Gmask >>= 1);
284         for (Bshift = 0;!(Bmask & 1);Bshift++, Bmask >>= 1);
285         if (((Rmask + 1) & Rmask) != 0)
286         {
287                 s->error = DPVSIMPLEDECODEERROR_INVALIDRMASK;
288                 return s->error;
289         }
290         if (((Gmask + 1) & Gmask) != 0)
291         {
292                 s->error = DPVSIMPLEDECODEERROR_INVALIDGMASK;
293                 return s->error;
294         }
295         if (((Bmask + 1) & Bmask) != 0)
296         {
297                 s->error = DPVSIMPLEDECODEERROR_INVALIDBMASK;
298                 return s->error;
299         }
300         for (Rbits = 0;Rmask & 1;Rbits++, Rmask >>= 1);
301         for (Gbits = 0;Gmask & 1;Gbits++, Gmask >>= 1);
302         for (Bbits = 0;Bmask & 1;Bbits++, Bmask >>= 1);
303         if (Rbits > 8)
304         {
305                 Rshift += (Rbits - 8);
306                 Rbits = 8;
307         }
308         if (Gbits > 8)
309         {
310                 Gshift += (Gbits - 8);
311                 Gbits = 8;
312         }
313         if (Bbits > 8)
314         {
315                 Bshift += (Bbits - 8);
316                 Bbits = 8;
317         }
318         s->info_imagebpp = bytesperpixel;
319         s->info_imageRloss = 16 + (8 - Rbits);
320         s->info_imageGloss =  8 + (8 - Gbits);
321         s->info_imageBloss =  0 + (8 - Bbits);
322         s->info_imageRmask = (1 << Rbits) - 1;
323         s->info_imageGmask = (1 << Gbits) - 1;
324         s->info_imageBmask = (1 << Bbits) - 1;
325         s->info_imageRshift = Rshift;
326         s->info_imageGshift = Gshift;
327         s->info_imageBshift = Bshift;
328         s->info_imagesize = s->info_imagewidth * s->info_imageheight * s->info_imagebpp;
329         return s->error;
330 }
331
332 // opening and closing streams
333
334 // opens a stream
335 void *dpvsimpledecode_open(clvideo_t *video, char *filename, const char **errorstring)
336 {
337         dpvsimpledecodestream_t *s;
338         char t[8], *wavename;
339         if (errorstring != NULL)
340                 *errorstring = NULL;
341         s = (dpvsimpledecodestream_t *)Z_Malloc(sizeof(dpvsimpledecodestream_t));
342         if (s != NULL)
343         {
344                 s->bitstream = hz_bitstream_read_open(filename);
345                 if (s->bitstream != NULL)
346                 {
347                         // check file identification
348                         s->framedatablocks = hz_bitstream_read_blocks_new();
349                         if (s->framedatablocks != NULL)
350                         {
351                                 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 8);
352                                 hz_bitstream_read_bytes(s->framedatablocks, t, 8);
353                                 if (!memcmp(t, "DPVideo", 8))
354                                 {
355                                         // check version number
356                                         hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 2);
357                                         if (hz_bitstream_read_short(s->framedatablocks) == 1)
358                                         {
359                                                 hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 12);
360                                                 s->info_imagewidth = hz_bitstream_read_short(s->framedatablocks);
361                                                 s->info_imageheight = hz_bitstream_read_short(s->framedatablocks);
362                                                 s->info_framerate = (double) hz_bitstream_read_int(s->framedatablocks) * (1.0 / 65536.0);
363
364                                                 if (s->info_framerate > 0.0)
365                                                 {
366                                                         s->videopixels = (unsigned int *)Z_Malloc(s->info_imagewidth * s->info_imageheight * sizeof(*s->videopixels));
367                                                         if (s->videopixels != NULL)
368                                                         {
369                                                                 size_t namelen;
370
371                                                                 namelen = strlen(filename) + 10;
372                                                                 wavename = (char *)Z_Malloc(namelen);
373                                                                 if (wavename)
374                                                                 {
375                                                                         sfx_t* sfx;
376
377                                                                         FS_StripExtension(filename, wavename, namelen);
378                                                                         strlcat(wavename, ".wav", namelen);
379                                                                         sfx = S_PrecacheSound (wavename, false, false);
380                                                                         if (sfx != NULL)
381                                                                                 s->sndchan = S_StartSound (-1, 0, sfx, vec3_origin, 1.0f, 0);
382                                                                         else
383                                                                                 s->sndchan = -1;
384                                                                         Z_Free(wavename);
385                                                                 }
386                                                                 // all is well...
387                                                                 // set the module functions
388                                                                 s->videoframenum = -10000;
389                                                                 video->close = dpvsimpledecode_close;
390                                                                 video->getwidth = dpvsimpledecode_getwidth;
391                                                                 video->getheight = dpvsimpledecode_getheight;
392                                                                 video->getframerate = dpvsimpledecode_getframerate;
393                                                                 video->decodeframe = dpvsimpledecode_video;
394
395                                                                 return s;
396                                                         }
397                                                         else if (errorstring != NULL)
398                                                                 *errorstring = "unable to allocate video image buffer";
399                                                 }
400                                                 else if (errorstring != NULL)
401                                                         *errorstring = "error in video info chunk";
402                                         }
403                                         else if (errorstring != NULL)
404                                                 *errorstring = "read error";
405                                 }
406                                 else if (errorstring != NULL)
407                                         *errorstring = "not a dpvideo file";
408                                 hz_bitstream_read_blocks_free(s->framedatablocks);
409                         }
410                         else if (errorstring != NULL)
411                                 *errorstring = "unable to allocate memory for reading buffer";
412                         hz_bitstream_read_close(s->bitstream);
413                 }
414                 else if (errorstring != NULL)
415                         *errorstring = "unable to open file";
416                 Z_Free(s);
417         }
418         else if (errorstring != NULL)
419                 *errorstring = "unable to allocate memory for stream info structure";
420         return NULL;
421 }
422
423 // closes a stream
424 void dpvsimpledecode_close(void *stream)
425 {
426         dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
427         if (s == NULL)
428                 return;
429         if (s->videopixels)
430                 Z_Free(s->videopixels);
431         if (s->sndchan != -1)
432                 S_StopChannel (s->sndchan, true);
433         if (s->framedatablocks)
434                 hz_bitstream_read_blocks_free(s->framedatablocks);
435         if (s->bitstream)
436                 hz_bitstream_read_close(s->bitstream);
437         Z_Free(s);
438 }
439
440 // utilitarian functions
441
442 // returns the current error number for the stream, and resets the error
443 // number to DPVSIMPLEDECODEERROR_NONE
444 // if the supplied string pointer variable is not NULL, it will be set to the
445 // error message
446 int dpvsimpledecode_error(void *stream, const char **errorstring)
447 {
448         dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
449         int e;
450         e = s->error;
451         s->error = 0;
452         if (errorstring)
453         {
454                 switch (e)
455                 {
456                         case DPVSIMPLEDECODEERROR_NONE:
457                                 *errorstring = "no error";
458                                 break;
459                         case DPVSIMPLEDECODEERROR_EOF:
460                                 *errorstring = "end of file reached (this is not an error)";
461                                 break;
462                         case DPVSIMPLEDECODEERROR_READERROR:
463                                 *errorstring = "read error (corrupt or incomplete file)";
464                                 break;
465                         case DPVSIMPLEDECODEERROR_SOUNDBUFFERTOOSMALL:
466                                 *errorstring = "sound buffer is too small for decoding frame (please allocate it as large as dpvsimpledecode_getneededsoundbufferlength suggests)";
467                                 break;
468                         case DPVSIMPLEDECODEERROR_INVALIDRMASK:
469                                 *errorstring = "invalid red bits mask";
470                                 break;
471                         case DPVSIMPLEDECODEERROR_INVALIDGMASK:
472                                 *errorstring = "invalid green bits mask";
473                                 break;
474                         case DPVSIMPLEDECODEERROR_INVALIDBMASK:
475                                 *errorstring = "invalid blue bits mask";
476                                 break;
477                         case DPVSIMPLEDECODEERROR_COLORMASKSOVERLAP:
478                                 *errorstring = "color bit masks overlap";
479                                 break;
480                         case DPVSIMPLEDECODEERROR_COLORMASKSEXCEEDBPP:
481                                 *errorstring = "color masks too big for specified bytes per pixel";
482                                 break;
483                         case DPVSIMPLEDECODEERROR_UNSUPPORTEDBPP:
484                                 *errorstring = "unsupported bytes per pixel (must be 2 for 16bit, or 4 for 32bit)";
485                                 break;
486                         default:
487                                 *errorstring = "unknown error";
488                                 break;
489                 }
490         }
491         return e;
492 }
493
494 // returns the width of the image data
495 unsigned int dpvsimpledecode_getwidth(void *stream)
496 {
497         dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
498         return s->info_imagewidth;
499 }
500
501 // returns the height of the image data
502 unsigned int dpvsimpledecode_getheight(void *stream)
503 {
504         dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
505         return s->info_imageheight;
506 }
507
508 // returns the framerate of the stream
509 double dpvsimpledecode_getframerate(void *stream)
510 {
511         dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
512         return s->info_framerate;
513 }
514
515 static int dpvsimpledecode_convertpixels(dpvsimpledecodestream_t *s, void *imagedata, int imagebytesperrow)
516 {
517         unsigned int a, x, y, width, height;
518         unsigned int Rloss, Rmask, Rshift, Gloss, Gmask, Gshift, Bloss, Bmask, Bshift;
519         unsigned int *in;
520
521         width = s->info_imagewidth;
522         height = s->info_imageheight;
523
524         Rloss = s->info_imageRloss;
525         Rmask = s->info_imageRmask;
526         Rshift = s->info_imageRshift;
527         Gloss = s->info_imageGloss;
528         Gmask = s->info_imageGmask;
529         Gshift = s->info_imageGshift;
530         Bloss = s->info_imageBloss;
531         Bmask = s->info_imageBmask;
532         Bshift = s->info_imageBshift;
533
534         in = s->videopixels;
535         if (s->info_imagebpp == 4)
536         {
537                 unsigned int *outrow;
538                 for (y = 0;y < height;y++)
539                 {
540                         outrow = (unsigned int *)((unsigned char *)imagedata + y * imagebytesperrow);
541                         for (x = 0;x < width;x++)
542                         {
543                                 a = *in++;
544                                 outrow[x] = (((a >> Rloss) & Rmask) << Rshift) | (((a >> Gloss) & Gmask) << Gshift) | (((a >> Bloss) & Bmask) << Bshift);
545                         }
546                 }
547         }
548         else
549         {
550                 unsigned short *outrow;
551                 for (y = 0;y < height;y++)
552                 {
553                         outrow = (unsigned short *)((unsigned char *)imagedata + y * imagebytesperrow);
554                         if (Rloss == 19 && Gloss == 10 && Bloss == 3 && Rshift == 11 && Gshift == 5 && Bshift == 0)
555                         {
556                                 // optimized
557                                 for (x = 0;x < width;x++)
558                                 {
559                                         a = *in++;
560                                         outrow[x] = ((a >> 8) & 0xF800) | ((a >> 5) & 0x07E0) | ((a >> 3) & 0x001F);
561                                 }
562                         }
563                         else
564                         {
565                                 for (x = 0;x < width;x++)
566                                 {
567                                         a = *in++;
568                                         outrow[x] = (((a >> Rloss) & Rmask) << Rshift) | (((a >> Gloss) & Gmask) << Gshift) | (((a >> Bloss) & Bmask) << Bshift);
569                                 }
570                         }
571                 }
572         }
573         return s->error;
574 }
575
576 static int dpvsimpledecode_decompressimage(dpvsimpledecodestream_t *s)
577 {
578         int i, a, b, colors, g, x1, y1, bw, bh, width, height, palettebits;
579         unsigned int palette[256], *outrow, *out;
580         g = BLOCKSIZE;
581         width = s->info_imagewidth;
582         height = s->info_imageheight;
583         for (y1 = 0;y1 < height;y1 += g)
584         {
585                 outrow = s->videopixels + y1 * width;
586                 bh = g;
587                 if (y1 + bh > height)
588                         bh = height - y1;
589                 for (x1 = 0;x1 < width;x1 += g)
590                 {
591                         out = outrow + x1;
592                         bw = g;
593                         if (x1 + bw > width)
594                                 bw = width - x1;
595                         if (hz_bitstream_read_bit(s->framedatablocks))
596                         {
597                                 // updated block
598                                 palettebits = hz_bitstream_read_bits(s->framedatablocks, 3);
599                                 colors = 1 << palettebits;
600                                 for (i = 0;i < colors;i++)
601                                         palette[i] = hz_bitstream_read_bits(s->framedatablocks, 24);
602                                 if (palettebits)
603                                 {
604                                         for (b = 0;b < bh;b++, out += width)
605                                                 for (a = 0;a < bw;a++)
606                                                         out[a] = palette[hz_bitstream_read_bits(s->framedatablocks, palettebits)];
607                                 }
608                                 else
609                                 {
610                                         for (b = 0;b < bh;b++, out += width)
611                                                 for (a = 0;a < bw;a++)
612                                                         out[a] = palette[0];
613                                 }
614                         }
615                 }
616         }
617         return s->error;
618 }
619
620 // decodes a video frame to the supplied output pixels
621 int dpvsimpledecode_video(void *stream, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow)
622 {
623         dpvsimpledecodestream_t *s = (dpvsimpledecodestream_t *)stream;
624         unsigned int framedatasize;
625         char t[4];
626         s->error = DPVSIMPLEDECODEERROR_NONE;
627         if (dpvsimpledecode_setpixelformat(s, Rmask, Gmask, Bmask, bytesperpixel))
628                 return s->error;
629
630         hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, 8);
631         hz_bitstream_read_bytes(s->framedatablocks, t, 4);
632         if (memcmp(t, "VID0", 4))
633         {
634                 if (t[0] == 0)
635                         return (s->error = DPVSIMPLEDECODEERROR_EOF);
636                 else
637                         return (s->error = DPVSIMPLEDECODEERROR_READERROR);
638         }
639         framedatasize = hz_bitstream_read_int(s->framedatablocks);
640         hz_bitstream_read_blocks_read(s->framedatablocks, s->bitstream, framedatasize);
641         if (dpvsimpledecode_decompressimage(s))
642                 return s->error;
643
644         dpvsimpledecode_convertpixels(s, imagedata, imagebytesperrow);
645         return s->error;
646 }